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

Button.h

Go to the documentation of this file.
00001 /*
00002 12345678901234567890123456789012345678901234567890123456789012345678901234567890
00003 */
00004 #ifndef OglGui_Button_h
00005 #define OglGui_Button_h
00006 
00007 #ifndef OglGui_RepeatTimer_h
00008 #include "OglGui/RepeatTimer.h"
00009 #endif
00010 
00011 #ifndef OglGui_StaticText_h
00012 #include "OglGui/StaticText.h"
00013 #endif
00014 
00015 #ifndef OglGui_ButtonListener_h
00016 #include "OglGui/ButtonListener.h"
00017 #endif
00018 
00019 // RvB: Zou wat mij betreft ook niet van static text afgeleid kunnen worden.
00020 // Ik wil in ieder geval nog een functie in Window.h
00021 // die een gegeven (meerlijnige) text kan aligneren in beide richtingen.
00022 // Iets als:
00023 // AlignText(strconst txt, TxtAlign hor=AlignCenter, TxtAlign vert=AlignCenter,
00024 //           bool shaded=false, shadCol=oglGREY )
00025 // De text zou dan in de mForeGroundCol getekend kunnen worden.
00026 // Wanneer deze functie bestaat heeft de afleiding van Static text
00027 // weinig zin meer
00028 
00029 namespace OglGui
00030 {
00031 
00032 class Button : public StaticText, public RepeatTimer
00033 {
00034 public:
00035 
00036     Button(Window* parent, int x, int y, int width, int height, strconst label,
00037            int borderType = BEV_RIDGE, bool isRect=false) :
00038         StaticText(parent, x, y, width, height, label, true, true)
00039     {
00040         Init(borderType, isRect);
00041     }
00042 
00043     Button(Window* parent, int width, int height, strconst label,
00044            int borderType = BEV_RIDGE, bool isRect=false) :
00045         StaticText(parent, width, height, label, true, true)
00046     {
00047         Init(borderType, isRect);
00048     }
00049 
00050     void
00051     SetButtonListener(ButtonListener* listener, void* listenerData = 0)
00052     {
00053         mButtonListener = listener;
00054         mButtonListenerData = listenerData;
00055     }
00056     void
00057     SetButtonListener(ButtonListener* listener, int listenerData)
00058     {
00059         SetButtonListener(listener, (void*) /*(long long)*/ listenerData);
00060     }
00061 
00062     void
00063     DoButtonSelectionEvent()
00064     {
00065         if( mButtonListener && GetState() )
00066             mButtonListener->ButtonSelectionEvent(this, mButtonListenerData);
00067     }
00068 
00069     virtual void
00070     InitDisplayFunc()
00071     {
00072         oglSys.SetBorderFillShaded( mOglWnd, mFillShadedType * (mIsPressed ? -1 : 1) );
00073         glNormal3f(0,0,1);
00074         if( mIsSelected )
00075         {
00076             ULONG oldBg = oglSys.GetBorderBackground( mOglWnd );
00077             oglSys.SetBorderBackground( mOglWnd, mSelectedColor );
00078             HandleShadeBorder( mOglWnd );
00079             oglSys.SetBorderBackground( mOglWnd, oldBg );
00080         }
00081         StaticText::InitDisplayFunc();
00082     }
00083 
00084     virtual void
00085     DisplayFunc()
00086     {
00087         if( mRepeatMode && mIsPressed && RepeatTime() )
00088             DoButtonSelectionEvent();
00089 
00090         Window::SetBorderType( mIsPressed ? mDownBorderType : mUpBorderType );
00091 
00092         Window::DisplayFunc();
00093         AlignString( mAlignOffH + (mIsPressed?1:0), mAlignOffV + (mIsPressed?-1:0) );
00094     }
00095 
00096     virtual void
00097     MouseFunc(INT msg, INT but, INT state, INT x, INT y)
00098     {
00099         if (!GetState()) return;
00100 
00101         Window::MouseFunc( msg, but, state, x, y );
00102 
00103         if (state & (oglAlt|oglShift|oglControl))
00104             return;
00105 
00106         // RvB: Experimental
00107         if (msg == oglMouseWheelUp || msg == oglMouseWheelDown)
00108             GetParent()->MouseFunc(msg, but, state, x, y);
00109 
00110         if ((msg == oglMouseDown || msg == oglMouseDblClick)
00111             && (but == oglLeftButton) )
00112         {
00113             mIsPressed = true;
00114             if (mRepeatMode)
00115             {
00116                 StartRepeatTime();
00117                 oglSys.SetAlwaysDraw(mOglWnd, true);
00118                 DoButtonSelectionEvent();
00119             }
00120         }
00121         if (msg == oglMouseMove && (state & oglLeftButton) )
00122         {
00123             if( (x < 0 || x > mOglWnd->width || y < 0 || y > mOglWnd->height) &&
00124                 mShowCancelState )
00125                 mIsPressed = false;
00126             else
00127                 mIsPressed = true;
00128             if( mShowCancelState )
00129                 SetState( mIsPressed ? 2 : 1 );
00130         }
00131         if (msg == oglMouseUp)
00132         {
00133             oglSys.SetAlwaysDraw(mOglWnd, false);
00134             oglSys.UpdateSceneFlag(mOglWnd, 1);
00135             if (!mRepeatMode && mIsPressed)
00136             {   mIsPressed = false;
00137                 DoButtonSelectionEvent();
00138             }
00139             else
00140                 mIsPressed = false;
00141         }
00142     }
00143 
00144     /*
00145     virtual void
00146     KeyboardFunc( INT c, INT state )
00147     {
00148         Window::KeyboardFunc(c, state);
00149 
00150         // Next experimental lines made conditional after introduction of MapKeysTo
00151         // Now if you want the keyboard input of a button you must
00152         // call: but->MapKeysTo(Window*);
00153 
00154         // RvB: Experimental, passing on to parent
00155         if (!mMapKeysTarget)
00156             GetParent()->KeyboardFunc(c, state);
00157     }
00158     */
00159 
00160     void
00161     SetLabel( std::string label )
00162     {
00163         SetText( label );
00164     }
00165 
00166     std::string
00167     GetLabel()
00168     {
00169         return GetText();
00170     }
00171 
00172     virtual void
00173     SetBorderType( int type )
00174     {
00175         Window::SetBorderType( type );
00176         mUpBorderType = mDownBorderType = type;
00177     }
00178 
00179     void
00180     SetUpBorderType( int type )
00181     {
00182         mUpBorderType = type;
00183     }
00184 
00185     void
00186     SetDownBorderType( int type )
00187     {
00188         mDownBorderType = type;
00189     }
00190 
00191         virtual void
00192         SetBorderFillShaded(int type=2)
00193         {
00194                 mFillShadedType = type;
00195         Window::SetBorderFillShaded(type);
00196         }
00197 
00198     void
00199     SetRepeatMode(bool b=true)
00200     {
00201         mRepeatMode = b;
00202     }
00203 
00204     bool
00205     IsPressed()
00206     {
00207         return mIsPressed;
00208     }
00209 
00210     void
00211     SetPressed( bool pressed )
00212     {
00213         mIsPressed = pressed;
00214     }
00215 
00216     void
00217     SetSelected( bool selected )
00218     {
00219         mIsSelected = selected;
00220     }
00221 
00222     bool
00223     IsSelected()
00224     {
00225         return mIsSelected;
00226     }
00227 
00228     void
00229     SetShowCancelState( bool mode )
00230     {
00231         mShowCancelState = mode;
00232     }
00233 
00234 private:
00235 
00236     void
00237     Init(int borderType, bool isRect)
00238     {
00239         GuiGeneralName("Button");
00240 
00241         mButtonListener = 0;
00242         mRepeatMode = false;
00243         SetRepeatDelays( 400, 20 );
00244 
00245         mDoStateFeedback = true;
00246         mShowCancelState = true;
00247         mIsPressed = false;
00248         mIsSelected = false;
00249         SetForeground( oglBLACK );
00250         
00251         mSelectedColor = 0xffff0000;
00252 
00253         SetBorderType( borderType );
00254         mDownBorderType = BEV_SUNKEN;
00255 
00256         SetBorderFillShaded(2);
00257         SetBorderFillOpacity( 0.5f );
00258         if( !isRect )
00259             SetRoundness( 200, 200, 200, 200 );
00260 
00261         SetDisableOGLViewKeys( true );
00262         SetDisableOGLViewMouse( true );
00263     }
00264 
00265     ButtonListener*   mButtonListener;
00266     void*             mButtonListenerData;
00267     ULONG             mSelectedColor;
00268     bool              mRepeatMode;
00269 
00270     bool              mIsPressed;
00271     bool              mIsSelected;
00272     bool              mShowCancelState;
00273     int               mUpBorderType;
00274     int               mDownBorderType;
00275     int               mFillShadedType;
00276 
00277 };
00278 
00279 } // namespace OglGui
00280 
00281 #endif

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