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
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.
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 ***** */
41 #include "nsBaseWidget.h"
42 #include "nsIRegion.h"
44 #include "nsIDragService.h"
46 #include "nsClipboard.h"
56 #define NS_TO_PH_RGB(ns) (ns & 0xff) << 16 | (ns & 0xff00) | ((ns >> 16) & 0xff)
57 #define PH_TO_NS_RGB(ns) (ns & 0xff) << 16 | (ns & 0xff00) | ((ns >> 16) & 0xff)
60 * Base of all Photon native widgets.
63 class nsWidget
: public nsBaseWidget
69 NS_DECL_ISUPPORTS_INHERITED
73 // create with nsIWidget parent
74 inline NS_IMETHOD
Create(nsIWidget
*aParent
,
76 EVENT_CALLBACK aHandleEventFunction
,
77 nsIDeviceContext
*aContext
,
78 nsIAppShell
*aAppShell
= nsnull
,
79 nsIToolkit
*aToolkit
= nsnull
,
80 nsWidgetInitData
*aInitData
= nsnull
)
82 return(CreateWidget(aParent
, aRect
, aHandleEventFunction
, aContext
, aAppShell
, aToolkit
, aInitData
, nsnull
));
85 // create with a native parent
86 inline NS_IMETHOD
Create(nsNativeWidget aParent
,
88 EVENT_CALLBACK aHandleEventFunction
,
89 nsIDeviceContext
*aContext
,
90 nsIAppShell
*aAppShell
= nsnull
,
91 nsIToolkit
*aToolkit
= nsnull
,
92 nsWidgetInitData
*aInitData
= nsnull
)
94 return(CreateWidget(nsnull
, aRect
, aHandleEventFunction
, aContext
, aAppShell
, aToolkit
, aInitData
,aParent
));
97 NS_IMETHOD
Destroy(void);
98 inline nsIWidget
* GetParent(void)
100 if( mIsDestroying
) return nsnull
;
101 nsIWidget
* result
= mParent
;
102 if( mParent
) NS_ADDREF( result
);
106 virtual void OnDestroy();
108 NS_IMETHOD
SetModal(PRBool aModal
);
109 NS_IMETHOD
Show(PRBool state
);
110 inline NS_IMETHOD
CaptureRollupEvents(nsIRollupListener
*aListener
, PRBool aDoCapture
, PRBool aConsumeRollupEvent
) { return NS_OK
; }
112 inline NS_IMETHOD
IsVisible(PRBool
&aState
) { aState
= mShown
; return NS_OK
; }
114 inline NS_IMETHOD
ConstrainPosition(PRBool aAllowSlop
, PRInt32
*aX
, PRInt32
*aY
) { return NS_OK
; }
115 NS_IMETHOD
Move(PRInt32 aX
, PRInt32 aY
);
116 NS_IMETHOD
Resize(PRInt32 aWidth
, PRInt32 aHeight
, PRBool aRepaint
);
117 inline NS_IMETHOD
Resize(PRInt32 aX
, PRInt32 aY
, PRInt32 aWidth
, PRInt32 aHeight
, PRBool aRepaint
)
120 Resize(aWidth
,aHeight
,aRepaint
);
124 inline NS_IMETHOD
Enable(PRBool aState
)
126 if( mWidget
) PtSetResource( mWidget
, Pt_ARG_FLAGS
, aState
? 0 : Pt_BLOCKED
, Pt_BLOCKED
);
130 inline NS_IMETHOD
IsEnabled(PRBool
*aState
)
132 if(PtWidgetFlags(mWidget
) & Pt_BLOCKED
) *aState
= PR_FALSE
;
133 else *aState
= PR_TRUE
;
137 PRBool
OnResize(nsSizeEvent event
);
138 virtual PRBool
OnResize(nsRect
&aRect
);
139 virtual PRBool
OnMove(PRInt32 aX
, PRInt32 aY
);
141 inline nsIFontMetrics
*GetFont(void) { return nsnull
; }
142 NS_IMETHOD
SetFont(const nsFont
&aFont
);
144 inline NS_IMETHOD
SetBackgroundColor(const nscolor
&aColor
)
146 nsBaseWidget::SetBackgroundColor( aColor
);
147 if( mWidget
) PtSetResource( mWidget
, Pt_ARG_FILL_COLOR
, NS_TO_PH_RGB( aColor
), 0 );
151 NS_IMETHOD
SetCursor(nsCursor aCursor
);
153 inline NS_IMETHOD
SetColorMap(nsColorMap
*aColorMap
) { return NS_OK
; }
155 inline void* GetNativeData(PRUint32 aDataType
) { return (void *)mWidget
; }
157 NS_IMETHOD
WidgetToScreen(const nsRect
&aOldRect
, nsRect
&aNewRect
);
158 NS_IMETHOD
ScreenToWidget(const nsRect
&aOldRect
, nsRect
&aNewRect
);
160 inline NS_IMETHOD
BeginResizingChildren(void)
166 inline NS_IMETHOD
EndResizingChildren(void)
172 inline NS_IMETHOD
GetPreferredSize(PRInt32
& aWidth
, PRInt32
& aHeight
)
174 aWidth
= mPreferredWidth
;
175 aHeight
= mPreferredHeight
;
176 return (mPreferredWidth
!= 0 && mPreferredHeight
!= 0)?NS_OK
:NS_ERROR_FAILURE
;
179 inline NS_IMETHOD
SetPreferredSize(PRInt32 aWidth
, PRInt32 aHeight
)
181 mPreferredWidth
= aWidth
;
182 mPreferredHeight
= aHeight
;
186 // Use this to set the name of a widget for normal widgets.. not the same as the nsWindow version
187 inline NS_IMETHOD
SetTitle(const nsAString
& aTitle
) { return NS_OK
; }
189 inline void ConvertToDeviceCoordinates(nscoord
&aX
, nscoord
&aY
) { }
191 // the following are nsWindow specific, and just stubbed here
192 inline NS_IMETHOD
Scroll(PRInt32 aDx
, PRInt32 aDy
, nsRect
*aClipRect
) { return NS_OK
; }
193 inline NS_IMETHOD
ScrollWidgets(PRInt32 aDx
, PRInt32 aDy
) { return NS_OK
; }
195 inline NS_IMETHOD
SetMenuBar(nsIMenuBar
*aMenuBar
) { return NS_ERROR_FAILURE
; }
196 inline NS_IMETHOD
ShowMenuBar(PRBool aShow
) { return NS_ERROR_FAILURE
; }
197 // *could* be done on a widget, but that would be silly wouldn't it?
198 NS_IMETHOD
CaptureMouse(PRBool aCapture
) { return NS_ERROR_FAILURE
; }
201 NS_IMETHOD
Invalidate(PRBool aIsSynchronous
);
202 NS_IMETHOD
Invalidate(const nsRect
&aRect
, PRBool aIsSynchronous
);
203 NS_IMETHOD
InvalidateRegion(const nsIRegion
*aRegion
, PRBool aIsSynchronous
);
204 inline NS_IMETHOD
Update(void)
206 /* if the widget has been invalidated or damaged then re-draw it */
211 NS_IMETHOD
DispatchEvent(nsGUIEvent
* event
, nsEventStatus
& aStatus
);
213 inline void InitEvent(nsGUIEvent
& event
, PRUint32 aEventType
, nsPoint
* aPoint
= nsnull
)
215 if( aPoint
== nsnull
) {
216 event
.refPoint
.x
= 0;
217 event
.refPoint
.y
= 0;
220 event
.refPoint
.x
= aPoint
->x
;
221 event
.refPoint
.y
= aPoint
->y
;
224 event
.time
= PR_IntervalNow();
225 event
.message
= aEventType
;
230 inline PRBool
ConvertStatus(nsEventStatus aStatus
)
232 return aStatus
== nsEventStatus_eConsumeNoDefault
;
235 PRBool
DispatchMouseEvent(nsMouseEvent
& aEvent
);
236 inline PRBool
DispatchStandardEvent(PRUint32 aMsg
)
238 nsGUIEvent
event(PR_TRUE
, 0, nsnull
);
239 InitEvent(event
, aMsg
);
240 return DispatchWindowEvent(&event
);
243 // are we a "top level" widget?
246 // Set/Get the Pt_ARG_USER_DATA associated with each PtWidget_t *
247 // this is always set to the "this" that owns the PtWidget_t
248 static inline void SetInstance( PtWidget_t
*pWidget
, nsWidget
* inst
) { PtSetResource( pWidget
, Pt_ARG_POINTER
, (void *)inst
, 0 ); }
249 static inline nsWidget
* GetInstance( PtWidget_t
*pWidget
)
252 PtGetResource( pWidget
, Pt_ARG_POINTER
, &data
, 0 );
257 NS_IMETHOD
CreateNative(PtWidget_t
*parentWindow
) { return NS_OK
; }
259 nsresult
CreateWidget(nsIWidget
*aParent
,
261 EVENT_CALLBACK aHandleEventFunction
,
262 nsIDeviceContext
*aContext
,
263 nsIAppShell
*aAppShell
,
264 nsIToolkit
*aToolkit
,
265 nsWidgetInitData
*aInitData
,
266 nsNativeWidget aNativeParent
= nsnull
);
268 inline PRBool
DispatchWindowEvent(nsGUIEvent
* event
)
270 nsEventStatus status
;
271 DispatchEvent(event
, status
);
272 return ConvertStatus(status
);
276 void DispatchDragDropEvent( PhEvent_t
*phevent
, PRUint32 aEventType
, PhPoint_t
*pos
);
277 void ProcessDrag( PhEvent_t
*event
, PRUint32 aEventType
, PhPoint_t
*pos
);
280 // this is the "native" destroy code that will destroy any
281 // native windows / widgets for this logical widget
282 virtual void DestroyNative(void);
284 //////////////////////////////////////////////////////////////////
286 // Photon event support methods
288 //////////////////////////////////////////////////////////////////
289 static int RawEventHandler( PtWidget_t
*widget
, void *data
, PtCallbackInfo_t
*cbinfo
);
290 inline PRBool
HandleEvent( PtWidget_t
*, PtCallbackInfo_t
* aCbInfo
);
291 PRBool
DispatchKeyEvent(PhKeyEvent_t
*aPhKeyEvent
);
293 inline void ScreenToWidgetPos( PhPoint_t
&pt
)
295 // pt is in screen coordinates - convert it to be relative to ~this~ widgets origin
297 PtGetAbsPosition( mWidget
, &x
, &y
);
298 pt
.x
-= x
; pt
.y
-= y
;
301 inline void InitKeyEvent(PhKeyEvent_t
*aPhKeyEvent
, nsKeyEvent
&anEvent
);
302 inline void InitKeyPressEvent(PhKeyEvent_t
*aPhKeyEvent
, nsKeyEvent
&anEvent
);
303 void InitMouseEvent( PhPointerEvent_t
* aPhButtonEvent
,
305 nsMouseEvent
& anEvent
,
310 /* Convert Photon key codes to Mozilla key codes */
311 PRUint32
nsConvertKey( PhKeyEvent_t
*aPhKeyEvent
);
314 //Enable/Disable Photon Damage
315 inline void EnableDamage( PtWidget_t
*widget
, PRBool enable
)
317 PtWidget_t
*top
= PtFindDisjoint( widget
);
319 if( PR_TRUE
== enable
) PtEndFlux( top
);
320 else PtStartFlux( top
);
326 //////////////////////////////////////////////////////////////////
328 // Photon widget callbacks
330 //////////////////////////////////////////////////////////////////
331 static int GotFocusCallback( PtWidget_t
*widget
, void *data
, PtCallbackInfo_t
*cbinfo
);
332 static int LostFocusCallback( PtWidget_t
*widget
, void *data
, PtCallbackInfo_t
*cbinfo
);
333 static int DestroyedCallback( PtWidget_t
*widget
, void *data
, PtCallbackInfo_t
*cbinfo
);
335 static int DndCallback( PtWidget_t
*widget
, void *data
, PtCallbackInfo_t
*cbinfo
);
342 PRUint32 mPreferredWidth
, mPreferredHeight
;
343 PRBool mListenForResizes
;
345 // Focus used global variable
346 static nsWidget
* sFocusWidget
; //Current Focus Widget
348 static nsILookAndFeel
*sLookAndFeel
;
350 static nsIDragService
*sDragService
;
352 static nsClipboard
*sClipboard
;
353 static PRUint32 sWidgetCount
;
356 #endif /* nsWidget_h__ */