Author: mordante
Date: Sun Feb 14 20:36:36 2010
New Revision: 41216

URL: http://svn.gna.org/viewcvs/wesnoth?rev=41216&view=rev
Log:
Various changes to the tree view node.

The main reason for the change is to better handle resize requests,
which the previous commits allowed. The code still needs some more love
and polishing later on.

Modified:
    trunk/src/gui/widgets/tree_view_node.cpp
    trunk/src/gui/widgets/tree_view_node.hpp

Modified: trunk/src/gui/widgets/tree_view_node.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/tree_view_node.cpp?rev=41216&r1=41215&r2=41216&view=diff
==============================================================================
--- trunk/src/gui/widgets/tree_view_node.cpp (original)
+++ trunk/src/gui/widgets/tree_view_node.cpp Sun Feb 14 20:36:36 2010
@@ -129,10 +129,318 @@
                        , parent_widget_
                        , data));
 
+       if(is_folded() || is_root_node()) {
+               return *itor;
+       }
+
        if(parent_widget_->get_size() == tpoint(0, 0)) {
                return *itor;
        }
-       /** @todo Test whether this resizing works properly. */
+
+
+       tpoint best_size = itor->get_best_size();
+       best_size.x += indention_level() * parent_widget_->indention_step_size_;
+       const tpoint size = parent_widget_->content_grid()->get_size();
+       const unsigned width_modification = best_size.x > size.x
+                       ? best_size.x - size.x
+                       : 0;
+
+       if(parent_widget_->content_resize_request(
+                         width_modification
+                       , best_size.y)) {
+
+               parent_widget_->root_node_->set_size(tpoint(
+                         size.x + width_modification
+                       , size.y + best_size.y));
+               parent_widget_->content_grid()->set_size(tpoint(
+                         size.x + width_modification
+                       , size.y + best_size.y));
+
+               parent_widget_->need_layout_ = true;
+       }
+
+       return *itor;
+}
+
+unsigned ttree_view_node::indention_level() const
+{
+       unsigned level = 0;
+
+       const ttree_view_node* node = this;
+       while(node->is_root_node()) {
+               node = &node->parent();
+               ++level;
+       }
+
+       return level;
+}
+
+
+ttree_view_node& ttree_view_node::parent()
+{
+       assert(!is_root_node());
+
+       return *parent_;
+}
+
+const ttree_view_node& ttree_view_node::parent() const
+{
+       assert(!is_root_node());
+
+       return *parent_;
+}
+
+bool ttree_view_node::is_folded() const
+{
+       return icon_ && icon_->get_value();
+}
+#if 0
+void ttree_view_node::fold(const bool /*recursive*/)
+{
+       // FIXME set state
+
+       parent_widget_->set_size(
+                         parent_widget_->get_origin()
+                       , parent_widget_->get_size());
+}
+
+void ttree_view_node::unfold(const texpand_mode /*mode*/)
+{
+       // FIXME set state
+
+       parent_widget_->set_size(
+                         parent_widget_->get_origin()
+                       , parent_widget_->get_size());
+}
+#endif
+
+void ttree_view_node::clear()
+{
+       /** @todo Also try to find the optimal width. */
+       unsigned height_reduction = 0;
+
+       if(!is_folded()) {
+               foreach(const ttree_view_node& node, children_) {
+                       height_reduction += node.get_size().y;
+               }
+       }
+
+       children_.clear();
+
+       if(height_reduction == 0) {
+               return;
+       }
+
+       if(parent_widget_->content_resize_request(0, -height_reduction)) {
+               const tpoint size = parent_widget_->root_node_->get_size();
+               parent_widget_->content_grid()->set_size(tpoint(
+                         size.x
+                       , size.y - height_reduction));
+               parent_widget_->root_node_->set_size(tpoint(
+                         size.x
+                       , size.y - height_reduction));
+
+               parent_widget_->need_layout_ = true;
+       }
+}
+
+struct ttree_view_node_implementation
+{
+
+       template<class W>
+       static W* find_at(
+                         typename tconst_duplicator<W, ttree_view_node>::type&
+                               tree_view_node
+                       , const tpoint& coordinate, const bool must_be_active)
+       {
+               if(W* widget =
+                               tree_view_node.grid_.find_at(coordinate, 
must_be_active)) {
+
+                       return widget;
+               }
+
+               if(tree_view_node.is_folded()) {
+                       return NULL;
+               }
+
+               typedef typename tconst_duplicator<W, ttree_view_node>::type 
thack;
+               foreach(thack& node, tree_view_node.children_) {
+                       if(W* widget = node.find_at(coordinate, 
must_be_active)) {
+                               return widget;
+                       }
+               }
+
+               return NULL;
+       }
+};
+
+twidget* ttree_view_node::find_at(
+                 const tpoint& coordinate
+               , const bool must_be_active)
+{
+       return ttree_view_node_implementation::find_at<twidget>(
+                       *this, coordinate, must_be_active);
+}
+
+const twidget* ttree_view_node::find_at(
+                 const tpoint& coordinate
+               , const bool must_be_active) const
+{
+       return ttree_view_node_implementation::find_at<const twidget>(
+                       *this, coordinate, must_be_active);
+}
+
+void ttree_view_node::impl_populate_dirty_list(twindow& caller
+               , const std::vector<twidget*>& call_stack)
+{
+       std::vector<twidget*> child_call_stack = call_stack;
+       grid_.populate_dirty_list(caller, child_call_stack);
+
+       if(is_folded()) {
+               return;
+       }
+
+       foreach(ttree_view_node& node, children_) {
+               std::vector<twidget*> child_call_stack = call_stack;
+               node.impl_populate_dirty_list(caller, child_call_stack);
+       }
+}
+
+tpoint ttree_view_node::calculate_best_size() const
+{
+       assert(parent_widget_);
+       return calculate_best_size(-1, parent_widget_->indention_step_size_);
+}
+
+tpoint ttree_view_node::calculate_best_size(const int indention_level
+               , const unsigned indention_step_size) const
+{
+       log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
+
+       tpoint best_size = grid_.get_best_size();
+       if(indention_level > 0) {
+               best_size.x += indention_level * indention_step_size;
+       }
+
+       if(is_folded()) {
+
+               DBG_GUI_L << LOG_HEADER
+                               << " Folded grid return own best size " << 
best_size << ".\n";
+               return best_size;
+       }
+
+       DBG_GUI_L << LOG_HEADER << " own grid best size " << best_size << ".\n";
+
+       foreach(const ttree_view_node& node, children_) {
+
+               if(node.grid_.get_visible() == twidget::INVISIBLE) {
+                       continue;
+               }
+
+               const tpoint node_size = 
node.calculate_best_size(indention_level + 1,
+                               indention_step_size);
+
+               best_size.y += node_size.y;
+               best_size.x = std::max(best_size.x, node_size.x);
+       }
+
+       DBG_GUI_L << LOG_HEADER << " result " << best_size << ".\n";
+       return best_size;
+}
+
+void ttree_view_node::set_origin(const tpoint& origin)
+{
+       // Inherited.
+       twidget::set_origin(origin);
+
+       assert(parent_widget_);
+       set_size(parent_widget_->indention_step_size_, origin, get_size().x);
+}
+
+void ttree_view_node::set_size(const tpoint& origin, const tpoint& size)
+{
+       // Inherited.
+       twidget::set_size(origin, size);
+
+       assert(parent_widget_);
+       set_size(parent_widget_->indention_step_size_, origin, size.x);
+}
+
+unsigned ttree_view_node::set_size(
+         const unsigned indention_step_size
+       , tpoint origin
+       , unsigned width)
+{
+       log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
+       DBG_GUI_L << LOG_HEADER << " origin " << origin << ".\n";
+
+       const unsigned offset = origin.y;
+       tpoint best_size = grid_.get_best_size();
+       best_size.x = width;
+       grid_.set_size(origin, best_size);
+
+       if(!is_root_node()) {
+               origin.x += indention_step_size;
+               width -= indention_step_size;
+       }
+       origin.y += best_size.y;
+
+       if(is_folded()) {
+               DBG_GUI_L << LOG_HEADER << " folded node done.\n";
+               return origin.y - offset;
+       }
+
+       DBG_GUI_L << LOG_HEADER << " set children.\n";
+       foreach(ttree_view_node& node, children_) {
+               origin.y += node.set_size(indention_step_size, origin, width);
+       }
+
+       // Inherited.
+       twidget::set_size(tpoint(width, origin.y - offset));
+
+       DBG_GUI_L << LOG_HEADER << " result " << ( origin.y - offset) << ".\n";
+       return origin.y - offset;
+}
+
+void ttree_view_node::set_visible_area(const SDL_Rect& area)
+{
+       log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
+       DBG_GUI_L << LOG_HEADER << " area " << area << ".\n";
+       grid_.set_visible_area(area);
+
+       if(is_folded()) {
+               DBG_GUI_L << LOG_HEADER << " folded node done.\n";
+               return;
+       }
+
+       foreach(ttree_view_node& node, children_) {
+               node.set_visible_area(area);
+       }
+}
+
+void ttree_view_node::impl_draw_children(surface& frame_buffer)
+{
+       if(is_root_node()) {
+               parent_widget_->layout();
+       }
+
+       grid_.draw_children(frame_buffer);
+
+       if(is_folded()) {
+               return;
+       }
+
+       foreach(ttree_view_node& node, children_) {
+               node.impl_draw_children(frame_buffer);
+       }
+}
+
+void ttree_view_node::signal_handler_left_button_click(
+               const event::tevent event)
+{
+       DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
+// FIXME rewrite as well
+
        if(parent_widget_->content_resize_request()) {
                parent_widget_->set_size(
                                  parent_widget_->get_origin()
@@ -142,238 +450,6 @@
                assert(window);
                window->invalidate_layout();
        }
-
-       return *itor;
-}
-
-ttree_view_node& ttree_view_node::parent()
-{
-       assert(!is_root_node());
-
-       return *parent_;
-}
-
-bool ttree_view_node::is_folded() const
-{
-       return icon_ && icon_->get_value();
-}
-#if 0
-void ttree_view_node::fold(const bool /*recursive*/)
-{
-       // FIXME set state
-
-       parent_widget_->set_size(
-                         parent_widget_->get_origin()
-                       , parent_widget_->get_size());
-}
-
-void ttree_view_node::unfold(const texpand_mode /*mode*/)
-{
-       // FIXME set state
-
-       parent_widget_->set_size(
-                         parent_widget_->get_origin()
-                       , parent_widget_->get_size());
-}
-#endif
-struct ttree_view_node_implementation
-{
-
-       template<class W>
-       static W* find_at(
-                         typename tconst_duplicator<W, ttree_view_node>::type&
-                               tree_view_node
-                       , const tpoint& coordinate, const bool must_be_active)
-       {
-               if(W* widget =
-                               tree_view_node.grid_.find_at(coordinate, 
must_be_active)) {
-
-                       return widget;
-               }
-
-               if(tree_view_node.is_folded()) {
-                       return NULL;
-               }
-
-               typedef typename tconst_duplicator<W, ttree_view_node>::type 
thack;
-               foreach(thack& node, tree_view_node.children_) {
-                       if(W* widget = node./*grid_.*/find_at(coordinate, 
must_be_active)) {
-                               return widget;
-                       }
-               }
-
-               return NULL;
-       }
-};
-
-twidget* ttree_view_node::find_at(
-                 const tpoint& coordinate
-               , const bool must_be_active)
-{
-       return ttree_view_node_implementation::find_at<twidget>(
-                       *this, coordinate, must_be_active);
-}
-
-const twidget* ttree_view_node::find_at(
-                 const tpoint& coordinate
-               , const bool must_be_active) const
-{
-       return ttree_view_node_implementation::find_at<const twidget>(
-                       *this, coordinate, must_be_active);
-}
-
-void ttree_view_node::impl_populate_dirty_list(twindow& caller
-               , const std::vector<twidget*>& call_stack)
-{
-       std::vector<twidget*> child_call_stack = call_stack;
-       grid_.populate_dirty_list(caller, child_call_stack);
-
-       if(is_folded()) {
-               return;
-       }
-
-       foreach(ttree_view_node& node, children_) {
-               std::vector<twidget*> child_call_stack = call_stack;
-               node.impl_populate_dirty_list(caller, child_call_stack);
-       }
-}
-
-tpoint ttree_view_node::calculate_best_size() const
-{
-       assert(parent_widget_);
-       return calculate_best_size(-1, parent_widget_->indention_step_size_);
-}
-
-tpoint ttree_view_node::calculate_best_size(const int indention_level
-               , const unsigned indention_step_size) const
-{
-       log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
-
-       tpoint best_size = grid_.get_best_size();
-       if(indention_level > 0) {
-               best_size.x += indention_level * indention_step_size;
-       }
-
-       if(is_folded()) {
-
-               DBG_GUI_L << LOG_HEADER
-                               << " Folded grid return own best size " << 
best_size << ".\n";
-               return best_size;
-       }
-
-       DBG_GUI_L << LOG_HEADER << " own grid best size " << best_size << ".\n";
-
-       foreach(const ttree_view_node& node, children_) {
-
-               if(node.grid_.get_visible() == twidget::INVISIBLE) {
-                       continue;
-               }
-
-               const tpoint node_size = 
node.calculate_best_size(indention_level + 1,
-                               indention_step_size);
-
-               best_size.y += node_size.y;
-               best_size.x = std::max(best_size.x, node_size.x);
-       }
-
-       DBG_GUI_L << LOG_HEADER << " result " << best_size << ".\n";
-       return best_size;
-}
-
-void ttree_view_node::set_origin(const tpoint& origin)
-{
-       // Inherited.
-       twidget::set_origin(origin);
-
-       assert(parent_widget_);
-       set_size(parent_widget_->indention_step_size_, origin, get_size().x);
-}
-
-void ttree_view_node::set_size(const tpoint& origin, const tpoint& size)
-{
-       // Inherited.
-       twidget::set_size(origin, size);
-
-       assert(parent_widget_);
-       set_size(parent_widget_->indention_step_size_, origin, size.x);
-}
-
-unsigned ttree_view_node::set_size(
-         const unsigned indention_step_size
-       , tpoint origin
-       , unsigned width)
-{
-       log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
-       DBG_GUI_L << LOG_HEADER << " origin " << origin << ".\n";
-
-       const unsigned offset = origin.y;
-       tpoint best_size = grid_.get_best_size();
-       best_size.x = width;
-       grid_.set_size(origin, best_size);
-
-       if(!is_root_node()) {
-               origin.x += indention_step_size;
-               width -= indention_step_size;
-       }
-       origin.y += best_size.y;
-
-       if(is_folded()) {
-               DBG_GUI_L << LOG_HEADER << " folded node done.\n";
-               return origin.y - offset;
-       }
-
-       DBG_GUI_L << LOG_HEADER << " set children.\n";
-       foreach(ttree_view_node& node, children_) {
-               origin.y += node.set_size(indention_step_size, origin, width);
-       }
-
-       DBG_GUI_L << LOG_HEADER << " result " << ( origin.y - offset) << ".\n";
-       return origin.y - offset;
-}
-
-void ttree_view_node::set_visible_area(const SDL_Rect& area)
-{
-       log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
-       DBG_GUI_L << LOG_HEADER << " area " << area << ".\n";
-       grid_.set_visible_area(area);
-
-       if(is_folded()) {
-               DBG_GUI_L << LOG_HEADER << " folded node done.\n";
-               return;
-       }
-
-       foreach(ttree_view_node& node, children_) {
-               node.set_visible_area(area);
-       }
-}
-
-void ttree_view_node::impl_draw_children(surface& frame_buffer)
-{
-       grid_.draw_children(frame_buffer);
-
-       if(is_folded()) {
-               return;
-       }
-
-       foreach(ttree_view_node& node, children_) {
-               node.impl_draw_children(frame_buffer);
-       }
-}
-
-void ttree_view_node::signal_handler_left_button_click(
-               const event::tevent event)
-{
-       DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
-
-       if(parent_widget_->content_resize_request()) {
-               parent_widget_->set_size(
-                                 parent_widget_->get_origin()
-                               , parent_widget_->get_size());
-       } else {
-               twindow *window = get_window();
-               assert(window);
-               window->invalidate_layout();
-       }
 }
 
 void ttree_view_node::signal_handler_label_left_button_click(

Modified: trunk/src/gui/widgets/tree_view_node.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/tree_view_node.hpp?rev=41216&r1=41215&r2=41216&view=diff
==============================================================================
--- trunk/src/gui/widgets/tree_view_node.hpp (original)
+++ trunk/src/gui/widgets/tree_view_node.hpp Sun Feb 14 20:36:36 2010
@@ -59,10 +59,13 @@
 
        bool is_root_node() const { return parent_ == NULL; }
 
+       unsigned indention_level() const;
+
        /**
         * Returns the parent node, can't be used on the root node.
         */
        ttree_view_node& parent();
+       const ttree_view_node& parent() const;
 
        bool empty() const { return children_.empty(); }
 
@@ -105,7 +108,7 @@
 
        size_t size() const { return children_.size(); }
 
-       void clear() { children_.clear(); }
+       void clear();
 
 private:
 
@@ -150,6 +153,8 @@
                        , tpoint origin
                        , unsigned width);
 
+       using twidget::set_size;
+
        void set_visible_area(const SDL_Rect& area);
 
        void impl_draw_children(surface& frame_buffer);


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to