Merge branch 'editable-label' into 'master'

Editable label

See merge request GNOME/gtk!2081
This commit is contained in:
Matthias Clasen 2020-06-15 16:47:57 +00:00
commit 180f7e4861
12 changed files with 661 additions and 190 deletions

View File

@ -143,8 +143,9 @@ settings_key_new (GSettings *settings,
}
static void
item_value_changed (GtkListItem *item,
GtkEntry *entry)
item_value_changed (GtkEditableLabel *label,
GParamSpec *pspec,
GtkListItem *item)
{
SettingsKey *self;
const char *text;
@ -152,8 +153,9 @@ item_value_changed (GtkListItem *item,
GVariant *variant;
GError *error = NULL;
const char *name;
char *value;
text = gtk_editable_get_text (GTK_EDITABLE (entry));
text = gtk_editable_get_text (GTK_EDITABLE (label));
g_object_get (item, "item", &self, NULL);
g_object_unref (self);
@ -166,17 +168,25 @@ item_value_changed (GtkListItem *item,
{
g_warning ("%s", error->message);
g_clear_error (&error);
return;
goto revert;
}
if (!g_settings_schema_key_range_check (self->key, variant))
{
g_warning ("Not a valid value for %s", name);
return;
goto revert;
}
g_settings_set_value (self->settings, name, variant);
g_variant_unref (variant);
return;
revert:
gtk_widget_error_bell (GTK_WIDGET (label));
g_object_get (self, "value", &value, NULL);
gtk_editable_set_text (GTK_EDITABLE (label), value);
g_free (value);
}
static int

View File

@ -113,14 +113,13 @@
<interface>
<template class="GtkListItem">
<property name="child">
<object class="GtkEntry">
<signal name="activate" handler="item_value_changed" object="GtkListItem" swapped="yes"/>
<property name="xalign">0</property>
<binding name="text">
<object class="GtkEditableLabel">
<binding name="label">
<lookup name="value" type="SettingsKey">
<lookup name="item">GtkListItem</lookup>
</lookup>
</binding>
<signal name="notify::label" handler="item_value_changed"/>
</object>
</property>
</template>

View File

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

View File

@ -982,6 +982,18 @@ GTK_EDITABLE_GET_IFACE
gtk_editable_get_type
</SECTION>
<SECTION>
<FILE>gtkeditablelabel</FILE>
<TITLE>GtkEditableLabel</TITLE>
GtkEditableLabel
gtk_editable_label_new
gtk_editable_label_get_editing
gtk_editable_label_start_editing
gtk_editable_label_stop_editing
<SUBSECTION Private>
gtk_editable_label_get_type
</SECTION>
<SECTION>
<FILE>gtktext</FILE>
<TITLE>GtkText</TITLE>

View File

@ -69,6 +69,7 @@ gtk_drop_target_get_type
gtk_drop_target_async_get_type
gtk_drop_down_get_type
gtk_editable_get_type
gtk_editable_label_get_type
gtk_emoji_chooser_get_type
gtk_entry_buffer_get_type
gtk_entry_completion_get_type

View File

@ -100,6 +100,7 @@
#include <gtk/gtkdroptargetasync.h>
#include <gtk/gtkdropdown.h>
#include <gtk/gtkeditable.h>
#include <gtk/gtkeditablelabel.h>
#include <gtk/gtkemojichooser.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkentrybuffer.h>

View File

@ -30,7 +30,7 @@
#include "gtkliststore.h"
#include "gtktreeviewcolumn.h"
#include "gtksignallistitemfactory.h"
#include "gtklabel.h"
#include "gtkspinbutton.h"
@ -53,7 +53,7 @@ struct _GtkCustomPaperUnixDialog
{
GtkDialog parent_instance;
GtkWidget *treeview;
GtkWidget *listview;
GtkWidget *values_box;
GtkWidget *printer_combo;
GtkWidget *width_widget;
@ -63,8 +63,6 @@ struct _GtkCustomPaperUnixDialog
GtkWidget *left_widget;
GtkWidget *right_widget;
GtkTreeViewColumn *text_column;
gulong printer_inserted_tag;
gulong printer_removed_tag;
@ -73,7 +71,7 @@ struct _GtkCustomPaperUnixDialog
guint non_user_change : 1;
GtkListStore *custom_paper_list;
GListStore *custom_paper_list;
GtkListStore *printer_list;
GList *print_backends;
@ -253,11 +251,9 @@ gtk_print_load_custom_papers (GListStore *store)
g_list_free (papers);
}
void
_gtk_print_save_custom_papers (GtkListStore *store)
static void
gtk_print_save_custom_papers (GListStore *store)
{
GtkTreeModel *model = GTK_TREE_MODEL (store);
GtkTreeIter iter;
GKeyFile *keyfile;
gchar *filename, *data, *parentdir;
gsize len;
@ -265,21 +261,15 @@ _gtk_print_save_custom_papers (GtkListStore *store)
keyfile = g_key_file_new ();
if (gtk_tree_model_get_iter_first (model, &iter))
for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (store)); i++)
{
do
{
GtkPageSetup *page_setup;
gchar group[32];
GtkPageSetup *page_setup;
gchar group[32];
g_snprintf (group, sizeof (group), "Paper%u", i);
gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
gtk_page_setup_to_key_file (page_setup, keyfile, group);
++i;
} while (gtk_tree_model_iter_next (model, &iter));
page_setup = g_list_model_get_item (G_LIST_MODEL (store), i);
g_snprintf (group, sizeof (group), "Paper%u", i);
gtk_page_setup_to_key_file (page_setup, keyfile, group);
g_object_unref (page_setup);
}
filename = custom_paper_get_filename ();
@ -310,7 +300,7 @@ custom_paper_dialog_response_cb (GtkDialog *dialog,
{
GtkCustomPaperUnixDialog *self = GTK_CUSTOM_PAPER_UNIX_DIALOG (dialog);
_gtk_print_save_custom_papers (self->custom_paper_list);
gtk_print_save_custom_papers (self->custom_paper_list);
}
static void
@ -331,8 +321,8 @@ gtk_custom_paper_unix_dialog_init (GtkCustomPaperUnixDialog *dialog)
gtk_list_store_append (dialog->printer_list, &iter);
dialog->custom_paper_list = gtk_list_store_new (1, G_TYPE_OBJECT);
_gtk_print_load_custom_papers (dialog->custom_paper_list);
dialog->custom_paper_list = g_list_store_new (GTK_TYPE_PAGE_SETUP);
gtk_print_load_custom_papers (dialog->custom_paper_list);
populate_dialog (dialog);
@ -380,11 +370,7 @@ gtk_custom_paper_unix_dialog_finalize (GObject *object)
dialog->request_details_tag = 0;
}
if (dialog->custom_paper_list)
{
g_object_unref (dialog->custom_paper_list);
dialog->custom_paper_list = NULL;
}
g_clear_object (&dialog->custom_paper_list);
g_free (dialog->waiting_for_printer);
dialog->waiting_for_printer = NULL;
@ -638,17 +624,19 @@ custom_paper_printer_data_func (GtkCellLayout *cell_layout,
static void
update_combo_sensitivity_from_printers (GtkCustomPaperUnixDialog *dialog)
{
GtkTreeIter iter;
gboolean sensitive;
GtkTreeSelection *selection;
gboolean sensitive = FALSE;
GtkTreeModel *model;
GtkTreeIter iter;
GListModel *selection;
sensitive = FALSE;
model = GTK_TREE_MODEL (dialog->printer_list);
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
selection = gtk_list_view_get_model (GTK_LIST_VIEW (dialog->listview));
if (gtk_tree_model_get_iter_first (model, &iter) &&
gtk_tree_model_iter_next (model, &iter) &&
gtk_tree_selection_get_selected (selection, NULL, &iter))
gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (selection)))
sensitive = TRUE;
gtk_widget_set_sensitive (dialog->printer_combo, sensitive);
@ -657,19 +645,15 @@ update_combo_sensitivity_from_printers (GtkCustomPaperUnixDialog *dialog)
static void
update_custom_widgets_from_list (GtkCustomPaperUnixDialog *dialog)
{
GtkTreeSelection *selection;
GtkTreeModel *model;
GtkTreeIter iter;
GListModel *model;
GtkPageSetup *page_setup;
model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
model = gtk_list_view_get_model (GTK_LIST_VIEW (dialog->listview));
page_setup = gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (model));
dialog->non_user_change = TRUE;
if (gtk_tree_selection_get_selected (selection, NULL, &iter))
if (page_setup != NULL)
{
gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
unit_widget_set (dialog->width_widget,
gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM));
unit_widget_set (dialog->height_widget,
@ -696,7 +680,8 @@ update_custom_widgets_from_list (GtkCustomPaperUnixDialog *dialog)
}
static void
selected_custom_paper_changed (GtkTreeSelection *selection,
selected_custom_paper_changed (GObject *list,
GParamSpec *pspec,
GtkCustomPaperUnixDialog *dialog)
{
update_custom_widgets_from_list (dialog);
@ -705,21 +690,19 @@ selected_custom_paper_changed (GtkTreeSelection *selection,
static void
unit_widget_changed (GtkCustomPaperUnixDialog *dialog)
{
gdouble w, h, top, bottom, left, right;
GtkTreeSelection *selection;
GtkTreeIter iter;
double w, h, top, bottom, left, right;
GListModel *model;
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
if (dialog->non_user_change)
return;
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
model = gtk_list_view_get_model (GTK_LIST_VIEW (dialog->listview));
page_setup = gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (model));
if (gtk_tree_selection_get_selected (selection, NULL, &iter))
if (page_setup != NULL)
{
gtk_tree_model_get (GTK_TREE_MODEL (dialog->custom_paper_list), &iter, 0, &page_setup, -1);
w = unit_widget_get (dialog->width_widget);
h = unit_widget_get (dialog->height_widget);
@ -735,8 +718,6 @@ unit_widget_changed (GtkCustomPaperUnixDialog *dialog)
gtk_page_setup_set_bottom_margin (page_setup, bottom, GTK_UNIT_MM);
gtk_page_setup_set_left_margin (page_setup, left, GTK_UNIT_MM);
gtk_page_setup_set_right_margin (page_setup, right, GTK_UNIT_MM);
g_object_unref (page_setup);
}
}
@ -744,27 +725,20 @@ static gboolean
custom_paper_name_used (GtkCustomPaperUnixDialog *dialog,
const gchar *name)
{
GtkTreeModel *model;
GtkTreeIter iter;
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
guint i;
model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
if (gtk_tree_model_get_iter_first (model, &iter))
for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (dialog->custom_paper_list)); i++)
{
do
page_setup = g_list_model_get_item (G_LIST_MODEL (dialog->custom_paper_list), i);
paper_size = gtk_page_setup_get_paper_size (page_setup);
if (strcmp (name, gtk_paper_size_get_name (paper_size)) == 0)
{
gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
paper_size = gtk_page_setup_get_paper_size (page_setup);
if (strcmp (name,
gtk_paper_size_get_name (paper_size)) == 0)
{
g_object_unref (page_setup);
return TRUE;
}
g_object_unref (page_setup);
} while (gtk_tree_model_iter_next (model, &iter));
return TRUE;
}
g_object_unref (page_setup);
}
return FALSE;
@ -773,18 +747,11 @@ custom_paper_name_used (GtkCustomPaperUnixDialog *dialog,
static void
add_custom_paper (GtkCustomPaperUnixDialog *dialog)
{
GtkListStore *store;
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
GtkTreeSelection *selection;
GtkTreePath *path;
GtkTreeIter iter;
gchar *name;
gint i;
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
store = dialog->custom_paper_list;
i = 1;
name = NULL;
do
@ -802,48 +769,31 @@ add_custom_paper (GtkCustomPaperUnixDialog *dialog)
gtk_page_setup_set_paper_size (page_setup, paper_size);
gtk_paper_size_free (paper_size);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, page_setup, -1);
g_list_store_append (dialog->custom_paper_list, page_setup);
g_object_unref (page_setup);
gtk_tree_selection_select_iter (selection, &iter);
path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
gtk_widget_grab_focus (dialog->treeview);
gtk_tree_view_set_cursor (GTK_TREE_VIEW (dialog->treeview), path,
dialog->text_column, TRUE);
gtk_tree_path_free (path);
/* FIXME start editing */
g_free (name);
}
static void
remove_custom_paper (GtkCustomPaperUnixDialog *dialog)
{
GtkTreeSelection *selection;
GtkTreeIter iter;
GtkListStore *store;
GListModel *model;
guint selected;
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->treeview));
store = dialog->custom_paper_list;
if (gtk_tree_selection_get_selected (selection, NULL, &iter))
{
GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
gtk_list_store_remove (store, &iter);
if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path))
gtk_tree_selection_select_iter (selection, &iter);
else if (gtk_tree_path_prev (path) && gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path))
gtk_tree_selection_select_iter (selection, &iter);
gtk_tree_path_free (path);
}
model = gtk_list_view_get_model (GTK_LIST_VIEW (dialog->listview));
selected = gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (model));
if (selected != GTK_INVALID_LIST_POSITION)
g_list_store_remove (dialog->custom_paper_list, selected);
}
static void
set_margins_from_printer (GtkCustomPaperUnixDialog *dialog,
GtkPrinter *printer)
{
gdouble top, bottom, left, right;
double top, bottom, left, right;
top = bottom = left = right = 0;
if (!gtk_printer_get_hard_margins (printer, &top, &bottom, &left, &right))
@ -921,53 +871,6 @@ margins_from_printer_changed (GtkCustomPaperUnixDialog *dialog)
}
}
static void
custom_size_name_edited (GtkCellRenderer *cell,
gchar *path_string,
gchar *new_text,
GtkCustomPaperUnixDialog *dialog)
{
GtkTreePath *path;
GtkTreeIter iter;
GtkListStore *store;
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
store = dialog->custom_paper_list;
path = gtk_tree_path_new_from_string (path_string);
gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path);
gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &page_setup, -1);
gtk_tree_path_free (path);
paper_size = gtk_paper_size_new_custom (new_text, new_text,
gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM),
gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM),
GTK_UNIT_MM);
gtk_page_setup_set_paper_size (page_setup, paper_size);
gtk_paper_size_free (paper_size);
g_object_unref (page_setup);
}
static void
custom_name_func (GtkTreeViewColumn *tree_column,
GtkCellRenderer *cell,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gpointer data)
{
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
gtk_tree_model_get (tree_model, iter, 0, &page_setup, -1);
if (page_setup)
{
paper_size = gtk_page_setup_get_paper_size (page_setup);
g_object_set (cell, "text", gtk_paper_size_get_display_name (paper_size), NULL);
g_object_unref (page_setup);
}
}
static GtkWidget *
wrap_in_frame (const gchar *label,
GtkWidget *child)
@ -998,18 +901,86 @@ wrap_in_frame (const gchar *label,
return frame;
}
static void
setup_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
gtk_list_item_set_child (item, gtk_editable_label_new (""));
}
static void
label_changed (GObject *object,
GParamSpec *pspec,
GtkListItem *item)
{
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
const char *new_text;
page_setup = gtk_list_item_get_item (item);
new_text = gtk_editable_get_text (GTK_EDITABLE (object));
paper_size = gtk_paper_size_new_custom (new_text, new_text,
gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM),
gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM), GTK_UNIT_MM);
gtk_page_setup_set_paper_size (page_setup, paper_size);
gtk_paper_size_free (paper_size);
}
static void
state_changed (GtkWidget *item,
GtkStateFlags old_state,
GtkWidget *label)
{
gboolean selected;
selected = (gtk_widget_get_state_flags (item) & GTK_STATE_FLAG_SELECTED) != 0;
gtk_editable_set_editable (GTK_EDITABLE (label), selected);
}
static void
bind_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
GtkWidget *label;
page_setup = gtk_list_item_get_item (item);
label = gtk_list_item_get_child (item);
paper_size = gtk_page_setup_get_paper_size (page_setup);
gtk_editable_set_text (GTK_EDITABLE (label),
gtk_paper_size_get_display_name (paper_size));
g_signal_connect (label, "notify::text",
G_CALLBACK (label_changed), item);
g_signal_connect (gtk_widget_get_parent (label), "state-flags-changed",
G_CALLBACK (state_changed), label);
}
static void
unbind_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkWidget *label;
label = gtk_list_item_get_child (item);
g_signal_handlers_disconnect_by_func (label, label_changed, item);
g_signal_handlers_disconnect_by_func (gtk_widget_get_parent (label), state_changed, label);
}
static void
populate_dialog (GtkCustomPaperUnixDialog *dialog)
{
GtkDialog *cpu_dialog = GTK_DIALOG (dialog);
GtkWidget *content_area;
GtkWidget *grid, *label, *widget, *frame, *combo;
GtkWidget *hbox, *vbox, *treeview, *scrolled, *toolbar, *button;
GtkWidget *hbox, *vbox, *listview, *scrolled, *toolbar, *button;
GtkCellRenderer *cell;
GtkTreeViewColumn *column;
GtkTreeIter iter;
GtkTreeSelection *selection;
GtkUnit user_units;
GListModel *model;
GtkListItemFactory *factory;
content_area = gtk_dialog_get_content_area (cpu_dialog);
gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
@ -1034,27 +1005,24 @@ populate_dialog (GtkCustomPaperUnixDialog *dialog)
gtk_box_append (GTK_BOX (vbox), scrolled);
gtk_widget_show (scrolled);
treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (dialog->custom_paper_list));
dialog->treeview = treeview;
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);
gtk_widget_set_size_request (treeview, 140, -1);
listview = gtk_list_view_new ();
gtk_widget_set_size_request (listview, 140, -1);
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
g_signal_connect (selection, "changed", G_CALLBACK (selected_custom_paper_changed), dialog);
model = G_LIST_MODEL (gtk_single_selection_new (G_LIST_MODEL (dialog->custom_paper_list)));
gtk_list_view_set_model (GTK_LIST_VIEW (listview), model);
g_signal_connect (model, "notify::selected", G_CALLBACK (selected_custom_paper_changed), dialog);
g_object_unref (model);
cell = gtk_cell_renderer_text_new ();
g_object_set (cell, "editable", TRUE, NULL);
g_signal_connect (cell, "edited",
G_CALLBACK (custom_size_name_edited), dialog);
dialog->text_column = column =
gtk_tree_view_column_new_with_attributes ("paper", cell, NULL);
gtk_tree_view_column_set_cell_data_func (column, cell, custom_name_func, NULL, NULL);
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);
g_signal_connect (factory, "unbind", G_CALLBACK (unbind_item), NULL);
gtk_list_view_set_factory (GTK_LIST_VIEW (listview), factory);
g_object_unref (factory);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
dialog->listview = listview;
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled), treeview);
gtk_widget_show (treeview);
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled), listview);
toolbar = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
@ -1194,14 +1162,8 @@ populate_dialog (GtkCustomPaperUnixDialog *dialog)
update_custom_widgets_from_list (dialog);
/* If no custom sizes, add one */
if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dialog->custom_paper_list),
&iter))
{
/* Need to realize treeview so we can start the rename */
gtk_widget_realize (treeview);
add_custom_paper (dialog);
}
if (g_list_model_get_n_items (G_LIST_MODEL (dialog->custom_paper_list)) == 0)
add_custom_paper (dialog);
load_print_backends (dialog);
}

