]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
*add* clock tooltip finished, see issue 115
[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 <unistd.h>
26 #include <signal.h>
27
28 #include "window.h"
29 #include "server.h"
30 #include "area.h"
31 #include "panel.h"
32 #include "taskbar.h"
33 #include "clock.h"
34 #include "timer.h"
35
36
37 char *time1_format=0;
38 char *time2_format=0;
39 char *time_tooltip_format=0;
40 char *clock_lclick_command=0;
41 char *clock_rclick_command=0;
42 struct timeval time_clock;
43 PangoFontDescription *time1_font_desc=0;
44 PangoFontDescription *time2_font_desc=0;
45 static char buf_time[40];
46 static char buf_date[40];
47 static char buf_tooltip[40];
48 int clock_enabled;
49
50
51 void update_clocks()
52 {
53 gettimeofday(&time_clock, 0);
54 int i;
55 if (time1_format) {
56 for (i=0 ; i < nb_panel ; i++)
57 panel1[i].clock.area.resize = 1;
58 }
59 panel_refresh = 1;
60 }
61
62
63 const char* clock_get_tooltip(void* obj)
64 {
65 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, localtime(&time_clock.tv_sec));
66 return buf_tooltip;
67 }
68
69
70 void init_clock()
71 {
72 if(time1_format) {
73 if (strchr(time1_format, 'S') || strchr(time1_format, 'T') || strchr(time1_format, 'r'))
74 install_timer(0, 1000000, 1, 0, update_clocks);
75 else install_timer(0, 1000000, 60, 0, update_clocks);
76 }
77 }
78
79
80 void init_clock_panel(void *p)
81 {
82 Panel *panel =(Panel*)p;
83 Clock *clock = &panel->clock;
84 int time_height, time_height_ink, date_height, date_height_ink;
85
86 clock->area.parent = p;
87 clock->area.panel = p;
88 clock->area._draw_foreground = draw_clock;
89 clock->area._resize = resize_clock;
90 clock->area.resize = 1;
91 clock->area.redraw = 1;
92 clock->area.on_screen = 1;
93
94 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
95 get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
96 if (time2_format) {
97 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
98 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
99 }
100
101 if (panel_horizontal) {
102 // panel horizonal => fixed height and posy
103 clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
104 clock->area.height = panel->area.height - (2 * clock->area.posy);
105 }
106 else {
107 // panel vertical => fixed width, height, posy and posx
108 clock->area.posy = panel->area.pix.border.width + panel->area.paddingxlr;
109 clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
110 clock->area.posx = panel->area.pix.border.width + panel->area.paddingy;
111 clock->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
112 }
113
114 clock->time1_posy = (clock->area.height - time_height) / 2;
115 if (time2_format) {
116 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
117 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
118
119 clock->time1_posy -= ((date_height_ink + 2) / 2);
120 clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
121 }
122
123 if (time_tooltip_format) {
124 clock->area._get_tooltip_text = clock_get_tooltip;
125 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, localtime(&time_clock.tv_sec));
126 }
127 }
128
129
130 void cleanup_clock()
131 {
132 clock_enabled = 0;
133 if (time1_font_desc)
134 pango_font_description_free(time1_font_desc);
135 if (time2_font_desc)
136 pango_font_description_free(time2_font_desc);
137 if (time1_format)
138 g_free(time1_format);
139 if (time2_format)
140 g_free(time2_format);
141 if (time_tooltip_format)
142 g_free(time_tooltip_format);
143 if (clock_lclick_command)
144 g_free(clock_lclick_command);
145 if (clock_rclick_command)
146 g_free(clock_rclick_command);
147 time1_font_desc = time2_font_desc = 0;
148 time1_format = time2_format = 0;
149 clock_lclick_command = clock_rclick_command = 0;
150 }
151
152
153 void draw_clock (void *obj, cairo_t *c, int active)
154 {
155 Clock *clock = obj;
156 PangoLayout *layout;
157
158 layout = pango_cairo_create_layout (c);
159
160 // draw layout
161 pango_layout_set_font_description (layout, time1_font_desc);
162 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
163 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
164 pango_layout_set_text (layout, buf_time, strlen(buf_time));
165
166 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
167
168 pango_cairo_update_layout (c, layout);
169 cairo_move_to (c, 0, clock->time1_posy);
170 pango_cairo_show_layout (c, layout);
171
172 if (time2_format) {
173 pango_layout_set_font_description (layout, time2_font_desc);
174 pango_layout_set_indent(layout, 0);
175 pango_layout_set_text (layout, buf_date, strlen(buf_date));
176 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
177
178 pango_cairo_update_layout (c, layout);
179 cairo_move_to (c, 0, clock->time2_posy);
180 pango_cairo_show_layout (c, layout);
181 }
182
183 g_object_unref (layout);
184 }
185
186
187 void resize_clock (void *obj)
188 {
189 Clock *clock = obj;
190 PangoLayout *layout;
191 int time_width, date_width, new_width;
192
193 clock->area.redraw = 1;
194 time_width = date_width = 0;
195 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
196 if (time2_format)
197 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
198
199 // vertical panel doen't adjust width
200 if (!panel_horizontal) return;
201
202 //printf(" resize_clock\n");
203 cairo_surface_t *cs;
204 cairo_t *c;
205 Pixmap pmap;
206 pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
207
208 cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, clock->area.width, clock->area.height);
209 c = cairo_create (cs);
210 layout = pango_cairo_create_layout (c);
211
212 // check width
213 pango_layout_set_font_description (layout, time1_font_desc);
214 pango_layout_set_indent(layout, 0);
215 pango_layout_set_text (layout, buf_time, strlen(buf_time));
216 pango_layout_get_pixel_size (layout, &time_width, NULL);
217 if (time2_format) {
218 pango_layout_set_font_description (layout, time2_font_desc);
219 pango_layout_set_indent(layout, 0);
220 pango_layout_set_text (layout, buf_date, strlen(buf_date));
221 pango_layout_get_pixel_size (layout, &date_width, NULL);
222 }
223
224 if (time_width > date_width) new_width = time_width;
225 else new_width = date_width;
226 new_width += (2*clock->area.paddingxlr) + (2*clock->area.pix.border.width);
227
228 Panel *panel = ((Area*)obj)->panel;
229 clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
230
231 if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
232 // resize clock
233 // we try to limit the number of resize
234 // printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
235 clock->area.width = new_width + 1;
236
237 // resize other objects on panel
238 panel->area.resize = 1;
239 #ifdef ENABLE_BATTERY
240 panel->battery.area.resize = 1;
241 #endif
242 systray.area.resize = 1;
243 panel_refresh = 1;
244 }
245
246 g_object_unref (layout);
247 cairo_destroy (c);
248 cairo_surface_destroy (cs);
249 XFreePixmap (server.dsp, pmap);
250 }
251
252
253 void clock_action(int button)
254 {
255 char *command = 0;
256 switch (button) {
257 case 1:
258 command = clock_lclick_command;
259 break;
260 case 3:
261 command = clock_rclick_command;
262 break;
263 }
264 if (command) {
265 pid_t pid;
266 sigset_t sigset;
267 sigprocmask(SIG_SETMASK, &sigset, 0);
268 sigprocmask(SIG_UNBLOCK, &sigset, 0);
269 pid = fork();
270 sigprocmask(SIG_BLOCK, &sigset, 0);
271 if (pid == 0) {
272 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
273 _exit(0);
274 }
275 }
276 }
277
This page took 0.04722 seconds and 5 git commands to generate.