Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUIScrollbar.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIScrollbar.cpp
00003         created:        13/4/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implementation of Scrollbar widget base class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "elements/CEGUIScrollbar.h"
00027 #include "elements/CEGUIThumb.h"
00028 
00029 
00030 // Start of CEGUI namespace section
00031 namespace CEGUI
00032 {
00033 const String Scrollbar::EventNamespace("Scrollbar");
00034 
00035 /*************************************************************************
00036         Definition of Properties for this class
00037 *************************************************************************/
00038 ScrollbarProperties::DocumentSize       Scrollbar::d_documentSizeProperty;
00039 ScrollbarProperties::PageSize           Scrollbar::d_pageSizeProperty;
00040 ScrollbarProperties::StepSize           Scrollbar::d_stepSizeProperty;
00041 ScrollbarProperties::OverlapSize        Scrollbar::d_overlapSizeProperty;
00042 ScrollbarProperties::ScrollPosition     Scrollbar::d_scrollPositionProperty;
00043 
00044 
00045 /*************************************************************************
00046         Event name constants
00047 *************************************************************************/
00048 const String Scrollbar::EventScrollPositionChanged( (utf8*)"ScrollPosChanged" );
00049 const String Scrollbar::EventThumbTrackStarted( (utf8*)"ThumbTrackStarted" );
00050 const String Scrollbar::EventThumbTrackEnded( (utf8*)"ThumbTrackEnded" );
00051 const String Scrollbar::EventScrollConfigChanged( (utf8*)"ScrollConfigChanged" );
00052 
00053 
00054 /*************************************************************************
00055         Constructor for Scrollbar objects
00056 *************************************************************************/
00057 Scrollbar::Scrollbar(const String& type, const String& name) :
00058         Window(type, name),
00059         d_documentSize(1.0f),
00060         d_pageSize(0.0f),
00061         d_stepSize(1.0f),
00062         d_overlapSize(0.0f),
00063         d_position(0.0f)
00064 {
00065         addScrollbarEvents();
00066         addScrollbarProperties();
00067 }
00068 
00069 
00070 /*************************************************************************
00071         Destructor for Scrollbar objects
00072 *************************************************************************/
00073 Scrollbar::~Scrollbar(void)
00074 {
00075 }
00076 
00077 
00078 /*************************************************************************
00079         Initialises the Scrollbar object ready for use.
00080 *************************************************************************/
00081 void Scrollbar::initialise(void)
00082 {
00083         // Set up thumb
00084         d_thumb = createThumb();
00085         addChildWindow(d_thumb);
00086         d_thumb->subscribeEvent(Thumb::EventThumbPositionChanged, Event::Subscriber(&CEGUI::Scrollbar::handleThumbMoved, this));
00087         d_thumb->subscribeEvent(Thumb::EventThumbTrackStarted, Event::Subscriber(&CEGUI::Scrollbar::handleThumbTrackStarted, this));
00088         d_thumb->subscribeEvent(Thumb::EventThumbTrackEnded, Event::Subscriber(&CEGUI::Scrollbar::handleThumbTrackEnded, this));
00089 
00090         // set up Increase button
00091         d_increase = createIncreaseButton();
00092         addChildWindow(d_increase);
00093         d_increase->subscribeEvent(PushButton::EventMouseButtonDown, Event::Subscriber(&CEGUI::Scrollbar::handleIncreaseClicked, this));
00094 
00095         // set up Decrease button
00096         d_decrease = createDecreaseButton();
00097         addChildWindow(d_decrease);
00098         d_decrease->subscribeEvent(PushButton::EventMouseButtonDown, Event::Subscriber(&CEGUI::Scrollbar::handleDecreaseClicked, this));
00099 
00100         // do initial layout
00101         layoutComponentWidgets();
00102 }
00103 
00104 
00105 /*************************************************************************
00106         Set the size of the document or data.
00107 *************************************************************************/
00108 void Scrollbar::setDocumentSize(float document_size)
00109 {
00110         if (d_documentSize != document_size)
00111         {
00112                 d_documentSize = document_size;
00113                 updateThumb();
00114 
00115                 WindowEventArgs args(this);
00116                 onScrollConfigChanged(args);
00117         }
00118 
00119 }
00120 
00121 
00122 /*************************************************************************
00123         Set the page size for this scroll bar.
00124 *************************************************************************/
00125 void Scrollbar::setPageSize(float page_size)
00126 {
00127         if (d_pageSize != page_size)
00128         {
00129                 d_pageSize = page_size;
00130                 updateThumb();
00131 
00132                 WindowEventArgs args(this);
00133                 onScrollConfigChanged(args);
00134         }
00135 
00136 }
00137 
00138 
00139 /*************************************************************************
00140         Set the step size for this scroll bar.
00141 *************************************************************************/
00142 void Scrollbar::setStepSize(float step_size)
00143 {
00144         if (d_stepSize != step_size)
00145         {
00146                 d_stepSize = step_size;
00147 
00148                 WindowEventArgs args(this);
00149                 onScrollConfigChanged(args);
00150         }
00151 
00152 }
00153 
00154 
00155 /*************************************************************************
00156         Set the overlap size for this scroll bar.
00157 *************************************************************************/
00158 void Scrollbar::setOverlapSize(float overlap_size)
00159 {
00160         if (d_overlapSize != overlap_size)
00161         {
00162                 d_overlapSize = overlap_size;
00163 
00164                 WindowEventArgs args(this);
00165                 onScrollConfigChanged(args);
00166         }
00167 
00168 }
00169 
00170 
00171 /*************************************************************************
00172         Set the current position of scroll bar within the document.
00173 *************************************************************************/
00174 void Scrollbar::setScrollPosition(float position)
00175 {
00176         float old_pos = d_position;
00177 
00178         // max position is (docSize - pageSize), but must be at least 0 (in case doc size is very small)
00179         float max_pos = ceguimax((d_documentSize - d_pageSize), 0.0f);
00180 
00181         // limit position to valid range:  0 <= position <= max_pos
00182         d_position = (position >= 0) ? ((position <= max_pos) ? position : max_pos) : 0.0f;
00183 
00184         updateThumb();
00185 
00186         // notification if required
00187         if (d_position != old_pos)
00188         {
00189                 WindowEventArgs args(this);
00190                 onScrollPositionChanged(args);
00191         }
00192 
00193 }
00194 
00195 
00196 /*************************************************************************
00197         Add scroll bar specific events
00198 *************************************************************************/
00199 void Scrollbar::addScrollbarEvents(void)
00200 {
00201         addEvent(EventScrollPositionChanged);
00202         addEvent(EventThumbTrackStarted);
00203         addEvent(EventThumbTrackEnded);
00204         addEvent(EventScrollConfigChanged);
00205 }
00206 
00207 
00208 /*************************************************************************
00209         Handler triggered when the scroll position changes
00210 *************************************************************************/
00211 void Scrollbar::onScrollPositionChanged(WindowEventArgs& e)
00212 {
00213         fireEvent(EventScrollPositionChanged, e, EventNamespace);
00214 }
00215 
00216 
00217 /*************************************************************************
00218         Handler triggered when the user begins to drag the scroll bar thumb.    
00219 *************************************************************************/
00220 void Scrollbar::onThumbTrackStarted(WindowEventArgs& e)
00221 {
00222         fireEvent(EventThumbTrackStarted, e, EventNamespace);
00223 }
00224 
00225 
00226 /*************************************************************************
00227         Handler triggered when the scroll bar thumb is released
00228 *************************************************************************/
00229 void Scrollbar::onThumbTrackEnded(WindowEventArgs& e)
00230 {
00231         fireEvent(EventThumbTrackEnded, e, EventNamespace);
00232 }
00233 
00234 
00235 /*************************************************************************
00236         Handler triggered when the scroll bar data configuration changes
00237 *************************************************************************/
00238 void Scrollbar::onScrollConfigChanged(WindowEventArgs& e)
00239 {
00240         fireEvent(EventScrollConfigChanged, e, EventNamespace);
00241 }
00242 
00243 
00244 
00245 /*************************************************************************
00246         Handler for when mouse button is clicked within the container
00247 *************************************************************************/
00248 void Scrollbar::onMouseButtonDown(MouseEventArgs& e)
00249 {
00250         // base class processing
00251         Window::onMouseButtonDown(e);
00252 
00253         if (e.button == LeftButton)
00254         {
00255                 float adj = getAdjustDirectionFromPoint(e.position);
00256 
00257                 // adjust scroll bar position in whichever direction as required.
00258                 if (adj != 0)
00259                 {
00260                         setScrollPosition(d_position + ((d_pageSize - d_overlapSize) * adj));
00261                 }
00262 
00263                 e.handled = true;
00264         }
00265 
00266 }
00267 
00268 
00269 /*************************************************************************
00270         Handler for when widget is re-sized
00271 *************************************************************************/
00272 void Scrollbar::onSized(WindowEventArgs& e)
00273 {
00274         // base class processing
00275         Window::onSized(e);
00276 
00277         layoutComponentWidgets();
00278 
00279         e.handled = true;
00280 }
00281 
00282 
00283 /*************************************************************************
00284         Handler for scroll wheel changes
00285 *************************************************************************/
00286 void Scrollbar::onMouseWheel(MouseEventArgs& e)
00287 {
00288         // base class processing
00289         Window::onMouseWheel(e);
00290 
00291         // scroll by e.wheelChange * stepSize
00292         setScrollPosition(d_position + d_stepSize * -e.wheelChange);
00293 
00294         // ensure the message does not go to our parent.
00295         e.handled = true;
00296 }
00297 
00298 
00299 /*************************************************************************
00300         handler function for when thumb moves.
00301 *************************************************************************/
00302 bool Scrollbar::handleThumbMoved(const EventArgs& e)
00303 {
00304         // adjust scroll bar position as required.
00305         setScrollPosition(getValueFromThumb());
00306 
00307         return true;
00308 }
00309 
00310 
00311 /*************************************************************************
00312         handler function for when the increase button is clicked.
00313 *************************************************************************/
00314 bool Scrollbar::handleIncreaseClicked(const EventArgs& e)
00315 {
00316         if (((const MouseEventArgs&)e).button == LeftButton)
00317         {
00318                 // adjust scroll bar position as required.
00319                 setScrollPosition(d_position + d_stepSize);
00320 
00321                 return true;
00322         }
00323         else
00324         {
00325                 return false;
00326         }
00327 
00328 }
00329 
00330 
00331 /*************************************************************************
00332         handler function for when the decrease button is clicked.
00333 *************************************************************************/
00334 bool Scrollbar::handleDecreaseClicked(const EventArgs& e)
00335 {
00336         if (((const MouseEventArgs&)e).button == LeftButton)
00337         {
00338                 // adjust scroll bar position as required.
00339                 setScrollPosition(d_position - d_stepSize);
00340 
00341                 return true;
00342         }
00343         else
00344         {
00345                 return false;
00346         }
00347 }
00348 
00349 
00350 /*************************************************************************
00351         handler function for when thumb tracking begins
00352 *************************************************************************/
00353 bool Scrollbar::handleThumbTrackStarted(const EventArgs& e)
00354 {
00355         // simply trigger our own version of this event
00356         WindowEventArgs args(this);
00357         onThumbTrackStarted(args);
00358 
00359         return true;
00360 }
00361 
00362 
00363 /*************************************************************************
00364         handler function for when thumb tracking begins
00365 *************************************************************************/
00366 bool Scrollbar::handleThumbTrackEnded(const EventArgs& e)
00367 {
00368         // simply trigger our own version of this event
00369         WindowEventArgs args(this);
00370         onThumbTrackEnded(args);
00371 
00372         return true;
00373 }
00374 
00375 
00376 /*************************************************************************
00377         Add scroll bar properties
00378 *************************************************************************/
00379 void Scrollbar::addScrollbarProperties(void)
00380 {
00381         addProperty(&d_documentSizeProperty);
00382         addProperty(&d_pageSizeProperty);
00383         addProperty(&d_stepSizeProperty);
00384         addProperty(&d_overlapSizeProperty);
00385         addProperty(&d_scrollPositionProperty);
00386 }
00387 
00388 
00389 
00390 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:07 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1