Debian Codegraph @

wxcrafter/src/wxc_edit_manager.cpp

raw
1 #include "wxc_edit_manager.h"
2 #include "event_notifier.h"
3 #include "wxc_project_metadata.h"
4 #include "wxguicraft_main_view.h"
5 #include <wx/app.h>
6
7 wxDEFINE_EVENT(wxEVT_MULTIPLE_UNREDO, wxCommandEvent);
8
9 const int FIRST_MENU_ID = 10000;
10
11 wxcEditManager::wxcEditManager()
12 : m_isModified(false)
13 {
14 EventNotifier::Get()->Connect(wxEVT_WXGUI_PROJECT_SAVED, wxCommandEventHandler(wxcEditManager::OnProjectSaved),
15 NULL, this);
16 EventNotifier::Get()->Connect(wxEVT_WXC_PROJECT_LOADED, wxCommandEventHandler(wxcEditManager::OnProjectLoaded),
17 NULL, this);
18 EventNotifier::Get()->Connect(wxEVT_WXGUI_PROJECT_CLOSED, wxCommandEventHandler(wxcEditManager::OnProjectClosed),
19 NULL, this);
20 EventNotifier::Get()->Connect(wxEVT_PROJECT_METADATA_MODIFIED,
21 wxCommandEventHandler(wxcEditManager::OnProjectMetadataChanged), NULL, this);
22 EventNotifier::Get()->Connect(wxEVT_PROPERTIES_MODIFIED, wxCommandEventHandler(wxcEditManager::OnPropertyChanged),
23 NULL, this);
24 }
25
26 wxcEditManager::~wxcEditManager() {}
27
28 wxcEditManager& wxcEditManager::Get()
29 {
30 static wxcEditManager editManager;
31 return editManager;
32 }
33
34 State::Ptr_t wxcEditManager::Redo()
35 {
36 State::Ptr_t state = m_redoList.back();
37 m_redoList.pop_back();
38 m_undoList.push_back(state);
39 SetModified(true);
40 return state;
41 }
42
43 State::Ptr_t wxcEditManager::Undo()
44 {
45 // move the last item to the re-do list
46 State::Ptr_t state = m_undoList.back();
47 m_undoList.pop_back();
48 m_redoList.push_back(state);
49 SetModified(true);
50
51 // and return the current state
52 return GetCurrentState();
53 }
54
55 void wxcEditManager::OnUndoDropdownItem(wxCommandEvent& event)
56 {
57 const int count = event.GetId() - FIRST_MENU_ID + 1;
58 wxCHECK_RET(count > 0 && count <= (int)m_undoList.size(), "Invalid command index");
59 for(int n = 0; n < count; ++n) {
60 State::Ptr_t state = m_undoList.back();
61 m_undoList.pop_back();
62 m_redoList.push_back(state);
63 }
64
65 SetModified(true);
66
67 wxCommandEvent evt(wxEVT_MULTIPLE_UNREDO,
68 wxID_UNDO); // This tells GUICraftMainPanel to reload the new 'current' state
69 wxPostEvent(wxTheApp, evt);
70 }
71
72 void wxcEditManager::OnRedoDropdownItem(wxCommandEvent& event)
73 {
74 const int count = event.GetId() - FIRST_MENU_ID + 1;
75 wxCHECK_RET(count > 0 && count <= (int)m_redoList.size(), "Invalid command index");
76 for(int n = 0; n < count; ++n) {
77 State::Ptr_t state = m_redoList.back();
78 m_redoList.pop_back();
79 m_undoList.push_back(state);
80 }
81
82 SetModified(true);
83
84 wxCommandEvent evt(wxEVT_MULTIPLE_UNREDO,
85 wxID_REDO); // This tells GUICraftMainPanel to reload the new 'current' state
86 wxPostEvent(wxTheApp, evt);
87 }
88
89 void wxcEditManager::SaveState(State::Ptr_t state)
90 {
91 m_redoList.clear();
92 m_undoList.push_back(state);
93 }
94
95 void wxcEditManager::Clear()
96 {
97 m_redoList.clear();
98 m_undoList.clear();
99 m_initialState.reset();
100 SetModified(false);
101 }
102
103 State::Ptr_t wxcEditManager::GetCurrentState() const
104 {
105 if(m_undoList.empty()) return m_initialState;
106 return m_undoList.back();
107 }
108
109 bool wxcEditManager::IsDirty() const { return m_isModified; }
110
111 void wxcEditManager::OnProjectLoaded(wxCommandEvent& event)
112 {
113 event.Skip();
114 Clear();
115 m_initialState = GUICraftMainPanel::m_MainPanel->CurrentState();
116 }
117
118 void wxcEditManager::OnProjectSaved(wxCommandEvent& event)
119 {
120 event.Skip();
121 SetModified(false);
122 }
123
124 void wxcEditManager::OnProjectClosed(wxCommandEvent& event)
125 {
126 event.Skip();
127 Clear();
128 }
129
130 void wxcEditManager::OnProjectMetadataChanged(wxCommandEvent& event)
131 {
132 event.Skip();
133 SetModified(true);
134 PushState("project metadata changed");
135 }
136
137 void wxcEditManager::OnPropertyChanged(wxCommandEvent& event)
138 {
139 event.Skip();
140 SetModified(true);
141 PushState("property changed");
142 }
143
144 void wxcEditManager::PushState(const wxString& label)
145 {
146 if(GUICraftMainPanel::m_MainPanel) {
147 SetModified(true);
148 State::Ptr_t state = GUICraftMainPanel::m_MainPanel->CurrentState();
149 state->label = label;
150 SaveState(state);
151 }
152 }
153
154 void wxcEditManager::NotifyProjectModified()
155 {
156 wxCommandEvent event(wxEVT_CMD_WXCRAFTER_PROJECT_MODIFIED);
157 EventNotifier::Get()->AddPendingEvent(event);
158 }
159
160 void wxcEditManager::NotifyProjectSynched()
161 {
162 wxCommandEvent event(wxEVT_CMD_WXCRAFTER_PROJECT_SYNCHED);
163 EventNotifier::Get()->AddPendingEvent(event);
164 }
165
166 void wxcEditManager::SetModified(bool modified)
167 {
168 m_isModified = modified;
169 if(m_isModified) {
170 NotifyProjectModified();
171 } else {
172 NotifyProjectSynched();
173 }
174 }
175
176 bool wxcEditManager::CanRedo() const { return !m_redoList.empty(); }
177
178 bool wxcEditManager::CanUndo() const { return !m_undoList.empty(); }
179
180 void wxcEditManager::DoPopulateUnRedoMenu(wxMenu& menu, bool undoing)
181 {
182 wxString prefix(undoing ? _("Undo ") : _("Redo "));
183 int id = FIRST_MENU_ID;
184 int count = 0;
185
186 if(undoing) {
187 if(m_undoList.size() > 0) {
188 for(State::List_t::const_reverse_iterator iter = m_undoList.rbegin(); iter != m_undoList.rend(); ++iter) {
189 State::Ptr_t command = *iter;
190 if(command) { menu.Append(id++, wxString::Format("%i ", ++count) + prefix + command->label); }
191 }
192 }
193 } else {
194 for(State::List_t::const_reverse_iterator iter = m_redoList.rbegin(); iter != m_redoList.rend(); ++iter) {
195 State::Ptr_t command = *iter;
196 if(command) { menu.Append(id++, wxString::Format("%i ", ++count) + prefix + command->label); }
197 }
198 }
199 }