]> git.sur5r.net Git - glabels/blobdiff - glabels2/src/color-combo.c
2009-09-06 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / color-combo.c
index f296f51ea038a386af9ba02ea18db7bab9257b5d..6b9e5761260c575a27f367e037fc4a11a3b8a454 100644 (file)
@@ -24,8 +24,7 @@
 
 #include "color-combo-menu.h"
 #include <glib/gi18n.h>
-#include <gtk/gtkbutton.h>
-#include <gtk/gtkvbox.h>
+#include <gtk/gtkhbox.h>
 #include "color-swatch.h"
 #include <gtk/gtkarrow.h>
 #include "marshal.h"
@@ -35,8 +34,6 @@
 #define IMAGE_W 24
 #define IMAGE_H 24
 
-#define SWATCH_H 5
-
 
 /*========================================================*/
 /* Private types.                                         */
@@ -50,10 +47,7 @@ struct _glColorComboPrivate {
 
         guint       default_color;
 
-        GtkWidget  *button;
-        GtkWidget  *button_vbox;
         GtkWidget  *swatch;
-        GtkWidget  *dropdown_button;
 
         GtkWidget  *menu;
 };
@@ -77,30 +71,31 @@ static guint signals[LAST_SIGNAL] = {0};
 
 static void gl_color_combo_finalize      (GObject             *object);
 
-static void
-button_clicked_cb (glColorCombo *this);
+static gboolean
+button_press_event_cb (GtkWidget      *widget,
+                       GdkEventButton *event,
+                       glColorCombo   *this);
 
 static GdkPixbuf *
 create_pixbuf (glColorCombo   *this,
                gdouble         w,
                gdouble         h);
 
-static gboolean
-dropdown_button_press_event_cb (GtkWidget      *widget,
-                                GdkEventButton *event,
-                                glColorCombo   *this);
-
 static void
 menu_color_changed_cb (glColorComboMenu   *object,
                        guint               color,
                        gboolean            is_default,
                        glColorCombo       *this);
 
+static void
+menu_selection_done_cb (GtkMenuShell      *object,
+                        glColorCombo      *this);
+
 
 /*****************************************************************************/
 /* Object infrastructure.                                                    */
 /*****************************************************************************/
-G_DEFINE_TYPE (glColorCombo, gl_color_combo, GTK_TYPE_HBOX);
+G_DEFINE_TYPE (glColorCombo, gl_color_combo, GTK_TYPE_TOGGLE_BUTTON);
 
 
 /*****************************************************************************/
