Re: entry activating default button



Doh, the patch.

Havoc

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gtk+/ChangeLog,v
retrieving revision 1.1757
diff -u -u -r1.1757 ChangeLog
--- ChangeLog	2001/02/28 19:07:42	1.1757
+++ ChangeLog	2001/02/28 23:07:53
@@ -1,5 +1,15 @@
 2001-02-28  Havoc Pennington  <hp redhat com>
 
+	* gtk/testgtk.c (create_entry): test the activate_default setting
+	on GtkEntry
+
+	* gtk/gtkentry.c (gtk_entry_set_activate_default): New function to 
+	cause the entry to activate the default button for a dialog when 
+	activated
+	(gtk_entry_get_activate_default): new function
+
+2001-02-28  Havoc Pennington  <hp redhat com>
+
 	* gtk/gtkwidget.h (struct _GtkWidgetClass): Remove draw_focus and
 	draw_default
 	(gtk_widget_draw_focus): remove this function; we could instead
Index: gtk/gtkentry.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkentry.c,v
retrieving revision 1.110
diff -u -u -r1.110 gtkentry.c
--- gtk/gtkentry.c	2001/02/28 19:07:45	1.110
+++ gtk/gtkentry.c	2001/02/28 23:07:53
@@ -41,6 +41,7 @@
 #include "gtkseparatormenuitem.h"
 #include "gtkselection.h"
 #include "gtksignal.h"
+#include "gtkwindow.h"
 
 #define MIN_ENTRY_WIDTH  150
 #define DRAW_TIMEOUT     20
@@ -73,7 +74,8 @@
   PROP_EDITABLE,
   PROP_MAX_LENGTH,
   PROP_VISIBILITY,
-  PROP_INVISIBLE_CHAR
+  PROP_INVISIBLE_CHAR,
+  PROP_ACTIVATE_DEFAULT
 };
 
 static guint signals[LAST_SIGNAL] = { 0 };
@@ -205,6 +207,7 @@
 static void gtk_entry_copy_clipboard     (GtkEntry        *entry);
 static void gtk_entry_paste_clipboard    (GtkEntry        *entry);
 static void gtk_entry_toggle_overwrite   (GtkEntry        *entry);
+static void gtk_entry_real_activate      (GtkEntry        *entry);
 
 /* IM Context Callbacks
  */
@@ -355,7 +358,8 @@
   class->copy_clipboard = gtk_entry_copy_clipboard;
   class->paste_clipboard = gtk_entry_paste_clipboard;
   class->toggle_overwrite = gtk_entry_toggle_overwrite;
