2019-12-09 01:22:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2019 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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.1 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/>.
|
|
|
|
*
|
|
|
|
* Authors: Matthias Clasen <mclasen@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkdropdown.h"
|
|
|
|
|
|
|
|
#include "gtkbuiltiniconprivate.h"
|
|
|
|
#include "gtklistview.h"
|
|
|
|
#include "gtklistitemfactory.h"
|
|
|
|
#include "gtksignallistitemfactory.h"
|
|
|
|
#include "gtklistitemwidgetprivate.h"
|
|
|
|
#include "gtkpopover.h"
|
|
|
|
#include "gtkprivate.h"
|
|
|
|
#include "gtksingleselection.h"
|
|
|
|
#include "gtkfilterlistmodel.h"
|
|
|
|
#include "gtkstringfilter.h"
|
|
|
|
#include "gtkmultifilter.h"
|
|
|
|
#include "gtkwidgetprivate.h"
|
|
|
|
#include "gtknative.h"
|
|
|
|
#include "gtktogglebutton.h"
|
|
|
|
#include "gtkexpression.h"
|
|
|
|
#include "gtkstack.h"
|
|
|
|
#include "gtksearchentry.h"
|
|
|
|
#include "gtklabel.h"
|
|
|
|
#include "gtklistitem.h"
|
|
|
|
#include "gtkbuildable.h"
|
|
|
|
#include "gtkbuilderprivate.h"
|
2020-06-21 22:00:29 +00:00
|
|
|
#include "gtkstringlist.h"
|
2020-10-27 16:17:37 +00:00
|
|
|
#include "gtkbox.h"
|
2019-12-09 01:22:06 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-22 00:41:58 +00:00
|
|
|
* GtkDropDown:
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* `GtkDropDown` is a widget that allows the user to choose an item
|
|
|
|
* from a list of options.
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* ![An example GtkDropDown](drop-down.png)
|
|
|
|
*
|
|
|
|
* The `GtkDropDown` displays the selected choice.
|
|
|
|
*
|
|
|
|
* The options are given to `GtkDropDown` in the form of `GListModel`
|
2019-12-09 01:22:06 +00:00
|
|
|
* and how the individual options are represented is determined by
|
2021-02-22 00:41:58 +00:00
|
|
|
* a [class@Gtk.ListItemFactory]. The default factory displays simple strings.
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* `GtkDropDown` knows how to obtain strings from the items in a
|
|
|
|
* [class@Gtk.StringList]; for other models, you have to provide an expression
|
|
|
|
* to find the strings via [method@Gtk.DropDown.set_expression].
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* `GtkDropDown` can optionally allow search in the popup, which is
|
2019-12-09 01:22:06 +00:00
|
|
|
* useful if the list of options is long. To enable the search entry,
|
2021-02-22 00:41:58 +00:00
|
|
|
* use [method@Gtk.DropDown.set_enable_search].
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2022-05-08 20:39:59 +00:00
|
|
|
* Here is a UI definition example for `GtkDropDown` with a simple model:
|
|
|
|
* ```xml
|
|
|
|
* <object class="GtkDropDown">
|
|
|
|
* <property name="model">
|
|
|
|
* <object class="GtkStringList">
|
|
|
|
* <items>
|
|
|
|
* <item translatable="yes">Factory</item>
|
|
|
|
* <item translatable="yes">Home</item>
|
|
|
|
* <item translatable="yes">Subway</item>
|
|
|
|
* </items>
|
|
|
|
* </object>
|
|
|
|
* </property>
|
|
|
|
* </object>
|
|
|
|
* ```
|
|
|
|
*
|
2020-07-17 01:58:52 +00:00
|
|
|
* # CSS nodes
|
2020-06-07 13:01:20 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* `GtkDropDown` has a single CSS node with name dropdown,
|
2020-06-07 13:01:20 +00:00
|
|
|
* with the button and popover nodes as children.
|
2020-10-13 22:31:08 +00:00
|
|
|
*
|
|
|
|
* # Accessibility
|
|
|
|
*
|
2021-02-28 00:45:50 +00:00
|
|
|
* `GtkDropDown` uses the %GTK_ACCESSIBLE_ROLE_COMBO_BOX role.
|
2019-12-09 01:22:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct _GtkDropDown
|
|
|
|
{
|
|
|
|
GtkWidget parent_instance;
|
|
|
|
|
|
|
|
GtkListItemFactory *factory;
|
|
|
|
GtkListItemFactory *list_factory;
|
|
|
|
GListModel *model;
|
2020-08-31 21:06:48 +00:00
|
|
|
GtkSelectionModel *selection;
|
2019-12-09 01:22:06 +00:00
|
|
|
GListModel *filter_model;
|
2020-08-31 21:06:48 +00:00
|
|
|
GtkSelectionModel *popup_selection;
|
2019-12-09 01:22:06 +00:00
|
|
|
|
|
|
|
GtkWidget *popup;
|
|
|
|
GtkWidget *button;
|
2021-10-05 21:27:14 +00:00
|
|
|
GtkWidget *arrow;
|
2019-12-09 01:22:06 +00:00
|
|
|
|
|
|
|
GtkWidget *popup_list;
|
|
|
|
GtkWidget *button_stack;
|
|
|
|
GtkWidget *button_item;
|
|
|
|
GtkWidget *button_placeholder;
|
2020-12-12 06:30:01 +00:00
|
|
|
GtkWidget *search_box;
|
2019-12-09 01:22:06 +00:00
|
|
|
GtkWidget *search_entry;
|
|
|
|
|
|
|
|
GtkExpression *expression;
|
2021-10-05 21:27:14 +00:00
|
|
|
|
|
|
|
guint enable_search : 1;
|
|
|
|
guint show_arrow : 1;
|
2019-12-09 01:22:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkDropDownClass
|
|
|
|
{
|
|
|
|
GtkWidgetClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_FACTORY,
|
|
|
|
PROP_LIST_FACTORY,
|
|
|
|
PROP_MODEL,
|
|
|
|
PROP_SELECTED,
|
2020-06-20 17:11:48 +00:00
|
|
|
PROP_SELECTED_ITEM,
|
2019-12-09 01:22:06 +00:00
|
|
|
PROP_ENABLE_SEARCH,
|
|
|
|
PROP_EXPRESSION,
|
2021-10-05 21:27:14 +00:00
|
|
|
PROP_SHOW_ARROW,
|
2019-12-09 01:22:06 +00:00
|
|
|
|
|
|
|
N_PROPS
|
|
|
|
};
|
|
|
|
|
2021-09-10 13:42:47 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
ACTIVATE,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2020-06-21 22:00:29 +00:00
|
|
|
G_DEFINE_TYPE (GtkDropDown, gtk_drop_down, GTK_TYPE_WIDGET)
|
2019-12-09 01:22:06 +00:00
|
|
|
|
|
|
|
static GParamSpec *properties[N_PROPS] = { NULL, };
|
2021-09-10 13:42:47 +00:00
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
2019-12-09 01:22:06 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
button_toggled (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
|
|
|
|
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
|
|
|
|
gtk_popover_popup (GTK_POPOVER (self->popup));
|
|
|
|
else
|
|
|
|
gtk_popover_popdown (GTK_POPOVER (self->popup));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
popover_closed (GtkPopover *popover,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
|
|
|
|
gtk_editable_set_text (GTK_EDITABLE (self->search_entry), "");
|
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
row_activated (GtkListView *listview,
|
|
|
|
guint position,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
GtkFilter *filter;
|
|
|
|
|
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->button), FALSE);
|
|
|
|
gtk_popover_popdown (GTK_POPOVER (self->popup));
|
|
|
|
|
|
|
|
/* reset the filter so positions are 1-1 */
|
|
|
|
filter = gtk_filter_list_model_get_filter (GTK_FILTER_LIST_MODEL (self->filter_model));
|
|
|
|
if (GTK_IS_STRING_FILTER (filter))
|
|
|
|
gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "");
|
|
|
|
gtk_drop_down_set_selected (self, gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (self->popup_selection)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
selection_changed (GtkSingleSelection *selection,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
guint selected;
|
|
|
|
GtkFilter *filter;
|
|
|
|
|
|
|
|
selected = gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (self->selection));
|
|
|
|
|
|
|
|
if (selected == GTK_INVALID_LIST_POSITION)
|
|
|
|
{
|
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (self->button_stack), "empty");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (self->button_stack), "item");
|
2022-10-17 18:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (selected != gtk_list_item_widget_get_position (GTK_LIST_ITEM_WIDGET (self->button_item)))
|
|
|
|
{
|
|
|
|
gtk_list_item_widget_update (GTK_LIST_ITEM_WIDGET (self->button_item),
|
|
|
|
selected,
|
|
|
|
gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (self->selection)),
|
|
|
|
FALSE);
|
2019-12-09 01:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* reset the filter so positions are 1-1 */
|
|
|
|
filter = gtk_filter_list_model_get_filter (GTK_FILTER_LIST_MODEL (self->filter_model));
|
|
|
|
if (GTK_IS_STRING_FILTER (filter))
|
|
|
|
gtk_string_filter_set_search (GTK_STRING_FILTER (filter), "");
|
|
|
|
gtk_single_selection_set_selected (GTK_SINGLE_SELECTION (self->popup_selection), selected);
|
2020-06-21 00:17:47 +00:00
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED]);
|
2022-10-17 18:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
selection_item_changed (GtkSingleSelection *selection,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
gpointer item;
|
|
|
|
|
|
|
|
item = gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (self->selection));
|
|
|
|
|
|
|
|
if (item != gtk_list_item_widget_get_item (GTK_LIST_ITEM_WIDGET (self->button_item)))
|
|
|
|
{
|
|
|
|
gtk_list_item_widget_update (GTK_LIST_ITEM_WIDGET (self->button_item),
|
|
|
|
gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (self->selection)),
|
|
|
|
item,
|
|
|
|
FALSE);
|
|
|
|
}
|
|
|
|
|
2020-06-21 00:17:47 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED_ITEM]);
|
2019-12-09 01:22:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 13:42:47 +00:00
|
|
|
static void
|
|
|
|
gtk_drop_down_activate (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
gtk_widget_activate (self->button);
|
|
|
|
}
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
static void
|
|
|
|
update_filter (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
if (self->filter_model)
|
|
|
|
{
|
|
|
|
GtkFilter *filter;
|
|
|
|
|
|
|
|
if (self->expression)
|
|
|
|
{
|
2020-08-03 19:04:33 +00:00
|
|
|
filter = GTK_FILTER (gtk_string_filter_new (gtk_expression_ref (self->expression)));
|
2019-12-09 01:22:06 +00:00
|
|
|
gtk_string_filter_set_match_mode (GTK_STRING_FILTER (filter), GTK_STRING_FILTER_MATCH_MODE_PREFIX);
|
|
|
|
}
|
|
|
|
else
|
2020-08-03 19:04:33 +00:00
|
|
|
filter = GTK_FILTER (gtk_every_filter_new ());
|
2019-12-09 01:22:06 +00:00
|
|
|
gtk_filter_list_model_set_filter (GTK_FILTER_LIST_MODEL (self->filter_model), filter);
|
|
|
|
g_object_unref (filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
search_changed (GtkSearchEntry *entry, gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
const char *text;
|
|
|
|
GtkFilter *filter;
|
|
|
|
|
|
|
|
text = gtk_editable_get_text (GTK_EDITABLE (entry));
|
|
|
|
|
|
|
|
filter = gtk_filter_list_model_get_filter (GTK_FILTER_LIST_MODEL (self->filter_model));
|
|
|
|
if (GTK_IS_STRING_FILTER (filter))
|
|
|
|
gtk_string_filter_set_search (GTK_STRING_FILTER (filter), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
search_stop (GtkSearchEntry *entry, gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
GtkFilter *filter;
|
|
|
|
|
|
|
|
filter = gtk_filter_list_model_get_filter (GTK_FILTER_LIST_MODEL (self->filter_model));
|
|
|
|
if (GTK_IS_STRING_FILTER (filter))
|
|
|
|
{
|
|
|
|
if (gtk_string_filter_get_search (GTK_STRING_FILTER (filter)))
|
|
|
|
gtk_string_filter_set_search (GTK_STRING_FILTER (filter), NULL);
|
|
|
|
else
|
|
|
|
gtk_popover_popdown (GTK_POPOVER (self->popup));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_drop_down_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = GTK_DROP_DOWN (object);
|
|
|
|
|
|
|
|
g_clear_pointer (&self->popup, gtk_widget_unparent);
|
|
|
|
g_clear_pointer (&self->button, gtk_widget_unparent);
|
|
|
|
|
|
|
|
g_clear_object (&self->model);
|
|
|
|
if (self->selection)
|
2022-10-17 18:07:51 +00:00
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (self->selection, selection_changed, self);
|
|
|
|
g_signal_handlers_disconnect_by_func (self->selection, selection_item_changed, self);
|
|
|
|
}
|
2019-12-09 01:22:06 +00:00
|
|
|
g_clear_object (&self->filter_model);
|
|
|
|
g_clear_pointer (&self->expression, gtk_expression_unref);
|
|
|
|
g_clear_object (&self->selection);
|
|
|
|
g_clear_object (&self->popup_selection);
|
|
|
|
g_clear_object (&self->factory);
|
|
|
|
g_clear_object (&self->list_factory);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_drop_down_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_drop_down_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = GTK_DROP_DOWN (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_FACTORY:
|
|
|
|
g_value_set_object (value, self->factory);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_LIST_FACTORY:
|
|
|
|
g_value_set_object (value, self->list_factory);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_MODEL:
|
|
|
|
g_value_set_object (value, self->model);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_SELECTED:
|
|
|
|
g_value_set_uint (value, gtk_drop_down_get_selected (self));
|
|
|
|
break;
|
|
|
|
|
2020-06-20 17:11:48 +00:00
|
|
|
case PROP_SELECTED_ITEM:
|
|
|
|
g_value_set_object (value, gtk_drop_down_get_selected_item (self));
|
|
|
|
break;
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
case PROP_ENABLE_SEARCH:
|
|
|
|
g_value_set_boolean (value, self->enable_search);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_EXPRESSION:
|
2020-06-01 20:07:53 +00:00
|
|
|
gtk_value_set_expression (value, self->expression);
|
2019-12-09 01:22:06 +00:00
|
|
|
break;
|
|
|
|
|
2021-10-05 21:27:14 +00:00
|
|
|
case PROP_SHOW_ARROW:
|
|
|
|
g_value_set_boolean (value, gtk_drop_down_get_show_arrow (self));
|
|
|
|
break;
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_drop_down_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = GTK_DROP_DOWN (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_FACTORY:
|
|
|
|
gtk_drop_down_set_factory (self, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_LIST_FACTORY:
|
|
|
|
gtk_drop_down_set_list_factory (self, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_MODEL:
|
|
|
|
gtk_drop_down_set_model (self, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_SELECTED:
|
|
|
|
gtk_drop_down_set_selected (self, g_value_get_uint (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ENABLE_SEARCH:
|
|
|
|
gtk_drop_down_set_enable_search (self, g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_EXPRESSION:
|
2020-06-01 20:07:53 +00:00
|
|
|
gtk_drop_down_set_expression (self, gtk_value_get_expression (value));
|
2019-12-09 01:22:06 +00:00
|
|
|
break;
|
|
|
|
|
2021-10-05 21:27:14 +00:00
|
|
|
case PROP_SHOW_ARROW:
|
|
|
|
gtk_drop_down_set_show_arrow (self, g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_drop_down_measure (GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = GTK_DROP_DOWN (widget);
|
|
|
|
|
|
|
|
gtk_widget_measure (self->button,
|
|
|
|
orientation,
|
|
|
|
size,
|
|
|
|
minimum, natural,
|
|
|
|
minimum_baseline, natural_baseline);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_drop_down_size_allocate (GtkWidget *widget,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int baseline)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = GTK_DROP_DOWN (widget);
|
|
|
|
|
|
|
|
gtk_widget_size_allocate (self->button, &(GtkAllocation) { 0, 0, width, height }, baseline);
|
|
|
|
|
2020-06-11 22:29:58 +00:00
|
|
|
gtk_widget_set_size_request (self->popup, width, -1);
|
2020-07-24 13:07:45 +00:00
|
|
|
gtk_widget_queue_resize (self->popup);
|
2020-06-11 22:29:58 +00:00
|
|
|
|
2020-12-04 08:20:50 +00:00
|
|
|
gtk_popover_present (GTK_POPOVER (self->popup));
|
2019-12-09 01:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_drop_down_focus (GtkWidget *widget,
|
|
|
|
GtkDirectionType direction)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = GTK_DROP_DOWN (widget);
|
|
|
|
|
|
|
|
if (self->popup && gtk_widget_get_visible (self->popup))
|
|
|
|
return gtk_widget_child_focus (self->popup, direction);
|
|
|
|
else
|
|
|
|
return gtk_widget_child_focus (self->button, direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_drop_down_grab_focus (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = GTK_DROP_DOWN (widget);
|
|
|
|
|
|
|
|
return gtk_widget_grab_focus (self->button);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_drop_down_class_init (GtkDropDownClass *klass)
|
|
|
|
{
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->dispose = gtk_drop_down_dispose;
|
|
|
|
gobject_class->get_property = gtk_drop_down_get_property;
|
|
|
|
gobject_class->set_property = gtk_drop_down_set_property;
|
|
|
|
|
|
|
|
widget_class->measure = gtk_drop_down_measure;
|
|
|
|
widget_class->size_allocate = gtk_drop_down_size_allocate;
|
|
|
|
widget_class->focus = gtk_drop_down_focus;
|
|
|
|
widget_class->grab_focus = gtk_drop_down_grab_focus;
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* GtkDropDown:factory: (attributes org.gtk.Property.get=gtk_drop_down_get_factory org.gtk.Property.set=gtk_drop_down_set_factory)
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Factory for populating list items.
|
|
|
|
*/
|
|
|
|
properties[PROP_FACTORY] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_object ("factory", NULL, NULL,
|
2019-12-09 01:22:06 +00:00
|
|
|
GTK_TYPE_LIST_ITEM_FACTORY,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* GtkDropDown:list-factory: (attributes org.gtk.Property.get=gtk_drop_down_get_list_factory org.gtk.Property.set=gtk_drop_down_set_list_factory)
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* The factory for populating list items in the popup.
|
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* If this is not set, [property@Gtk.DropDown:factory] is used.
|
2019-12-09 01:22:06 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_LIST_FACTORY] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_object ("list-factory", NULL, NULL,
|
2019-12-09 01:22:06 +00:00
|
|
|
GTK_TYPE_LIST_ITEM_FACTORY,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* GtkDropDown:model: (attributes org.gtk.Property.get=gtk_drop_down_get_model org.gtk.Property.set=gtk_drop_down_set_model)
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Model for the displayed items.
|
|
|
|
*/
|
|
|
|
properties[PROP_MODEL] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_object ("model", NULL, NULL,
|
2019-12-09 01:22:06 +00:00
|
|
|
G_TYPE_LIST_MODEL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* GtkDropDown:selected: (attributes org.gtk.Property.get=gtk_drop_down_get_selected org.gtk.Property.set=gtk_drop_down_set_selected)
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* The position of the selected item.
|
|
|
|
*
|
|
|
|
* If no item is selected, the property has the value
|
|
|
|
* %GTK_INVALID_LIST_POSITION.
|
2019-12-09 01:22:06 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_SELECTED] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_uint ("selected", NULL, NULL,
|
2019-12-09 01:22:06 +00:00
|
|
|
0, G_MAXUINT, GTK_INVALID_LIST_POSITION,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
2020-06-20 17:11:48 +00:00
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* GtkDropDown:selected-item: (attributes org.gtk.Property.get=gtk_drop_down_get_selected_item)
|
2020-06-20 17:11:48 +00:00
|
|
|
*
|
|
|
|
* The selected item.
|
|
|
|
*/
|
|
|
|
properties[PROP_SELECTED_ITEM] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_object ("selected-item", NULL, NULL,
|
2020-06-20 17:11:48 +00:00
|
|
|
G_TYPE_OBJECT,
|
|
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* GtkDropDown:enable-search: (attributes org.gtk.Property.get=gtk_drop_down_get_enable_search org.gtk.Property.set=gtk_drop_down_set_enable_search)
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Whether to show a search entry in the popup.
|
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Note that search requires [property@Gtk.DropDown:expression]
|
|
|
|
* to be set.
|
2019-12-09 01:22:06 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_ENABLE_SEARCH] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_boolean ("enable-search", NULL, NULL,
|
2019-12-09 01:22:06 +00:00
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* GtkDropDown:expression: (type GtkExpression) (attributes org.gtk.Property.get=gtk_drop_down_get_expression org.gtk.Property.set=gtk_drop_down_set_expression)
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* An expression to evaluate to obtain strings to match against the search
|
2021-02-22 00:41:58 +00:00
|
|
|
* term.
|
|
|
|
*
|
|
|
|
* See [property@Gtk.DropDown:enable-search] for how to enable search.
|
|
|
|
* If [property@Gtk.DropDown:factory] is not set, the expression is also
|
|
|
|
* used to bind strings to labels produced by a default factory.
|
2019-12-09 01:22:06 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_EXPRESSION] =
|
2022-05-11 12:19:39 +00:00
|
|
|
gtk_param_spec_expression ("expression", NULL, NULL,
|
2020-06-01 20:07:53 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
2019-12-09 01:22:06 +00:00
|
|
|
|
2021-10-05 21:27:14 +00:00
|
|
|
/**
|
|
|
|
* GtkDropDown:show-arrow: (attributes org.gtk.Property.get=gtk_drop_down_get_show_arrow org.gtk.Property.set=gtk_drop_down_set_show_arrow)
|
|
|
|
*
|
|
|
|
* Whether to show an arrow within the GtkDropDown widget.
|
|
|
|
*
|
|
|
|
* Since: 4.6
|
|
|
|
*/
|
|
|
|
properties[PROP_SHOW_ARROW] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_boolean ("show-arrow", NULL, NULL,
|
2021-10-05 21:27:14 +00:00
|
|
|
TRUE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
g_object_class_install_properties (gobject_class, N_PROPS, properties);
|
|
|
|
|
2021-09-10 13:42:47 +00:00
|
|
|
/**
|
|
|
|
* GtkDropDown::activate:
|
|
|
|
* @widget: the object which received the signal.
|
|
|
|
*
|
|
|
|
* Emitted to when the drop down is activated.
|
|
|
|
*
|
|
|
|
* The `::activate` signal on `GtkDropDown` is an action signal and
|
|
|
|
* emitting it causes the drop down to pop up its dropdown.
|
|
|
|
*
|
|
|
|
* Since: 4.6
|
|
|
|
*/
|
|
|
|
signals[ACTIVATE] =
|
|
|
|
g_signal_new_class_handler (I_ ("activate"),
|
|
|
|
G_OBJECT_CLASS_TYPE (gobject_class),
|
|
|
|
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
|
|
|
|
G_CALLBACK (gtk_drop_down_activate),
|
|
|
|
NULL, NULL,
|
|
|
|
NULL,
|
|
|
|
G_TYPE_NONE, 0);
|
|
|
|
|
|
|
|
gtk_widget_class_set_activate_signal (widget_class, signals[ACTIVATE]);
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkdropdown.ui");
|
2021-10-05 21:27:14 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkDropDown, arrow);
|
2019-12-09 01:22:06 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkDropDown, button);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkDropDown, button_stack);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkDropDown, button_item);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkDropDown, popup);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkDropDown, popup_list);
|
2020-12-12 06:30:01 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkDropDown, search_box);
|
2019-12-09 01:22:06 +00:00
|
|
|
gtk_widget_class_bind_template_child (widget_class, GtkDropDown, search_entry);
|
|
|
|
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, row_activated);
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, button_toggled);
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, popover_closed);
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, search_changed);
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, search_stop);
|
|
|
|
|
2020-06-07 13:01:20 +00:00
|
|
|
gtk_widget_class_set_css_name (widget_class, I_("dropdown"));
|
2020-10-13 22:31:08 +00:00
|
|
|
gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_COMBO_BOX);
|
2019-12-09 01:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_item (GtkSignalListItemFactory *factory,
|
|
|
|
GtkListItem *list_item,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2020-10-27 16:17:37 +00:00
|
|
|
GtkWidget *box;
|
2019-12-09 01:22:06 +00:00
|
|
|
GtkWidget *label;
|
2020-10-27 16:17:37 +00:00
|
|
|
GtkWidget *icon;
|
2019-12-09 01:22:06 +00:00
|
|
|
|
2020-10-27 16:17:37 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
2019-12-09 01:22:06 +00:00
|
|
|
label = gtk_label_new (NULL);
|
|
|
|
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
|
2020-10-27 16:17:37 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), label);
|
|
|
|
icon = gtk_image_new_from_icon_name ("object-select-symbolic");
|
|
|
|
gtk_box_append (GTK_BOX (box), icon);
|
|
|
|
gtk_list_item_set_child (list_item, box);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
selected_item_changed (GtkDropDown *self,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GtkListItem *list_item)
|
|
|
|
{
|
|
|
|
GtkWidget *box;
|
|
|
|
GtkWidget *icon;
|
|
|
|
|
|
|
|
box = gtk_list_item_get_child (list_item);
|
|
|
|
icon = gtk_widget_get_last_child (box);
|
|
|
|
|
|
|
|
if (gtk_drop_down_get_selected_item (self) == gtk_list_item_get_item (list_item))
|
|
|
|
gtk_widget_set_opacity (icon, 1.0);
|
|
|
|
else
|
|
|
|
gtk_widget_set_opacity (icon, 0.0);
|
2019-12-09 01:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bind_item (GtkSignalListItemFactory *factory,
|
|
|
|
GtkListItem *list_item,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
gpointer item;
|
2020-10-27 16:17:37 +00:00
|
|
|
GtkWidget *box;
|
2019-12-09 01:22:06 +00:00
|
|
|
GtkWidget *label;
|
2020-10-27 16:17:37 +00:00
|
|
|
GtkWidget *icon;
|
2019-12-09 01:22:06 +00:00
|
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
|
|
|
|
item = gtk_list_item_get_item (list_item);
|
2020-10-27 16:17:37 +00:00
|
|
|
box = gtk_list_item_get_child (list_item);
|
|
|
|
label = gtk_widget_get_first_child (box);
|
|
|
|
icon = gtk_widget_get_last_child (box);
|
2019-12-09 01:22:06 +00:00
|
|
|
|
2020-06-21 22:00:29 +00:00
|
|
|
if (self->expression &&
|
|
|
|
gtk_expression_evaluate (self->expression, item, &value))
|
2019-12-09 01:22:06 +00:00
|
|
|
{
|
|
|
|
gtk_label_set_label (GTK_LABEL (label), g_value_get_string (&value));
|
|
|
|
g_value_unset (&value);
|
|
|
|
}
|
2020-06-21 22:00:29 +00:00
|
|
|
else if (GTK_IS_STRING_OBJECT (item))
|
|
|
|
{
|
|
|
|
const char *string;
|
|
|
|
|
|
|
|
string = gtk_string_object_get_string (GTK_STRING_OBJECT (item));
|
|
|
|
gtk_label_set_label (GTK_LABEL (label), string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_critical ("Either GtkDropDown:factory or GtkDropDown:expression must be set");
|
|
|
|
}
|
2020-10-27 16:17:37 +00:00
|
|
|
|
|
|
|
if (gtk_widget_get_ancestor (box, GTK_TYPE_POPOVER) == self->popup)
|
|
|
|
{
|
2022-11-28 18:43:36 +00:00
|
|
|
gtk_widget_set_visible (icon, TRUE);
|
2020-10-27 16:17:37 +00:00
|
|
|
g_signal_connect (self, "notify::selected-item",
|
|
|
|
G_CALLBACK (selected_item_changed), list_item);
|
|
|
|
selected_item_changed (self, NULL, list_item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-28 18:43:36 +00:00
|
|
|
gtk_widget_set_visible (icon, FALSE);
|
2020-10-27 16:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unbind_item (GtkSignalListItemFactory *factory,
|
|
|
|
GtkListItem *list_item,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkDropDown *self = data;
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (self, selected_item_changed, list_item);
|
2019-12-09 01:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_default_factory (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
GtkListItemFactory *factory;
|
|
|
|
|
|
|
|
factory = gtk_signal_list_item_factory_new ();
|
|
|
|
|
|
|
|
g_signal_connect (factory, "setup", G_CALLBACK (setup_item), self);
|
|
|
|
g_signal_connect (factory, "bind", G_CALLBACK (bind_item), self);
|
2020-10-27 16:17:37 +00:00
|
|
|
g_signal_connect (factory, "unbind", G_CALLBACK (unbind_item), self);
|
2019-12-09 01:22:06 +00:00
|
|
|
|
|
|
|
gtk_drop_down_set_factory (self, factory);
|
|
|
|
|
|
|
|
g_object_unref (factory);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_drop_down_init (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_type_ensure (GTK_TYPE_BUILTIN_ICON);
|
|
|
|
g_type_ensure (GTK_TYPE_LIST_ITEM_WIDGET);
|
|
|
|
|
|
|
|
gtk_widget_init_template (GTK_WIDGET (self));
|
|
|
|
|
2021-10-05 21:27:14 +00:00
|
|
|
self->show_arrow = gtk_widget_get_visible (self->arrow);
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
set_default_factory (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_drop_down_new:
|
2021-05-21 00:45:06 +00:00
|
|
|
* @model: (transfer full) (nullable): the model to use
|
|
|
|
* @expression: (transfer full) (nullable): the expression to use
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Creates a new `GtkDropDown`.
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* You may want to call [method@Gtk.DropDown.set_factory]
|
2020-07-26 13:34:08 +00:00
|
|
|
* to set up a way to map its items to widgets.
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Returns: a new `GtkDropDown`
|
2021-10-06 16:56:11 +00:00
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
GtkWidget *
|
2020-07-26 13:34:08 +00:00
|
|
|
gtk_drop_down_new (GListModel *model,
|
|
|
|
GtkExpression *expression)
|
2019-12-09 01:22:06 +00:00
|
|
|
{
|
2020-07-26 13:34:08 +00:00
|
|
|
GtkWidget *self;
|
|
|
|
|
|
|
|
self = g_object_new (GTK_TYPE_DROP_DOWN,
|
|
|
|
"model", model,
|
|
|
|
"expression", expression,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* we're consuming the references */
|
|
|
|
g_clear_object (&model);
|
|
|
|
g_clear_pointer (&expression, gtk_expression_unref);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_drop_down_new_from_strings:
|
|
|
|
* @strings: (array zero-terminated=1): The strings to put in the dropdown
|
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Creates a new `GtkDropDown` that is populated with
|
|
|
|
* the strings.
|
2020-07-26 13:34:08 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Returns: a new `GtkDropDown`
|
2020-07-26 13:34:08 +00:00
|
|
|
*/
|
|
|
|
GtkWidget *
|
|
|
|
gtk_drop_down_new_from_strings (const char * const *strings)
|
|
|
|
{
|
|
|
|
return gtk_drop_down_new (G_LIST_MODEL (gtk_string_list_new (strings)), NULL);
|
2019-12-09 01:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_get_model: (attributes org.gtk.Method.get_property=model)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Gets the model that provides the displayed items.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): The model in use
|
2021-02-22 00:41:58 +00:00
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
GListModel *
|
|
|
|
gtk_drop_down_get_model (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), NULL);
|
|
|
|
|
|
|
|
return self->model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_set_model: (attributes org.gtk.Method.set_property=model)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2021-05-21 00:45:06 +00:00
|
|
|
* @model: (nullable) (transfer none): the model to use
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Sets the `GListModel` to use.
|
2019-12-09 01:22:06 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_drop_down_set_model (GtkDropDown *self,
|
|
|
|
GListModel *model)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_DROP_DOWN (self));
|
|
|
|
g_return_if_fail (model == NULL || G_IS_LIST_MODEL (model));
|
|
|
|
|
|
|
|
if (!g_set_object (&self->model, model))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (model == NULL)
|
|
|
|
{
|
|
|
|
gtk_list_view_set_model (GTK_LIST_VIEW (self->popup_list), NULL);
|
|
|
|
|
|
|
|
if (self->selection)
|
2022-10-17 18:07:51 +00:00
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (self->selection, selection_changed, self);
|
|
|
|
g_signal_handlers_disconnect_by_func (self->selection, selection_item_changed, self);
|
|
|
|
}
|
2019-12-09 01:22:06 +00:00
|
|
|
|
|
|
|
g_clear_object (&self->selection);
|
|
|
|
g_clear_object (&self->filter_model);
|
|
|
|
g_clear_object (&self->popup_selection);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GListModel *filter_model;
|
2020-08-31 21:06:48 +00:00
|
|
|
GtkSelectionModel *selection;
|
2019-12-09 01:22:06 +00:00
|
|
|
|
2020-07-26 19:55:44 +00:00
|
|
|
filter_model = G_LIST_MODEL (gtk_filter_list_model_new (g_object_ref (model), NULL));
|
2019-12-09 01:22:06 +00:00
|
|
|
g_set_object (&self->filter_model, filter_model);
|
|
|
|
|
|
|
|
update_filter (self);
|
|
|
|
|
2020-08-31 21:06:48 +00:00
|
|
|
selection = GTK_SELECTION_MODEL (gtk_single_selection_new (filter_model));
|
2019-12-09 01:22:06 +00:00
|
|
|
g_set_object (&self->popup_selection, selection);
|
|
|
|
gtk_list_view_set_model (GTK_LIST_VIEW (self->popup_list), selection);
|
|
|
|
g_object_unref (selection);
|
|
|
|
|
2020-08-31 21:06:48 +00:00
|
|
|
selection = GTK_SELECTION_MODEL (gtk_single_selection_new (g_object_ref (model)));
|
2019-12-09 01:22:06 +00:00
|
|
|
g_set_object (&self->selection, selection);
|
|
|
|
g_object_unref (selection);
|
|
|
|
|
|
|
|
g_signal_connect (self->selection, "notify::selected", G_CALLBACK (selection_changed), self);
|
2022-10-17 18:07:51 +00:00
|
|
|
g_signal_connect (self->selection, "notify::selected-item", G_CALLBACK (selection_item_changed), self);
|
2019-12-09 01:22:06 +00:00
|
|
|
selection_changed (GTK_SINGLE_SELECTION (self->selection), NULL, self);
|
2022-10-17 18:07:51 +00:00
|
|
|
selection_item_changed (GTK_SINGLE_SELECTION (self->selection), NULL, self);
|
2019-12-09 01:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_get_factory: (attributes org.gtk.Method.get_property=factory)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Gets the factory that's currently used to populate list items.
|
|
|
|
*
|
|
|
|
* The factory returned by this function is always used for the
|
|
|
|
* item in the button. It is also used for items in the popup
|
2021-02-22 00:41:58 +00:00
|
|
|
* if [property@Gtk.DropDown:list-factory] is not set.
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): The factory in use
|
2021-02-22 00:41:58 +00:00
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
GtkListItemFactory *
|
|
|
|
gtk_drop_down_get_factory (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), NULL);
|
|
|
|
|
|
|
|
return self->factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_set_factory: (attributes org.gtk.Method.set_property=factory)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2021-05-21 00:45:06 +00:00
|
|
|
* @factory: (nullable) (transfer none): the factory to use
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Sets the `GtkListItemFactory` to use for populating list items.
|
2021-10-06 16:56:11 +00:00
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
void
|
|
|
|
gtk_drop_down_set_factory (GtkDropDown *self,
|
|
|
|
GtkListItemFactory *factory)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_DROP_DOWN (self));
|
|
|
|
g_return_if_fail (factory == NULL || GTK_LIST_ITEM_FACTORY (factory));
|
|
|
|
|
|
|
|
if (!g_set_object (&self->factory, factory))
|
|
|
|
return;
|
|
|
|
|
|
|
|
gtk_list_item_widget_set_factory (GTK_LIST_ITEM_WIDGET (self->button_item), factory);
|
|
|
|
if (self->list_factory == NULL)
|
|
|
|
gtk_list_view_set_factory (GTK_LIST_VIEW (self->popup_list), factory);
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FACTORY]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_get_list_factory: (attributes org.gtk.Method.get_property=list-factory)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Gets the factory that's currently used to populate list items in the popup.
|
|
|
|
*
|
|
|
|
* Returns: (nullable) (transfer none): The factory in use
|
2021-10-06 16:56:11 +00:00
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
GtkListItemFactory *
|
|
|
|
gtk_drop_down_get_list_factory (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), NULL);
|
|
|
|
|
|
|
|
return self->list_factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_set_list_factory: (attributes org.gtk.Method.set_property=list-factory)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2021-05-21 00:45:06 +00:00
|
|
|
* @factory: (nullable) (transfer none): the factory to use
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Sets the `GtkListItemFactory` to use for populating list items in the popup.
|
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
void
|
|
|
|
gtk_drop_down_set_list_factory (GtkDropDown *self,
|
|
|
|
GtkListItemFactory *factory)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_DROP_DOWN (self));
|
|
|
|
g_return_if_fail (factory == NULL || GTK_LIST_ITEM_FACTORY (factory));
|
|
|
|
|
|
|
|
if (!g_set_object (&self->list_factory, factory))
|
|
|
|
return;
|
2021-10-05 21:27:14 +00:00
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
if (self->list_factory != NULL)
|
|
|
|
gtk_list_view_set_factory (GTK_LIST_VIEW (self->popup_list), self->list_factory);
|
|
|
|
else
|
|
|
|
gtk_list_view_set_factory (GTK_LIST_VIEW (self->popup_list), self->factory);
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LIST_FACTORY]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_set_selected: (attributes org.gtk.Method.set_property=selected)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
|
|
|
* @position: the position of the item to select, or %GTK_INVALID_LIST_POSITION
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Selects the item at the given position.
|
2021-02-22 00:41:58 +00:00
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
void
|
|
|
|
gtk_drop_down_set_selected (GtkDropDown *self,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_DROP_DOWN (self));
|
|
|
|
|
|
|
|
if (self->selection == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (self->selection)) == position)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gtk_single_selection_set_selected (GTK_SINGLE_SELECTION (self->selection), position);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_get_selected: (attributes org.gtk.Method.get_property=selected)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Gets the position of the selected item.
|
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Returns: the position of the selected item, or %GTK_INVALID_LIST_POSITION
|
2021-05-18 21:05:26 +00:00
|
|
|
* if not item is selected
|
2021-02-22 00:41:58 +00:00
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
guint
|
|
|
|
gtk_drop_down_get_selected (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), GTK_INVALID_LIST_POSITION);
|
|
|
|
|
|
|
|
if (self->selection == NULL)
|
|
|
|
return GTK_INVALID_LIST_POSITION;
|
|
|
|
|
|
|
|
return gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (self->selection));
|
|
|
|
}
|
|
|
|
|
2020-06-20 17:11:48 +00:00
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_get_selected_item: (attributes org.gtk.Method.get_property=selected-item)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2020-06-20 17:11:48 +00:00
|
|
|
*
|
|
|
|
* Gets the selected item. If no item is selected, %NULL is returned.
|
|
|
|
*
|
2020-06-26 07:59:12 +00:00
|
|
|
* Returns: (transfer none) (type GObject) (nullable): The selected item
|
2020-06-20 17:11:48 +00:00
|
|
|
*/
|
|
|
|
gpointer
|
|
|
|
gtk_drop_down_get_selected_item (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), NULL);
|
|
|
|
|
|
|
|
if (self->selection == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (self->selection));
|
|
|
|
}
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_set_enable_search: (attributes org.gtk.Method.set_property=enable-search)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2019-12-09 01:22:06 +00:00
|
|
|
* @enable_search: whether to enable search
|
|
|
|
*
|
|
|
|
* Sets whether a search entry will be shown in the popup that
|
|
|
|
* allows to search for items in the list.
|
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Note that [property@Gtk.DropDown:expression] must be set for
|
|
|
|
* search to work.
|
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
void
|
|
|
|
gtk_drop_down_set_enable_search (GtkDropDown *self,
|
|
|
|
gboolean enable_search)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_DROP_DOWN (self));
|
|
|
|
|
2021-10-05 21:27:14 +00:00
|
|
|
enable_search = !!enable_search;
|
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
if (self->enable_search == enable_search)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self->enable_search = enable_search;
|
|
|
|
|
|
|
|
gtk_editable_set_text (GTK_EDITABLE (self->search_entry), "");
|
2020-12-12 06:30:01 +00:00
|
|
|
gtk_widget_set_visible (self->search_box, enable_search);
|
2021-10-05 21:27:14 +00:00
|
|
|
|
2019-12-09 01:22:06 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ENABLE_SEARCH]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_get_enable_search: (attributes org.gtk.Method.set_property=enable-search)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
|
|
|
* Returns whether search is enabled.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the popup includes a search entry
|
2021-10-05 21:27:14 +00:00
|
|
|
*/
|
2019-12-09 01:22:06 +00:00
|
|
|
gboolean
|
|
|
|
gtk_drop_down_get_enable_search (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), FALSE);
|
|
|
|
|
|
|
|
return self->enable_search;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_set_expression: (attributes org.gtk.Method.set_property=expression)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
2021-05-21 00:45:06 +00:00
|
|
|
* @expression: (nullable): a `GtkExpression`
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* Sets the expression that gets evaluated to obtain strings from items.
|
|
|
|
*
|
|
|
|
* This is used for search in the popup. The expression must have
|
|
|
|
* a value type of %G_TYPE_STRING.
|
2019-12-09 01:22:06 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_drop_down_set_expression (GtkDropDown *self,
|
|
|
|
GtkExpression *expression)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_DROP_DOWN (self));
|
|
|
|
g_return_if_fail (expression == NULL ||
|
|
|
|
gtk_expression_get_value_type (expression) == G_TYPE_STRING);
|
|
|
|
|
|
|
|
if (self->expression == expression)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (self->expression)
|
|
|
|
gtk_expression_unref (self->expression);
|
|
|
|
self->expression = expression;
|
|
|
|
if (self->expression)
|
|
|
|
gtk_expression_ref (self->expression);
|
|
|
|
|
|
|
|
update_filter (self);
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EXPRESSION]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 23:21:57 +00:00
|
|
|
* gtk_drop_down_get_expression: (attributes org.gtk.Method.get_property=expression)
|
2021-02-22 00:41:58 +00:00
|
|
|
* @self: a `GtkDropDown`
|
|
|
|
*
|
|
|
|
* Gets the expression set that is used to obtain strings from items.
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-02-22 00:41:58 +00:00
|
|
|
* See [method@Gtk.DropDown.set_expression].
|
2019-12-09 01:22:06 +00:00
|
|
|
*
|
2021-05-21 00:45:06 +00:00
|
|
|
* Returns: (nullable) (transfer none): a `GtkExpression`
|
2019-12-09 01:22:06 +00:00
|
|
|
*/
|
|
|
|
GtkExpression *
|
|
|
|
gtk_drop_down_get_expression (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), NULL);
|
|
|
|
|
|
|
|
return self->expression;
|
|
|
|
}
|
2021-10-05 21:27:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_drop_down_set_show_arrow: (attributes org.gtk.Method.set_property=show-arrow)
|
|
|
|
* @self: a `GtkDropDown`
|
|
|
|
* @show_arrow: whether to show an arrow within the widget
|
|
|
|
*
|
|
|
|
* Sets whether an arrow will be displayed within the widget.
|
|
|
|
*
|
|
|
|
* Since: 4.6
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_drop_down_set_show_arrow (GtkDropDown *self,
|
|
|
|
gboolean show_arrow)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_DROP_DOWN (self));
|
|
|
|
|
|
|
|
show_arrow = !!show_arrow;
|
|
|
|
|
|
|
|
if (self->show_arrow == show_arrow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self->show_arrow = show_arrow;
|
|
|
|
|
|
|
|
gtk_widget_set_visible (self->arrow, show_arrow);
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SHOW_ARROW]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_drop_down_get_show_arrow: (attributes org.gtk.Method.set_property=show-arrow)
|
|
|
|
* @self: a `GtkDropDown`
|
|
|
|
*
|
|
|
|
* Returns whether to show an arrow within the widget.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if an arrow will be shown.
|
|
|
|
*
|
|
|
|
* Since: 4.6
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gtk_drop_down_get_show_arrow (GtkDropDown *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), FALSE);
|
|
|
|
|
|
|
|
return self->show_arrow;
|
|
|
|
}
|