Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / gtk_window_util.cc
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 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)(),
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     RenderFrameHost* frame = web_contents->GetFocusedFrame();
44     (frame->*method)();
45   } else {
46     guint id;
47     if ((id = g_signal_lookup(signal, G_OBJECT_TYPE(widget))) != 0)
48       g_signal_emit(widget, id, 0);
49   }
50 }
51
52 void DoCut(GtkWindow* window, WebContents* web_contents) {
53   DoCutCopyPaste(window, web_contents,
54                  &RenderFrameHost::Cut, "cut-clipboard");
55 }
56
57 void DoCopy(GtkWindow* window, WebContents* web_contents) {
58   DoCutCopyPaste(window, web_contents,
59                  &RenderFrameHost::Copy, "copy-clipboard");
60 }
61
62 void DoPaste(GtkWindow* window, WebContents* web_contents) {
63   DoCutCopyPaste(window, web_contents,
64                  &RenderFrameHost::Paste, "paste-clipboard");
65 }
66
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;
74
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"));
82   }
83   if (gtk_window_set_has_resize_grip_sym)
84     gtk_window_set_has_resize_grip_sym(window, FALSE);
85 }
86
87 GdkCursorType GdkWindowEdgeToGdkCursorType(GdkWindowEdge edge) {
88   switch (edge) {
89     case GDK_WINDOW_EDGE_NORTH_WEST:
90       return GDK_TOP_LEFT_CORNER;
91     case GDK_WINDOW_EDGE_NORTH:
92       return GDK_TOP_SIDE;
93     case GDK_WINDOW_EDGE_NORTH_EAST:
94       return GDK_TOP_RIGHT_CORNER;
95     case GDK_WINDOW_EDGE_WEST:
96       return GDK_LEFT_SIDE;
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;
105     default:
106       NOTREACHED();
107   }
108   return GDK_LAST_CURSOR;
109 }
110
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;
115
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);
122   }
123
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))
132       return true;
133   }
134   return false;
135 }
136
137 bool HandleTitleBarLeftMousePress(
138     GtkWindow* window,
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);
151
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,
158                NULL);
159
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);
163
164   last_click_time = event->time;
165   last_click_x = static_cast<int>(event->x);
166   last_click_y = static_cast<int>(event->y);
167
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),
179                                  event->time);
180     }
181     return TRUE;
182   }
183   return FALSE;
184 }
185
186 void UnMaximize(GtkWindow* window,
187                 const gfx::Rect& bounds,
188                 const gfx::Rect& restored_bounds) {
189   gtk_window_unmaximize(window);
190
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);
197 }
198
199 void SetWindowCustomClass(GtkWindow* window, const std::string& wmclass) {
200   gtk_window_set_wmclass(window,
201                          wmclass.c_str(),
202                          gdk_get_program_class());
203
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());
207 }
208
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, &current_width, &current_height);
214   GdkRectangle size_with_decorations = {0};
215   GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
216   if (gdk_window) {
217     gdk_window_get_frame_extents(gdk_window,
218                                  &size_with_decorations);
219   }
220
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);
233         return;
234       }
235     }
236   } else {
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 +
241           current_width);
242     }
243     if (size_with_decorations.height > current_height) {
244       new_size.set_height(size.height() - size_with_decorations.height +
245           current_height);
246     }
247   }
248
249   gtk_window_resize(window, new_size.width(), new_size.height());
250 }
251
252 void UpdateWindowPosition(ui::BaseWindow* window,
253                           gfx::Rect* bounds,
254                           gfx::Rect* restored_bounds) {
255   gint x, y;
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;
260 }
261
262 bool GetWindowEdge(const gfx::Size& window_size,
263                    int top_edge_inset,
264                    int x,
265                    int y,
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))
273     return false;
274
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());
282
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;
288     else
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;
295     else
296       *edge = GDK_WINDOW_EDGE_SOUTH;
297   } else {
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;
302     else
303       return false;  // The cursor must be outside the window.
304   }
305   return true;
306 }
307
308 }  // namespace gtk_window_util