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