input only event boxes



I finally made some time to implement the input-only window support for
GtkEventBox. This is really useful when you're trying to write code that
follows themes well.

Comments?

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Alexander Larsson                                            Red Hat, Inc 
                   alexl redhat com    alla lysator liu se 
He's a jaded shark-wrestling romance novelist who hides his scarred face 
behind a mask. She's a pregnant cigar-chomping doctor who hides her beauty 
behind a pair of thick-framed spectacles. They fight crime! 
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gtk+/ChangeLog,v
retrieving revision 1.4407
diff -u -p -r1.4407 ChangeLog
--- ChangeLog	19 Aug 2003 21:17:14 -0000	1.4407
+++ ChangeLog	20 Aug 2003 11:40:47 -0000
@@ -1,3 +1,11 @@
+2003-08-20  Alexander Larsson  <alexl redhat com>
+
+	* gtk/gtkeventbox.[hc]:
+	Implement gtk_event_box_get/set_input_only()
+	
+	* tests/testgtk.c:
+	Tests for the new input only event boxes
+
 Mon Aug 18 17:19:12 2003  Owen Taylor  <otaylor redhat com>
 
 	* modules/input/gtkimcontextxim.[ch]: Substantially
Index: gtk/gtkeventbox.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkeventbox.c,v
retrieving revision 1.26
diff -u -p -r1.26 gtkeventbox.c
--- gtk/gtkeventbox.c	10 Oct 2002 22:00:09 -0000	1.26
+++ gtk/gtkeventbox.c	20 Aug 2003 11:40:48 -0000
@@ -26,10 +26,20 @@
 
 #include "gtkeventbox.h"
 
+typedef struct 
+{
+  gboolean input_only;
+  GdkWindow *event_window;
+} GtkEventBoxPrivate;
+
+#define GTK_EVENT_BOX_GET_PRIVATE(obj)  G_TYPE_INSTANCE_GET_PRIVATE((obj), GTK_TYPE_EVENT_BOX, GtkEventBoxPrivate)
 
 static void gtk_event_box_class_init               (GtkEventBoxClass *klass);
 static void gtk_event_box_init                     (GtkEventBox      *event_box);
 static void gtk_event_box_realize                  (GtkWidget        *widget);
+static void gtk_event_box_unrealize                (GtkWidget        *widget);
+static void gtk_event_box_map                      (GtkWidget        *widget);
+static void gtk_event_box_unmap                    (GtkWidget        *widget);
 static void gtk_event_box_size_request             (GtkWidget        *widget,
 						    GtkRequisition   *requisition);
 static void gtk_event_box_size_allocate            (GtkWidget        *widget,
@@ -79,15 +89,25 @@ gtk_event_box_class_init (GtkEventBoxCla
   widget_class = (GtkWidgetClass*) class;
 
   widget_class->realize = gtk_event_box_realize;
+  widget_class->unrealize = gtk_event_box_unrealize;
+  widget_class->map = gtk_event_box_map;
+  widget_class->unmap = gtk_event_box_unmap;
   widget_class->size_request = gtk_event_box_size_request;
   widget_class->size_allocate = gtk_event_box_size_allocate;
   widget_class->expose_event = gtk_event_box_expose;
+
+  g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
 }
 
 static void
 gtk_event_box_init (GtkEventBox *event_box)
 {
+  GtkEventBoxPrivate *priv;
+
   GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
+ 
+  priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
+  priv->input_only = FALSE;
 }
 
 GtkWidget*
@@ -96,12 +116,108 @@ gtk_event_box_new (void)
   return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
 }
 
