scrollbar.hpp

Go to the documentation of this file.
00001 /* $Id: scrollbar.hpp 26682 2008-05-18 07:14:57Z mordante $ */
00002 /*
00003    copyright (C) 2008 by mark de wever <koraq@xs4all.nl>
00004    part of the battle for wesnoth project http://www.wesnoth.org/
00005 
00006    this program is free software; you can redistribute it and/or modify
00007    it under the terms of the gnu general public license version 2
00008    or at your option any later version.
00009    this program is distributed in the hope that it will be useful,
00010    but without any warranty.
00011 
00012    see the copying file for more details.
00013 */
00014 
00015 #ifndef __GUI_WIDGETS_SCROLLBAR_HPP_INCLUDED__
00016 #define __GUI_WIDGETS_SCROLLBAR_HPP_INCLUDED__
00017 
00018 #include "gui/widgets/control.hpp"
00019 
00020 namespace gui2 {
00021 
00022 //! Base class for a scroll bar.
00023 //!
00024 //! class will be subclassed for the horizontal and vertical scroll bar. 
00025 //! It might be subclassed for a slider class.
00026 //!
00027 //! To make this class generic we talk a lot about offset and length and use 
00028 //! pure virtual functions. The classes implementing us can use the heights or
00029 //! widths, whichever is applicable.
00030 class tscrollbar_ : public tcontrol
00031 {
00032 public:
00033     
00034     tscrollbar_() :
00035         tcontrol(COUNT),
00036         state_(ENABLED),
00037         item_count_(0),
00038         item_position_(0),
00039         visible_items_(1),
00040         step_size_(1),
00041         pixels_per_step_(0.0),
00042         mouse_(0, 0),
00043         positioner_offset_(0),
00044         positioner_length_(0),
00045         callback_positioner_move_(0)
00046     {
00047     }
00048 
00049     //! Inherited from twidget.
00050     //! We only need to track the mouse if it's on the positioner so the normal
00051     //! enter doesn't help so transfer the control.
00052     void mouse_enter(tevent_handler& event) { mouse_move(event); }
00053 
00054     //! Inherited from twidget.
00055     void mouse_move(tevent_handler& event);
00056 
00057     //! Inherited from twidget.
00058     //! Leave doesn't have the problem which mouse_enter has so it does it's own
00059     //! job.
00060     void mouse_leave(tevent_handler&);
00061 
00062     //! Inherited from twidget.
00063     void mouse_left_button_down(tevent_handler& event);
00064 
00065     //! Inherited from twidget.
00066     void mouse_left_button_up(tevent_handler& event);
00067 
00068     //! Inherited from tcontrol.
00069     void set_active(const bool active) 
00070         { if(get_active() != active) set_state(active ? ENABLED : DISABLED); };
00071 
00072     //! Inherited from tcontrol.
00073     bool get_active() const { return state_ != DISABLED; }
00074 
00075     //! Inherited from tcontrol.
00076     unsigned get_state() const { return state_; }
00077 
00078     //! Inherited from tcontrol.
00079     void set_size(const SDL_Rect& rect);
00080 
00081     unsigned get_item_count() const { return item_count_; }
00082     void set_item_count(const unsigned item_count)
00083         { item_count_ = item_count; recalculate(); }
00084 
00085     unsigned get_item_position() const { return item_position_; }
00086 
00087     //! Note the position isn't guaranteed to be the wanted position
00088     //! the step size is honoured. The value will be rouded down
00089     void set_item_position(const unsigned item_position);
00090 
00091     enum tscroll { 
00092         BEGIN,               /**< Go to begin position. */
00093         ITEM_BACKWARDS,      /**< Go one item towards the begin. */
00094         HALF_JUMP_BACKWARDS, /**< Go half the visible items towards the begin. */
00095         JUMP_BACKWARDS,      /**< Go the visibile items towards the begin. */
00096         END,                 /**< Go to the end position. */
00097         ITEM_FORWARD,        /**< Go one item towards the end. */
00098         HALF_JUMP_FORWARD,   /**< Go half the visible items towards the end. */
00099         JUMP_FORWARD };      /**< Go the visible items towards the end. */
00100 
00101     /** Sets the item position. */ 
00102     void scroll(const tscroll scroll);
00103 
00104     unsigned get_visible_items() const { return visible_items_; }
00105     void set_visible_items(const unsigned visible_items)
00106         { visible_items_ = visible_items; recalculate(); }
00107 
00108     unsigned get_step_size() const { return step_size_; }
00109     void set_step_size(const unsigned step_size)
00110         { step_size_ = step_size; recalculate(); }
00111 
00112     //! Is the positioner at the beginning of the scrollbar.
00113     bool at_begin() const { return item_position_ == 0; }
00114 
00115     //! Is the positioner at the and of the scrollbar, note both begin and end
00116     //! might be true at the same time.
00117     bool at_end() const 
00118         { return item_position_ + visible_items_ == item_count_; }
00119 
00120     void set_callback_positioner_move(void (*callback) (twidget*)) 
00121         { callback_positioner_move_ = callback; }
00122 
00123 protected:
00124     unsigned get_positioner_offset() const { return positioner_offset_; }
00125 
00126     unsigned get_positioner_length() const { return positioner_length_; }
00127 
00128 private:
00129     //! Note the order of the states must be the same as defined in settings.hpp.
00130     enum tstate { ENABLED, DISABLED, PRESSED, FOCUSSED, COUNT };
00131 
00132     void set_state(const tstate state);
00133     tstate state_;
00134 
00135     //! Inherited from tcontrol.
00136     void load_config_extra();
00137 
00138     //! The number of items the scrollbar 'holds'.
00139     unsigned item_count_;
00140 
00141     //! The item the positioner is at, starts at 0.
00142     unsigned item_position_;
00143 
00144     //! The number of items which can be shown at the same time.
00145     //! As long as all items are visible we don't need to scroll.
00146     unsigned visible_items_;
00147 
00148     //! The step size is the minimum number of items we scroll through when we
00149     //! move. Normally this value is 1, we can move per item. But for example
00150     //! sliders want for example to move per 5 items.
00151     unsigned step_size_;
00152 
00153     //! The number of pixels the positioner needs to move to go to the next step.
00154     //! Note if there is too little space it can happen 1 pixel does more than 1
00155     //! step.
00156     float pixels_per_step_;
00157 
00158     //! Get the length of the object.
00159     virtual unsigned get_length() const = 0;
00160 
00161     //! The minimum length of the positioner.
00162     virtual unsigned minimum_positioner_length() const = 0;
00163 
00164     //! The number of pixels we can't use since they're used for borders.
00165     //! These are the pixels before the widget (left side if horizontal,
00166     //! top side if vertical).
00167     virtual unsigned offset_before() const = 0;
00168 
00169     //! The number of pixels we can't use since they're used for borders.
00170     //! These are the pixels after the widget (right side if horizontal,
00171     //! bottom side if vertical).
00172     virtual unsigned offset_after() const = 0;
00173 
00174     //! Is the current location on the positioner?
00175     virtual bool on_positioner(const tpoint& coordinate) const = 0;
00176 
00177     //! Gets the relevent difference in between the two positions.
00178     //!
00179     //! This function is used to determine how much the positioner needs to  be
00180     //! moved.
00181     virtual int get_length_difference(
00182         const tpoint& original, const tpoint& current) const = 0;
00183 
00184     //! The position the mouse was at the last movement. This is used during
00185     //! dragging the positioner.
00186     tpoint mouse_;
00187 
00188     //! The start offset of the positioner.
00189     //! This takes the offset before in consideration.
00190     unsigned positioner_offset_;
00191 
00192     //! The current length of the positioner.
00193     unsigned positioner_length_;
00194 
00195     //! Updates the scrollbar.
00196     //!
00197     //! Needs to be called when someting changes eg number of items
00198     //! or available size. It can only be called once we have a size
00199     //! otherwise we can't calulate a thing.
00200     void recalculate();
00201 
00202     //! After a recalculation the canvasses also need to be updated.
00203     void update_canvas();
00204 
00205     //! Moves the positioner.
00206     //!
00207     //! @param distance                The distance moved, negative to begin,
00208     //!                                positive to end.
00209     void move_positioner(const int distance);
00210 
00211     /** 
00212      * This callback is used when the positioner is moved by the user.
00213      */
00214     void (*callback_positioner_move_) (twidget*);
00215 };
00216 
00217 } // namespace gui2
00218 
00219 #endif
00220 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs