From e28d0cd77588b827e14fde4d7934a38a4b3d4b45 Mon Sep 17 00:00:00 2001 From: John Lindal Date: Sun, 24 Mar 2024 14:27:56 -0700 Subject: [PATCH] jx_layout_editor: changing partition sizes should be undoable --- libjcore/code/JCoreLibVersion.h | 2 ++ libjcore/code/JPartition.cpp | 34 +++++++++++++++-------- libjcore/code/JPartition.h | 29 ++++++++++++++++++- todo-jxlayout | 5 ---- tools/jx_layout_editor/code/LayoutUndo.h | 3 +- tools/jx_layout_editor/code/Partition.cpp | 26 +++++++++++++++++ tools/jx_layout_editor/code/Partition.h | 6 ++++ 7 files changed, 86 insertions(+), 19 deletions(-) delete mode 100644 todo-jxlayout diff --git a/libjcore/code/JCoreLibVersion.h b/libjcore/code/JCoreLibVersion.h index 67f64d966..fee52c36a 100644 --- a/libjcore/code/JCoreLibVersion.h +++ b/libjcore/code/JCoreLibVersion.h @@ -68,6 +68,8 @@ static const char* kCurrentJCoreLibVersionStr = "4.1.0"; // JStyledText: // Fixed performance issues introduced in 4.0.0 caused by having multiple, // active FontIterators during some operations. +// JPartition: +// Added BeginResizeCompartments & EndResizeCompartments messages. // version 4.0.0: // *** Upgraded to C++20 diff --git a/libjcore/code/JPartition.cpp b/libjcore/code/JPartition.cpp index e76c85f6d..487fd3e6c 100644 --- a/libjcore/code/JPartition.cpp +++ b/libjcore/code/JPartition.cpp @@ -32,9 +32,9 @@ DeleteCompartmentObject Delete the specified compartment object. - BASE CLASS = none + BASE CLASS = virtual JBroadcaster - Copyright (C) 1996 by John Lindal. + Copyright (C) 1996-2024 by John Lindal. ******************************************************************************/ @@ -50,6 +50,11 @@ const JUtf8Byte kGeometryDataEndDelimiter = '\1'; // version 1: removed elastic index and min sizes +// JBroadcaster message types + +const JUtf8Byte* JPartition::kBeginResizeCompartments = "BeginResizeCompartments::JPartition"; +const JUtf8Byte* JPartition::kEndResizeCompartments = "EndResizeCompartments::JPartition"; + /****************************************************************************** Constructor (protected) @@ -77,11 +82,8 @@ JPartition::JPartition assert( compartmentCount == minSizes.GetItemCount() ); assert( elasticIndex <= compartmentCount ); - itsSizes = jnew JArray(sizes); - assert( itsSizes != nullptr ); - + itsSizes = jnew JArray(sizes); itsMinSizes = jnew JArray(minSizes); - assert( itsMinSizes != nullptr ); } /****************************************************************************** @@ -496,10 +498,12 @@ JPartition::PrepareToDrag assert( itsDragIndex > 0 ); assert( *maxDragCoord >= *minDragCoord ); + + Broadcast(BeginResizeCompartments()); } /****************************************************************************** - AdjustCompartmentsAfterDrag + AdjustCompartmentsAfterDrag (protected) Shift space from one compartment to the other. @@ -517,10 +521,12 @@ JPartition::AdjustCompartmentsAfterDrag itsDragMax - coord - kDragRegionHalfSize - 1); UpdateCompartmentSizes(); + + Broadcast(EndResizeCompartments()); } /****************************************************************************** - PrepareToDragAll + PrepareToDragAll (protected) Prepare to drag dividing line between two adjacent compartments and allow other compartments to shrink to get more space. @@ -568,10 +574,12 @@ JIndex i; *minDragCoord = itsDragMin; *maxDragCoord = itsDragMax; + + Broadcast(BeginResizeCompartments()); } /****************************************************************************** - AdjustCompartmentsAfterDragAll + AdjustCompartmentsAfterDragAll (protected) Expand one compartment at the expense of all the others. @@ -610,7 +618,7 @@ JIndex i; itsSizes->SetItem(i, newSizes.GetItem(i)); } itsSizes->SetItem(itsDragIndex+1, - itsSizes->GetItem(itsDragIndex+1) + reqSize); + itsSizes->GetItem(itsDragIndex+1) + reqSize); UpdateCompartmentSizes(); } @@ -637,7 +645,7 @@ JIndex i; assert( ok ); itsSizes->SetItem(itsDragIndex, - itsSizes->GetItem(itsDragIndex) + reqSize); + itsSizes->GetItem(itsDragIndex) + reqSize); for (i=itsDragIndex+1; i<=compartmentCount; i++) { itsSizes->SetItem(i, newSizes.GetItem(i-itsDragIndex)); @@ -645,6 +653,8 @@ JIndex i; UpdateCompartmentSizes(); } + + Broadcast(EndResizeCompartments()); } /****************************************************************************** @@ -673,7 +683,7 @@ JPartition::PTBoundsChanged() { JCoordinate trueDelta; const bool ok = CreateSpace(*itsSizes, *itsMinSizes, itsElasticIndex, - -delta, -delta, &newSizes, &trueDelta); + -delta, -delta, &newSizes, &trueDelta); assert( ok ); } *itsSizes = newSizes; diff --git a/libjcore/code/JPartition.h b/libjcore/code/JPartition.h index c2e13d7ce..478137f79 100644 --- a/libjcore/code/JPartition.h +++ b/libjcore/code/JPartition.h @@ -12,7 +12,7 @@ #include "JArray.h" -class JPartition +class JPartition : virtual public JBroadcaster { public: @@ -104,6 +104,33 @@ class JPartition JPartition(const JPartition&) = delete; JPartition& operator=(const JPartition&) = delete; + +public: + + // JBroadcaster messages + + static const JUtf8Byte* kBeginResizeCompartments; + static const JUtf8Byte* kEndResizeCompartments; + + class BeginResizeCompartments : public JBroadcaster::Message + { + public: + + BeginResizeCompartments() + : + JBroadcaster::Message(kBeginResizeCompartments) + { }; + }; + + class EndResizeCompartments : public JBroadcaster::Message + { + public: + + EndResizeCompartments() + : + JBroadcaster::Message(kEndResizeCompartments) + { }; + }; }; /****************************************************************************** diff --git a/todo-jxlayout b/todo-jxlayout deleted file mode 100644 index 6c5bfa058..000000000 --- a/todo-jxlayout +++ /dev/null @@ -1,5 +0,0 @@ -changing partition sizes should set "needs save" - JPartition must broadcast - -expand scrollbarSet vertically in main layout: too tall - horizontally becomes too wide diff --git a/tools/jx_layout_editor/code/LayoutUndo.h b/tools/jx_layout_editor/code/LayoutUndo.h index 6a0170a9a..1257fcfc3 100644 --- a/tools/jx_layout_editor/code/LayoutUndo.h +++ b/tools/jx_layout_editor/code/LayoutUndo.h @@ -22,7 +22,8 @@ class LayoutUndo : public JUndo kUnclassifiedType, kWindowResizeType, kArrowType, - kDragResizeType + kDragResizeType, + kResizePartitionType }; public: diff --git a/tools/jx_layout_editor/code/Partition.cpp b/tools/jx_layout_editor/code/Partition.cpp index f53f89ceb..8d99794c5 100644 --- a/tools/jx_layout_editor/code/Partition.cpp +++ b/tools/jx_layout_editor/code/Partition.cpp @@ -53,6 +53,8 @@ Partition::Partition InsertLayoutContainer(1, itsPartition->GetCompartment(1)); InsertLayoutContainer(2, itsPartition->GetCompartment(2)); + + PartitionX(); } Partition::Partition @@ -103,6 +105,29 @@ Partition::Partition { InsertLayoutContainer(i, itsPartition->GetCompartment(i)); } + + PartitionX(); +} + +// private + +void +Partition::PartitionX() +{ + itsUndo = nullptr; + + ListenTo(itsPartition, std::function([this](const JPartition::BeginResizeCompartments&) + { + assert( itsUndo == nullptr ); + itsUndo = jnew LayoutUndo(GetParentContainer()->GetDocument(), LayoutUndo::kResizePartitionType); + })); + + ListenTo(itsPartition, std::function([this](const JPartition::EndResizeCompartments&) + { + assert( itsUndo != nullptr ); + GetParentContainer()->NewUndo(itsUndo); + itsUndo = nullptr; + })); } /****************************************************************************** @@ -112,6 +137,7 @@ Partition::Partition Partition::~Partition() { + jdelete itsUndo; } /****************************************************************************** diff --git a/tools/jx_layout_editor/code/Partition.h b/tools/jx_layout_editor/code/Partition.h index 45da99c42..1c60b177b 100644 --- a/tools/jx_layout_editor/code/Partition.h +++ b/tools/jx_layout_editor/code/Partition.h @@ -12,6 +12,7 @@ class JXPartition; class PartitionPanel; +class LayoutUndo; class Partition : public MultiContainerWidget { @@ -61,6 +62,11 @@ class Partition : public MultiContainerWidget Type itsType; JXPartition* itsPartition; PartitionPanel* itsPanel; + LayoutUndo* itsUndo; + +private: + + void PartitionX(); }; #endif