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.
22 #include "menuframe.h"
25 #include "client_list_menu.h"
32 #define MENU_NAME "client-list-menu"
34 static GSList *desktop_menus;
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;
50 gboolean empty = TRUE;
51 gboolean onlyiconic = TRUE;
52 gboolean noicons = TRUE;
54 menu_clear_entries(menu);
56 for (it = focus_order; it; it = g_list_next(it)) {
57 ObClient *c = it->data;
58 if (focus_valid_target(c, d->desktop,
59 TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE)) {
66 menu_add_separator(menu, -1, NULL);
69 e = menu_add_normal(menu, d->desktop, c->icon_title, NULL, FALSE);
72 e = menu_add_normal(menu, d->desktop, c->title, NULL, FALSE);
75 if (config_menu_show_icons) {
76 e->data.normal.icon = client_icon(c);
77 RrImageRef(e->data.normal.icon);
78 e->data.normal.icon_alpha = c->iconic ? OB_ICONIC_ALPHA : 0xff;
81 e->data.normal.data = c;
85 if (empty || onlyiconic) {
88 /* no entries or only iconified windows, so add a
89 * way to go to this desktop without uniconifying a window */
91 menu_add_separator(menu, SEPARATOR, NULL);
93 e = menu_add_normal(menu, d->desktop, _("Go there..."), NULL, TRUE);
94 if (d->desktop == screen_desktop)
95 e->data.normal.enabled = FALSE;
98 return TRUE; /* always show */
101 static void desk_menu_execute(ObMenuEntry *self, ObMenuFrame *f,
102 ObClient *c, guint state, gpointer data)
104 ObClient *t = self->data.normal.data;
105 if (t && !t->locked) { /* it's set to NULL if its destroyed */
106 gboolean here = state & ShiftMask;
108 client_activate(t, TRUE, here, TRUE, TRUE, TRUE);
109 /* if the window is omnipresent then we need to go to its
111 if (!here && t->desktop == DESKTOP_ALL)
112 screen_set_desktop(self->id, FALSE);
115 screen_set_desktop(self->id, TRUE);
118 static void desk_menu_destroy(ObMenu *menu, gpointer data)
120 DesktopData *d = data;
122 g_slice_free(DesktopData, d);
124 desktop_menus = g_slist_remove(desktop_menus, menu);
127 static void self_cleanup(ObMenu *menu, gpointer data)
129 menu_clear_entries(menu);
131 while (desktop_menus) {
132 menu_free(desktop_menus->data);
133 desktop_menus = g_slist_delete_link(desktop_menus, desktop_menus);
137 static gboolean self_update(ObMenuFrame *frame, gpointer data)
139 ObMenu *menu = frame->menu;
142 menu_clear_entries(menu);
144 while (desktop_menus) {
145 menu_free(desktop_menus->data);
146 desktop_menus = g_slist_delete_link(desktop_menus, desktop_menus);
149 for (i = 0; i < screen_num_desktops; ++i) {
151 gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
152 DesktopData *ddata = g_slice_new(DesktopData);
155 submenu = menu_new(name, screen_desktop_names[i], FALSE, ddata);
156 menu_set_update_func(submenu, desk_menu_update);
157 menu_set_execute_func(submenu, desk_menu_execute);
158 menu_set_destroy_func(submenu, desk_menu_destroy);
160 menu_add_submenu(menu, i, name);
164 desktop_menus = g_slist_append(desktop_menus, submenu);
167 if (config_menu_manage_desktops) {
168 menu_add_separator(menu, SEPARATOR, NULL);
169 menu_add_normal(menu, ADD_DESKTOP, _("_Add new desktop"), NULL, TRUE);
170 menu_add_normal(menu, REMOVE_DESKTOP, _("_Remove last desktop"),
174 return TRUE; /* always show */
177 static void self_execute(ObMenuEntry *self, ObMenuFrame *f,
178 ObClient *c, guint state, gpointer data)
180 if (self->id == ADD_DESKTOP) {
181 screen_add_desktop(FALSE);
182 menu_frame_hide_all();
184 else if (self->id == REMOVE_DESKTOP) {
185 screen_remove_desktop(FALSE);
186 menu_frame_hide_all();
190 static void client_dest(ObClient *client, gpointer data)
192 /* This concise function removes all references to a closed
193 * client in the client_list_menu, so we don't have to check
196 for (it = desktop_menus; it; it = g_slist_next(it)) {
197 ObMenu *mit = it->data;
199 for (eit = mit->entries; eit; eit = g_list_next(eit)) {
200 ObMenuEntry *meit = eit->data;
201 if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL &&
202 meit->data.normal.data == client)
204 meit->data.normal.data = NULL;
210 void client_list_menu_startup(gboolean reconfig)
215 client_add_destroy_notify(client_dest, NULL);
217 menu = menu_new(MENU_NAME, _("Desktops"), TRUE, NULL);
218 menu_set_update_func(menu, self_update);
219 menu_set_cleanup_func(menu, self_cleanup);
220 menu_set_execute_func(menu, self_execute);
223 void client_list_menu_shutdown(gboolean reconfig)
226 client_remove_destroy_notify(client_dest);