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

label.h

00001 // $Id: label.h,v 1.1.1.1 2003/06/24 15:33:12 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(SJLABEL_H_INCLUDED)
00012 #define SJLABEL_H_INCLUDED
00013 
00014 #include <string>
00015 
00016 #include "font.h"
00017 
00018 namespace sjgui{
00019 
00020     namespace SJGUI_USE_STYLE
00021     {
00022         extern GLfloat def_label_color[4]; 
00023         extern CWnd::eAligns def_label_hor_aling; 
00024         extern CWnd::eAligns def_label_ver_aling; 
00025     }
00033 template<class Tbase>
00034 class CLabelCtrlTmpl: public Tbase, public CFont
00035 {
00036     int             m_iDispChars; // For correct clipping of the text.
00037     // Point where font will be drawn, within m_TextPos
00038     CWnd::CPoint    m_TxShift;
00039     // Position of the real text, due to alignment
00040     CWnd::CPoint    m_AbsTxPt;
00041 protected:
00044     void Adjust();
00045     std::string     m_strText; 
00046     float           m_Color[4]; 
00047     CWnd::eAligns   m_eVAlign; 
00048     CWnd::eAligns   m_eHAlign; 
00049     CWnd::CRect     m_TextPos; 
00050     virtual void OnUpdatePos(); 
00051 public:
00052     // This function will be called to display the contest of the window
00053     virtual void OnDraw(); 
00054     virtual void OnReshape(); 
00055 public:
00056     CLabelCtrlTmpl(); 
00058     void SetColor(GLfloat* Color){memcpy(m_Color,Color,sizeof(float)*4);}
00060     void SetColor(GLfloat R, GLfloat G, GLfloat B, GLfloat A=1.0f){m_Color[0]=R;m_Color[1]=G;m_Color[2]=B;m_Color[3]=A;}
00062     float* GetColor(){return m_Color;}
00064     void SetText(const char* str){m_strText=str;Adjust();}
00066     const char* GetText(){return m_strText.data();}
00068     void SetHAlign(CWnd::eAligns e){m_eHAlign=e;Adjust();}
00070     CWnd::eAligns GetHAlign(){return m_eHAlign;}
00072     void SetVAlign(CWnd::eAligns e){m_eVAlign=e;Adjust();}
00074     CWnd::eAligns GetVAlign(){return m_eVAlign;}
00076     void SetAlign(CWnd::eAligns eH=CWnd::ALIGN_CENTER,CWnd::eAligns eV=CWnd::ALIGN_CENTER)
00077                 {m_eHAlign=eH;m_eVAlign=eV;Adjust();}
00079     unsigned int GetTextLen(){return m_strText.length();}
00080 };
00081 
00082 template<class Tbase>
00083 inline void CLabelCtrlTmpl<Tbase>::OnUpdatePos()
00084 {
00085     Tbase::OnUpdatePos();
00086     m_TextPos=m_AbsPos;
00087     m_TextPos.Deflate(GetBorderSize());
00088     // Width is presereved
00089     m_AbsTxPt=m_TextPos.GetPos()+m_TxShift;
00090 }
00091 
00092 template<class Tbase>
00093 inline void CLabelCtrlTmpl<Tbase>::OnReshape()
00094 {
00095     Adjust();
00096     Tbase::OnReshape();
00097 }
00098 
00099 template<class Tbase>
00100 inline void CLabelCtrlTmpl<Tbase>::Adjust()
00101 {
00102     int iX=0;
00103     int iY=0;
00104     switch(m_eHAlign)
00105     {
00106         case ALIGN_CENTER:
00107             iX= (GetFontSize()*(int)m_strText.length()<m_TextPos.GetWidth()?
00108                 (m_TextPos.GetWidth()-GetFontSize()*m_strText.length())/2:0);
00109             break;
00110         case ALIGN_LEFT:
00111             iX=0;
00112             break;
00113         case ALIGN_RIGHT:
00114             iX=m_TextPos.GetWidth()-(int)(GetFontSize()*m_strText.length());
00115             break;
00116     }
00117     switch(m_eVAlign)
00118     {
00119         case ALIGN_CENTER:
00120             iY=(int)((m_TextPos.GetHeight()-GetFontSize())/2);
00121             break;
00122         case ALIGN_TOP:
00123             iY=0;
00124             break;
00125         case ALIGN_BOTTOM:
00126             iY=m_TextPos.GetHeight()-GetFontSize();
00127             break;
00128     }
00129     if(GetFontSize()*(int)m_strText.length()<=m_TextPos.GetWidth())
00130         m_iDispChars=(int)m_strText.length();
00131     else
00132         m_iDispChars=(int)(m_TextPos.GetWidth()/GetFontSize());
00133     m_TxShift.SetPos(iX,iY);
00134     // Width is presereved
00135     m_AbsTxPt=m_TextPos.GetPos()+m_TxShift;
00136 }
00137 
00138 template<class Tbase>
00139 inline CLabelCtrlTmpl<Tbase>::CLabelCtrlTmpl():Tbase(),CFont()
00140 {
00141     SetAllowedForFocus(false);
00142     SetColor(SJGUI_USE_STYLE::def_label_color);
00143     m_strText="Label";
00144     m_eHAlign=SJGUI_USE_STYLE::def_label_hor_aling;
00145     m_eVAlign=SJGUI_USE_STYLE::def_label_ver_aling;
00146     SetHeight(GetFontSize()+GetBorderSize()*2);
00147 }
00148 
00149 template<class Tbase>
00150 inline void CLabelCtrlTmpl<Tbase>::OnDraw()
00151 {
00152     Tbase::OnDraw();
00153     FontDrawBegin();
00154     FontDraw(m_AbsTxPt.GetX(),m_AbsTxPt.GetY(),m_strText.data(),m_iDispChars,m_Color);
00155     FontDrawEnd();
00156 }
00157 
00158 } // namespace sjgui
00159 
00160 #endif // !defined(SJLABEL_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