Author: mordante
Date: Sun Dec  7 16:09:25 2008
New Revision: 31341

URL: http://svn.gna.org/viewcvs/wesnoth?rev=31341&view=rev
Log:
Enable the horizontal scrollbar.

Modified:
    trunk/src/gui/widgets/container.cpp
    trunk/src/gui/widgets/container.hpp
    trunk/src/gui/widgets/grid.cpp
    trunk/src/gui/widgets/grid.hpp

Modified: trunk/src/gui/widgets/container.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/container.cpp?rev=31341&r1=31340&r2=31341&view=diff
==============================================================================
--- trunk/src/gui/widgets/container.cpp (original)
+++ trunk/src/gui/widgets/container.cpp Sun Dec  7 16:09:25 2008
@@ -92,6 +92,40 @@
        set_layout_size(size);
 }
 
+void tcontainer_::layout_use_horizontal_scrollbar(const unsigned maximum_width)
+{
+       // Inherited.
+       twidget::layout_use_horizontal_scrollbar(maximum_width);
+
+       log_scope2(gui_layout, "tcontainer(" + get_control_type() + ") " + 
__func__);
+       
+       // 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_use_horizontal_scrollbar(maximum_width - border_space().y);
+       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
+               << " 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=31341&r1=31340&r2=31341&view=diff
==============================================================================
--- trunk/src/gui/widgets/container.hpp (original)
+++ trunk/src/gui/widgets/container.hpp Sun Dec  7 16:09:25 2008
@@ -70,10 +70,23 @@
         * others as a collection of multiple objects.
         */
        bool has_vertical_scrollbar() const
-
                { return grid_.has_vertical_scrollbar(); }
+
        /** Inherited from twidget. */
        void layout_use_vertical_scrollbar(const unsigned maximum_height);
+
+       /** 
+        * Inherited from twidget. 
+        * 
+        * Since we can't define a good default behaviour we force the 
inheriting
+        * classes to define this function. So inheriting classes act as one 
widget
+        * others as a collection of multiple objects.
+        */
+       bool has_horizontal_scrollbar() const
+               { return grid_.has_horizontal_scrollbar(); }
+
+       /** Inherited from twidget. */
+       void layout_use_horizontal_scrollbar(const unsigned maximum_width);
 
        /** Inherited from twidget. */
        void set_size(const tpoint& origin, const tpoint& size);

Modified: trunk/src/gui/widgets/grid.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/grid.cpp?rev=31341&r1=31340&r2=31341&view=diff
==============================================================================
--- trunk/src/gui/widgets/grid.cpp (original)
+++ trunk/src/gui/widgets/grid.cpp Sun Dec  7 16:09:25 2008
@@ -432,6 +432,85 @@
        set_layout_size(calculate_best_size());
 }
 
+bool tgrid::has_horizontal_scrollbar() const 
+{
+       for(std::vector<tchild>::const_iterator itor = children_.begin();
+                       itor != children_.end(); ++itor) {
+               // FIXME we should check per column and the entire column
+               // should have the flag!!!!
+               if(itor->widget() && 
itor->widget()->has_horizontal_scrollbar()) {
+                       return true;
+               } 
+       }
+       
+       // Inherit
+       return twidget::has_horizontal_scrollbar();
+}
+
+void tgrid::layout_use_horizontal_scrollbar(const unsigned maximum_width)
+{
+       // Inherited.
+       twidget::layout_use_horizontal_scrollbar(maximum_width);
+
+       log_scope2(gui_layout, std::string("tgrid ") + __func__);
+       DBG_G_L << "tgrid: maximum width " << maximum_width << ".\n";
+       
+       tpoint size = get_best_size();
+
+       // If we honoured the size or can't resize return the result.
+       if(size.x <= static_cast<int>(maximum_width) || 
!has_horizontal_scrollbar()) {
+               DBG_G_L << "tgrid: maximum width " 
+                       << maximum_width << " returning " << size << ".\n";
+               return;
+       }
+
+       // Try to resize.
+       
+       // The amount we're too wide.
+       const unsigned too_wide = size.x - maximum_width;
+       // The amount we reduced
+       unsigned reduced = 0;
+       for(size_t x = 0; x < cols_; ++x) {
+
+               const unsigned wanted_width = (too_wide - reduced) >= 
col_width_[x]
+                       ? 1 : col_width_[x] - (too_wide - reduced);
+
+               const unsigned width = column_use_horizontal_scrollbar(x, 
wanted_width);
+
+               if(width < col_width_[x]) {
+                       DBG_G_L << "tgrid: reduced " << col_width_[x] - width 
+                               << " pixels for column " << x << ".\n";
+
+                       reduced += col_width_[x] - width;
+                       col_width_[x] = width;
+               }
+               
+               if(reduced >= too_wide) {
+                       break;
+               }
+       }
+
+       size.x -= reduced;
+       if(reduced >= too_wide) {
+               DBG_G_L << "tgrid: maximum width " << maximum_width
+                       << " need to reduce " << too_wide 
+                       << " reduced " << reduced 
+                       << " resizing succeeded returning " << size.x << ".\n";
+       } else if(reduced == 0) {
+               DBG_G_L << "tgrid: maximum width " << maximum_width
+                       << " need to reduce " << too_wide 
+                       << " reduced " << reduced 
+                       << " resizing completely failed returning " << size.x 
<< ".\n";
+       } else {
+               DBG_G_L << "tgrid: maximum width " << maximum_width
+                       << " need to reduce " << too_wide 
+                       << " reduced " << reduced 
+                       << " resizing partly failed returning " << size.x << 
".\n";
+       }
+
+       set_layout_size(calculate_best_size());
+}
+
 void tgrid::set_size(const tpoint& origin, const tpoint& size)
 {
        log_scope2(gui_layout, "tgrid: set size");
@@ -869,6 +948,20 @@
        widget_->layout_use_vertical_scrollbar(maximum_height - 
border_space().y);
 }
 
