#include "browser/native_window_gtk.h"
#include "base/values.h"
+#include "browser/ui/gtk/gtk_window_util.h"
+#include "common/draggable_region.h"
#include "common/options_switches.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/common/renderer_preferences.h"
+#include "ui/base/x/x11_util.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/skia_utils_gtk.h"
base::DictionaryValue* options)
: NativeWindow(web_contents, options),
window_(GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL))),
- fullscreen_(false),
+ state_(GDK_WINDOW_STATE_WITHDRAWN),
is_always_on_top_(false) {
gtk_container_add(GTK_CONTAINER(window_),
GetWebContents()->GetView()->GetNativeView());
options->GetInteger(switches::kHeight, &height);
gtk_window_set_default_size(window_, width, height);
- if (!has_frame_)
- gtk_window_set_decorated(window_, false);
-
if (!icon_.IsEmpty())
gtk_window_set_icon(window_, icon_.ToGdkPixbuf());
+ // In some (older) versions of compiz, raising top-level windows when they
+ // are partially off-screen causes them to get snapped back on screen, not
+ // always even on the current virtual desktop. If we are running under
+ // compiz, suppress such raises, as they are not necessary in compiz anyway.
+ if (ui::GuessWindowManager() == ui::WM_COMPIZ)
+ suppress_window_raise_ = true;
+
g_signal_connect(window_, "delete-event",
G_CALLBACK(OnWindowDeleteEventThunk), this);
g_signal_connect(window_, "focus-out-event",
G_CALLBACK(OnFocusOutThunk), this);
+ if (!has_frame_) {
+ gtk_window_set_decorated(window_, false);
+
+ g_signal_connect(window_, "motion-notify-event",
+ G_CALLBACK(OnMouseMoveEventThunk), this);
+ g_signal_connect(window_, "button-press-event",
+ G_CALLBACK(OnButtonPressThunk), this);
+ }
+
SetWebKitColorStyle();
}
}
void NativeWindowGtk::SetFullscreen(bool fullscreen) {
- fullscreen_ = fullscreen;
if (fullscreen)
gtk_window_fullscreen(window_);
else
}
bool NativeWindowGtk::IsFullscreen() {
- return fullscreen_;
+ return state_ & GDK_WINDOW_STATE_FULLSCREEN;
}
void NativeWindowGtk::SetSize(const gfx::Size& size) {
void NativeWindowGtk::UpdateDraggableRegions(
const std::vector<DraggableRegion>& regions) {
+ // Draggable region is not supported for non-frameless window.
+ if (has_frame_)
+ return;
+
+ SkRegion draggable_region;
+
+ // By default, the whole window is non-draggable. We need to explicitly
+ // include those draggable regions.
+ for (std::vector<DraggableRegion>::const_iterator iter =
+ regions.begin();
+ iter != regions.end(); ++iter) {
+ const DraggableRegion& region = *iter;
+ draggable_region.op(
+ region.bounds.x(),
+ region.bounds.y(),
+ region.bounds.right(),
+ region.bounds.bottom(),
+ region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op);
+ }
+
+ draggable_region_ = draggable_region;
}
void NativeWindowGtk::SetWebKitColorStyle() {
0;
}
+bool NativeWindowGtk::IsMaximized() const {
+ return state_ & GDK_WINDOW_STATE_MAXIMIZED;
+}
+
+bool NativeWindowGtk::GetWindowEdge(int x, int y, GdkWindowEdge* edge) {
+ if (has_frame_)
+ return false;
+
+ if (IsMaximized() || IsFullscreen())
+ return false;
+
+ return gtk_window_util::GetWindowEdge(GetSize(), 0, x, y, edge);
+}
+
gboolean NativeWindowGtk::OnWindowDeleteEvent(GtkWidget* widget,
GdkEvent* event) {
Close();
return FALSE;
}
+gboolean NativeWindowGtk::OnWindowState(GtkWidget* window,
+ GdkEventWindowState* event) {
+ state_ = event->new_window_state;
+ return FALSE;
+}
+
+gboolean NativeWindowGtk::OnMouseMoveEvent(GtkWidget* widget,
+ GdkEventMotion* event) {
+ if (!IsResizable())
+ return FALSE;
+
+ int win_x, win_y;
+ GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window_));
+ gdk_window_get_origin(gdk_window, &win_x, &win_y);
+ gfx::Point point(static_cast<int>(event->x_root - win_x),
+ static_cast<int>(event->y_root - win_y));
+
+ // Update the cursor if we're on the custom frame border.
+ GdkWindowEdge edge;
+ bool has_hit_edge = GetWindowEdge(point.x(), point.y(), &edge);
+ GdkCursorType new_cursor = GDK_LAST_CURSOR;
+ if (has_hit_edge)
+ new_cursor = gtk_window_util::GdkWindowEdgeToGdkCursorType(edge);
+
+ GdkCursorType last_cursor = GDK_LAST_CURSOR;
+ if (frame_cursor_)
+ last_cursor = frame_cursor_->type;
+
+ if (last_cursor != new_cursor) {
+ frame_cursor_ = has_hit_edge ? gfx::GetCursor(new_cursor) : NULL;
+ gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(window_)),
+ frame_cursor_);
+ }
+ return FALSE;
+}
+
+gboolean NativeWindowGtk::OnButtonPress(GtkWidget* widget,
+ GdkEventButton* event) {
+ // Make the button press coordinate relative to the browser window.
+ int win_x, win_y;
+ GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window_));
+ gdk_window_get_origin(gdk_window, &win_x, &win_y);
+
+ bool resizable = IsResizable();
+ GdkWindowEdge edge;
+ gfx::Point point(static_cast<int>(event->x_root - win_x),
+ static_cast<int>(event->y_root - win_y));
+ bool has_hit_edge = resizable && GetWindowEdge(point.x(), point.y(), &edge);
+ bool has_hit_titlebar = !draggable_region_.isEmpty() &&
+ draggable_region_.contains(event->x, event->y);
+
+ if (event->button == 1) {
+ if (GDK_BUTTON_PRESS == event->type) {
+ // Raise the window after a click on either the titlebar or the border to
+ // match the behavior of most window managers, unless that behavior has
+ // been suppressed.
+ if ((has_hit_titlebar || has_hit_edge) && !suppress_window_raise_)
+ gdk_window_raise(GTK_WIDGET(widget)->window);
+
+ if (has_hit_edge) {
+ gtk_window_begin_resize_drag(window_, edge, event->button,
+ static_cast<gint>(event->x_root),
+ static_cast<gint>(event->y_root),
+ event->time);
+ return TRUE;
+ } else if (has_hit_titlebar) {
+ return gtk_window_util::HandleTitleBarLeftMousePress(
+ window_, gfx::Rect(GetPosition(), GetSize()), event);
+ }
+ } else if (GDK_2BUTTON_PRESS == event->type) {
+ if (has_hit_titlebar && resizable) {
+ // Maximize/restore on double click.
+ if (IsMaximized())
+ gtk_window_unmaximize(window_);
+ else
+ gtk_window_maximize(window_);
+ return TRUE;
+ }
+ }
+ } else if (event->button == 2) {
+ if (has_hit_titlebar || has_hit_edge)
+ gdk_window_lower(gdk_window);
+ return TRUE;
+ }
+ return FALSE;
+}
+
// static
NativeWindow* NativeWindow::Create(content::WebContents* web_contents,
base::DictionaryValue* options) {
--- /dev/null
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "browser/ui/gtk/gtk_window_util.h"
+
+#include <dlfcn.h>
+#include "content/public/browser/render_view_host.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_contents_view.h"
+
+using content::RenderWidgetHost;
+using content::WebContents;
+
+namespace gtk_window_util {
+
+const int kFrameBorderThickness = 4;
+const int kResizeAreaCornerSize = 16;
+
+// Keep track of the last click time and the last click position so we can
+// filter out extra GDK_BUTTON_PRESS events when a double click happens.
+static guint32 last_click_time;
+static int last_click_x;
+static int last_click_y;
+
+// Performs Cut/Copy/Paste operation on the |window|.
+// If the current render view is focused, then just call the specified |method|
+// against the current render view host, otherwise emit the specified |signal|
+// against the focused widget.
+// TODO(suzhe): This approach does not work for plugins.
+void DoCutCopyPaste(GtkWindow* window,
+ WebContents* web_contents,
+ void (RenderWidgetHost::*method)(),
+ const char* signal) {
+ GtkWidget* widget = gtk_window_get_focus(window);
+ if (widget == NULL)
+ return; // Do nothing if no focused widget.
+
+ if (web_contents &&
+ widget == web_contents->GetView()->GetContentNativeView()) {
+ (web_contents->GetRenderViewHost()->*method)();
+ } else {
+ guint id;
+ if ((id = g_signal_lookup(signal, G_OBJECT_TYPE(widget))) != 0)
+ g_signal_emit(widget, id, 0);
+ }
+}
+
+void DoCut(GtkWindow* window, WebContents* web_contents) {
+ DoCutCopyPaste(window, web_contents,
+ &RenderWidgetHost::Cut, "cut-clipboard");
+}
+
+void DoCopy(GtkWindow* window, WebContents* web_contents) {
+ DoCutCopyPaste(window, web_contents,
+ &RenderWidgetHost::Copy, "copy-clipboard");
+}
+
+void DoPaste(GtkWindow* window, WebContents* web_contents) {
+ DoCutCopyPaste(window, web_contents,
+ &RenderWidgetHost::Paste, "paste-clipboard");
+}
+
+// Ubuntu patches their version of GTK+ so that there is always a
+// gripper in the bottom right corner of the window. We dynamically
+// look up this symbol because it's a non-standard Ubuntu extension to
+// GTK+. We always need to disable this feature since we can't
+// communicate this to WebKit easily.
+typedef void (*gtk_window_set_has_resize_grip_func)(GtkWindow*, gboolean);
+gtk_window_set_has_resize_grip_func gtk_window_set_has_resize_grip_sym;
+
+void DisableResizeGrip(GtkWindow* window) {
+ static bool resize_grip_looked_up = false;
+ if (!resize_grip_looked_up) {
+ resize_grip_looked_up = true;
+ gtk_window_set_has_resize_grip_sym =
+ reinterpret_cast<gtk_window_set_has_resize_grip_func>(
+ dlsym(NULL, "gtk_window_set_has_resize_grip"));
+ }
+ if (gtk_window_set_has_resize_grip_sym)
+ gtk_window_set_has_resize_grip_sym(window, FALSE);
+}
+
+GdkCursorType GdkWindowEdgeToGdkCursorType(GdkWindowEdge edge) {
+ switch (edge) {
+ case GDK_WINDOW_EDGE_NORTH_WEST:
+ return GDK_TOP_LEFT_CORNER;
+ case GDK_WINDOW_EDGE_NORTH:
+ return GDK_TOP_SIDE;
+ case GDK_WINDOW_EDGE_NORTH_EAST:
+ return GDK_TOP_RIGHT_CORNER;
+ case GDK_WINDOW_EDGE_WEST:
+ return GDK_LEFT_SIDE;
+ case GDK_WINDOW_EDGE_EAST:
+ return GDK_RIGHT_SIDE;
+ case GDK_WINDOW_EDGE_SOUTH_WEST:
+ return GDK_BOTTOM_LEFT_CORNER;
+ case GDK_WINDOW_EDGE_SOUTH:
+ return GDK_BOTTOM_SIDE;
+ case GDK_WINDOW_EDGE_SOUTH_EAST:
+ return GDK_BOTTOM_RIGHT_CORNER;
+ default:
+ NOTREACHED();
+ }
+ return GDK_LAST_CURSOR;
+}
+
+bool BoundsMatchMonitorSize(GtkWindow* window, gfx::Rect bounds) {
+ // A screen can be composed of multiple monitors.
+ GdkScreen* screen = gtk_window_get_screen(window);
+ GdkRectangle monitor_size;
+
+ if (gtk_widget_get_realized(GTK_WIDGET(window))) {
+ // |window| has been realized.
+ gint monitor_num = gdk_screen_get_monitor_at_window(screen,
+ gtk_widget_get_window(GTK_WIDGET(window)));
+ gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor_size);
+ return bounds.size() == gfx::Size(monitor_size.width, monitor_size.height);
+ }
+
+ // Make sure the window doesn't match any monitor size. We compare against
+ // all monitors because we don't know which monitor the window is going to
+ // open on before window realized.
+ gint num_monitors = gdk_screen_get_n_monitors(screen);
+ for (gint i = 0; i < num_monitors; ++i) {
+ GdkRectangle monitor_size;
+ gdk_screen_get_monitor_geometry(screen, i, &monitor_size);
+ if (bounds.size() == gfx::Size(monitor_size.width, monitor_size.height))
+ return true;
+ }
+ return false;
+}
+
+bool HandleTitleBarLeftMousePress(
+ GtkWindow* window,
+ const gfx::Rect& bounds,
+ GdkEventButton* event) {
+ // We want to start a move when the user single clicks, but not start a
+ // move when the user double clicks. However, a double click sends the
+ // following GDK events: GDK_BUTTON_PRESS, GDK_BUTTON_RELEASE,
+ // GDK_BUTTON_PRESS, GDK_2BUTTON_PRESS, GDK_BUTTON_RELEASE. If we
+ // start a gtk_window_begin_move_drag on the second GDK_BUTTON_PRESS,
+ // the call to gtk_window_maximize fails. To work around this, we
+ // keep track of the last click and if it's going to be a double click,
+ // we don't call gtk_window_begin_move_drag.
+ DCHECK(event->type == GDK_BUTTON_PRESS);
+ DCHECK(event->button == 1);
+
+ static GtkSettings* settings = gtk_settings_get_default();
+ gint double_click_time = 250;
+ gint double_click_distance = 5;
+ g_object_get(G_OBJECT(settings),
+ "gtk-double-click-time", &double_click_time,
+ "gtk-double-click-distance", &double_click_distance,
+ NULL);
+
+ guint32 click_time = event->time - last_click_time;
+ int click_move_x = abs(event->x - last_click_x);
+ int click_move_y = abs(event->y - last_click_y);
+
+ last_click_time = event->time;
+ last_click_x = static_cast<int>(event->x);
+ last_click_y = static_cast<int>(event->y);
+
+ if (click_time > static_cast<guint32>(double_click_time) ||
+ click_move_x > double_click_distance ||
+ click_move_y > double_click_distance) {
+ // Ignore drag requests if the window is the size of the screen.
+ // We do this to avoid triggering fullscreen mode in metacity
+ // (without the --no-force-fullscreen flag) and in compiz (with
+ // Legacy Fullscreen Mode enabled).
+ if (!BoundsMatchMonitorSize(window, bounds)) {
+ gtk_window_begin_move_drag(window, event->button,
+ static_cast<gint>(event->x_root),
+ static_cast<gint>(event->y_root),
+ event->time);
+ }
+ return TRUE;
+ }
+ return FALSE;
+}
+
+void UnMaximize(GtkWindow* window,
+ const gfx::Rect& bounds,
+ const gfx::Rect& restored_bounds) {
+ gtk_window_unmaximize(window);
+
+ // It can happen that you end up with a window whose restore size is the same
+ // as the size of the screen, so unmaximizing it merely remaximizes it due to
+ // the same WM feature that SetWindowSize() works around. We try to detect
+ // this and resize the window to work around the issue.
+ if (bounds.size() == restored_bounds.size())
+ gtk_window_resize(window, bounds.width(), bounds.height() - 1);
+}
+
+void SetWindowCustomClass(GtkWindow* window, const std::string& wmclass) {
+ gtk_window_set_wmclass(window,
+ wmclass.c_str(),
+ gdk_get_program_class());
+
+ // Set WM_WINDOW_ROLE for session management purposes.
+ // See http://tronche.com/gui/x/icccm/sec-5.html .
+ gtk_window_set_role(window, wmclass.c_str());
+}
+
+void SetWindowSize(GtkWindow* window, const gfx::Size& size) {
+ gfx::Size new_size = size;
+ gint current_width = 0;
+ gint current_height = 0;
+ gtk_window_get_size(window, ¤t_width, ¤t_height);
+ GdkRectangle size_with_decorations = {0};
+ GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
+ if (gdk_window) {
+ gdk_window_get_frame_extents(gdk_window,
+ &size_with_decorations);
+ }
+
+ if (current_width == size_with_decorations.width &&
+ current_height == size_with_decorations.height) {
+ // Make sure the window doesn't match any monitor size. We compare against
+ // all monitors because we don't know which monitor the window is going to
+ // open on (the WM decides that).
+ GdkScreen* screen = gtk_window_get_screen(window);
+ gint num_monitors = gdk_screen_get_n_monitors(screen);
+ for (gint i = 0; i < num_monitors; ++i) {
+ GdkRectangle monitor_size;
+ gdk_screen_get_monitor_geometry(screen, i, &monitor_size);
+ if (gfx::Size(monitor_size.width, monitor_size.height) == size) {
+ gtk_window_resize(window, size.width(), size.height() - 1);
+ return;
+ }
+ }
+ } else {
+ // gtk_window_resize is the size of the window not including decorations,
+ // but we are given the |size| including window decorations.
+ if (size_with_decorations.width > current_width) {
+ new_size.set_width(size.width() - size_with_decorations.width +
+ current_width);
+ }
+ if (size_with_decorations.height > current_height) {
+ new_size.set_height(size.height() - size_with_decorations.height +
+ current_height);
+ }
+ }
+
+ gtk_window_resize(window, new_size.width(), new_size.height());
+}
+
+bool GetWindowEdge(const gfx::Size& window_size,
+ int top_edge_inset,
+ int x,
+ int y,
+ GdkWindowEdge* edge) {
+ gfx::Rect middle(window_size);
+ middle.Inset(kFrameBorderThickness,
+ kFrameBorderThickness - top_edge_inset,
+ kFrameBorderThickness,
+ kFrameBorderThickness);
+ if (middle.Contains(x, y))
+ return false;
+
+ gfx::Rect north(0, 0, window_size.width(),
+ kResizeAreaCornerSize - top_edge_inset);
+ gfx::Rect west(0, 0, kResizeAreaCornerSize, window_size.height());
+ gfx::Rect south(0, window_size.height() - kResizeAreaCornerSize,
+ window_size.width(), kResizeAreaCornerSize);
+ gfx::Rect east(window_size.width() - kResizeAreaCornerSize, 0,
+ kResizeAreaCornerSize, window_size.height());
+
+ if (north.Contains(x, y)) {
+ if (west.Contains(x, y))
+ *edge = GDK_WINDOW_EDGE_NORTH_WEST;
+ else if (east.Contains(x, y))
+ *edge = GDK_WINDOW_EDGE_NORTH_EAST;
+ else
+ *edge = GDK_WINDOW_EDGE_NORTH;
+ } else if (south.Contains(x, y)) {
+ if (west.Contains(x, y))
+ *edge = GDK_WINDOW_EDGE_SOUTH_WEST;
+ else if (east.Contains(x, y))
+ *edge = GDK_WINDOW_EDGE_SOUTH_EAST;
+ else
+ *edge = GDK_WINDOW_EDGE_SOUTH;
+ } else {
+ if (west.Contains(x, y))
+ *edge = GDK_WINDOW_EDGE_WEST;
+ else if (east.Contains(x, y))
+ *edge = GDK_WINDOW_EDGE_EAST;
+ else
+ return false; // The cursor must be outside the window.
+ }
+ return true;
+}
+
+} // namespace gtk_window_util
--- /dev/null
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_UI_GTK_GTK_WINDOW_UTIL_H_
+#define ATOM_BROWSER_UI_GTK_GTK_WINDOW_UTIL_H_
+
+#include <gtk/gtk.h>
+#include <gdk/gdk.h>
+#include "ui/gfx/rect.h"
+
+namespace content {
+class WebContents;
+}
+
+namespace gtk_window_util {
+
+// The frame border is only visible in restored mode and is hardcoded to 4 px
+// on each side regardless of the system window border size.
+extern const int kFrameBorderThickness;
+// In the window corners, the resize areas don't actually expand bigger, but
+// the 16 px at the end of each edge triggers diagonal resizing.
+extern const int kResizeAreaCornerSize;
+
+// Performs Cut/Copy/Paste operation on the |window|'s |web_contents|.
+void DoCut(GtkWindow* window, content::WebContents* web_contents);
+void DoCopy(GtkWindow* window, content::WebContents* web_contents);
+void DoPaste(GtkWindow* window, content::WebContents* web_contents);
+
+// Ubuntu patches their version of GTK+ to that there is always a
+// gripper in the bottom right corner of the window. We always need to
+// disable this feature since we can't communicate this to WebKit easily.
+void DisableResizeGrip(GtkWindow* window);
+
+// Returns the resize cursor corresponding to the window |edge|.
+GdkCursorType GdkWindowEdgeToGdkCursorType(GdkWindowEdge edge);
+
+// Returns |true| if the window bounds match the monitor size.
+bool BoundsMatchMonitorSize(GtkWindow* window, gfx::Rect bounds);
+
+bool HandleTitleBarLeftMousePress(GtkWindow* window,
+ const gfx::Rect& bounds,
+ GdkEventButton* event);
+
+// Request the underlying window to unmaximize. Also tries to work around
+// a window manager "feature" that can prevent this in some edge cases.
+void UnMaximize(GtkWindow* window,
+ const gfx::Rect& bounds,
+ const gfx::Rect& restored_bounds);
+
+// Set a custom WM_CLASS for a window.
+void SetWindowCustomClass(GtkWindow* window, const std::string& wmclass);
+
+// A helper method for setting the GtkWindow size that should be used in place
+// of calling gtk_window_resize directly. This is done to avoid a WM "feature"
+// where setting the window size to the monitor size causes the WM to set the
+// EWMH for full screen mode.
+void SetWindowSize(GtkWindow* window, const gfx::Size& size);
+
+// If the point (|x|, |y|) is within the resize border area of the window,
+// returns true and sets |edge| to the appropriate GdkWindowEdge value.
+// Otherwise, returns false.
+// |top_edge_inset| specifies how much smaller (in px) than the default edge
+// size the top edge should be, used by browser windows to make it easier to
+// move the window since a lot of title bar space is taken by the tabs.
+bool GetWindowEdge(const gfx::Size& window_size,
+ int top_edge_inset,
+ int x,
+ int y,
+ GdkWindowEdge* edge);
+
+} // namespace gtk_window_util
+
+#endif // ATOM_BROWSER_UI_GTK_GTK_WINDOW_UTIL_H_