[gnome-software] trivial: Add the ability to add a GtkSwitch to the source row widget



commit 7e0cfe9025a7f021da29f87813df4fed0e091916
Author: Richard Hughes <richard hughsie com>
Date:   Mon Aug 29 17:13:50 2016 +0100

    trivial: Add the ability to add a GtkSwitch to the source row widget
    
    Also, allow an extra line of text for future functionality.

 src/gs-sources-dialog-row.c  |   92 +++++++++++++++++++++++++++++++++++++++--
 src/gs-sources-dialog-row.h  |    7 +++
 src/gs-sources-dialog-row.ui |   67 +++++++++++++++++++++++-------
 3 files changed, 146 insertions(+), 20 deletions(-)
---
diff --git a/src/gs-sources-dialog-row.c b/src/gs-sources-dialog-row.c
index 29b88d4..6b41d3b 100644
--- a/src/gs-sources-dialog-row.c
+++ b/src/gs-sources-dialog-row.c
@@ -27,40 +27,122 @@ struct _GsSourcesDialogRow
 {
        GtkListBoxRow    parent_instance;
 
+       GtkWidget       *active_switch;
        GtkWidget       *name_label;
+       GtkWidget       *comment_label;
        GtkWidget       *description_label;
 };
 
+enum {
+       PROP_0,
+       PROP_SWITCH_ACTIVE,
+       PROP_LAST
+};
+
 G_DEFINE_TYPE (GsSourcesDialogRow, gs_sources_dialog_row, GTK_TYPE_LIST_BOX_ROW)
 
 void
-gs_sources_dialog_row_set_name (GsSourcesDialogRow *row,
-                                const gchar        *name)
+gs_sources_dialog_row_set_switch_enabled (GsSourcesDialogRow *row,
+                                      gboolean switch_enabled)
+{
+       gtk_widget_set_visible (row->active_switch, switch_enabled);
+}
+
+void
+gs_sources_dialog_row_set_switch_active (GsSourcesDialogRow *row,
+                                        gboolean switch_active)
+{
+       gtk_switch_set_active (GTK_SWITCH (row->active_switch), switch_active);
+}
+
+void
+gs_sources_dialog_row_set_name (GsSourcesDialogRow *row, const gchar *name)
 {
        gtk_label_set_text (GTK_LABEL (row->name_label), name);
+       gtk_widget_set_visible (row->name_label, name != NULL);
+}
+
+void
+gs_sources_dialog_row_set_comment (GsSourcesDialogRow *row, const gchar *comment)
+{
+       gtk_label_set_markup (GTK_LABEL (row->comment_label), comment);
+       gtk_widget_set_visible (row->comment_label, comment != NULL);
+
+       /* make the name bold */
+       if (comment != NULL) {
+               PangoAttrList *attr_list = pango_attr_list_new ();
+               pango_attr_list_insert (attr_list,
+                                       pango_attr_weight_new (PANGO_WEIGHT_BOLD));
+               gtk_label_set_attributes (GTK_LABEL (row->name_label), attr_list);
+               pango_attr_list_unref (attr_list);
+       }
 }
 
 void
