source: branches/reyalp-flt/core/gui_mbox.c @ 1513

Revision 1513, 7.1 KB checked in by philmoz, 2 years ago (diff)

Cleanup of code for handling CHDK 'gui' mode.

  • Property svn:eol-style set to native
Line 
1#include "stdlib.h"
2#include "keyboard.h"
3#include "platform.h"
4#include "core.h"
5#include "lang.h"
6#include "gui.h"
7#include "gui_draw.h"
8#include "gui_lang.h"
9#include "gui_mbox.h"
10
11
12//-------------------------------------------------------------------
13void gui_mbox_draw(int enforce_redraw);
14void gui_mbox_kbd_process();
15
16static gui_handler mboxGuiHandler =    { GUI_MODE_MBOX,    gui_mbox_draw,  gui_mbox_kbd_process,   0,   GUI_MODE_FLAG_NORESTORE_ON_SWITCH,    GUI_MODE_MAGICNUM };
17
18static gui_handler      *gui_mbox_mode_old;
19static const char*      mbox_title;
20static const char*      mbox_msg;
21static char             mbox_to_draw;
22static unsigned int     mbox_flags;
23#define MAX_LINES       8
24#define MAX_WIDTH       35
25#define SPACING_TITLE   4
26#define SPACING_BTN     4
27static struct {
28        unsigned int    flag;
29        int             text;
30} buttons[] = {
31        { MBOX_BTN_OK,          LANG_MBOX_BTN_OK    },
32        { MBOX_BTN_YES,         LANG_MBOX_BTN_YES   },
33        { MBOX_BTN_NO,          LANG_MBOX_BTN_NO    },
34        { MBOX_BTN_CANCEL,      LANG_MBOX_BTN_CANCEL}
35};
36#define BUTTON_SIZE     6
37#define BUTTONSNUM      (sizeof(buttons)/sizeof(buttons[0]))
38#define MAX_BUTTONS     3
39static int      mbox_buttons[MAX_BUTTONS], mbox_buttons_num, mbox_button_active;
40static coord    mbox_buttons_x, mbox_buttons_y;
41#define BUTTON_SEP      18
42static void (*mbox_on_select)(unsigned int btn);
43
44//-------------------------------------------------------------------
45void gui_mbox_init(int title, int msg, const unsigned int flags, void (*on_select)(unsigned int btn)) {
46    int i;
47
48    mbox_buttons_num = 0;
49    for (i=0; i<BUTTONSNUM && mbox_buttons_num<MAX_BUTTONS; ++i) {
50        if (flags & MBOX_BTN_MASK & buttons[i].flag)
51            mbox_buttons[mbox_buttons_num++] = i;
52    }
53    if (mbox_buttons_num == 0)
54        mbox_buttons[mbox_buttons_num++] = 0; // Add button "Ok" if there is no buttons
55
56    switch (flags & MBOX_DEF_MASK) {
57        case MBOX_DEF_BTN1: mbox_button_active = 0; break;
58        case MBOX_DEF_BTN2: mbox_button_active = 1; break;
59        case MBOX_DEF_BTN3: mbox_button_active = 2; break;
60        default: mbox_button_active = 0; break;
61    }
62
63    mbox_title = lang_str(title);
64    mbox_msg = lang_str(msg);
65    mbox_to_draw = 1;
66    mbox_flags = flags;
67    mbox_on_select = on_select;
68    gui_mbox_mode_old = gui_set_mode(&mboxGuiHandler);
69}
70
71//-------------------------------------------------------------------
72unsigned int gui_mbox_result() {
73    return buttons[mbox_buttons[mbox_button_active]].flag;
74}
75
76//-------------------------------------------------------------------
77static void gui_mbox_draw_buttons() {
78    int i;
79    coord x = mbox_buttons_x;
80    color cl;
81
82    for (i=0; i<mbox_buttons_num; ++i) {
83        cl = MAKE_COLOR((mbox_button_active==i)?COLOR_RED:COLOR_BLACK, COLOR_WHITE);
84        draw_rect(x-1, mbox_buttons_y-1, x+BUTTON_SIZE*FONT_WIDTH+3, mbox_buttons_y+FONT_HEIGHT+3, COLOR_BLACK); //shadow
85        draw_filled_rect(x-2, mbox_buttons_y-2, x+BUTTON_SIZE*FONT_WIDTH+2, mbox_buttons_y+FONT_HEIGHT+2, cl);
86        draw_string(x+(((BUTTON_SIZE-strlen(lang_str(buttons[mbox_buttons[i]].text)))*FONT_WIDTH)>>1), mbox_buttons_y, lang_str(buttons[mbox_buttons[i]].text), cl);
87        x+=BUTTON_SIZE*FONT_WIDTH+BUTTON_SEP;
88    }
89}
90
91//-------------------------------------------------------------------
92void gui_mbox_draw(int enforce_redraw) {
93    if (mbox_to_draw) {
94        char c[MAX_LINES][MAX_WIDTH+1];
95        const char *p=mbox_msg;
96        coord x=0, y=0, d;
97        unsigned int w, h=0, l=0, bw=(mbox_buttons_num*BUTTON_SIZE*FONT_WIDTH+(mbox_buttons_num-1)*BUTTON_SEP);
98        color cl_t =((mode_get()&MODE_MASK) == MODE_PLAY)?MAKE_COLOR(0xAA, COLOR_WHITE):MAKE_COLOR(0xDF, COLOR_WHITE);
99
100        w =strlen(mbox_title);
101        if (w > MAX_WIDTH) w = MAX_WIDTH;
102        while (*p) {
103            if (*p == '\n') {
104                c[h++][l] = 0;
105                l=0;
106                if (h == MAX_LINES) break;
107            } else {
108                if (l < MAX_WIDTH) {
109                    c[h][l++]=*p;
110                    if (l > w) w = l;
111                }
112            }
113            ++p;
114        }
115        w+=2;
116        if (h<MAX_LINES)
117            c[h++][l] = 0;
118        if (bw+BUTTON_SEP>w*FONT_WIDTH)
119            w=(bw+BUTTON_SEP)/FONT_WIDTH+1;
120   
121        x = (vid_get_bitmap_screen_width() - w * FONT_WIDTH) >> 1;
122        y = (vid_get_bitmap_screen_height() - (h+2) * FONT_HEIGHT) >> 1;
123        draw_rect_shadow(x-3, y-3, x+w*FONT_WIDTH+5, y+(h+2)*FONT_HEIGHT+SPACING_BTN+2+SPACING_TITLE+8, COLOR_BLACK, 3); //shadow
124        draw_filled_rect_thick(x-4, y-4, x+w*FONT_WIDTH+4, y+(h+2)*FONT_HEIGHT+SPACING_BTN+2+SPACING_TITLE+7, MAKE_COLOR(COLOR_GREY, COLOR_WHITE), 3); // main box
125        draw_filled_rect(x-2, y-2, x+w*FONT_WIDTH+2, y+FONT_HEIGHT+2, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); //title
126   
127        l = strlen(mbox_title);
128        draw_string(x+((w-l)>>1)*FONT_WIDTH, y, mbox_title, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); //title text
129        y+=FONT_HEIGHT+2+SPACING_TITLE;
130   
131        mbox_buttons_x = x+((w*FONT_WIDTH-bw)>>1);
132        mbox_buttons_y = y+h*FONT_HEIGHT+SPACING_BTN;
133
134        while (h) {
135            l = strlen(c[--h]);
136            switch (mbox_flags & MBOX_TEXT_MASK) {
137                case MBOX_TEXT_LEFT:    d = FONT_WIDTH; break;
138                case MBOX_TEXT_CENTER:  d = ((w-l)>>1)*FONT_WIDTH; break;
139                case MBOX_TEXT_RIGHT:   d = (w-l-1)*FONT_WIDTH; break;
140                default:                d = FONT_WIDTH; break;
141            }
142            draw_string(x+d, y+h*FONT_HEIGHT, c[h], MAKE_COLOR(COLOR_GREY, COLOR_WHITE)); // text
143        }
144
145        gui_mbox_draw_buttons();
146
147        mbox_to_draw = 0;
148    }
149}
150
151//-------------------------------------------------------------------
152void gui_mbox_kbd_process() {
153    switch (kbd_get_clicked_key() | get_jogdial_direction()) {
154    case JOGDIAL_LEFT:
155    case KEY_LEFT:
156        if (mbox_button_active > 0) --mbox_button_active;
157        else mbox_button_active = mbox_buttons_num-1;
158        gui_mbox_draw_buttons();
159        break;
160    case JOGDIAL_RIGHT:
161    case KEY_RIGHT:
162        if (mbox_button_active < mbox_buttons_num-1) ++mbox_button_active;
163        else mbox_button_active = 0;
164        gui_mbox_draw_buttons();
165        break;
166    case KEY_SET:
167        kbd_reset_autoclicked_key();
168        gui_set_mode(gui_mbox_mode_old);
169        if (mbox_flags & MBOX_FUNC_RESTORE)
170            draw_restore();
171        if (mbox_on_select)
172            mbox_on_select(buttons[mbox_buttons[mbox_button_active]].flag);
173        break;
174    }
175}
176
177//-------------------------------------------------------------------
178
179void gui_browser_progress_show(const char* msg, const unsigned int perc) {
180    coord x=60, y=100;
181    unsigned int w=240, h=40, len;
182
183    draw_rect_shadow(x+1, y+1, x+w+1, y+h+1, COLOR_BLACK, 3); //shadow
184    draw_filled_rect(x, y, x+w, y+h, MAKE_COLOR(COLOR_GREY, COLOR_WHITE)); // main box
185    len = strlen(msg);
186    draw_string(x+((w-len*FONT_WIDTH)>>1), y+2, msg, MAKE_COLOR(COLOR_GREY, COLOR_WHITE)); //title text
187    draw_filled_rect(x+10, y+4+FONT_HEIGHT, x+w-10, y+h-10, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); // progress rect
188    draw_filled_rect(x+11, y+5+FONT_HEIGHT, x+11+(w-22)*perc/100, y+h-11, MAKE_COLOR(COLOR_RED, COLOR_RED)); // progress bar
189}
Note: See TracBrowser for help on using the repository browser.