]>
Dogcows Code - chaz/tint2/blob - src/clock/clock.c
1 /**************************************************************************
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
23 #include <cairo-xlib.h>
24 #include <pango/pangocairo.h>
37 char *clock_lclick_command
;
38 char *clock_rclick_command
;
39 struct timeval time_clock
;
41 PangoFontDescription
*time1_font_desc
;
42 PangoFontDescription
*time2_font_desc
;
43 static char buf_time
[40];
44 static char buf_date
[40];
49 if (!time1_format
) time_precision
= 60;
50 else if (strchr(time1_format
, 'S')) time_precision
= 1;
51 else if (strchr(time1_format
, 'T')) time_precision
= 1;
52 else if (strchr(time1_format
, 'r')) time_precision
= 1;
53 else time_precision
= 60;
61 // update clock to force update (-time_precision)
63 gettimeofday(&stv
, 0);
64 time_clock
.tv_sec
= stv
.tv_sec
- time_precision
;
65 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
69 void init_clock_panel(void *p
)
71 Panel
*panel
=(Panel
*)p
;
72 Clock
*clock
= &panel
->clock
;
73 int time_height
, time_height_ink
, date_height
, date_height_ink
;
75 clock
->area
.parent
= p
;
76 clock
->area
.panel
= p
;
77 clock
->area
._draw_foreground
= draw_clock
;
78 clock
->area
._resize
= resize_clock
;
79 clock
->area
.resize
= 1;
80 clock
->area
.redraw
= 1;
82 strftime(buf_time
, sizeof(buf_time
), time1_format
, localtime(&time_clock
.tv_sec
));
83 get_text_size(time1_font_desc
, &time_height_ink
, &time_height
, panel
->area
.height
, buf_time
, strlen(buf_time
));
85 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
86 get_text_size(time2_font_desc
, &date_height_ink
, &date_height
, panel
->area
.height
, buf_date
, strlen(buf_date
));
89 if (panel_horizontal
) {
90 // panel horizonal => fixed height and posy
91 clock
->area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
92 clock
->area
.height
= panel
->area
.height
- (2 * clock
->area
.posy
);
95 // panel vertical => fixed width, height, posy and posx
96 clock
->area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingxlr
;
97 clock
->area
.height
= (2 * clock
->area
.paddingxlr
) + (time_height
+ date_height
);
98 clock
->area
.posx
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
99 clock
->area
.width
= panel
->area
.width
- (2 * panel
->area
.pix
.border
.width
) - (2 * panel
->area
.paddingy
);
102 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
104 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
105 get_text_size(time2_font_desc
, &date_height_ink
, &date_height
, panel
->area
.height
, buf_date
, strlen(buf_date
));
107 clock
->time1_posy
-= ((date_height_ink
+ 2) / 2);
108 clock
->time2_posy
= clock
->time1_posy
+ time_height
+ 2 - (time_height
- time_height_ink
)/2 - (date_height
- date_height_ink
)/2;
113 void draw_clock (void *obj
, cairo_t
*c
, int active
)
118 layout
= pango_cairo_create_layout (c
);
121 pango_layout_set_font_description (layout
, time1_font_desc
);
122 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
123 pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
124 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
126 cairo_set_source_rgba (c
, clock
->font
.color
[0], clock
->font
.color
[1], clock
->font
.color
[2], clock
->font
.alpha
);
128 pango_cairo_update_layout (c
, layout
);
129 cairo_move_to (c
, 0, clock
->time1_posy
);
130 pango_cairo_show_layout (c
, layout
);
133 pango_layout_set_font_description (layout
, time2_font_desc
);
134 pango_layout_set_indent(layout
, 0);
135 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
136 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
138 pango_cairo_update_layout (c
, layout
);
139 cairo_move_to (c
, 0, clock
->time2_posy
);
140 pango_cairo_show_layout (c
, layout
);
143 g_object_unref (layout
);
147 void resize_clock (void *obj
)
151 int time_width
, date_width
, new_width
;
153 clock
->area
.redraw
= 1;
154 time_width
= date_width
= 0;
155 strftime(buf_time
, sizeof(buf_time
), time1_format
, localtime(&time_clock
.tv_sec
));
157 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
159 // vertical panel doen't adjust width
160 if (!panel_horizontal
) return;
162 //printf(" resize_clock\n");
166 pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, clock
->area
.width
, clock
->area
.height
, server
.depth
);
168 cs
= cairo_xlib_surface_create (server
.dsp
, pmap
, server
.visual
, clock
->area
.width
, clock
->area
.height
);
169 c
= cairo_create (cs
);
170 layout
= pango_cairo_create_layout (c
);
173 pango_layout_set_font_description (layout
, time1_font_desc
);
174 pango_layout_set_indent(layout
, 0);
175 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
176 pango_layout_get_pixel_size (layout
, &time_width
, NULL
);
178 pango_layout_set_font_description (layout
, time2_font_desc
);
179 pango_layout_set_indent(layout
, 0);
180 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
181 pango_layout_get_pixel_size (layout
, &date_width
, NULL
);
184 if (time_width
> date_width
) new_width
= time_width
;
185 else new_width
= date_width
;
186 new_width
+= (2*clock
->area
.paddingxlr
) + (2*clock
->area
.pix
.border
.width
);
188 Panel
*panel
= ((Area
*)obj
)->panel
;
189 clock
->area
.posx
= panel
->area
.width
- clock
->area
.width
- panel
->area
.paddingxlr
- panel
->area
.pix
.border
.width
;
191 if (new_width
> clock
->area
.width
|| new_width
< (clock
->area
.width
-6)) {
193 // we try to limit the number of resize
194 // printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
195 clock
->area
.width
= new_width
+ 1;
197 // resize other objects on panel
198 panel
->area
.resize
= 1;
199 #ifdef ENABLE_BATTERY
200 panel
->battery
.area
.resize
= 1;
202 systray
.area
.resize
= 1;
206 g_object_unref (layout
);
208 cairo_surface_destroy (cs
);
209 XFreePixmap (server
.dsp
, pmap
);
213 void clock_action(int button
)
218 command
= clock_lclick_command
;
221 command
= clock_rclick_command
;
228 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
This page took 0.042783 seconds and 4 git commands to generate.