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