00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "pgspinnerbox.h"
00030 #include "pgmaskedit.h"
00031 #include "pglineedit.h"
00032 #include "pgbutton.h"
00033
00038 PG_SpinnerBox::PG_SpinnerBox(PG_Widget *parent, const PG_Rect& r, const std::string& style) : PG_ThemeWidget( parent, r, style ) {
00039 PG_Rect box_rect = r;
00040 PG_Rect up_rect, down_rect;
00041 box_rect.my_width -= (my_height/2);
00042 if( box_rect.my_width < my_height ) {
00043 box_rect.my_width = my_height;
00044 }
00045 SizeWidget( box_rect.my_width+(my_height/2), my_height );
00046
00047 m_pParent = parent;
00048
00049 box_rect.my_xpos = box_rect.my_ypos = 0;
00050 up_rect.SetRect( box_rect.my_width, 0, (my_height/2), (my_height/2));
00051 down_rect.SetRect( box_rect.my_width, my_height - (my_height/2), (my_height/2), (my_height/2));
00052
00053 m_pEditBox = new PG_MaskEdit(this, box_rect, style);
00054 m_pEditBox->sigEditEnd.connect(slot(*this, &PG_SpinnerBox::handleEditEnd));
00055
00056 m_pButtonUp = new PG_Button(this, up_rect);
00057 m_pButtonUp->SetID(IDSPINNERBOX_UP);
00058 m_pButtonUp->sigClick.connect(slot(*this, &PG_SpinnerBox::handleButtonClick));
00059 m_pButtonUp->LoadThemeStyle(style, "ButtonUp");
00060
00061 m_pButtonDown = new PG_Button( this, down_rect);
00062 m_pButtonDown->SetID(IDSPINNERBOX_DOWN);
00063 m_pButtonDown->sigClick.connect(slot(*this, &PG_SpinnerBox::handleButtonClick));
00064 m_pButtonDown->LoadThemeStyle(style, "ButtonDown");
00065
00066 m_iMinValue = 0;
00067 m_iMaxValue = 99;
00068 m_iValue = 0;
00069 SetMask( "##" );
00070 m_pEditBox->SetText( "0" );
00071 m_pEditBox->SetValidKeys("-0123456789");
00072 }
00073
00074 bool PG_SpinnerBox::handleButtonClick(PG_Button* button) {
00075
00076 switch(button->GetID()) {
00077
00078 case IDSPINNERBOX_UP:
00079 if( m_iValue < m_iMaxValue ) {
00080 m_iValue++;
00081 SetTextValue();
00082 return true;
00083 }
00084 return false;
00085
00086 case IDSPINNERBOX_DOWN:
00087 if( m_iValue > m_iMinValue ) {
00088 m_iValue--;
00089 SetTextValue();
00090 return true;
00091 }
00092 return false;
00093 }
00094
00095 return false;
00096 }
00097
00098 void PG_SpinnerBox::SetTextValue() {
00099 m_pEditBox->SetTextFormat("%u", m_iValue );
00100 m_pEditBox->Update();
00101 sigChange(this, m_iValue);
00102 }
00103
00104 void PG_SpinnerBox::AdjustSize() {
00105 Uint16 x, y;
00106
00107 PG_FontEngine::GetTextSize(m_sMask, GetFont(), &x, &y);
00108 x += 3;
00109 y = m_pEditBox->my_height;
00110
00111 Hide();
00112
00113 SizeWidget( x+ (y >> 1), y );
00114 m_pEditBox->SizeWidget( x, y );
00115 m_pButtonUp->MoveWidget( x, 0 );
00116 m_pButtonDown->MoveWidget( x, y - (y >> 1) );
00117
00118 Show();
00119 }
00120
00121 bool PG_SpinnerBox::handleEditEnd(PG_LineEdit* edit) {
00122 const std::string& text = m_pEditBox->GetText();
00123 m_iValue = (!text.empty()) ? atoi(text.c_str()) : 0;
00124
00125
00126 if (m_iValue>m_iMaxValue) {
00127 m_iValue=m_iMaxValue;
00128 }
00129
00130 if (m_iValue<m_iMinValue) {
00131 m_iValue=m_iMinValue;
00132 }
00133
00134 SetTextValue();
00135 return true;
00136 }
00137
00138 void PG_SpinnerBox::SetMask(const std::string& value) {
00139 m_sMask = value;
00140 m_pEditBox->SetMask( m_sMask );
00141 AdjustSize();
00142 }