Skip to content

Commit e28d0cd

Browse files
committed
jx_layout_editor: changing partition sizes should be undoable
1 parent c46dbb6 commit e28d0cd

File tree

7 files changed

+86
-19
lines changed

7 files changed

+86
-19
lines changed

libjcore/code/JCoreLibVersion.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ static const char* kCurrentJCoreLibVersionStr = "4.1.0";
6868
// JStyledText:
6969
// Fixed performance issues introduced in 4.0.0 caused by having multiple,
7070
// active FontIterators during some operations.
71+
// JPartition:
72+
// Added BeginResizeCompartments & EndResizeCompartments messages.
7173

7274
// version 4.0.0:
7375
// *** Upgraded to C++20

libjcore/code/JPartition.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
DeleteCompartmentObject
3333
Delete the specified compartment object.
3434
35-
BASE CLASS = none
35+
BASE CLASS = virtual JBroadcaster
3636
37-
Copyright (C) 1996 by John Lindal.
37+
Copyright (C) 1996-2024 by John Lindal.
3838
3939
******************************************************************************/
4040

@@ -50,6 +50,11 @@ const JUtf8Byte kGeometryDataEndDelimiter = '\1';
5050

5151
// version 1: removed elastic index and min sizes
5252

53+
// JBroadcaster message types
54+
55+
const JUtf8Byte* JPartition::kBeginResizeCompartments = "BeginResizeCompartments::JPartition";
56+
const JUtf8Byte* JPartition::kEndResizeCompartments = "EndResizeCompartments::JPartition";
57+
5358
/******************************************************************************
5459
Constructor (protected)
5560
@@ -77,11 +82,8 @@ JPartition::JPartition
7782
assert( compartmentCount == minSizes.GetItemCount() );
7883
assert( elasticIndex <= compartmentCount );
7984

80-
itsSizes = jnew JArray<JCoordinate>(sizes);
81-
assert( itsSizes != nullptr );
82-
85+
itsSizes = jnew JArray<JCoordinate>(sizes);
8386
itsMinSizes = jnew JArray<JCoordinate>(minSizes);
84-
assert( itsMinSizes != nullptr );
8587
}
8688

8789
/******************************************************************************
@@ -496,10 +498,12 @@ JPartition::PrepareToDrag
496498

497499
assert( itsDragIndex > 0 );
498500
assert( *maxDragCoord >= *minDragCoord );
501+
502+
Broadcast(BeginResizeCompartments());
499503
}
500504

501505
/******************************************************************************
502-
AdjustCompartmentsAfterDrag
506+
AdjustCompartmentsAfterDrag (protected)
503507
504508
Shift space from one compartment to the other.
505509
@@ -517,10 +521,12 @@ JPartition::AdjustCompartmentsAfterDrag
517521
itsDragMax - coord - kDragRegionHalfSize - 1);
518522

519523
UpdateCompartmentSizes();
524+
525+
Broadcast(EndResizeCompartments());
520526
}
521527

522528
/******************************************************************************
523-
PrepareToDragAll
529+
PrepareToDragAll (protected)
524530
525531
Prepare to drag dividing line between two adjacent compartments and
526532
allow other compartments to shrink to get more space.
@@ -568,10 +574,12 @@ JIndex i;
568574

569575
*minDragCoord = itsDragMin;
570576
*maxDragCoord = itsDragMax;
577+
578+
Broadcast(BeginResizeCompartments());
571579
}
572580

573581
/******************************************************************************
574-
AdjustCompartmentsAfterDragAll
582+
AdjustCompartmentsAfterDragAll (protected)
575583
576584
Expand one compartment at the expense of all the others.
577585
@@ -610,7 +618,7 @@ JIndex i;
610618
itsSizes->SetItem(i, newSizes.GetItem(i));
611619
}
612620
itsSizes->SetItem(itsDragIndex+1,
613-
itsSizes->GetItem(itsDragIndex+1) + reqSize);
621+
itsSizes->GetItem(itsDragIndex+1) + reqSize);
614622

615623
UpdateCompartmentSizes();
616624
}
@@ -637,14 +645,16 @@ JIndex i;
637645
assert( ok );
638646

639647
itsSizes->SetItem(itsDragIndex,
640-
itsSizes->GetItem(itsDragIndex) + reqSize);
648+
itsSizes->GetItem(itsDragIndex) + reqSize);
641649
for (i=itsDragIndex+1; i<=compartmentCount; i++)
642650
{
643651
itsSizes->SetItem(i, newSizes.GetItem(i-itsDragIndex));
644652
}
645653

646654
UpdateCompartmentSizes();
647655
}
656+
657+
Broadcast(EndResizeCompartments());
648658
}
649659

650660
/******************************************************************************
@@ -673,7 +683,7 @@ JPartition::PTBoundsChanged()
673683
{
674684
JCoordinate trueDelta;
675685
const bool ok = CreateSpace(*itsSizes, *itsMinSizes, itsElasticIndex,
676-
-delta, -delta, &newSizes, &trueDelta);
686+
-delta, -delta, &newSizes, &trueDelta);
677687
assert( ok );
678688
}
679689
*itsSizes = newSizes;

libjcore/code/JPartition.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "JArray.h"
1414

15-
class JPartition
15+
class JPartition : virtual public JBroadcaster
1616
{
1717
public:
1818

@@ -104,6 +104,33 @@ class JPartition
104104

105105
JPartition(const JPartition&) = delete;
106106
JPartition& operator=(const JPartition&) = delete;
107+
108+
public:
109+
110+
// JBroadcaster messages
111+
112+
static const JUtf8Byte* kBeginResizeCompartments;
113+
static const JUtf8Byte* kEndResizeCompartments;
114+
115+
class BeginResizeCompartments : public JBroadcaster::Message
116+
{
117+
public:
118+
119+
BeginResizeCompartments()
120+
:
121+
JBroadcaster::Message(kBeginResizeCompartments)
122+
{ };
123+
};
124+
125+
class EndResizeCompartments : public JBroadcaster::Message
126+
{
127+
public:
128+
129+
EndResizeCompartments()
130+
:
131+
JBroadcaster::Message(kEndResizeCompartments)
132+
{ };
133+
};
107134
};
108135

109136
/******************************************************************************

todo-jxlayout

Lines changed: 0 additions & 5 deletions
This file was deleted.

tools/jx_layout_editor/code/LayoutUndo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class LayoutUndo : public JUndo
2222
kUnclassifiedType,
2323
kWindowResizeType,
2424
kArrowType,
25-
kDragResizeType
25+
kDragResizeType,
26+
kResizePartitionType
2627
};
2728

2829
public:

tools/jx_layout_editor/code/Partition.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Partition::Partition
5353

5454
InsertLayoutContainer(1, itsPartition->GetCompartment(1));
5555
InsertLayoutContainer(2, itsPartition->GetCompartment(2));
56+
57+
PartitionX();
5658
}
5759

5860
Partition::Partition
@@ -103,6 +105,29 @@ Partition::Partition
103105
{
104106
InsertLayoutContainer(i, itsPartition->GetCompartment(i));
105107
}
108+
109+
PartitionX();
110+
}
111+
112+
// private
113+
114+
void
115+
Partition::PartitionX()
116+
{
117+
itsUndo = nullptr;
118+
119+
ListenTo(itsPartition, std::function([this](const JPartition::BeginResizeCompartments&)
120+
{
121+
assert( itsUndo == nullptr );
122+
itsUndo = jnew LayoutUndo(GetParentContainer()->GetDocument(), LayoutUndo::kResizePartitionType);
123+
}));
124+
125+
ListenTo(itsPartition, std::function([this](const JPartition::EndResizeCompartments&)
126+
{
127+
assert( itsUndo != nullptr );
128+
GetParentContainer()->NewUndo(itsUndo);
129+
itsUndo = nullptr;
130+
}));
106131
}
107132

108133
/******************************************************************************
@@ -112,6 +137,7 @@ Partition::Partition
112137

113138
Partition::~Partition()
114139
{
140+
jdelete itsUndo;
115141
}
116142

117143
/******************************************************************************

tools/jx_layout_editor/code/Partition.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class JXPartition;
1414
class PartitionPanel;
15+
class LayoutUndo;
1516

1617
class Partition : public MultiContainerWidget
1718
{
@@ -61,6 +62,11 @@ class Partition : public MultiContainerWidget
6162
Type itsType;
6263
JXPartition* itsPartition;
6364
PartitionPanel* itsPanel;
65+
LayoutUndo* itsUndo;
66+
67+
private:
68+
69+
void PartitionX();
6470
};
6571

6672
#endif

0 commit comments

Comments
 (0)