summaryrefslogtreecommitdiff
path: root/src/menu.c
blob: 0e3868b9f25f9c5aa52d8b1deb1517aeeea1cc07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; You may only use version 2 of the License,
        you have no option to use any other version.
 
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
 
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
        Metacity - (c) 2001 Havoc Pennington
        xfwm4    - (c) 2002-2003 Olivier Fourdan
 
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#ifdef GDK_MULTIHEAD_SAFE
#undef GDK_MULTIHEAD_SAFE
#endif

#include <glib.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <X11/Xlib.h>
#include <unistd.h>
#include <libxfcegui4/libxfcegui4.h>
#include "menu.h"
#include "debug.h"
#include "my_intl.h"

static GtkWidget *menu_open = NULL;
static MenuItem menuitems[] = {
                                  {MENU_OP_MAXIMIZE, NULL, N_("Ma_ximize")},
                                  {MENU_OP_UNMAXIMIZE, NULL, N_("Un_maximize")},
                                  {MENU_OP_MINIMIZE, NULL, N_("_Hide")},
                                  {MENU_OP_MINIMIZE_ALL, NULL, N_("Hide _all others")},
                                  {MENU_OP_UNMINIMIZE, NULL, N_("S_how")},
                                  {MENU_OP_SHADE, NULL, N_("_Shade")},
                                  {MENU_OP_UNSHADE, NULL, N_("Un_shade")},
                                  {MENU_OP_STICK, NULL, N_("S_tick")},
                                  {MENU_OP_UNSTICK, NULL, N_("Uns_tick")},
                                  {MENU_OP_MOVE, NULL, N_("_Move")},
                                  {MENU_OP_RESIZE, NULL, N_("_Resize")},
                                  {MENU_OP_SWITCH, NULL, N_("S_witch")},
                                  {0, NULL, NULL},
                                  {MENU_OP_DELETE, NULL, N_("_Close")},
#if 0
                                  {MENU_OP_DESTROY, NULL, N_("Destroy")},
                                  {0, NULL, NULL},
                                  {MENU_OP_WORKSPACES, NULL, N_("Wor_kspace")},
                                  {0, NULL, NULL},
#endif
                                  {MENU_OP_QUIT, NULL, N_("_Quit")},
                                  {MENU_OP_RESTART, NULL, N_("Restart")},
                              };

static GtkToXEventFilterStatus menu_filter(XEvent * xevent, gpointer data)
{
    switch (xevent->type)
    {
    case KeyPress:
    case KeyRelease:
    case ButtonPress:
    case ButtonRelease:
    case MotionNotify:
    case EnterNotify:
    case LeaveNotify:
        return XEV_FILTER_STOP;
        break;
    default:
        return XEV_FILTER_CONTINUE;
        break;
    }
    return XEV_FILTER_STOP;
}


static void popup_position_func(GtkMenu * menu, gint * x, gint * y, gboolean * push_in, gpointer user_data)
{
    GtkRequisition req;
    GdkPoint *pos;

    pos = user_data;

    gtk_widget_size_request(GTK_WIDGET(menu), &req);

    if(pos->x >= 0)
    {
        *x = pos->x;
        *x = CLAMP(*x, 0, MAX(0, gdk_screen_width() - req.width));
    }
    else
    {
        *x = (gdk_screen_width() - req.width) / 2;
    }
    if(pos->x >= 0)
    {
        *y = pos->y;
        *y = CLAMP(*y, 0, MAX(0, gdk_screen_height() - req.height));
    }
    else
    {
        *y = (gdk_screen_height() - req.height) / 2;
    }
    g_free(user_data);
}

static gboolean activate_cb(GtkWidget * menuitem, gpointer data)
{
    MenuData *md;

    DBG("entering activate_cb\n");
    g_return_val_if_fail(GTK_IS_WIDGET(menuitem), FALSE);

    menu_open = NULL;

    md = data;

    DBG("deactivating menu_filter\n");
    popEventFilter();
    (*md->menu->func) (md->menu, md->op, md->client_xwindow, md->menu->data, md->data);
    return (FALSE);
}

static gboolean menu_closed(GtkMenu * widget, gpointer data)
{
    Menu *menu;

    DBG("entering menu_closed\n");
    menu = data;
    menu_open = NULL;
    DBG("deactivating menu_filter\n");
    popEventFilter();
    (*menu->func) (menu, 0, None, menu->data, NULL);
    return (FALSE);
}

Menu *menu_default(MenuOp ops, MenuOp insensitive, MenuFunc func, gpointer data)
{
    int i;
    Menu *menu;

    DBG("entering menu_new\n");
    menu = g_new(Menu, 1);
    menu->func = func;
    menu->data = data;
    menu->ops = ops;
    menu->insensitive = insensitive;
    menu->menu = gtk_menu_new();

    i = 0;
    while(i < (int)(sizeof(menuitems) / sizeof(MenuItem)))
    {
        if(ops & menuitems[i].op || menuitems[i].op == 0)
        {
            GtkWidget *mi;
            MenuData *md;

            if(menuitems[i].op == 0)
            {
                mi = gtk_separator_menu_item_new();
            }
            else
            {
                mi = gtk_menu_item_new_with_mnemonic(_(menuitems[i].label));
                if(insensitive & menuitems[i].op)
                {
                    gtk_widget_set_sensitive(mi, FALSE);
                }
                md = g_new(MenuData, 1);
                md->menu = menu;
                md->op = menuitems[i].op;
                md->client_xwindow = None;
                md->data = NULL;
                menu_item_connect(mi, md);
            }
            gtk_menu_shell_append(GTK_MENU_SHELL(menu->menu), mi);
            gtk_widget_show(mi);
        }
        ++i;
    }
    menu_connect(menu);

    return (menu);
}

