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

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(getName() + "__auto_thumb__");
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(getName() + "__auto_incbtn__");
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(getName() + "__auto_decbtn__");
00097         addChildWindow(d_decrease);
00098         d_decrease->subscribeEvent(PushButton::EventMouseButtonDown, Event::Subscriber(&CEGUI::Scrollbar::handleDecreaseClicked, this));
00099 
00100         // do initial layout
00101         performChildWindowLayout();
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 scroll wheel changes
00271 *************************************************************************/
00272 void Scrollbar::onMouseWheel(MouseEventArgs& e)
00273 {
00274         // base class processing
00275         Window::onMouseWheel(e);
00276 
00277         // scroll by e.wheelChange * stepSize
00278         setScrollPosition(d_position + d_stepSize * -e.wheelChange);
00279 
00280         // ensure the message does not go to our parent.
00281         e.handled = true;
00282 }
00283 
00284 
00285 /*************************************************************************
00286         handler function for when thumb moves.
00287 *************************************************************************/
00288 bool Scrollbar::handleThumbMoved(const EventArgs& e)
00289 {
00290         // adjust scroll bar position as required.
00291         setScrollPosition(getValueFromThumb());
00292 
00293         return true;
00294 }
00295 
00296 
00297 /*************************************************************************
00298         handler function for when the increase button is clicked.
00299 *************************************************************************/
00300 bool Scrollbar::handleIncreaseClicked(const EventArgs& e)
00301 {
00302         if (((const MouseEventArgs&)e).button == LeftButton)
00303         {
00304                 // adjust scroll bar position as required.
00305                 setScrollPosition(d_position + d_stepSize);
00306 
00307                 return true;
00308         }
00309         else
00310         {
00311                 return false;
00312         }
00313 
00314 }
00315 
00316 
00317 /*************************************************************************
00318         handler function for when the decrease button is clicked.
00319 *************************************************************************/
00320 bool Scrollbar::handleDecreaseClicked(const EventArgs& e)
00321 {
00322         if (((const MouseEventArgs&)e).button == LeftButton)
00323         {
00324                 // adjust scroll bar position as required.
00325                 setScrollPosition(d_position - d_stepSize);
00326 
00327                 return true;
00328         }
00329         else
00330         {
00331                 return false;
00332         }
00333 }
00334 
00335 
00336 /*************************************************************************
00337         handler function for when thumb tracking begins
00338 *************************************************************************/
00339 bool Scrollbar::handleThumbTrackStarted(const EventArgs& e)
00340 {
00341         // simply trigger our own version of this event
00342         WindowEventArgs args(this);
00343         onThumbTrackStarted(args);
00344 
00345         return true;
00346 }
00347 
00348 
00349 /*************************************************************************
00350         handler function for when thumb tracking begins
00351 *************************************************************************/
00352 bool Scrollbar::handleThumbTrackEnded(const EventArgs& e)
00353 {
00354         // simply trigger our own version of this event
00355         WindowEventArgs args(this);
00356         onThumbTrackEnded(args);
00357 
00358         return true;
00359 }
00360 
00361 
00362 /*************************************************************************
00363         Add scroll bar properties
00364 *************************************************************************/
00365 void Scrollbar::addScrollbarProperties(void)
00366 {
00367         addProperty(&d_documentSizeProperty);
00368         addProperty(&d_pageSizeProperty);
00369         addProperty(&d_stepSizeProperty);
00370         addProperty(&d_overlapSizeProperty);
00371         addProperty(&d_scrollPositionProperty);
00372 }
00373 
00374 
00375 
00376 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:33 2005 for Crazy Eddies GUI System by  doxygen 1.4.3