aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-multi-config-dialog.c
blob: a843a0178c949dd560712ee447d766e5d8429716 (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
505
506
507
508
509
510
511
512
513
514
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-multi-config-dialog.c
 *
 * Copyright (C) 2002  Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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.
 *
 * Author: Ettore Perazzoli <ettore@ximian.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-multi-config-dialog.h"

#include "e-clipped-label.h"

#include <gal/e-table/e-table-scrolled.h>
#include <gal/e-table/e-table-memory-callbacks.h>

#include <gdk-pixbuf/gdk-pixbuf.h>


#define PARENT_TYPE gnome_dialog_get_type ()
static GnomeDialogClass *parent_class = NULL;


struct _Page {
    char *title;
    char *description;
    GdkPixbuf *icon;
    EConfigPage *page_widget;
};
typedef struct _Page Page;

struct _EMultiConfigDialogPrivate {
    GSList *pages;

    GtkWidget *list_e_table;
    ETableModel *list_e_table_model;

    GtkWidget *notebook;

    int num_unapplied;
};


/* ETable stuff.  */

static char *list_e_table_spec =
    "<ETableSpecification cursor-mode=\"line\""
    "             selection-mode=\"browse\""
    "                     no-headers=\"true\">"
    "  <ETableColumn model_col=\"0\""
    "            expansion=\"1.0\""
    "                cell=\"string\""
    "                minimum_width=\"32\""
    "                resizable=\"true\""
    "            _title=\"blah\""
    "                compare=\"string\"/>"
    "  <ETableState>"
    "    <column source=\"0\"/>"
    "    <grouping>"
    "      <leaf column=\"0\" ascending=\"true\"/>"
    "    </grouping>"
    "  </ETableState>"
    "</ETableSpecification>";


/* Button handling.  */

static void
update_buttons (EMultiConfigDialog *dialog)
{
    EMultiConfigDialogPrivate *priv;

    priv = dialog->priv;

    if (priv->num_unapplied > 0) {
        gnome_dialog_set_sensitive (GNOME_DIALOG (dialog), 0, TRUE); /* OK */
        gnome_dialog_set_sensitive (GNOME_DIALOG (dialog), 1, TRUE); /* Apply */
    } else {
        gnome_dialog_set_sensitive (GNOME_DIALOG (dialog), 0, FALSE); /* OK */
        gnome_dialog_set_sensitive (GNOME_DIALOG (dialog), 1, FALSE); /* Apply */
    }
}


/* Page handling.  */

static Page *
page_new (const char *title,
      const char *description,
      GdkPixbuf *icon,
      EConfigPage *page_widget)
{
    Page *new;

    new = g_new (Page, 1);
    new->title       = g_strdup (title);
    new->description = g_strdup (description);
    new->icon        = icon;
    new->page_widget = page_widget;

    if (icon != NULL)
        gdk_pixbuf_ref (icon);

    return new;
}

static void
page_free (Page *page)
{
    g_free (page->title);
    g_free (page->description);

    if (page->icon != NULL)
        gdk_pixbuf_unref (page->icon);

    g_free (page);
}

static GtkWidget *
create_page_container (const char *description,
               GtkWidget *widget)
{
    GtkWidget *vbox;
    GtkWidget *label;
    GtkWidget *separator;

    vbox = gtk_vbox_new (FALSE, 3);
    gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);

    label = e_clipped_label_new (description);
    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
    gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);

    separator = gtk_hseparator_new ();
    gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, TRUE, 0);

    gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);

    gtk_widget_show (label);
    gtk_widget_show (separator);
    gtk_widget_show (widget);
    gtk_widget_show (vbox);

    return vbox;
}

/* Page callbacks.  */

static void
page_changed_callback (EConfigPage *page,
               void *data)
{
    EMultiConfigDialog *dialog;
    EMultiConfigDialogPrivate *priv;

    dialog = E_MULTI_CONFIG_DIALOG (data);
    priv = dialog->priv;

    priv->num_unapplied ++;

    update_buttons (dialog);
}


/* Button handling.  */

static void
do_close (EMultiConfigDialog *dialog)
{
    gnome_dialog_close (GNOME_DIALOG (dialog));
}

