gui/widgets/tree_view.cpp

Go to the documentation of this file.
00001 /* $Id: tree_view.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2010 - 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/tree_view.hpp"
00019 
00020 #include "gui/auxiliary/log.hpp"
00021 #include "gui/auxiliary/widget_definition/tree_view.hpp"
00022 #include "gui/auxiliary/window_builder/tree_view.hpp"
00023 #include "gui/widgets/settings.hpp"
00024 #include "gui/widgets/tree_view_node.hpp"
00025 #include "gui/widgets/window.hpp"
00026 
00027 #include <boost/bind.hpp>
00028 
00029 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
00030 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
00031 
00032 namespace gui2 {
00033 
00034 REGISTER_WIDGET(tree_view)
00035 
00036 ttree_view::ttree_view(const std::vector<tnode_definition>& node_definitions)
00037     : tscrollbar_container(2)
00038     , node_definitions_(node_definitions)
00039     , indention_step_size_(0)
00040     , need_layout_(false)
00041     , root_node_(new ttree_view_node(
00042           "root"
00043         , node_definitions_
00044         , NULL
00045         , *this
00046         , std::map<std::string, string_map>()))
00047     , selected_item_(NULL)
00048     , selection_change_callback_()
00049 {
00050     connect_signal<event::LEFT_BUTTON_DOWN>(
00051               boost::bind(
00052                     &ttree_view::signal_handler_left_button_down
00053                   , this
00054                   , _2)
00055             , event::tdispatcher::back_pre_child);
00056 }
00057 
00058 ttree_view_node& ttree_view::add_node(const std::string& id
00059         , const std::map<std::string /* widget id */, string_map>& data)
00060 {
00061     return get_root_node().add_child(id, data);
00062 }
00063 
00064 void ttree_view::remove_node(ttree_view_node* node)
00065 {
00066     assert(node && node != root_node_ && node->parent_node_);
00067     const tpoint node_size = node->get_size();
00068 
00069     boost::ptr_vector<ttree_view_node>::iterator itor =
00070                   node->parent_node_->children_.begin();
00071 
00072     for( ; itor != node->parent_node_->children_.end(); ++itor) {
00073         if(&*itor == node) {
00074             break;
00075         }
00076     }
00077 
00078     assert(itor != node->parent_node_->children_.end());
00079 
00080     node->parent_node_->children_.erase(itor);
00081 
00082     if(get_size() == tpoint(0, 0)) {
00083         return;
00084     }
00085 
00086     // Don't shrink the width, need to think about a good algorithm to do so.
00087     resize_content(0, -node_size.y);
00088 }
00089 
00090 void ttree_view::child_populate_dirty_list(twindow& caller
00091         , const std::vector<twidget*>& call_stack)
00092 {
00093     // Inherited.
00094     tscrollbar_container::child_populate_dirty_list(caller, call_stack);
00095 
00096     assert(root_node_);
00097     root_node_->impl_populate_dirty_list(caller, call_stack);
00098 }
00099 
00100 bool ttree_view::empty() const
00101 {
00102     return root_node_->empty();
00103 }
00104 
00105 void ttree_view::layout_children()
00106 {
00107     layout_children(false);
00108 }
00109 
00110 void ttree_view::resize_content(
00111           const int width_modification
00112         , const int height_modification)
00113 {
00114     DBG_GUI_L << LOG_HEADER << " current size " << content_grid()->get_size()
00115             << " width_modification " << width_modification
00116             << " height_modification " << height_modification
00117             << ".\n";
00118 
00119     if(content_resize_request(width_modification, height_modification)) {
00120 
00121         // Calculate new size.
00122         tpoint size = content_grid()->get_size();
00123         size.x += width_modification;
00124         size.y += height_modification;
00125 
00126         // Set new size.
00127         content_grid()->set_size(size);
00128 
00129         // Set status.
00130         need_layout_ = true;
00131         // If the content grows assume it "overwrites" the old content.
00132         if(width_modification < 0 || height_modification < 0) {
00133             set_dirty();
00134         }
00135         DBG_GUI_L << LOG_HEADER << " succeeded.\n";
00136     } else {
00137         DBG_GUI_L << LOG_HEADER << " failed.\n";
00138     }
00139 }
00140 
00141 void ttree_view::layout_children(const bool force)
00142 {
00143     assert(root_node_ && content_grid());
00144 
00145     if(need_layout_ || force) {
00146         root_node_->place(indention_step_size_
00147             , get_origin()
00148             , content_grid()->get_size().x);
00149         root_node_->set_visible_area(content_visible_area_);
00150 
00151         need_layout_ = false;
00152     }
00153 }
00154 
00155 void ttree_view::finalize_setup()
00156 {
00157     // Inherited.
00158     tscrollbar_container::finalize_setup();
00159 
00160     assert(content_grid());
00161     content_grid()->set_rows_cols(1, 1);
00162     content_grid()->set_child(
00163               root_node_
00164             , 0
00165             , 0
00166             , tgrid::VERTICAL_GROW_SEND_TO_CLIENT
00167                 | tgrid::HORIZONTAL_GROW_SEND_TO_CLIENT
00168             , 0);
00169 }
00170 
00171 const std::string& ttree_view::get_control_type() const
00172 {
00173     static const std::string type = "tree_view";
00174     return type;
00175 }
00176 
00177 void ttree_view::signal_handler_left_button_down(const event::tevent event)
00178 {
00179     DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
00180 
00181     get_window()->keyboard_capture(this);
00182 }
00183 
00184 } // namespace gui2
00185 
 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