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.
5 #include "chrome/browser/ui/gtk/gtk_window_util.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"
14 using content::RenderFrameHost;
15 using content::WebContents;
17 namespace gtk_window_util {
19 const int kFrameBorderThickness = 4;
20 const int kResizeAreaCornerSize = 16;
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;
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 current render view host, 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 (RenderFrameHost::*method)(),
37 GtkWidget* widget = gtk_window_get_focus(window);
39 return; // Do nothing if no focused widget.
42 widget == web_contents->GetView()->GetContentNativeView()) {
43 RenderFrameHost* frame = web_contents->GetFocusedFrame();
47 if ((id = g_signal_lookup(signal, G_OBJECT_TYPE(widget))) != 0)
48 g_signal_emit(widget, id, 0);
52 void DoCut(GtkWindow* window, WebContents* web_contents) {
53 DoCutCopyPaste(window, web_contents,
54 &RenderFrameHost::Cut, "cut-clipboard");
57 void DoCopy(GtkWindow* window, WebContents* web_contents) {
58 DoCutCopyPaste(window, web_contents,
59 &RenderFrameHost::Copy, "copy-clipboard");
62 void DoPaste(GtkWindow* window, WebContents* web_contents) {
63 DoCutCopyPaste(window, web_contents,
64 &RenderFrameHost::Paste, "paste-clipboard");
67 // Ubuntu patches their version of GTK+ so that there is always a
68 // gripper in the bottom right corner of the window. We dynamically
69 // look up this symbol because it's a non-standard Ubuntu extension to
70 // GTK+. We always need to disable this feature since we can't
71 // communicate this to WebKit easily.
72 typedef void (*gtk_window_set_has_resize_grip_func)(GtkWindow*, gboolean);
73 gtk_window_set_has_resize_grip_func gtk_window_set_has_resize_grip_sym;
75 void DisableResizeGrip(GtkWindow* window) {
76 static bool resize_grip_looked_up = false;
77 if (!resize_grip_looked_up) {
78 resize_grip_looked_up = true;
79 gtk_window_set_has_resize_grip_sym =
80 reinterpret_cast<gtk_window_set_has_resize_grip_func>(
81 dlsym(NULL, "gtk_window_set_has_resize_grip"));
83 if (gtk_window_set_has_resize_grip_sym)
84 gtk_window_set_has_resize_grip_sym(window, FALSE);
87 GdkCursorType GdkWindowEdgeToGdkCursorType(GdkWindowEdge edge) {
89 case GDK_WINDOW_EDGE_NORTH_WEST:
90 return GDK_TOP_LEFT_CORNER;
91 case GDK_WINDOW_EDGE_NORTH:
93 case GDK_WINDOW_EDGE_NORTH_EAST:
94 return GDK_TOP_RIGHT_CORNER;
95 case GDK_WINDOW_EDGE_WEST:
97 case GDK_WINDOW_EDGE_EAST:
98 return GDK_RIGHT_SIDE;
99 case GDK_WINDOW_EDGE_SOUTH_WEST:
100 return GDK_BOTTOM_LEFT_CORNER;
101 case GDK_WINDOW_EDGE_SOUTH:
102 return GDK_BOTTOM_SIDE;
103 case GDK_WINDOW_EDGE_SOUTH_EAST:
104 return GDK_BOTTOM_RIGHT_CORNER;
108 return GDK_LAST_CURSOR;
111 bool BoundsMatchMonitorSize(GtkWindow* window, gfx::Rect bounds) {
112 // A screen can be composed of multiple monitors.
113 GdkScreen* screen = gtk_window_get_screen(window);
114 GdkRectangle monitor_size;
116 if (gtk_widget_get_realized(GTK_WIDGET(window))) {
117 // |window| has been realized.
118 gint monitor_num = gdk_screen_get_monitor_at_window(screen,
119 gtk_widget_get_window(GTK_WIDGET(window)));
120 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor_size);
121 return bounds.size() == gfx::Size(monitor_size.width, monitor_size.height);
124 // Make sure the window doesn't match any monitor size. We compare against
125 // all monitors because we don't know which monitor the window is going to
126 // open on before window realized.
127 gint num_monitors = gdk_screen_get_n_monitors(screen);
128 for (gint i = 0; i < num_monitors; ++i) {
129 GdkRectangle monitor_size;
130 gdk_screen_get_monitor_geometry(screen, i, &monitor_size);
131 if (bounds.size() == gfx::Size(monitor_size.width, monitor_size.height))
137 bool HandleTitleBarLeftMousePress(
139 const gfx::Rect& bounds,
140 GdkEventButton* event) {
141 // We want to start a move when the user single clicks, but not start a
142 // move when the user double clicks. However, a double click sends the
143 // following GDK events: GDK_BUTTON_PRESS, GDK_BUTTON_RELEASE,
144 // GDK_BUTTON_PRESS, GDK_2BUTTON_PRESS, GDK_BUTTON_RELEASE. If we
145 // start a gtk_window_begin_move_drag on the second GDK_BUTTON_PRESS,
146 // the call to gtk_window_maximize fails. To work around this, we
147 // keep track of the last click and if it's going to be a double click,
148 // we don't call gtk_window_begin_move_drag.
149 DCHECK(event->type == GDK_BUTTON_PRESS);
150 DCHECK(event->button == 1);
152 static GtkSettings* settings = gtk_settings_get_default();
153 gint double_click_time = 250;
154 gint double_click_distance = 5;
155 g_object_get(G_OBJECT(settings),
156 "gtk-double-click-time", &double_click_time,
157 "gtk-double-click-distance", &double_click_distance,
160 guint32 click_time = event->time - last_click_time;
161 int click_move_x = abs(event->x - last_click_x);
162 int click_move_y = abs(event->y - last_click_y);
164 last_click_time = event->time;
165 last_click_x = static_cast<int>(event->x);
166 last_click_y = static_cast<int>(event->y);
168 if (click_time > static_cast<guint32>(double_click_time) ||
169 click_move_x > double_click_distance ||
170 click_move_y > double_click_distance) {
171 // Ignore drag requests if the window is the size of the screen.
172 // We do this to avoid triggering fullscreen mode in metacity
173 // (without the --no-force-fullscreen flag) and in compiz (with
174 // Legacy Fullscreen Mode enabled).
175 if (!BoundsMatchMonitorSize(window, bounds)) {
176 gtk_window_begin_move_drag(window, event->button,
177 static_cast<gint>(event->x_root),
178 static_cast<gint>(event->y_root),
186 void UnMaximize(GtkWindow* window,
187 const gfx::Rect& bounds,
188 const gfx::Rect& restored_bounds) {
189 gtk_window_unmaximize(window);
191 // It can happen that you end up with a window whose restore size is the same
192 // as the size of the screen, so unmaximizing it merely remaximizes it due to
193 // the same WM feature that SetWindowSize() works around. We try to detect
194 // this and resize the window to work around the issue.
195 if (bounds.size() == restored_bounds.size())
196 gtk_window_resize(window, bounds.width(), bounds.height() - 1);
199 void SetWindowCustomClass(GtkWindow* window, const std::string& wmclass) {
200 gtk_window_set_wmclass(window,
202 gdk_get_program_class());
204 // Set WM_WINDOW_ROLE for session management purposes.
205 // See http://tronche.com/gui/x/icccm/sec-5.html .
206 gtk_window_set_role(window, wmclass.c_str());
209 void SetWindowSize(GtkWindow* window, const gfx::Size& size) {
210 gfx::Size new_size = size;
211 gint current_width = 0;
212 gint current_height = 0;
213 gtk_window_get_size(window, ¤t_width, ¤t_height);
214 GdkRectangle size_with_decorations = {0};
215 GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
217 gdk_window_get_frame_extents(gdk_window,
218 &size_with_decorations);
221 if (current_width == size_with_decorations.width &&
222 current_height == size_with_decorations.height) {
223 // Make sure the window doesn't match any monitor size. We compare against
224 // all monitors because we don't know which monitor the window is going to
225 // open on (the WM decides that).
226 GdkScreen* screen = gtk_window_get_screen(window);
227 gint num_monitors = gdk_screen_get_n_monitors(screen);
228 for (gint i = 0; i < num_monitors; ++i) {
229 GdkRectangle monitor_size;
230 gdk_screen_get_monitor_geometry(screen, i, &monitor_size);
231 if (gfx::Size(monitor_size.width, monitor_size.height) == size) {
232 gtk_window_resize(window, size.width(), size.height() - 1);
237 // gtk_window_resize is the size of the window not including decorations,
238 // but we are given the |size| including window decorations.
239 if (size_with_decorations.width > current_width) {
240 new_size.set_width(size.width() - size_with_decorations.width +
243 if (size_with_decorations.height > current_height) {
244 new_size.set_height(size.height() - size_with_decorations.height +
249 gtk_window_resize(window, new_size.width(), new_size.height());
252 void UpdateWindowPosition(ui::BaseWindow* window,
254 gfx::Rect* restored_bounds) {
256 gtk_window_get_position(window->GetNativeWindow(), &x, &y);
257 (*bounds).set_origin(gfx::Point(x, y));
258 if (!window->IsFullscreen() && !window->IsMaximized())
259 *restored_bounds = *bounds;
262 bool GetWindowEdge(const gfx::Size& window_size,
266 GdkWindowEdge* edge) {
267 gfx::Rect middle(window_size);
268 middle.Inset(kFrameBorderThickness,
269 kFrameBorderThickness - top_edge_inset,
270 kFrameBorderThickness,
271 kFrameBorderThickness);
272 if (middle.Contains(x, y))
275 gfx::Rect north(0, 0, window_size.width(),
276 kResizeAreaCornerSize - top_edge_inset);
277 gfx::Rect west(0, 0, kResizeAreaCornerSize, window_size.height());
278 gfx::Rect south(0, window_size.height() - kResizeAreaCornerSize,
279 window_size.width(), kResizeAreaCornerSize);
280 gfx::Rect east(window_size.width() - kResizeAreaCornerSize, 0,
281 kResizeAreaCornerSize, window_size.height());
283 if (north.Contains(x, y)) {
284 if (west.Contains(x, y))
285 *edge = GDK_WINDOW_EDGE_NORTH_WEST;
286 else if (east.Contains(x, y))
287 *edge = GDK_WINDOW_EDGE_NORTH_EAST;
289 *edge = GDK_WINDOW_EDGE_NORTH;
290 } else if (south.Contains(x, y)) {
291 if (west.Contains(x, y))
292 *edge = GDK_WINDOW_EDGE_SOUTH_WEST;
293 else if (east.Contains(x, y))
294 *edge = GDK_WINDOW_EDGE_SOUTH_EAST;
296 *edge = GDK_WINDOW_EDGE_SOUTH;
298 if (west.Contains(x, y))
299 *edge = GDK_WINDOW_EDGE_WEST;
300 else if (east.Contains(x, y))
301 *edge = GDK_WINDOW_EDGE_EAST;
303 return false; // The cursor must be outside the window.
308 } // namespace gtk_window_util