Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

slider.h

00001 // $Id: slider.h,v 1.1.1.1 2003/06/24 15:33:13 sjcomp Exp $
00002 // Copyright (c) 2003, Alexander Shyrokov aka SJ
00003 // E-mail: sjcomp[@]users.sourceforge.com
00004 // Web:    http://sjgui.sourceforge.net
00005 //
00006 // This code is under BSD license agreement.
00007 //
00008 // Alexander Shyrokov makes no representations about the suitability of this software 
00009 // for any purpose. It is provided "AS IS" without express or implied warranty.
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     // how many pixels are in one step shift
00045     int         m_iStep; 
00046     int         m_iPageStep;  
00047     // Number of units currently displayed, so the size of 
00048     // scroll could be changed accordingly
00049     float       m_fPercent; 
00050     // where slider is located at the moment
00051     int         m_iPosition; 
00052     // Position of the slider
00053     CWnd::CRect m_SliderPos; 
00054     CWnd::CRect m_OldSliderPos; 
00055     // The kind of slider we have
00056     CWnd::eOrient       m_eOrient; 
00057     // Keep position of mouse when this element was pressed
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     //void SetDisplayed(int iD){if(iD<0)iD=1;m_iDispNum=iD;m_iPageStep=iD;}
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                 //Move right
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                 // Move left (iD is negative so still "+")
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                 //Move down
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                 // Move up (iD is negative so still "+")
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         // Space and enter presses button
00224         case SJ_KEY_MOUSE_LEFT:
00225         case SJ_KEY_SPACE:
00226         case SJ_KEY_ENTER:
00227             // If it's just a click on the slider make a page shift
00228             if(IsPressed())
00229             {
00230                 // If it was dragged set a new position
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                 // Make it unusable for the next window
00239                 iKey=SJ_KEY_IGNORE;
00240             }
00241             // in any case I want to release focus
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         // Space and enter presses button
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                 // iD here is used as a sign description
00268                 // if click was on the left of the slider bar move it left,
00269                 // otherwise move right
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                 // in any case I want to release focus
00287                 if(GetParentWnd()!=NULL)
00288                     GetParentWnd()->ReleaseFocus();
00289             }
00290             // Make it unusable for next windows
00291             iKey=SJ_KEY_IGNORE;
00292             break;
00293     }
00294 }
00295 
00296 template<class Tbase>
00297 inline void CSliderCtrlTmpl<Tbase>::Adjust()
00298 {
00299     // Should be less then one.
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         // calculate ration between pixels and distance to be moved
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 } // namespace sjgui
00337 
00338 #endif // !defined(SJSLIDER_H_INCLUDED)
sjgui logo
Quick Links:

 News
 Description
 Screen Shots
 Projects
 Downloads
 Source Code
 Help/FAQ
 Want to help?
 Credits
 Disclaimer


Documentation:

 Documentation
 Reference
 Lessons


Useful links:

sjcomp logo
sjcomp

opengl logo

nehe logo

SourceForge.net Logo

Last modified:


Started by Alexander Shyrokov. Generated at Wed Apr 28 12:31:04 2004 for the sjgui by doxygen 1.3.1. SourceForge.net Logo