5 #include "mainindexes.h"
7 #include "mainsession.h"
10 #include "mwindowgui.h"
11 #include "undostackitem.h"
14 // Minimum number of undoable operations on the undo stack
15 #define UNDOMINLEVELS 5
16 // Limits the bytes of memory used by the undo stack
17 #define UNDOMEMORY 50000000
20 class MainUndoStackItem : public UndoStackItem
23 MainUndoStackItem(MainUndo* undo, char* description,
24 uint32_t load_flags, void* creator);
25 virtual ~MainUndoStackItem();
27 void set_data_before(char *data);
29 virtual int get_size();
32 // type of modification
33 unsigned long load_flags;
35 // data before the modification for undos
40 void load_from_undo(FileXML *file, uint32_t load_flags); // loads undo from the stringfile to the project
44 MainUndo::MainUndo(MWindow *mwindow)
46 this->mwindow = mwindow;
49 last_update = new Timer;
51 // get the initial project so we have something that the last undo reverts to
61 void MainUndo::update_undo(char *description, uint32_t load_flags,
62 void *creator, int changes_made)
64 if (ignore_push(description, load_flags, creator))
70 MainUndoStackItem* new_entry = new MainUndoStackItem(this, description, load_flags, creator);
72 // the old data_after is the state before the change
73 new_entry->set_data_before(data_after);
75 push_undo_item(new_entry);
78 void MainUndo::push_undo_item(UndoStackItem *item)
81 while (redo_stack.last)
82 redo_stack.remove(redo_stack.last);
84 // move item onto undo_stack
85 undo_stack.append(item);
90 mwindow->session->changes_made = 1;
91 mwindow->gui->lock_window("MainUndo::update_undo_before");
92 mwindow->gui->mainmenu->undo->update_caption(item->description);
93 mwindow->gui->mainmenu->redo->update_caption("");
94 mwindow->gui->unlock_window();
97 void MainUndo::capture_state()
100 mwindow->edl->save_xml(mwindow->plugindb,
105 file.terminate_string();
106 delete [] data_after;
107 data_after = new char[strlen(file.string)+1];
108 strcpy(data_after, file.string);
111 bool MainUndo::ignore_push(char *description, uint32_t load_flags, void* creator)
113 // ignore this push under certain conditions:
114 // - if nothing was undone
115 bool ignore = redo_stack.last == 0 &&
116 // - if it is not the first push
118 // - if it has the same description as the previous undo
119 strcmp(undo_stack.last->description, description) == 0 &&
120 // - if it originates from the same creator
121 undo_stack.last->creator == creator &&
122 // - if it follows closely after the previous undo
123 last_update->get_difference() < 300 /*millisec*/;
124 last_update->update();
128 void MainUndo::push_state(char *description, uint32_t load_flags, void* creator)
130 if (ignore_push(description, load_flags, creator))
136 MainUndoStackItem* new_entry = new MainUndoStackItem(this, description, load_flags, creator);
137 // the old data_after is the state before the change
138 new_entry->set_data_before(data_after);
139 push_undo_item(new_entry);
141 mwindow->session->changes_made = 1;
151 UndoStackItem* current_entry = undo_stack.last;
155 // move item to redo_stack
156 undo_stack.remove_pointer(current_entry);
157 current_entry->undo();
158 redo_stack.append(current_entry);
163 mwindow->gui->mainmenu->redo->update_caption(current_entry->description);
166 mwindow->gui->mainmenu->undo->update_caption(undo_stack.last->description);
168 mwindow->gui->mainmenu->undo->update_caption("");
178 UndoStackItem* current_entry = redo_stack.last;
182 // move item to undo_stack
183 redo_stack.remove_pointer(current_entry);
184 current_entry->undo();
185 undo_stack.append(current_entry);
190 mwindow->gui->mainmenu->undo->update_caption(current_entry->description);
193 mwindow->gui->mainmenu->redo->update_caption(redo_stack.last->description);
195 mwindow->gui->mainmenu->redo->update_caption("");
202 // enforces that the undo stack does not exceed a size of UNDOMEMORY
203 // except that it always has at least UNDOMINLEVELS entries
204 void MainUndo::prune_undo()
209 UndoStackItem* i = undo_stack.last;
210 while (i != 0 && (levels < UNDOMINLEVELS || size <= UNDOMEMORY))
212 size += i->get_size();
219 // truncate everything before and including i
220 while (undo_stack.first != i)
221 undo_stack.remove(undo_stack.first);
222 undo_stack.remove(undo_stack.first);
230 MainUndoStackItem::MainUndoStackItem(MainUndo* main_undo, char* description,
231 uint32_t load_flags, void* creator)
234 this->load_flags = load_flags;
235 this->main_undo = main_undo;
236 set_description(description);
237 set_creator(creator);
240 MainUndoStackItem::~MainUndoStackItem()
242 delete [] data_before;
245 void MainUndoStackItem::set_data_before(char *data)
247 data_before = new char[strlen(data) + 1];
248 strcpy(data_before, data);
251 void MainUndoStackItem::undo()
253 // move the old data_after here
254 char* before = data_before;
256 set_data_before(main_undo->data_after);
261 file.read_from_string(before);
262 load_from_undo(&file, load_flags);
265 int MainUndoStackItem::get_size()
267 return data_before ? strlen(data_before) : 0;
270 // Here the master EDL loads
271 void MainUndoStackItem::load_from_undo(FileXML *file, uint32_t load_flags)
273 MWindow* mwindow = main_undo->mwindow;
274 mwindow->edl->load_xml(mwindow->plugindb, file, load_flags);
275 for(Asset *asset = mwindow->edl->assets->first;
279 mwindow->mainindexes->add_next_asset(0, asset);
281 mwindow->mainindexes->start_build();
285 void MainUndo::reset_creators()
287 for(UndoStackItem *current = undo_stack.first;
291 current->set_creator(0);