Compare commits

...

6 Commits

Author SHA1 Message Date
Matthias Clasen
20422f98ab Fix up the warning
It is misleading to complain about the factory here.
2020-08-25 13:54:32 -04:00
Matthias Clasen
ebfdd71a3e gtk-demo: Add suggestion entry demos
Move the Dropdowns demo to Lists/Selections,
and make it show both GtkDropDown and
GtkSuggestionEntry, with some variations.
2020-07-24 10:41:35 -04:00
Matthias Clasen
56069698b1 docs: Mention GtkSuggestionEntry in list widget overview 2020-07-24 10:41:35 -04:00
Matthias Clasen
c86a4222a9 Add GtkSuggestionEntry
GtkSuggestionEntry is a replacement for GtkComboBoxText
and GtkEntryCompletion, very similar to GtkDropDown, but
using an entry as widget.
2020-07-24 10:41:35 -04:00
Matthias Clasen
16d09f154e sortlistmodel: Fix a crash 2020-07-24 10:41:35 -04:00
Matthias Clasen
67fdc4f533 dropdown: Fix popup sizing
Setting a width request is not quite enough, since
gtk_widget_set_size_request() only queues a resize
when the widget is visible. Explicitly force one
here. Without this, the popup sometimes shows up
too small.
2020-07-24 10:41:35 -04:00
15 changed files with 1845 additions and 29 deletions

View File

