Bug 439354 - OS X toolbar background doesn't have a good gradient. Part 1 (widget...
[mozilla-central.git] / widget / src / cocoa / nsChildView.h
blob47e3f307b8af22e1b574bc2cbd7d142eca6968d3
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsChildView_h_
39 #define nsChildView_h_
41 // formal protocols
42 #include "mozView.h"
43 #ifdef ACCESSIBILITY
44 #include "nsIAccessible.h"
45 #include "mozAccessibleProtocol.h"
46 #endif
48 #include "nsAutoPtr.h"
49 #include "nsISupports.h"
50 #include "nsBaseWidget.h"
51 #include "nsIPluginInstanceOwner.h"
52 #include "nsIPluginWidget.h"
53 #include "nsIScrollableView.h"
54 #include "nsWeakPtr.h"
56 #include "nsIWidget.h"
57 #include "nsIAppShell.h"
59 #include "nsIEventListener.h"
60 #include "nsString.h"
61 #include "nsIDragService.h"
63 #include "nsplugindefs.h"
65 #import <Carbon/Carbon.h>
66 #import <Cocoa/Cocoa.h>
68 class gfxASurface;
69 class nsChildView;
70 union nsPluginPort;
72 enum {
73 // Currently focused ChildView (while this TSM document is active).
74 // Transient (only set while TSMProcessRawKeyEvent() is processing a key
75 // event), and the ChildView will be retained and released around the call
76 // to TSMProcessRawKeyEvent() -- so it can be weak.
77 kFocusedChildViewTSMDocPropertyTag = 'GKFV', // type ChildView* [WEAK]
80 // Undocumented HIToolbox function used by WebKit to allow Carbon-based IME
81 // to work in a Cocoa-based browser (like Safari or Cocoa-widgets Firefox).
82 // (Recent WebKit versions actually use a thin wrapper around this function
83 // called WKSendKeyEventToTSM().)
85 // Calling TSMProcessRawKeyEvent() from ChildView's keyDown: and keyUp:
86 // methods (when the ChildView is a plugin view) bypasses Cocoa's IME
87 // infrastructure and (instead) causes Carbon TSM events to be sent on each
88 // NSKeyDown event. We install a Carbon event handler
89 // (PluginKeyEventsHandler()) to catch these events and pass them to Gecko
90 // (which in turn passes them to the plugin).
91 extern "C" long TSMProcessRawKeyEvent(EventRef carbonEvent);
93 @interface NSEvent (Undocumented)
95 // Return Cocoa event's corresponding Carbon event. Not initialized (on
96 // synthetic events) until the OS actually "sends" the event. This method
97 // has been present in the same form since at least OS X 10.2.8.
98 - (EventRef)_eventRef;
100 @end
102 @interface ChildView : NSView<
103 #ifdef ACCESSIBILITY
104 mozAccessible,
105 #endif
106 mozView, NSTextInput>
108 @private
109 NSWindow* mWindow; // shortcut to the top window, [WEAK]
111 // the nsChildView that created the view. It retains this NSView, so
112 // the link back to it must be weak.
113 nsChildView* mGeckoChild;
115 // tag for our mouse enter/exit tracking rect
116 NSTrackingRectTag mMouseEnterExitTag;
118 // Whether we're a plugin view.
119 BOOL mIsPluginView;
121 // The following variables are only valid during key down event processing.
122 // Their current usage needs to be fixed to avoid problems with nested event
123 // loops that can confuse them. Once a variable is set during key down event
124 // processing, if an event spawns a nested event loop the previously set value
125 // will be wiped out.
126 NSEvent* mCurKeyEvent;
127 PRBool mKeyDownHandled;
128 // While we process key down events we need to keep track of whether or not
129 // we sent a key press event. This helps us make sure we do send one
130 // eventually.
131 BOOL mKeyPressSent;
132 // Valid when mKeyPressSent is true.
133 PRBool mKeyPressHandled;
135 // needed for NSTextInput implementation
136 NSRange mMarkedRange;
138 BOOL mInHandScroll; // true for as long as we are hand scrolling
139 // hand scroll locations
140 NSPoint mHandScrollStartMouseLoc;
141 nscoord mHandScrollStartScrollX, mHandScrollStartScrollY;
143 // when mouseDown: is called, we store its event here (strong)
144 NSEvent* mLastMouseDownEvent;
146 // rects that were invalidated during a draw, so have pending drawing
147 NSMutableArray* mPendingDirtyRects;
148 BOOL mPendingFullDisplay;
150 // All views are always opaque (non-transparent). The only exception is when we're
151 // the content view in a transparent XUL window.
152 BOOL mIsTransparent;
154 // Holds our drag service across multiple drag calls. The reference to the
155 // service is obtained when the mouse enters the view and is released when
156 // the mouse exits or there is a drop. This prevents us from having to
157 // re-establish the connection to the service manager many times per second
158 // when handling |draggingUpdated:| messages.
159 nsIDragService* mDragService;
161 PRUint32 mLastModifierState;
163 // For use with plugins, so that we can support IME in them. We can't use
164 // Cocoa TSM documents (those created and managed by the NSTSMInputContext
165 // class) -- for some reason TSMProcessRawKeyEvent() doesn't work with them.
166 TSMDocumentID mPluginTSMDoc;
169 // these are sent to the first responder when the window key status changes
170 - (void)viewsWindowDidBecomeKey;
171 - (void)viewsWindowDidResignKey;
173 // Stop NSView hierarchy being changed during [ChildView drawRect:]
174 - (void)delayedTearDown;
176 - (void)setTransparent:(BOOL)transparent;
178 - (void)sendFocusEvent:(PRUint32)eventType;
180 - (void) processPluginKeyEvent:(EventRef)aKeyEvent;
181 @end
185 //-------------------------------------------------------------------------
187 // nsTSMManager
189 //-------------------------------------------------------------------------
191 class nsTSMManager {
192 public:
193 static PRBool IsComposing() { return sComposingView ? PR_TRUE : PR_FALSE; }
194 static PRBool IsIMEEnabled() { return sIsIMEEnabled; }
195 static PRBool IgnoreCommit() { return sIgnoreCommit; }
197 static void OnDestroyView(NSView<mozView>* aDestroyingView);
199 // Note that we cannot get the actual state in TSM. But we can trust this
200 // value. Because nsIMEStateManager reset this at every focus changing.
201 static PRBool IsRomanKeyboardsOnly() { return sIsRomanKeyboardsOnly; }
203 static PRBool GetIMEOpenState();
205 static void InitTSMDocument(NSView<mozView>* aViewForCaret);
206 static void StartComposing(NSView<mozView>* aComposingView);
207 static void UpdateComposing(NSString* aComposingString);
208 static void EndComposing();
209 static void EnableIME(PRBool aEnable);
210 static void SetIMEOpenState(PRBool aOpen);
211 static void SetRomanKeyboardsOnly(PRBool aRomanOnly);
213 static void CommitIME();
214 static void CancelIME();
215 private:
216 static PRBool sIsIMEEnabled;
217 static PRBool sIsRomanKeyboardsOnly;
218 static PRBool sIgnoreCommit;
219 static NSView<mozView>* sComposingView;
220 static TSMDocumentID sDocumentID;
221 static NSString* sComposingString;
223 static void KillComposing();
226 //-------------------------------------------------------------------------
228 // nsChildView
230 //-------------------------------------------------------------------------
232 class nsChildView : public nsBaseWidget,
233 public nsIPluginWidget
235 private:
236 typedef nsBaseWidget Inherited;
238 public:
239 nsChildView();
240 virtual ~nsChildView();
242 NS_DECL_ISUPPORTS_INHERITED
244 // nsIWidget interface
245 NS_IMETHOD Create(nsIWidget *aParent,
246 const nsRect &aRect,
247 EVENT_CALLBACK aHandleEventFunction,
248 nsIDeviceContext *aContext,
249 nsIAppShell *aAppShell = nsnull,
250 nsIToolkit *aToolkit = nsnull,
251 nsWidgetInitData *aInitData = nsnull);
252 NS_IMETHOD Create(nsNativeWidget aNativeParent,
253 const nsRect &aRect,
254 EVENT_CALLBACK aHandleEventFunction,
255 nsIDeviceContext *aContext,
256 nsIAppShell *aAppShell = nsnull,
257 nsIToolkit *aToolkit = nsnull,
258 nsWidgetInitData *aInitData = nsnull);
260 // Utility method for implementing both Create(nsIWidget ...) and
261 // Create(nsNativeWidget...)
263 virtual nsresult StandardCreate(nsIWidget *aParent,
264 const nsRect &aRect,
265 EVENT_CALLBACK aHandleEventFunction,
266 nsIDeviceContext *aContext,
267 nsIAppShell *aAppShell,
268 nsIToolkit *aToolkit,
269 nsWidgetInitData *aInitData,
270 nsNativeWidget aNativeParent = nsnull);
272 NS_IMETHOD Destroy();
274 NS_IMETHOD Show(PRBool aState);
275 NS_IMETHOD IsVisible(PRBool& outState);
277 virtual nsIWidget* GetParent(void);
279 NS_IMETHOD ModalEventFilter(PRBool aRealEvent, void *aEvent,
280 PRBool *aForWindow);
282 NS_IMETHOD ConstrainPosition(PRBool aAllowSlop,
283 PRInt32 *aX, PRInt32 *aY);
284 NS_IMETHOD Move(PRInt32 aX, PRInt32 aY);
285 NS_IMETHOD Resize(PRInt32 aWidth,PRInt32 aHeight, PRBool aRepaint);
286 NS_IMETHOD Resize(PRInt32 aX, PRInt32 aY,PRInt32 aWidth,PRInt32 aHeight, PRBool aRepaint);
288 NS_IMETHOD Enable(PRBool aState);
289 NS_IMETHOD IsEnabled(PRBool *aState);
290 NS_IMETHOD SetFocus(PRBool aRaise);
291 NS_IMETHOD SetBounds(const nsRect &aRect);
292 NS_IMETHOD GetBounds(nsRect &aRect);
294 NS_IMETHOD Invalidate(PRBool aIsSynchronous);
295 NS_IMETHOD Invalidate(const nsRect &aRect,PRBool aIsSynchronous);
296 NS_IMETHOD InvalidateRegion(const nsIRegion *aRegion, PRBool aIsSynchronous);
297 NS_IMETHOD Validate();
299 virtual void* GetNativeData(PRUint32 aDataType);
300 NS_IMETHOD SetColorMap(nsColorMap *aColorMap);
301 NS_IMETHOD Scroll(PRInt32 aDx, PRInt32 aDy, nsRect *aClipRect);
302 NS_IMETHOD WidgetToScreen(const nsRect& aOldRect, nsRect& aNewRect);
303 NS_IMETHOD ScreenToWidget(const nsRect& aOldRect, nsRect& aNewRect);
304 NS_IMETHOD BeginResizingChildren(void);
305 NS_IMETHOD EndResizingChildren(void);
306 virtual PRBool ShowsResizeIndicator(nsIntRect* aResizerRect);
308 static PRBool ConvertStatus(nsEventStatus aStatus)
309 { return aStatus == nsEventStatus_eConsumeNoDefault; }
310 NS_IMETHOD DispatchEvent(nsGUIEvent* event, nsEventStatus & aStatus);
312 NS_IMETHOD Update();
314 virtual void ConvertToDeviceCoordinates(nscoord &aX, nscoord &aY);
315 void LocalToWindowCoordinate(nsPoint& aPoint) { ConvertToDeviceCoordinates(aPoint.x, aPoint.y); }
316 void LocalToWindowCoordinate(nscoord& aX, nscoord& aY) { ConvertToDeviceCoordinates(aX, aY); }
317 void LocalToWindowCoordinate(nsRect& aRect) { ConvertToDeviceCoordinates(aRect.x, aRect.y); }
319 NS_IMETHOD SetMenuBar(void* aMenuBar);
320 NS_IMETHOD ShowMenuBar(PRBool aShow);
322 NS_IMETHOD GetPreferredSize(PRInt32& aWidth, PRInt32& aHeight);
323 NS_IMETHOD SetPreferredSize(PRInt32 aWidth, PRInt32 aHeight);
325 NS_IMETHOD SetCursor(nsCursor aCursor);
326 NS_IMETHOD SetCursor(imgIContainer* aCursor, PRUint32 aHotspotX, PRUint32 aHotspotY);
328 NS_IMETHOD CaptureRollupEvents(nsIRollupListener * aListener, PRBool aDoCapture, PRBool aConsumeRollupEvent);
329 NS_IMETHOD SetTitle(const nsAString& title);
331 NS_IMETHOD GetAttention(PRInt32 aCycleCount);
333 NS_IMETHOD ActivateNativeMenuItemAt(const nsAString& indexString);
334 NS_IMETHOD ForceNativeMenuReload();
336 NS_IMETHOD ResetInputState();
337 NS_IMETHOD SetIMEOpenState(PRBool aState);
338 NS_IMETHOD GetIMEOpenState(PRBool* aState);
339 NS_IMETHOD SetIMEEnabled(PRUint32 aState);
340 NS_IMETHOD GetIMEEnabled(PRUint32* aState);
341 NS_IMETHOD CancelIMEComposition();
342 NS_IMETHOD GetToggledKeyState(PRUint32 aKeyCode,
343 PRBool* aLEDState);
345 // nsIPluginWidget
346 NS_IMETHOD GetPluginClipRect(nsRect& outClipRect, nsPoint& outOrigin, PRBool& outWidgetVisible);
347 NS_IMETHOD StartDrawPlugin();
348 NS_IMETHOD EndDrawPlugin();
349 NS_IMETHOD SetPluginInstanceOwner(nsIPluginInstanceOwner* aInstanceOwner);
351 virtual nsTransparencyMode GetTransparencyMode();
352 virtual void SetTransparencyMode(nsTransparencyMode aMode);
354 // Mac specific methods
355 virtual PRBool PointInWidget(Point aThePoint);
357 virtual PRBool DispatchWindowEvent(nsGUIEvent& event);
359 void LiveResizeStarted();
360 void LiveResizeEnded();
362 #ifdef ACCESSIBILITY
363 void GetDocumentAccessible(nsIAccessible** aAccessible);
364 #endif
366 virtual gfxASurface* GetThebesSurface();
368 NS_IMETHOD BeginSecureKeyboardInput();
369 NS_IMETHOD EndSecureKeyboardInput();
371 void HidePlugin();
373 protected:
375 PRBool ReportDestroyEvent();
376 PRBool ReportMoveEvent();
377 PRBool ReportSizeEvent();
379 NS_IMETHOD CalcOffset(PRInt32 &aX,PRInt32 &aY);
381 virtual PRBool OnPaint(nsPaintEvent & aEvent);
383 // override to create different kinds of child views. Autoreleases, so
384 // caller must retain.
385 virtual NSView* CreateCocoaView(NSRect inFrame);
386 void TearDownView();
388 virtual nsresult SynthesizeNativeKeyEvent(PRInt32 aNativeKeyboardLayout,
389 PRInt32 aNativeKeyCode,
390 PRUint32 aModifierFlags,
391 const nsAString& aCharacters,
392 const nsAString& aUnmodifiedCharacters);
394 protected:
396 NSView<mozView>* mView; // my parallel cocoa view (ChildView or NativeScrollbarView), [STRONG]
398 NSView<mozView>* mParentView;
399 nsIWidget* mParentWidget;
401 #ifdef ACCESSIBILITY
402 // weak ref to this childview's associated mozAccessible for speed reasons
403 // (we get queried for it *a lot* but don't want to own it)
404 nsWeakPtr mAccessible;
405 #endif
407 nsRefPtr<gfxASurface> mTempThebesSurface;
409 PRPackedBool mVisible;
410 PRPackedBool mDrawing;
411 PRPackedBool mLiveResizeInProgress;
412 PRPackedBool mIsPluginView; // true if this is a plugin view
413 PRPackedBool mPluginDrawing;
414 PRPackedBool mPluginIsCG; // true if this is a CoreGraphics plugin
416 PRPackedBool mInSetFocus;
418 nsPluginPort mPluginPort;
419 nsIPluginInstanceOwner* mPluginInstanceOwner; // [WEAK]
422 void NS_InstallPluginKeyEventsHandler();
423 void NS_RemovePluginKeyEventsHandler();
425 #endif // nsChildView_h_