]> Dogcows Code - chaz/tint2/blobdiff - src/clock/clock.c
fixed issue 98
[chaz/tint2] / src / clock / clock.c
index 720d4980dd2955e74161e3bc2d35087fac5ba458..01b1991e247d090e85015c720403ef4b9cedd086 100644 (file)
@@ -22,6 +22,7 @@
 #include <cairo.h>
 #include <cairo-xlib.h>
 #include <pango/pangocairo.h>
+#include <unistd.h>
 
 #include "window.h"
 #include "server.h"
 #include "clock.h"
 
 
-char *time1_format = 0;
-char *time2_format = 0;
+char *time1_format;
+char *time2_format;
+char *clock_lclick_command;
+char *clock_rclick_command;
 struct timeval time_clock;
 int  time_precision;
 PangoFontDescription *time1_font_desc;
@@ -41,54 +44,68 @@ static char buf_time[40];
 static char buf_date[40];
 
 
-void init_clock(Clock *clock, Area *parent)
+void init_precision()
 {
-   Panel *panel = (Panel *)parent;
-   int time_height, time_height_ink, date_height, date_height_ink;
+       if (!time1_format) time_precision = 60;
+       else if (strchr(time1_format, 'S')) time_precision = 1;
+       else if (strchr(time1_format, 'T')) time_precision = 1;
+       else if (strchr(time1_format, 'r')) time_precision = 1;
+       else time_precision = 60;
+}
 
-       clock->area.parent = parent;
-       clock->area.panel = panel;
-   if (!time1_format) return;
 
-   clock->area._draw_foreground = draw_foreground_clock;
-   clock->area._resize = resize_clock;
+void init_clock()
+{
+   Panel *panel;
+   Clock *clock;
+   int i, time_height, time_height_ink, date_height, date_height_ink;
 
-   if (strchr(time1_format, 'S') == NULL) time_precision = 60;
-   else time_precision = 1;
+       init_precision();
+       // update clock to force update (-time_precision)
+       struct timeval stv;
+       gettimeofday(&stv, 0);
+       time_clock.tv_sec = stv.tv_sec - time_precision;
+       time_clock.tv_sec -= time_clock.tv_sec % time_precision;
 
-   // update clock to force update (-time_precision)
-   struct timeval stv;
-   gettimeofday(&stv, 0);
-   time_clock.tv_sec = stv.tv_sec - time_precision;
-   time_clock.tv_sec -= time_clock.tv_sec % time_precision;
+       for (i=0 ; i < nb_panel ; i++) {
+          panel = &panel1[i];
+          clock = &panel->clock;
 
-   clock->area.posy = parent->pix.border.width + parent->paddingy;
-   clock->area.height = parent->height - (2 * clock->area.posy);
-   clock->area.resize = 1;
-   clock->area.redraw = 1;
+               clock->area.parent = panel;
+               clock->area.panel = panel;
+               clock->area._draw_foreground = draw_clock;
+               clock->area._resize = resize_clock;
 
-   strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
-   if (time2_format)
-      strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
+               if (!clock->area.on_screen) continue;
 
-   get_text_size(time1_font_desc, &time_height_ink, &time_height, parent->height, buf_time, strlen(buf_time));
-   clock->time1_posy = (clock->area.height - time_height) / 2;
+               clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
+               clock->area.height = panel->area.height - (2 * clock->area.posy);
+               clock->area.resize = 1;
+               clock->area.redraw = 1;
 
-   if (time2_format) {
-      get_text_size(time2_font_desc, &date_height_ink, &date_height, parent->height, buf_date, strlen(buf_date));
+               strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
+               if (time2_format)
+                       strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
 
-      clock->time1_posy -= ((date_height_ink + 2) / 2);
-      clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
-   }
+               get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
+               clock->time1_posy = (clock->area.height - time_height) / 2;
+
+               if (time2_format) {
+                       get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
+
+                       clock->time1_posy -= ((date_height_ink + 2) / 2);
+                       clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
+               }
+       }
 }
 
 
-void draw_foreground_clock (void *obj, cairo_t *c, int active)
+void draw_clock (void *obj, cairo_t *c, int active)
 {
    Clock *clock = obj;
    PangoLayout *layout;
 
-   //printf("  draw_foreground_clock : %s en (%d, %d)\n", buf_time, clock->area.posx, clock->area.width);
+   //printf("  draw_clock : %s en (%d, %d)\n", buf_time, clock->area.posx, clock->area.width);
    layout = pango_cairo_create_layout (c);
 
    // draw layout
@@ -157,19 +174,19 @@ void resize_clock (void *obj)
    else new_width = date_width;
    new_width += (2*clock->area.paddingxlr) + (2*clock->area.pix.border.width);
 
-   if (new_width > clock->area.width || new_width < (clock->area.width-3)) {
-      int i;
+   if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
       Panel *panel = ((Area*)obj)->panel;
 
-      //printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
+      printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
       // resize clock
-      clock->area.width = new_width;
+      // we try to limit the number of resize
+      clock->area.width = new_width + 1;
       clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
 
       // resize other objects on panel
-               for (i=0 ; i < nb_panel ; i++) {
-                       panel1[i].area.resize = 1;
-               }
+               panel->area.resize = 1;
+               panel->battery.area.resize = 1;
+               systray.area.resize = 1;
                panel_refresh = 1;
    }
 
@@ -179,3 +196,25 @@ void resize_clock (void *obj)
    XFreePixmap (server.dsp, pmap);
 }
 
+
+void clock_action(int button)
+{
+       char *command = 0;
+       switch (button) {
+               case 1:
+               command = clock_lclick_command;
+               break;
+               case 3:
+               command = clock_rclick_command;
+               break;
+       }
+       if (command) {
+               pid_t pid;
+               pid = fork();
+               if (pid == 0) {
+                       execl("/bin/sh", "/bin/sh", "-c", command, NULL);
+                       _exit(0);
+               }
+       }
+}
+
This page took 0.02613 seconds and 4 git commands to generate.