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