mail[Wesnoth-commits] r33676 - /trunk/src/gui/widgets/


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by koraq on March 15, 2009 - 14:46:
Author: mordante
Date: Sun Mar 15 14:46:39 2009
New Revision: 33676

URL: http://svn.gna.org/viewcvs/wesnoth?rev=33676&view=rev
Log:
Add another way to try to fit the widgets.

This way a widget will be tried with wrapping and horizontal scrollbars in one
run. If that fails a second run will try to shrink them. The code calling this
way will be added later.

Modified:
    trunk/src/gui/widgets/container.cpp
    trunk/src/gui/widgets/container.hpp
    trunk/src/gui/widgets/control.cpp
    trunk/src/gui/widgets/control.hpp
    trunk/src/gui/widgets/generator.hpp
    trunk/src/gui/widgets/grid.cpp
    trunk/src/gui/widgets/grid.hpp
    trunk/src/gui/widgets/scrollbar_container.cpp
    trunk/src/gui/widgets/scrollbar_container.hpp
    trunk/src/gui/widgets/widget.hpp

Modified: trunk/src/gui/widgets/container.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/container.cpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/container.cpp (original)
+++ trunk/src/gui/widgets/container.cpp Sun Mar 15 14:46:39 2009
@@ -136,6 +136,46 @@
        set_layout_size(size);
 }
 
+void tcontainer_::layout_fit_width(const unsigned maximum_width, 
+               const tfit_flags flags)
+{
+       // Inherited.
+       twidget::layout_fit_width(maximum_width, flags);
+
+       log_scope2(gui_layout, "tcontainer(" + get_control_type() + ") " + 
__func__);
+       DBG_G_L << "tcontainer(" + get_control_type() + "):"
+               << " maximum_width " << maximum_width
+               << " flags " << flags
+               << ".\n";
+
+       // We need a copy and adjust if for the borders, no use to ask the 
grid for
+       // the best size if it won't fit in the end due to our borders.
+       const tpoint border_size = border_space();
+
+       // Calculate the best size
+       grid_.layout_fit_width(maximum_width - border_space().y, flags);
+       tpoint size = grid_.get_best_size();
+
+       // If the best size has a value of 0 it's means no limit so don't add 
the
+       // border_size might set a very small best size.
+       if(size.x) {
+               size.x += border_size.x;
+       }
+
+       if(size.y) {
+               size.y += border_size.y;
+       }
+
+       DBG_G_L << "tcontainer(" + get_control_type() + "):"
+               << " maximum_width " << maximum_width
+               << " flags " << flags
+               << " border size " << border_size
+               << " returning " << size
+               << ".\n";
+
+       set_layout_size(size);
+}
+
 void tcontainer_::set_size(const tpoint& origin, const tpoint& size)
 {
        tcontrol::set_size(origin, size);

Modified: trunk/src/gui/widgets/container.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/container.hpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/container.hpp (original)
+++ trunk/src/gui/widgets/container.hpp Sun Mar 15 14:46:39 2009
@@ -89,6 +89,10 @@
        void layout_use_horizontal_scrollbar(const unsigned maximum_width);
 
        /** Inherited from twidget. */
+       void layout_fit_width(const unsigned maximum_width, 
+                       const tfit_flags flags);
+
+       /** Inherited from twidget. */
        void set_size(const tpoint& origin, const tpoint& size);
 
        /***** ***** ***** ***** Inherited ***** ***** ***** *****/

Modified: trunk/src/gui/widgets/control.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/control.cpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/control.cpp (original)
+++ trunk/src/gui/widgets/control.cpp Sun Mar 15 14:46:39 2009
@@ -173,13 +173,56 @@
 
        /** @todo handle the tooltip properly. */
 
-       std::cerr << "Shrink from : " << get_best_size().x 
-               << " to " << maximum_width << ".\n";
-
        shrunken_ = true;
        set_layout_size(tpoint(maximum_width, get_best_size().y));
 
        assert(static_cast<unsigned>(get_best_size().x) == maximum_width);
+}
+
+void tcontrol::layout_fit_width(const unsigned maximum_width,
+               const tfit_flags flags)
+{
+       assert(get_visible() != twidget::INVISIBLE);
+
+       log_scope2(gui_layout, 
+                       "tcontrol(" + get_control_type() + ") " + __func__);
+       DBG_G_L << "maximum_width " << maximum_width
+                       << " flags " << flags
+                       << ".\n";
+
+       // Already fits.
+       if(get_best_size().x <= static_cast<int>(maximum_width)) {
+               DBG_G_L << "Already fits.\n";
+               return;
+       }
+
+       // Wrap.
+       if((flags & twidget::WRAP) && can_wrap()) {
+               layout_wrap(maximum_width);
+
+               if(get_best_size().x <= static_cast<int>(maximum_width)) {
+                       DBG_G_L << "Success: Wrapped.\n";
+                       return;
+               }
+       }
+
+       // Horizontal scrollbar.
+       if((flags & twidget::SCROLLBAR) && has_horizontal_scrollbar()) {
+               layout_use_horizontal_scrollbar(maximum_width);
+
+               if(get_best_size().x <= static_cast<int>(maximum_width)) {
+                       DBG_G_L << "Success: Horizontal scrollbar.\n";
+                       return;
+               }
+       }
+
+       // Shrink.
+       if((flags & twidget::SHRINK) && can_shrink_width()) {
+               layout_shrink_width(maximum_width);
+               DBG_G_L << "Success: Shrunken.\n";
+       }
+       
+       DBG_G_L << "Failed.\n";
 }
 
 tpoint tcontrol::calculate_best_size() const

