gnome-terminal r2455 - trunk/src



Author: chpe
Date: Wed Mar 19 15:21:05 2008
New Revision: 2455
URL: http://svn.gnome.org/viewvc/gnome-terminal?rev=2455&view=rev

Log:
Add tabs menu. Code adapted from Epiphany.


Added:
   trunk/src/terminal-tabs-menu.c
   trunk/src/terminal-tabs-menu.h
Modified:
   trunk/src/Makefile.am
   trunk/src/terminal-window.c
   trunk/src/terminal-window.h

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	(original)
+++ trunk/src/Makefile.am	Wed Mar 19 15:21:05 2008
@@ -42,6 +42,8 @@
 	terminal-profile.h			\
 	terminal-screen.c			\
 	terminal-screen.h			\
+	terminal-tabs-menu.c			\
+	terminal-tabs-menu.h			\
 	terminal-widget.h			\
 	terminal-widget-vte.c			\
 	terminal-window.c			\

Added: trunk/src/terminal-tabs-menu.c
==============================================================================
--- (empty file)
+++ trunk/src/terminal-tabs-menu.c	Wed Mar 19 15:21:05 2008
@@ -0,0 +1,488 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ *  Copyright  2003  David Bordoley
+ *  Copyright  2003-2004 Christian Persch
+ *
+ *  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, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ *  $Id$
+ */
+
+#include "config.h"
+
+#include "terminal-tabs-menu.h"
+#include "terminal-screen.h"
+
+#include <glib/gi18n.h>
+#include <gtk/gtklabel.h>
+#include <gtk/gtkmenuitem.h>
+#include <gtk/gtkaccelmap.h>
+#include <gtk/gtkaction.h>
+#include <gtk/gtktoggleaction.h>
+#include <gtk/gtkradioaction.h>
+#include <gtk/gtkuimanager.h>
+#include <string.h>
+#include <stdlib.h>
+
+#define LABEL_WIDTH_CHARS 32
+#define ACTION_VERB_FORMAT_PREFIX       "JmpTab"
+#define ACTION_VERB_FORMAT_PREFIX_LEN   (6) /* strlen (ACTION_VERB_FORMAT_PREFIX) */
+#define ACTION_VERB_FORMAT		ACTION_VERB_FORMAT_PREFIX "%x"
+#define ACTION_VERB_FORMAT_LENGTH	strlen (ACTION_VERB_FORMAT) + 14 + 1
+#define ACTION_VERB_FORMAT_BASE         (16) /* %x is hex */
+#define ACCEL_PATH_FORMAT		"<Actions>/TabsActions/%s"
+#define ACCEL_PATH_FORMAT_LENGTH	strlen (ACCEL_PATH_FORMAT) -2 + ACTION_VERB_FORMAT_LENGTH
+#define DATA_KEY			"TerminalTabsMenu::Action"
+
+#define UI_PATH                         "/menubar/Tabs"
+
+#define TERMINAL_TABS_MENU_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), TERMINAL_TYPE_TABS_MENU, TerminalTabsMenuPrivate))
+
+struct _TerminalTabsMenuPrivate
+{
+	TerminalWindow *window;
+	GtkActionGroup *action_group;
+	GtkAction *anchor_action;
+	guint ui_id;
+};
+
+enum
+{
+	PROP_0,
+	PROP_WINDOW
+};
+
+static void	terminal_tabs_menu_class_init	(TerminalTabsMenuClass *klass);
+static void	terminal_tabs_menu_init	  	(TerminalTabsMenu *menu);
+static void	terminal_tabs_menu_update		(TerminalTabsMenu *menu);
+
+/* FIXME: this can be severely optimised */
+static GByteArray *tabs_id_array = NULL;
+static guint n_tabs = 0;
+
+G_DEFINE_TYPE (TerminalTabsMenu, terminal_tabs_menu, G_TYPE_OBJECT)
+
+/* We need to assign unique IDs to tabs, otherwise accels get confused in the
+ * tabs menu (bug #339548). We could use a serial #, but the ID is used in the
+ * action name which is stored in a GQuark and so we should allocate them
+ * efficiently.
+ */
+static guint
+allocate_tab_id (void)
+{
+        int bit;
+        guint b, len;
+        guint8 *data;
+        guint8 byte, mask;
+
+        if (n_tabs++ == 0)
+        {
+                g_assert (tabs_id_array == NULL);
+                tabs_id_array = g_byte_array_sized_new (16);
+        }
+
+        /* Find a free ID */
+        len = tabs_id_array->len;
+        data = tabs_id_array->data;
+        for (b = 0; b < len; ++b)
+        {
+                if (data[b] != 0xff)
+                        break;
+        }
+
+        /* Need to append a new byte */
+        if (b == len)
+        {
+                guint8 bytes[] = { 0 };
+                g_byte_array_append (tabs_id_array, bytes, G_N_ELEMENTS (bytes));
+                g_assert (tabs_id_array->len > b);
+        }
+
+        data = tabs_id_array->data + b;
+        byte = 0xff ^ *data;
+        /* Now find the first free bit */
+        bit = g_bit_nth_lsf (byte, -1);
+        mask = 1 << bit;
+        g_assert (bit >= 0 && bit <= 7);
+        g_assert ((*data & mask) == 0);
+        /* And mark it as allocated */
+        *data |= mask;
+
+        return b * 8 + bit;
+}
+
+static void
+free_tab_id (GtkAction *action)
+{
+        const char *name;
+        guint id;
+        guint8 *data;
+        guint b, bit;
+
+        name = gtk_action_get_name (action);
+        id = g_ascii_strtoull (name + ACTION_VERB_FORMAT_PREFIX_LEN, NULL,
+                               ACTION_VERB_FORMAT_BASE);
+        g_assert (id >= 0 && id < tabs_id_array->len * 8);
+
+        b = id >> 3;
+        bit = id & 0x7;
+        data = tabs_id_array->data + b;
+        *data &= ~(1 << bit);
+
+        g_assert (n_tabs > 0);
+        if (--n_tabs == 0)
+        {
+                g_assert (tabs_id_array != NULL);
+                g_byte_array_free (tabs_id_array, TRUE);
+                tabs_id_array = NULL;
+        }
+}
+
+static void
+tab_action_activate_cb (GtkToggleAction *action,
+			TerminalTabsMenu *menu)
+{
+	TerminalTabsMenuPrivate *priv = menu->priv;
+	TerminalScreen *screen;
+
+	if (gtk_toggle_action_get_active (action) == FALSE)
+	{
+		return;
+	}
+
+	screen = g_object_get_data (G_OBJECT (action), DATA_KEY);
+	g_return_if_fail (screen != NULL);
+
+	if (terminal_window_get_active (priv->window) != screen)
+	{
+                terminal_window_set_active (priv->window, screen);
+	}
+}
+
+static void
+sync_tab_title (TerminalScreen *screen,
+		GParamSpec *pspec,
+		GtkAction *action)
+{
+	const char *title;
+
+	title = terminal_screen_get_title (screen);
+
+	g_object_set (action, "label", title, NULL);
+}
+
+static void
+notebook_page_added_cb (GtkNotebook *notebook,
+			TerminalScreen *screen,
+			guint position,
+			TerminalTabsMenu *menu)
+{
+	TerminalTabsMenuPrivate *priv = menu->priv;
+	GtkAction *action;
+	char verb[ACTION_VERB_FORMAT_LENGTH];
+	GSList *group;
+
+	g_snprintf (verb, sizeof (verb), ACTION_VERB_FORMAT, allocate_tab_id ());
+  
+	action = g_object_new (GTK_TYPE_RADIO_ACTION,
+			       "name", verb,
+			       "tooltip", _("Switch to this tab"),
+			       NULL);
+
+	sync_tab_title (screen, NULL, action);
+	/* make sure the action is alive when handling the signal, see bug #169833 */
+	g_signal_connect_object (screen, "notify::title",
+				 G_CALLBACK (sync_tab_title), action, 0);
+
+	gtk_action_group_add_action_with_accel (priv->action_group, action, NULL);
+
+	group = gtk_radio_action_get_group (GTK_RADIO_ACTION (priv->anchor_action));
+	gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
+
+	/* set this here too, since tab-added comes after notify::active-child */
+	if (terminal_window_get_active (priv->window) == screen)
+	{
+		gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
+	}
+
+	g_object_set_data (G_OBJECT (screen), DATA_KEY, action);
+	g_object_set_data (G_OBJECT (action), DATA_KEY, screen);
+
+	g_signal_connect (action, "activate",
+			  G_CALLBACK (tab_action_activate_cb), menu);
+
+	g_object_unref (action);
+
+	terminal_tabs_menu_update (menu);
+}
+
+static void
+notebook_page_removed_cb (GtkNotebook *notebook,
+			  TerminalScreen *screen,
+			  guint position,
+			  TerminalTabsMenu *menu)
+{
+	TerminalTabsMenuPrivate *priv = menu->priv;
+	GtkAction *action;
+
+	action = g_object_get_data (G_OBJECT (screen), DATA_KEY);
+	g_return_if_fail (action != NULL);
+
+        free_tab_id (action);
+
+	g_signal_handlers_disconnect_by_func
+		(screen, G_CALLBACK (sync_tab_title), action);
+
+	g_signal_handlers_disconnect_by_func
+		(action, G_CALLBACK (tab_action_activate_cb), menu);
+
+	g_object_set_data (G_OBJECT (screen), DATA_KEY, NULL);
+ 	gtk_action_group_remove_action (priv->action_group, action);
+
+	terminal_tabs_menu_update (menu);
+}
+
+static void
+notebook_page_reordered_cb (GtkNotebook *notebook,
+			    TerminalScreen *screen,
+			    guint position,
+			    TerminalTabsMenu *menu)
+{
+	terminal_tabs_menu_update (menu);
+}
+
+static void
+connect_proxy_cb (GtkActionGroup *action_group,
+		  GtkAction *action,
+		  GtkWidget *proxy,
+		  gpointer dummy)
+{
+	if (GTK_IS_MENU_ITEM (proxy))
+	{
+		GtkLabel *label;
+
+		label = GTK_LABEL (GTK_BIN (proxy)->child);
+
+		gtk_label_set_use_underline (label, FALSE);
+		gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END);
+		gtk_label_set_max_width_chars (label, LABEL_WIDTH_CHARS);
+	}
+}
+
+static void
+sync_active_tab (TerminalWindow *window,
+		 GParamSpec *pspec,
+		 TerminalTabsMenu *menu)
+{
+	TerminalScreen *screen;
+	GtkAction *action;
+
+	screen = terminal_window_get_active (window);
+	if (screen == NULL) return;
+
+	action = g_object_get_data (G_OBJECT (screen), DATA_KEY);
+	/* happens initially, since the ::active-child comes before
+	* the ::tab-added signal
+	*/
+	/* FIXME that's not true with gtk+ 2.9 anymore */
+	if (action == NULL) return;
+
+	gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
+}
+
+static void
+terminal_tabs_menu_set_window (TerminalTabsMenu *menu,
+			   TerminalWindow *window)
+{
+	TerminalTabsMenuPrivate *priv = menu->priv;
+	GtkWidget *notebook;
+	GtkUIManager *manager;
+
+	priv->window = window;
+
+	manager = GTK_UI_MANAGER (terminal_window_get_ui_manager (window));
+	priv->action_group = gtk_action_group_new ("TabsActions");
+	gtk_ui_manager_insert_action_group (manager, priv->action_group, -1);
+	g_object_unref (priv->action_group);
+
+	priv->anchor_action = g_object_new (GTK_TYPE_RADIO_ACTION,
+					    "name", "TabsMenuAnchorAction",
+					    NULL);
+	gtk_action_group_add_action (priv->action_group, priv->anchor_action);
+
+	g_signal_connect (priv->action_group, "connect-proxy",
+			  G_CALLBACK (connect_proxy_cb), NULL);
+
+	g_signal_connect (window, "notify::active-child",
+			  G_CALLBACK (sync_active_tab), menu);
+
+	notebook = terminal_window_get_notebook (window);
+	g_signal_connect_object (notebook, "page-added",
+				 G_CALLBACK (notebook_page_added_cb), menu, 0);
+	g_signal_connect_object (notebook, "page-removed",
+				 G_CALLBACK (notebook_page_removed_cb), menu, 0);
+	g_signal_connect_object (notebook, "page-reordered",
+				 G_CALLBACK (notebook_page_reordered_cb), menu, 0);
+}
+
+static void
+terminal_tabs_menu_set_property (GObject *object,
+			     guint prop_id,
+			     const GValue *value,
+			     GParamSpec *pspec)
+{
+	TerminalTabsMenu *menu = TERMINAL_TABS_MENU (object);
+
+	switch (prop_id)
+	{
+		case PROP_WINDOW:
+			terminal_tabs_menu_set_window (menu, g_value_get_object (value));
+			break;
+	}
+}
+
+static void
+terminal_tabs_menu_get_property (GObject *object,
+			     guint prop_id,
+			     GValue *value,
+			     GParamSpec *pspec)
+{
+	/* no readable properties */
+	g_return_if_reached ();
+}
+
+static void
+terminal_tabs_menu_class_init (TerminalTabsMenuClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+	object_class->set_property = terminal_tabs_menu_set_property;
+	object_class->get_property = terminal_tabs_menu_get_property;
+
+	g_object_class_install_property (object_class,
+					 PROP_WINDOW,
+					 g_param_spec_object ("window", NULL, NULL,
+							      TERMINAL_TYPE_WINDOW,
+							      G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB |
+							      G_PARAM_CONSTRUCT_ONLY));
+
+	g_type_class_add_private (object_class, sizeof (TerminalTabsMenuPrivate));
+}
+
+static void
+terminal_tabs_menu_init (TerminalTabsMenu *menu)
+{
+	menu->priv = TERMINAL_TABS_MENU_GET_PRIVATE (menu);
+}
+
+static void
+terminal_tabs_menu_clean (TerminalTabsMenu *menu)
+{
+	TerminalTabsMenuPrivate *p = menu->priv;
+	GtkUIManager *manager = GTK_UI_MANAGER (terminal_window_get_ui_manager (p->window));
+
+	if (p->ui_id != 0)
+	{
+		gtk_ui_manager_remove_ui (manager, p->ui_id);
+		gtk_ui_manager_ensure_update (manager);
+		p->ui_id = 0;
+	}
+}
+
+TerminalTabsMenu *
+terminal_tabs_menu_new (TerminalWindow *window)
+{
+	return TERMINAL_TABS_MENU (g_object_new (TERMINAL_TYPE_TABS_MENU,
+					     "window", window,
+					     NULL));
+}
+
+static void
+tab_set_action_accelerator (GtkActionGroup *action_group,
+			    GtkAction *action,
+			    guint tab_number)
+{
+	const char *verb;
+	char accel_path[ACCEL_PATH_FORMAT_LENGTH];
+	char accel[7];
+	gint accel_number;
+	guint accel_key;
+	GdkModifierType accel_mods;
+
+	verb = gtk_action_get_name (action);
+
+	/* set the accel path for the menu item */
+	g_snprintf (accel_path, sizeof (accel_path),
+		    ACCEL_PATH_FORMAT, verb);
+	gtk_action_set_accel_path (action, accel_path);
+
+	/* Only the first ten tabs get accelerators starting from 1 through 0 */
+	if (tab_number < 10)
+	{
+		accel_key = 0;
+		accel_number = (tab_number + 1) % 10;
+
+		g_snprintf (accel, sizeof (accel), "<alt>%d", accel_number);
+
+		gtk_accelerator_parse (accel, &accel_key, &accel_mods);
+
+		if (accel_key != 0)
+		{
+			gtk_accel_map_change_entry (accel_path, accel_key,
+						    accel_mods, TRUE);
+		}
+	}
+	else
+	{
+		gtk_accel_map_change_entry (accel_path, 0, 0, TRUE);
+	}
+}
+
+static void
+terminal_tabs_menu_update (TerminalTabsMenu *menu)
+{
+	TerminalTabsMenuPrivate *p = menu->priv;
+	GtkUIManager *manager;
+	GtkAction *action;
+	GList *tabs = NULL, *l;
+	guint i = 0;
+	const char *verb;
+
+	terminal_tabs_menu_clean (menu);
+
+	tabs = terminal_window_list_screens (p->window);
+
+	if (g_list_length (tabs) == 0) return;
+
+	manager =  GTK_UI_MANAGER (terminal_window_get_ui_manager (p->window));
+	p->ui_id = gtk_ui_manager_new_merge_id (manager);
+
+	for (l = tabs; l != NULL; l = l->next)
+	{
+		action = g_object_get_data (G_OBJECT (l->data), DATA_KEY);
+		g_return_if_fail (action != NULL);
+  
+		verb = gtk_action_get_name (action);
+
+		tab_set_action_accelerator (p->action_group, action, i++);
+
+		gtk_ui_manager_add_ui (manager, p->ui_id,
+				       UI_PATH,
+				       verb, verb,
+				       GTK_UI_MANAGER_MENUITEM, FALSE);
+	}
+
+	g_list_free (tabs);
+}

