gui/widgets/multi_page.cpp

Go to the documentation of this file.
00001 /* $Id: multi_page.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 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 as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "gui/widgets/multi_page.hpp"
00019 
00020 #include "gui/auxiliary/widget_definition/multi_page.hpp"
00021 #include "gui/auxiliary/window_builder/multi_page.hpp"
00022 #include "gui/widgets/settings.hpp"
00023 #include "gui/widgets/generator.hpp"
00024 
00025 #include <boost/bind.hpp>
00026 
00027 namespace gui2 {
00028 
00029 REGISTER_WIDGET(multi_page)
00030 tmulti_page::tmulti_page()
00031     : tcontainer_(0)
00032     , generator_(NULL)
00033     , page_builder_(NULL)
00034 {
00035     generator_ = tgenerator_::build(
00036             true, true, tgenerator_::independent, false);
00037 }
00038 
00039 void tmulti_page::add_page(const string_map& item)
00040 {
00041     assert(generator_);
00042     generator_->create_item(-1, page_builder_, item, NULL);
00043 }
00044 
00045 void tmulti_page::add_page(
00046         const std::map<std::string /* widget id */, string_map>& data)
00047 {
00048     assert(generator_);
00049     generator_->create_item(-1, page_builder_, data, NULL);
00050 }
00051 
00052 void tmulti_page::remove_page(const unsigned page, unsigned count)
00053 {
00054     assert(generator_);
00055 
00056     if(page >= get_page_count()) {
00057         return;
00058     }
00059 
00060     if(!count || count > get_page_count()) {
00061         count = get_page_count();
00062     }
00063 
00064     for(; count; --count) {
00065         generator_->delete_item(page);
00066     }
00067 }
00068 
00069 void tmulti_page::clear()
00070 {
00071     assert(generator_);
00072     generator_->clear();
00073 }
00074 
00075 unsigned tmulti_page::get_page_count() const
00076 {
00077     assert(generator_);
00078     return generator_->get_item_count();
00079 }
00080 
00081 void tmulti_page::select_page(const unsigned page, const bool select)
00082 {
00083     assert(generator_);
00084     generator_->select_item(page, select);
00085 }
00086 
00087 int tmulti_page::get_selected_page() const
00088 {
00089     assert(generator_);
00090     return generator_->get_selected_item();
00091 }
00092 
00093 const tgrid& tmulti_page::page_grid(const unsigned page) const
00094 {
00095     assert(generator_);
00096     return generator_->item(page);
00097 }
00098 
00099 tgrid& tmulti_page::page_grid(const unsigned page)
00100 {
00101     assert(generator_);
00102     return generator_->item(page);
00103 }
00104 
00105 namespace {
00106 
00107 /**
00108  * Swaps an item in a grid for another one.*/
00109 void swap_grid(tgrid* grid,
00110         tgrid* content_grid, twidget* widget, const std::string& id)
00111 {
00112     assert(content_grid);
00113     assert(widget);
00114 
00115     // Make sure the new child has same id.
00116     widget->set_id(id);
00117 
00118     // Get the container containing the wanted widget.
00119     tgrid* parent_grid = NULL;
00120     if(grid) {
00121         parent_grid = find_widget<tgrid>(grid, id, false, false);
00122     }
00123     if(!parent_grid) {
00124         parent_grid = find_widget<tgrid>(content_grid, id, true, false);
00125     }
00126     parent_grid = dynamic_cast<tgrid*>(parent_grid->parent());
00127     assert(parent_grid);
00128 
00129     // Replace the child.
00130     widget = parent_grid->swap_child(id, widget, false);
00131     assert(widget);
00132 
00133     delete widget;
00134 }
00135 
00136 } // namespace
00137 
00138 void tmulti_page::finalize(
00139         const std::vector<string_map>& page_data)
00140 {
00141     assert(generator_);
00142     generator_->create_items(-1, page_builder_, page_data, NULL);
00143     swap_grid(NULL, &grid(), generator_, "_content_grid");
00144 }
00145 
00146 const std::string& tmulti_page::get_control_type() const
00147 {
00148     static const std::string type = "multi_page";
00149     return type;
00150 }
00151 
00152 } // namespace gui2
00153 
00154 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:55 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs