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