]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/client_list_combined_menu.c
Allow ignoring repeat events for a keybind
[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-2007   Dana 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 "screen.h"
24 #include "client.h"
25 #include "client_list_combined_menu.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 static ObMenu *combined_menu;
35
36 #define SEPARATOR -1
37 #define ADD_DESKTOP -2
38 #define REMOVE_DESKTOP -3
39
40 static void self_cleanup(ObMenu *menu, gpointer data)
41 {
42     menu_clear_entries(menu);
43 }
44
45 static gboolean self_update(ObMenuFrame *frame, gpointer data)
46 {
47     ObMenu *menu = frame->menu;
48     ObMenuEntry *e;
49     GList *it;
50     guint desktop, desktop_it;
51
52     menu_clear_entries(menu);
53
54     for (desktop_it = 0; desktop_it < screen_num_desktops; desktop_it++) {
55         gboolean empty = TRUE;
56         gboolean onlyiconic = TRUE;
57         gboolean noicons = TRUE;
58         
59         desktop = desktop_it;
60         if (desktop == 0)
61             desktop = screen_desktop;
62         else if (desktop <= screen_desktop)
63             desktop -= 1;
64
65         menu_add_separator(menu, SEPARATOR, screen_desktop_names[desktop]);
66         for (it = focus_order; it; it = g_list_next(it)) {
67             ObClient *c = it->data;
68             if (focus_valid_target(c, desktop,
69                                    TRUE, TRUE,
70                                    FALSE, TRUE, FALSE, FALSE, FALSE))
71             {
72                 empty = FALSE;
73
74                 if (c->iconic) {
75                     if (noicons) {
76                         menu_add_separator(menu, -1, NULL);
77                         noicons = FALSE;
78                     }
79                     e = menu_add_normal(menu, desktop, c->icon_title, NULL, FALSE);
80                 } else {
81                     onlyiconic = FALSE;
82                     e = menu_add_normal(menu, desktop, c->title, NULL, FALSE);
83                 }
84
85                 if (config_menu_show_icons) {
86                     e->data.normal.icon = client_icon(c);
87                     RrImageRef(e->data.normal.icon);
88                     e->data.normal.icon_alpha =
89                         c->iconic ? OB_ICONIC_ALPHA : 0xff;
90                 }
91
92                 e->data.normal.data = c;
93             }
94         }
95
96         if (empty || onlyiconic) {
97             /* no entries or only iconified windows, so add a
98              * way to go to this desktop without uniconifying a window */
99             if (!empty)
100                 menu_add_separator(menu, SEPARATOR, NULL);
101
102             e = menu_add_normal(menu, desktop, _("Go there..."), NULL, TRUE);
103             if (desktop == screen_desktop)
104                 e->data.normal.enabled = FALSE;
105         }
106     }
107
108     if (config_menu_manage_desktops) {
109         menu_add_separator(menu, SEPARATOR, _("Manage desktops"));
110         menu_add_normal(menu, ADD_DESKTOP, _("_Add new desktop"), NULL, TRUE);
111         menu_add_normal(menu, REMOVE_DESKTOP, _("_Remove last desktop"),
112                         NULL, TRUE);
113     }
114
115     return TRUE; /* always show the menu */
116 }
117
118 static void menu_execute(ObMenuEntry *self, ObMenuFrame *f,
119                          ObClient *c, guint state, gpointer data)
120 {
121     if (self->id == ADD_DESKTOP) {
122         screen_add_desktop(FALSE);
123         menu_frame_hide_all();
124     }
125     else if (self->id == REMOVE_DESKTOP) {
126         screen_remove_desktop(FALSE);
127         menu_frame_hide_all();
128     }
129     else {
130         ObClient *t = self->data.normal.data;
131         if (t && !t->locked) { /* it's set to NULL if its destroyed */
132             gboolean here = state & ShiftMask;
133
134             client_activate(t, TRUE, here, TRUE, TRUE, TRUE);
135             /* if the window is omnipresent then we need to go to its
136                desktop */
137             if (!here && t->desktop == DESKTOP_ALL)
138                 screen_set_desktop(self->id, FALSE);
139         }
140         else
141             screen_set_desktop(self->id, TRUE);
142     }
143 }
144
145 static void client_dest(ObClient *client, gpointer data)
146 {
147     /* This concise function removes all references to a closed
148      * client in the client_list_menu, so we don't have to check
149      * in client.c */
150     GList *eit;
151     for (eit = combined_menu->entries; eit; eit = g_list_next(eit)) {
152         ObMenuEntry *meit = eit->data;
153         if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL &&
154             meit->data.normal.data == client)
155         {
156             meit->data.normal.data = NULL;
157         }
158     }
159 }
160
161 void client_list_combined_menu_startup(gboolean reconfig)
162 {
163     if (!reconfig)
164         client_add_destroy_notify(client_dest, NULL);
165
166     combined_menu = menu_new(MENU_NAME, _("Windows"), TRUE, NULL);
167     menu_set_update_func(combined_menu, self_update);
168     menu_set_cleanup_func(combined_menu, self_cleanup);
169     menu_set_execute_func(combined_menu, menu_execute);
170     combined_menu->warp = TRUE;
171 }
172
173 void client_list_combined_menu_shutdown(gboolean reconfig)
174 {
175     if (!reconfig)
176         client_remove_destroy_notify(client_dest);
177 }