Skip to content

Commit

Permalink
jx_layout_editor: changing partition sizes should be undoable
Browse files Browse the repository at this point in the history
  • Loading branch information
jafl committed Mar 24, 2024
1 parent c46dbb6 commit e28d0cd
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 19 deletions.
2 changes: 2 additions & 0 deletions libjcore/code/JCoreLibVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 22 additions & 12 deletions libjcore/code/JPartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
******************************************************************************/

Expand All @@ -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)
Expand Down Expand Up @@ -77,11 +82,8 @@ JPartition::JPartition
assert( compartmentCount == minSizes.GetItemCount() );
assert( elasticIndex <= compartmentCount );

itsSizes = jnew JArray<JCoordinate>(sizes);
assert( itsSizes != nullptr );

itsSizes = jnew JArray<JCoordinate>(sizes);
itsMinSizes = jnew JArray<JCoordinate>(minSizes);
assert( itsMinSizes != nullptr );
}

/******************************************************************************
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand All @@ -637,14 +645,16 @@ 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));
}

UpdateCompartmentSizes();
}

Broadcast(EndResizeCompartments());
}

/******************************************************************************
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 28 additions & 1 deletion libjcore/code/JPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "JArray.h"

class JPartition
class JPartition : virtual public JBroadcaster
{
public:

Expand Down Expand Up @@ -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)
{ };
};
};

/******************************************************************************
Expand Down
5 changes: 0 additions & 5 deletions todo-jxlayout

This file was deleted.

3 changes: 2 additions & 1 deletion tools/jx_layout_editor/code/LayoutUndo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class LayoutUndo : public JUndo
kUnclassifiedType,
kWindowResizeType,
kArrowType,
kDragResizeType
kDragResizeType,
kResizePartitionType
};

public:
Expand Down
26 changes: 26 additions & 0 deletions tools/jx_layout_editor/code/Partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Partition::Partition

InsertLayoutContainer(1, itsPartition->GetCompartment(1));
InsertLayoutContainer(2, itsPartition->GetCompartment(2));

PartitionX();
}

Partition::Partition
Expand Down Expand Up @@ -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;
}));
}

/******************************************************************************
Expand All @@ -112,6 +137,7 @@ Partition::Partition

Partition::~Partition()
{
jdelete itsUndo;
}

/******************************************************************************
Expand Down
6 changes: 6 additions & 0 deletions tools/jx_layout_editor/code/Partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class JXPartition;
class PartitionPanel;
class LayoutUndo;

class Partition : public MultiContainerWidget
{
Expand Down Expand Up @@ -61,6 +62,11 @@ class Partition : public MultiContainerWidget
Type itsType;
JXPartition* itsPartition;
PartitionPanel* itsPanel;
LayoutUndo* itsUndo;

private:

void PartitionX();
};

#endif

0 comments on commit e28d0cd

Please sign in to comment.