only refocus the omnipresent window if its allowed
[mikachu/openbox.git] / openbox / client_list_combined_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-combined-menu"
33
34 ObMenu *combined_menu;
35
36 static void self_update(ObMenuFrame *frame, gpointer data)
37 {
38     ObMenu *menu = frame->menu;
39     ObMenuEntry *e;
40     GList *it;
41     gint i;
42     gboolean icons = FALSE;
43     guint desktop;
44
45     menu_clear_entries(menu);
46
47     for (desktop = 0; desktop < screen_num_desktops; desktop++) {
48         gboolean empty = TRUE;
49
50         /* Don't need a separator at the very top */
51         if (desktop > 0)
52             menu_add_separator(menu, -1);
53         e = menu_add_normal(menu, -1, NULL, NULL);
54         e->data.normal.enabled = FALSE;
55         e->data.normal.label = g_strdup(screen_desktop_names[desktop]);
56         /* The one at the bottom will always have entries below it though */
57         menu_add_separator(menu, -1);
58         for (it = focus_order, i = 0; it; it = g_list_next(it), ++i) {
59             ObClient *c = it->data;
60             if (client_normal(c) && (!c->skip_taskbar || c->iconic) &&
61                 (c->desktop == desktop || c->desktop == DESKTOP_ALL))
62             {
63                 GSList *acts = NULL;
64                 ObAction* act;
65                 const ObClientIcon *icon;
66
67                 empty = FALSE;
68
69                 if (!icons && c->iconic) {
70                     icons = TRUE;
71                     menu_add_separator(menu, -1);
72                 }
73
74                 act = action_from_string("Activate",
75                                          OB_USER_ACTION_MENU_SELECTION);
76                 act->data.activate.any.c = c;
77                 acts = g_slist_append(acts, act);
78                 act = action_from_string("Desktop",
79                                          OB_USER_ACTION_MENU_SELECTION);
80                 act->data.desktop.desk = desktop;
81                 acts = g_slist_append(acts, act);
82                 e = menu_add_normal(menu, i, (c->iconic ?
83                                               c->icon_title : c->title), acts);
84
85                 if (config_menu_client_list_icons
86                         && (icon = client_icon(c, 32, 32))) {
87                     e->data.normal.icon_width = icon->width;
88                     e->data.normal.icon_height = icon->height;
89                     e->data.normal.icon_data = icon->data;
90                 }
91             }
92         }
93         icons = FALSE;
94
95         if (empty) {
96             /* no entries */
97
98             GSList *acts = NULL;
99             ObAction* act;
100             ObMenuEntry *e;
101
102             act = action_from_string("Desktop", OB_USER_ACTION_MENU_SELECTION);
103             act->data.desktop.desk = desktop;
104             acts = g_slist_append(acts, act);
105             e = menu_add_normal(menu, 0, _("Go there..."), acts);
106             if (desktop == screen_desktop)
107                 e->data.normal.enabled = FALSE;
108         }
109     }
110 }
111
112 /* executes it using the client in the actions, since we set that
113    when we make the actions! */
114 static void menu_execute(ObMenuEntry *self, guint state, gpointer data,
115                          Time time)
116 {
117     ObAction *a;
118
119     if (self->data.normal.actions) {
120         a = self->data.normal.actions->data;
121         action_run(self->data.normal.actions, a->data.any.c, state, time);
122     }
123 }
124
125 static void client_dest(ObClient *client, gpointer data)
126 {
127     /* This concise function removes all references to a closed
128      * client in the client_list_menu, so we don't have to check
129      * in client.c */
130     GList *eit;
131     for (eit = combined_menu->entries; eit; eit = g_list_next(eit)) {
132         ObMenuEntry *meit = eit->data;
133         if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL &&
134             meit->data.normal.actions)
135         {
136             ObAction *a = meit->data.normal.actions->data;
137             ObClient *c = a->data.any.c;
138             if (c == client)
139                 a->data.any.c = NULL;
140         }
141     }
142 }
143
144 void client_list_combined_menu_startup(gboolean reconfig)
145 {
146     if (!reconfig)
147         client_add_destructor(client_dest, NULL);
148
149     combined_menu = menu_new(MENU_NAME, _("Windows"), NULL);
150     menu_set_update_func(combined_menu, self_update);
151     menu_set_execute_func(combined_menu, menu_execute);
152 }
153
154 void client_list_combined_menu_shutdown(gboolean reconfig)
155 {
156     if (!reconfig)
157         client_remove_destructor(client_dest);
158 }