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