root/modules/gui_hexbox.c

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

DEFINITIONS

This source file includes following definitions.
  1. hexbox_init
  2. edit_hex_digit
  3. gui_hexbox_draw_hex32
  4. gui_hexbox_draw_hint
  5. gui_hexbox_draw
  6. gui_hexbox_kbd_process
  7. gui_module_menu_kbd_process
  8. _module_unloader
  9. _module_can_unload
  10. _module_exit_alt

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "keyboard.h"
   4 #include "conf.h"
   5 #include "lang.h"
   6 #include "gui.h"
   7 #include "gui_draw.h"
   8 #include "gui_lang.h"
   9 
  10 #include "gui_hexbox.h"
  11 #include "module_def.h"
  12 
  13 //-------------------------------------------------------------------
  14 int gui_hexbox_kbd_process();
  15 void gui_module_menu_kbd_process();
  16 void gui_hexbox_draw();
  17 
  18 gui_handler GUI_MODE_HEXBOX =
  19     /*GUI_MODE_HEXBOX*/ { GUI_MODE_MODULE, gui_hexbox_draw, gui_hexbox_kbd_process, gui_module_menu_kbd_process, 0, GUI_MODE_FLAG_NODRAWRESTORE };
  20 
  21 static gui_handler *gui_hexbox_mode_old; // stored previous gui_mode
  22 static int running = 0;
  23 static int currpos = 7; // editing cursor position
  24 static int hexbox_to_draw;
  25 static int *num_to_edit;
  26 static int num_backup;
  27 static int hexbox_flags;
  28 static char box_title[64];
  29 static char box_helpline[64];
  30 static int w, h, x, y;
  31 static int offs_title, offs_num;
  32 
  33 //-------------------------------------------------------
  34 int hexbox_init(int *num, char *title, int flags)
  35 {
  36     running = 1;
  37 
  38     gui_hexbox_mode_old = gui_set_mode( &GUI_MODE_HEXBOX );
  39     
  40     num_to_edit = num;
  41     num_backup = *num;
  42     hexbox_flags = flags;
  43     strncpy(box_title, title, 63);
  44     box_title[63] = 0;
  45 
  46     sprintf(box_helpline,"\x18\x19\x1b\x1a [SET]:%s [MENU]:%s",lang_str(LANG_MBOX_BTN_OK),lang_str(LANG_MBOX_BTN_CANCEL));
  47     int btl = strlen(box_title);
  48     int hll = strlen(box_helpline);
  49     if (btl < hll) btl = hll;
  50     w = ( btl + 4) * FONT_WIDTH;
  51 
  52     if (w > camera_screen.width - 4 * FONT_WIDTH) {
  53         w = camera_screen.width - 4 * FONT_WIDTH;
  54         box_title[(w/FONT_WIDTH - 4 < 63)?w/FONT_WIDTH - 4:63] = 0; // shorten the title
  55     }
  56     h = FONT_HEIGHT * 5;
  57     x = (camera_screen.width - w) >> 1;
  58     y = ((camera_screen.height - h)>>5)<<4;
  59 
  60     offs_title = ((camera_screen.width - strlen(box_title)*FONT_WIDTH)>>1);
  61     offs_num = ((camera_screen.width - (10+6+1)*FONT_WIDTH)>>1); // "0x12345678 4095M+"
  62 
  63     hexbox_to_draw = 1;
  64 
  65     return 1;
  66 }
  67 
  68 //-------------------------------------------------------
  69 
  70 void edit_hex_digit(unsigned int* addr, int digsel, int increment) {
  71 /*
  72  * addr: address of the 32bit integer under edit
  73  * digsel: selected digit (MSD=0, LSD=7)
  74  * increment: only the sign matters, or selected digit is zeroed if 0
  75 */
  76     register unsigned int a,b;
  77     a=( (*addr)&(0xf<<((7-digsel)*4)) )>>((7-digsel)*4);
  78     b=0xffffffff^(0xf<<((7-digsel)*4));
  79     if (increment<0) {
  80         if((digsel!=7)||!(hexbox_flags&HEXBOX_FLAG_WALIGN)) {
  81             if (a>0) {a-=1;} else {a=0xf;}
  82         }
  83         else {
  84             if (a>3) {a-=4;} else {a=0xc;}
  85         }
  86     }
  87     else if (increment>0) {
  88         if((digsel!=7)||!(hexbox_flags&HEXBOX_FLAG_WALIGN)) {
  89             if (a<0xf) {a+=1;} else {a=0;}
  90         }
  91         else {
  92             if (a<=0x8) {a+=4;} else {a=0;}
  93         }
  94     }
  95     else {
  96         a=0;
  97     }
  98     *addr=(a<<((7-digsel)*4))|( *addr&b );
  99 }
 100 
 101 void gui_hexbox_draw_hex32(unsigned int hexnum, int digit, unsigned int posx, unsigned int posy) {
 102 /*
 103  * draws unsigned 32 bit hex integer
 104  * digit: digit under edit (0..7), -1 when not edited
 105  * posx: x position, posy: y position
 106 */
 107     char buf[12];
 108     digit += 2; // due to '0x'
 109     sprintf(buf, "0x%08X", hexnum);
 110     draw_string(posx, posy, buf,  MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 111     if ((digit >= 2) && (digit < 10))
 112     {
 113         draw_char(posx+digit*FONT_WIDTH, posy, buf[digit], MAKE_COLOR(COLOR_RED, COLOR_WHITE));
 114     }
 115 }
 116 
 117 void gui_hexbox_draw_hint(unsigned int hexnum, unsigned int posx, unsigned int posy) {
 118 /*
 119  * show a decimal hint about the entered value
 120 */
 121     char buf[12];
 122     if (hexnum > 1024*1024) {
 123         sprintf(buf, "%4dM%s", hexnum>>20, ((hexnum&0xfffff)>0)?"+":" " );
 124     }
 125     else if (hexnum > 1024) {
 126         sprintf(buf, "%4dk%s", hexnum>>10, ((hexnum&0x3ff)>0)?"+":" " );
 127     }
 128     else {
 129         sprintf(buf, "%4d  ", hexnum);
 130     }
 131     draw_string(posx, posy, buf, MAKE_COLOR(COLOR_WHITE, COLOR_BLACK));
 132 }
 133 
 134 //-------------------------------------------------------
 135 
 136 void gui_hexbox_draw()
 137 {
 138     switch (hexbox_to_draw) {
 139         case 1:
 140             draw_rectangle(x-4, y-4, x+w+4, y+h+4, MAKE_COLOR(COLOR_GREY, COLOR_WHITE), RECT_BORDER3|DRAW_FILLED|RECT_SHADOW3);
 141             draw_rectangle(x-2, y-2, x+w+2, y+FONT_HEIGHT+2, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE), RECT_BORDER1|DRAW_FILLED);
 142             draw_string(offs_title, y, box_title,  MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 143             draw_string(x+FONT_WIDTH*2, y+FONT_HEIGHT*4, box_helpline,  MAKE_COLOR(COLOR_GREY, COLOR_WHITE));
 144 
 145             // fall through
 146         case 2:
 147             gui_hexbox_draw_hex32(*num_to_edit, currpos, offs_num, y + FONT_HEIGHT * 2);
 148             gui_hexbox_draw_hint(*num_to_edit, offs_num + 11*FONT_WIDTH, y + FONT_HEIGHT * 2);
 149             hexbox_to_draw = 0;
 150             break;
 151     }
 152 }
 153 
 154 //-------------------------------------------------------
 155 
 156 int gui_hexbox_kbd_process()
 157 {
 158     int need_redraw = 1;
 159     switch (kbd_get_autoclicked_key() | get_jogdial_direction()) {
 160         case KEY_UP:
 161         case JOGDIAL_RIGHT:
 162             edit_hex_digit((unsigned int*)num_to_edit, currpos, 1);
 163             break;
 164         case KEY_DOWN:
 165         case JOGDIAL_LEFT:
 166             edit_hex_digit((unsigned int*)num_to_edit, currpos, -1);
 167             break;
 168         case KEY_LEFT:
 169         //case KEY_ZOOM_OUT:
 170             currpos -= 1;
 171             break;
 172         case KEY_RIGHT:
 173         //case KEY_ZOOM_IN:
 174             currpos += 1;
 175             break;
 176         case KEY_SET:
 177             need_redraw = 0;
 178             gui_set_mode(gui_hexbox_mode_old);
 179             running = 0; // editing done
 180             break;
 181         case KEY_SHOOT_HALF:
 182             edit_hex_digit((unsigned int*)num_to_edit, currpos, 0); // zero current digit only
 183             break;
 184         case KEY_SHOOT_FULL:
 185         case KEY_DISPLAY:
 186             *num_to_edit = 0; // zero the whole number
 187             currpos = 7;
 188             break;
 189         default:
 190             need_redraw = 0;
 191     }
 192     if (currpos > 7) currpos = 0;
 193     if (currpos < 0) currpos = 7;
 194 
 195     if (need_redraw) hexbox_to_draw = 2;
 196     return 0;
 197 }
 198 
 199 //-------------------------------------------------------
 200 
 201 void gui_module_menu_kbd_process()
 202 {
 203     *num_to_edit = num_backup; // edit cancelled
 204     running = 0;
 205     gui_set_mode(gui_hexbox_mode_old);
 206 }
 207 
 208 
 209 //==================================================
 210 
 211 //---------------------------------------------------------
 212 // PURPOSE: Finalize module operations (close allocs, etc)
 213 // RETURN VALUE: 0-ok, 1-fail
 214 //---------------------------------------------------------
 215 int _module_unloader()
 216 {
 217     // free allocated resource, if any
 218 
 219     return 0;
 220 }
 221 
 222 int _module_can_unload()
 223 {
 224     return running == 0;
 225 }
 226 
 227 int _module_exit_alt()
 228 {
 229     *num_to_edit = num_backup; // edit cancelled
 230     running = 0;
 231     return 0;
 232 }
 233 
 234 /******************** Module Information structure ******************/
 235 
 236 libhexbox_sym _libhexbox =
 237 {
 238     {
 239          0, _module_unloader, _module_can_unload, _module_exit_alt, 0
 240     },
 241 
 242     hexbox_init,
 243 };
 244 
 245 ModuleInfo _module_info =
 246 {
 247     MODULEINFO_V1_MAGICNUM,
 248     sizeof(ModuleInfo),
 249     GUI_HEXBOX_VERSION,          // Module version
 250 
 251     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,         // Requirements of CHDK version
 252     ANY_PLATFORM_ALLOWED,       // Specify platform dependency
 253 
 254     (int32_t)"Hex number editor",
 255     MTYPE_EXTENSION,
 256 
 257     &_libhexbox.base,
 258 
 259     CONF_VERSION,               // CONF version
 260     CAM_SCREEN_VERSION,         // CAM SCREEN version
 261     ANY_VERSION,                // CAM SENSOR version
 262     ANY_VERSION,                // CAM INFO version
 263 };

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