Menu *menu_connect(Menu * menu)
{
    DBG("entering menu_connect\n");
    g_return_val_if_fail(menu != NULL, NULL);
    g_return_val_if_fail(GTK_IS_MENU(menu->menu), NULL);
    g_signal_connect(GTK_OBJECT(menu->menu), "selection_done", GTK_SIGNAL_FUNC(menu_closed), menu);
    return (menu);
}

static void closure_notify(gpointer data, GClosure * closure)
{
    DBG("entering closure_notify\n");
    if(data)
    {
        DBG("freeing data\n");
        g_free(data);
    }
}

GtkWidget *menu_item_connect(GtkWidget * item, MenuData * item_data)
{
    DBG("entering menu_item_connect\n");
    g_return_val_if_fail(item != NULL, NULL);
    g_return_val_if_fail(GTK_IS_MENU_ITEM(item), NULL);
    g_signal_connect_closure(GTK_OBJECT(item), "activate", g_cclosure_new(GTK_SIGNAL_FUNC(activate_cb), item_data, (GClosureNotify) closure_notify), FALSE);
    return (item);
}

gboolean menu_is_opened(void)
{
    DBG("entering menu_is_opened\n");
    return (menu_open != NULL);
}

gboolean menu_check_and_close(void)
{
    DBG("entering menu_check_or_close\n");
    if(menu_open)
    {
        DBG("menu open, emitting deactivate signal\n");
        g_signal_emit_by_name(GTK_OBJECT(menu_open), "deactivate");
        menu_open = NULL;
        return (TRUE);
    }
    return (FALSE);
}

static gboolean grab_available(guint32 timestamp)
{
    GdkEventMask mask = GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_POINTER_MOTION_MASK;
    GdkGrabStatus g1;
    GdkGrabStatus g2;
    gboolean grab_failed = FALSE;
    gint i = 0;

    DBG("entering grab_available\n");

    g1 = gdk_pointer_grab(getGdkEventWindow(), TRUE, mask, NULL, NULL, timestamp);
    g2 = gdk_keyboard_grab(getGdkEventWindow(), TRUE, timestamp);

    while((i++ < 100) && (grab_failed = ((g1 != GDK_GRAB_SUCCESS) || (g2 != GDK_GRAB_SUCCESS))))
    {
        DBG("grab not available yet, waiting... (%i)\n", i);
        usleep(100);
        if(g1 != GDK_GRAB_SUCCESS)
        {
            g1 = gdk_pointer_grab(getGdkEventWindow(), TRUE, mask, NULL, NULL, timestamp);
        }
        if(g2 != GDK_GRAB_SUCCESS)
        {
            g2 = gdk_keyboard_grab(getGdkEventWindow(), TRUE, timestamp);
        }
    }

    if(g1 == GDK_GRAB_SUCCESS)
    {
        gdk_pointer_ungrab(timestamp);
    }
    if(g2 == GDK_GRAB_SUCCESS)
    {
        gdk_keyboard_ungrab(timestamp);
    }

    return (!grab_failed);
}

gboolean menu_popup(Menu * menu, int root_x, int root_y, int button, guint32 timestamp)
{
    GdkPoint *pt;

    DBG("entering menu_popup\n");

    g_return_val_if_fail(menu != NULL, FALSE);
    g_return_val_if_fail(GTK_IS_MENU(menu->menu), FALSE);

    pt = g_new(GdkPoint, 1);
    pt->x = root_x;
    pt->y = root_y;

    if(!menu_check_and_close())
    {
        if(!grab_available(timestamp))
        {
            g_free(pt);
            DBG("Cannot get grab on pointer/keyboard, cancel.\n");
            return FALSE;
        }
        DBG("opening new menu\n");
        menu_open = menu->menu;
        pushEventFilter(menu_filter, NULL);
        gtk_menu_popup(GTK_MENU(menu->menu), NULL, NULL, popup_position_func, pt, button, timestamp);

        if(!GTK_MENU_SHELL(GTK_MENU(menu->menu))->have_xgrab)
        {
            gdk_beep();
            g_message(_("%s: GtkMenu failed to grab the pointer\n"), g_get_prgname());
        }
    }
    return TRUE;
}

void menu_free(Menu * menu)
{
    DBG("entering menu_free\n");
    g_return_if_fail(menu != NULL);
    g_return_if_fail(menu->menu != NULL);
    g_return_if_fail(GTK_IS_MENU(menu->menu));
    DBG("freeing menu\n");
    gtk_widget_destroy(menu->menu);
    g_free(menu);
}