4 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "mainindexes.h"
30 #include "mainsession.h"
33 #include "mwindowgui.h"
35 #include "undostack.h"
37 MainUndo::MainUndo(MWindow *mwindow)
39 this->mwindow = mwindow;
40 undo_stack = new UndoStack;
41 last_update = new Timer;
51 void MainUndo::update_undo_entry(const char *description,
58 mwindow->edl->save_xml(&file, "");
59 file.terminate_string();
60 if(changes_made) mwindow->session->changes_made = 1;
62 // Remove all entries after current and create new one
63 UndoStackItem *current = undo_stack->push();
65 current->set_flags(load_flags);
66 current->set_data(file.string());
67 current->set_description((char*)description);
68 current->set_creator(creator);
69 current->set_filename(mwindow->session->filename);
70 //printf("MainUndo::update_undo_entry %d %p %s\n", __LINE__, current, current->get_filename());
72 // Can't undo only 1 record.
73 if(undo_stack->total() > 1)
75 // mwindow->gui->lock_window("MainUndo::update_undo");
76 mwindow->gui->mainmenu->undo->update_caption(description);
77 mwindow->gui->mainmenu->redo->update_caption("");
78 // mwindow->gui->unlock_window();
82 last_update->update();
85 void MainUndo::update_undo_before(const char *description, void *creator)
87 //printf("MainUndo::update_undo_before %d\n", __LINE__);
88 if(undo_stack->current && !(undo_stack->number_of(undo_stack->current) % 2))
90 printf("MainUndo::update_undo_before %d \"%s\": must be on an after entry to do this. size=%d\n",
98 // Move up an entry to get back in sync
102 mwindow->commit_commercial();
104 // Discard if creator matches previous before entry and within a time limit
107 UndoStackItem *current = undo_stack->current;
108 // Currently on an after entry
114 // Now on a before entry
117 if(current->get_creator() == creator &&
118 !strcmp(current->get_description(), description) &&
119 last_update->get_difference() < UNDO_SPAN)
121 // Before entry has same creator within minimum time. Reuse it.
122 // Stack must point to the before entry
123 undo_stack->current = current;
129 // Append new entry after current position
130 update_undo_entry("", 0, creator, 0);
133 void MainUndo::update_undo_after(const char *description,
137 //printf("MainUndo::update_undo_after %d\n", __LINE__);
138 if(undo_stack->number_of(undo_stack->current) % 2)
140 printf("MainUndo::update_undo_after %d \"%s\": must be on a before entry to do this. size=%d\n",
143 undo_stack->total());
147 // Not getting any update_undo_before to get back in sync, so just append 1 here
151 update_undo_entry(description, load_flags, 0, changes_made);
153 // Update the before entry flags
154 UndoStackItem *current = undo_stack->last;
155 if(current) current = PREVIOUS;
158 current->set_flags(load_flags);
159 current->set_description((char*)description);
164 UndoStackItem *MainUndo::next_undo()
166 return undo_stack->get_current_undo();
169 UndoStackItem *MainUndo::next_redo()
171 return undo_stack->get_current_redo();
174 int MainUndo::undo_load_flags()
176 UndoStackItem *item = next_undo();
177 return item ? item->get_flags() : 0;
180 int MainUndo::redo_load_flags()
182 UndoStackItem *item = next_redo();
183 return item ? item->get_flags() : 0;
189 mwindow->undo_commercial();
190 UndoStackItem *current = undo_stack->current;
192 undo_stack->current = next_undo();
193 if( undo_stack->number_of(current) % 2 )
194 current = PREVIOUS; // Now have an even number
197 // Set the redo text to the current description
199 UndoStackItem *next = NEXT;
200 mwindow->gui->mainmenu->redo->
201 update_caption(next ? next->get_description() : "");
203 char *current_data = current->get_data();
206 file.read_from_string(current_data);
207 delete [] current_data;
208 load_from_undo(&file, current->get_flags());
209 //printf("MainUndo::undo %d %s\n", __LINE__, current->get_filename());
210 mwindow->set_filename(current->get_filename());
213 // Now update the menu with the after entry
214 UndoStackItem *prev = PREVIOUS;
215 mwindow->gui->mainmenu->undo->
216 update_caption(prev ? prev->get_description() : "");
222 mwindow->reset_caches();
229 UndoStackItem *current = next_redo();
231 undo_stack->current = current;
232 char *current_data = current->get_data();
234 mwindow->set_filename(current->get_filename());
236 file.read_from_string(current_data);
237 load_from_undo(&file, current->get_flags());
238 delete [] current_data;
240 mwindow->gui->mainmenu->undo->
241 update_caption(current->get_description());
242 // Get next after entry
243 if( (current=NEXT) ) current = NEXT;
244 mwindow->gui->mainmenu->redo->
245 update_caption(current ? current->get_description() : "");
249 mwindow->reset_caches();
255 // Here the master EDL loads
256 int MainUndo::load_from_undo(FileXML *file, uint32_t load_flags)
258 if( load_flags & LOAD_SESSION ) {
259 mwindow->gui->unlock_window();
260 mwindow->close_mixers();
261 mwindow->gui->lock_window("MainUndo::load_from_undo");
263 if( (load_flags & LOAD_ALL) == LOAD_ALL ) {
265 edl->create_objects();
266 mwindow->edl->remove_user();
269 mwindow->edl->load_xml(file, load_flags);
270 for( Asset *asset=mwindow->edl->assets->first; asset; asset=asset->next ) {
271 mwindow->mainindexes->add_next_asset(0, asset);
273 for( int i=0; i<mwindow->edl->nested_edls.size(); ++i ) {
274 EDL *nested_edl = mwindow->edl->nested_edls[i];
275 mwindow->mainindexes->add_next_asset(0, nested_edl);
277 mwindow->mainindexes->start_build();
278 mwindow->update_plugin_guis(1);
279 if( load_flags & LOAD_SESSION ) {
280 mwindow->gui->unlock_window();
281 mwindow->open_mixers();
282 mwindow->gui->lock_window("MainUndo::load_from_undo");
288 void MainUndo::reset_creators()
290 for( UndoStackItem *current=undo_stack->first; current; current=NEXT ) {
291 current->set_creator(0);
296 void MainUndo::dump(FILE *fp)
298 undo_stack->dump(fp);
301 void MainUndo::save(FILE *fp)
303 undo_stack->save(fp);
306 void MainUndo::load(FILE *fp)
308 undo_stack->load(fp);
309 UndoStackItem *current = undo_stack->current;
310 char *current_data = current ? current->get_data() : 0;
311 if( !current_data ) return;
312 mwindow->gui->lock_window("MainUndo::load");
313 UndoStackItem *next = current->next;
314 mwindow->gui->mainmenu->redo->
315 update_caption(next ? next->get_description() : "");
316 mwindow->set_filename(current->get_filename());
318 file.read_from_string(current_data);
319 load_from_undo(&file, LOAD_ALL);
320 delete [] current_data;
321 UndoStackItem *prev = current->previous;
322 mwindow->gui->mainmenu->undo->
323 update_caption(prev ? prev->get_description() : "");
324 mwindow->update_project(LOADMODE_REPLACE);
325 mwindow->gui->unlock_window();