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 = xml_tag;
50 void Labels::dump(FILE *fp)
52 for(Label *current = first; current; current = NEXT)
54 fprintf(fp, " label: %f '%s'\n", current->position, current->textstr);
58 void Labels::insert_labels(Labels *labels, double start, double length, int paste_silence)
64 //printf("Labels::insert_labels 1 %f\n", start);
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, new_label->textstr));
102 append(new Label(edl, this, new_label->position + start, new_label->textstr));
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 xml->tag.set_title("LABELS");
233 xml->append_newline();
236 for(current = label_of(start);
237 current && current->position <= end;
240 xml->tag.set_title("LABEL");
241 xml->tag.set_property("TIME", (double)current->position - start);
242 xml->tag.set_property("TEXTSTR", current->textstr);
243 //printf("Labels::copy %f\n", current->position - start);
245 xml->tag.set_title("/LABEL");
247 xml->append_newline();
250 xml->tag.set_title("/LABELS");
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, current->textstr));
280 Labels& Labels::operator=(Labels &that)
283 printf("Labels::operator= 1\n");
288 int Labels::save(FileXML *xml)
289 // Note: Normally the saving of Labels is done by Labels::copy()
291 xml->tag.set_title("LABELS");
293 xml->append_newline();
297 for(current = first; current; current = NEXT)
299 xml->tag.set_title("LABEL");
300 xml->tag.set_property("TIME", (double)current->position);
301 xml->tag.set_property("TEXTSTR", current->textstr);
303 xml->tag.set_title("/LABEL");
305 xml->append_newline();
308 xml->append_newline();
309 xml->tag.set_title("/LABELS");
311 xml->append_newline();
312 xml->append_newline();
316 int Labels::load(FileXML *xml, uint32_t load_flags)
319 char string1[BCTEXTLEN], string2[BCTEXTLEN];
321 sprintf(string1, "/%s", xml_tag);
322 strcpy(string2, xml_tag);
323 string2[strlen(string2) - 1] = 0;
326 result = xml->read_tag();
329 if(xml->tag.title_is(string1))
334 if(xml->tag.title_is(string2))
336 double position = xml->tag.get_property("TIME", (double)-1);
338 position = xml->tag.get_property("SAMPLE", (double)-1);
339 //printf("Labels::load %f\n", position);
342 Label *current = label_of(position);
343 current = insert_before(current, new Label(edl, this, position, ""));
344 xml->tag.get_property("TEXTSTR", current->textstr);
348 if(xml->tag.title_is("INPOINT"))
350 double position = xml->tag.get_property("TIME", (double)-1);
352 position = xml->tag.get_property("SAMPLE", (double)-1);
359 if(xml->tag.title_is("OUTPOINT"))
361 double position = xml->tag.get_property("TIME", (double)-1);
363 position = xml->tag.get_property("SAMPLE", (double)-1);
376 int Labels::clear(double start, double end, int follow)
381 //printf("Labels::clear 1\n");
382 current = label_of(start);
383 //printf("Labels::clear 2\n");
384 // remove selected labels
385 while(current && current->position < end)
391 // Shift later labels
392 //printf("Labels::clear 3\n");
397 current->position -= end - start; // shift labels forward
400 //printf("Labels::clear 4\n");
402 //printf("Labels::clear 5\n");
409 Label* Labels::prev_label(double position)
413 // Test for label under cursor position
415 current && !edl->equivalent(current->position, position);
419 // Test for label after cursor position
422 current && current->position < position;
426 // Test for label before cursor position
430 // Get previous label
436 Label* Labels::next_label(double position)
440 // Test for label under cursor position
442 current && !edl->equivalent(current->position, position);
446 // Test for label before cursor position
449 current && current->position > position;
453 // Test for label after cursor position
463 int Labels::insert(double start, double length)
464 { // shift every label including the first one back
467 for(current = label_of(start); current; current = NEXT)
469 current->position += length;
474 int Labels::paste_silence(double start, double end)
476 insert(start, end - start);
481 int Labels::modify_handles(double oldposition,
488 handle_mode == MOVE_ALL_EDITS)
490 if(currentend == 0) // left handle
492 if(newposition < oldposition)
494 insert(oldposition, oldposition - newposition); // shift all labels right
498 clear(oldposition, newposition); // clear selection
503 if(newposition < oldposition)
505 clear(newposition, oldposition);
509 insert(oldposition, newposition - oldposition);
516 int Labels::optimize()
525 for(current = first; current && NEXT && !result;)
528 if(current->position == next->position)
539 Label* Labels::label_of(double position)
543 for(current = first; current; current = NEXT)
545 if(current->position >= position) return current;
564 Label::Label(EDL *edl, Labels *labels, double position, const char *textstr)
568 this->labels = labels;
569 this->position = position;
570 strcpy(this->textstr, textstr ? textstr : "");
576 // if(toggle) delete toggle;
579 LabelToggle::LabelToggle(MWindow *mwindow,
586 this->mwindow = mwindow;
590 LabelToggle::~LabelToggle() { }
592 int LabelToggle::handle_event()