aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/gncal-day-view.c
blob: 76cd00e90f645fd0016ce647498d10a69c61801b (plain) (blame)
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
/* Day view widget for gncal
 *
 * Copyright (C) 1998 The Free Software Foundation
 *
 * Authors: Federico Mena   <quartic@gimp.org>
 *          Miguel de Icaza <miguel@kernel.org>
 */

#include <gnome.h>
#include "gncal-day-view.h"
#include "timeutil.h"
#include "view-utils.h"
#include "main.h"
#include "eventedit.h"
#include "popup-menu.h"


#define TEXT_BORDER 2
#define MIN_INFO_WIDTH 50


static void gncal_day_view_class_init   (GncalDayViewClass *class);
static void gncal_day_view_init         (GncalDayView      *dview);
static void gncal_day_view_destroy      (GtkObject         *object);
static void gncal_day_view_realize      (GtkWidget         *widget);
static void gncal_day_view_size_request (GtkWidget         *widget,
                     GtkRequisition    *requisition);
static gint gncal_day_view_expose       (GtkWidget         *widget,
                     GdkEventExpose    *event);
static gint gncal_day_view_button_press (GtkWidget         *widget,
                     GdkEventButton    *event);


static GtkWidgetClass *parent_class;


guint
gncal_day_view_get_type (void)
{
    static guint day_view_type = 0;

    if (!day_view_type) {
        GtkTypeInfo day_view_info = {
            "GncalDayView",
            sizeof (GncalDayView),
            sizeof (GncalDayViewClass),
            (GtkClassInitFunc) gncal_day_view_class_init,
            (GtkObjectInitFunc) gncal_day_view_init,
            (GtkArgSetFunc) NULL,
            (GtkArgGetFunc) NULL
        };

        day_view_type = gtk_type_unique (gtk_widget_get_type (), &day_view_info);
    }

    return day_view_type;
}

static void
gncal_day_view_class_init (GncalDayViewClass *class)
{
    GtkObjectClass *object_class;
    GtkWidgetClass *widget_class;

    object_class = (GtkObjectClass *) class;
    widget_class = (GtkWidgetClass *) class;

    parent_class = gtk_type_class (gtk_widget_get_type ());

    object_class->destroy = gncal_day_view_destroy;

    widget_class->realize = gncal_day_view_realize;
    widget_class->size_request = gncal_day_view_size_request;
    widget_class->expose_event = gncal_day_view_expose;
    widget_class->button_press_event = gncal_day_view_button_press;
}

static void
gncal_day_view_init (GncalDayView *dview)
{
    GTK_WIDGET_UNSET_FLAGS (dview, GTK_NO_WINDOW);

    dview->calendar = NULL;

    dview->lower = 0;
    dview->upper = 0;

    dview->shadow_type = GTK_SHADOW_ETCHED_IN;
}

static void
gncal_day_view_destroy (GtkObject *object)
{
    GncalDayView *dview;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNCAL_IS_DAY_VIEW (object));

    dview = GNCAL_DAY_VIEW (object);

    if (dview->day_str)
        g_free (dview->day_str);
    if (dview->events)
        calendar_destroy_event_list (dview->events);
    
    if (GTK_OBJECT_CLASS (parent_class)->destroy)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}

static int
new_appointment (GtkWidget *widget, gpointer data)
{
    GncalDayView *dayview = GNCAL_DAY_VIEW (data);
    iCalObject *ico;
    GtkWidget *ee;
    struct tm tm;
    
    ico = ical_new ("", user_name, "");
    ico->new = 1;

    tm = *localtime (&dayview->lower);
    tm.tm_hour = day_begin;
    ico->dtstart = mktime (&tm);
    tm.tm_hour++;
    ico->dtend   = mktime (&tm);
    ee = event_editor_new (dayview->calendar, ico);
    gtk_widget_show (ee);
    return 1;
}

static void
context_menu (GncalDayView *dayview, GdkEventButton *event)
{
    static struct menu_item main_items[] = {
        { N_("New appointment..."), (GtkSignalFunc) new_appointment, NULL, TRUE }
    };

    main_items [0].data = dayview;
    popup_menu (main_items, 1, event);
}