static void
do_apply (EMultiConfigDialog *dialog)
{
    EMultiConfigDialogPrivate *priv;
    GSList *p;

    priv = dialog->priv;

    for (p = priv->pages; p != NULL; p = p->next) {
        const Page *page;

        page = (const Page *) p->data;

        if (! e_config_page_is_applied (page->page_widget)) {
            e_config_page_apply (page->page_widget);
            priv->num_unapplied --;
        }
    }

    g_assert (priv->num_unapplied == 0);
    update_buttons (dialog);
}

static void
do_ok (EMultiConfigDialog *dialog)
{
    do_apply (dialog);
    do_close (dialog);
}


/* ETable mess.  */

static int
table_model_column_count (ETableModel *etm,
              void *data)
{
        return 1;
}

static void *
table_model_value_at (ETableModel *etm,
              int col,
              int row,
              void *model_data)
{
    EMultiConfigDialog *dialog;
    EMultiConfigDialogPrivate *priv;
    const Page *page;
    GSList *p;

    dialog = E_MULTI_CONFIG_DIALOG (model_data);
    priv = dialog->priv;

    p = g_slist_nth (priv->pages, row);
    if (p == NULL)
        return NULL;

    page = (const Page *) p->data;
    return page->title;
}

static gboolean
table_model_is_editable (ETableModel *etm,
             int col,
             int row,
             void *model_data)
{
    return FALSE;
}

static void *
table_model_duplicate_value (ETableModel *etm,
                 int col,
                 const void *value,
                 void *data)
{
        return g_strdup (value);
}

static void
table_model_free_value (ETableModel *etm,
            int col,
            void *value,
            void *data)
{
        g_free (value);
}

static void *
table_model_initialize_value (ETableModel *etm,
                  int col,
                  void *data)
{
        return g_strdup ("");
}

static gboolean
table_model_value_is_empty (ETableModel *etm,
                int col,
                const void *value,
                void *data)
{
        return !(value && *(char *)value);
}

static char *
table_model_value_to_string (ETableModel *etm,
                 int col,
                 const void *value,
                 void *data)
{
    return (char *)value;
}


/* ETable signals.  */

static void
table_cursor_change_callback (ETable *etable,
                  int row,
                  void *data)
{
    EMultiConfigDialog *dialog;
    EMultiConfigDialogPrivate *priv;

    dialog = E_MULTI_CONFIG_DIALOG (data);
    priv = dialog->priv;

    gtk_notebook_set_page (GTK_NOTEBOOK (priv->notebook), row);
}


/* GtkObject methods.  */