Modified: trunk/src/gui/widgets/control.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/control.hpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/control.hpp (original)
+++ trunk/src/gui/widgets/control.hpp Sun Mar 15 14:46:39 2009
@@ -159,6 +159,10 @@
        /** Inherited from twidget. */
        void layout_shrink_width(const unsigned maximum_width);
 
+       /** Inherited from twidget. */
+       void layout_fit_width(const unsigned maximum_width,
+                       const tfit_flags flags);
+
 protected:
        /** Inherited from twidget. */
        tpoint calculate_best_size() const;

Modified: trunk/src/gui/widgets/generator.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/generator.hpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/generator.hpp (original)
+++ trunk/src/gui/widgets/generator.hpp Sun Mar 15 14:46:39 2009
@@ -203,6 +203,10 @@
        virtual void layout_init2(const bool /*full_initialization*/) = 0;
 
        /** Inherited from twidget. */
+       virtual void layout_fit_width(const unsigned /*maximum_width */, 
+                       const tfit_flags /*flags*/) { /* Make pure virtual? 
*/ }
+
+       /** Inherited from twidget. */
        virtual tpoint calculate_best_size() const = 0;
 
        /** Inherited from twidget. */

Modified: trunk/src/gui/widgets/grid.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/grid.cpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/grid.cpp (original)
+++ trunk/src/gui/widgets/grid.cpp Sun Mar 15 14:46:39 2009
@@ -535,6 +535,110 @@
        set_layout_size(calculate_best_size());
 }
 
