1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2013 Red Hat, Inc.
3 *
4 * Authors:
5 * - Bastien Nocera <bnocera@redhat.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * Modified by the GTK+ Team and others 2013. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 */
27
28 #include "config.h"
29
30 #include "gtkentry.h"
31 #include "gtkentryprivate.h"
32 #include "gtkintl.h"
33 #include "gtkprivate.h"
34 #include "gtkrender.h"
35 #include "gtksearchbar.h"
36 #include "gtksearchentryprivate.h"
37
38 /**
39 * SECTION:gtksearchbar
40 * @Short_description: A toolbar to integrate a search entry with
41 * @Title: GtkSearchBar
42 *
43 * #GtkSearchBar is a container made to have a search entry (possibly
44 * with additional connex widgets, such as drop-down menus, or buttons)
45 * built-in. The search bar would appear when a search is started through
46 * typing on the keyboard, or the application’s search mode is toggled on.
47 *
48 * For keyboard presses to start a search, events will need to be
49 * forwarded from the top-level window that contains the search bar.
50 * See gtk_search_bar_handle_event() for example code. Common shortcuts
51 * such as Ctrl+F should be handled as an application action, or through
52 * the menu items.
53 *
54 * You will also need to tell the search bar about which entry you
55 * are using as your search entry using gtk_search_bar_connect_entry().
56 * The following example shows you how to create a more complex search
57 * entry.
58 *
59 * # CSS nodes
60 *
61 * GtkSearchBar has a single CSS node with name searchbar.
62 *
63 * ## Creating a search bar
64 *
65 * [A simple example](https://gitlab.gnome.org/GNOME/gtk/blob/gtk-3-24/examples/search-bar.c)
66 *
67 * Since: 3.10
68 */
69
70 typedef struct {
71 /* Template widgets */
72 GtkWidget *revealer;
73 GtkWidget *tool_box;
74 GtkWidget *box_center;
75 GtkWidget *close_button;
76
77 GtkWidget *entry;
78 gboolean reveal_child;
79 } GtkSearchBarPrivate;
80
81 G_DEFINE_TYPE_WITH_PRIVATE (GtkSearchBar, gtk_search_bar, GTK_TYPE_BIN)
82
83 enum {
84 PROP_0,
85 PROP_SEARCH_MODE_ENABLED,
86 PROP_SHOW_CLOSE_BUTTON,
87 LAST_PROPERTY
88 };
89
90 static GParamSpec *widget_props[LAST_PROPERTY] = { NULL, };
91
92 static void
stop_search_cb(GtkWidget * entry,GtkSearchBar * bar)93 stop_search_cb (GtkWidget *entry,
94 GtkSearchBar *bar)
95 {
96 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
97 gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);
98 }
99
100 static gboolean
entry_key_pressed_event_cb(GtkWidget * widget,GdkEvent * event,GtkSearchBar * bar)101 entry_key_pressed_event_cb (GtkWidget *widget,
102 GdkEvent *event,
103 GtkSearchBar *bar)
104 {
105 if (event->key.keyval == GDK_KEY_Escape)
106 {
107 stop_search_cb (widget, bar);
108 return GDK_EVENT_STOP;
109 }
110 else
111 return GDK_EVENT_PROPAGATE;
112 }
113
114 static void
preedit_changed_cb(GtkEntry * entry,GtkWidget * popup,gboolean * preedit_changed)115 preedit_changed_cb (GtkEntry *entry,
116 GtkWidget *popup,
117 gboolean *preedit_changed)
118 {
119 *preedit_changed = TRUE;
120 }
121
122 static gboolean
gtk_search_bar_handle_event_for_entry(GtkSearchBar * bar,GdkEvent * event)123 gtk_search_bar_handle_event_for_entry (GtkSearchBar *bar,
124 GdkEvent *event)
125 {
126 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
127 gboolean handled;
128 gboolean preedit_changed;
129 guint preedit_change_id;
130 gboolean res;
131 char *old_text, *new_text;
132
133 if (gtk_search_entry_is_keynav_event (event) ||
134 event->key.keyval == GDK_KEY_space ||
135 event->key.keyval == GDK_KEY_Menu)
136 return GDK_EVENT_PROPAGATE;
137
138 if (!gtk_widget_get_realized (priv->entry))
139 gtk_widget_realize (priv->entry);
140
141 handled = GDK_EVENT_PROPAGATE;
142 preedit_changed = FALSE;
143 preedit_change_id = g_signal_connect (priv->entry, "preedit-changed",
144 G_CALLBACK (preedit_changed_cb), &preedit_changed);
145
146 old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
147 res = gtk_widget_event (priv->entry, event);
148 new_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
149
150 g_signal_handler_disconnect (priv->entry, preedit_change_id);
151
152 if ((res && g_strcmp0 (new_text, old_text) != 0) || preedit_changed)
153 handled = GDK_EVENT_STOP;
154
155 g_free (old_text);
156 g_free (new_text);
157
158 return handled;
159 }
160
161 /**
162 * gtk_search_bar_handle_event:
163 * @bar: a #GtkSearchBar
164 * @event: a #GdkEvent containing key press events
165 *
166 * This function should be called when the top-level
167 * window which contains the search bar received a key event.
168 *
169 * If the key event is handled by the search bar, the bar will
170 * be shown, the entry populated with the entered text and %GDK_EVENT_STOP
171 * will be returned. The caller should ensure that events are
172 * not propagated further.
173 *
174 * If no entry has been connected to the search bar, using
175 * gtk_search_bar_connect_entry(), this function will return
176 * immediately with a warning.
177 *
178 * ## Showing the search bar on key presses
179 *
180 * |[<!-- language="C" -->
181 * static gboolean
182 * on_key_press_event (GtkWidget *widget,
183 * GdkEvent *event,
184 * gpointer user_data)
185 * {
186 * GtkSearchBar *bar = GTK_SEARCH_BAR (user_data);
187 * return gtk_search_bar_handle_event (bar, event);
188 * }
189 *
190 * static void
191 * create_toplevel (void)
192 * {
193 * GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
194 * GtkWindow *search_bar = gtk_search_bar_new ();
195 *
196 * // Add more widgets to the window...
197 *
198 * g_signal_connect (window,
199 * "key-press-event",
200 * G_CALLBACK (on_key_press_event),
201 * search_bar);
202 * }
203 * ]|
204 *
205 * Returns: %GDK_EVENT_STOP if the key press event resulted
206 * in text being entered in the search entry (and revealing
207 * the search bar if necessary), %GDK_EVENT_PROPAGATE otherwise.
208 *
209 * Since: 3.10
210 */
211 gboolean
gtk_search_bar_handle_event(GtkSearchBar * bar,GdkEvent * event)212 gtk_search_bar_handle_event (GtkSearchBar *bar,
213 GdkEvent *event)
214 {
215 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
216 gboolean handled;
217
218 if (priv->reveal_child)
219 return GDK_EVENT_PROPAGATE;
220
221 if (priv->entry == NULL)
222 {
223 g_warning ("The search bar does not have an entry connected to it. Call gtk_search_bar_connect_entry() to connect one.");
224 return GDK_EVENT_PROPAGATE;
225 }
226
227 if (GTK_IS_SEARCH_ENTRY (priv->entry))
228 handled = gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->entry), event);
229 else
230 handled = gtk_search_bar_handle_event_for_entry (bar, event);
231
232 if (handled == GDK_EVENT_STOP)
233 gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), TRUE);
234
235 return handled;
236 }
237
238 static void
reveal_child_changed_cb(GObject * object,GParamSpec * pspec,GtkSearchBar * bar)239 reveal_child_changed_cb (GObject *object,
240 GParamSpec *pspec,
241 GtkSearchBar *bar)
242 {
243 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
244 gboolean reveal_child;
245
246 g_object_get (object, "reveal-child", &reveal_child, NULL);
247 if (reveal_child)
248 gtk_widget_set_child_visible (priv->revealer, TRUE);
249
250 if (reveal_child == priv->reveal_child)
251 return;
252
253 priv->reveal_child = reveal_child;
254
255 if (priv->entry)
256 {
257 if (reveal_child)
258 _gtk_entry_grab_focus (GTK_ENTRY (priv->entry), FALSE);
259 else
260 gtk_entry_set_text (GTK_ENTRY (priv->entry), "");
261 }
262
263 g_object_notify (G_OBJECT (bar), "search-mode-enabled");
264 }
265
266 static void
child_revealed_changed_cb(GObject * object,GParamSpec * pspec,GtkSearchBar * bar)267 child_revealed_changed_cb (GObject *object,
268 GParamSpec *pspec,
269 GtkSearchBar *bar)
270 {
271 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
272 gboolean val;
273
274 g_object_get (object, "child-revealed", &val, NULL);
275 if (!val)
276 gtk_widget_set_child_visible (priv->revealer, FALSE);
277 }
278
279 static void
close_button_clicked_cb(GtkWidget * button,GtkSearchBar * bar)280 close_button_clicked_cb (GtkWidget *button,
281 GtkSearchBar *bar)
282 {
283 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
284
285 gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);
286 }
287
288 static void
gtk_search_bar_add(GtkContainer * container,GtkWidget * child)289 gtk_search_bar_add (GtkContainer *container,
290 GtkWidget *child)
291 {
292 GtkSearchBar *bar = GTK_SEARCH_BAR (container);
293 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
294
295 /* When constructing the widget, we want the revealer to be added
296 * as the first child of the search bar, as an implementation detail.
297 * After that, the child added by the application should be added
298 * to box_center.
299 */
300 if (priv->box_center == NULL)
301 {
302 GTK_CONTAINER_CLASS (gtk_search_bar_parent_class)->add (container, child);
303 }
304 else
305 {
306 gtk_container_add (GTK_CONTAINER (priv->box_center), child);
307 /* If an entry is the only child, save the developer a couple of
308 * lines of code
309 */
310 if (GTK_IS_ENTRY (child))
311 gtk_search_bar_connect_entry (bar, GTK_ENTRY (child));
312 }
313 }
314
315 static void
gtk_search_bar_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)316 gtk_search_bar_set_property (GObject *object,
317 guint prop_id,
318 const GValue *value,
319 GParamSpec *pspec)
320 {
321 GtkSearchBar *bar = GTK_SEARCH_BAR (object);
322
323 switch (prop_id)
324 {
325 case PROP_SEARCH_MODE_ENABLED:
326 gtk_search_bar_set_search_mode (bar, g_value_get_boolean (value));
327 break;
328 case PROP_SHOW_CLOSE_BUTTON:
329 gtk_search_bar_set_show_close_button (bar, g_value_get_boolean (value));
330 break;
331 default:
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333 break;
334 }
335 }
336
337 static void
gtk_search_bar_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)338 gtk_search_bar_get_property (GObject *object,
339 guint prop_id,
340 GValue *value,
341 GParamSpec *pspec)
342 {
343 GtkSearchBar *bar = GTK_SEARCH_BAR (object);
344 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
345
346 switch (prop_id)
347 {
348 case PROP_SEARCH_MODE_ENABLED:
349 g_value_set_boolean (value, priv->reveal_child);
350 break;
351 case PROP_SHOW_CLOSE_BUTTON:
352 g_value_set_boolean (value, gtk_search_bar_get_show_close_button (bar));
353 break;
354 default:
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 break;
357 }
358 }
359
360 static void gtk_search_bar_set_entry (GtkSearchBar *bar,
361 GtkEntry *entry);
362
363 static void
gtk_search_bar_dispose(GObject * object)364 gtk_search_bar_dispose (GObject *object)
365 {
366 GtkSearchBar *bar = GTK_SEARCH_BAR (object);
367
368 gtk_search_bar_set_entry (bar, NULL);
369
370 G_OBJECT_CLASS (gtk_search_bar_parent_class)->dispose (object);
371 }
372
373 static gboolean
gtk_search_bar_draw(GtkWidget * widget,cairo_t * cr)374 gtk_search_bar_draw (GtkWidget *widget,
375 cairo_t *cr)
376 {
377 gint width, height;
378 GtkStyleContext *context;
379
380 width = gtk_widget_get_allocated_width (widget);
381 height = gtk_widget_get_allocated_height (widget);
382 context = gtk_widget_get_style_context (widget);
383
384 gtk_render_background (context, cr, 0, 0, width, height);
385 gtk_render_frame (context, cr, 0, 0, width, height);
386
387 GTK_WIDGET_CLASS (gtk_search_bar_parent_class)->draw (widget, cr);
388
389 return FALSE;
390 }
391
392 static void
gtk_search_bar_class_init(GtkSearchBarClass * klass)393 gtk_search_bar_class_init (GtkSearchBarClass *klass)
394 {
395 GObjectClass *object_class = G_OBJECT_CLASS (klass);
396 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
397 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
398
399 object_class->dispose = gtk_search_bar_dispose;
400 object_class->set_property = gtk_search_bar_set_property;
401 object_class->get_property = gtk_search_bar_get_property;
402 widget_class->draw = gtk_search_bar_draw;
403
404 container_class->add = gtk_search_bar_add;
405
406 /**
407 * GtkEntry:search-mode-enabled:
408 *
409 * Whether the search mode is on and the search bar shown.
410 *
411 * See gtk_search_bar_set_search_mode() for details.
412 */
413 widget_props[PROP_SEARCH_MODE_ENABLED] = g_param_spec_boolean ("search-mode-enabled",
414 P_("Search Mode Enabled"),
415 P_("Whether the search mode is on and the search bar shown"),
416 FALSE,
417 GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
418
419 /**
420 * GtkEntry:show-close-button:
421 *
422 * Whether to show the close button in the toolbar.
423 */
424 widget_props[PROP_SHOW_CLOSE_BUTTON] = g_param_spec_boolean ("show-close-button",
425 P_("Show Close Button"),
426 P_("Whether to show the close button in the toolbar"),
427 FALSE,
428 GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY);
429
430 g_object_class_install_properties (object_class, LAST_PROPERTY, widget_props);
431
432 gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtksearchbar.ui");
433 gtk_widget_class_bind_template_child_private (widget_class, GtkSearchBar, tool_box);
434 gtk_widget_class_bind_template_child_private (widget_class, GtkSearchBar, revealer);
435 gtk_widget_class_bind_template_child_private (widget_class, GtkSearchBar, box_center);
436 gtk_widget_class_bind_template_child_private (widget_class, GtkSearchBar, close_button);
437
438 gtk_widget_class_set_css_name (widget_class, "searchbar");
439 }
440
441 static void
gtk_search_bar_init(GtkSearchBar * bar)442 gtk_search_bar_init (GtkSearchBar *bar)
443 {
444 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
445
446 gtk_widget_init_template (GTK_WIDGET (bar));
447
448 /* We use child-visible to avoid the unexpanded revealer
449 * peaking out by 1 pixel
450 */
451 gtk_widget_set_child_visible (priv->revealer, FALSE);
452
453 g_signal_connect (priv->revealer, "notify::reveal-child",
454 G_CALLBACK (reveal_child_changed_cb), bar);
455 g_signal_connect (priv->revealer, "notify::child-revealed",
456 G_CALLBACK (child_revealed_changed_cb), bar);
457
458 gtk_widget_set_no_show_all (priv->close_button, TRUE);
459 g_signal_connect (priv->close_button, "clicked",
460 G_CALLBACK (close_button_clicked_cb), bar);
461 };
462
463 /**
464 * gtk_search_bar_new:
465 *
466 * Creates a #GtkSearchBar. You will need to tell it about
467 * which widget is going to be your text entry using
468 * gtk_search_bar_connect_entry().
469 *
470 * Returns: a new #GtkSearchBar
471 *
472 * Since: 3.10
473 */
474 GtkWidget *
gtk_search_bar_new(void)475 gtk_search_bar_new (void)
476 {
477 return g_object_new (GTK_TYPE_SEARCH_BAR, NULL);
478 }
479
480 static void
gtk_search_bar_set_entry(GtkSearchBar * bar,GtkEntry * entry)481 gtk_search_bar_set_entry (GtkSearchBar *bar,
482 GtkEntry *entry)
483 {
484 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
485
486 if (priv->entry != NULL)
487 {
488 if (GTK_IS_SEARCH_ENTRY (priv->entry))
489 g_signal_handlers_disconnect_by_func (priv->entry, stop_search_cb, bar);
490 else
491 g_signal_handlers_disconnect_by_func (priv->entry, entry_key_pressed_event_cb, bar);
492 g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry);
493 }
494
495 priv->entry = GTK_WIDGET (entry);
496
497 if (priv->entry != NULL)
498 {
499 g_object_add_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry);
500 if (GTK_IS_SEARCH_ENTRY (priv->entry))
501 g_signal_connect (priv->entry, "stop-search",
502 G_CALLBACK (stop_search_cb), bar);
503 else
504 g_signal_connect (priv->entry, "key-press-event",
505 G_CALLBACK (entry_key_pressed_event_cb), bar);
506 }
507 }
508
509 /**
510 * gtk_search_bar_connect_entry:
511 * @bar: a #GtkSearchBar
512 * @entry: a #GtkEntry
513 *
514 * Connects the #GtkEntry widget passed as the one to be used in
515 * this search bar. The entry should be a descendant of the search bar.
516 * This is only required if the entry isn’t the direct child of the
517 * search bar (as in our main example).
518 *
519 * Since: 3.10
520 */
521 void
gtk_search_bar_connect_entry(GtkSearchBar * bar,GtkEntry * entry)522 gtk_search_bar_connect_entry (GtkSearchBar *bar,
523 GtkEntry *entry)
524 {
525 g_return_if_fail (GTK_IS_SEARCH_BAR (bar));
526 g_return_if_fail (entry == NULL || GTK_IS_ENTRY (entry));
527
528 gtk_search_bar_set_entry (bar, entry);
529 }
530
531 /**
532 * gtk_search_bar_get_search_mode:
533 * @bar: a #GtkSearchBar
534 *
535 * Returns whether the search mode is on or off.
536 *
537 * Returns: whether search mode is toggled on
538 *
539 * Since: 3.10
540 */
541 gboolean
gtk_search_bar_get_search_mode(GtkSearchBar * bar)542 gtk_search_bar_get_search_mode (GtkSearchBar *bar)
543 {
544 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
545
546 g_return_val_if_fail (GTK_IS_SEARCH_BAR (bar), FALSE);
547
548 return priv->reveal_child;
549 }
550
551 /**
552 * gtk_search_bar_set_search_mode:
553 * @bar: a #GtkSearchBar
554 * @search_mode: the new state of the search mode
555 *
556 * Switches the search mode on or off.
557 *
558 * Since: 3.10
559 */
560 void
gtk_search_bar_set_search_mode(GtkSearchBar * bar,gboolean search_mode)561 gtk_search_bar_set_search_mode (GtkSearchBar *bar,
562 gboolean search_mode)
563 {
564 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
565
566 g_return_if_fail (GTK_IS_SEARCH_BAR (bar));
567
568 gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), search_mode);
569 }
570
571 /**
572 * gtk_search_bar_get_show_close_button:
573 * @bar: a #GtkSearchBar
574 *
575 * Returns whether the close button is shown.
576 *
577 * Returns: whether the close button is shown
578 *
579 * Since: 3.10
580 */
581 gboolean
gtk_search_bar_get_show_close_button(GtkSearchBar * bar)582 gtk_search_bar_get_show_close_button (GtkSearchBar *bar)
583 {
584 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
585
586 g_return_val_if_fail (GTK_IS_SEARCH_BAR (bar), FALSE);
587
588 return gtk_widget_get_visible (priv->close_button);
589 }
590
591 /**
592 * gtk_search_bar_set_show_close_button:
593 * @bar: a #GtkSearchBar
594 * @visible: whether the close button will be shown or not
595 *
596 * Shows or hides the close button. Applications that
597 * already have a “search” toggle button should not show a close
598 * button in their search bar, as it duplicates the role of the
599 * toggle button.
600 *
601 * Since: 3.10
602 */
603 void
gtk_search_bar_set_show_close_button(GtkSearchBar * bar,gboolean visible)604 gtk_search_bar_set_show_close_button (GtkSearchBar *bar,
605 gboolean visible)
606 {
607 GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
608
609 g_return_if_fail (GTK_IS_SEARCH_BAR (bar));
610
611 visible = visible != FALSE;
612
613 if (gtk_widget_get_visible (priv->close_button) != visible)
614 {
615 gtk_widget_set_visible (priv->close_button, visible);
616 g_object_notify (G_OBJECT (bar), "show-close-button");
617 }
618 }
619