]>
Dogcows Code - chaz/tint2/blob - src/clock/clock.c
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>
34 char *time1_format
= 0;
35 char *time2_format
= 0;
36 struct timeval time_clock
;
38 PangoFontDescription
*time1_font_desc
;
39 PangoFontDescription
*time2_font_desc
;
40 static char buf_time
[40];
41 static char buf_date
[40];
44 void init_clock(Clock
*clock
, Area
*parent
)
46 Panel
*panel
= (Panel
*)parent
;
47 int time_height
, time_height_ink
, date_height
, date_height_ink
;
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;
55 if (strchr(time1_format
, 'S') == NULL
) time_precision
= 60;
56 else time_precision
= 1;
58 // update clock to force update (-time_precision)
60 gettimeofday(&stv
, 0);
61 time_clock
.tv_sec
= stv
.tv_sec
- time_precision
;
62 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
64 clock
->area
.posy
= parent
->pix
.border
.width
+ parent
->paddingy
;
65 clock
->area
.height
= parent
->height
- (2 * clock
->area
.posy
);
66 clock
->area
.resize
= 1;
67 clock
->area
.redraw
= 1;
69 strftime(buf_time
, sizeof(buf_time
), time1_format
, localtime(&time_clock
.tv_sec
));
71 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
73 get_text_size(time1_font_desc
, &time_height_ink
, &time_height
, parent
->height
, buf_time
, strlen(buf_time
));
74 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
77 get_text_size(time2_font_desc
, &date_height_ink
, &date_height
, parent
->height
, buf_date
, strlen(buf_date
));
79 clock
->time1_posy
-= ((date_height_ink
+ 2) / 2);
80 clock
->time2_posy
= clock
->time1_posy
+ time_height
+ 2 - (time_height
- time_height_ink
)/2 - (date_height
- date_height_ink
)/2;
85 void draw_foreground_clock (void *obj
, cairo_t
*c
, int active
)
90 //printf(" draw_foreground_clock : %s en (%d, %d)\n", buf_time, clock->area.posx, clock->area.width);
91 layout
= pango_cairo_create_layout (c
);
94 pango_layout_set_font_description (layout
, time1_font_desc
);
95 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
96 pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
97 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
99 cairo_set_source_rgba (c
, clock
->font
.color
[0], clock
->font
.color
[1], clock
->font
.color
[2], clock
->font
.alpha
);
101 pango_cairo_update_layout (c
, layout
);
102 cairo_move_to (c
, 0, clock
->time1_posy
);
103 pango_cairo_show_layout (c
, layout
);
106 pango_layout_set_font_description (layout
, time2_font_desc
);
107 pango_layout_set_indent(layout
, 0);
108 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
109 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
111 pango_cairo_update_layout (c
, layout
);
112 cairo_move_to (c
, 0, clock
->time2_posy
);
113 pango_cairo_show_layout (c
, layout
);
116 g_object_unref (layout
);
120 void resize_clock (void *obj
)
122 Area
*parent
= ((Area
*)obj
)->parent
;
125 int time_width
, date_width
, new_width
;
127 time_width
= date_width
= 0;
128 strftime(buf_time
, sizeof(buf_time
), time1_format
, localtime(&time_clock
.tv_sec
));
130 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
132 //printf(" resize_clock\n");
136 pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, clock
->area
.width
, clock
->area
.height
, server
.depth
);
138 cs
= cairo_xlib_surface_create (server
.dsp
, pmap
, server
.visual
, clock
->area
.width
, clock
->area
.height
);
139 c
= cairo_create (cs
);
140 layout
= pango_cairo_create_layout (c
);
143 pango_layout_set_font_description (layout
, time1_font_desc
);
144 pango_layout_set_indent(layout
, 0);
145 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
146 pango_layout_get_pixel_size (layout
, &time_width
, NULL
);
148 pango_layout_set_font_description (layout
, time2_font_desc
);
149 pango_layout_set_indent(layout
, 0);
150 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
151 pango_layout_get_pixel_size (layout
, &date_width
, NULL
);
154 if (time_width
> date_width
) new_width
= time_width
;
155 else new_width
= date_width
;
156 new_width
+= (2*clock
->area
.paddingxlr
) + (2*clock
->area
.pix
.border
.width
);
158 if (new_width
> clock
->area
.width
|| new_width
< (clock
->area
.width
-3)) {
160 //printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
161 clock
->area
.width
= new_width
;
162 clock
->area
.posx
= parent
->width
- clock
->area
.width
- parent
->paddingxlr
- parent
->pix
.border
.width
;
166 g_object_unref (layout
);
168 cairo_surface_destroy (cs
);
169 XFreePixmap (server
.dsp
, pmap
);
This page took 0.040333 seconds and 4 git commands to generate.