60d6e687e46e58266ac2e696623f35a2dc54b67d
[dana/openbox-history.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 "focus.h"
26 #include "config.h"
27 #include "gettext.h"
28
29 #include <glib.h>
30
31 #define MENU_NAME "client-list-combined-menu"
32
33 ObMenu *combined_menu;
34
35 static gboolean self_update(ObMenuFrame *frame, gpointer data)
36 {
37     ObMenu *menu = frame->menu;
38     ObMenuEntry *e;
39     GList *it;
40     guint desktop;
41
42     menu_clear_entries(menu);
43
44     for (desktop = 0; desktop < screen_num_desktops; desktop++) {
45         gboolean empty = TRUE;
46         gboolean onlyiconic = TRUE;
47
48         menu_add_separator(menu, -1, screen_desktop_names[desktop]);
49         for (it = focus_order; it; it = g_list_next(it)) {
50             ObClient *c = it->data;
51             if (client_normal(c) && (!c->skip_taskbar || c->iconic) &&
52                 (c->desktop == desktop || c->desktop == DESKTOP_ALL))
53             {
54                 const ObClientIcon *icon;
55
56                 empty = FALSE;
57
58                 if (c->iconic) {
59                     gchar *title = g_strdup_printf("(%s)", c->icon_title);
60                     e = menu_add_normal(menu, -1, title, NULL, FALSE);
61                     g_free(title);
62                 } else {
63                     onlyiconic = FALSE;
64                     e = menu_add_normal(menu, -1, c->title, NULL, FALSE);
65                 }
66
67                 if (config_menu_client_list_icons
68                         && (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                     e->data.normal.icon_alpha =
73                         c->iconic ? OB_ICONIC_ALPHA : 0xff;
74                 }
75
76                 e->data.normal.data = c;
77             }
78         }
79
80         if (empty || onlyiconic) {
81             ObMenuEntry *e;
82
83             /* no entries or only iconified windows, so add a
84              * way to go to this desktop without uniconifying a window */
85             if (!empty)
86                 menu_add_separator(menu, -1, NULL);
87
88             e = menu_add_normal(menu, desktop, _("Go there..."), NULL, TRUE);
89             if (desktop == screen_desktop)
90                 e->data.normal.enabled = FALSE;
91         }
92     }
93     return TRUE; /* always show the menu */
94 }
95
96 static void menu_execute(ObMenuEntry *self, ObMenuFrame *f,
97                          ObClient *c, guint state, gpointer data)
98 {
99     if (self->id == -1) {
100         if (self->data.normal.data) /* it's set to NULL if its destroyed */
101             client_activate(self->data.normal.data, FALSE, TRUE);
102     }
103     else
104         screen_set_desktop(self->id, TRUE);
105 }
106
107 static void client_dest(ObClient *client, gpointer data)
108 {
109     /* This concise function removes all references to a closed
110      * client in the client_list_menu, so we don't have to check
111      * in client.c */
112     GList *eit;
113     for (eit = combined_menu->entries; eit; eit = g_list_next(eit)) {
114         ObMenuEntry *meit = eit->data;
115         if (meit->type == OB_MENU_ENTRY_TYPE_NORMAL &&
116             meit->data.normal.data == client)
117         {
118             meit->data.normal.data = NULL;
119         }
120     }
121 }
122
123 void client_list_combined_menu_startup(gboolean reconfig)
124 {
125     if (!reconfig)
126         client_add_destroy_notify(client_dest, NULL);
127
128     combined_menu = menu_new(MENU_NAME, _("Windows"), TRUE, NULL);
129     menu_set_update_func(combined_menu, self_update);
130     menu_set_execute_func(combined_menu, menu_execute);
131 }
132
133 void client_list_combined_menu_shutdown(gboolean reconfig)
134 {
135     if (!reconfig)
136         client_remove_destroy_notify(client_dest);
137 }