- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / apps / native_app_window_cocoa.h
1 // Copyright 2013 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 CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
6 #define CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
7
8 #import <Cocoa/Cocoa.h>
9 #include <vector>
10
11 #include "apps/shell_window.h"
12 #include "apps/ui/native_app_window.h"
13 #include "base/mac/scoped_nsobject.h"
14 #include "base/memory/scoped_ptr.h"
15 #import "chrome/browser/ui/cocoa/browser_command_executor.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "extensions/common/draggable_region.h"
18 #include "ui/gfx/rect.h"
19
20 class ExtensionKeybindingRegistryCocoa;
21 class Profile;
22 class NativeAppWindowCocoa;
23 @class ShellNSWindow;
24 class SkRegion;
25
26 // A window controller for a minimal window to host a web app view. Passes
27 // Objective-C notifications to the C++ bridge.
28 @interface NativeAppWindowController : NSWindowController
29                                       <NSWindowDelegate,
30                                        BrowserCommandExecutor> {
31  @private
32   NativeAppWindowCocoa* appWindow_;  // Weak; owns self.
33 }
34
35 @property(assign, nonatomic) NativeAppWindowCocoa* appWindow;
36
37 // Consults the Command Registry to see if this |event| needs to be handled as
38 // an extension command and returns YES if so (NO otherwise).
39 - (BOOL)handledByExtensionCommand:(NSEvent*)event;
40
41 @end
42
43 // Cocoa bridge to AppWindow.
44 class NativeAppWindowCocoa : public apps::NativeAppWindow,
45                              public content::WebContentsObserver {
46  public:
47   NativeAppWindowCocoa(apps::ShellWindow* shell_window,
48                        const apps::ShellWindow::CreateParams& params);
49
50   // ui::BaseWindow implementation.
51   virtual bool IsActive() const OVERRIDE;
52   virtual bool IsMaximized() const OVERRIDE;
53   virtual bool IsMinimized() const OVERRIDE;
54   virtual bool IsFullscreen() const OVERRIDE;
55   virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
56   virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
57   virtual ui::WindowShowState GetRestoredState() const OVERRIDE;
58   virtual gfx::Rect GetBounds() const OVERRIDE;
59   virtual void Show() OVERRIDE;
60   virtual void ShowInactive() OVERRIDE;
61   virtual void Hide() OVERRIDE;
62   virtual void Close() OVERRIDE;
63   virtual void Activate() OVERRIDE;
64   virtual void Deactivate() OVERRIDE;
65   virtual void Maximize() OVERRIDE;
66   virtual void Minimize() OVERRIDE;
67   virtual void Restore() OVERRIDE;
68   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
69   virtual void FlashFrame(bool flash) OVERRIDE;
70   virtual bool IsAlwaysOnTop() const OVERRIDE;
71
72   // Called when the window is about to be closed.
73   void WindowWillClose();
74
75   // Called when the window is focused.
76   void WindowDidBecomeKey();
77
78   // Called when the window is defocused.
79   void WindowDidResignKey();
80
81   // Called when the window finishes resizing, i.e. after zoom/unzoom, after
82   // entering/leaving fullscreen, and after a user is done resizing.
83   void WindowDidFinishResize();
84
85   // Called when the window is resized. This is called repeatedly during a
86   // zoom/unzoom, and while a user is resizing.
87   void WindowDidResize();
88
89   // Called when the window is moved.
90   void WindowDidMove();
91
92   // Called when the window is minimized.
93   void WindowDidMiniaturize();
94
95   // Called when the window is un-minimized.
96   void WindowDidDeminiaturize();
97
98   // Called when the window is zoomed (maximized or de-maximized).
99   void WindowWillZoom();
100
101   // Called to handle a key event.
102   bool HandledByExtensionCommand(NSEvent* event);
103
104   // Called to handle a mouse event.
105   void HandleMouseEvent(NSEvent* event);
106
107   // Returns true if |point| in local Cocoa coordinate system falls within
108   // the draggable region.
109   bool IsWithinDraggableRegion(NSPoint point) const;
110
111   NSRect restored_bounds() const { return restored_bounds_; }
112   bool use_system_drag() const { return use_system_drag_; }
113
114  protected:
115   // NativeAppWindow implementation.
116   virtual void SetFullscreen(bool fullscreen) OVERRIDE;
117   virtual bool IsFullscreenOrPending() const OVERRIDE;
118   virtual bool IsDetached() const OVERRIDE;
119   virtual void UpdateWindowIcon() OVERRIDE;
120   virtual void UpdateWindowTitle() OVERRIDE;
121   virtual void UpdateInputRegion(scoped_ptr<SkRegion> region) OVERRIDE;
122   virtual void UpdateDraggableRegions(
123       const std::vector<extensions::DraggableRegion>& regions) OVERRIDE;
124   virtual SkRegion* GetDraggableRegion() OVERRIDE;
125   virtual void HandleKeyboardEvent(
126       const content::NativeWebKeyboardEvent& event) OVERRIDE;
127   virtual bool IsFrameless() const OVERRIDE;
128   virtual gfx::Insets GetFrameInsets() const OVERRIDE;
129
130   // These are used to simulate Mac-style hide/show. Since windows can be hidden
131   // and shown using the app.window API, this sets is_hidden_with_app_ to
132   // differentiate the reason a window was hidden.
133   virtual void ShowWithApp() OVERRIDE;
134   virtual void HideWithApp() OVERRIDE;
135   // Calls setContent[Min|Max]Size with the current size constraints.
136   virtual void UpdateWindowMinMaxSize() OVERRIDE;
137
138   // WebContentsObserver implementation.
139   virtual void RenderViewHostChanged(
140       content::RenderViewHost* old_host,
141       content::RenderViewHost* new_host) OVERRIDE;
142
143   virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
144
145   // WebContentsModalDialogHost implementation.
146   virtual gfx::NativeView GetHostView() const OVERRIDE;
147   virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
148   virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
149   virtual void AddObserver(
150       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
151   virtual void RemoveObserver(
152       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
153
154  private:
155   virtual ~NativeAppWindowCocoa();
156
157   ShellNSWindow* window() const;
158
159   content::WebContents* web_contents() const {
160     return shell_window_->web_contents();
161   }
162   const extensions::Extension* extension() const {
163     return shell_window_->extension();
164   }
165
166   // Returns the WindowStyleMask based on the type of window frame.
167   // Specifically, this includes NSResizableWindowMask if the window is
168   // resizable, and does not include NSTexturedBackgroundWindowMask when a
169   // native frame is used.
170   NSUInteger GetWindowStyleMask() const;
171
172   void InstallView();
173   void UninstallView();
174   void InstallDraggableRegionViews();
175   void UpdateDraggableRegionsForSystemDrag(
176       const std::vector<extensions::DraggableRegion>& regions,
177       const extensions::DraggableRegion* draggable_area);
178   void UpdateDraggableRegionsForCustomDrag(
179       const std::vector<extensions::DraggableRegion>& regions);
180
181   // Cache |restored_bounds_| only if the window is currently restored.
182   void UpdateRestoredBounds();
183
184   // Hides the window unconditionally. Used by Hide and HideWithApp.
185   void HideWithoutMarkingHidden();
186
187   apps::ShellWindow* shell_window_; // weak - ShellWindow owns NativeAppWindow.
188
189   bool has_frame_;
190
191   // Whether this window is hidden according to the app.window API. This is set
192   // by Hide, Show, and ShowInactive.
193   bool is_hidden_;
194   // Whether this window last became hidden due to a request to hide the entire
195   // app, e.g. via the dock menu or Cmd+H. This is set by Hide/ShowWithApp.
196   bool is_hidden_with_app_;
197
198   bool is_maximized_;
199   bool is_fullscreen_;
200   NSRect restored_bounds_;
201
202   bool shows_resize_controls_;
203   bool shows_fullscreen_controls_;
204
205   base::scoped_nsobject<NativeAppWindowController> window_controller_;
206   NSInteger attention_request_id_;  // identifier from requestUserAttention
207
208   // Indicates whether system drag or custom drag should be used, depending on
209   // the complexity of draggable regions.
210   bool use_system_drag_;
211
212   // For system drag, the whole window is draggable and the non-draggable areas
213   // have to been explicitly excluded.
214   std::vector<gfx::Rect> system_drag_exclude_areas_;
215
216   // For custom drag, the whole window is non-draggable and the draggable region
217   // has to been explicitly provided.
218   scoped_ptr<SkRegion> draggable_region_;  // used in custom drag.
219
220   // Mouse location since the last mouse event, in screen coordinates. This is
221   // used in custom drag to compute the window movement.
222   NSPoint last_mouse_location_;
223
224   // The Extension Command Registry used to determine which keyboard events to
225   // handle.
226   scoped_ptr<ExtensionKeybindingRegistryCocoa> extension_keybinding_registry_;
227
228   DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa);
229 };
230
231 #endif  // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_