label edit deadlock, build openexr cfg option, code cleanup
[goodguy/history.git] / cinelerra-5.1 / cinelerra / recordbatches.C
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <fcntl.h>
7
8 #include "asset.h"
9 #include "batch.h"
10 #include "bchash.h"
11 #include "bclistbox.h"
12 #include "channel.h"
13 #include "channeldb.h"
14 #include "filesystem.h"
15 #include "mwindow.h"
16 #include "record.h"
17 #include "recordbatches.h"
18 #include "timeentry.h"
19
20 const char * RecordBatches::
21 batch_titles[] = {
22         N_("On"), N_("Path"), N_("News"), N_("Start time"),
23         N_("Duration"), N_("Source"), N_("Mode")
24 };
25
26 const int RecordBatches::
27 default_columnwidth[] = {
28         30, 200, 100, 100, 100, 100, 70
29 };
30
31 void RecordBatches::
32 load_defaults(ChannelDB * channeldb, Record * record)
33 {
34         char string[BCTEXTLEN];
35         BC_Hash *defaults = mwindow->defaults;
36         defaults->get("RECORD_DEFAULT_DIRECTORY", default_directory);
37         early_margin = defaults->get("RECORD_EARLY_MARGIN", early_margin);
38         late_margin = defaults->get("RECORD_LATE_MARGIN", late_margin);
39         for(int i = 0; i < BATCH_COLUMNS; i++) {
40                 sprintf(string, "BATCH_COLUMNWIDTH_%d", i);
41                 column_widths[i] = defaults->get(string, default_columnwidth[i]);
42         }
43         int total_batches = defaults->get("TOTAL_BATCHES", 1);
44         if(total_batches < 1) total_batches = 1;
45         for(int i = 0; i < total_batches; ++i) {
46                 Batch *batch = new Batch(mwindow, record);
47                 batch->create_objects();
48                 batches.append(batch);
49                 Asset *asset = batch->asset;
50                 sprintf(string, "RECORD_PATH_%d", i);
51                 defaults->get(string, asset->path);
52                 sprintf(string, "RECORD_CHANNEL_%d", i);
53                 int chan_no = channeldb->number_of(batch->channel);
54                 chan_no = defaults->get(string, chan_no);
55                 batch->channel = channeldb->get(chan_no);
56                 sprintf(string, "RECORD_STARTDAY_%d", i);
57                 batch->start_day = defaults->get(string, batch->start_day);
58                 sprintf(string, "RECORD_STARTTIME_%d", i);
59                 batch->start_time = defaults->get(string, batch->start_time);
60                 sprintf(string, "RECORD_DURATION_%d", i);
61                 batch->duration = defaults->get(string, batch->duration);
62                 sprintf(string, "RECORD_MODE_%d", i);
63                 batch->record_mode = defaults->get(string, batch->record_mode);
64                 sprintf(string, "BATCH_ENABLED_%d", i);
65                 batch->enabled = defaults->get(string, batch->enabled);
66                 batch->update_times();
67         }
68
69         current_item = editing_item = 0;
70 }
71
72 void RecordBatches::
73 save_defaults(ChannelDB * channeldb)
74 {
75         char string[BCTEXTLEN];
76         BC_Hash *defaults = mwindow->defaults;
77         defaults->update("RECORD_DEFAULT_DIRECTORY", default_directory);
78         defaults->update("RECORD_EARLY_MARGIN", early_margin);
79         defaults->update("RECORD_LATE_MARGIN", late_margin);
80         for(int i = 0; i < BATCH_COLUMNS; i++) {
81                 sprintf(string, "BATCH_COLUMNWIDTH_%d", i);
82                 defaults->update(string, column_widths[i]);
83         }
84
85         defaults->update("TOTAL_BATCHES", batches.total);
86         for(int i = 0; i < batches.total; ++i) {
87                 Batch *batch = batches.values[i];
88                 Asset *asset = batch->asset;
89                 sprintf(string, "RECORD_PATH_%d", i);
90                 defaults->update(string, asset->path);
91                 sprintf(string, "RECORD_CHANNEL_%d", i);
92                 int chan_no = channeldb->number_of(batch->channel);
93                 defaults->update(string, chan_no);
94                 sprintf(string, "RECORD_STARTDAY_%d", i);
95                 defaults->update(string, batch->start_day);
96                 sprintf(string, "RECORD_STARTTIME_%d", i);
97                 defaults->update(string, batch->start_time);
98                 sprintf(string, "RECORD_DURATION_%d", i);
99                 defaults->update(string, batch->duration);
100                 sprintf(string, "RECORD_MODE_%d", i);
101                 defaults->update(string, batch->record_mode);
102                 sprintf(string, "BATCH_ENABLED_%d", i);
103                 defaults->update(string, batch->enabled);
104         }
105 }
106
107 void RecordBatches::
108 save_default_channel(ChannelDB * channeldb)
109 {
110         BC_Hash *defaults = mwindow->defaults;
111         Batch *batch = get_editing_batch();
112         if( !batch ) return;
113         int chan_no = channeldb->number_of(batch->channel);
114         if( chan_no < 0 ) return;
115         defaults->update("RECORD_CURRENT_CHANNEL", chan_no);
116 }
117
118 RecordBatches::
119 RecordBatches(MWindow * mwindow)
120 {
121         this->mwindow = mwindow;
122         editing_item = current_item = -1;
123         batch_active = 0;
124         early_margin = late_margin = 0.;
125         default_directory[0] = 0;
126 }
127
128 RecordBatches::
129 ~RecordBatches()
130 {
131 }
132
133 void RecordBatches::
134 remove(Batch *batch)
135 {
136         batches.remove(batch);
137         int total_batches = total();
138         if( current_item == editing_item ) {
139                 if(current_item >= total_batches)
140                         current_item = total_batches - 1;
141                 editing_item = current_item;
142         }
143         else {
144                 if(current_item > editing_item)
145                         --current_item;
146                 if( editing_item >= total_batches )
147                         editing_item = total_batches - 1;
148         }
149 }
150
151 void RecordBatches::
152 clear()
153 {
154         batches.remove_all_objects();
155         for(int j = 0; j < BATCH_COLUMNS; j++) {
156                 data[j].remove_all_objects();
157         }
158         current_item = editing_item = -1;
159 }
160
161 RecordBatchesGUI::
162 RecordBatchesGUI(RecordBatches &batches,
163         int x, int y, int w, int h)
164  : BC_ListBox(x, y, w, h, LISTBOX_TEXT,         // Display text list or icons
165                         batches.data,           // Each column has an ArrayList of BC_ListBoxItems.
166                         batches.batch_titles,   // Titles for columns.  Set to 0 for no titles
167                         batches.column_widths,  // width of each column
168                         BATCH_COLUMNS,          // Total columns.
169                         0,                      // Pixel of top of window.
170                         0,                      // If this listbox is a popup window
171                         LISTBOX_SINGLE,         // Select one item or multiple items
172                         ICON_LEFT,              // Position of icon relative to text of each item
173                         1),                     // Allow dragging
174    batches(batches)
175 {
176         dragging_item = 0;
177 }
178
179 // Do nothing for double clicks to protect active batch
180 int RecordBatchesGUI::
181 handle_event()
182 {
183         return 1;
184 }
185
186 int RecordBatchesGUI::
187 update(int highlighted_item, int recalc_positions)
188 {
189         return BC_ListBox::update(batches.data, batches.batch_titles,
190                         batches.column_widths, BATCH_COLUMNS,
191                         get_xposition(), get_yposition(),
192                         highlighted_item, recalc_positions);
193 }
194
195 int RecordBatchesGUI::
196 column_resize_event()
197 {
198         for(int i = 0; i < BATCH_COLUMNS; i++) {
199                 batches.column_widths[i] = get_column_width(i);
200         }
201         return 1;
202 }
203
204 int RecordBatchesGUI::
205 drag_start_event()
206 {
207         if(!BC_ListBox::drag_start_event()) return 0;
208         dragging_item = 1;
209         return 1;
210 }
211
212 int RecordBatchesGUI::
213 drag_motion_event()
214 {
215         if(!BC_ListBox::drag_motion_event()) return 0;
216         return 1;
217 }
218
219 int RecordBatchesGUI::
220 selection_changed()
221 {
222         int n = get_selection_number(0, 0);
223         if(n >= 0) {
224                 set_editing_batch(n);
225                 if(get_cursor_x() < batches.column_widths[0]) {
226                         Batch *batch = batches[n];
227                         batch->enabled = !batch->enabled;
228                         update_batches(n);
229                 }
230         }
231         return 1;
232 }
233
234 int RecordBatchesGUI::
235 drag_stop_event()
236 {
237         if(dragging_item) {
238                 int total_batches = batches.total();
239                 int src_item = editing_batch();
240                 Batch *src_batch = batches[src_item];
241                 int dst_item = get_highlighted_item();
242                 if(dst_item < 0) dst_item = total_batches;
243                 if(dst_item > src_item) --dst_item;
244
245                 for(int i = src_item; i < total_batches - 1; i++)
246                         batches[i] = batches[i + 1];
247                 for(int i = total_batches - 1; i > dst_item; --i)
248                         batches[i] = batches[i - 1];
249                 batches[dst_item] = src_batch;
250
251                 BC_ListBox::drag_stop_event();
252                 dragging_item = 0;
253                 set_editing_batch(dst_item);
254                 update_batches(dst_item);
255         }
256         return 0;
257 }
258
259 void RecordBatchesGUI::
260 update_batch_news(int item)
261 {
262         Batch *batch = batches[item];
263         batch->calculate_news();
264         batches.data[2].values[item]->set_text(batch->news);
265         update();  flush();
266 }
267
268 void RecordBatchesGUI::
269 update_batches(int selection_item)
270 {
271         time_t t;  time(&t);
272         char string[BCTEXTLEN], string2[BCTEXTLEN];
273
274         for(int j = 0; j < BATCH_COLUMNS; j++) {
275                 batches.data[j].remove_all_objects();
276         }
277         int total_batches = batches.total();
278         for(int i = 0; i < total_batches; i++) {
279                 Batch *batch = batches[i];
280                 batch->update_times();
281                 batch->calculate_news();
282
283                 int color = LTGREEN;
284                 if( i != batches.current_item ) {
285                         if( batch->time_start < t )
286                                 color = RED;
287                         else if( i > 0 && batches[i-1]->time_end > batch->time_start )
288                                 color = RED;
289                         else if( batch->time_start - t > 2*24*3600 )
290                                 color = YELLOW;
291                 }
292                 else
293                         color = LTYELLOW;
294
295                 batches.data[0].append(
296                         new BC_ListBoxItem((char *)(batch->enabled ? "X" : " "), color));
297                 batches.data[1].append(
298                         new BC_ListBoxItem(batch->asset->path, color));
299                 batches.data[2].append(
300                         new BC_ListBoxItem(batch->news, RED));
301                 Units::totext(string2, batch->start_time, TIME_HMS3);
302                 sprintf(string, "%s %s", TimeEntry::day_table[batch->start_day], string2);
303                 batches.data[3].append(
304                         new BC_ListBoxItem(string, color));
305                 Units::totext(string, batch->duration, TIME_HMS3);
306                 batches.data[4].append(
307                         new BC_ListBoxItem(string, color));
308                 sprintf(string, "%s", batch->channel ? batch->channel->title : _("None"));
309                 batches.data[5].append(
310                         new BC_ListBoxItem(string, color));
311                 sprintf(string, "%s", Batch::mode_to_text(batch->record_mode));
312                 batches.data[6].append(
313                         new BC_ListBoxItem(string, color));
314
315                 if(i == selection_item) {
316                         for(int j = 0; j < BATCH_COLUMNS; j++) {
317                                 batches.data[j].values[i]->set_selected(1);
318                         }
319                 }
320         }
321         update(batches.editing_item, 1);
322         flush();
323 }
324
325 void RecordBatchesGUI::
326 set_row_color(int row, int color)
327 {
328         for(int i = 0; i < BATCH_COLUMNS; i++) {
329                 BC_ListBoxItem *batch = batches.data[i].values[row];
330                 batch->set_color(color);
331         }
332 }
333
334
335 RecordBatchesGUI::Dir::
336 Dir(RecordBatches &batches, const char *dir, int x, int y)
337  : BC_TextBox(x, y, 200, 1, dir),
338    batches(batches),
339    directory(batches.default_directory)
340 {
341         strncpy(directory, dir, sizeof(directory));
342         dir_entries = new ArrayList<BC_ListBoxItem*>;
343         entries_dir[0] = 0;
344         load_dirs(dir);
345 }
346
347 RecordBatchesGUI::Dir::
348 ~Dir()
349 {
350         dir_entries->remove_all_objects();
351         delete dir_entries;
352 }
353
354 int RecordBatchesGUI::Dir::
355 handle_event()
356 {
357         char *path = FileSystem::basepath(directory);
358         load_dirs(path);
359         calculate_suggestions(dir_entries);
360         delete [] path;
361         return 1;
362 }
363
364 void RecordBatchesGUI::Dir::
365 load_dirs(const char *dir)
366 {
367         if( !strncmp(dir, entries_dir, sizeof(entries_dir)) ) return;
368         strncpy(entries_dir, dir, sizeof(entries_dir));
369         dir_entries->remove_all_objects();
370         FileSystem fs;  fs.update(dir);
371         int total_files = fs.total_files();
372         for(int i = 0; i < total_files; i++) {
373                 if( !fs.get_entry(i)->get_is_dir() ) continue;
374                 const char *name = fs.get_entry(i)->get_name();
375                 dir_entries->append(new BC_ListBoxItem(name));
376         }
377 }
378
379 RecordBatchesGUI::Path::
380 Path(RecordBatches &batches, int x, int y)
381  : BC_TextBox(x, y, 200, 1, batches.get_editing_batch()->asset->path),
382    batches(batches)
383 {
384 }
385
386 RecordBatchesGUI::Path::
387 ~Path()
388 {
389 }
390
391 int RecordBatchesGUI::Path::
392 handle_event()
393 {
394         calculate_suggestions();
395         Batch *batch = batches.gui->get_editing_batch();
396         strcpy(batch->asset->path, get_text());
397         batches.gui->update_batches();
398         return 1;
399 }
400
401 RecordBatchesGUI::StartTime::
402 StartTime(BC_Window *win, RecordBatches &batches, int x, int y, int w)
403  : TimeEntry(win, x, y,
404                  &batches.get_editing_batch()->start_day,
405                  &batches.get_editing_batch()->start_time, TIME_HMS3, w),
406    batches(batches)
407 {
408 }
409
410 int RecordBatchesGUI::StartTime::
411 handle_event()
412 {
413         batches.gui->update_batches();
414         return 1;
415 }
416
417
418 RecordBatchesGUI::Duration::
419 Duration(BC_Window *win, RecordBatches &batches, int x, int y, int w)
420  : TimeEntry(win, x, y, 0,
421                  &batches.get_editing_batch()->duration, TIME_HMS2, w),
422    batches(batches)
423 {
424 }
425
426 int RecordBatchesGUI::Duration::
427 handle_event()
428 {
429         batches.gui->update_batches();
430         return 1;
431 }
432
433
434 RecordBatchesGUI::Source::
435 Source(BC_Window *win, RecordBatches &batches, int x, int y)
436  : BC_PopupTextBox(win, &sources,
437                  batches.get_editing_batch()->get_source_text(),
438                  x, y, 200, 200),
439    batches(batches)
440 {
441 }
442
443 int RecordBatchesGUI::Source::
444 handle_event()
445 {
446         batches.gui->update_batches();
447         return 1;
448 }
449
450
451 RecordBatchesGUI::News::
452 News(RecordBatches &batches, int x, int y)
453  : BC_TextBox(x, y, 200, 1, batches.get_editing_batch()->news),
454    batches(batches)
455 {
456 }
457
458 int RecordBatchesGUI::News::
459 handle_event()
460 {
461         return 1;
462 }
463
464
465 RecordBatchesGUI::NewBatch::
466 NewBatch(RecordBatches &batches, int x, int y)
467  : BC_GenericButton(x, y, _("New")),
468    batches(batches)
469 {
470         set_tooltip(_("Create new clip."));
471 }
472
473 int RecordBatchesGUI::NewBatch::
474 handle_event()
475 {
476         int new_item = batches.total()-1;
477         batches.editing_item = new_item;
478         batches.gui->update_batches(new_item);
479         return 1;
480 }
481
482
483 RecordBatchesGUI::DeleteBatch::
484 DeleteBatch(RecordBatches &batches, int x, int y)
485  : BC_GenericButton(x, y, _("Delete")),
486    batches(batches)
487 {
488         set_tooltip(_("Delete clip."));
489 }
490
491 int RecordBatchesGUI::DeleteBatch::
492 handle_event()
493 {
494         batches.gui->update_batches();
495         return 1;
496 }
497
498
499 RecordBatchesGUI::StartBatches::
500 StartBatches(RecordBatches &batches, int x, int y)
501  : BC_GenericButton(x, y, _("Start")), batches(batches)
502 {
503         set_tooltip(_("Start batch recording\nfrom the current position."));
504 }
505
506 int RecordBatchesGUI::StartBatches::
507 handle_event()
508 {
509         batches.batch_active = 1;
510         return 1;
511 }
512
513
514 RecordBatchesGUI::StopBatches::
515 StopBatches(RecordBatches &batches, int x, int y)
516  : BC_GenericButton(x, y, _("Stop")), batches(batches)
517 {
518 }
519
520 int RecordBatchesGUI::StopBatches::
521 handle_event()
522 {
523         batches.batch_active = 0;
524         return 1;
525 }
526
527
528 RecordBatchesGUI::ActivateBatch::
529 ActivateBatch(RecordBatches &batches, int x, int y)
530  : BC_GenericButton(x, y, _("Activate")), batches(batches)
531 {
532         set_tooltip(_("Make the highlighted\nclip active."));
533 }
534
535 int RecordBatchesGUI::ActivateBatch::
536 handle_event()
537 {
538         return 1;
539 }
540
541
542 RecordBatchesGUI::ClearBatch::
543 ClearBatch(RecordBatches &batches, int x, int y)
544  : BC_GenericButton(x, y, _("Clear")), batches(batches)
545 {
546         set_tooltip(_("Delete all clips."));
547 }
548
549 int RecordBatchesGUI::ClearBatch::
550 handle_event()
551 {
552         batches.gui->update_batches();
553         return 1;
554 }
555