forked from juddmon/jpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweekview_gui.c
410 lines (340 loc) · 13.7 KB
/
weekview_gui.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*******************************************************************************
* weekview_gui.c
* A module of J-Pilot http://jpilot.org
*
* Copyright (C) 1999-2014 by Judd Montgomery
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
******************************************************************************/
/********************************* Includes ***********************************/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "pi-calendar.h"
#include "i18n.h"
#include "utils.h"
#include "prefs.h"
#include "log.h"
#include "datebook.h"
#include "calendar.h"
#include "print.h"
#include "jpilot.h"
/******************************* Global vars **********************************/
extern int datebk_category;
extern int glob_app;
extern GtkTooltips *glob_tooltips;
GtkWidget *weekview_window=NULL;
static GtkWidget *week_day_label[8];
static GtkWidget *week_day_text[8];
static GObject *week_day_text_buffer[8];
static struct tm glob_week_date;
/****************************** Prototypes ************************************/
static int clear_weeks_appts(GtkWidget **day_texts);
static int display_weeks_appts(struct tm *date_in, GtkWidget **day_texts);
/****************************** Main Code *************************************/
static gboolean cb_destroy(GtkWidget *widget)
{
weekview_window = NULL;
return FALSE;
}
void cb_weekview_quit(GtkWidget *widget, gpointer data)
{
int w, h;
gdk_window_get_size(weekview_window->window, &w, &h);
set_pref(PREF_WEEKVIEW_WIDTH, w, NULL, FALSE);
set_pref(PREF_WEEKVIEW_HEIGHT, h, NULL, FALSE);
gtk_widget_destroy(weekview_window);
}
/* This is where week printing is kicked off from */
static void cb_week_print(GtkWidget *widget, gpointer data)
{
long paper_size;
jp_logf(JP_LOG_DEBUG, "cb_week_print called\n");
if (print_gui(weekview_window, DATEBOOK, 2, 0x02) == DIALOG_SAID_PRINT) {
get_pref(PREF_PAPER_SIZE, &paper_size, NULL);
if (paper_size==1) {
print_weeks_appts(&glob_week_date, PAPER_A4);
} else {
print_weeks_appts(&glob_week_date, PAPER_Letter);
}
}
}
static void freeze_weeks_appts(void)
{
int i;
for (i=0; i<8; i++) {
gtk_widget_freeze_child_notify(week_day_text[i]);
}
}
static void thaw_weeks_appts(void)
{
int i;
for (i=0; i<8; i++) {
gtk_widget_thaw_child_notify(week_day_text[i]);
}
}
static void cb_week_move(GtkWidget *widget, gpointer data)
{
if (GPOINTER_TO_INT(data)==-1) {
sub_days_from_date(&glob_week_date, 7);
}
if (GPOINTER_TO_INT(data)==1) {
add_days_to_date(&glob_week_date, 7);
}
freeze_weeks_appts();
clear_weeks_appts(week_day_text);
display_weeks_appts(&glob_week_date, week_day_text);
thaw_weeks_appts();
}
static int clear_weeks_appts(GtkWidget **day_texts)
{
int i;
GObject *text_buffer;
for (i=0; i<8; i++) {
text_buffer = G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(day_texts[i])));
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(text_buffer), "", -1);
}
return EXIT_SUCCESS;
}
/*
* This function requires that date_in be the date of the first day of
* the week (be it a Sunday, or a Monday).
* It will then print the next eight days to the day_texts array of
* text boxes.
*/
static int display_weeks_appts(struct tm *date_in, GtkWidget **day_texts)
{
CalendarEventList *ce_list;
CalendarEventList *temp_cel;
struct tm date;
GtkWidget **text;
char desc[256];
char datef[20];
int n, i;
const char *svalue;
char str[82];
char str_dow[32];
char short_date[32];
char default_date[]="%x";
int now_today;
#ifdef ENABLE_DATEBK
int ret;
int cat_bit;
int db3_type;
long use_db3_tags;
struct db4_struct db4;
#endif
GObject *text_buffer;
char *markup_str;
ce_list = NULL;
text = day_texts;
memcpy(&date, date_in, sizeof(struct tm));
get_pref(PREF_SHORTDATE, NULL, &svalue);
if (svalue==NULL) {
svalue = default_date;
}
/* Label each day including special markup for TODAY */
for (i=0; i<8; i++, add_days_to_date(&date, 1)) {
strftime(short_date, sizeof(short_date), svalue, &date);
jp_strftime(str_dow, sizeof(str_dow), "%A", &date);
/* Determine today for highlighting */
now_today = get_highlighted_today(&date);
g_snprintf(str, sizeof(str), "%s %s", str_dow, short_date);
if (date.tm_mday == now_today)
{
markup_str = g_markup_printf_escaped("<b>%s</b>", str);
gtk_widget_set_name(GTK_WIDGET(text[i]), "today");
} else {
markup_str = g_markup_printf_escaped("%s", str);
gtk_widget_set_name(GTK_WIDGET(text[i]), "");
}
gtk_label_set_markup(GTK_LABEL(week_day_label[i]), markup_str);
g_free(markup_str);
}
/* Get all of the appointments */
get_days_calendar_events2(&ce_list, NULL, 2, 2, 2, CATEGORY_ALL, NULL);
memcpy(&date, date_in, sizeof(struct tm));
/* Iterate through 8 days */
for (n=0; n<8; n++, add_days_to_date(&date, 1)) {
text_buffer = G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text[n])));
for (temp_cel = ce_list; temp_cel; temp_cel=temp_cel->next) {
#ifdef ENABLE_DATEBK
get_pref(PREF_USE_DB3, &use_db3_tags, NULL);
if (use_db3_tags) {
ret = db3_parse_tag(temp_cel->mcale.cale.note, &db3_type, &db4);
jp_logf(JP_LOG_DEBUG, "category = 0x%x\n", db4.category);
cat_bit=1<<db4.category;
if (!(cat_bit & datebk_category)) {
jp_logf(JP_LOG_DEBUG, "skipping rec not in this category\n");
continue;
}
}
#endif
if (calendar_isApptOnDate(&(temp_cel->mcale.cale), &date)) {
if (temp_cel->mcale.cale.event) {
strcpy(desc, "*");
} else {
get_pref_time_no_secs(datef);
strftime(desc, sizeof(desc), datef, &(temp_cel->mcale.cale.begin));
strcat(desc, " ");
}
if (temp_cel->mcale.cale.description) {
strncat(desc, temp_cel->mcale.cale.description, 70);
/* FIXME: This kind of truncation is bad for UTF-8 */
desc[62]='\0';
}
/* FIXME: Add location in parentheses (loc) as the Palm does.
* We would need to check strlen, etc., before adding */
remove_cr_lfs(desc);
/* Append number of anniversary years if enabled & appropriate */
append_anni_years(desc, 62, &date, NULL, &temp_cel->mcale.cale);
strcat(desc, "\n");
gtk_text_buffer_insert_at_cursor(GTK_TEXT_BUFFER(text_buffer),desc,-1);
}
}
}
free_CalendarEventList(&ce_list);
return EXIT_SUCCESS;
}
/* Called when a day is clicked on the week view */
static void cb_enter_selected_day(GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
struct tm date;
if (glob_app != DATEBOOK)
return;
date = glob_week_date;
/* Calculate the date which the user has clicked on */
add_days_to_date(&date, GPOINTER_TO_INT(data));
/* Redisplay the day view based on this date */
datebook_gui_setdate(date.tm_year, date.tm_mon, date.tm_mday);
}
void weekview_gui(struct tm *date_in)
{
GtkWidget *button;
GtkWidget *align;
GtkWidget *vbox, *hbox;
GtkWidget *hbox_temp;
GtkWidget *vbox_left, *vbox_right;
GtkAccelGroup *accel_group;
long fdow;
int i;
char title[200];
long w, h, show_tooltips;
if (weekview_window) {
/* Delete any existing window to ensure that new window is biased
* around currently selected date and so that the new window
* contents are updated with any changes on the day view. */
gtk_widget_destroy(weekview_window);
}
memcpy(&glob_week_date, date_in, sizeof(struct tm));
get_pref(PREF_WEEKVIEW_WIDTH, &w, NULL);
get_pref(PREF_WEEKVIEW_HEIGHT, &h, NULL);
get_pref(PREF_SHOW_TOOLTIPS, &show_tooltips, NULL);
g_snprintf(title, sizeof(title), "%s %s", PN, _("Weekly View"));
weekview_window = gtk_widget_new(GTK_TYPE_WINDOW,
"type", GTK_WINDOW_TOPLEVEL,
"title", title,
NULL);
gtk_window_set_default_size(GTK_WINDOW(weekview_window), w, h);
gtk_container_set_border_width(GTK_CONTAINER(weekview_window), 10);
gtk_signal_connect(GTK_OBJECT(weekview_window), "destroy",
GTK_SIGNAL_FUNC(cb_destroy), weekview_window);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(weekview_window), vbox);
/* Make accelerators for some buttons window */
accel_group = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(gtk_widget_get_toplevel(vbox)), accel_group);
/* This box has the close button and arrows in it */
align = gtk_alignment_new(0.5, 0.5, 0, 0);
gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, FALSE, 0);
hbox_temp = gtk_hbutton_box_new();
gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbox_temp), 6);
gtk_container_set_border_width(GTK_CONTAINER(hbox_temp), 6);
gtk_container_add(GTK_CONTAINER(align), hbox_temp);
/* Make a left arrow for going back a week */
button = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_week_move),
GINT_TO_POINTER(-1));
gtk_box_pack_start(GTK_BOX(hbox_temp), button, FALSE, FALSE, 0);
/* Accelerator key for left arrow */
gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel_group,
GDK_Left, GDK_MOD1_MASK, GTK_ACCEL_VISIBLE);
set_tooltip(show_tooltips, glob_tooltips,
button, _("Last week Alt+LeftArrow"), NULL);
/* Close button */
button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_weekview_quit), NULL);
/* Closing the window via a delete event uses the same cleanup routine */
gtk_signal_connect(GTK_OBJECT(weekview_window), "delete_event",
GTK_SIGNAL_FUNC(cb_weekview_quit), NULL);
gtk_box_pack_start(GTK_BOX(hbox_temp), button, FALSE, FALSE, 0);
/* Print button */
button = gtk_button_new_from_stock(GTK_STOCK_PRINT);
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_week_print), weekview_window);
gtk_box_pack_start(GTK_BOX(hbox_temp), button, FALSE, FALSE, 0);
/* Make a right arrow for going forward a week */
button = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_week_move),
GINT_TO_POINTER(1));
gtk_box_pack_start(GTK_BOX(hbox_temp), button, FALSE, FALSE, 0);
/* Accelerator key for right arrow */
gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel_group,
GDK_Right, GDK_MOD1_MASK, GTK_ACCEL_VISIBLE);
set_tooltip(show_tooltips, glob_tooltips,
button, _("Next week Alt+RightArrow"), NULL);
get_pref(PREF_FDOW, &fdow, NULL);
hbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
vbox_left = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), vbox_left, TRUE, TRUE, 0);
vbox_right = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), vbox_right, TRUE, TRUE, 0);
/* Get the first day of the week */
sub_days_from_date(&glob_week_date, (7 - fdow + glob_week_date.tm_wday)%7);
/* Make 8 boxes, 1 for each day, to hold appt. descriptions */
for (i=0; i<8; i++) {
week_day_label[i] = gtk_label_new("");
gtk_misc_set_alignment(GTK_MISC(week_day_label[i]), 0.0, 0.5);
week_day_text[i] = gtk_text_view_new();
week_day_text_buffer[i] = G_OBJECT(gtk_text_view_get_buffer(GTK_TEXT_VIEW(week_day_text[i])));
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(week_day_text[i]), FALSE);
gtk_text_view_set_editable(GTK_TEXT_VIEW(week_day_text[i]), FALSE);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(week_day_text[i]), GTK_WRAP_WORD);
gtk_container_set_border_width(GTK_CONTAINER(week_day_text[i]), 1);
gtk_text_buffer_create_tag(GTK_TEXT_BUFFER(week_day_text_buffer[i]),
"gray_background", "background", "gray",
NULL);
gtk_widget_set_usize(GTK_WIDGET(week_day_text[i]), 10, 10);
gtk_signal_connect(GTK_OBJECT(week_day_text[i]), "button_release_event",
GTK_SIGNAL_FUNC(cb_enter_selected_day),
GINT_TO_POINTER(i));
if (i>3) {
gtk_box_pack_start(GTK_BOX(vbox_right), week_day_label[i], FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox_right), week_day_text[i], TRUE, TRUE, 0);
} else {
gtk_box_pack_start(GTK_BOX(vbox_left), week_day_label[i], FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox_left), week_day_text[i], TRUE, TRUE, 0);
}
}
display_weeks_appts(&glob_week_date, week_day_text);
gtk_widget_show_all(weekview_window);
}