r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / labels.C
bloba4c1d04a262cd0016019fd487e399ae14ffa5a9b
1 #include "clip.h"
2 #include "edl.h"
3 #include "edlsession.h"
4 #include "filexml.h"
5 #include "labels.h"
6 #include "mwindow.h"
7 #include "mwindowgui.h"
8 #include "patchbay.h"
9 #include "recordlabel.h"
10 #include "mainsession.h"
11 #include "stringfile.h"
12 #include "theme.h"
13 #include "timebar.h"
14 #include <string.h>
18 Labels::Labels(EDL *edl, char *xml_tag)
19  : List<Label>()
21         this->edl = edl;
22         this->xml_tag = xml_tag;
25 Labels::~Labels()
27         delete_all();
30 void Labels::dump()
32         for(Label *current = first; current; current = NEXT)
33         {
34                 printf("  label: %f '%s'\n", current->position, current->textstr);
35         }
38 void Labels::insert_labels(Labels *labels, double start, double length, int paste_silence)
40         Label *new_label;
41         Label *old_label;
44 //printf("Labels::insert_labels 1 %f\n", start);
46 // Insert silence in old labels
47         if(paste_silence)
48         {
49                 for(old_label = first; old_label; old_label = old_label->next)
50                 {
51                         if(old_label->position > start ||
52                                 edl->equivalent(old_label->position, start))
53                                 old_label->position += length;
54                 }
55         }
58 // Insert one new label at a time
59         for(new_label = labels->first; new_label; new_label = new_label->next)
60         {
61                 int exists = 0;
62 //printf("Labels::insert_labels 2 %f\n", new_label->position + start);
64 // Check every old label for existence
65                 for(old_label = first; old_label; old_label = old_label->next)
66                 {
67                         if(edl->equivalent(old_label->position, new_label->position + start))
68                         {
69                                 exists = 1;
70                                 break;
71                         }
72                         else
73                         if(old_label->position > new_label->position + start)
74                                 break;
75                 }
77                 if(!exists)
78                 {
79                         if(old_label)
80                                 insert_before(old_label, new Label(edl, this, new_label->position + start, new_label->textstr));
81                         else
82                                 append(new Label(edl, this, new_label->position + start, new_label->textstr));
83                 }
84         }
87 int Labels::toggle_label(double start, double end)
89         Label *current;
90 //printf("Labels::toggle_label 1 %f %f\n", start, end);
92 // handle selection start
93 // find label the selectionstart is after
94         for(current = first; 
95                 current && current->position < start && !edl->equivalent(current->position, start); 
96                 current = NEXT)
97         {
98 //printf("Labels::toggle_label 2 %f %f %f\n", start, end, current->position);
99                 ;
100         }
102         if(current)
103         {
104 //printf("Labels::toggle_label 3 %f %f %f\n", start, end, current->position);
105                 if(edl->equivalent(current->position, start))
106                 {        // remove it
107 //printf("Labels::toggle_label 1\n");
108                         remove(current);
109                 }
110                 else
111                 {        // insert before it
112                         current = insert_before(current, new Label(edl, this, start, ""));
113                 }
114         }
115         else
116         {           // insert after last
117 //printf("Labels::toggle_label 1\n");
118                 current = append(new Label(edl, this, start, ""));
119         }
121 // handle selection end
122         if(!EQUIV(start, end))
123         {
124 //printf("Labels::toggle_label 2 %.16e %.16e\n", start, end);
125 // find label the selectionend is after
126                 for(current = first; 
127                         current && current->position < end && !edl->equivalent(current->position, end); 
128                         current = NEXT)
129                 {
130                         ;
131                 }
133                 if(current)
134                 {
135                         if(edl->equivalent(current->position, end))
136                         {
137                                 remove(current);
138                         }
139                         else
140                         {
141                                 current = insert_before(current, new Label(edl, this, end, ""));
142                         }
143                 }
144                 else
145                 {
146                         current = append(new Label(edl, this, end, ""));
147                 }
148         }
149         return 0;
152 int Labels::delete_all()
154         while(last)
155                 remove(last);
156         return 0;
159 int Labels::copy(double start, double end, FileXML *xml)
161         char string[BCTEXTLEN];
162         xml->tag.set_title(xml_tag);
163         xml->append_tag();
164         xml->append_newline();
166         Label *current;
167         sprintf(string, xml_tag);
168         string[strlen(string) - 1] = 0;
169         for(current = label_of(start); 
170                 current && current->position <= end; 
171                 current = NEXT)
172         {
173                 xml->tag.set_title(string);
174                 xml->tag.set_property("TIME", (double)current->position - start);
175                 xml->tag.set_property("TEXTSTR", current->textstr);
176 //printf("Labels::copy %f\n", current->position - start);
177                 xml->append_tag();
178         }
179         
180         sprintf(string, "/%s", xml_tag);
181         xml->tag.set_title(string);
182         xml->append_tag();
183         xml->append_newline();
184         xml->append_newline();
185         return 0;
188 int Labels::copy_length(long start, long end) // return number of Labels in selection
190         int result = 0;
191         Label *current;
192         
193         for(current = label_of(start); current && current->position <= end; current = NEXT)
194         {
195                 result++;
196         }
197         return result;
200 void Labels::copy_from(Labels *labels)
202         while(last) delete last;
204         for(Label *current = labels->first; current; current = NEXT)
205         {
206                 append(new Label(edl, this, current->position, current->textstr));
207         }
211 Labels& Labels::operator=(Labels &that)
213         copy_from(&that);
214 printf("Labels::operator= 1\n");
215         return *this;
219 int Labels::save(FileXML *xml)
221         xml->tag.set_title("LABELS");
222         xml->append_tag();
223         xml->append_newline();
225         Label *current;
227         for(current = first; current; current = NEXT)
228         {
229                 xml->tag.set_title("LABEL");
230                 xml->tag.set_property("TIME", (double)current->position);
231                 xml->tag.set_property("TEXTSTR", current->textstr);
232                 xml->append_tag();
233         }
234         
235         xml->append_newline();
236         xml->tag.set_title("/LABELS");
237         xml->append_tag();
238         xml->append_newline();
239         xml->append_newline();
240         return 0;
243 int Labels::load(FileXML *xml, uint32_t load_flags)
245         int result = 0;
246         char string1[BCTEXTLEN], string2[BCTEXTLEN];
248         sprintf(string1, "/%s", xml_tag);
249         strcpy(string2, xml_tag);
250         string2[strlen(string2) - 1] = 0;
252         do{
253                 result = xml->read_tag();
254                 if(!result)
255                 {
256                         if(xml->tag.title_is(string1))
257                         {
258                                 result = 1;
259                         }
260                         else
261                         if(xml->tag.title_is(string2))
262                         {
263                                 double position = xml->tag.get_property("TIME", (double)-1);
264                                 if(position < 0)
265                                         position = xml->tag.get_property("SAMPLE", (double)-1);
266 //printf("Labels::load %f\n", position);
267                                 if(position > -1)
268                                 {
269                                         Label *current = label_of(position);
270                                         current = insert_before(current, new Label(edl, this, position, ""));
271                                         xml->tag.get_property("TEXTSTR", current->textstr);
272                                 }
273                         }
274                         else
275                         if(xml->tag.title_is("INPOINT"))
276                         {
277                                 double position = xml->tag.get_property("TIME", (double)-1);
278                                 if(position < 0)
279                                         position = xml->tag.get_property("SAMPLE", (double)-1);
280                                 if(position > -1)
281                                 {
282                                         ;
283                                 }
284                         }
285                         else
286                         if(xml->tag.title_is("OUTPOINT"))
287                         {
288                                 double position = xml->tag.get_property("TIME", (double)-1);
289                                 if(position < 0)
290                                         position = xml->tag.get_property("SAMPLE", (double)-1);
291                                 if(position > -1)
292                                 {
293                                         ;
294                                 }
295                         }
296                 }
297         }while(!result);
298         return 0;
303 int Labels::clear(double start, double end, int follow)
305         Label *current;
306         Label *next;
308 //printf("Labels::clear 1\n");
309         current = label_of(start);
310 //printf("Labels::clear 2\n");
311 // remove selected labels
312         while(current && current->position < end)
313         {
314                 next = NEXT;
315                 delete current;              
316                 current = next;
317         }
318 // Shift later labels
319 //printf("Labels::clear 3\n");
320         if(follow)
321         {
322                 while(current)
323                 {
324                         current->position -= end - start;   // shift labels forward
325                         current = NEXT;
326                 }
327 //printf("Labels::clear 4\n");
328                 optimize();
329 //printf("Labels::clear 5\n");
330         }
332         return 0;
336 Label* Labels::prev_label(double position)
338         Label *current;
340 // Test for label under cursor position
341         for(current = first; 
342                 current && !edl->equivalent(current->position, position); 
343                 current = NEXT)
344                 ;
346 // Test for label after cursor position
347         if(!current)
348                 for(current = first;
349                         current && current->position < position;
350                         current = NEXT)
351                         ;
353 // Test for label before cursor position
354         if(!current) 
355                 current = last;
356         else
357 // Get previous label
358                 current = PREVIOUS;
360         return current;
363 Label* Labels::next_label(double position)
365         Label *current;
367 // Test for label under cursor position
368         for(current = first; 
369                 current && !edl->equivalent(current->position, position); 
370                 current = NEXT)
371                 ;
373 // Test for label before cursor position
374         if(!current)
375                 for(current = last;
376                         current && current->position > position;
377                         current = PREVIOUS)
378                         ;
380 // Test for label after cursor position
381         if(!current)
382                 current = first;
383         else
384 // Get next label
385                 current = NEXT;
387         return current;
390 int Labels::insert(double start, double length)
391 {      // shift every label including the first one back
392         Label *current;
394         for(current = label_of(start); current; current = NEXT)
395         {
396                 current->position += length;
397         }
398         return 0;
401 int Labels::paste_silence(double start, double end)
403         insert(start, end - start);
404         optimize();
405         return 0;
408 int Labels::modify_handles(double oldposition, 
409         double newposition, 
410         int currentend, 
411         int handle_mode,
412         int edit_labels)
414         if(edit_labels &&
415                 handle_mode == MOVE_ALL_EDITS)
416         {
417                 if(currentend == 0)          // left handle
418                 {
419                         if(newposition < oldposition)
420                         {
421                                 insert(oldposition, oldposition - newposition);    // shift all labels right
422                         }
423                         else
424                         {
425                                 clear(oldposition, newposition);   // clear selection
426                         }
427                 }
428                 else
429                 {                            // right handle
430                         if(newposition < oldposition)
431                         {
432                                 clear(newposition, oldposition);
433                         }
434                         else
435                         {
436                                 insert(oldposition, newposition - oldposition);
437                         }
438                 }
439         }
440         return 0;
443 int Labels::optimize()
445         int result = 1;
446         Label *current;
448         while(result)
449         {
450                 result = 0;
451                 
452                 for(current = first; current && NEXT && !result;)
453                 {
454                         Label *next = NEXT;
455                         if(current->position == next->position)
456                         {
457                                 delete current;
458                                 result  = 1;
459                         }
460                         current = next;
461                 }
462         }
463         return 0;
466 Label* Labels::label_of(double position)
468         Label *current;
470         for(current = first; current; current = NEXT)
471         {
472                 if(current->position >= position) return current;
473         }
474         return 0;
486 Label::Label()
487  : ListItem<Label>()
491 Label::Label(EDL *edl, Labels *labels, double position, char *textstr = 0)
492  : ListItem<Label>()
494         this->edl = edl;
495         this->labels = labels;
496         this->position = position;
497         if (textstr)
498                 strcpy(this->textstr, textstr);
499         else
500                 strcpy(this->textstr, "");
504 Label::~Label()
506 //      if(toggle) delete toggle;
509 LabelToggle::LabelToggle(MWindow *mwindow, 
510         Label *label, 
511         int x, 
512         int y, 
513         long position)
514  : BC_Label(x, y, 0)
516         this->mwindow = mwindow;
517         this->label = label;
520 LabelToggle::~LabelToggle() { }
522 int LabelToggle::handle_event()
524         return 0;