Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

Window.h

Go to the documentation of this file.
00001 #ifndef OglGui_Window_h
00002 #define OglGui_Window_h
00003 
00004 // (RvB) Experimental mapping to old C functions.
00005 // Maybe this should only be done in OglWindow and not here as well
00006 //
00007 // Discussion with Dennis and Michiel!!!
00008 // IMPORTANT: I am strongly considering to combine OglWindow and Window
00009 // Reason to split at first was:
00010 //      1) to get a pure wrapper without extra functionality
00011 //          (the purity however was offended when I added layout to it)
00012 //      2) I initially thought that the mapping to OGLView2D3D prevented
00013 //          overloading of existing C apps. This however is not the case
00014 
00015 #ifndef OglGui_OglWindow_h
00016 #include "OglGui/OglWindow.h"
00017 #endif
00018 
00019 #ifndef OglGui_WindowListener_h
00020 #include "OglGui/WindowListener.h"
00021 #endif
00022 
00023 #ifndef OglGui_KeyListener_h
00024 #include "OglGui/KeyListener.h"
00025 #endif
00026 
00027 #ifndef OglGui_RectTiler_h
00028 // RvB: Ik wil nog een eigen versie hiervan om onafhankelijk te zijn
00029 #include "OglGui/CxRectZTiler.h"
00030 #define OglGui_RectTiler_h
00031 #endif
00032 
00033 #ifndef strconst
00034 #define strconst    const std::string&
00035 #endif
00036 
00037 //using OglGui::OglWindow;
00038 
00039 namespace OglGui
00040 {
00041 
00093 class Window : public OglWindow
00094 {
00095 public:
00100     Window(int x, int y, int width, int height, bool is2d=true) :
00101         OglWindow(x, y, width, height)
00102     {
00103         Init(is2d);
00104     }
00105 
00110     Window(Window* parent, int width, int height, bool is2d=true) :
00111         OglWindow(parent, 0, 0, width, height)
00112     {
00113         Init(is2d);
00114         parent->WindowAdded(this);
00115     }
00116 
00121     Window(Window* parent, int x, int y, int width, int height, bool is2d=true):
00122         OglWindow(parent, x, y, width, height)
00123     {
00124         Init(is2d);
00125         mAllowReposition = false;
00126         parent->WindowAdded(this);
00127     }
00128 
00129     Window(OGLWND *oglWnd, bool is2d=true) :
00130         OglWindow(oglWnd)
00131     {
00132         Init(is2d);
00133     }
00134 
00135     Window(OGLWND *oglWnd, int width, int height, bool is2d=true) :
00136         OglWindow(oglWnd, 0, 0, width, height)
00137     {
00138         Init(is2d);
00139     }
00140 
00141     Window(OGLWND *parent, int x, int y, int width, int height, bool is2d=true):
00142         OglWindow(parent, x, y, width, height)
00143     {
00144         Init(is2d);
00145         mAllowReposition = false;
00146     }
00147 
00148     virtual ~Window()
00149     {
00150 #ifdef DESTRUCT_VERBOSE
00151 printf("virtual OglGui::Window destructor\n");
00152 #endif
00153 //        ExitFunc();
00154         if (mOldUserCFunctions.exitFunc)
00155             mOldUserCFunctions.exitFunc(mOglWnd);
00156         else
00157         if (mIs2d)
00158             viewSys.ExitView2D(mOglWnd);
00159         else
00160            view3DSys.ExitView3D(mOglWnd);
00161 
00162         // RvB: 17-2-2009 Dangerous as mWindowListener may no longer live.
00163         // Commented for now since no one was using it yet.
00164         //if (mWindowListener)
00165         //  mWindowListener->WindowExitEvent(this, mListenerData);
00166     }
00167 
00168     static Window* CastToWindow(OglWindow *oWnd)
00169     {
00170         if (!oWnd)
00171             return 0;
00172 
00173         if (oWnd->GetRunTimeType() & 2)
00174             return (Window *) oWnd;
00175         return 0;
00176     }
00177 
00178     Window* GetParent() const
00179     {
00180        return CastToWindow(GetPeer(mOglWnd->parent));    
00181     }
00182 
00183     void OffsetXY(Window* someParent, int& x, int& y)
00184     {
00185         if (this == someParent)
00186             return;
00187         x += X();
00188         y += Y();
00189         GetParent()->OffsetXY(someParent, x, y);
00190     }
00191 
00192     bool Is2d() const
00193     {
00194         return mIs2d;
00195     }
00196     void SetIs2d(bool flag)
00197     {
00198         mIs2d = flag;
00199     }
00200 
00201     void RemoveViewports() const
00202     {
00203         while (mOglWnd->wndList)
00204         {
00205             OGLWND* child = (OGLWND *) mOglWnd->wndList->info;
00206             oglSys.ExitWindow(child, 1);
00207         }
00208     }
00209 
00210 
00212 // Positioning and Scaling
00213 
00214     // For derived classes implementing RepositionViewers
00215     virtual void RepositionViewers()
00216     {
00217     }
00218 
00219     virtual void RepositionViewports()
00220     {
00221         LIST* obj;
00222 
00223         mViewportTiler.ResetWnd(mOglWnd->width, mOglWnd->height, 5);
00224 
00225         ForAllElements(obj, mOglWnd->wndList)
00226         {
00227             OGLWND* child = (OGLWND *) obj->info;
00228             if (child->tags & visibleTag)
00229             {
00230                 Window* Window = CastToWindow(GetPeer(child));
00231                 if (Window && Window->mAllowReposition)
00232                 {
00233                     int x, y, w, h;
00234                     oglSys.GetDimensions(child, &x, &y, &w, &h);
00235                     mViewportTiler.GetPos2d((float)w, (float)h, x, y);
00236                     // RvB: Added 2 here rather than in the recttiler itself
00237                     oglSys.SetDimensions(child, x+2, y, w, h);
00238                     Window->RepositionViewports();
00239                 }
00240             }
00241         }
00242     }
00243 
00244     void SetReposChildren(bool mode, bool recursive)
00245     {
00246         LIST        *obj;
00247         OGLWND      *child;
00248         Window      *wnd;
00249 
00250         ForAllElements (obj, mOglWnd->wndList)
00251         {
00252             child = (OGLWND *) obj->info;
00253             if (wnd = CastToWindow(GetPeer(child)))
00254             {
00255                 wnd->mAllowReposition = mode;
00256                 if (recursive)
00257                     wnd->SetReposChildren(mode, true);
00258             }
00259         }
00260     }
00261 
00262     void ScaleChildren(int mode=0, bool recursive=0)
00263     {
00264         LIST    *obj;
00265         OGLWND  *child;
00266         Window  *wnd;
00267 
00268         if (!mAllowChildScaling)
00269             return;
00270 
00271         ForAllElements (obj, mOglWnd->wndList)
00272         {
00273             child = (OGLWND *) obj->info;
00274             wnd = CastToWindow(GetPeer(child));
00275             if (wnd && wnd->mAllowScaling)
00276             {
00277                 wnd->ScaleTo(this, mode);
00278                 if (recursive)
00279                     wnd->ScaleChildren(mode, true);
00280             }
00281         }
00282     }
00283     /*
00284     void ScaleChildren(int mode=0, bool recursive=0)
00285     {
00286         LIST    *obj;
00287         OGLWND  *child;
00288         Window  *wnd = CastToWindow(GetPeer(mOglWnd));
00289 
00290         if (!wnd || !wnd->mAllowChildScaling)
00291             return;
00292 
00293         ForAllElements (obj, mOglWnd->wndList)
00294         {
00295             child = (OGLWND *) obj->info;
00296             wnd = CastToWindow(GetPeer(child));
00297             if (wnd && wnd->mAllowScaling)
00298             {
00299                 wnd->ScaleTo(this, mode);
00300                 if (recursive)
00301                     wnd->ScaleChildren(mode, true);
00302             }
00303         }
00304     }
00305     */
00306 
00308 // Set/Get methods
00309 
00316     void SetWindowListener(WindowListener* listener, void* listenerData = 0)
00317     {
00318         mWindowListener = listener;
00319         mListenerData = listenerData;
00320     }
00321     void SetWindowListener(WindowListener* listener, int listenerData)
00322     {
00323         SetWindowListener(listener, (void*) listenerData);
00324     }
00325 
00326     // RvB: Experimental
00327     // allowing multiple listeners with chain of responsibility
00328     WindowListener* GetWindowListener(void*& userData)
00329     {
00330         userData = mListenerData;
00331         return mWindowListener;
00332     }
00333 
00334     // RvB: In destructor of Classes that set the globalKeyListener
00335     // a check should be made if they are the global key listener and
00336     // set it to 0, otherwise we risk calling a non-existing object.
00337     // Should we allow for multiple global key listeners? Discuss with Dennis!
00338     static void SetGlobalKeyListener(KeyListener* listener)
00339     {
00340         sGlobalKeyListener = listener;
00341     }
00342 
00343     void SetDisableOGLViewKeys(bool flag)
00344     {
00345         mDisableOGLViewKeys = flag;
00346     }
00347 
00348     bool GetDisableOGLViewKeys() const
00349     {
00350         return mDisableOGLViewKeys;
00351     }
00352 
00353     void SetDisableOGLViewMouse(bool flag)
00354     {
00355         mDisableOGLViewMouse = flag;
00356     }
00357 
00358     bool GetDisableOGLViewMouse() const
00359     {
00360         return mDisableOGLViewMouse;
00361     }
00362 
00363     static void GlobalKeyListeningDisabled(bool flag)
00364     {
00365         sGlobalKeyListeningDisabled = flag;
00366     }
00367 
00368     static bool GlobalKeyListeningDisabled()
00369     {
00370         return sGlobalKeyListeningDisabled;
00371     }
00372 
00373     void SetDisableGlobalKeyListener(bool flag)
00374     {
00375         mDisableGlobalKeyListener = flag;
00376     }
00377 
00378     bool GetDisableGlobalKeyListener() const
00379     {
00380         return mDisableGlobalKeyListener;
00381     }
00382 
00383     void SetAllowReposition(bool mode=true)
00384     {
00385         mAllowReposition = mode;
00386     }
00387 
00388     bool GetAllowReposition() const
00389     {
00390         return mAllowReposition;
00391     }
00392 
00393     void SetAllowScaling(bool mode)
00394     {
00395         mAllowScaling = mode;
00396     }
00397 
00398     bool GetAllowScaling() const
00399     {
00400         return mAllowScaling;
00401     }
00402 
00403     void SetAllowChildScaling(bool mode)
00404     {
00405         mAllowChildScaling = mode;
00406     }
00407 
00408     bool GetAllowChildScaling() const
00409     {
00410         return mAllowChildScaling;
00411     }
00412 
00413     void SetForeground(ULONG col)
00414     {
00415         mForeGroundColor = col;
00416     }
00417 
00418     ULONG GetForeground() const
00419     {
00420         return mForeGroundColor;
00421     }
00422 
00423     virtual int SetState(int nState)
00424     {
00425         int oldState = mState;
00426         mState = nState;
00427         return oldState;
00428     }
00429 
00430     int GetState() const
00431     {
00432         return mState;
00433     }
00434 
00435     void SetDoStateFeedback(bool on)
00436     {
00437         mDoStateFeedback = on;
00438     }
00439 
00440     void SetStateFeedbackColor(ULONG col)
00441     {
00442         mStateFeedbackColor = col;
00443     }
00444 
00445     bool GetDoStateFeedback() const
00446     {
00447         return mDoStateFeedback;
00448     }
00449 
00450     void SetExternWindowReference(Window *refWnd)
00451     {
00452         mExternWindowReference = refWnd;
00453     }
00454 
00455     Window* GetExternWindowReference() const
00456     {
00457         return mExternWindowReference;
00458     }
00459 
00460     void SetWindowText(char *txt)
00461     {
00462         OGLWND* oglWnd = oglSys.GetTopOGLWND(mOglWnd);
00463 //#ifndef OGL_USING_GLUT
00464 #if !defined(OGL_USING_GLUT) && !defined(OGL_USING_QT)
00465         if (oglWnd->hWnd)
00466             ::SetWindowText((HWND)oglWnd->hWnd, txt);
00467 #endif
00468         oglWnd->topTitle = txt;
00469     }
00470 
00471     virtual void MapKeysTo(Window* wnd)
00472     {
00473         mMapKeysTarget = wnd;
00474     }
00475 
00476     void        GuiGeneralName(std::string name)  { mGuiGeneralName = name; }
00477     std::string GuiGeneralName()                  { return mGuiGeneralName; }
00478 
00479     void        GuiName(std::string name) { mGuiName = name;}
00480     std::string GuiName()                 { return mGuiName;}
00481 
00482 
00484 // OGL standard callback functions
00485 
00486     virtual void InitFunc()
00487     {
00488         if (mOldUserCFunctions.initFunc != 0)
00489             OglWindow::InitFunc();
00490         else if (mIs2d)
00491             viewSys.InitView2D(mOglWnd);
00492         else
00493             view3DSys.InitView3D(mOglWnd);
00494 
00495         RepositionViewports();
00496         RepositionViewers();
00497 
00498         if (mWindowListener)
00499             mWindowListener->WindowInitEvent(this, mListenerData);
00500     }
00501 
00502     //Still somewhat experimental phase.
00503     //For now handled by destructor.
00504 //    virtual void ExitFunc()
00505 //    {
00506 //    }
00507 
00508     virtual void InitDisplayFunc()
00509     {
00510         if (mOldUserCFunctions.initDisplayFunc)
00511             OglWindow::InitDisplayFunc();
00512         else if (mIs2d)
00513             viewSys.InitDrawScene(mOglWnd);
00514         else
00515             view3DSys.InitDrawScene(mOglWnd);
00516 
00517         mOldBorderBackground = mOglWnd->borderBgCol;
00518 
00519         if ((GetState() == 2) && mDoStateFeedback)
00520             mOglWnd->borderBgCol = mStateFeedbackColor;
00521 
00522         if (mWindowListener)
00523             mWindowListener->WindowInitDisplayEvent(this, mListenerData);
00524     }
00525 
00526     virtual void DisplayFunc()
00527     {
00528         mOglWnd->borderBgCol = mOldBorderBackground;
00529 
00530         if (mOldUserCFunctions.displayFunc)
00531             OglWindow::DisplayFunc();
00532         else if (mIs2d)
00533             viewSys.DrawScene(mOglWnd);
00534         else
00535             view3DSys.DrawScene(mOglWnd);
00536 
00537         if (mWindowListener)
00538             mWindowListener->WindowDisplayEvent(this, mListenerData);
00539     }
00540 
00548     virtual void MouseFunc(int msg, int but, int state, int x, int y)
00549     {
00550         if (!GetState())
00551             return;
00552 
00553         if (msg == oglMouseEnter)
00554         {
00555             SetState(2);
00556             oglSys.UpdateSceneFlag(mOglWnd, 1);
00557         }
00558         if (msg == oglMouseLeave)
00559         {
00560             SetState(1);
00561             oglSys.UpdateSceneFlag(mOglWnd, 1);
00562         }
00563 
00564         if (mOldUserCFunctions.mouseFunc)
00565             OglWindow::MouseFunc(msg, but, state, x, y);
00566         else if (!mDisableOGLViewMouse)
00567         {
00568             if (mIs2d)
00569                 viewSys.MouseFunc(mOglWnd, msg, but, state, x, y);
00570             else
00571                 view3DSys.MouseFunc(mOglWnd, msg, but, state, x, y);
00572         }
00573 
00574         if (mWindowListener)
00575             mWindowListener->WindowMouseEvent(this, msg, but, state, x, y,
00576                                               mListenerData);
00577     }
00578 
00579     virtual void KeyboardFunc(int c, int state)
00580     {
00581         if (!GetState())
00582             return;
00583         if (mMapKeysTarget)
00584             mMapKeysTarget->KeyboardFunc(c, state);
00585 
00586         if (mOldUserCFunctions.keyboardFunc)
00587             OglWindow::KeyboardFunc(c, state);
00588         else if (!mDisableOGLViewKeys)
00589         {
00590             if (mIs2d)
00591                 viewSys.KeyFunc(mOglWnd, c, state);
00592             else
00593                 view3DSys.KeyFunc(mOglWnd, c, state);
00594         }
00595 
00596         if (!mMapKeysTarget && sGlobalKeyListener && !mDisableGlobalKeyListener &&
00597             !sGlobalKeyListeningDisabled)
00598             sGlobalKeyListener->KeyEvent(this, c, state);
00599 
00600         if (mWindowListener)
00601             mWindowListener->WindowKeyboardEvent(this, c, state, mListenerData);
00602     }
00603 
00604     virtual void ViewportFunc(int width, int height)
00605     {
00606         if (mOldUserCFunctions.viewportFunc)
00607             OglWindow::ViewportFunc(width, height);
00608         else if (mIs2d)
00609             viewSys.Viewport(mOglWnd, width, height);
00610         else
00611             view3DSys.Viewport(mOglWnd, width, height);
00612 
00613         if (mWindowListener)
00614             mWindowListener->WindowViewportEvent(this, width, height,
00615                                                  mListenerData);
00616     }
00617 
00618     virtual void ReshapeFunc(int width, int height)
00619     {
00620         if (mOldUserCFunctions.reshapeFunc)
00621             OglWindow::ReshapeFunc(width, height);
00622         else if (mIs2d)
00623             viewSys.Reshape(mOglWnd, width, height);
00624         else
00625             view3DSys.Reshape(mOglWnd, width, height);
00626 
00627         RepositionViewports();
00628         RepositionViewers();
00629         OnLayoutChange();
00630 
00631         if (mWindowListener)
00632             mWindowListener->WindowReshapeEvent(this, width, height,
00633                                                 mListenerData);
00634     }
00635 
00636 protected:
00637 
00638     bool                mDoStateFeedback;
00639     ULONG               mStateFeedbackColor;
00640     ULONG               mForeGroundColor;
00641 
00642     Window*             mMapKeysTarget;
00643 
00644     bool                mIs2d;
00645     bool                mAllowReposition;
00646     bool                mAllowScaling;
00647     bool                mAllowChildScaling;
00648 
00649     std::string         mGuiName;
00650     std::string         mGuiGeneralName;
00651 
00652     static std::string  mClipboardString;
00653 
00654     virtual void WindowAdded(Window* wnd)
00655     {
00656     }
00657 
00659 private:
00660 
00661     void
00662     Init(bool is2d)
00663     {
00664         //GuiGeneralName("Window");
00665         mWindowListener = 0;
00666         mListenerData = 0;
00667 
00668         mDisableGlobalKeyListener = false;
00669         mMapKeysTarget = 0;
00670 
00671         oglSys.SetAllowDeleteView(mOglWnd, FALSE);
00672         oglSys.SetAllowCameraMove(mOglWnd, FALSE);
00673         oglSys.SetAllowPick(mOglWnd, FALSE);
00674         oglSys.SetAllowRubber(mOglWnd, FALSE);
00675 
00676         // RvB: Maybe next 2 even standard true, and those needing them
00677         // have to set to false
00678         mDisableOGLViewKeys = false;
00679         mDisableOGLViewMouse = false;
00680 
00681         mRunTimeType |= 2;
00682         mExternWindowReference = 0; // Reference to some external wnd
00683         mState = 1; // 0: disabled, 1:normal, 2:mouse in wnd (highlight)
00684         mDoStateFeedback = false;
00685         mStateFeedbackColor = 0x80e08080;
00686         mForeGroundColor = 0xff000000;
00687         mOldBorderBackground = 0x0;
00688         mAllowReposition = true;
00689         mAllowScaling = true;
00690         mAllowChildScaling = true;
00691         mIs2d = is2d;
00692     }
00693 
00694     static KeyListener*   sGlobalKeyListener;
00695     static bool           sGlobalKeyListeningDisabled; // Globally
00696     bool                  mDisableGlobalKeyListener;   // Per window
00697 
00698     Window*               mExternWindowReference;
00699     WindowListener*       mWindowListener;
00700     void*                 mListenerData;
00701     int                   mState;
00702     ULONG                 mOldBorderBackground;
00703     CxRectZTiler          mViewportTiler;
00704     // RvB: Maybe introduce mDisableOGLViewKeys so that components not
00705     // using the viewer modules can prevent things like popping up help on ?
00706     // Decided to implement!
00707     bool                  mDisableOGLViewKeys;
00708     bool                  mDisableOGLViewMouse;
00709 };
00710 
00711 } // namespace OglGui
00712 #endif

Generated on Fri Mar 19 09:31:41 2010 for ImpalaSrc by  doxygen 1.5.1