@@ -136,31 +131,22 @@ gl_color_combo_class_init (glColorComboClass *class)
 static void
 gl_color_combo_init (glColorCombo *this)
 {
+        GtkWidget *hbox;
         GtkWidget *arrow;
 
-        gtk_box_set_spacing (GTK_BOX (this), 0);
-
         this->priv = g_new0 (glColorComboPrivate, 1);
 
-        this->priv->button_vbox = gtk_vbox_new (FALSE, 0);
-
-        this->priv->button = gtk_button_new ();
-        gtk_container_add (GTK_CONTAINER (this->priv->button), this->priv->button_vbox);
-        gtk_button_set_focus_on_click (GTK_BUTTON (this->priv->button), FALSE);
-        g_signal_connect_swapped (this->priv->button, "clicked",
-                          G_CALLBACK(button_clicked_cb), this);
+        hbox = gtk_hbox_new (FALSE, 3);
+        gtk_container_add (GTK_CONTAINER (this), hbox);
+        
+        this->priv->swatch = gl_color_swatch_new (IMAGE_W, IMAGE_H, GL_COLOR_NONE);
+        gtk_box_pack_start (GTK_BOX (hbox), this->priv->swatch, FALSE, FALSE, 0);
 
-        gtk_box_pack_start (GTK_BOX (this), this->priv->button, FALSE, FALSE, 0);
-
-        this->priv->dropdown_button = gtk_button_new ();
         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
-        gtk_container_add (GTK_CONTAINER (this->priv->dropdown_button), arrow);
-        gtk_button_set_relief (GTK_BUTTON (this->priv->dropdown_button), GTK_RELIEF_NONE);
-        gtk_button_set_focus_on_click (GTK_BUTTON (this->priv->dropdown_button), FALSE);
-        g_signal_connect (this->priv->dropdown_button, "button_press_event",
-                          G_CALLBACK(dropdown_button_press_event_cb), this);
+        gtk_box_pack_end (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
 
-        gtk_box_pack_start (GTK_BOX (this), this->priv->dropdown_button, FALSE, FALSE, 0);
+        g_signal_connect (this, "button_press_event",
+                          G_CALLBACK(button_press_event_cb), this);
 }
 
 
@@ -175,6 +161,7 @@ gl_color_combo_finalize (GObject *object)
         g_return_if_fail (object && IS_GL_COLOR_COMBO (object));
         this = GL_COLOR_COMBO (object);
 
+        g_object_ref_sink (this->priv->menu);
         g_free (this->priv);
 
         G_OBJECT_CLASS (gl_color_combo_parent_class)->finalize (object);
@@ -185,8 +172,7 @@ gl_color_combo_finalize (GObject *object)
 /** New Object Generator.                                                    */
 /*****************************************************************************/
 GtkWidget *
-gl_color_combo_new (GdkPixbuf    *icon,
-                    const gchar  *default_label,
+gl_color_combo_new (const gchar  *default_label,
                     guint         default_color,
                     guint         color)
 {
@@ -204,20 +190,7 @@ gl_color_combo_new (GdkPixbuf    *icon,
         this->priv->default_color = default_color;
         this->priv->color = color;
 
-        if (icon)
-        {
-                pixbuf = gdk_pixbuf_new_subpixbuf (icon, 0, 0, IMAGE_W, IMAGE_H-SWATCH_H);
-                wimage = gtk_image_new_from_pixbuf (pixbuf);
-                g_object_unref (G_OBJECT (pixbuf));
-                gtk_box_pack_start (GTK_BOX (this->priv->button_vbox), wimage, FALSE, FALSE, 0);
-
-                this->priv->swatch = gl_color_swatch_new (IMAGE_W, SWATCH_H, color);
-        }
-        else
-        {
-                this->priv->swatch = gl_color_swatch_new (IMAGE_W, IMAGE_H, color);
-        }
-        gtk_box_pack_start (GTK_BOX (this->priv->button_vbox), this->priv->swatch, FALSE, FALSE, 0);
+        gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
 
         this->priv->menu = gl_color_combo_menu_new (default_label,
                                                     color);
@@ -225,6 +198,8 @@ gl_color_combo_new (GdkPixbuf    *icon,
 
         g_signal_connect (this->priv->menu, "color_changed",
                           G_CALLBACK (menu_color_changed_cb), this);
+        g_signal_connect (this->priv->menu, "selection_done",
+                          G_CALLBACK (menu_selection_done_cb), this);
 
         return GTK_WIDGET (this);
 }
@@ -268,38 +243,15 @@ gl_color_combo_get_color (glColorCombo  *this,
 }
 
 
-/*****************************************************************************/
-/** Set relief style.                                                        */
-/*****************************************************************************/
-void
-gl_color_combo_set_relief( glColorCombo  *this,
-                           GtkReliefStyle relief )
-{
-        gtk_button_set_relief (GTK_BUTTON (this->priv->button), relief);
-}
-
-
-/*****************************************************************************/
-/* Color button "clicked" callback.                                          */
-/*****************************************************************************/
-static void
-button_clicked_cb( glColorCombo *this )
-{
-        g_signal_emit (this, signals[COLOR_CHANGED], 0,
-                       this->priv->color,
-                       this->priv->is_default_flag);
-}
-
-
 /*****************************************************************************/
 /* Menu positioning function.                                                */
 /*****************************************************************************/
 static void
-menu_position_function (GtkMenu *menu,
-                        gint *x,
-                        gint *y,
+menu_position_function (GtkMenu  *menu,
+                        gint     *x,
+                        gint     *y,
                         gboolean *push_in,
-                        gpointer user_data)
+                        gpointer  user_data)
 {
         glColorCombo *this = GL_COLOR_COMBO (user_data);
         gint          x1, y1;
@@ -332,17 +284,19 @@ menu_position_function (GtkMenu *menu,
 
 
 /*****************************************************************************/
-/* Dropdown button "clicked" callback.                                       */
+/* Button "button_press_event" callback.                                     */
 /*****************************************************************************/
 static gboolean
-dropdown_button_press_event_cb (GtkWidget      *widget,
-                                GdkEventButton *event,
-                                glColorCombo   *this)
+button_press_event_cb (GtkWidget      *widget,
+                       GdkEventButton *event,
+                       glColorCombo   *this)
 {
         switch (event->button)
         {
 
         case 1:
+                gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), TRUE);
+
                 gtk_menu_popup (GTK_MENU (this->priv->menu),
                                 NULL, NULL,
                                 menu_position_function, this,
@@ -359,7 +313,7 @@ dropdown_button_press_event_cb (GtkWidget      *widget,
 
 
 /*****************************************************************************/
-/* Menu "color changed" callback.                                          */
+/* Menu "color changed" callback.                                            */
 /*****************************************************************************/
 static void
 menu_color_changed_cb (glColorComboMenu     *object,
@@ -377,7 +331,8 @@ menu_color_changed_cb (glColorComboMenu     *object,
         }
         this->priv->is_default_flag = is_default;
 
-        gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch), color);
+        gl_color_swatch_set_color (GL_COLOR_SWATCH (this->priv->swatch),
+                                   this->priv->color);
 
         g_signal_emit (this, signals[COLOR_CHANGED], 0,
                        this->priv->color,
@@ -385,6 +340,17 @@ menu_color_changed_cb (glColorComboMenu     *object,
 }
 
 
+/*****************************************************************************/
+/* Menu "color changed" callback.                                            */
+/*****************************************************************************/
+static void
+menu_selection_done_cb (GtkMenuShell         *object,
+                        glColorCombo         *this)
+{
+        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (this), FALSE);
+}
+
+
 
 /*
  * Local Variables:       -- emacs