From f51eed31d82a17c72e6856c7b294a4627cdad38a Mon Sep 17 00:00:00 2001 From: Ross Burton Date: Tue, 23 Jul 2002 20:52:22 +0000 Subject: [PATCH] Show the real workspace names in the window menu --- ChangeLog | 5 ++++ src/core.c | 12 ++++++++ src/core.h | 2 ++ src/menu.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index ebb1ba77a..d5784307a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2002-07-23 Ross Burton + + * src/menu.c (meta_window_menu_new): Use the real workspace names + instead of making up numbers. + 2002-07-23 Havoc Pennington * src/keybindings.c (meta_display_process_key_event): handle diff --git a/src/core.c b/src/core.c index 4c86aec84..4a66c7a9a 100644 --- a/src/core.c +++ b/src/core.c @@ -506,6 +506,18 @@ meta_core_show_window_menu (Display *xdisplay, meta_window_show_menu (window, root_x, root_y, button, timestamp); } +char * +meta_core_get_workspace_name_with_index (Display *xdisplay, + int index) +{ + MetaDisplay *display; + MetaWorkspace *workspace; + + display = meta_display_for_x_display (xdisplay); + workspace = meta_display_get_workspace_by_index (display, index); + return (workspace != NULL) ? workspace->name : NULL; +} + gboolean meta_core_begin_grab_op (Display *xdisplay, Window frame_xwindow, diff --git a/src/core.h b/src/core.h index bcd9dd90d..63b254dbf 100644 --- a/src/core.h +++ b/src/core.h @@ -98,6 +98,8 @@ int meta_core_get_num_workspaces (Screen *xscreen); int meta_core_get_active_workspace (Screen *xscreen); int meta_core_get_frame_workspace (Display *xdisplay, Window frame_xwindow); +char* meta_core_get_workspace_name_with_index (Display *xdisplay, + int index); void meta_core_get_frame_extents (Display *xdisplay, Window frame_xwindow, diff --git a/src/menu.c b/src/menu.c index 92a850206..07d2b9ea6 100644 --- a/src/menu.c +++ b/src/menu.c @@ -20,6 +20,7 @@ */ #include +#include #include "menu.h" #include "main.h" #include "util.h" @@ -152,6 +153,69 @@ menu_icon_expose_func (MetaArea *area, type); } +/* + * Given a Display and an index, get the workspace name and add any + * accelerators. At the moment this means adding a _ if the name is of + * the form "Workspace n" where n is less than 10, and escaping any + * other '_'s so they do not create inadvertant accelerators. + * + * The calling code owns the string, and is reponsible to free the + * memory after use. + */ +static char * +get_workspace_name_with_accel (Display *display, + int index) +{ + char *name; + unsigned int number; + + name = meta_core_get_workspace_name_with_index (display, index); + + /* + * If the name is of the form "Workspace x" where x is an unsigned + * integer, insert a '_' before the number if it is less than 10 and + * return it + */ + if (sscanf (name, _("Workspace %u"), &number) == 1) + { + /* + * Above name is a pointer into the Workspace struct. Here we make + * a copy copy so we can have our wicked way with it. + */ + name = g_strdup_printf (_("Workspace %s%d"), + number < 10 ? "_" : "", + number); + return name; + } + else + { + /* + * Otherwise this is just a normal name to which we cannot really + * add accelerators. Escape any _ characters so that the user's + * workspace names do not get mangled. + */ + char *new_name, *source, *dest; + source = name; + /* + * Assume the worst case, that every character is a _ + */ + dest = new_name = g_malloc0 (strlen (name) * 2); + /* + * Now iterate down the strings, adding '_' to escape as we go + */ + while (*source != '\0') + { + if (*source == '_') + *dest++ = '_'; + *dest++ = *source++; + } + /* + * We don't free *name as we don't own it, and pass ownership of + * *new_name to the calling code. + */ + return new_name; + } +} MetaWindowMenu* meta_window_menu_new (MetaFrames *frames, @@ -290,22 +354,24 @@ meta_window_menu_new (MetaFrames *frames, if (n_workspaces > 0) { GtkWidget *mi; - + Display *display; + + display = gdk_x11_drawable_get_xdisplay(GTK_WIDGET(frames)->window); + i = 0; while (i < n_workspaces) { - char *label; + char *label, *name; MenuData *md; + name = get_workspace_name_with_accel (display, i); if (ops & META_MENU_OP_UNSTICK) - label = g_strdup_printf (_("Only on workspace %s%d"), - i < 9 ? "_" : "", i + 1); + label = g_strdup_printf (_("Only on %s"), name); else - label = g_strdup_printf (_("Move to workspace %s%d"), - i < 9 ? "_" : "", i + 1); - + label = g_strdup_printf(_("Move to %s"), name); mi = gtk_menu_item_new_with_mnemonic (label); + g_free (name); g_free (label); if (!(ops & META_MENU_OP_UNSTICK) &&