-gs_sources_dialog_row_set_description (GsSourcesDialogRow *row,
-                                       const gchar        *description)
+gs_sources_dialog_row_set_description (GsSourcesDialogRow *row, const gchar *description)
+{
+       gtk_label_set_markup (GTK_LABEL (row->description_label), description);
+       gtk_widget_set_visible (row->description_label, description != NULL);
+}
+
+static void
+gs_sources_dialog_switch_active_cb (GtkSwitch *active_switch,
+                                   GParamSpec *pspec,
+                                   GsSourcesDialogRow *row)
+{
+       g_object_notify (G_OBJECT (row), "switch-active");
+}
+
+gboolean
+gs_sources_dialog_row_get_switch_active (GsSourcesDialogRow *row)
 {
-       gtk_label_set_text (GTK_LABEL (row->description_label), description);
+       return gtk_switch_get_active (GTK_SWITCH (row->active_switch));
+}
+
+static void
+gs_sources_dialog_row_get_property (GObject *object,
+                                   guint prop_id,
+                                   GValue *value,
+                                   GParamSpec *pspec)
+{
+       GsSourcesDialogRow *row = GS_SOURCES_DIALOG_ROW (object);
+       switch (prop_id) {
+       case PROP_SWITCH_ACTIVE:
+               g_value_set_boolean (value,
+                                    gs_sources_dialog_row_get_switch_active (row));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
 }
 
 static void
 gs_sources_dialog_row_init (GsSourcesDialogRow *row)
 {
        gtk_widget_init_template (GTK_WIDGET (row));
+       g_signal_connect (row->active_switch, "notify::active",
+                         G_CALLBACK (gs_sources_dialog_switch_active_cb), row);
 }
 
 static void
 gs_sources_dialog_row_class_init (GsSourcesDialogRowClass *klass)
 {
+       GParamSpec *pspec;
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->get_property = gs_sources_dialog_row_get_property;
+
+       pspec = g_param_spec_string ("switch-active", NULL, NULL, FALSE,
+                                    G_PARAM_READABLE);
+       g_object_class_install_property (object_class, PROP_SWITCH_ACTIVE, pspec);
 
        gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/Software/gs-sources-dialog-row.ui");
 
+       gtk_widget_class_bind_template_child (widget_class, GsSourcesDialogRow, active_switch);
        gtk_widget_class_bind_template_child (widget_class, GsSourcesDialogRow, name_label);
+       gtk_widget_class_bind_template_child (widget_class, GsSourcesDialogRow, comment_label);
        gtk_widget_class_bind_template_child (widget_class, GsSourcesDialogRow, description_label);
 }
 
diff --git a/src/gs-sources-dialog-row.h b/src/gs-sources-dialog-row.h
index 0d0f00c..9ee7eb1 100644
--- a/src/gs-sources-dialog-row.h
+++ b/src/gs-sources-dialog-row.h
@@ -31,8 +31,15 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (GsSourcesDialogRow, gs_sources_dialog_row, GS, SOURCES_DIALOG_ROW, GtkListBoxRow)
 
 GtkWidget      *gs_sources_dialog_row_new              (void);
+void            gs_sources_dialog_row_set_switch_enabled (GsSourcesDialogRow   *row,
+                                                        gboolean                switch_enabled);
+void            gs_sources_dialog_row_set_switch_active (GsSourcesDialogRow    *row,
+                                                        gboolean                switch_active);
+gboolean        gs_sources_dialog_row_get_switch_active (GsSourcesDialogRow    *row);
 void            gs_sources_dialog_row_set_name         (GsSourcesDialogRow     *row,
                                                         const gchar            *name);
+void            gs_sources_dialog_row_set_comment      (GsSourcesDialogRow     *row,
+                                                        const gchar            *comment);
 void            gs_sources_dialog_row_set_description  (GsSourcesDialogRow     *row,
                                                         const gchar            *description);
 
diff --git a/src/gs-sources-dialog-row.ui b/src/gs-sources-dialog-row.ui
index fc30790..67ba900 100644
--- a/src/gs-sources-dialog-row.ui
+++ b/src/gs-sources-dialog-row.ui
@@ -3,33 +3,70 @@
   <!-- interface-requires gtk+ 3.10 -->
   <template class="GsSourcesDialogRow" parent="GtkListBoxRow">
     <child>
-      <object class="GtkBox" id="vbox">
+      <object class="GtkBox">
         <property name="visible">True</property>
         <property name="margin-top">12</property>
         <property name="margin-bottom">12</property>
         <property name="margin-start">12</property>
         <property name="margin-end">12</property>
-        <property name="orientation">vertical</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">horizontal</property>
+        <property name="spacing">16</property>
         <child>
-          <object class="GtkLabel" id="name_label">
+          <object class="GtkBox" id="vbox">
             <property name="visible">True</property>
-            <property name="halign">start</property>
-            <property name="ellipsize">end</property>
+            <property name="orientation">vertical</property>
+            <property name="spacing">4</property>
+            <child>
+              <object class="GtkLabel" id="name_label">
+                <property name="visible">True</property>
+                <property name="halign">start</property>
+                <property name="ellipsize">end</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="comment_label">
+                <property name="visible">False</property>
+                <property name="halign">start</property>
+                <property name="xalign">0</property>
+                <property name="wrap">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="description_label">
+                <property name="visible">True</property>
+                <property name="halign">start</property>
+                <property name="xalign">0</property>
+                <property name="wrap">True</property>
+                <style>
+                  <class name="dim-label"/>
+                </style>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+              </packing>
+            </child>
           </object>
           <packing>
-            <property name="expand">False</property>
-            <property name="fill">False</property>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
           </packing>
         </child>
         <child>
-          <object class="GtkLabel" id="description_label">
-            <property name="visible">True</property>
-            <property name="halign">start</property>
-            <property name="xalign">0</property>
-            <property name="wrap">True</property>
-            <style>
-              <class name="dim-label"/>
-            </style>
+          <object class="GtkSwitch" id="active_switch">
+            <property name="visible">False</property>
+            <property name="can_focus">True</property>
+            <property name="halign">end</property>
+            <property name="valign">start</property>
           </object>
           <packing>
             <property name="expand">False</property>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]