4 * Copyright (C) 2008 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
24 #include "edlsession.h"
28 #include "mwindowgui.h"
30 #include "recordlabel.h"
31 #include "mainsession.h"
38 Labels::Labels(EDL *edl, const char *xml_tag)
42 this->xml_tag = (char*)xml_tag;
50 void Labels::dump(FILE *fp)
52 for(Label *current = first; current; current = NEXT)
54 fprintf(fp," label: %f\n", current->position);
58 void Labels::insert_labels(Labels *labels, double start, double length, int paste_silence)
64 //printf("Labels::insert_labels 1 %d %d\n", __LINE__, paste_silence);
66 // Insert silence in old labels
69 for(old_label = first; old_label; old_label = old_label->next)
71 if(old_label->position > start ||
72 edl->equivalent(old_label->position, start))
73 old_label->position += length;
78 // Insert one new label at a time
79 for(new_label = labels->first; new_label; new_label = new_label->next)
82 //printf("Labels::insert_labels 2 %f\n", new_label->position + start);
84 // Check every old label for existence
85 for(old_label = first; old_label; old_label = old_label->next)
87 if(edl->equivalent(old_label->position, new_label->position + start))
93 if(old_label->position > new_label->position + start)
100 insert_before(old_label, new Label(edl, this, new_label->position + start));
102 append(new Label(edl, this, new_label->position + start));
108 void Labels::insert_label(double position)
111 Label *old_label = 0;
113 // Check every old label for existence
114 for(old_label = first; old_label; old_label = old_label->next)
116 if(edl->equivalent(old_label->position, position))
122 if(old_label->position > position)
129 insert_before(old_label, new Label(edl, this, position));
131 append(new Label(edl, this, position));
136 int Labels::toggle_label(double start, double end)
139 //printf("Labels::toggle_label 1 %f %f\n", start, end);
141 // handle selection start
142 // find label the selectionstart is after
144 current && current->position < start && !edl->equivalent(current->position, start);
147 //printf("Labels::toggle_label 2 %f %f %f\n", start, end, current->position);
153 //printf("Labels::toggle_label 3 %f %f %f\n", start, end, current->position);
154 if(edl->equivalent(current->position, start))
156 //printf("Labels::toggle_label 1\n");
160 { // insert before it
161 current = insert_before(current, new Label(edl, this, start));
165 { // insert after last
166 //printf("Labels::toggle_label 1\n");
167 current = append(new Label(edl, this, start));
170 // handle selection end
171 if(!EQUIV(start, end))
173 //printf("Labels::toggle_label 2 %.16e %.16e\n", start, end);
174 // find label the selectionend is after
176 current && current->position < end && !edl->equivalent(current->position, end);
184 if(edl->equivalent(current->position, end))
190 current = insert_before(current, new Label(edl, this, end));
195 current = append(new Label(edl, this, end));
201 Label *Labels::add_label(double position)
205 current && current->position < position;
208 if( edl->equivalent(current->position, position) ) return 0;
213 current = insert_before(current, new Label(edl, this, position));
217 current = append(new Label(edl, this, position));
222 int Labels::delete_all()
229 int Labels::copy(double start, double end, FileXML *xml)
231 char string[BCTEXTLEN];
232 xml->tag.set_title(xml_tag);
234 xml->append_newline();
237 strcpy(string, xml_tag);
238 string[strlen(string) - 1] = 0;
239 for(current = label_of(start);
240 current && current->position <= end;
243 xml->tag.set_title(string);
244 xml->tag.set_property("TIME", (double)current->position - start);
245 //printf("Labels::copy %f\n", current->position - start);
249 sprintf(string, "/%s", xml_tag);
250 xml->tag.set_title(string);
252 xml->append_newline();
253 xml->append_newline();
257 int Labels::copy_length(long start, long end) // return number of Labels in selection
262 for(current = label_of(start); current && current->position <= end; current = NEXT)
269 void Labels::copy_from(Labels *labels)
271 while(last) delete last;
273 for(Label *current = labels->first; current; current = NEXT)
275 append(new Label(edl, this, current->position));
280 Labels& Labels::operator=(Labels &that)
283 printf("Labels::operator= 1\n");
288 int Labels::save(FileXML *xml)
290 xml->tag.set_title("LABELS");
292 xml->append_newline();
296 for(current = first; current; current = NEXT)
298 xml->tag.set_title("LABEL");
299 xml->tag.set_property("TIME", (double)current->position);
303 xml->append_newline();
304 xml->tag.set_title("/LABELS");
306 xml->append_newline();
307 xml->append_newline();
311 int Labels::load(FileXML *xml, uint32_t load_flags)
314 char string1[BCTEXTLEN], string2[BCTEXTLEN];
316 sprintf(string1, "/%s", xml_tag);
317 strcpy(string2, xml_tag);
318 string2[strlen(string2) - 1] = 0;
321 result = xml->read_tag();
324 if(xml->tag.title_is(string1))
329 if(xml->tag.title_is(string2))
331 double position = xml->tag.get_property("TIME", (double)-1);
333 position = xml->tag.get_property("SAMPLE", (double)-1);
334 //printf("Labels::load %f\n", position);
337 Label *current = label_of(position);
338 current = insert_before(current, new Label(edl, this, position));
342 if(xml->tag.title_is("INPOINT"))
344 double position = xml->tag.get_property("TIME", (double)-1);
346 position = xml->tag.get_property("SAMPLE", (double)-1);
353 if(xml->tag.title_is("OUTPOINT"))
355 double position = xml->tag.get_property("TIME", (double)-1);
357 position = xml->tag.get_property("SAMPLE", (double)-1);
370 int Labels::clear(double start, double end, int follow)
375 //printf("Labels::clear 1\n");
376 current = label_of(start);
377 //printf("Labels::clear 2\n");
379 // remove selected labels
380 while(current && current->position < end)
387 // Shift later labels
392 //printf("Labels::clear %d %f %f\n", __LINE__, current->position, end - start);
393 current->position -= end - start; // shift labels forward
394 //printf("Labels::clear %d %f %f\n", __LINE__, current->position, end - start);
397 //printf("Labels::clear 4\n");
399 //printf("Labels::clear 5\n");
406 Label* Labels::prev_label(double position)
410 // Test for label under cursor position
412 current && !edl->equivalent(current->position, position);
416 // Test for label after cursor position
419 current && current->position < position;
423 // Test for label before cursor position
427 // Get previous label
433 Label* Labels::next_label(double position)
437 // Test for label under cursor position
439 current && !edl->equivalent(current->position, position);
443 // Test for label before cursor position
446 current && current->position > position;
450 // Test for label after cursor position
460 int Labels::insert(double start, double length)
462 // shift every label including the first one back
465 //printf("Labels::insert %d\n", __LINE__);
466 for(current = label_of(start); current; current = NEXT)
468 current->position += length;
473 int Labels::paste_silence(double start, double end)
475 insert(start, end - start);
480 int Labels::modify_handles(double oldposition,
487 handle_mode == MOVE_ALL_EDITS)
489 if(currentend == 0) // left handle
491 if(newposition < oldposition)
493 insert(oldposition, oldposition - newposition); // shift all labels right
497 clear(oldposition, newposition); // clear selection
502 if(newposition < oldposition)
504 clear(newposition, oldposition);
508 insert(oldposition, newposition - oldposition);
515 int Labels::optimize()
524 for(current = first; current && NEXT && !result;)
527 if(current->position == next->position)
538 Label* Labels::label_of(double position)
542 for(current = first; current; current = NEXT)
544 if(current->position >= position) return current;
563 Label::Label(EDL *edl, Labels *labels, double position)
567 this->labels = labels;
568 this->position = position;
574 // if(toggle) delete toggle;
577 LabelToggle::LabelToggle(MWindow *mwindow,
584 this->mwindow = mwindow;
588 LabelToggle::~LabelToggle() { }
590 int LabelToggle::handle_event()