]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
order of panel items : position of each object is update by layering engine (area)
[chaz/tint2] / src / clock / clock.c
1 /**************************************************************************
2 *
3 * Tint2 : clock
4 *
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
6 *
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.
10 *
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 **************************************************************************/
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <cairo.h>
23 #include <cairo-xlib.h>
24 #include <pango/pangocairo.h>
25 #include <stdlib.h>
26
27 #include "window.h"
28 #include "server.h"
29 #include "panel.h"
30 #include "clock.h"
31 #include "timer.h"
32 #include "common.h"
33
34
35 char *time1_format;
36 char *time1_timezone;
37 char *time2_format;
38 char *time2_timezone;
39 char *time_tooltip_format;
40 char *time_tooltip_timezone;
41 char *clock_lclick_command;
42 char *clock_rclick_command;
43 struct timeval time_clock;
44 PangoFontDescription *time1_font_desc;
45 PangoFontDescription *time2_font_desc;
46 static char buf_time[40];
47 static char buf_date[40];
48 static char buf_tooltip[40];
49 int clock_enabled;
50 static timeout* clock_timeout;
51
52
53 void default_clock()
54 {
55 clock_enabled = 0;
56 clock_timeout = 0;
57 time1_format = 0;
58 time1_timezone = 0;
59 time2_format = 0;
60 time2_timezone = 0;
61 time_tooltip_format = 0;
62 time_tooltip_timezone = 0;
63 clock_lclick_command = 0;
64 clock_rclick_command = 0;
65 time1_font_desc = 0;
66 time2_font_desc = 0;
67 }
68
69 void cleanup_clock()
70 {
71 if (time1_font_desc) pango_font_description_free(time1_font_desc);
72 if (time2_font_desc) pango_font_description_free(time2_font_desc);
73 if (time1_format) g_free(time1_format);
74 if (time2_format) g_free(time2_format);
75 if (time_tooltip_format) g_free(time_tooltip_format);
76 if (time1_timezone) g_free(time1_timezone);
77 if (time2_timezone) g_free(time2_timezone);
78 if (time_tooltip_timezone) g_free(time_tooltip_timezone);
79 if (clock_lclick_command) g_free(clock_lclick_command);
80 if (clock_rclick_command) g_free(clock_rclick_command);
81 }
82
83
84 void update_clocks_sec(void* arg)
85 {
86 gettimeofday(&time_clock, 0);
87 int i;
88 if (time1_format) {
89 for (i=0 ; i < nb_panel ; i++)
90 panel1[i].clock.area.resize = 1;
91 }
92 panel_refresh = 1;
93 }
94
95 void update_clocks_min(void* arg)
96 {
97 // remember old_sec because after suspend/hibernate the clock should be updated directly, and not
98 // on next minute change
99 time_t old_sec = time_clock.tv_sec;
100 gettimeofday(&time_clock, 0);
101 if (time_clock.tv_sec % 60 == 0 || time_clock.tv_sec - old_sec > 60) {
102 int i;
103 if (time1_format) {
104 for (i=0 ; i < nb_panel ; i++)
105 panel1[i].clock.area.resize = 1;
106 }
107 panel_refresh = 1;
108 }
109 }
110
111 struct tm* clock_gettime_for_tz(const char* timezone) {
112 if (timezone) {
113 const char* old_tz = getenv("TZ");
114 setenv("TZ", timezone, 1);
115 struct tm* result = localtime(&time_clock.tv_sec);
116 if (old_tz) setenv("TZ", old_tz, 1);
117 else unsetenv("TZ");
118 return result;
119 }
120 else return localtime(&time_clock.tv_sec);
121 }
122
123 const char* clock_get_tooltip(void* obj)
124 {
125 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
126 return buf_tooltip;
127 }
128
129
130 void init_clock()
131 {
132 if(time1_format && clock_timeout==0) {
133 if (strchr(time1_format, 'S') || strchr(time1_format, 'T') || strchr(time1_format, 'r'))
134 clock_timeout = add_timeout(10, 1000, update_clocks_sec, 0);
135 else
136 clock_timeout = add_timeout(10, 1000, update_clocks_min, 0);
137 }
138 }
139
140
141 void init_clock_panel(void *p)
142 {
143 Panel *panel =(Panel*)p;
144 Clock *clock = &panel->clock;
145 int time_height, time_height_ink, date_height, date_height_ink;
146
147 clock->area.parent = p;
148 clock->area.panel = p;
149 clock->area._draw_foreground = draw_clock;
150 clock->area.size_mode = SIZE_BY_CONTENT;
151 clock->area._resize = resize_clock;
152 clock->area.resize = 1;
153 clock->area.redraw = 1;
154 clock->area.on_screen = 1;
155
156 strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone));
157 get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
158 if (time2_format) {
159 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
160 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
161 }
162
163 if (panel_horizontal) {
164 // panel horizonal => fixed height and posy
165 clock->area.posy = panel->area.bg->border.width + panel->area.paddingy;
166 clock->area.height = panel->area.height - (2 * clock->area.posy);
167 }
168 else {
169 // panel vertical => fixed width, height, posy and posx
170 // clock->area.posy = panel->area.bg->border.width + panel->area.paddingxlr;
171 // clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
172 clock->area.posx = panel->area.bg->border.width + panel->area.paddingy;
173 clock->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
174 }
175
176 clock->time1_posy = (clock->area.height - time_height) / 2;
177 if (time2_format) {
178 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
179 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
180
181 clock->time1_posy -= ((date_height_ink + 2) / 2);
182 clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
183 }
184
185 if (time_tooltip_format) {
186 clock->area._get_tooltip_text = clock_get_tooltip;
187 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
188 }
189 }
190
191
192 void draw_clock (void *obj, cairo_t *c)
193 {
194 Clock *clock = obj;
195 PangoLayout *layout;
196
197 layout = pango_cairo_create_layout (c);
198
199 // draw layout
200 pango_layout_set_font_description (layout, time1_font_desc);
201 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
202 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
203 pango_layout_set_text (layout, buf_time, strlen(buf_time));
204
205 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
206
207 pango_cairo_update_layout (c, layout);
208 cairo_move_to (c, 0, clock->time1_posy);
209 pango_cairo_show_layout (c, layout);
210
211 if (time2_format) {
212 pango_layout_set_font_description (layout, time2_font_desc);
213 pango_layout_set_indent(layout, 0);
214 pango_layout_set_text (layout, buf_date, strlen(buf_date));
215 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
216
217 pango_cairo_update_layout (c, layout);
218 cairo_move_to (c, 0, clock->time2_posy);
219 pango_cairo_show_layout (c, layout);
220 }
221
222 g_object_unref (layout);
223 }
224
225
226 int resize_clock (void *obj)
227 {
228 Clock *clock = obj;
229 PangoLayout *layout;
230 int time_width, date_width, new_width, ret = 0;
231
232 clock->area.redraw = 1;
233 time_width = date_width = 0;
234 strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone));
235 if (time2_format)
236 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
237
238 // vertical panel doen't adjust width
239 if (!panel_horizontal) return ret;
240
241 //printf(" resize_clock\n");
242 cairo_surface_t *cs;
243 cairo_t *c;
244 Pixmap pmap;
245 pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
246
247 cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, clock->area.width, clock->area.height);
248 c = cairo_create (cs);
249 layout = pango_cairo_create_layout (c);
250
251 // check width
252 pango_layout_set_font_description (layout, time1_font_desc);
253 pango_layout_set_indent(layout, 0);
254 pango_layout_set_text (layout, buf_time, strlen(buf_time));
255 pango_layout_get_pixel_size (layout, &time_width, NULL);
256 if (time2_format) {
257 pango_layout_set_font_description (layout, time2_font_desc);
258 pango_layout_set_indent(layout, 0);
259 pango_layout_set_text (layout, buf_date, strlen(buf_date));
260 pango_layout_get_pixel_size (layout, &date_width, NULL);
261 }
262
263 if (time_width > date_width) new_width = time_width;
264 else new_width = date_width;
265 new_width += (2*clock->area.paddingxlr) + (2*clock->area.bg->border.width);
266
267 if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
268 // resize clock
269 // we try to limit the number of resize
270 // printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
271 clock->area.width = new_width + 1;
272
273 // resize other objects on panel
274 ret = 1;
275 panel_refresh = 1;
276 }
277
278 g_object_unref (layout);
279 cairo_destroy (c);
280 cairo_surface_destroy (cs);
281 XFreePixmap (server.dsp, pmap);
282 return ret;
283 }
284
285
286 void clock_action(int button)
287 {
288 char *command = 0;
289 switch (button) {
290 case 1:
291 command = clock_lclick_command;
292 break;
293 case 3:
294 command = clock_rclick_command;
295 break;
296 }
297 tint_exec(command);
298 }
299
This page took 0.041748 seconds and 4 git commands to generate.