diff --git a/FL/Fl_Tree.H b/FL/Fl_Tree.H index d9b2a47aa..c52b59156 100644 --- a/FL/Fl_Tree.H +++ b/FL/Fl_Tree.H @@ -1,4 +1,19 @@ // +// Fl_Tree.H -- This file is part of the Fl_Tree widget for FLTK +// +// Copyright 2009-2010 by Greg Ercolano. +// Copyright 2011-2026 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// #ifndef FL_TREE_H #define FL_TREE_H @@ -11,24 +26,6 @@ #include #include -////////////////////// -// FL/Fl_Tree.H -////////////////////// -// -// Fl_Tree -- This file is part of the Fl_Tree widget for FLTK -// Copyright (C) 2009-2010 by Greg Ercolano. -// -// This library is free software. Distribution and use rights are outlined in -// the file "COPYING" which should have been included with this file. If this -// file is missing or damaged, see the license at: -// -// https://www.fltk.org/COPYING.php -// -// Please see the following page on how to report bugs and issues: -// -// https://www.fltk.org/bugs.php -// - /// /// \file /// \brief This file contains the definitions of the Fl_Tree class @@ -293,7 +290,9 @@ class FL_EXPORT Fl_Tree : public Fl_Group { int _scrollbar_size; // size of scrollbar trough Fl_Tree_Item *_lastselect; // last selected item char _lastpushed; // FL_PUSH occurred on: 0=nothing, 1=open/close, 2=usericon, 3=label - void fix_scrollbar_order(); + int _auto_resize_children; // if true: resize children when the Fl_Tree container is resized + + void fix_scrollbar_order(); // internal: rearrange scrollbars in list of children protected: Fl_Scrollbar *_vscroll; ///< Vertical scrollbar @@ -321,6 +320,58 @@ public: void show_self(); void resize(int,int,int,int) override; + /** + Set mode to resize children when the Fl_Tree container widget is resized. + + The default value is \c false, meaning that child widgets within Fl_Tree + widgets are not automatically resized. + + This default behavior was changed in version 1.5.0 because resizing children + automatically led to unpredictable behavior depending on their positions and + sizes. This was caused by using the default resizing mechanism, Fl_Group::resize(), + and was fixed in version 1.5.0 by disabling automatic resizing of children. + + This method can be used to restore the 1.4.x behavior to resize children + automatically for backwards compatibility by calling + Fl_Tree::auto_resize_children(1). See example below. + + \b Note: use with care and only if required for strict backwards compatibility. + The resulting resize behavior can be \e surprising. If your development + target is FLTK 1.4 or lower you can't use this method (you may include + it inside conditional compilation for FLTK 1.5 and higher). + + Example code for strict backwards compatibility to 1.4.x or lower: + + \code + #include + Fl_Tree *tree = new Fl_Tree(0, 0, 400, 400); + #if FL_API_VERSION >= 10500 + tree->auto_resize_children(1); + #endif + \endcode + + \param[in] mode enable auto resizing if true (non-zero) + + \since 1.5.0 + \see int auto_resize_children() + */ + void auto_resize_children(int mode) { + _auto_resize_children = (mode) ? 1 : 0; + } + + /** + Returns whether auto resizing of children is active. + + \returns 0 (auto resizing \b off) or 1 ( \b on) + + \since 1.5.0 + + \see void auto_resize_children(int mode) for details + */ + int auto_resize_children() { + return _auto_resize_children; + } + /////////////////////// // root methods /////////////////////// diff --git a/src/Fl_Tree.cxx b/src/Fl_Tree.cxx index b888700ca..4f9c8a463 100644 --- a/src/Fl_Tree.cxx +++ b/src/Fl_Tree.cxx @@ -1,19 +1,8 @@ // - -#include -#include -#include - -#include -#include -#include - -////////////////////// -// Fl_Tree.cxx -////////////////////// +// Fl_Tree.cxx -- This file is part of the Fl_Tree widget for FLTK // -// Fl_Tree -- This file is part of the Fl_Tree widget for FLTK -// Copyright (C) 2009-2010 by Greg Ercolano. +// Copyright 2009-2010 by Greg Ercolano. +// Copyright 2011-2026 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -26,6 +15,14 @@ // https://www.fltk.org/bugs.php // +#include +#include +#include + +#include +#include +#include + // INTERNAL: scroller callback (hor+vert scroll) static void scroll_cb(Fl_Widget*,void *data) { ((Fl_Tree*)data)->redraw(); @@ -79,12 +76,13 @@ Fl_Tree::Fl_Tree(int X, int Y, int W, int H, const char *L) : Fl_Group(X,Y,W,H,L _root = new Fl_Tree_Item(this); _root->parent(0); // we are root of tree _root->label("ROOT"); - _item_focus = 0; - _callback_item = 0; - _callback_reason = FL_TREE_REASON_NONE; - _scrollbar_size = 0; // 0: uses Fl::scrollbar_size() - - _lastselect = 0; + _item_focus = 0; + _callback_item = 0; + _callback_reason = FL_TREE_REASON_NONE; + _scrollbar_size = 0; // 0: uses Fl::scrollbar_size() + _lastselect = nullptr; + _lastpushed = 0; + _auto_resize_children = 0; // don't resize children automatically box(FL_DOWN_BOX); color(FL_BACKGROUND2_COLOR, FL_SELECTION_COLOR); @@ -691,9 +689,13 @@ void Fl_Tree::calc_tree() { void Fl_Tree::resize(int X,int Y,int W, int H) { fix_scrollbar_order(); - Fl_Group::resize(X,Y,W,H); + if (auto_resize_children()) { // backwards compatibility to 1.4.x + Fl_Group::resize(X, Y, W, H); + init_sizes(); // remove stored sizes + } else { + Fl_Widget::resize(X, Y, W, H); + } calc_dimensions(); - init_sizes(); } /// Standard FLTK draw() method, handles drawing the tree widget.