root/modules/gui_calendar.c

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

DEFINITIONS

This source file includes following definitions.
  1. calendar_goto_today
  2. calendar_days_in_month
  3. calendar_day_of_week
  4. gui_calendar_initial_draw
  5. basic_module_init
  6. gui_calendar_kbd_process
  7. gui_calendar_draw

   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 #include "time.h"
   9 
  10 #include "module_def.h"
  11 
  12 void gui_module_menu_kbd_process();
  13 int gui_calendar_kbd_process();
  14 void gui_calendar_draw();
  15 
  16 gui_handler GUI_MODE_CALENDAR = 
  17     /*GUI_MODE_CALENDAR*/   { GUI_MODE_MODULE, gui_calendar_draw, gui_calendar_kbd_process, gui_module_menu_kbd_process, 0, GUI_MODE_FLAG_NODRAWRESTORE };
  18 
  19 //-------------------------------------------------------------------
  20 #define TITLE_COLOR             (MAKE_COLOR(COLOR_BLACK, COLOR_WHITE))
  21 #define CALENDAR_COLOR          (MAKE_COLOR(COLOR_GREY, COLOR_WHITE))
  22 #define WEEKEND_COLOR           (MAKE_COLOR(COLOR_YELLOW, COLOR_RED))
  23 
  24 //-------------------------------------------------------------------
  25 static int need_redraw;
  26 static int cal_year, cal_month;
  27 static coord cal_x, cal_y, cal_w, cal_h;
  28 static int months[] = {LANG_CALENDAR_JANUARY, LANG_CALENDAR_FEBRUARY, LANG_CALENDAR_MARCH,
  29                        LANG_CALENDAR_APRIL, LANG_CALENDAR_MAY, LANG_CALENDAR_JUNE,
  30                        LANG_CALENDAR_JULY, LANG_CALENDAR_AUGUST, LANG_CALENDAR_SEPTEMBER,
  31                        LANG_CALENDAR_OCTOBER, LANG_CALENDAR_NOVEMBER, LANG_CALENDAR_DECEMBER};
  32 static int days[] = {LANG_CALENDAR_MON, LANG_CALENDAR_TUE, LANG_CALENDAR_WED, LANG_CALENDAR_THU,
  33                      LANG_CALENDAR_FRI, LANG_CALENDAR_SAT, LANG_CALENDAR_SUN};
  34 //-------------------------------------------------------------------
  35 static void calendar_goto_today() {
  36     struct tm *ttm;
  37 
  38     ttm = get_localtime();
  39     cal_year = 1900+ttm->tm_year;
  40     cal_month = ttm->tm_mon;
  41 }
  42 
  43 //-------------------------------------------------------------------
  44 static int calendar_days_in_month(int month, int year) {
  45     switch (month) {
  46         case 2: 
  47             return ((year%4==0 && year%100!=0) || year%400==0)?29:28;
  48         case 4: case 6: case 9: case 11:
  49             return 30;
  50         default:
  51             return 31;
  52     }
  53 }
  54 //-------------------------------------------------------------------
  55 static int calendar_day_of_week(int day /*1-31*/, int month /*1-12*/, int year) {
  56     register int a;
  57 
  58     a = (14 - month) / 12;
  59     year -= a;
  60     month += a*12 - 2;
  61 
  62     return (((7000 + day + year + year/4 - year/100 + year/400 + 31*month/12) % 7) + 6) % 7;
  63 }
  64 
  65 //-------------------------------------------------------------------
  66 static void gui_calendar_initial_draw() {
  67     int x, i;
  68 
  69     draw_rectangle(camera_screen.disp_left, 0, camera_screen.disp_right, camera_screen.height-1, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK), RECT_BORDER0|DRAW_FILLED);
  70     draw_string(camera_screen.disp_left+FONT_WIDTH, 0, lang_str(LANG_CALENDAR_TODAY), MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
  71     draw_rectangle(cal_x-3, cal_y-3, cal_x+cal_w+2, cal_y+cal_h+2, CALENDAR_COLOR, RECT_BORDER1);
  72     draw_rectangle(cal_x-1, cal_y-1, cal_x+cal_w, cal_y+FONT_HEIGHT+8, TITLE_COLOR, RECT_BORDER1|DRAW_FILLED);
  73     draw_rectangle(cal_x-1, cal_y+FONT_HEIGHT+8, cal_x+cal_w, cal_y+cal_h, CALENDAR_COLOR, RECT_BORDER1|DRAW_FILLED);
  74 
  75     draw_rectangle(cal_x+cal_w-FONT_WIDTH*4*2, cal_y+FONT_HEIGHT+8+1, cal_x+cal_w-1, cal_y+cal_h-1, MAKE_COLOR(BG_COLOR(WEEKEND_COLOR), BG_COLOR(WEEKEND_COLOR)), RECT_BORDER0|DRAW_FILLED);
  76     for (x=cal_x+FONT_WIDTH/2, i=0; i<7; x+=FONT_WIDTH*4, ++i) {
  77         draw_string(x, cal_y+4+FONT_HEIGHT+4+4, lang_str(days[i]), (i<5)?CALENDAR_COLOR:WEEKEND_COLOR);
  78     }
  79 }
  80 
  81 //-------------------------------------------------------------------
  82 int basic_module_init() {
  83 
  84     gui_set_mode(&GUI_MODE_CALENDAR);
  85 
  86     calendar_goto_today();
  87     cal_w = FONT_WIDTH*4*7;
  88     cal_h = 4+FONT_HEIGHT+4+4+FONT_HEIGHT+4+(FONT_HEIGHT+4)*6;
  89     cal_x = (camera_screen.width-cal_w)/2;
  90     cal_y = FONT_HEIGHT+(camera_screen.height-FONT_HEIGHT-cal_h)/2;
  91     need_redraw = 2;
  92         return 1;
  93 }
  94 
  95 //-------------------------------------------------------------------
  96 int gui_calendar_kbd_process() {
  97     switch (kbd_get_autoclicked_key()) {
  98         case KEY_UP:
  99             ++cal_year;
 100             need_redraw = 1;
 101             break;
 102         case KEY_DOWN:
 103             if (cal_year>1) --cal_year;
 104             need_redraw = 1;
 105             break;
 106         case KEY_LEFT:
 107             if (--cal_month<0) {
 108                 cal_month=11;
 109                 --cal_year;
 110             }
 111             need_redraw = 1;
 112             break;
 113         case KEY_RIGHT:
 114             if (++cal_month>11) {
 115                 cal_month=0;
 116                 ++cal_year;
 117             }
 118             need_redraw = 1;
 119             break;
 120         case KEY_ERASE:
 121         case KEY_DISPLAY:
 122             calendar_goto_today();
 123             need_redraw = 1;
 124             break;
 125     }
 126     return 0;
 127 }
 128 
 129 //-------------------------------------------------------------------
 130 void gui_calendar_draw() {
 131     int x, y;
 132     static char str[32];
 133     int w, d, i;
 134     static struct tm *ttm;
 135 
 136     if (need_redraw == 2)
 137         gui_calendar_initial_draw();
 138 
 139     ttm = get_localtime();
 140     sprintf(str, " %2u %s %04u  %2u:%02u:%02u   ", ttm->tm_mday, lang_str(months[ttm->tm_mon]), 1900+ttm->tm_year, ttm->tm_hour, ttm->tm_min, ttm->tm_sec);
 141     draw_string(camera_screen.disp_left+FONT_WIDTH*8, 0, str, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE));
 142 
 143     if (need_redraw) {
 144         need_redraw = 0;
 145         
 146         i = strlen(lang_str(months[cal_month]));
 147         x = cal_x + (cal_w-FONT_WIDTH*2-FONT_WIDTH*4-FONT_WIDTH*2-i*FONT_WIDTH)/2;
 148         y = cal_y + 4;
 149         draw_rectangle(cal_x+FONT_WIDTH, y, cal_x+cal_w-FONT_WIDTH-FONT_WIDTH*4-FONT_WIDTH, y+FONT_HEIGHT, MAKE_COLOR(BG_COLOR(TITLE_COLOR), BG_COLOR(TITLE_COLOR)), RECT_BORDER0|DRAW_FILLED);
 150         draw_string(x+FONT_WIDTH, y, lang_str(months[cal_month]), TITLE_COLOR);
 151         
 152         sprintf(str, "%04d", cal_year);
 153         draw_string(cal_x+cal_w-FONT_WIDTH*2-FONT_WIDTH*4, y, str, TITLE_COLOR);
 154 
 155         d = calendar_days_in_month(cal_month+1, cal_year);
 156         w = calendar_day_of_week(1, cal_month+1, cal_year);
 157 
 158         y += FONT_HEIGHT+4+4;
 159 
 160         y += FONT_HEIGHT+4;
 161         for (x=0; x<w; ++x) {
 162             draw_string(cal_x+x*FONT_WIDTH*4, y, "    ", (x<5)?CALENDAR_COLOR:WEEKEND_COLOR);
 163         }
 164         
 165         for (i=1; i<=d; ++i) {
 166             sprintf(str, " %2d ", i);
 167             draw_string(cal_x+x*FONT_WIDTH*4, y, str, (x<5)?CALENDAR_COLOR:WEEKEND_COLOR);
 168             
 169             if (++x>6) {
 170               x=0;
 171               y += FONT_HEIGHT+4;
 172             }
 173         }
 174         for (; y<cal_y+cal_h; y += FONT_HEIGHT+4, x=0) {
 175             for (; x<7; ++x) {
 176                 draw_string(cal_x+x*FONT_WIDTH*4, y, "    ", (x<5)?CALENDAR_COLOR:WEEKEND_COLOR);
 177             }
 178         }
 179     }
 180 }
 181 
 182 #include "simple_module.c"
 183 
 184 /******************** Module Information structure ******************/
 185 
 186 ModuleInfo _module_info =
 187 {
 188     MODULEINFO_V1_MAGICNUM,
 189     sizeof(ModuleInfo),
 190     SIMPLE_MODULE_VERSION,              // Module version
 191 
 192     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,                       // Requirements of CHDK version
 193     ANY_PLATFORM_ALLOWED,               // Specify platform dependency
 194 
 195     -LANG_MENU_MISC_CALENDAR,
 196     MTYPE_TOOL,
 197 
 198     &_librun.base,
 199 
 200     ANY_VERSION,                // CONF version
 201     CAM_SCREEN_VERSION,         // CAM SCREEN version
 202     ANY_VERSION,                // CAM SENSOR version
 203     ANY_VERSION,                // CAM INFO version
 204 
 205     0x36,                       // Calendar menu symbol
 206 };

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