Gin
Loading...
Searching...
No Matches
TextHistory Class Reference

Undo/redo history management for text editing with efficient diff-based storage. More...

#include <gin_texthistory.h>

Public Member Functions

void setHistoryLimit (int numItems)
void undo ()
void redo ()
bool canUndo ()
bool canRedo ()
bool addText (const juce::String &)
const juce::String & getCurrentText ()

Public Attributes

std::function< void()> onStateChanged

Detailed Description

Undo/redo history management for text editing with efficient diff-based storage.

TextHistory provides unlimited undo/redo functionality for text documents using a diff-based approach (binary patches) for memory-efficient storage. Instead of storing complete text snapshots, only the differences between states are stored.

Key Features:

  • Unlimited undo/redo with configurable history limit
  • Efficient diff-based storage (binary patches)
  • Change notification via callback
  • Minimal memory footprint
  • Linear history (no branching)

The implementation uses binary diff patches (forward and backward) to efficiently store changes, making it suitable for large documents or long editing sessions.

Usage:

TextHistory history;
history.setHistoryLimit(100); // Keep last 100 changes
// Register for state changes
history.onStateChanged = []() {
DBG("History state changed");
};
// Add text changes
history.addText("Initial text");
history.addText("Modified text");
history.addText("Final text");
// Undo changes
if (history.canUndo())
{
history.undo();
juce::String text = history.getCurrentText(); // Returns "Modified text"
}
// Redo changes
if (history.canRedo())
{
history.redo();
juce::String text = history.getCurrentText(); // Returns "Final text"
}
Undo/redo history management for text editing with efficient diff-based storage.
Definition gin_texthistory.h:65
std::function< void()> onStateChanged
Definition gin_texthistory.h:77
bool canRedo()
bool addText(const juce::String &)
const juce::String & getCurrentText()
void setHistoryLimit(int numItems)
bool canUndo()

Implementation Details:

  • Uses binary diff algorithm for patch generation
  • Maintains stack of forward/backward patches
  • Stack pointer tracks current position in history
  • Adding new text while mid-history discards forward history
See also
juce::UndoManager for alternative undo/redo approach

Member Function Documentation

◆ setHistoryLimit()

void TextHistory::setHistoryLimit ( int numItems)

◆ undo()

void TextHistory::undo ( )

◆ redo()

void TextHistory::redo ( )

◆ canUndo()

bool TextHistory::canUndo ( )

◆ canRedo()

bool TextHistory::canRedo ( )

◆ addText()

bool TextHistory::addText ( const juce::String & )

◆ getCurrentText()

const juce::String & TextHistory::getCurrentText ( )

Member Data Documentation

◆ onStateChanged

std::function<void ()> TextHistory::onStateChanged

The documentation for this class was generated from the following file: