]>
Dogcows Code - chaz/tint2/blob - clock.c
a29eae545f465cd08c60b8a973b1d13b72c4ed75
1 /**************************************************************************
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
23 #include <cairo-xlib.h>
24 #include <pango/pangocairo.h>
37 char *clock_lclick_command
;
38 char *clock_rclick_command
;
39 struct timeval time_clock
;
41 PangoFontDescription
*time1_font_desc
;
42 PangoFontDescription
*time2_font_desc
;
43 static char buf_time
[40];
44 static char buf_date
[40];
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;
61 int i
, time_height
, time_height_ink
, date_height
, date_height_ink
;
64 // update clock to force update (-time_precision)
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
;
70 for (i
=0 ; i
< nb_panel
; i
++) {
72 clock
= &panel
->clock
;
74 if (!clock
->area
.on_screen
) continue;
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;
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
));
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
));
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
);
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
);
103 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
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
));
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;
115 void draw_clock (void *obj
, cairo_t
*c
, int active
)
120 layout
= pango_cairo_create_layout (c
);
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
));
128 cairo_set_source_rgba (c
, clock
->font
.color
[0], clock
->font
.color
[1], clock
->font
.color
[2], clock
->font
.alpha
);
130 pango_cairo_update_layout (c
, layout
);
131 cairo_move_to (c
, 0, clock
->time1_posy
);
132 pango_cairo_show_layout (c
, layout
);
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
);
140 pango_cairo_update_layout (c
, layout
);
141 cairo_move_to (c
, 0, clock
->time2_posy
);
142 pango_cairo_show_layout (c
, layout
);
145 g_object_unref (layout
);
149 void resize_clock (void *obj
)
153 int time_width
, date_width
, new_width
;
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
));
159 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
161 // vertical panel doen't adjust width
162 if (!panel_horizontal
) return;
164 //printf(" resize_clock\n");
168 pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, clock
->area
.width
, clock
->area
.height
, server
.depth
);
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
);
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
);
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
);
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
);
190 if (new_width
> clock
->area
.width
|| new_width
< (clock
->area
.width
-6)) {
191 Panel
*panel
= ((Area
*)obj
)->panel
;
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
;
199 // resize other objects on panel
200 panel
->area
.resize
= 1;
201 #ifdef ENABLE_BATTERY
202 panel
->battery
.area
.resize
= 1;
204 systray
.area
.resize
= 1;
208 g_object_unref (layout
);
210 cairo_surface_destroy (cs
);
211 XFreePixmap (server
.dsp
, pmap
);
215 void clock_action(int button
)
220 command
= clock_lclick_command
;
223 command
= clock_rclick_command
;
230 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
This page took 0.087162 seconds and 3 git commands to generate.