[recipes] Allow editing unit and ingredient by clicking on thier labels
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes] Allow editing unit and ingredient by clicking on thier labels
- Date: Mon, 8 May 2017 15:33:50 +0000 (UTC)
commit 9528624532c0a6edbb002b739739d7c36629d1c0
Author: Ekta Nandwani <mailnandwaniekta gmail com>
Date: Mon May 8 20:18:49 2017 +0530
Allow editing unit and ingredient by clicking on thier labels
Clicking the GtkLabel(unit/ingredient) opens GtkEntry(unit/ingredient)
to edit contents one at a time and pressing enter/Esc saves it
src/gr-ingredients-viewer-row.c | 73 +++++++++++++++++++++++++++----------
src/gr-ingredients-viewer-row.ui | 62 +++++++++++++-------------------
src/gr-ingredients-viewer.c | 2 +-
src/gr-ingredients-viewer.h | 3 ++
4 files changed, 82 insertions(+), 58 deletions(-)
---
diff --git a/src/gr-ingredients-viewer-row.c b/src/gr-ingredients-viewer-row.c
index acfe5c0..dc01012 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;
@@ -192,6 +194,8 @@ gr_ingredients_viewer_row_set_editable (GrIngredientsViewerRow *row,
}
static void save_row (GrIngredientsViewerRow *row);
+static void save_unit (GrIngredientsViewerRow *row);
+static void save_ingredient (GrIngredientsViewerRow *row);
static void
gr_ingredients_viewer_row_set_active (GrIngredientsViewerRow *row,
@@ -257,24 +261,38 @@ drag_handle_clicked (GrIngredientsViewerRow *row)
static void
edit_ingredient (GrIngredientsViewerRow *row)
{
+ GrIngredientsViewer *viewer = GR_INGREDIENTS_VIEWER (gtk_widget_get_ancestor (GTK_WIDGET (row),
GR_TYPE_INGREDIENTS_VIEWER));
+ save_unit (row);
+ if (row->editable) {
+ set_active_row (viewer, GTK_WIDGET (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;
const char *unit;
-
amount = row->amount ? row->amount : "";
space = amount[0] ? " " : "";
unit = row->unit ? row->unit : "";
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);
-
+ GrIngredientsViewer *viewer = GR_INGREDIENTS_VIEWER (gtk_widget_get_ancestor (GTK_WIDGET (row),
GR_TYPE_INGREDIENTS_VIEWER));
+ save_ingredient (row);
+
+ if (row->editable) {
+ set_active_row (viewer, GTK_WIDGET (row));
+ gtk_entry_set_text (GTK_ENTRY (row->unit_entry), tmp);
+ gtk_stack_set_visible_child_name (GTK_STACK (row->unit_stack), "unit_entry");
+ gtk_widget_grab_focus (row->unit_entry);
+ g_signal_emit (row, signals[EDIT], 0);
+ }
}
static void
@@ -297,32 +315,44 @@ parse_unit (const char *text,
}
static void
-save_row (GrIngredientsViewerRow *row)
+save_unit (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) {
+static void
+save_ingredient (GrIngredientsViewerRow *row)
+{
+ GtkWidget *visible;
+ 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);
}
}
+static void
+save_row (GrIngredientsViewerRow *row)
+{
+ save_unit (row);
+ save_ingredient (row);
+}
+
static gboolean
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 +433,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..a230ee0 100644
--- a/src/gr-ingredients-viewer-row.ui
+++ b/src/gr-ingredients-viewer-row.ui
@@ -12,19 +12,22 @@
<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>
+ </child>
</object>
- <packing>
- <property name="name">unit_label</property>
- </packing>
</child>
<child>
<object class="GtkEntry" id="unit_entry">
@@ -47,16 +50,19 @@
<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>
+ </child>
</object>
- <packing>
- <property name="name">ingredient_label</property>
- </packing>
</child>
<child>
<object class="GtkEntry" id="ingredient_entry">
@@ -101,24 +107,6 @@
<object class="GtkButton">
<property name="visible">1</property>
<property name="relief">none</property>
- <signal name="clicked" handler="edit_ingredient" swapped="yes"/>
- <style>
- <class name="image-button"/>
- <class name="circular"/>
- </style>
- <child>
- <object class="GtkImage">
- <property name="visible">1</property>
- <property name="icon-name">document-edit-symbolic</property>
- <property name="icon-size">1</property>
- </object>
- </child>
- </object>
- </child>
- <child>
- <object class="GtkButton">
- <property name="visible">1</property>
- <property name="relief">none</property>
<signal name="clicked" handler="emit_delete" swapped="yes"/>
<style>
<class name="image-button"/>
diff --git a/src/gr-ingredients-viewer.c b/src/gr-ingredients-viewer.c
index e5006ea..54efd1f 100644
--- a/src/gr-ingredients-viewer.c
+++ b/src/gr-ingredients-viewer.c
@@ -82,7 +82,7 @@ gr_ingredients_viewer_finalize (GObject *object)
G_OBJECT_CLASS (gr_ingredients_viewer_parent_class)->finalize (object);
}
-static void
+void
set_active_row (GrIngredientsViewer *viewer,
GtkWidget *row)
{
diff --git a/src/gr-ingredients-viewer.h b/src/gr-ingredients-viewer.h
index caef6a4..fa15edd 100644
--- a/src/gr-ingredients-viewer.h
+++ b/src/gr-ingredients-viewer.h
@@ -28,5 +28,8 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE (GrIngredientsViewer, gr_ingredients_viewer, GR, INGREDIENTS_VIEWER, GtkBox)
+void set_active_row (GrIngredientsViewer *viewer,
+ GtkWidget *row);
+
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]