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

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