OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/gtk/gtk_window_util.h" | |
6 | |
7 #include <dlfcn.h> | |
8 #include "content/public/browser/render_frame_host.h" | |
9 #include "content/public/browser/render_view_host.h" | |
10 #include "content/public/browser/web_contents.h" | |
11 #include "content/public/browser/web_contents_view.h" | |
12 #include "ui/base/base_window.h" | |
13 | |
14 using content::RenderFrameHost; | |
15 using content::WebContents; | |
16 | |
17 namespace gtk_window_util { | |
18 | |
19 const int kFrameBorderThickness = 4; | |
20 const int kResizeAreaCornerSize = 16; | |
21 | |
22 // Keep track of the last click time and the last click position so we can | |
23 // filter out extra GDK_BUTTON_PRESS events when a double click happens. | |
24 static guint32 last_click_time; | |
25 static int last_click_x; | |
26 static int last_click_y; | |
27 | |
28 // Performs Cut/Copy/Paste operation on the |window|. | |
29 // If the current render view is focused, then just call the specified |method| | |
30 // against the given web contents, otherwise emit the specified |signal| | |
31 // against the focused widget. | |
32 // TODO(suzhe): This approach does not work for plugins. | |
33 void DoCutCopyPaste(GtkWindow* window, | |
34 WebContents* web_contents, | |
35 void (WebContents::*method)(), | |
36 const char* signal) { | |
37 GtkWidget* widget = gtk_window_get_focus(window); | |
38 if (widget == NULL) | |
39 return; // Do nothing if no focused widget. | |
40 | |
41 if (web_contents && | |
42 widget == web_contents->GetView()->GetContentNativeView()) { | |
43 (web_contents->*method)(); | |
44 } else { | |
45 guint id; | |
46 if ((id = g_signal_lookup(signal, G_OBJECT_TYPE(widget))) != 0) | |
47 g_signal_emit(widget, id, 0); | |
48 } | |
49 } | |
50 | |
51 void DoCut(GtkWindow* window, WebContents* web_contents) { | |
52 DoCutCopyPaste(window, web_contents, | |
53 &WebContents::Cut, "cut-clipboard"); | |
54 } | |
55 | |
56 void DoCopy(GtkWindow* window, WebContents* web_contents) { | |
57 DoCutCopyPaste(window, web_contents, | |
58 &WebContents::Copy, "copy-clipboard"); | |
59 } | |
60 | |
61 void DoPaste(GtkWindow* window, WebContents* web_contents) { | |
62 DoCutCopyPaste(window, web_contents, | |
63 &WebContents::Paste, "paste-clipboard"); | |
64 } | |
65 | |
66 // Ubuntu patches their version of GTK+ so that there is always a | |
67 // gripper in the bottom right corner of the window. We dynamically | |
68 // look up this symbol because it's a non-standard Ubuntu extension to | |
69 // GTK+. We always need to disable this feature since we can't | |
70 // communicate this to WebKit easily. | |
71 typedef void (*gtk_window_set_has_resize_grip_func)(GtkWindow*, gboolean); | |
72 gtk_window_set_has_resize_grip_func gtk_window_set_has_resize_grip_sym; | |
73 | |
74 void DisableResizeGrip(GtkWindow* window) { | |
75 static bool resize_grip_looked_up = false; | |
76 if (!resize_grip_looked_up) { | |
77 resize_grip_looked_up = true; | |
78 gtk_window_set_has_resize_grip_sym = | |
79 reinterpret_cast<gtk_window_set_has_resize_grip_func>( | |
80 dlsym(NULL, "gtk_window_set_has_resize_grip")); | |
81 } | |
82 if (gtk_window_set_has_resize_grip_sym) | |
83 gtk_window_set_has_resize_grip_sym(window, FALSE); | |
84 } | |
85 | |
86 GdkCursorType GdkWindowEdgeToGdkCursorType(GdkWindowEdge edge) { | |
87 switch (edge) { | |
88 case GDK_WINDOW_EDGE_NORTH_WEST: | |
89 return GDK_TOP_LEFT_CORNER; | |
90 case GDK_WINDOW_EDGE_NORTH: | |
91 return GDK_TOP_SIDE; | |
92 case GDK_WINDOW_EDGE_NORTH_EAST: | |
93 return GDK_TOP_RIGHT_CORNER; | |
94 case GDK_WINDOW_EDGE_WEST: | |
95 return GDK_LEFT_SIDE; | |
96 case GDK_WINDOW_EDGE_EAST: | |
97 return GDK_RIGHT_SIDE; | |
98 case GDK_WINDOW_EDGE_SOUTH_WEST: | |
99 return GDK_BOTTOM_LEFT_CORNER; | |
100 case GDK_WINDOW_EDGE_SOUTH: | |
101 return GDK_BOTTOM_SIDE; | |
102 case GDK_WINDOW_EDGE_SOUTH_EAST: | |
103 return GDK_BOTTOM_RIGHT_CORNER; | |
104 default: | |
105 NOTREACHED(); | |
106 } | |
107 return GDK_LAST_CURSOR; | |
108 } | |
109 | |
110 bool BoundsMatchMonitorSize(GtkWindow* window, gfx::Rect bounds) { | |
111 // A screen can be composed of multiple monitors. | |
112 GdkScreen* screen = gtk_window_get_screen(window); | |
113 GdkRectangle monitor_size; | |
114 | |
115 if (gtk_widget_get_realized(GTK_WIDGET(window))) { | |
116 // |window| has been realized. | |
117 gint monitor_num = gdk_screen_get_monitor_at_window(screen, | |
118 gtk_widget_get_window(GTK_WIDGET(window))); | |
119 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor_size); | |
120 return bounds.size() == gfx::Size(monitor_size.width, monitor_size.height); | |
121 } | |
122 | |
123 // Make sure the window doesn't match any monitor size. We compare against | |
124 // all monitors because we don't know which monitor the window is going to | |
125 // open on before window realized. | |
126 gint num_monitors = gdk_screen_get_n_monitors(screen); | |
127 for (gint i = 0; i < num_monitors; ++i) { | |
128 GdkRectangle monitor_size; | |
129 gdk_screen_get_monitor_geometry(screen, i, &monitor_size); | |
130 if (bounds.size() == gfx::Size(monitor_size.width, monitor_size.height)) | |
131 return true; | |
132 } | |
133 return false; | |
134 } | |
135 | |
136 bool HandleTitleBarLeftMousePress( | |
137 GtkWindow* window, | |
138 const gfx::Rect& bounds, | |
139 GdkEventButton* event) { | |
140 // We want to start a move when the user single clicks, but not start a | |
141 // move when the user double clicks. However, a double click sends the | |
142 // following GDK events: GDK_BUTTON_PRESS, GDK_BUTTON_RELEASE, | |
143 // GDK_BUTTON_PRESS, GDK_2BUTTON_PRESS, GDK_BUTTON_RELEASE. If we | |
144 // start a gtk_window_begin_move_drag on the second GDK_BUTTON_PRESS, | |
145 // the call to gtk_window_maximize fails. To work around this, we | |
146 // keep track of the last click and if it's going to be a double click, | |
147 // we don't call gtk_window_begin_move_drag. | |
148 DCHECK(event->type == GDK_BUTTON_PRESS); | |
149 DCHECK(event->button == 1); | |
150 | |
151 static GtkSettings* settings = gtk_settings_get_default(); | |
152 gint double_click_time = 250; | |
153 gint double_click_distance = 5; | |
154 g_object_get(G_OBJECT(settings), | |
155 "gtk-double-click-time", &double_click_time, | |
156 "gtk-double-click-distance", &double_click_distance, | |
157 NULL); | |
158 | |
159 guint32 click_time = event->time - last_click_time; | |
160 int click_move_x = abs(event->x - last_click_x); | |
161 int click_move_y = abs(event->y - last_click_y); | |
162 | |
163 last_click_time = event->time; | |
164 last_click_x = static_cast<int>(event->x); | |
165 last_click_y = static_cast<int>(event->y); | |
166 | |
167 if (click_time > static_cast<guint32>(double_click_time) || | |
168 click_move_x > double_click_distance || | |
169 click_move_y > double_click_distance) { | |
170 // Ignore drag requests if the window is the size of the screen. | |
171 // We do this to avoid triggering fullscreen mode in metacity | |
172 // (without the --no-force-fullscreen flag) and in compiz (with | |
173 // Legacy Fullscreen Mode enabled). | |
174 if (!BoundsMatchMonitorSize(window, bounds)) { | |
175 gtk_window_begin_move_drag(window, event->button, | |
176 static_cast<gint>(event->x_root), | |
177 static_cast<gint>(event->y_root), | |
178 event->time); | |
179 } | |
180 return TRUE; | |
181 } | |
182 return FALSE; | |
183 } | |
184 | |
185 void UnMaximize(GtkWindow* window, | |
186 const gfx::Rect& bounds, | |
187 const gfx::Rect& restored_bounds) { | |
188 gtk_window_unmaximize(window); | |
189 | |
190 // It can happen that you end up with a window whose restore size is the same | |
191 // as the size of the screen, so unmaximizing it merely remaximizes it due to | |
192 // the same WM feature that SetWindowSize() works around. We try to detect | |
193 // this and resize the window to work around the issue. | |
194 if (bounds.size() == restored_bounds.size()) | |
195 gtk_window_resize(window, bounds.width(), bounds.height() - 1); | |
196 } | |
197 | |
198 void SetWindowCustomClass(GtkWindow* window, const std::string& wmclass) { | |
199 gtk_window_set_wmclass(window, | |
200 wmclass.c_str(), | |
201 gdk_get_program_class()); | |
202 | |
203 // Set WM_WINDOW_ROLE for session management purposes. | |
204 // See http://tronche.com/gui/x/icccm/sec-5.html . | |
205 gtk_window_set_role(window, wmclass.c_str()); | |
206 } | |
207 | |
208 void SetWindowSize(GtkWindow* window, const gfx::Size& size) { | |
209 gfx::Size new_size = size; | |
210 gint current_width = 0; | |
211 gint current_height = 0; | |
212 gtk_window_get_size(window, ¤t_width, ¤t_height); | |
213 GdkRectangle size_with_decorations = {0}; | |
214 GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window)); | |
215 if (gdk_window) { | |
216 gdk_window_get_frame_extents(gdk_window, | |
217 &size_with_decorations); | |
218 } | |
219 | |
220 if (current_width == size_with_decorations.width && | |
221 current_height == size_with_decorations.height) { | |
222 // Make sure the window doesn't match any monitor size. We compare against | |
223 // all monitors because we don't know which monitor the window is going to | |
224 // open on (the WM decides that). | |
225 GdkScreen* screen = gtk_window_get_screen(window); | |
226 gint num_monitors = gdk_screen_get_n_monitors(screen); | |
227 for (gint i = 0; i < num_monitors; ++i) { | |
228 GdkRectangle monitor_size; | |
229 gdk_screen_get_monitor_geometry(screen, i, &monitor_size); | |
230 if (gfx::Size(monitor_size.width, monitor_size.height) == size) { | |
231 gtk_window_resize(window, size.width(), size.height() - 1); | |
232 return; | |
233 } | |
234 } | |
235 } else { | |
236 // gtk_window_resize is the size of the window not including decorations, | |
237 // but we are given the |size| including window decorations. | |
238 if (size_with_decorations.width > current_width) { | |
239 new_size.set_width(size.width() - size_with_decorations.width + | |
240 current_width); | |
241 } | |
242 if (size_with_decorations.height > current_height) { | |
243 new_size.set_height(size.height() - size_with_decorations.height + | |
244 current_height); | |
245 } | |
246 } | |
247 | |
248 gtk_window_resize(window, new_size.width(), new_size.height()); | |
249 } | |
250 | |
251 void UpdateWindowPosition(ui::BaseWindow* window, | |
252 gfx::Rect* bounds, | |
253 gfx::Rect* restored_bounds) { | |
254 gint x, y; | |
255 gtk_window_get_position(window->GetNativeWindow(), &x, &y); | |
256 (*bounds).set_origin(gfx::Point(x, y)); | |
257 if (!window->IsFullscreen() && !window->IsMaximized()) | |
258 *restored_bounds = *bounds; | |
259 } | |
260 | |
261 bool GetWindowEdge(const gfx::Size& window_size, | |
262 int top_edge_inset, | |
263 int x, | |
264 int y, | |
265 GdkWindowEdge* edge) { | |
266 gfx::Rect middle(window_size); | |
267 middle.Inset(kFrameBorderThickness, | |
268 kFrameBorderThickness - top_edge_inset, | |
269 kFrameBorderThickness, | |
270 kFrameBorderThickness); | |
271 if (middle.Contains(x, y)) | |
272 return false; | |
273 | |
274 gfx::Rect north(0, 0, window_size.width(), | |
275 kResizeAreaCornerSize - top_edge_inset); | |
276 gfx::Rect west(0, 0, kResizeAreaCornerSize, window_size.height()); | |
277 gfx::Rect south(0, window_size.height() - kResizeAreaCornerSize, | |
278 window_size.width(), kResizeAreaCornerSize); | |
279 gfx::Rect east(window_size.width() - kResizeAreaCornerSize, 0, | |
280 kResizeAreaCornerSize, window_size.height()); | |
281 | |
282 if (north.Contains(x, y)) { | |
283 if (west.Contains(x, y)) | |
284 *edge = GDK_WINDOW_EDGE_NORTH_WEST; | |
285 else if (east.Contains(x, y)) | |
286 *edge = GDK_WINDOW_EDGE_NORTH_EAST; | |
287 else | |
288 *edge = GDK_WINDOW_EDGE_NORTH; | |
289 } else if (south.Contains(x, y)) { | |
290 if (west.Contains(x, y)) | |
291 *edge = GDK_WINDOW_EDGE_SOUTH_WEST; | |
292 else if (east.Contains(x, y)) | |
293 *edge = GDK_WINDOW_EDGE_SOUTH_EAST; | |
294 else | |
295 *edge = GDK_WINDOW_EDGE_SOUTH; | |
296 } else { | |
297 if (west.Contains(x, y)) | |
298 *edge = GDK_WINDOW_EDGE_WEST; | |
299 else if (east.Contains(x, y)) | |
300 *edge = GDK_WINDOW_EDGE_EAST; | |
301 else | |
302 return false; // The cursor must be outside the window. | |
303 } | |
304 return true; | |
305 } | |
306 | |
307 } // namespace gtk_window_util | |
OLD | NEW |