[gedit] Refactor close button common code in a simple widget



commit ea3a3bd357b70967e23a8ef2cc30247e2b57b511
Author: Paolo Borelli <pborelli gnome org>
Date:   Thu Jan 7 12:28:36 2010 +0100

    Refactor close button common code in a simple widget

 gedit/Makefile.am          |   34 ++++++++-------
 gedit/gedit-close-button.c |   80 ++++++++++++++++++++++++++++++++++
 gedit/gedit-close-button.h |   56 ++++++++++++++++++++++++
 gedit/gedit-notebook.c     |   48 ++-------------------
 gedit/gedit-panel.c        |  103 +++++++++++++-------------------------------
 5 files changed, 188 insertions(+), 133 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index 007dbf0..dc99229 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -14,7 +14,7 @@ INCLUDES =								\
 	-I$(srcdir)							\
 	-I$(srcdir)/smclient						\
 	$(GEDIT_CFLAGS)							\
-	$(IGE_MAC_CFLAGS)							\
+	$(IGE_MAC_CFLAGS)						\
 	$(WARN_CFLAGS)							\
 	$(DISABLE_DEPRECATED_CFLAGS)					\
 	-DDATADIR=\""$(datadir)"\"					\
@@ -63,29 +63,30 @@ gedit_LDADD += gedit-res.o
 endif
 
 NOINST_H_FILES =			\
+	gedit-close-button.h		\
+	gedit-dirs.h			\
+	gedit-document-loader.h		\
+	gedit-document-saver.h		\
+	gedit-documents-panel.h		\
+	gedit-gio-document-loader.h	\
+	gedit-gio-document-saver.h	\
+	gedit-history-entry.h		\
+	gedit-io-error-message-area.h	\
 	gedit-language-manager.h	\
-	gedit-style-scheme-manager.h	\
+	gedit-local-document-saver.h	\
+	gedit-object-module.h		\
 	gedit-plugin-info.h		\
 	gedit-plugin-info-priv.h	\
 	gedit-plugin-manager.h		\
 	gedit-plugins-engine.h		\
-	gedit-object-module.h		\
-	gedit-ui.h			\
-	gedit-window-private.h		\
-	gedit-documents-panel.h		\
-	gedit-document-loader.h		\
-	gedit-gio-document-loader.h	\
-	gedit-document-saver.h		\
-	gedit-local-document-saver.h	\
-	gedit-gio-document-saver.h	\
-	gedit-history-entry.h		\
+	gedit-prefs-manager-private.h	\
 	gedit-print-job.h		\
 	gedit-print-preview.h		\
-	gedit-io-error-message-area.h	\
-	gedit-prefs-manager-private.h	\
-	gedittextregion.h		\
 	gedit-session.h			\
-	gedit-dirs.h
+	gedit-style-scheme-manager.h	\
+	gedittextregion.h		\
+	gedit-ui.h			\
+	gedit-window-private.h
 
 INST_H_FILES =				\
 	gedit-app.h			\
@@ -136,6 +137,7 @@ libgedit_la_SOURCES = 			\
 	$(BACON_FILES)			\
 	$(POSIXIO_FILES)		\
 	gedit-app.c			\
+	gedit-close-button.c		\
 	gedit-commands-documents.c	\
 	gedit-commands-edit.c		\
 	gedit-commands-file.c		\
