]>
Dogcows Code - chaz/tint2/blob - src/clock/clock.c
eaaf69cb83e8a3c493f39151f4d5778c8be72899
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];
51 int i
, time_height
, time_height_ink
, date_height
, date_height_ink
;
53 for (i
=0 ; i
< nb_panel
; i
++) {
55 clock
= &panel
->clock
;
57 clock
->area
.parent
= panel
;
58 clock
->area
.panel
= panel
;
59 clock
->area
._draw_foreground
= draw_clock
;
60 clock
->area
._resize
= resize_clock
;
62 if (!clock
->area
.on_screen
) continue;
64 if (strchr(time1_format
, 'S')) time_precision
= 1;
65 else if (strchr(time1_format
, 'T')) time_precision
= 1;
66 else if (strchr(time1_format
, 'r')) time_precision
= 1;
67 else time_precision
= 60;
69 // update clock to force update (-time_precision)
71 gettimeofday(&stv
, 0);
72 time_clock
.tv_sec
= stv
.tv_sec
- time_precision
;
73 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
75 clock
->area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
76 clock
->area
.height
= panel
->area
.height
- (2 * clock
->area
.posy
);
77 clock
->area
.resize
= 1;
78 clock
->area
.redraw
= 1;
80 strftime(buf_time
, sizeof(buf_time
), time1_format
, localtime(&time_clock
.tv_sec
));
82 strftime(buf_date
, sizeof(buf_date
), time2_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 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
88 get_text_size(time2_font_desc
, &date_height_ink
, &date_height
, panel
->area
.height
, buf_date
, strlen(buf_date
));
90 clock
->time1_posy
-= ((date_height_ink
+ 2) / 2);
91 clock
->time2_posy
= clock
->time1_posy
+ time_height
+ 2 - (time_height
- time_height_ink
)/2 - (date_height
- date_height_ink
)/2;
97 void draw_clock (void *obj
, cairo_t
*c
, int active
)
102 //printf(" draw_clock : %s en (%d, %d)\n", buf_time, clock->area.posx, clock->area.width);
103 layout
= pango_cairo_create_layout (c
);
106 pango_layout_set_font_description (layout
, time1_font_desc
);
107 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
108 pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
109 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
111 cairo_set_source_rgba (c
, clock
->font
.color
[0], clock
->font
.color
[1], clock
->font
.color
[2], clock
->font
.alpha
);
113 pango_cairo_update_layout (c
, layout
);
114 cairo_move_to (c
, 0, clock
->time1_posy
);
115 pango_cairo_show_layout (c
, layout
);
118 pango_layout_set_font_description (layout
, time2_font_desc
);
119 pango_layout_set_indent(layout
, 0);
120 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
121 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
123 pango_cairo_update_layout (c
, layout
);
124 cairo_move_to (c
, 0, clock
->time2_posy
);
125 pango_cairo_show_layout (c
, layout
);
128 g_object_unref (layout
);
132 void resize_clock (void *obj
)
136 int time_width
, date_width
, new_width
;
138 time_width
= date_width
= 0;
139 clock
->area
.redraw
= 1;
141 strftime(buf_time
, sizeof(buf_time
), time1_format
, localtime(&time_clock
.tv_sec
));
143 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
145 //printf(" resize_clock\n");
149 pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, clock
->area
.width
, clock
->area
.height
, server
.depth
);
151 cs
= cairo_xlib_surface_create (server
.dsp
, pmap
, server
.visual
, clock
->area
.width
, clock
->area
.height
);
152 c
= cairo_create (cs
);
153 layout
= pango_cairo_create_layout (c
);
156 pango_layout_set_font_description (layout
, time1_font_desc
);
157 pango_layout_set_indent(layout
, 0);
158 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
159 pango_layout_get_pixel_size (layout
, &time_width
, NULL
);
161 pango_layout_set_font_description (layout
, time2_font_desc
);
162 pango_layout_set_indent(layout
, 0);
163 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
164 pango_layout_get_pixel_size (layout
, &date_width
, NULL
);
167 if (time_width
> date_width
) new_width
= time_width
;
168 else new_width
= date_width
;
169 new_width
+= (2*clock
->area
.paddingxlr
) + (2*clock
->area
.pix
.border
.width
);
171 if (new_width
> clock
->area
.width
|| new_width
< (clock
->area
.width
-6)) {
173 Panel
*panel
= ((Area
*)obj
)->panel
;
175 printf("clock_width %d, new_width %d\n", clock
->area
.width
, new_width
);
177 // we try to limit the number of resize
178 clock
->area
.width
= new_width
+ 1;
179 clock
->area
.posx
= panel
->area
.width
- clock
->area
.width
- panel
->area
.paddingxlr
- panel
->area
.pix
.border
.width
;
181 // resize other objects on panel
182 for (i
=0 ; i
< nb_panel
; i
++) {
183 panel1
[i
].area
.resize
= 1;
185 systray
.area
.resize
= 1;
189 g_object_unref (layout
);
191 cairo_surface_destroy (cs
);
192 XFreePixmap (server
.dsp
, pmap
);
196 void clock_action(int button
)
201 command
= clock_lclick_command
;
204 command
= clock_rclick_command
;
211 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
This page took 0.046231 seconds and 3 git commands to generate.