also add the go there entry to client list menus if the only windows on a desktop...
[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 "action.h"
24 #include "screen.h"
25 #include "client.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 static void desk_menu_update(ObMenuFrame *frame, gpointer data)
42 {
43     ObMenu *menu = frame->menu;
44     DesktopData *d = data;
45     GList *it;
46     gint i;
47     gboolean empty = TRUE;
48     gboolean onlyiconic = TRUE;
49
50     menu_clear_entries(menu);
51
52     for (it = focus_order, i = 0; it; it = g_list_next(it), ++i) {
53         ObClient *c = it->data;
54         if (client_normal(c) && (!c->skip_taskbar || c->iconic) &&
55             (c->desktop == d->desktop || c->desktop == DESKTOP_ALL))
56         {
57             GSList *acts = NULL;
58             ObAction* act;
59             ObMenuEntry *e;
60             const ObClientIcon *icon;
61
62             empty = FALSE;
63
64             act = action_from_string("Activate",
65                                      OB_USER_ACTION_MENU_SELECTION);
66             act->data.activate.any.c = c;
67             acts = g_slist_append(acts, act);
68             act = action_from_string("Desktop",
69                                      OB_USER_ACTION_MENU_SELECTION);
70             act->data.desktop.desk = d->desktop;
71             acts = g_slist_append(acts, act);
72
73             if (c->iconic) {
74                 gchar *title = g_strdup_printf("(%s)", c->icon_title);
75                 e = menu_add_normal(menu, i, title, acts, FALSE);
76                 g_free(title);
77             } else {
78                 onlyiconic = FALSE;
79                 e = menu_add_normal(menu, i, c->title, acts, FALSE);
80             }
81
82             if (config_menu_client_list_icons
83                 && (icon = client_icon(c, 32, 32))) {
84                 e->data.normal.icon_width = icon->width;
85                 e->data.normal.icon_height = icon->height;
86                 e->data.normal.icon_data = icon->data;
87             }
88         }
89     }
90
91     if (empty || onlyiconic) {
92         /* no entries or only iconified windows, so add a
93          * way to go to this desktop without uniconifying a window */
94         if (!empty)
95             menu_add_separator(menu, -1, NULL);
96
97         GSList *acts = NULL;
98         ObAction* act;
99         ObMenuEntry *e;
100
101         act = action_from_string("Desktop", OB_USER_ACTION_MENU_SELECTION);
102         act->data.desktop.desk = d->desktop;
103         acts = g_slist_append(acts, act);
104         e = menu_add_normal(menu, 0, _("Go there..."), acts, TRUE);
105         if (d->desktop == screen_desktop)
106             e->data.normal.enabled = FALSE;
107     }
108 }
109
110 /* executes it using the client in the actions, since we set that
111    when we make the actions! */
112 static void desk_menu_execute(ObMenuEntry *self, guint state, gpointer data,
113                               Time time)
114 {
115     ObAction *a;
116
117     if (self->data.normal.actions) {
118         a = self->data.normal.actions->data;
119         action_run(self->data.normal.actions, a->data.any.c, state, time);
120     }
121 }
122
123 static void desk_menu_destroy(ObMenu *menu, gpointer data)
124 {
125     DesktopData *d = data;
126
127     g_free(d);
128
129     desktop_menus = g_slist_remove(desktop_menus, menu);
130 }
131
132 static void self_update(ObMenuFrame *frame, gpointer data)
133 {
134     ObMenu *menu = frame->menu;
135     guint i;
136     GSList *it, *next;
137     
138     it = desktop_menus;
139     for (i = 0; i < screen_num_desktops; ++i) {
140         if (!it) {
141             ObMenu *submenu;
142             gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
143             DesktopData *data = g_new(DesktopData, 1);
144
145             data->desktop = i;
146             submenu = menu_new(name, screen_desktop_names[i], FALSE, data);
147             menu_set_update_func(submenu, desk_menu_update);
148             menu_set_execute_func(submenu, desk_menu_execute);
149             menu_set_destroy_func(submenu, desk_menu_destroy);
150
151             menu_add_submenu(menu, i, name);
152
153             g_free(name);
154
155             desktop_menus = g_slist_append(desktop_menus, submenu);
156         } else
157             it = g_slist_next(it);
158     }
159     for (; it; it = next, ++i) {
160         next = g_slist_next(it);
161         menu_free(it->data);
162         desktop_menus = g_slist_delete_link(desktop_menus, it);
163         menu_entry_remove(menu_find_entry_id(menu, i));
164     }
165 }
166
167 static void client_dest(ObClient *client, gpointer data)
168 {
169     /* This concise function removes all references to a closed
170      * client in the client_list_menu, so we don't have to check
171      * in client.c */
172     GSList *it;
173     for (it = desktop_menus; it; it = g_slist_next(it)) {
174         ObMenu *mit = it->data;
175         GList *eit;
176         for (eit = mit->entries; eit; eit = g_list_next(eit)) {
177             ObMenuEntry *meit = eit->data;
178             if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL) {
179                 ObAction *a = meit->data.normal.actions->data;
180                 ObClient *c = a->data.any.c;
181                 if (c == client)
182                     a->data.any.c = NULL;
183             }
184         }
185     }
186 }
187
188 void client_list_menu_startup(gboolean reconfig)
189 {
190     ObMenu *menu;
191
192     if (!reconfig)
193         client_add_destructor(client_dest, NULL);
194
195     menu = menu_new(MENU_NAME, _("Desktops"), TRUE, NULL);
196     menu_set_update_func(menu, self_update);
197 }
198
199 void client_list_menu_shutdown(gboolean reconfig)
200 {
201     if (!reconfig)
202         client_remove_destructor(client_dest);
203 }