[gtk+/composite-templates-new: 13/18] GtkScaleButton: Define children with a GtkBuilder template



commit f0fa81bdcdcc6f7bd8c7c229ab179796014a1e0d
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Sat Mar 23 20:17:15 2013 +0900

    GtkScaleButton: Define children with a GtkBuilder template

 gtk/Makefile.am       |    3 +-
 gtk/gtk.gresource.xml |    1 +
 gtk/gtkscalebutton.c  |  149 +++++++++++++++---------------------------------
 gtk/gtkscalebutton.ui |  100 +++++++++++++++++++++++++++++++++
 gtk/tests/templates.c |   12 ++++
 po/POTFILES.in        |    1 +
 6 files changed, 163 insertions(+), 103 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 166d000..4639210 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -1099,7 +1099,8 @@ COMPOSITE_TEMPLATES =                     \
        gtkdialog.ui                    \
        gtkinfobar.ui                   \
        gtklockbutton.ui                \
-       gtkmessagedialog.ui
+       gtkmessagedialog.ui             \
+       gtkscalebutton.ui
 
 #
 # rules to generate built sources
diff --git a/gtk/gtk.gresource.xml b/gtk/gtk.gresource.xml
index f4452be..ae46681 100644
--- a/gtk/gtk.gresource.xml
+++ b/gtk/gtk.gresource.xml
@@ -17,5 +17,6 @@
     <file>gtkinfobar.ui</file>
     <file>gtklockbutton.ui</file>
     <file>gtkmessagedialog.ui</file>
+    <file>gtkscalebutton.ui</file>
   </gresource>
 </gresources>
diff --git a/gtk/gtkscalebutton.c b/gtk/gtkscalebutton.c
index f10bb03..48311c5 100644
--- a/gtk/gtkscalebutton.c
+++ b/gtk/gtkscalebutton.c
@@ -167,20 +167,33 @@ static void cb_scale_grab_notify          (GtkWidget           *widget,
 static void gtk_scale_button_update_icon       (GtkScaleButton      *button);
 static void gtk_scale_button_scale_value_changed(GtkRange            *range);
 
-/* see below for scale definitions */
-static GtkWidget *gtk_scale_button_scale_new    (GtkScaleButton      *button);
-
 G_DEFINE_TYPE_WITH_CODE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON,
                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
                                                 NULL))
 
 static guint signals[LAST_SIGNAL] = { 0, };
 
+/*
+ * ScaleButtonScale forward declarations
+ */
+#define GTK_TYPE_SCALE_BUTTON_SCALE    (_gtk_scale_button_scale_get_type ())
+#define GTK_SCALE_BUTTON_SCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, 
GtkScaleButtonScale))
+#define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
+GType _gtk_scale_button_scale_get_type (void);
+
+typedef struct _GtkScaleButtonScale
+{
+  GtkScale parent_instance;
+  GtkScaleButton *button;
+} GtkScaleButtonScale;
+
+
 static void
 gtk_scale_button_class_init (GtkScaleButtonClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
   GtkBindingSet *binding_set;
 
   g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
@@ -345,6 +358,27 @@ gtk_scale_button_class_init (GtkScaleButtonClass *klass)
   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
                                "popdown", 0);
 
+  /* Bind class to template
+   */
+  gtk_container_class_set_template_from_resource (container_class,
+                                                 "/org/gtk/libgtk/gtkscalebutton.ui");
+
+  gtk_container_class_bind_child (container_class, GtkScaleButtonPrivate, plus_button);
+  gtk_container_class_bind_child (container_class, GtkScaleButtonPrivate, minus_button);
+  gtk_container_class_bind_child (container_class, GtkScaleButtonPrivate, dock);
+  gtk_container_class_bind_child (container_class, GtkScaleButtonPrivate, box);
+  gtk_container_class_bind_child (container_class, GtkScaleButtonPrivate, scale);
+  gtk_container_class_bind_child (container_class, GtkScaleButtonPrivate, image);
+  gtk_container_class_bind_child (container_class, GtkScaleButtonPrivate, adjustment);
+
+  gtk_container_class_bind_callback (container_class, cb_dock_button_press);
+  gtk_container_class_bind_callback (container_class, cb_dock_key_release);
+  gtk_container_class_bind_callback (container_class, cb_dock_grab_notify);
+  gtk_container_class_bind_callback (container_class, cb_dock_grab_broken_event);
+  gtk_container_class_bind_callback (container_class, cb_button_press);
+  gtk_container_class_bind_callback (container_class, cb_button_release);
+  gtk_container_class_bind_callback (container_class, cb_scale_grab_notify);
+
   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCALE_BUTTON_ACCESSIBLE);
 }
 
