00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(SJSLIDER_H_INCLUDED)
00012 #define SJSLIDER_H_INCLUDED
00013
00014 #include "wnd.h"
00015
00016 namespace sjgui{
00017
00018 namespace SJGUI_USE_STYLE
00019 {
00020 extern CWnd::CSize def_slider_size;
00021 extern float def_slider_length;
00022 extern CWnd::eOrient def_slider_orientation;
00023 }
00034 template<class Tbase>
00035 class CSliderCtrlTmpl : public Tbase
00036 {
00037 public:
00038 private:
00039 float m_fScale;
00040 void Adjust();
00041 protected:
00042 int m_iMin;
00043 int m_iMax;
00044
00045 int m_iStep;
00046 int m_iPageStep;
00047
00048
00049 float m_fPercent;
00050
00051 int m_iPosition;
00052
00053 CWnd::CRect m_SliderPos;
00054 CWnd::CRect m_OldSliderPos;
00055
00056 CWnd::eOrient m_eOrient;
00057
00058 int m_iClickX;
00059 int m_iClickY;
00060 virtual void OnUpdatePos();
00061 public:
00063 virtual void OnKeyUp(int &iKey);
00065 virtual void OnKeyDown(int &iKey);
00066 virtual void OnReshape();
00067
00068 virtual void OnMouseMove(int iX, int iY);
00069 public:
00070 CSliderCtrlTmpl();
00078 void SetSliderLength(float f)
00079 {
00080 if(f<0)f=0;
00081 if(f>1)f=1;
00082 m_fPercent=f;
00083 Adjust();
00084 }
00090
00092 void SetMinMax(int a, int b){if(a<0)a=0;if(b<0)b=0;m_iMin=min(a,b);m_iMax=max(a,b);}
00094 int GetMin(){return m_iMin;}
00096 int GetMax(){return m_iMax;}
00098 int GetStep(){return m_iStep;}
00100 int GetPageStep(){return m_iPageStep;}
00102 void SetOrient(CWnd::eOrient e){m_eOrient=e;}
00104 CWnd::eOrient GetOrient(){return m_eOrient;}
00109 int GetSliderPos(){return m_iPosition;}
00115 void SetSliderPos(int i);
00116 };
00117
00118 template<class Tbase>
00119 inline void CSliderCtrlTmpl<Tbase>::OnReshape()
00120 {
00121 Tbase::OnReshape();
00122 Adjust();
00123 }
00124
00125 template<class Tbase>
00126 inline void CSliderCtrlTmpl<Tbase>::OnUpdatePos()
00127 {
00128 Tbase::OnUpdatePos();
00129 Adjust();
00130 }
00131
00132 template<class Tbase>
00133 inline void CSliderCtrlTmpl<Tbase>::SetSliderPos(int i)
00134 {
00135 if(i<m_iMin)i=m_iMin;
00136 if(i>m_iMax)i=m_iMax;
00137 m_iPosition=i;
00138 Adjust();
00139 }
00140
00141 template<class Tbase>
00142 inline void CSliderCtrlTmpl<Tbase>::OnMouseMove(int iX, int iY)
00143 {
00144 Tbase::OnMouseMove(iX,iY);
00145 if(IsPressed())
00146 {
00147 int iD=0;
00148 if(GetOrient()==HORIZONTAL)
00149 {
00150 iD=iX-m_iClickX;
00151 if(abs(iD)<m_iStep)return;
00152 if(iD>0)
00153 {
00154
00155 if(m_OldSliderPos.GetRight()+iD<m_AbsPos.GetRight())
00156 {
00157 m_SliderPos=m_OldSliderPos;
00158 m_SliderPos.Translate(iD,0);
00159 }
00160 else
00161 {
00162 m_SliderPos=m_OldSliderPos;
00163 m_SliderPos.SetX(m_AbsPos.GetX()+(GetWidth()-m_SliderPos.GetWidth()));
00164 }
00165 }
00166 else
00167 {
00168
00169 if(m_OldSliderPos.GetX()+iD>m_AbsPos.GetX())
00170 {
00171 m_SliderPos=m_OldSliderPos;
00172 m_SliderPos.Translate(iD,0);
00173 }
00174 else
00175 {
00176 m_SliderPos=m_OldSliderPos;
00177 m_SliderPos.SetX(m_AbsPos.GetX());
00178 }
00179 }
00180 }
00181 else
00182 {
00183 iD=iY-m_iClickY;
00184 if(abs(iD)<m_iStep)return;
00185 if(iD>0)
00186 {
00187
00188 if(m_OldSliderPos.GetBottom()+iD<m_AbsPos.GetBottom())
00189 {
00190 m_SliderPos=m_OldSliderPos;
00191 m_SliderPos.Translate(0,iD);
00192 }
00193 else
00194 {
00195 m_SliderPos=m_OldSliderPos;
00196 m_SliderPos.SetY(m_AbsPos.GetY()+(GetHeight()-m_SliderPos.GetHeight()));
00197 }
00198 }
00199 else
00200 {
00201
00202 if(m_OldSliderPos.GetY()+iD>m_AbsPos.GetY())
00203 {
00204 m_SliderPos=m_OldSliderPos;
00205 m_SliderPos.Translate(0,iD);
00206 }
00207 else
00208 {
00209 m_SliderPos=m_OldSliderPos;
00210 m_SliderPos.SetY(m_AbsPos.GetY());
00211 }
00212 }
00213 }
00214 }
00215 }
00216
00217 template<class Tbase>
00218 inline void CSliderCtrlTmpl<Tbase>::OnKeyUp(int &iKey)
00219 {
00220 Tbase::OnKeyUp(iKey);
00221 switch(iKey)
00222 {
00223
00224 case SJ_KEY_MOUSE_LEFT:
00225 case SJ_KEY_SPACE:
00226 case SJ_KEY_ENTER:
00227
00228 if(IsPressed())
00229 {
00230
00231 int iD=0;
00232 if(GetOrient()==HORIZONTAL)
00233 iD=(int)((m_SliderPos.GetX()-GetAbsX())/m_fScale);
00234 else
00235 iD=(int)((m_SliderPos.GetY()-GetAbsY())/m_fScale);
00236 SetSliderPos(iD);
00237 SetReleased();
00238
00239 iKey=SJ_KEY_IGNORE;
00240 }
00241
00242 if(GetParentWnd()!=NULL)
00243 GetParentWnd()->ReleaseFocus();
00244 break;
00245 }
00246 }
00247
00248 template<class Tbase>
00249 inline void CSliderCtrlTmpl<Tbase>::OnKeyDown(int &iKey)
00250 {
00251 Tbase::OnKeyDown(iKey);
00252 switch(iKey)
00253 {
00254
00255 case SJ_KEY_MOUSE_LEFT:
00256 case SJ_KEY_SPACE:
00257 case SJ_KEY_ENTER:
00258 if(IsInFocus() && m_SliderPos.PtInRect(GetMouseX(),GetMouseY()))
00259 {
00260 SetPressed();
00261 m_iClickX=GetMouseX();
00262 m_iClickY=GetMouseY();
00263 m_OldSliderPos=m_SliderPos;
00264 }
00265 else if(!m_SliderPos.PtInRect(GetMouseX(),GetMouseY()))
00266 {
00267
00268
00269
00270 int iD=0;
00271 if(GetOrient()==HORIZONTAL)
00272 {
00273 if(GetMouseX()<m_SliderPos.GetX())
00274 iD=-1;
00275 if(GetMouseX()>m_SliderPos.GetRight())
00276 iD=1;
00277 }
00278 else
00279 {
00280 if(GetMouseY()<m_SliderPos.GetY())
00281 iD=-1;
00282 if(GetMouseY()>m_SliderPos.GetBottom())
00283 iD=1;
00284 }
00285 SetSliderPos(GetSliderPos()+iD*m_iPageStep);
00286
00287 if(GetParentWnd()!=NULL)
00288 GetParentWnd()->ReleaseFocus();
00289 }
00290
00291 iKey=SJ_KEY_IGNORE;
00292 break;
00293 }
00294 }
00295
00296 template<class Tbase>
00297 inline void CSliderCtrlTmpl<Tbase>::Adjust()
00298 {
00299
00300 int iDif=m_iMax-m_iMin;
00301 if(iDif==0)iDif=1;
00302 if(GetOrient()==HORIZONTAL)
00303 {
00304 m_SliderPos.SetSize((int)(GetWidth()*m_fPercent),GetHeight()-2);
00305 m_iPageStep=m_SliderPos.GetWidth()-1;
00306
00307 m_fScale=(float)(GetWidth()-m_SliderPos.GetWidth())/iDif;
00308 m_SliderPos.SetPos((int)(m_iPosition*m_fScale),1);
00309 m_iStep=(int)((GetWidth()-m_SliderPos.GetWidth())/iDif);
00310 }
00311 else
00312 {
00313 m_SliderPos.SetSize(GetWidth()-2,(int)(GetHeight()*m_fPercent));
00314 m_iPageStep=m_SliderPos.GetHeight()-1;
00315 m_fScale=(float)(GetHeight()-m_SliderPos.GetHeight())/iDif;
00316 m_SliderPos.SetPos(1,(int)(m_iPosition*m_fScale));
00317 m_iStep=(int)((GetHeight()-m_SliderPos.GetHeight())/iDif);
00318 }
00319 m_SliderPos.Translate(m_AbsPos.GetX(),m_AbsPos.GetY());
00320 m_OldSliderPos=m_SliderPos;
00321 }
00322
00323 template<class Tbase>
00324 inline CSliderCtrlTmpl<Tbase>::CSliderCtrlTmpl():Tbase()
00325 {
00326 SetSize(SJGUI_USE_STYLE::def_slider_size);
00327 SetAllowedForFocus(true);
00328 m_iPosition=0;
00329 SetMinMax(0,10);
00330 SetOrient(SJGUI_USE_STYLE::def_slider_orientation);
00331 m_iClickX=0;
00332 m_iClickY=0;
00333 SetSliderLength(SJGUI_USE_STYLE::def_slider_length);
00334 }
00335
00336 }
00337
00338 #endif // !defined(SJSLIDER_H_INCLUDED)