[Goodies-commits] r5602 - xfce4-power-manager/trunk/src

Ali Abdallah aliov at xfce.org
Mon Oct 13 11:53:03 CEST 2008


Author: aliov
Date: 2008-10-13 09:53:03 +0000 (Mon, 13 Oct 2008)
New Revision: 5602

Added:
   xfce4-power-manager/trunk/src/xfpm-spin-button.c
   xfce4-power-manager/trunk/src/xfpm-spin-button.h
Modified:
   xfce4-power-manager/trunk/src/Makefile.am
   xfce4-power-manager/trunk/src/xfpm-dpms-spins.c
   xfce4-power-manager/trunk/src/xfpm-settings.c
Log:
Implementing a custom spin button with suffix label

Modified: xfce4-power-manager/trunk/src/Makefile.am
===================================================================
--- xfce4-power-manager/trunk/src/Makefile.am	2008-10-13 08:54:05 UTC (rev 5601)
+++ xfce4-power-manager/trunk/src/Makefile.am	2008-10-13 09:53:03 UTC (rev 5602)
@@ -49,6 +49,8 @@
 				xfpm-battery.h			\
 				xfpm-battery-icon.c		\
 				xfpm-battery-icon.h		\
+				xfpm-spin-button.c		\
+				xfpm-spin-button.h		\
 				xfpm-dpms.c			\
 				xfpm-dpms.h			\
 				xfpm-dpms-spins.c		\

Modified: xfce4-power-manager/trunk/src/xfpm-dpms-spins.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-dpms-spins.c	2008-10-13 08:54:05 UTC (rev 5601)
+++ xfce4-power-manager/trunk/src/xfpm-dpms-spins.c	2008-10-13 09:53:03 UTC (rev 5602)
@@ -40,6 +40,7 @@
 #include <glib/gi18n.h>
 
 #include "xfpm-dpms-spins.h"
+#include "xfpm-spin-button.h"
 #include "xfpm-marshal.h"
 
 #ifdef HAVE_DPMS
@@ -116,21 +117,24 @@
     label = gtk_label_new(_("Standby after"));
     gtk_widget_show(label);
     gtk_table_attach_defaults(GTK_TABLE(dpms_spins),label,0,1,0,1);
-    priv->spin_1 = gtk_spin_button_new_with_range(1,238,1);
+    priv->spin_1 = xfpm_spin_button_new_with_range(1,238,1);
+    xfpm_spin_button_set_suffix(XFPM_SPIN_BUTTON(priv->spin_1),_(" min"));
     gtk_widget_show(priv->spin_1);
     gtk_table_attach(GTK_TABLE(dpms_spins),priv->spin_1,1,2,0,1,GTK_SHRINK,GTK_SHRINK,0,0);
     
     label = gtk_label_new(_("Suspend after"));
     gtk_widget_show(label);
     gtk_table_attach_defaults(GTK_TABLE(dpms_spins),label,0,1,1,2);
-    priv->spin_2 = gtk_spin_button_new_with_range(1,239,1);
+    priv->spin_2 = xfpm_spin_button_new_with_range(1,239,1);
+    xfpm_spin_button_set_suffix(XFPM_SPIN_BUTTON(priv->spin_2),_(" min"));
     gtk_widget_show(priv->spin_2);
     gtk_table_attach(GTK_TABLE(dpms_spins),priv->spin_2,1,2,1,2,GTK_SHRINK,GTK_SHRINK,0,0);
     
     label = gtk_label_new(_("Turn off after"));
     gtk_widget_show(label);
     gtk_table_attach_defaults(GTK_TABLE(dpms_spins),label,0,1,2,3);
-    priv->spin_3 = gtk_spin_button_new_with_range(1,300,1);
+    priv->spin_3 = xfpm_spin_button_new_with_range(1,300,1);
+    xfpm_spin_button_set_suffix(XFPM_SPIN_BUTTON(priv->spin_3),_(" min"));
     gtk_widget_show(priv->spin_3);
     gtk_table_attach(GTK_TABLE(dpms_spins),priv->spin_3,1,2,2,3,GTK_SHRINK,GTK_SHRINK,0,0);
     