@@ -352,7 +386,6 @@ static void
 gtk_scale_button_init (GtkScaleButton *button)
 {
   GtkScaleButtonPrivate *priv;
-  GtkWidget *frame;
 
   button->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
                                                      GTK_TYPE_SCALE_BUTTON,
@@ -363,60 +396,16 @@ gtk_scale_button_init (GtkScaleButton *button)
   priv->click_timeout = CLICK_TIMEOUT;
   priv->orientation = GTK_ORIENTATION_VERTICAL;
 
-  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
-  gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
-
-  /* image */
-  priv->image = gtk_image_new ();
-  gtk_container_add (GTK_CONTAINER (button), priv->image);
-  gtk_widget_show_all (priv->image);
-
-  /* window */
-  priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
-  gtk_widget_set_name (priv->dock, "gtk-scalebutton-popup-window");
-  g_signal_connect (priv->dock, "button-press-event",
-                   G_CALLBACK (cb_dock_button_press), button);
-  g_signal_connect (priv->dock, "key-release-event",
-                   G_CALLBACK (cb_dock_key_release), button);
-  g_signal_connect (priv->dock, "grab-notify",
-                   G_CALLBACK (cb_dock_grab_notify), button);
-  g_signal_connect (priv->dock, "grab-broken-event",
-                   G_CALLBACK (cb_dock_grab_broken_event), button);
-  gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);
-
-  /* frame */
-  frame = gtk_frame_new (NULL);
-  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
-  gtk_container_add (GTK_CONTAINER (priv->dock), frame);
-
-  /* box for scale and +/- buttons */
-  priv->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
-  gtk_container_add (GTK_CONTAINER (frame), priv->box);
-
-  /* + */
-  priv->plus_button = gtk_button_new_with_label ("+");
-  gtk_button_set_relief (GTK_BUTTON (priv->plus_button), GTK_RELIEF_NONE);
-  g_signal_connect (priv->plus_button, "button-press-event",
-                   G_CALLBACK (cb_button_press), button);
-  g_signal_connect (priv->plus_button, "button-release-event",
-                   G_CALLBACK (cb_button_release), button);
-  gtk_box_pack_start (GTK_BOX (priv->box), priv->plus_button, FALSE, FALSE, 0);
-
-  /* - */
-  priv->minus_button = gtk_button_new_with_label ("-");
-  gtk_button_set_relief (GTK_BUTTON (priv->minus_button), GTK_RELIEF_NONE);
-  g_signal_connect (priv->minus_button, "button-press-event",
-                  G_CALLBACK (cb_button_press), button);
-  g_signal_connect (priv->minus_button, "button-release-event",
-                   G_CALLBACK (cb_button_release), button);
-  gtk_box_pack_end (GTK_BOX (priv->box), priv->minus_button, FALSE, FALSE, 0);
-
-  priv->adjustment = gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0);
-  g_object_ref_sink (priv->adjustment);
-
-  /* the scale */
-  priv->scale = gtk_scale_button_scale_new (button);
-  gtk_container_add (GTK_CONTAINER (priv->box), priv->scale);
+  g_type_ensure (GTK_TYPE_SCALE_BUTTON_SCALE);
+  gtk_container_init_template (GTK_CONTAINER (button));
+
+  /* Assign GtkScaleButtonScale pointer back to 'button',
+   * since there's no property for that we can't do it in the template
+   */
+  GTK_SCALE_BUTTON_SCALE (priv->scale)->button = button;
+
+  /* Need a local reference to the adjustment */
+  g_object_ref (priv->adjustment);
 
   gtk_widget_add_events (GTK_WIDGET (button), GDK_SCROLL_MASK);
 }