Added: trunk/src/terminal-tabs-menu.h
==============================================================================
--- (empty file)
+++ trunk/src/terminal-tabs-menu.h	Wed Mar 19 15:21:05 2008
@@ -0,0 +1,58 @@
+/*
+ *  Copyright  2003 David Bordoley
+ *
+ *  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, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ *  $Id$
+ */
+
+#ifndef TERMINAL_TABS_MENU_H
+#define TERMINAL_TABS_MENU_H
+
+#include "terminal-window.h"
+
+G_BEGIN_DECLS
+
+#define TERMINAL_TYPE_TABS_MENU		(terminal_tabs_menu_get_type ())
+#define TERMINAL_TABS_MENU(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), TERMINAL_TYPE_TABS_MENU, TerminalTabsMenu))
+#define TERMINAL_TABS_MENU_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), TERMINAL_TYPE_TABS_MENU, TerminalTabsMenuClass))
+#define TERMINAL_IS_TABS_MENU(o)	(G_TYPE_CHECK_INSTANCE_TYPE ((o), TERMINAL_TYPE_TABS_MENU))
+#define TERMINAL_IS_TABS_MENU_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), TERMINAL_TYPE_TABS_MENU))
+#define TERMINAL_TABS_MENU_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), TERMINAL_TYPE_TABS_MENU, TerminalTabsMenuClass))
+
+typedef struct _TerminalTabsMenu TerminalTabsMenu;
+typedef struct _TerminalTabsMenuClass TerminalTabsMenuClass;
+typedef struct _TerminalTabsMenuPrivate TerminalTabsMenuPrivate;
+
+struct _TerminalTabsMenuClass
+{
+	GObjectClass parent_class;
+};
+
+struct _TerminalTabsMenu
+{
+	GObject parent_object;
+
+	/*< private >*/
+	TerminalTabsMenuPrivate *priv;
+};
+
+GType		terminal_tabs_menu_get_type		(void);
+
+TerminalTabsMenu   *terminal_tabs_menu_new		(TerminalWindow *window);
+
+G_END_DECLS
+
+#endif