static void
impl_destroy (GtkObject *object)
{
    EMultiConfigDialog *dialog;
    EMultiConfigDialogPrivate *priv;
    GSList *p;

    dialog = E_MULTI_CONFIG_DIALOG (object);
    priv = dialog->priv;

    for (p = priv->pages; p != NULL; p = p->next)
        page_free ((Page *) p->data);
    g_slist_free (priv->pages);

    g_free (priv);

    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


/* GnomeDialog methods.  */

static void
impl_clicked (GnomeDialog *dialog,
          int button_number)
{
    EMultiConfigDialog *multi_config_dialog;
    EMultiConfigDialogPrivate *priv;

    multi_config_dialog = E_MULTI_CONFIG_DIALOG (dialog);
    priv = multi_config_dialog->priv;

    switch (button_number) {
    case 0:         /* OK */
        do_ok (multi_config_dialog);
        break;
    case 1:         /* Apply */
        do_apply (multi_config_dialog);
        break;
    case 2:         /* Close */
        do_close (multi_config_dialog);
        break;
    default:
        g_assert_not_reached ();
    }
}


/* GTK+ ctors.  */

static void
class_init (EMultiConfigDialogClass *class)
{
    GnomeDialogClass *dialog_class;
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS (class);
    object_class->destroy = impl_destroy;

    dialog_class = GNOME_DIALOG_CLASS (class);
    dialog_class->clicked = impl_clicked;

    parent_class = gtk_type_class (PARENT_TYPE);
}

static void
init (EMultiConfigDialog *multi_config_dialog)
{
    EMultiConfigDialogPrivate *priv;
    ETableModel *list_e_table_model;
    GtkWidget *gnome_dialog_vbox;
    GtkWidget *hbox;
    GtkWidget *notebook;
    GtkWidget *list_e_table;

    hbox = gtk_hbox_new (FALSE, 2);
    gnome_dialog_vbox = GNOME_DIALOG (multi_config_dialog)->vbox;
    gtk_container_add (GTK_CONTAINER (gnome_dialog_vbox), hbox);

    list_e_table_model = e_table_memory_callbacks_new (table_model_column_count,
                               table_model_value_at,
                               NULL, /* set_value_at */
                               table_model_is_editable,
                               table_model_duplicate_value,
                               table_model_free_value,
                               table_model_initialize_value,
                               table_model_value_is_empty,
                               table_model_value_to_string,
                               multi_config_dialog);

    list_e_table = e_table_scrolled_new (list_e_table_model, NULL, list_e_table_spec, NULL);
    gtk_signal_connect (GTK_OBJECT (e_table_scrolled_get_table (E_TABLE_SCROLLED (list_e_table))),
                "cursor_change", GTK_SIGNAL_FUNC (table_cursor_change_callback),
                multi_config_dialog);

    gtk_widget_set_usize (list_e_table, 150, -1);

    gtk_box_pack_start (GTK_BOX (hbox), list_e_table, FALSE, TRUE, 0);

    notebook = gtk_notebook_new ();
    gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE);
    gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
    gtk_box_pack_start (GTK_BOX (hbox), notebook, TRUE, TRUE, 0);

    gtk_widget_show (hbox);
    gtk_widget_show (notebook);
    gtk_widget_show (list_e_table);

    gnome_dialog_append_buttons (GNOME_DIALOG (multi_config_dialog),
                     GNOME_STOCK_BUTTON_OK,
                     GNOME_STOCK_BUTTON_APPLY,
                     GNOME_STOCK_BUTTON_CLOSE,
                     NULL);
    gnome_dialog_set_default (GNOME_DIALOG (multi_config_dialog), 0);

    gtk_window_set_policy (GTK_WINDOW (multi_config_dialog),
                   FALSE /* allow_shrink */,
                   TRUE /* allow_grow */,
                   FALSE /* auto_shrink */);

    priv = g_new (EMultiConfigDialogPrivate, 1);
    priv->pages              = NULL;
    priv->list_e_table       = list_e_table;
    priv->list_e_table_model = list_e_table_model;
    priv->notebook           = notebook;
    priv->num_unapplied      = 0;

    multi_config_dialog->priv = priv;
}


GtkWidget *
e_multi_config_dialog_new (void)
{
    EMultiConfigDialog *dialog;

    dialog = gtk_type_new (e_multi_config_dialog_get_type ());

    return GTK_WIDGET (dialog);
}


void
e_multi_config_dialog_add_page (EMultiConfigDialog *dialog,
                const char *title,
                const char *description,
                GdkPixbuf *icon,
                EConfigPage *page_widget)
{
    EMultiConfigDialogPrivate *priv;
    Page *new_page;

    g_return_if_fail (E_IS_MULTI_CONFIG_DIALOG (dialog));
    g_return_if_fail (title != NULL);
    g_return_if_fail (description != NULL);
    g_return_if_fail (E_IS_CONFIG_PAGE (page_widget));

    priv = dialog->priv;

    new_page = page_new (title, description, icon, page_widget);

    priv->pages = g_slist_append (priv->pages, new_page);

    if (priv->pages->next == NULL) {
        ETable *table;

        /* FIXME: This is supposed to select the first entry by default
           but it doesn't seem to work at all.  */
        table = e_table_scrolled_get_table (E_TABLE_SCROLLED (priv->list_e_table));
        e_table_set_cursor_row (table, 0);
        e_selection_model_select_all (e_table_get_selection_model (table));
    }

    e_table_memory_insert (E_TABLE_MEMORY (priv->list_e_table_model), -1, new_page->title);

    gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
                  create_page_container (new_page->description, GTK_WIDGET (page_widget)),
                  NULL);

    if (! e_config_page_is_applied (page_widget))
        priv->num_unapplied ++;

    gtk_signal_connect (GTK_OBJECT (page_widget), "changed",
                GTK_SIGNAL_FUNC (page_changed_callback), dialog);

    update_buttons (dialog);
}


E_MAKE_TYPE (e_multi_config_dialog, "EMultiConfigDialog", EMultiConfigDialog, class_init, init, PARENT_TYPE)