1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// Copyright (c) 2009 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 "views/controls/native/native_view_host_gtk.h"
#include <gtk/gtk.h>
#include "base/logging.h"
#include "views/controls/native/native_view_host.h"
#include "views/widget/widget_gtk.h"
namespace views {
////////////////////////////////////////////////////////////////////////////////
// NativeViewHostGtk, public:
NativeViewHostGtk::NativeViewHostGtk(NativeViewHost* host)
: host_(host),
installed_clip_(false),
destroy_signal_id_(0) {
}
NativeViewHostGtk::~NativeViewHostGtk() {
}
// static
View* NativeViewHostGtk::GetViewForNative(GtkWidget* widget) {
gpointer user_data = g_object_get_data(G_OBJECT(widget), "chrome-view");
return static_cast<View*>(user_data);
}
// static
void NativeViewHostGtk::SetViewForNative(GtkWidget* widget, View* view) {
g_object_set_data(G_OBJECT(widget), "chrome-view", view);
}
////////////////////////////////////////////////////////////////////////////////
// NativeViewHostGtk, NativeViewHostWrapper implementation:
void NativeViewHostGtk::NativeViewAttached() {
DCHECK(host_->native_view());
GtkWidget* current_parent = gtk_widget_get_parent(host_->native_view());
GtkWidget* new_parent = host_->GetWidget()->GetNativeView();
// Only adjust the parent if the parent actually changed.
if (current_parent != new_parent) {
// First hide the new window. We don't want anything to draw (like sub-hwnd
// borders), when we change the parent below.
gtk_widget_hide(host_->native_view());
if (current_parent) {
gtk_container_remove(GTK_CONTAINER(current_parent),
host_->native_view());
}
// Adds a mapping between the GtkWidget and us.
SetViewForNative(host_->native_view(), host_);
if (!destroy_signal_id_) {
destroy_signal_id_ = g_signal_connect(G_OBJECT(host_->native_view()),
"destroy", G_CALLBACK(CallDestroy),
NULL);
}
// Set the parent.
static_cast<WidgetGtk*>(host_->GetWidget())->AddChild(host_->native_view());
}
// Always layout though.
host_->Layout();
// TODO(port): figure out focus.
// FocusManager::InstallFocusSubclass(
// hwnd, associated_focus_view()_ ? associated_focus_view() : this);
}
void NativeViewHostGtk::NativeViewDetaching() {
DCHECK(host_->native_view());
g_signal_handler_disconnect(G_OBJECT(host_->native_view()),
destroy_signal_id_);
destroy_signal_id_ = 0;
// TODO(port): focus.
// FocusManager::UninstallFocusSubclass(native_view());
installed_clip_ = false;
}
void NativeViewHostGtk::AddedToWidget() {
if (!host_->native_view())
return;
WidgetGtk* parent_widget = static_cast<WidgetGtk*>(host_->GetWidget());
GtkWidget* widget_parent = gtk_widget_get_parent(host_->native_view());
GtkWidget* parent_widget_widget = parent_widget->child_widget_parent();
if (widget_parent != parent_widget_widget) {
g_object_ref(host_->native_view());
if (widget_parent)
gtk_container_remove(GTK_CONTAINER(widget_parent), host_->native_view());
gtk_container_add(GTK_CONTAINER(parent_widget_widget),
host_->native_view());
g_object_unref(host_->native_view());
}
if (host_->IsVisibleInRootView())
gtk_widget_show(host_->native_view());
else
gtk_widget_hide(host_->native_view());
host_->Layout();
}
void NativeViewHostGtk::RemovedFromWidget() {
if (!host_->native_view())
return;
WidgetGtk* parent_widget = static_cast<WidgetGtk*>(host_->GetWidget());
gtk_widget_hide(host_->native_view());
if (parent_widget) {
gtk_container_remove(GTK_CONTAINER(parent_widget->child_widget_parent()),
host_->native_view());
}
}
void NativeViewHostGtk::InstallClip(int x, int y, int w, int h) {
DCHECK(w > 0 && h > 0);
bool has_window =
(GTK_WIDGET_FLAGS(host_->native_view()) & GTK_NO_WINDOW) == 0;
if (!has_window) {
// Clip is only supported on GtkWidgets that have windows. If this becomes
// an issue (as it may be in the options dialog) we'll need to wrap the
// widget in a GtkFixed with a window. We have to do this as not all widgets
// support turning on GTK_NO_WINDOW (for example, buttons don't appear to
// draw anything when they have a window).
// NOTREACHED();
return;
}
DCHECK(has_window);
// Unset the current region.
gdk_window_shape_combine_region(host_->native_view()->window, NULL, 0, 0);
// Set a new region.
// TODO: using shapes is a bit expensive. Should investigated if there is
// another more efficient way to accomplish this.
GdkRectangle clip_rect = { x, y, w, h };
GdkRegion* clip_region = gdk_region_rectangle(&clip_rect);
gdk_window_shape_combine_region(host_->native_view()->window, clip_region, x,
y);
gdk_region_destroy(clip_region);
installed_clip_ = true;
}
bool NativeViewHostGtk::HasInstalledClip() {
return installed_clip_;
}
void NativeViewHostGtk::UninstallClip() {
gtk_widget_shape_combine_mask(host_->native_view(), NULL, 0, 0);
installed_clip_ = false;
}
void NativeViewHostGtk::ShowWidget(int x, int y, int w, int h) {
WidgetGtk* parent = static_cast<WidgetGtk*>(host_->GetWidget());
parent->PositionChild(host_->native_view(), x, y, w, h);
gtk_widget_show(host_->native_view());
}
void NativeViewHostGtk::HideWidget() {
gtk_widget_hide(host_->native_view());
}
void NativeViewHostGtk::SetFocus() {
NOTIMPLEMENTED();
}
////////////////////////////////////////////////////////////////////////////////
// NativeViewHostGtk, private:
// static
void NativeViewHostGtk::CallDestroy(GtkObject* object) {
View* view = GetViewForNative(GTK_WIDGET(object));
if (!view)
return;
return static_cast<NativeViewHost*>(view)->NativeViewDestroyed();
}
////////////////////////////////////////////////////////////////////////////////
// NativeViewHostWrapper, public:
// static
NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper(
NativeViewHost* host) {
return new NativeViewHostGtk(host);
}
} // namespace views
|