forked from nsf/bmpanel2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget-clock.c
195 lines (162 loc) · 5.03 KB
/
widget-clock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <time.h>
#include "settings.h"
#include "builtin-widgets.h"
static int create_widget_private(struct widget *w, struct config_format_entry *e,
struct config_format_tree *tree);
static void destroy_widget_private(struct widget *w);
static void draw(struct widget *w);
static void clock_tick(struct widget *w);
static void button_click(struct widget *w, XButtonEvent *e);
static void reconfigure(struct widget *w);
struct widget_interface clock_interface = {
.theme_name = "clock",
.size_type = WIDGET_SIZE_CONSTANT,
.create_widget_private = create_widget_private,
.destroy_widget_private = destroy_widget_private,
.draw = draw,
.clock_tick = clock_tick,
.button_click = button_click,
.reconfigure = reconfigure
};
/**************************************************************************
Clock theme
**************************************************************************/
static int parse_clock_theme(struct clock_theme *ct,
struct config_format_entry *e, struct config_format_tree *tree)
{
if (parse_text_info_named(&ct->font, "font", e, 1))
return -1;
parse_triple_image_named(&ct->background, "background", e, tree, 0);
ct->time_format = parse_string("time_format", e, "%H:%M:%S");
return 0;
}
static void free_clock_theme(struct clock_theme *ct)
{
free_triple_image(&ct->background);
free_text_info(&ct->font);
xfree(ct->time_format);
}
/**************************************************************************
Clock interface
**************************************************************************/
static void fill_buftime(char *buf, size_t size, struct clock_theme *ct)
{
time_t current_time;
current_time = time(0);
strftime(buf, size, ct->time_format, localtime(¤t_time));
}
static int get_clock_width(struct widget *w, const char *bufover)
{
struct clock_widget *cw = (struct clock_widget*)w->private;
int text_width = 0;
int pics_width = 0;
char buftime[128];
if (!bufover) {
fill_buftime(buftime, sizeof(buftime), &cw->theme);
bufover = buftime;
}
text_extents(w->panel->layout, cw->theme.font.pfd,
bufover, &text_width, 0);
if (cw->theme.background.center) {
pics_width += image_width(cw->theme.background.left);
pics_width += image_width(cw->theme.background.right);
}
return text_width + pics_width;
}
static int create_widget_private(struct widget *w, struct config_format_entry *e,
struct config_format_tree *tree)
{
struct clock_widget *cw = xmallocz(sizeof(struct clock_widget));
if (parse_clock_theme(&cw->theme, e, tree)) {
xfree(cw);
XWARNING("Failed to parse clock theme");
return -1;
}
cw->clock_prog = parse_string_or_null("clock_prog",
&g_settings.root);
cw->mouse_button = parse_int("clock_mouse_button",
&g_settings.root, 1);
w->private = cw;
w->width = get_clock_width(w, 0);
return 0;
}
static void destroy_widget_private(struct widget *w)
{
struct clock_widget *cw = (struct clock_widget*)w->private;
free_clock_theme(&cw->theme);
if (cw->clock_prog)
xfree(cw->clock_prog);
xfree(cw);
}
static void draw(struct widget *w)
{
struct clock_widget *cw = (struct clock_widget*)w->private;
/* time */
char buftime[128];
fill_buftime(buftime, sizeof(buftime), &cw->theme);
/* drawing */
cairo_t *cr = w->panel->cr;
int x = w->x;
/* calcs */
int leftw = 0;
int rightw = 0;
int centerw = w->width;
/* draw background only if the center image is here */
if (cw->theme.background.center) {
leftw += image_width(cw->theme.background.left);
rightw += image_width(cw->theme.background.right);
centerw -= leftw + rightw;
/* left part */
if (leftw)
blit_image(cw->theme.background.left, cr, x, 0);
x += leftw;
/* center part */
pattern_image(cw->theme.background.center, cr, x, 0,
centerw, 1);
x += centerw;
/* right part */
if (rightw)
blit_image(cw->theme.background.right, cr, x, 0);
x -= centerw;
}
/* text */
draw_text(cr, w->panel->layout, &cw->theme.font, buftime,
x, 0, centerw, w->panel->height, 0);
}
static void clock_tick(struct widget *w)
{
struct clock_widget *cw = (struct clock_widget*)w->private;
static char buflasttime[128];
char buftime[128];
time_t current_time;
current_time = time(0);
strftime(buftime, sizeof(buftime), cw->theme.time_format, localtime(¤t_time));
if (!strcmp(buflasttime, buftime))
return;
strcpy(buflasttime, buftime);
int nw = get_clock_width(w, buftime);
if (nw != w->width) {
w->width = nw;
recalculate_widgets_sizes(w->panel);
return;
}
w->needs_expose = 1;
}
static void button_click(struct widget *w, XButtonEvent *e)
{
struct clock_widget *cw = (struct clock_widget*)w->private;
if (!cw->clock_prog)
return;
if (cw->mouse_button == e->button && e->type == ButtonRelease)
g_spawn_command_line_async(cw->clock_prog, 0);
}
static void reconfigure(struct widget *w)
{
struct clock_widget *cw = (struct clock_widget*)w->private;
if (cw->clock_prog)
xfree(cw->clock_prog);
cw->clock_prog = parse_string_or_null("clock_prog",
&g_settings.root);
cw->mouse_button = parse_int("clock_mouse_button",
&g_settings.root, 1);
}