FLTK logo

[master] 72415b5 - Fix rearranging children in Fl_Scroll (#546)

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] 72415b5 - Fix rearranging children in Fl_Scroll (#546) "Matthias Melcher" Nov 16, 2022  
 
commit 72415b534de36888e1a393847ec38544cdbbc3f7
Author:     Matthias Melcher <github@matthiasm.com>
AuthorDate: Thu Nov 17 08:34:06 2022 +0100
Commit:     GitHub <noreply@github.com>
CommitDate: Thu Nov 17 08:34:06 2022 +0100

    Fix rearranging children in Fl_Scroll (#546)
    
    
    Implement Fl_Scroll::on_move()
    Fix indexing in Fl_Group::on_move()

 FL/Fl_Scroll.H    |  4 ++--
 src/Fl_Group.cxx  | 15 +++++++--------
 src/Fl_Scroll.cxx | 18 ++++++++++++++++++
 3 files changed, 27 insertions(+), 10 deletions(-)

diff --git FL/Fl_Scroll.H FL/Fl_Scroll.H
index 1d49343..1917c96 100644
--- FL/Fl_Scroll.H
+++ FL/Fl_Scroll.H
@@ -146,8 +146,8 @@ protected:      //  (STR#1895)
 
 protected:
 
-  virtual int on_insert(Fl_Widget*, int);
-  // virtual int on_move(int, int);         // Not (yet) implemented. Do we need this?
+  int on_insert(Fl_Widget*, int); // override
+  int on_move(int, int); // override
   void fix_scrollbar_order();
   void bbox(int&,int&,int&,int&) const;
   void draw();
diff --git src/Fl_Group.cxx src/Fl_Group.cxx
index bcdb962..f9bbcb5 100644
--- src/Fl_Group.cxx
+++ src/Fl_Group.cxx
@@ -489,10 +489,9 @@ int Fl_Group::on_insert(Fl_Widget *candidate, int index) {
  By returning -1, Fl_Group::insert will not move the child.
 
  \param oldIndex the current index of the child that will be moved
- \param newIndex the new index of the child, counted with the old
-    child already removed
- \return index to position the child as planned
- \return a new index to force the child to a different position
+ \param newIndex the new index of the child
+ \return \p newIndex to position the child as planned
+ \return a different index to force the child to a different position
  \return -1 to keep the group from moving the child
  */
 int Fl_Group::on_move(int oldIndex, int newIndex) {
@@ -511,11 +510,11 @@ void Fl_Group::insert(Fl_Widget &o, int index) {
   if (o.parent()) {
     Fl_Group* g = o.parent();
     int n = g->find(o);
-    if (g == this) { // avoid expensive remove() and add() if we just move a widget within the group
-      if (index > n) index--;
+    if (g == this) {
+      // avoid expensive remove() and add() if we just move a widget within the group
       index = on_move(n, index);
-      if (index == n) return; // this includes (children_ == 1)
-      if (index >= children_ || index < 0) return;
+      if ((index == n) || (index < 0)) return; // this includes (children_ == 1)
+      if (index >= children_) index = children_ - 1;
       if (index > n)
         memmove(array_+n, array_+(n+1), (index-n) * sizeof(Fl_Widget*));
       else
diff --git src/Fl_Scroll.cxx src/Fl_Scroll.cxx
index f218915..fd7f4fb 100644
--- src/Fl_Scroll.cxx
+++ src/Fl_Scroll.cxx
@@ -105,6 +105,24 @@ int Fl_Scroll::on_insert(Fl_Widget *candidate, int index) {
 }
 
 /**
+ Change new position of a child before it is moved.
+
+ Fix new position if the new child is planned to be moved after the scrollbars.
+ We can assume that the scrollbars are always the last two children!
+
+ Fl_Group calls this when a widget is moved within the list of children.
+ We return a new index if the widget would be moved after the scrollbars.
+
+ \param oldIndex the current index of the child that will be moved
+ \param newIndex the new index of the child
+ \return new index, possibly corrected to avoid last two scrollbar entries
+ */
+int Fl_Scroll::on_move(int oldIndex, int newIndex) {
+  // Compensate index for the widget that will be removed before it re-inserted
+  return on_insert( child(oldIndex), newIndex+1 ) - 1;
+}
+
+/**
   Removes the widget at \p index from the group and deletes it.
 
   This method does nothing if \p index is out of bounds or
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.