@@ -1402,17 +1391,6 @@ cb_scale_grab_notify (GtkWidget *widget,
 /*
  * Scale stuff.
  */
-
-#define GTK_TYPE_SCALE_BUTTON_SCALE    (_gtk_scale_button_scale_get_type ())
-#define GTK_SCALE_BUTTON_SCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, 
GtkScaleButtonScale))
-#define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
-
-typedef struct _GtkScaleButtonScale
-{
-  GtkScale parent_instance;
-  GtkScaleButton *button;
-} GtkScaleButtonScale;
-
 typedef struct _GtkScaleButtonScaleClass
 {
   GtkScaleClass parent_class;
@@ -1423,8 +1401,6 @@ static gboolean   gtk_scale_button_scale_press   (GtkWidget      *widget,
 static gboolean gtk_scale_button_scale_release (GtkWidget      *widget,
                                                 GdkEventButton *event);
 
-GType           _gtk_scale_button_scale_get_type (void);
-
 G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
 
 static void
@@ -1443,37 +1419,6 @@ _gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
 {
 }
 
-static GtkWidget *
-gtk_scale_button_scale_new (GtkScaleButton *button)
-{
-  GtkScaleButtonPrivate *priv = button->priv;
-  GtkScaleButtonScale *scale;
-
-  scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
-                        "orientation", priv->orientation,
-                        "adjustment",  priv->adjustment,
-                        "draw-value",  FALSE,
-                        NULL);
-
-  scale->button = button;
-
-  g_signal_connect (scale, "grab-notify",
-                    G_CALLBACK (cb_scale_grab_notify), button);
-
-  if (priv->orientation == GTK_ORIENTATION_VERTICAL)
-    {
-      gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
-      gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
-    }
-  else
-    {
-      gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
-      gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
-    }
-
-  return GTK_WIDGET (scale);
-}
-
 static gboolean
 gtk_scale_button_scale_press (GtkWidget      *widget,
                              GdkEventButton *event)
diff --git a/gtk/gtkscalebutton.ui b/gtk/gtkscalebutton.ui
new file mode 100644
index 0000000..5dd3763
--- /dev/null
+++ b/gtk/gtkscalebutton.ui
@@ -0,0 +1,100 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <!-- interface-requires gtk+ 3.10 -->
+  <object class="GtkAdjustment" id="adjustment">
+    <property name="upper">100</property>
+    <property name="step_increment">2</property>
+    <property name="page_increment">20</property>
+  </object>
+  <template class="GtkScaleButton" parent="GtkButton">
+    <property name="can_focus">True</property>
+    <property name="receives_default">True</property>
+    <property name="relief">none</property>
+    <property name="focus_on_click">False</property>
+    <child>
+      <object class="GtkImage" id="image">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="stock">gtk-missing-image</property>
+      </object>
+    </child>
+  </template>
+  <object class="GtkWindow" id="dock">
+    <property name="name">gtk-scalebutton-popup-window</property>
+    <property name="can_focus">False</property>
+    <property name="type">popup</property>
+    <property name="decorated">False</property>
+    <signal name="button-press-event" handler="cb_dock_button_press" swapped="no"/>
+    <signal name="key-release-event" handler="cb_dock_key_release" swapped="no"/>
+    <signal name="grab-notify" handler="cb_dock_grab_notify" swapped="no"/>
+    <signal name="grab-broken-event" handler="cb_dock_grab_broken_event" swapped="no"/>
+    <child>
+      <object class="GtkFrame" id="frame1">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="label_xalign">0</property>
+        <property name="shadow_type">out</property>
+        <child>
+          <object class="GtkBox" id="box">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="orientation">vertical</property>
+            <child>
+              <object class="GtkButton" id="plus_button">
+                <property name="label">+</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="relief">none</property>
+                <signal name="button-press-event" handler="cb_button_press" swapped="no"/>
+                <signal name="button-release-event" handler="cb_button_release" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkScaleButtonScale" id="scale">
+                <property name="height_request">100</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="orientation">vertical</property>
+                <property name="adjustment">adjustment</property>
+                <property name="inverted">True</property>
+                <property name="round_digits">1</property>
+                <property name="draw_value">False</property>
+                <signal name="grab-notify" handler="cb_scale_grab_notify" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="minus_button">
+                <property name="label">-</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="relief">none</property>
+                <signal name="button-press-event" handler="cb_button_press" swapped="no"/>
+                <signal name="button-release-event" handler="cb_button_release" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">2</property>
+              </packing>
+            </child>
+          </object>
+        </child>
+        <child type="label_item">
+          <placeholder/>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/gtk/tests/templates.c b/gtk/tests/templates.c
index 1a646c9..cf821bb 100644
--- a/gtk/tests/templates.c
+++ b/gtk/tests/templates.c
@@ -98,6 +98,17 @@ test_assistant_basic (void)
   gtk_widget_destroy (widget);
 }
 
+static void
+test_scale_button_basic (void)
+{
+  GtkWidget *widget;
+
+  widget = gtk_scale_button_new (GTK_ICON_SIZE_MENU,
+                                0, 100, 10, NULL);
+  g_assert (GTK_IS_SCALE_BUTTON (widget));
+  gtk_widget_destroy (widget);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -111,6 +122,7 @@ main (int argc, char **argv)
   g_test_add_func ("/Template/GtkInfoBar/Basic", test_info_bar_basic);
   g_test_add_func ("/Template/GtkLockButton/Basic", test_lock_button_basic);
   g_test_add_func ("/Template/GtkAssistant/Basic", test_assistant_basic);
+  g_test_add_func ("/Template/GtkScaleButton/Basic", test_scale_button_basic);
 
   return g_test_run();
 }
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 6732b69..1f9b2f8 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -280,3 +280,4 @@ modules/printbackends/test/gtkprintbackendtest.c
 [type: gettext/glade]gtk/gtkinfobar.ui
 [type: gettext/glade]gtk/gtklockbutton.ui
 [type: gettext/glade]gtk/gtkmessagedialog.ui
+[type: gettext/glade]gtk/gtkscalebutton.ui


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