@@ -1,13 +1,15 @@
/* Drop Downs
/* Lists/Selections
*
* The GtkDropDown widget is a modern alternative to GtkComboBox.
* It uses list models instead of tree models, and the content is
* displayed using widgets instead of cell renderers.
* The GtkDropDown and GtkSuggestionEntry widgets are modern
* alternatives to GtkComboBox and GtkEntryCompletion.
*
* They use list models instead of tree models, and the content
* is displayed using widgets instead of cell renderers.
*
* The examples here demonstrate how to use different kinds of
* list models with GtkDropDown, how to use search and how to
* display the selected item differently from the presentation
* in the popup.
* list models with GtkDropDown and GtkSuggestionEntry, how to
* use search and how to display the selected item differently
* from the presentation in the popup.
*/
#include <gtk/gtk.h>
@@ -218,13 +220,110 @@ get_title (gpointer item)
return g_strdup (STRING_HOLDER (item)->title);
}
static char *
get_file_name (gpointer item)
{
return g_strdup (g_file_info_get_display_name (G_FILE_INFO (item)));
}
static void
setup_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkWidget *box;
GtkWidget *icon;
GtkWidget *label;
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
icon = gtk_image_new ();
label = gtk_label_new ("");
gtk_label_set_xalign (GTK_LABEL (label), 0);
gtk_box_append (GTK_BOX (box), icon);
gtk_box_append (GTK_BOX (box), label);
gtk_list_item_set_child (item, box);
}
static void
bind_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkMatchObject *match = GTK_MATCH_OBJECT (gtk_list_item_get_item (item));
GFileInfo *info = G_FILE_INFO (gtk_match_object_get_item (match));
GtkWidget *box = gtk_list_item_get_child (item);
GtkWidget *icon = gtk_widget_get_first_child (box);
GtkWidget *label = gtk_widget_get_last_child (box);
gtk_image_set_from_gicon (GTK_IMAGE (icon), g_file_info_get_icon (info));
gtk_label_set_label (GTK_LABEL (label), g_file_info_get_display_name (info));
}
static void
setup_highlight_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkWidget *label;
label = gtk_label_new ("");
gtk_label_set_xalign (GTK_LABEL (label), 0);
gtk_list_item_set_child (item, label);
}
static void
bind_highlight_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkMatchObject *obj;
GtkWidget *label;
PangoAttrList *attrs;
PangoAttribute *attr;
const char *str;
obj = GTK_MATCH_OBJECT (gtk_list_item_get_item (item));
label = gtk_list_item_get_child (item);
str = gtk_match_object_get_string (obj);
gtk_label_set_label (GTK_LABEL (label), str);
attrs = pango_attr_list_new ();
attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
attr->start_index = gtk_match_object_get_match_start (obj);
attr->end_index = gtk_match_object_get_match_end (obj);
pango_attr_list_insert (attrs, attr);
gtk_label_set_attributes (GTK_LABEL (label), attrs);
pango_attr_list_unref (attrs);
}
static void
match_func (GtkMatchObject *obj,
const char *search,
gpointer user_data)
{
char *tmp1, *tmp2;
char *p;
tmp1 = g_utf8_normalize (gtk_match_object_get_string (obj), -1, G_NORMALIZE_ALL);
tmp2 = g_utf8_normalize (search, -1, G_NORMALIZE_ALL);
if ((p = strstr (tmp1, tmp2)) != NULL)
gtk_match_object_set_match (obj,
p - tmp1,
(p - tmp1) + g_utf8_strlen (search, -1),
1);
else
gtk_match_object_set_match (obj, 0, 0, 0);
g_free (tmp1);
g_free (tmp2);
}
GtkWidget *
do_dropdown (GtkWidget *do_widget)
{
static GtkWidget *window = NULL;
GtkWidget *button, *box, *spin, *check;
GtkWidget *button, *box, *spin, *check, *hbox, *label, *entry;
GListModel *model;
GtkExpression *expression;
GtkListItemFactory *factory;
const char * const times[] = { "1 minute", "2 minutes", "5 minutes", "20 minutes", NULL };
const char * const many_times[] = {
"1 minute", "2 minutes", "5 minutes", "10 minutes", "15 minutes", "20 minutes",
@@ -237,23 +336,51 @@ do_dropdown (GtkWidget *do_widget)
const char * const device_descriptions[] = {
"Built-in Audio", "Built-in audio", "Thinkpad Tunderbolt 3 Dock USB Audio", "Thinkpad Tunderbolt 3 Dock USB Audio", NULL
};
char *cwd;
GFile *file;
GListModel *dir;
GtkStringList *strings;
if (!window)
{
window = gtk_window_new ();
gtk_window_set_display (GTK_WINDOW (window),
gtk_widget_get_display (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Drop Downs");
gtk_window_set_title (GTK_WINDOW (window), "Selections");
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
gtk_widget_set_margin_start (box, 10);
gtk_widget_set_margin_end (box, 10);
gtk_widget_set_margin_top (box, 10);
gtk_widget_set_margin_bottom (box, 10);
gtk_window_set_child (GTK_WINDOW (window), box);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 20);
gtk_widget_set_margin_start (hbox, 20);
gtk_widget_set_margin_end (hbox, 20);
gtk_widget_set_margin_top (hbox, 20);
gtk_widget_set_margin_bottom (hbox, 20);
gtk_window_set_child (GTK_WINDOW (window), hbox);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
gtk_box_append (GTK_BOX (hbox), box);
label = gtk_label_new ("Dropdowns");
gtk_widget_add_css_class (label, "title-4");
gtk_box_append (GTK_BOX (box), label);
/* A basic dropdown */
button = drop_down_new_from_strings (times, NULL, NULL);
gtk_box_append (GTK_BOX (box), button);
/* A dropdown using an expression to obtain strings */
button = drop_down_new_from_strings (many_times, NULL, NULL);
gtk_drop_down_set_enable_search (GTK_DROP_DOWN (button), TRUE);
expression = gtk_cclosure_expression_new (G_TYPE_STRING, NULL,
0, NULL,
(GCallback)get_title,
NULL, NULL);
gtk_drop_down_set_expression (GTK_DROP_DOWN (button), expression);
gtk_expression_unref (expression);
gtk_box_append (GTK_BOX (box), button);
/* A dropdown using a non-trivial model, and search */
button = gtk_drop_down_new ();
model = G_LIST_MODEL (pango_cairo_font_map_get_default ());
@@ -270,30 +397,118 @@ do_dropdown (GtkWidget *do_widget)
spin = gtk_spin_button_new_with_range (-1, g_list_model_get_n_items (G_LIST_MODEL (model)), 1);
gtk_widget_set_halign (spin, GTK_ALIGN_START);
gtk_widget_set_margin_start (spin, 20);
g_object_bind_property (button, "selected", spin, "value", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
gtk_box_append (GTK_BOX (box), spin);
check = gtk_check_button_new_with_label ("Enable search");
gtk_widget_set_margin_start (check, 20);
g_object_bind_property (button, "enable-search", check, "active", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
gtk_box_append (GTK_BOX (box), check);
g_object_unref (model);
button = drop_down_new_from_strings (times, NULL, NULL);
gtk_box_append (GTK_BOX (box), button);
button = drop_down_new_from_strings (many_times, NULL, NULL);
gtk_drop_down_set_enable_search (GTK_DROP_DOWN (button), TRUE);
expression = gtk_cclosure_expression_new (G_TYPE_STRING, NULL,
0, NULL,
(GCallback)get_title,
NULL, NULL);
gtk_drop_down_set_expression (GTK_DROP_DOWN (button), expression);
gtk_expression_unref (expression);
gtk_box_append (GTK_BOX (box), button);
/* A dropdown with a separate list factory */
button = drop_down_new_from_strings (device_titles, device_icons, device_descriptions);
gtk_box_append (GTK_BOX (box), button);
gtk_box_append (GTK_BOX (hbox), gtk_separator_new (GTK_ORIENTATION_VERTICAL));
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
gtk_box_append (GTK_BOX (hbox), box);
label = gtk_label_new ("Suggestions");
gtk_widget_add_css_class (label, "title-4");
gtk_box_append (GTK_BOX (box), label);
/* A basic suggestion entry */
entry = gtk_suggestion_entry_new ();
g_object_set (entry, "placeholder-text", "Words with T or G…", NULL);
strings = gtk_string_list_new ((const char *[]){
"GNOME",
"gnominious",
"Gnomonic projection",
"total",
"totally",
"toto",
"tottery",
"totterer",
"Totten trust",
"totipotent",
"totipotency",
"totemism",
"totem pole",
"Totara",
"totalizer",
"totalizator",
"totalitarianism",
"total parenteral nutrition",
"total hysterectomy",
"total eclipse",
"Totipresence",
"Totipalmi",
"Tomboy",
"zombie",
NULL});
gtk_suggestion_entry_set_model (GTK_SUGGESTION_ENTRY (entry), G_LIST_MODEL (strings));
g_object_unref (strings);
gtk_box_append (GTK_BOX (box), entry);
/* A suggestion entry using a custom model, and no filtering */
entry = gtk_suggestion_entry_new ();
cwd = g_get_current_dir ();
file = g_file_new_for_path (cwd);
dir = G_LIST_MODEL (gtk_directory_list_new ("standard::display-name,standard::content-type,standard::icon,standard::size", file));
gtk_suggestion_entry_set_model (GTK_SUGGESTION_ENTRY (entry), dir);
g_object_unref (dir);
g_object_unref (file);
g_free (cwd);
expression = gtk_cclosure_expression_new (G_TYPE_STRING, NULL,
0, NULL,
(GCallback)get_file_name,
NULL, NULL);
gtk_suggestion_entry_set_expression (GTK_SUGGESTION_ENTRY (entry), expression);
gtk_expression_unref (expression);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_item), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_item), NULL);
gtk_suggestion_entry_set_factory (GTK_SUGGESTION_ENTRY (entry), factory);
g_object_unref (factory);
gtk_suggestion_entry_set_use_filter (GTK_SUGGESTION_ENTRY (entry), FALSE);
gtk_suggestion_entry_set_show_arrow (GTK_SUGGESTION_ENTRY (entry), TRUE);
gtk_box_append (GTK_BOX (box), entry);
/* A suggestion entry with match highlighting */
entry = gtk_suggestion_entry_new ();
g_object_set (entry, "placeholder-text", "Destination", NULL);
strings = gtk_string_list_new ((const char *[]){
"app-mockups",
"settings-mockups",
"os-mockups",
"software-mockups",
"mocktails",
NULL});
gtk_suggestion_entry_set_model (GTK_SUGGESTION_ENTRY (entry), G_LIST_MODEL (strings));
g_object_unref (strings);
gtk_box_append (GTK_BOX (box), entry);
gtk_suggestion_entry_set_match_func (GTK_SUGGESTION_ENTRY (entry), match_func, NULL, NULL);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_highlight_item), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_highlight_item), NULL);
gtk_suggestion_entry_set_factory (GTK_SUGGESTION_ENTRY (entry), factory);
g_object_unref (factory);
}
if (!gtk_widget_get_visible (window))

