]> icculus.org git repositories - dana/openbox.git/blob - openbox/client_list_menu.c
config option for icons in desktop menus
[dana/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 "gettext.h"
27
28 #include <glib.h>
29
30 #define MENU_NAME "client-list-menu"
31
32 static GSList *desktop_menus;
33
34 typedef struct
35 {
36     guint desktop;
37 } DesktopData;
38
39 static void desk_menu_update(ObMenuFrame *frame, gpointer data)
40 {
41     ObMenu *menu = frame->menu;
42     DesktopData *d = data;
43     GList *it;
44     gint i;
45     gboolean icons = FALSE;
46     gboolean empty = TRUE;
47
48     menu_clear_entries(menu);
49
50     for (it = focus_order[d->desktop], i = 0; it; it = g_list_next(it), ++i) {
51         ObClient *c = it->data;
52         if (client_normal(c) && !c->skip_taskbar) {
53             GSList *acts = NULL;
54             ObAction* act;
55             ObMenuEntry *e;
56             const ObClientIcon *icon;
57
58             empty = FALSE;
59
60             if (!icons && c->iconic) {
61                 icons = TRUE;
62                 menu_add_separator(menu, -1);
63             }
64
65             act = action_from_string("Activate",
66                                      OB_USER_ACTION_MENU_SELECTION);
67             act->data.activate.any.c = c;
68             acts = g_slist_append(acts, act);
69             act = action_from_string("Desktop",
70                                      OB_USER_ACTION_MENU_SELECTION);
71             act->data.desktop.desk = d->desktop;
72             acts = g_slist_append(acts, act);
73             e = menu_add_normal(menu, i,
74                                 (c->iconic ? c->icon_title : c->title), acts);
75
76             if (config_menu_client_list_icons && (icon = client_icon(c, 32, 32))) {
77                 e->data.normal.icon_width = icon->width;
78                 e->data.normal.icon_height = icon->height;
79                 e->data.normal.icon_data = icon->data;
80             }
81         }
82     }
83
84     if (empty) {
85         /* no entries */
86
87         GSList *acts = NULL;
88         ObAction* act;
89         ObMenuEntry *e;
90
91         act = action_from_string("Desktop", OB_USER_ACTION_MENU_SELECTION);
92         act->data.desktop.desk = d->desktop;
93         acts = g_slist_append(acts, act);
94         e = menu_add_normal(menu, 0, _("Go there..."), acts);
95         if (d->desktop == screen_desktop)
96             e->data.normal.enabled = FALSE;
97     }
98 }
99
100 /* executes it using the client in the actions, since we set that
101    when we make the actions! */
102 static void desk_menu_execute(ObMenuEntry *self, guint state, gpointer data)
103 {
104     ObAction *a;
105
106     if (self->data.normal.actions) {
107         a = self->data.normal.actions->data;
108         action_run(self->data.normal.actions, a->data.any.c, state);
109     }
110 }
111
112 static void desk_menu_destroy(ObMenu *menu, gpointer data)
113 {
114     DesktopData *d = data;
115
116     g_free(d);
117
118     desktop_menus = g_slist_remove(desktop_menus, menu);
119 }
120
121 static void self_update(ObMenuFrame *frame, gpointer data)
122 {
123     ObMenu *menu = frame->menu;
124     guint i;
125     GSList *it, *next;
126     
127     it = desktop_menus;
128     for (i = 0; i < screen_num_desktops; ++i) {
129         if (!it) {
130             ObMenu *submenu;
131             gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
132             DesktopData *data = g_new(DesktopData, 1);
133
134             data->desktop = i;
135             submenu = menu_new(name, screen_desktop_names[i], data);
136             menu_set_update_func(submenu, desk_menu_update);
137             menu_set_execute_func(submenu, desk_menu_execute);
138             menu_set_destroy_func(submenu, desk_menu_destroy);
139
140             menu_add_submenu(menu, i, name);
141
142             g_free(name);
143
144             desktop_menus = g_slist_append(desktop_menus, submenu);
145         } else
146             it = g_slist_next(it);
147     }
148     for (; it; it = next, ++i) {
149         next = g_slist_next(it);
150         menu_free(it->data);
151         desktop_menus = g_slist_delete_link(desktop_menus, it);
152         menu_entry_remove(menu_find_entry_id(menu, i));
153     }
154 }
155
156 void client_list_menu_startup()
157 {
158     ObMenu *menu;
159
160     menu = menu_new(MENU_NAME, _("Desktops"), NULL);
161     menu_set_update_func(menu, self_update);
162 }