Back to index

lightning-sunbird  0.9+nobinonly
nsWindow.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Paul Ashford <arougthopher@lizardland.net>
00024  *   Sergei Dolgov <sergei_d@fi.tartu.ee>
00025  *   Fredrik Holmqvist <thesuckiestemail@yahoo.se>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 #ifndef Window_h__
00041 #define Window_h__
00042 #define BeIME
00043 
00044 #include "nsBaseWidget.h"
00045 #include "nsdefs.h"
00046 #include "nsSwitchToUIThread.h"
00047 #include "nsToolkit.h"
00048 
00049 #include "nsIWidget.h"
00050 
00051 #include "nsIMenuBar.h"
00052 
00053 #include "nsIMouseListener.h"
00054 #include "nsIEventListener.h"
00055 #include "nsString.h"
00056 #include "nsRegion.h"
00057 
00058 #include <Window.h>
00059 #include <View.h>
00060 #include <Region.h>
00061 
00062 #if defined(BeIME)
00063 #include <Messenger.h>
00064 #endif
00065 
00066 #define NSRGB_2_COLOREF(color) \
00067             RGB(NS_GET_R(color),NS_GET_G(color),NS_GET_B(color))
00068 
00069 // forward declaration
00070 class nsViewBeOS;
00071 class nsIRollupListener;
00072 #if defined(BeIME)
00073 class nsIMEBeOS;
00074 #endif
00075 
00079 class nsWindow : public nsSwitchToUIThread,
00080                  public nsBaseWidget
00081 {
00082 
00083 public:
00084        nsWindow();
00085        virtual ~nsWindow();
00086 
00087        // In BeOS, each window runs in its own thread.  Because of this,
00088        // we have a proxy layer between the mozilla UI thread, and calls made
00089        // within the window's thread via CallMethod().  However, since the windows
00090        // are still running in their own thread, and reference counting takes place within
00091        // that thread, we need to reference and de-reference outselves atomically.
00092        // See BugZilla Bug# 92793
00093        NS_IMETHOD_(nsrefcnt) AddRef(void);                                       
00094        NS_IMETHOD_(nsrefcnt) Release(void);          
00095 
00096        // nsIWidget interface
00097        NS_IMETHOD              Create(nsIWidget *aParent,
00098                                       const nsRect &aRect,
00099                                       EVENT_CALLBACK aHandleEventFunction,
00100                                       nsIDeviceContext *aContext,
00101                                       nsIAppShell *aAppShell = nsnull,
00102                                       nsIToolkit *aToolkit = nsnull,
00103                                       nsWidgetInitData *aInitData = nsnull);
00104        NS_IMETHOD              Create(nsNativeWidget aParent,
00105                                       const nsRect &aRect,
00106                                       EVENT_CALLBACK aHandleEventFunction,
00107                                       nsIDeviceContext *aContext,
00108                                       nsIAppShell *aAppShell = nsnull,
00109                                       nsIToolkit *aToolkit = nsnull,
00110                                       nsWidgetInitData *aInitData = nsnull);
00111 
00112        // Utility method for implementing both Create(nsIWidget ...) and
00113        // Create(nsNativeWidget...)
00114 
00115        NS_IMETHOD          PreCreateWidget(nsWidgetInitData *aWidgetInitData);
00116 
00117        virtual nsresult        StandardWindowCreate(nsIWidget *aParent,
00118                                                     const nsRect &aRect,
00119                                                     EVENT_CALLBACK aHandleEventFunction,
00120                                                     nsIDeviceContext *aContext,
00121                                                     nsIAppShell *aAppShell,
00122                                                     nsIToolkit *aToolkit,
00123                                                     nsWidgetInitData *aInitData,
00124                                                     nsNativeWidget aNativeParent = nsnull);
00125 
00126        NS_IMETHOD              Destroy();
00127        virtual nsIWidget*        GetParent(void);
00128        NS_IMETHOD              Show(PRBool bState);
00129        NS_IMETHOD              CaptureMouse(PRBool aCapture);
00130        NS_IMETHOD              CaptureRollupEvents(nsIRollupListener *aListener,
00131                                                    PRBool aDoCapture,
00132                                                    PRBool aConsumeRollupEvent);
00133        NS_IMETHOD              IsVisible(PRBool & aState);
00134 
00135        NS_IMETHOD              ConstrainPosition(PRBool aAllowSlop,
00136                                                  PRInt32 *aX, PRInt32 *aY);
00137        NS_IMETHOD              Move(PRInt32 aX, PRInt32 aY);
00138        NS_IMETHOD              Resize(PRInt32 aWidth,
00139                                       PRInt32 aHeight,
00140                                       PRBool   aRepaint);
00141        NS_IMETHOD              Resize(PRInt32 aX,
00142                                       PRInt32 aY,
00143                                       PRInt32 aWidth,
00144                                       PRInt32 aHeight,
00145                                       PRBool   aRepaint);
00146        NS_IMETHOD              SetModal(PRBool aModal);
00147        NS_IMETHOD              Enable(PRBool aState);
00148        NS_IMETHOD              IsEnabled(PRBool *aState);
00149        NS_IMETHOD              SetFocus(PRBool aRaise);
00150        NS_IMETHOD              GetScreenBounds(nsRect &aRect);
00151        NS_IMETHOD              SetBackgroundColor(const nscolor &aColor);
00152        virtual nsIFontMetrics* GetFont(void);
00153        NS_IMETHOD              SetFont(const nsFont &aFont);
00154        NS_IMETHOD              SetCursor(nsCursor aCursor);
00155        NS_IMETHOD              Invalidate(PRBool aIsSynchronous);
00156        NS_IMETHOD              Invalidate(const nsRect & aRect, PRBool aIsSynchronous);
00157        NS_IMETHOD              InvalidateRegion(const nsIRegion *aRegion,
00158                                                 PRBool aIsSynchronous);
00159        NS_IMETHOD              Update();
00160        virtual void*           GetNativeData(PRUint32 aDataType);
00161        NS_IMETHOD              SetColorMap(nsColorMap *aColorMap);
00162        NS_IMETHOD              Scroll(PRInt32 aDx, PRInt32 aDy, nsRect *aClipRect);
00163        NS_IMETHOD              SetTitle(const nsAString& aTitle);
00164        NS_IMETHOD              SetMenuBar(nsIMenuBar * aMenuBar) { return NS_ERROR_FAILURE; }
00165        NS_IMETHOD              ShowMenuBar(PRBool aShow) { return NS_ERROR_FAILURE; }
00166        NS_IMETHOD              WidgetToScreen(const nsRect& aOldRect, nsRect& aNewRect);
00167        NS_IMETHOD              ScreenToWidget(const nsRect& aOldRect, nsRect& aNewRect);
00168        NS_IMETHOD              BeginResizingChildren(void);
00169        NS_IMETHOD              EndResizingChildren(void);
00170        NS_IMETHOD              GetPreferredSize(PRInt32& aWidth, PRInt32& aHeight);
00171        NS_IMETHOD              SetPreferredSize(PRInt32 aWidth, PRInt32 aHeight);
00172        NS_IMETHOD              DispatchEvent(nsGUIEvent* event, nsEventStatus & aStatus);
00173 
00174        virtual void            ConvertToDeviceCoordinates(nscoord &aX, nscoord &aY) {}
00175 
00176 
00177        // nsSwitchToUIThread interface
00178        virtual bool            CallMethod(MethodInfo *info);
00179        virtual PRBool          DispatchMouseEvent(PRUint32 aEventType, 
00180                                                   nsPoint aPoint, 
00181                                                   PRUint32 clicks, 
00182                                                   PRUint32 mod);
00183 
00184 
00185        void                   InitEvent(nsGUIEvent& event, nsPoint* aPoint = nsnull);
00186 
00187 protected:
00188 
00189        static PRBool           EventIsInsideWindow(nsWindow* aWindow, nsPoint pos) ;
00190        static PRBool           DealWithPopups(uint32 methodID, nsPoint pos);
00191     // Following methods need to be virtual if we will subclassing
00192        void                    OnDestroy();
00193        void                    OnWheel(PRInt32 aDirection, uint32 aButtons, BPoint aPoint, nscoord aDelta);
00194        PRBool                  OnMove(PRInt32 aX, PRInt32 aY);
00195        nsresult                OnPaint(BRegion *breg);
00196        PRBool                  OnResize(nsRect &aWindowRect);
00197        PRBool                  OnKeyDown(PRUint32 aEventType, 
00198                                          const char *bytes, 
00199                                          int32 numBytes, 
00200                                          PRUint32 mod, 
00201                                          PRUint32 bekeycode, 
00202                                          int32 rawcode);
00203        PRBool                  OnKeyUp(PRUint32 aEventType, 
00204                                        const char *bytes, 
00205                                        int32 numBytes, 
00206                                        PRUint32 mod, 
00207                                        PRUint32 bekeycode, 
00208                                        int32 rawcode);
00209        PRBool                  DispatchKeyEvent(PRUint32 aEventType, PRUint32 aCharCode,
00210                                            PRUint32 aKeyCode, PRUint32 aFlags = 0);
00211        PRBool                  DispatchFocus(PRUint32 aEventType);
00212        static PRBool           ConvertStatus(nsEventStatus aStatus);
00213        PRBool                  DispatchStandardEvent(PRUint32 aMsg);
00214 
00215        PRBool                  DispatchWindowEvent(nsGUIEvent* event);
00216        void                    HideKids(PRBool state);
00217 
00218 
00219        nsCOMPtr<nsIWidget> mParent;
00220        nsWindow*        mWindowParent;
00221        nsCOMPtr<nsIRegion> mUpdateArea;
00222        nsIFontMetrics*  mFontMetrics;
00223 
00224        nsViewBeOS*      mView;
00225        PRInt32          mPreferredWidth;
00226        PRInt32          mPreferredHeight;
00227        window_feel      mBWindowFeel;
00228  
00229        //Just for saving space we use packed bools.
00230        PRPackedBool           mIsTopWidgetWindow;
00231        PRPackedBool           mIsMetaDown;
00232        PRPackedBool           mIsVisible;
00233        PRPackedBool           mEnabled;
00234        PRPackedBool           mIsScrolling;
00235        PRPackedBool           mListenForResizes;
00236        
00237 public:       // public on BeOS to allow BViews to access it
00238 
00239        nsToolkit *GetToolkit() { return (nsToolkit *)nsBaseWidget::GetToolkit(); }
00240 };
00241 
00242 //
00243 // Each class need to subclass this as part of the subclass
00244 //
00245 class nsIWidgetStore
00246 {
00247 public:
00248                                nsIWidgetStore(nsIWidget *aWindow);
00249        virtual                ~nsIWidgetStore();
00250 
00251        virtual nsIWidget      *GetMozillaWidget(void);
00252 
00253 private:
00254        nsIWidget       *mWidget;
00255 };
00256 
00257 //
00258 // A BWindow subclass
00259 //
00260 class nsWindowBeOS : public BWindow, public nsIWidgetStore
00261 {
00262 public:
00263                                nsWindowBeOS(nsIWidget *aWidgetWindow,  
00264                                             BRect aFrame, 
00265                                             const char *aName, 
00266                                             window_look aLook,
00267                                             window_feel aFeel, 
00268                                             int32 aFlags, 
00269                                             int32 aWorkspace = B_CURRENT_WORKSPACE);
00270        virtual                ~nsWindowBeOS();
00271 
00272        virtual bool            QuitRequested( void );
00273        virtual void            MessageReceived(BMessage *msg);
00274        virtual void            DispatchMessage(BMessage *msg, BHandler *handler);
00275        virtual void            WindowActivated(bool active);
00276        virtual void            FrameMoved(BPoint origin);
00277        virtual void            WorkspacesChanged(uint32 oldworkspace, uint32 newworkspace);
00278        virtual void            FrameResized(float width, float height);
00279 
00280        bool                    fJustGotBounds;   
00281 private:
00282        BPoint          lastWindowPoint;
00283 };
00284 
00285 //
00286 // A BView subclass
00287 //
00288 class nsViewBeOS : public BView, public nsIWidgetStore
00289 {
00290 public:
00291                                nsViewBeOS(nsIWidget *aWidgetWindow, 
00292                                           BRect aFrame, 
00293                                           const char *aName,
00294                                           uint32 aResizingMode, 
00295                                           uint32 aFlags);
00296 
00297        virtual void            Draw(BRect updateRect);
00298        virtual void            MouseDown(BPoint point);
00299        virtual void            MouseMoved(BPoint point, 
00300                                           uint32 transit, 
00301                                           const BMessage *message);
00302        virtual void            MouseUp(BPoint point);
00303        bool                    GetPaintRegion(BRegion *breg);
00304        void                    Validate(BRegion *reg);
00305        BPoint                  GetWheel();
00306        void                    KeyDown(const char *bytes, int32 numBytes);
00307        void                    KeyUp(const char *bytes, int32 numBytes);
00308        virtual void            MakeFocus(bool focused);
00309        virtual void            MessageReceived(BMessage *msg);
00310        void                    SetVisible(bool visible);
00311        bool                    Visible();
00312        BRegion                 paintregion;
00313        uint32                  buttons;
00314 
00315 private:
00316 #if defined(BeIME)
00317        void                 DoIME(BMessage *msg);
00318 #endif
00319        BPoint               mousePos;
00320        uint32               mouseMask;
00321        // actually it is delta, not point, using it as convinient x,y storage
00322        BPoint               wheel;
00323        bool                 fRestoreMouseMask;   
00324        bool                 fJustValidated;
00325        bool                 fWheelDispatched;
00326        bool                 fVisible;
00327 };
00328 
00329 //
00330 // A child window is a window with different style
00331 //
00332 class ChildWindow : public nsWindow 
00333 {
00334 public:
00335                                ChildWindow() {};
00336        virtual PRBool          IsChild() { return(PR_TRUE); };
00337 };
00338 
00339 #if defined(BeIME)
00340 class nsIMEBeOS 
00341 {
00342 public:
00343        nsIMEBeOS();
00344 //     virtual ~nsIMEBeOS();
00345        void   RunIME(uint32 *args, nsWindow *owner, BView* view);
00346        void   DispatchText(nsString &text, PRUint32 txtCount, nsTextRange* txtRuns);
00347        void   DispatchIME(PRUint32 what);
00348        void   DispatchCancelIME();
00349        PRBool DispatchWindowEvent(nsGUIEvent* event);
00350        
00351        static  nsIMEBeOS *GetIME();
00352  
00353 private:
00354        nsWindow*     imeTarget;
00355        BMessenger    imeMessenger;
00356        nsString      imeText;
00357        BPoint        imeCaret;
00358        PRUint32      imeState, imeWidth, imeHeight;
00359        static     nsIMEBeOS *beosIME;
00360 };
00361 #endif
00362 #endif // Window_h__