View File

@ -26,7 +26,6 @@ G_BEGIN_DECLS
#define GTK_CUSTOM_PAPER_UNIX_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_CUSTOM_PAPER_UNIX_DIALOG, GtkCustomPaperUnixDialog))
#define GTK_IS_CUSTOM_PAPER_UNIX_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_CUSTOM_PAPER_UNIX_DIALOG))
typedef struct _GtkCustomPaperUnixDialog GtkCustomPaperUnixDialog;
GDK_AVAILABLE_IN_ALL
@ -34,9 +33,8 @@ GType gtk_custom_paper_unix_dialog_get_type (void) G_GNUC_
GtkWidget * _gtk_custom_paper_unix_dialog_new (GtkWindow *parent,
const gchar *title);
GtkUnit _gtk_print_get_default_user_units (void);
void gtk_print_load_custom_papers (GListStore *store);
void _gtk_print_load_custom_papers (GtkListStore *store);
void _gtk_print_save_custom_papers (GtkListStore *store);
void gtk_print_load_custom_papers (GListStore *store);
GList * _gtk_load_custom_papers (void);

429
gtk/gtkeditablelabel.c Normal file
View File

@ -0,0 +1,429 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2020, 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 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/>.
*/
#include "config.h"
#include "gtkeditablelabel.h"
#include "gtkeditable.h"
#include "gtklabel.h"
#include "gtkstack.h"
#include "gtktext.h"
#include "gtkbinlayout.h"
#include "gtkgestureclick.h"
#include "gtkprivate.h"
#include "gtkshortcut.h"
#include "gtkshortcuttrigger.h"
#include "gtkwidgetprivate.h"
#include "gtkintl.h"
/**
* SECTION:gtkeditablelabel
* @Short_description: A label that can be edited
* @Title: GtkEditableLabel
* @See_also: #GtkEditable, #GtkLabel, #GtkEntry
*
* A GtkEditableLabel is a #GtkLabel that allows users to
* edit the text by switching the widget to an edit mode.
*
* GtkEditableLabel does not have API of its own, but it
* implements the #GtkEditable interface.
*
* The default bindings for activating the edit mode is
* to click or press the Enter key. The default bindings
* for leaving the edit mode are the Enter key (to save
* the results) or the Escape key (to cancel the editing).
*/
struct _GtkEditableLabel
{
GtkWidget parent_instance;
GtkWidget *stack;
GtkWidget *label;
GtkWidget *entry;
};
struct _GtkEditableLabelClass
{
GtkWidgetClass parent_class;
};
enum
{
PROP_EDITING = 1,
NUM_PROPERTIES
};
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
static GtkEditable *
gtk_editable_label_get_delegate (GtkEditable *editable)
{
GtkEditableLabel *self = GTK_EDITABLE_LABEL (editable);
return GTK_EDITABLE (self->entry);
}
static void
gtk_editable_label_editable_init (GtkEditableInterface *iface)
{
iface->get_delegate = gtk_editable_label_get_delegate;
}
G_DEFINE_TYPE_WITH_CODE (GtkEditableLabel, gtk_editable_label, GTK_TYPE_WIDGET,
G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
gtk_editable_label_editable_init))
static void
start_editing (GtkWidget *widget,
const char *action_name,
GVariant *parameters)
{
gtk_editable_label_start_editing (GTK_EDITABLE_LABEL (widget));
}
static void
stop_editing (GtkWidget *widget,
const char *action_name,
GVariant *parameters)
{
gtk_editable_label_stop_editing (GTK_EDITABLE_LABEL (widget),
g_variant_get_boolean (parameters));
}
static void
clicked_cb (GtkWidget *self)
{
gtk_widget_activate_action (self, "editing.start", NULL);
}
static void
activate_cb (GtkWidget *self)
{
gtk_widget_activate_action (self, "editing.stop", "b", TRUE);
}
static void
text_changed (GtkEditableLabel *self)
{
/* Sync the entry text to the label, unless we are editing.
*
* This is necessary to catch apis like gtk_editable_insert_text(),
* which don't go through the ::text property.
*/
if (!gtk_editable_label_get_editing (self))
{
const char *text = gtk_editable_get_text (GTK_EDITABLE (self->entry));
gtk_label_set_label (GTK_LABEL (self->label), text);
}
}
static void
gtk_editable_label_init (GtkEditableLabel *self)
{
GtkGesture *gesture;
gtk_widget_set_focusable (GTK_WIDGET (self), TRUE);
self->stack = gtk_stack_new ();
self->label = gtk_label_new ("");
gtk_label_set_xalign (GTK_LABEL (self->label), 0.0);
self->entry = gtk_text_new ();
gtk_stack_add_named (GTK_STACK (self->stack), self->label, "label");
gtk_stack_add_named (GTK_STACK (self->stack), self->entry, "entry");
gtk_widget_set_parent (self->stack, GTK_WIDGET (self));
gesture = gtk_gesture_click_new ();
g_signal_connect_swapped (gesture, "pressed", G_CALLBACK (clicked_cb), self);
gtk_widget_add_controller (self->label, GTK_EVENT_CONTROLLER (gesture));
g_signal_connect_swapped (self->entry, "activate", G_CALLBACK (activate_cb), self);
g_signal_connect_swapped (self->entry, "notify::text", G_CALLBACK (text_changed), self);
gtk_editable_init_delegate (GTK_EDITABLE (self));
}
static gboolean
gtk_editable_label_grab_focus (GtkWidget *widget)
{
GtkEditableLabel *self = GTK_EDITABLE_LABEL (widget);
if (gtk_editable_label_get_editing (self))
return gtk_widget_grab_focus (self->entry);
else
return gtk_widget_grab_focus_self (widget);
}
static void
gtk_editable_label_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkEditableLabel *self = GTK_EDITABLE_LABEL (object);
if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
{
switch (prop_id)
{
case NUM_PROPERTIES + GTK_EDITABLE_PROP_TEXT:
gtk_label_set_label (GTK_LABEL (self->label), g_value_get_string (value));
break;
case NUM_PROPERTIES + GTK_EDITABLE_PROP_WIDTH_CHARS:
gtk_label_set_width_chars (GTK_LABEL (self->label), g_value_get_int (value));
break;
case NUM_PROPERTIES + GTK_EDITABLE_PROP_MAX_WIDTH_CHARS:
gtk_label_set_max_width_chars (GTK_LABEL (self->label), g_value_get_int (value));
break;
case NUM_PROPERTIES + GTK_EDITABLE_PROP_XALIGN:
gtk_label_set_xalign (GTK_LABEL (self->label), g_value_get_float (value));
break;
case NUM_PROPERTIES + GTK_EDITABLE_PROP_EDITABLE:
{
gboolean editable;
editable = g_value_get_boolean (value);
if (!editable)
gtk_editable_label_stop_editing (self, FALSE);
gtk_widget_action_set_enabled (GTK_WIDGET (self), "editing.start", editable);
gtk_widget_action_set_enabled (GTK_WIDGET (self), "editing.stop", editable);
}
break;
default: ;
}
return;
}
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_editable_label_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkEditableLabel *self = GTK_EDITABLE_LABEL (object);
if (gtk_editable_delegate_get_property (object, prop_id, value, pspec))
return;
switch (prop_id)
{
case PROP_EDITING:
g_value_set_boolean (value, gtk_editable_label_get_editing (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_editable_label_dispose (GObject *object)
{
GtkEditableLabel *self = GTK_EDITABLE_LABEL (object);
gtk_editable_finish_delegate (GTK_EDITABLE (self));
g_clear_pointer (&self->stack, gtk_widget_unparent);
G_OBJECT_CLASS (gtk_editable_label_parent_class)->dispose (object);
}
static void
gtk_editable_label_class_init (GtkEditableLabelClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
GtkShortcut *shortcut;
GtkShortcutTrigger *trigger;
GtkShortcutAction *action;
object_class->set_property = gtk_editable_label_set_property;
object_class->get_property = gtk_editable_label_get_property;
object_class->dispose = gtk_editable_label_dispose;
widget_class->grab_focus = gtk_editable_label_grab_focus;
/**
* GtkEditableLabel:editing:
*
* This property is %TRUE while the widget is in edit mode.
*/
properties[PROP_EDITING] =
g_param_spec_boolean ("editing",
P_("Editing"),
P_("Whether the widget is in editing mode"),
FALSE,
GTK_PARAM_READABLE);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
gtk_editable_install_properties (object_class, NUM_PROPERTIES);
/**
* GtkEditableLabel|editing.start:
*
* Switch the widget into editing mode, so that the
* user can make changes to the text.
*
* The default bindings for this action are clicking
* on the widget and the Enter key.
*
* This action is disabled when #GtkEditable:editing
* is %FALSE.
*/
gtk_widget_class_install_action (widget_class, "editing.start", NULL, start_editing);
/**
* GtkEditableLabel|editing.stop:
* @commit: Whether the make changes permanent
*
* Switch the widget out of editing mode. If @commit
* is %TRUE, then the results of the editing are taken
* as the new value of #GtkEditable:text.
*
* The default binding for this action is the Escape
* key.
*
* This action is disabled when #GtkEditable:editing
* is %FALSE.
*/
gtk_widget_class_install_action (widget_class, "editing.stop", "b", stop_editing);
trigger = gtk_alternative_trigger_new (
gtk_alternative_trigger_new (
gtk_keyval_trigger_new (GDK_KEY_Return, 0),
gtk_keyval_trigger_new (GDK_KEY_ISO_Enter, 0)),
gtk_keyval_trigger_new (GDK_KEY_KP_Enter, 0));
action = gtk_named_action_new ("editing.start");
shortcut = gtk_shortcut_new (trigger, action);
gtk_widget_class_add_shortcut (widget_class, shortcut);
g_object_unref (shortcut);
gtk_widget_class_add_binding_action (widget_class,
GDK_KEY_Escape, 0,
"editing.stop",
"b", FALSE);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
gtk_widget_class_set_css_name (widget_class, "editablelabel");
}
/**
* gtk_editable_label_new:
* @str: the text for the label
*
* Creates a new #GtkEditableLabel widget.
*
* Returns: the new #GtkEditableLabel
*/
GtkWidget *
gtk_editable_label_new (const char *str)
{
return g_object_new (GTK_TYPE_EDITABLE_LABEL,
"text", str,
NULL);
}
/**
* gtk_editable_label_get_editing:
* @self: a #GtkEditableLabel
*
* Returns whether the label is currently in editing mode.
*
* Returns: %TRUE if @self is currently in editing mode
*/
gboolean
gtk_editable_label_get_editing (GtkEditableLabel *self)
{
g_return_val_if_fail (GTK_IS_EDITABLE_LABEL (self), FALSE);
return gtk_stack_get_visible_child (GTK_STACK (self->stack)) == self->entry;
}
/**
* gtk_editable_label_start_editing:
* @self: a #GtkEditableLabel
*
* Switches the label into editing mode.
*/
void
gtk_editable_label_start_editing (GtkEditableLabel *self)
{
g_return_if_fail (GTK_IS_EDITABLE_LABEL (self));
if (gtk_editable_label_get_editing (self))
return;
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "entry");
gtk_widget_grab_focus (self->entry);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EDITING]);
}
/**
* gtk_editable_label_stop_editing:
* @self: a #GtkEditableLabel
* @commit: whether to set the edited text on the label
*
* Switches the label out of editing mode. If @commit is %TRUE,
* the resulting text is kept as the #GtkEditableLabel:text property
* value, otherwise the resulting text is discarded and the label
* will keep its previous #GtkEditableLabel:text property value.
*/
void
gtk_editable_label_stop_editing (GtkEditableLabel *self,
gboolean commit)
{
g_return_if_fail (GTK_IS_EDITABLE_LABEL (self));
if (!gtk_editable_label_get_editing (self))
return;
if (commit)
{
gtk_label_set_label (GTK_LABEL (self->label),
gtk_editable_get_text (GTK_EDITABLE (self->entry)));
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "label");
}
else
{
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "label");
gtk_editable_set_text (GTK_EDITABLE (self->entry),
gtk_label_get_label (GTK_LABEL (self->label)));
}
gtk_widget_grab_focus (GTK_WIDGET (self));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EDITING]);
}

