1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Mozilla OS/2 libraries.
16 * The Initial Developer of the Original Code is
17 * John Fairhurst, <john_fairhurst@iname.com>.
18 * Portions created by the Initial Developer are Copyright (C) 1999
19 * the Initial Developer. All Rights Reserved.
22 * Rich Walsh <dragtext@e-vertise.com>
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 *****
37 * This Original Code has been modified by IBM Corporation.
38 * Modifications made by IBM described herein are
39 * Copyright (c) International Business Machines
42 * Modifications to Mozilla code or documentation
43 * identified per MPL Section 3.3
45 * Date Modified by Description of modification
46 * 03/23/2000 IBM Corp. Added InvalidateRegion method.
47 * 04/12/2000 IBM Corp. Changed params on DispatchMouseEvent to match Windows..
48 * 04/14/2000 IBM Corp. Declared EventIsInsideWindow for CaptureRollupEvents
49 * 06/15/2000 IBM Corp. Added NS2PM for rectangles
50 * 06/21/2000 IBM Corp. Added CaptureMouse
57 #include "nsWidgetDefs.h"
58 #include "nsBaseWidget.h"
59 #include "nsToolkit.h"
60 #include "nsSwitchToUIThread.h"
61 #include "gfxOS2Surface.h"
62 #include "gfxContext.h"
69 #define DEBUGFOCUS(what) printf("[%x] "#what" (%d)\n", (int)this, mWindowIdentifier)
71 #define DEBUGFOCUS(what)
75 // This is abstract. Controls (labels, radio buttons, listboxen) derive
76 // from here. A thing called a child window derives from here, and the
77 // frame window class derives from the child. The content-displaying
78 // classes are off on their own branch to avoid creating a palette for
79 // every window we create. This may turn out to be what's required, in
80 // which case the paint & palette code from nsChildWindow needs to be
81 // munged in here. nsFrameWindow is separate because work needs to be done
82 // there to decide whether methods apply to frame or client.
84 /* Possible states of the window, used to emulate windows better... */
85 // default state; Create() not called
86 #define nsWindowState_ePrecreate 0x00000001
87 // processing Create() method
88 #define nsWindowState_eInCreate 0x00000002
89 // active, existing window
90 #define nsWindowState_eLive 0x00000004
91 //processing Close() method
92 #define nsWindowState_eClosing 0x00000008
93 // object destructor running
94 #define nsWindowState_eDoingDelete 0x00000010
96 #define nsWindowState_eDead 0x00000100
98 MRESULT EXPENTRY
fnwpNSWindow( HWND
, ULONG
, MPARAM
, MPARAM
);
99 MRESULT EXPENTRY
fnwpFrame( HWND
, ULONG
, MPARAM
, MPARAM
);
101 class nsWindow
: public nsBaseWidget
,
102 public nsSwitchToUIThread
109 static void ReleaseGlobals();
113 // Creation from native (eh?) or widget parent, destroy
114 NS_IMETHOD
Create( nsIWidget
*aParent
,
116 EVENT_CALLBACK aHandleEventFunction
,
117 nsIDeviceContext
*aContext
,
118 nsIAppShell
*aAppShell
= nsnull
,
119 nsIToolkit
*aToolkit
= nsnull
,
120 nsWidgetInitData
*aInitData
= nsnull
);
121 NS_IMETHOD
Create( nsNativeWidget aParent
,
123 EVENT_CALLBACK aHandleEventFunction
,
124 nsIDeviceContext
*aContext
,
125 nsIAppShell
*aAppShell
= nsnull
,
126 nsIToolkit
*aToolkit
= nsnull
,
127 nsWidgetInitData
*aInitData
= nsnull
);
128 gfxASurface
* GetThebesSurface();
129 NS_IMETHOD
Destroy(); // call before releasing
131 // Hierarchy: only interested in widget children (it seems)
132 virtual nsIWidget
*GetParent();
134 NS_IMETHOD
SetSizeMode(PRInt32 aMode
);
136 // Physical properties
137 NS_IMETHOD
Show( PRBool bState
);
138 NS_IMETHOD
ConstrainPosition(PRBool aAllowSlop
, PRInt32
*aX
, PRInt32
*aY
);
139 NS_IMETHOD
Move( PRInt32 aX
, PRInt32 aY
);
140 NS_IMETHOD
Resize( PRInt32 aWidth
,
143 NS_IMETHOD
Resize( PRInt32 aX
,
148 NS_IMETHOD
GetClientBounds( nsRect
&aRect
);
149 NS_IMETHOD
Enable( PRBool aState
);
150 NS_IMETHOD
IsEnabled(PRBool
*aState
);
151 NS_IMETHOD
SetFocus(PRBool aRaise
);
152 NS_IMETHOD
GetBounds(nsRect
&aRect
);
153 NS_IMETHOD
IsVisible( PRBool
&aState
);
154 NS_IMETHOD
PlaceBehind(nsTopLevelWidgetZPlacement aPlacement
,
155 nsIWidget
*aWidget
, PRBool aActivate
);
157 NS_IMETHOD
CaptureMouse(PRBool aCapture
);
159 NS_IMETHOD
ModalEventFilter( PRBool aRealEvent
, void *aEvent
,
160 PRBool
*aForWindow
);
162 NS_IMETHOD
GetPreferredSize( PRInt32
&aWidth
, PRInt32
&aHeight
);
163 NS_IMETHOD
SetPreferredSize( PRInt32 aWidth
, PRInt32 aHeight
);
165 NS_IMETHOD
BeginResizingChildren();
166 NS_IMETHOD
EndResizingChildren();
167 NS_IMETHOD
WidgetToScreen( const nsRect
&aOldRect
, nsRect
&aNewRect
);
168 NS_IMETHOD
ScreenToWidget( const nsRect
&aOldRect
, nsRect
&aNewRect
);
169 NS_IMETHOD
DispatchEvent( struct nsGUIEvent
*event
, nsEventStatus
&aStatus
);
170 NS_IMETHOD
CaptureRollupEvents(nsIRollupListener
* aListener
, PRBool aDoCapture
, PRBool aConsumeRollupEvent
);
172 NS_IMETHOD
GetLastInputEventTime(PRUint32
& aTime
);
175 virtual nsIFontMetrics
*GetFont();
176 NS_IMETHOD
SetFont( const nsFont
&aFont
);
177 NS_IMETHOD
SetColorMap( nsColorMap
*aColorMap
);
178 NS_IMETHOD
SetCursor( nsCursor aCursor
);
179 NS_IMETHOD
SetCursor(imgIContainer
* aCursor
,
180 PRUint32 aHotspotX
, PRUint32 aHotspotY
);
181 NS_IMETHOD
HideWindowChrome(PRBool aShouldHide
);
182 NS_IMETHOD
SetTitle( const nsAString
& aTitle
);
183 NS_IMETHOD
SetIcon(const nsAString
& aIconSpec
);
184 NS_IMETHOD
SetMenuBar(nsIMenuBar
* aMenuBar
) { return NS_ERROR_FAILURE
; }
185 NS_IMETHOD
ShowMenuBar(PRBool aShow
) { return NS_ERROR_FAILURE
; }
186 NS_IMETHOD
Invalidate( PRBool aIsSynchronous
);
187 NS_IMETHOD
Invalidate( const nsRect
& aRect
, PRBool aIsSynchronous
);
188 NS_IMETHOD
InvalidateRegion(const nsIRegion
*aRegion
, PRBool aIsSynchronous
);
190 NS_IMETHOD
Scroll( PRInt32 aDx
, PRInt32 aDy
, nsRect
*aClipRect
);
191 NS_IMETHOD
ScrollWidgets(PRInt32 aDx
, PRInt32 aDy
);
192 NS_IMETHOD
ScrollRect(nsRect
&aRect
, PRInt32 aDx
, PRInt32 aDy
);
194 // Get a HWND or a HPS.
195 virtual void *GetNativeData( PRUint32 aDataType
);
196 virtual void FreeNativeData( void *aDatum
, PRUint32 aDataType
);
197 virtual HWND
GetMainWindow() const { return mWnd
; }
199 // nsSwitchToPMThread interface
200 virtual BOOL
CallMethod(MethodInfo
*info
);
202 // PM methods which need to be public (menus, etc)
203 ULONG
GetNextID() { return mNextID
++; }
204 void NS2PM_PARENT( POINTL
&ptl
);
205 void NS2PM( POINTL
&ptl
);
206 void NS2PM( RECTL
&rcl
);
209 static BOOL
DealWithPopups ( ULONG inMsg
, MRESULT
* outResult
) ;
211 static PRBool
EventIsInsideWindow(nsWindow
* aWindow
);
213 static nsWindow
* GetNSWindowPtr(HWND aWnd
);
214 static BOOL
SetNSWindowPtr(HWND aWnd
, nsWindow
* ptr
);
216 static nsWindow
* gCurrentWindow
;
217 // nsWindow methods subclasses must provide for creation to work
218 virtual PCSZ
WindowClass();
219 virtual ULONG
WindowStyle();
221 // hooks subclasses may wish to override!
222 virtual void PostCreateWidget() {}
223 virtual PRInt32
GetClientHeight() { return mBounds
.height
; }
224 virtual ULONG
GetSWPFlags( ULONG flags
) { return flags
; }
225 virtual void SetupForPrint( HWND
/*hwnd*/) {}
227 // Useful functions for subclasses to use, threaded as necessary.
228 virtual nsresult
GetWindowText( nsString
&str
, PRUint32
*rc
);
229 virtual void AddToStyle( ULONG style
);
230 virtual void RemoveFromStyle( ULONG style
);
231 // return true if deferred
232 virtual BOOL
SetWindowPos( HWND hwndInsertBehind
, long x
, long y
,
233 long cx
, long cy
, unsigned long flags
);
235 // Message handlers - may wish to override. Default implementation for
236 // palette, control, paint & scroll is to do nothing.
238 // Return whether message has been processed.
239 virtual PRBool
ProcessMessage( ULONG m
, MPARAM p1
, MPARAM p2
, MRESULT
&r
);
240 virtual PRBool
OnPaint();
241 virtual void OnDestroy();
242 virtual PRBool
OnReposition( PSWP pNewSwp
);
243 virtual PRBool
OnResize( PRInt32 aX
, PRInt32 aY
);
244 virtual PRBool
OnMove( PRInt32 aX
, PRInt32 aY
);
245 virtual PRBool
OnKey( MPARAM mp1
, MPARAM mp2
);
246 virtual PRBool
OnRealizePalette();
247 virtual PRBool
DispatchFocus( PRUint32 aEventType
, PRBool isMozWindowTakingFocus
);
248 virtual PRBool
OnScroll( ULONG msgid
, MPARAM mp1
, MPARAM mp2
);
249 virtual PRBool
OnVScroll( MPARAM mp1
, MPARAM mp2
);
250 virtual PRBool
OnHScroll( MPARAM mp1
, MPARAM mp2
);
251 virtual PRBool
OnControl( MPARAM mp1
, MPARAM mp2
);
252 // called after param has been set...
253 virtual PRBool
OnPresParamChanged( MPARAM mp1
, MPARAM mp2
);
254 virtual PRBool
OnDragDropMsg(ULONG msg
, MPARAM mp1
, MPARAM mp2
, MRESULT
&mr
);
256 static BOOL sIsRegistered
;
259 HWND mWnd
; // window handle
260 HWND mFrameWnd
; // frame window handle
261 PFNWP mPrevWndProc
; // previous window procedure
262 nsWindow
*mParent
; // parent widget
263 ULONG mNextID
; // next child window id
264 PSWP mSWPs
; // SWPs for deferred window positioning
265 ULONG mlHave
, mlUsed
; // description of mSWPs array
266 HPOINTER mFrameIcon
; // current frame icon
267 VDKEY mDeadKey
; // dead key from previous keyevent
268 BOOL mHaveDeadKey
; // is mDeadKey valid [0 may be a valid dead key, for all I know]
270 PRBool mIsTopWidgetWindow
;
274 nsContentType mContentType
;
275 HPS mDragHps
; // retrieved by DrgGetPS() during a drag
276 PRUint32 mDragStatus
; // set while this object is being dragged over
277 HPOINTER mCssCursorHPtr
; // created by SetCursor(imgIContainer*)
278 nsCOMPtr
<imgIContainer
> mCssCursorImg
; // saved by SetCursor(imgIContainer*)
280 HWND
GetParentHWND() const;
281 HWND
GetHWND() const { return mWnd
; }
282 PFNWP
GetPrevWP() const { return mPrevWndProc
; }
284 // nglayout data members
285 PRInt32 mPreferredHeight
;
286 PRInt32 mPreferredWidth
;
287 nsToolkit
*mOS2Toolkit
;
289 nsIMenuBar
*mMenuBar
;
290 PRInt32 mWindowState
;
291 nsRefPtr
<gfxOS2Surface
> mThebesSurface
;
293 // Implementation ------------------------------
294 void DoCreate( HWND hwndP
, nsWindow
*wndP
, const nsRect
&rect
,
295 EVENT_CALLBACK aHandleEventFunction
,
296 nsIDeviceContext
*aContext
, nsIAppShell
*aAppShell
,
297 nsIToolkit
*aToolkit
, nsWidgetInitData
*aInitData
);
299 virtual void RealDoCreate( HWND hwndP
, nsWindow
*aParent
,
301 EVENT_CALLBACK aHandleEventFunction
,
302 nsIDeviceContext
*aContext
,
303 nsIAppShell
*aAppShell
,
304 nsWidgetInitData
*aInitData
,
307 // hook so dialog can be created looking like a dialog
308 virtual ULONG
GetFCFlags();
310 virtual void SubclassWindow(BOOL bState
);
312 PRBool
ConvertStatus( nsEventStatus aStatus
)
313 { return aStatus
== nsEventStatus_eConsumeNoDefault
; }
314 void InitEvent( nsGUIEvent
&event
, nsPoint
*pt
= 0);
315 virtual PRBool
DispatchWindowEvent(nsGUIEvent
* event
);
316 virtual PRBool
DispatchWindowEvent(nsGUIEvent
*event
, nsEventStatus
&aStatus
);
317 PRBool
DispatchStandardEvent( PRUint32 aMsg
);
318 PRBool
DispatchCommandEvent(PRUint32 aEventCommand
);
319 PRBool
DispatchDragDropEvent( PRUint32 aMsg
);
320 virtual PRBool
DispatchMouseEvent(PRUint32 aEventType
, MPARAM mp1
, MPARAM mp2
,
321 PRBool aIsContextMenuKey
= PR_FALSE
,
322 PRInt16 aButton
= nsMouseEvent::eLeftButton
);
323 virtual PRBool
DispatchResizeEvent( PRInt32 aClientX
, PRInt32 aClientY
);
324 void GetNonClientBounds(nsRect
&aRect
);
325 void DeferPosition( HWND
, HWND
, long, long, long, long, ULONG
);
326 void ConstrainZLevel(HWND
*aAfter
);
328 PRBool
CheckDragStatus(PRUint32 aAction
, HPS
* oHps
);
329 PRBool
ReleaseIfDragHPS(HPS aHps
);
331 HBITMAP
DataToBitmap(PRUint8
* aImageData
, PRUint32 aWidth
,
332 PRUint32 aHeight
, PRUint32 aDepth
);
333 HBITMAP
CreateBitmapRGB(PRUint8
* aImageData
, PRUint32 aWidth
, PRUint32 aHeight
);
334 // 'format' should be 'gfx_format' which is a PRInt32
335 HBITMAP
CreateTransparencyMask(PRInt32 format
, PRUint8
* aImageData
,
336 PRUint32 aWidth
, PRUint32 aHeight
);
338 // Enumeration of the methods which are accessible on the PM thread
347 friend MRESULT EXPENTRY
fnwpNSWindow( HWND
, ULONG
, MPARAM
, MPARAM
);
348 friend MRESULT EXPENTRY
fnwpFrame( HWND
, ULONG
, MPARAM
, MPARAM
);
350 int mWindowIdentifier
;
354 #define PM2NS_PARENT NS2PM_PARENT
357 #define PMSCAN_PADMULT 0x37
358 #define PMSCAN_PAD7 0x47
359 #define PMSCAN_PAD8 0x48
360 #define PMSCAN_PAD9 0x49
361 #define PMSCAN_PADMINUS 0x4A
362 #define PMSCAN_PAD4 0x4B
363 #define PMSCAN_PAD5 0x4C
364 #define PMSCAN_PAD6 0x4D
365 #define PMSCAN_PADPLUS 0x4E
366 #define PMSCAN_PAD1 0x4F
367 #define PMSCAN_PAD2 0x50
368 #define PMSCAN_PAD3 0x51
369 #define PMSCAN_PAD0 0x52
370 #define PMSCAN_PADPERIOD 0x53
371 #define PMSCAN_PADDIV 0x5c
373 #define isNumPadScanCode(scanCode) !( (scanCode < PMSCAN_PAD7) || \
374 (scanCode > PMSCAN_PADPERIOD) || \
375 (scanCode == PMSCAN_PADMULT) || \
376 (scanCode == PMSCAN_PADDIV) || \
377 (scanCode == PMSCAN_PADMINUS) || \
378 (scanCode == PMSCAN_PADPLUS) )
379 #define isNumlockOn (BOOL)WinGetKeyState(HWND_DESKTOP, VK_NUMLOCK) & 0x0001
381 extern PRUint32
WMChar2KeyCode( MPARAM mp1
, MPARAM mp2
);
383 extern nsWindow
*NS_HWNDToWindow( HWND hwnd
);
385 #define NSCANVASCLASS "WarpzillaCanvas"
389 // Need to do this because the cross-platform widgets (toolbars) assume
390 // that the name of the NS_CHILD_CID is ChildWindow and that it gets
391 // defined in "nsWindow.h".
393 // However, if we've included this header *from nsCanvas.h*, then we
394 // get a lovely circular dependency, and so special-case this.
396 // Yes, I suppose I'm just being perverse by having three separate classes
397 // here, but I just baulk at naming a class 'ChildWindow'.
399 // (and don't tell me there's a JLib class called JMother. Believe me,
400 // I know, and I regret it at least twice a week...)
403 #include "nsCanvas.h"
404 typedef nsCanvas ChildWindow
;