2013-05-21 15:26:37 +00:00
|
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
|
* Copyright (C) 2013 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Authors:
|
|
|
|
|
* - Bastien Nocera <bnocera@redhat.com>
|
|
|
|
|
*
|
|
|
|
|
* This library 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) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Modified by the GTK+ Team and others 2013. See the AUTHORS
|
|
|
|
|
* file for a list of people on the GTK+ Team. See the ChangeLog
|
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
|
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include "gtkentry.h"
|
|
|
|
|
#include "gtkentryprivate.h"
|
|
|
|
|
#include "gtkintl.h"
|
2014-06-09 13:39:53 +00:00
|
|
|
|
#include "gtkprivate.h"
|
2014-06-15 15:57:59 +00:00
|
|
|
|
#include "gtkrender.h"
|
2013-05-21 15:26:37 +00:00
|
|
|
|
#include "gtksearchbar.h"
|
2014-12-27 23:29:07 +00:00
|
|
|
|
#include "gtksearchentryprivate.h"
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SECTION:gtksearchbar
|
2013-06-30 04:50:29 +00:00
|
|
|
|
* @Short_description: A toolbar to integrate a search entry with
|
2013-05-21 15:26:37 +00:00
|
|
|
|
* @Title: GtkSearchBar
|
|
|
|
|
*
|
|
|
|
|
* #GtkSearchBar is a container made to have a search entry (possibly
|
2013-06-30 04:50:29 +00:00
|
|
|
|
* with additional connex widgets, such as drop-down menus, or buttons)
|
|
|
|
|
* built-in. The search bar would appear when a search is started through
|
2014-02-07 18:01:26 +00:00
|
|
|
|
* typing on the keyboard, or the application’s search mode is toggled on.
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
2013-06-30 04:50:29 +00:00
|
|
|
|
* For keyboard presses to start a search, events will need to be
|
|
|
|
|
* forwarded from the top-level window that contains the search bar.
|
|
|
|
|
* See gtk_search_bar_handle_event() for example code. Common shortcuts
|
|
|
|
|
* such as Ctrl+F should be handled as an application action, or through
|
|
|
|
|
* the menu items.
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
2013-06-30 04:50:29 +00:00
|
|
|
|
* You will also need to tell the search bar about which entry you
|
|
|
|
|
* are using as your search entry using gtk_search_bar_connect_entry().
|
|
|
|
|
* The following example shows you how to create a more complex search
|
|
|
|
|
* entry.
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
2015-10-29 11:43:30 +00:00
|
|
|
|
* # CSS nodes
|
|
|
|
|
*
|
|
|
|
|
* GtkSearchBar has a single CSS node with name searchbar.
|
|
|
|
|
*
|
2014-02-04 21:57:57 +00:00
|
|
|
|
* ## Creating a search bar
|
|
|
|
|
*
|
2018-06-16 05:05:55 +00:00
|
|
|
|
* [A simple example](https://gitlab.gnome.org/GNOME/gtk/blob/gtk-3-24/examples/search-bar.c)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 3.10
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
typedef struct {
|
2013-05-21 15:26:37 +00:00
|
|
|
|
/* Template widgets */
|
|
|
|
|
GtkWidget *revealer;
|
2013-10-09 23:10:40 +00:00
|
|
|
|
GtkWidget *tool_box;
|
2013-05-21 15:26:37 +00:00
|
|
|
|
GtkWidget *box_center;
|
|
|
|
|
GtkWidget *close_button;
|
|
|
|
|
|
|
|
|
|
GtkWidget *entry;
|
|
|
|
|
gboolean reveal_child;
|
2013-06-27 19:02:52 +00:00
|
|
|
|
} GtkSearchBarPrivate;
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkSearchBar, gtk_search_bar, GTK_TYPE_BIN)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
PROP_0,
|
|
|
|
|
PROP_SEARCH_MODE_ENABLED,
|
|
|
|
|
PROP_SHOW_CLOSE_BUTTON,
|
|
|
|
|
LAST_PROPERTY
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static GParamSpec *widget_props[LAST_PROPERTY] = { NULL, };
|
|
|
|
|
|
2014-12-27 23:29:07 +00:00
|
|
|
|
static void
|
|
|
|
|
stop_search_cb (GtkWidget *entry,
|
|
|
|
|
GtkSearchBar *bar)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
{
|
2014-12-27 23:29:07 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
entry_key_pressed_event_cb (GtkWidget *widget,
|
|
|
|
|
GdkEvent *event,
|
|
|
|
|
GtkSearchBar *bar)
|
|
|
|
|
{
|
2014-12-27 23:29:07 +00:00
|
|
|
|
if (event->key.keyval == GDK_KEY_Escape)
|
|
|
|
|
{
|
|
|
|
|
stop_search_cb (widget, bar);
|
|
|
|
|
return GDK_EVENT_STOP;
|
|
|
|
|
}
|
|
|
|
|
else
|
2013-05-21 15:26:37 +00:00
|
|
|
|
return GDK_EVENT_PROPAGATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
preedit_changed_cb (GtkEntry *entry,
|
|
|
|
|
GtkWidget *popup,
|
|
|
|
|
gboolean *preedit_changed)
|
|
|
|
|
{
|
|
|
|
|
*preedit_changed = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 23:29:07 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
gtk_search_bar_handle_event_for_entry (GtkSearchBar *bar,
|
|
|
|
|
GdkEvent *event)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
gboolean handled;
|
|
|
|
|
gboolean preedit_changed;
|
|
|
|
|
guint preedit_change_id;
|
|
|
|
|
gboolean res;
|
|
|
|
|
char *old_text, *new_text;
|
|
|
|
|
|
|
|
|
|
if (gtk_search_entry_is_keynav_event (event) ||
|
|
|
|
|
event->key.keyval == GDK_KEY_space ||
|
|
|
|
|
event->key.keyval == GDK_KEY_Menu)
|
|
|
|
|
return GDK_EVENT_PROPAGATE;
|
|
|
|
|
|
|
|
|
|
if (!gtk_widget_get_realized (priv->entry))
|
|
|
|
|
gtk_widget_realize (priv->entry);
|
|
|
|
|
|
|
|
|
|
handled = GDK_EVENT_PROPAGATE;
|
|
|
|
|
preedit_changed = FALSE;
|
|
|
|
|
preedit_change_id = g_signal_connect (priv->entry, "preedit-changed",
|
|
|
|
|
G_CALLBACK (preedit_changed_cb), &preedit_changed);
|
|
|
|
|
|
|
|
|
|
old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
|
|
|
|
|
res = gtk_widget_event (priv->entry, event);
|
|
|
|
|
new_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
|
|
|
|
|
|
|
|
|
|
g_signal_handler_disconnect (priv->entry, preedit_change_id);
|
|
|
|
|
|
|
|
|
|
if ((res && g_strcmp0 (new_text, old_text) != 0) || preedit_changed)
|
|
|
|
|
handled = GDK_EVENT_STOP;
|
|
|
|
|
|
|
|
|
|
g_free (old_text);
|
|
|
|
|
g_free (new_text);
|
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_search_bar_handle_event:
|
|
|
|
|
* @bar: a #GtkSearchBar
|
|
|
|
|
* @event: a #GdkEvent containing key press events
|
|
|
|
|
*
|
|
|
|
|
* This function should be called when the top-level
|
|
|
|
|
* window which contains the search bar received a key event.
|
|
|
|
|
*
|
|
|
|
|
* If the key event is handled by the search bar, the bar will
|
|
|
|
|
* be shown, the entry populated with the entered text and %GDK_EVENT_STOP
|
|
|
|
|
* will be returned. The caller should ensure that events are
|
|
|
|
|
* not propagated further.
|
|
|
|
|
*
|
|
|
|
|
* If no entry has been connected to the search bar, using
|
2013-06-30 04:50:29 +00:00
|
|
|
|
* gtk_search_bar_connect_entry(), this function will return
|
|
|
|
|
* immediately with a warning.
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
2014-02-04 21:57:57 +00:00
|
|
|
|
* ## Showing the search bar on key presses
|
|
|
|
|
*
|
2014-01-27 19:55:18 +00:00
|
|
|
|
* |[<!-- language="C" -->
|
2013-05-21 15:26:37 +00:00
|
|
|
|
* static gboolean
|
2014-02-12 21:09:09 +00:00
|
|
|
|
* on_key_press_event (GtkWidget *widget,
|
|
|
|
|
* GdkEvent *event,
|
|
|
|
|
* gpointer user_data)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
* {
|
2014-02-12 21:09:09 +00:00
|
|
|
|
* GtkSearchBar *bar = GTK_SEARCH_BAR (user_data);
|
|
|
|
|
* return gtk_search_bar_handle_event (bar, event);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
* }
|
|
|
|
|
*
|
2018-01-03 13:37:01 +00:00
|
|
|
|
* static void
|
|
|
|
|
* create_toplevel (void)
|
|
|
|
|
* {
|
|
|
|
|
* GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
|
* GtkWindow *search_bar = gtk_search_bar_new ();
|
|
|
|
|
*
|
|
|
|
|
* // Add more widgets to the window...
|
|
|
|
|
*
|
|
|
|
|
* g_signal_connect (window,
|
|
|
|
|
* "key-press-event",
|
|
|
|
|
* G_CALLBACK (on_key_press_event),
|
|
|
|
|
* search_bar);
|
|
|
|
|
* }
|
2014-01-27 17:12:55 +00:00
|
|
|
|
* ]|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
|
* Returns: %GDK_EVENT_STOP if the key press event resulted
|
2013-06-30 04:50:29 +00:00
|
|
|
|
* in text being entered in the search entry (and revealing
|
|
|
|
|
* the search bar if necessary), %GDK_EVENT_PROPAGATE otherwise.
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 3.10
|
2013-06-30 04:50:29 +00:00
|
|
|
|
*/
|
2013-05-21 15:26:37 +00:00
|
|
|
|
gboolean
|
|
|
|
|
gtk_search_bar_handle_event (GtkSearchBar *bar,
|
|
|
|
|
GdkEvent *event)
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
gboolean handled;
|
2014-12-27 23:29:07 +00:00
|
|
|
|
|
|
|
|
|
if (priv->reveal_child)
|
|
|
|
|
return GDK_EVENT_PROPAGATE;
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
if (priv->entry == NULL)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
{
|
|
|
|
|
g_warning ("The search bar does not have an entry connected to it. Call gtk_search_bar_connect_entry() to connect one.");
|
|
|
|
|
return GDK_EVENT_PROPAGATE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 23:29:07 +00:00
|
|
|
|
if (GTK_IS_SEARCH_ENTRY (priv->entry))
|
|
|
|
|
handled = gtk_search_entry_handle_event (GTK_SEARCH_ENTRY (priv->entry), event);
|
|
|
|
|
else
|
|
|
|
|
handled = gtk_search_bar_handle_event_for_entry (bar, event);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
2014-12-27 23:29:07 +00:00
|
|
|
|
if (handled == GDK_EVENT_STOP)
|
|
|
|
|
gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), TRUE);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
reveal_child_changed_cb (GObject *object,
|
|
|
|
|
GParamSpec *pspec,
|
|
|
|
|
GtkSearchBar *bar)
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
gboolean reveal_child;
|
|
|
|
|
|
|
|
|
|
g_object_get (object, "reveal-child", &reveal_child, NULL);
|
2014-10-12 22:18:04 +00:00
|
|
|
|
if (reveal_child)
|
|
|
|
|
gtk_widget_set_child_visible (priv->revealer, TRUE);
|
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
if (reveal_child == priv->reveal_child)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
priv->reveal_child = reveal_child;
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
2014-06-09 13:36:47 +00:00
|
|
|
|
if (priv->entry)
|
|
|
|
|
{
|
|
|
|
|
if (reveal_child)
|
|
|
|
|
_gtk_entry_grab_focus (GTK_ENTRY (priv->entry), FALSE);
|
|
|
|
|
else
|
|
|
|
|
gtk_entry_set_text (GTK_ENTRY (priv->entry), "");
|
|
|
|
|
}
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (bar), "search-mode-enabled");
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 22:18:04 +00:00
|
|
|
|
static void
|
|
|
|
|
child_revealed_changed_cb (GObject *object,
|
|
|
|
|
GParamSpec *pspec,
|
|
|
|
|
GtkSearchBar *bar)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
gboolean val;
|
|
|
|
|
|
|
|
|
|
g_object_get (object, "child-revealed", &val, NULL);
|
|
|
|
|
if (!val)
|
|
|
|
|
gtk_widget_set_child_visible (priv->revealer, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
static void
|
|
|
|
|
close_button_clicked_cb (GtkWidget *button,
|
|
|
|
|
GtkSearchBar *bar)
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
|
|
|
|
|
gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_search_bar_add (GtkContainer *container,
|
|
|
|
|
GtkWidget *child)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchBar *bar = GTK_SEARCH_BAR (container);
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
/* When constructing the widget, we want the revealer to be added
|
|
|
|
|
* as the first child of the search bar, as an implementation detail.
|
|
|
|
|
* After that, the child added by the application should be added
|
2013-10-09 23:10:40 +00:00
|
|
|
|
* to box_center.
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*/
|
2013-06-27 19:02:52 +00:00
|
|
|
|
if (priv->box_center == NULL)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
{
|
|
|
|
|
GTK_CONTAINER_CLASS (gtk_search_bar_parent_class)->add (container, child);
|
2013-07-01 17:46:13 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
gtk_container_add (GTK_CONTAINER (priv->box_center), child);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
/* If an entry is the only child, save the developer a couple of
|
2013-06-30 04:50:29 +00:00
|
|
|
|
* lines of code
|
|
|
|
|
*/
|
2013-05-21 15:26:37 +00:00
|
|
|
|
if (GTK_IS_ENTRY (child))
|
|
|
|
|
gtk_search_bar_connect_entry (bar, GTK_ENTRY (child));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2013-06-30 04:50:29 +00:00
|
|
|
|
gtk_search_bar_set_property (GObject *object,
|
|
|
|
|
guint prop_id,
|
|
|
|
|
const GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
{
|
|
|
|
|
GtkSearchBar *bar = GTK_SEARCH_BAR (object);
|
|
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
|
|
|
|
case PROP_SEARCH_MODE_ENABLED:
|
|
|
|
|
gtk_search_bar_set_search_mode (bar, g_value_get_boolean (value));
|
|
|
|
|
break;
|
|
|
|
|
case PROP_SHOW_CLOSE_BUTTON:
|
|
|
|
|
gtk_search_bar_set_show_close_button (bar, g_value_get_boolean (value));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2013-06-30 04:50:29 +00:00
|
|
|
|
gtk_search_bar_get_property (GObject *object,
|
|
|
|
|
guint prop_id,
|
|
|
|
|
GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
{
|
|
|
|
|
GtkSearchBar *bar = GTK_SEARCH_BAR (object);
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
|
|
|
|
case PROP_SEARCH_MODE_ENABLED:
|
2013-06-27 19:02:52 +00:00
|
|
|
|
g_value_set_boolean (value, priv->reveal_child);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
break;
|
|
|
|
|
case PROP_SHOW_CLOSE_BUTTON:
|
|
|
|
|
g_value_set_boolean (value, gtk_search_bar_get_show_close_button (bar));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 15:55:33 +00:00
|
|
|
|
static void gtk_search_bar_set_entry (GtkSearchBar *bar,
|
|
|
|
|
GtkEntry *entry);
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_search_bar_dispose (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchBar *bar = GTK_SEARCH_BAR (object);
|
|
|
|
|
|
2015-03-22 15:46:16 +00:00
|
|
|
|
gtk_search_bar_set_entry (bar, NULL);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_search_bar_parent_class)->dispose (object);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-09 23:10:40 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
gtk_search_bar_draw (GtkWidget *widget,
|
|
|
|
|
cairo_t *cr)
|
|
|
|
|
{
|
|
|
|
|
gint width, height;
|
|
|
|
|
GtkStyleContext *context;
|
|
|
|
|
|
|
|
|
|
width = gtk_widget_get_allocated_width (widget);
|
|
|
|
|
height = gtk_widget_get_allocated_height (widget);
|
|
|
|
|
context = gtk_widget_get_style_context (widget);
|
|
|
|
|
|
|
|
|
|
gtk_render_background (context, cr, 0, 0, width, height);
|
|
|
|
|
gtk_render_frame (context, cr, 0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
GTK_WIDGET_CLASS (gtk_search_bar_parent_class)->draw (widget, cr);
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_search_bar_class_init (GtkSearchBarClass *klass)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
|
|
|
|
|
|
|
|
|
|
object_class->dispose = gtk_search_bar_dispose;
|
|
|
|
|
object_class->set_property = gtk_search_bar_set_property;
|
|
|
|
|
object_class->get_property = gtk_search_bar_get_property;
|
2013-10-09 23:10:40 +00:00
|
|
|
|
widget_class->draw = gtk_search_bar_draw;
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
container_class->add = gtk_search_bar_add;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GtkEntry:search-mode-enabled:
|
|
|
|
|
*
|
|
|
|
|
* Whether the search mode is on and the search bar shown.
|
|
|
|
|
*
|
|
|
|
|
* See gtk_search_bar_set_search_mode() for details.
|
2013-06-30 04:50:29 +00:00
|
|
|
|
*/
|
2013-05-21 15:26:37 +00:00
|
|
|
|
widget_props[PROP_SEARCH_MODE_ENABLED] = g_param_spec_boolean ("search-mode-enabled",
|
|
|
|
|
P_("Search Mode Enabled"),
|
|
|
|
|
P_("Whether the search mode is on and the search bar shown"),
|
|
|
|
|
FALSE,
|
2014-06-09 13:39:53 +00:00
|
|
|
|
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
|
2013-06-30 04:50:29 +00:00
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* GtkEntry:show-close-button:
|
|
|
|
|
*
|
|
|
|
|
* Whether to show the close button in the toolbar.
|
2013-06-30 04:50:29 +00:00
|
|
|
|
*/
|
2013-05-21 15:26:37 +00:00
|
|
|
|
widget_props[PROP_SHOW_CLOSE_BUTTON] = g_param_spec_boolean ("show-close-button",
|
|
|
|
|
P_("Show Close Button"),
|
|
|
|
|
P_("Whether to show the close button in the toolbar"),
|
2013-07-05 14:05:56 +00:00
|
|
|
|
FALSE,
|
2014-06-09 13:39:53 +00:00
|
|
|
|
GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
g_object_class_install_properties (object_class, LAST_PROPERTY, widget_props);
|
|
|
|
|
|
2014-01-23 23:59:20 +00:00
|
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtksearchbar.ui");
|
2016-01-29 03:47:28 +00:00
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkSearchBar, tool_box);
|
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkSearchBar, revealer);
|
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkSearchBar, box_center);
|
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkSearchBar, close_button);
|
2015-10-29 11:43:30 +00:00
|
|
|
|
|
|
|
|
|
gtk_widget_class_set_css_name (widget_class, "searchbar");
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_search_bar_init (GtkSearchBar *bar)
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
|
|
|
|
gtk_widget_init_template (GTK_WIDGET (bar));
|
|
|
|
|
|
2014-10-12 22:18:04 +00:00
|
|
|
|
/* We use child-visible to avoid the unexpanded revealer
|
|
|
|
|
* peaking out by 1 pixel
|
|
|
|
|
*/
|
|
|
|
|
gtk_widget_set_child_visible (priv->revealer, FALSE);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
g_signal_connect (priv->revealer, "notify::reveal-child",
|
2013-05-21 15:26:37 +00:00
|
|
|
|
G_CALLBACK (reveal_child_changed_cb), bar);
|
2014-10-12 22:18:04 +00:00
|
|
|
|
g_signal_connect (priv->revealer, "notify::child-revealed",
|
|
|
|
|
G_CALLBACK (child_revealed_changed_cb), bar);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
gtk_widget_set_no_show_all (priv->close_button, TRUE);
|
|
|
|
|
g_signal_connect (priv->close_button, "clicked",
|
2013-05-21 15:26:37 +00:00
|
|
|
|
G_CALLBACK (close_button_clicked_cb), bar);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_search_bar_new:
|
|
|
|
|
*
|
|
|
|
|
* Creates a #GtkSearchBar. You will need to tell it about
|
|
|
|
|
* which widget is going to be your text entry using
|
2014-01-21 20:00:47 +00:00
|
|
|
|
* gtk_search_bar_connect_entry().
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
|
* Returns: a new #GtkSearchBar
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 3.10
|
|
|
|
|
*/
|
|
|
|
|
GtkWidget *
|
|
|
|
|
gtk_search_bar_new (void)
|
|
|
|
|
{
|
|
|
|
|
return g_object_new (GTK_TYPE_SEARCH_BAR, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 15:46:16 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_search_bar_set_entry (GtkSearchBar *bar,
|
|
|
|
|
GtkEntry *entry)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
|
|
|
|
|
if (priv->entry != NULL)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
{
|
2014-12-27 23:29:07 +00:00
|
|
|
|
if (GTK_IS_SEARCH_ENTRY (priv->entry))
|
|
|
|
|
g_signal_handlers_disconnect_by_func (priv->entry, stop_search_cb, bar);
|
|
|
|
|
else
|
|
|
|
|
g_signal_handlers_disconnect_by_func (priv->entry, entry_key_pressed_event_cb, bar);
|
2013-06-27 19:02:52 +00:00
|
|
|
|
g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 15:46:16 +00:00
|
|
|
|
priv->entry = GTK_WIDGET (entry);
|
|
|
|
|
|
|
|
|
|
if (priv->entry != NULL)
|
2013-05-21 15:26:37 +00:00
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
g_object_add_weak_pointer (G_OBJECT (priv->entry), (gpointer *) &priv->entry);
|
2014-12-27 23:29:07 +00:00
|
|
|
|
if (GTK_IS_SEARCH_ENTRY (priv->entry))
|
|
|
|
|
g_signal_connect (priv->entry, "stop-search",
|
|
|
|
|
G_CALLBACK (stop_search_cb), bar);
|
|
|
|
|
else
|
|
|
|
|
g_signal_connect (priv->entry, "key-press-event",
|
|
|
|
|
G_CALLBACK (entry_key_pressed_event_cb), bar);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 15:46:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_search_bar_connect_entry:
|
|
|
|
|
* @bar: a #GtkSearchBar
|
|
|
|
|
* @entry: a #GtkEntry
|
|
|
|
|
*
|
|
|
|
|
* Connects the #GtkEntry widget passed as the one to be used in
|
|
|
|
|
* this search bar. The entry should be a descendant of the search bar.
|
|
|
|
|
* This is only required if the entry isn’t the direct child of the
|
|
|
|
|
* search bar (as in our main example).
|
|
|
|
|
*
|
|
|
|
|
* Since: 3.10
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
gtk_search_bar_connect_entry (GtkSearchBar *bar,
|
|
|
|
|
GtkEntry *entry)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (GTK_IS_SEARCH_BAR (bar));
|
|
|
|
|
g_return_if_fail (entry == NULL || GTK_IS_ENTRY (entry));
|
|
|
|
|
|
|
|
|
|
gtk_search_bar_set_entry (bar, entry);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* gtk_search_bar_get_search_mode:
|
|
|
|
|
* @bar: a #GtkSearchBar
|
|
|
|
|
*
|
|
|
|
|
* Returns whether the search mode is on or off.
|
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
|
* Returns: whether search mode is toggled on
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 3.10
|
2013-06-30 04:50:29 +00:00
|
|
|
|
*/
|
2013-05-21 15:26:37 +00:00
|
|
|
|
gboolean
|
|
|
|
|
gtk_search_bar_get_search_mode (GtkSearchBar *bar)
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
g_return_val_if_fail (GTK_IS_SEARCH_BAR (bar), FALSE);
|
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
return priv->reveal_child;
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_search_bar_set_search_mode:
|
|
|
|
|
* @bar: a #GtkSearchBar
|
|
|
|
|
* @search_mode: the new state of the search mode
|
|
|
|
|
*
|
|
|
|
|
* Switches the search mode on or off.
|
|
|
|
|
*
|
|
|
|
|
* Since: 3.10
|
2013-06-30 04:50:29 +00:00
|
|
|
|
*/
|
2013-05-21 15:26:37 +00:00
|
|
|
|
void
|
|
|
|
|
gtk_search_bar_set_search_mode (GtkSearchBar *bar,
|
|
|
|
|
gboolean search_mode)
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_SEARCH_BAR (bar));
|
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), search_mode);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_search_bar_get_show_close_button:
|
|
|
|
|
* @bar: a #GtkSearchBar
|
|
|
|
|
*
|
|
|
|
|
* Returns whether the close button is shown.
|
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
|
* Returns: whether the close button is shown
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 3.10
|
2013-06-30 04:50:29 +00:00
|
|
|
|
*/
|
2013-05-21 15:26:37 +00:00
|
|
|
|
gboolean
|
|
|
|
|
gtk_search_bar_get_show_close_button (GtkSearchBar *bar)
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
g_return_val_if_fail (GTK_IS_SEARCH_BAR (bar), FALSE);
|
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
|
return gtk_widget_get_visible (priv->close_button);
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* gtk_search_bar_set_show_close_button:
|
|
|
|
|
* @bar: a #GtkSearchBar
|
2013-06-30 04:50:29 +00:00
|
|
|
|
* @visible: whether the close button will be shown or not
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
2013-07-05 14:05:56 +00:00
|
|
|
|
* Shows or hides the close button. Applications that
|
2014-02-05 18:07:34 +00:00
|
|
|
|
* already have a “search” toggle button should not show a close
|
2013-07-05 14:05:56 +00:00
|
|
|
|
* button in their search bar, as it duplicates the role of the
|
|
|
|
|
* toggle button.
|
2013-05-21 15:26:37 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 3.10
|
2013-06-30 04:50:29 +00:00
|
|
|
|
*/
|
2013-05-21 15:26:37 +00:00
|
|
|
|
void
|
|
|
|
|
gtk_search_bar_set_show_close_button (GtkSearchBar *bar,
|
|
|
|
|
gboolean visible)
|
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
|
GtkSearchBarPrivate *priv = gtk_search_bar_get_instance_private (bar);
|
|
|
|
|
|
2013-05-21 15:26:37 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_SEARCH_BAR (bar));
|
|
|
|
|
|
2014-06-09 13:39:53 +00:00
|
|
|
|
visible = visible != FALSE;
|
|
|
|
|
|
|
|
|
|
if (gtk_widget_get_visible (priv->close_button) != visible)
|
|
|
|
|
{
|
|
|
|
|
gtk_widget_set_visible (priv->close_button, visible);
|
|
|
|
|
g_object_notify (G_OBJECT (bar), "show-close-button");
|
|
|
|
|
}
|
2013-05-21 15:26:37 +00:00
|
|
|
|
}
|