root/core/gui_user_menu.c

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

DEFINITIONS

This source file includes following definitions.
  1. user_menu_saved_size
  2. user_menu_store_data
  3. user_menu_load_data
  4. init_user_menu
  5. rinit
  6. load_user_menu_script_callback
  7. gui_load_user_menu_script
  8. user_menu_save
  9. set_user_menu_extern
  10. user_menu_restore

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "conf.h"
   4 #include "keyboard.h"
   5 #include "font.h"
   6 #include "lang.h"
   7 #include "gui.h"
   8 #include "gui_lang.h"
   9 #include "gui_draw.h"
  10 #include "gui_menu.h"
  11 #include "gui_user_menu.h"
  12 #include "gui_mbox.h"
  13 #include "modules.h"
  14 #include "module_load.h"
  15 
  16 #define USER_MENU_IS_SCRIPT     -1
  17 #define USER_MENU_IS_MODULE     -2
  18 
  19 //-------------------------------------------------------------------
  20 static int user_menu_saved_size()
  21 {
  22     // User menu saved as:
  23     //      - num_items (int)
  24     //      - (var + script_file + script_title)[num_items]
  25     return conf.user_menu_vars.cfg.num_items * (sizeof(int) + CONF_STR_LEN * 2) + sizeof(int);
  26 }
  27 
  28 static char* user_menu_store_data(char *p)
  29 {
  30     int i;
  31 
  32     memcpy(p, &conf.user_menu_vars.cfg.num_items, sizeof(int));
  33     p += sizeof(int);
  34     
  35     for (i=0; i<conf.user_menu_vars.cfg.num_items; i++)
  36     {
  37         memcpy(p, &conf.user_menu_vars.items[i].var, sizeof(int));
  38         p += sizeof(int);
  39         memset(p, 0, CONF_STR_LEN * 2);
  40         if (conf.user_menu_vars.items[i].script_file) memcpy(p, conf.user_menu_vars.items[i].script_file, strlen(conf.user_menu_vars.items[i].script_file));
  41         p += CONF_STR_LEN;
  42         if (conf.user_menu_vars.items[i].script_title) memcpy(p, conf.user_menu_vars.items[i].script_title, strlen(conf.user_menu_vars.items[i].script_title));
  43         p += CONF_STR_LEN;
  44     }
  45 
  46     return p;
  47 }
  48 
  49 static int user_menu_load_data(char *p)
  50 {
  51     int i, n;
  52 
  53     memcpy(&n, p, sizeof(int));
  54     if (n > conf.user_menu_vars.cfg.num_items)
  55         init_user_menu(n);
  56     p += sizeof(int);
  57     
  58     for (i=0; i<n; i++)
  59     {
  60         memcpy(&conf.user_menu_vars.items[i].var, p, sizeof(int));
  61         p += sizeof(int);
  62         if (*p)
  63         {
  64             if (conf.user_menu_vars.items[i].script_file == 0)
  65                 conf.user_menu_vars.items[i].script_file = malloc(CONF_STR_LEN);
  66             strcpy(conf.user_menu_vars.items[i].script_file,p);
  67         }
  68         p += CONF_STR_LEN;
  69         if (*p)
  70         {
  71             if (conf.user_menu_vars.items[i].script_title == 0)
  72                 conf.user_menu_vars.items[i].script_title = malloc(CONF_STR_LEN);
  73             strcpy(conf.user_menu_vars.items[i].script_title,p);
  74         }
  75         p += CONF_STR_LEN;
  76     }
  77 
  78     return user_menu_saved_size();
  79 }
  80 
  81 void init_user_menu(int num_items)
  82 {
  83     if (conf.user_menu_vars.items)
  84     {
  85         int i;
  86         for (i=0; i<conf.user_menu_vars.cfg.num_items; i++)
  87         {
  88             if (conf.user_menu_vars.items[i].script_file)
  89                 free(conf.user_menu_vars.items[i].script_file);
  90             if (conf.user_menu_vars.items[i].script_title)
  91                 free(conf.user_menu_vars.items[i].script_title);
  92         }
  93         free(conf.user_menu_vars.items);
  94         conf.user_menu_vars.items = 0;
  95     }
  96 
  97     conf.user_menu_vars.cfg.num_items = num_items;
  98     conf.user_menu_vars.cfg.item_size = sizeof(tUserMenuItem);
  99     conf.user_menu_vars.cfg.saved_size = user_menu_saved_size;
 100     conf.user_menu_vars.cfg.save = user_menu_store_data;
 101     conf.user_menu_vars.cfg.load = user_menu_load_data;
 102     conf.user_menu_vars.items = malloc(num_items * sizeof(tUserMenuItem));
 103     memset(conf.user_menu_vars.items, 0, num_items * sizeof(tUserMenuItem));
 104 }
 105 
 106 //-------------------------------------------------------------------
 107 static void rinit()
 108 {
 109         // Erase screen if switching from user menu to main menu
 110         // in case the user menu is larger than the main menu
 111         // otherwise it leaves remnants of the user menu above and below
 112         // the main menu.
 113     gui_set_need_restore();
 114         gui_menu_init(&root_menu);
 115 }
 116 
 117 //-------------------------------------------------------------------
 118 /*
 119  * 1 extra entry for the "Main menu" and 1 for null when the menu is full with user selections
 120  * Compiler will zero init remaining portion of array so no there is no hidden relationship between
 121  * this structure and the value of USER_MENU_ITEMS. The value of USER_MENU_ITEMS can be anything you
 122  * wish and everything automagically works.
 123 */
 124 
 125 static CMenuItem user_submenu_items[USER_MENU_ITEMS + 2] = {
 126         MENU_ITEM(0x20,LANG_MENU_MAIN_TITLE,     MENUITEM_PROC,  rinit, 0 )
 127 };
 128 
 129 CMenu user_submenu = {0x2e,LANG_MENU_USER_MENU, user_submenu_items };
 130 
 131 //-------------------------------------------------------------------
 132 
 133 extern CMenu script_submenu;
 134 static char sfname[CONF_STR_LEN];
 135 
 136 static void load_user_menu_script_callback(unsigned int btn)
 137 {
 138     if (btn==MBOX_BTN_YES)
 139     {
 140         script_load(sfname);
 141         gui_activate_sub_menu(&script_submenu) ;
 142     }
 143 }
 144 
 145 void gui_load_user_menu_script(const char *fn) 
 146 {
 147     if (fn)
 148     {
 149         strcpy(sfname, fn);
 150         
 151         if( kbd_get_clicked_key() == KEY_SHOOT_FULL )       
 152         {
 153             script_load(sfname);                            // load script from menu choice            
 154             gui_set_mode(&altGuiHandler);                   // exit the menu system
 155             script_start_gui(0);                            // and run the script
 156         }
 157         else
 158         {
 159             gui_mbox_init(LANG_MENU_SCRIPT_LOAD, (int)sfname, MBOX_BTN_YES_NO|MBOX_TEXT_CENTER|MBOX_FUNC_RESTORE, load_user_menu_script_callback);   
 160         }
 161     }
 162 }
 163 
 164 //-------------------------------------------------------------------
 165 void user_menu_save() {
 166     int x;
 167     for (x=0; x<USER_MENU_ITEMS; x++) {
 168                 /*
 169                  * First entry in user_submenu_items is reserved for the "Main Menu"
 170                  * conf.user_menu_vars only tracks/saves the real user entries.
 171                  */
 172         
 173         if ( user_submenu_items[x+1].text )
 174         {
 175             if ( user_submenu_items[x+1].value == (int *)gui_load_user_menu_script )
 176             {
 177                 conf.user_menu_vars.items[x].var = USER_MENU_IS_SCRIPT;                             // flag script entries specially 
 178             }
 179             else if ((user_submenu_items[x+1].value == (int *)module_run) && ((user_submenu_items[x+1].type & MENUITEM_USER_MODULE) == MENUITEM_USER_MODULE))
 180             {
 181                 conf.user_menu_vars.items[x].var = USER_MENU_IS_MODULE;                             // flag module entries specially 
 182             }
 183             else
 184             {
 185                 conf.user_menu_vars.items[x].var = lang_strhash31(user_submenu_items[x+1].text);    // otherwise save a hash
 186             }
 187         }
 188         else
 189         {
 190             conf.user_menu_vars.items[x].var = 0;
 191             if (conf.user_menu_vars.items[x].script_file != 0)
 192             {
 193                 free(conf.user_menu_vars.items[x].script_file);
 194                 conf.user_menu_vars.items[x].script_file = 0;
 195             }
 196             if (conf.user_menu_vars.items[x].script_title != 0)
 197             {
 198                 free(conf.user_menu_vars.items[x].script_title);
 199                 conf.user_menu_vars.items[x].script_title = 0;
 200             }
 201         }
 202     } 
 203 }
 204 
 205 static void set_user_menu_extern(int menu, int var, char sym, short type, int* func)
 206 {
 207     // restore the script/module entry
 208     user_submenu_items[menu].symbol = sym;
 209     user_submenu_items[menu].opt_len = 0;
 210     user_submenu_items[menu].type = type;
 211     user_submenu_items[menu].text = (int)conf.user_menu_vars.items[var].script_title;
 212     user_submenu_items[menu].value = func;
 213     user_submenu_items[menu].arg = (int)conf.user_menu_vars.items[var].script_file;  
 214 }
 215 
 216 void user_menu_restore()
 217 {
 218     int x, y;
 219     CMenuItem* item=0;
 220     /*
 221      * First entry in user_submenu_items is reserved for the "Main Menu"
 222      * conf.user_menu_vars only tracks/saves the real user entries.
 223      */
 224        
 225     for (x=0, y=1; x<USER_MENU_ITEMS; x++, y++)
 226     {
 227         if (conf.user_menu_vars.items[x].var == USER_MENU_IS_SCRIPT)    // special flag- there is no hash for script entries
 228         {
 229             set_user_menu_extern(y, x, 0x35, MENUITEM_PROC, (int*)gui_load_user_menu_script);
 230         }
 231         else if (conf.user_menu_vars.items[x].var == USER_MENU_IS_MODULE)    // special flag- there is no hash for module entries
 232         {   
 233             set_user_menu_extern(y, x, 0x28, MENUITEM_PROC|MENUITEM_USER_MODULE, (int*)module_run);
 234         }
 235         else
 236         { 
 237             if (conf.user_menu_vars.items[x].var > 0 )  // look up the menu text
 238                  item = find_menu_item(&root_menu, conf.user_menu_vars.items[x].var);
 239             else item = NULL ;
 240             
 241             if ( item )                                 // add back in if found
 242             {
 243                 user_submenu_items[y] = *item;
 244             }
 245             else                                        // otherwise clear the menu entry
 246             {
 247                 user_submenu_items[y].text = 0;            
 248                 break ;                                 // and exit - all done !
 249             }
 250         }
 251     }  
 252 }
 253 

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