Modified: xfce4-power-manager/trunk/src/xfpm-settings.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-settings.c	2008-10-13 08:54:05 UTC (rev 5601)
+++ xfce4-power-manager/trunk/src/xfpm-settings.c	2008-10-13 09:53:03 UTC (rev 5602)
@@ -46,6 +46,7 @@
 #include "xfpm-settings.h"
 #include "xfpm-enums.h"
 #include "xfpm-common.h"
+#include "xfpm-spin-button.h"
 
 #ifdef HAVE_DPMS
 #include "xfpm-dpms-spins.h"
@@ -393,7 +394,8 @@
     gtk_widget_show(label);
     gtk_table_attach_defaults(GTK_TABLE(table),label,0,1,0,1);
     
-    critical_spin = gtk_spin_button_new_with_range(1,15,1);
+    critical_spin = xfpm_spin_button_new_with_range(1,15,1);
+    xfpm_spin_button_set_suffix(XFPM_SPIN_BUTTON(critical_spin),_(" percent"));
     gtk_widget_show(critical_spin);
     gtk_spin_button_set_value(GTK_SPIN_BUTTON(critical_spin),
                               xfconf_channel_get_uint(channel,CRITICAL_BATT_CFG,12));

Added: xfce4-power-manager/trunk/src/xfpm-spin-button.c
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-spin-button.c	                        (rev 0)
+++ xfce4-power-manager/trunk/src/xfpm-spin-button.c	2008-10-13 09:53:03 UTC (rev 5602)
@@ -0,0 +1,266 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+#include <math.h>
+#include <locale.h>
+
+#include <gtk/gtk.h>
+#include <glib/gi18n.h>
+
+#include "xfpm-spin-button.h"
+
+#define MAX_DIGITS 20
+
+/* init */
+static void xfpm_spin_button_init      (XfpmSpinButton *spin_button);
+static void xfpm_spin_button_class_init(XfpmSpinButtonClass *klass);
+static void xfpm_spin_button_finalize  (GObject *object);
+
+static void xfpm_spin_button_editable_init(GtkEditableClass *iface);
+static void xfpm_spin_button_delete_text (GtkEditable *editable,
+                                          gint         start_pos,
+                                          gint         end_pos);
+static void xfpm_spin_button_insert_text (GtkEditable *editable,
+										  const gchar *new_text,
+										  gint         new_text_length,
+										  gint        *position);
+			     
+G_DEFINE_TYPE_WITH_CODE(XfpmSpinButton,xfpm_spin_button,GTK_TYPE_SPIN_BUTTON,
+						G_IMPLEMENT_INTERFACE(GTK_TYPE_EDITABLE,
+											  xfpm_spin_button_editable_init))
+
+static void
+xfpm_spin_button_size_request(GtkWidget *widget,GtkRequisition *req)
+{
+    GtkSpinButton *gtk_spin_button;
+    XfpmSpinButton *xfpm_spin_button;
+    
+    gtk_spin_button = GTK_SPIN_BUTTON(widget);
+    xfpm_spin_button = XFPM_SPIN_BUTTON(widget);
+    
+    GTK_WIDGET_CLASS(xfpm_spin_button_parent_class)->size_request(widget,req);
+    
+    PangoFontMetrics *metrics;
+    PangoContext *context;
+      
+    gtk_widget_ensure_style (GTK_WIDGET(xfpm_spin_button));
+    context = gtk_widget_get_pango_context (GTK_WIDGET(xfpm_spin_button));
+    metrics = pango_context_get_metrics (context,
+                           widget->style->font_desc,
+                           pango_context_get_language (context));	
+
+    gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
+    
+    gint char_pixels = (char_width + PANGO_SCALE - 1) / PANGO_SCALE;
+    
+    req->width += char_pixels * xfpm_spin_button->suffix_length;
+}
+
+static void
+xfpm_spin_button_class_init(XfpmSpinButtonClass *klass) 
+{
+    GObjectClass *object_class     = (GObjectClass *)klass;
+    GtkWidgetClass *widget_class   = (GtkWidgetClass *) klass;
+    
+    widget_class->size_request = xfpm_spin_button_size_request;
+    
+    object_class->finalize   = xfpm_spin_button_finalize;
+    
+}
+
+static void
+xfpm_spin_button_init(XfpmSpinButton *spin_button)
+{
+    spin_button->suffix_length = 0;
+    spin_button->suffix_position = 0;
+    spin_button->suffix = NULL;
+}
+
+static void
+xfpm_spin_button_finalize(GObject *object)
+{
+    XfpmSpinButton *spin;
+    spin = XFPM_SPIN_BUTTON(object);
+    
+    if ( spin->suffix )
+    {
+        g_free(spin->suffix);
+    }
+    
+    G_OBJECT_CLASS(xfpm_spin_button_parent_class)->finalize(object);
+}
+
+static void
+xfpm_spin_button_editable_init(GtkEditableClass *iface)
+{
+	iface->insert_text = xfpm_spin_button_insert_text;
+	iface->delete_text = xfpm_spin_button_delete_text;
+}
+
+
+static void
+_spin_button_update(XfpmSpinButton *spin)
+{
+    GtkWidget *widget;
+    
+    widget = GTK_WIDGET(spin);
+    
+    if ( GTK_WIDGET_DRAWABLE(widget) )
+    {
+        gtk_widget_queue_resize_no_redraw(widget);
+    }
+    
+    
+}
+
+static void
+xfpm_spin_button_delete_text (GtkEditable *editable,
+                              gint         start_pos,
+                              gint         end_pos)
+{
+    
+    GtkEditableClass *spin_iface = g_type_interface_peek (xfpm_spin_button_parent_class,
+	                                                      GTK_TYPE_EDITABLE);
+    GtkEditableClass *entry_iface = g_type_interface_peek_parent(spin_iface);
+    
+    XfpmSpinButton *spin = XFPM_SPIN_BUTTON(editable);
+    
+    const gchar *text = gtk_entry_get_text(GTK_ENTRY(editable));
+    gint text_length = strlen(text);
+
+    gint length;
+    if ( spin->suffix )
+    {
+        length = text_length - spin->suffix_length ;
+        if ( start_pos >= ABS(length) ) return;
+        entry_iface->delete_text(editable,start_pos,ABS(length));
+        _spin_button_update(spin);
+    }
+    else
+    {
+        entry_iface->delete_text(editable,start_pos,end_pos);
+        _spin_button_update(spin);
+    }
+}                                   
+
+static gboolean
+_is_digits(const gchar *text,gint length)
+{
+    gint i;
+
+    for ( i = 0 ; i < length ; i++)
+    {
+        if ( text[i] < 0x30 || text[i] > 0x39 )
+        {
+            return FALSE;
+        }
+    }
+    return TRUE;
+}
+
+static void xfpm_spin_button_insert_text (GtkEditable *editable,
+										  const gchar *new_text,
+										  gint         new_text_length,
+										  gint        *position)
+{
+    GtkEditableClass *spin_iface = g_type_interface_peek (xfpm_spin_button_parent_class,
+	                                                      GTK_TYPE_EDITABLE);
+    GtkEditableClass *entry_iface = g_type_interface_peek_parent(spin_iface);
+    
+    XfpmSpinButton *spin = XFPM_SPIN_BUTTON(editable);
+    
+    if ( !_is_digits(new_text,new_text_length) )
+    {
+        if ( spin->suffix && !strcmp(new_text,spin->suffix) )
+        {
+            entry_iface->insert_text(editable,spin->suffix,spin->suffix_length,position);
+            _spin_button_update(spin);
+        }
+    }
+    else 
+    {
+        if ( spin->suffix )
+        {
+            const gchar *text = gtk_entry_get_text(GTK_ENTRY(editable));
+            gint text_length = strlen(text);
+            gint length = text_length - spin->suffix_length ;
+            if ( *position > ABS(length) ) return;
+            entry_iface->insert_text(editable,new_text,new_text_length,position);
+            _spin_button_update(spin);
+        }
+        else
+        {
+            entry_iface->insert_text(editable,new_text,new_text_length,position);
+            _spin_button_update(spin);
+        }
+    }
+}										  
+
+/* Constructor for the xfpm-spin button is the same as 
+ * gtk_spin_button_new_with_range.
+ * 
+ * GTK - The GIMP Toolkit
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * GtkSpinButton widget for GTK+
+ * Copyright (C) 1998 Lars Hamann and Stefan Jeske
+ *
+ */
+  
+GtkWidget *
+xfpm_spin_button_new_with_range(gdouble min,gdouble max,gdouble step)
+{
+    GtkObject *adj;
+    XfpmSpinButton *spin;
+    
+	spin = g_object_new(XFPM_TYPE_SPIN_BUTTON,NULL);
+
+    gint digits;
+
+    g_return_val_if_fail (min <= max, NULL);
+    g_return_val_if_fail (step != 0.0, NULL);
+
+    adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
+
+    if (fabs (step) >= 1.0 || step == 0.0)
+        digits = 0;
+    else {
+        digits = abs ((gint) floor (log10 (fabs (step))));
+        if (digits > MAX_DIGITS)
+            digits = MAX_DIGITS;
+    }
+
+    gtk_spin_button_configure (GTK_SPIN_BUTTON(spin), GTK_ADJUSTMENT (adj), step, digits);
+
+    gtk_spin_button_set_numeric (GTK_SPIN_BUTTON(spin), TRUE);
+
+	return GTK_WIDGET(spin);
+}
+
+void           
+xfpm_spin_button_set_suffix (XfpmSpinButton *spin,
+							 const gchar *suffix)
+{
+	g_return_if_fail(XFPM_IS_SPIN_BUTTON(spin));
+	g_return_if_fail(suffix != NULL);
+	
+    spin->suffix = g_strdup(suffix);
+	spin->suffix_length = strlen(spin->suffix);
+
+	gtk_entry_append_text(GTK_ENTRY(spin),spin->suffix);
+}

