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-"
30 static GtkWidget
*calendar
;
31 static GtkWidget
*closewindow
;
34 static GtkWidget
*text_buttons
;
35 static GtkWidget
*label_u
;
36 static GtkWidget
*label_y
;
37 static GtkWidget
*label_m
;
38 static GtkWidget
*button_u
= NULL
;
39 static GtkWidget
*button_y
= NULL
;
40 static GtkWidget
*button_m
= NULL
;
41 static GtkWidget
*edit
;
42 static GtkWidget
*save
;
43 static GtkWidget
*delete;
44 static GtkWidget
*cancel
;
47 static char daystr
[MAXSTRLEN
+ 1];
50 static void show_editor() {
51 gtk_widget_hide(calendar
);
52 gtk_widget_hide(closewindow
);
53 gtk_widget_grab_default(GTK_WIDGET(cancel
));
54 gtk_widget_grab_focus(GTK_WIDGET(edit
));
55 gtk_widget_show(text_buttons
);
56 gtk_widget_show(edit
);
57 gtk_widget_show(save
);
58 gtk_widget_show(delete);
59 gtk_widget_show(cancel
);
63 static void hide_editor() {
64 gtk_widget_hide(text_buttons
);
65 gtk_widget_hide(edit
);
66 gtk_widget_hide(save
);
67 gtk_widget_hide(delete);
68 gtk_widget_hide(cancel
);
69 gtk_widget_show(calendar
);
70 gtk_widget_grab_default(GTK_WIDGET(closewindow
));
71 gtk_widget_grab_focus(GTK_WIDGET(closewindow
));
72 gtk_widget_show(closewindow
);
76 static void toggle_displ(GtkWidget
*widget
, void *data
) {
90 static void load_file(const char *datestr
) {
92 char *filename
= get_file(datestr
);
97 buf
= GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
)));
98 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
))), &iter
);
99 gtk_text_buffer_place_cursor(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
))), &iter
);
101 if ((file
= fopen(filename
, "r")) != NULL
) {
102 while (! feof(file
)) {
103 char line
[MAXSTRLEN
+ 1];
104 bzero(line
, MAXSTRLEN
+ 1);
105 fgets(line
, MAXSTRLEN
, file
);
107 gtk_text_buffer_insert(buf
, &iter
, line
, -1);
115 static void toggle_button(GtkWidget
*button
) {
116 int is_active
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button
));
119 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button
), FALSE
);
121 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button
), TRUE
);
125 static void toggle_buttons(int button
) {
127 case UNIQUE
: toggle_button(button_u
); break;
128 case YEAR
: toggle_button(button_y
); break;
129 case MONTH
: toggle_button(button_m
); break;
135 static void to_button(int button
) {
136 if (dateb
== 0) dateb
== button
;
137 if (button
!= dateb
) {
145 static void set_buttons_text() {
146 int year
, month
, day
;
147 char datestr
[MAXSTRLEN
+ 1];
149 bzero(datestr
, MAXSTRLEN
+ 1);
150 gtk_calendar_get_date(GTK_CALENDAR(calendar
), &year
, &month
, &day
);
152 snprintf(datestr
, MAXSTRLEN
, UNIQSTR
"%02d", year
, month
, day
);
153 gtk_label_set_text(GTK_LABEL(label_u
), datestr
);
154 snprintf(datestr
, MAXSTRLEN
, YEARSTR
"%02d", month
, day
);
155 gtk_label_set_text(GTK_LABEL(label_y
), datestr
);
156 snprintf(datestr
, MAXSTRLEN
, MONTSTR
"%02d", day
);
157 gtk_label_set_text(GTK_LABEL(label_m
), datestr
);
161 static void editor_flush() {
162 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
))), "", 0);
166 static void editor_fill(int which
) {
167 char *filename
, *dstr
;
172 case UNIQUE
: label
= label_u
; break;
173 case YEAR
: label
= label_y
; break;
174 case MONTH
: label
= label_m
; break;
177 gtk_label_get(GTK_LABEL(label
), &dstr
);
178 strcpy(daystr
, dstr
);
182 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
))), &iter
);
183 gtk_text_buffer_place_cursor(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
))), &iter
);
187 static void check_button(int bnum
, GtkWidget
*button
) {
188 int is_active
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button
));
190 if (!(button_u
&& button_y
&& button_m
)) return;
191 if (!is_active
&& (dateb
== bnum
))
192 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button
), TRUE
);
193 if (is_active
&& (dateb
!= bnum
)) editor_fill(bnum
);
197 static void cal_click() {
199 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_u
), TRUE
);
204 static void set_text_u() {
205 check_button(UNIQUE
, button_u
);
209 static void set_text_y() {
210 check_button(YEAR
, button_y
);
214 static void set_text_m() {
215 check_button(MONTH
, button_m
);
219 static void save_datas() {
220 char *filename
= get_file(daystr
);
221 int len
= strlen(robust_home()) + strlen(DEFAULT_CONFIGDIR
);
222 char *dirname
= xmalloc(len
+ 2);
223 struct stat stat_buf
;
229 tlen
= gtk_text_buffer_get_char_count(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
))));
230 gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
))), &ts
, &te
);
232 sprintf(dirname
, "%s/%s", robust_home(), DEFAULT_CONFIGDIR
);
234 if (! ((stat(dirname
, &stat_buf
) == 0) && S_ISDIR(stat_buf
.st_mode
)))
235 mkdir(dirname
, 0755);
237 if ((stat(dirname
, &stat_buf
) == 0) && S_ISDIR(stat_buf
.st_mode
)) {
238 FILE *file
= fopen(filename
, "w");
239 int year
, month
, day
;
242 tbuf
= gtk_text_buffer_get_text(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(edit
))), &ts
, &te
, TRUE
);
243 fprintf(file
, "%s", tbuf
);
248 gtk_calendar_get_date(GTK_CALENDAR(calendar
), &year
, &month
, &day
);
249 gtk_calendar_mark_day(GTK_CALENDAR(calendar
), day
);
256 static void delete_file() {
257 char *filename
= get_file(daystr
);
258 int year
, month
, day
;
261 gtk_calendar_get_date(GTK_CALENDAR(calendar
), &year
, &month
, &day
);
262 gtk_calendar_unmark_day(GTK_CALENDAR(calendar
), day
);
268 static int check_day(const char *filename
, const char *startstr
) {
271 if (strncmp(filename
, startstr
, strlen(startstr
)) == 0) {
273 sprintf(format
, "%s%%d", startstr
);
274 sscanf(filename
, format
, &day
);
281 static void mark_days() {
282 char *Home
= robust_home();
284 struct dirent
*dir_ent
;
285 char *dirname
= xmalloc(
287 strlen(DEFAULT_CONFIGDIR
) +
290 gtk_calendar_clear_marks(GTK_CALENDAR(calendar
));
291 sprintf(dirname
, "%s/%s", Home
, DEFAULT_CONFIGDIR
);
292 if ((dir
= opendir(dirname
)) != NULL
) {
293 char startstr_u
[9]; /* unique (full date) */
294 char startstr_y
[9]; /* yearly date */
295 char startstr_m
[9]; /* monthly date */
296 int year
, month
, day
;
298 gtk_calendar_get_date(GTK_CALENDAR(calendar
), &year
, &month
, &day
);
300 sprintf(startstr_u
, UNIQSTR
, year
, month
);
301 sprintf(startstr_y
, YEARSTR
, month
);
302 sprintf(startstr_m
, MONTSTR
);
303 while ((dir_ent
= readdir(dir
)) != NULL
) {
304 int day_u
= check_day(dir_ent
->d_name
, startstr_u
);
305 int day_y
= check_day(dir_ent
->d_name
, startstr_y
);
306 int day_m
= check_day(dir_ent
->d_name
, startstr_m
);
308 gtk_calendar_mark_day(GTK_CALENDAR(calendar
), day_u
);
310 gtk_calendar_mark_day(GTK_CALENDAR(calendar
), day_y
);
312 gtk_calendar_mark_day(GTK_CALENDAR(calendar
), day_m
);
320 void create_mainwindow() {
321 GtkWidget
*main_vbox
;
322 GtkWidget
*buttons_hbox
;
324 /*** FENÊTRE PRINCIPALE ***/
325 application
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
326 gtk_window_set_title(GTK_WINDOW(application
), PACKAGE
" Calendar");
327 /*-- Connexion aux signaux --*/
328 gtk_signal_connect(GTK_OBJECT(application
), "delete_event",
329 GTK_SIGNAL_FUNC(quit_app
), NULL
);
330 gtk_signal_connect(GTK_OBJECT(application
), "destroy",
331 GTK_SIGNAL_FUNC(quit_app
), "WM destroy");
332 /*-- Taille de la fenêtre --*/
333 gtk_widget_set_usize(GTK_WIDGET(application
), WIN_WIDTH
, WIN_HEIGHT
);
334 gtk_widget_realize(application
);
336 /*** Zone principale de placement des widgets***/
337 main_vbox
= gtk_vbox_new(FALSE
, 1);
338 gtk_container_border_width(GTK_CONTAINER(main_vbox
), 1);
339 gtk_container_add(GTK_CONTAINER(application
), main_vbox
);
340 gtk_widget_show(main_vbox
);
342 calendar
= gtk_calendar_new();
343 gtk_calendar_display_options(GTK_CALENDAR(calendar
),
344 GTK_CALENDAR_SHOW_HEADING
|
345 GTK_CALENDAR_SHOW_DAY_NAMES
|
346 GTK_CALENDAR_WEEK_START_MONDAY
);
347 gtk_calendar_select_month(GTK_CALENDAR(calendar
),
348 timeinfos
->tm_mon
, timeinfos
->tm_year
+ 1900);
349 gtk_calendar_select_day(GTK_CALENDAR(calendar
), timeinfos
->tm_mday
);
351 gtk_box_pack_start(GTK_BOX(main_vbox
), calendar
, TRUE
, TRUE
, 1);
352 gtk_signal_connect(GTK_OBJECT(calendar
), "day-selected-double-click",
353 GTK_SIGNAL_FUNC(cal_click
), NULL
);
354 gtk_signal_connect(GTK_OBJECT(calendar
), "day-selected-double-click",
355 GTK_SIGNAL_FUNC(toggle_displ
), NULL
);
356 gtk_signal_connect(GTK_OBJECT(calendar
), "month-changed",
357 GTK_SIGNAL_FUNC(mark_days
), NULL
);
358 gtk_widget_show(calendar
);
360 edit
= gtk_text_view_new();
361 gtk_text_view_set_editable(GTK_TEXT_VIEW(edit
), TRUE
);
362 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(edit
), GTK_WRAP_WORD_CHAR
);
364 gtk_box_pack_start(GTK_BOX(main_vbox
), edit
, TRUE
, TRUE
, 1);
367 /*** BOUTONS DE CHANGEMENT DE TEXTE ***/
368 text_buttons
= gtk_hbox_new(FALSE
, 1);
369 gtk_box_pack_start(GTK_BOX(main_vbox
), text_buttons
, FALSE
, TRUE
, 1);
372 button_u
= gtk_toggle_button_new();
373 gtk_signal_connect(GTK_OBJECT(button_u
), "clicked",
374 GTK_SIGNAL_FUNC(set_text_u
), NULL
);
375 gtk_box_pack_start(GTK_BOX(text_buttons
), button_u
, TRUE
, TRUE
, 0);
376 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_u
), TRUE
);
377 gtk_widget_show(button_u
);
378 label_u
= gtk_label_new(" Unique ");
379 gtk_widget_show(label_u
);
380 gtk_container_add(GTK_CONTAINER(button_u
), label_u
);
383 button_y
= gtk_toggle_button_new();
384 gtk_signal_connect(GTK_OBJECT(button_y
), "clicked",
385 GTK_SIGNAL_FUNC(set_text_y
), NULL
);
386 gtk_box_pack_start(GTK_BOX(text_buttons
), button_y
, TRUE
, TRUE
, 0);
387 gtk_widget_show(button_y
);
388 label_y
= gtk_label_new(" Yearly ");
389 gtk_widget_show(label_y
);
390 gtk_container_add(GTK_CONTAINER(button_y
), label_y
);
393 button_m
= gtk_toggle_button_new();
394 gtk_signal_connect(GTK_OBJECT(button_m
), "clicked",
395 GTK_SIGNAL_FUNC(set_text_m
), NULL
);
396 gtk_box_pack_start(GTK_BOX(text_buttons
), button_m
, TRUE
, TRUE
, 0);
397 gtk_widget_show(button_m
);
398 label_m
= gtk_label_new(" Monthly ");
399 gtk_widget_show(label_m
);
400 gtk_container_add(GTK_CONTAINER(button_m
), label_m
);
404 /*** BOUTONS DE SAUVEGARDE ET ANNULATION ***/
405 buttons_hbox
= gtk_hbox_new(FALSE
, 1);
406 gtk_box_pack_start(GTK_BOX(main_vbox
), buttons_hbox
, FALSE
, TRUE
, 1);
407 gtk_widget_show(buttons_hbox
);
410 closewindow
= gtk_button_new_with_label(" Close ");
411 gtk_signal_connect(GTK_OBJECT(closewindow
), "clicked",
412 GTK_SIGNAL_FUNC(quit_app
), NULL
);
413 gtk_box_pack_start(GTK_BOX(buttons_hbox
), closewindow
, TRUE
, TRUE
, 0);
414 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(closewindow
), GTK_CAN_DEFAULT
);
415 gtk_widget_grab_default(GTK_WIDGET(closewindow
));
416 gtk_widget_show(closewindow
);
419 save
= gtk_button_new_with_label(" Save ");
420 gtk_signal_connect(GTK_OBJECT(save
), "clicked",
421 GTK_SIGNAL_FUNC(save_datas
), NULL
);
422 gtk_box_pack_start(GTK_BOX(buttons_hbox
), save
, TRUE
, TRUE
, 0);
423 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(save
), GTK_CAN_DEFAULT
);
426 delete = gtk_button_new_with_label(" Delete ");
427 gtk_signal_connect(GTK_OBJECT(delete), "clicked",
428 GTK_SIGNAL_FUNC(delete_file
), NULL
);
429 gtk_box_pack_start(GTK_BOX(buttons_hbox
), delete, TRUE
, TRUE
, 0);
430 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(delete), GTK_CAN_DEFAULT
);
433 cancel
= gtk_button_new_with_label(" Close ");
434 gtk_signal_connect(GTK_OBJECT(cancel
), "clicked",
435 GTK_SIGNAL_FUNC(toggle_displ
), NULL
);
436 gtk_box_pack_start(GTK_BOX(buttons_hbox
), cancel
, TRUE
, TRUE
, 0);
437 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(cancel
), GTK_CAN_DEFAULT
);
440 /*** AFFICHAGE DE LA FENÊTRE ***/
441 gtk_widget_show(application
);