~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to libslab/search-bar.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of libslab.
3
 
 *
4
 
 * Copyright (c) 2006 Novell, Inc.
5
 
 *
6
 
 * Libslab is free software; you can redistribute it and/or modify it under the
7
 
 * terms of the GNU Lesser General Public License as published by the Free
8
 
 * Software Foundation; either version 2 of the License, or (at your option)
9
 
 * any later version.
10
 
 *
11
 
 * Libslab is distributed in the hope that it will be useful, but WITHOUT ANY
12
 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14
 
 * more details.
15
 
 *
16
 
 * You should have received a copy of the GNU Lesser General Public License
17
 
 * along with libslab; if not, write to the Free Software Foundation, Inc., 51
18
 
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
#include "search-bar.h"
22
 
#include "config.h"
23
 
 
24
 
#include "search-entry.h"
25
 
#include "search-context-picker.h"
26
 
#include "nld-marshal.h"
27
 
 
28
 
#include <glib/gi18n-lib.h>
29
 
 
30
 
typedef struct
31
 
{
32
 
        GtkWidget *hbox;
33
 
        NldSearchContextPicker *context_picker;
34
 
        GtkEntry *entry;
35
 
        GtkWidget *button;
36
 
 
37
 
        int search_timeout;
38
 
        guint timeout_id;
39
 
 
40
 
        gboolean block_signal;
41
 
} NldSearchBarPrivate;
42
 
 
43
 
#define NLD_SEARCH_BAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NLD_TYPE_SEARCH_BAR, NldSearchBarPrivate))
44
 
 
45
 
static void nld_search_bar_class_init (NldSearchBarClass *);
46
 
static void nld_search_bar_init (NldSearchBar *);
47
 
static void nld_search_bar_finalize (GObject *);
48
 
 
49
 
static gboolean nld_search_bar_focus (GtkWidget *, GtkDirectionType);
50
 
static void nld_search_bar_grab_focus (GtkWidget *);
51
 
 
52
 
enum
53
 
{
54
 
        SEARCH,
55
 
        LAST_SIGNAL
56
 
};
57
 
 
58
 
static guint signals[LAST_SIGNAL] = { 0 };
59
 
 
60
 
G_DEFINE_TYPE (NldSearchBar, nld_search_bar, GTK_TYPE_VBOX)
61
 
 
62
 
static void emit_search (NldSearchBar * search_bar);
63
 
static void emit_search_callback (GtkWidget * widget, gpointer search_bar);
64
 
 
65
 
static void nld_search_bar_class_init (NldSearchBarClass * nld_search_bar_class)
66
 
{
67
 
        GObjectClass *object_class = G_OBJECT_CLASS (nld_search_bar_class);
68
 
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (nld_search_bar_class);
69
 
 
70
 
        object_class->finalize = nld_search_bar_finalize;
71
 
        widget_class->focus = nld_search_bar_focus;
72
 
        widget_class->grab_focus = nld_search_bar_grab_focus;
73
 
 
74
 
        g_type_class_add_private (nld_search_bar_class, sizeof (NldSearchBarPrivate));
75
 
 
76
 
        signals[SEARCH] =
77
 
                g_signal_new ("search", G_TYPE_FROM_CLASS (nld_search_bar_class),
78
 
                G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (NldSearchBarClass, search),
79
 
                NULL, NULL, nld_marshal_VOID__INT_STRING, G_TYPE_NONE, 2, G_TYPE_INT,
80
 
                G_TYPE_STRING);
81
 
}
82
 
 
83
 
static void
84
 
nld_search_bar_init (NldSearchBar * search_bar)
85
 
{
86
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
87
 
        GtkWidget *alignment;
88
 
        GtkWidget *entry;
89
 
 
90
 
        GTK_WIDGET_SET_FLAGS (search_bar, GTK_CAN_FOCUS);
91
 
 
92
 
        priv->hbox = gtk_hbox_new (FALSE, 3);
93
 
        gtk_box_pack_start (GTK_BOX (search_bar), priv->hbox, TRUE, FALSE, 0);
94
 
 
95
 
        alignment = gtk_alignment_new (0.0, 0.5, 1.0, 0.0);
96
 
        gtk_box_pack_start (GTK_BOX (priv->hbox), alignment, TRUE, TRUE, 0);
97
 
 
98
 
        entry = nld_search_entry_new ();
99
 
        priv->entry = GTK_ENTRY (entry);
100
 
        gtk_widget_show (entry);
101
 
        gtk_container_add (GTK_CONTAINER (alignment), entry);
102
 
 
103
 
        g_signal_connect (entry, "activate", G_CALLBACK (emit_search_callback), search_bar);
104
 
 
105
 
        priv->search_timeout = -1;
106
 
}
107
 
 
108
 
