aboutsummaryrefslogtreecommitdiffstats
path: root/filter/e-search-bar.c
blob: 38ac0c5249dde9e29da6d88a9a544b9d2f0a2321 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-search-bar.c
 * Copyright (C) 2000  Helix Code, Inc.
 * Author: Chris Lahey <clahey@helixcode.com>
 *
 * This library 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; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <gnome.h>

#include "e-search-bar.h"
#include <gal/widgets/e-unicode.h>
#include <gal/widgets/e-gui-utils.h>

static void e_search_bar_init       (ESearchBar      *card);
static void e_search_bar_class_init (ESearchBarClass     *klass);
static void e_search_bar_set_arg (GtkObject *o, GtkArg *arg, guint arg_id);
static void e_search_bar_get_arg (GtkObject *object, GtkArg *arg, guint arg_id);
static void e_search_bar_destroy (GtkObject *object);

enum {
    QUERY_CHANGED,
    MENU_ACTIVATED,

    LAST_SIGNAL
};

static gint esb_signals [LAST_SIGNAL] = { 0, };

static GtkHBoxClass *parent_class = NULL;

/* The arguments we take */
enum {
    ARG_0,
    ARG_OPTION_CHOICE,
    ARG_TEXT,
};

GtkType
e_search_bar_get_type (void)
{
    static GtkType type = 0;

    if (!type) {
        static const GtkTypeInfo info =
        {
            "ESearchBar",
            sizeof (ESearchBar),
            sizeof (ESearchBarClass),
            (GtkClassInitFunc) e_search_bar_class_init,
            (GtkObjectInitFunc) e_search_bar_init,
            /* reserved_1 */ NULL,
                /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };
        
        type = gtk_type_unique (gtk_hbox_get_type (), &info);
    }

    return type;
}

static void
e_search_bar_class_init (ESearchBarClass *klass)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS(klass);

    parent_class = gtk_type_class (gtk_hbox_get_type ());

    object_class->set_arg = e_search_bar_set_arg;
    object_class->get_arg = e_search_bar_get_arg;
    object_class->destroy = e_search_bar_destroy;

    gtk_object_add_arg_type ("ESearchBar::option_choice", GTK_TYPE_ENUM,
                 GTK_ARG_READWRITE, ARG_OPTION_CHOICE);
    gtk_object_add_arg_type ("ESearchBar::text", GTK_TYPE_STRING,
                 GTK_ARG_READWRITE, ARG_TEXT);

    esb_signals [QUERY_CHANGED] =
        gtk_signal_new ("query_changed",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (ESearchBarClass, query_changed),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    esb_signals [MENU_ACTIVATED] =
        gtk_signal_new ("menu_activated",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (ESearchBarClass, menu_activated),
                gtk_marshal_NONE__INT,
                GTK_TYPE_NONE, 1, GTK_TYPE_INT);

    gtk_object_class_add_signals (object_class, esb_signals, LAST_SIGNAL);
}

static void
esb_query_changed(ESearchBar *esb)
{
    gtk_signal_emit(GTK_OBJECT (esb),
            esb_signals [QUERY_CHANGED]);
}

static void
esb_menu_activated(ESearchBar *esb, int item)
{
    gtk_signal_emit(GTK_OBJECT (esb),
            esb_signals [MENU_ACTIVATED],
            item);
}

static void
esb_menubar_activated(GtkWidget *widget, ESearchBar *esb)
{
    int id = GPOINTER_TO_INT(gtk_object_get_data (GTK_OBJECT (widget), "EsbMenuId"));

    esb_menu_activated(esb, id);
}

static void
esb_check_labels(GtkWidget *widget, gpointer data)
{
    if (GTK_IS_LABEL(widget)) {
        char *text;
        gtk_object_get(GTK_OBJECT(widget),
                   "label", &text,
                   NULL);
        gtk_label_parse_uline(GTK_LABEL(widget), text);
        g_free(text);
    }
}

static void
esb_pack_menubar(ESearchBar *esb, ESearchBarItem *items)
{
    GtkWidget *menu, *menuitem;
    int i;

    menu = gtk_menu_new ();
    for (i = 0; items[i].id != -1; i++) {
        GtkWidget *item;

        if (items[i].text)
            item = gtk_menu_item_new_with_label (_(items[i].text));
        else
            item = gtk_menu_item_new();

        gtk_menu_append (GTK_MENU (menu), item);

        gtk_object_set_data (GTK_OBJECT (item), "EsbMenuId", GINT_TO_POINTER(items[i].id));

        gtk_signal_connect (GTK_OBJECT (item), "activate",
                    GTK_SIGNAL_FUNC (esb_menubar_activated),
                    esb);
    }
    gtk_widget_show_all (menu);

    menuitem = gnome_stock_menu_item(GNOME_STOCK_MENU_SEARCH, _("Sear_ch"));
    e_container_foreach_leaf(GTK_CONTAINER(menuitem), esb_check_labels, NULL);
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);

    gtk_widget_show (menuitem);

    gtk_menu_bar_append (GTK_MENU_BAR(esb->menubar), menuitem);
    gtk_widget_set_sensitive (esb->menubar, TRUE);
}

