Enable the horizontal scrollbar.

This commit is contained in:
Mark de Wever 2008-12-07 15:09:25 +00:00
parent c03a7fc55b
commit f336864a0c
4 changed files with 187 additions and 1 deletions

View file

@ -92,6 +92,40 @@ void tcontainer_::layout_use_vertical_scrollbar(const unsigned maximum_height)
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);

View file

@ -70,11 +70,24 @@ public:
* 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);

View file

@ -432,6 +432,85 @@ void tgrid::layout_use_vertical_scrollbar(const unsigned maximum_height)
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 @@ void tgrid::tchild::layout_use_vertical_scrollbar(const unsigned maximum_height)
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_);
@ -937,6 +1030,31 @@ unsigned tgrid::row_use_vertical_scrollbar(
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

View file

@ -200,6 +200,12 @@ public:
/** 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);
@ -284,6 +290,9 @@ private:
/** 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 @@ private:
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