hex spin button



I need a spin button that displays as hex.
I need it for BROWSING MEMORY, it is an ADDRESS button.
hex mode is set with gtk_spin_button_show_as_hex()
In gtk_wpin_button_update it will not parse entry as hex unless first to
chars are 0x.
I tried to follow the numeric & wrap parameters as examples for this patch:


diff -u gtk/gtkspinbutton.c spin.hex/gtkspinbutton.c
--- gtk/gtkspinbutton.c	Wed Nov 25 11:51:38 1998
+++ spin.hex/gtkspinbutton.c	Wed Nov 25 13:51:18 1998
@@ -49,7 +49,8 @@
   ARG_WRAP,
   ARG_UPDATE_POLICY,
   ARG_SHADOW_TYPE,
-  ARG_VALUE
+  ARG_VALUE,
+  ARG_HEX
 };
 
 
@@ -185,6 +186,10 @@
 			   GTK_TYPE_FLOAT,
 			   GTK_ARG_READWRITE,
 			   ARG_VALUE);
+  gtk_object_add_arg_type ("GtkSpinButton::hex",
+			   GTK_TYPE_BOOL,
+			   GTK_ARG_READWRITE,
+			   ARG_HEX);
   
 
   object_class->set_arg = gtk_spin_button_set_arg;
@@ -261,6 +266,9 @@
     case ARG_VALUE:
       gtk_spin_button_set_value (spin_button, GTK_VALUE_FLOAT (*arg));
       break;
+    case ARG_HEX:
+      gtk_spin_button_show_as_hex (spin_button, GTK_VALUE_BOOL (*arg));
+      break;
     default:
       break;
     }
@@ -304,6 +312,9 @@
     case ARG_VALUE:
       GTK_VALUE_FLOAT (*arg) = spin_button->adjustment->value;
       break;
+    case ARG_HEX:
+      GTK_VALUE_BOOL (*arg) = spin_button->hex;
+      break;
     default:
       arg->type = GTK_TYPE_INVALID;
       break;
@@ -330,6 +341,7 @@
   spin_button->numeric = FALSE;
   spin_button->wrap = FALSE;
   spin_button->snap_to_ticks = FALSE;
+  spin_button->hex = FALSE;
 
   gtk_spin_button_set_adjustment (spin_button,
 				  (GtkAdjustment*) gtk_adjustment_new (0, 0, 0, 0, 0, 0));
@@ -935,7 +947,10 @@
   g_return_if_fail (adjustment != NULL);
   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
 
-  sprintf (buf, "%0.*f", spin_button->digits, adjustment->value);
+  if (spin_button->hex)
+     sprintf (buf, "%#x", (int)adjustment->value);
+  else
+     sprintf (buf, "%0.*f", spin_button->digits, adjustment->value);
   gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
 }
 
@@ -1086,8 +1101,11 @@
     {
       char buf[MAX_TEXT_LENGTH];
 
-      sprintf (buf, "%0.*f", spin_button->digits, 
-	       spin_button->adjustment->value);
+      if (spin_button->hex)
+        sprintf (buf, "%#x", (int)spin_button->adjustment->value);
+      else
+        sprintf (buf, "%0.*f", spin_button->digits, 
+	         spin_button->adjustment->value);
       if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
 	gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
     }
@@ -1098,11 +1116,16 @@
 {
   gfloat val;
   gchar *error = NULL;
+  gchar *text = NULL;
 
   g_return_if_fail (spin_button != NULL);
   g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
 
-  val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &error);
+  text = gtk_entry_get_text (GTK_ENTRY (spin_button));
+  if (spin_button->hex && text[0] == '0' && text[1] == 'x')
+    val = strtol (text, &error, 16);
+  else
+    val = strtod (text, &error);
 
   if (spin_button->update_policy == GTK_UPDATE_ALWAYS)
     {
@@ -1128,10 +1151,13 @@
 	gtk_adjustment_set_value (spin_button->adjustment, val);
       else
 	{
-	  char buf[MAX_TEXT_LENGTH];
-	  
-	  sprintf (buf, "%0.*f", spin_button->digits, 
-		   spin_button->adjustment->value);
+      char buf[MAX_TEXT_LENGTH];
+
+      if (spin_button->hex)
+	    sprintf (buf, "%#x", (int)spin_button->adjustment->value);
+      else
+	    sprintf (buf, "%0.*f", spin_button->digits, 
+		     spin_button->adjustment->value);
 	  if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
 	    gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
 	}
@@ -1409,8 +1435,11 @@
     {
       char buf[MAX_TEXT_LENGTH];
 
-      sprintf (buf, "%0.*f", spin_button->digits, 
-               spin_button->adjustment->value);
+      if (spin_button->hex)
+        sprintf (buf, "%#x", (int)spin_button->adjustment->value);
+      else
+        sprintf (buf, "%0.*f", spin_button->digits, 
+                 spin_button->adjustment->value);
       if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (spin_button))))
         gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
     }
@@ -1437,6 +1466,19 @@
 }
 
 void
+gtk_spin_button_show_as_hex		(GtkSpinButton *spin_button,
+			gboolean		hex)
+{
+  char buf[MAX_TEXT_LENGTH];
+  g_return_if_fail (spin_button != NULL);
+  g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
+
+  spin_button->hex = (hex != 0);
+  sprintf (buf, "%#x", (int)spin_button->adjustment->value);
+  gtk_entry_set_text (GTK_ENTRY (spin_button), buf);
+}
+
+void
 gtk_spin_button_set_wrap (GtkSpinButton  *spin_button,
 			  gboolean        wrap)
 {
@@ -1480,7 +1522,10 @@
 	  gchar *error = NULL;
 	  gfloat val;
 
-	  val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &error);
+      if (spin_button->hex)
+	    val = strtol (gtk_entry_get_text (GTK_ENTRY (spin_button)), &error, 16);
+      else
+	    val = strtod (gtk_entry_get_text (GTK_ENTRY (spin_button)), &error);
 	  gtk_spin_button_snap (spin_button, val);
 	}
     }
diff -u gtk/gtkspinbutton.h spin.hex/gtkspinbutton.h
--- gtk/gtkspinbutton.h	Wed Nov 25 11:51:38 1998
+++ spin.hex/gtkspinbutton.h	Wed Nov 25 13:13:14 1998
@@ -89,6 +89,7 @@
   guint numeric : 1;
   guint wrap : 1;
   guint snap_to_ticks : 1;
+  guint hex : 1;
 };
 
 struct _GtkSpinButtonClass
@@ -132,6 +133,9 @@
 void		gtk_spin_button_spin		   (GtkSpinButton  *spin_button,
 						    GtkSpinType     direction,
 						    gfloat	    increment);
+
+void		gtk_spin_button_show_as_hex		(GtkSpinButton *spin_button,
+								gboolean	hex);
 
 void		gtk_spin_button_set_wrap	   (GtkSpinButton  *spin_button,
 						    gboolean	    wrap);



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