gui/widgets/stacked_widget.cpp

Go to the documentation of this file.
00001 /* $Id: stacked_widget.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 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/stacked_widget.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gui/auxiliary/widget_definition/stacked_widget.hpp"
00022 #include "gui/auxiliary/window_builder/stacked_widget.hpp"
00023 #include "gui/widgets/settings.hpp"
00024 #include "gui/widgets/generator.hpp"
00025 
00026 #include <boost/bind.hpp>
00027 
00028 namespace gui2 {
00029 
00030 REGISTER_WIDGET(stacked_widget)
00031 
00032 tstacked_widget::tstacked_widget()
00033     : tcontainer_(1)
00034     , generator_(NULL)
00035 {
00036     generator_ = tgenerator_::build(
00037             false, false, tgenerator_::independent, false);
00038 }
00039 
00040 void tstacked_widget::layout_children()
00041 {
00042     assert(generator_);
00043     for(unsigned i = 0; i < generator_->get_item_count(); ++i) {
00044         generator_->item(i).layout_children();
00045     }
00046 }
00047 
00048 namespace {
00049 
00050 /**
00051  * Swaps an item in a grid for another one.*/
00052 void swap_grid(tgrid* grid,
00053         tgrid* content_grid, twidget* widget, const std::string& id)
00054 {
00055     assert(content_grid);
00056     assert(widget);
00057 
00058     // Make sure the new child has same id.
00059     widget->set_id(id);
00060 
00061     // Get the container containing the wanted widget.
00062     tgrid* parent_grid = NULL;
00063     if(grid) {
00064         parent_grid = find_widget<tgrid>(grid, id, false, false);
00065     }
00066     if(!parent_grid) {
00067         parent_grid = find_widget<tgrid>(content_grid, id, true, false);
00068     }
00069     parent_grid = dynamic_cast<tgrid*>(parent_grid->parent());
00070     assert(parent_grid);
00071 
00072     // Replace the child.
00073     widget = parent_grid->swap_child(id, widget, false);
00074     assert(widget);
00075 
00076     delete widget;
00077 }
00078 
00079 } // namespace
00080 
00081 void tstacked_widget::finalize(
00082         std::vector<tbuilder_grid_const_ptr> widget_builder)
00083 {
00084     assert(generator_);
00085     string_map empty_data;
00086     foreach(const tbuilder_grid_const_ptr& builder, widget_builder) {
00087         generator_->create_item(-1, builder, empty_data, NULL);
00088     }
00089     swap_grid(NULL, &grid(), generator_, "_content_grid");
00090 
00091     for(size_t i = 0; i < generator_->get_item_count(); ++i) {
00092         generator_->select_item(i, true);
00093     }
00094 }
00095 
00096 const std::string& tstacked_widget::get_control_type() const
00097 {
00098     static const std::string type = "stacked_widget";
00099     return type;
00100 }
00101 
00102 } // namespace gui2
00103 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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