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
27 #include "mainindexes.h"
29 #include "mainsession.h"
32 #include "mwindowgui.h"
33 #include "nestededls.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,
62 file.terminate_string();
63 if(changes_made) mwindow->session->changes_made = 1;
65 // Remove all entries after current and create new one
66 UndoStackItem *current = undo_stack->push();
68 current->set_flags(load_flags);
69 current->set_data(file.string());
70 current->set_description((char*)description);
71 current->set_creator(creator);
72 current->set_filename(mwindow->session->filename);
73 //printf("MainUndo::update_undo_entry %d %p %s\n", __LINE__, current, current->get_filename());
75 // Can't undo only 1 record.
76 if(undo_stack->total() > 1)
78 // mwindow->gui->lock_window("MainUndo::update_undo");
79 mwindow->gui->mainmenu->undo->update_caption(description);
80 mwindow->gui->mainmenu->redo->update_caption("");
81 // mwindow->gui->unlock_window();
85 last_update->update();
88 void MainUndo::update_undo_before(const char *description, void *creator)
90 //printf("MainUndo::update_undo_before %d\n", __LINE__);
91 if(undo_stack->current && !(undo_stack->number_of(undo_stack->current) % 2))
93 printf("MainUndo::update_undo_before %d \"%s\": must be on an after entry to do this. size=%d\n",
101 // Move up an entry to get back in sync
105 mwindow->commit_commercial();
107 // Discard if creator matches previous before entry and within a time limit
110 UndoStackItem *current = undo_stack->current;
111 // Currently on an after entry
117 // Now on a before entry
120 if(current->get_creator() == creator &&
121 !strcmp(current->get_description(), description) &&
122 last_update->get_difference() < UNDO_SPAN)
124 // Before entry has same creator within minimum time. Reuse it.
125 // Stack must point to the before entry
126 undo_stack->current = current;
132 // Append new entry after current position
133 update_undo_entry("", 0, creator, 0);
136 void MainUndo::update_undo_after(const char *description,
140 //printf("MainUndo::update_undo_after %d\n", __LINE__);
141 if(undo_stack->number_of(undo_stack->current) % 2)
143 printf("MainUndo::update_undo_after %d \"%s\": must be on a before entry to do this. size=%d\n",
146 undo_stack->total());
150 // Not getting any update_undo_before to get back in sync, so just append 1 here
154 update_undo_entry(description, load_flags, 0, changes_made);
156 // Update the before entry flags
157 UndoStackItem *current = undo_stack->last;
158 if(current) current = PREVIOUS;
161 current->set_flags(load_flags);
162 current->set_description((char*)description);
167 UndoStackItem *MainUndo::next_undo()
169 return undo_stack->get_current_undo();
172 UndoStackItem *MainUndo::next_redo()
174 return undo_stack->get_current_redo();
177 int MainUndo::undo_load_flags()
179 UndoStackItem *item = next_undo();
180 return item ? item->get_flags() : 0;
183 int MainUndo::redo_load_flags()
185 UndoStackItem *item = next_redo();
186 return item ? item->get_flags() : 0;
192 mwindow->undo_commercial();
193 UndoStackItem *current = undo_stack->current;
195 undo_stack->current = next_undo();
196 if( undo_stack->number_of(current) % 2 )
197 current = PREVIOUS; // Now have an even number
200 // Set the redo text to the current description
202 UndoStackItem *next = NEXT;
203 mwindow->gui->mainmenu->redo->
204 update_caption(next ? next->get_description() : "");
207 char *current_data = current->get_data();
209 file.read_from_string(current_data);
210 load_from_undo(&file, current->get_flags());
211 //printf("MainUndo::undo %d %s\n", __LINE__, current->get_filename());
212 mwindow->set_filename(current->get_filename());
213 delete [] current_data;
216 // Now update the menu with the after entry
217 UndoStackItem *prev = PREVIOUS;
218 mwindow->gui->mainmenu->undo->
219 update_caption(prev ? prev->get_description() : "");
225 mwindow->reset_caches();
232 UndoStackItem *current = next_redo();
234 undo_stack->current = current;
235 char *current_data = current->get_data();
238 mwindow->set_filename(current->get_filename());
239 file.read_from_string(current_data);
240 load_from_undo(&file, current->get_flags());
241 delete [] current_data;
245 mwindow->gui->mainmenu->undo->
246 update_caption(current->get_description());
247 // Get next after entry
248 if( (current=NEXT) ) current = NEXT;
249 mwindow->gui->mainmenu->redo->
250 update_caption(current ? current->get_description() : "");
255 mwindow->reset_caches();
261 // Here the master EDL loads
262 int MainUndo::load_from_undo(FileXML *file, uint32_t load_flags)
264 if( load_flags & LOAD_SESSION )
265 mwindow->close_mixers();
266 mwindow->edl->load_xml(file, load_flags);
267 for(Asset *asset = mwindow->edl->assets->first;
271 mwindow->mainindexes->add_next_asset(0, asset);
274 for(int i = 0; i < mwindow->edl->nested_edls->size(); i++)
276 EDL *nested_edl = mwindow->edl->nested_edls->get(i);
277 mwindow->mainindexes->add_next_asset(0, nested_edl);
279 mwindow->mainindexes->start_build();
280 mwindow->update_plugin_guis(1);
281 if( load_flags & LOAD_SESSION )
282 mwindow->open_mixers();
287 void MainUndo::reset_creators()
289 for(UndoStackItem *current = undo_stack->first;
293 current->set_creator(0);
298 void MainUndo::dump(FILE *fp)
300 undo_stack->dump(fp);