Upstream version 7.35.139.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/app_window.h"
12 #include "apps/size_constraints.h"
13 #include "apps/ui/native_app_window.h"
14 #include "base/mac/scoped_nsobject.h"
15 #include "base/memory/scoped_ptr.h"
16 #import "chrome/browser/ui/cocoa/browser_command_executor.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "extensions/common/draggable_region.h"
19 #include "ui/gfx/rect.h"
20
21 class ExtensionKeybindingRegistryCocoa;
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::AppWindow* app_window,
48                        const apps::AppWindow::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   // Returns true if |point| in local Cocoa coordinate system falls within
105   // the draggable region.
106   bool IsWithinDraggableRegion(NSPoint point) const;
107
108   NSRect restored_bounds() const { return restored_bounds_; }
109
110  protected:
111   // NativeAppWindow implementation.
112   virtual void SetFullscreen(int fullscreen_types) OVERRIDE;
113   virtual bool IsFullscreenOrPending() const OVERRIDE;
114   virtual bool IsDetached() const OVERRIDE;
115   virtual void UpdateWindowIcon() OVERRIDE;
116   virtual void UpdateWindowTitle() OVERRIDE;
117   virtual void UpdateBadgeIcon() OVERRIDE;
118   virtual void UpdateShape(scoped_ptr<SkRegion> region) OVERRIDE;
119   virtual void UpdateDraggableRegions(
120       const std::vector<extensions::DraggableRegion>& regions) OVERRIDE;
121   virtual SkRegion* GetDraggableRegion() OVERRIDE;
122   virtual void HandleKeyboardEvent(
123       const content::NativeWebKeyboardEvent& event) OVERRIDE;
124   virtual bool IsFrameless() const OVERRIDE;
125   virtual bool HasFrameColor() const OVERRIDE;
126   virtual SkColor FrameColor() const OVERRIDE;
127   virtual gfx::Insets GetFrameInsets() const OVERRIDE;
128
129   // These are used to simulate Mac-style hide/show. Since windows can be hidden
130   // and shown using the app.window API, this sets is_hidden_with_app_ to
131   // differentiate the reason a window was hidden.
132   virtual void ShowWithApp() OVERRIDE;
133   virtual void HideWithApp() OVERRIDE;
134   virtual void UpdateShelfMenu() OVERRIDE;
135   virtual gfx::Size GetContentMinimumSize() const OVERRIDE;
136   virtual gfx::Size GetContentMaximumSize() const OVERRIDE;
137   virtual void SetContentSizeConstraints(const gfx::Size& min_size,
138                                          const gfx::Size& max_size) OVERRIDE;
139
140   // WebContentsObserver implementation.
141   virtual void RenderViewCreated(content::RenderViewHost* rvh) 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 app_window_->web_contents();
161   }
162   const extensions::Extension* extension() const {
163     return app_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 UpdateDraggableRegionViews();
175
176   // Cache |restored_bounds_| only if the window is currently restored.
177   void UpdateRestoredBounds();
178
179   // Hides the window unconditionally. Used by Hide and HideWithApp.
180   void HideWithoutMarkingHidden();
181
182   apps::AppWindow* app_window_;  // weak - AppWindow owns NativeAppWindow.
183
184   bool has_frame_;
185
186   // Whether this window is hidden according to the app.window API. This is set
187   // by Hide, Show, and ShowInactive.
188   bool is_hidden_;
189   // Whether this window last became hidden due to a request to hide the entire
190   // app, e.g. via the dock menu or Cmd+H. This is set by Hide/ShowWithApp.
191   bool is_hidden_with_app_;
192
193   bool is_maximized_;
194   bool is_fullscreen_;
195   NSRect restored_bounds_;
196
197   bool is_resizable_;
198   bool shows_resize_controls_;
199   bool shows_fullscreen_controls_;
200
201   apps::SizeConstraints size_constraints_;
202
203   base::scoped_nsobject<NativeAppWindowController> window_controller_;
204   NSInteger attention_request_id_;  // identifier from requestUserAttention
205
206   // For system drag, the whole window is draggable and the non-draggable areas
207   // have to been explicitly excluded.
208   std::vector<extensions::DraggableRegion> draggable_regions_;
209
210   // The Extension Command Registry used to determine which keyboard events to
211   // handle.
212   scoped_ptr<ExtensionKeybindingRegistryCocoa> extension_keybinding_registry_;
213
214   DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa);
215 };
216
217 #endif  // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_