[gedit] Add internals for ShowTabber plugin



commit c5c8fa6b03f4ee2184850354ec9f5ec7379376fb
Author: Garrett Regier <alias301 gmail com>
Date:   Thu Jun 3 10:33:02 2010 -0700

    Add internals for ShowTabber plugin
    
    Add the GeditNotebookShowTabsModeType and the property
    "show-tabs-mode" to GeditNotebook.

 data/org.gnome.gedit.gschema.xml.in.in |    5 +
 gedit/gedit-notebook.c                 |  232 +++++++++++++++++++++++++-------
 gedit/gedit-notebook.h                 |   11 +-
 gedit/gedit-settings.h                 |    1 +
 4 files changed, 198 insertions(+), 51 deletions(-)
---
diff --git a/data/org.gnome.gedit.gschema.xml.in.in b/data/org.gnome.gedit.gschema.xml.in.in
index 07878d2..7171695 100644
--- a/data/org.gnome.gedit.gschema.xml.in.in
+++ b/data/org.gnome.gedit.gschema.xml.in.in
@@ -133,6 +133,11 @@
       <_summary>Toolbar Buttons Style</_summary>
       <_description>Style for the toolbar buttons. Possible values are "GEDIT_TOOLBAR_SYSTEM" to use the system's default style, "GEDIT_TOOLBAR_ICONS" to display icons only, "GEDIT_TOOLBAR_ICONS_AND_TEXT" to display both icons and text, and "GEDIT_TOOLBAR_ICONS_BOTH_HORIZ" to display prioritized text beside icons. Note that the values are case-sensitive, so make sure they appear exactly as mentioned here.</_description>
     </key>
+    <key name="notebook-show-tabs-mode" type="s">
+      <default>'ALWAYS'</default>
+      <_summary>Notebook Show Tabs Mode</_summary>
+      <_description>Specifies when to show the notebook tabs. Use "NEVER" to never show the tabs, "ALWAYS" to always show the tabs, and "AUTO" to show the tabs only when there is more than one tab. Note that the values are case-sensitive, so make sure they appear exactly as mentioned here.</_description>
+    </key>
     <key name="statusbar-visible" type="b">
       <default>true</default>
       <_summary>Status Bar is Visible</_summary>
diff --git a/gedit/gedit-notebook.c b/gedit/gedit-notebook.c
index a134b69..38507af 100644
--- a/gedit/gedit-notebook.c
+++ b/gedit/gedit-notebook.c
@@ -48,6 +48,8 @@
 #include "gedit-tab-label.h"
 #include "gedit-marshal.h"
 #include "gedit-window.h"
+#include "gedit-enum-types.h"
+#include "gedit-settings.h"
 
 #include "gseal-gtk-compat.h"
 
@@ -58,14 +60,19 @@
 
 struct _GeditNotebookPrivate
 {
+	GSettings     *ui_settings;
+
 	GList         *focused_pages;
 	gulong         motion_notify_handler_id;
+
 	gint           x_start;
 	gint           y_start;
-	gint           drag_in_progress : 1;
-	gint	       always_show_tabs : 1;
-	gint           close_buttons_sensitive : 1;
-	gint           tab_drag_and_drop_enabled : 1;
+
+	GeditNotebookShowTabsModeType show_tabs_mode;
+
+	guint          drag_in_progress : 1;
+	guint          close_buttons_sensitive : 1;
+	guint          tab_drag_and_drop_enabled : 1;
 	guint          destroy_has_run : 1;
 };
 
