]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
big change : panel_monitor = all will draw one panel per monitor, panel_size accept...
[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 "taskbar.h"
29 #include "panel.h"
30 #include "area.h"
31 #include "clock.h"
32
33
34 char *time1_format = 0;
35 char *time2_format = 0;
36 struct timeval time_clock;
37 int time_precision;
38 PangoFontDescription *time1_font_desc;
39 PangoFontDescription *time2_font_desc;
40
41
42 void init_clock(Clock *clock, Area *parent)
43 {
44 Panel *panel = (Panel *)parent;
45 char buf_time[40];
46 char buf_date[40];
47 int time_height, time_height_ink, date_height, date_height_ink;
48
49 clock->area.parent = parent;
50 clock->area.panel = panel;
51 clock->area.draw_foreground = draw_foreground_clock;
52 if (!time1_format) return;
53
54 if (strchr(time1_format, 'S') == NULL) time_precision = 60;
55 else time_precision = 1;
56
57 // update clock to force update (-time_precision)
58 struct timeval stv;
59 gettimeofday(&stv, 0);
60 time_clock.tv_sec = stv.tv_sec - time_precision;
61 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
62
63 clock->area.posy = parent->pix.border.width + parent->paddingy;
64 clock->area.height = parent->height - (2 * clock->area.posy);
65 clock->area.width = 0; // force posx and width detection
66 clock->area.redraw = 1;
67
68 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
69 if (time2_format)
70 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
71
72 get_text_size(time1_font_desc, &time_height_ink, &time_height, parent->height, buf_time, strlen(buf_time));
73 clock->time1_posy = (clock->area.height - time_height) / 2;
74
75 if (time2_format) {
76 get_text_size(time2_font_desc, &date_height_ink, &date_height, parent->height, buf_date, strlen(buf_date));
77
78 clock->time1_posy -= ((date_height_ink + 2) / 2);
79 clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
80 }
81 }
82
83
84 void draw_foreground_clock (void *obj, cairo_t *c, int active)
85 {
86 Area *parent = ((Area*)obj)->parent;
87 Clock *clock = obj;
88 PangoLayout *layout;
89 char buf_time[40];
90 char buf_date[40];
91 int time_width, date_width, new_width;
92
93 time_width = date_width = 0;
94 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
95 if (time2_format)
96 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
97
98 //printf(" draw_foreground_clock : %s\n", buf_time);
99 redraw:
100 layout = pango_cairo_create_layout (c);
101
102 // check width
103 pango_layout_set_font_description (layout, time1_font_desc);
104 pango_layout_set_indent(layout, 0);
105 pango_layout_set_text (layout, buf_time, strlen(buf_time));
106 pango_layout_get_pixel_size (layout, &time_width, NULL);
107 if (time2_format) {
108 pango_layout_set_font_description (layout, time2_font_desc);
109 pango_layout_set_indent(layout, 0);
110 pango_layout_set_text (layout, buf_date, strlen(buf_date));
111 pango_layout_get_pixel_size (layout, &date_width, NULL);
112 }
113 if (time_width > date_width) new_width = time_width;
114 else new_width = date_width;
115 new_width += (2*clock->area.paddingxlr) + (2*clock->area.pix.border.width);
116
117 if (new_width > clock->area.width || (new_width != clock->area.width && date_width > time_width)) {
118 //printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
119 // resize clock
120 clock->area.width = new_width;
121 clock->area.posx = parent->width - clock->area.width - parent->paddingxlr - parent->pix.border.width;
122
123 g_object_unref (layout);
124 resize_taskbar(parent);
125 goto redraw;
126 }
127
128 // draw layout
129 pango_layout_set_font_description (layout, time1_font_desc);
130 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
131 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
132 pango_layout_set_text (layout, buf_time, strlen(buf_time));
133
134 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
135
136 pango_cairo_update_layout (c, layout);
137 cairo_move_to (c, 0, clock->time1_posy);
138 pango_cairo_show_layout (c, layout);
139
140 if (time2_format) {
141 pango_layout_set_font_description (layout, time2_font_desc);
142 pango_layout_set_indent(layout, 0);
143 pango_layout_set_text (layout, buf_date, strlen(buf_date));
144 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
145
146 pango_cairo_update_layout (c, layout);
147 cairo_move_to (c, 0, clock->time2_posy);
148 pango_cairo_show_layout (c, layout);
149 }
150
151 g_object_unref (layout);
152 }
153
This page took 0.048078 seconds and 5 git commands to generate.