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