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