[recipes/inline-editing-2] Allow editing unit and ingredient by clicking on thier labels
- From: Ekta Nandwani <ektan src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes/inline-editing-2] Allow editing unit and ingredient by clicking on thier labels
- Date: Thu, 4 May 2017 10:03:21 +0000 (UTC)
commit e09caf78562cc30b0d1a785e2c4d980c36c170f3
Author: Ekta Nandwani <mailnandwaniekta gmail com>
Date: Thu May 4 15:28:40 2017 +0530
Allow editing unit and ingredient by clicking on thier labels
Clicking the GtkLabel(unit/ingredient) opens GtkEntry(unit/ingredient)
to make changes and pressing enter/Esc saves it
src/gr-ingredients-viewer-row.c | 36 +++++++++++++++++----------
src/gr-ingredients-viewer-row.ui | 50 +++++++++++++++++++++++--------------
2 files changed, 54 insertions(+), 32 deletions(-)
---
diff --git a/src/gr-ingredients-viewer-row.c b/src/gr-ingredients-viewer-row.c
index acfe5c0..1efc1fa 100644
--- a/src/gr-ingredients-viewer-row.c
+++ b/src/gr-ingredients-viewer-row.c
@@ -42,6 +42,8 @@ struct _GrIngredientsViewerRow
GtkWidget *ingredient_label;
GtkWidget *ingredient_entry;
GtkWidget *ebox;
+ GtkWidget *unit_event_box;
+ GtkWidget *ingredient_event_box;
char *amount;
char *unit;
@@ -257,6 +259,15 @@ drag_handle_clicked (GrIngredientsViewerRow *row)
static void
edit_ingredient (GrIngredientsViewerRow *row)
{
+ gtk_entry_set_text (GTK_ENTRY (row->ingredient_entry), row->ingredient);
+ gtk_stack_set_visible_child_name (GTK_STACK (row->ingredient_stack), "ingredient_entry");
+ gtk_widget_grab_focus (row->ingredient_entry);
+ g_signal_emit (row, signals[EDIT], 0);
+}
+
+static void
+edit_unit (GrIngredientsViewerRow *row)
+{
g_autofree char *tmp = NULL;
const char *amount;
const char *space;
@@ -268,13 +279,9 @@ edit_ingredient (GrIngredientsViewerRow *row)
tmp = g_strdup_printf ("%s%s%s", amount, space, unit);
gtk_entry_set_text (GTK_ENTRY (row->unit_entry), tmp);
- gtk_entry_set_text (GTK_ENTRY (row->ingredient_entry), row->ingredient);
-
gtk_stack_set_visible_child_name (GTK_STACK (row->unit_stack), "unit_entry");
- gtk_stack_set_visible_child_name (GTK_STACK (row->ingredient_stack), "ingredient_entry");
gtk_widget_grab_focus (row->unit_entry);
g_signal_emit (row, signals[EDIT], 0);
-
}
static void
@@ -299,20 +306,20 @@ parse_unit (const char *text,
static void
save_row (GrIngredientsViewerRow *row)
{
- const char *visible;
+ GtkWidget *visible;
- visible = gtk_stack_get_visible_child_name (GTK_STACK (row->unit_stack));
- if (strcmp (visible, "unit_entry") == 0) {
+ visible = gtk_stack_get_visible_child (GTK_STACK (row->unit_stack));
+ if (visible == row->unit_entry) {
parse_unit (gtk_entry_get_text (GTK_ENTRY (row->unit_entry)), &row->amount, &row->unit);
update_unit (row);
- gtk_stack_set_visible_child_name (GTK_STACK (row->unit_stack), "unit_label");
+ gtk_stack_set_visible_child (GTK_STACK (row->unit_stack), row->unit_event_box);
}
- visible = gtk_stack_get_visible_child_name (GTK_STACK (row->ingredient_stack));
- if (strcmp (visible, "ingredient_entry") == 0) {
+ visible = gtk_stack_get_visible_child (GTK_STACK (row->ingredient_stack));
+ if (visible == row->ingredient_entry) {
row->ingredient = g_strdup (gtk_entry_get_text (GTK_ENTRY (row->ingredient_entry)));
gtk_label_set_label (GTK_LABEL (row->ingredient_label), row->ingredient);
- gtk_stack_set_visible_child_name (GTK_STACK (row->ingredient_stack), "ingredient_label");
+ gtk_stack_set_visible_child (GTK_STACK (row->ingredient_stack), row->ingredient_event_box);
}
}
@@ -321,8 +328,8 @@ entry_key_press (GrIngredientsViewerRow *row,
GdkEventKey *event)
{
if (event->keyval == GDK_KEY_Escape) {
- gtk_stack_set_visible_child_name (GTK_STACK (row->unit_stack), "unit_label");
- gtk_stack_set_visible_child_name (GTK_STACK (row->ingredient_stack), "ingredient_label");
+ gtk_stack_set_visible_child (GTK_STACK(row->unit_stack), row->unit_event_box);
+ gtk_stack_set_visible_child (GTK_STACK (row->ingredient_stack), row->ingredient_event_box);
return GDK_EVENT_STOP;
}
@@ -403,9 +410,12 @@ gr_ingredients_viewer_row_class_init (GrIngredientsViewerRowClass *klass)
gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewerRow, ingredient_entry);
gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewerRow, buttons_stack);
gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewerRow, ebox);
+ gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewerRow, unit_event_box);
+ gtk_widget_class_bind_template_child (widget_class, GrIngredientsViewerRow, ingredient_event_box);
gtk_widget_class_bind_template_callback (widget_class, emit_delete);
gtk_widget_class_bind_template_callback (widget_class, drag_handle_clicked);
+ gtk_widget_class_bind_template_callback (widget_class, edit_unit);
gtk_widget_class_bind_template_callback (widget_class, edit_ingredient);
gtk_widget_class_bind_template_callback (widget_class, save_row);
gtk_widget_class_bind_template_callback (widget_class, entry_key_press);
diff --git a/src/gr-ingredients-viewer-row.ui b/src/gr-ingredients-viewer-row.ui
index b1ae59b..839cc79 100644
--- a/src/gr-ingredients-viewer-row.ui
+++ b/src/gr-ingredients-viewer-row.ui
@@ -12,19 +12,25 @@
<object class="GtkStack" id="unit_stack">
<property name="visible">1</property>
<child>
- <object class="GtkLabel" id="unit_label">
+ <object class="GtkEventBox" id="unit_event_box">
<property name="visible">1</property>
- <property name="xalign">0</property>
- <property name="width-chars">10</property>
- <property name="margin-start">9</property>
- <property name="margin-end">9</property>
- <style>
- <class name="dim-label"/>
- </style>
+ <signal name="button-press-event" handler="edit_unit" swapped="yes"/>
+ <child>
+ <object class="GtkLabel" id="unit_label">
+ <property name="visible">1</property>
+ <property name="xalign">0</property>
+ <property name="width-chars">10</property>
+ <property name="margin-start">9</property>
+ <property name="margin-end">9</property>
+ <style>
+ <class name="dim-label"/>
+ </style>
+ </object>
+ <packing>
+ <property name="name">unit_label</property>
+ </packing>
+ </child>
</object>
- <packing>
- <property name="name">unit_label</property>
- </packing>
</child>
<child>
<object class="GtkEntry" id="unit_entry">
@@ -47,16 +53,22 @@
<object class="GtkStack" id="ingredient_stack">
<property name="visible">1</property>
<child>
- <object class="GtkLabel" id="ingredient_label">
+ <object class="GtkEventBox" id="ingredient_event_box">
<property name="visible">1</property>
- <property name="xalign">0</property>
- <property name="width-chars">20</property>
- <property name="margin-start">9</property>
- <property name="margin-end">9</property>
+ <signal name="button-press-event" handler="edit_ingredient" swapped="yes"/>
+ <child>
+ <object class="GtkLabel" id="ingredient_label">
+ <property name="visible">1</property>
+ <property name="xalign">0</property>
+ <property name="width-chars">20</property>
+ <property name="margin-start">9</property>
+ <property name="margin-end">9</property>
+ </object>
+ <packing>
+ <property name="name">ingredient_label</property>
+ </packing>
+ </child>
</object>
- <packing>
- <property name="name">ingredient_label</property>
- </packing>
</child>
<child>
<object class="GtkEntry" id="ingredient_entry">
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]