]> Dogcows Code - chaz/openbox/blob - openbox/client_list_menu.c
add shortcuts to add/remove desktops in the client list menus.
[chaz/openbox] / openbox / client_list_menu.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 client_list_menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
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.
11
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.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "openbox.h"
21 #include "menu.h"
22 #include "menuframe.h"
23 #include "screen.h"
24 #include "client.h"
25 #include "focus.h"
26 #include "config.h"
27 #include "gettext.h"
28
29 #include <glib.h>
30
31 #define MENU_NAME "client-list-menu"
32
33 static GSList *desktop_menus;
34
35 typedef struct
36 {
37 guint desktop;
38 } DesktopData;
39
40 #define CLIENT -1
41 #define ADD_DESKTOP -2
42 #define REMOVE_DESKTOP -3
43
44 static gboolean desk_menu_update(ObMenuFrame *frame, gpointer data)
45 {
46 ObMenu *menu = frame->menu;
47 DesktopData *d = data;
48 GList *it;
49 gboolean empty = TRUE;
50 gboolean onlyiconic = TRUE;
51
52 menu_clear_entries(menu);
53
54 for (it = focus_order; it; it = g_list_next(it)) {
55 ObClient *c = it->data;
56 if (client_normal(c) && (!c->skip_taskbar || c->iconic) &&
57 (c->desktop == d->desktop || c->desktop == DESKTOP_ALL))
58 {
59 ObMenuEntry *e;
60 const ObClientIcon *icon;
61
62 empty = FALSE;
63
64 if (c->iconic) {
65 gchar *title = g_strdup_printf("(%s)", c->icon_title);
66 e = menu_add_normal(menu, CLIENT, title, NULL, FALSE);
67 g_free(title);
68 } else {
69 onlyiconic = FALSE;
70 e = menu_add_normal(menu, CLIENT, c->title, NULL, FALSE);
71 }
72
73 if (config_menu_client_list_icons
74 && (icon = client_icon(c, 32, 32))) {
75 e->data.normal.icon_width = icon->width;
76 e->data.normal.icon_height = icon->height;
77 e->data.normal.icon_data = icon->data;
78 e->data.normal.icon_alpha = c->iconic ? OB_ICONIC_ALPHA : 0xff;
79 }
80
81 e->data.normal.data = c;
82 }
83 }
84
85 if (empty || onlyiconic) {
86 ObMenuEntry *e;
87
88 /* no entries or only iconified windows, so add a
89 * way to go to this desktop without uniconifying a window */
90 if (!empty)
91 menu_add_separator(menu, CLIENT, NULL);
92
93 e = menu_add_normal(menu, d->desktop, _("Go there..."), NULL, TRUE);
94 if (d->desktop == screen_desktop)
95 e->data.normal.enabled = FALSE;
96 }
97
98 return TRUE; /* always show */
99 }
100
101 static void desk_menu_execute(ObMenuEntry *self, ObMenuFrame *f,
102 ObClient *c, guint state, gpointer data)
103 {
104 if (self->id == CLIENT) {
105 if (self->data.normal.data) /* it's set to NULL if its destroyed */
106 client_activate(self->data.normal.data, FALSE, TRUE, TRUE, TRUE);
107 }
108 else
109 screen_set_desktop(self->id, TRUE);
110 }
111
112 static void desk_menu_destroy(ObMenu *menu, gpointer data)
113 {
114 DesktopData *d = data;
115
116 g_free(d);
117
118 desktop_menus = g_slist_remove(desktop_menus, menu);
119 }
120
121 static gboolean self_update(ObMenuFrame *frame, gpointer data)
122 {
123 ObMenu *menu = frame->menu;
124 guint i;
125
126 menu_clear_entries(menu);
127
128 while (desktop_menus) {
129 menu_free(desktop_menus->data);
130 desktop_menus = g_slist_delete_link(desktop_menus, desktop_menus);
131 }
132
133 for (i = 0; i < screen_num_desktops; ++i) {
134 ObMenu *submenu;
135 gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
136 DesktopData *data = g_new(DesktopData, 1);
137
138 data->desktop = i;
139 submenu = menu_new(name, screen_desktop_names[i], FALSE, data);
140 menu_set_update_func(submenu, desk_menu_update);
141 menu_set_execute_func(submenu, desk_menu_execute);
142 menu_set_destroy_func(submenu, desk_menu_destroy);
143
144 menu_add_submenu(menu, i, name);
145
146 g_free(name);
147
148 desktop_menus = g_slist_append(desktop_menus, submenu);
149 }
150
151 menu_add_separator(menu, CLIENT, NULL);
152 menu_add_normal(menu, ADD_DESKTOP, _("&Add new desktop"), NULL, TRUE);
153 menu_add_normal(menu, REMOVE_DESKTOP, _("&Remove last desktop"),
154 NULL, TRUE);
155
156 return TRUE; /* always show */
157 }
158
159 static void self_execute(ObMenuEntry *self, ObMenuFrame *f,
160 ObClient *c, guint state, gpointer data)
161 {
162 if (self->id == ADD_DESKTOP) {
163 screen_add_desktop(FALSE);
164 menu_frame_hide_all();
165 }
166 else if (self->id == REMOVE_DESKTOP) {
167 screen_remove_desktop(FALSE);
168 menu_frame_hide_all();
169 }
170 }
171
172 static void client_dest(ObClient *client, gpointer data)
173 {
174 /* This concise function removes all references to a closed
175 * client in the client_list_menu, so we don't have to check
176 * in client.c */
177 GSList *it;
178 for (it = desktop_menus; it; it = g_slist_next(it)) {
179 ObMenu *mit = it->data;
180 GList *eit;
181 for (eit = mit->entries; eit; eit = g_list_next(eit)) {
182 ObMenuEntry *meit = eit->data;
183 if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL &&
184 meit->data.normal.data == client)
185 {
186 meit->data.normal.data = NULL;
187 }
188 }
189 }
190 }
191
192 void client_list_menu_startup(gboolean reconfig)
193 {
194 ObMenu *menu;
195
196 if (!reconfig)
197 client_add_destroy_notify(client_dest, NULL);
198
199 menu = menu_new(MENU_NAME, _("Desktops"), TRUE, NULL);
200 menu_set_update_func(menu, self_update);
201 menu_set_execute_func(menu, self_execute);
202 }
203
204 void client_list_menu_shutdown(gboolean reconfig)
205 {
206 if (!reconfig)
207 client_remove_destroy_notify(client_dest);
208 }
This page took 0.049819 seconds and 5 git commands to generate.