-
+  class->activate = gtk_entry_real_activate;
+  
   g_object_class_install_property (gobject_class,
                                    PROP_TEXT_POSITION,
                                    g_param_spec_int ("text_position",
@@ -401,6 +405,14 @@
                                                      '*',
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
 
+  g_object_class_install_property (gobject_class,
+                                   PROP_ACTIVATE_DEFAULT,
+                                   g_param_spec_boolean ("activate_default",
+							 _("Activate default"),
+							 _("Whether to activate the default widget when Enter is pressed, such as the default button in a dialog"),
+                                                         FALSE,
+							 G_PARAM_READABLE | G_PARAM_WRITABLE));
+  
   signals[INSERT_TEXT] =
     gtk_signal_new ("insert_text",
 		    GTK_RUN_LAST,
@@ -675,6 +687,10 @@
       gtk_entry_set_invisible_char (entry, g_value_get_int (value));
       break;
 
+    case PROP_ACTIVATE_DEFAULT:
+      gtk_entry_set_activate_default (entry, g_value_get_boolean (value));
+      break;
+      
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -708,6 +724,9 @@
     case PROP_INVISIBLE_CHAR:
       g_value_set_int (value, entry->invisible_char);
       break;
+    case PROP_ACTIVATE_DEFAULT:
+      g_value_set_boolean (value, entry->activate_default);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1777,6 +1796,24 @@
   entry->overwrite_mode = !entry->overwrite_mode;
 }
 
+static void
+gtk_entry_real_activate (GtkEntry *entry)
+{
+  GtkWindow *window;
+  GtkWidget *widget;
+
+  widget = GTK_WIDGET (entry);
+
+  if (entry->activate_default)
+    {
+      window = (GtkWindow *) gtk_widget_get_toplevel (widget);
+      
+      if (window &&
+          window->default_widget != widget)
+        gtk_window_activate_default (window);
+    }
+}
+
 /* IM Context Callbacks
  */
 
@@ -2702,6 +2739,51 @@
   
   entry->text_max_length = max;
   g_object_notify (G_OBJECT (entry), "max_length");
+}
+
+/**
+ * gtk_entry_set_activate_default:
+ * @entry: a #GtkEntry
+ * @setting: %TRUE to activate window's default widget on Enter keypress
+ *
+ * If @setting is %TRUE, pressing Enter in the @entry will activate the default
+ * widget for the window containing the entry. This usually means that
+ * the dialog box containing the entry will be closed, since the default
+ * widget is usually one of the dialog buttons.
+ *
+ * (For experts: if @setting is %TRUE, the entry calls
+ * gtk_window_activate_default() on the window containing the entry in
+ * the default handler for the "activate" signal.)
+ * 
+ **/
+void
+gtk_entry_set_activate_default (GtkEntry *entry,
+                                gboolean  setting)
+{
+  g_return_if_fail (GTK_IS_ENTRY (entry));
+  setting = setting != FALSE;
+
+  if (setting != entry->activate_default)
+    {
+      entry->activate_default = setting;
+      g_object_notify (G_OBJECT (entry), "activate_default");
+    }
+}
+
+/**
+ * gtk_entry_get_activate_default:
+ * @entry: a #GtkEntry
+ * 
+ * Retrieves the value set by gtk_entry_set_activate_default().
+ * 
+ * Return value: %TRUE if the entry will activate the default widget
+ **/
+gboolean
+gtk_entry_get_activate_default (GtkEntry *entry)
+{
+  g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
+
+  return entry->activate_default;
 }
 
 /**
Index: gtk/gtkentry.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkentry.h,v
retrieving revision 1.39
diff -u -u -r1.39 gtkentry.h
--- gtk/gtkentry.h	2001/02/21 20:22:25	1.39
+++ gtk/gtkentry.h	2001/02/28 23:07:53
@@ -77,6 +77,8 @@
   guint        need_im_reset : 1;
 
   guint        has_frame : 1;
+
+  guint        activate_default : 1;
   
   guint   button;
   guint   timer;
@@ -146,6 +148,9 @@
 /* text is truncated if needed */
 void       gtk_entry_set_max_length 		(GtkEntry      *entry,
 						 gint           max);
+void       gtk_entry_set_activate_default       (GtkEntry      *entry,
+                                                 gboolean       setting);
+gboolean   gtk_entry_get_activate_default       (GtkEntry      *entry);
 
 /* Somewhat more convenient than the GtkEditable generic functions
  */
Index: gtk/testgtk.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/testgtk.c,v
retrieving revision 1.221
diff -u -u -r1.221 testgtk.c
--- gtk/testgtk.c	2001/02/27 20:40:15	1.221
+++ gtk/testgtk.c	2001/02/28 23:07:53
@@ -3429,8 +3429,8 @@
 entry_toggle_visibility (GtkWidget *checkbutton,
 			GtkWidget *entry)
 {
-   gtk_entry_set_visibility(GTK_ENTRY(entry),
-                            GTK_TOGGLE_BUTTON(checkbutton)->active);
+   gtk_entry_set_visibility (GTK_ENTRY (entry),
+                             GTK_TOGGLE_BUTTON (checkbutton)->active);
 }
 
 static void
@@ -3443,7 +3443,16 @@
     gtk_entry_set_invisible_char (GTK_ENTRY (entry), '*');
 }
 
+
 static void
+entry_toggle_activate_default (GtkWidget *checkbutton,
+                               GtkWidget *entry)
+{
+   gtk_entry_set_activate_default (GTK_ENTRY (entry),
+                                   GTK_TOGGLE_BUTTON (checkbutton)->active);
+}
+
+static void
 create_entry (void)
 {
   static GtkWidget *window = NULL;
@@ -3452,6 +3461,7 @@
   GtkWidget *editable_check;
   GtkWidget *sensitive_check;
   GtkWidget *invisible_char_check;
+  GtkWidget *activate_check;
   GtkWidget *entry, *cb;
   GtkWidget *button;
   GtkWidget *separator;
@@ -3525,6 +3535,13 @@
       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(sensitive_check), TRUE);
       gtk_widget_show (sensitive_check);
 
+      activate_check = gtk_check_button_new_with_label ("Activate default");
+      gtk_box_pack_start (GTK_BOX (box2), activate_check, FALSE, TRUE, 0);
+      gtk_signal_connect (GTK_OBJECT (activate_check), "toggled",
+			  GTK_SIGNAL_FUNC (entry_toggle_activate_default), entry);
+      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (activate_check), FALSE);
+      gtk_widget_show (activate_check);
+      
       invisible_char_check = gtk_check_button_new_with_label("invisible_char = 0");
       gtk_box_pack_start (GTK_BOX (box2), invisible_char_check, FALSE, TRUE, 0);
       gtk_signal_connect (GTK_OBJECT(invisible_char_check), "toggled",
@@ -3541,7 +3558,6 @@
       separator = gtk_hseparator_new ();
       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
       gtk_widget_show (separator);
-
 
       box2 = gtk_vbox_new (FALSE, 10);
       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);




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