scrollarea.cpp

Go to the documentation of this file.
00001 /* $Id: scrollarea.cpp 23842 2008-02-16 08:47:16Z mordante $*/
00002 /*
00003    Copyright (C) 2004 - 2008 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
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 //! @file widgets/scrollarea.cpp
00016 //!
00017 
00018 #include "global.hpp"
00019 
00020 #include "widgets/scrollarea.hpp"
00021 
00022 #include <algorithm>
00023 #include <iostream>
00024 
00025 namespace gui {
00026 
00027 scrollarea::scrollarea(CVideo &video, const bool auto_join)
00028     : widget(video, auto_join), scrollbar_(video),
00029       old_position_(0), recursive_(false), shown_scrollbar_(false),
00030       shown_size_(0), full_size_(0)
00031 {
00032     scrollbar_.hide(true);
00033 }
00034 
00035 bool scrollarea::has_scrollbar() const
00036 {
00037     return shown_size_ < full_size_ && scrollbar_.is_valid_height(location().h);
00038 }
00039 
00040 handler_vector scrollarea::handler_members()
00041 {
00042     handler_vector h;
00043     h.push_back(&scrollbar_);
00044     return h;
00045 }
00046 
00047 void scrollarea::update_location(SDL_Rect const &rect)
00048 {
00049     SDL_Rect r = rect;
00050     shown_scrollbar_ = has_scrollbar();
00051     if (shown_scrollbar_) {
00052         int w = r.w - scrollbar_.width();
00053         r.x += w;
00054         r.w -= w;
00055         scrollbar_.set_location(r);
00056         r.x -= w;
00057         r.w = w;
00058     }
00059 
00060     if (!hidden())
00061         scrollbar_.hide(!shown_scrollbar_);
00062     set_inner_location(r);
00063 }
00064 
00065 void scrollarea::test_scrollbar()
00066 {
00067     if (recursive_)
00068         return;
00069     recursive_ = true;
00070     if (shown_scrollbar_ != has_scrollbar()) {
00071         bg_restore();
00072         bg_cancel();
00073         update_location(location());
00074     }
00075     recursive_ = false;
00076 }
00077 
00078 void scrollarea::hide(bool value)
00079 {
00080     widget::hide(value);
00081     if (shown_scrollbar_)
00082         scrollbar_.hide(value);
00083 }
00084 
00085 unsigned scrollarea::get_position() const
00086 {
00087     return scrollbar_.get_position();
00088 }
00089 
00090 unsigned scrollarea::get_max_position() const
00091 {
00092     return scrollbar_.get_max_position();
00093 }
00094 
00095 void scrollarea::set_position(unsigned pos)
00096 {
00097     scrollbar_.set_position(pos);
00098 }
00099 
00100 void scrollarea::adjust_position(unsigned pos)
00101 {
00102     scrollbar_.adjust_position(pos);
00103 }
00104 
00105 void scrollarea::move_position(int dep)
00106 {
00107     scrollbar_.move_position(dep);
00108 }
00109 
00110 void scrollarea::set_shown_size(unsigned h)
00111 {
00112     scrollbar_.set_shown_size(h);
00113     shown_size_ = h;
00114     test_scrollbar();
00115 }
00116 
00117 void scrollarea::set_full_size(unsigned h)
00118 {
00119     scrollbar_.set_full_size(h);
00120     full_size_ = h;
00121     test_scrollbar();
00122 }
00123 
00124 void scrollarea::set_scroll_rate(unsigned r)
00125 {
00126     scrollbar_.set_scroll_rate(r);
00127 }
00128 
00129 void scrollarea::process_event()
00130 {
00131     int grip_position = scrollbar_.get_position();
00132     if (grip_position == old_position_)
00133         return;
00134     old_position_ = grip_position;
00135     scroll(grip_position);
00136 }
00137 
00138 SDL_Rect scrollarea::inner_location() const
00139 {
00140     SDL_Rect r = location();
00141     if (shown_scrollbar_)
00142         r.w -= scrollbar_.width();
00143     return r;
00144 }
00145 
00146 unsigned scrollarea::scrollbar_width() const
00147 {
00148     return scrollbar_.width();
00149 }
00150 
00151 void scrollarea::handle_event(const SDL_Event& event)
00152 {
00153     if (hidden() || event.type != SDL_MOUSEBUTTONDOWN)
00154         return;
00155 
00156     SDL_MouseButtonEvent const &e = event.button;
00157     if (point_in_rect(e.x, e.y, inner_location())) {
00158         if (e.button == SDL_BUTTON_WHEELDOWN) {
00159             scrollbar_.scroll_down();
00160         } else if (e.button == SDL_BUTTON_WHEELUP) {
00161             scrollbar_.scroll_up();
00162         }
00163     }
00164 }
00165 
00166 } // end namespace gui
00167 

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