]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
fixed issue 48
[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
26 #include "window.h"
27 #include "server.h"
28 #include "taskbar.h"
29 #include "panel.h"
30 #include "area.h"
31 #include "clock.h"
32
33
34 char *time1_format = 0;
35 char *time2_format = 0;
36 struct timeval time_clock;
37 int time_precision;
38 PangoFontDescription *time1_font_desc;
39 PangoFontDescription *time2_font_desc;
40 static char buf_time[40];
41 static char buf_date[40];
42
43
44 void init_clock()
45 {
46 Panel *panel;
47 Clock *clock;
48 int i, time_height, time_height_ink, date_height, date_height_ink;
49
50 for (i=0 ; i < nb_panel ; i++) {
51 panel = &panel1[i];
52 clock = &panel->clock;
53
54 clock->area.parent = panel;
55 clock->area.panel = panel;
56 if (!clock->area.visible) return;
57
58 clock->area._draw_foreground = draw_foreground_clock;
59 clock->area._resize = resize_clock;
60
61 if (strchr(time1_format, 'S') == NULL) time_precision = 60;
62 else time_precision = 1;
63
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 clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
71 clock->area.height = panel->area.height - (2 * clock->area.posy);
72 clock->area.resize = 1;
73 clock->area.redraw = 1;
74
75 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
76 if (time2_format)
77 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
78
79 get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
80 clock->time1_posy = (clock->area.height - time_height) / 2;
81
82 if (time2_format) {
83 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
84
85 clock->time1_posy -= ((date_height_ink + 2) / 2);
86 clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
87 }
88 }
89 }
90
91
92 void draw_foreground_clock (void *obj, cairo_t *c, int active)
93 {
94 Clock *clock = obj;
95 PangoLayout *layout;
96
97 //printf(" draw_foreground_clock : %s en (%d, %d)\n", buf_time, clock->area.posx, clock->area.width);
98 layout = pango_cairo_create_layout (c);
99
100 // draw layout
101 pango_layout_set_font_description (layout, time1_font_desc);
102 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
103 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
104 pango_layout_set_text (layout, buf_time, strlen(buf_time));
105
106 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
107
108 pango_cairo_update_layout (c, layout);
109 cairo_move_to (c, 0, clock->time1_posy);
110 pango_cairo_show_layout (c, layout);
111
112 if (time2_format) {
113 pango_layout_set_font_description (layout, time2_font_desc);
114 pango_layout_set_indent(layout, 0);
115 pango_layout_set_text (layout, buf_date, strlen(buf_date));
116 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
117
118 pango_cairo_update_layout (c, layout);
119 cairo_move_to (c, 0, clock->time2_posy);
120 pango_cairo_show_layout (c, layout);
121 }
122
123 g_object_unref (layout);
124 }
125
126
127 void resize_clock (void *obj)
128 {
129 Clock *clock = obj;
130 PangoLayout *layout;
131 int time_width, date_width, new_width;
132
133 time_width = date_width = 0;
134 clock->area.redraw = 1;
135
136 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
137 if (time2_format)
138 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
139
140 //printf(" resize_clock\n");
141 cairo_surface_t *cs;
142 cairo_t *c;
143 Pixmap pmap;
144 pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
145
146 cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, clock->area.width, clock->area.height);
147 c = cairo_create (cs);
148 layout = pango_cairo_create_layout (c);
149
150 // check width
151 pango_layout_set_font_description (layout, time1_font_desc);
152 pango_layout_set_indent(layout, 0);
153 pango_layout_set_text (layout, buf_time, strlen(buf_time));
154 pango_layout_get_pixel_size (layout, &time_width, NULL);
155 if (time2_format) {
156 pango_layout_set_font_description (layout, time2_font_desc);
157 pango_layout_set_indent(layout, 0);
158 pango_layout_set_text (layout, buf_date, strlen(buf_date));
159 pango_layout_get_pixel_size (layout, &date_width, NULL);
160 }
161
162 if (time_width > date_width) new_width = time_width;
163 else new_width = date_width;
164 new_width += (2*clock->area.paddingxlr) + (2*clock->area.pix.border.width);
165
166 if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
167 int i;
168 Panel *panel = ((Area*)obj)->panel;
169
170 printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
171 // resize clock
172 // we try to limit the number of resize
173 clock->area.width = new_width + 1;
174 clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
175
176 // resize other objects on panel
177 for (i=0 ; i < nb_panel ; i++) {
178 panel1[i].area.resize = 1;
179 }
180 panel_refresh = 1;
181 }
182
183 g_object_unref (layout);
184 cairo_destroy (c);
185 cairo_surface_destroy (cs);
186 XFreePixmap (server.dsp, pmap);
187 }
188
This page took 0.04476 seconds and 5 git commands to generate.