00001
00002
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
00020
00021
00022
00023
00024
00025
00026
00027
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*) 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
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
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
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 }
00280
00281 #endif