Back to index

extremetuxracer  0.5beta
widget.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00003  * 
00004  * Copyright (C) 1999-2001 Jasmin F. Patry
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00021 #include "widget.h"
00022 
00023 #include "debug.h"
00024 #include "ui_mgr.h"
00025 
00026 #include <iostream>
00027 
00028 namespace pp {
00029        
00030 Widget::Widget()
00031 {
00032        UIMgr.add(this);     
00033        m_hasFocus=false;
00034 }
00035 
00036 Widget::~Widget()
00037 {
00038        UIMgr.remove(this);
00039 }
00040 
00041 void
00042 Widget::setSensitive(bool sensitive)
00043 {
00044        if ( m_sensitive != sensitive ) {
00045               UIMgr.setDirty();
00046     }
00047 
00048     m_sensitive = sensitive;
00049 
00050     if ( !m_sensitive ) {
00051               m_clicked = false;
00052               m_focused = false;
00053     }
00054 }
00055 
00056 void
00057 Widget::setVisible(bool visible)
00058 {
00059     /* This check is necessary to prevent infinite mutual recursion */
00060     if ( m_active != visible ) {
00061               setActive( visible );
00062     }
00063 
00064     if ( !m_visible && visible ) {
00065               UIMgr.setDirty();
00066     } else if ( m_visible && !visible ) {
00067               UIMgr.setDirty();
00068     }
00069     m_visible = visible;
00070 }
00071 
00072 void 
00073 Widget::setActive( bool active )
00074 {
00075     if ( !m_active && active ) {
00076               UIMgr.setDirty();
00077     } else if ( m_active && !active ) {
00078               UIMgr.setDirty();
00079     }
00080 
00081     m_active = active;
00082 
00083     if ( !active ) {
00084               setVisible( false );
00085     }
00086 }
00087 
00088 void
00089 Widget::performClickAction()
00090 {
00091     signalClicked.Emit();
00092 }
00093        
00094        
00095 bool
00096 Widget::isInBox(const int x, const int y) const
00097 {
00098     return (bool) (
00099        ( x >= m_position.x ) &&
00100        ( x <= m_position.x + m_size.x ) &&
00101        ( y >= m_position.y ) &&
00102        ( y <= m_position.y + m_size.y ) );
00103 }
00104        
00105 void
00106 Widget::mouseMotion(int x, int y)
00107 {
00108     if ( !m_sensitive ) {
00109               return;
00110     }
00111 
00112     if ( isInBox(x, y) ) {
00113               if ( m_focused == false ) {
00114                      print_debug( DEBUG_UI, "Mouse entered button" );
00115                   m_focused = true;
00116                   UIMgr.setDirty();
00117               }
00118     } else {
00119               if ( m_focused == true ) {
00120                      print_debug( DEBUG_UI, "Mouse left button" );
00121                   m_focused = false;
00122                   UIMgr.setDirty();
00123               }
00124     }
00125 }
00126 void
00127 Widget::mouseDown(int button,int x, int y )
00128 {
00129     if ( button != SDL_BUTTON_LEFT ) {
00130               return;
00131     }
00132 
00133     if ( !isInBox(x, y) ) {
00134               return;
00135     }
00136 
00137     if ( !m_sensitive ) {
00138               return;
00139     }
00140 
00141     if ( m_clicked == false ) {
00142               print_debug( DEBUG_UI, "Button is down" );
00143               m_clicked = true;
00144               UIMgr.setDirty();
00145     }
00146 }
00147 
00148 void
00149 Widget::mouseUp(int button, int x, int y )
00150 {
00151        if ( button != SDL_BUTTON_LEFT ) {
00152               return;
00153        }
00154 
00155     if ( !m_sensitive ) {
00156               return;
00157     }
00158 
00159     if ( !isInBox(x, y) ) {
00160               if ( m_clicked ) {
00161                   print_debug( DEBUG_UI, "Button is up (not clicked)" );
00162                   m_clicked = false;
00163                   UIMgr.setDirty();
00164               }
00165               return;
00166     } 
00167 
00168     if ( m_clicked ) {
00169               m_clicked = false;
00170               print_debug( DEBUG_UI, "Button was clicked" );
00171               performClickAction();
00172               UIMgr.setDirty();
00173     }
00174 }
00175 
00176 void
00177 Widget::simulateMouseClick()
00178 {
00179        performClickAction();
00180 }
00181 
00182 void
00183 Widget::removeFocus()
00184 {
00185        m_hasFocus=false;
00186 }
00187 
00188 void
00189 Widget::setFocus(bool focus)
00190 {
00191        if(focus!=m_hasFocus){
00192               UIMgr.grabFocus(this,focus);
00193               m_hasFocus=focus;
00194        }
00195 }
00196 
00197 
00198 } //namespace pp