widget/cocoa/nsCocoaWindow.h

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:677805e63ccb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #ifndef nsCocoaWindow_h_
7 #define nsCocoaWindow_h_
8
9 #undef DARWIN
10
11 #import <Cocoa/Cocoa.h>
12
13 #include "nsBaseWidget.h"
14 #include "nsPIWidgetCocoa.h"
15 #include "nsAutoPtr.h"
16 #include "nsCocoaUtils.h"
17
18 class nsCocoaWindow;
19 class nsChildView;
20 class nsMenuBarX;
21 @class ChildView;
22
23 // Value copied from BITMAP_MAX_AREA, used in nsNativeThemeCocoa.mm
24 #define CUIDRAW_MAX_AREA 500000
25
26 // If we are using an SDK older than 10.7, define bits we need that are missing
27 // from it.
28 #if !defined(MAC_OS_X_VERSION_10_7) || \
29 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
30
31 enum {
32 NSWindowAnimationBehaviorDefault = 0,
33 NSWindowAnimationBehaviorNone = 2,
34 NSWindowAnimationBehaviorDocumentWindow = 3,
35 NSWindowAnimationBehaviorUtilityWindow = 4,
36 NSWindowAnimationBehaviorAlertPanel = 5,
37 NSWindowCollectionBehaviorFullScreenPrimary = 128, // 1 << 7
38 };
39
40 typedef NSInteger NSWindowAnimationBehavior;
41
42 @interface NSWindow (LionWindowFeatures)
43 - (void)setAnimationBehavior:(NSWindowAnimationBehavior)newAnimationBehavior;
44 - (void)toggleFullScreen:(id)sender;
45 @end
46
47 #endif
48
49 typedef struct _nsCocoaWindowList {
50 _nsCocoaWindowList() : prev(nullptr), window(nullptr) {}
51 struct _nsCocoaWindowList *prev;
52 nsCocoaWindow *window; // Weak
53 } nsCocoaWindowList;
54
55 // NSWindow subclass that is the base class for all of our own window classes.
56 // Among other things, this class handles the storage of those settings that
57 // need to be persisted across window destruction and reconstruction, i.e. when
58 // switching to and from fullscreen mode.
59 // We don't save shadow, transparency mode or background color because it's not
60 // worth the hassle - Gecko will reset them anyway as soon as the window is
61 // resized.
62 @interface BaseWindow : NSWindow
63 {
64 // Data Storage
65 NSMutableDictionary* mState;
66 BOOL mDrawsIntoWindowFrame;
67 NSColor* mActiveTitlebarColor;
68 NSColor* mInactiveTitlebarColor;
69
70 // Shadow
71 BOOL mScheduledShadowInvalidation;
72
73 // Invalidation disabling
74 BOOL mDisabledNeedsDisplay;
75
76 // DPI cache. Getting the physical screen size (CGDisplayScreenSize)
77 // is ridiculously slow, so we cache it in the toplevel window for all
78 // descendants to use.
79 float mDPI;
80
81 NSTrackingArea* mTrackingArea;
82
83 BOOL mBeingShown;
84 BOOL mDrawTitle;
85 }
86
87 - (void)importState:(NSDictionary*)aState;
88 - (NSMutableDictionary*)exportState;
89 - (void)setDrawsContentsIntoWindowFrame:(BOOL)aState;
90 - (BOOL)drawsContentsIntoWindowFrame;
91 - (void)setTitlebarColor:(NSColor*)aColor forActiveWindow:(BOOL)aActive;
92 - (NSColor*)titlebarColorForActiveWindow:(BOOL)aActive;
93
94 - (void)deferredInvalidateShadow;
95 - (void)invalidateShadow;
96 - (float)getDPI;
97
98 - (void)mouseEntered:(NSEvent*)aEvent;
99 - (void)mouseExited:(NSEvent*)aEvent;
100 - (void)mouseMoved:(NSEvent*)aEvent;
101 - (void)updateTrackingArea;
102 - (NSView*)trackingAreaView;
103
104 - (void)setBeingShown:(BOOL)aValue;
105 - (BOOL)isBeingShown;
106 - (BOOL)isVisibleOrBeingShown;
107
108 - (ChildView*)mainChildView;
109
110 - (NSArray*)titlebarControls;
111
112 - (void)setWantsTitleDrawn:(BOOL)aDrawTitle;
113 - (BOOL)wantsTitleDrawn;
114
115 - (void)disableSetNeedsDisplay;
116 - (void)enableSetNeedsDisplay;
117
118 @end
119
120 @interface NSWindow (Undocumented)
121
122 // If a window has been explicitly removed from the "window cache" (to
123 // deactivate it), it's sometimes necessary to "reset" it to reactivate it
124 // (and put it back in the "window cache"). One way to do this, which Apple
125 // often uses, is to set the "window number" to '-1' and then back to its
126 // original value.
127 - (void)_setWindowNumber:(NSInteger)aNumber;
128
129 // If we set the window's stylemask to be textured, the corners on the bottom of
130 // the window are rounded by default. We use this private method to make
131 // the corners square again, a la Safari. Starting with 10.7, all windows have
132 // rounded bottom corners, so this call doesn't have any effect there.
133 - (void)setBottomCornerRounded:(BOOL)rounded;
134 - (BOOL)bottomCornerRounded;
135
136 // Present in the same form on OS X since at least OS X 10.5.
137 - (NSRect)contentRectForFrameRect:(NSRect)windowFrame styleMask:(NSUInteger)windowStyle;
138 - (NSRect)frameRectForContentRect:(NSRect)windowContentRect styleMask:(NSUInteger)windowStyle;
139
140 // Present since at least OS X 10.5. The OS calls this method on NSWindow
141 // (and its subclasses) to find out which NSFrameView subclass to instantiate
142 // to create its "frame view".
143 + (Class)frameViewClassForStyleMask:(NSUInteger)styleMask;
144
145 @end
146
147 @interface PopupWindow : BaseWindow
148 {
149 @private
150 BOOL mIsContextMenu;
151 }
152
153 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
154 backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation;
155 - (BOOL)isContextMenu;
156 - (void)setIsContextMenu:(BOOL)flag;
157 - (BOOL)canBecomeMainWindow;
158
159 @end
160
161 @interface BorderlessWindow : BaseWindow
162 {
163 }
164
165 - (BOOL)canBecomeKeyWindow;
166 - (BOOL)canBecomeMainWindow;
167
168 @end
169
170 #if defined( MAC_OS_X_VERSION_10_6 ) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 )
171 @interface WindowDelegate : NSObject <NSWindowDelegate>
172 #else
173 @interface WindowDelegate : NSObject
174 #endif
175 {
176 nsCocoaWindow* mGeckoWindow; // [WEAK] (we are owned by the window)
177 // Used to avoid duplication when we send NS_ACTIVATE and
178 // NS_DEACTIVATE to Gecko for toplevel widgets. Starts out
179 // false.
180 bool mToplevelActiveState;
181 BOOL mHasEverBeenZoomed;
182 }
183 + (void)paintMenubarForWindow:(NSWindow*)aWindow;
184 - (id)initWithGeckoWindow:(nsCocoaWindow*)geckoWind;
185 - (void)windowDidResize:(NSNotification*)aNotification;
186 - (nsCocoaWindow*)geckoWidget;
187 - (bool)toplevelActiveState;
188 - (void)sendToplevelActivateEvents;
189 - (void)sendToplevelDeactivateEvents;
190 @end
191
192 @class ToolbarWindow;
193
194 // NSColor subclass that allows us to draw separate colors both in the titlebar
195 // and for background of the window.
196 @interface TitlebarAndBackgroundColor : NSColor
197 {
198 ToolbarWindow *mWindow; // [WEAK] (we are owned by the window)
199 }
200
201 - (id)initWithWindow:(ToolbarWindow*)aWindow;
202
203 @end
204
205 // NSWindow subclass for handling windows with toolbars.
206 @interface ToolbarWindow : BaseWindow
207 {
208 TitlebarAndBackgroundColor *mColor;
209 CGFloat mUnifiedToolbarHeight;
210 NSColor *mBackgroundColor;
211 NSView *mTitlebarView; // strong
212 NSRect mWindowButtonsRect;
213 NSRect mFullScreenButtonRect;
214 }
215 // Pass nil here to get the default appearance.
216 - (void)setTitlebarColor:(NSColor*)aColor forActiveWindow:(BOOL)aActive;
217 - (void)setUnifiedToolbarHeight:(CGFloat)aHeight;
218 - (CGFloat)unifiedToolbarHeight;
219 - (CGFloat)titlebarHeight;
220 - (NSRect)titlebarRect;
221 - (void)setTitlebarNeedsDisplayInRect:(NSRect)aRect sync:(BOOL)aSync;
222 - (void)setTitlebarNeedsDisplayInRect:(NSRect)aRect;
223 - (void)setDrawsContentsIntoWindowFrame:(BOOL)aState;
224 - (void)placeWindowButtons:(NSRect)aRect;
225 - (void)placeFullScreenButton:(NSRect)aRect;
226 - (NSPoint)windowButtonsPositionWithDefaultPosition:(NSPoint)aDefaultPosition;
227 - (NSPoint)fullScreenButtonPositionWithDefaultPosition:(NSPoint)aDefaultPosition;
228 @end
229
230 class nsCocoaWindow : public nsBaseWidget, public nsPIWidgetCocoa
231 {
232 private:
233
234 typedef nsBaseWidget Inherited;
235
236 public:
237
238 nsCocoaWindow();
239 virtual ~nsCocoaWindow();
240
241 NS_DECL_ISUPPORTS_INHERITED
242 NS_DECL_NSPIWIDGETCOCOA
243
244 NS_IMETHOD Create(nsIWidget* aParent,
245 nsNativeWidget aNativeParent,
246 const nsIntRect &aRect,
247 nsDeviceContext *aContext,
248 nsWidgetInitData *aInitData = nullptr);
249
250 NS_IMETHOD Destroy();
251
252 NS_IMETHOD Show(bool aState);
253 virtual nsIWidget* GetSheetWindowParent(void);
254 NS_IMETHOD Enable(bool aState);
255 virtual bool IsEnabled() const;
256 NS_IMETHOD SetModal(bool aState);
257 virtual bool IsVisible() const;
258 NS_IMETHOD SetFocus(bool aState=false);
259 virtual nsIntPoint WidgetToScreenOffset();
260 virtual nsIntPoint GetClientOffset();
261 virtual nsIntSize ClientToWindowSize(const nsIntSize& aClientSize);
262
263 virtual void* GetNativeData(uint32_t aDataType) ;
264
265 NS_IMETHOD ConstrainPosition(bool aAllowSlop,
266 int32_t *aX, int32_t *aY);
267 virtual void SetSizeConstraints(const SizeConstraints& aConstraints);
268 NS_IMETHOD Move(double aX, double aY);
269 NS_IMETHOD PlaceBehind(nsTopLevelWidgetZPlacement aPlacement,
270 nsIWidget *aWidget, bool aActivate);
271 NS_IMETHOD SetSizeMode(int32_t aMode);
272 NS_IMETHOD HideWindowChrome(bool aShouldHide);
273 void EnteredFullScreen(bool aFullScreen);
274 NS_IMETHOD MakeFullScreen(bool aFullScreen);
275 NS_IMETHOD Resize(double aWidth, double aHeight, bool aRepaint);
276 NS_IMETHOD Resize(double aX, double aY, double aWidth, double aHeight, bool aRepaint);
277 NS_IMETHOD GetClientBounds(nsIntRect &aRect);
278 NS_IMETHOD GetScreenBounds(nsIntRect &aRect);
279 void ReportMoveEvent();
280 void ReportSizeEvent();
281 NS_IMETHOD SetCursor(nsCursor aCursor);
282 NS_IMETHOD SetCursor(imgIContainer* aCursor, uint32_t aHotspotX, uint32_t aHotspotY);
283
284 CGFloat BackingScaleFactor();
285 void BackingScaleFactorChanged();
286 virtual double GetDefaultScaleInternal();
287 virtual int32_t RoundsWidgetCoordinatesTo() MOZ_OVERRIDE;
288
289 NS_IMETHOD SetTitle(const nsAString& aTitle);
290
291 NS_IMETHOD Invalidate(const nsIntRect &aRect);
292 virtual nsresult ConfigureChildren(const nsTArray<Configuration>& aConfigurations);
293 virtual LayerManager* GetLayerManager(PLayerTransactionChild* aShadowManager = nullptr,
294 LayersBackend aBackendHint = mozilla::layers::LayersBackend::LAYERS_NONE,
295 LayerManagerPersistence aPersistence = LAYER_MANAGER_CURRENT,
296 bool* aAllowRetaining = nullptr);
297 NS_IMETHOD DispatchEvent(mozilla::WidgetGUIEvent* aEvent,
298 nsEventStatus& aStatus);
299 NS_IMETHOD CaptureRollupEvents(nsIRollupListener * aListener, bool aDoCapture);
300 NS_IMETHOD GetAttention(int32_t aCycleCount);
301 virtual bool HasPendingInputEvent();
302 virtual nsTransparencyMode GetTransparencyMode();
303 virtual void SetTransparencyMode(nsTransparencyMode aMode);
304 NS_IMETHOD SetWindowShadowStyle(int32_t aStyle);
305 virtual void SetShowsToolbarButton(bool aShow);
306 virtual void SetShowsFullScreenButton(bool aShow);
307 virtual void SetWindowAnimationType(WindowAnimationType aType);
308 virtual void SetDrawsTitle(bool aDrawTitle);
309 NS_IMETHOD SetNonClientMargins(nsIntMargin &margins);
310 NS_IMETHOD SetWindowTitlebarColor(nscolor aColor, bool aActive);
311 virtual void SetDrawsInTitlebar(bool aState);
312 virtual nsresult SynthesizeNativeMouseEvent(nsIntPoint aPoint,
313 uint32_t aNativeMessage,
314 uint32_t aModifierFlags);
315
316 void DispatchSizeModeEvent();
317
318 virtual gfxASurface* GetThebesSurface();
319
320 // be notified that a some form of drag event needs to go into Gecko
321 virtual bool DragEvent(unsigned int aMessage, Point aMouseGlobal, UInt16 aKeyModifiers);
322
323 bool HasModalDescendents() { return mNumModalDescendents > 0; }
324 NSWindow *GetCocoaWindow() { return mWindow; }
325
326 void SetMenuBar(nsMenuBarX* aMenuBar);
327 nsMenuBarX *GetMenuBar();
328
329 NS_IMETHOD NotifyIME(const IMENotification& aIMENotification) MOZ_OVERRIDE;
330 NS_IMETHOD_(void) SetInputContext(
331 const InputContext& aContext,
332 const InputContextAction& aAction) MOZ_OVERRIDE;
333 NS_IMETHOD_(InputContext) GetInputContext()
334 {
335 NSView* view = mWindow ? [mWindow contentView] : nil;
336 if (view) {
337 mInputContext.mNativeIMEContext = [view inputContext];
338 }
339 // If inputContext isn't available on this window, returns this window's
340 // pointer since nullptr means the platform has only one context per
341 // process.
342 if (!mInputContext.mNativeIMEContext) {
343 mInputContext.mNativeIMEContext = this;
344 }
345 return mInputContext;
346 }
347 NS_IMETHOD_(bool) ExecuteNativeKeyBinding(
348 NativeKeyBindingsType aType,
349 const mozilla::WidgetKeyboardEvent& aEvent,
350 DoCommandCallback aCallback,
351 void* aCallbackData) MOZ_OVERRIDE;
352
353 void SetPopupWindowLevel();
354
355 NS_IMETHOD ReparentNativeWidget(nsIWidget* aNewParent);
356 protected:
357
358 nsresult CreateNativeWindow(const NSRect &aRect,
359 nsBorderStyle aBorderStyle,
360 bool aRectIsFrameRect);
361 nsresult CreatePopupContentView(const nsIntRect &aRect,
362 nsDeviceContext *aContext);
363 void DestroyNativeWindow();
364 void AdjustWindowShadow();
365 void SetWindowBackgroundBlur();
366 void UpdateBounds();
367
368 nsresult DoResize(double aX, double aY, double aWidth, double aHeight,
369 bool aRepaint, bool aConstrainToCurrentScreen);
370
371 virtual already_AddRefed<nsIWidget>
372 AllocateChildPopupWidget()
373 {
374 static NS_DEFINE_IID(kCPopUpCID, NS_POPUP_CID);
375 nsCOMPtr<nsIWidget> widget = do_CreateInstance(kCPopUpCID);
376 return widget.forget();
377 }
378
379 nsIWidget* mParent; // if we're a popup, this is our parent [WEAK]
380 BaseWindow* mWindow; // our cocoa window [STRONG]
381 WindowDelegate* mDelegate; // our delegate for processing window msgs [STRONG]
382 nsRefPtr<nsMenuBarX> mMenuBar;
383 NSWindow* mSheetWindowParent; // if this is a sheet, this is the NSWindow it's attached to
384 nsChildView* mPopupContentView; // if this is a popup, this is its content widget
385 int32_t mShadowStyle;
386
387 CGFloat mBackingScaleFactor;
388
389 WindowAnimationType mAnimationType;
390
391 bool mWindowMadeHere; // true if we created the window, false for embedding
392 bool mSheetNeedsShow; // if this is a sheet, are we waiting to be shown?
393 // this is used for sibling sheet contention only
394 bool mFullScreen;
395 bool mInFullScreenTransition; // true from the request to enter/exit fullscreen
396 // (MakeFullScreen() call) to EnteredFullScreen()
397 bool mModal;
398
399 bool mUsesNativeFullScreen; // only true on Lion if SetShowsFullScreenButton(true);
400
401 bool mIsAnimationSuppressed;
402
403 bool mInReportMoveEvent; // true if in a call to ReportMoveEvent().
404 bool mInResize; // true if in a call to DoResize().
405
406 int32_t mNumModalDescendents;
407 InputContext mInputContext;
408 };
409
410 #endif // nsCocoaWindow_h_

mercurial