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 #ifndef UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_ 6 #define UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_ 7 8 #import <Cocoa/Cocoa.h> 9 10 #import "base/mac/scoped_nsobject.h" 11 #include "base/memory/scoped_ptr.h" 12 #import "ui/views/focus/focus_manager.h" 13 #include "ui/views/ime/input_method_delegate.h" 14 #include "ui/views/views_export.h" 15 #include "ui/views/widget/widget.h" 16 17 @class BridgedContentView; 18 @class ViewsNSWindowDelegate; 19 20 namespace ui { 21 class InputMethod; 22 } 23 24 namespace views { 25 26 class InputMethod; 27 class NativeWidgetMac; 28 class View; 29 30 // A bridge to an NSWindow managed by an instance of NativeWidgetMac or 31 // DesktopNativeWidgetMac. Serves as a helper class to bridge requests from the 32 // NativeWidgetMac to the Cocoa window. Behaves a bit like an aura::Window. 33 class VIEWS_EXPORT BridgedNativeWidget : public internal::InputMethodDelegate, 34 public FocusChangeListener { 35 public: 36 // Creates one side of the bridge. |parent| must not be NULL. 37 explicit BridgedNativeWidget(NativeWidgetMac* parent); 38 virtual ~BridgedNativeWidget(); 39 40 // Initialize the bridge, "retains" ownership of |window|. 41 void Init(base::scoped_nsobject<NSWindow> window, 42 const Widget::InitParams& params); 43 44 // Sets or clears the focus manager to use for tracking focused views. 45 // This does NOT take ownership of |focus_manager|. 46 void SetFocusManager(FocusManager* focus_manager); 47 48 // Set or clears the views::View bridged by the content view. This does NOT 49 // take ownership of |view|. 50 void SetRootView(views::View* view); 51 52 // Called internally by the NSWindowDelegate when the window is closing. 53 void OnWindowWillClose(); 54 55 // See widget.h for documentation. 56 InputMethod* CreateInputMethod(); 57 ui::InputMethod* GetHostInputMethod(); 58 native_widget_mac()59 NativeWidgetMac* native_widget_mac() { return native_widget_mac_; } ns_view()60 BridgedContentView* ns_view() { return bridged_view_; } ns_window()61 NSWindow* ns_window() { return window_; } 62 63 // Overridden from internal::InputMethodDelegate: 64 virtual void DispatchKeyEventPostIME(const ui::KeyEvent& key) OVERRIDE; 65 66 private: 67 // Closes all child windows. BridgedNativeWidget children will be destroyed. 68 void RemoveOrDestroyChildren(); 69 70 views::NativeWidgetMac* native_widget_mac_; // Weak. Owns this. 71 base::scoped_nsobject<NSWindow> window_; 72 base::scoped_nsobject<ViewsNSWindowDelegate> window_delegate_; 73 base::scoped_nsobject<BridgedContentView> bridged_view_; 74 scoped_ptr<ui::InputMethod> input_method_; 75 FocusManager* focus_manager_; // Weak. Owned by our Widget. 76 77 // Overridden from FocusChangeListener: 78 virtual void OnWillChangeFocus(View* focused_before, 79 View* focused_now) OVERRIDE; 80 virtual void OnDidChangeFocus(View* focused_before, 81 View* focused_now) OVERRIDE; 82 83 DISALLOW_COPY_AND_ASSIGN(BridgedNativeWidget); 84 }; 85 86 } // namespace views 87 88 #endif // UI_VIEWS_COCOA_BRIDGED_NATIVE_WIDGET_H_ 89