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

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