49
gtk/gtkeditablelabel.h Normal file
View File

@ -0,0 +1,49 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2020 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 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_EDITABLE_LABEL_H__
#define __GTK_EDITABLE_LABEL_H__
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkwidget.h>
G_BEGIN_DECLS
#define GTK_TYPE_EDITABLE_LABEL (gtk_editable_label_get_type ())
GDK_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (GtkEditableLabel, gtk_editable_label, GTK, EDITABLE_LABEL, GtkWidget)
GDK_AVAILABLE_IN_ALL
GtkWidget * gtk_editable_label_new (const char *str);
GDK_AVAILABLE_IN_ALL
gboolean gtk_editable_label_get_editing (GtkEditableLabel *self);
GDK_AVAILABLE_IN_ALL
void gtk_editable_label_start_editing (GtkEditableLabel *self);
GDK_AVAILABLE_IN_ALL
void gtk_editable_label_stop_editing (GtkEditableLabel *self,
gboolean commit);
G_END_DECLS
#endif /* __GTK_EDITABLE_LABEL_H__ */

View File

@ -221,6 +221,7 @@ gtk_public_sources = files([
'gtkdroptargetasync.c',
'gtkdropdown.c',
'gtkeditable.c',
'gtkeditablelabel.c',
'gtkemojichooser.c',
'gtkemojicompletion.c',
'gtkentry.c',
@ -504,6 +505,7 @@ gtk_public_headers = files([
'gtkdroptargetasync.h',
'gtkdropdown.h',
'gtkeditable.h',
'gtkeditablelabel.h',
'gtkemojichooser.h',
'gtkentry.h',
'gtkentrybuffer.h',

View File

@ -434,6 +434,13 @@ treeview entry {
}
}
/*******************
* Editable Labels *
*******************/
editablelabel > stack > text {
@include entry(normal);
}
/***********
* Buttons *
***********/