@@ -82,6 +89,13 @@ static void move_current_tab_to_another_notebook  (GeditNotebook  *src,
 /* Local variables */
 static GdkCursor *cursor = NULL;
 
+/* Properties */
+enum
+{
+	PROP_0,
+	PROP_SHOW_TABS_MODE
+};
+
 /* Signals */
 enum
 {
@@ -93,6 +107,93 @@ enum
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
+
+static void
+update_tabs_visibility (GeditNotebook *notebook,
+			gboolean       before_inserting)
+{
+	gboolean show_tabs;
+
+	switch (notebook->priv->show_tabs_mode)
+	{
+		case GEDIT_NOTEBOOK_SHOW_TABS_NEVER:
+			show_tabs = FALSE;
+			break;
+		case GEDIT_NOTEBOOK_SHOW_TABS_AUTO:
+			{
+				guint num;
+
+				num = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));
+
+				if (before_inserting)
+					++num;
+
+				show_tabs = num > 1;
+			}
+			break;
+		case GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS:
+		default:
+			show_tabs = TRUE;
+			break;
+	}
+
+	gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), show_tabs);
+}
+
+static void
+gedit_notebook_get_property (GObject    *object,
+			     guint       prop_id,
+			     GValue     *value,
+			     GParamSpec *pspec)
+{
+	GeditNotebook *notebook = GEDIT_NOTEBOOK (object);
+
+	switch (prop_id)
+	{
+		case PROP_SHOW_TABS_MODE:
+			g_value_set_enum (value,
+					  notebook->priv->show_tabs_mode);
+			break;
+		default:
+			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+			break;
+	}
+}
+
+static void
+gedit_notebook_set_property (GObject      *object,
+			     guint         prop_id,
+			     const GValue *value,
+			     GParamSpec   *pspec)
+{
+	GeditNotebook *notebook = GEDIT_NOTEBOOK (object);
+
+	switch (prop_id)
+	{
+		case PROP_SHOW_TABS_MODE:
+			notebook->priv->show_tabs_mode = g_value_get_enum (value);
+			update_tabs_visibility (notebook, FALSE);
+			break;
+		default:
+			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+			break;
+	}
+}
+
+static void
+gedit_notebook_dispose (GObject *object)
+{
+	GeditNotebook *notebook = GEDIT_NOTEBOOK (object);
+
+	if (notebook->priv->ui_settings != NULL)
+	{
+		g_object_unref (notebook->priv->ui_settings);
+		notebook->priv->ui_settings = NULL;
+	}
+
+	G_OBJECT_CLASS (gedit_notebook_parent_class)->dispose (object);
+}
+
 static void
 gedit_notebook_finalize (GObject *object)
 {
@@ -125,11 +226,23 @@ gedit_notebook_class_init (GeditNotebookClass *klass)
 	GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
 	GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
 
+	object_class->dispose = gedit_notebook_dispose;
 	object_class->finalize = gedit_notebook_finalize;
+	object_class->get_property = gedit_notebook_get_property;
+	object_class->set_property = gedit_notebook_set_property;
+
 	gtkwidget_class->grab_focus = gedit_notebook_grab_focus;
 	
 	notebook_class->change_current_page = gedit_notebook_change_current_page;
 
+	g_object_class_install_property (object_class, PROP_SHOW_TABS_MODE,
+					 g_param_spec_enum ("show-tabs-mode",
+							    "Show Tabs Mode",
+							    "When tabs should be shown",
+							    GEDIT_TYPE_NOTEBOOK_SHOW_TABS_MODE_TYPE,
+							    GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS,
+							    G_PARAM_READWRITE));
+
 	signals[TAB_DETACHED] =
 		g_signal_new ("tab_detached",
 			      G_OBJECT_CLASS_TYPE (object_class),
@@ -655,24 +768,58 @@ gedit_notebook_switch_page_cb (GtkNotebook     *notebook,
 	gtk_widget_grab_focus (child);
 }
 
-/*
- * update_tabs_visibility: Hide tabs if there is only one tab
- * and the pref is not set.
- */
-static void
-update_tabs_visibility (GeditNotebook *nb, 
-			gboolean       before_inserting)
+static gboolean
+show_tabs_mode_get_mapping (GValue   *value,
+			    GVariant *variant,
+			    gpointer  user_data G_GNUC_UNUSED)
 {
-	gboolean show_tabs;
-	guint num;
+	const gchar *str;
+	GeditNotebookShowTabsModeType show_tabs_mode;
+
+	str = g_variant_get_string (variant, NULL);
+
+	if (g_strcmp0 (str, "NEVER") == 0)
+	{
+		show_tabs_mode = GEDIT_NOTEBOOK_SHOW_TABS_NEVER;
+	}
+	else if (g_strcmp0 (str, "AUTO") == 0)
+	{
+		show_tabs_mode = GEDIT_NOTEBOOK_SHOW_TABS_AUTO;
+	}
+	else /* ALWAYS */
+	{
+		show_tabs_mode = GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS;
+	}
+	
+	g_value_set_enum (value, show_tabs_mode);
 
-	num = gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb));
+	return TRUE;
+}
 
-	if (before_inserting) num++;
+static GVariant *
+show_tabs_mode_set_mapping (const GValue       *value,
+			    const GVariantType *expected_type,
+			    gpointer            user_data G_GNUC_UNUSED)
+{
+	GVariant *result = NULL;
 
-	show_tabs = (nb->priv->always_show_tabs || num > 1);
+	switch (g_value_get_enum (value))
+	{
+		case GEDIT_NOTEBOOK_SHOW_TABS_NEVER:
+			result = g_variant_new_string ("NEVER");
+			break;
+
+		case GEDIT_NOTEBOOK_SHOW_TABS_AUTO:
+			result = g_variant_new_string ("AUTO");
+			break;
+
+		case GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS:
+		default:
+			result = g_variant_new_string ("ALWAYS");
+			break;
+	}
 
-	gtk_notebook_set_show_tabs (GTK_NOTEBOOK (nb), show_tabs);
+	return result;
 }
 
 static void
