2 * Create the main window.
13 #include "../config.h"
15 #include "mainwindow.h"
24 #define UNIQSTR "%04d-%02d-"
25 #define YEARSTR "XXXX-%02d-"
26 #define MONTSTR "XXXX-XX-"
29 GtkWidget
*application
;
32 static GtkWidget
*calendar
;
33 static GtkWidget
*closewindow
;
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
;
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(void) {
92 static void load_file(const char *datestr
) {
94 char *filename
= get_file(datestr
);
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
))
109 gtk_text_buffer_insert(buf
, &iter
, line
, -1);
117 static void toggle_button(GtkWidget
*button
) {
118 int is_active
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button
));
121 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button
), FALSE
);
123 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button
), TRUE
);
127 static void toggle_buttons(int 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;
137 static void to_button(int button
) {
140 if (button
!= dateb
) {
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
);
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
) {
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
);
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) {
200 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_u
), TRUE
);
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
;
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
);
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
;
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
);
249 gtk_calendar_get_date(GTK_CALENDAR(calendar
), &year
, &month
, &day
);
250 gtk_calendar_mark_day(GTK_CALENDAR(calendar
), day
);
257 static void delete_file(void) {
258 char *filename
= get_file(daystr
);
259 guint year
, month
, day
;
262 gtk_calendar_get_date(GTK_CALENDAR(calendar
), &year
, &month
, &day
);
263 gtk_calendar_unmark_day(GTK_CALENDAR(calendar
), day
);
269 static int check_day(const char *filename
, const char *startstr
) {
272 if (strncmp(filename
, startstr
, strlen(startstr
)) == 0) {
274 sprintf(format
, "%s%%d", startstr
);
275 sscanf(filename
, format
, &day
);
282 static void mark_days(void) {
283 char *Home
= robust_home();
285 struct dirent
*dir_ent
;
286 char *dirname
= xmalloc(
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
);
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
);
309 gtk_calendar_mark_day(GTK_CALENDAR(calendar
), day_u
);
311 gtk_calendar_mark_day(GTK_CALENDAR(calendar
), day_y
);
313 gtk_calendar_mark_day(GTK_CALENDAR(calendar
), day_m
);
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 g_signal_connect(application
, "delete_event", G_CALLBACK(quit_app
), NULL
);
330 g_signal_connect(application
, "destroy", G_CALLBACK(quit_app
), NULL
);
331 /*-- Taille de la fenêtre --*/
332 gtk_widget_set_size_request(GTK_WIDGET(application
), WIN_WIDTH
, WIN_HEIGHT
);
333 gtk_widget_realize(application
);
335 /*** Zone principale de placement des widgets***/
336 main_vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 1);
337 gtk_container_set_border_width(GTK_CONTAINER(main_vbox
), 1);
338 gtk_container_add(GTK_CONTAINER(application
), main_vbox
);
339 gtk_widget_show(main_vbox
);
341 calendar
= gtk_calendar_new();
342 gtk_calendar_set_display_options(GTK_CALENDAR(calendar
),
343 GTK_CALENDAR_SHOW_HEADING
|
344 GTK_CALENDAR_SHOW_DAY_NAMES
);
345 gtk_calendar_select_month(GTK_CALENDAR(calendar
),
346 timeinfos
->tm_mon
, timeinfos
->tm_year
+ 1900);
347 gtk_calendar_select_day(GTK_CALENDAR(calendar
), timeinfos
->tm_mday
);
349 gtk_box_pack_start(GTK_BOX(main_vbox
), calendar
, TRUE
, TRUE
, 1);
350 g_signal_connect(calendar
, "day-selected-double-click",
351 G_CALLBACK(cal_click
), NULL
);
352 g_signal_connect(calendar
, "day-selected-double-click",
353 G_CALLBACK(toggle_displ
), NULL
);
354 g_signal_connect(calendar
, "month-changed", G_CALLBACK(mark_days
), NULL
);
355 gtk_widget_show(calendar
);
357 edit
= gtk_text_view_new();
358 gtk_text_view_set_editable(GTK_TEXT_VIEW(edit
), TRUE
);
359 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(edit
), GTK_WRAP_WORD_CHAR
);
361 gtk_box_pack_start(GTK_BOX(main_vbox
), edit
, TRUE
, TRUE
, 1);
364 /*** BOUTONS DE CHANGEMENT DE TEXTE ***/
365 text_buttons
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
366 gtk_box_pack_start(GTK_BOX(main_vbox
), text_buttons
, FALSE
, TRUE
, 1);
369 button_u
= gtk_toggle_button_new();
370 g_signal_connect(button_u
, "clicked", G_CALLBACK(set_text_u
), NULL
);
371 gtk_box_pack_start(GTK_BOX(text_buttons
), button_u
, TRUE
, TRUE
, 0);
372 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_u
), TRUE
);
373 gtk_widget_show(button_u
);
374 label_u
= gtk_label_new(" Unique ");
375 gtk_widget_show(label_u
);
376 gtk_container_add(GTK_CONTAINER(button_u
), label_u
);
379 button_y
= gtk_toggle_button_new();
380 g_signal_connect(button_y
, "clicked", G_CALLBACK(set_text_y
), NULL
);
381 gtk_box_pack_start(GTK_BOX(text_buttons
), button_y
, TRUE
, TRUE
, 0);
382 gtk_widget_show(button_y
);
383 label_y
= gtk_label_new(" Yearly ");
384 gtk_widget_show(label_y
);
385 gtk_container_add(GTK_CONTAINER(button_y
), label_y
);
388 button_m
= gtk_toggle_button_new();
389 g_signal_connect(button_m
, "clicked", G_CALLBACK(set_text_m
), NULL
);
390 gtk_box_pack_start(GTK_BOX(text_buttons
), button_m
, TRUE
, TRUE
, 0);
391 gtk_widget_show(button_m
);
392 label_m
= gtk_label_new(" Monthly ");
393 gtk_widget_show(label_m
);
394 gtk_container_add(GTK_CONTAINER(button_m
), label_m
);
398 /*** BOUTONS DE SAUVEGARDE ET ANNULATION ***/
399 buttons_hbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 1);
400 gtk_box_pack_start(GTK_BOX(main_vbox
), buttons_hbox
, FALSE
, TRUE
, 1);
401 gtk_widget_show(buttons_hbox
);
404 closewindow
= gtk_button_new_with_label(" Close ");
405 g_signal_connect(closewindow
, "clicked", G_CALLBACK(quit_app
), NULL
);
406 gtk_box_pack_start(GTK_BOX(buttons_hbox
), closewindow
, TRUE
, TRUE
, 0);
407 gtk_widget_set_can_default(GTK_WIDGET(closewindow
), TRUE
);
408 gtk_widget_grab_default(GTK_WIDGET(closewindow
));
409 gtk_widget_show(closewindow
);
412 save
= gtk_button_new_with_label(" Save ");
413 g_signal_connect(save
, "clicked", G_CALLBACK(save_datas
), NULL
);
414 gtk_box_pack_start(GTK_BOX(buttons_hbox
), save
, TRUE
, TRUE
, 0);
415 gtk_widget_set_can_default(GTK_WIDGET(save
), TRUE
);
418 delete = gtk_button_new_with_label(" Delete ");
419 g_signal_connect(delete, "clicked", G_CALLBACK(delete_file
), NULL
);
420 gtk_box_pack_start(GTK_BOX(buttons_hbox
), delete, TRUE
, TRUE
, 0);
421 gtk_widget_set_can_default(GTK_WIDGET(delete), TRUE
);
424 cancel
= gtk_button_new_with_label(" Close ");
425 g_signal_connect(cancel
, "clicked", G_CALLBACK(toggle_displ
), NULL
);
426 gtk_box_pack_start(GTK_BOX(buttons_hbox
), cancel
, TRUE
, TRUE
, 0);
427 gtk_widget_set_can_default(GTK_WIDGET(cancel
), TRUE
);
430 /*** AFFICHAGE DE LA FENÊTRE ***/
431 gtk_widget_show(application
);