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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
// Copyright (c) 2011 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/widget/native_widget_views.h"
#include "ui/gfx/compositor/compositor.h"
#include "views/desktop/desktop_window_view.h"
#include "views/view.h"
#include "views/views_delegate.h"
#include "views/widget/native_widget_view.h"
#include "views/widget/root_view.h"
#if defined(HAVE_IBUS)
#include "views/ime/input_method_ibus.h"
#else
#include "views/ime/mock_input_method.h"
#endif
namespace views {
////////////////////////////////////////////////////////////////////////////////
// NativeWidgetViews, public:
NativeWidgetViews::NativeWidgetViews(internal::NativeWidgetDelegate* delegate)
: delegate_(delegate),
view_(NULL),
active_(false),
minimized_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(close_widget_factory_(this)),
hosting_widget_(NULL),
ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET),
delete_native_view_(true) {
}
NativeWidgetViews::~NativeWidgetViews() {
delegate_->OnNativeWidgetDestroying();
// We must prevent the NativeWidgetView from attempting to delete us.
if (view_) {
view_->set_delete_native_widget(false);
if (delete_native_view_)
delete view_;
}
delegate_->OnNativeWidgetDestroyed();
if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
delete delegate_;
}
View* NativeWidgetViews::GetView() {
return view_;
}
const View* NativeWidgetViews::GetView() const {
return view_;
}
void NativeWidgetViews::OnActivate(bool active) {
if (active_ == active)
return;
active_ = active;
delegate_->OnNativeWidgetActivationChanged(active);
InputMethod* input_method = GetInputMethodNative();
// TODO(oshima): Focus change should be separated from window activation.
// This will be fixed when we have WM API.
if (active) {
input_method->OnFocus();
// See description of got_initial_focus_in_ for details on this.
GetWidget()->GetFocusManager()->RestoreFocusedView();
} else {
input_method->OnBlur();
GetWidget()->GetFocusManager()->StoreFocusedView();
}
view_->SchedulePaint();
}
bool NativeWidgetViews::OnKeyEvent(const KeyEvent& key_event) {
GetInputMethodNative()->DispatchKeyEvent(key_event);
return true;
}
void NativeWidgetViews::DispatchKeyEventPostIME(const KeyEvent& key) {
// TODO(oshima): GTK impl handles menu_key in special way. This may be
// necessary when running under NativeWidgetX.
delegate_->OnKeyEvent(key);
}
////////////////////////////////////////////////////////////////////////////////
// NativeWidgetViews, NativeWidget implementation:
void NativeWidgetViews::InitNativeWidget(const Widget::InitParams& params) {
ownership_ = params.ownership;
View* parent_view = NULL;
if (params.parent_widget) {
hosting_widget_ = params.parent_widget;
parent_view = hosting_widget_->GetChildViewParent();
} else {
parent_view = ViewsDelegate::views_delegate->GetDefaultParentView();
hosting_widget_ = parent_view->GetWidget();
}
view_ = new internal::NativeWidgetView(this);
view_->SetBoundsRect(params.bounds);
view_->SetPaintToLayer(true);
// With the default NATIVE_WIDGET_OWNS_WIDGET ownership, the
// deletion of either of the NativeWidgetViews or NativeWidgetView
// (e.g. via View hierarchy destruction) will delete the other.
// With WIDGET_OWNS_NATIVE_WIDGET, NativeWidgetViews should only
// be deleted by its Widget.
if (ownership_ == Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET)
view_->set_delete_native_widget(false);
parent_view->AddChildView(view_);
// TODO(beng): SetInitParams().
}
NonClientFrameView* NativeWidgetViews::CreateNonClientFrameView() {
return NULL;
}
void NativeWidgetViews::UpdateFrameAfterFrameChange() {
}
bool NativeWidgetViews::ShouldUseNativeFrame() const {
// NOTIMPLEMENTED();
return false;
}
void NativeWidgetViews::FrameTypeChanged() {
}
Widget* NativeWidgetViews::GetWidget() {
return delegate_->AsWidget();
}
const Widget* NativeWidgetViews::GetWidget() const {
return delegate_->AsWidget();
}
gfx::NativeView NativeWidgetViews::GetNativeView() const {
return GetParentNativeWidget()->GetNativeView();
}
gfx::NativeWindow NativeWidgetViews::GetNativeWindow() const {
return GetParentNativeWidget()->GetNativeWindow();
}
Widget* NativeWidgetViews::GetTopLevelWidget() {
// This can get called when this is in the process of being destroyed, and
// view_ has already been unset.
if (!view_)
return GetWidget();
if (view_->parent() == ViewsDelegate::views_delegate->GetDefaultParentView())
return GetWidget();
// During Widget destruction, this function may be called after |view_| is
// detached from a Widget, at which point this NativeWidget's Widget becomes
// the effective toplevel.
Widget* containing_widget = view_->GetWidget();
return containing_widget ? containing_widget->GetTopLevelWidget()
: GetWidget();
}
const ui::Compositor* NativeWidgetViews::GetCompositor() const {
return hosting_widget_->GetCompositor();
}
ui::Compositor* NativeWidgetViews::GetCompositor() {
return hosting_widget_->GetCompositor();
}
void NativeWidgetViews::MarkLayerDirty() {
view_->MarkLayerDirty();
}
void NativeWidgetViews::CalculateOffsetToAncestorWithLayer(gfx::Point* offset,
View** ancestor) {
view_->CalculateOffsetToAncestorWithLayer(offset, ancestor);
}
void NativeWidgetViews::ViewRemoved(View* view) {
internal::NativeWidgetPrivate* parent = GetParentNativeWidget();
if (parent)
parent->ViewRemoved(view);
}
void NativeWidgetViews::SetNativeWindowProperty(const char* name, void* value) {
if (value)
window_properties_[name] = value;
else
window_properties_.erase(name);
}
void* NativeWidgetViews::GetNativeWindowProperty(const char* name) const {
std::map<const char*, void*>::const_iterator iter =
window_properties_.find(name);
return iter != window_properties_.end() ? iter->second : NULL;
}
TooltipManager* NativeWidgetViews::GetTooltipManager() const {
const internal::NativeWidgetPrivate* parent = GetParentNativeWidget();
return parent ? parent->GetTooltipManager() : NULL;
}
bool NativeWidgetViews::IsScreenReaderActive() const {
return GetParentNativeWidget()->IsScreenReaderActive();
}
void NativeWidgetViews::SendNativeAccessibilityEvent(
View* view,
ui::AccessibilityTypes::Event event_type) {
return GetParentNativeWidget()->SendNativeAccessibilityEvent(view,
event_type);
}
void NativeWidgetViews::SetMouseCapture() {
View* parent_root_view = GetParentNativeWidget()->GetWidget()->GetRootView();
static_cast<internal::RootView*>(parent_root_view)->set_capture_view(view_);
GetParentNativeWidget()->SetMouseCapture();
}
void NativeWidgetViews::ReleaseMouseCapture() {
View* parent_root_view = GetParentNativeWidget()->GetWidget()->GetRootView();
static_cast<internal::RootView*>(parent_root_view)->set_capture_view(NULL);
GetParentNativeWidget()->ReleaseMouseCapture();
}
bool NativeWidgetViews::HasMouseCapture() const {
// NOTE: we may need to tweak this to only return true if the parent native
// widget's RootView has us as the capture view.
const internal::NativeWidgetPrivate* parent_widget = GetParentNativeWidget();
if (!parent_widget)
return false;
const internal::RootView* parent_root =
static_cast<const internal::RootView*>(parent_widget->GetWidget()->
GetRootView());
return parent_widget->HasMouseCapture() &&
view_ == parent_root->capture_view();
}
void NativeWidgetViews::SetKeyboardCapture() {
GetParentNativeWidget()->SetKeyboardCapture();
}
void NativeWidgetViews::ReleaseKeyboardCapture() {
GetParentNativeWidget()->ReleaseKeyboardCapture();
}
bool NativeWidgetViews::HasKeyboardCapture() const {
return GetParentNativeWidget()->HasKeyboardCapture();
}
InputMethod* NativeWidgetViews::GetInputMethodNative() {
if (!input_method_.get()) {
#if defined(HAVE_IBUS)
input_method_.reset(static_cast<InputMethod*>(new InputMethodIBus(this)));
#else
// TODO(suzhe|oshima): Figure out what to do on windows.
input_method_.reset(new MockInputMethod(this));
#endif
input_method_->Init(GetWidget());
}
return input_method_.get();
}
void NativeWidgetViews::ReplaceInputMethod(InputMethod* input_method) {
CHECK(input_method);
input_method_.reset(input_method);
input_method->set_delegate(this);
input_method->Init(GetWidget());
}
void NativeWidgetViews::CenterWindow(const gfx::Size& size) {
// TODO(beng): actually center.
GetView()->SetBounds(0, 0, size.width(), size.height());
}
void NativeWidgetViews::GetWindowBoundsAndMaximizedState(
gfx::Rect* bounds,
bool* maximized) const {
*bounds = GetView()->bounds();
*maximized = false;
}
void NativeWidgetViews::SetWindowTitle(const std::wstring& title) {
}
void NativeWidgetViews::SetWindowIcons(const SkBitmap& window_icon,
const SkBitmap& app_icon) {
}
void NativeWidgetViews::SetAccessibleName(const std::wstring& name) {
}
void NativeWidgetViews::SetAccessibleRole(ui::AccessibilityTypes::Role role) {
}
void NativeWidgetViews::SetAccessibleState(
ui::AccessibilityTypes::State state) {
}
void NativeWidgetViews::BecomeModal() {
NOTIMPLEMENTED();
}
gfx::Rect NativeWidgetViews::GetWindowScreenBounds() const {
if (GetWidget() == GetWidget()->GetTopLevelWidget())
return view_->bounds();
gfx::Point origin = view_->bounds().origin();
View::ConvertPointToScreen(view_->parent(), &origin);
return gfx::Rect(origin.x(), origin.y(), view_->width(), view_->height());
}
gfx::Rect NativeWidgetViews::GetClientAreaScreenBounds() const {
return GetWindowScreenBounds();
}
gfx::Rect NativeWidgetViews::GetRestoredBounds() const {
return GetWindowScreenBounds();
}
void NativeWidgetViews::SetBounds(const gfx::Rect& bounds) {
// |bounds| are supplied in the coordinates of the parent.
view_->SetBoundsRect(bounds);
}
void NativeWidgetViews::SetSize(const gfx::Size& size) {
view_->SetSize(size);
}
void NativeWidgetViews::SetBoundsConstrained(const gfx::Rect& bounds,
Widget* other_widget) {
// TODO(beng): honor other_widget.
SetBounds(bounds);
}
void NativeWidgetViews::MoveAbove(gfx::NativeView native_view) {
NOTIMPLEMENTED();
}
void NativeWidgetViews::MoveToTop() {
view_->parent()->ReorderChildView(view_, -1);
}
void NativeWidgetViews::SetShape(gfx::NativeRegion region) {
NOTIMPLEMENTED();
}
void NativeWidgetViews::Close() {
Hide();
if (close_widget_factory_.empty()) {
MessageLoop::current()->PostTask(FROM_HERE,
close_widget_factory_.NewRunnableMethod(&NativeWidgetViews::CloseNow));
}
}
void NativeWidgetViews::CloseNow() {
// reset input_method before destroying widget.
input_method_.reset();
delete view_;
view_ = NULL;
}
void NativeWidgetViews::EnableClose(bool enable) {
}
void NativeWidgetViews::Show() {
view_->SetVisible(true);
GetWidget()->SetInitialFocus();
}
void NativeWidgetViews::Hide() {
view_->SetVisible(false);
}
void NativeWidgetViews::ShowWithState(ShowState state) {
Show();
}
void NativeWidgetViews::ShowMaximizedWithBounds(
const gfx::Rect& restored_bounds) {
Show();
}
bool NativeWidgetViews::IsVisible() const {
return view_->IsVisible();
}
void NativeWidgetViews::Activate() {
// Enable WidgetObserverTest.ActivationChange when this is implemented.
NOTIMPLEMENTED();
}
void NativeWidgetViews::Deactivate() {
NOTIMPLEMENTED();
}
bool NativeWidgetViews::IsActive() const {
return active_;
}
void NativeWidgetViews::SetAlwaysOnTop(bool on_top) {
NOTIMPLEMENTED();
}
void NativeWidgetViews::Maximize() {
NOTIMPLEMENTED();
}
void NativeWidgetViews::Minimize() {
gfx::Rect view_bounds = view_->bounds();
gfx::Rect parent_bounds = view_->parent()->bounds();
restored_bounds_ = view_bounds;
restored_transform_ = view_->GetTransform();
float aspect_ratio = static_cast<float>(view_bounds.width()) /
static_cast<float>(view_bounds.height());
int target_size = 100;
int target_height = target_size;
int target_width = static_cast<int>(aspect_ratio * target_height);
if (target_width > target_size) {
target_width = target_size;
target_height = static_cast<int>(target_width / aspect_ratio);
}
int target_x = 20;
int target_y = parent_bounds.height() - target_size - 20;
view_->SetBounds(
target_x, target_y, view_bounds.width(), view_bounds.height());
ui::Transform transform;
transform.SetScale((float)target_width / (float)view_bounds.width(),
(float)target_height / (float)view_bounds.height());
view_->SetTransform(transform);
minimized_ = true;
}
bool NativeWidgetViews::IsMaximized() const {
// NOTIMPLEMENTED();
return false;
}
bool NativeWidgetViews::IsMinimized() const {
return minimized_;
}
void NativeWidgetViews::Restore() {
minimized_ = false;
view_->SetBoundsRect(restored_bounds_);
view_->SetTransform(restored_transform_);
}
void NativeWidgetViews::SetFullscreen(bool fullscreen) {
NOTIMPLEMENTED();
}
bool NativeWidgetViews::IsFullscreen() const {
// NOTIMPLEMENTED();
return false;
}
void NativeWidgetViews::SetOpacity(unsigned char opacity) {
NOTIMPLEMENTED();
}
void NativeWidgetViews::SetUseDragFrame(bool use_drag_frame) {
NOTIMPLEMENTED();
}
bool NativeWidgetViews::IsAccessibleWidget() const {
NOTIMPLEMENTED();
return false;
}
void NativeWidgetViews::RunShellDrag(View* view,
const ui::OSExchangeData& data,
int operation) {
GetParentNativeWidget()->RunShellDrag(view, data, operation);
}
void NativeWidgetViews::SchedulePaintInRect(const gfx::Rect& rect) {
view_->SchedulePaintInternal(rect);
}
void NativeWidgetViews::SetCursor(gfx::NativeCursor cursor) {
GetParentNativeWidget()->SetCursor(cursor);
}
void NativeWidgetViews::ClearNativeFocus() {
GetParentNativeWidget()->ClearNativeFocus();
}
void NativeWidgetViews::FocusNativeView(gfx::NativeView native_view) {
GetParentNativeWidget()->FocusNativeView(native_view);
}
////////////////////////////////////////////////////////////////////////////////
// NativeWidgetViews, private:
internal::NativeWidgetPrivate* NativeWidgetViews::GetParentNativeWidget() {
Widget* containing_widget = view_ ? view_->GetWidget() : NULL;
return containing_widget ? static_cast<internal::NativeWidgetPrivate*>(
containing_widget->native_widget()) :
NULL;
}
const internal::NativeWidgetPrivate*
NativeWidgetViews::GetParentNativeWidget() const {
const Widget* containing_widget = view_ ? view_->GetWidget() : NULL;
return containing_widget ? static_cast<const internal::NativeWidgetPrivate*>(
containing_widget->native_widget()) :
NULL;
}
} // namespace views
|