ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

scrollbar.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/rect.h"
00024 #include "common/system.h"
00025 #include "common/timer.h"
00026 #include "gui/widgets/scrollbar.h"
00027 #include "gui/gui-manager.h"
00028 #include "gui/ThemeEngine.h"
00029 #include "gui/widgets/scrollcontainer.h"
00030 
00031 namespace GUI {
00032 
00033 #define UP_DOWN_BOX_HEIGHT  (_w+1)
00034 
00035 ScrollBarWidget::ScrollBarWidget(GuiObject *boss, int x, int y, int w, int h)
00036     : Widget (boss, x, y, w, h), CommandSender(boss) {
00037     setFlags(WIDGET_ENABLED | WIDGET_TRACK_MOUSE | WIDGET_CLEARBG | WIDGET_WANT_TICKLE);
00038     _type = kScrollBarWidget;
00039 
00040     _part = kNoPart;
00041     _sliderHeight = 0;
00042     _sliderPos = 0;
00043 
00044     _draggingPart = kNoPart;
00045     _sliderDeltaMouseDownPos = 0;
00046 
00047     _numEntries = 0;
00048     _entriesPerPage = 0;
00049     _currentPos = 0;
00050     _singleStep = 1;
00051 
00052     _repeatTimer = 0;
00053 }
00054 
00055 void ScrollBarWidget::handleMouseDown(int x, int y, int button, int clickCount) {
00056     int old_pos = _currentPos;
00057 
00058     // Do nothing if there are less items than fit on one page
00059     if (_numEntries <= _entriesPerPage)
00060         return;
00061 
00062     if (y <= UP_DOWN_BOX_HEIGHT) {
00063         // Up arrow
00064         _currentPos -= _singleStep;
00065         _repeatTimer = g_system->getMillis() + kRepeatInitialDelay;
00066         _draggingPart = kUpArrowPart;
00067     } else if (y >= _h - UP_DOWN_BOX_HEIGHT) {
00068         // Down arrow
00069         _currentPos += _singleStep;
00070         _repeatTimer = g_system->getMillis() + kRepeatInitialDelay;
00071         _draggingPart = kDownArrowPart;
00072     } else if (y < _sliderPos) {
00073         _currentPos -= _entriesPerPage - 1;
00074     } else if (y >= _sliderPos + _sliderHeight) {
00075         _currentPos += _entriesPerPage - 1;
00076     } else {
00077         _draggingPart = kSliderPart;
00078         _sliderDeltaMouseDownPos = y - _sliderPos;
00079     }
00080 
00081     // Make sure that _currentPos is still inside the bounds
00082     checkBounds(old_pos);
00083 }
00084 
00085 void ScrollBarWidget::handleMouseUp(int x, int y, int button, int clickCount) {
00086     _draggingPart = kNoPart;
00087     _repeatTimer = 0;
00088 }
00089 
00090 void ScrollBarWidget::handleMouseWheel(int x, int y, int direction) {
00091     int old_pos = _currentPos;
00092 
00093     if (_numEntries < _entriesPerPage)
00094         return;
00095 
00096     if (direction < 0) {
00097         _currentPos -= _singleStep;
00098     } else {
00099         _currentPos += _singleStep;
00100     }
00101 
00102     // Make sure that _currentPos is still inside the bounds
00103     checkBounds(old_pos);
00104 }
00105 
00106 void ScrollBarWidget::handleMouseMoved(int x, int y, int button) {
00107     // Do nothing if there are less items than fit on one page
00108     if (_numEntries <= _entriesPerPage)
00109         return;
00110 
00111     if (_draggingPart == kSliderPart) {
00112         int old_pos = _currentPos;
00113         _sliderPos = y - _sliderDeltaMouseDownPos;
00114 
00115         if (_sliderPos < UP_DOWN_BOX_HEIGHT)
00116             _sliderPos = UP_DOWN_BOX_HEIGHT;
00117 
00118         if (_sliderPos > _h - UP_DOWN_BOX_HEIGHT - _sliderHeight)
00119             _sliderPos = _h - UP_DOWN_BOX_HEIGHT - _sliderHeight;
00120 
00121         _currentPos =
00122             (_sliderPos - UP_DOWN_BOX_HEIGHT) * (_numEntries - _entriesPerPage) / (_h - 2 * UP_DOWN_BOX_HEIGHT - _sliderHeight);
00123         checkBounds(old_pos);
00124     } else {
00125         int old_part = _part;
00126 
00127         if (y <= UP_DOWN_BOX_HEIGHT)    // Up arrow
00128             _part = kUpArrowPart;
00129         else if (y >= _h - UP_DOWN_BOX_HEIGHT)  // Down arrow
00130             _part = kDownArrowPart;
00131         else if (y < _sliderPos)
00132             _part = kPageUpPart;
00133         else if (y >= _sliderPos + _sliderHeight)
00134             _part = kPageDownPart;
00135         else
00136             _part = kSliderPart;
00137 
00138         if (old_part != _part)
00139             markAsDirty();
00140     }
00141 }
00142 
00143 void ScrollBarWidget::handleTickle() {
00144     if (_repeatTimer) {
00145         const uint32 curTime = g_system->getMillis();
00146         if (curTime >= _repeatTimer) {
00147             const int old_pos = _currentPos;
00148 
00149             if (_part == kUpArrowPart)
00150                 _currentPos -= 3 * _singleStep;
00151             else if (_part == kDownArrowPart)
00152                 _currentPos += 3 * _singleStep;
00153 
00154             checkBounds(old_pos);
00155 
00156             _repeatTimer = curTime + kRepeatDelay;
00157         }
00158     }
00159 }
00160 
00161 void ScrollBarWidget::checkBounds(int old_pos) {
00162     if (_numEntries <= _entriesPerPage || _currentPos < 0)
00163         _currentPos = 0;
00164     else if (_currentPos > _numEntries - _entriesPerPage)
00165         _currentPos = _numEntries - _entriesPerPage;
00166 
00167     if (old_pos != _currentPos) {
00168         recalc();
00169         markAsDirty();
00170         sendCommand(kSetPositionCmd, _currentPos);
00171     }
00172 }
00173 
00174 void ScrollBarWidget::recalc() {
00175     if (_numEntries > _entriesPerPage) {
00176         _sliderHeight = (_h - 2 * UP_DOWN_BOX_HEIGHT) * _entriesPerPage / _numEntries;
00177         if (_sliderHeight < UP_DOWN_BOX_HEIGHT)
00178             _sliderHeight = UP_DOWN_BOX_HEIGHT;
00179 
00180         _sliderPos =
00181             UP_DOWN_BOX_HEIGHT + (_h - 2 * UP_DOWN_BOX_HEIGHT - _sliderHeight) * _currentPos / (_numEntries - _entriesPerPage);
00182         if (_sliderPos < 0)
00183             _sliderPos = 0;
00184         setVisible(true);
00185     } else {
00186         _sliderHeight = _h - 2 * UP_DOWN_BOX_HEIGHT;
00187         _sliderPos = UP_DOWN_BOX_HEIGHT;
00188         setVisible(false);
00189     }
00190 }
00191 
00192 void ScrollBarWidget::drawWidget() {
00193     if (_draggingPart != kNoPart)
00194         _part = _draggingPart;
00195 
00196     ThemeEngine::ScrollbarState state = ThemeEngine::kScrollbarStateNo;
00197     if (_numEntries <= _entriesPerPage) {
00198         state = ThemeEngine::kScrollbarStateSinglePage;
00199     } else if (_part == kUpArrowPart) {
00200         state = ThemeEngine::kScrollbarStateUp;
00201     } else if (_part == kDownArrowPart) {
00202         state = ThemeEngine::kScrollbarStateDown;
00203     } else if (_part == kSliderPart) {
00204         state = ThemeEngine::kScrollbarStateSlider;
00205     }
00206 
00207     g_gui.theme()->drawScrollbar(Common::Rect(_x, _y, _x + _w, _y + _h), _sliderPos, _sliderHeight, state);
00208 }
00209 
00210 } // End of namespace GUI


Generated on Sat Oct 19 2019 05:01:23 for ResidualVM by doxygen 1.7.1
curved edge   curved edge