[gnome-calendar] time-selector: make it GtkMenuButton descendant



commit fa014972bcb635292e41f23da4333fdb9bad4c53
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sat Jan 3 15:11:20 2015 -0200

    time-selector: make it GtkMenuButton descendant
    
    With it, it is up to Gtk+ to handle the popover.

 data/ui/time-selector.ui |  131 ++++++++++++++++++++++++++-------------------
 src/gcal-time-selector.c |   65 +++++------------------
 src/gcal-time-selector.h |    4 +-
 3 files changed, 91 insertions(+), 109 deletions(-)
---
diff --git a/data/ui/time-selector.ui b/data/ui/time-selector.ui
index a1bc70c..cda16db 100644
--- a/data/ui/time-selector.ui
+++ b/data/ui/time-selector.ui
@@ -1,74 +1,95 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
+  <template class="GcalTimeSelector" parent="GtkMenuButton">
+    <property name="use_popover">True</property>
+    <property name="popover">time_selector_popover</property>
+    <child>
+      <object class="GtkLabel" id="time_label">
+        <property name="visible">True</property>
+        <property name="label" translatable="yes">00:00</property>
+      </object>
+    </child>
+  </template>
   <object class="GtkAdjustment" id="hour_adjustment">
     <property name="upper">23</property>
     <property name="step_increment">1</property>
     <property name="page_increment">5</property>
+    <signal name="value-changed" handler="time_changed" object="GcalTimeSelector" swapped="no"/>
   </object>
   <object class="GtkAdjustment" id="minute_adjustment">
     <property name="upper">59</property>
     <property name="step_increment">1</property>
     <property name="page_increment">10</property>
+    <signal name="value-changed" handler="time_changed" object="GcalTimeSelector" swapped="no"/>
   </object>
-  <object class="GtkGrid" id="grid">
-    <property name="visible">True</property>
-    <property name="can_focus">False</property>
-    <property name="border_width">12</property>
-    <property name="column_spacing">6</property>
-    <child>
-      <object class="GtkSpinButton" id="hour_spin">
-        <property name="visible">True</property>
-        <property name="can_focus">True</property>
-        <property name="xalign">0.5</property>
-        <property name="input_purpose">number</property>
-        <property name="orientation">vertical</property>
-        <property name="adjustment">hour_adjustment</property>
-      </object>
-      <packing>
-        <property name="left_attach">0</property>
-        <property name="top_attach">0</property>
-      </packing>
-    </child>
+  <object class="GtkPopover" id="time_selector_popover">
+    <property name="visible">False</property>
+    <property name="position">bottom</property>
     <child>
-      <object class="GtkSpinButton" id="minute_spin">
+      <object class="GtkGrid" id="grid">
         <property name="visible">True</property>
-        <property name="can_focus">True</property>
-        <property name="xalign">0.5</property>
-        <property name="input_purpose">number</property>
-        <property name="orientation">vertical</property>
-        <property name="adjustment">minute_adjustment</property>
-      </object>
-      <packing>
-        <property name="left_attach">2</property>
-        <property name="top_attach">0</property>
-      </packing>
-    </child>
-    <child>
-      <object class="GtkLabel" id="label1">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="label" translatable="yes">:</property>
-      </object>
-      <packing>
-        <property name="left_attach">1</property>
-        <property name="top_attach">0</property>
-      </packing>
-    </child>
-    <child>
-      <object class="GtkComboBoxText" id="period_combo">
-        <property name="visible">False</property>
         <property name="can_focus">False</property>
-        <property name="valign">center</property>
-        <property name="active">0</property>
-        <items>
-          <item translatable="yes">AM</item>
-          <item translatable="yes">PM</item>
-        </items>
+        <property name="border_width">12</property>
+        <property name="column_spacing">6</property>
+        <child>
+          <object class="GtkSpinButton" id="hour_spin">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="xalign">0.5</property>
+            <property name="input_purpose">number</property>
+            <property name="orientation">vertical</property>
+            <property name="adjustment">hour_adjustment</property>
+            <signal name="output" handler="on_output" object="GcalTimeSelector" swapped="no"/>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkSpinButton" id="minute_spin">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="xalign">0.5</property>
+            <property name="input_purpose">number</property>
+            <property name="orientation">vertical</property>
+            <property name="adjustment">minute_adjustment</property>
+            <signal name="output" handler="on_output" object="GcalTimeSelector" swapped="no"/>
+          </object>
+          <packing>
+            <property name="left_attach">2</property>
+            <property name="top_attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkLabel" id="label1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="label" translatable="yes">:</property>
+          </object>
+          <packing>
+            <property name="left_attach">1</property>
+            <property name="top_attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkComboBoxText" id="period_combo">
+            <property name="visible">False</property>
+            <property name="can_focus">False</property>
+            <property name="valign">center</property>
+            <property name="active">0</property>
+            <signal name="changed" handler="period_changed" object="GcalTimeSelector" swapped="no"/>
+            <items>
+              <item translatable="yes">AM</item>
+              <item translatable="yes">PM</item>
+            </items>
+          </object>
+          <packing>
+            <property name="left_attach">3</property>
+            <property name="top_attach">0</property>
+          </packing>
+        </child>
       </object>
-      <packing>
-        <property name="left_attach">3</property>
-        <property name="top_attach">0</property>
-      </packing>
     </child>
   </object>
 </interface>