View File

@@ -18,7 +18,6 @@ demos = files([
'cursors.c',
'dialog.c',
'drawingarea.c',
'dropdown.c',
'dnd.c',
'editable_cells.c',
'entry_completion.c',
@@ -47,6 +46,7 @@ demos = files([
'listview_colors.c',
'listview_filebrowser.c',
'listview_minesweeper.c',
'dropdown.c',
'listview_settings.c',
'listview_weather.c',
'listview_words.c',

View File

@@ -223,6 +223,7 @@
<xi:include href="xml/gtksearchentry.xml" />
<xi:include href="xml/gtksearchbar.xml" />
<xi:include href="xml/gtkeditablelabel.xml" />
<xi:include href="xml/gtksuggestionentry.xml" />
</chapter>
<chapter id="TextWidgetObjects">

View File

@@ -7641,3 +7641,27 @@ gtk_selection_filter_model_new_for_type
gtk_selection_filter_model_set_model
gtk_selection_filter_model_get_model
</SECTION>
<SECTION>
<FILE>gtksuggestionentry</FILE>
<TITLE>GtkSuggestionEntry</TITLE>
GtkSuggestionEntry
gtk_suggestion_entry_new
gtk_suggestion_entry_set_model
gtk_suggestion_entry_get_model
gtk_suggestion_entry_set_from_strings
gtk_suggestion_entry_set_factory
gtk_suggestion_entry_get_factory
gtk_suggestion_entry_set_expression
gtk_suggestion_entry_get_expression
gtk_suggestion_entry_set_use_filter
gtk_suggestion_entry_get_use_filter
gtk_suggestion_entry_set_insert_selection
gtk_suggestion_entry_get_insert_selection
gtk_suggestion_entry_set_insert_prefix
gtk_suggestion_entry_get_insert_prefix
gtk_suggestion_entry_set_show_button
gtk_suggestion_entry_get_show_button
gtk_suggestion_entry_set_minimum_length
gtk_suggestion_entry_get_minimum_length
</SECTION>

View File

@@ -220,6 +220,7 @@ gtk_string_filter_get_type
gtk_string_list_get_type
gtk_string_object_get_type
gtk_string_sorter_get_type
gtk_suggestion_entry_get_type
gtk_switch_get_type
gtk_level_bar_get_type
gtk_style_context_get_type

View File

@@ -224,4 +224,6 @@ transitioning code for easy lookup:
| #GtkCellLayout | #GtkListItemFactory |
| #GtkCellArea | #GtkWidget |
| #GtkCellRenderer | #GtkWidget |
| #GtkComboBoxText | #GtkSuggestionEntry |
| #GtkEntryCompletion | #GtkSuggestionEntry |

View File

@@ -247,6 +247,7 @@
#include <gtk/gtkstringsorter.h>
#include <gtk/gtkstylecontext.h>
#include <gtk/gtkstyleprovider.h>
#include <gtk/gtksuggestionentry.h>
#include <gtk/gtkswitch.h>
#include <gtk/gtktext.h>
#include <gtk/gtktextbuffer.h>

View File

@@ -379,6 +379,7 @@ gtk_drop_down_size_allocate (GtkWidget *widget,
gtk_widget_size_allocate (self->button, &(GtkAllocation) { 0, 0, width, height }, baseline);
gtk_widget_set_size_request (self->popup, width, -1);
gtk_widget_queue_resize (self->popup);
gtk_native_check_resize (GTK_NATIVE (self->popup));
}

View File

@@ -148,6 +148,9 @@ gtk_sort_list_model_get_item (GListModel *list,
if (self->model == NULL)
return NULL;
if (position >= self->n_items)
return NULL;
if (self->positions)
position = pos_from_key (self, self->positions[position]);

1441
gtk/gtksuggestionentry.c Normal file

File diff suppressed because it is too large Load Diff

115
gtk/gtksuggestionentry.h Normal file
View File

@@ -0,0 +1,115 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2020 Red Hat, Inc.
*
* Author: Matthias Clasen <mclasen@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/>.
*/
#ifndef __GTK_SUGGESTION_ENTRY_H__
#define __GTK_SUGGESTION_ENTRY_H__
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkfilter.h>
#include <gtk/gtkexpression.h>
#include <gtk/gtklistitemfactory.h>
#include <gtk/gtkwidget.h>
G_BEGIN_DECLS
#define GTK_TYPE_MATCH_OBJECT (gtk_match_object_get_type ())
#define GTK_MATCH_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_MATCH_OBJECT, GtkMatchObject))
#define GTK_IS_MATCH_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_MATCH_OBJECT))
typedef struct _GtkMatchObject GtkMatchObject;
GDK_AVAILABLE_IN_ALL
GType gtk_match_object_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL
gpointer gtk_match_object_get_item (GtkMatchObject *object);
GDK_AVAILABLE_IN_ALL
const char * gtk_match_object_get_string (GtkMatchObject *object);
GDK_AVAILABLE_IN_ALL
guint gtk_match_object_get_match_start (GtkMatchObject *object);
GDK_AVAILABLE_IN_ALL
guint gtk_match_object_get_match_end (GtkMatchObject *object);
GDK_AVAILABLE_IN_ALL
guint gtk_match_object_get_score (GtkMatchObject *object);
GDK_AVAILABLE_IN_ALL
void gtk_match_object_set_match (GtkMatchObject *object,
guint start,
guint end,
guint score);
#define GTK_TYPE_SUGGESTION_ENTRY (gtk_suggestion_entry_get_type ())
#define GTK_SUGGESTION_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SUGGESTION_ENTRY, GtkSuggestionEntry))
#define GTK_IS_SUGGESTION_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SUGGESTION_ENTRY))
typedef struct _GtkSuggestionEntry GtkSuggestionEntry;
GDK_AVAILABLE_IN_ALL
GType gtk_suggestion_entry_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL
GtkWidget* gtk_suggestion_entry_new (void);
GDK_AVAILABLE_IN_ALL
void gtk_suggestion_entry_set_model (GtkSuggestionEntry *self,
GListModel *model);
GDK_AVAILABLE_IN_ALL
GListModel * gtk_suggestion_entry_get_model (GtkSuggestionEntry *self);
GDK_AVAILABLE_IN_ALL
void gtk_suggestion_entry_set_factory (GtkSuggestionEntry *self,
GtkListItemFactory *factory);
GDK_AVAILABLE_IN_ALL
GtkListItemFactory *
gtk_suggestion_entry_get_factory (GtkSuggestionEntry *self);
GDK_AVAILABLE_IN_ALL
void gtk_suggestion_entry_set_use_filter (GtkSuggestionEntry *self,
gboolean use_ilter);
GDK_AVAILABLE_IN_ALL
gboolean gtk_suggestion_entry_get_use_filter (GtkSuggestionEntry *self);
GDK_AVAILABLE_IN_ALL
void gtk_suggestion_entry_set_expression (GtkSuggestionEntry *self,
GtkExpression *expression);
GDK_AVAILABLE_IN_ALL
GtkExpression * gtk_suggestion_entry_get_expression (GtkSuggestionEntry *self);
GDK_AVAILABLE_IN_ALL
void gtk_suggestion_entry_set_show_arrow (GtkSuggestionEntry *self,
gboolean show_arrow);
GDK_AVAILABLE_IN_ALL
gboolean gtk_suggestion_entry_get_show_arrow (GtkSuggestionEntry *self);
typedef void (* GtkSuggestionEntryMatchFunc) (GtkMatchObject *object,
const char *search,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
void gtk_suggestion_entry_set_match_func (GtkSuggestionEntry *self,
GtkSuggestionEntryMatchFunc func,
gpointer user_data,
GDestroyNotify destroy);
G_END_DECLS
#endif /* __GTK_SUGGESTION_ENTRY_H__ */

View File

@@ -383,6 +383,7 @@ gtk_public_sources = files([
'gtkstringsorter.c',
'gtkstylecontext.c',
'gtkstyleprovider.c',
'gtksuggestionentry.c',
'gtkswitch.c',
'gtktestutils.c',
'gtktext.c',
@@ -652,6 +653,7 @@ gtk_public_headers = files([
'gtkstringsorter.h',
'gtkstylecontext.h',
'gtkstyleprovider.h',
'gtksuggestionentry.h',
'gtkswitch.h',
'gtktestutils.h',
'gtktext.h',

View File

@@ -1152,8 +1152,10 @@ spinbutton {
/**************
* ComboBoxes *
**************/
entry.suggestion > popover.menu.background > contents,
dropdown > popover.menu.background > contents { padding: 0; } //allow search entries with no margin
entry.suggestion,
dropdown,
combobox {
arrow {

View File

@@ -417,6 +417,10 @@ G_GNUC_END_IGNORE_DEPRECATIONS
strcmp (pspec->name, "factory") == 0)
continue;
if (g_type_is_a (type, GTK_TYPE_SUGGESTION_ENTRY) &&
strcmp (pspec->name, "factory") == 0)
continue;
if (g_type_is_a (type, GTK_TYPE_BOOKMARK_LIST) &&
(strcmp (pspec->name, "filename") == 0 ||
strcmp (pspec->name, "loading") == 0))

View File

@@ -653,6 +653,10 @@ test_type (gconstpointer data)
g_str_equal (pspec->name, "selected"))
continue;
if (g_type_is_a (type, GTK_TYPE_SUGGESTION_ENTRY) &&
g_str_equal (pspec->name, "popup-visible"))
continue;
/* can't set position without a notebook */
if (g_type_is_a (type, GTK_TYPE_NOTEBOOK_PAGE) &&
g_str_equal (pspec->name, "position"))