forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_gtk.cc
More file actions
134 lines (114 loc) · 4.88 KB
/
menu_gtk.cc
File metadata and controls
134 lines (114 loc) · 4.88 KB
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
// Copyright (c) 2012 Intel Corp
// Copyright (c) 2012 The Chromium Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
// pies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in al
// l copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "content/nw/src/api/menu/menu.h"
#include "base/logging.h"
#include "base/values.h"
#include "content/nw/src/api/menuitem/menuitem.h"
#include "content/nw/src/nw_shell.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/render_widget_host_view.h"
#include "ui/gfx/point.h"
namespace nwapi {
namespace {
// Popup menus may get squished if they open up too close to the bottom of the
// screen. This function takes the size of the screen, the size of the menu,
// an optional widget, the Y position of the mouse click, and adjusts the popup
// menu's Y position to make it fit if it's possible to do so.
// Returns the new Y position of the popup menu.
int CalculateMenuYPosition(const GdkRectangle* screen_rect,
const GtkRequisition* menu_req,
GtkWidget* widget, const int y) {
CHECK(screen_rect);
CHECK(menu_req);
// If the menu would run off the bottom of the screen, and there is enough
// screen space upwards to accommodate the menu, then pop upwards. If there
// is a widget, then also move the anchor point to the top of the widget
// rather than the bottom.
const int screen_top = screen_rect->y;
const int screen_bottom = screen_rect->y + screen_rect->height;
const int menu_bottom = y + menu_req->height;
int alternate_y = y - menu_req->height;
if (widget) {
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
alternate_y -= allocation.height;
}
if (menu_bottom >= screen_bottom && alternate_y >= screen_top)
return alternate_y;
return y;
}
void PointMenuPositionFunc(GtkMenu* menu,
int* x,
int* y,
gboolean* push_in,
gpointer userdata) {
*push_in = TRUE;
gfx::Point* point = reinterpret_cast<gfx::Point*>(userdata);
*x = point->x();
*y = point->y();
GtkRequisition menu_req;
gtk_widget_size_request(GTK_WIDGET(menu), &menu_req);
GdkScreen* screen;
gdk_display_get_pointer(gdk_display_get_default(), &screen, NULL, NULL, NULL);
gint monitor = gdk_screen_get_monitor_at_point(screen, *x, *y);
GdkRectangle screen_rect;
gdk_screen_get_monitor_geometry(screen, monitor, &screen_rect);
*y = CalculateMenuYPosition(&screen_rect, &menu_req, NULL, *y);
}
} // namespace
void Menu::Create(const base::DictionaryValue& option) {
std::string type;
if (option.GetString("type", &type) && type == "menubar")
menu_ = gtk_menu_bar_new();
else
menu_ = gtk_menu_new();
gtk_widget_show(menu_);
g_object_ref_sink(G_OBJECT(menu_));
}
void Menu::Destroy() {
gtk_widget_destroy(menu_);
g_object_unref(G_OBJECT(menu_));
}
void Menu::Append(MenuItem* menu_item) {
gtk_menu_shell_append(GTK_MENU_SHELL(menu_), menu_item->menu_item_);
}
void Menu::Insert(MenuItem* menu_item, int pos) {
gtk_menu_shell_insert(GTK_MENU_SHELL(menu_), menu_item->menu_item_, pos);
}
void Menu::Remove(MenuItem* menu_item, int pos) {
gtk_container_remove(GTK_CONTAINER(menu_), menu_item->menu_item_);
}
void Menu::Popup(int x, int y, content::Shell* shell) {
GdkEventButton* event = shell->web_contents()->GetRenderWidgetHostView()->
GetLastMouseDown();
uint32_t triggering_event_time = event ? event->time : GDK_CURRENT_TIME;
gfx::Point point;
if (!event) {
// gfx::Rect bounds = shell->web_contents()->GetRenderWidgetHostView()->GetViewBounds();
// point = gfx::Point(x + bounds.x(), y + bounds.y());
DVLOG(1) << "no last mouse down event";
point = gfx::Point(x, y);
}else
point = gfx::Point(event->x_root, event->y_root);
gtk_menu_popup(GTK_MENU(menu_), NULL, NULL,
PointMenuPositionFunc, &point,
3, triggering_event_time);
}
} // namespace nwapi