Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

menu: avoid deprecated gtk_menu_popup #709

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,20 +530,18 @@ meta_core_get_active_workspace (Screen *xscreen)
}

void
meta_core_show_window_menu (Display *xdisplay,
Window frame_xwindow,
int root_x,
int root_y,
int button,
guint32 timestamp)
meta_core_show_window_menu (Display *xdisplay,
Window frame_xwindow,
const GdkRectangle *rect,
const GdkEventButton *event)
{
MetaWindow *window = get_window (xdisplay, frame_xwindow);

if (meta_prefs_get_raise_on_click ())
meta_window_raise (window);
meta_window_focus (window, timestamp);
meta_window_focus (window, event->time);

meta_window_show_menu (window, root_x, root_y, button, timestamp);
meta_window_show_menu (window, rect, (GdkEvent *) event);
}

void
Expand Down
68 changes: 63 additions & 5 deletions src/core/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "bell.h"
#include "effects.h"
#include "compositor.h"
#include <gdk/gdkx.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>

Expand Down Expand Up @@ -1640,6 +1641,56 @@ mouse_event_is_in_tab_popup (MetaDisplay *display,
return FALSE;
}

static gint
get_window_scaling_factor (void)
{
GdkScreen *screen;
GValue value = G_VALUE_INIT;

screen = gdk_screen_get_default ();

g_value_init (&value, G_TYPE_INT);

if (gdk_screen_get_setting (screen, "gdk-window-scaling-factor", &value))
return g_value_get_int (&value);
else
return 1;
}

static GdkEvent *
button_press_event_new (XEvent *xevent)
{
GdkDisplay *display;
GdkSeat *seat;
gint scale;
GdkWindow *window;
GdkDevice *device;
GdkEvent *event;

display = gdk_display_get_default ();
seat = gdk_display_get_default_seat (display);
scale = get_window_scaling_factor ();

window = gdk_x11_window_lookup_for_display (display, xevent->xbutton.window);
device = gdk_seat_get_pointer (seat);

event = gdk_event_new (GDK_BUTTON_PRESS);

event->button.window = window ? g_object_ref (window) : NULL;
event->button.send_event = xevent->xbutton.send_event ? TRUE : FALSE;
event->button.time = xevent->xbutton.time;
event->button.x = xevent->xbutton.x / scale;
event->button.y = xevent->xbutton.y / scale;
event->button.state = (GdkModifierType) xevent->xbutton.state;
event->button.button = xevent->xbutton.button;
event->button.x_root = xevent->xbutton.x_root / scale;
event->button.y_root = xevent->xbutton.y_root / scale;

gdk_event_set_device (event, device);

return event;
}

