Convert two // to /*
[mikachu/openbox.git] / 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 "client_list_menu.h"
26 #include "focus.h"
27 #include "config.h"
28 #include "gettext.h"
29
30 #include <glib.h>
31
32 #define MENU_NAME "client-list-menu"
33
34 static GSList *desktop_menus;
35
36 typedef struct
37 {
38     guint desktop;
39 } DesktopData;
40
41 #define SEPARATOR -1
42 #define ADD_DESKTOP -2
43 #define REMOVE_DESKTOP -3
44
45 static gboolean desk_menu_update(ObMenuFrame *frame, gpointer data)
46 {
47     ObMenu *menu = frame->menu;
48     DesktopData *d = data;
49     GList *it;
50     gboolean empty = TRUE;
51     gboolean onlyiconic = TRUE;
52
53     menu_clear_entries(menu);
54
55     for (it = focus_order; it; it = g_list_next(it)) {
56         ObClient *c = it->data;
57         if (focus_valid_target(c, d->desktop,
58                                TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE)) {
59             ObMenuEntry *e;
60
61             empty = FALSE;
62
63             if (c->iconic) {
64                 gchar *title = g_strdup_printf("(%s)", c->icon_title);
65                 e = menu_add_normal(menu, d->desktop, title, NULL, FALSE);
66                 g_free(title);
67             } else {
68                 onlyiconic = FALSE;
69                 e = menu_add_normal(menu, d->desktop, c->title, NULL, FALSE);
70             }
71
72             if (config_menu_show_icons) {
73                 e->data.normal.icon = client_icon(c);
74                 RrImageRef(e->data.normal.icon);
75                 e->data.normal.icon_alpha = c->iconic ? OB_ICONIC_ALPHA : 0xff;
76             }
77
78             e->data.normal.data = c;
79         }
80     }
81
82     if (empty || onlyiconic) {
83         ObMenuEntry *e;
84
85         /* no entries or only iconified windows, so add a
86          * way to go to this desktop without uniconifying a window */
87         if (!empty)
88             menu_add_separator(menu, SEPARATOR, NULL);
89
90         e = menu_add_normal(menu, d->desktop, _("Go there..."), NULL, TRUE);
91         if (d->desktop == screen_desktop)
92             e->data.normal.enabled = FALSE;
93     }
94
95     return TRUE; /* always show */
96 }
97
98 static void desk_menu_execute(ObMenuEntry *self, ObMenuFrame *f,
99                               ObClient *c, guint state, gpointer data)
100 {
101     ObClient *t = self->data.normal.data;
102     if (t) { /* it's set to NULL if its destroyed */
103         gboolean here = state & ShiftMask;
104
105         client_activate(t, TRUE, here, TRUE, TRUE, TRUE);
106         /* if the window is omnipresent then we need to go to its
107            desktop */
108         if (!here && t->desktop == DESKTOP_ALL)
109             screen_set_desktop(self->id, FALSE);
110     }
111     else
112         screen_set_desktop(self->id, TRUE);
113 }
114
115 static void desk_menu_destroy(ObMenu *menu, gpointer data)
116 {
117     DesktopData *d = data;
118
119     g_slice_free(DesktopData, d);
120
121     desktop_menus = g_slist_remove(desktop_menus, menu);
122 }
123
124 static void self_cleanup(ObMenu *menu, gpointer data)
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
134 static gboolean self_update(ObMenuFrame *frame, gpointer data)
135 {
136     ObMenu *menu = frame->menu;
137     guint i;
138
139     menu_clear_entries(menu);
140
141     while (desktop_menus) {
142         menu_free(desktop_menus->data);
143         desktop_menus = g_slist_delete_link(desktop_menus, desktop_menus);
144     }
145
146     for (i = 0; i < screen_num_desktops; ++i) {
147         ObMenu *submenu;
148         gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
149         DesktopData *ddata = g_slice_new(DesktopData);
150
151         ddata->desktop = i;
152         submenu = menu_new(name, screen_desktop_names[i], FALSE, ddata);
153         menu_set_update_func(submenu, desk_menu_update);
154         menu_set_execute_func(submenu, desk_menu_execute);
155         menu_set_destroy_func(submenu, desk_menu_destroy);
156
157         menu_add_submenu(menu, i, name);
158
159         g_free(name);
160
161         desktop_menus = g_slist_append(desktop_menus, submenu);
162     }
163
164     if (config_menu_manage_desktops) {
165         menu_add_separator(menu, SEPARATOR, NULL);
166         menu_add_normal(menu, ADD_DESKTOP, _("_Add new desktop"), NULL, TRUE);
167         menu_add_normal(menu, REMOVE_DESKTOP, _("_Remove last desktop"),
168                         NULL, TRUE);
169     }
170
171     return TRUE; /* always show */
172 }
173
174 static void self_execute(ObMenuEntry *self, ObMenuFrame *f,
175                          ObClient *c, guint state, gpointer data)
176 {
177     if (self->id == ADD_DESKTOP) {
178         screen_add_desktop(FALSE);
179         menu_frame_hide_all();
180     }
181     else if (self->id == REMOVE_DESKTOP) {
182         screen_remove_desktop(FALSE);
183         menu_frame_hide_all();
184     }
185 }
186
187 static void client_dest(ObClient *client, gpointer data)
188 {
189     /* This concise function removes all references to a closed
190      * client in the client_list_menu, so we don't have to check
191      * in client.c */
192     GSList *it;
193     for (it = desktop_menus; it; it = g_slist_next(it)) {
194         ObMenu *mit = it->data;
195         GList *eit;
196         for (eit = mit->entries; eit; eit = g_list_next(eit)) {
197             ObMenuEntry *meit = eit->data;
198             if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL &&
199                 meit->data.normal.data == client)
200             {
201                     meit->data.normal.data = NULL;
202             }
203         }
204     }
205 }
206
207 void client_list_menu_startup(gboolean reconfig)
208 {
209     ObMenu *menu;
210
211     if (!reconfig)
212         client_add_destroy_notify(client_dest, NULL);
213
214     menu = menu_new(MENU_NAME, _("Desktops"), TRUE, NULL);
215     menu_set_update_func(menu, self_update);
216     menu_set_cleanup_func(menu, self_cleanup);
217     menu_set_execute_func(menu, self_execute);
218 }
219
220 void client_list_menu_shutdown(gboolean reconfig)
221 {
222     if (!reconfig)
223         client_remove_destroy_notify(client_dest);
224 }