+void tgrid::layout_fit_width(const unsigned maximum_width, 
+               const tfit_flags flags)
+{
+       log_scope2(gui_layout, std::string("tgrid ") + __func__);
+       DBG_G_L << "tgrid: maximum width " << maximum_width
+                       << " flags " << flags
+                       << ".\n";
+
+       // Do we already fit?
+       if(get_best_size().x <= static_cast<int>(maximum_width)) {
+               DBG_G_L << "Already fits.\n";
+               return;
+       }
+
+       // Try to reduce our size; friendly first.
+       tfit_flags current_flags = 
+                       static_cast<tfit_flags>(flags & (WRAP | SCROLLBAR));
+
+       if(current_flags) {
+               impl_layout_fit_width(maximum_width, current_flags);
+               if(get_best_size().x <= static_cast<int>(maximum_width)) {
+                       DBG_G_L << "Success: Friendly.\n";
+                       return;
+               }
+       }
+
+       // Try to reduce our size; the aggresive way.
+       current_flags = static_cast<tfit_flags>(flags & SHRINK);
+       if(current_flags) {
+               impl_layout_fit_width(maximum_width, current_flags);
+               if(get_best_size().x <= static_cast<int>(maximum_width)) {
+                       DBG_G_L << "Success: Forced.\n";
+                       return;
+               }
+       }
+
+       DBG_G_L << "Failed.\n";
+       assert(flags != FORCE 
+                       || static_cast<unsigned>(get_best_size().x) <= 
maximum_width);
+}
+
+void tgrid::impl_layout_fit_width(const unsigned maximum_width, 
+               const tfit_flags flags)
+{
+       log_scope2(gui_layout, std::string("tgrid ") + __func__);
+       DBG_G_L << "tgrid: maximum width " << maximum_width
+                       << " flags " << flags
+                       << ".\n";
+
+       for(unsigned col = 0; col < cols_; ++col) {
+
+               // Find out how much size there still has to be reduced.
+               unsigned current_size = get_best_size().x;
+               int too_wide = current_size - maximum_width;
+
+               DBG_G_L << "tgrid: col " << col
+                               << " maximum_width " << maximum_width
+                               << " current_size " << current_size
+                               << " too_wide " << too_wide
+                               << ".\n";
+
+               if(too_wide < 0) {
+                       DBG_G_L << "tgrid: col " << col << " fits stop 
searching.\n";
+                       break;
+               }
+
+               // If the current column too small to be reduced the entire 
width
+               // skip it. This can be optimized later by only trying to 
reduce
+               // it partially, especially if multiple columns can be 
reduced.
+               if(static_cast<int>(col_width_[col]) < too_wide) {
+                       DBG_G_L << "tgrid: col " << col
+                                       << " col_width_ " << col_width_[col]
+                                       << " too small to be resized.\n";
+                       continue;
+               }
+
+               // Try to reduce all widgets too wide in this column. We 
don't care
+               // whether or not we succeeded, if we fail there might be the
+               // aggressive run that finishes the task, but that means we 
tried
+               // nicely before.
+               const unsigned wanted_width = col_width_[col] - too_wide;
+
+               DBG_G_L << "tgrid: col " << col
+                               << " try to fit the rows in " << wanted_width
+                               << ".\n";
+
+               for(unsigned row = 0; row < rows_; ++row) {
+                       tchild& chld = child(row, col);
+                       const twidget* widget = chld.widget();
+                       assert(widget);
+
+                       if(widget->get_visible() != twidget::INVISIBLE
+                                       && widget->get_best_size().x 
+                                       > static_cast<int>(wanted_width)) {
+
+                               chld.layout_fit_width(wanted_width, flags);
+                       }
+               }
+       }
+
+       // Update the status.
+       set_layout_size(calculate_best_size());
+}
+
 void tgrid::set_size(const tpoint& origin, const tpoint& size)
 {
        log_scope2(gui_layout, "tgrid: set size");
@@ -1015,6 +1119,28 @@
 
        widget_->layout_use_horizontal_scrollbar(
                        maximum_width - border_space().x);
+}
+
+void tgrid::tchild::layout_fit_width(const unsigned maximum_width, 
+               const tfit_flags flags)
+{
+       assert(widget_);
+       assert(widget_->get_visible() != twidget::INVISIBLE);
+
+       log_scope2(gui_layout, std::string("tgrid::tchild ") + __func__);
+       DBG_G_L << "tgrid: maximum width " << maximum_width
+                       << " flags " << flags
+                       << ".\n";
+
+       widget_->layout_fit_width(maximum_width - border_space().x, flags);
+       if(widget_->get_best_size().x 
+                       + border_space().x
+                       <= static_cast<int>(maximum_width)) {
+
+               DBG_G_L << "Succeeded.\n";
+       } else {
+               DBG_G_L << "Failed.\n";
+       }
 }
 
 const std::string& tgrid::tchild::id() const

Modified: trunk/src/gui/widgets/grid.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/grid.hpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/grid.hpp (original)
+++ trunk/src/gui/widgets/grid.hpp Sun Mar 15 14:46:39 2009
@@ -208,6 +208,14 @@
        void layout_use_horizontal_scrollbar(const unsigned maximum_width);
 
        /** Inherited from twidget. */
+       void layout_fit_width(const unsigned maximum_width, 
+                       const tfit_flags flags);
+private:
+       /** Internal helper for layout_fit_width. */
+       void impl_layout_fit_width(const unsigned maximum_width, 
+                       const tfit_flags flags);
+public:        
+       /** Inherited from twidget. */
        void set_size(const tpoint& origin, const tpoint& size);
 
        /***** ***** ***** ***** Inherited ***** ***** ***** *****/
@@ -296,6 +304,10 @@
                /** Forwards layout_use_horizontal_scrollbar() to the cell. */
                void layout_use_horizontal_scrollbar(const unsigned 
maximum_width);
 