diff --git a/gedit/gedit-close-button.c b/gedit/gedit-close-button.c
new file mode 100644
index 0000000..0e9f157
--- /dev/null
+++ b/gedit/gedit-close-button.c
@@ -0,0 +1,80 @@
+/*
+ * gedit-close-button.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Paolo Borelli
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, 
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "gedit-close-button.h"
+
+G_DEFINE_TYPE (GeditCloseButton, gedit_close_button, GTK_TYPE_BUTTON)
+
+static void
+gedit_close_button_style_set (GtkWidget *button,
+			      GtkStyle *previous_style)
+{
+	gint h, w;
+
+	gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
+					   GTK_ICON_SIZE_MENU, &w, &h);
+
+	gtk_widget_set_size_request (button, w + 2, h + 2);
+
+	GTK_WIDGET_CLASS (gedit_close_button_parent_class)->style_set (button, previous_style);
+}
+
+static void
+gedit_close_button_class_init (GeditCloseButtonClass *klass)
+{
+	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+	widget_class->style_set = gedit_close_button_style_set;
+}
+
+static void
+gedit_close_button_init (GeditCloseButton *button)
+{
+	GtkRcStyle *rcstyle;
+	GtkWidget *image;
+
+	/* make it as small as possible */
+	rcstyle = gtk_rc_style_new ();
+	rcstyle->xthickness = rcstyle->ythickness = 0;
+	gtk_widget_modify_style (GTK_WIDGET (button), rcstyle);
+	g_object_unref (rcstyle);
+
+	image = gtk_image_new_from_stock (GTK_STOCK_CLOSE,
+					  GTK_ICON_SIZE_MENU);
+	gtk_widget_show (image);
+
+	gtk_container_add (GTK_CONTAINER (button), image);
+}
+
+GtkWidget *
+gedit_close_button_new ()
+{
+	GeditCloseButton *button;
+
+	button = g_object_new (GEDIT_TYPE_CLOSE_BUTTON,
+			       "relief", GTK_RELIEF_NONE,
+			       "focus-on-click", FALSE,
+			       NULL);
+
+	return GTK_WIDGET (button);
+}
+
diff --git a/gedit/gedit-close-button.h b/gedit/gedit-close-button.h
new file mode 100644
index 0000000..4f63138
--- /dev/null
+++ b/gedit/gedit-close-button.h
@@ -0,0 +1,56 @@
+/*
+ * gedit-close-button.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Paolo Borelli
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, 
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GEDIT_CLOSE_BUTTON_H__
+#define __GEDIT_CLOSE_BUTTON_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GEDIT_TYPE_CLOSE_BUTTON			(gedit_close_button_get_type ())
+#define GEDIT_CLOSE_BUTTON(obj)			(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_CLOSE_BUTTON, GeditCloseButton))
+#define GEDIT_CLOSE_BUTTON_CONST(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_CLOSE_BUTTON, GeditCloseButton const))
+#define GEDIT_CLOSE_BUTTON_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST ((klass), GEDIT_TYPE_CLOSE_BUTTON, GeditCloseButtonClass))
+#define GEDIT_IS_CLOSE_BUTTON(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEDIT_TYPE_CLOSE_BUTTON))
+#define GEDIT_IS_CLOSE_BUTTON_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_CLOSE_BUTTON))
+#define GEDIT_CLOSE_BUTTON_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GEDIT_TYPE_CLOSE_BUTTON, GeditCloseButtonClass))
+
+typedef struct _GeditCloseButton	GeditCloseButton;
+typedef struct _GeditCloseButtonClass	GeditCloseButtonClass;
+typedef struct _GeditCloseButtonPrivate	GeditCloseButtonPrivate;
+
+struct _GeditCloseButton {
+	GtkButton parent;
+};
+
+struct _GeditCloseButtonClass {
+	GtkButtonClass parent_class;
+};
+
+GType		  gedit_close_button_get_type (void) G_GNUC_CONST;
+
+GtkWidget	 *gedit_close_button_new (void);
+
+G_END_DECLS
+
+#endif /* __GEDIT_CLOSE_BUTTON_H__ */
diff --git a/gedit/gedit-notebook.c b/gedit/gedit-notebook.c
index b0fc2fb..73f697a 100644
--- a/gedit/gedit-notebook.c
+++ b/gedit/gedit-notebook.c
@@ -43,6 +43,7 @@
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
+#include "gedit-close-button.h"
 #include "gedit-notebook.h"
 #include "gedit-marshal.h"
 #include "gedit-window.h"
@@ -157,17 +158,6 @@ gedit_notebook_class_init (GeditNotebookClass *klass)
 			      GEDIT_TYPE_TAB);
 
 	g_type_class_add_private (object_class, sizeof(GeditNotebookPrivate));
-	
-	/* Set up a style for the close button with no focus padding. */
-	gtk_rc_parse_string (
-		"style \"gedit-close-button-style\"\n"
-		"{\n"
-		"  GtkWidget::focus-padding = 0\n"
-		"  GtkWidget::focus-line-width = 0\n"
-		"  xthickness = 0\n"
-		"  ythickness = 0\n"
-		"}\n"
-		"widget \"*.gedit-close-button\" style \"gedit-close-button-style\"");
 }
 
 static GeditNotebook *
@@ -863,19 +853,6 @@ close_button_clicked_cb (GtkWidget *widget,
 	g_signal_emit (notebook, signals[TAB_CLOSE_REQUEST], 0, tab);
 }
 
-static void
-close_button_style_set_cb (GtkWidget *button,
-			   GtkStyle *previous_style,
-			   gpointer user_data)
-{
-	gint h, w;
-
-	gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
-					   GTK_ICON_SIZE_MENU, &w, &h);
-
-	gtk_widget_set_size_request (button, w + 2, h + 2);
-}
-
 static GtkWidget *
 build_tab_label (GeditNotebook *nb, 
 		 GeditTab      *tab)
