972780793ea5d33a60dbf4a18bec4553c6850058
[mikachu/openbox.git] / openbox / client_list_menu.c
1 /* -*- indent-tabs-mode: t; 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     guint desktop;
36 } DesktopData;
37
38 static void desk_menu_update(ObMenuFrame *frame, gpointer data)
39 {
40     ObMenu *menu = frame->menu;
41     DesktopData *d = data;
42     GList *it;
43     gint i;
44     gboolean icons = FALSE;
45
46     menu_clear_entries(menu);
47
48     for (it = focus_order[d->desktop], i = 0; it; it = g_list_next(it), ++i) {
49         ObClient *c = it->data;
50         if (client_normal(c)) {
51             GSList *acts;
52             ObAction* act;
53             ObMenuEntry *e;
54             ObClientIcon *icon;
55
56             if (!icons && c->iconic) {
57                 icons = TRUE;
58                 menu_add_separator(menu, -1);
59             }
60
61             act = action_from_string("Activate",
62                                      OB_USER_ACTION_MENU_SELECTION);
63             act->data.activate.any.c = c;
64             acts = g_slist_prepend(NULL, act);
65             e = menu_add_normal(menu, i,
66                                 (c->iconic ? c->icon_title : c->title), acts);
67
68             if ((icon = client_icon(c, 32, 32))) {
69                 e->data.normal.icon_width = icon->width;
70                 e->data.normal.icon_height = icon->height;
71                 e->data.normal.icon_data = icon->data;
72             }
73         }
74     }
75     
76 }
77
78 /* executes it using the client in the actions, since we set that
79    when we make the actions! */
80 static void desk_menu_execute(ObMenuEntry *self, guint state, gpointer data)
81 {
82     GSList *it;
83
84     for (it = self->data.normal.actions; it; it = g_slist_next(it))
85     {
86         ObAction *act = it->data;
87         action_run(it->data, act->data.any.c, state);
88     }
89 }
90
91 static void desk_menu_destroy(ObMenu *menu, gpointer data)
92 {
93     DesktopData *d = data;
94
95     g_free(d);
96
97     desktop_menus = g_slist_remove(desktop_menus, menu);
98 }
99
100 static void self_update(ObMenuFrame *frame, gpointer data)
101 {
102     ObMenu *menu = frame->menu;
103     guint i;
104     GSList *it, *next;
105     
106     it = desktop_menus;
107     for (i = 0; i < screen_num_desktops; ++i) {
108         if (!it) {
109             ObMenu *submenu;
110             gchar *name = g_strdup_printf("%s-%u", MENU_NAME, i);
111             DesktopData *data = g_new(DesktopData, 1);
112
113             data->desktop = i;
114             submenu = menu_new(name, screen_desktop_names[i], data);
115             menu_set_update_func(submenu, desk_menu_update);
116             menu_set_execute_func(submenu, desk_menu_execute);
117             menu_set_destroy_func(submenu, desk_menu_destroy);
118
119             menu_add_submenu(menu, i, name);
120
121             g_free(name);
122
123             desktop_menus = g_slist_append(desktop_menus, submenu);
124         } else
125             it = g_slist_next(it);
126     }
127     for (; it; it = next, ++i) {
128         next = g_slist_next(it);
129         menu_free(it->data);
130         desktop_menus = g_slist_delete_link(desktop_menus, it);
131         menu_entry_remove(menu_find_entry_id(menu, i));
132     }
133 }
134
135 void client_list_menu_startup()
136 {
137     ObMenu *menu;
138
139     menu = menu_new(MENU_NAME, _("Desktops"), NULL);
140     menu_set_update_func(menu, self_update);
141 }