diff --git a/src/gcal-time-selector.c b/src/gcal-time-selector.c
index 7abea5e..cf96a30 100644
--- a/src/gcal-time-selector.c
+++ b/src/gcal-time-selector.c
@@ -25,10 +25,8 @@
 struct _GcalTimeSelectorPrivate
 {
   GtkWidget *time_label;
-  GtkWidget *popover;
   GtkWidget *hour_spin;
   GtkWidget *minute_spin;
-  GtkWidget *period_check;
   GtkWidget *period_combo;
 
   gboolean   format_24h;
@@ -61,7 +59,7 @@ static void     time_changed                                   (GtkAdjustment
 
 static void     gcal_time_selector_constructed                 (GObject              *object);
 
-G_DEFINE_TYPE_WITH_PRIVATE (GcalTimeSelector, gcal_time_selector, GTK_TYPE_TOGGLE_BUTTON);
+G_DEFINE_TYPE_WITH_PRIVATE (GcalTimeSelector, gcal_time_selector, GTK_TYPE_MENU_BUTTON);
 
 static void
 format_date_label (GcalTimeSelector *selector)
@@ -156,21 +154,28 @@ gcal_time_selector_class_init (GcalTimeSelectorClass *klass)
                                     G_TYPE_NONE,
                                     0);
 
+  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), 
"/org/gnome/calendar/time-selector.ui");
+
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GcalTimeSelector, time_label);
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GcalTimeSelector, hour_spin);
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GcalTimeSelector, minute_spin);
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GcalTimeSelector, period_combo);
+
+  gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (klass), on_output);
+  gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (klass), period_changed);
+  gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (klass), time_changed);
 }
 
 static void
 gcal_time_selector_init (GcalTimeSelector *self)
 {
-  ;
+  gtk_widget_init_template (GTK_WIDGET (self));
 }
 
 static void
 gcal_time_selector_constructed (GObject *object)
 {
   GcalTimeSelectorPrivate *priv;
-  GtkWidget *grid;
-  GtkBuilder *builder;
-  GtkAdjustment *adj;
 
   GSettings *settings;
   gchar *clock_format;
@@ -188,55 +193,11 @@ gcal_time_selector_constructed (GObject *object)
   g_free (clock_format);
   g_object_unref (settings);
 
-  /* time label */
-  priv->time_label = gtk_label_new (NULL);
-  gtk_label_set_label (GTK_LABEL (priv->time_label), "00:00");
-  gtk_widget_show (priv->time_label);
-
-  gtk_container_add (GTK_CONTAINER (object), priv->time_label);
-
-  /* popover */
-  builder = gtk_builder_new ();
-  gtk_builder_add_from_resource (builder, "/org/gnome/calendar/time-selector.ui", NULL);
-
-  priv->popover = gtk_popover_new (GTK_WIDGET (object));
-  gtk_popover_set_position (GTK_POPOVER (priv->popover), GTK_POS_BOTTOM);
-
-  grid = (GtkWidget*) gtk_builder_get_object (builder, "grid");
-  g_object_ref (grid);
-
-  priv->hour_spin = (GtkWidget*) gtk_builder_get_object (builder, "hour_spin");
-  g_object_ref (priv->hour_spin);
-
-  priv->minute_spin = (GtkWidget*) gtk_builder_get_object (builder, "minute_spin");
-  g_object_ref (priv->minute_spin);
-
-  priv->period_combo = (GtkWidget*) gtk_builder_get_object (builder, "period_combo");
-  gtk_widget_set_visible (priv->period_combo, !priv->format_24h);
-  g_object_ref (priv->period_combo);
-
-  g_object_unref (builder);
-
-  gtk_container_add (GTK_CONTAINER (priv->popover), grid);
-  g_object_bind_property (priv->popover, "visible", object, "active", G_BINDING_BIDIRECTIONAL);
-
   /* maximum of 11 for 12h format */
   if (! priv->format_24h)
     {
-      adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->hour_spin));
-      gtk_adjustment_set_upper (adj, 11.0);
+      gtk_adjustment_set_upper (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->hour_spin)), 11.0);
     }
-
-  /* signals */
-  adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->hour_spin));
-  g_signal_connect (adj, "value-changed", G_CALLBACK (time_changed), object);
-
-  adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->minute_spin));
-  g_signal_connect (adj, "value-changed", G_CALLBACK (time_changed), object);
-
-  g_signal_connect (priv->period_combo, "changed", G_CALLBACK (period_changed), object);
-  g_signal_connect (priv->hour_spin, "output", G_CALLBACK (on_output), object);
-  g_signal_connect (priv->minute_spin, "output", G_CALLBACK (on_output), object);
 }
 
 /* Public API */
diff --git a/src/gcal-time-selector.h b/src/gcal-time-selector.h
index 1aeb453..262fd14 100644
--- a/src/gcal-time-selector.h
+++ b/src/gcal-time-selector.h
@@ -37,7 +37,7 @@ typedef struct _GcalTimeSelectorPrivate         GcalTimeSelectorPrivate;
 
 struct _GcalTimeSelector
 {
-  GtkToggleButton parent;
+  GtkMenuButton parent;
   /* add your public declarations here */
 
   GcalTimeSelectorPrivate *priv;
@@ -45,7 +45,7 @@ struct _GcalTimeSelector
 
 struct _GcalTimeSelectorClass
 {
-  GtkToggleButtonClass parent_class;
+  GtkMenuButtonClass parent_class;
 
   /* signals */
   void (*modified)  (GcalTimeSelector *selector);


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