@@ -883,7 +860,6 @@ build_tab_label (GeditNotebook *nb,
 	GtkWidget *hbox, *label_hbox, *label_ebox;
 	GtkWidget *label, *dummy_label;
 	GtkWidget *close_button;
-	GtkWidget *image;
 	GtkWidget *spinner;
 	GtkWidget *icon;
 
@@ -896,23 +872,12 @@ build_tab_label (GeditNotebook *nb,
 	label_hbox = gtk_hbox_new (FALSE, 4);
 	gtk_container_add (GTK_CONTAINER (label_ebox), label_hbox);
 
-	/* setup close button */
-	close_button = gtk_button_new ();
-	gtk_button_set_relief (GTK_BUTTON (close_button),
-			       GTK_RELIEF_NONE);
-	/* don't allow focus on the close button */
-	gtk_button_set_focus_on_click (GTK_BUTTON (close_button), FALSE);
+	close_button = gedit_close_button_new ();
 
-	/* make it as small as possible */
-	gtk_widget_set_name (close_button, "gedit-close-button");
+	gtk_widget_set_tooltip_text (close_button, _("Close document"));
 
-	image = gtk_image_new_from_stock (GTK_STOCK_CLOSE,
-					  GTK_ICON_SIZE_MENU);
-	gtk_container_add (GTK_CONTAINER (close_button), image);
 	gtk_box_pack_start (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
 
-	gtk_widget_set_tooltip_text (close_button, _("Close document"));
-
 	g_signal_connect (close_button,
 			  "clicked",
 			  G_CALLBACK (close_button_clicked_cb),
@@ -939,17 +904,12 @@ build_tab_label (GeditNotebook *nb,
 
 	dummy_label = gtk_label_new ("");
 	gtk_box_pack_start (GTK_BOX (label_hbox), dummy_label, TRUE, TRUE, 0);
-	
-	/* Set minimal size */
-	g_signal_connect (close_button, "style-set",
-			  G_CALLBACK (close_button_style_set_cb), NULL);
-	
+
 	gtk_widget_show (hbox);
 	gtk_widget_show (label_ebox);
 	gtk_widget_show (label_hbox);
 	gtk_widget_show (label);
 	gtk_widget_show (dummy_label);	
-	gtk_widget_show (image);
 	gtk_widget_show (close_button);
 	gtk_widget_show (icon);
 	
diff --git a/gedit/gedit-panel.c b/gedit/gedit-panel.c
index 78c8633..305bf2a 100644
--- a/gedit/gedit-panel.c
+++ b/gedit/gedit-panel.c
@@ -35,6 +35,7 @@
 #include <gdk/gdk.h>
 #include <gdk/gdkkeysyms.h>
 
+#include "gedit-close-button.h"
 #include "gedit-window.h"
 #include "gedit-debug.h"
 
@@ -239,7 +240,7 @@ gedit_panel_class_init (GeditPanelClass *klass)
 		  	      g_cclosure_marshal_VOID__VOID,
 			      G_TYPE_NONE, 0);					
 	binding_set = gtk_binding_set_by_class (klass);
-  
+
 	gtk_binding_entry_add_signal (binding_set, 
 				      GDK_Escape, 
 				      0, 
@@ -395,57 +396,31 @@ panel_show (GeditPanel *panel,
 }
 
 static void
-close_button_clicked_cb (GtkWidget *widget,
-			 GtkWidget *panel)
-{
-	gtk_widget_hide (panel);
-}
-
-static void
 gedit_panel_init (GeditPanel *panel)
 {
 	panel->priv = GEDIT_PANEL_GET_PRIVATE (panel);
-	g_return_if_fail (panel->priv != NULL);	
 }
 
 static void
-button_style_set_cb (GtkWidget *button,
-		     GtkStyle *previous_style,
-		     gpointer user_data)
+close_button_clicked_cb (GtkWidget *widget,
+			 GtkWidget *panel)
 {
-	gint h, w;
-
-	gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
-					   GTK_ICON_SIZE_MENU, &w, &h);
-
-	gtk_widget_set_size_request (button, w + 2, h + 2);
+	gtk_widget_hide (panel);
 }
 
 static GtkWidget *
-create_small_button (GtkWidget *image)
+create_close_button (GeditPanel *panel)
 {
 	GtkWidget *button;
-	GtkRcStyle *rcstyle;
 
-	button = gtk_button_new ();
-	gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
+	button = gedit_close_button_new ();
 
-	/* don't allow focus on the close button */
-	gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
+	gtk_widget_set_tooltip_text (button, _("Hide panel"));
 
-	/* make it as small as possible */
-	rcstyle = gtk_rc_style_new ();
-	rcstyle->xthickness = rcstyle->ythickness = 0;
-	gtk_widget_modify_style (button, rcstyle);
-	g_object_unref (rcstyle);
-
-	gtk_widget_show (image);
-
-	gtk_container_add (GTK_CONTAINER (button), image);
-
-	/* Set minimal size */
-	g_signal_connect (button, "style-set",
-			  G_CALLBACK (button_style_set_cb), NULL);
+	g_signal_connect (button,
+			  "clicked",
+			  G_CALLBACK (close_button_clicked_cb),
+			  panel);
 
 	return button;
 }
@@ -458,16 +433,16 @@ build_notebook_for_panel (GeditPanel *panel)
 
 	gtk_notebook_set_tab_pos (GTK_NOTEBOOK (panel->priv->notebook),
 				  GTK_POS_BOTTOM);
-  	gtk_notebook_set_scrollable (GTK_NOTEBOOK (panel->priv->notebook),
-  				     TRUE);
-  	gtk_notebook_popup_enable (GTK_NOTEBOOK (panel->priv->notebook));
-  
-  	gtk_widget_show (GTK_WIDGET (panel->priv->notebook));
-  
-  	g_signal_connect (panel->priv->notebook,
-  			  "switch-page",
-  			  G_CALLBACK (notebook_page_changed),
-  			  panel);  
+	gtk_notebook_set_scrollable (GTK_NOTEBOOK (panel->priv->notebook),
+				     TRUE);
+	gtk_notebook_popup_enable (GTK_NOTEBOOK (panel->priv->notebook));
+
+	gtk_widget_show (GTK_WIDGET (panel->priv->notebook));
+
+	g_signal_connect (panel->priv->notebook,
+			  "switch-page",
+			  G_CALLBACK (notebook_page_changed),
+			  panel);
 }
 
 static void
@@ -476,8 +451,7 @@ build_horizontal_panel (GeditPanel *panel)
 	GtkWidget *box;
 	GtkWidget *sidebar;
 	GtkWidget *close_button;
-	GtkWidget *image;
-	
+
 	box = gtk_hbox_new(FALSE, 0);
 
 	gtk_box_pack_start (GTK_BOX (box), 
@@ -496,8 +470,7 @@ build_horizontal_panel (GeditPanel *panel)
 			    FALSE, 
 			    0);
 
-	image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
-	close_button = create_small_button (image);
+	close_button = create_close_button (panel);
 
 	gtk_box_pack_start (GTK_BOX (sidebar),
 			    close_button,
@@ -505,13 +478,6 @@ build_horizontal_panel (GeditPanel *panel)
 			    FALSE, 
 			    0);
 
-	gtk_widget_set_tooltip_text (close_button, _("Hide panel"));
-
-	g_signal_connect (close_button,
-			  "clicked",
-                          G_CALLBACK (close_button_clicked_cb),
-                          panel);
-
 	gtk_widget_show_all (box);
 
 	gtk_box_pack_start (GTK_BOX (panel),
@@ -527,9 +493,8 @@ build_vertical_panel (GeditPanel *panel)
 	GtkWidget *close_button;
 	GtkWidget *title_hbox;
 	GtkWidget *icon_name_hbox;
-	GtkWidget *image;
 	GtkWidget *dummy_label;
-	
+
 	/* Create title hbox */
 	title_hbox = gtk_hbox_new (FALSE, 6);
 	gtk_container_set_border_width (GTK_CONTAINER (title_hbox), 5);
@@ -545,7 +510,7 @@ build_vertical_panel (GeditPanel *panel)
 		
 	panel->priv->title_image = 
 				gtk_image_new_from_stock (GTK_STOCK_FILE,
-							  GTK_ICON_SIZE_MENU); 							     
+							  GTK_ICON_SIZE_MENU);
 	gtk_box_pack_start (GTK_BOX (icon_name_hbox), 
 			    panel->priv->title_image, 
 			    FALSE, 
@@ -553,13 +518,13 @@ build_vertical_panel (GeditPanel *panel)
 			    0);
 
 	dummy_label = gtk_label_new (" ");
-		
+
 	gtk_box_pack_start (GTK_BOX (icon_name_hbox), 
 			    dummy_label, 
 			    FALSE, 
 			    FALSE, 
 			    0);	
-				    
+
 	panel->priv->title_label = gtk_label_new (_("Empty"));
 	gtk_misc_set_alignment (GTK_MISC (panel->priv->title_label), 0, 0.5);
 	gtk_label_set_ellipsize(GTK_LABEL (panel->priv->title_label), PANGO_ELLIPSIZE_END);
@@ -570,22 +535,14 @@ build_vertical_panel (GeditPanel *panel)
 			    TRUE,
 			    0);
 
-	image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
-	close_button = create_small_button (image);
-				       
+	close_button = create_close_button (panel);
+
 	gtk_box_pack_start (GTK_BOX (title_hbox),
 			    close_button, 
 			    FALSE, 
 			    FALSE, 
 			    0);
 
-	gtk_widget_set_tooltip_text (close_button, _("Hide panel"));
-
-	g_signal_connect (close_button,
-			  "clicked",
-                          G_CALLBACK (close_button_clicked_cb),
-                          panel);
-
 	gtk_widget_show_all (title_hbox);
 
 	gtk_box_pack_start (GTK_BOX (panel),



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