]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
New import
[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 "area.h"
29 #include "clock.h"
30
31
32 void init_clock(Clock *clock, int panel_height)
33 {
34 char buf_time[40];
35 char buf_date[40];
36 int time_height, time_height_ink, date_height, date_height_ink;
37
38 if (!clock->time1_format) return;
39
40 if (strchr(clock->time1_format, 'S') == NULL) clock->time_precision = 60;
41 else clock->time_precision = 1;
42
43 gettimeofday(&clock->clock, 0);
44 clock->clock.tv_sec -= clock->clock.tv_sec % clock->time_precision;
45
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));
49
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;
52
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));
55
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;
58 }
59 }
60
61
62 int draw_foreground_clock (void *obj, cairo_t *c)
63 {
64 Clock *clock = obj;
65 PangoLayout *layout;
66 char buf_time[40];
67 char buf_date[40];
68 int time_width, date_width, new_width;
69
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));
74
75 layout = pango_cairo_create_layout (c);
76
77 // check width
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);
87 }
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);
91
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;
95
96 g_object_unref (layout);
97 return 1;
98 }
99
100 // draw 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));
105
106 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
107
108 pango_cairo_update_layout (c, layout);
109 cairo_move_to (c, 0, clock->time1_posy);
110 pango_cairo_show_layout (c, layout);
111
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);
117
118 pango_cairo_update_layout (c, layout);
119 cairo_move_to (c, 0, clock->time2_posy);
120 pango_cairo_show_layout (c, layout);
121 }
122
123 g_object_unref (layout);
124 return 0;
125 }
126
This page took 0.046357 seconds and 5 git commands to generate.