a more proper fix for the client list menu trying to activate a closed client, no...
[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) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "openbox.h"
20 #include "menu.h"
21 #include "menuframe.h"
22 #include "action.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 static void desk_menu_update(ObMenuFrame *frame, gpointer data)
41 {
42     ObMenu *menu = frame->menu;
43     DesktopData *d = data;
44     GList *it;
45     gint i;
46     gboolean icons = FALSE;
47     gboolean empty = TRUE;
48
49     menu_clear_entries(menu);
50
51     for (it = focus_order[d->desktop], i = 0; it; it = g_list_next(it), ++i) {
52         ObClient *c = it->data;
53         if (client_normal(c) && !c->skip_taskbar) {
54             GSList *acts = NULL;
55             ObAction* act;
56             ObMenuEntry *e;
57             const ObClientIcon *icon;
58
59             empty = FALSE;
60
61             if (!icons && c->iconic) {
62                 icons = TRUE;
63                 menu_add_separator(menu, -1);
64             }
65
66             act = action_from_string("Activate",
67                                      OB_USER_ACTION_MENU_SELECTION);
68             act->data.activate.any.c = c;
69             acts = g_slist_append(acts, act);
70             act = action_from_string("Desktop",
71                                      OB_USER_ACTION_MENU_SELECTION);
72             act->data.desktop.desk = d->desktop;
73             acts = g_slist_append(acts, act);
74             e = menu_add_normal(menu, i,
75                                 (c->iconic ? c->icon_title : c->title), acts);
76
77             if (config_menu_client_list_icons && (icon = client_icon(c, 32, 32))) {
78                 e->data.normal.icon_width = icon->width;
79                 e->data.normal.icon_height = icon->height;
80                 e->data.normal.icon_data = icon->data;
81             }
82         }
83     }
84
85     if (empty) {
86         /* no entries */
87
88         GSList *acts = NULL;
89         ObAction* act;
90         ObMenuEntry *e;
91
92         act = action_from_string("Desktop", OB_USER_ACTION_MENU_SELECTION);
93         act->data.desktop.desk = d->desktop;
94         acts = g_slist_append(acts, act);
95         e = menu_add_normal(menu, 0, _("Go there..."), acts);
96         if (d->desktop == screen_desktop)
97             e->data.normal.enabled = FALSE;
98     }
99 }
100
101 /* executes it using the client in the actions, since we set that
102    when we make the actions! */
103 static void desk_menu_execute(ObMenuEntry *self, guint state, gpointer data)
104 {
105     ObAction *a;
106
107     if (self->data.normal.actions) {
108         a = self->data.normal.actions->data;
109         action_run(self->data.normal.actions, a->data.any.c, state);
110     }
111 }
112
113 static void desk_menu_destroy(ObMenu *menu, gpointer data)
114 {
115     DesktopData *d = data;
116
117     g_free(d);
118
119     desktop_menus = g_slist_remove(desktop_menus, menu);
120 }
121
122 static void self_update(ObMenuFrame *frame, gpointer data)
123 {
124     ObMenu *menu = frame->menu;
125     guint i;
126     GSList *it, *next;
127     
128     it = desktop_menus;
129     for (i = 0; i < screen_num_desktops; ++i) {
130         if (!it) {
131             ObMenu *submenu;
132             gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
133             DesktopData *data = g_new(DesktopData, 1);
134
135             data->desktop = i;
136             submenu = menu_new(name, screen_desktop_names[i], data);
137             menu_set_update_func(submenu, desk_menu_update);
138             menu_set_execute_func(submenu, desk_menu_execute);
139             menu_set_destroy_func(submenu, desk_menu_destroy);
140
141             menu_add_submenu(menu, i, name);
142
143             g_free(name);
144
145             desktop_menus = g_slist_append(desktop_menus, submenu);
146         } else
147             it = g_slist_next(it);
148     }
149     for (; it; it = next, ++i) {
150         next = g_slist_next(it);
151         menu_free(it->data);
152         desktop_menus = g_slist_delete_link(desktop_menus, it);
153         menu_entry_remove(menu_find_entry_id(menu, i));
154     }
155 }
156
157 static void client_dest(ObClient *client, gpointer data)
158 {
159     /* This concise function removes all references to a closed
160      * client in the client_list_menu, so we don't have to check
161      * in client.c */
162     GSList *it;
163     for (it = desktop_menus; it; it = g_slist_next(it)) {
164         ObMenu *mit = it->data;
165         GList *eit;
166         for (eit = mit->entries; eit; eit = g_list_next(eit)) {
167             ObMenuEntry *meit = eit->data;
168             if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL) {
169                 ObAction *a = meit->data.normal.actions->data;
170                 ObClient *c = a->data.any.c;
171                 if (c == client)
172                     a->data.any.c = NULL;
173             }
174         }
175     }
176 }
177
178 void client_list_menu_startup(gboolean reconfig)
179 {
180     ObMenu *menu;
181
182     if (!reconfig)
183         client_add_destructor(client_dest, NULL);
184
185     menu = menu_new(MENU_NAME, _("Desktops"), NULL);
186     menu_set_update_func(menu, self_update);
187 }
188
189 void client_list_menu_shutdown(gboolean reconfig)
190 {
191     if (!reconfig)
192         client_remove_destructor(client_dest);
193 }