]> icculus.org git repositories - dana/openbox.git/blob - openbox/client_list_menu.c
9b904f4dc6fe6c184d926a5c772e160206812548
[dana/openbox.git] / openbox / client_list_menu.c
1 #include "openbox.h"
2 #include "menu.h"
3 #include "menuframe.h"
4 #include "action.h"
5 #include "screen.h"
6 #include "client.h"
7 #include "focus.h"
8 #include "gettext.h"
9
10 #include <glib.h>
11
12 #define MENU_NAME "client-list-menu"
13
14 static GSList *desktop_menus;
15
16 typedef struct {
17     guint desktop;
18 } DesktopData;
19
20 static void desk_menu_update(ObMenuFrame *frame, gpointer data)
21 {
22     ObMenu *menu = frame->menu;
23     DesktopData *d = data;
24     GList *it;
25     gint i;
26     gboolean icons = FALSE;
27
28     menu_clear_entries(menu);
29
30     for (it = focus_order[d->desktop], i = 0; it; it = g_list_next(it), ++i) {
31         ObClient *c = it->data;
32         if (client_normal(c)) {
33             GSList *acts;
34             ObAction* act;
35             ObMenuEntry *e;
36             ObClientIcon *icon;
37
38             if (!icons && c->iconic) {
39                 icons = TRUE;
40                 menu_add_separator(menu, -1);
41             }
42
43             act = action_from_string("Activate",
44                                      OB_USER_ACTION_MENU_SELECTION);
45             act->data.activate.any.c = c;
46             acts = g_slist_prepend(NULL, act);
47             e = menu_add_normal(menu, i,
48                                 (c->iconic ? c->icon_title : c->title), acts);
49
50             if ((icon = client_icon(c, 32, 32))) {
51                 e->data.normal.icon_width = icon->width;
52                 e->data.normal.icon_height = icon->height;
53                 e->data.normal.icon_data = icon->data;
54             }
55         }
56     }
57     
58 }
59
60 /* executes it using the client in the actions, since we set that
61    when we make the actions! */
62 static void desk_menu_execute(ObMenuEntry *self, guint state, gpointer data)
63 {
64     GSList *it;
65
66     for (it = self->data.normal.actions; it; it = g_slist_next(it))
67     {
68         ObAction *act = it->data;
69         action_run(it->data, act->data.any.c, state);
70     }
71 }
72
73 static void desk_menu_destroy(ObMenu *menu, gpointer data)
74 {
75     DesktopData *d = data;
76
77     g_free(d);
78
79     desktop_menus = g_slist_remove(desktop_menus, menu);
80 }
81
82 static void self_update(ObMenuFrame *frame, gpointer data)
83 {
84     ObMenu *menu = frame->menu;
85     guint i;
86     GSList *it, *next;
87     
88     it = desktop_menus;
89     for (i = 0; i < screen_num_desktops; ++i) {
90         if (!it) {
91             ObMenu *submenu;
92             gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
93             DesktopData *data = g_new(DesktopData, 1);
94
95             data->desktop = i;
96             submenu = menu_new(name, screen_desktop_names[i], data);
97             menu_set_update_func(submenu, desk_menu_update);
98             menu_set_execute_func(submenu, desk_menu_execute);
99             menu_set_destroy_func(submenu, desk_menu_destroy);
100
101             menu_add_submenu(menu, i, name);
102
103             g_free(name);
104
105             desktop_menus = g_slist_append(desktop_menus, submenu);
106         } else
107             it = g_slist_next(it);
108     }
109     for (; it; it = next, ++i) {
110         next = g_slist_next(it);
111         menu_free(it->data);
112         desktop_menus = g_slist_delete_link(desktop_menus, it);
113         menu_entry_remove(menu_find_entry_id(menu, i));
114     }
115 }
116
117 void client_list_menu_startup()
118 {
119     ObMenu *menu;
120
121     menu = menu_new(MENU_NAME, _("Desktops"), NULL);
122     menu_set_update_func(menu, self_update);
123 }