+void tgrid::tchild::layout_use_horizontal_scrollbar(
+               const unsigned maximum_width)
+{
+
+       assert(widget_);
+
+       if(! widget_->has_horizontal_scrollbar()) {
+               return;
+       }
+
+       widget_->layout_use_horizontal_scrollbar(
+                       maximum_width - border_space().x);
+}
+
 const std::string& tgrid::tchild::id() const 
 { 
        assert(widget_);
@@ -935,6 +1028,31 @@
                << " returning " << required_height << ".\n";
 
        return required_height;
+}
+
+unsigned tgrid::column_use_horizontal_scrollbar(
+               const unsigned column, const unsigned maximum_width) 
+{
+       // The minimum width required.
+       unsigned required_width = 0;
+
+       for(size_t y = 0; y < rows_; ++y) {
+               tchild& cell = child(y, column);
+               cell.layout_use_horizontal_scrollbar(maximum_width);
+
+               const tpoint size(cell.get_best_size());
+
+               if(required_width == 0 
+                               || static_cast<size_t>(size.y) > 
required_width) {
+
+                       required_width = size.y;
+               }
+       }
+
+       DBG_G_L << "tgrid: maximum column width " << maximum_width 
+               << " returning " << required_width << ".\n";
+
+       return required_width;
 }
 
 } // namespace gui2

Modified: trunk/src/gui/widgets/grid.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/grid.hpp?rev=31341&r1=31340&r2=31341&view=diff
==============================================================================
--- trunk/src/gui/widgets/grid.hpp (original)
+++ trunk/src/gui/widgets/grid.hpp Sun Dec  7 16:09:25 2008
@@ -199,6 +199,12 @@
 
        /** Inherited from twidget. */
        void layout_use_vertical_scrollbar(const unsigned maximum_height);
+
+       /** Inherited from twidget. */
+       bool has_horizontal_scrollbar() const;
+
+       /** Inherited from twidget. */
+       void layout_use_horizontal_scrollbar(const unsigned maximum_width);
 
        /** Inherited from twidget. */
        void set_size(const tpoint& origin, const tpoint& size);
@@ -283,6 +289,9 @@
 
                /** Forwards layout_use_vertical_scrollbar() to the cell. */
                void layout_use_vertical_scrollbar(const unsigned 
maximum_height);
+
+               /** Forwards layout_use_horizontal_scrollbar() to the cell. */
+               void layout_use_horizontal_scrollbar(const unsigned 
maximum_width);
 
                /** Returns the id of the widget/ */
                const std::string& id() const;
@@ -427,6 +436,18 @@
        unsigned row_use_vertical_scrollbar(
                        const unsigned row, const unsigned maximum_height);
 
+       /**
+        * Gets the best width for a column.
+        *
+        * @param column              The column to get the best width for.
+        * @param maximum_width       The wanted maximum width.
+        *
+        * @returns                   The best width for a column, if possible
+        *                            smaller as the maximum.
+        */
+       unsigned column_use_horizontal_scrollbar(
+                       const unsigned column, const unsigned maximum_width);
+
 };
 
 } // namespace gui2


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

Reply via email to