aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/widgets/e-minicard-view-widget.c
blob: e6f97917317e5706afaa4bec599971925302b474 (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include <config.h>

#include <gtk/gtk.h>
#include <misc/e-canvas-background.h>
#include <misc/e-canvas.h>
#include <glib/gi18n.h>

#include "e-util/e-util.h"
#include "e-minicard-view-widget.h"

static void e_minicard_view_widget_init      (EMinicardViewWidget        *widget);
static void e_minicard_view_widget_class_init    (EMinicardViewWidgetClass   *class);
static void e_minicard_view_widget_set_property  (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void e_minicard_view_widget_get_property  (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static void e_minicard_view_widget_dispose       (GObject *object);
static void e_minicard_view_widget_reflow        (ECanvas *canvas);
static void e_minicard_view_widget_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
static void e_minicard_view_widget_style_set     (GtkWidget *widget, GtkStyle *previous_style);
static void e_minicard_view_widget_realize       (GtkWidget *widget);
static gboolean e_minicard_view_widget_real_focus_in_event (GtkWidget *widget, GdkEventFocus *event);

static gpointer parent_class;

/* The arguments we take */
enum {
    PROP_0,
    PROP_BOOK,
    PROP_QUERY,
    PROP_EDITABLE,
    PROP_COLUMN_WIDTH
};

enum {
    CREATE_CONTACT,
    CREATE_CONTACT_LIST,
    SELECTION_CHANGE,
    COLUMN_WIDTH_CHANGED,
    RIGHT_CLICK,
    LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = {0, };

GType
e_minicard_view_widget_get_type (void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info =  {
            sizeof (EMinicardViewWidgetClass),
            NULL,           /* base_init */
            NULL,           /* base_finalize */
            (GClassInitFunc) e_minicard_view_widget_class_init,
            NULL,           /* class_finalize */
            NULL,           /* class_data */
            sizeof (EMinicardViewWidget),
            0,             /* n_preallocs */
            (GInstanceInitFunc) e_minicard_view_widget_init,
        };

        type = g_type_register_static (e_canvas_get_type (), "EMinicardViewWidget", &info, 0);
    }

    return type;
}

static void
e_minicard_view_widget_class_init (EMinicardViewWidgetClass *class)
{
    GObjectClass *object_class;
    GtkWidgetClass *widget_class;
    ECanvasClass *canvas_class;

    parent_class = g_type_class_peek_parent (class);

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = e_minicard_view_widget_set_property;
    object_class->get_property = e_minicard_view_widget_get_property;
    object_class->dispose = e_minicard_view_widget_dispose;

    widget_class = GTK_WIDGET_CLASS (class);
    widget_class->style_set = e_minicard_view_widget_style_set;
    widget_class->realize = e_minicard_view_widget_realize;
    widget_class->size_allocate = e_minicard_view_widget_size_allocate;
    widget_class->focus_in_event = e_minicard_view_widget_real_focus_in_event;

    canvas_class = E_CANVAS_CLASS (class);
    canvas_class->reflow = e_minicard_view_widget_reflow;

    class->selection_change = NULL;
    class->column_width_changed = NULL;
    class->right_click = NULL;

    g_object_class_install_property (object_class, PROP_BOOK,
                     g_param_spec_object ("book",
                                  _("Book"),
                                  /*_( */"XXX blurb" /*)*/,
                                  E_TYPE_BOOK,
                                  G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_QUERY,
                     g_param_spec_string ("query",
                                  _("Query"),
                                  /*_( */"XXX blurb" /*)*/,
                                  NULL,
                                  G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_EDITABLE,
                     g_param_spec_boolean ("editable",
                                   _("Editable"),
                                   /*_( */"XXX blurb" /*)*/,
                                   FALSE,
                                   G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_COLUMN_WIDTH,
                     g_param_spec_double ("column_width",
                                  _("Column Width"),
                                  /*_( */"XXX blurb" /*)*/,
                                  0.0, G_MAXDOUBLE, 150.0,
                                  G_PARAM_READWRITE));

    signals [CREATE_CONTACT] =
        g_signal_new ("create-contact",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EMinicardViewWidgetClass, create_contact),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

    signals [CREATE_CONTACT_LIST] =
        g_signal_new ("create-contact-list",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EMinicardViewWidgetClass, create_contact_list),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

    signals [SELECTION_CHANGE] =
        g_signal_new ("selection_change",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EMinicardViewWidgetClass, selection_change),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

    signals [COLUMN_WIDTH_CHANGED] =
        g_signal_new ("column_width_changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EMinicardViewWidgetClass, column_width_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__DOUBLE,
                  G_TYPE_NONE, 1, G_TYPE_DOUBLE);

    signals [RIGHT_CLICK] =
        g_signal_new ("right_click",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EMinicardViewWidgetClass, right_click),
                  NULL, NULL,
                  e_marshal_INT__POINTER,
                  G_TYPE_INT, 1, G_TYPE_POINTER);
}

static void
e_minicard_view_widget_init (EMinicardViewWidget *view)
{
    view->emv = NULL;

    view->book = NULL;
    view->query = NULL;
    view->editable = FALSE;
    view->column_width = 150;
}

GtkWidget *
e_minicard_view_widget_new (EAddressbookReflowAdapter *adapter)
{
    EMinicardViewWidget *widget = E_MINICARD_VIEW_WIDGET (g_object_new (e_minicard_view_widget_get_type (), NULL));

    widget->adapter = adapter;
    g_object_ref (widget->adapter);

    return GTK_WIDGET (widget);
}

static void
e_minicard_view_widget_set_property (GObject *object,
                     guint prop_id,
                     const GValue *value,
                     GParamSpec *pspec)
{
    EMinicardViewWidget *emvw;

    emvw = E_MINICARD_VIEW_WIDGET (object);

    switch (prop_id) {
    case PROP_BOOK:
        if (emvw->book)
            g_object_unref (emvw->book);
        if (g_value_get_object (value)) {
            emvw->book = E_BOOK(g_value_get_object (value));
            if (emvw->book)
                g_object_ref(emvw->book);
        } else
            emvw->book = NULL;
        if (emvw->emv)
            g_object_set(emvw->emv,
                     "book", emvw->book,
                       NULL);
        break;
    case PROP_QUERY:
        emvw->query = g_strdup(g_value_get_string (value));
        if (emvw->emv)
            g_object_set(emvw->emv,
                     "query", emvw->query,
                     NULL);
        break;
    case PROP_EDITABLE:
        emvw->editable = g_value_get_boolean (value);
        if (emvw->emv)
            g_object_set (emvw->emv,
                      "editable", emvw->editable,
                      NULL);
        break;
    case PROP_COLUMN_WIDTH:
        emvw->column_width = g_value_get_double (value);
        if (emvw->emv) {
            g_object_set (emvw->emv,
                      "column_width", emvw->column_width,
                      NULL);
        }
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
e_minicard_view_widget_get_property (GObject *object,
                     guint prop_id,
                     GValue *value,
                     GParamSpec *pspec)
{
    EMinicardViewWidget *emvw;

    emvw = E_MINICARD_VIEW_WIDGET (object);

    switch (prop_id) {
    case PROP_BOOK:
        g_value_set_object (value, emvw->book);
        break;
    case PROP_QUERY:
        g_value_set_string (value, emvw->query);
        break;
    case PROP_EDITABLE:
        g_value_set_boolean (value, emvw->editable);
        break;
    case PROP_COLUMN_WIDTH:
        g_value_set_double (value, emvw->column_width);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
e_minicard_view_widget_dispose (GObject *object)
{
    EMinicardViewWidget *view = E_MINICARD_VIEW_WIDGET(object);

    if (view->book) {
        g_object_unref (view->book);
        view->book = NULL;
    }
    if (view->query) {
        g_free(view->query);
        view->query = NULL;
    }

    if (view->adapter) {
        g_object_unref (view->adapter);
        view->adapter = NULL;
    }

    if (G_OBJECT_CLASS(parent_class)->dispose)
        G_OBJECT_CLASS(parent_class)->dispose (object);
}

static void
selection_change (ESelectionModel *esm, EMinicardViewWidget *widget)
{
    g_signal_emit (widget,
               signals [SELECTION_CHANGE], 0);
}

static void
selection_row_change (ESelectionModel *esm, gint row, EMinicardViewWidget *widget)
{
    selection_change (esm, widget);
}

static void
column_width_changed (ESelectionModel *esm, double width, EMinicardViewWidget *widget)
{
    g_signal_emit (widget,
               signals [COLUMN_WIDTH_CHANGED], 0, width);
}

static void
create_contact (EMinicardView *view, EMinicardViewWidget *widget)
{
    g_signal_emit (widget, signals[CREATE_CONTACT], 0);
}

static void
create_contact_list (EMinicardView *view, EMinicardViewWidget *widget)
{
    g_signal_emit (widget, signals[CREATE_CONTACT_LIST], 0);
}

static guint
right_click (EMinicardView *view, GdkEvent *event, EMinicardViewWidget *widget)
{
    guint ret_val;
    g_signal_emit (widget,
               signals [RIGHT_CLICK], 0,
               event, &ret_val);
    return ret_val;
}

static void
e_minicard_view_widget_style_set (GtkWidget *widget, GtkStyle *previous_style)
{
    EMinicardViewWidget *view = E_MINICARD_VIEW_WIDGET(widget);

    if (view->background)
        gnome_canvas_item_set (view->background,
                       "fill_color_gdk", &widget->style->base[GTK_STATE_NORMAL],
                       NULL );

    if (GTK_WIDGET_CLASS(parent_class)->style_set)
        GTK_WIDGET_CLASS(parent_class)->style_set (widget, previous_style);
}

static void
e_minicard_view_widget_realize (GtkWidget *widget)
{
    EMinicardViewWidget *view = E_MINICARD_VIEW_WIDGET(widget);
    GtkStyle *style = gtk_widget_get_style (widget);

    view->background = gnome_canvas_item_new(gnome_canvas_root( GNOME_CANVAS(view) ),
                         e_canvas_background_get_type(),
                         "fill_color_gdk", &style->base[GTK_STATE_NORMAL],
                         NULL );

    view->emv = gnome_canvas_item_new(
        gnome_canvas_root( GNOME_CANVAS(view) ),
        e_minicard_view_get_type(),
        "height", (double) 100,
        "minimum_width", (double) 100,
        "adapter", view->adapter,
        "column_width", view->column_width,
        NULL );

    g_signal_connect (E_REFLOW(view->emv)->selection,
              "selection_changed",
              G_CALLBACK (selection_change), view);
    g_signal_connect (E_REFLOW(view->emv)->selection,
              "selection_row_changed",
              G_CALLBACK (selection_row_change), view);
    g_signal_connect (view->emv,
              "column_width_changed",
              G_CALLBACK (column_width_changed), view);
    g_signal_connect (view->emv,
              "create-contact",
              G_CALLBACK (create_contact), view);
    g_signal_connect (view->emv,
              "create-contact-list",
              G_CALLBACK (create_contact_list), view);
    g_signal_connect (view->emv,
              "right_click",
              G_CALLBACK (right_click), view);

    if (GTK_WIDGET_CLASS(parent_class)->realize)
        GTK_WIDGET_CLASS(parent_class)->realize (widget);
}

static void
e_minicard_view_widget_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
    if (GTK_WIDGET_CLASS(parent_class)->size_allocate)
        GTK_WIDGET_CLASS(parent_class)->size_allocate (widget, allocation);

#if GTK_CHECK_VERSION(2,19,7)
    if (gtk_widget_get_realized (widget)) {
#else
    if (GTK_WIDGET_REALIZED(widget)) {
#endif
        gdouble width;
        EMinicardViewWidget *view = E_MINICARD_VIEW_WIDGET(widget);

        gnome_canvas_item_set( view->emv,
                       "height", (double) allocation->height,
                       NULL );
        gnome_canvas_item_set( view->emv,
                       "minimum_width", (double) allocation->width,
                       NULL );
        g_object_get(view->emv,
                 "width", &width,
                 NULL);
        width = MAX(width, allocation->width);
        gnome_canvas_set_scroll_region (GNOME_CANVAS (view), 0, 0, width - 1, allocation->height - 1);
    }
}

static void
e_minicard_view_widget_reflow(ECanvas *canvas)
{
    gdouble width;
    EMinicardViewWidget *view = E_MINICARD_VIEW_WIDGET(canvas);

    if (E_CANVAS_CLASS(parent_class)->reflow)
        E_CANVAS_CLASS(parent_class)->reflow (canvas);

    g_object_get(view->emv,
             "width", &width,
             NULL);
    width = MAX(width, GTK_WIDGET(canvas)->allocation.width);
    gnome_canvas_set_scroll_region(GNOME_CANVAS(canvas), 0, 0, width - 1, GTK_WIDGET(canvas)->allocation.height - 1);
}

ESelectionModel *
e_minicard_view_widget_get_selection_model (EMinicardViewWidget *view)
{
    if (view->emv)
        return E_SELECTION_MODEL (E_REFLOW (view->emv)->selection);
    else
        return NULL;
}

EMinicardView *
e_minicard_view_widget_get_view             (EMinicardViewWidget       *view)
{
    if (view->emv)
        return E_MINICARD_VIEW (view->emv);
    else
        return NULL;
}

static gboolean
e_minicard_view_widget_real_focus_in_event(GtkWidget *widget, GdkEventFocus *event)
{
    GnomeCanvas *canvas;
    EMinicardViewWidget *view;

    canvas = GNOME_CANVAS (widget);
    view = E_MINICARD_VIEW_WIDGET(widget);

    if (!canvas->focused_item) {
        EReflow *reflow = E_REFLOW (view->emv);
        if (reflow->count) {
            gint unsorted = e_sorter_sorted_to_model (E_SORTER (reflow->sorter), 0);

            if (unsorted != -1)
                canvas->focused_item = reflow->items [unsorted];
        }
    }

    if (GTK_WIDGET_CLASS(parent_class)->focus_in_event)
        return GTK_WIDGET_CLASS(parent_class)->focus_in_event (widget, event);

    return FALSE;

}