+/**
+ * gtk_event_box_get_input_only:
+ * @event_box: a #GtkEventBox
+ *
+ * Return whether the event box is using input only windows
+ * or not.
+ *
+ * Return value: TRUE if the event box uses an input only
+ *   window, and FALSE if it uses a normal window.
+ *
+ * Since: 2.4
+ **/
+gboolean
+gtk_event_box_get_input_only (GtkEventBox *event_box)
+{
+  GtkEventBoxPrivate *priv;
+
+  g_return_if_fail (event_box != NULL);
+  g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
+
+  priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
+
+  return priv->input_only;
+}
+
+/**
+ * gtk_event_box_set_input_only:
+ * @event_box: a #GtkEventBox
+ * @input_only: boolean value
+ *
+ * Set whether the event box uses a normal (GDK_INPUT_OUTPUT)
+ * child window, or an invisible (GDK_INPUT_ONLY) window.
+ * The default is to use normal windows.
+ *
+ * Input only event boxes are useful when you want the contents
+ * of the event box to follow the theme of the parent widget
+ * better. They don't overdraw the background of the parent
+ * window.
+ * 
+ * Since: 2.4
+ **/
+void
+gtk_event_box_set_input_only (GtkEventBox *event_box,
+			      gboolean input_only)
+{
+  GtkWidget *widget;
+  GtkEventBoxPrivate *priv;
+
+  g_return_if_fail (event_box != NULL);
+  g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
+
+  widget = GTK_WIDGET (event_box);
+  priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
+
+  input_only = input_only != FALSE;
+
+  if (priv->input_only != input_only)
+    {
+      if (GTK_WIDGET_REALIZED (widget))
+	{
+	  gboolean visible = GTK_WIDGET_VISIBLE (widget);
+
+	  if (visible)
+	    gtk_widget_hide (widget);
+
+	  gtk_widget_unrealize (widget);
+	  priv->input_only = input_only;
+
+	  if (priv->input_only)
+	    GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
+	  else
+	    GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
+	  
+	  gtk_widget_realize (widget);
+
+	  if (visible)
+	    gtk_widget_show (widget);
+	}
+      else
+	{
+	  priv->input_only = input_only;
+
+	  if (priv->input_only)
+	    GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
+	  else
+	    GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
+	}
+
+      if (GTK_WIDGET_VISIBLE (widget))
+	gtk_widget_queue_resize (widget);
+    }
+  
+}
+
+
 static void
 gtk_event_box_realize (GtkWidget *widget)
 {
   GdkWindowAttr attributes;
   gint attributes_mask;
   gint border_width;
+  GtkEventBoxPrivate *priv;
 
   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
 
@@ -112,9 +228,6 @@ gtk_event_box_realize (GtkWidget *widget
   attributes.width = widget->allocation.width - 2*border_width;
   attributes.height = widget->allocation.height - 2*border_width;
   attributes.window_type = GDK_WINDOW_CHILD;
-  attributes.wclass = GDK_INPUT_OUTPUT;
-  attributes.visual = gtk_widget_get_visual (widget);
-  attributes.colormap = gtk_widget_get_colormap (widget);
   attributes.event_mask = gtk_widget_get_events (widget)
 			| GDK_BUTTON_MOTION_MASK
 			| GDK_BUTTON_PRESS_MASK
@@ -123,16 +236,94 @@ gtk_event_box_realize (GtkWidget *widget
 			| GDK_ENTER_NOTIFY_MASK
 			| GDK_LEAVE_NOTIFY_MASK;
 
-  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
+  
+  if (GTK_WIDGET_NO_WINDOW (widget))
+    {
+      attributes.wclass = GDK_INPUT_ONLY;
+      attributes_mask = GDK_WA_X | GDK_WA_Y;
+      
+      widget->window = gtk_widget_get_parent_window (widget);
+      g_object_ref (widget->window);
+      
+      priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
+      priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
+					   &attributes, attributes_mask);
+      gdk_window_set_user_data (priv->event_window, widget);
+    }
+  else
+    {
+      attributes.visual = gtk_widget_get_visual (widget);
+      attributes.colormap = gtk_widget_get_colormap (widget);
+      attributes.wclass = GDK_INPUT_OUTPUT;
+      
+      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
+      
+      widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
+      gdk_window_set_user_data (widget->window, widget);
+    }
 
-  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
-  gdk_window_set_user_data (widget->window, widget);
 
   widget->style = gtk_style_attach (widget->style, widget->window);
-  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
+  
+  if (!GTK_WIDGET_NO_WINDOW (widget))
+    gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
+}
+
+static void
+gtk_event_box_unrealize (GtkWidget *widget)
+{
+  GtkEventBox *event_box;
+  GtkEventBoxPrivate *priv;
+  
+  g_return_if_fail (widget != NULL);
+  g_return_if_fail (GTK_IS_EVENT_BOX (widget));
+
+  event_box = GTK_EVENT_BOX (widget);
+  
+  if (GTK_WIDGET_NO_WINDOW (widget))
+    {
+      priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
+      
+      gdk_window_set_user_data (priv->event_window, NULL);
+      gdk_window_destroy (priv->event_window);
+      priv->event_window = NULL;
+    }
+
+  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
+    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
 }
 
 static void
+gtk_event_box_map (GtkWidget *widget)
+{
+  GtkEventBoxPrivate *priv;
+
+  (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
+
+  if (GTK_WIDGET_NO_WINDOW (widget))
+    {
+      priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
+      gdk_window_show (priv->event_window);
+    }
+}
+
+static void
+gtk_event_box_unmap (GtkWidget *widget)
+{
+  GtkEventBoxPrivate *priv;
+
+  if (GTK_WIDGET_NO_WINDOW (widget))
+    {
+      priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
+      gdk_window_hide (priv->event_window);
+    }
+  
+  (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
+}
+
+
+
+static void
 gtk_event_box_size_request (GtkWidget      *widget,
 			    GtkRequisition *requisition)
 {
@@ -154,22 +345,36 @@ gtk_event_box_size_request (GtkWidget   
 
 static void
 gtk_event_box_size_allocate (GtkWidget     *widget,
-			    GtkAllocation *allocation)
+			     GtkAllocation *allocation)
 {
   GtkBin *bin;
   GtkAllocation child_allocation;
+  GdkWindow *window;
 
   widget->allocation = *allocation;
   bin = GTK_BIN (widget);
 
-  child_allocation.x = 0;
-  child_allocation.y = 0;
+  
+  if (GTK_WIDGET_NO_WINDOW (widget))
+    {
+      child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
+      child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
+    }
+  else
+    {
+      child_allocation.x = 0;
+      child_allocation.y = 0;
+    }
   child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
   child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
 
   if (GTK_WIDGET_REALIZED (widget))
     {
-      gdk_window_move_resize (widget->window,
+      if (GTK_WIDGET_NO_WINDOW (widget))
+	window = GTK_EVENT_BOX_GET_PRIVATE (widget)->event_window;
+      else
+	window = widget->window;
+      gdk_window_move_resize (window,
 			      allocation->x + GTK_CONTAINER (widget)->border_width,
 			      allocation->y + GTK_CONTAINER (widget)->border_width,
 			      child_allocation.width,
@@ -199,7 +404,8 @@ gtk_event_box_expose (GtkWidget      *wi
 {
   if (GTK_WIDGET_DRAWABLE (widget))
     {
-      gtk_event_box_paint (widget, &event->area);
+      if (!GTK_WIDGET_NO_WINDOW (widget))
+	gtk_event_box_paint (widget, &event->area);
       
       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
     }
Index: gtk/gtkeventbox.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkeventbox.h,v
retrieving revision 1.9
diff -u -p -r1.9 gtkeventbox.h
--- gtk/gtkeventbox.h	10 Oct 2002 22:00:09 -0000	1.9
+++ gtk/gtkeventbox.h	20 Aug 2003 11:40:48 -0000
@@ -44,7 +44,6 @@ extern "C" {
 #define GTK_IS_EVENT_BOX_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_EVENT_BOX))
 #define GTK_EVENT_BOX_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_EVENT_BOX, GtkEventBoxClass))
 
-
 typedef struct _GtkEventBox	  GtkEventBox;
 typedef struct _GtkEventBoxClass  GtkEventBoxClass;
 
@@ -60,6 +59,9 @@ struct _GtkEventBoxClass
 
 GType	       gtk_event_box_get_type	     (void) G_GNUC_CONST;
 GtkWidget*     gtk_event_box_new	     (void);
+void           gtk_event_box_set_input_only  (GtkEventBox *event_box,
+					      gboolean input_only);
+gboolean       gtk_event_box_get_input_only  (GtkEventBox *event_box);
 
 #ifdef __cplusplus
 }
Index: tests/testgtk.c
===================================================================
RCS file: /cvs/gnome/gtk+/tests/testgtk.c,v
retrieving revision 1.336
diff -u -p -r1.336 testgtk.c
--- tests/testgtk.c	10 Aug 2003 23:37:27 -0000	1.336
+++ tests/testgtk.c	20 Aug 2003 11:40:49 -0000
@@ -4434,6 +4434,102 @@ create_entry (GtkWidget *widget)
     gtk_widget_destroy (window);
 }
 
+/* GtkEventBox */
+
+
+static void
+event_box_label_pressed (GtkWidget        *widget,
+			 GdkEventButton   *event,
+			 gpointer user_data)
+{
+  g_print ("clicked on label\n");
+}
+
+static void
+event_box_toggle_input_only (GtkWidget *checkbutton,
+			     GtkEventBox *event_box)
+{
+   gtk_event_box_set_input_only (event_box,
+				 GTK_TOGGLE_BUTTON(checkbutton)->active);
+}
+
+static void
+create_event_box (GtkWidget *widget)
+{
+  static GtkWidget *window = NULL;
+  GtkWidget *box1;
+  GtkWidget *box2;
+  GtkWidget *hbox;
+  GtkWidget *button;
+  GtkWidget *separator;
+  GtkWidget *event_box;
+  GtkWidget *label;
+  GtkWidget *input_only_check;
+  GdkColor color;
+
+  if (!window)
+    {
+      color.red = 0;
+      color.blue = 65535;
+      color.green = 0;
+      
+      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+      gtk_window_set_screen (GTK_WINDOW (window),
+			     gtk_widget_get_screen (widget));
+
+      g_signal_connect (window, "destroy",
+			G_CALLBACK (gtk_widget_destroyed),
+			&window);
+
+      gtk_window_set_title (GTK_WINDOW (window), "event box");
+      gtk_container_set_border_width (GTK_CONTAINER (window), 0);
+
+      box1 = gtk_vbox_new (FALSE, 0);
+      gtk_container_add (GTK_CONTAINER (window), box1);
+      gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &color);
+
+      hbox = gtk_hbox_new (FALSE, 0);
+      gtk_box_pack_start (GTK_BOX (box1), hbox, TRUE, FALSE, 0);
+      
+      event_box = gtk_event_box_new ();
+      gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, FALSE, 0);
+
+      label = gtk_label_new ("Click on this label");
+      gtk_container_add (GTK_CONTAINER (event_box), label);
+          
+      g_signal_connect (event_box, "button_press_event",
+			G_CALLBACK (event_box_label_pressed),
+			NULL);
+
+      input_only_check = gtk_check_button_new_with_label("Input Only Window");
+      gtk_box_pack_start (GTK_BOX (box1), input_only_check, FALSE, TRUE, 0);
+      g_signal_connect (input_only_check, "toggled",
+			G_CALLBACK (event_box_toggle_input_only), event_box);
+      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (input_only_check), FALSE);
+      
+      separator = gtk_hseparator_new ();
+      gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
+
+      box2 = gtk_vbox_new (FALSE, 10);
+      gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
+      gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
+
+      button = gtk_button_new_with_label ("close");
+      g_signal_connect_swapped (button, "clicked",
+			        G_CALLBACK (gtk_widget_destroy),
+				window);
+      gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
+      GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
+      gtk_widget_grab_default (button);
+    }
+
+  if (!GTK_WIDGET_VISIBLE (window))
+    gtk_widget_show_all (window);
+  else
+    gtk_widget_destroy (window);
+}
+
+
 /*
  * GtkSizeGroup
  */
@@ -12310,6 +12406,7 @@ struct {
   { "dialog", create_dialog },
   { "display & screen", create_display_screen },
   { "entry", create_entry },
+  { "event box", create_event_box },
   { "event watcher", create_event_watcher },
   { "file selection", create_file_selection },
   { "flipping", create_flipping },


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