wmclockmon: replace deprecated `gtk_label_get`
[dockapps.git] / wmclockmon / wmclockmon-cal / mainwindow.c
blob315e4cbb79c2a3904dcb261fd2f8d07ac343d388
1 /*
2 * Create the main window.
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <dirent.h>
10 #include <gtk/gtk.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include "../config.h"
14 #include "defines.h"
15 #include "mainwindow.h"
16 #include "main.h"
17 #include "tools.h"
20 #define UNIQUE 1
21 #define YEAR 2
22 #define MONTH 3
24 #define UNIQSTR "%04d-%02d-"
25 #define YEARSTR "XXXX-%02d-"
26 #define MONTSTR "XXXX-XX-"
29 GtkWidget *application;
31 /* Calendar part */
32 static GtkWidget *calendar;
33 static GtkWidget *closewindow;
35 /* Editor part */
36 static GtkWidget *text_buttons;
37 static GtkWidget *label_u;
38 static GtkWidget *label_y;
39 static GtkWidget *label_m;
40 static GtkWidget *button_u = NULL;
41 static GtkWidget *button_y = NULL;
42 static GtkWidget *button_m = NULL;
43 static GtkWidget *edit;
44 static GtkWidget *save;
45 static GtkWidget *delete;
46 static GtkWidget *cancel;
47 static int shown = 1;
48 static int dateb = 0;
49 static char daystr[MAXSTRLEN + 1];
52 static void show_editor(void) {
53 gtk_widget_hide(calendar);
54 gtk_widget_hide(closewindow);
55 gtk_widget_grab_default(GTK_WIDGET(cancel));
56 gtk_widget_grab_focus(GTK_WIDGET(edit));
57 gtk_widget_show(text_buttons);
58 gtk_widget_show(edit);
59 gtk_widget_show(save);
60 gtk_widget_show(delete);
61 gtk_widget_show(cancel);
65 static void hide_editor(void) {
66 gtk_widget_hide(text_buttons);
67 gtk_widget_hide(edit);
68 gtk_widget_hide(save);
69 gtk_widget_hide(delete);
70 gtk_widget_hide(cancel);
71 gtk_widget_show(calendar);
72 gtk_widget_grab_default(GTK_WIDGET(closewindow));
73 gtk_widget_grab_focus(GTK_WIDGET(closewindow));
74 gtk_widget_show(closewindow);
78 static void toggle_displ(GtkWidget *widget UNUSED, void *data UNUSED) {
79 switch (shown) {
80 case 1:
81 show_editor();
82 shown = 2;
83 break;
84 case 2:
85 hide_editor();
86 shown = 1;
87 break;
92 static void load_file(const char *datestr) {
93 FILE *file;
94 char *filename = get_file(datestr);
96 GtkTextIter iter;
97 GtkTextBuffer *buf;
99 buf = GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit)));
100 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit))), &iter);
101 gtk_text_buffer_place_cursor(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit))), &iter);
103 if ((file = fopen(filename, "r")) != NULL) {
104 while (! feof(file)) {
105 char line[MAXSTRLEN + 1];
106 if (!fgets(line, MAXSTRLEN, file))
107 break;
108 if (line[0] != 0)
109 gtk_text_buffer_insert(buf, &iter, line, -1);
111 fclose(file);
113 FREE(filename);
117 static void toggle_button(GtkWidget *button) {
118 int is_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
120 if (is_active)
121 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
122 else
123 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
127 static void toggle_buttons(int button) {
128 switch (button) {
129 case UNIQUE: toggle_button(button_u); break;
130 case YEAR: toggle_button(button_y); break;
131 case MONTH: toggle_button(button_m); break;
132 default: break;
137 static void to_button(int button) {
138 if (dateb == 0)
139 dateb = button;
140 if (button != dateb) {
141 int b = dateb;
142 dateb = button;
143 toggle_buttons(b);
148 static void set_buttons_text(void) {
149 guint year, month, day;
150 char datestr[MAXSTRLEN + 1];
152 gtk_calendar_get_date(GTK_CALENDAR(calendar), &year, &month, &day);
153 month++;
154 snprintf(datestr, MAXSTRLEN, UNIQSTR"%02u", year, month, day);
155 gtk_label_set_text(GTK_LABEL(label_u), datestr);
156 snprintf(datestr, MAXSTRLEN, YEARSTR"%02u", month, day);
157 gtk_label_set_text(GTK_LABEL(label_y), datestr);
158 snprintf(datestr, MAXSTRLEN, MONTSTR"%02u", day);
159 gtk_label_set_text(GTK_LABEL(label_m), datestr);
163 static void editor_flush(void) {
164 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit))), "", 0);
168 static void editor_fill(int which) {
169 const char *dstr;
170 GtkWidget *label;
171 GtkTextIter iter;
173 switch (which) {
174 case UNIQUE: label = label_u; break;
175 case YEAR: label = label_y; break;
176 case MONTH: label = label_m; break;
178 dstr = gtk_label_get_text(GTK_LABEL(label));
179 strcpy(daystr, dstr);
180 to_button(which);
181 editor_flush();
182 load_file(daystr);
183 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit))), &iter);
184 gtk_text_buffer_place_cursor(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit))), &iter);
188 static void check_button(int bnum, GtkWidget *button) {
189 int is_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
191 if (!(button_u && button_y && button_m)) return;
192 if (!is_active && (dateb == bnum))
193 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
194 if (is_active && (dateb != bnum)) editor_fill(bnum);
198 static void cal_click(void) {
199 set_buttons_text();
200 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_u), TRUE);
201 editor_fill(UNIQUE);
205 static void set_text_u(void) {
206 check_button(UNIQUE, button_u);
210 static void set_text_y(void) {
211 check_button(YEAR, button_y);
215 static void set_text_m(void) {
216 check_button(MONTH, button_m);
220 static void save_datas(void) {
221 char *filename = get_file(daystr);
222 int len = strlen(robust_home()) + strlen(DEFAULT_CONFIGDIR);
223 char *dirname = xmalloc(len + 2);
224 struct stat stat_buf;
226 GtkTextIter ts, te;
227 gchar *tbuf;
228 int tlen;
230 tlen = gtk_text_buffer_get_char_count(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit))));
231 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit))), &ts, &te);
233 sprintf(dirname, "%s/%s", robust_home(), DEFAULT_CONFIGDIR);
234 if (tlen > 0) {
235 if (! ((stat(dirname, &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode)))
236 mkdir(dirname, 0755);
238 if ((stat(dirname, &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode)) {
239 FILE *file = fopen(filename, "w");
240 guint year, month, day;
242 if (file) {
243 tbuf = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit))), &ts, &te, TRUE);
244 fprintf(file, "%s", tbuf);
245 g_free(tbuf);
246 fflush(file);
247 fclose(file);
249 gtk_calendar_get_date(GTK_CALENDAR(calendar), &year, &month, &day);
250 gtk_calendar_mark_day(GTK_CALENDAR(calendar), day);
253 FREE(filename);
257 static void delete_file(void) {
258 char *filename = get_file(daystr);
259 guint year, month, day;
261 unlink(filename);
262 gtk_calendar_get_date(GTK_CALENDAR(calendar), &year, &month, &day);
263 gtk_calendar_unmark_day(GTK_CALENDAR(calendar), day);
264 FREE(filename);
265 editor_flush();
269 static int check_day(const char *filename, const char *startstr) {
270 int day = -1;
272 if (strncmp(filename, startstr, strlen(startstr)) == 0) {
273 char format[12];
274 sprintf(format, "%s%%d", startstr);
275 sscanf(filename, format, &day);
278 return day;
282 static void mark_days(void) {
283 char *Home = robust_home();
284 DIR *dir;
285 struct dirent *dir_ent;
286 char *dirname = xmalloc(
287 strlen(Home) +
288 strlen(DEFAULT_CONFIGDIR) +
291 gtk_calendar_clear_marks(GTK_CALENDAR(calendar));
292 sprintf(dirname, "%s/%s", Home, DEFAULT_CONFIGDIR);
293 if ((dir = opendir(dirname)) != NULL) {
294 char startstr_u[9]; /* unique (full date) */
295 char startstr_y[9]; /* yearly date */
296 char startstr_m[9]; /* monthly date */
297 guint year, month, day;
299 gtk_calendar_get_date(GTK_CALENDAR(calendar), &year, &month, &day);
300 month++;
301 sprintf(startstr_u, UNIQSTR, year, month);
302 sprintf(startstr_y, YEARSTR, month);
303 sprintf(startstr_m, MONTSTR);
304 while ((dir_ent = readdir(dir)) != NULL) {
305 int day_u = check_day(dir_ent->d_name, startstr_u);
306 int day_y = check_day(dir_ent->d_name, startstr_y);
307 int day_m = check_day(dir_ent->d_name, startstr_m);
308 if (day_u != -1)
309 gtk_calendar_mark_day(GTK_CALENDAR(calendar), day_u);
310 if (day_y != -1)
311 gtk_calendar_mark_day(GTK_CALENDAR(calendar), day_y);
312 if (day_m != -1)
313 gtk_calendar_mark_day(GTK_CALENDAR(calendar), day_m);
315 closedir(dir);
317 FREE(dirname);
321 void create_mainwindow(void) {
322 GtkWidget *main_vbox;
323 GtkWidget *buttons_hbox;
325 /*** FENÊTRE PRINCIPALE ***/
326 application = gtk_window_new(GTK_WINDOW_TOPLEVEL);
327 gtk_window_set_title(GTK_WINDOW(application), PACKAGE" Calendar");
328 /*-- Connexion aux signaux --*/
329 gtk_signal_connect(GTK_OBJECT(application), "delete_event",
330 GTK_SIGNAL_FUNC(quit_app), NULL);
331 gtk_signal_connect(GTK_OBJECT(application), "destroy",
332 GTK_SIGNAL_FUNC(quit_app), "WM destroy");
333 /*-- Taille de la fenêtre --*/
334 gtk_widget_set_usize(GTK_WIDGET(application), WIN_WIDTH, WIN_HEIGHT);
335 gtk_widget_realize(application);
337 /*** Zone principale de placement des widgets***/
338 main_vbox = gtk_vbox_new(FALSE, 1);
339 gtk_container_border_width(GTK_CONTAINER(main_vbox), 1);
340 gtk_container_add(GTK_CONTAINER(application), main_vbox);
341 gtk_widget_show(main_vbox);
343 calendar = gtk_calendar_new();
344 gtk_calendar_display_options(GTK_CALENDAR(calendar),
345 GTK_CALENDAR_SHOW_HEADING |
346 GTK_CALENDAR_SHOW_DAY_NAMES |
347 GTK_CALENDAR_WEEK_START_MONDAY);
348 gtk_calendar_select_month(GTK_CALENDAR(calendar),
349 timeinfos->tm_mon, timeinfos->tm_year + 1900);
350 gtk_calendar_select_day(GTK_CALENDAR(calendar), timeinfos->tm_mday);
351 mark_days();
352 gtk_box_pack_start(GTK_BOX(main_vbox), calendar, TRUE, TRUE, 1);
353 gtk_signal_connect(GTK_OBJECT(calendar), "day-selected-double-click",
354 GTK_SIGNAL_FUNC(cal_click), NULL);
355 gtk_signal_connect(GTK_OBJECT(calendar), "day-selected-double-click",
356 GTK_SIGNAL_FUNC(toggle_displ), NULL);
357 gtk_signal_connect(GTK_OBJECT(calendar), "month-changed",
358 GTK_SIGNAL_FUNC(mark_days), NULL);
359 gtk_widget_show(calendar);
361 edit = gtk_text_view_new();
362 gtk_text_view_set_editable(GTK_TEXT_VIEW(edit), TRUE);
363 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(edit), GTK_WRAP_WORD_CHAR);
365 gtk_box_pack_start(GTK_BOX(main_vbox), edit, TRUE, TRUE, 1);
368 /*** BOUTONS DE CHANGEMENT DE TEXTE ***/
369 text_buttons = gtk_hbox_new(FALSE, 1);
370 gtk_box_pack_start(GTK_BOX(main_vbox), text_buttons, FALSE, TRUE, 1);
373 button_u = gtk_toggle_button_new();
374 gtk_signal_connect(GTK_OBJECT(button_u), "clicked",
375 GTK_SIGNAL_FUNC(set_text_u), NULL);
376 gtk_box_pack_start(GTK_BOX(text_buttons), button_u, TRUE, TRUE, 0);
377 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_u), TRUE);
378 gtk_widget_show(button_u);
379 label_u = gtk_label_new(" Unique ");
380 gtk_widget_show(label_u);
381 gtk_container_add(GTK_CONTAINER(button_u), label_u);
384 button_y = gtk_toggle_button_new();
385 gtk_signal_connect(GTK_OBJECT(button_y), "clicked",
386 GTK_SIGNAL_FUNC(set_text_y), NULL);
387 gtk_box_pack_start(GTK_BOX(text_buttons), button_y, TRUE, TRUE, 0);
388 gtk_widget_show(button_y);
389 label_y = gtk_label_new(" Yearly ");
390 gtk_widget_show(label_y);
391 gtk_container_add(GTK_CONTAINER(button_y), label_y);
394 button_m = gtk_toggle_button_new();
395 gtk_signal_connect(GTK_OBJECT(button_m), "clicked",
396 GTK_SIGNAL_FUNC(set_text_m), NULL);
397 gtk_box_pack_start(GTK_BOX(text_buttons), button_m, TRUE, TRUE, 0);
398 gtk_widget_show(button_m);
399 label_m = gtk_label_new(" Monthly ");
400 gtk_widget_show(label_m);
401 gtk_container_add(GTK_CONTAINER(button_m), label_m);
405 /*** BOUTONS DE SAUVEGARDE ET ANNULATION ***/
406 buttons_hbox = gtk_hbox_new(FALSE, 1);
407 gtk_box_pack_start(GTK_BOX(main_vbox), buttons_hbox, FALSE, TRUE, 1);
408 gtk_widget_show(buttons_hbox);
411 closewindow = gtk_button_new_with_label(" Close ");
412 gtk_signal_connect(GTK_OBJECT(closewindow), "clicked",
413 GTK_SIGNAL_FUNC(quit_app), NULL);
414 gtk_box_pack_start(GTK_BOX(buttons_hbox), closewindow, TRUE, TRUE, 0);
415 gtk_widget_set_can_default(GTK_WIDGET(closewindow), TRUE);
416 gtk_widget_grab_default(GTK_WIDGET(closewindow));
417 gtk_widget_show(closewindow);
420 save = gtk_button_new_with_label(" Save ");
421 gtk_signal_connect(GTK_OBJECT(save), "clicked",
422 GTK_SIGNAL_FUNC(save_datas), NULL);
423 gtk_box_pack_start(GTK_BOX(buttons_hbox), save, TRUE, TRUE, 0);
424 gtk_widget_set_can_default(GTK_WIDGET(save), TRUE);
427 delete = gtk_button_new_with_label(" Delete ");
428 gtk_signal_connect(GTK_OBJECT(delete), "clicked",
429 GTK_SIGNAL_FUNC(delete_file), NULL);
430 gtk_box_pack_start(GTK_BOX(buttons_hbox), delete, TRUE, TRUE, 0);
431 gtk_widget_set_can_default(GTK_WIDGET(delete), TRUE);
434 cancel = gtk_button_new_with_label(" Close ");
435 gtk_signal_connect(GTK_OBJECT(cancel), "clicked",
436 GTK_SIGNAL_FUNC(toggle_displ), NULL);
437 gtk_box_pack_start(GTK_BOX(buttons_hbox), cancel, TRUE, TRUE, 0);
438 gtk_widget_set_can_default(GTK_WIDGET(cancel), TRUE);
441 /*** AFFICHAGE DE LA FENÊTRE ***/
442 gtk_widget_show(application);