Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / ui / views / cocoa / bridged_native_widget.mm
1 // Copyright 2014 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 #import "ui/views/cocoa/bridged_native_widget.h"
6
7 #include "base/logging.h"
8 #include "ui/base/ime/input_method.h"
9 #include "ui/base/ime/input_method_factory.h"
10 #include "ui/base/ui_base_switches_util.h"
11 #import "ui/views/cocoa/bridged_content_view.h"
12 #include "ui/views/ime/input_method_bridge.h"
13 #include "ui/views/ime/null_input_method.h"
14 #include "ui/views/view.h"
15 #include "ui/views/widget/widget.h"
16
17 namespace views {
18
19 BridgedNativeWidget::BridgedNativeWidget() {
20 }
21
22 BridgedNativeWidget::~BridgedNativeWidget() {
23   SetRootView(NULL);
24 }
25
26 void BridgedNativeWidget::Init(base::scoped_nsobject<NSWindow> window) {
27   DCHECK(!window_);
28   window_.swap(window);
29 }
30
31 void BridgedNativeWidget::SetRootView(views::View* view) {
32   if (view == [bridged_view_ hostedView])
33     return;
34
35   [bridged_view_ clearView];
36   bridged_view_.reset();
37   // Note that there can still be references to the old |bridged_view_|
38   // floating around in Cocoa libraries at this point. However, references to
39   // the old views::View will be gone, so any method calls will become no-ops.
40
41   if (view) {
42     bridged_view_.reset([[BridgedContentView alloc] initWithView:view]);
43     // Objective C initializers can return nil. However, if |view| is non-NULL
44     // this should be treated as an error and caught early.
45     CHECK(bridged_view_);
46   }
47   [window_ setContentView:bridged_view_];
48 }
49
50 InputMethod* BridgedNativeWidget::CreateInputMethod() {
51   if (switches::IsTextInputFocusManagerEnabled())
52     return new NullInputMethod();
53
54   return new InputMethodBridge(this, GetHostInputMethod(), true);
55 }
56
57 ui::InputMethod* BridgedNativeWidget::GetHostInputMethod() {
58   if (!input_method_) {
59     // Delegate is NULL because Mac IME does not need DispatchKeyEventPostIME
60     // callbacks.
61     input_method_ = ui::CreateInputMethod(NULL, nil);
62   }
63   return input_method_.get();
64 }
65
66 ////////////////////////////////////////////////////////////////////////////////
67 // BridgedNativeWidget, internal::InputMethodDelegate:
68
69 void BridgedNativeWidget::DispatchKeyEventPostIME(const ui::KeyEvent& key) {
70   // Mac key events don't go through this, but some unit tests that use
71   // MockInputMethod do.
72   Widget* widget = [bridged_view_ hostedView]->GetWidget();
73   widget->OnKeyEvent(const_cast<ui::KeyEvent*>(&key));
74   if (!key.handled() && widget->GetFocusManager())
75     widget->GetFocusManager()->OnKeyEvent(key);
76 }
77
78 }  // namespace views