dc884e55896a2d0729c7d07a58f15d7ab97dae87
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / awindowgui.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "arender.h"
23 #include "asset.h"
24 #include "assetedit.h"
25 #include "assetpopup.h"
26 #include "assetremove.h"
27 #include "assets.h"
28 #include "audiodevice.h"
29 #include "awindow.h"
30 #include "awindowgui.h"
31 #include "bccmodels.h"
32 #include "bcsignals.h"
33 #include "bchash.h"
34 #include "binfolder.h"
35 #include "cache.h"
36 #include "cstrdup.h"
37 #include "clip.h"
38 #include "clipedls.h"
39 #include "clippopup.h"
40 #include "cursors.h"
41 #include "cwindowgui.h"
42 #include "cwindow.h"
43 #include "edits.h"
44 #include "edit.h"
45 #include "edl.h"
46 #include "edlsession.h"
47 #include "effectlist.h"
48 #include "file.h"
49 #include "filesystem.h"
50 #include "folderlistmenu.h"
51 #include "indexable.h"
52 #include "keys.h"
53 #include "language.h"
54 #include "labels.h"
55 #include "labelpopup.h"
56 #include "localsession.h"
57 #include "mainerror.h"
58 #include "mainmenu.h"
59 #include "mainsession.h"
60 #include "mwindowgui.h"
61 #include "mwindow.h"
62 #include "preferences.h"
63 #include "proxy.h"
64 #include "proxypopup.h"
65 #include "renderengine.h"
66 #include "samples.h"
67 #include "theme.h"
68 #include "tracks.h"
69 #include "track.h"
70 #include "transportque.h"
71 #include "vframe.h"
72 #include "vicon.h"
73 #include "vrender.h"
74 #include "vwindowgui.h"
75 #include "vwindow.h"
76
77 #include "data/lad_picon_png.h"
78 #include "data/ff_audio_png.h"
79 #include "data/ff_video_png.h"
80
81 #include<stdio.h>
82 #include<unistd.h>
83 #include<fcntl.h>
84
85
86 const char *AWindowGUI::folder_names[] =
87 {
88         N_("Audio Effects"),
89         N_("Video Effects"),
90         N_("Audio Transitions"),
91         N_("Video Transitions"),
92         N_("Labels"),
93         N_("Clips"),
94         N_("Media"),
95         N_("Proxy"),
96 };
97
98 const char *AVIconDrawing::avicon_names[] =
99 {
100         N_("Full Play"),
101         N_("Mouse Over"),
102         N_("Src Target"),
103         N_("No Play"),
104 };
105
106 AssetVIcon::AssetVIcon(AssetPicon *picon, int w, int h, double framerate, int64_t length)
107  : VIcon(w, h, framerate), Garbage("AssetVIcon")
108 {
109         this->picon = picon;
110         this->length = length;
111         temp = 0;
112 }
113
114 AssetVIcon::~AssetVIcon()
115 {
116         picon->gui->vicon_thread->del_vicon(this);
117         delete temp;
118 }
119
120 VFrame *AssetVIcon::frame()
121 {
122         AssetVIconThread *avt = picon->gui->vicon_thread;
123         Indexable *idxbl = picon->indexable;
124         Asset *asset = idxbl && idxbl->is_asset ? (Asset *)idxbl : 0;
125         if( !asset ) return vframes()>0 ? (VFrame*)*images[0] : 0;
126         if( !asset->video_data && audio_data && audio_size && length > 0 ) {
127                 if( !temp ) temp = new VFrame(0, -1, w, h, BC_RGB888, -1);
128                 temp->clear_frame();
129                 int sample_rate = asset->get_sample_rate();
130                 int64_t audio_samples = asset->get_audio_samples();
131                 double duration = (double)audio_samples / sample_rate;
132                 picon->draw_hue_bar(temp, duration);
133                 int secs = length / frame_rate + 0.5;
134                 if( !secs ) secs = 1;
135                 int bfrsz = VICON_SAMPLE_RATE;
136                 int samples = audio_size/sizeof(vicon_audio_t);
137                 double line_pos = (double)seq_no/(length-1);
138                 int audio_pos = samples * line_pos * (secs-1)/secs;
139                 vicon_audio_t *audio_data = ((vicon_audio_t *)this->audio_data) + audio_pos;
140                 static const int mx = (1<<(8*sizeof(*audio_data)-1)) - 1;
141                 double data[bfrsz], sample_scale = 1./mx;
142                 int len = samples - audio_pos;
143                 if( len > bfrsz ) len = bfrsz;
144                 int i = 0;
145                 while( i < len ) data[i++] = *audio_data++ * sample_scale;
146                 while( i < bfrsz ) data[i++] = 0;
147                 picon->draw_wave(temp, data, bfrsz, RED, GREEN);
148                 int x = (w-1) * line_pos;
149                 temp->pixel_rgb = RED;
150                 temp->draw_line(x,0, x,h);
151                 return temp;
152         }
153         int vw = avt->vw, vh = avt->vh, vicon_cmodel = avt->vicon_cmodel;
154         if( !asset->video_data ) {
155                 if( !temp ) {
156                         temp = new VFrame(0, -1, vw, vh, BC_RGB888, -1);
157                         temp->clear_frame();
158                 }
159                 return temp;
160         }
161         if( seq_no >= images.size() ) {
162                 MWindow *mwindow = picon->mwindow;
163                 File *file = mwindow->video_cache->check_out(asset, mwindow->edl, 1);
164                 if( !file ) return 0;
165                 if( temp && (temp->get_w() != asset->width || temp->get_h() != asset->height) ) {
166                         delete temp;  temp = 0;
167                 }
168                 if( !temp )
169                         temp = new VFrame(0, -1, asset->width, asset->height, BC_RGB888, -1);
170                 while( seq_no >= images.size() ) {
171                         mwindow->video_cache->check_in(asset);
172                         Thread::yield();
173                         file = mwindow->video_cache->check_out(asset, mwindow->edl, 0);
174                         if( !file ) { usleep(1000);  continue; }
175                         file->set_layer(0);
176                         int64_t pos = images.size() / picon->gui->vicon_thread->refresh_rate * frame_rate;
177                         file->set_video_position(pos,0);
178                         if( file->read_frame(temp) ) temp->clear_frame();
179                         add_image(temp, vw, vh, vicon_cmodel);
180                 }
181                 mwindow->video_cache->check_in(asset);
182         }
183         return *images[seq_no];
184 }
185
186 int64_t AssetVIcon::set_seq_no(int64_t no)
187 {
188         if( no >= length ) no = 0;
189         return seq_no = no;
190 }
191
192 int AssetVIcon::get_vx()
193 {
194         BC_ListBox *lbox = picon->gui->asset_list;
195         return lbox->get_icon_x(picon);
196 }
197 int AssetVIcon::get_vy()
198 {
199         BC_ListBox *lbox = picon->gui->asset_list;
200         return lbox->get_icon_y(picon);
201 }
202
203 void AssetVIcon::load_audio()
204 {
205         MWindow *mwindow = picon->mwindow;
206         Asset *asset = (Asset *)picon->indexable;
207         File *file = mwindow->audio_cache->check_out(asset, mwindow->edl, 1);
208         int channels = asset->get_audio_channels();
209         if( channels > 2 ) channels = 2;
210         int sample_rate = asset->get_sample_rate();
211         int bfrsz = sample_rate;
212         Samples samples(bfrsz);
213         double time_scale = (double)sample_rate / VICON_SAMPLE_RATE;
214         vicon_audio_t *audio_data = (vicon_audio_t *)this->audio_data;
215         static const int mx = (1<<(8*sizeof(*audio_data)-1)) - 1;
216         double sample_scale = (double)mx / channels;
217         int audio_pos = 0, audio_len = audio_size/sizeof(vicon_audio_t);
218         while( audio_pos < audio_len ) {
219                 int64_t pos = audio_pos * time_scale;
220                 for( int ch=0; ch<channels; ++ch ) {
221                         file->set_channel(ch);
222                         file->set_audio_position(pos);
223                         file->read_samples(&samples, bfrsz);
224                         double *data = samples.get_data();
225                         for( int64_t k=audio_pos; k<audio_len; ++k ) {
226                                 int i = k * time_scale - pos;
227                                 if( i >= bfrsz ) break;
228                                 int v = audio_data[k] + data[i] * sample_scale;
229                                 audio_data[k] = CLIP(v, -mx,mx);
230                         }
231                 }
232                 audio_pos = (pos + bfrsz) / time_scale;
233         }
234         mwindow->audio_cache->check_in(asset);
235 }
236
237
238 AssetVIconAudio::AssetVIconAudio(AWindowGUI *gui)
239  : Thread(1, 0, 0)
240 {
241         this->gui = gui;
242         audio = new AudioDevice(gui->mwindow);
243         interrupted = 0;
244         vicon = 0;
245 }
246 AssetVIconAudio::~AssetVIconAudio()
247 {
248         delete audio;
249 }
250
251 void AssetVIconAudio::run()
252 {
253         int channels = 2;
254         int64_t bfrsz = VICON_SAMPLE_RATE;
255         MWindow *mwindow = gui->mwindow;
256         EDL *edl = mwindow->edl;
257         EDLSession *session = edl->session;
258         AudioOutConfig *aconfig = session->playback_config->aconfig;
259         audio->open_output(aconfig, VICON_SAMPLE_RATE, bfrsz, channels, 0);
260         audio->start_playback();
261         double out0[bfrsz], out1[bfrsz], *out[2] = { out0, out1 };
262         vicon_audio_t *audio_data = (vicon_audio_t *)vicon->audio_data;
263         static const int mx = (1<<(8*sizeof(*audio_data)-1)) - 1;
264
265         int audio_len = vicon->audio_size/sizeof(vicon_audio_t);
266         while( !interrupted ) {
267                 int len = audio_len - audio_pos;
268                 if( len <= 0 ) break;
269                 if( len > bfrsz ) len = bfrsz;
270                 int k = audio_pos;
271                 for( int i=0; i<len; ++i,++k )
272                         out0[i] = out1[i] = (double)audio_data[k] / mx;
273                 audio_pos = k;
274                 audio->write_buffer(out, channels, len);
275         }
276
277         if( !interrupted )
278                 audio->set_last_buffer();
279         audio->stop_audio(interrupted ? 0 : 1);
280         audio->close_all();
281 }
282
283 void AssetVIconAudio::start(AssetVIcon *vicon)
284 {
285         if( running() ) return;
286         interrupted = 0;
287         double pos = vicon->length > 0 ? (double)vicon->seq_no/vicon->length : 0;
288         int audio_len = vicon->audio_size/sizeof(vicon_audio_t);
289         audio_pos = pos * audio_len;
290         this->vicon = vicon;
291         Thread::start();
292 }
293
294 void AssetVIconAudio::stop(int wait)
295 {
296         if( running() && !interrupted ) {
297                 interrupted = 1;
298                 audio->stop_audio(wait);
299         }
300         Thread::join();
301         if( vicon ) {
302                 vicon->playing_audio = 0;
303                 vicon = 0;
304         }
305 }
306
307 void AssetVIcon::start_audio()
308 {
309         if( playing_audio < 0 ) return;
310         picon->gui->vicon_audio->stop(0);
311         playing_audio = 1;
312         picon->gui->vicon_audio->start(this);
313 }
314
315 void AssetVIcon::stop_audio()
316 {
317         if( playing_audio > 0 ) {
318                 picon->gui->vicon_audio->stop(0);
319                 playing_audio = 0;
320         }
321 }
322
323 AssetViewPopup::AssetViewPopup(VIconThread *vt, int draw_mode,
324                 int x, int y, int w, int h)
325  : ViewPopup(vt, x, y, w, h)
326 {
327         this->draw_mode = draw_mode;
328         this->bar_h = (VIEW_POPUP_BAR_H * h) / 200;
329         dragging = 0;
330 }
331
332 AssetViewPopup::~AssetViewPopup()
333 {
334 }
335
336 int AssetViewPopup::reposition_window(int x, int y, int w, int h)
337 {
338         this->bar_h = (VIEW_POPUP_BAR_H * h) / 200;
339         return ViewPopup::reposition_window(x, y, w, h);
340 }
341
342 int AssetViewPopup::button_press_event()
343 {
344         if( !is_event_win() ) return 0;
345         AssetVIconThread *avt = (AssetVIconThread *)vt;
346         if( !avt->vicon ) return 0;
347
348         int dir = 1;
349         switch( get_buttonpress() ) {
350         case LEFT_BUTTON:
351                 break;
352         case MIDDLE_BUTTON:
353                 avt->set_view_popup(0);
354                 return 1;
355         case WHEEL_DOWN:
356                 dir = -1; // fall thru
357         case WHEEL_UP:
358                 switch( draw_mode ) {
359                 case ASSET_VIEW_MEDIA:
360                 case ASSET_VIEW_MEDIA_MAP:
361                         avt->zoom_scale(dir);
362                         return 1;
363                 } // fall thru
364         default:
365                 return 0;
366         }
367
368         int x = get_cursor_x(), y = get_cursor_y();
369         AssetVIcon *vicon = (AssetVIcon *)avt->vicon;
370         AssetPicon *picon = vicon->picon;
371         MWindow *mwindow = picon->mwindow;
372         EDL *edl = mwindow->edl;
373         dragging = 0;
374         if( y < get_h()/2 ) {
375                 Indexable *idxbl =
376                         picon->indexable ? picon->indexable :
377                         picon->edl ? picon->edl : 0;
378                 if( !idxbl ) return 0;
379                 double sample_rate = idxbl->get_sample_rate();
380                 double audio_length = sample_rate > 0 && idxbl->have_audio() ?
381                         idxbl->get_audio_samples() / sample_rate : 0;
382                 double frame_rate = idxbl->get_frame_rate();
383                 double video_length = frame_rate > 0 && idxbl->have_video() ?
384                         idxbl->get_video_frames() / frame_rate : 0;
385                 double idxbl_length = bmax(audio_length, video_length);
386                 double pos = x * idxbl_length / get_w();
387                 double start = 0, end = idxbl_length;
388                 double lt = DBL_MAX, rt = DBL_MAX;
389                 for( Track *track=edl->tracks->first; track!=0; track=track->next ) {
390                         for( Edit *edit=track->edits->first; edit!=0; edit=edit->next ) {
391                                 Indexable *indexable = (Indexable *)edit->asset;
392                                 if( !indexable ) indexable = (Indexable *)edit->nested_edl;
393                                 if( !indexable ) continue;
394                                 if( indexable->id == idxbl->id ||
395                                     (!indexable->is_asset == !idxbl->is_asset &&
396                                      !strcmp(indexable->path, idxbl->path)) ) {
397                                         double start_pos = track->from_units(edit->startsource);
398                                         double end_pos = track->from_units(edit->startsource + edit->length);
399                                         double dlt = pos - start_pos, drt = end_pos - pos;
400                                         if( dlt >= 0 &&  dlt < lt ) { lt = dlt;  start = start_pos; }
401                                         else if( dlt < 0 && -dlt < rt ) { rt = -dlt;  end = start_pos; }
402                                         if( drt >= 0 &&  drt < rt ) { rt = drt;  end = end_pos; }
403                                         else if( drt < 0 && -drt < lt ) { lt = -drt; start = end_pos; }
404                                 }
405                         }
406                 }
407                 mwindow->gui->lock_window("AssetVIcon::popup_button_press");
408                 VWindow *vwindow = mwindow->get_viewer(1, 0);
409                 vwindow->change_source(idxbl);
410                 mwindow->gui->unlock_window();
411                 EDL *vedl = vwindow->get_edl();
412                 vedl->set_inpoint(start);
413                 vedl->set_outpoint(end);
414                 vedl->local_session->set_selectionstart(start);
415                 vedl->local_session->set_selectionend(start);
416                 vwindow->gui->lock_window("AssetViewPopup::button_press_event");
417                 vwindow->update_position();
418                 vwindow->gui->unlock_window();
419                 return 1;
420         }
421         else {
422                 dragging = 1;
423                 if( !ctrl_down() && !shift_down() )
424                         return cursor_motion_event();
425                 Indexable *idxbl =
426                         picon->indexable ? picon->indexable :
427                         picon->edl ? picon->edl : 0;
428                 if( !idxbl ) return 0;
429                 double total_length = mwindow->edl->tracks->total_length();
430                 double pos = x * total_length / get_w();
431                 double start = 0, end = total_length;
432                 double lt = DBL_MAX, rt = DBL_MAX;
433                 for( Track *track=edl->tracks->first; track!=0; track=track->next ) {
434                         for( Edit *edit=track->edits->first; edit!=0; edit=edit->next ) {
435                                 Indexable *indexable = (Indexable *)edit->asset;
436                                 if( !indexable ) indexable = (Indexable *)edit->nested_edl;
437                                 if( !indexable ) continue;
438                                 if( indexable->id == idxbl->id ||
439                                     (!indexable->is_asset == !idxbl->is_asset &&
440                                      !strcmp(indexable->path, idxbl->path)) ) {
441                                         double start_pos = track->from_units(edit->startproject);
442                                         double end_pos = track->from_units(edit->startproject + edit->length);
443                                         double dlt = pos - start_pos, drt = end_pos - pos;
444                                         if( dlt >= 0 &&  dlt < lt ) { lt = dlt;  start = start_pos; }
445                                         else if( dlt < 0 && -dlt < rt ) { rt = -dlt;  end = start_pos; }
446                                         if( drt >= 0 &&  drt < rt ) { rt = drt;  end = end_pos; }
447                                         else if( drt < 0 && -drt < lt ) { lt = -drt; start = end_pos; }
448                                 }
449                         }
450                 }
451                 mwindow->gui->lock_window("AssetVIcon::popup_button_press");
452                 mwindow->select_point(start);
453                 edl->local_session->set_selectionstart(start);
454                 edl->local_session->set_selectionend(!shift_down() ? start : end);
455                 mwindow->zoom_sample(edl->local_session->zoom_sample);
456                 mwindow->gui->unlock_window();
457                 return 1;
458         }
459         return 0;
460 }
461
462 int AssetViewPopup::button_release_event()
463 {
464         if( !is_event_win() ) return 0;
465         dragging = 0;
466         return 1;
467 }
468
469 int AssetViewPopup::cursor_motion_event()
470 {
471         AssetVIconThread *avt = (AssetVIconThread *)vt;
472         AssetVIcon *vicon = (AssetVIcon *)avt->vicon;
473         if( vicon && dragging && is_event_win() &&
474             get_button_down() && get_buttonpress() == LEFT_BUTTON &&
475             !ctrl_down() && !alt_down() && !shift_down() ) {
476                 switch( draw_mode ) {
477                 case ASSET_VIEW_MEDIA_MAP:
478                 case ASSET_VIEW_FULL:
479                         MWindow *mwindow = vicon->picon->mwindow;
480                         EDL *edl = mwindow->edl;
481                         int x = get_cursor_x();
482                         double total_length = edl->tracks->total_length();
483                         double pos = x * total_length / get_w();
484                         mwindow->gui->lock_window("AssetVIcon::popup_cursor_motion");
485                         mwindow->select_point(pos);
486                         mwindow->zoom_sample(edl->local_session->zoom_sample);
487                         mwindow->gui->unlock_window();
488                         return 1;
489                 }
490         }
491         return 0;
492 }
493
494 void AssetViewPopup::draw_vframe(VFrame *vframe) 
495 {
496         switch( draw_mode ) {
497         case ASSET_VIEW_MEDIA:
498         case ASSET_VIEW_ICON:
499                 ViewPopup::draw_vframe(vframe);
500         case ASSET_VIEW_NONE:
501         default:
502                 return;
503         case ASSET_VIEW_MEDIA_MAP:
504         case ASSET_VIEW_FULL:
505                 break;
506         }
507         set_color(BLACK);
508         draw_box(0,0,get_w(),get_h());
509         int y1 = bar_h;
510         int y2 = get_h()-bar_h;
511         BC_WindowBase::draw_vframe(vframe, 0,y1, get_w(),y2-y1);
512         AssetVIconThread *avt = (AssetVIconThread *)vt;
513         AssetVIcon *vicon = (AssetVIcon *)avt->vicon;
514         AssetPicon *picon = (AssetPicon *)vicon->picon;
515         Indexable *idxbl =
516                 picon->indexable ? picon->indexable :
517                 picon->edl ? picon->edl : 0;
518         if( !idxbl ) return;
519         double sample_rate = idxbl->get_sample_rate();
520         double audio_length = sample_rate > 0 && idxbl->have_audio() ?
521                 idxbl->get_audio_samples() / sample_rate : 0;
522         double frame_rate = idxbl->get_frame_rate();
523         double video_length = frame_rate > 0 && idxbl->have_video() ?
524                 idxbl->get_video_frames() / frame_rate : 0;
525         double idxbl_length = bmax(audio_length, video_length);
526         if( !idxbl_length ) idxbl_length = 1;
527
528         EDL *edl = picon->mwindow->edl;
529         double total_length = edl->tracks->total_length();
530         if( !total_length ) total_length = 1;
531         for( Track *track=edl->tracks->first; track!=0; track=track->next ) {
532                 if( !track->record ) continue;
533                 for( Edit *edit=track->edits->first; edit!=0; edit=edit->next ) {
534                         Indexable *indexable = (Indexable *)edit->asset;
535                         if( !indexable ) indexable = (Indexable *)edit->nested_edl;
536                         if( !indexable ) continue;
537                         if( indexable->id == idxbl->id ||
538                             (!indexable->is_asset == !idxbl->is_asset &&
539                              !strcmp(indexable->path, idxbl->path)) ) {
540                                 double pos1 = track->from_units(edit->startsource);
541                                 double pos2 = track->from_units(edit->startsource + edit->length);
542                                 double xscale = get_w() / idxbl_length;
543                                 int ex1 = pos1 * xscale, ex2 = pos2 * xscale;
544                                 set_color(WHITE);
545                                 draw_box(ex1,0, ex2-ex1,y1);
546                                 set_color(BLACK);
547                                 draw_line(ex1,0, ex1,y1);
548                                 draw_line(ex2,0, ex2,y1);
549                                 pos1 = track->from_units(edit->startproject);
550                                 pos2 = track->from_units(edit->startproject + edit->length);
551                                 xscale = get_w() / total_length;
552                                 int px1 = pos1 * xscale, px2 = pos2 * xscale;
553                                 set_color(RED);
554                                 draw_box(px1,y2, px2-px1,get_h()-y2);
555                                 set_color(BLACK);
556                                 draw_line(px1,y2, px1,get_h()-1);
557                                 draw_line(px2,y2, px2,get_h()-1);
558
559                                 set_color(YELLOW);
560                                 draw_line(ex1,y1, px1,y2);
561                                 draw_line(ex2,y1, px2,y2);
562                         }
563                 }
564         }
565 }
566
567 int AssetViewPopup::keypress_event()
568 {
569         int result = 0;
570         AssetVIconThread *avt = (AssetVIconThread *)vt;
571         switch( avt->draw_mode ) {
572         case ASSET_VIEW_MEDIA_MAP:
573                 switch( get_keypress() ) {
574                 case 'f':
575                 case 'F':
576                         avt->draw_mode = ASSET_VIEW_FULL;
577                         result = 1;
578                 }
579                 break;
580         case ASSET_VIEW_FULL:
581                 avt->draw_mode = ASSET_VIEW_MEDIA_MAP;
582                 result = 1;
583         }
584         if( result ) // zero change for refresh
585                 avt->zoom_scale(0);
586         return ViewPopup::keypress_event();
587 }
588
589
590 AssetVIconThread::AssetVIconThread(AWindowGUI *gui, Preferences *preferences)
591  : VIconThread(gui->asset_list, preferences->vicon_size * 16/9, preferences->vicon_size,
592         4*preferences->awindow_picon_h * 16/9, 4*preferences->awindow_picon_h)
593 {
594         this->gui = gui;
595         draw_mode = ASSET_VIEW_NONE;
596         int vicon_cmodel = BC_RGB8;
597         switch( preferences->vicon_color_mode ) {
598         case VICON_COLOR_MODE_LOW:   vicon_cmodel = BC_RGB8;    break;
599         case VICON_COLOR_MODE_MED:   vicon_cmodel = BC_RGB565;  break;
600         case VICON_COLOR_MODE_HIGH:  vicon_cmodel = BC_RGB888;  break;
601         }
602         this->vicon_cmodel = vicon_cmodel;
603         this->draw_lock = new Mutex("AssetVIconThread::draw_lock");
604 }
605
606 AssetVIconThread::~AssetVIconThread()
607 {
608         delete draw_lock;
609 }
610
611 void AssetVIconThread::drawing_started()
612 {
613         draw_lock->lock("AssetVIconThread::drawing_started");
614 }
615
616 void AssetVIconThread::drawing_stopped()
617 {
618         draw_lock->unlock();
619 }
620
621 void AssetVIconThread::set_view_popup(AssetVIcon *v, int draw_mode)
622 {
623         gui->stop_vicon_drawing();
624         this->draw_mode = draw_mode;
625         set_view_popup(v);
626         gui->start_vicon_drawing();
627 }
628
629 void AssetVIconThread::set_view_popup(AssetVIcon *v)
630 {
631         if( !v ) draw_mode = ASSET_VIEW_NONE;
632         VIconThread::set_view_popup(v);
633 }
634
635 ViewPopup *AssetVIconThread::new_view_window(ViewPopup *vpopup)
636 {
637         BC_WindowBase *parent = wdw->get_parent();
638         int rx = 0, ry = 0, rw = 0, rh = 0;
639         if( draw_mode != ASSET_VIEW_FULL ) {
640                 XineramaScreenInfo *info = parent->get_xinerama_info(-1);
641                 int cx = info ? info->x_org + info->width/2 : parent->get_root_w(0)/2;
642                 int cy = info ? info->y_org + info->height/2 : parent->get_root_h(0)/2;
643                 int vx = viewing->get_vx(), vy = viewing->get_vy();
644                 wdw->get_root_coordinates(vx, vy, &rx, &ry);
645                 rx += (rx >= cx ? -view_w+viewing->w/4 : viewing->w-viewing->w/4);
646                 ry += (ry >= cy ? -view_h+viewing->h/4 : viewing->h-viewing->h/4);
647                 rw = view_w;  rh = view_h;
648         }
649         else
650                 parent->get_fullscreen_geometry(rx, ry, rw, rh);
651         AssetViewPopup *av_popup = (AssetViewPopup *)vpopup;
652         if( av_popup )
653                 av_popup->reposition_window(rx, ry, rw, rh);
654         else
655                 av_popup = new AssetViewPopup(this, draw_mode, rx, ry, rw, rh);
656         int playing_audio = gui->play_off ? -1 : 0;
657         if( !playing_audio ) {
658                 switch( draw_mode ) {
659                 case ASSET_VIEW_NONE:
660                 case ASSET_VIEW_ICON:
661                 case ASSET_VIEW_MEDIA_MAP:
662                 case ASSET_VIEW_FULL:
663                         playing_audio = -1;
664                         break;
665                 case ASSET_VIEW_MEDIA:
666                         switch( gui->vicon_drawing ) {
667                         case AVICON_SRC_TARGET:
668                         case AVICON_NO_PLAY:
669                                 playing_audio = -1;
670                                 break;
671                         }
672                 }
673         }
674         vicon->playing_audio = playing_audio;
675         wdw->set_active_subwindow(av_popup);
676         return av_popup;
677 }
678
679 void AssetVIconThread::close_view_popup()
680 {
681         stop_drawing();
682         drawing_started(); // waits for draw lock
683         drawing_stopped();
684 }
685
686
687 AWindowFolderItem::AWindowFolderItem()
688  : BC_ListBoxItem()
689 {
690         parent = 0;
691 }
692
693 AWindowFolderItem::AWindowFolderItem(const char *text, int color)
694  : BC_ListBoxItem(text, color)
695 {
696         parent = 0;
697 }
698
699 AWindowFolderItem::AWindowFolderItem(const char *text, BC_Pixmap *icon, int color)
700  : BC_ListBoxItem(text, icon, color)
701 {
702         parent = 0;
703 }
704
705 AssetPicon *AWindowFolderItem::get_picon()
706 {
707         AWindowFolderItem *item = this;
708         while( item->parent ) { item = (AWindowFolderItem*)item->parent; }
709         return (AssetPicon*)item;
710 }
711
712 int AWindowFolderSubItems::matches(const char *text)
713 {
714         int i = names.size();
715         while( --i >= 0 && strcmp(names[i], text) );
716         if( i < 0 ) {
717                 ArrayList<BC_ListBoxItem *> *sublist = get_sublist();
718                 i = sublist ? sublist->size() : 0;
719                 while( --i >= 0 ) {
720                         AWindowFolderSubItems *item = (AWindowFolderSubItems *)sublist->get(i);
721                         if( item->matches(text) ) break;
722                 }
723         }
724         return i >= 0 ? 1 : 0;
725 }
726
727
728 AssetPicon::AssetPicon(MWindow *mwindow,
729         AWindowGUI *gui, Indexable *indexable)
730  : AWindowFolderItem()
731 {
732         reset();
733         this->mwindow = mwindow;
734         this->gui = gui;
735         this->indexable = indexable;
736         indexable->add_user();
737         this->id = indexable->id;
738 }
739
740 AssetPicon::AssetPicon(MWindow *mwindow,
741         AWindowGUI *gui, EDL *edl)
742  : AWindowFolderItem()
743 {
744         reset();
745         this->mwindow = mwindow;
746         this->gui = gui;
747         this->edl = edl;
748         edl->add_user();
749         this->id = edl->id;
750 }
751
752 AssetPicon::AssetPicon(MWindow *mwindow,
753         AWindowGUI *gui, int folder, int persist)
754  : AWindowFolderItem(_(AWindowGUI::folder_names[folder]),
755         folder>=0 && folder<AWINDOW_FOLDERS ?
756                 gui->folder_icons[folder] : gui->folder_icon)
757 {
758         reset();
759         foldernum = folder;
760         this->mwindow = mwindow;
761         this->gui = gui;
762         persistent = persist;
763 }
764
765 AssetPicon::AssetPicon(MWindow *mwindow,
766         AWindowGUI *gui, int folder, const char *title)
767  : AWindowFolderItem(title, gui->folder_icon)
768 {
769         reset();
770         foldernum = folder;
771         this->mwindow = mwindow;
772         this->gui = gui;
773         persistent = 0;
774 }
775
776 AssetPicon::AssetPicon(MWindow *mwindow,
777         AWindowGUI *gui, PluginServer *plugin)
778  : AWindowFolderItem()
779 {
780         reset();
781         this->mwindow = mwindow;
782         this->gui = gui;
783         this->plugin = plugin;
784 }
785
786 AssetPicon::AssetPicon(MWindow *mwindow,
787         AWindowGUI *gui, Label *label)
788  : AWindowFolderItem()
789 {
790         reset();
791         this->mwindow = mwindow;
792         this->gui = gui;
793         this->label = label;
794         indexable = 0;
795         icon = 0;
796         id = 0;
797 }
798
799 AssetPicon::~AssetPicon()
800 {
801         if( vicon ) vicon->remove_user();
802         delete vicon_frame;
803         if( indexable ) indexable->remove_user();
804         if( edl ) edl->remove_user();
805         if( icon && !gui->protected_pixmap(icon) ) {
806                 delete icon;
807                 if( !plugin ) delete icon_vframe;
808         }
809 }
810
811 void AssetPicon::draw_hue_bar(VFrame *frame, double duration)
812 {
813         float t = duration > 1 ? (log(duration) / log(3600.f)) : 0;
814         if( t > 1 ) t = 1;
815         float h = 300 * t, s = 1., v = 1.;
816         float r, g, b; // duration, 0..1hr == hue red..magenta
817         HSV::hsv_to_rgb(r,g,b, h,s,v);
818         int ih = frame->get_h()/8, iw = frame->get_w();
819         int ir = r * 256;  CLAMP(ir, 0,255);
820         int ig = g * 256;  CLAMP(ig, 0,255);
821         int ib = b * 256;  CLAMP(ib, 0,255);
822         unsigned char **rows = frame->get_rows();
823         for( int y=0; y<ih; ++y ) {
824                 unsigned char *rp = rows[y];
825                 for( int x=0; x<iw; rp+=3,++x ) {
826                         rp[0] = ir;  rp[1] = ig;  rp[2] = ib;
827                 }
828         }
829 }
830
831 void AssetPicon::draw_wave(VFrame *frame, double *dp, int len,
832                 int base_color, int line_color, int x, int y, int w, int h)
833 {
834         int h1 = h-1, h2 = h/2, iy = h2;
835         int rgb_color = frame->pixel_rgb;
836         for( int ix=0,x1=0,x2=0; ix<w; ++ix,x1=x2 ) {
837                 double min = *dp, max = min;
838                 x2 = (len * (ix+1))/w;
839                 for( int i=x1; i<x2; ++i ) {
840                         double value = *dp++;
841                         if( value < min ) min = value;
842                         if( value > max ) max = value;
843                 }
844                 int ctr = (min + max) / 2;
845                 int y0 = (int)(h2 - ctr*h2);  CLAMP(y0, 0,h1);
846                 int y1 = (int)(h2 - min*h2);  CLAMP(y1, 0,h1);
847                 int y2 = (int)(h2 - max*h2);  CLAMP(y2, 0,h1);
848                 frame->pixel_rgb = line_color;
849                 frame->draw_line(ix+x,y1+y, ix+x,y2+y);
850                 frame->pixel_rgb = base_color;
851                 frame->draw_line(ix+x,iy+y, ix+x,y0+y);
852                 iy = y0;
853         }
854         frame->pixel_rgb = rgb_color;
855 }
856
857 void AssetPicon::reset()
858 {
859         plugin = 0;
860         label = 0;
861         indexable = 0;
862         edl = 0;
863         parent = 0;
864         sub_items = 0;
865         foldernum = AW_NO_FOLDER;
866         sort_key = -1;
867         icon = 0;
868         icon_vframe = 0;
869         vicon = 0;
870         vicon_frame = 0;
871         in_use = 1;
872         comments_time = 0;
873         comments_rate = -1;
874         comments_ffmt = ' ';
875         comments_type = "";
876         id = 0;
877         persistent = 0;
878 }
879
880 void AssetPicon::open_render_engine(EDL *edl, int is_audio)
881 {
882         TransportCommand command;
883         command.command = is_audio ? NORMAL_FWD : CURRENT_FRAME;
884         command.get_edl()->copy_all(edl);
885         command.change_type = CHANGE_ALL;
886         command.realtime = 0;
887         render_engine = new RenderEngine(0, mwindow->preferences, 0, 0);
888         render_engine->set_vcache(mwindow->video_cache);
889         render_engine->set_acache(mwindow->audio_cache);
890         render_engine->arm_command(&command);
891 }
892 void AssetPicon::close_render_engine()
893 {
894         delete render_engine;  render_engine = 0;
895 }
896 void AssetPicon::render_video(int64_t pos, VFrame *vfrm)
897 {
898         if( !render_engine || !render_engine->vrender ) return;
899         render_engine->vrender->process_buffer(vfrm, pos, 0);
900 }
901 void AssetPicon::render_audio(int64_t pos, Samples **samples, int len)
902 {
903         if( !render_engine || !render_engine->arender ) return;
904         render_engine->arender->process_buffer(samples, len, pos);
905 }
906
907 void AssetPicon::create_objects()
908 {
909         FileSystem fs;
910         char name[BCTEXTLEN];
911         int pixmap_w, pixmap_h;
912
913         int picon_h = mwindow->preferences->awindow_picon_h;
914         pixmap_h = picon_h * BC_WindowBase::get_resources()->icon_scale;
915
916         Asset *asset = 0;
917         EDL *edl = 0;
918         int is_clip = 0;
919
920         if( this->indexable ) {
921                 char *cp = name;
922                 if( this->indexable->is_asset ) {
923                         asset = (Asset *)indexable;
924                         if( asset->format == FILE_REF ) {
925                                 cp += sprintf(cp, "ref:");
926                                 set_color(get_color() ^ 0x5599CC);
927                         }
928                 }
929                 else {
930                         edl = (EDL *)indexable;
931                         cp += sprintf(cp, "edl:");
932                         set_color(get_color() ^ 0xCC9955);
933                 }
934                 fs.extract_name(cp, indexable->path);
935                 set_text(name);
936         }
937         else if( this->edl ) {
938                 edl = this->edl;
939                 strcpy(name, edl->local_session->clip_title);
940                 set_text(name);
941                 is_clip = 1;
942         }
943
944         if( asset ) {
945                 if( asset->video_data ) {
946                         if( mwindow->preferences->use_thumbnails ) {
947                                 gui->unlock_window();
948                                 File *file = mwindow->video_cache->check_out(asset,
949                                         mwindow->edl,
950                                         1);
951
952                                 if( file ) {
953                                         int height = asset->height > 0 ? asset->height : 1;
954                                         pixmap_w = pixmap_h * asset->width / height;
955
956                                         file->set_layer(0);
957                                         file->set_video_position(0, 0);
958
959                                         if( gui->temp_picon &&
960                                                 (gui->temp_picon->get_w() != asset->width ||
961                                                 gui->temp_picon->get_h() != asset->height) ) {
962                                                 delete gui->temp_picon;
963                                                 gui->temp_picon = 0;
964                                         }
965
966                                         if( !gui->temp_picon ) {
967                                                 gui->temp_picon = new VFrame(0, -1,
968                                                         asset->width, asset->height,
969                                                         BC_RGB888, -1);
970                                         }
971                                         { char string[BCTEXTLEN];
972                                         sprintf(string, _("Reading %s"), name);
973                                         mwindow->gui->lock_window("AssetPicon::create_objects");
974                                         mwindow->gui->show_message(string);
975                                         mwindow->gui->unlock_window(); }
976                                         file->read_frame(gui->temp_picon);
977                                         mwindow->video_cache->check_in(asset);
978
979                                         gui->lock_window("AssetPicon::create_objects 0");
980                                         icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
981                                         icon->draw_vframe(gui->temp_picon,
982                                                 0, 0, pixmap_w, pixmap_h, 0, 0);
983                                         icon_vframe = new VFrame(0,
984                                                 -1, pixmap_w, pixmap_h, BC_RGB888, -1);
985                                         icon_vframe->transfer_from(gui->temp_picon);
986                                         if( asset->folder_no == AW_MEDIA_FOLDER ||
987                                             asset->folder_no == AW_PROXY_FOLDER ) {
988 // vicon images
989                                                 double framerate = asset->get_frame_rate();
990                                                 if( !framerate ) framerate = VICON_RATE;
991                                                 int64_t frames = asset->get_video_frames();
992                                                 double secs = frames / framerate;
993                                                 if( secs > 5 ) secs = 5;
994                                                 int64_t length = secs * gui->vicon_thread->refresh_rate;
995                                                 vicon = new AssetVIcon(this, pixmap_w, pixmap_h, framerate, length);
996                                                 if( asset->audio_data && secs > 0 ) {
997                                                         gui->unlock_window();
998                                                         int audio_len = VICON_SAMPLE_RATE*secs+0.5;
999                                                         vicon->init_audio(audio_len*sizeof(vicon_audio_t));
1000                                                         vicon->load_audio();
1001                                                         gui->lock_window("AssetPicon::create_objects 1");
1002                                                 }
1003                                                 gui->vicon_thread->add_vicon(vicon);
1004                                         }
1005                                 }
1006                                 else {
1007                                         eprintf("Unable to open %s\nin asset: %s",asset->path, get_text());
1008                                         gui->lock_window("AssetPicon::create_objects 2");
1009                                         icon = gui->video_icon;
1010                                         icon_vframe = gui->video_vframe;
1011                                 }
1012                         }
1013                         else {
1014                                 icon = gui->video_icon;
1015                                 icon_vframe = gui->video_vframe;
1016                         }
1017                 }
1018                 else
1019                 if( asset->audio_data ) {
1020                         if( mwindow->preferences->use_thumbnails ) {
1021                                 gui->unlock_window();
1022                                 File *file = mwindow->audio_cache->check_out(asset,
1023                                         mwindow->edl,
1024                                         1);
1025                                 if( file ) {
1026                                         pixmap_w = pixmap_h * 16/9;
1027                                         icon_vframe = new VFrame(0,
1028                                                 -1, pixmap_w, pixmap_h, BC_RGB888, -1);
1029                                         icon_vframe->clear_frame();
1030                                         { char string[BCTEXTLEN];
1031                                         sprintf(string, _("Reading %s"), name);
1032                                         mwindow->gui->lock_window("AssetPicon::create_objects 3");
1033                                         mwindow->gui->show_message(string);
1034                                         mwindow->gui->unlock_window(); }
1035                                         int sample_rate = asset->get_sample_rate();
1036                                         int channels = asset->get_audio_channels();
1037                                         if( channels > 2 ) channels = 2;
1038                                         int64_t audio_samples = asset->get_audio_samples();
1039                                         double duration = (double)audio_samples / sample_rate;
1040                                         draw_hue_bar(icon_vframe, duration);
1041                                         int bfrsz = sample_rate;
1042                                         Samples samples(bfrsz);
1043                                         static int line_colors[2] = { GREEN, YELLOW };
1044                                         static int base_colors[2] = { RED, PINK };
1045                                         for( int i=channels; --i>=0; ) {
1046                                                 file->set_channel(i);
1047                                                 file->set_audio_position(0);
1048                                                 file->read_samples(&samples, bfrsz);
1049                                                 draw_wave(icon_vframe, samples.get_data(), bfrsz,
1050                                                         base_colors[i], line_colors[i]);
1051                                         }
1052                                         mwindow->audio_cache->check_in(asset);
1053                                         if( asset->folder_no == AW_MEDIA_FOLDER ) {
1054                                                 double secs = duration;
1055                                                 if( secs > 5 ) secs = 5;
1056                                                 double refresh_rate = gui->vicon_thread->refresh_rate;
1057                                                 int64_t length = secs * refresh_rate;
1058                                                 vicon = new AssetVIcon(this, pixmap_w, pixmap_h, refresh_rate, length);
1059                                                 int audio_len = VICON_SAMPLE_RATE*secs+0.5;
1060                                                 vicon->init_audio(audio_len*sizeof(vicon_audio_t));
1061                                                 vicon->load_audio();
1062                                                 gui->vicon_thread->add_vicon(vicon);
1063                                         }
1064                                         gui->lock_window("AssetPicon::create_objects 4");
1065                                         icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
1066                                         icon->draw_vframe(icon_vframe,
1067                                                 0, 0, pixmap_w, pixmap_h, 0, 0);
1068                                 }
1069                                 else {
1070                                         eprintf("Unable to open %s\nin asset: %s",asset->path, get_text());
1071                                         gui->lock_window("AssetPicon::create_objects 5");
1072                                         icon = gui->audio_icon;
1073                                         icon_vframe = gui->audio_vframe;
1074                                 }
1075                         }
1076                         else {
1077                                 icon = gui->audio_icon;
1078                                 icon_vframe = gui->audio_vframe;
1079                         }
1080
1081                 }
1082                 struct stat st;
1083                 comments_time = !stat(asset->path, &st) ? st.st_mtime : 0;
1084                 comments_rate = asset->get_frame_rate();
1085                 comments_ffmt = asset->format == FILE_FFMPEG ? '=' : '-';
1086                 comments_type = asset->format == FILE_FFMPEG ?
1087                                 asset->vcodec : File::formattostr(asset->format);
1088         }
1089         else if( edl ) {
1090                 if( edl->tracks->playable_video_tracks() ) {
1091                         if( mwindow->preferences->use_thumbnails ) {
1092                                 gui->unlock_window();
1093                                 AssetVIconThread *avt = gui->vicon_thread;
1094                                 char clip_icon_path[BCTEXTLEN];
1095                                 char *clip_icon = edl->local_session->clip_icon;
1096                                 VFrame *vframe = 0;
1097                                 if( is_clip && clip_icon[0] ) {
1098                                         snprintf(clip_icon_path, sizeof(clip_icon_path),
1099                                                 "%s/%s", File::get_config_path(), clip_icon);
1100                                         vframe = VFramePng::vframe_png(clip_icon_path);
1101                                 }
1102                                 if( vframe &&
1103                                     ( vframe->get_w() != avt->vw || vframe->get_h() != avt->vh ) ) {
1104                                         delete vframe;  vframe = 0;
1105                                 }
1106                                 int edl_h = edl->get_h(), edl_w = edl->get_w();
1107                                 int height = edl_h > 0 ? edl_h : 1;
1108                                 int width = edl_w > 0 ? edl_w : 1;
1109                                 int color_model = edl->session->color_model;
1110                                 if( !vframe ) {
1111 //printf("render clip: %s\n", name);
1112                                         VFrame::get_temp(gui->temp_picon, width, height, color_model);
1113                                         char string[BCTEXTLEN];
1114                                         sprintf(string, _("Rendering %s"), name);
1115                                         mwindow->gui->lock_window("AssetPicon::create_objects");
1116                                         mwindow->gui->show_message(string);
1117                                         mwindow->gui->unlock_window();
1118                                         open_render_engine(edl, 0);
1119                                         render_video(0, gui->temp_picon);
1120                                         close_render_engine();
1121                                         vframe = new VFrame(avt->vw, avt->vh, BC_RGB888);
1122                                         vframe->transfer_from(gui->temp_picon);
1123                                         if( this->edl && clip_icon[0] )
1124                                                 vframe->write_png(clip_icon_path);
1125                                 }
1126                                 pixmap_w = pixmap_h * width / height;
1127                                 vicon = new AssetVIcon(this, pixmap_w, pixmap_h, VICON_RATE, 1);
1128                                 vicon->add_image(vframe, avt->vw, avt->vh, avt->vicon_cmodel);
1129                                 icon_vframe = new VFrame(pixmap_w, pixmap_h, BC_RGB888);
1130                                 icon_vframe->transfer_from(vframe);
1131                                 delete vframe;
1132                                 gui->lock_window("AssetPicon::create_objects 0");
1133                                 icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
1134                                 icon->draw_vframe(icon_vframe,
1135                                         0, 0, pixmap_w, pixmap_h, 0, 0);
1136                         }
1137                         else if( is_clip ) {
1138                                 icon = gui->clip_icon;
1139                                 icon_vframe = gui->clip_vframe;
1140                         }
1141                         else {
1142                                 icon = gui->video_icon;
1143                                 icon_vframe = gui->video_vframe;
1144                         }
1145                 }
1146                 else
1147                 if( edl->tracks->playable_audio_tracks() ) {
1148                         if( mwindow->preferences->use_thumbnails ) {
1149                                 gui->unlock_window();
1150                                 char clip_icon_path[BCTEXTLEN];
1151                                 char *clip_icon = edl->local_session->clip_icon;
1152                                 if( is_clip && clip_icon[0] ) {
1153                                         snprintf(clip_icon_path, sizeof(clip_icon_path),
1154                                                 "%s/%s", File::get_config_path(), clip_icon);
1155                                         icon_vframe = VFramePng::vframe_png(clip_icon_path);
1156                                 }
1157                                 if( !icon_vframe ) {
1158                                         pixmap_w = pixmap_h * 16/9;
1159                                         icon_vframe = new VFrame(0,
1160                                                 -1, pixmap_w, pixmap_h, BC_RGB888, -1);
1161                                         icon_vframe->clear_frame();
1162                                         char string[BCTEXTLEN];
1163                                         sprintf(string, _("Rendering %s"), name);
1164                                         mwindow->gui->lock_window("AssetPicon::create_objects 3");
1165                                         mwindow->gui->show_message(string);
1166                                         mwindow->gui->unlock_window();
1167                                         int sample_rate = edl->get_sample_rate();
1168                                         int channels = edl->get_audio_channels();
1169                                         if( channels > 2 ) channels = 2;
1170                                         int64_t audio_samples = edl->get_audio_samples();
1171                                         double duration = (double)audio_samples / sample_rate;
1172                                         draw_hue_bar(icon_vframe, duration);
1173                                         Samples *samples[MAX_CHANNELS];
1174                                         int bfrsz = sample_rate;
1175                                         for( int i=0; i<MAX_CHANNELS; ++i )
1176                                                 samples[i] = i<channels ? new Samples(bfrsz) : 0;
1177                                         open_render_engine(edl, 1);
1178                                         render_audio(0, samples, bfrsz);
1179                                         close_render_engine();
1180                                         gui->lock_window("AssetPicon::create_objects 4");
1181                                         static int line_colors[2] = { GREEN, YELLOW };
1182                                         static int base_colors[2] = { RED, PINK };
1183                                         for( int i=channels; --i>=0; ) {
1184                                                 draw_wave(icon_vframe, samples[i]->get_data(), bfrsz,
1185                                                         base_colors[i], line_colors[i]);
1186                                         }
1187                                         for( int i=0; i<channels; ++i ) delete samples[i];
1188                                         if( is_clip && clip_icon[0] )
1189                                                 icon_vframe->write_png(clip_icon_path);
1190                                 }
1191                                 else {
1192                                         pixmap_w = icon_vframe->get_w();
1193                                         pixmap_h = icon_vframe->get_h();
1194                                 }
1195                                 icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
1196                                 icon->draw_vframe(icon_vframe,
1197                                         0, 0, pixmap_w, pixmap_h, 0, 0);
1198                         }
1199                         else if( !indexable ) {
1200                                 icon = gui->clip_icon;
1201                                 icon_vframe = gui->clip_vframe;
1202                         }
1203                         else {
1204                                 icon = gui->audio_icon;
1205                                 icon_vframe = gui->audio_vframe;
1206                         }
1207                 }
1208         }
1209         else if( plugin ) {
1210                 strcpy(name, _(plugin->title));
1211                 set_text(name);
1212                 icon_vframe = plugin->get_picon();
1213                 if( icon_vframe )
1214                         icon = gui->create_pixmap(icon_vframe);
1215                 else if( plugin->audio ) {
1216                         if( plugin->transition ) {
1217                                 icon = gui->atransition_icon;
1218                                 icon_vframe = gui->atransition_vframe;
1219                         }
1220                         else if( plugin->is_ffmpeg() ) {
1221                                 icon = gui->ff_aud_icon;
1222                                 icon_vframe = gui->ff_aud_vframe;
1223                         }
1224                         else if( plugin->is_ladspa() ) {
1225                                 icon = gui->ladspa_icon;
1226                                 icon_vframe = gui->ladspa_vframe;
1227                         }
1228                         else {
1229                                 icon = gui->aeffect_icon;
1230                                 icon_vframe = gui->aeffect_vframe;
1231                         }
1232                 }
1233                 else if( plugin->video ) {
1234                         if( plugin->transition ) {
1235                                 icon = gui->vtransition_icon;
1236                                 icon_vframe = gui->vtransition_vframe;
1237                         }
1238                         else if( plugin->is_ffmpeg() ) {
1239                                 icon = gui->ff_vid_icon;
1240                                 icon_vframe = gui->ff_vid_vframe;
1241                         }
1242                         else {
1243                                 icon = gui->veffect_icon;
1244                                 icon_vframe = gui->veffect_vframe;
1245                         }
1246                 }
1247         }
1248         else if( label ) {
1249                 Units::totext(name,
1250                               label->position,
1251                               mwindow->edl->session->time_format,
1252                               mwindow->edl->session->sample_rate,
1253                               mwindow->edl->session->frame_rate,
1254                               mwindow->edl->session->frames_per_foot);
1255                 set_text(name);
1256                 icon = gui->label_icon;
1257                 icon_vframe = gui->label_vframe;
1258         }
1259         if( !icon ) {
1260                 icon = gui->file_icon;
1261                 icon_vframe = BC_WindowBase::get_resources()->type_to_icon[ICON_UNKNOWN];
1262         }
1263         set_icon(icon);
1264         set_icon_vframe(icon_vframe);
1265 }
1266
1267 AWindowGUI::AWindowGUI(MWindow *mwindow, AWindow *awindow)
1268  : BC_Window(_(PROGRAM_NAME ": Resources"),
1269         mwindow->session->awindow_x, mwindow->session->awindow_y,
1270         mwindow->session->awindow_w, mwindow->session->awindow_h,
1271         xS(100), yS(100), 1, 1, 1)
1272 {
1273         this->mwindow = mwindow;
1274         this->awindow = awindow;
1275
1276         file_vframe = 0;                file_icon = 0;
1277         folder_vframe = 0;              folder_icon = 0;
1278         audio_vframe = 0;               audio_icon = 0;
1279         video_vframe = 0;               video_icon = 0;
1280         label_vframe = 0;               label_icon = 0;
1281
1282         aeffect_folder_vframe = 0;      aeffect_folder_icon = 0;
1283         atransition_folder_vframe = 0;  atransition_folder_icon = 0;
1284         clip_folder_vframe = 0;         clip_folder_icon = 0;
1285         label_folder_vframe = 0;        label_folder_icon = 0;
1286         media_folder_vframe = 0;        media_folder_icon = 0;
1287         proxy_folder_vframe = 0;        proxy_folder_icon = 0;
1288         veffect_folder_vframe = 0;      veffect_folder_icon = 0;
1289         vtransition_folder_vframe = 0;  vtransition_folder_icon = 0;
1290
1291         ladspa_vframe = 0;              ladspa_icon = 0;
1292         ff_aud_vframe = 0;              ff_aud_icon = 0;
1293         ff_vid_vframe = 0;              ff_vid_icon = 0;
1294
1295         clip_vframe = 0;                clip_icon = 0;
1296         atransition_vframe = 0;         atransition_icon = 0;
1297         vtransition_vframe = 0;         vtransition_icon = 0;
1298         aeffect_vframe = 0;             aeffect_icon = 0;
1299         veffect_vframe = 0;             veffect_icon = 0;
1300
1301         plugin_visibility = ((uint64_t)1<<(8*sizeof(uint64_t)-1))-1;
1302         asset_menu = 0;
1303         effectlist_menu = 0;
1304         assetlist_menu = 0;
1305         cliplist_menu = 0;
1306         proxylist_menu = 0;
1307         labellist_menu = 0;
1308         folderlist_menu = 0;
1309         temp_picon = 0;
1310         search_text = 0;
1311         allow_iconlisting = 1;
1312         remove_plugin = 0;
1313         vicon_thread = 0;
1314         vicon_audio = 0;
1315         vicon_drawing = AVICON_FULL_PLAY;
1316         play_off = 0;
1317         tip_info = 0;
1318         displayed_folder = AW_NO_FOLDER;
1319         new_folder_thread = 0;
1320         modify_folder_thread = 0;
1321         folder_lock = new Mutex("AWindowGUI::folder_lock");
1322 }
1323
1324 AWindowGUI::~AWindowGUI()
1325 {
1326         assets.remove_all_objects();
1327         folders.remove_all_objects();
1328         aeffects.remove_all_objects();
1329         veffects.remove_all_objects();
1330         atransitions.remove_all_objects();
1331         vtransitions.remove_all_objects();
1332         labellist.remove_all_objects();
1333         displayed_assets[1].remove_all_objects();
1334
1335         delete new_folder_thread;
1336         delete modify_folder_thread;
1337         delete vicon_thread;
1338         delete vicon_audio;
1339
1340         delete search_text;
1341         delete temp_picon;
1342         delete remove_plugin;
1343
1344         delete file_vframe;             delete file_icon;
1345         delete folder_vframe;           delete folder_icon;
1346         delete audio_vframe;            delete audio_icon;
1347         delete video_vframe;            delete video_icon;
1348         delete label_vframe;            delete label_icon;
1349         delete clip_vframe;             delete clip_icon;
1350         delete aeffect_folder_vframe;   delete aeffect_folder_icon;
1351         delete atransition_folder_vframe; delete atransition_folder_icon;
1352         delete veffect_folder_vframe;   delete veffect_folder_icon;
1353         delete vtransition_folder_vframe; delete vtransition_folder_icon;
1354         delete clip_folder_vframe;      delete clip_folder_icon;
1355         delete label_folder_vframe;     delete label_folder_icon;
1356         delete media_folder_vframe;     delete media_folder_icon;
1357         delete proxy_folder_vframe;     delete proxy_folder_icon;
1358         delete ladspa_vframe;           delete ladspa_icon;
1359         delete ff_aud_vframe;           delete ff_aud_icon;
1360         delete ff_vid_vframe;           delete ff_vid_icon;
1361         delete atransition_vframe;      delete atransition_icon;
1362         delete vtransition_vframe;      delete vtransition_icon;
1363         delete aeffect_vframe;          delete aeffect_icon;
1364         delete veffect_vframe;          delete veffect_icon;
1365         delete folder_lock;
1366 }
1367
1368 bool AWindowGUI::protected_pixmap(BC_Pixmap *icon)
1369 {
1370         return  icon == file_icon ||
1371                 icon == folder_icon ||
1372                 icon == audio_icon ||
1373                 icon == video_icon ||
1374                 icon == clip_icon ||
1375                 icon == label_icon ||
1376                 icon == vtransition_icon ||
1377                 icon == atransition_icon ||
1378                 icon == veffect_icon ||
1379                 icon == aeffect_icon ||
1380                 icon == ladspa_icon ||
1381                 icon == ff_aud_icon ||
1382                 icon == ff_vid_icon ||
1383                 icon == aeffect_folder_icon ||
1384                 icon == veffect_folder_icon ||
1385                 icon == atransition_folder_icon ||
1386                 icon == vtransition_folder_icon ||
1387                 icon == label_folder_icon ||
1388                 icon == clip_folder_icon ||
1389                 icon == media_folder_icon ||
1390                 icon == proxy_folder_icon;
1391 }
1392
1393 VFrame *AWindowGUI::get_picon(const char *name, const char *plugin_icons)
1394 {
1395         char png_path[BCTEXTLEN];
1396         char *pp = png_path, *ep = pp + sizeof(png_path)-1;
1397         snprintf(pp, ep-pp, "%s/picon/%s/%s.png",
1398                 File::get_plugin_path(), plugin_icons, name);
1399         if( access(png_path, R_OK) ) return 0;
1400         return VFramePng::vframe_png(png_path,0,0);
1401 }
1402
1403 VFrame *AWindowGUI::get_picon(const char *name)
1404 {
1405         VFrame *vframe = get_picon(name, mwindow->preferences->plugin_icons);
1406         if( !vframe ) {
1407                 char png_name[BCSTRLEN], *pp = png_name, *ep = pp + sizeof(png_name)-1;
1408                 snprintf(pp, ep-pp, "%s.png", name);
1409                 unsigned char *data = mwindow->theme->get_image_data(png_name);
1410                 if( data ) vframe = new VFramePng(data, 0.);
1411         }
1412         return vframe;
1413 }
1414
1415 void AWindowGUI::resource_icon(VFrame *&vfrm, BC_Pixmap *&icon, const char *fn, int idx)
1416 {
1417         vfrm = get_picon(fn);
1418         if( !vfrm ) vfrm = BC_WindowBase::get_resources()->type_to_icon[idx];
1419         icon = new BC_Pixmap(this, vfrm, PIXMAP_ALPHA);
1420 }
1421 void AWindowGUI::theme_icon(VFrame *&vfrm, BC_Pixmap *&icon, const char *fn)
1422 {
1423         vfrm = get_picon(fn);
1424         if( !vfrm ) vfrm = mwindow->theme->get_image(fn);
1425         icon = new BC_Pixmap(this, vfrm, PIXMAP_ALPHA);
1426 }
1427 void AWindowGUI::plugin_icon(VFrame *&vfrm, BC_Pixmap *&icon, const char *fn, unsigned char *png)
1428 {
1429         vfrm = get_picon(fn);
1430         if( !vfrm ) vfrm = new VFramePng(png);
1431         icon = new BC_Pixmap(this, vfrm, PIXMAP_ALPHA);
1432 }
1433
1434 void AWindowGUI::create_objects()
1435 {
1436         int ys5 = yS(5), ys10 = yS(10);
1437         lock_window("AWindowGUI::create_objects");
1438         asset_titles[0] = C_("Title");
1439         asset_titles[1] = _("Comments");
1440
1441         set_icon(mwindow->theme->get_image("awindow_icon"));
1442
1443         resource_icon(file_vframe,   file_icon,   "film_icon",   ICON_UNKNOWN);
1444         resource_icon(folder_vframe, folder_icon, "folder_icon", ICON_FOLDER);
1445         resource_icon(audio_vframe,  audio_icon,  "audio_icon",  ICON_SOUND);
1446         resource_icon(video_vframe,  video_icon,  "video_icon",  ICON_FILM);
1447         resource_icon(label_vframe,  label_icon,  "label_icon",  ICON_LABEL);
1448
1449         theme_icon(aeffect_folder_vframe,      aeffect_folder_icon,     "aeffect_folder");
1450         theme_icon(atransition_folder_vframe,  atransition_folder_icon, "atransition_folder");
1451         theme_icon(clip_folder_vframe,         clip_folder_icon,        "clip_folder");
1452         theme_icon(label_folder_vframe,        label_folder_icon,       "label_folder");
1453         theme_icon(media_folder_vframe,        media_folder_icon,       "media_folder");
1454         theme_icon(proxy_folder_vframe,        proxy_folder_icon,       "proxy_folder");
1455         theme_icon(veffect_folder_vframe,      veffect_folder_icon,     "veffect_folder");
1456         theme_icon(vtransition_folder_vframe,  vtransition_folder_icon, "vtransition_folder");
1457
1458         folder_icons[AW_AEFFECT_FOLDER] = aeffect_folder_icon;
1459         folder_icons[AW_VEFFECT_FOLDER] = veffect_folder_icon;
1460         folder_icons[AW_ATRANSITION_FOLDER] = atransition_folder_icon;
1461         folder_icons[AW_VTRANSITION_FOLDER] = vtransition_folder_icon;
1462         folder_icons[AW_LABEL_FOLDER] = label_folder_icon;
1463         folder_icons[AW_CLIP_FOLDER] = clip_folder_icon;
1464         folder_icons[AW_MEDIA_FOLDER] = media_folder_icon;
1465         folder_icons[AW_PROXY_FOLDER] = proxy_folder_icon;
1466
1467         theme_icon(clip_vframe,        clip_icon,        "clip_icon");
1468         theme_icon(atransition_vframe, atransition_icon, "atransition_icon");
1469         theme_icon(vtransition_vframe, vtransition_icon, "vtransition_icon");
1470         theme_icon(aeffect_vframe,     aeffect_icon,     "aeffect_icon");
1471         theme_icon(veffect_vframe,     veffect_icon,     "veffect_icon");
1472
1473         plugin_icon(ladspa_vframe, ladspa_icon, "lad_picon", lad_picon_png);
1474         plugin_icon(ff_aud_vframe, ff_aud_icon, "ff_audio",  ff_audio_png);
1475         plugin_icon(ff_vid_vframe, ff_vid_icon, "ff_video",  ff_video_png);
1476         folder_lock->lock("AWindowGUI::create_objects");
1477 // Mandatory folders
1478         folders.append(new AssetPicon(mwindow, this, AW_AEFFECT_FOLDER, 1));
1479         folders.append(new AssetPicon(mwindow, this, AW_VEFFECT_FOLDER, 1));
1480         folders.append(new AssetPicon(mwindow, this, AW_ATRANSITION_FOLDER, 1));
1481         folders.append(new AssetPicon(mwindow, this, AW_VTRANSITION_FOLDER, 1));
1482         folders.append(new AssetPicon(mwindow, this, AW_LABEL_FOLDER, 1));
1483         folders.append(new AssetPicon(mwindow, this, AW_CLIP_FOLDER, 1));
1484         folders.append(new AssetPicon(mwindow, this, AW_PROXY_FOLDER, 1));
1485         folders.append(new AssetPicon(mwindow, this, AW_MEDIA_FOLDER, 1));
1486
1487         create_label_folder();
1488         folder_lock->unlock();
1489
1490         mwindow->theme->get_awindow_sizes(this);
1491         load_defaults(mwindow->defaults);
1492         new_folder_thread = new NewFolderThread(this);
1493         modify_folder_thread = new ModifyFolderThread(this);
1494
1495         int x1 = mwindow->theme->alist_x, y1 = mwindow->theme->alist_y;
1496         int w1 = mwindow->theme->alist_w, h1 = mwindow->theme->alist_h;
1497         search_text = new AWindowSearchText(mwindow, this, x1, y1+ys5);
1498         search_text->create_objects();
1499         int dy = search_text->get_h() + ys10;
1500         y1 += dy;  h1 -= dy;
1501         add_subwindow(asset_list = new AWindowAssets(mwindow, this, x1, y1, w1, h1));
1502
1503         vicon_thread = new AssetVIconThread(this, mwindow->preferences);
1504         asset_list->update_vicon_area();
1505         vicon_thread->start();
1506         vicon_audio = new AssetVIconAudio(this);
1507
1508         add_subwindow(divider = new AWindowDivider(mwindow, this,
1509                 mwindow->theme->adivider_x, mwindow->theme->adivider_y,
1510                 mwindow->theme->adivider_w, mwindow->theme->adivider_h));
1511
1512         divider->set_cursor(HSEPARATE_CURSOR, 0, 0);
1513
1514         int fx = mwindow->theme->afolders_x, fy = mwindow->theme->afolders_y;
1515         int fw = mwindow->theme->afolders_w, fh = mwindow->theme->afolders_h;
1516         int n = sizeof(AVIconDrawing::avicon_names)/sizeof(AVIconDrawing::avicon_names[0]);
1517         int tw = 0;  const char **av_names = AVIconDrawing::avicon_names;
1518         for( int i=0; i<n; ++i ) {
1519                 int nw = get_text_width(MEDIUMFONT, _(av_names[i]));
1520                 if( tw < nw )  tw = nw;
1521         }
1522         int pw = BC_PopupMenu::calculate_w(xS(16), tw, 1);
1523         const char *text = _(AVIconDrawing::avicon_names[vicon_drawing]);
1524         add_subwindow(avicon_drawing = new AVIconDrawing(this, fw, fy, pw, text));
1525         avicon_drawing->create_objects();
1526         add_subwindow(add_tools = new AddTools(mwindow, this, fx, fy, _("Visibility")));
1527         add_tools->create_objects();
1528         fy += add_tools->get_h();  fh -= add_tools->get_h();
1529         add_subwindow(folder_list = new AWindowFolders(mwindow,
1530                 this, fx, fy, fw, fh));
1531         update_effects();
1532         folder_list->load_expanders();
1533
1534         //int x = mwindow->theme->abuttons_x;
1535         //int y = mwindow->theme->abuttons_y;
1536
1537         add_subwindow(asset_menu = new AssetPopup(mwindow, this));
1538         asset_menu->create_objects();
1539         add_subwindow(clip_menu = new ClipPopup(mwindow, this));
1540         clip_menu->create_objects();
1541         add_subwindow(label_menu = new LabelPopup(mwindow, this));
1542         label_menu->create_objects();
1543         add_subwindow(proxy_menu = new ProxyPopup(mwindow, this));
1544         proxy_menu->create_objects();
1545
1546         add_subwindow(effectlist_menu = new EffectListMenu(mwindow, this));
1547         effectlist_menu->create_objects();
1548         add_subwindow(assetlist_menu = new AssetListMenu(mwindow, this));
1549         assetlist_menu->create_objects();
1550         add_subwindow(cliplist_menu = new ClipListMenu(mwindow, this));
1551         cliplist_menu->create_objects();
1552         add_subwindow(labellist_menu = new LabelListMenu(mwindow, this));
1553         labellist_menu->create_objects();
1554         add_subwindow(proxylist_menu = new ProxyListMenu(mwindow, this));
1555         proxylist_menu->create_objects();
1556
1557         add_subwindow(folderlist_menu = new FolderListMenu(mwindow, this));
1558         folderlist_menu->create_objects();
1559
1560         create_custom_xatoms();
1561         unlock_window();
1562 }
1563
1564 int AWindowGUI::resize_event(int w, int h)
1565 {
1566         mwindow->session->awindow_x = get_x();
1567         mwindow->session->awindow_y = get_y();
1568         mwindow->session->awindow_w = w;
1569         mwindow->session->awindow_h = h;
1570
1571         mwindow->theme->get_awindow_sizes(this);
1572         mwindow->theme->draw_awindow_bg(this);
1573         reposition_objects();
1574
1575 //      int x = mwindow->theme->abuttons_x;
1576 //      int y = mwindow->theme->abuttons_y;
1577 //      new_bin->reposition_window(x, y);
1578 //      x += new_bin->get_w();
1579 //      delete_bin->reposition_window(x, y);
1580 //      x += delete_bin->get_w();
1581 //      rename_bin->reposition_window(x, y);
1582 //      x += rename_bin->get_w();
1583 //      delete_disk->reposition_window(x, y);
1584 //      x += delete_disk->get_w();
1585 //      delete_project->reposition_window(x, y);
1586 //      x += delete_project->get_w();
1587 //      info->reposition_window(x, y);
1588 //      x += info->get_w();
1589 //      redraw_index->reposition_window(x, y);
1590 //      x += redraw_index->get_w();
1591 //      paste->reposition_window(x, y);
1592 //      x += paste->get_w();
1593 //      append->reposition_window(x, y);
1594 //      x += append->get_w();
1595 //      view->reposition_window(x, y);
1596
1597         BC_WindowBase::resize_event(w, h);
1598         asset_list->update_vicon_area();
1599         return 1;
1600 }
1601
1602 int AWindowGUI::translation_event()
1603 {
1604         mwindow->session->awindow_x = get_x();
1605         mwindow->session->awindow_y = get_y();
1606         return 0;
1607 }
1608
1609 void AWindowGUI::reposition_objects()
1610 {
1611         int ys5 = yS(5), ys10 = yS(10);
1612         int x1 = mwindow->theme->alist_x, y1 = mwindow->theme->alist_y;
1613         int w1 = mwindow->theme->alist_w, h1 = mwindow->theme->alist_h;
1614         search_text->reposition_window(x1, y1+ys5, w1);
1615         int dy = search_text->get_h() + ys10;
1616         y1 += dy;  h1 -= dy;
1617         asset_list->reposition_window(x1, y1, w1, h1);
1618         divider->reposition_window(
1619                 mwindow->theme->adivider_x, mwindow->theme->adivider_y,
1620                 mwindow->theme->adivider_w, mwindow->theme->adivider_h);
1621         int fx = mwindow->theme->afolders_x, fy = mwindow->theme->afolders_y;
1622         int fw = mwindow->theme->afolders_w, fh = mwindow->theme->afolders_h;
1623         add_tools->resize_event(fw-avicon_w, add_tools->get_h());
1624         avicon_drawing->reposition_window(fw-avicon_drawing->get_w(), fy);
1625         fy += add_tools->get_h();  fh -= add_tools->get_h();
1626         folder_list->reposition_window(fx, fy, fw, fh);
1627 }
1628
1629 int AWindowGUI::save_defaults(BC_Hash *defaults)
1630 {
1631         defaults->update("PLUGIN_VISIBILTY", plugin_visibility);
1632         defaults->update("VICON_DRAWING", vicon_drawing);
1633         defaults->update("TIP_INFO", tip_info);
1634         return 0;
1635 }
1636
1637 int AWindowGUI::load_defaults(BC_Hash *defaults)
1638 {
1639         plugin_visibility = defaults->get("PLUGIN_VISIBILTY", plugin_visibility);
1640         vicon_drawing = defaults->get("VICON_DRAWING", vicon_drawing);
1641         tip_info = defaults->get("TIP_INFO", tip_info);
1642         return 0;
1643 }
1644
1645 int AWindowGUI::close_event()
1646 {
1647         hide_window();
1648         mwindow->session->show_awindow = 0;
1649         unlock_window();
1650
1651         mwindow->gui->lock_window("AWindowGUI::close_event");
1652         mwindow->gui->mainmenu->show_awindow->set_checked(0);
1653         mwindow->gui->unlock_window();
1654
1655         lock_window("AWindowGUI::close_event");
1656         save_defaults(mwindow->defaults);
1657         mwindow->save_defaults();
1658         return 1;
1659 }
1660
1661 int AWindowGUI::start_vicon_drawing()
1662 {
1663         if( play_off ) return stop_vicon_drawing();
1664         if( !vicon_thread->interrupted ) return 1;
1665         switch( vicon_drawing ) {
1666         case AVICON_FULL_PLAY:  // all vicons, viewing, target
1667                 break;
1668         case AVICON_MOUSE_OVER: // one vicon, viewing, target
1669                 if( vicon_thread->solo ) break;
1670         // fall thru
1671         case AVICON_SRC_TARGET: // no vicons, no view, target
1672         case AVICON_NO_PLAY:    // no vicons, no view, no target
1673                 return 0;
1674         }
1675         if( mwindow->edl->session->awindow_folder == AW_MEDIA_FOLDER ||
1676             mwindow->edl->session->awindow_folder == AW_PROXY_FOLDER ||
1677             mwindow->edl->session->awindow_folder == AW_CLIP_FOLDER ||
1678             mwindow->edl->session->awindow_folder >= AWINDOW_USER_FOLDERS ) {
1679                 switch( mwindow->edl->session->assetlist_format ) {
1680                 case ASSETS_ICONS:
1681                 case ASSETS_ICONS_PACKED:
1682                 case ASSETS_ICON_LIST:
1683                         asset_list->update_vicon_area();
1684                         vicon_thread->start_drawing();
1685                         break;
1686                 default:
1687                         break;
1688                 }
1689         }
1690         return 1;
1691 }
1692
1693 int AWindowGUI::stop_vicon_drawing()
1694 {
1695         if( !vicon_thread->interrupted )
1696                 vicon_thread->stop_drawing();
1697         return 0;
1698 }
1699
1700 void AWindowGUI::close_view_popup()
1701 {
1702         vicon_thread->close_view_popup();
1703 }
1704
1705 VFrame *AssetPicon::get_vicon_frame()
1706 {
1707         if( !vicon ) return 0;
1708         if( gui->vicon_thread->interrupted ) return 0;
1709         VFrame *frame = vicon->frame();
1710         if( !frame ) return 0;
1711         if( !vicon_frame )
1712                 vicon_frame = new VFrame(vicon->w, vicon->h, frame->get_color_model());
1713         vicon_frame->transfer_from(frame);
1714         return vicon_frame;
1715 }
1716
1717 int AWindowGUI::cycle_assetlist_format()
1718 {
1719         EDLSession *session = mwindow->edl->session;
1720         int format = ASSETS_TEXT;
1721         if( allow_iconlisting ) {
1722                 switch( session->assetlist_format ) {
1723                 case ASSETS_TEXT:
1724                         format = ASSETS_ICONS;
1725                         break;
1726                 case ASSETS_ICONS:
1727                         format = ASSETS_ICONS_PACKED;
1728                         break;
1729                 case ASSETS_ICONS_PACKED:
1730                         format = ASSETS_ICON_LIST;
1731                         break;
1732                 case ASSETS_ICON_LIST:
1733                         format = ASSETS_TEXT;
1734                         break;
1735                 }
1736         }
1737         stop_vicon_drawing();
1738         session->assetlist_format = format;
1739         asset_list->update_format(session->assetlist_format, 0);
1740         async_update_assets();
1741         start_vicon_drawing();
1742         return 1;
1743 }
1744
1745 void AWindowGUI::hide_tip_info()
1746 {
1747         asset_list->hide_tooltip();
1748 }
1749
1750
1751 AWindowRemovePluginGUI::
1752 AWindowRemovePluginGUI(AWindow *awindow, AWindowRemovePlugin *thread,
1753         int x, int y, PluginServer *plugin)
1754  : BC_Window(_(PROGRAM_NAME ": Remove plugin"), x,y,
1755                 xS(500),yS(200), xS(50), yS(50),
1756                 1, 0, 1, -1, "", 1)
1757 {
1758         this->awindow = awindow;
1759         this->thread = thread;
1760         this->plugin = plugin;
1761         VFrame *vframe = plugin->get_picon();
1762         icon = vframe ? create_pixmap(vframe) : 0;
1763         plugin_list.append(new BC_ListBoxItem(plugin->title, icon));
1764 }
1765
1766 AWindowRemovePluginGUI::
1767 ~AWindowRemovePluginGUI()
1768 {
1769         if( !awindow->gui->protected_pixmap(icon) )
1770                 delete icon;
1771         plugin_list.remove_all();
1772 }
1773
1774 void AWindowRemovePluginGUI::create_objects()
1775 {
1776         int xs10 = xS(10), xs20 = xS(20);
1777         int ys5 = yS(5), ys10 = yS(10);
1778         lock_window("AWindowRemovePluginGUI::create_objects");
1779         BC_Button *ok_button = new BC_OKButton(this);
1780         add_subwindow(ok_button);
1781         BC_Button *cancel_button = new BC_CancelButton(this);
1782         add_subwindow(cancel_button);
1783         int x = xs10, y = ys10;
1784         BC_Title *title = new BC_Title(x, y, _("remove plugin?"));
1785         add_subwindow(title);
1786         y += title->get_h() + ys5;
1787         list = new BC_ListBox(x, y,
1788                 get_w() - xs20, ok_button->get_y() - y - ys5, LISTBOX_TEXT, &plugin_list,
1789                 0, 0, 1, 0, 0, LISTBOX_SINGLE, ICON_LEFT, 0);
1790         add_subwindow(list);
1791         show_window();
1792         unlock_window();
1793 }
1794
1795 int AWindowRemovePlugin::remove_plugin(PluginServer *plugin, ArrayList<BC_ListBoxItem*> &folder)
1796 {
1797         int ret = 0;
1798         for( int i=0; i<folder.size(); ) {
1799                 AssetPicon *picon = (AssetPicon *)folder[i];
1800                 if( picon->plugin == plugin ) {
1801                         folder.remove_object_number(i);
1802                         ++ret;
1803                         continue;
1804                 }
1805                 ++i;
1806         }
1807         return ret;
1808 }
1809
1810 void AWindowRemovePlugin::handle_close_event(int result)
1811 {
1812         if( !result ) {
1813                 printf(_("remove %s\n"), plugin->path);
1814                 awindow->gui->lock_window("AWindowRemovePlugin::handle_close_event");
1815                 ArrayList<BC_ListBoxItem*> *folder =
1816                         plugin->audio ? plugin->transition ?
1817                                 &awindow->gui->atransitions :
1818                                 &awindow->gui->aeffects :
1819                         plugin->video ?  plugin->transition ?
1820                                 &awindow->gui->vtransitions :
1821                                 &awindow->gui->veffects :
1822                         0;
1823                 if( folder ) remove_plugin(plugin, *folder);
1824                 MWindow *mwindow = awindow->mwindow;
1825                 awindow->gui->unlock_window();
1826                 char plugin_path[BCTEXTLEN];
1827                 strcpy(plugin_path, plugin->path);
1828                 mwindow->plugindb->remove(plugin);
1829                 remove(plugin_path);
1830                 char index_path[BCTEXTLEN];
1831                 mwindow->create_defaults_path(index_path, PLUGIN_FILE);
1832                 remove(index_path);
1833                 char picon_path[BCTEXTLEN];
1834                 FileSystem fs;
1835                 snprintf(picon_path, sizeof(picon_path), "%s/picon",
1836                         File::get_plugin_path());
1837                 char png_name[BCSTRLEN], png_path[BCTEXTLEN];
1838                 plugin->get_plugin_png_name(png_name);
1839                 fs.update(picon_path);
1840                 for( int i=0; i<fs.dir_list.total; ++i ) {
1841                         char *fs_path = fs.dir_list[i]->path;
1842                         if( !fs.is_dir(fs_path) ) continue;
1843                         snprintf(png_path, sizeof(picon_path), "%s/%s",
1844                                 fs_path, png_name);
1845                         remove(png_path);
1846                 }
1847                 delete plugin;  plugin = 0;
1848                 awindow->gui->async_update_assets();
1849         }
1850 }
1851
1852 AWindowRemovePlugin::
1853 AWindowRemovePlugin(AWindow *awindow, PluginServer *plugin)
1854  : BC_DialogThread()
1855 {
1856         this->awindow = awindow;
1857         this->plugin = plugin;
1858 }
1859
1860 AWindowRemovePlugin::
1861 ~AWindowRemovePlugin()
1862 {
1863         close_window();
1864 }
1865
1866 BC_Window* AWindowRemovePlugin::new_gui()
1867 {
1868         int x = awindow->gui->get_abs_cursor_x(0);
1869         int y = awindow->gui->get_abs_cursor_y(0);
1870         AWindowRemovePluginGUI *gui = new AWindowRemovePluginGUI(awindow, this, x, y, plugin);
1871         gui->create_objects();
1872         return gui;
1873 }
1874
1875 int AWindowGUI::keypress_event()
1876 {
1877         switch( get_keypress() ) {
1878         case 'w': case 'W':
1879                 if( ctrl_down() ) {
1880                         close_event();
1881                         return 1;
1882                 }
1883                 break;
1884         case 'i':
1885                 tip_info = !tip_info ? 1 : 0;
1886                 if( !tip_info ) hide_tip_info();
1887                 return 1;
1888         case 'o':
1889                 if( !ctrl_down() && !shift_down() ) {
1890                         assetlist_menu->load_file->handle_event();
1891                         return 1;
1892                 }
1893                 break;
1894         case 'v':
1895                 return cycle_assetlist_format();
1896         case DELETE:
1897                 if( shift_down() && ctrl_down() ) {
1898                         PluginServer* plugin = selected_plugin();
1899                         if( !plugin ) break;
1900                         remove_plugin = new AWindowRemovePlugin(awindow, plugin);
1901                         unlock_window();
1902                         remove_plugin->start();
1903                         lock_window("AWindowGUI::keypress_event 1");
1904                         return 1;
1905                 }
1906                 collect_assets();
1907                 if( shift_down() ) {
1908                         mwindow->awindow->asset_remove->start();
1909                         return 1;
1910                 }
1911                 unlock_window();
1912                 mwindow->remove_assets_from_project(1, 1, 1,
1913                         mwindow->session->drag_assets,
1914                         mwindow->session->drag_clips);
1915                 lock_window("AWindowGUI::keypress_event 2");
1916                 return 1;
1917         case KEY_F1:
1918         case KEY_F2:
1919         case KEY_F3:
1920         case KEY_F4:
1921                 if( shift_down() && ctrl_down() ) {
1922                         resend_event(mwindow->gui);
1923                         return 1;
1924                 }
1925                 break;
1926         }
1927         return 0;
1928 }
1929
1930
1931
1932 int AWindowGUI::create_custom_xatoms()
1933 {
1934         UpdateAssetsXAtom = create_xatom("CWINDOWGUI_UPDATE_ASSETS");
1935         return 0;
1936 }
1937 int AWindowGUI::receive_custom_xatoms(xatom_event *event)
1938 {
1939         if( event->message_type == UpdateAssetsXAtom ) {
1940                 update_assets();
1941                 return 1;
1942         }
1943         return 0;
1944 }
1945
1946 void AWindowGUI::async_update_assets()
1947 {
1948         xatom_event event;
1949         event.message_type = UpdateAssetsXAtom;
1950         send_custom_xatom(&event);
1951 }
1952
1953
1954 void AWindowGUI::update_folder_list()
1955 {
1956         for( int i = 0; i < folders.total; i++ ) {
1957                 AssetPicon *picon = (AssetPicon*)folders.values[i];
1958                 picon->in_use = 0;
1959         }
1960
1961 // Search assets for folders
1962         for( int i = 0; i < mwindow->edl->folders.total; i++ ) {
1963                 BinFolder *bin_folder = mwindow->edl->folders[i];
1964                 int exists = 0;
1965
1966                 for( int j = 0; j < folders.total; j++ ) {
1967                         AssetPicon *picon = (AssetPicon*)folders[j];
1968                         if( !strcasecmp(picon->get_text(), bin_folder->title) ) {
1969                                 exists = 1;
1970                                 picon->in_use = 1;
1971                                 break;
1972                         }
1973                 }
1974
1975                 if( !exists ) {
1976                         const char *title = bin_folder->title;
1977                         int folder = bin_folder->awindow_folder;
1978                         AssetPicon *picon = new AssetPicon(mwindow, this, folder, title);
1979                         picon->create_objects();
1980                         folders.append(picon);
1981                 }
1982         }
1983
1984 // Delete unused non-persistent folders
1985         int do_autoplace = 0;
1986         for( int i=folders.total; --i>=0; ) {
1987                 AssetPicon *picon = (AssetPicon*)folders.values[i];
1988                 if( !picon->in_use && !picon->persistent ) {
1989                         delete picon;
1990                         folders.remove_number(i);
1991                         do_autoplace = 1;
1992                 }
1993         }
1994         if( do_autoplace )
1995                 folder_list->set_autoplacement(&folders, 0, 1);
1996 }
1997
1998 void AWindowGUI::create_persistent_folder(ArrayList<BC_ListBoxItem*> *output,
1999         int do_audio, int do_video, int is_realtime, int is_transition)
2000 {
2001         ArrayList<PluginServer*> plugin_list;
2002 // Get pointers to plugindb entries
2003         mwindow->search_plugindb(do_audio, do_video, is_realtime, is_transition,
2004                         0, plugin_list);
2005
2006         for( int i = 0; i < plugin_list.total; i++ ) {
2007                 PluginServer *server = plugin_list.values[i];
2008                 int visible = plugin_visibility & (1<<server->dir_idx);
2009                 if( !visible ) continue;
2010 // Create new listitem
2011                 AssetPicon *picon = new AssetPicon(mwindow, this, server);
2012                 picon->create_objects();
2013                 output->append(picon);
2014         }
2015 }
2016
2017 void AWindowGUI::create_label_folder()
2018 {
2019         Label *current;
2020         for( current = mwindow->edl->labels->first; current; current = NEXT ) {
2021                 AssetPicon *picon = new AssetPicon(mwindow, this, current);
2022                 picon->create_objects();
2023                 labellist.append(picon);
2024         }
2025 }
2026
2027
2028 void AWindowGUI::update_asset_list()
2029 {
2030         ArrayList<AssetPicon *> new_assets;
2031         for( int i = 0; i < assets.total; i++ ) {
2032                 AssetPicon *picon = (AssetPicon*)assets.values[i];
2033                 picon->in_use = 0;
2034         }
2035
2036         mwindow->gui->lock_window("AWindowGUI::update_asset_list");
2037 // Synchronize EDL clips
2038         for( int i=0; i<mwindow->edl->clips.size(); ++i ) {
2039                 int exists = 0;
2040
2041 // Look for clip in existing listitems
2042                 for( int j = 0; j < assets.total && !exists; j++ ) {
2043                         AssetPicon *picon = (AssetPicon*)assets.values[j];
2044
2045                         if( picon->id == mwindow->edl->clips[i]->id ) {
2046                                 picon->edl = mwindow->edl->clips[i];
2047                                 picon->set_text(mwindow->edl->clips[i]->local_session->clip_title);
2048                                 exists = 1;
2049                                 picon->in_use = 1;
2050                         }
2051                 }
2052
2053 // Create new listitem
2054                 if( !exists ) {
2055                         AssetPicon *picon = new AssetPicon(mwindow,
2056                                 this, mwindow->edl->clips[i]);
2057                         new_assets.append(picon);
2058                 }
2059         }
2060
2061 // Synchronize EDL assets
2062         for( Asset *current=mwindow->edl->assets->first; current; current=NEXT ) {
2063                 int exists = 0;
2064
2065 // Look for asset in existing listitems
2066                 for( int j = 0; j < assets.total && !exists; j++ ) {
2067                         AssetPicon *picon = (AssetPicon*)assets.values[j];
2068
2069                         if( picon->id == current->id ) {
2070                                 picon->indexable = current;
2071                                 picon->in_use = 1;
2072                                 exists = 1;
2073                         }
2074                 }
2075
2076 // Create new listitem
2077                 if( !exists ) {
2078                         AssetPicon *picon = new AssetPicon(mwindow,
2079                                 this, current);
2080                         new_assets.append(picon);
2081                 }
2082         }
2083
2084 // Synchronize nested EDLs
2085         for( int i=0; i<mwindow->edl->nested_edls.size(); ++i ) {
2086                 int exists = 0;
2087                 EDL *nested_edl = mwindow->edl->nested_edls[i];
2088
2089 // Look for asset in existing listitems
2090                 for( int j=0; j<assets.total && !exists; ++j ) {
2091                         AssetPicon *picon = (AssetPicon*)assets.values[j];
2092
2093                         if( picon->id == nested_edl->id ) {
2094                                 picon->indexable = nested_edl;
2095                                 picon->in_use = 1;
2096                                 exists = 1;
2097                         }
2098                 }
2099
2100 // Create new listitem
2101                 if( !exists ) {
2102                         AssetPicon *picon = new AssetPicon(mwindow,
2103                                 this, (Indexable*)nested_edl);
2104                         new_assets.append(picon);
2105                 }
2106         }
2107         mwindow->gui->unlock_window();
2108
2109         for( int i=0; i<new_assets.size(); ++i ) {
2110                 AssetPicon *picon = new_assets[i];
2111                 picon->create_objects();
2112                 if( picon->indexable )
2113                         picon->foldernum = AW_MEDIA_FOLDER;
2114                 else if( picon->edl )
2115                         picon->foldernum = AW_CLIP_FOLDER;
2116                 assets.append(picon);
2117         }
2118
2119         mwindow->gui->lock_window();
2120         mwindow->gui->default_message();
2121         mwindow->gui->unlock_window();
2122
2123         for( int i = assets.size() - 1; i >= 0; i-- ) {
2124                 AssetPicon *picon = (AssetPicon*)assets.get(i);
2125                 if( !picon->in_use ) {
2126                         delete picon;
2127                         assets.remove_number(i);
2128                         continue;
2129                 }
2130                 if( picon->indexable && picon->indexable->is_asset ) {
2131                         Asset *asset = (Asset *)picon->indexable;
2132                         struct stat st;
2133                         picon->comments_time = !stat(asset->path, &st) ? st.st_mtime : 0;
2134                         picon->comments_rate = asset->get_frame_rate();
2135                         picon->comments_ffmt = asset->format == FILE_FFMPEG ? '=' : '-';
2136                         picon->comments_type = asset->format == FILE_FFMPEG ?
2137                                 asset->vcodec : File::formattostr(asset->format);
2138                 }
2139         }
2140 }
2141
2142 void AWindowGUI::update_picon(Indexable *indexable)
2143 {
2144         VIcon *vicon = 0;
2145         for( int i = 0; i < assets.total; i++ ) {
2146                 AssetPicon *picon = (AssetPicon*)assets.values[i];
2147                 if( picon->indexable == indexable ||
2148                     picon->edl == (EDL *)indexable ) {
2149                         char name[BCTEXTLEN];
2150                         FileSystem fs;
2151                         fs.extract_name(name, indexable->path);
2152                         picon->set_text(name);
2153                         vicon = picon->vicon;
2154                         break;
2155                 }
2156         }
2157         if( vicon ) {
2158                 stop_vicon_drawing();
2159                 vicon->clear_images();
2160                 vicon->reset(indexable->get_frame_rate());
2161                 start_vicon_drawing();
2162         }
2163 }
2164
2165 void AWindowGUI::sort_assets()
2166 {
2167         folder_lock->lock("AWindowGUI::sort_assets");
2168         switch( mwindow->edl->session->awindow_folder ) {
2169         case AW_AEFFECT_FOLDER:
2170                 sort_picons(&aeffects);
2171                 break;
2172         case AW_VEFFECT_FOLDER:
2173                 sort_picons(&veffects);
2174                 break;
2175         case AW_ATRANSITION_FOLDER:
2176                 sort_picons(&atransitions);
2177                 break;
2178         case AW_VTRANSITION_FOLDER:
2179                 sort_picons(&vtransitions);
2180                 break;
2181         case AW_LABEL_FOLDER:
2182                 sort_picons(&labellist);
2183                 break;
2184         default:
2185                 sort_picons(&assets);
2186                 break;
2187         }
2188 // reset xyposition
2189         asset_list->update_format(asset_list->get_format(), 0);
2190         folder_lock->unlock();
2191         update_assets();
2192 }
2193
2194 void AWindowGUI::sort_folders()
2195 {
2196         folder_lock->lock("AWindowGUI::update_assets");
2197 //      folder_list->collapse_recursive(&folders, 0);
2198         folder_list->set_autoplacement(&folders, 0, 1);
2199         sort_picons(&folders);
2200         folder_list->update_format(folder_list->get_format(), 0);
2201         folder_lock->unlock();
2202         update_assets();
2203 }
2204
2205 EDL *AWindowGUI::collect_proxy(Indexable *indexable)
2206 {
2207         Asset *proxy_asset = (Asset *)indexable;
2208         char path[BCTEXTLEN];
2209         int proxy_scale = mwindow->edl->session->proxy_scale;
2210         ProxyRender::from_proxy_path(path, proxy_asset, proxy_scale);
2211         Asset *unproxy_asset = mwindow->edl->assets->get_asset(path);
2212         if( !unproxy_asset || !unproxy_asset->layers ) return 0;
2213 // make a clip from proxy video tracks and unproxy audio tracks
2214         EDL *proxy_edl = new EDL(mwindow->edl);
2215         proxy_edl->create_objects();
2216         proxy_edl->set_path(proxy_asset->path);
2217         FileSystem fs;  fs.extract_name(path, proxy_asset->path);
2218         strcpy(proxy_edl->local_session->clip_title, path);
2219         strcpy(proxy_edl->local_session->clip_notes, _("Proxy clip"));
2220         proxy_edl->session->video_tracks = proxy_asset->layers;
2221         proxy_edl->session->audio_tracks = unproxy_asset->channels;
2222         proxy_edl->create_default_tracks();
2223         double length = proxy_asset->frame_rate > 0 ?
2224                 ( proxy_asset->video_length >= 0 ?
2225                         ( proxy_asset->video_length / proxy_asset->frame_rate ) :
2226                         ( proxy_edl->session->si_useduration ?
2227                                 proxy_edl->session->si_duration :
2228                                 1.0 / proxy_asset->frame_rate ) ) :
2229                 1.0 / proxy_edl->session->frame_rate;
2230         Track *current = proxy_edl->tracks->first;
2231         for( int vtrack=0; current; current=NEXT ) {
2232                 if( current->data_type != TRACK_VIDEO ) continue;
2233                 current->insert_asset(proxy_asset, 0, length, 0, vtrack++);
2234         }
2235         length = (double)unproxy_asset->audio_length / unproxy_asset->sample_rate;
2236         current = proxy_edl->tracks->first;
2237         for( int atrack=0; current; current=NEXT ) {
2238                 if( current->data_type != TRACK_AUDIO ) continue;
2239                 current->insert_asset(unproxy_asset, 0, length, 0, atrack++);
2240         }
2241         proxy_edl->folder_no = AW_PROXY_FOLDER;
2242         return proxy_edl;
2243 }
2244
2245
2246 void AWindowGUI::collect_assets(int proxy)
2247 {
2248         mwindow->session->drag_assets->remove_all();
2249         mwindow->session->drag_clips->remove_all();
2250         int i = 0;  AssetPicon *result;
2251         while( (result = (AssetPicon*)asset_list->get_selection(0, i++)) != 0 ) {
2252                 Indexable *indexable = result->indexable;
2253                 if( proxy && indexable && indexable->is_asset &&
2254                     indexable->folder_no == AW_PROXY_FOLDER ) {
2255                         EDL *drag_edl = collect_proxy(indexable);
2256                         if( drag_edl ) mwindow->session->drag_clips->append(drag_edl);
2257                         continue;
2258                 }
2259                 if( indexable ) {
2260                         mwindow->session->drag_assets->append(indexable);
2261                         continue;
2262                 }
2263                 if( result->edl ) {
2264                         mwindow->session->drag_clips->append(result->edl);
2265                         continue;
2266                 }
2267         }
2268 }
2269
2270 void AWindowGUI::copy_picons(AssetPicon *picon, ArrayList<BC_ListBoxItem*> *src)
2271 {
2272 // Remove current pointers
2273         ArrayList<BC_ListBoxItem*> *dst = displayed_assets;
2274         dst[0].remove_all();
2275         dst[1].remove_all_objects();
2276
2277         AWindowFolderSubItems *sub_items = picon ? picon->sub_items : 0;
2278         int folder = mwindow->edl->session->awindow_folder;
2279         BinFolder *bin_folder = folder < AWINDOW_USER_FOLDERS ? 0 :
2280                 mwindow->edl->get_folder(folder);
2281
2282 // Create new pointers
2283         for( int i = 0; i < src->total; i++ ) {
2284                 int visible = folder >= AW_CLIP_FOLDER ? 0 : 1;
2285                 picon = (AssetPicon*)src->values[i];
2286                 picon->sort_key = -1;
2287                 if( !visible && bin_folder ) {
2288                         Indexable *idxbl = bin_folder->is_clips ? (Indexable *)picon->edl :
2289                             picon->indexable ? picon->indexable :
2290                             picon->edl ? picon->edl->get_proxy_asset() : 0;
2291                         if( idxbl ) {
2292                                 picon->sort_key = mwindow->edl->folders.matches_indexable(folder, idxbl);
2293                                 if( picon->sort_key < 0 ) continue;
2294                                 visible = 1;
2295                         }
2296                 }
2297                 if( !visible && picon->indexable && picon->indexable->folder_no == folder )
2298                         visible = 1;
2299                 if( !visible && picon->edl && picon->edl->folder_no == folder )
2300                         visible = 1;
2301                 if( visible && sub_items ) {
2302                         if( !sub_items->matches(picon->get_text()) )
2303                                 visible = 0;
2304                 }
2305                 if( visible ) {
2306                         const char *text = search_text->get_text();
2307                         if( text && text[0] )
2308                                 visible = bstrcasestr(picon->get_text(), text) ? 1 : 0;
2309                 }
2310                 if( visible && picon->vicon && picon->vicon->hidden )
2311                         picon->vicon->hidden = 0;
2312                 if( visible ) {
2313                         BC_ListBoxItem *item2, *item1;
2314                         dst[0].append(item1 = picon);
2315                         if( picon->edl )
2316                                 dst[1].append(item2 = new BC_ListBoxItem(picon->edl->local_session->clip_notes));
2317                         else
2318                         if( picon->label )
2319                                 dst[1].append(item2 = new BC_ListBoxItem(picon->label->textstr));
2320                         else if( picon->comments_time ) {
2321                                 char date_time[BCSTRLEN];
2322                                 struct tm stm;  localtime_r(&picon->comments_time, &stm);
2323                                 sprintf(date_time,"%04d.%02d.%02d %02d:%02d:%02d @%0.2f %c%s",
2324                                          stm.tm_year+1900, stm.tm_mon+1, stm.tm_mday,
2325                                          stm.tm_hour, stm.tm_min, stm.tm_sec,
2326                                         picon->comments_rate, picon->comments_ffmt,
2327                                         picon->comments_type);
2328                                 dst[1].append(item2 = new BC_ListBoxItem(date_time));
2329                         }
2330                         else
2331                                 dst[1].append(item2 = new BC_ListBoxItem(""));
2332                         item1->set_autoplace_text(1);  item1->set_autoplace_icon(1);
2333                         item2->set_autoplace_text(1);  item2->set_autoplace_icon(1);
2334                 }
2335         }
2336 }
2337
2338 void AWindowGUI::sort_picons(ArrayList<BC_ListBoxItem*> *src)
2339 {
2340         int done = 0, changed = 0;
2341         while( !done ) {
2342                 done = 1;
2343                 for( int i=0; i<src->total-1; ++i ) {
2344                         AssetPicon *item1 = (AssetPicon *)src->values[i];
2345                         AssetPicon *item2 = (AssetPicon *)src->values[i + 1];
2346                         double v = item2->sort_key - item1->sort_key;
2347                         if( v > 0 ) continue;
2348                         if( v == 0 ) {
2349                                 const char *cp1 = item1->get_text();
2350                                 const char *bp1 = strrchr(cp1, '/');
2351                                 if( bp1 ) cp1 = bp1 + 1;
2352                                 const char *cp2 = item2->get_text();
2353                                 const char *bp2 = strrchr(cp2, '/');
2354                                 if( bp2 ) cp2 = bp2 + 1;
2355                                 if( strcmp(cp2, cp1) >= 0 ) continue;
2356                         }
2357                         src->values[i + 1] = item1;
2358                         src->values[i] = item2;
2359                         done = 0;  changed = 1;
2360                 }
2361         }
2362         if( changed ) {
2363                 for( int i=0; i<src->total; ++i ) {
2364                         AssetPicon *item = (AssetPicon *)src->values[i];
2365                         item->set_autoplace_icon(1);
2366                         item->set_autoplace_text(1);
2367                 }
2368         }
2369 }
2370
2371 void AWindowGUI::filter_displayed_assets()
2372 {
2373         //allow_iconlisting = 1;
2374         asset_titles[0] = C_("Title");
2375         asset_titles[1] = _("Comments");
2376         AssetPicon *picon = 0;
2377         int selected_folder = mwindow->edl->session->awindow_folder;
2378         // Ensure the current folder icon is highlighted
2379         for( int i = 0; i < folders.total; i++ ) {
2380                 AssetPicon *folder_item = (AssetPicon *)folders.values[i];
2381                 int selected = folder_item->foldernum == selected_folder ? 1 : 0;
2382                 folder_item->set_selected(selected);
2383                 if( selected ) picon = folder_item;
2384         }
2385
2386         ArrayList<BC_ListBoxItem*> *src = &assets;
2387         switch( selected_folder ) {
2388         case AW_AEFFECT_FOLDER:  src = &aeffects;  break;
2389         case AW_VEFFECT_FOLDER:  src = &veffects;  break;
2390         case AW_ATRANSITION_FOLDER:  src = &atransitions;  break;
2391         case AW_VTRANSITION_FOLDER:  src = &vtransitions;  break;
2392         case AW_LABEL_FOLDER:  src = &labellist;
2393                 asset_titles[0] = _("Time Stamps");
2394                 asset_titles[1] = C_("Title");
2395                 //allow_iconlisting = 0;
2396                 break;
2397         }
2398         copy_picons(picon, src);
2399 }
2400
2401
2402 void AWindowGUI::update_assets()
2403 {
2404         stop_vicon_drawing();
2405         folder_lock->lock("AWindowGUI::update_assets");
2406         update_folder_list();
2407         update_asset_list();
2408         labellist.remove_all_objects();
2409         create_label_folder();
2410
2411         if( displayed_folder != mwindow->edl->session->awindow_folder )
2412                 search_text->clear();
2413         vicon_thread->hide_vicons();
2414         filter_displayed_assets();
2415         folder_lock->unlock();
2416
2417         if( mwindow->edl->session->folderlist_format != folder_list->get_format() ) {
2418                 folder_list->update_format(mwindow->edl->session->folderlist_format, 0);
2419         }
2420         int folder_xposition = folder_list->get_xposition();
2421         int folder_yposition = folder_list->get_yposition();
2422         folder_list->update(&folders, 0, 0, 1, folder_xposition, folder_yposition, -1);
2423
2424         if( mwindow->edl->session->assetlist_format != asset_list->get_format() ) {
2425                 asset_list->update_format(mwindow->edl->session->assetlist_format, 0);
2426         }
2427         int asset_xposition = asset_list->get_xposition();
2428         int asset_yposition = asset_list->get_yposition();
2429         if( displayed_folder != mwindow->edl->session->awindow_folder ) {
2430                 displayed_folder = mwindow->edl->session->awindow_folder;
2431                 asset_xposition = asset_yposition = 0;
2432         }
2433         asset_list->update(displayed_assets, asset_titles,
2434                 mwindow->edl->session->asset_columns, ASSET_COLUMNS,
2435                 asset_xposition, asset_yposition, -1, 0);
2436         asset_list->center_selection();
2437
2438         flush();
2439         start_vicon_drawing();
2440         return;
2441 }
2442
2443 void AWindowGUI::update_effects()
2444 {
2445         aeffects.remove_all_objects();
2446         create_persistent_folder(&aeffects, 1, 0, 1, 0);
2447         veffects.remove_all_objects();
2448         create_persistent_folder(&veffects, 0, 1, 1, 0);
2449         atransitions.remove_all_objects();
2450         create_persistent_folder(&atransitions, 1, 0, 0, 1);
2451         vtransitions.remove_all_objects();
2452         create_persistent_folder(&vtransitions, 0, 1, 0, 1);
2453 }
2454
2455 int AWindowGUI::drag_motion()
2456 {
2457         if( get_hidden() ) return 0;
2458
2459         int result = 0;
2460         return result;
2461 }
2462
2463 int AWindowGUI::drag_stop()
2464 {
2465         if( get_hidden() ) return 0;
2466         return 0;
2467 }
2468
2469 Indexable* AWindowGUI::selected_asset()
2470 {
2471         AssetPicon *picon = (AssetPicon*)asset_list->get_selection(0, 0);
2472         return picon ? picon->indexable : 0;
2473 }
2474
2475 PluginServer* AWindowGUI::selected_plugin()
2476 {
2477         AssetPicon *picon = (AssetPicon*)asset_list->get_selection(0, 0);
2478         return picon ? picon->plugin : 0;
2479 }
2480
2481 AssetPicon* AWindowGUI::selected_folder()
2482 {
2483         AssetPicon *picon = (AssetPicon*)folder_list->get_selection(0, 0);
2484         return picon;
2485 }
2486
2487
2488
2489
2490
2491
2492
2493
2494 AWindowDivider::AWindowDivider(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2495  : BC_SubWindow(x, y, w, h)
2496 {
2497         this->mwindow = mwindow;
2498         this->gui = gui;
2499 }
2500 AWindowDivider::~AWindowDivider()
2501 {
2502 }
2503
2504 int AWindowDivider::button_press_event()
2505 {
2506         if( is_event_win() && cursor_inside() ) {
2507                 mwindow->session->current_operation = DRAG_PARTITION;
2508                 return 1;
2509         }
2510         return 0;
2511 }
2512
2513 int AWindowDivider::cursor_motion_event()
2514 {
2515         if( mwindow->session->current_operation == DRAG_PARTITION ) {
2516                 int wmin = xS(25);
2517                 int wmax = mwindow->session->awindow_w - mwindow->theme->adivider_w - wmin;
2518                 int fw = gui->get_relative_cursor_x();
2519                 if( fw > wmax ) fw = wmax;
2520                 if( fw < wmin ) fw = wmin;
2521                 mwindow->session->afolders_w = fw;
2522                 mwindow->theme->get_awindow_sizes(gui);
2523                 gui->reposition_objects();
2524                 gui->flush();
2525         }
2526         return 0;
2527 }
2528
2529 int AWindowDivider::button_release_event()
2530 {
2531         if( mwindow->session->current_operation == DRAG_PARTITION ) {
2532                 mwindow->session->current_operation = NO_OPERATION;
2533                 return 1;
2534         }
2535         return 0;
2536 }
2537
2538
2539
2540
2541
2542
2543 AWindowFolders::AWindowFolders(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2544  : BC_ListBox(x, y, w, h,
2545                 mwindow->edl->session->folderlist_format == FOLDERS_ICONS ?
2546                         LISTBOX_ICONS : LISTBOX_TEXT,
2547                 &gui->folders,    // Each column has an ArrayList of BC_ListBoxItems.
2548                 0,                // Titles for columns.  Set to 0 for no titles
2549                 0,                // width of each column
2550                 1,                // Total columns.
2551                 0,                // Pixel of top of window.
2552                 0,                // If this listbox is a popup window
2553                 LISTBOX_SINGLE,   // Select one item or multiple items
2554                 ICON_TOP,         // Position of icon relative to text of each item
2555                 1)                // Allow drags
2556 {
2557         this->mwindow = mwindow;
2558         this->gui = gui;
2559         set_drag_scroll(0);
2560         last_item0 = 0;
2561         last_item1 = 0;
2562 }
2563
2564 AWindowFolders::~AWindowFolders()
2565 {
2566 }
2567
2568 int AWindowFolders::selection_changed()
2569 {
2570         AWindowFolderItem *item0 = (AWindowFolderItem*)get_selection(0, 0);
2571         AWindowFolderItem *item1 = (AWindowFolderItem*)get_selection(0, 1);
2572 // prefer expanded entry
2573         AWindowFolderItem *item = item1 ? item1 : item0;
2574         if( item0 && item1 && last_item0 == item0 && last_item1 == item1 ) {
2575                 item1->set_selected(0);
2576                 item1 = 0;
2577                 item = item0;
2578         }
2579         last_item0 = item0;
2580         last_item1 = item1;
2581         if( item ) {
2582                 AssetPicon *picon = item->get_picon();
2583                 picon->sub_items = (AWindowFolderSubItems*)(!item->parent ? 0 : item);
2584
2585                 gui->stop_vicon_drawing();
2586
2587                 if( get_button_down() && get_buttonpress() == RIGHT_BUTTON ) {
2588                         gui->folderlist_menu->update_titles();
2589                         gui->folderlist_menu->activate_menu();
2590                 }
2591
2592                 mwindow->edl->session->awindow_folder = picon->foldernum;
2593                 gui->asset_list->draw_background();
2594                 gui->async_update_assets();
2595
2596                 gui->start_vicon_drawing();
2597         }
2598         return 1;
2599 }
2600
2601 int AWindowFolders::button_press_event()
2602 {
2603         gui->hide_tip_info();
2604         int result = BC_ListBox::button_press_event();
2605
2606         if( !result ) {
2607                 if( get_buttonpress() == RIGHT_BUTTON &&
2608                     is_event_win() && cursor_inside() ) {
2609                         gui->folderlist_menu->update_titles();
2610                         gui->folderlist_menu->activate_menu();
2611                         result = 1;
2612                 }
2613         }
2614
2615         return result;
2616 }
2617
2618 int AWindowFolders::drag_stop()
2619 {
2620         int result = 0;
2621         if( get_hidden() ) return 0;
2622         if( mwindow->session->current_operation == DRAG_ASSET &&
2623             gui->folder_list->cursor_above() ) { // check user folder
2624                 int item_no = gui->folder_list->get_cursor_data_item_no();
2625                 AssetPicon *picon = (AssetPicon *)(item_no < 0 ? 0 : gui->folders[item_no]);
2626                 if( picon && picon->foldernum >= AWINDOW_USER_FOLDERS ) {
2627                         BinFolder *folder = mwindow->edl->get_folder(picon->foldernum);
2628                         ArrayList<Indexable *> *drags = folder->is_clips ?
2629                                 ((ArrayList<Indexable *> *)mwindow->session->drag_clips) :
2630                                 ((ArrayList<Indexable *> *)mwindow->session->drag_assets);
2631                         if( folder && drags && !folder->add_patterns(drags, shift_down()) )
2632                                 flicker(1,30);
2633                         mwindow->session->current_operation = ::NO_OPERATION;
2634                         result = 1;
2635                 }
2636         }
2637         return result;
2638 }
2639
2640 AWindowFolderSubItems::AWindowFolderSubItems(AWindowFolderItem *parent, const char *text)
2641  : AWindowFolderItem(text)
2642 {
2643         this->parent = parent;
2644 }
2645
2646 int AWindowFolders::load_expanders()
2647 {
2648         char expanders_path[BCTEXTLEN];
2649         mwindow->create_defaults_path(expanders_path, EXPANDERS_FILE);
2650         FILE *fp = fopen(expanders_path, "r");
2651         if( !fp ) {
2652                 snprintf(expanders_path, sizeof(expanders_path), "%s/%s",
2653                         File::get_cindat_path(), EXPANDERS_FILE);
2654                 fp = fopen(expanders_path, "r");
2655         }
2656
2657         if( !fp ) return 1;
2658         const char tab = '\t';
2659         char line[BCTEXTLEN];   line[0] = 0;
2660         AWindowFolderItem *item = 0, *parent;
2661         AWindowFolderSubItems *sub_items = 0;
2662         int k = 0;
2663         while( fgets(line,sizeof(line),fp) ) {
2664                 if( line[0] == '#' ) continue;
2665                 int i = strlen(line);
2666                 if( i > 0 && line[i-1] == '\n' ) line[--i] = 0;
2667                 if( i == 0 ) continue;
2668                 i = 0;
2669                 for( char *cp=line; *cp==tab; ++cp ) ++i;
2670                 if( i == 0 ) {
2671                         int i = gui->folders.size();
2672                         while( --i >= 0 ) {
2673                                 AssetPicon *folder = (AssetPicon *)gui->folders[i];
2674                                 if( !strcmp(folder->get_text(),_(line)) ) break;
2675                         }
2676                         item = (AWindowFolderItem*)(i >= 0 ? gui->folders[i] : 0);
2677                         sub_items = 0;
2678                         k = 0;
2679                         continue;
2680                 }
2681                 if( i > k+1 ) continue;
2682                 if( i == k+1 ) {
2683                         if( line[i] != '-' && sub_items ) {
2684                                 sub_items->names.append(cstrdup(_(&line[i])));
2685                                 continue;
2686                         }
2687                         parent = item;
2688                         k = i;
2689                 }
2690                 else {
2691                         while( i < k ) {
2692                                 item = item->parent;
2693                                 --k;
2694                         }
2695                         parent = item->parent;
2696                 }
2697                 ArrayList<BC_ListBoxItem*> *sublist = parent->get_sublist();
2698                 if( !sublist ) sublist = parent->new_sublist(1);
2699                 sub_items = new AWindowFolderSubItems(parent, &line[i]);
2700                 sublist->append(item = sub_items);
2701         }
2702         fclose(fp);
2703         return 0;
2704 }
2705
2706
2707 AWindowAssets::AWindowAssets(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2708  : BC_ListBox(x, y, w, h, !gui->allow_iconlisting ? LISTBOX_TEXT :
2709                 mwindow->edl->session->assetlist_format == ASSETS_ICONS ? LISTBOX_ICONS :
2710                 mwindow->edl->session->assetlist_format == ASSETS_ICONS_PACKED ? LISTBOX_ICONS_PACKED :
2711                 mwindow->edl->session->assetlist_format == ASSETS_ICON_LIST ? LISTBOX_ICON_LIST :
2712                         LISTBOX_TEXT,
2713                 &gui->assets,     // Each column has an ArrayList of BC_ListBoxItems.
2714                 gui->asset_titles,// Titles for columns.  Set to 0 for no titles
2715                 mwindow->edl->session->asset_columns, // width of each column
2716                 1,                // Total columns.
2717                 0,                // Pixel of top of window.
2718                 0,                // If this listbox is a popup window
2719                 LISTBOX_MULTIPLE, // Select one item or multiple items
2720                 ICON_TOP,         // Position of icon relative to text of each item
2721                 -1)               // Allow drags, require shift for scrolling
2722 {
2723         this->mwindow = mwindow;
2724         this->gui = gui;
2725         this->info_tip = -1;
2726         set_drag_scroll(0);
2727         set_scroll_stretch(1, 1);
2728 }
2729
2730 AWindowAssets::~AWindowAssets()
2731 {
2732 }
2733
2734 int AWindowAssets::button_press_event()
2735 {
2736         hide_tip_info();
2737         AssetVIconThread *avt = gui->vicon_thread;
2738         if( avt->draw_mode != ASSET_VIEW_NONE && is_event_win() ) {
2739                 int dir = 1, button = get_buttonpress();
2740                 switch( button ) {
2741                 case WHEEL_DOWN: dir = -1;  // fall thru
2742                 case WHEEL_UP: {
2743                         int x = get_cursor_x(), y = get_cursor_y();
2744                         if( avt->cursor_inside(x, y) && avt->view_win )
2745                                 avt->zoom_scale(dir);
2746                         return 1; }
2747                 }
2748         }
2749
2750         int result = BC_ListBox::button_press_event();
2751
2752         if( !result && get_buttonpress() == RIGHT_BUTTON &&
2753             is_event_win() && cursor_inside() ) {
2754                 BC_ListBox::deactivate_selection();
2755                 int folder = mwindow->edl->session->awindow_folder;
2756                 switch( folder ) {
2757                 case AW_AEFFECT_FOLDER:
2758                 case AW_VEFFECT_FOLDER:
2759                 case AW_ATRANSITION_FOLDER:
2760                 case AW_VTRANSITION_FOLDER:
2761                         gui->effectlist_menu->update();
2762                         gui->effectlist_menu->activate_menu();
2763                         break;
2764                 case AW_LABEL_FOLDER:
2765                         gui->labellist_menu->update();
2766                         gui->labellist_menu->activate_menu();
2767                         break;
2768                 case AW_CLIP_FOLDER:
2769                         gui->cliplist_menu->update();
2770                         gui->cliplist_menu->activate_menu();
2771                         break;
2772                 case AW_PROXY_FOLDER:
2773                         gui->proxylist_menu->update();
2774                         gui->proxylist_menu->activate_menu();
2775                         break;
2776                 default:
2777                 case AW_MEDIA_FOLDER: {
2778                         int shots =  folder==AW_MEDIA_FOLDER || folder>=AWINDOW_USER_FOLDERS;
2779                         gui->assetlist_menu->update_titles(shots);
2780                         gui->assetlist_menu->activate_menu();
2781                         break; }
2782                 }
2783                 result = 1;
2784         }
2785
2786         return result;
2787 }
2788
2789
2790 int AWindowAssets::handle_event()
2791 {
2792         AssetPicon *asset_picon = (AssetPicon *)get_selection(0, 0);
2793         if( !asset_picon ) return 0;
2794         Indexable *picon_idxbl = asset_picon->indexable;
2795         EDL *picon_edl = asset_picon->edl;
2796         int proxy = 0;
2797         VWindow *vwindow = 0;
2798         switch( mwindow->edl->session->awindow_folder ) {
2799         case AW_AEFFECT_FOLDER:
2800         case AW_VEFFECT_FOLDER:
2801         case AW_ATRANSITION_FOLDER:
2802         case AW_VTRANSITION_FOLDER: return 1;
2803         case AW_PROXY_FOLDER:
2804                 proxy = 1; // fall thru
2805         default:
2806                 if( mwindow->vwindows.size() > DEFAULT_VWINDOW )
2807                         vwindow = mwindow->vwindows.get(DEFAULT_VWINDOW);
2808                 break;
2809         }
2810         if( !vwindow || !vwindow->is_running() ) return 1;
2811         if( proxy && picon_idxbl ) {
2812                 picon_edl = gui->collect_proxy(picon_idxbl);
2813                 picon_idxbl = 0;
2814         }
2815
2816         if( picon_idxbl ) vwindow->change_source(picon_idxbl);
2817         else if( picon_edl ) vwindow->change_source(picon_edl);
2818         return 1;
2819 }
2820
2821 int AWindowAssets::selection_changed()
2822 {
2823 // Show popup window
2824         AssetPicon *item;
2825         int folder = mwindow->edl->session->awindow_folder;
2826         if( get_button_down() && get_buttonpress() == RIGHT_BUTTON &&
2827             (item = (AssetPicon*)get_selection(0, 0)) ) {
2828                 switch( folder ) {
2829                 case AW_AEFFECT_FOLDER:
2830                 case AW_VEFFECT_FOLDER:
2831                 case AW_ATRANSITION_FOLDER:
2832                 case AW_VTRANSITION_FOLDER:
2833                         gui->effectlist_menu->update();
2834                         gui->effectlist_menu->activate_menu();
2835                         break;
2836                 case AW_LABEL_FOLDER:
2837                         if( !item->label ) break;
2838                         gui->label_menu->activate_menu();
2839                         break;
2840                 case AW_CLIP_FOLDER:
2841                         if( !item->indexable && !item->edl ) break;
2842                         gui->clip_menu->update();
2843                         gui->clip_menu->activate_menu();
2844                         break;
2845                 case AW_PROXY_FOLDER:
2846                         if( !item->indexable && !item->edl ) break;
2847                         gui->proxy_menu->update();
2848                         gui->proxy_menu->activate_menu();
2849                         break;
2850                 default:
2851                         if( !item->indexable && !item->edl ) break;
2852                         gui->asset_menu->update();
2853                         gui->asset_menu->activate_menu();
2854                         break;
2855                 }
2856
2857                 deactivate_selection();
2858         }
2859         else if( get_button_down() && !gui->play_off &&
2860                  mwindow->edl->session->assetlist_format != ASSETS_TEXT ) {
2861                 item = (AssetPicon*)get_selection(0, 0);
2862                 if( item && !get_selection(0, 1) ) {
2863                         switch( folder ) {
2864                         case AW_MEDIA_FOLDER:
2865                         case AW_PROXY_FOLDER:
2866                         case AWINDOW_USER_FOLDERS:
2867                                 if( get_buttonpress() == LEFT_BUTTON ||
2868                                     get_buttonpress() == MIDDLE_BUTTON ) {
2869                                         AssetVIcon *vicon = 0;
2870                                         AssetVIconThread *avt = gui->vicon_thread;
2871                                         if( !avt->vicon && gui->vicon_drawing != AVICON_NO_PLAY )
2872                                                 vicon = item->vicon;
2873                                         int draw_mode = !vicon ?
2874                                                         ASSET_VIEW_NONE :
2875                                                 get_buttonpress() == MIDDLE_BUTTON ?
2876                                                         ASSET_VIEW_MEDIA_MAP : ASSET_VIEW_MEDIA;
2877                                         avt->set_view_popup(vicon, draw_mode);
2878                                 }
2879                                 break;
2880                         case AW_CLIP_FOLDER:
2881                                 if( get_buttonpress() == LEFT_BUTTON ) {
2882                                         AssetVIcon *vicon = 0;
2883                                         AssetVIconThread *avt = gui->vicon_thread;
2884                                         if( !avt->vicon && gui->vicon_drawing != AVICON_NO_PLAY )
2885                                                 vicon = item->vicon;
2886                                         gui->vicon_thread->set_view_popup(vicon, ASSET_VIEW_ICON);
2887                                 }
2888                                 break;
2889                         }
2890                 }
2891                 else {
2892                         gui->vicon_thread->set_view_popup(0);
2893                         if( gui->vicon_drawing != AVICON_FULL_PLAY )
2894                                 gui->stop_vicon_drawing();
2895                 }
2896         }
2897         return 1;
2898 }
2899
2900 void AWindowAssets::draw_background()
2901 {
2902         clear_box(0,0,get_w(),get_h(),get_bg_surface());
2903         set_color(BC_WindowBase::get_resources()->audiovideo_color);
2904         set_font(LARGEFONT);
2905         int folder = mwindow->edl->session->awindow_folder;
2906         const char *title = mwindow->edl->get_folder_name(folder);
2907         draw_text(get_w() - get_text_width(LARGEFONT, title) - xS(4), yS(30),
2908                 title, -1, get_bg_surface());
2909 }
2910
2911 int AWindowAssets::drag_start_event()
2912 {
2913         gui->vicon_thread->set_view_popup(0);
2914         int collect_pluginservers = 0;
2915         int collect_assets = 0, proxy = 0;
2916
2917         if( BC_ListBox::drag_start_event() ) {
2918                 switch( mwindow->edl->session->awindow_folder ) {
2919                 case AW_AEFFECT_FOLDER:
2920                         mwindow->session->current_operation = DRAG_AEFFECT;
2921                         collect_pluginservers = 1;
2922                         break;
2923                 case AW_VEFFECT_FOLDER:
2924                         mwindow->session->current_operation = DRAG_VEFFECT;
2925                         collect_pluginservers = 1;
2926                         break;
2927                 case AW_ATRANSITION_FOLDER:
2928                         mwindow->session->current_operation = DRAG_ATRANSITION;
2929                         collect_pluginservers = 1;
2930                         break;
2931                 case AW_VTRANSITION_FOLDER:
2932                         mwindow->session->current_operation = DRAG_VTRANSITION;
2933                         collect_pluginservers = 1;
2934                         break;
2935                 case AW_LABEL_FOLDER:
2936                         // do nothing!
2937                         break;
2938                 case AW_PROXY_FOLDER:
2939                         proxy = 1; // fall thru
2940                 case AW_MEDIA_FOLDER:
2941                 default:
2942                         mwindow->session->current_operation = DRAG_ASSET;
2943                         collect_assets = 1;
2944                         break;
2945                 }
2946
2947                 if( collect_pluginservers ) {
2948                         int i = 0;
2949                         mwindow->session->drag_pluginservers->remove_all();
2950                         while(1)
2951                         {
2952                                 AssetPicon *result = (AssetPicon*)get_selection(0, i++);
2953                                 if( !result ) break;
2954
2955                                 mwindow->session->drag_pluginservers->append(result->plugin);
2956                         }
2957                 }
2958
2959                 if( collect_assets ) {
2960                         gui->collect_assets(proxy);
2961                 }
2962
2963                 return 1;
2964         }
2965         return 0;
2966 }
2967
2968 int AWindowAssets::drag_motion_event()
2969 {
2970         BC_ListBox::drag_motion_event();
2971         unlock_window();
2972
2973         mwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2974         mwindow->gui->drag_motion();
2975         mwindow->gui->unlock_window();
2976
2977         for( int i = 0; i < mwindow->vwindows.size(); i++ ) {
2978                 VWindow *vwindow = mwindow->vwindows.get(i);
2979                 if( !vwindow->is_running() ) continue;
2980                 vwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2981                 vwindow->gui->drag_motion();
2982                 vwindow->gui->unlock_window();
2983         }
2984
2985         mwindow->cwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2986         mwindow->cwindow->gui->drag_motion();
2987         mwindow->cwindow->gui->unlock_window();
2988
2989         lock_window("AWindowAssets::drag_motion_event");
2990         if( mwindow->session->current_operation == DRAG_ASSET &&
2991             gui->folder_list->cursor_above() ) { // highlight user folder
2992                 BC_ListBoxItem *item = 0;
2993                 int item_no = gui->folder_list->get_cursor_data_item_no(&item);
2994                 if( item_no >= 0 ) {
2995                         AssetPicon *folder = (AssetPicon *)gui->folders[item_no];
2996                         if( folder->foldernum < AWINDOW_USER_FOLDERS ) item_no = -1;
2997                 }
2998                 if( item_no >= 0 )
2999                         item_no = gui->folder_list->item_to_index(&gui->folders, item);
3000                 int folder_xposition = gui->folder_list->get_xposition();
3001                 int folder_yposition = gui->folder_list->get_yposition();
3002                 gui->folder_list->update(&gui->folders, 0, 0, 1,
3003                         folder_xposition, folder_yposition, item_no, 0, 1);
3004         }
3005         return 0;
3006 }
3007
3008 int AWindowAssets::drag_stop_event()
3009 {
3010         int result = 0;
3011
3012         result = gui->drag_stop();
3013
3014         unlock_window();
3015
3016         if( !result ) {
3017                 mwindow->gui->lock_window("AWindowAssets::drag_stop_event");
3018                 result = mwindow->gui->drag_stop();
3019                 mwindow->gui->unlock_window();
3020         }
3021
3022         if( !result ) {
3023                 for( int i = 0; !result && i < mwindow->vwindows.size(); i++ ) {
3024                         VWindow *vwindow = mwindow->vwindows.get(i);
3025                         if( !vwindow ) continue;
3026                         if( !vwindow->is_running() ) continue;
3027                         if( vwindow->gui->is_hidden() ) continue;
3028                         vwindow->gui->lock_window("AWindowAssets::drag_stop_event");
3029                         if( vwindow->gui->cursor_above() &&
3030                             vwindow->gui->get_cursor_over_window() ) {
3031                                 result = vwindow->gui->drag_stop();
3032                         }
3033                         vwindow->gui->unlock_window();
3034                 }
3035         }
3036
3037         if( !result ) {
3038                 mwindow->cwindow->gui->lock_window("AWindowAssets::drag_stop_event");
3039                 result = mwindow->cwindow->gui->drag_stop();
3040                 mwindow->cwindow->gui->unlock_window();
3041         }
3042
3043         lock_window("AWindowAssets::drag_stop_event");
3044         if( !result ) {
3045                 result = gui->folder_list->drag_stop();
3046         }
3047
3048
3049         if( result )
3050                 get_drag_popup()->set_animation(0);
3051
3052         BC_ListBox::drag_stop_event();
3053 // since NO_OPERATION is also defined in listbox, we have to reach for global scope...
3054         mwindow->session->current_operation = ::NO_OPERATION;
3055
3056         return 1;
3057 }
3058
3059 int AWindowAssets::column_resize_event()
3060 {
3061         mwindow->edl->session->asset_columns[0] = get_column_width(0);
3062         mwindow->edl->session->asset_columns[1] = get_column_width(1);
3063         return 1;
3064 }
3065
3066 int AWindowAssets::cursor_enter_event()
3067 {
3068         int ret = BC_ListBox::cursor_enter_event();
3069         switch( gui->vicon_drawing ) {
3070         case AVICON_FULL_PLAY:
3071                 gui->start_vicon_drawing();
3072                 break;
3073         case AVICON_MOUSE_OVER:
3074         case AVICON_SRC_TARGET:
3075         case AVICON_NO_PLAY:
3076         default:
3077                 break;
3078         }
3079         return ret;
3080 }
3081
3082 int AWindowAssets::cursor_leave_event()
3083 {
3084         hide_tip_info();
3085         if( !is_event_win() ) return 0;
3086         if( !gui->vicon_thread->viewing )
3087                 gui->stop_vicon_drawing();
3088         return BC_ListBox::cursor_leave_event();
3089 }
3090
3091 int AWindowAssets::focus_out_event()
3092 {
3093         gui->stop_vicon_drawing();
3094         gui->vicon_thread->set_view_popup(0);
3095         return BC_ListBox::focus_out_event();
3096 }
3097
3098 void AWindowAssets::update_vicon_area()
3099 {
3100         int x0 = 0, x1 = get_w();
3101         int y0 = get_title_h();
3102         int y1 = get_h();
3103         if( is_highlighted() ) {
3104                 x0 += LISTBOX_BORDER;  x1 -= LISTBOX_BORDER;
3105                 y0 += LISTBOX_BORDER;  y1 -= LISTBOX_BORDER;
3106         }
3107         gui->vicon_thread->set_drawing_area(x0,y0, x1,y1);
3108 }
3109
3110 int AWindowAssets::mouse_over_event(int no)
3111 {
3112         AssetPicon *picon = no >= 0 && no < gui->displayed_assets[0].size() ?
3113                 (AssetPicon *)gui->displayed_assets[0][no] : 0;
3114         AssetVIcon *vicon = !picon ? 0 : picon->vicon;
3115         switch( gui->vicon_drawing ) {
3116         case AVICON_FULL_PLAY:
3117                 gui->vicon_thread->solo = 0;
3118                 if( vicon && gui->vicon_thread->viewing )
3119                         gui->vicon_thread->set_view_popup(vicon);
3120                 break;
3121         case AVICON_MOUSE_OVER:
3122                 if( !vicon ) break;
3123                 gui->vicon_thread->solo = vicon;
3124                 gui->start_vicon_drawing();
3125         // fall thru
3126         case AVICON_SRC_TARGET:
3127                 if( !vicon ) break;
3128                 if( gui->vicon_thread->viewing )
3129                         gui->vicon_thread->set_view_popup(vicon);
3130                 break;
3131         case AVICON_NO_PLAY:
3132         default:
3133                 break;
3134         }
3135         if( no < 0 && info_tip >= 0 ) {
3136                 hide_tip_info();
3137         }
3138         if( gui->tip_info && no >= 0 &&
3139             info_tip != no && picon && picon->plugin ) {
3140                 const char *info = picon->plugin->tip;
3141                 if( !info ) info = _("No info available");
3142                 show_tip_info(info, no);
3143         }
3144         return 1;
3145 }
3146
3147 void AWindowAssets::show_tip_info(const char *info, int no)
3148 {
3149         int tw = get_text_width(MEDIUMFONT, info) + TOOLTIP_MARGIN * 2;
3150         int th = get_text_height(MEDIUMFONT, info) + TOOLTIP_MARGIN * 2;
3151         int tx = get_w() - (tw + xS(28));
3152         int ty = get_h() - (th + yS(28));
3153         show_tooltip(info, tx, ty, tw, th);
3154         info_tip = no;
3155 }
3156
3157 void AWindowAssets::hide_tip_info()
3158 {
3159         hide_tooltip();
3160         info_tip = -1;
3161 }
3162
3163
3164 AWindowSearchTextBox::AWindowSearchTextBox(AWindowSearchText *search_text, int x, int y, int w)
3165  : BC_TextBox(x, y, w, 1, "")
3166 {
3167         this->search_text = search_text;
3168 }
3169
3170 int AWindowSearchTextBox::handle_event()
3171 {
3172         return search_text->handle_event();
3173 }
3174
3175 AWindowSearchText::AWindowSearchText(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3176 {
3177         this->mwindow = mwindow;
3178         this->gui = gui;
3179         this->x = x;
3180         this->y = y;
3181 }
3182
3183 void AWindowSearchText::create_objects()
3184 {
3185         int xs10 = xS(10);
3186         int x1 = x, y1 = y;
3187         gui->add_subwindow(text_title = new BC_Title(x1, y1, _("Search:")));
3188         x1 += text_title->get_w() + xs10;
3189         int w1 = gui->get_w() - x1 - 2*xs10;
3190         gui->add_subwindow(text_box = new AWindowSearchTextBox(this, x1, y1, w1));
3191 }
3192
3193 int AWindowSearchText::handle_event()
3194 {
3195         gui->async_update_assets();
3196         return 1;
3197 }
3198
3199 int AWindowSearchText::get_w()
3200 {
3201         return text_box->get_w() + text_title->get_w() + xS(10);
3202 }
3203
3204 int AWindowSearchText::get_h()
3205 {
3206         return bmax(text_box->get_h(),text_title->get_h());
3207 }
3208
3209 void AWindowSearchText::reposition_window(int x, int y, int w)
3210 {
3211         int xs10 = xS(10);
3212         int x1 = x, y1 = y;
3213         text_title->reposition_window(x1, y1);
3214         x1 += text_title->get_w() + xs10;
3215         int w1 = gui->get_w() - x1 - 2*xs10;
3216         text_box->reposition_window(x1, y1, w1);
3217 }
3218
3219 const char *AWindowSearchText::get_text()
3220 {
3221         return text_box->get_text();
3222 }
3223
3224 void AWindowSearchText::clear()
3225 {
3226         text_box->update("");
3227 }
3228
3229 AWindowDeleteDisk::AWindowDeleteDisk(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3230  : BC_Button(x, y, mwindow->theme->deletedisk_data)
3231 {
3232         this->mwindow = mwindow;
3233         this->gui = gui;
3234         set_tooltip(_("Delete asset from disk"));
3235 }
3236
3237 int AWindowDeleteDisk::handle_event()
3238 {
3239         return 1;
3240 }
3241
3242 AWindowDeleteProject::AWindowDeleteProject(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3243  : BC_Button(x, y, mwindow->theme->deleteproject_data)
3244 {
3245         this->mwindow = mwindow;
3246         this->gui = gui;
3247         set_tooltip(_("Delete asset from project"));
3248 }
3249
3250 int AWindowDeleteProject::handle_event()
3251 {
3252         return 1;
3253 }
3254
3255 // AWindowInfo::AWindowInfo(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3256 //  : BC_Button(x, y, mwindow->theme->infoasset_data)
3257 // {
3258 //      this->mwindow = mwindow;
3259 //      this->gui = gui;
3260 //      set_tooltip(_("Edit information on asset"));
3261 // }
3262 // 
3263 // int AWindowInfo::handle_event()
3264 // {
3265 //      int cur_x, cur_y;
3266 //      gui->get_abs_cursor(cur_x, cur_y, 0);
3267 //      gui->awindow->asset_edit->edit_asset(gui->selected_asset(), cur_x, cur_y);
3268 //      return 1;
3269 // }
3270
3271 AWindowRedrawIndex::AWindowRedrawIndex(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3272  : BC_Button(x, y, mwindow->theme->redrawindex_data)
3273 {
3274         this->mwindow = mwindow;
3275         this->gui = gui;
3276         set_tooltip(_("Redraw index"));
3277 }
3278
3279 int AWindowRedrawIndex::handle_event()
3280 {
3281         return 1;
3282 }
3283
3284 AWindowPaste::AWindowPaste(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3285  : BC_Button(x, y, mwindow->theme->pasteasset_data)
3286 {
3287         this->mwindow = mwindow;
3288         this->gui = gui;
3289         set_tooltip(_("Paste asset on recordable tracks"));
3290 }
3291
3292 int AWindowPaste::handle_event()
3293 {
3294         return 1;
3295 }
3296
3297 AWindowAppend::AWindowAppend(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3298  : BC_Button(x, y, mwindow->theme->appendasset_data)
3299 {
3300         this->mwindow = mwindow;
3301         this->gui = gui;
3302         set_tooltip(_("Append asset in new tracks"));
3303 }
3304
3305 int AWindowAppend::handle_event()
3306 {
3307         return 1;
3308 }
3309
3310 AWindowView::AWindowView(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3311  : BC_Button(x, y, mwindow->theme->viewasset_data)
3312 {
3313         this->mwindow = mwindow;
3314         this->gui = gui;
3315         set_tooltip(_("View asset"));
3316 }
3317
3318 int AWindowView::handle_event()
3319 {
3320         return 1;
3321 }
3322
3323 AddTools::AddTools(MWindow *mwindow, AWindowGUI *gui, int x, int y, const char *title)
3324  : BC_PopupMenu(x, y, title, -1, 0)
3325 {
3326         this->mwindow = mwindow;
3327         this->gui = gui;
3328 }
3329
3330 void AddTools::create_objects()
3331 {
3332         uint64_t vis = 0;
3333         add_item(new AddPluginItem(this, "ffmpeg", PLUGIN_FFMPEG_ID));
3334         vis |= 1 << PLUGIN_FFMPEG_ID;
3335         add_item(new AddPluginItem(this, "ladspa", PLUGIN_LADSPA_ID));
3336         vis |= 1 << PLUGIN_LADSPA_ID;
3337 #ifdef HAVE_LV2
3338         add_item(new AddPluginItem(this, "lv2", PLUGIN_LV2_ID));
3339         vis |= 1 << PLUGIN_LV2_ID;
3340 #endif
3341         for( int i=0; i<MWindow::plugindb->size(); ++i ) {
3342                 PluginServer *plugin = MWindow::plugindb->get(i);
3343                 if( !plugin->audio && !plugin->video ) continue;
3344                 int idx = plugin->dir_idx;
3345                 uint32_t msk = 1 << idx;
3346                 if( (msk & vis) != 0 ) continue;
3347                 vis |= msk;
3348                 char parent[BCTEXTLEN];
3349                 strcpy(parent, plugin->path);
3350                 char *bp = strrchr(parent, '/');
3351                 if( bp ) { *bp = 0;  bp = strrchr(parent, '/'); }
3352                 if( !bp ) bp = parent; else ++bp;
3353                 add_item(new AddPluginItem(this, bp, idx));
3354         }
3355 }
3356
3357 #if 0
3358 // plugin_dirs list from toplevel makefile include plugin_defs
3359 N_("ffmpeg")
3360 N_("ladspa")
3361 N_("lv2")
3362 N_("audio_tools")
3363 N_("audio_transitions")
3364 N_("blending")
3365 N_("colors")
3366 N_("exotic")
3367 N_("transforms")
3368 N_("tv_effects")
3369 N_("video_tools")
3370 N_("video_transitions")
3371 #endif
3372
3373 AddPluginItem::AddPluginItem(AddTools *menu, char const *text, int idx)
3374  : BC_MenuItem(_(text))
3375 {
3376         this->menu = menu;
3377         this->idx = idx;
3378         uint64_t msk = (uint64_t)1 << idx, vis = menu->gui->plugin_visibility;
3379         int chk = (msk & vis) ? 1 : 0;
3380         set_checked(chk);
3381 }
3382
3383 int AddPluginItem::handle_event()
3384 {
3385         int chk = get_checked() ^ 1;
3386         set_checked(chk);
3387         uint64_t msk = (uint64_t)1 << idx, vis = menu->gui->plugin_visibility;
3388         menu->gui->plugin_visibility = chk ? vis | msk : vis & ~msk;
3389         menu->gui->update_effects();
3390         menu->gui->save_defaults(menu->mwindow->defaults);
3391         menu->gui->async_update_assets();
3392         return 1;
3393 }
3394
3395 AVIconDrawingItem::AVIconDrawingItem(AVIconDrawing *avicon, const char *text, int id)
3396  : BC_MenuItem(text)
3397 {
3398         this->avicon = avicon;
3399         this->id = id;
3400 }
3401
3402 int AVIconDrawingItem::handle_event()
3403 {
3404         for( int i=0; i<AVICON_PLAY_MODES; ++i ) {
3405                 AVIconDrawingItem *item = (AVIconDrawingItem *) avicon->get_item(i);
3406                 item->set_checked(id == i);
3407         }
3408         AWindowGUI *agui = avicon->agui;
3409         agui->play_off = 0;
3410         avicon->set_text(get_text());
3411         agui->stop_vicon_drawing();
3412         agui->vicon_thread->set_view_popup(0);
3413         agui->vicon_thread->solo = 0;
3414         agui->vicon_drawing = id;
3415         agui->start_vicon_drawing();
3416         return 1;
3417 }
3418
3419 int AVIconDrawing::draw_face(int dx, int color)
3420 {
3421         int ret = BC_PopupMenu::draw_face(dx, color);
3422         if( agui->play_off && agui->vicon_drawing != AVICON_NO_PLAY ) {
3423                 int lx = get_margin(), ly = get_h()/2;
3424                 draw_line(lx,ly, get_w()-2*lx,ly);
3425         }
3426         return ret;
3427 }
3428
3429 AVIconDrawing::AVIconDrawing(AWindowGUI *agui, int x, int y, int w, const char *text)
3430  : BC_PopupMenu(x-w, y, w, text, 1, 0, 4)
3431 {
3432         this->agui = agui;
3433 }
3434
3435 AVIconDrawing::~AVIconDrawing()
3436 {
3437 }
3438
3439 void AVIconDrawing::create_objects()
3440 {
3441         AVIconDrawingItem *item;
3442         for( int i=0; i<AVICON_PLAY_MODES; ++i ) {
3443                 add_item(item = new AVIconDrawingItem(this, avicon_names[i], i));
3444                 item->set_checked(agui->vicon_drawing == i);
3445         }
3446 }
3447
3448 int AVIconDrawing::button_press_event()
3449 {
3450         if( !is_event_win() ) return 0;
3451         if( get_buttonpress() == MIDDLE_BUTTON ) {
3452                 agui->play_off = !agui->play_off ? 1 : 0;
3453                 draw_face(0, -1);
3454                 flash(1);
3455                 agui->start_vicon_drawing();
3456                 return 1;
3457         }
3458         return BC_PopupMenu::button_press_event();
3459 }
3460
3461 AWindowListFormat::AWindowListFormat(MWindow *mwindow, AWindowGUI *gui)
3462  : BC_MenuItem("","v",'v')
3463 {
3464         this->mwindow = mwindow;
3465         this->gui = gui;
3466 }
3467
3468 int AWindowListFormat::handle_event()
3469 {
3470         return gui->cycle_assetlist_format();
3471 }
3472
3473 void AWindowListFormat::update()
3474 {
3475         EDLSession *session = mwindow->edl->session;
3476         const char *text = 0;
3477         switch( session->assetlist_format ) {
3478         case ASSETS_TEXT:
3479                 text = _("Display icons");
3480                 break;
3481         case ASSETS_ICONS:
3482                 text = _("Display icons packed");
3483                 break;
3484         case ASSETS_ICONS_PACKED:
3485                 text = _("Display icon list");
3486                 break;
3487         case ASSETS_ICON_LIST:
3488                 text = _("Display text");
3489                 break;
3490         }
3491         set_text(text);
3492 }
3493
3494 AWindowListSort::AWindowListSort(MWindow *mwindow, AWindowGUI *gui)
3495  : BC_MenuItem(_("Sort items"))
3496 {
3497         this->mwindow = mwindow;
3498         this->gui = gui;
3499 }
3500
3501 int AWindowListSort::handle_event()
3502 {
3503         gui->sort_assets();
3504         return 1;
3505 }
3506
3507 AssetSelectUsedItem::AssetSelectUsedItem(AssetSelectUsed *select_used, const char *text, int action)
3508  : BC_MenuItem(text)
3509 {
3510         this->select_used = select_used;
3511         this->action = action;
3512 }
3513
3514 int AssetSelectUsedItem::handle_event()
3515 {
3516         MWindow *mwindow = select_used->mwindow;
3517         AWindowGUI *gui = select_used->gui;
3518         AWindowAssets *asset_list = gui->asset_list;
3519         ArrayList<BC_ListBoxItem*> *data = gui->displayed_assets;
3520
3521         switch( action ) {
3522         case SELECT_ALL:
3523         case SELECT_NONE:
3524                 asset_list->set_all_selected(data, action==SELECT_ALL ? 1 : 0);
3525                 break;
3526         case SELECT_USED:
3527         case SELECT_UNUSED:
3528                 asset_list->set_all_selected(data, 0);
3529                 for( int i = 0; i < data->total; i++ ) {
3530                         AssetPicon *picon = (AssetPicon*)data->values[i];
3531                         Indexable *idxbl = picon->indexable ? picon->indexable :
3532                             picon->edl ? picon->edl->get_proxy_asset() : 0;
3533                         int used = idxbl && mwindow->edl->in_use(idxbl) ? 1 : 0;
3534                         asset_list->set_selected(data, i, action==SELECT_USED ? used : !used);
3535                 }
3536                 break;
3537         }
3538
3539         int asset_xposition = asset_list->get_xposition();
3540         int asset_yposition = asset_list->get_yposition();
3541         asset_list->update(gui->displayed_assets, gui->asset_titles,
3542                 mwindow->edl->session->asset_columns, ASSET_COLUMNS,
3543                 asset_xposition, asset_yposition, -1, 0);
3544         asset_list->center_selection();
3545         return 1;
3546 }
3547
3548 AssetSelectUsed::AssetSelectUsed(MWindow *mwindow, AWindowGUI *gui)
3549  : BC_MenuItem(_("Select"))
3550 {
3551         this->mwindow = mwindow;
3552         this->gui = gui;
3553 }
3554