2014-01-15 13:28:00 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2014-01-15 13:28:00 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/native_window_gtk.h"
|
2014-01-15 13:28:00 +00:00
|
|
|
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/common/draggable_region.h"
|
|
|
|
#include "atom/common/options_switches.h"
|
2014-03-29 06:18:47 +00:00
|
|
|
#include "base/environment.h"
|
|
|
|
#include "base/nix/xdg_util.h"
|
2014-03-27 13:03:27 +00:00
|
|
|
#include "chrome/browser/ui/gtk/gtk_window_util.h"
|
2014-04-05 11:21:18 +00:00
|
|
|
#include "content/public/browser/native_web_keyboard_event.h"
|
2014-01-15 13:28:00 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
#include "content/public/browser/web_contents_view.h"
|
2014-01-15 14:38:38 +00:00
|
|
|
#include "content/public/common/renderer_preferences.h"
|
2014-06-23 13:51:42 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2014-03-15 08:16:12 +00:00
|
|
|
#include "ui/base/accelerators/platform_accelerator_gtk.h"
|
|
|
|
#include "ui/base/models/simple_menu_model.h"
|
2014-03-20 09:44:08 +00:00
|
|
|
#include "ui/base/x/active_window_watcher_x.h"
|
2014-01-15 15:15:45 +00:00
|
|
|
#include "ui/base/x/x11_util.h"
|
2014-05-14 08:45:45 +00:00
|
|
|
#include "ui/gfx/font_render_params_linux.h"
|
2014-01-15 14:31:26 +00:00
|
|
|
#include "ui/gfx/gtk_util.h"
|
2014-06-23 14:26:01 +00:00
|
|
|
#include "ui/gfx/image.h"
|
2014-01-15 13:28:00 +00:00
|
|
|
#include "ui/gfx/rect.h"
|
2014-01-15 14:38:38 +00:00
|
|
|
#include "ui/gfx/skia_utils_gtk.h"
|
2014-01-15 13:28:00 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2014-01-15 14:31:26 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Dividing GTK's cursor blink cycle time (in milliseconds) by this value yields
|
|
|
|
// an appropriate value for content::RendererPreferences::caret_blink_interval.
|
|
|
|
// This matches the logic in the WebKit GTK port.
|
|
|
|
const double kGtkCursorBlinkCycleFactor = 2000.0;
|
|
|
|
|
2014-03-29 06:18:47 +00:00
|
|
|
// Substract window border's size from window size according to current window
|
|
|
|
// manager.
|
|
|
|
void SubstractBorderSize(int* width, int* height) {
|
|
|
|
scoped_ptr<base::Environment> env(base::Environment::Create());
|
|
|
|
base::nix::DesktopEnvironment de(base::nix::GetDesktopEnvironment(env.get()));
|
|
|
|
if (de == base::nix::DESKTOP_ENVIRONMENT_UNITY) {
|
|
|
|
*width -= 2;
|
|
|
|
*height -= 29;
|
|
|
|
} else if (de == base::nix::DESKTOP_ENVIRONMENT_GNOME) {
|
|
|
|
*width -= 2;
|
|
|
|
*height -= 33;
|
|
|
|
} else if (de == base::nix::DESKTOP_ENVIRONMENT_XFCE) {
|
|
|
|
*width -= 6;
|
|
|
|
*height -= 27;
|
|
|
|
} else {
|
|
|
|
*width -= 2;
|
|
|
|
*height -= 29;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 08:45:45 +00:00
|
|
|
content::RendererPreferencesHintingEnum GetRendererPreferencesHintingEnum(
|
|
|
|
gfx::FontRenderParams::Hinting hinting) {
|
|
|
|
switch (hinting) {
|
|
|
|
case gfx::FontRenderParams::HINTING_NONE:
|
|
|
|
return content::RENDERER_PREFERENCES_HINTING_NONE;
|
|
|
|
case gfx::FontRenderParams::HINTING_SLIGHT:
|
|
|
|
return content::RENDERER_PREFERENCES_HINTING_SLIGHT;
|
|
|
|
case gfx::FontRenderParams::HINTING_MEDIUM:
|
|
|
|
return content::RENDERER_PREFERENCES_HINTING_MEDIUM;
|
|
|
|
case gfx::FontRenderParams::HINTING_FULL:
|
|
|
|
return content::RENDERER_PREFERENCES_HINTING_FULL;
|
|
|
|
default:
|
|
|
|
NOTREACHED() << "Unhandled hinting style " << hinting;
|
|
|
|
return content::RENDERER_PREFERENCES_HINTING_SYSTEM_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
content::RendererPreferencesSubpixelRenderingEnum
|
|
|
|
GetRendererPreferencesSubpixelRenderingEnum(
|
|
|
|
gfx::FontRenderParams::SubpixelRendering subpixel_rendering) {
|
|
|
|
switch (subpixel_rendering) {
|
|
|
|
case gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE:
|
|
|
|
return content::RENDERER_PREFERENCES_SUBPIXEL_RENDERING_NONE;
|
|
|
|
case gfx::FontRenderParams::SUBPIXEL_RENDERING_RGB:
|
|
|
|
return content::RENDERER_PREFERENCES_SUBPIXEL_RENDERING_RGB;
|
|
|
|
case gfx::FontRenderParams::SUBPIXEL_RENDERING_BGR:
|
|
|
|
return content::RENDERER_PREFERENCES_SUBPIXEL_RENDERING_BGR;
|
|
|
|
case gfx::FontRenderParams::SUBPIXEL_RENDERING_VRGB:
|
|
|
|
return content::RENDERER_PREFERENCES_SUBPIXEL_RENDERING_VRGB;
|
|
|
|
case gfx::FontRenderParams::SUBPIXEL_RENDERING_VBGR:
|
|
|
|
return content::RENDERER_PREFERENCES_SUBPIXEL_RENDERING_VBGR;
|
|
|
|
default:
|
|
|
|
NOTREACHED() << "Unhandled subpixel rendering style "
|
|
|
|
<< subpixel_rendering;
|
|
|
|
return content::RENDERER_PREFERENCES_SUBPIXEL_RENDERING_SYSTEM_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 14:31:26 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
NativeWindowGtk::NativeWindowGtk(content::WebContents* web_contents,
|
2014-06-23 13:51:42 +00:00
|
|
|
const mate::Dictionary& options)
|
2014-01-15 13:28:00 +00:00
|
|
|
: NativeWindow(web_contents, options),
|
|
|
|
window_(GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL))),
|
2014-03-14 13:44:09 +00:00
|
|
|
vbox_(gtk_vbox_new(FALSE, 0)),
|
2014-01-15 15:15:45 +00:00
|
|
|
state_(GDK_WINDOW_STATE_WITHDRAWN),
|
2014-03-15 09:07:44 +00:00
|
|
|
is_always_on_top_(false),
|
2014-03-20 09:44:08 +00:00
|
|
|
is_active_(false),
|
2014-03-15 09:07:44 +00:00
|
|
|
suppress_window_raise_(false),
|
2014-03-29 08:07:44 +00:00
|
|
|
has_ever_been_shown_(false),
|
2014-03-15 09:07:44 +00:00
|
|
|
frame_cursor_(NULL) {
|
2014-03-14 13:44:09 +00:00
|
|
|
gtk_container_add(GTK_CONTAINER(window_), vbox_);
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox_),
|
2014-01-15 13:28:00 +00:00
|
|
|
GetWebContents()->GetView()->GetNativeView());
|
|
|
|
|
|
|
|
int width = 800, height = 600;
|
2014-06-23 13:51:42 +00:00
|
|
|
options.Get(switches::kWidth, &width);
|
|
|
|
options.Get(switches::kHeight, &height);
|
2014-03-29 06:18:47 +00:00
|
|
|
|
2014-05-15 07:38:22 +00:00
|
|
|
bool use_content_size = false;
|
2014-06-23 13:51:42 +00:00
|
|
|
options.Get(switches::kUseContentSize, &use_content_size);
|
2014-05-15 07:38:22 +00:00
|
|
|
if (has_frame_ && !use_content_size)
|
2014-03-29 06:18:47 +00:00
|
|
|
SubstractBorderSize(&width, &height);
|
2014-01-15 13:28:00 +00:00
|
|
|
|
2014-03-28 08:01:38 +00:00
|
|
|
// Force a size allocation so the web page of hidden window can have correct
|
|
|
|
// value of $(window).width().
|
|
|
|
GtkAllocation size = { 0, 0, width, height };
|
|
|
|
gtk_widget_show_all(vbox_);
|
|
|
|
gtk_widget_size_allocate(GTK_WIDGET(window_), &size);
|
2014-03-29 08:07:44 +00:00
|
|
|
gtk_window_util::SetWindowSize(window_, gfx::Size(width, height));
|
2014-03-28 08:01:38 +00:00
|
|
|
|
2014-03-29 06:08:25 +00:00
|
|
|
// Create the underlying gdk window.
|
|
|
|
gtk_widget_realize(GTK_WIDGET(window_));
|
|
|
|
|
2014-06-23 14:26:01 +00:00
|
|
|
if (icon_)
|
|
|
|
gtk_window_set_icon(window_, icon_->ToGdkPixbuf());
|
2014-01-15 13:28:00 +00:00
|
|
|
|
2014-03-20 09:44:08 +00:00
|
|
|
ui::ActiveWindowWatcherX::AddObserver(this);
|
|
|
|
|
2014-01-15 15:15:45 +00:00
|
|
|
// 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;
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
g_signal_connect(window_, "delete-event",
|
|
|
|
G_CALLBACK(OnWindowDeleteEventThunk), this);
|
2014-01-15 14:38:38 +00:00
|
|
|
g_signal_connect(window_, "focus-out-event",
|
|
|
|
G_CALLBACK(OnFocusOutThunk), this);
|
2014-05-23 15:00:04 +00:00
|
|
|
g_signal_connect(window_, "focus-in-event",
|
|
|
|
G_CALLBACK(OnFocusInThunk), this);
|
2014-04-08 01:32:44 +00:00
|
|
|
g_signal_connect(window_, "window-state-event",
|
|
|
|
G_CALLBACK(OnWindowStateThunk), this);
|
2014-01-15 14:31:26 +00:00
|
|
|
|
2014-01-15 15:15:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-15 14:31:26 +00:00
|
|
|
SetWebKitColorStyle();
|
2014-05-14 08:45:45 +00:00
|
|
|
SetFontRenderering();
|
2014-01-15 13:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NativeWindowGtk::~NativeWindowGtk() {
|
2014-04-23 04:59:03 +00:00
|
|
|
CloseImmediately();
|
2014-01-15 13:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Close() {
|
|
|
|
CloseWebContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::CloseImmediately() {
|
2014-04-23 04:59:03 +00:00
|
|
|
if (window_ == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-04-23 02:24:46 +00:00
|
|
|
NotifyWindowClosed();
|
2014-04-23 04:59:03 +00:00
|
|
|
ui::ActiveWindowWatcherX::RemoveObserver(this);
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
gtk_widget_destroy(GTK_WIDGET(window_));
|
|
|
|
window_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Move(const gfx::Rect& pos) {
|
|
|
|
gtk_window_move(window_, pos.x(), pos.y());
|
2014-03-27 13:03:27 +00:00
|
|
|
SetSize(pos.size());
|
2014-01-15 13:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Focus(bool focus) {
|
2014-02-21 07:34:38 +00:00
|
|
|
if (!IsVisible())
|
|
|
|
return;
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
if (focus)
|
|
|
|
gtk_window_present(window_);
|
|
|
|
else
|
|
|
|
gdk_window_lower(gtk_widget_get_window(GTK_WIDGET(window_)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindowGtk::IsFocused() {
|
2014-03-20 09:44:08 +00:00
|
|
|
if (ui::ActiveWindowWatcherX::WMSupportsActivation())
|
|
|
|
return is_active_;
|
|
|
|
|
|
|
|
// This still works even though we don't get the activation notification.
|
2014-01-15 13:28:00 +00:00
|
|
|
return gtk_window_is_active(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Show() {
|
2014-03-29 08:07:44 +00:00
|
|
|
has_ever_been_shown_ = true;
|
2014-01-15 13:28:00 +00:00
|
|
|
gtk_widget_show_all(GTK_WIDGET(window_));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Hide() {
|
|
|
|
gtk_widget_hide(GTK_WIDGET(window_));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindowGtk::IsVisible() {
|
|
|
|
return gtk_widget_get_visible(GTK_WIDGET(window_));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Maximize() {
|
|
|
|
gtk_window_maximize(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Unmaximize() {
|
|
|
|
gtk_window_unmaximize(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Minimize() {
|
|
|
|
gtk_window_iconify(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Restore() {
|
|
|
|
gtk_window_present(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::SetFullscreen(bool fullscreen) {
|
|
|
|
if (fullscreen)
|
|
|
|
gtk_window_fullscreen(window_);
|
|
|
|
else
|
|
|
|
gtk_window_unfullscreen(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindowGtk::IsFullscreen() {
|
2014-01-15 15:15:45 +00:00
|
|
|
return state_ & GDK_WINDOW_STATE_FULLSCREEN;
|
2014-01-15 13:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::SetSize(const gfx::Size& size) {
|
2014-03-29 08:07:44 +00:00
|
|
|
// When the window has not been mapped the window size does not include frame.
|
|
|
|
int width = size.width();
|
|
|
|
int height = size.height();
|
|
|
|
if (has_frame_ && !has_ever_been_shown_)
|
|
|
|
SubstractBorderSize(&width, &height);
|
|
|
|
|
|
|
|
gtk_window_util::SetWindowSize(window_, gfx::Size(width, height));
|
2014-01-15 13:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size NativeWindowGtk::GetSize() {
|
|
|
|
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window_));
|
|
|
|
|
|
|
|
GdkRectangle frame_extents;
|
|
|
|
gdk_window_get_frame_extents(gdk_window, &frame_extents);
|
|
|
|
|
|
|
|
return gfx::Size(frame_extents.width, frame_extents.height);
|
|
|
|
}
|
|
|
|
|
2014-05-15 08:05:35 +00:00
|
|
|
void NativeWindowGtk::SetContentSize(const gfx::Size& size) {
|
2014-05-15 08:27:18 +00:00
|
|
|
if (!has_frame_ || !has_ever_been_shown_) {
|
|
|
|
gtk_window_util::SetWindowSize(window_, size);
|
|
|
|
} else {
|
2014-05-15 08:44:56 +00:00
|
|
|
gfx::Size large = GetSize();
|
|
|
|
gfx::Size small = GetContentSize();
|
|
|
|
gfx::Size target(size.width() + large.width() - small.width(),
|
|
|
|
size.height() + large.height() - small.height());
|
2014-05-15 08:27:18 +00:00
|
|
|
gtk_window_util::SetWindowSize(window_, target);
|
|
|
|
}
|
2014-05-15 08:05:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 07:30:04 +00:00
|
|
|
gfx::Size NativeWindowGtk::GetContentSize() {
|
|
|
|
gint width, height;
|
|
|
|
gtk_window_get_size(window_, &width, &height);
|
|
|
|
return gfx::Size(width, height);
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
void NativeWindowGtk::SetMinimumSize(const gfx::Size& size) {
|
|
|
|
minimum_size_ = size;
|
|
|
|
|
|
|
|
GdkGeometry geometry = { 0 };
|
|
|
|
geometry.min_width = size.width();
|
|
|
|
geometry.min_height = size.height();
|
|
|
|
int hints = GDK_HINT_POS | GDK_HINT_MIN_SIZE;
|
|
|
|
gtk_window_set_geometry_hints(
|
|
|
|
window_, GTK_WIDGET(window_), &geometry, (GdkWindowHints)hints);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size NativeWindowGtk::GetMinimumSize() {
|
|
|
|
return minimum_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::SetMaximumSize(const gfx::Size& size) {
|
|
|
|
maximum_size_ = size;
|
|
|
|
|
|
|
|
GdkGeometry geometry = { 0 };
|
|
|
|
geometry.max_width = size.width();
|
|
|
|
geometry.max_height = size.height();
|
|
|
|
int hints = GDK_HINT_POS | GDK_HINT_MAX_SIZE;
|
|
|
|
gtk_window_set_geometry_hints(
|
|
|
|
window_, GTK_WIDGET(window_), &geometry, (GdkWindowHints)hints);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size NativeWindowGtk::GetMaximumSize() {
|
|
|
|
return maximum_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::SetResizable(bool resizable) {
|
|
|
|
// Should request widget size after setting unresizable, otherwise the
|
|
|
|
// window will shrink to a very small size.
|
2014-05-25 03:44:25 +00:00
|
|
|
if (!IsResizable() || !has_ever_been_shown_) {
|
2014-01-15 13:28:00 +00:00
|
|
|
gint width, height;
|
|
|
|
gtk_window_get_size(window_, &width, &height);
|
|
|
|
gtk_widget_set_size_request(GTK_WIDGET(window_), width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_window_set_resizable(window_, resizable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindowGtk::IsResizable() {
|
|
|
|
return gtk_window_get_resizable(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::SetAlwaysOnTop(bool top) {
|
|
|
|
is_always_on_top_ = top;
|
|
|
|
gtk_window_set_keep_above(window_, top ? TRUE : FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindowGtk::IsAlwaysOnTop() {
|
|
|
|
return is_always_on_top_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::Center() {
|
|
|
|
gtk_window_set_position(window_, GTK_WIN_POS_CENTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::SetPosition(const gfx::Point& position) {
|
|
|
|
gtk_window_move(window_, position.x(), position.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Point NativeWindowGtk::GetPosition() {
|
|
|
|
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window_));
|
|
|
|
|
|
|
|
GdkRectangle frame_extents;
|
|
|
|
gdk_window_get_frame_extents(gdk_window, &frame_extents);
|
|
|
|
|
|
|
|
return gfx::Point(frame_extents.x, frame_extents.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::SetTitle(const std::string& title) {
|
|
|
|
gtk_window_set_title(window_, title.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string NativeWindowGtk::GetTitle() {
|
|
|
|
return gtk_window_get_title(window_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeWindowGtk::FlashFrame(bool flash) {
|
|
|
|
gtk_window_set_urgency_hint(window_, flash);
|
|
|
|
}
|
|
|
|
|
2014-06-16 02:29:51 +00:00
|
|
|
void NativeWindowGtk::SetSkipTaskbar(bool skip) {
|
|
|
|
gtk_window_set_skip_taskbar_hint(window_, skip);
|
|
|
|
gtk_window_set_skip_pager_hint(window_, skip);
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
void NativeWindowGtk::SetKiosk(bool kiosk) {
|
|
|
|
SetFullscreen(kiosk);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NativeWindowGtk::IsKiosk() {
|
|
|
|
return IsFullscreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::NativeWindow NativeWindowGtk::GetNativeWindow() {
|
|
|
|
return window_;
|
|
|
|
}
|
|
|
|
|
2014-03-14 13:44:09 +00:00
|
|
|
void NativeWindowGtk::SetMenu(ui::MenuModel* menu_model) {
|
|
|
|
menu_.reset(new ::MenuGtk(this, menu_model, true));
|
|
|
|
gtk_box_pack_start(GTK_BOX(vbox_), menu_->widget(), FALSE, FALSE, 0);
|
|
|
|
gtk_box_reorder_child(GTK_BOX(vbox_), menu_->widget(), 0);
|
2014-05-12 13:31:46 +00:00
|
|
|
gtk_widget_show_all(vbox_);
|
2014-03-15 08:16:12 +00:00
|
|
|
RegisterAccelerators();
|
2014-03-14 13:44:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
void NativeWindowGtk::UpdateDraggableRegions(
|
|
|
|
const std::vector<DraggableRegion>& regions) {
|
2014-01-15 15:15:45 +00:00
|
|
|
// Draggable region is not supported for non-frameless window.
|
|
|
|
if (has_frame_)
|
|
|
|
return;
|
|
|
|
|
2014-03-15 09:07:44 +00:00
|
|
|
draggable_region_.reset(new SkRegion);
|
2014-01-15 15:15:45 +00:00
|
|
|
|
|
|
|
// 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;
|
2014-03-15 09:07:44 +00:00
|
|
|
draggable_region_->op(
|
2014-01-15 15:15:45 +00:00
|
|
|
region.bounds.x(),
|
|
|
|
region.bounds.y(),
|
|
|
|
region.bounds.right(),
|
|
|
|
region.bounds.bottom(),
|
|
|
|
region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op);
|
|
|
|
}
|
2014-01-15 13:28:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 11:21:18 +00:00
|
|
|
void NativeWindowGtk::HandleKeyboardEvent(
|
|
|
|
content::WebContents*,
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
|
|
|
if (event.type == WebKit::WebInputEvent::RawKeyDown) {
|
|
|
|
GdkEventKey* os_event = reinterpret_cast<GdkEventKey*>(event.os_event);
|
|
|
|
ui::Accelerator accelerator = ui::AcceleratorForGdkKeyCodeAndModifier(
|
|
|
|
os_event->keyval, static_cast<GdkModifierType>(os_event->state));
|
|
|
|
accelerator_util::TriggerAcceleratorTableCommand(&accelerator_table_,
|
|
|
|
accelerator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 09:44:08 +00:00
|
|
|
void NativeWindowGtk::ActiveWindowChanged(GdkWindow* active_window) {
|
|
|
|
is_active_ = gtk_widget_get_window(GTK_WIDGET(window_)) == active_window;
|
|
|
|
}
|
|
|
|
|
2014-03-15 08:16:12 +00:00
|
|
|
void NativeWindowGtk::RegisterAccelerators() {
|
|
|
|
DCHECK(menu_);
|
2014-03-15 08:36:29 +00:00
|
|
|
accelerator_table_.clear();
|
|
|
|
accelerator_util::GenerateAcceleratorTable(&accelerator_table_,
|
|
|
|
menu_->model());
|
2014-03-15 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 14:31:26 +00:00
|
|
|
void NativeWindowGtk::SetWebKitColorStyle() {
|
|
|
|
content::RendererPreferences* prefs =
|
|
|
|
GetWebContents()->GetMutableRendererPrefs();
|
|
|
|
GtkStyle* frame_style = gtk_rc_get_style(GTK_WIDGET(window_));
|
|
|
|
prefs->focus_ring_color =
|
|
|
|
gfx::GdkColorToSkColor(frame_style->bg[GTK_STATE_SELECTED]);
|
|
|
|
prefs->thumb_active_color = SkColorSetRGB(244, 244, 244);
|
|
|
|
prefs->thumb_inactive_color = SkColorSetRGB(234, 234, 234);
|
|
|
|
prefs->track_color = SkColorSetRGB(211, 211, 211);
|
|
|
|
|
|
|
|
GtkWidget* url_entry = gtk_entry_new();
|
|
|
|
GtkStyle* entry_style = gtk_rc_get_style(url_entry);
|
|
|
|
prefs->active_selection_bg_color =
|
|
|
|
gfx::GdkColorToSkColor(entry_style->base[GTK_STATE_SELECTED]);
|
|
|
|
prefs->active_selection_fg_color =
|
|
|
|
gfx::GdkColorToSkColor(entry_style->text[GTK_STATE_SELECTED]);
|
|
|
|
prefs->inactive_selection_bg_color =
|
|
|
|
gfx::GdkColorToSkColor(entry_style->base[GTK_STATE_ACTIVE]);
|
|
|
|
prefs->inactive_selection_fg_color =
|
|
|
|
gfx::GdkColorToSkColor(entry_style->text[GTK_STATE_ACTIVE]);
|
|
|
|
gtk_widget_destroy(url_entry);
|
|
|
|
|
|
|
|
const base::TimeDelta cursor_blink_time = gfx::GetCursorBlinkCycle();
|
|
|
|
prefs->caret_blink_interval =
|
|
|
|
cursor_blink_time.InMilliseconds() ?
|
|
|
|
cursor_blink_time.InMilliseconds() / kGtkCursorBlinkCycleFactor :
|
|
|
|
0;
|
|
|
|
}
|
|
|
|
|
2014-05-14 08:45:45 +00:00
|
|
|
void NativeWindowGtk::SetFontRenderering() {
|
|
|
|
content::RendererPreferences* prefs =
|
|
|
|
GetWebContents()->GetMutableRendererPrefs();
|
|
|
|
const gfx::FontRenderParams& params = gfx::GetDefaultWebKitFontRenderParams();
|
|
|
|
prefs->should_antialias_text = params.antialiasing;
|
|
|
|
prefs->use_subpixel_positioning = params.subpixel_positioning;
|
|
|
|
prefs->hinting = GetRendererPreferencesHintingEnum(params.hinting);
|
|
|
|
prefs->use_autohinter = params.autohinter;
|
|
|
|
prefs->use_bitmaps = params.use_bitmaps;
|
|
|
|
prefs->subpixel_rendering =
|
|
|
|
GetRendererPreferencesSubpixelRenderingEnum(params.subpixel_rendering);
|
|
|
|
}
|
|
|
|
|
2014-01-15 15:15:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
gboolean NativeWindowGtk::OnWindowDeleteEvent(GtkWidget* widget,
|
|
|
|
GdkEvent* event) {
|
|
|
|
Close();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-05-21 17:46:13 +00:00
|
|
|
gboolean NativeWindowGtk::OnFocusIn(GtkWidget* window, GdkEventFocus*) {
|
|
|
|
NotifyWindowFocus();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-15 14:38:38 +00:00
|
|
|
gboolean NativeWindowGtk::OnFocusOut(GtkWidget* window, GdkEventFocus*) {
|
|
|
|
NotifyWindowBlur();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-15 15:15:45 +00:00
|
|
|
gboolean NativeWindowGtk::OnWindowState(GtkWidget* window,
|
|
|
|
GdkEventWindowState* event) {
|
|
|
|
state_ = event->new_window_state;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean NativeWindowGtk::OnMouseMoveEvent(GtkWidget* widget,
|
|
|
|
GdkEventMotion* event) {
|
2014-03-15 09:07:44 +00:00
|
|
|
if (has_frame_) {
|
|
|
|
// Reset the cursor.
|
|
|
|
if (frame_cursor_) {
|
|
|
|
frame_cursor_ = NULL;
|
|
|
|
gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(window_)), NULL);
|
|
|
|
}
|
2014-01-15 15:15:45 +00:00
|
|
|
return FALSE;
|
2014-03-15 09:07:44 +00:00
|
|
|
}
|
2014-01-15 15:15:45 +00:00
|
|
|
|
2014-03-15 09:07:44 +00:00
|
|
|
if (!IsResizable())
|
|
|
|
return FALSE;
|
2014-01-15 15:15:45 +00:00
|
|
|
|
|
|
|
// Update the cursor if we're on the custom frame border.
|
|
|
|
GdkWindowEdge edge;
|
2014-03-15 09:07:44 +00:00
|
|
|
bool has_hit_edge = GetWindowEdge(static_cast<int>(event->x),
|
|
|
|
static_cast<int>(event->y), &edge);
|
2014-01-15 15:15:45 +00:00
|
|
|
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) {
|
2014-03-15 09:07:44 +00:00
|
|
|
DCHECK(!has_frame_);
|
2014-01-15 15:15:45 +00:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
GdkWindowEdge edge;
|
|
|
|
gfx::Point point(static_cast<int>(event->x_root - win_x),
|
|
|
|
static_cast<int>(event->y_root - win_y));
|
2014-03-15 09:07:44 +00:00
|
|
|
bool has_hit_edge = IsResizable() &&
|
|
|
|
GetWindowEdge(point.x(), point.y(), &edge);
|
|
|
|
bool has_hit_titlebar =
|
|
|
|
draggable_region_ && draggable_region_->contains(event->x, event->y);
|
2014-01-15 15:15:45 +00:00
|
|
|
|
|
|
|
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) {
|
2014-03-15 09:07:44 +00:00
|
|
|
GdkRectangle window_bounds = {0};
|
|
|
|
gdk_window_get_frame_extents(gdk_window, &window_bounds);
|
|
|
|
gfx::Rect bounds(window_bounds.x, window_bounds.y,
|
|
|
|
window_bounds.width, window_bounds.height);
|
2014-01-15 15:15:45 +00:00
|
|
|
return gtk_window_util::HandleTitleBarLeftMousePress(
|
2014-03-15 09:07:44 +00:00
|
|
|
window_, bounds, event);
|
2014-01-15 15:15:45 +00:00
|
|
|
}
|
|
|
|
} else if (GDK_2BUTTON_PRESS == event->type) {
|
2014-03-15 09:07:44 +00:00
|
|
|
if (has_hit_titlebar && IsResizable()) {
|
2014-01-15 15:15:45 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2014-03-15 09:07:44 +00:00
|
|
|
|
2014-01-15 15:15:45 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:28:00 +00:00
|
|
|
// static
|
|
|
|
NativeWindow* NativeWindow::Create(content::WebContents* web_contents,
|
2014-06-23 13:51:42 +00:00
|
|
|
const mate::Dictionary& options) {
|
2014-01-15 13:28:00 +00:00
|
|
|
return new NativeWindowGtk(web_contents, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|