root/modules/gui_mpopup.c

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

DEFINITIONS

This source file includes following definitions.
  1. gui_mpopup_init
  2. gui_mpopup_result
  3. gui_mpopup_draw_actions
  4. gui_mpopup_draw
  5. exit_mpopup
  6. gui_mpopup_kbd_process_menu_btn
  7. gui_mpopup_kbd_process
  8. gui_mpopup_touch_handler
  9. _module_unloader
  10. _module_can_unload
  11. _module_exit_alt

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "keyboard.h"
   4 #include "lang.h"
   5 #include "gui.h"
   6 #include "gui_draw.h"
   7 #include "gui_lang.h"
   8 
   9 #include "gui_mpopup.h"
  10 #include "module_def.h"
  11 
  12 /*
  13         History:        1.1 - make possible call next mpopup in callback [multilevel mpopups]
  14                                 1.2 - allow to use MPOPUP_CANCEL item in the list
  15 */
  16 
  17 int gui_mpopup_kbd_process();
  18 void gui_mpopup_kbd_process_menu_btn();
  19 void gui_mpopup_draw();
  20 static int gui_mpopup_touch_handler(int,int);
  21 
  22 gui_handler GUI_MODE_MPOPUP_MODULE =
  23     /*GUI_MODE_MPOPUP*/ { GUI_MODE_MPOPUP, gui_mpopup_draw, gui_mpopup_kbd_process, gui_mpopup_kbd_process_menu_btn, gui_mpopup_touch_handler, GUI_MODE_FLAG_NORESTORE_ON_SWITCH };
  24 
  25 // Simple popup menu. No title, no separators, only processing items
  26 
  27 //-------------------------------------------------------------------
  28 static gui_handler              *gui_mpopup_mode_old;
  29 static int                      running = 0;
  30 static char                     mpopup_to_draw;
  31 
  32 #define MAX_ACTIONS             14
  33 
  34 struct mpopup_item* actions;
  35 
  36 static short                    mpopup_actions[MAX_ACTIONS];    // Content of raised popupmenu
  37 static int                      mpopup_actions_num;             // Num of items in raised popupmenu
  38 static int                      mpopup_actions_active;          // Idx of current item (cursor)
  39 static coord                    mpopup_actions_x, mpopup_actions_y;    // top-left coord of window
  40 static unsigned int             mpopup_actions_w;               // width of window
  41 
  42 typedef void (*mpopup_on_select_t)(unsigned int btn);
  43 static mpopup_on_select_t mpopup_on_select;
  44 
  45 //-------------------------------------------------------------------
  46 void gui_mpopup_init(struct mpopup_item* popup_actions, const unsigned int flags, void (*on_select)(unsigned int actn)) 
  47 {
  48     int i;
  49 
  50     running++;
  51 
  52     mpopup_actions_num = 0;
  53     actions = popup_actions;
  54     for (i=0; actions[i].flag && mpopup_actions_num<MAX_ACTIONS; ++i)
  55     {
  56         if ((flags & MPOPUP_MASK & actions[i].flag) || (actions[i].flag == MPOPUP_CANCEL))
  57             mpopup_actions[mpopup_actions_num++] = i;
  58     }
  59     if (mpopup_actions_num == 0)
  60     {
  61         on_select(MPOPUP_CANCEL);
  62         running--;
  63                 return;
  64         }
  65 
  66     mpopup_actions_active = 0;
  67 
  68     mpopup_to_draw = 1;
  69     mpopup_on_select = on_select;
  70     gui_mpopup_mode_old = gui_set_mode(&GUI_MODE_MPOPUP_MODULE);
  71 }
  72 
  73 //-------------------------------------------------------------------
  74 unsigned int gui_mpopup_result()
  75 {
  76     return actions[mpopup_actions[mpopup_actions_active]].flag;
  77 }
  78 
  79 //-------------------------------------------------------------------
  80 static void gui_mpopup_draw_actions() {
  81     int i;
  82     coord y = mpopup_actions_y;
  83     twoColors cl;
  84 
  85     for (i=0; i<mpopup_actions_num; ++i) {
  86         cl = MAKE_COLOR((mpopup_actions_active==i)?COLOR_RED:COLOR_GREY, COLOR_WHITE);
  87         draw_string_justified(mpopup_actions_x, y, lang_str(actions[mpopup_actions[i]].text), cl, FONT_WIDTH, mpopup_actions_w*FONT_WIDTH+1, TEXT_LEFT|TEXT_FILL);
  88         y+=FONT_HEIGHT;
  89     }
  90 }
  91 
  92 //-------------------------------------------------------------------
  93 void gui_mpopup_draw() {
  94     if (mpopup_to_draw) {
  95         int i;
  96         coord x=0, y=0;
  97         unsigned int w, h;
  98 
  99         w = 0;
 100         for (i=0; i<mpopup_actions_num; ++i) {
 101             h=strlen(lang_str(actions[mpopup_actions[i]].text));
 102             if (h > w) w=h;
 103         }
 104         w+=2;
 105         h = mpopup_actions_num;
 106     
 107         x = (camera_screen.width - w*FONT_WIDTH) / 2;
 108         y = (camera_screen.height - h*FONT_HEIGHT) / 2;
 109         draw_rectangle(x-4, y-4, x+w*FONT_WIDTH+4, y+h*FONT_HEIGHT+3,
 110                        MAKE_COLOR(COLOR_GREY, COLOR_WHITE), RECT_BORDER3|DRAW_FILLED|RECT_SHADOW3); // main box
 111     
 112         mpopup_actions_x = x;
 113         mpopup_actions_y = y;
 114         mpopup_actions_w = w;
 115 
 116         gui_mpopup_draw_actions();
 117         mpopup_to_draw = 0;
 118     }
 119 }
 120 
 121 //-------------------------------------------------------------------
 122 void exit_mpopup(int action)
 123 {
 124     gui_set_mode(gui_mpopup_mode_old);
 125 
 126         mpopup_on_select_t on_select = mpopup_on_select;        // this could be reinited in callback
 127         mpopup_on_select = 0;
 128     if (on_select) 
 129         on_select(action);
 130 
 131     running--;
 132 }
 133 
 134 //-------------------------------------------------------------------
 135 void gui_mpopup_kbd_process_menu_btn()
 136 {
 137         exit_mpopup(MPOPUP_CANCEL);             
 138 }
 139 
 140 int gui_mpopup_kbd_process()
 141 {
 142     switch (kbd_get_clicked_key() | get_jogdial_direction()) {
 143     case JOGDIAL_LEFT:
 144     case KEY_UP:
 145         if (mpopup_actions_active > 0) --mpopup_actions_active;
 146         else mpopup_actions_active = mpopup_actions_num-1;
 147         gui_mpopup_draw_actions();
 148         break;
 149     case JOGDIAL_RIGHT:
 150     case KEY_DOWN:
 151         if (mpopup_actions_active < mpopup_actions_num-1) ++mpopup_actions_active;
 152         else mpopup_actions_active = 0;
 153         gui_mpopup_draw_actions();
 154         break;
 155     case KEY_LEFT:
 156         gui_mpopup_kbd_process_menu_btn();
 157         break;
 158     case KEY_SET:
 159                 exit_mpopup(actions[mpopup_actions[mpopup_actions_active]].flag);               
 160         break;
 161     }
 162     return 0;
 163 }
 164 
 165 static int gui_mpopup_touch_handler(int sx, int sy)
 166 {
 167     if ((sx >= mpopup_actions_x) && (sx <= mpopup_actions_x+mpopup_actions_w*FONT_WIDTH) && (sy >= mpopup_actions_y) && (sy <= mpopup_actions_y+mpopup_actions_num*FONT_HEIGHT))
 168     {
 169         sy = (sy - mpopup_actions_y) / FONT_HEIGHT;
 170         if (mpopup_actions_active != sy)
 171         {
 172             mpopup_actions_active = sy;
 173             mpopup_to_draw = 1;
 174         }
 175         return KEY_SET;
 176     }
 177     return 0;
 178 }
 179 
 180 //-------------------------------------------------------------------
 181 
 182 // =========  MODULE INIT =================
 183 
 184 /***************** BEGIN OF AUXILARY PART *********************
 185   ATTENTION: DO NOT REMOVE OR CHANGE SIGNATURES IN THIS SECTION
 186  **************************************************************/
 187 
 188 //---------------------------------------------------------
 189 // PURPOSE: Finalize module operations (close allocs, etc)
 190 // RETURN VALUE: 0-ok, 1-fail
 191 //---------------------------------------------------------
 192 int _module_unloader()
 193 {
 194     if (mpopup_on_select) 
 195         mpopup_on_select(MPOPUP_CANCEL);
 196 
 197     return 0;
 198 }
 199 
 200 int _module_can_unload()
 201 {
 202     return running == 0;
 203 }
 204 
 205 int _module_exit_alt()
 206 {
 207         exit_mpopup(MPOPUP_CANCEL);             
 208     running = 0;
 209     return 0;
 210 }
 211 
 212 /******************** Module Information structure ******************/
 213 
 214 libmpopup_sym _libmpopup =
 215 {
 216     {
 217          0, _module_unloader, _module_can_unload, _module_exit_alt, 0
 218     },
 219 
 220     gui_mpopup_init
 221 };
 222 
 223 ModuleInfo _module_info =
 224 {
 225     MODULEINFO_V1_MAGICNUM,
 226     sizeof(ModuleInfo),
 227     GUI_MPOPUP_VERSION,                 // Module version
 228 
 229     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,                       // Requirements of CHDK version
 230     ANY_PLATFORM_ALLOWED,               // Specify platform dependency
 231 
 232     -LANG_MODULE_POPUP_MENU,    // Module name
 233     MTYPE_EXTENSION,
 234 
 235     &_libmpopup.base,
 236 
 237     ANY_VERSION,                // CONF version
 238     ANY_VERSION,                // CAM SCREEN version
 239     ANY_VERSION,                // CAM SENSOR version
 240     ANY_VERSION,                // CAM INFO version
 241 };
 242 
 243 /*************** END OF AUXILARY PART *******************/

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