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