listbox.hpp

Go to the documentation of this file.
00001 /* $Id: listbox.hpp 26706 2008-05-18 17:28:31Z 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_LISTBOX_HPP_INCLUDED__
00016 #define __GUI_WIDGETS_LISTBOX_HPP_INCLUDED__
00017 
00018 #include "gui/widgets/container.hpp"
00019 
00020 class t_string;
00021 
00022 namespace gui2 {
00023 
00024 class tscrollbar_;
00025 class tspacer;
00026 
00027 //! @todo list
00028 //! header row + footer row same width as client data
00029 //! cell or row select
00030 //! sort at some way
00031 //
00032 //maybe create two types 1 fixed size and one with a builder to add new rows
00033 
00034 class tlistbox : public tcontainer_
00035 {
00036     friend class tbuilder_listbox;
00037 public:
00038     
00039     tlistbox();
00040 
00041     // FIXME this might not the right thing to do.
00042     void set_active(const bool active) { set_state(active ? ENABLED : DISABLED); };
00043     bool get_active() const { return state_ != DISABLED; }
00044     unsigned get_state() const { return state_; }
00045 
00046     /** Inherited from twidget. */
00047     bool has_vertical_scrollbar() const { return true; }
00048 
00049     /** 
00050      * When an item in the list is selected by the user we need to
00051      * update the state. We installed a callback handler which 
00052      * calls us.
00053      */
00054     void list_item_selected(twidget* caller);
00055 
00056     /**
00057      * Callback when the scrollbar moves.
00058      */
00059     void scrollbar_moved(twidget* /*caller*/)
00060         { set_scrollbar_button_status(); set_dirty(); }
00061 
00062     /** 
00063      * When an item scrollbar control button is clicked we need to move the
00064      * scrollbar and update the list. 
00065      */
00066     void scrollbar_click(twidget* caller);
00067 
00068     /** The builder needs to call us so we can write in the proper callbacks. */
00069     void finalize_setup();
00070 
00071     void set_list_builder(tbuilder_grid* list_builder) 
00072         { list_builder_ = list_builder; }
00073 
00074     void set_assume_fixed_row_size(bool assume = true) 
00075         { assume_fixed_row_size_ = assume; }
00076 
00077     /** Inherited from tcontainer. */
00078     tpoint get_best_size() const;
00079 
00080     /** Inherited from tcontainer. */
00081     void draw(surface& surface);
00082 
00083     /** Inherited from tcontainer. */
00084     void set_size(const SDL_Rect& rect);
00085 
00086     /** Inherited from tcontainer. */
00087     twidget* find_widget(const tpoint& coordinate, const bool must_be_active);
00088 
00089     /** Inherited from tcontainer. */
00090     const twidget* find_widget(const tpoint& coordinate, 
00091             const bool must_be_active) const;
00092 
00093     /*
00094      * NOTE twidget* find_widget(const std::string& id, const bool must_be_active);
00095      * and it's const version are inherited from tcontainer_ but gcc isn't too 
00096      * happy with that so we need to call tcontainer_::find_widget() so when
00097      * it's required to override those, check that the tcontainer_:: is dropped.
00098      */
00099 
00100     /**
00101      * Adds an item to the list, it requires the builder_list to be defined. 
00102      * NOTE this is for a listbox with one item per row, for multiple items
00103      * there will be a version with gets a vector with values. Probably there
00104      * also will be a version which gets the name of an image next to the
00105      * label so we can define lists even easier.
00106      *
00107      * Probably the hardcoded list will disappear as well at some point
00108      */
00109     void add_item(const t_string& label);
00110 
00111     unsigned get_item_count() const { return rows_.size(); }
00112 
00113     /** 
00114      * Selects an entire row. 
00115      *
00116      * @param row                 The row to (de)select.
00117      * @param select              true select, false deselect.
00118      *
00119      * @returns                   false if deselecting wasn't allowed.
00120      *                            true otherwise.
00121      */
00122     bool select_row(const unsigned row, const bool select = true);
00123 
00124     unsigned get_selected_row() const { return selected_row_; }
00125 
00126     /** 
00127      * Makes a row active or inactive.
00128      *
00129      * NOTE this doesn't change the select status of the row.
00130      *
00131      * @param row                 The row to (de)activate.
00132      * @param select              true activate, false deactivate.
00133      */
00134     void set_row_active(const unsigned row, const bool active);
00135 
00136 private:
00137 
00138     /** 
00139      * Sets the status of the scrollbar buttons.
00140      *
00141      * This is needed after the scrollbar moves so the status of the buttons
00142      * will be active or inactive as needed.
00143      */
00144     void set_scrollbar_button_status();
00145 
00146     //! Note the order of the states must be the same as defined in settings.hpp.
00147     enum tstate { ENABLED, DISABLED, COUNT };
00148 
00149     void set_state(tstate state) {} // FIXME implement
00150     tstate state_;
00151 
00152     /** It's possible to let the engine build the contents, we need the builder in that case */
00153     tbuilder_grid* list_builder_;
00154 
00155     /** Returns the scrollbar widget */
00156     tscrollbar_* scrollbar();
00157 
00158     /** Returns the scrollbar widget */
00159     const tscrollbar_* scrollbar() const;
00160 
00161     /** Returns the spacer widget which is used to reserve space of the real list. */
00162     tspacer* list();
00163 
00164     /** Returns the spacer widget which is used to reserve space of the real list. */
00165     const tspacer* list() const;
00166 
00167     bool assume_fixed_row_size_;
00168 
00169     //! Inherited from tcontrol.
00170     const std::string& get_control_type() const 
00171         { static const std::string type = "listbox"; return type; }
00172 
00173     /** The (lastly) selected row */
00174     unsigned selected_row_;
00175 
00176     /** Number of items selected */
00177     unsigned selection_count_;
00178 
00179     /** Select per cell or an entire row */
00180     bool row_select_;
00181 
00182     /** At least 1 item must be selected */
00183     bool must_select_;
00184 
00185     /** Multiple items can be selected */
00186     bool multi_select_; 
00187     
00188     /** The sizes of the spacer. */
00189     SDL_Rect list_rect_;
00190 
00191     /** The background of the list, needed for redrawing. */
00192     surface list_background_;
00193 
00194     /** The best size for the spacer, if not set it's calculated. */
00195     tpoint best_spacer_size_;
00196 
00197     class trow {
00198 
00199     public:
00200         trow(const tbuilder_grid& list_builder_, const t_string& label);
00201 
00202         void select(const bool sel = true);
00203     
00204         tgrid* grid() { return grid_; }
00205         const tgrid* grid() const { return grid_; }
00206 
00207         void set_height(const unsigned height) { height_ = height; }
00208         unsigned get_height() const { return height_; }
00209 
00210         const surface& canvas() const { return canvas_; }
00211         surface& canvas() { return canvas_; }
00212 
00213         bool get_selected() const { return selected_; }
00214     private:
00215 
00216         tgrid* grid_;
00217 
00218         unsigned height_;
00219 
00220         surface canvas_;
00221 
00222         bool selected_;
00223 
00224         void init_in_grid(tgrid* grid, const t_string& label);
00225 
00226         void select_in_grid(tgrid* grid, const bool sel);
00227     };
00228 
00229     std::vector<trow> rows_;
00230 };
00231 
00232 } // namespace gui2
00233 
00234 #endif
00235 
00236 

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