#include #include #include #include "izilla.h" #include "menu_bar.h" //Function Prototypes void ttk_new_clock_widget(int x, int y, int w, int h, TWidget * menubar); void check_time(TWidget * wid); void ttk_draw_clock_widget(TWidget * wid, ttk_surface srf); void ttk_destroy_clock_widget(TWidget * wid); //Structs typedef struct ttk_clockdata{ struct tm time; } ttk_clockdata; //Function Definitions void ttk_new_clock_widget(int x, int y, int w, int h, TWidget * menubar) { ttk_clockdata * cd; TWidget * ret = ttk_new_widget(x,y); time_t rawtime = time(); struct tm * timeinfo = localtime(&rawtime); ret->w = w; ret->h = h; ret->draw = ttk_draw_clock_widget; ret->timer = check_time; ret->destroy = ttk_destroy_clock_widget; cd = ttk_new_clockdata(); ret->data = (void *)cd; ttk_add_menubar_widget(menubar, ret); } //This checks to see if the time has changed, and if so, will redraw the clock. void check_time(TWidget * wid) { TWidget * menubar = (TWidget *)wid->data2; ttk_clockdata * cd = (ttk_clockdata *)wid->data; struct tm * timeinfo = (struct tm *)cd->time; time_t rawtime = time(); struct tm * check = localtime(&rawtime); if((check->tm_hour != timeinfo->tm_hour) || (check->tm_min != timeinfo->tm_min)) { cd->time = check; ttk_draw_clock_widget(wid, menubar->win->srf); } } void ttk_draw_clock_widget(TWidget * wid, ttk_surface srf) { char time[5]; TWidget * menubar = (TWidget *)wid->data2; struct tm * timeinfo = (struct tm *)wid->data; snprintf(time, 200, "%2d:%02d", timeinfo->tm_hour, timeinfo->tm_min); int text_height = ttk_text_height(font->f); int text_width = ttk_text_width(font->f, time); ttk_blit_image_ex(menubar->win->srf, wid->x, wid->y, wid->w, wid->h, ttk_screen->srf, wid->x, wid->y); ttk_text(menubar->win->srf, font->f, (wid->x - text_width)/2, (wid->y - text_height)/2, ttk_ap_getx("menu-bar.fg")->color, time); } ttk_clockdata ttk_new_clockdata(void) { ttk_clockdata * cd = (ttk_clockdata *)calloc(1, sizeof(ttk_clockdata)); time_t rawtime = time(); struct tm * t = localtime(&rawtime); cd->time = &t; return cd; } void ttk_destroy_clock_widget(TWidget * wid) { ttk_free_widget(wid); }