Modified: trunk/src/terminal-window.c
==============================================================================
--- trunk/src/terminal-window.c	(original)
+++ trunk/src/terminal-window.c	Wed Mar 19 15:21:05 2008
@@ -27,6 +27,7 @@
 #include "terminal-accels.h"
 #include "terminal-widget.h"
 #include "terminal-window.h"
+#include "terminal-tabs-menu.h"
 #include "terminal.h"
 #include "encoding.h"
 #include <string.h>
@@ -53,6 +54,8 @@
   GtkActionGroup *encodings_action_group;
   guint encodings_ui_id;
 
+  TerminalTabsMenu *tabs_menu;
+
   GtkWidget *menubar;
   GtkWidget *notebook;
   GList *tab_menuitems;
@@ -1085,6 +1088,9 @@
 		      FALSE, FALSE, 0);
   gtk_widget_show (priv->menubar);
 
+  /* Add tabs menu */
+  priv->tabs_menu = terminal_tabs_menu_new (window);
+
   /* FIXMEchpe remove this*/
   g_signal_connect (priv->menubar, "can-activate-accel",
 		    G_CALLBACK (gtk_true),
@@ -1170,6 +1176,8 @@
 
   g_free (priv->startup_id);
 
+  g_object_unref (priv->tabs_menu);
+
   G_OBJECT_CLASS (terminal_window_parent_class)->finalize (object);
 }
 
@@ -2938,3 +2946,11 @@
   g_free (priv->startup_id);
   priv->startup_id = g_strdup (startup_id);
 }
+
+GtkUIManager *
+terminal_window_get_ui_manager (TerminalWindow *window)
+{
+  TerminalWindowPrivate *priv = window->priv;
+
+  return priv->ui_manager;
+}

Modified: trunk/src/terminal-window.h
==============================================================================
--- trunk/src/terminal-window.h	(original)
+++ trunk/src/terminal-window.h	Wed Mar 19 15:21:05 2008
@@ -23,6 +23,7 @@
 #define TERMINAL_WINDOW_H
 
 #include <gtk/gtkwindow.h>
+#include <gtk/gtkuimanager.h>
 #include "terminal-screen.h"
 
 G_BEGIN_DECLS
@@ -54,6 +55,8 @@
 
 TerminalWindow* terminal_window_new (void);
 
+GtkUIManager *terminal_window_get_ui_manager (TerminalWindow *window);
+
 void terminal_window_add_screen (TerminalWindow *window,
                                  TerminalScreen *screen,
                                  gint            position);



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