Upstream version 7.36.149.0
[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/size_constraints.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 namespace apps {
21 class AppWindow;
22 }
23
24 class ExtensionKeybindingRegistryCocoa;
25 class NativeAppWindowCocoa;
26 @class ShellNSWindow;
27 class SkRegion;
28
29 // A window controller for a minimal window to host a web app view. Passes
30 // Objective-C notifications to the C++ bridge.
31 @interface NativeAppWindowController : NSWindowController
32                                       <NSWindowDelegate,
33                                        BrowserCommandExecutor> {
34  @private
35   NativeAppWindowCocoa* appWindow_;  // Weak; owns self.
36 }
37
38 @property(assign, nonatomic) NativeAppWindowCocoa* appWindow;
39
40 // Consults the Command Registry to see if this |event| needs to be handled as
41 // an extension command and returns YES if so (NO otherwise).
42 - (BOOL)handledByExtensionCommand:(NSEvent*)event;
43
44 @end
45
46 // Cocoa bridge to AppWindow.
47 class NativeAppWindowCocoa : public apps::NativeAppWindow,
48                              public content::WebContentsObserver {
49  public:
50   NativeAppWindowCocoa(apps::AppWindow* app_window,
51                        const apps::AppWindow::CreateParams& params);
52
53   // ui::BaseWindow implementation.
54   virtual bool IsActive() const OVERRIDE;
55   virtual bool IsMaximized() const OVERRIDE;
56   virtual bool IsMinimized() const OVERRIDE;
57   virtual bool IsFullscreen() const OVERRIDE;
58   virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
59   virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
60   virtual ui::WindowShowState GetRestoredState() const OVERRIDE;
61   virtual gfx::Rect GetBounds() const OVERRIDE;
62   virtual void Show() OVERRIDE;
63   virtual void ShowInactive() OVERRIDE;
64   virtual void Hide() OVERRIDE;
65   virtual void Close() OVERRIDE;
66   virtual void Activate() OVERRIDE;
67   virtual void Deactivate() OVERRIDE;
68   virtual void Maximize() OVERRIDE;
69   virtual void Minimize() OVERRIDE;
70   virtual void Restore() OVERRIDE;
71   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
72   virtual void FlashFrame(bool flash) OVERRIDE;
73   virtual bool IsAlwaysOnTop() const OVERRIDE;
74
75   // Called when the window is about to be closed.
76   void WindowWillClose();
77
78   // Called when the window is focused.
79   void WindowDidBecomeKey();
80
81   // Called when the window is defocused.
82   void WindowDidResignKey();
83
84   // Called when the window finishes resizing, i.e. after zoom/unzoom, after
85   // entering/leaving fullscreen, and after a user is done resizing.
86   void WindowDidFinishResize();
87
88   // Called when the window is resized. This is called repeatedly during a
89   // zoom/unzoom, and while a user is resizing.
90   void WindowDidResize();
91
92   // Called when the window is moved.
93   void WindowDidMove();
94
95   // Called when the window is minimized.
96   void WindowDidMiniaturize();
97
98   // Called when the window is un-minimized.
99   void WindowDidDeminiaturize();
100
101   // Called when the window is zoomed (maximized or de-maximized).
102   void WindowWillZoom();
103
104   // Called to handle a key event.
105   bool HandledByExtensionCommand(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
113  protected:
114   // NativeAppWindow implementation.
115   virtual void SetFullscreen(int fullscreen_types) OVERRIDE;
116   virtual bool IsFullscreenOrPending() const OVERRIDE;
117   virtual bool IsDetached() const OVERRIDE;
118   virtual void UpdateWindowIcon() OVERRIDE;
119   virtual void UpdateWindowTitle() OVERRIDE;
120   virtual void UpdateBadgeIcon() OVERRIDE;
121   virtual void UpdateShape(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 bool HasFrameColor() const OVERRIDE;
129   virtual SkColor ActiveFrameColor() const OVERRIDE;
130   virtual SkColor InactiveFrameColor() const OVERRIDE;
131   virtual gfx::Insets GetFrameInsets() const OVERRIDE;
132
133   // These are used to simulate Mac-style hide/show. Since windows can be hidden
134   // and shown using the app.window API, this sets is_hidden_with_app_ to
135   // differentiate the reason a window was hidden.
136   virtual void ShowWithApp() OVERRIDE;
137   virtual void HideWithApp() OVERRIDE;
138   virtual void UpdateShelfMenu() OVERRIDE;
139   virtual gfx::Size GetContentMinimumSize() const OVERRIDE;
140   virtual gfx::Size GetContentMaximumSize() const OVERRIDE;
141   virtual void SetContentSizeConstraints(const gfx::Size& min_size,
142                                          const gfx::Size& max_size) OVERRIDE;
143
144   // WebContentsObserver implementation.
145   virtual void RenderViewCreated(content::RenderViewHost* rvh) OVERRIDE;
146
147   virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
148
149   // WebContentsModalDialogHost implementation.
150   virtual gfx::NativeView GetHostView() const OVERRIDE;
151   virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
152   virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
153   virtual void AddObserver(
154       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
155   virtual void RemoveObserver(
156       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
157
158  private:
159   virtual ~NativeAppWindowCocoa();
160
161   ShellNSWindow* window() const;
162   content::WebContents* WebContents() const;
163
164   // Returns the WindowStyleMask based on the type of window frame.
165   // Specifically, this includes NSResizableWindowMask if the window is
166   // resizable, and does not include NSTexturedBackgroundWindowMask when a
167   // native frame is used.
168   NSUInteger GetWindowStyleMask() const;
169
170   void InstallView();
171   void UninstallView();
172   void UpdateDraggableRegionViews();
173
174   // Cache |restored_bounds_| only if the window is currently restored.
175   void UpdateRestoredBounds();
176
177   // Hides the window unconditionally. Used by Hide and HideWithApp.
178   void HideWithoutMarkingHidden();
179
180   apps::AppWindow* app_window_;  // weak - AppWindow owns NativeAppWindow.
181
182   bool has_frame_;
183
184   // Whether this window is hidden according to the app.window API. This is set
185   // by Hide, Show, and ShowInactive.
186   bool is_hidden_;
187   // Whether this window last became hidden due to a request to hide the entire
188   // app, e.g. via the dock menu or Cmd+H. This is set by Hide/ShowWithApp.
189   bool is_hidden_with_app_;
190
191   bool is_maximized_;
192   bool is_fullscreen_;
193   NSRect restored_bounds_;
194
195   bool is_resizable_;
196   bool shows_resize_controls_;
197   bool shows_fullscreen_controls_;
198
199   apps::SizeConstraints size_constraints_;
200
201   base::scoped_nsobject<NativeAppWindowController> window_controller_;
202   NSInteger attention_request_id_;  // identifier from requestUserAttention
203
204   // For system drag, the whole window is draggable and the non-draggable areas
205   // have to been explicitly excluded.
206   std::vector<extensions::DraggableRegion> draggable_regions_;
207
208   // The Extension Command Registry used to determine which keyboard events to
209   // handle.
210   scoped_ptr<ExtensionKeybindingRegistryCocoa> extension_keybinding_registry_;
211
212   DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa);
213 };
214
215 #endif  // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_