Added: xfce4-power-manager/trunk/src/xfpm-spin-button.h
===================================================================
--- xfce4-power-manager/trunk/src/xfpm-spin-button.h	                        (rev 0)
+++ xfce4-power-manager/trunk/src/xfpm-spin-button.h	2008-10-13 09:53:03 UTC (rev 5602)
@@ -0,0 +1,39 @@
+#ifndef __XFPM_SPIN_BUTTON_H
+#define __XFPM_SPIN_BUTTON_H
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define XFPM_TYPE_SPIN_BUTTON    (xfpm_spin_button_get_type())
+#define XFPM_SPIN_BUTTON(o)      (G_TYPE_CHECK_INSTANCE_CAST((o),XFPM_TYPE_SPIN_BUTTON,XfpmSpinButton))
+#define XFPM_IS_SPIN_BUTTON(o)   (G_TYPE_CHECK_INSTANCE_TYPE((o),XFPM_TYPE_SPIN_BUTTON))
+
+typedef struct XfpmSpinButtonPrivate XfpmSpinButtonPrivate;
+
+typedef struct 
+{
+    GtkSpinButton parent;
+    
+    gchar *suffix;
+    gint suffix_length;
+    gint suffix_position;
+    
+} XfpmSpinButton;
+
+typedef struct 
+{
+    GtkSpinButtonClass parent_class;
+    
+} XfpmSpinButtonClass;
+
+GType          xfpm_spin_button_get_type           (void);
+GtkWidget     *xfpm_spin_button_new_with_range     (gdouble min,
+													gdouble max,
+													gdouble step);
+void           xfpm_spin_button_set_suffix         (XfpmSpinButton *spin,
+													const gchar *suffix);
+G_END_DECLS
+
+#endif /* __XFPM_SPIN_BUTTON_H */




More information about the Goodies-commits mailing list