/**
* This is the most important function in the whole program. It is the heart,
* it is the nexus, it is the Grand Central Station of Marco's world.
Expand Down Expand Up @@ -1998,13 +2049,20 @@ static gboolean event_callback(XEvent* event, gpointer data)
}
else if (event->xbutton.button == meta_prefs_get_mouse_button_menu())
{
GdkRectangle rect;
GdkEvent *gdk_event;

if (meta_prefs_get_raise_on_click ())
meta_window_raise (window);
meta_window_show_menu (window,
event->xbutton.x_root,
event->xbutton.y_root,
event->xbutton.button,
event->xbutton.time);

rect.x = event->xbutton.x;
rect.y = event->xbutton.y;
rect.width = 0;
rect.height = 0;

gdk_event = button_press_event_new (event);
meta_window_show_menu (window, &rect, gdk_event);
gdk_event_free (gdk_event);
}

if (!frame_was_receiver && unmodified)
Expand Down
48 changes: 38 additions & 10 deletions src/core/keybindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include <gio/gio.h>

#include <gdk/gdkx.h>
#include <X11/keysym.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -2946,6 +2947,34 @@ handle_panel (MetaDisplay *display,
meta_error_trap_pop (display, FALSE);
}

static GdkEvent *
key_press_event_new (XEvent *xevent)
{
GdkDisplay *display;
GdkSeat *seat;
GdkWindow *window;
GdkDevice *device;
GdkEvent *event;

display = gdk_display_get_default ();
seat = gdk_display_get_default_seat (display);

window = gdk_x11_window_lookup_for_display (display, xevent->xkey.window);
device = gdk_seat_get_keyboard (seat);

event = gdk_event_new (GDK_KEY_PRESS);

event->key.window = window ? g_object_ref (window) : NULL;
event->key.send_event = xevent->xkey.send_event ? TRUE : FALSE;
event->key.time = xevent->xkey.time;
event->key.state = (GdkModifierType) xevent->xkey.state;
event->key.hardware_keycode = xevent->xkey.keycode;

gdk_event_set_device (event, device);

return event;
}

static void
handle_activate_window_menu (MetaDisplay *display,
MetaScreen *screen,
Expand All @@ -2955,18 +2984,17 @@ handle_activate_window_menu (MetaDisplay *display,
{
if (display->focus_window)
{
int x, y;

meta_window_get_position (display->focus_window,
&x, &y);
GdkRectangle rect;
GdkEvent *gdk_event;

if (meta_ui_get_direction() == META_UI_DIRECTION_RTL)
x += display->focus_window->rect.width;
rect.x = display->focus_window->rect.x;
rect.y = display->focus_window->rect.y;
rect.width = display->focus_window->rect.width;
rect.height = 0;

meta_window_show_menu (display->focus_window,
x, y,
0,
event->xkey.time);
gdk_event = key_press_event_new (event);
meta_window_show_menu (display->focus_window, &rect, gdk_event);
gdk_event_free (gdk_event);
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/core/window-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,9 @@ void meta_window_set_current_workspace_hint (MetaWindow *window);

unsigned long meta_window_get_net_wm_desktop (MetaWindow *window);

void meta_window_show_menu (MetaWindow *window,
int root_x,
int root_y,
int button,
guint32 timestamp);
void meta_window_show_menu (MetaWindow *window,
const GdkRectangle *rect,
const GdkEvent *event);

gboolean meta_window_titlebar_is_onscreen (MetaWindow *window);
void meta_window_shove_titlebar_onscreen (MetaWindow *window);
Expand Down
32 changes: 14 additions & 18 deletions src/core/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -5561,23 +5561,21 @@ meta_window_client_message (MetaWindow *window,
else if (event->xclient.message_type ==
display->atom__GTK_SHOW_WINDOW_MENU)
{
gulong x_root, y_root;
guint32 timestamp;
int button;
GdkRectangle rect;
GdkEvent *gdk_event;

if (meta_prefs_get_raise_on_click ())
meta_window_raise (window);

timestamp = meta_display_get_current_time_roundtrip (display);
x_root = event->xclient.data.l[1];
y_root = event->xclient.data.l[2];
button = 3;
rect.x = event->xclient.data.l[1];
rect.y = event->xclient.data.l[2];
rect.width = 0;
rect.height = 0;

meta_window_show_menu (window,
x_root,
y_root,
button,
timestamp);
gdk_event = gdk_event_new(GDK_BUTTON_PRESS);
gdk_event->button.button = 3;
meta_window_show_menu (window, &rect, gdk_event);
gdk_event_free(gdk_event);
}

return FALSE;
Expand Down Expand Up @@ -6995,11 +6993,9 @@ menu_callback (MetaWindowMenu *menu,
}

void
meta_window_show_menu (MetaWindow *window,
int root_x,
int root_y,
int button,
guint32 timestamp)
meta_window_show_menu (MetaWindow *window,
const GdkRectangle *rect,
const GdkEvent *event)
{
MetaMenuOp ops;
MetaMenuOp insensitive;
Expand Down Expand Up @@ -7120,7 +7116,7 @@ meta_window_show_menu (MetaWindow *window,

meta_verbose ("Popping up window menu for %s\n", window->desc);

meta_ui_window_menu_popup (menu, root_x, root_y, button, timestamp);
meta_ui_window_menu_popup (menu, rect, event);
}

void
Expand Down
10 changes: 4 additions & 6 deletions src/include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,10 @@ const char* meta_core_get_workspace_name_with_index (Display *xdisplay,
Window xroot,
int index);

void meta_core_show_window_menu (Display *xdisplay,
Window frame_xwindow,
int root_x,
int root_y,
int button,
guint32 timestamp);
void meta_core_show_window_menu (Display *xdisplay,
Window frame_xwindow,
const GdkRectangle *rect,
const GdkEventButton *event);

void meta_core_get_menu_accelerator (MetaMenuOp menu_op,
int workspace,
Expand Down
6 changes: 2 additions & 4 deletions src/include/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,8 @@ MetaWindowMenu* meta_ui_window_menu_new (MetaUI *ui,
MetaWindowMenuFunc func,
gpointer data);
void meta_ui_window_menu_popup (MetaWindowMenu *menu,
int root_x,
int root_y,
int button,
guint32 timestamp);
const GdkRectangle *rect,
const GdkEvent *event);
void meta_ui_window_menu_free (MetaWindowMenu *menu);

GdkPixbuf* meta_gdk_pixbuf_get_from_pixmap (GdkPixbuf *dest,
Expand Down
31 changes: 13 additions & 18 deletions src/ui/frames.c
Original file line number Diff line number Diff line change
Expand Up @@ -1564,12 +1564,18 @@ meta_frame_titlebar_event (MetaUIFrame *frame,
break;

case META_ACTION_TITLEBAR_MENU:
meta_core_show_window_menu (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
{
GdkRectangle rect;

rect.x = event->x;
rect.y = event->y;
rect.width = 0;
rect.height = 0;
meta_core_show_window_menu (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
frame->xwindow,
event->x_root,
event->y_root,
event->button,
event->time);
&rect,
event);
}
break;

case META_ACTION_TITLEBAR_LAST:
Expand Down Expand Up @@ -1738,26 +1744,15 @@ meta_frames_button_press_event (GtkWidget *widget,
{
MetaFrameGeometry fgeom;
GdkRectangle *rect;
int dx, dy;

meta_frames_calc_geometry (frames, frame, &fgeom);

rect = control_rect (META_FRAME_CONTROL_MENU, &fgeom);

/* get delta to convert to root coords */
dx = event->x_root - event->x;
dy = event->y_root - event->y;

