root/core/gui_mbox.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. gui_mbox_init
  2. gui_mbox_draw
  3. gui_mbox_kbd_process
  4. gui_mbox_touch_handler
  5. gui_browser_progress_show

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "lang.h"
   4 #include "modes.h"
   5 #include "keyboard.h"
   6 #include "gui.h"
   7 #include "gui_draw.h"
   8 #include "gui_lang.h"
   9 #include "gui_mbox.h"
  10 
  11 //-------------------------------------------------------------------
  12 void gui_mbox_draw();
  13 int gui_mbox_kbd_process();
  14 int gui_mbox_touch_handler(int,int);
  15 
  16 static gui_handler mboxGuiHandler = { GUI_MODE_MBOX, gui_mbox_draw, gui_mbox_kbd_process, 0, gui_mbox_touch_handler, GUI_MODE_FLAG_NORESTORE_ON_SWITCH };
  17 
  18 static gui_handler      *gui_mbox_mode_old;
  19 static const char*      mbox_title;
  20 static const char*      mbox_msg;
  21 static char             mbox_to_draw;
  22 static unsigned int     mbox_flags;
  23 #define MAX_LINES       8
  24 #define MAX_WIDTH       35
  25 #define SPACING_TITLE   4
  26 #define SPACING_BTN     4
  27 static 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
  39 static int      mbox_buttons[MAX_BUTTONS], mbox_buttons_num, mbox_button_active;
  40 static coord    mbox_buttons_x, mbox_buttons_y;
  41 #define BUTTON_SEP      18
  42 static void (*mbox_on_select)(unsigned int btn);
  43 
  44 //-------------------------------------------------------------------
  45 void gui_mbox_init(int title, int msg, const unsigned int flags, void (*on_select)(unsigned int btn))
  46 {
  47     int i;
  48 
  49     mbox_buttons_num = 0;
  50     for (i=0; i<BUTTONSNUM && mbox_buttons_num<MAX_BUTTONS; ++i)
  51     {
  52         if (flags & MBOX_BTN_MASK & buttons[i].flag)
  53             mbox_buttons[mbox_buttons_num++] = i;
  54     }
  55     if (mbox_buttons_num == 0)
  56         mbox_buttons[mbox_buttons_num++] = 0; // Add button "Ok" if there is no buttons
  57 
  58     switch (flags & MBOX_DEF_MASK)
  59     {
  60         case MBOX_DEF_BTN2:
  61             mbox_button_active = 1;
  62             break;
  63         case MBOX_DEF_BTN3:
  64             mbox_button_active = 2;
  65             break;
  66         case MBOX_DEF_BTN1:
  67         default:
  68             mbox_button_active = 0;
  69             break;
  70     }
  71 
  72     mbox_title = lang_str(title);
  73     mbox_msg = lang_str(msg);
  74     mbox_to_draw = 3;
  75     mbox_flags = flags;
  76     mbox_on_select = on_select;
  77 
  78     gui_mbox_mode_old = gui_set_mode(&mboxGuiHandler);
  79 }
  80 
  81 //-------------------------------------------------------------------
  82 void gui_mbox_draw()
  83 {
  84     if (mbox_to_draw & 1)
  85     {
  86         int bw=(mbox_buttons_num*BUTTON_SIZE*FONT_WIDTH+(mbox_buttons_num-1)*BUTTON_SEP);
  87 
  88         int h = MAX_LINES;
  89         int w = text_dimensions(mbox_msg, strlen(mbox_title), MAX_WIDTH, &h) + 2;
  90 
  91         if (bw+BUTTON_SEP > w*FONT_WIDTH)
  92             w = (bw+BUTTON_SEP)/FONT_WIDTH+1;
  93     
  94         coord x = (camera_screen.width - w * FONT_WIDTH) >> 1;
  95         coord y = (camera_screen.height - (h+2) * FONT_HEIGHT) >> 1;
  96         draw_rectangle(x-4, y-4, x+w*FONT_WIDTH+4, y+(h+2)*FONT_HEIGHT+SPACING_BTN+2+SPACING_TITLE+7,
  97                        MAKE_COLOR(COLOR_GREY, COLOR_WHITE), RECT_BORDER3|DRAW_FILLED|RECT_SHADOW3); // main box
  98         draw_rectangle(x-2, y-2, x+w*FONT_WIDTH+2, y+FONT_HEIGHT+2, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE), RECT_BORDER1|DRAW_FILLED); //title
  99     
 100         draw_string_justified(x, y, mbox_title, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE), 0, w*FONT_WIDTH, TEXT_CENTER); //title text
 101         y += FONT_HEIGHT+2+SPACING_TITLE;
 102 
 103         int justification = TEXT_LEFT;
 104         switch (mbox_flags & MBOX_TEXT_MASK)
 105         {
 106             case MBOX_TEXT_CENTER:  justification = TEXT_CENTER; break;
 107             case MBOX_TEXT_RIGHT:   justification = TEXT_RIGHT; break;
 108         }
 109         draw_text_justified(x+FONT_WIDTH, y, mbox_msg, MAKE_COLOR(COLOR_GREY, COLOR_WHITE), w-1, MAX_LINES, justification); // text
 110 
 111         mbox_buttons_x = x+((w*FONT_WIDTH-bw)>>1);
 112         mbox_buttons_y = y+h*FONT_HEIGHT+SPACING_BTN;
 113     }
 114 
 115     if (mbox_to_draw & 2)
 116     {
 117         int i;
 118         for (i=0; i<mbox_buttons_num; ++i)
 119         {
 120             draw_button(mbox_buttons_x + i * (BUTTON_SIZE*FONT_WIDTH+BUTTON_SEP), mbox_buttons_y, BUTTON_SIZE, buttons[mbox_buttons[i]].text, (mbox_button_active == i));
 121         }
 122     }
 123 
 124     mbox_to_draw = 0;
 125 }
 126 
 127 //-------------------------------------------------------------------
 128 int gui_mbox_kbd_process()
 129 {
 130     switch (kbd_get_clicked_key() | get_jogdial_direction())
 131     {
 132     case JOGDIAL_LEFT:
 133     case KEY_LEFT:
 134         if (--mbox_button_active < 0) mbox_button_active = mbox_buttons_num - 1;
 135         mbox_to_draw = 2;
 136         break;
 137     case JOGDIAL_RIGHT:
 138     case KEY_RIGHT:
 139         if (++mbox_button_active >= mbox_buttons_num) mbox_button_active = 0;
 140         mbox_to_draw = 2;
 141         break;
 142     case KEY_SET:
 143         gui_set_mode(gui_mbox_mode_old);
 144         if (mbox_flags & MBOX_FUNC_RESTORE)
 145             gui_set_need_restore();
 146         if (mbox_on_select) 
 147             mbox_on_select(buttons[mbox_buttons[mbox_button_active]].flag);
 148         break;
 149     }
 150     return 0;
 151 }
 152 
 153 int gui_mbox_touch_handler(int sx, int sy)
 154 {
 155     int i;
 156     coord x = mbox_buttons_x;
 157 
 158     if ((sy >= mbox_buttons_y-2) && (sy <= mbox_buttons_y+FONT_HEIGHT+2))
 159     {
 160         for (i=0; i<mbox_buttons_num; ++i)
 161         {
 162             if ((sx >= x) && (sx <= x+BUTTON_SIZE*FONT_WIDTH+3))
 163             {
 164                 if (mbox_button_active != i)
 165                 {
 166                     mbox_to_draw = 2;
 167                     mbox_button_active = i;
 168                 }
 169                 return KEY_SET;
 170             }
 171             x += BUTTON_SIZE*FONT_WIDTH+BUTTON_SEP;
 172         }
 173     }
 174     return 0;
 175 }
 176 
 177 //-------------------------------------------------------------------
 178 
 179 void gui_browser_progress_show(const char* msg, const unsigned int perc)
 180 {
 181     coord x=60, y=100;
 182     unsigned int w=240, h=40;
 183 
 184     draw_rectangle(x, y, x+w, y+h, MAKE_COLOR(COLOR_GREY, COLOR_WHITE), RECT_BORDER1|DRAW_FILLED|RECT_SHADOW3); // main box
 185     draw_string_justified(x, y+2, msg, MAKE_COLOR(COLOR_GREY, COLOR_WHITE), 0, w, TEXT_CENTER); //title text
 186     draw_rectangle(x+10, y+4+FONT_HEIGHT, x+w-10, y+h-10, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE), RECT_BORDER1|DRAW_FILLED); // progress rect
 187     draw_rectangle(x+11, y+5+FONT_HEIGHT, x+11+(w-22)*perc/100, y+h-11, MAKE_COLOR(COLOR_RED, COLOR_RED), RECT_BORDER0|DRAW_FILLED); // progress bar
 188 }

/* [<][>][^][v][top][bottom][index][help] */