static void
109
 
nld_search_bar_finalize (GObject * object)
110
 
{
111
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (object);
112
 
 
113
 
        if (priv->timeout_id)
114
 
                g_source_remove (priv->timeout_id);
115
 
 
116
 
        G_OBJECT_CLASS (nld_search_bar_parent_class)->finalize (object);
117
 
}
118
 
 
119
 
static gboolean
120
 
nld_search_bar_focus (GtkWidget * widget, GtkDirectionType dir)
121
 
{
122
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (widget);
123
 
 
124
 
        return gtk_widget_child_focus (priv->hbox, dir);
125
 
}
126
 
 
127
 
gboolean
128
 
nld_search_bar_has_focus (NldSearchBar * search_bar)
129
 
{
130
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
131
 
 
132
 
        return GTK_WIDGET_HAS_FOCUS (GTK_WIDGET (priv->entry));
133
 
}
134
 
 
135
 
static void
136
 
nld_search_bar_grab_focus (GtkWidget * widget)
137
 
{
138
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (widget);
139
 
 
140
 
        gtk_widget_grab_focus (GTK_WIDGET (priv->entry));
141
 
}
142
 
 
143
 
GtkWidget *
144
 
nld_search_bar_new (void)
145
 
{
146
 
        return g_object_new (NLD_TYPE_SEARCH_BAR, NULL);
147
 
}
148
 
 
149
 
void
150
 
nld_search_bar_clear (NldSearchBar * search_bar)
151
 
{
152
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
153
 
 
154
 
        priv->block_signal = TRUE;
155
 
        gtk_entry_set_text (priv->entry, "");
156
 
        if (priv->context_picker)
157
 
                nld_search_context_picker_set_context (priv->context_picker, 0);
158
 
        priv->block_signal = FALSE;
159
 
}
160
 
 
161
 
static void
162
 
emit_search (NldSearchBar * search_bar)
163
 
{
164
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
165
 
 
166
 
        if (priv->block_signal)
167
 
                return;
168
 
 
169
 
        if (priv->timeout_id)
170
 
        {
171
 
                g_source_remove (priv->timeout_id);
172
 
                priv->timeout_id = 0;
173
 
        }
174
 
 
175
 
        g_signal_emit (search_bar, signals[SEARCH], 0, nld_search_bar_get_context_id (search_bar),
176
 
                nld_search_bar_get_text (search_bar));
177
 
}
178
 
 
179
 
static void
180
 
emit_search_callback (GtkWidget * widget, gpointer search_bar)
181
 
{
182
 
        emit_search (search_bar);
183
 
}
184
 
 
185
 
gboolean
186
 
nld_search_bar_get_show_contexts (NldSearchBar * search_bar)
187
 
{
188
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
189
 
 
190
 
        return priv->context_picker && GTK_WIDGET_VISIBLE (priv->context_picker);
191
 
}
192
 
 
193
 
static NldSearchContextPicker *
194
 
build_context_picker (NldSearchBar * search_bar)
195
 
{
196
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
197
 
        GtkWidget *picker;
198
 
 
199
 
        picker = nld_search_context_picker_new ();
200
 
        g_signal_connect (picker, "context_changed", G_CALLBACK (emit_search_callback), search_bar);
201
 
 
202
 
        gtk_box_pack_start (GTK_BOX (priv->hbox), picker, 0, 0, FALSE);
203
 
        gtk_box_reorder_child (GTK_BOX (priv->hbox), picker, 0);
204
 
 
205
 
        return NLD_SEARCH_CONTEXT_PICKER (picker);
206
 
}
207
 
 
208
 
void
209
 
nld_search_bar_set_show_contexts (NldSearchBar * search_bar, gboolean show_contexts)
210
 
{
211
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
212
 
 
213
 
        if (show_contexts)
214
 
        {
215
 
                if (!priv->context_picker)
216
 
                        priv->context_picker = build_context_picker (search_bar);
217
 
                gtk_widget_show (GTK_WIDGET (priv->context_picker));
218
 
        }
219
 
        else if (priv->context_picker)
220
 
                gtk_widget_hide (GTK_WIDGET (priv->context_picker));
221
 
}
222
 
 
223
 
void
224
 
nld_search_bar_add_context (NldSearchBar * search_bar, const char *label, const char *icon_name,
225
 
        int context_id)
226
 