@@ -680,27 +827,37 @@ gedit_notebook_init (GeditNotebook *notebook)
 {
 	notebook->priv = GEDIT_NOTEBOOK_GET_PRIVATE (notebook);
 
+	notebook->priv->ui_settings = g_settings_new ("org.gnome.gedit.preferences.ui");
+
+	notebook->priv->show_tabs_mode = GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS;
 	notebook->priv->close_buttons_sensitive = TRUE;
 	notebook->priv->tab_drag_and_drop_enabled = TRUE;
 	
 	gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
 	gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
-	gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE);
-
-	notebook->priv->always_show_tabs = TRUE;
-
-	g_signal_connect (notebook, 
+	gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
+
+	g_settings_bind_with_mapping (notebook->priv->ui_settings,
+				      GEDIT_SETTINGS_SHOW_TABS_MODE,
+				      notebook,
+				      "show-tabs-mode",
+				      G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET,
+				      show_tabs_mode_get_mapping,
+				      show_tabs_mode_set_mapping,
+				      NULL, NULL);
+
+	g_signal_connect (notebook,
 			  "button-press-event",
-			  (GCallback)button_press_cb, 
+			  (GCallback)button_press_cb,
 			  NULL);
-	g_signal_connect (notebook, 
+	g_signal_connect (notebook,
 			  "button-release-event",
 			  (GCallback)button_release_cb,
 			  NULL);
-	gtk_widget_add_events (GTK_WIDGET (notebook), 
+	gtk_widget_add_events (GTK_WIDGET (notebook),
 			       GDK_BUTTON1_MOTION_MASK);
 
-	g_signal_connect_after (G_OBJECT (notebook), 
+	g_signal_connect_after (G_OBJECT (notebook),
 				"switch_page",
                                 G_CALLBACK (gedit_notebook_switch_page_cb),
                                 NULL);
@@ -803,24 +960,6 @@ get_tab_label (GeditTab *tab)
 }
 
 /**
- * gedit_notebook_set_always_show_tabs:
- * @nb: a #GeditNotebook
- * @show_tabs: %TRUE to always show the tabs
- *
- * Sets the visibility of the tabs in the @nb.
- */
-void
-gedit_notebook_set_always_show_tabs (GeditNotebook *nb, 
-				     gboolean       show_tabs)
-{
-	g_return_if_fail (GEDIT_IS_NOTEBOOK (nb));
-
-	nb->priv->always_show_tabs = (show_tabs != FALSE);
-
-	update_tabs_visibility (nb, FALSE);
-}
-
-/**
  * gedit_notebook_add_tab:
  * @nb: a #GeditNotebook
  * @tab: a #GeditTab
@@ -846,10 +985,11 @@ gedit_notebook_add_tab (GeditNotebook *nb,
 				  GTK_WIDGET (tab),
 				  tab_label,
 				  position);
-	update_tabs_visibility (nb, TRUE);
+
+	update_tabs_visibility (nb, FALSE);
 
 	/* The signal handler may have reordered the tabs */
-	position = gtk_notebook_page_num (GTK_NOTEBOOK (nb), 
+	position = gtk_notebook_page_num (GTK_NOTEBOOK (nb),
 					  GTK_WIDGET (tab));
 
 	if (jump_to)
diff --git a/gedit/gedit-notebook.h b/gedit/gedit-notebook.h
index fa7d41b..89b91ad 100644
--- a/gedit/gedit-notebook.h
+++ b/gedit/gedit-notebook.h
@@ -55,6 +55,12 @@ G_BEGIN_DECLS
 #define GEDIT_IS_NOTEBOOK_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), GEDIT_TYPE_NOTEBOOK))
 #define GEDIT_NOTEBOOK_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), GEDIT_TYPE_NOTEBOOK, GeditNotebookClass))
 
+typedef enum {
+	GEDIT_NOTEBOOK_SHOW_TABS_NEVER,
+	GEDIT_NOTEBOOK_SHOW_TABS_AUTO,
+	GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS
+} GeditNotebookShowTabsModeType;
+
 /* Private structure type */
 typedef struct _GeditNotebookPrivate	GeditNotebookPrivate;
 
@@ -114,11 +120,6 @@ void            gedit_notebook_move_tab		(GeditNotebook *src,
 						 GeditTab      *tab,
 						 gint           dest_position);
 
-/* FIXME: do we really need this function ? */
-void		gedit_notebook_set_always_show_tabs	
-						(GeditNotebook *nb,
-						 gboolean       show_tabs);
-
 void		gedit_notebook_set_close_buttons_sensitive
 						(GeditNotebook *nb,
 						 gboolean       sensitive);
diff --git a/gedit/gedit-settings.h b/gedit/gedit-settings.h
index 75c945b..64b9f47 100644
--- a/gedit/gedit-settings.h
+++ b/gedit/gedit-settings.h
@@ -130,6 +130,7 @@ void			 gedit_settings_set_wrap_mode			(GSettings     *settings,
 /* window state keys */
 #define GEDIT_SETTINGS_WINDOW_STATE			"state"
 #define GEDIT_SETTINGS_WINDOW_SIZE			"size"
+#define GEDIT_SETTINGS_SHOW_TABS_MODE			"notebook-show-tabs-mode"
 #define GEDIT_SETTINGS_SIDE_PANEL_SIZE			"side-panel-size"
 #define GEDIT_SETTINGS_SIDE_PANEL_ACTIVE_PAGE		"side-panel-active-page"
 #define GEDIT_SETTINGS_BOTTOM_PANEL_SIZE		"bottom-panel-size"



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