static void
switch_to_day (GncalDayView *dayview)
{
}

static gint
gncal_day_view_button_press (GtkWidget *widget, GdkEventButton *event)
{
    GncalDayView *dayview;

    dayview = GNCAL_DAY_VIEW (widget);

    if (event->button == 3)
        context_menu (dayview, event);

    if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
        gnome_calendar_dayjump (dayview->calendar, dayview->lower);

    return TRUE;
}

GtkWidget *
gncal_day_view_new (GnomeCalendar *calendar, time_t lower, time_t upper)
{
    GncalDayView *dview;

    g_return_val_if_fail (calendar != NULL, NULL);

    dview = gtk_type_new (gncal_day_view_get_type ());

    dview->calendar = calendar;
    dview->lower    = lower;
    dview->upper    = upper;
    dview->events   = 0;

    gncal_day_view_update (dview, NULL, 0);

    return GTK_WIDGET (dview);
}

static void
gncal_day_view_realize (GtkWidget *widget)
{
    GdkWindowAttr attributes;
    gint attributes_mask;

    g_return_if_fail (widget != NULL);
    g_return_if_fail (GNCAL_IS_DAY_VIEW (widget));

    GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

    attributes.window_type = GDK_WINDOW_CHILD;
    attributes.x = widget->allocation.x;
    attributes.y = widget->allocation.y;
    attributes.width = widget->allocation.width;
    attributes.height = widget->allocation.height;
    attributes.wclass = GDK_INPUT_OUTPUT;
    attributes.visual = gtk_widget_get_visual (widget);
    attributes.colormap = gtk_widget_get_colormap (widget);
    attributes.event_mask = (gtk_widget_get_events (widget)
                 | GDK_EXPOSURE_MASK
                 | GDK_BUTTON_PRESS_MASK);

    attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

    widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
    gdk_window_set_user_data (widget->window, widget);

    widget->style = gtk_style_attach (widget->style, widget->window);

    gdk_window_set_background (widget->window, &widget->style->bg[GTK_STATE_PRELIGHT]);
}

static void
gncal_day_view_size_request (GtkWidget *widget, GtkRequisition *requisition)
{
    GncalDayView *dview;
    int str_width, width;

    g_return_if_fail (widget != NULL);
    g_return_if_fail (GNCAL_IS_DAY_VIEW (widget));
    g_return_if_fail (requisition != NULL);

    dview = GNCAL_DAY_VIEW (widget);

    /* border and min width */

    str_width = gdk_string_width (widget->style->font, dview->day_str);

    width = MAX (MIN_INFO_WIDTH, str_width);

    requisition->width = 2 * (widget->style->klass->xthickness + TEXT_BORDER) + width;
    requisition->height = 2 * (widget->style->klass->ythickness + TEXT_BORDER);

    /* division line */

    requisition->height += 2 * TEXT_BORDER + widget->style->klass->ythickness;

    /* title and at least one line of text */

    requisition->height += 2 * (widget->style->font->ascent + widget->style->font->descent);
}