{
227
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
228
 
 
229
 
        if (!priv->context_picker)
230
 
                priv->context_picker = build_context_picker (search_bar);
231
 
 
232
 
        nld_search_context_picker_add_context (priv->context_picker, label, icon_name, context_id);
233
 
}
234
 
 
235
 
gboolean
236
 
nld_search_bar_get_show_button (NldSearchBar * search_bar)
237
 
{
238
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
239
 
 
240
 
        return priv->button != NULL;
241
 
}
242
 
 
243
 
void
244
 
nld_search_bar_set_show_button (NldSearchBar * search_bar, gboolean show_button)
245
 
{
246
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
247
 
 
248
 
        if (show_button)
249
 
        {
250
 
                GtkWidget *image;
251
 
 
252
 
                if (priv->button)
253
 
                        return;
254
 
 
255
 
                priv->button = gtk_button_new_with_label (_("Find Now"));
256
 
                image = gtk_image_new_from_icon_name ("system-search", GTK_ICON_SIZE_MENU);
257
 
                gtk_button_set_image (GTK_BUTTON (priv->button), image);
258
 
                gtk_widget_show (priv->button);
259
 
 
260
 
                g_signal_connect (priv->button, "clicked", G_CALLBACK (emit_search_callback),
261
 
                        search_bar);
262
 
 
263
 
                gtk_box_pack_end (GTK_BOX (priv->hbox), priv->button, FALSE, FALSE, 0);
264
 
        }
265
 
        else
266
 
        {
267
 
                if (!priv->button)
268
 
                        return;
269
 
 
270
 
                gtk_widget_destroy (priv->button);
271
 
                priv->button = NULL;
272
 
        }
273
 
}
274
 
 
275
 
static gboolean
276
 
search_timeout (gpointer search_bar)
277
 
{
278
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
279
 
 
280
 
        priv->timeout_id = 0;
281
 
        emit_search (search_bar);
282
 
        return FALSE;
283
 
}
284
 
 
285
 
static void
286
 
entry_changed (GtkWidget * entry, gpointer search_bar)
287
 
{
288
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
289
 
 
290
 
        if (priv->search_timeout == 0)
291
 
                emit_search (search_bar);
292
 
        else if (priv->search_timeout > 0)
293
 
        {
294
 
                if (priv->timeout_id != 0)
295
 
                        g_source_remove (priv->timeout_id);
296
 
                priv->timeout_id =
297
 
                        g_timeout_add (priv->search_timeout * 1000, search_timeout, search_bar);
298
 
        }
299
 
}
300
 
 
301
 
int
302
 
nld_search_bar_get_search_timeout (NldSearchBar * search_bar)
303
 
{
304
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
305
 
 
306
 
        return priv->search_timeout;
307
 
}
308
 
 
309
 
void
310
 
nld_search_bar_set_search_timeout (NldSearchBar * search_bar, int search_timeout)
311
 
{
312
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
313
 
 
314
 
        if (priv->search_timeout != -1 && search_timeout == -1)
315
 
                g_signal_handlers_disconnect_by_func (priv->entry, entry_changed, search_bar);
316
 
        else if (search_timeout != -1)
317
 
        {
318
 
                g_signal_connect (priv->entry, "changed", G_CALLBACK (entry_changed), search_bar);
319
 
        }
320
 
 
321
 
        priv->search_timeout = search_timeout;
322
 
}
323
 
 
324
 
const char *
325
 
nld_search_bar_get_text (NldSearchBar * search_bar)
326
 
{
327
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
328
 
 
329
 
        return gtk_entry_get_text (priv->entry);
330
 
}
331
 
 
332
 
void
333
 
nld_search_bar_set_text (NldSearchBar * search_bar, const char *text, gboolean activate)
334
 
{
335
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
336
 
 
337
 
        gtk_entry_set_text (priv->entry, text);
338
 
        if (activate)
339
 
                emit_search (search_bar);
340
 
}
341
 
 
342
 
int
343
 
nld_search_bar_get_context_id (NldSearchBar * search_bar)
344
 
{
345
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
346
 
 
347
 
        if (priv->context_picker && GTK_WIDGET_VISIBLE (priv->context_picker))
348
 
                return nld_search_context_picker_get_context (priv->context_picker);
349
 
        else
350
 
                return -1;
351
 
}
352
 
 
353
 
void
354
 
nld_search_bar_set_context_id (NldSearchBar * search_bar, int context_id)
355
 
{
356
 
        NldSearchBarPrivate *priv = NLD_SEARCH_BAR_GET_PRIVATE (search_bar);
357
 
 
358
 
        g_return_if_fail (priv->context_picker != NULL);
359
 
 
360
 
        nld_search_context_picker_set_context (priv->context_picker, context_id);
361
 
}