]>
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>
32 void init_clock(Clock
*clock
, int panel_height
)
36 int time_height
, time_height_ink
, date_height
, date_height_ink
;
38 if (!clock
->time1_format
) return;
40 if (strchr(clock
->time1_format
, 'S') == NULL
) clock
->time_precision
= 60;
41 else clock
->time_precision
= 1;
43 gettimeofday(&clock
->clock
, 0);
44 clock
->clock
.tv_sec
-= clock
->clock
.tv_sec
% clock
->time_precision
;
46 strftime(buf_time
, sizeof(buf_time
), clock
->time1_format
, localtime(&clock
->clock
.tv_sec
));
47 if (clock
->time2_format
)
48 strftime(buf_date
, sizeof(buf_date
), clock
->time2_format
, localtime(&clock
->clock
.tv_sec
));
50 get_text_size(clock
->time1_font_desc
, &time_height_ink
, &time_height
, panel_height
, buf_time
, strlen(buf_time
));
51 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
53 if (clock
->time2_format
) {
54 get_text_size(clock
->time2_font_desc
, &date_height_ink
, &date_height
, panel_height
, buf_date
, strlen(buf_date
));
56 clock
->time1_posy
-= ((date_height_ink
+ 2) / 2);
57 clock
->time2_posy
= clock
->time1_posy
+ time_height
+ 2 - (time_height
- time_height_ink
)/2 - (date_height
- date_height_ink
)/2;
62 int draw_foreground_clock (void *obj
, cairo_t
*c
)
68 int time_width
, date_width
, new_width
;
70 time_width
= date_width
= 0;
71 strftime(buf_time
, sizeof(buf_time
), clock
->time1_format
, localtime(&clock
->clock
.tv_sec
));
72 if (clock
->time2_format
)
73 strftime(buf_date
, sizeof(buf_date
), clock
->time2_format
, localtime(&clock
->clock
.tv_sec
));
75 layout
= pango_cairo_create_layout (c
);
78 pango_layout_set_font_description (layout
, clock
->time1_font_desc
);
79 pango_layout_set_indent(layout
, 0);
80 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
81 pango_layout_get_pixel_size (layout
, &time_width
, NULL
);
82 if (clock
->time2_format
) {
83 pango_layout_set_font_description (layout
, clock
->time2_font_desc
);
84 pango_layout_set_indent(layout
, 0);
85 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
86 pango_layout_get_pixel_size (layout
, &date_width
, NULL
);
88 if (time_width
> date_width
) new_width
= time_width
;
89 else new_width
= date_width
;
90 new_width
+= (2*clock
->area
.paddingx
) + (2*clock
->area
.border
.width
);
92 if (new_width
> clock
->area
.width
|| (new_width
!= clock
->area
.width
&& date_width
> time_width
)) {
93 printf("clock_width %d, new_width %d\n", clock
->area
.width
, new_width
);
94 clock
->area
.width
= new_width
;
96 g_object_unref (layout
);
101 pango_layout_set_font_description (layout
, clock
->time1_font_desc
);
102 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
103 pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
104 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
106 cairo_set_source_rgba (c
, clock
->font
.color
[0], clock
->font
.color
[1], clock
->font
.color
[2], clock
->font
.alpha
);
108 pango_cairo_update_layout (c
, layout
);
109 cairo_move_to (c
, 0, clock
->time1_posy
);
110 pango_cairo_show_layout (c
, layout
);
112 if (clock
->time2_format
) {
113 pango_layout_set_font_description (layout
, clock
->time2_font_desc
);
114 pango_layout_set_indent(layout
, 0);
115 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
116 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
118 pango_cairo_update_layout (c
, layout
);
119 cairo_move_to (c
, 0, clock
->time2_posy
);
120 pango_cairo_show_layout (c
, layout
);
123 g_object_unref (layout
);
This page took 0.042361 seconds and 4 git commands to generate.