+               /** Forwards layout_fit_width() to the cell. */
+               void layout_fit_width(const unsigned maximum_width, 
+                               const tfit_flags flags);
+
                /** Returns the id of the widget/ */
                const std::string& id() const;
 

Modified: trunk/src/gui/widgets/scrollbar_container.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/scrollbar_container.cpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/scrollbar_container.cpp (original)
+++ trunk/src/gui/widgets/scrollbar_container.cpp Sun Mar 15 14:46:39 2009
@@ -197,6 +197,52 @@
        set_layout_size(size);
 }
 
+void tscrollbar_container::layout_fit_width(const unsigned maximum_width, 
+               const tfit_flags flags)
+{
+       assert(get_visible() != twidget::INVISIBLE);
+
+       log_scope2(gui_layout, 
+                       "tscrollbar_container(" + get_control_type() + ") " + 
__func__);
+       DBG_G_L << "maximum_width " << maximum_width
+                       << " flags " << flags
+                       << ".\n";
+
+       // Already fits.
+       if(get_best_size().x <= static_cast<int>(maximum_width)) {
+               DBG_G_L << "Already fits.\n";
+               return;
+       }
+
+       // Wrap.
+       if((flags & twidget::WRAP) && can_wrap()) {
+               layout_wrap(maximum_width);
+
+               if(get_best_size().x <= static_cast<int>(maximum_width)) {
+                       DBG_G_L << "Success: Wrapped.\n";
+                       return;
+               }
+       }
+
+       // Horizontal scrollbar.
+       if((flags & twidget::SCROLLBAR) && has_horizontal_scrollbar()) {
+               layout_use_horizontal_scrollbar(maximum_width);
+
+               if(get_best_size().x <= static_cast<int>(maximum_width)) {
+                       DBG_G_L << "Success: Horizontal scrollbar.\n";
+                       return;
+               }
+       }
+/* Can't shrink but should not be needed as well
+       // Shrink.
+       if((flags & twidget::TRUNCATE) && can_shrink_width()) {
+               layout_shrink_width(maximum_width);
+               DBG_G_L << "Success: Shrunken.\n";
+       }
+*/     
+       DBG_G_L << "Failed.\n";
+}
+
 tpoint tscrollbar_container::calculate_best_size() const
 {
        log_scope2(gui_layout,

Modified: trunk/src/gui/widgets/scrollbar_container.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/scrollbar_container.hpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/scrollbar_container.hpp (original)
+++ trunk/src/gui/widgets/scrollbar_container.hpp Sun Mar 15 14:46:39 2009
@@ -94,6 +94,10 @@
        /** Inherited from tcontainer_. */
        void layout_use_horizontal_scrollbar(const unsigned maximum_width);
 
+       /** Inherited from tcontainer. */
+       void layout_fit_width(const unsigned maximum_width, 
+                       const tfit_flags flags);
+
 private:
        /** Inherited from tcontainer_. */
        tpoint calculate_best_size() const;

Modified: trunk/src/gui/widgets/widget.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/widget.hpp?rev=33676&r1=33675&r2=33676&view=diff
==============================================================================
--- trunk/src/gui/widgets/widget.hpp (original)
+++ trunk/src/gui/widgets/widget.hpp Sun Mar 15 14:46:39 2009
@@ -242,6 +242,26 @@
        virtual void layout_shrink_width(const unsigned /*maximum_width*/)
                { assert(can_shrink_width()); }
 
+       /** Flags to decide how to try to fit the widget in the layout. */
+       enum tfit_flags {
+                 WRAP      = 1 /**< Try to wrap the widget. */
+               , SCROLLBAR = 2 /**< Try to use scrollbars. */
+               , SHRINK    = 4 /**< Try to shrink the widget. */
+               , FORCE     = 7 /**< Try all of the above. */
+       };
+
+       /**
+        * Fits the layout in the wanted width.
+        *
+        * @param maximum_width       The maximum width of the window.
+        * @param flags               The flags for the fitting.
+        */
+       virtual void layout_fit_width(const unsigned /*maximum_width*/, 
+                       const tfit_flags /*flags*/) 
+       {
+               //FIXME should be pure abstract but gives linker errors.
+       }
+
        /**
         * Does the widget have a vertical scrollbar.
         *




Related Messages


Powered by MHonArc, Updated Sun Mar 15 16:20:15 2009