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

button.h

00001 // $Id: button.h,v 1.1.1.1 2003/06/24 15:33:07 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(SJBUTTON_H_INCLUDED)
00012 #define SJBUTTON_H_INCLUDED
00013 
00014 namespace sjgui{
00015     namespace SJGUI_USE_STYLE
00016     {
00017         extern CWnd::CSize def_button_size; 
00018     }
00019 
00057 template<class Tlabel,class Tbase>
00058 class CButtonCtrlTmpl : public Tbase
00059 {
00060 protected:
00061     bool        m_yPushed; 
00062 
00066     Tlabel      m_Label;
00067     int         m_iNumStates; 
00068     int         m_iState; 
00069     bool        m_yChecked; 
00070 public:
00071     virtual void OnKeyUp(int &iKey); 
00072     virtual void OnKeyDown(int &iKey); 
00073 public:
00074     CButtonCtrlTmpl();
00076     void SetNumStates(int i){assert(i>=1);m_iNumStates=i;}
00078     int GetNumStates(){return m_iNumStates;}
00080     int GetState(){return m_iState;}
00082     bool IsChecked(){return m_yChecked;}
00084     void GoToNextState(){m_iState=(m_iState+1)%m_iNumStates;}
00086     void SetChecked(bool b=true){m_yChecked=b;}
00088     void SetTextColor(GLfloat R,GLfloat G,GLfloat B){m_Label.SetColor(R,G,B);}
00090     void SetTextColor(GLfloat* pC){m_Label.SetColor(pC);}
00092     GLfloat* GetTextColor(){return m_Label.GetColor();}
00094     void SetLabel(const char* pc){m_Label.SetText(pc);}
00096     int GetTextLen(){return m_Label.GetTextLen();}
00098     int GetFontSize(){return m_Label.GetFontSize();}
00099     // Clears pressed and pushed variables
00100     virtual void OnReset();  
00101 
00105     bool IsPushed(){return m_yPushed;}
00107     void PushIt(bool b=true){m_yPushed=b;}
00109     Tlabel& GetLabel(){return m_Label;}
00110 };
00111 
00112 template<class Tlabel,class Tbase>
00113 inline void CButtonCtrlTmpl<Tlabel,Tbase>::OnReset()
00114 {   
00115     m_yPushed=false;
00116     Tbase::OnReset();
00117 }
00118 
00119 template<class Tlabel,class Tbase>
00120 inline CButtonCtrlTmpl<Tlabel,Tbase>::CButtonCtrlTmpl():Tbase()
00121 {
00122     RegisterChild(&m_Label);
00123     m_yPushed=false;
00124     m_iState=0;
00125     m_iNumStates=2;
00126     m_yChecked=false;
00127     SetAllowedForFocus(true);
00128     SetSize(SJGUI_USE_STYLE::def_button_size);
00129 }
00130 
00131 template<class Tlabel,class Tbase>
00132 inline void CButtonCtrlTmpl<Tlabel,Tbase>::OnKeyUp(int &iKey)
00133 {
00134     Tbase::OnKeyUp(iKey);
00135     switch(iKey)
00136     {
00137         // Space and enter presses button
00138         case SJ_KEY_MOUSE_LEFT:
00139         case SJ_KEY_SPACE:
00140         case SJ_KEY_ENTER:
00141             if(IsPressed())
00142             {
00143                 // for mouse I want to check if it is above the window
00144                 // but for keyboard input I do not care
00145                 if(iKey!=SJ_KEY_MOUSE_LEFT || IsOnWindow(GetMouseX(),GetMouseY()))
00146                 {
00147                     PushIt();
00148                     SetChecked(!IsChecked());
00149                     GoToNextState();
00150                 }
00151             // Make it unusable for next windows
00152                 iKey=SJ_KEY_IGNORE;
00153                 SetReleased();
00154             }
00155             break;
00156     }
00157 }
00158 
00159 template<class Tlabel,class Tbase>
00160 inline void CButtonCtrlTmpl<Tlabel,Tbase>::OnKeyDown(int &iKey)
00161 {
00162     Tbase::OnKeyDown(iKey);
00163     switch(iKey)
00164     {
00165         // Space and enter presses button
00166         case SJ_KEY_MOUSE_LEFT:
00167         case SJ_KEY_SPACE:
00168         case SJ_KEY_ENTER:
00169             if(IsInFocus())
00170             {
00171                 SetPressed();
00172                 // Make it unusable for next windows
00173                 iKey=SJ_KEY_IGNORE;
00174             }
00175             break;
00176     }
00177 }
00178 
00179 } // namespace sjgui
00180 
00181 #endif // !defined(SJBUTTON_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