Add the manageDesktops option.
[openbox.git] / openbox / client_list_menu.c
blobe6521a0aafe8761dcce6a2b392dbe2139af69d9e
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 client_list_menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "openbox.h"
21 #include "menu.h"
22 #include "menuframe.h"
23 #include "screen.h"
24 #include "client.h"
25 #include "client_list_menu.h"
26 #include "focus.h"
27 #include "config.h"
28 #include "gettext.h"
30 #include <glib.h>
32 #define MENU_NAME "client-list-menu"
34 static GSList *desktop_menus;
36 typedef struct
38 guint desktop;
39 } DesktopData;
41 #define SEPARATOR -1
42 #define ADD_DESKTOP -2
43 #define REMOVE_DESKTOP -3
45 static gboolean desk_menu_update(ObMenuFrame *frame, gpointer data)
47 ObMenu *menu = frame->menu;
48 DesktopData *d = data;
49 GList *it;
50 gboolean empty = TRUE;
51 gboolean onlyiconic = TRUE;
53 menu_clear_entries(menu);
55 for (it = focus_order; it; it = g_list_next(it)) {
56 ObClient *c = it->data;
57 if (client_normal(c) && (!c->skip_taskbar || c->iconic) &&
58 (c->desktop == d->desktop || c->desktop == DESKTOP_ALL))
60 ObMenuEntry *e;
61 const ObClientIcon *icon;
63 empty = FALSE;
65 if (c->iconic) {
66 gchar *title = g_strdup_printf("(%s)", c->icon_title);
67 e = menu_add_normal(menu, d->desktop, title, NULL, FALSE);
68 g_free(title);
69 } else {
70 onlyiconic = FALSE;
71 e = menu_add_normal(menu, d->desktop, c->title, NULL, FALSE);
74 if (config_menu_client_list_icons
75 && (icon = client_icon(c, 32, 32))) {
76 e->data.normal.icon_width = icon->width;
77 e->data.normal.icon_height = icon->height;
78 e->data.normal.icon_data = icon->data;
79 e->data.normal.icon_alpha = c->iconic ? OB_ICONIC_ALPHA : 0xff;
82 e->data.normal.data = c;
86 if (empty || onlyiconic) {
87 ObMenuEntry *e;
89 /* no entries or only iconified windows, so add a
90 * way to go to this desktop without uniconifying a window */
91 if (!empty)
92 menu_add_separator(menu, SEPARATOR, NULL);
94 e = menu_add_normal(menu, d->desktop, _("Go there..."), NULL, TRUE);
95 if (d->desktop == screen_desktop)
96 e->data.normal.enabled = FALSE;
99 return TRUE; /* always show */
102 static void desk_menu_execute(ObMenuEntry *self, ObMenuFrame *f,
103 ObClient *c, guint state, gpointer data)
105 ObClient *t = self->data.normal.data;
106 if (t) { /* it's set to NULL if its destroyed */
107 client_activate(t, FALSE, TRUE, TRUE, TRUE);
108 /* if the window is omnipresent then we need to go to its
109 desktop */
110 if (t->desktop == DESKTOP_ALL)
111 screen_set_desktop(self->id, FALSE);
113 else
114 screen_set_desktop(self->id, TRUE);
117 static void desk_menu_destroy(ObMenu *menu, gpointer data)
119 DesktopData *d = data;
121 g_free(d);
123 desktop_menus = g_slist_remove(desktop_menus, menu);
126 static gboolean self_update(ObMenuFrame *frame, gpointer data)
128 ObMenu *menu = frame->menu;
129 guint i;
131 menu_clear_entries(menu);
133 while (desktop_menus) {
134 menu_free(desktop_menus->data);
135 desktop_menus = g_slist_delete_link(desktop_menus, desktop_menus);
138 for (i = 0; i < screen_num_desktops; ++i) {
139 ObMenu *submenu;
140 gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
141 DesktopData *ddata = g_new(DesktopData, 1);
143 ddata->desktop = i;
144 submenu = menu_new(name, screen_desktop_names[i], FALSE, ddata);
145 menu_set_update_func(submenu, desk_menu_update);
146 menu_set_execute_func(submenu, desk_menu_execute);
147 menu_set_destroy_func(submenu, desk_menu_destroy);
149 menu_add_submenu(menu, i, name);
151 g_free(name);
153 desktop_menus = g_slist_append(desktop_menus, submenu);
156 if (config_menu_manage_desktops) {
157 menu_add_separator(menu, SEPARATOR, NULL);
158 menu_add_normal(menu, ADD_DESKTOP, _("_Add new desktop"), NULL, TRUE);
159 menu_add_normal(menu, REMOVE_DESKTOP, _("_Remove last desktop"),
160 NULL, TRUE);
163 return TRUE; /* always show */
166 static void self_execute(ObMenuEntry *self, ObMenuFrame *f,
167 ObClient *c, guint state, gpointer data)
169 if (self->id == ADD_DESKTOP) {
170 screen_add_desktop(FALSE);
171 menu_frame_hide_all();
173 else if (self->id == REMOVE_DESKTOP) {
174 screen_remove_desktop(FALSE);
175 menu_frame_hide_all();
179 static void client_dest(ObClient *client, gpointer data)
181 /* This concise function removes all references to a closed
182 * client in the client_list_menu, so we don't have to check
183 * in client.c */
184 GSList *it;
185 for (it = desktop_menus; it; it = g_slist_next(it)) {
186 ObMenu *mit = it->data;
187 GList *eit;
188 for (eit = mit->entries; eit; eit = g_list_next(eit)) {
189 ObMenuEntry *meit = eit->data;
190 if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL &&
191 meit->data.normal.data == client)
193 meit->data.normal.data = NULL;
199 void client_list_menu_startup(gboolean reconfig)
201 ObMenu *menu;
203 if (!reconfig)
204 client_add_destroy_notify(client_dest, NULL);
206 menu = menu_new(MENU_NAME, _("Desktops"), TRUE, NULL);
207 menu_set_update_func(menu, self_update);
208 menu_set_execute_func(menu, self_execute);
211 void client_list_menu_shutdown(gboolean reconfig)
213 if (!reconfig)
214 client_remove_destroy_notify(client_dest);