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