static void
esb_option_activated(GtkWidget *widget, ESearchBar *esb)
{
    int id = GPOINTER_TO_INT(gtk_object_get_data (GTK_OBJECT (widget), "EsbChoiceId"));

    esb->option_choice = id;
    esb_query_changed(esb);
}

static void
esb_entry_activated(GtkWidget *widget, ESearchBar *esb)
{
    esb_query_changed(esb);
}

static void
esb_pack_option_menu(ESearchBar *esb, ESearchBarItem *items)
{
    GtkWidget *menu;
    int i;

    menu = gtk_menu_new ();
    for (i = 0; items[i].id != -1; i++) {
        GtkWidget *item;

        if (items[i].text)
            item = gtk_menu_item_new_with_label (_(items[i].text));
        else
            item = gtk_menu_item_new();

        gtk_menu_append (GTK_MENU (menu), item);

        gtk_object_set_data (GTK_OBJECT (item), "EsbChoiceId", GINT_TO_POINTER(items[i].id));

        gtk_signal_connect (GTK_OBJECT (item), "activate",
                    GTK_SIGNAL_FUNC (esb_option_activated),
                    esb);
    }
    gtk_widget_show_all (menu);

    gtk_option_menu_set_menu (GTK_OPTION_MENU (esb->option), 
                  menu);
    gtk_option_menu_set_history (GTK_OPTION_MENU (esb->option), 0);
    gtk_widget_set_sensitive (esb->option, TRUE);
}

static void
e_search_bar_init (ESearchBar *esb)
{
    GtkWidget *spacer;

    gtk_box_set_spacing(GTK_BOX(esb), 1);

    esb->menubar = gtk_menu_bar_new();
    gtk_widget_show(esb->menubar);
    gtk_box_pack_start(GTK_BOX(esb), esb->menubar, FALSE, FALSE, 0);

    esb->option = gtk_option_menu_new();
    gtk_widget_show(esb->option);
    gtk_box_pack_start(GTK_BOX(esb), esb->option, FALSE, FALSE, 0);

    esb->entry = gtk_entry_new();
    gtk_signal_connect (GTK_OBJECT (esb->entry), "activate",
                GTK_SIGNAL_FUNC (esb_entry_activated), esb);
    gtk_widget_show(esb->entry);
    gtk_box_pack_start(GTK_BOX(esb), esb->entry, TRUE, TRUE, 0);
    esb->option_choice = 0;

    spacer = gtk_drawing_area_new();
    gtk_widget_show(spacer);
    gtk_box_pack_start(GTK_BOX(esb), spacer, FALSE, FALSE, 0);
    gtk_widget_set_usize(spacer, 15, 1);
}

static void
e_search_bar_destroy (GtkObject *object)
{
    if (GTK_OBJECT_CLASS(parent_class)->destroy)
        GTK_OBJECT_CLASS(parent_class)->destroy(object);
}

GtkWidget *
e_search_bar_new        (ESearchBarItem *menu_items,
             ESearchBarItem *option_items)
{
    GtkWidget *widget = GTK_WIDGET (gtk_type_new (e_search_bar_get_type ()));
    esb_pack_menubar(E_SEARCH_BAR(widget), menu_items);
    esb_pack_option_menu(E_SEARCH_BAR(widget), option_items);
    return widget;
}

static void
e_search_bar_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ESearchBar *esb = E_SEARCH_BAR(object);

    switch (arg_id) {
    case ARG_OPTION_CHOICE:
        GTK_VALUE_ENUM (*arg) = esb->option_choice;
        break;

    case ARG_TEXT:
        GTK_VALUE_STRING (*arg) = e_utf8_gtk_editable_get_text(GTK_EDITABLE(esb->entry));
        break;

    default:
        arg->type = GTK_TYPE_INVALID;
        break;
    }
}

static void
e_search_bar_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ESearchBar *esb = E_SEARCH_BAR(object);

    switch (arg_id) {
    case ARG_OPTION_CHOICE:
        esb->option_choice = GTK_VALUE_ENUM (*arg);
        gtk_option_menu_set_history (GTK_OPTION_MENU (esb->option), esb->option_choice);
        esb_query_changed(esb);
        break;

    case ARG_TEXT:
        e_utf8_gtk_editable_set_text(GTK_EDITABLE(esb->entry), GTK_VALUE_STRING (*arg));
        esb_query_changed(esb);
        break;

    default:
        break;
    }
}