/* Align to the right end of the menu rectangle if RTL */
if (meta_ui_get_direction() == META_UI_DIRECTION_RTL)
dx += rect->width;

meta_core_show_window_menu (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
frame->xwindow,
rect->x + dx,
rect->y + rect->height + dy,
event->button,
event->time);
rect,
event);
}
}
else if (event->button == 1 &&
Expand Down
36 changes: 4 additions & 32 deletions src/ui/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,6 @@ static MenuItem menuitems[] = {
{META_MENU_OP_DELETE, MENU_ITEM_IMAGE, MARCO_STOCK_DELETE, FALSE, N_("_Close")}
};

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_get_preferred_size (GTK_WIDGET (menu), &req, NULL);

*x = pos->x;
*y = pos->y;

if (meta_ui_get_direction() == META_UI_DIRECTION_RTL)
{
*x = MAX (0, *x - req.width);
}

/* Ensure onscreen */
*x = CLAMP (*x, 0, MAX(0, WidthOfScreen (gdk_x11_screen_get_xscreen (gdk_screen_get_default ())) - req.width));
*y = CLAMP (*y, 0, MAX(0, HeightOfScreen (gdk_x11_screen_get_xscreen (gdk_screen_get_default ())) - req.height));
}

static void menu_closed(GtkMenu* widget, gpointer data)
{
MetaWindowMenu *menu;
Expand Down Expand Up @@ -489,18 +467,12 @@ meta_window_menu_new (MetaFrames *frames,
return menu;
}

void meta_window_menu_popup(MetaWindowMenu* menu, int root_x, int root_y, int button, guint32 timestamp)
void meta_window_menu_popup(MetaWindowMenu* menu, const GdkRectangle *rect, const GdkEvent *event)
{
GdkPoint* pt = g_new(GdkPoint, 1);
gint scale;

g_object_set_data_full(G_OBJECT(menu->menu), "destroy-point", pt, g_free);

scale = gtk_widget_get_scale_factor (menu->menu);
pt->x = root_x / scale;
pt->y = root_y / scale;
GdkEventAny *any;

gtk_menu_popup(GTK_MENU (menu->menu), NULL, NULL, popup_position_func, pt, button, timestamp);
any = (GdkEventAny *) event;
gtk_menu_popup_at_rect(GTK_MENU (menu->menu), any->window, rect, GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_NORTH_WEST, event);

if (!gtk_widget_get_visible (menu->menu))
meta_warning("GtkMenu failed to grab the pointer\n");
Expand Down
Loading