static gint
gncal_day_view_expose (GtkWidget *widget, GdkEventExpose *event)
{
    GncalDayView *dview;
    int x1, y1, width, height;
    GdkRectangle rect, dest;
    GdkFont *font;
    int str_width;
    GdkGC *gc;

    g_return_val_if_fail (widget != NULL, FALSE);
    g_return_val_if_fail (GNCAL_IS_DAY_VIEW (widget), FALSE);
    g_return_val_if_fail (event != NULL, FALSE);

    if (!GTK_WIDGET_DRAWABLE (widget))
        return FALSE;

    gc = widget->style->fg_gc [GTK_STATE_NORMAL];
    dview = GNCAL_DAY_VIEW (widget);

    x1 = widget->style->klass->xthickness;
    y1 = widget->style->klass->ythickness;
    width = widget->allocation.width - 2 * x1;
    height = widget->allocation.height - 2 * y1;

    /* Clear and paint frame shadow */

    gdk_window_clear_area (widget->window,
                   event->area.x, event->area.y,
                   event->area.width, event->area.height);

    gtk_draw_shadow (widget->style, widget->window,
             GTK_STATE_NORMAL, dview->shadow_type,
             0, 0,
             widget->allocation.width,
             widget->allocation.height);

    /* Clear and paint title */

    font = widget->style->font;

    rect.x = x1;
    rect.y = y1;
    rect.width = width;
    rect.height = 2 * TEXT_BORDER + font->ascent + font->descent;

    if (gdk_rectangle_intersect (&rect, &event->area, &dest)) {
        gdk_draw_rectangle (widget->window,
                    widget->style->bg_gc[GTK_STATE_NORMAL],
                    TRUE,
                    dest.x, dest.y,
                    dest.width, dest.height);

        dest = rect;

        dest.x += TEXT_BORDER;
        dest.y += TEXT_BORDER;
        dest.width -= 2 * TEXT_BORDER;
        dest.height -= 2 * TEXT_BORDER;

        gdk_gc_set_clip_rectangle (gc, &dest);

        str_width = gdk_string_width (font, dview->day_str);
        
        gdk_draw_string (widget->window, font, gc,
                 dest.x + (dest.width - str_width) / 2,
                 dest.y + font->ascent,
                 dview->day_str);

        gdk_gc_set_clip_rectangle (gc, NULL);
    }

    /* Division line */

    gtk_draw_hline (widget->style,
            widget->window,
            GTK_STATE_NORMAL,
            rect.x,
            rect.x + rect.width - 1,
            rect.y + rect.height);

    /* Text */

    if (dview->events != NULL){
        rect.x = x1 + TEXT_BORDER;
        rect.y = y1 + 3 * TEXT_BORDER +
            font->ascent + font->descent +
            widget->style->klass->ythickness;
        rect.width = width - 2 * TEXT_BORDER;
        rect.height = height - (rect.y - y1) - TEXT_BORDER;
        
        if (gdk_rectangle_intersect (&rect, &event->area, &dest))
            view_utils_draw_events (
                widget, widget->window, gc,
                &rect,
                VIEW_UTILS_DRAW_END | VIEW_UTILS_DRAW_SPLIT,
                dview->events,
                dview->lower,
                dview->upper);
    }

    return FALSE;
}

void
gncal_day_view_update (GncalDayView *dview, iCalObject *ico, int flags)
{
    struct tm tm;
    char buf[256];

    g_return_if_fail (dview != NULL);
    g_return_if_fail (GNCAL_IS_DAY_VIEW (dview));
    g_return_if_fail (dview->calendar->cal);

    if (dview->day_str)
        g_free (dview->day_str);

    tm = *localtime (&dview->lower);
    strftime (buf, sizeof (buf)-1, "%A %d", &tm);
    dview->day_str = g_strdup (buf);

    if (dview->events)
        calendar_destroy_event_list (dview->events);

    dview->events = calendar_get_events_in_range (dview->calendar->cal,
                              dview->lower,
                              dview->upper);

    gtk_widget_draw (GTK_WIDGET (dview), NULL);
}

void
gncal_day_view_set_bounds (GncalDayView *dview, time_t lower, time_t upper)
{
    g_return_if_fail (dview != NULL);
    g_return_if_fail (GNCAL_IS_DAY_VIEW (dview));

    if ((lower != dview->lower) || (upper != dview->upper)) {
        dview->lower = lower;
        dview->upper = upper;

        gncal_day_view_update (dview, NULL, 0);
    }
}

void
gncal_day_view_set_shadow (GncalDayView *dview, GtkShadowType shadow_type)
{
    g_return_if_fail (dview != NULL);
    g_return_if_fail (GNCAL_IS_DAY_VIEW (dview));

    if (shadow_type != dview->shadow_type) {
        dview->shadow_type = shadow_type;

        gtk_widget_draw (GTK_WIDGET (dview), NULL);
    }
}