summary refs log tree commit diff stats
path: root/tools/mapedit/src/undo.cpp
blob: bcf7b926b36e6cab537d5e9120847bfd17a3d086 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "undo.h"
#include "frame.h"

Undoable::Undoable(std::string title, std::function<void ()> forward, std::function<void ()> backward)
{
  this->title = title;
  this->forward = forward;
  this->backward = backward;
}

std::string Undoable::getTitle() const
{
  return title;
}

void Undoable::apply()
{
  forward();
}

void Undoable::undo()
{
  backward();
}

wxBEGIN_EVENT_TABLE(UndoableTextBox, wxTextCtrl)
  EVT_SET_FOCUS(UndoableTextBox::OnFocus)
  EVT_KILL_FOCUS(UndoableTextBox::OnKillFocus)
wxEND_EVENT_TABLE()

UndoableTextBox::UndoableTextBox(wxWindow* parent, wxWindowID id, wxString startText, std::string undoType, MapeditFrame* realParent, wxPoint pos, wxSize size, long style) : wxTextCtrl(parent, id, startText, pos, size, style)
{
  this->undoText = undoType;
  this->realParent = realParent;
  
  Init();
}

void UndoableTextBox::Init()
{
  Bind(wxEVT_TEXT, &UndoableTextBox::OnTextChange, this);
  
  curText = GetValue();
}

void UndoableTextBox::OnFocus(wxFocusEvent& event)
{
  curText = GetValue();
  
  event.Skip();
}

void UndoableTextBox::OnKillFocus(wxFocusEvent& event)
{
  undo.reset();
  
  event.Skip();
}

void UndoableTextBox::OnTextChange(wxCommandEvent& event)
{
  if (!undo)
  {
    auto tempUndo = std::make_shared<Undo>(undoText, curText, *this);
    
    realParent->commitAfter(tempUndo);
    
    undo = tempUndo;
  }
  
  undo->newText = GetValue();
  curText = GetValue();
  
  event.Skip();
}

UndoableTextBox::Undo::Undo(std::string title, wxString origText, UndoableTextBox& parent) : origText(origText), parent(parent)
{
  this->title = title;
}

void UndoableTextBox::Undo::apply()
{
  parent.ChangeValue(newText);
}

void UndoableTextBox::Undo::undo()
{
  parent.ChangeValue(origText);
}

void UndoableTextBox::Undo::endChanges()
{
  parent.undo.reset();
}