00001 #include "SdlArs.h"
00002
00003 namespace Ars
00004 {
00005
00006 XScrollBar::XScrollBar( void )
00007 : m_iMin( 0 ) , m_iMax( -1 ) , m_iPosition( 1 ) , m_bDragging( false )
00008 , m_pBtnUpLeft( 0 ) , m_pBtnDownRight( 0 )
00009 {
00010 }
00011
00012
00013 XScrollBar::~XScrollBar( void )
00014 {
00015 if( m_pBtnUpLeft ) delete m_pBtnUpLeft; m_pBtnUpLeft = 0;
00016 if( m_pBtnDownRight ) delete m_pBtnDownRight; m_pBtnDownRight = 0;
00017 }
00018
00019 void XScrollBar::Init( icstring &aStr )
00020 {
00021 XBox::Init( aStr );
00022
00023 icstring tmp = Rsrc::FindCrdOf( aStr , "cfg" );
00024
00025 sct = Rsrc::GetStr( tmp );
00026 key = Rsrc::GetStr( tmp );
00027 fname = Rsrc::GetStr( tmp );
00028
00029 if( Width() > Height() )
00030 {
00031 m_ScrollBarType = HORIZONTAL;
00032 m_pBtnUpLeft = new XButtonImage();
00033 m_pBtnUpLeft->SetWindowRect( XRect( TopLeft(), XPoint( x + Height(), Bottom() ) ) );
00034 m_pBtnDownRight = new XButtonImage();
00035 m_pBtnDownRight->SetWindowRect( XRect( XPoint( Right() - Height() , y ), BottomRight() ) );
00036 clientRect.x += m_pBtnUpLeft->Width() + 1;
00037 clientRect.w -= 2 * m_pBtnUpLeft->Width() + 1;
00038 }
00039 else
00040 {
00041 m_ScrollBarType = VERTICAL;
00042 m_pBtnUpLeft = new XButtonImage();
00043 m_pBtnUpLeft->SetWindowRect( XRect( TopLeft(), XPoint( Right(), y + Width() ) ) );
00044 m_pBtnDownRight = new XButtonImage();
00045 m_pBtnDownRight->SetWindowRect( XRect( XPoint( x , Bottom() - Width() ), BottomRight() ) );
00046 clientRect.y += m_pBtnUpLeft->Height() + 1;
00047 clientRect.h -= 2 * m_pBtnUpLeft->Height() + 1;
00048 }
00049
00050 if( m_iMax == -1 )
00051 {
00052 tmp = Rsrc::FindCrdOf( aStr , "max" );
00053 m_iMax = Rsrc::GetInt( tmp , 100 );
00054 }
00055
00056 m_pBtnUpLeft->SetBgColor( GetBgColor() );
00057 m_pBtnUpLeft->SetFgColor( GetFgColor().FadeColor( 32 ) );
00058 m_pBtnUpLeft->SetState( S_RAISED );
00059 m_pBtnUpLeft->SetFlags( F_NONE );
00060 m_pBtnUpLeft->SetFont( GetFont() );
00061 tmp = Rsrc::FindCrdOf( aStr , "ULButton" );
00062 m_pBtnUpLeft->Init( tmp );
00063 if( m_pBtnUpLeft->GetWindowText().empty() )
00064 if( m_ScrollBarType == HORIZONTAL )
00065 m_pBtnUpLeft->SetWndText( "<-" );
00066 else
00067 m_pBtnUpLeft->SetWndText( "/\\" );
00068
00069 m_pBtnDownRight->SetBgColor( GetBgColor() );
00070 m_pBtnDownRight->SetFgColor( GetFgColor().FadeColor( 32 ) );
00071 m_pBtnDownRight->SetFlags( F_NONE );
00072 m_pBtnDownRight->SetState( S_RAISED );
00073 m_pBtnDownRight->SetFont( GetFont() );
00074 tmp = Rsrc::FindCrdOf( aStr , "BRButton" );
00075 m_pBtnDownRight->Init( tmp );
00076 if( m_pBtnDownRight->GetWindowText().empty() )
00077 if( m_ScrollBarType == HORIZONTAL )
00078 m_pBtnDownRight->SetWndText( "->" );
00079 else
00080 m_pBtnDownRight->SetWndText( "\\/" );
00081 SetWindowRect( *this );
00082
00083 RGBColor c = GetFgColor();
00084 SetFgColor( GetBgColor() );
00085 SetBgColor( c );
00086 }
00087
00088 const bool XScrollBar::SetupWindow( const Message &msg )
00089 {
00090 XBox::SetupWindow( msg );
00091
00092 if( !sct.empty() && !key.empty() && fname.empty() )
00093 fname = Cfg::GetCfg().GetCfgFile( sct , key );
00094 else if( fname.empty() && !sct.empty() )
00095 fname = sct;
00096
00097 if( !fname.empty() )
00098 {
00099 Cfg::Handler &h = Cfg::GetCfg( fname );
00100 if( m_ScrollBarType = HORIZONTAL )
00101 {
00102 m_pBtnUpLeft->ChangeImage( h.GetCfgFile( "scrollbar" , "left" ) );
00103 m_pBtnDownRight->ChangeImage( h.GetCfgFile( "scrollbar" , "right" ) );
00104 }
00105 else
00106 {
00107 m_pBtnUpLeft->ChangeImage( h.GetCfgFile( "scrollbar" , "up" ) );
00108 m_pBtnDownRight->ChangeImage( h.GetCfgFile( "scrollbar" , "down" ) );
00109 }
00110 }
00111 m_ThumbRect = GetClientRect();
00112
00113 m_pBtnUpLeft->SetParent( this );
00114 m_pBtnUpLeft->SetupWindow( msg );
00115 m_pBtnDownRight->SetParent( this );
00116 m_pBtnDownRight->SetupWindow( msg );
00117
00118 SetPosition( 0 );
00119
00120 MessageServer::Instance().RegisterMessageClient(this, Message::MOUSE_BUTTONDOWN);
00121 MessageServer::Instance().RegisterMessageClient(this, Message::MOUSE_BUTTONUP);
00122 MessageServer::Instance().RegisterMessageClient(this, Message::MOUSE_MOVE);
00123 MessageServer::Instance().RegisterMessageClient(this, Message::CTRL_LCLICK);
00124
00125 return true;
00126 }
00127
00128 void XScrollBar::SetPosition( const int iPosition )
00129 {
00130 m_iPosition = min( m_iMax , max( m_iMin , iPosition ) );
00131 XRect clt = GetClientRect();
00132
00133 switch( m_ScrollBarType )
00134 {
00135 case VERTICAL:
00136 {
00137 int yPos = static_cast<int>(clt.y + (clt.h - 16) * (static_cast<double>(m_iPosition - m_iMin) / (m_iMax - m_iMin)) - 1);
00138 m_ThumbRect.y = yPos;
00139 m_ThumbRect.h= 16;
00140 break;
00141 }
00142 case HORIZONTAL:
00143 {
00144 int xPos = static_cast<int>(clt.x + (clt.w - 16) * (static_cast<double>(m_iPosition - m_iMin) / (m_iMax - m_iMin)) - 1);
00145 m_ThumbRect.x = xPos;
00146 m_ThumbRect.w = 16;
00147 break;
00148 }
00149 default:
00150
00151 break;
00152 }
00153 UpdateWindow();
00154 }
00155
00156
00157 void XScrollBar::Draw( void )
00158 {
00159 XBox::Draw();
00160
00161 XRect SubRect( m_ThumbRect );
00162
00163 if( HitTest( SubRect.TopLeft() ) == RELPOS_INSIDE )
00164 {
00165 SubRect.Grow( -1 );
00166
00167 DrawRaised( SubRect , GetFgColor() );
00168
00169 SubRect.Grow( -1 );
00170 DrawRect( SubRect , true , GetFgColor() , GetFgColor() );
00171 }
00172 }
00173
00174
00175 void XScrollBar::SetWindowRect(const XRect& WindowRect)
00176 {
00177 XBox::SetWindowRect( WindowRect );
00178
00179
00180 SetPosition( m_iPosition );
00181
00182 switch( m_ScrollBarType )
00183 {
00184 case VERTICAL:
00185 {
00186 m_ThumbRect.x = x;
00187 m_ThumbRect.SetRight( Right() );
00188 m_pBtnUpLeft->SetWindowRect( XRect( TopLeft(), XPoint( Right(), y + Width() ) ) );
00189 m_pBtnDownRight->SetWindowRect( XRect( XPoint( x , Bottom() - Width() ), BottomRight() ) );
00190 clientRect.y = m_pBtnUpLeft->Height() + 1;
00191 clientRect.SetBottom( Height() - m_pBtnUpLeft->Height() - 1 );
00192 break;
00193 }
00194 case HORIZONTAL:
00195 {
00196 m_ThumbRect.y = y;
00197 m_ThumbRect.SetBottom( Bottom() );
00198 m_pBtnUpLeft->SetWindowRect( XRect( TopLeft() , XPoint( x + Height(), Bottom() ) ) );
00199 m_pBtnDownRight->SetWindowRect( XRect( XPoint( Right() - Height(), y ), BottomRight() ) );
00200 clientRect.x = m_pBtnUpLeft->Width() + 1;
00201 clientRect.SetRight( Width() - m_pBtnUpLeft->Width() - 1 );
00202 break;
00203 }
00204 default:
00205
00206 break;
00207 }
00208 }
00209
00210 const bool XScrollBar::evMouseButtonDown( const MouseMessage &msg )
00211 {
00212 bool ans = XBox::evMouseButtonDown( msg );
00213
00214 if( m_ThumbRect.HitTest( msg.Point ) == RELPOS_INSIDE && msg.Button == MouseMessage::LEFT )
00215 {
00216 m_bDragging = true;
00217 ans = true;
00218 SetDirty();
00219 }
00220
00221 return ans;
00222 }
00223
00224 const bool XScrollBar::evMouseButtonUp( const MouseMessage &msg )
00225 {
00226 bool ans = XBox::evMouseButtonUp( msg );
00227
00228 if( m_bDragging && msg.Button == MouseMessage::LEFT )
00229 {
00230 m_bDragging = false;
00231 ArsSendMessage( CtrlMessage( Message::CTRL_VALUECHANGE , GetParent() , this, m_iPosition ) );
00232 ans = true;
00233 SetDirty();
00234 }
00235 return ans;
00236 }
00237
00238 const bool XScrollBar::evMouseMove( const MouseMessage &msg )
00239 {
00240 bool ans = XBox::evMouseMove( msg );
00241 if( m_bDragging )
00242 {
00243 int iOldPosition = m_iPosition;
00244 switch (m_ScrollBarType)
00245 {
00246 case VERTICAL:
00247 SetPosition( (msg.Point.y - GetClientRect().y) * (m_iMax - m_iMin) / GetClientRect().Height() + m_iMin );
00248 break;
00249 case HORIZONTAL:
00250 SetPosition( (msg.Point.x - GetClientRect().x) * (m_iMax - m_iMin) / GetClientRect().Width() + m_iMin );
00251 break;
00252 default:
00253
00254 break;
00255 }
00256 if (iOldPosition != m_iPosition)
00257 ArsSendMessage( CtrlMessage( Message::CTRL_VALUECHANGING, GetParent() , this , m_iPosition ) );
00258 SetDirty();
00259 }
00260
00261 return ans;
00262 }
00263
00264 const bool XScrollBar::evMouseLClick( const CtrlMessage &msg )
00265 {
00266 bool ans = XBox::evMouseLClick( msg );
00267 if( msg.Destination() == this )
00268 {
00269 if( msg.source == m_pBtnUpLeft )
00270 {
00271 SetPosition( m_iPosition - 1 );
00272 ans = true;
00273 }
00274 else if( msg.source == m_pBtnDownRight )
00275 {
00276 SetPosition( m_iPosition + 1 );
00277 ans = true;
00278 }
00279 if( ans )
00280 ArsSendMessage( CtrlMessage( Message::CTRL_VALUECHANGE, GetParent() , this, m_iPosition ) );
00281 SetDirty();
00282 }
00283
00284 return ans;
00285 }
00286
00287 }
00288
00289