PKGBUILD fix libva/vdpau deps
[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         Asset *asset = (Asset *)picon->indexable;
124         if( !asset )
125                 return *images[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         if( indexable ) {
917                 fs.extract_name(name, indexable->path);
918                 set_text(name);
919         }
920         else if( edl ) {
921                 set_text(strcpy(name, edl->local_session->clip_title));
922                 set_text(name);
923         }
924
925         if( indexable && indexable->is_asset ) {
926                 Asset *asset = (Asset*)indexable;
927                 if( asset->video_data ) {
928                         if( mwindow->preferences->use_thumbnails ) {
929                                 gui->unlock_window();
930                                 File *file = mwindow->video_cache->check_out(asset,
931                                         mwindow->edl,
932                                         1);
933
934                                 if( file ) {
935                                         int height = asset->height > 0 ? asset->height : 1;
936                                         pixmap_w = pixmap_h * asset->width / height;
937
938                                         file->set_layer(0);
939                                         file->set_video_position(0, 0);
940
941                                         if( gui->temp_picon &&
942                                                 (gui->temp_picon->get_w() != asset->width ||
943                                                 gui->temp_picon->get_h() != asset->height) ) {
944                                                 delete gui->temp_picon;
945                                                 gui->temp_picon = 0;
946                                         }
947
948                                         if( !gui->temp_picon ) {
949                                                 gui->temp_picon = new VFrame(0, -1,
950                                                         asset->width, asset->height,
951                                                         BC_RGB888, -1);
952                                         }
953                                         { char string[BCTEXTLEN];
954                                         sprintf(string, _("Reading %s"), name);
955                                         mwindow->gui->lock_window("AssetPicon::create_objects");
956                                         mwindow->gui->show_message(string);
957                                         mwindow->gui->unlock_window(); }
958                                         file->read_frame(gui->temp_picon);
959                                         mwindow->video_cache->check_in(asset);
960
961                                         gui->lock_window("AssetPicon::create_objects 0");
962                                         icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
963                                         icon->draw_vframe(gui->temp_picon,
964                                                 0, 0, pixmap_w, pixmap_h, 0, 0);
965                                         icon_vframe = new VFrame(0,
966                                                 -1, pixmap_w, pixmap_h, BC_RGB888, -1);
967                                         icon_vframe->transfer_from(gui->temp_picon);
968                                         if( asset->folder_no == AW_MEDIA_FOLDER ||
969                                             asset->folder_no == AW_PROXY_FOLDER ) {
970 // vicon images
971                                                 double framerate = asset->get_frame_rate();
972                                                 if( !framerate ) framerate = VICON_RATE;
973                                                 int64_t frames = asset->get_video_frames();
974                                                 double secs = frames / framerate;
975                                                 if( secs > 5 ) secs = 5;
976                                                 int64_t length = secs * gui->vicon_thread->refresh_rate;
977                                                 vicon = new AssetVIcon(this, pixmap_w, pixmap_h, framerate, length);
978                                                 if( asset->audio_data && secs > 0 ) {
979                                                         gui->unlock_window();
980                                                         int audio_len = VICON_SAMPLE_RATE*secs+0.5;
981                                                         vicon->init_audio(audio_len*sizeof(vicon_audio_t));
982                                                         vicon->load_audio();
983                                                         gui->lock_window("AssetPicon::create_objects 1");
984                                                 }
985                                                 gui->vicon_thread->add_vicon(vicon);
986                                         }
987                                 }
988                                 else {
989                                         eprintf("Unable to open %s\nin asset: %s",asset->path, get_text());
990                                         gui->lock_window("AssetPicon::create_objects 2");
991                                         icon = gui->video_icon;
992                                         icon_vframe = gui->video_vframe;
993                                 }
994                         }
995                         else {
996                                 icon = gui->video_icon;
997                                 icon_vframe = gui->video_vframe;
998                         }
999                 }
1000                 else
1001                 if( asset->audio_data ) {
1002                         if( mwindow->preferences->use_thumbnails ) {
1003                                 gui->unlock_window();
1004                                 File *file = mwindow->audio_cache->check_out(asset,
1005                                         mwindow->edl,
1006                                         1);
1007                                 if( file ) {
1008                                         pixmap_w = pixmap_h * 16/9;
1009                                         icon_vframe = new VFrame(0,
1010                                                 -1, pixmap_w, pixmap_h, BC_RGB888, -1);
1011                                         icon_vframe->clear_frame();
1012                                         { char string[BCTEXTLEN];
1013                                         sprintf(string, _("Reading %s"), name);
1014                                         mwindow->gui->lock_window("AssetPicon::create_objects 3");
1015                                         mwindow->gui->show_message(string);
1016                                         mwindow->gui->unlock_window(); }
1017                                         int sample_rate = asset->get_sample_rate();
1018                                         int channels = asset->get_audio_channels();
1019                                         if( channels > 2 ) channels = 2;
1020                                         int64_t audio_samples = asset->get_audio_samples();
1021                                         double duration = (double)audio_samples / sample_rate;
1022                                         draw_hue_bar(icon_vframe, duration);
1023                                         int bfrsz = sample_rate;
1024                                         Samples samples(bfrsz);
1025                                         static int line_colors[2] = { GREEN, YELLOW };
1026                                         static int base_colors[2] = { RED, PINK };
1027                                         for( int i=channels; --i>=0; ) {
1028                                                 file->set_channel(i);
1029                                                 file->set_audio_position(0);
1030                                                 file->read_samples(&samples, bfrsz);
1031                                                 draw_wave(icon_vframe, samples.get_data(), bfrsz,
1032                                                         base_colors[i], line_colors[i]);
1033                                         }
1034                                         mwindow->audio_cache->check_in(asset);
1035                                         if( asset->folder_no == AW_MEDIA_FOLDER ) {
1036                                                 double secs = duration;
1037                                                 if( secs > 5 ) secs = 5;
1038                                                 double refresh_rate = gui->vicon_thread->refresh_rate;
1039                                                 int64_t length = secs * refresh_rate;
1040                                                 vicon = new AssetVIcon(this, pixmap_w, pixmap_h, refresh_rate, length);
1041                                                 int audio_len = VICON_SAMPLE_RATE*secs+0.5;
1042                                                 vicon->init_audio(audio_len*sizeof(vicon_audio_t));
1043                                                 vicon->load_audio();
1044                                                 gui->vicon_thread->add_vicon(vicon);
1045                                         }
1046                                         gui->lock_window("AssetPicon::create_objects 4");
1047                                         icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
1048                                         icon->draw_vframe(icon_vframe,
1049                                                 0, 0, pixmap_w, pixmap_h, 0, 0);
1050                                 }
1051                                 else {
1052                                         eprintf("Unable to open %s\nin asset: %s",asset->path, get_text());
1053                                         gui->lock_window("AssetPicon::create_objects 5");
1054                                         icon = gui->audio_icon;
1055                                         icon_vframe = gui->audio_vframe;
1056                                 }
1057                         }
1058                         else {
1059                                 icon = gui->audio_icon;
1060                                 icon_vframe = gui->audio_vframe;
1061                         }
1062
1063                 }
1064                 struct stat st;
1065                 comments_time = !stat(asset->path, &st) ? st.st_mtime : 0;
1066                 comments_rate = asset->get_frame_rate();
1067                 comments_ffmt = asset->format == FILE_FFMPEG ? '=' : '-';
1068                 comments_type = asset->format == FILE_FFMPEG ?
1069                                 asset->vcodec : File::formattostr(asset->format);
1070         }
1071         else
1072         if( indexable && !indexable->is_asset ) {
1073                 icon = gui->video_icon;
1074                 icon_vframe = gui->video_vframe;
1075         }
1076         else
1077         if( edl ) {
1078                 if( edl->tracks->playable_video_tracks() ) {
1079                         if( mwindow->preferences->use_thumbnails ) {
1080                                 gui->unlock_window();
1081                                 AssetVIconThread *avt = gui->vicon_thread;
1082                                 char clip_icon_path[BCTEXTLEN];
1083                                 char *clip_icon = edl->local_session->clip_icon;
1084                                 VFrame *vframe = 0;
1085                                 if( clip_icon[0] ) {
1086                                         snprintf(clip_icon_path, sizeof(clip_icon_path),
1087                                                 "%s/%s", File::get_config_path(), clip_icon);
1088                                         vframe = VFramePng::vframe_png(clip_icon_path);
1089                                 }
1090                                 if( vframe &&
1091                                     ( vframe->get_w() != avt->vw || vframe->get_h() != avt->vh ) ) {
1092                                         delete vframe;  vframe = 0;
1093                                 }
1094                                 int edl_h = edl->get_h(), edl_w = edl->get_w();
1095                                 int height = edl_h > 0 ? edl_h : 1;
1096                                 int width = edl_w > 0 ? edl_w : 1;
1097                                 int color_model = edl->session->color_model;
1098                                 if( !vframe ) {
1099 //printf("render clip: %s\n", name);
1100                                         VFrame::get_temp(gui->temp_picon, width, height, color_model);
1101                                         char string[BCTEXTLEN];
1102                                         sprintf(string, _("Rendering %s"), name);
1103                                         mwindow->gui->lock_window("AssetPicon::create_objects");
1104                                         mwindow->gui->show_message(string);
1105                                         mwindow->gui->unlock_window();
1106                                         open_render_engine(edl, 0);
1107                                         render_video(0, gui->temp_picon);
1108                                         close_render_engine();
1109                                         vframe = new VFrame(avt->vw, avt->vh, BC_RGB888);
1110                                         vframe->transfer_from(gui->temp_picon);
1111                                         if( clip_icon[0] )
1112                                                 vframe->write_png(clip_icon_path);
1113                                 }
1114                                 pixmap_w = pixmap_h * width / height;
1115                                 vicon = new AssetVIcon(this, pixmap_w, pixmap_h, VICON_RATE, 1);
1116                                 vicon->add_image(vframe, avt->vw, avt->vh, avt->vicon_cmodel);
1117                                 icon_vframe = new VFrame(pixmap_w, pixmap_h, BC_RGB888);
1118                                 icon_vframe->transfer_from(vframe);
1119                                 delete vframe;
1120                                 gui->lock_window("AssetPicon::create_objects 0");
1121                                 icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
1122                                 icon->draw_vframe(icon_vframe,
1123                                         0, 0, pixmap_w, pixmap_h, 0, 0);
1124                         }
1125                         else {
1126                                 icon = gui->clip_icon;
1127                                 icon_vframe = gui->clip_vframe;
1128                         }
1129                 }
1130                 else
1131                 if( edl->tracks->playable_audio_tracks() ) {
1132                         if( mwindow->preferences->use_thumbnails ) {
1133                                 gui->unlock_window();
1134                                 char clip_icon_path[BCTEXTLEN];
1135                                 char *clip_icon = edl->local_session->clip_icon;
1136                                 if( clip_icon[0] ) {
1137                                         snprintf(clip_icon_path, sizeof(clip_icon_path),
1138                                                 "%s/%s", File::get_config_path(), clip_icon);
1139                                         icon_vframe = VFramePng::vframe_png(clip_icon_path);
1140                                 }
1141                                 if( !icon_vframe ) {
1142                                         pixmap_w = pixmap_h * 16/9;
1143                                         icon_vframe = new VFrame(0,
1144                                                 -1, pixmap_w, pixmap_h, BC_RGB888, -1);
1145                                         icon_vframe->clear_frame();
1146                                         char string[BCTEXTLEN];
1147                                         sprintf(string, _("Rendering %s"), name);
1148                                         mwindow->gui->lock_window("AssetPicon::create_objects 3");
1149                                         mwindow->gui->show_message(string);
1150                                         mwindow->gui->unlock_window();
1151                                         int sample_rate = edl->get_sample_rate();
1152                                         int channels = edl->get_audio_channels();
1153                                         if( channels > 2 ) channels = 2;
1154                                         int64_t audio_samples = edl->get_audio_samples();
1155                                         double duration = (double)audio_samples / sample_rate;
1156                                         draw_hue_bar(icon_vframe, duration);
1157                                         Samples *samples[MAX_CHANNELS];
1158                                         int bfrsz = sample_rate;
1159                                         for( int i=0; i<MAX_CHANNELS; ++i )
1160                                                 samples[i] = i<channels ? new Samples(bfrsz) : 0;
1161                                         open_render_engine(edl, 1);
1162                                         render_audio(0, samples, bfrsz);
1163                                         close_render_engine();
1164                                         gui->lock_window("AssetPicon::create_objects 4");
1165                                         static int line_colors[2] = { GREEN, YELLOW };
1166                                         static int base_colors[2] = { RED, PINK };
1167                                         for( int i=channels; --i>=0; ) {
1168                                                 draw_wave(icon_vframe, samples[i]->get_data(), bfrsz,
1169                                                         base_colors[i], line_colors[i]);
1170                                         }
1171                                         for( int i=0; i<channels; ++i ) delete samples[i];
1172                                         if( clip_icon[0] ) icon_vframe->write_png(clip_icon_path);
1173                                 }
1174                                 else {
1175                                         pixmap_w = icon_vframe->get_w();
1176                                         pixmap_h = icon_vframe->get_h();
1177                                 }
1178                                 icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
1179                                 icon->draw_vframe(icon_vframe,
1180                                         0, 0, pixmap_w, pixmap_h, 0, 0);
1181                         }
1182                         else {
1183                                 icon = gui->clip_icon;
1184                                 icon_vframe = gui->clip_vframe;
1185                         }
1186                 }
1187         }
1188         else
1189         if( plugin ) {
1190                 strcpy(name, _(plugin->title));
1191                 set_text(name);
1192                 icon_vframe = plugin->get_picon();
1193                 if( icon_vframe )
1194                         icon = gui->create_pixmap(icon_vframe);
1195                 else if( plugin->audio ) {
1196                         if( plugin->transition ) {
1197                                 icon = gui->atransition_icon;
1198                                 icon_vframe = gui->atransition_vframe;
1199                         }
1200                         else if( plugin->is_ffmpeg() ) {
1201                                 icon = gui->ff_aud_icon;
1202                                 icon_vframe = gui->ff_aud_vframe;
1203                         }
1204                         else if( plugin->is_ladspa() ) {
1205                                 icon = gui->ladspa_icon;
1206                                 icon_vframe = gui->ladspa_vframe;
1207                         }
1208                         else {
1209                                 icon = gui->aeffect_icon;
1210                                 icon_vframe = gui->aeffect_vframe;
1211                         }
1212                 }
1213                 else if( plugin->video ) {
1214                         if( plugin->transition ) {
1215                                 icon = gui->vtransition_icon;
1216                                 icon_vframe = gui->vtransition_vframe;
1217                         }
1218                         else if( plugin->is_ffmpeg() ) {
1219                                 icon = gui->ff_vid_icon;
1220                                 icon_vframe = gui->ff_vid_vframe;
1221                         }
1222                         else {
1223                                 icon = gui->veffect_icon;
1224                                 icon_vframe = gui->veffect_vframe;
1225                         }
1226                 }
1227         }
1228         else
1229         if( label ) {
1230                 Units::totext(name,
1231                               label->position,
1232                               mwindow->edl->session->time_format,
1233                               mwindow->edl->session->sample_rate,
1234                               mwindow->edl->session->frame_rate,
1235                               mwindow->edl->session->frames_per_foot);
1236                 set_text(name);
1237                 icon = gui->label_icon;
1238                 icon_vframe = gui->label_vframe;
1239         }
1240         if( !icon ) {
1241                 icon = gui->file_icon;
1242                 icon_vframe = BC_WindowBase::get_resources()->type_to_icon[ICON_UNKNOWN];
1243         }
1244         set_icon(icon);
1245         set_icon_vframe(icon_vframe);
1246 }
1247
1248 AWindowGUI::AWindowGUI(MWindow *mwindow, AWindow *awindow)
1249  : BC_Window(_(PROGRAM_NAME ": Resources"),
1250         mwindow->session->awindow_x, mwindow->session->awindow_y,
1251         mwindow->session->awindow_w, mwindow->session->awindow_h,
1252         100, 100, 1, 1, 1)
1253 {
1254         this->mwindow = mwindow;
1255         this->awindow = awindow;
1256
1257         file_vframe = 0;                file_icon = 0;
1258         folder_vframe = 0;              folder_icon = 0;
1259         audio_vframe = 0;               audio_icon = 0;
1260         video_vframe = 0;               video_icon = 0;
1261         label_vframe = 0;               label_icon = 0;
1262
1263         aeffect_folder_vframe = 0;      aeffect_folder_icon = 0;
1264         atransition_folder_vframe = 0;  atransition_folder_icon = 0;
1265         clip_folder_vframe = 0;         clip_folder_icon = 0;
1266         label_folder_vframe = 0;        label_folder_icon = 0;
1267         media_folder_vframe = 0;        media_folder_icon = 0;
1268         proxy_folder_vframe = 0;        proxy_folder_icon = 0;
1269         veffect_folder_vframe = 0;      veffect_folder_icon = 0;
1270         vtransition_folder_vframe = 0;  vtransition_folder_icon = 0;
1271
1272         ladspa_vframe = 0;              ladspa_icon = 0;
1273         ff_aud_vframe = 0;              ff_aud_icon = 0;
1274         ff_vid_vframe = 0;              ff_vid_icon = 0;
1275
1276         clip_vframe = 0;                clip_icon = 0;
1277         atransition_vframe = 0;         atransition_icon = 0;
1278         vtransition_vframe = 0;         vtransition_icon = 0;
1279         aeffect_vframe = 0;             aeffect_icon = 0;
1280         veffect_vframe = 0;             veffect_icon = 0;
1281
1282         plugin_visibility = ((uint64_t)1<<(8*sizeof(uint64_t)-1))-1;
1283         asset_menu = 0;
1284         effectlist_menu = 0;
1285         assetlist_menu = 0;
1286         cliplist_menu = 0;
1287         proxylist_menu = 0;
1288         labellist_menu = 0;
1289         folderlist_menu = 0;
1290         temp_picon = 0;
1291         search_text = 0;
1292         allow_iconlisting = 1;
1293         remove_plugin = 0;
1294         vicon_thread = 0;
1295         vicon_audio = 0;
1296         vicon_drawing = AVICON_FULL_PLAY;
1297         play_off = 0;
1298         displayed_folder = AW_NO_FOLDER;
1299         new_folder_thread = 0;
1300         modify_folder_thread = 0;
1301         folder_lock = new Mutex("AWindowGUI::folder_lock");
1302 }
1303
1304 AWindowGUI::~AWindowGUI()
1305 {
1306         assets.remove_all_objects();
1307         folders.remove_all_objects();
1308         aeffects.remove_all_objects();
1309         veffects.remove_all_objects();
1310         atransitions.remove_all_objects();
1311         vtransitions.remove_all_objects();
1312         labellist.remove_all_objects();
1313         displayed_assets[1].remove_all_objects();
1314
1315         delete new_folder_thread;
1316         delete modify_folder_thread;
1317         delete vicon_thread;
1318         delete vicon_audio;
1319
1320         delete search_text;
1321         delete temp_picon;
1322         delete remove_plugin;
1323
1324         delete file_vframe;             delete file_icon;
1325         delete folder_vframe;           delete folder_icon;
1326         delete audio_vframe;            delete audio_icon;
1327         delete video_vframe;            delete video_icon;
1328         delete label_vframe;            delete label_icon;
1329         delete clip_vframe;             delete clip_icon;
1330         delete aeffect_folder_vframe;   delete aeffect_folder_icon;
1331         delete atransition_folder_vframe; delete atransition_folder_icon;
1332         delete veffect_folder_vframe;   delete veffect_folder_icon;
1333         delete vtransition_folder_vframe; delete vtransition_folder_icon;
1334         delete clip_folder_vframe;      delete clip_folder_icon;
1335         delete label_folder_vframe;     delete label_folder_icon;
1336         delete media_folder_vframe;     delete media_folder_icon;
1337         delete proxy_folder_vframe;     delete proxy_folder_icon;
1338         delete ladspa_vframe;           delete ladspa_icon;
1339         delete ff_aud_vframe;           delete ff_aud_icon;
1340         delete ff_vid_vframe;           delete ff_vid_icon;
1341         delete atransition_vframe;      delete atransition_icon;
1342         delete vtransition_vframe;      delete vtransition_icon;
1343         delete aeffect_vframe;          delete aeffect_icon;
1344         delete veffect_vframe;          delete veffect_icon;
1345         delete folder_lock;
1346 }
1347
1348 bool AWindowGUI::protected_pixmap(BC_Pixmap *icon)
1349 {
1350         return  icon == file_icon ||
1351                 icon == folder_icon ||
1352                 icon == audio_icon ||
1353                 icon == video_icon ||
1354                 icon == clip_icon ||
1355                 icon == label_icon ||
1356                 icon == vtransition_icon ||
1357                 icon == atransition_icon ||
1358                 icon == veffect_icon ||
1359                 icon == aeffect_icon ||
1360                 icon == ladspa_icon ||
1361                 icon == ff_aud_icon ||
1362                 icon == ff_vid_icon ||
1363                 icon == aeffect_folder_icon ||
1364                 icon == veffect_folder_icon ||
1365                 icon == atransition_folder_icon ||
1366                 icon == vtransition_folder_icon ||
1367                 icon == label_folder_icon ||
1368                 icon == clip_folder_icon ||
1369                 icon == media_folder_icon ||
1370                 icon == proxy_folder_icon;
1371 }
1372
1373 VFrame *AWindowGUI::get_picon(const char *name, const char *plugin_icons)
1374 {
1375         char png_path[BCTEXTLEN];
1376         char *pp = png_path, *ep = pp + sizeof(png_path)-1;
1377         snprintf(pp, ep-pp, "%s/picon/%s/%s.png",
1378                 File::get_plugin_path(), plugin_icons, name);
1379         if( access(png_path, R_OK) ) return 0;
1380         return VFramePng::vframe_png(png_path,0,0);
1381 }
1382
1383 VFrame *AWindowGUI::get_picon(const char *name)
1384 {
1385         VFrame *vframe = get_picon(name, mwindow->preferences->plugin_icons);
1386         if( !vframe ) {
1387                 char png_name[BCSTRLEN], *pp = png_name, *ep = pp + sizeof(png_name)-1;
1388                 snprintf(pp, ep-pp, "%s.png", name);
1389                 unsigned char *data = mwindow->theme->get_image_data(png_name);
1390                 if( data ) vframe = new VFramePng(data, 0.);
1391         }
1392         return vframe;
1393 }
1394
1395 void AWindowGUI::resource_icon(VFrame *&vfrm, BC_Pixmap *&icon, const char *fn, int idx)
1396 {
1397         vfrm = get_picon(fn);
1398         if( !vfrm ) vfrm = BC_WindowBase::get_resources()->type_to_icon[idx];
1399         icon = new BC_Pixmap(this, vfrm, PIXMAP_ALPHA);
1400 }
1401 void AWindowGUI::theme_icon(VFrame *&vfrm, BC_Pixmap *&icon, const char *fn)
1402 {
1403         vfrm = get_picon(fn);
1404         if( !vfrm ) vfrm = mwindow->theme->get_image(fn);
1405         icon = new BC_Pixmap(this, vfrm, PIXMAP_ALPHA);
1406 }
1407 void AWindowGUI::plugin_icon(VFrame *&vfrm, BC_Pixmap *&icon, const char *fn, unsigned char *png)
1408 {
1409         vfrm = get_picon(fn);
1410         if( !vfrm ) vfrm = new VFramePng(png);
1411         icon = new BC_Pixmap(this, vfrm, PIXMAP_ALPHA);
1412 }
1413
1414 void AWindowGUI::create_objects()
1415 {
1416         lock_window("AWindowGUI::create_objects");
1417         asset_titles[0] = C_("Title");
1418         asset_titles[1] = _("Comments");
1419
1420         set_icon(mwindow->theme->get_image("awindow_icon"));
1421
1422         resource_icon(file_vframe,   file_icon,   "film_icon",   ICON_UNKNOWN);
1423         resource_icon(folder_vframe, folder_icon, "folder_icon", ICON_FOLDER);
1424         resource_icon(audio_vframe,  audio_icon,  "audio_icon",  ICON_SOUND);
1425         resource_icon(video_vframe,  video_icon,  "video_icon",  ICON_FILM);
1426         resource_icon(label_vframe,  label_icon,  "label_icon",  ICON_LABEL);
1427
1428         theme_icon(aeffect_folder_vframe,      aeffect_folder_icon,     "aeffect_folder");
1429         theme_icon(atransition_folder_vframe,  atransition_folder_icon, "atransition_folder");
1430         theme_icon(clip_folder_vframe,         clip_folder_icon,        "clip_folder");
1431         theme_icon(label_folder_vframe,        label_folder_icon,       "label_folder");
1432         theme_icon(media_folder_vframe,        media_folder_icon,       "media_folder");
1433         theme_icon(proxy_folder_vframe,        proxy_folder_icon,       "proxy_folder");
1434         theme_icon(veffect_folder_vframe,      veffect_folder_icon,     "veffect_folder");
1435         theme_icon(vtransition_folder_vframe,  vtransition_folder_icon, "vtransition_folder");
1436
1437         folder_icons[AW_AEFFECT_FOLDER] = aeffect_folder_icon;
1438         folder_icons[AW_VEFFECT_FOLDER] = veffect_folder_icon;
1439         folder_icons[AW_ATRANSITION_FOLDER] = atransition_folder_icon;
1440         folder_icons[AW_VTRANSITION_FOLDER] = vtransition_folder_icon;
1441         folder_icons[AW_LABEL_FOLDER] = label_folder_icon;
1442         folder_icons[AW_CLIP_FOLDER] = clip_folder_icon;
1443         folder_icons[AW_MEDIA_FOLDER] = media_folder_icon;
1444         folder_icons[AW_PROXY_FOLDER] = proxy_folder_icon;
1445
1446         theme_icon(clip_vframe,        clip_icon,        "clip_icon");
1447         theme_icon(atransition_vframe, atransition_icon, "atransition_icon");
1448         theme_icon(vtransition_vframe, vtransition_icon, "vtransition_icon");
1449         theme_icon(aeffect_vframe,     aeffect_icon,     "aeffect_icon");
1450         theme_icon(veffect_vframe,     veffect_icon,     "veffect_icon");
1451
1452         plugin_icon(ladspa_vframe, ladspa_icon, "lad_picon", lad_picon_png);
1453         plugin_icon(ff_aud_vframe, ff_aud_icon, "ff_audio",  ff_audio_png);
1454         plugin_icon(ff_vid_vframe, ff_vid_icon, "ff_video",  ff_video_png);
1455         folder_lock->lock("AWindowGUI::create_objects");
1456 // Mandatory folders
1457         folders.append(new AssetPicon(mwindow, this, AW_AEFFECT_FOLDER, 1));
1458         folders.append(new AssetPicon(mwindow, this, AW_VEFFECT_FOLDER, 1));
1459         folders.append(new AssetPicon(mwindow, this, AW_ATRANSITION_FOLDER, 1));
1460         folders.append(new AssetPicon(mwindow, this, AW_VTRANSITION_FOLDER, 1));
1461         folders.append(new AssetPicon(mwindow, this, AW_LABEL_FOLDER, 1));
1462         folders.append(new AssetPicon(mwindow, this, AW_CLIP_FOLDER, 1));
1463         folders.append(new AssetPicon(mwindow, this, AW_PROXY_FOLDER, 1));
1464         folders.append(new AssetPicon(mwindow, this, AW_MEDIA_FOLDER, 1));
1465
1466         create_label_folder();
1467         folder_lock->unlock();
1468
1469         mwindow->theme->get_awindow_sizes(this);
1470         load_defaults(mwindow->defaults);
1471         new_folder_thread = new NewFolderThread(this);
1472         modify_folder_thread = new ModifyFolderThread(this);
1473
1474         int x1 = mwindow->theme->alist_x, y1 = mwindow->theme->alist_y;
1475         int w1 = mwindow->theme->alist_w, h1 = mwindow->theme->alist_h;
1476         search_text = new AWindowSearchText(mwindow, this, x1, y1+5);
1477         search_text->create_objects();
1478         int dy = search_text->get_h() + 10;
1479         y1 += dy;  h1 -= dy;
1480         add_subwindow(asset_list = new AWindowAssets(mwindow, this, x1, y1, w1, h1));
1481
1482         vicon_thread = new AssetVIconThread(this, mwindow->preferences);
1483         asset_list->update_vicon_area();
1484         vicon_thread->start();
1485         vicon_audio = new AssetVIconAudio(this);
1486
1487         add_subwindow(divider = new AWindowDivider(mwindow, this,
1488                 mwindow->theme->adivider_x, mwindow->theme->adivider_y,
1489                 mwindow->theme->adivider_w, mwindow->theme->adivider_h));
1490
1491         divider->set_cursor(HSEPARATE_CURSOR, 0, 0);
1492
1493         int fx = mwindow->theme->afolders_x, fy = mwindow->theme->afolders_y;
1494         int fw = mwindow->theme->afolders_w, fh = mwindow->theme->afolders_h;
1495         int n = sizeof(AVIconDrawing::avicon_names)/sizeof(AVIconDrawing::avicon_names[0]);
1496         int tw = 0;  const char **av_names = AVIconDrawing::avicon_names;
1497         for( int i=0; i<n; ++i ) {
1498                 int nw = get_text_width(MEDIUMFONT, _(av_names[i]));
1499                 if( tw < nw )  tw = nw;
1500         }
1501         int pw = BC_PopupMenu::calculate_w(16, tw, 1);
1502         const char *text = _(AVIconDrawing::avicon_names[vicon_drawing]);
1503         add_subwindow(avicon_drawing = new AVIconDrawing(this, fw, fy, pw, text));
1504         avicon_drawing->create_objects();
1505         add_subwindow(add_tools = new AddTools(mwindow, this, fx, fy, _("Visibility")));
1506         add_tools->create_objects();
1507         fy += add_tools->get_h();  fh -= add_tools->get_h();
1508         add_subwindow(folder_list = new AWindowFolders(mwindow,
1509                 this, fx, fy, fw, fh));
1510         update_effects();
1511         folder_list->load_expanders();
1512
1513         //int x = mwindow->theme->abuttons_x;
1514         //int y = mwindow->theme->abuttons_y;
1515
1516         add_subwindow(asset_menu = new AssetPopup(mwindow, this));
1517         asset_menu->create_objects();
1518         add_subwindow(clip_menu = new ClipPopup(mwindow, this));
1519         clip_menu->create_objects();
1520         add_subwindow(label_menu = new LabelPopup(mwindow, this));
1521         label_menu->create_objects();
1522         add_subwindow(proxy_menu = new ProxyPopup(mwindow, this));
1523         proxy_menu->create_objects();
1524
1525         add_subwindow(effectlist_menu = new EffectListMenu(mwindow, this));
1526         effectlist_menu->create_objects();
1527         add_subwindow(assetlist_menu = new AssetListMenu(mwindow, this));
1528         assetlist_menu->create_objects();
1529         add_subwindow(cliplist_menu = new ClipListMenu(mwindow, this));
1530         cliplist_menu->create_objects();
1531         add_subwindow(labellist_menu = new LabelListMenu(mwindow, this));
1532         labellist_menu->create_objects();
1533         add_subwindow(proxylist_menu = new ProxyListMenu(mwindow, this));
1534         proxylist_menu->create_objects();
1535
1536         add_subwindow(folderlist_menu = new FolderListMenu(mwindow, this));
1537         folderlist_menu->create_objects();
1538
1539         create_custom_xatoms();
1540         unlock_window();
1541 }
1542
1543 int AWindowGUI::resize_event(int w, int h)
1544 {
1545         mwindow->session->awindow_x = get_x();
1546         mwindow->session->awindow_y = get_y();
1547         mwindow->session->awindow_w = w;
1548         mwindow->session->awindow_h = h;
1549
1550         mwindow->theme->get_awindow_sizes(this);
1551         mwindow->theme->draw_awindow_bg(this);
1552         reposition_objects();
1553
1554 //      int x = mwindow->theme->abuttons_x;
1555 //      int y = mwindow->theme->abuttons_y;
1556 //      new_bin->reposition_window(x, y);
1557 //      x += new_bin->get_w();
1558 //      delete_bin->reposition_window(x, y);
1559 //      x += delete_bin->get_w();
1560 //      rename_bin->reposition_window(x, y);
1561 //      x += rename_bin->get_w();
1562 //      delete_disk->reposition_window(x, y);
1563 //      x += delete_disk->get_w();
1564 //      delete_project->reposition_window(x, y);
1565 //      x += delete_project->get_w();
1566 //      info->reposition_window(x, y);
1567 //      x += info->get_w();
1568 //      redraw_index->reposition_window(x, y);
1569 //      x += redraw_index->get_w();
1570 //      paste->reposition_window(x, y);
1571 //      x += paste->get_w();
1572 //      append->reposition_window(x, y);
1573 //      x += append->get_w();
1574 //      view->reposition_window(x, y);
1575
1576         BC_WindowBase::resize_event(w, h);
1577         asset_list->update_vicon_area();
1578         return 1;
1579 }
1580
1581 int AWindowGUI::translation_event()
1582 {
1583         mwindow->session->awindow_x = get_x();
1584         mwindow->session->awindow_y = get_y();
1585         return 0;
1586 }
1587
1588 void AWindowGUI::reposition_objects()
1589 {
1590         int x1 = mwindow->theme->alist_x, y1 = mwindow->theme->alist_y;
1591         int w1 = mwindow->theme->alist_w, h1 = mwindow->theme->alist_h;
1592         search_text->reposition_window(x1, y1+5, w1);
1593         int dy = search_text->get_h() + 10;
1594         y1 += dy;  h1 -= dy;
1595         asset_list->reposition_window(x1, y1, w1, h1);
1596         divider->reposition_window(
1597                 mwindow->theme->adivider_x, mwindow->theme->adivider_y,
1598                 mwindow->theme->adivider_w, mwindow->theme->adivider_h);
1599         int fx = mwindow->theme->afolders_x, fy = mwindow->theme->afolders_y;
1600         int fw = mwindow->theme->afolders_w, fh = mwindow->theme->afolders_h;
1601         add_tools->resize_event(fw-avicon_w, add_tools->get_h());
1602         avicon_drawing->reposition_window(fw-avicon_drawing->get_w(), fy);
1603         fy += add_tools->get_h();  fh -= add_tools->get_h();
1604         folder_list->reposition_window(fx, fy, fw, fh);
1605 }
1606
1607 int AWindowGUI::save_defaults(BC_Hash *defaults)
1608 {
1609         defaults->update("PLUGIN_VISIBILTY", plugin_visibility);
1610         defaults->update("VICON_DRAWING", vicon_drawing);
1611         return 0;
1612 }
1613
1614 int AWindowGUI::load_defaults(BC_Hash *defaults)
1615 {
1616         plugin_visibility = defaults->get("PLUGIN_VISIBILTY", plugin_visibility);
1617         vicon_drawing = defaults->get("VICON_DRAWING", vicon_drawing);
1618         return 0;
1619 }
1620
1621 int AWindowGUI::close_event()
1622 {
1623         hide_window();
1624         mwindow->session->show_awindow = 0;
1625         unlock_window();
1626
1627         mwindow->gui->lock_window("AWindowGUI::close_event");
1628         mwindow->gui->mainmenu->show_awindow->set_checked(0);
1629         mwindow->gui->unlock_window();
1630
1631         lock_window("AWindowGUI::close_event");
1632         save_defaults(mwindow->defaults);
1633         mwindow->save_defaults();
1634         return 1;
1635 }
1636
1637 int AWindowGUI::start_vicon_drawing()
1638 {
1639         if( play_off ) return stop_vicon_drawing();
1640         if( !vicon_thread->interrupted ) return 1;
1641         switch( vicon_drawing ) {
1642         case AVICON_FULL_PLAY:  // all vicons, viewing, target
1643                 break;
1644         case AVICON_MOUSE_OVER: // one vicon, viewing, target
1645                 if( vicon_thread->solo ) break;
1646         // fall thru
1647         case AVICON_SRC_TARGET: // no vicons, no view, target
1648         case AVICON_NO_PLAY:    // no vicons, no view, no target
1649                 return 0;
1650         }
1651         if( mwindow->edl->session->awindow_folder == AW_MEDIA_FOLDER ||
1652             mwindow->edl->session->awindow_folder == AW_PROXY_FOLDER ||
1653             mwindow->edl->session->awindow_folder == AW_CLIP_FOLDER ||
1654             mwindow->edl->session->awindow_folder >= AWINDOW_USER_FOLDERS ) {
1655                 switch( mwindow->edl->session->assetlist_format ) {
1656                 case ASSETS_ICONS:
1657                 case ASSETS_ICONS_PACKED:
1658                 case ASSETS_ICON_LIST:
1659                         asset_list->update_vicon_area();
1660                         vicon_thread->start_drawing();
1661                         break;
1662                 default:
1663                         break;
1664                 }
1665         }
1666         return 1;
1667 }
1668
1669 int AWindowGUI::stop_vicon_drawing()
1670 {
1671         if( !vicon_thread->interrupted )
1672                 vicon_thread->stop_drawing();
1673         return 0;
1674 }
1675
1676 void AWindowGUI::close_view_popup()
1677 {
1678         vicon_thread->close_view_popup();
1679 }
1680
1681 VFrame *AssetPicon::get_vicon_frame()
1682 {
1683         if( !vicon ) return 0;
1684         if( gui->vicon_thread->interrupted ) return 0;
1685         VFrame *frame = vicon->frame();
1686         if( !frame ) return 0;
1687         if( !vicon_frame )
1688                 vicon_frame = new VFrame(vicon->w, vicon->h, frame->get_color_model());
1689         vicon_frame->transfer_from(frame);
1690         return vicon_frame;
1691 }
1692
1693 int AWindowGUI::cycle_assetlist_format()
1694 {
1695         EDLSession *session = mwindow->edl->session;
1696         int format = ASSETS_TEXT;
1697         if( allow_iconlisting ) {
1698                 switch( session->assetlist_format ) {
1699                 case ASSETS_TEXT:
1700                         format = ASSETS_ICONS;
1701                         break;
1702                 case ASSETS_ICONS:
1703                         format = ASSETS_ICONS_PACKED;
1704                         break;
1705                 case ASSETS_ICONS_PACKED:
1706                         format = ASSETS_ICON_LIST;
1707                         break;
1708                 case ASSETS_ICON_LIST:
1709                         format = ASSETS_TEXT;
1710                         break;
1711                 }
1712         }
1713         stop_vicon_drawing();
1714         session->assetlist_format = format;
1715         asset_list->update_format(session->assetlist_format, 0);
1716         async_update_assets();
1717         start_vicon_drawing();
1718         return 1;
1719 }
1720
1721 AWindowRemovePluginGUI::
1722 AWindowRemovePluginGUI(AWindow *awindow, AWindowRemovePlugin *thread,
1723         int x, int y, PluginServer *plugin)
1724  : BC_Window(_(PROGRAM_NAME ": Remove plugin"), x,y, 500,200, 50, 50, 1, 0, 1, -1, "", 1)
1725 {
1726         this->awindow = awindow;
1727         this->thread = thread;
1728         this->plugin = plugin;
1729         VFrame *vframe = plugin->get_picon();
1730         icon = vframe ? create_pixmap(vframe) : 0;
1731         plugin_list.append(new BC_ListBoxItem(plugin->title, icon));
1732 }
1733
1734 AWindowRemovePluginGUI::
1735 ~AWindowRemovePluginGUI()
1736 {
1737         if( !awindow->gui->protected_pixmap(icon) )
1738                 delete icon;
1739         plugin_list.remove_all();
1740 }
1741
1742 void AWindowRemovePluginGUI::create_objects()
1743 {
1744         lock_window("AWindowRemovePluginGUI::create_objects");
1745         BC_Button *ok_button = new BC_OKButton(this);
1746         add_subwindow(ok_button);
1747         BC_Button *cancel_button = new BC_CancelButton(this);
1748         add_subwindow(cancel_button);
1749         int x = 10, y = 10;
1750         BC_Title *title = new BC_Title(x, y, _("remove plugin?"));
1751         add_subwindow(title);
1752         y += title->get_h() + 5;
1753         list = new BC_ListBox(x, y,
1754                 get_w() - 20, ok_button->get_y() - y - 5, LISTBOX_TEXT, &plugin_list,
1755                 0, 0, 1, 0, 0, LISTBOX_SINGLE, ICON_LEFT, 0);
1756         add_subwindow(list);
1757         show_window();
1758         unlock_window();
1759 }
1760
1761 int AWindowRemovePlugin::remove_plugin(PluginServer *plugin, ArrayList<BC_ListBoxItem*> &folder)
1762 {
1763         int ret = 0;
1764         for( int i=0; i<folder.size(); ) {
1765                 AssetPicon *picon = (AssetPicon *)folder[i];
1766                 if( picon->plugin == plugin ) {
1767                         folder.remove_object_number(i);
1768                         ++ret;
1769                         continue;
1770                 }
1771                 ++i;
1772         }
1773         return ret;
1774 }
1775
1776 void AWindowRemovePlugin::handle_close_event(int result)
1777 {
1778         if( !result ) {
1779                 printf(_("remove %s\n"), plugin->path);
1780                 awindow->gui->lock_window("AWindowRemovePlugin::handle_close_event");
1781                 ArrayList<BC_ListBoxItem*> *folder =
1782                         plugin->audio ? plugin->transition ?
1783                                 &awindow->gui->atransitions :
1784                                 &awindow->gui->aeffects :
1785                         plugin->video ?  plugin->transition ?
1786                                 &awindow->gui->vtransitions :
1787                                 &awindow->gui->veffects :
1788                         0;
1789                 if( folder ) remove_plugin(plugin, *folder);
1790                 MWindow *mwindow = awindow->mwindow;
1791                 awindow->gui->unlock_window();
1792                 char plugin_path[BCTEXTLEN];
1793                 strcpy(plugin_path, plugin->path);
1794                 mwindow->plugindb->remove(plugin);
1795                 remove(plugin_path);
1796                 char index_path[BCTEXTLEN];
1797                 mwindow->create_defaults_path(index_path, PLUGIN_FILE);
1798                 remove(index_path);
1799                 char picon_path[BCTEXTLEN];
1800                 FileSystem fs;
1801                 snprintf(picon_path, sizeof(picon_path), "%s/picon",
1802                         File::get_plugin_path());
1803                 char png_name[BCSTRLEN], png_path[BCTEXTLEN];
1804                 plugin->get_plugin_png_name(png_name);
1805                 fs.update(picon_path);
1806                 for( int i=0; i<fs.dir_list.total; ++i ) {
1807                         char *fs_path = fs.dir_list[i]->path;
1808                         if( !fs.is_dir(fs_path) ) continue;
1809                         snprintf(png_path, sizeof(picon_path), "%s/%s",
1810                                 fs_path, png_name);
1811                         remove(png_path);
1812                 }
1813                 delete plugin;  plugin = 0;
1814                 awindow->gui->async_update_assets();
1815         }
1816 }
1817
1818 AWindowRemovePlugin::
1819 AWindowRemovePlugin(AWindow *awindow, PluginServer *plugin)
1820  : BC_DialogThread()
1821 {
1822         this->awindow = awindow;
1823         this->plugin = plugin;
1824 }
1825
1826 AWindowRemovePlugin::
1827 ~AWindowRemovePlugin()
1828 {
1829         close_window();
1830 }
1831
1832 BC_Window* AWindowRemovePlugin::new_gui()
1833 {
1834         int x = awindow->gui->get_abs_cursor_x(0);
1835         int y = awindow->gui->get_abs_cursor_y(0);
1836         AWindowRemovePluginGUI *gui = new AWindowRemovePluginGUI(awindow, this, x, y, plugin);
1837         gui->create_objects();
1838         return gui;
1839 }
1840
1841 int AWindowGUI::keypress_event()
1842 {
1843         switch( get_keypress() ) {
1844         case 'w': case 'W':
1845                 if( ctrl_down() ) {
1846                         close_event();
1847                         return 1;
1848                 }
1849                 break;
1850         case 'o':
1851                 if( !ctrl_down() && !shift_down() ) {
1852                         assetlist_menu->load_file->handle_event();
1853                         return 1;
1854                 }
1855                 break;
1856         case 'v':
1857                 return cycle_assetlist_format();
1858         case DELETE:
1859                 if( shift_down() && ctrl_down() ) {
1860                         PluginServer* plugin = selected_plugin();
1861                         if( !plugin ) break;
1862                         remove_plugin = new AWindowRemovePlugin(awindow, plugin);
1863                         unlock_window();
1864                         remove_plugin->start();
1865                         lock_window("AWindowGUI::keypress_event 1");
1866                         return 1;
1867                 }
1868                 collect_assets();
1869                 if( shift_down() ) {
1870                         mwindow->awindow->asset_remove->start();
1871                         return 1;
1872                 }
1873                 unlock_window();
1874                 mwindow->remove_assets_from_project(1, 1,
1875                         mwindow->session->drag_assets,
1876                         mwindow->session->drag_clips);
1877                 lock_window("AWindowGUI::keypress_event 2");
1878                 return 1;
1879         case KEY_F1:
1880         case KEY_F2:
1881         case KEY_F3:
1882         case KEY_F4:
1883                 if( shift_down() && ctrl_down() ) {
1884                         resend_event(mwindow->gui);
1885                         return 1;
1886                 }
1887                 break;
1888         }
1889         return 0;
1890 }
1891
1892
1893
1894 int AWindowGUI::create_custom_xatoms()
1895 {
1896         UpdateAssetsXAtom = create_xatom("CWINDOWGUI_UPDATE_ASSETS");
1897         return 0;
1898 }
1899 int AWindowGUI::receive_custom_xatoms(xatom_event *event)
1900 {
1901         if( event->message_type == UpdateAssetsXAtom ) {
1902                 update_assets();
1903                 return 1;
1904         }
1905         return 0;
1906 }
1907
1908 void AWindowGUI::async_update_assets()
1909 {
1910         xatom_event event;
1911         event.message_type = UpdateAssetsXAtom;
1912         send_custom_xatom(&event);
1913 }
1914
1915
1916 void AWindowGUI::update_folder_list()
1917 {
1918         for( int i = 0; i < folders.total; i++ ) {
1919                 AssetPicon *picon = (AssetPicon*)folders.values[i];
1920                 picon->in_use = 0;
1921         }
1922
1923 // Search assets for folders
1924         for( int i = 0; i < mwindow->edl->folders.total; i++ ) {
1925                 BinFolder *bin_folder = mwindow->edl->folders[i];
1926                 int exists = 0;
1927
1928                 for( int j = 0; j < folders.total; j++ ) {
1929                         AssetPicon *picon = (AssetPicon*)folders[j];
1930                         if( !strcasecmp(picon->get_text(), bin_folder->title) ) {
1931                                 exists = 1;
1932                                 picon->in_use = 1;
1933                                 break;
1934                         }
1935                 }
1936
1937                 if( !exists ) {
1938                         const char *title = bin_folder->title;
1939                         int folder = bin_folder->awindow_folder;
1940                         AssetPicon *picon = new AssetPicon(mwindow, this, folder, title);
1941                         picon->create_objects();
1942                         folders.append(picon);
1943                 }
1944         }
1945
1946 // Delete unused non-persistent folders
1947         int do_autoplace = 0;
1948         for( int i=folders.total; --i>=0; ) {
1949                 AssetPicon *picon = (AssetPicon*)folders.values[i];
1950                 if( !picon->in_use && !picon->persistent ) {
1951                         delete picon;
1952                         folders.remove_number(i);
1953                         do_autoplace = 1;
1954                 }
1955         }
1956         if( do_autoplace )
1957                 folder_list->set_autoplacement(&folders, 0, 1);
1958 }
1959
1960 void AWindowGUI::create_persistent_folder(ArrayList<BC_ListBoxItem*> *output,
1961         int do_audio, int do_video, int is_realtime, int is_transition)
1962 {
1963         ArrayList<PluginServer*> plugin_list;
1964 // Get pointers to plugindb entries
1965         mwindow->search_plugindb(do_audio, do_video, is_realtime, is_transition,
1966                         0, plugin_list);
1967
1968         for( int i = 0; i < plugin_list.total; i++ ) {
1969                 PluginServer *server = plugin_list.values[i];
1970                 int visible = plugin_visibility & (1<<server->dir_idx);
1971                 if( !visible ) continue;
1972 // Create new listitem
1973                 AssetPicon *picon = new AssetPicon(mwindow, this, server);
1974                 picon->create_objects();
1975                 output->append(picon);
1976         }
1977 }
1978
1979 void AWindowGUI::create_label_folder()
1980 {
1981         Label *current;
1982         for( current = mwindow->edl->labels->first; current; current = NEXT ) {
1983                 AssetPicon *picon = new AssetPicon(mwindow, this, current);
1984                 picon->create_objects();
1985                 labellist.append(picon);
1986         }
1987 }
1988
1989
1990 void AWindowGUI::update_asset_list()
1991 {
1992         ArrayList<AssetPicon *> new_assets;
1993         for( int i = 0; i < assets.total; i++ ) {
1994                 AssetPicon *picon = (AssetPicon*)assets.values[i];
1995                 picon->in_use = 0;
1996         }
1997
1998         mwindow->gui->lock_window("AWindowGUI::update_asset_list");
1999 // Synchronize EDL clips
2000         for( int i=0; i<mwindow->edl->clips.size(); ++i ) {
2001                 int exists = 0;
2002
2003 // Look for clip in existing listitems
2004                 for( int j = 0; j < assets.total && !exists; j++ ) {
2005                         AssetPicon *picon = (AssetPicon*)assets.values[j];
2006
2007                         if( picon->id == mwindow->edl->clips[i]->id ) {
2008                                 picon->edl = mwindow->edl->clips[i];
2009                                 picon->set_text(mwindow->edl->clips[i]->local_session->clip_title);
2010                                 exists = 1;
2011                                 picon->in_use = 1;
2012                         }
2013                 }
2014
2015 // Create new listitem
2016                 if( !exists ) {
2017                         AssetPicon *picon = new AssetPicon(mwindow,
2018                                 this, mwindow->edl->clips[i]);
2019                         new_assets.append(picon);
2020                 }
2021         }
2022
2023 // Synchronize EDL assets
2024         for( Asset *current=mwindow->edl->assets->first; current; current=NEXT ) {
2025                 int exists = 0;
2026
2027 // Look for asset in existing listitems
2028                 for( int j = 0; j < assets.total && !exists; j++ ) {
2029                         AssetPicon *picon = (AssetPicon*)assets.values[j];
2030
2031                         if( picon->id == current->id ) {
2032                                 picon->indexable = current;
2033                                 picon->in_use = 1;
2034                                 exists = 1;
2035                         }
2036                 }
2037
2038 // Create new listitem
2039                 if( !exists ) {
2040                         AssetPicon *picon = new AssetPicon(mwindow,
2041                                 this, current);
2042                         new_assets.append(picon);
2043                 }
2044         }
2045
2046 // Synchronize nested EDLs
2047         for( int i=0; i<mwindow->edl->nested_edls.size(); ++i ) {
2048                 int exists = 0;
2049                 EDL *nested_edl = mwindow->edl->nested_edls[i];
2050
2051 // Look for asset in existing listitems
2052                 for( int j=0; j<assets.total && !exists; ++j ) {
2053                         AssetPicon *picon = (AssetPicon*)assets.values[j];
2054
2055                         if( picon->id == nested_edl->id ) {
2056                                 picon->indexable = nested_edl;
2057                                 picon->in_use = 1;
2058                                 exists = 1;
2059                         }
2060                 }
2061
2062 // Create new listitem
2063                 if( !exists ) {
2064                         AssetPicon *picon = new AssetPicon(mwindow,
2065                                 this, (Indexable*)nested_edl);
2066                         new_assets.append(picon);
2067                 }
2068         }
2069         mwindow->gui->unlock_window();
2070
2071         for( int i=0; i<new_assets.size(); ++i ) {
2072                 AssetPicon *picon = new_assets[i];
2073                 picon->create_objects();
2074                 if( picon->indexable )
2075                         picon->foldernum = AW_MEDIA_FOLDER;
2076                 else if( picon->edl )
2077                         picon->foldernum = AW_CLIP_FOLDER;
2078                 assets.append(picon);
2079         }
2080
2081         mwindow->gui->lock_window();
2082         mwindow->gui->default_message();
2083         mwindow->gui->unlock_window();
2084
2085         for( int i = assets.size() - 1; i >= 0; i-- ) {
2086                 AssetPicon *picon = (AssetPicon*)assets.get(i);
2087                 if( !picon->in_use ) {
2088                         delete picon;
2089                         assets.remove_number(i);
2090                         continue;
2091                 }
2092                 if( picon->indexable && picon->indexable->is_asset ) {
2093                         Asset *asset = (Asset *)picon->indexable;
2094                         struct stat st;
2095                         picon->comments_time = !stat(asset->path, &st) ? st.st_mtime : 0;
2096                         picon->comments_rate = asset->get_frame_rate();
2097                         picon->comments_ffmt = asset->format == FILE_FFMPEG ? '=' : '-';
2098                         picon->comments_type = asset->format == FILE_FFMPEG ?
2099                                 asset->vcodec : File::formattostr(asset->format);
2100                 }
2101         }
2102 }
2103
2104 void AWindowGUI::update_picon(Indexable *indexable)
2105 {
2106         VIcon *vicon = 0;
2107         for( int i = 0; i < assets.total; i++ ) {
2108                 AssetPicon *picon = (AssetPicon*)assets.values[i];
2109                 if( picon->indexable == indexable ||
2110                     picon->edl == (EDL *)indexable ) {
2111                         char name[BCTEXTLEN];
2112                         FileSystem fs;
2113                         fs.extract_name(name, indexable->path);
2114                         picon->set_text(name);
2115                         vicon = picon->vicon;
2116                         break;
2117                 }
2118         }
2119         if( vicon ) {
2120                 stop_vicon_drawing();
2121                 vicon->clear_images();
2122                 vicon->reset(indexable->get_frame_rate());
2123                 start_vicon_drawing();
2124         }
2125 }
2126
2127 void AWindowGUI::sort_assets()
2128 {
2129         folder_lock->lock("AWindowGUI::sort_assets");
2130         switch( mwindow->edl->session->awindow_folder ) {
2131         case AW_AEFFECT_FOLDER:
2132                 sort_picons(&aeffects);
2133                 break;
2134         case AW_VEFFECT_FOLDER:
2135                 sort_picons(&veffects);
2136                 break;
2137         case AW_ATRANSITION_FOLDER:
2138                 sort_picons(&atransitions);
2139                 break;
2140         case AW_VTRANSITION_FOLDER:
2141                 sort_picons(&vtransitions);
2142                 break;
2143         case AW_LABEL_FOLDER:
2144                 sort_picons(&labellist);
2145                 break;
2146         default:
2147                 sort_picons(&assets);
2148                 break;
2149         }
2150 // reset xyposition
2151         asset_list->update_format(asset_list->get_format(), 0);
2152         folder_lock->unlock();
2153         update_assets();
2154 }
2155
2156 void AWindowGUI::sort_folders()
2157 {
2158         folder_lock->lock("AWindowGUI::update_assets");
2159 //      folder_list->collapse_recursive(&folders, 0);
2160         folder_list->set_autoplacement(&folders, 0, 1);
2161         sort_picons(&folders);
2162         folder_list->update_format(folder_list->get_format(), 0);
2163         folder_lock->unlock();
2164         update_assets();
2165 }
2166
2167 EDL *AWindowGUI::collect_proxy(Indexable *indexable)
2168 {
2169         Asset *proxy_asset = (Asset *)indexable;
2170         char path[BCTEXTLEN];
2171         int proxy_scale = mwindow->edl->session->proxy_scale;
2172         ProxyRender::from_proxy_path(path, proxy_asset, proxy_scale);
2173         Asset *unproxy_asset = mwindow->edl->assets->get_asset(path);
2174         if( !unproxy_asset || !unproxy_asset->layers ) return 0;
2175 // make a clip from proxy video tracks and unproxy audio tracks
2176         EDL *proxy_edl = new EDL(mwindow->edl);
2177         proxy_edl->create_objects();
2178         proxy_edl->set_path(proxy_asset->path);
2179         FileSystem fs;  fs.extract_name(path, proxy_asset->path);
2180         strcpy(proxy_edl->local_session->clip_title, path);
2181         strcpy(proxy_edl->local_session->clip_notes, _("Proxy clip"));
2182         proxy_edl->session->video_tracks = proxy_asset->layers;
2183         proxy_edl->session->audio_tracks = unproxy_asset->channels;
2184         proxy_edl->create_default_tracks();
2185         double length = proxy_asset->frame_rate > 0 ?
2186                 ( proxy_asset->video_length >= 0 ?
2187                         ( proxy_asset->video_length / proxy_asset->frame_rate ) :
2188                         ( proxy_edl->session->si_useduration ?
2189                                 proxy_edl->session->si_duration :
2190                                 1.0 / proxy_asset->frame_rate ) ) :
2191                 1.0 / proxy_edl->session->frame_rate;
2192         Track *current = proxy_edl->tracks->first;
2193         for( int vtrack=0; current; current=NEXT ) {
2194                 if( current->data_type != TRACK_VIDEO ) continue;
2195                 current->insert_asset(proxy_asset, 0, length, 0, vtrack++);
2196         }
2197         length = (double)unproxy_asset->audio_length / unproxy_asset->sample_rate;
2198         current = proxy_edl->tracks->first;
2199         for( int atrack=0; current; current=NEXT ) {
2200                 if( current->data_type != TRACK_AUDIO ) continue;
2201                 current->insert_asset(unproxy_asset, 0, length, 0, atrack++);
2202         }
2203         proxy_edl->folder_no = AW_PROXY_FOLDER;
2204         return proxy_edl;
2205 }
2206
2207
2208 void AWindowGUI::collect_assets(int proxy)
2209 {
2210         mwindow->session->drag_assets->remove_all();
2211         mwindow->session->drag_clips->remove_all();
2212         int i = 0;  AssetPicon *result;
2213         while( (result = (AssetPicon*)asset_list->get_selection(0, i++)) != 0 ) {
2214                 Indexable *indexable = result->indexable;
2215                 if( proxy && indexable && indexable->is_asset &&
2216                     indexable->folder_no == AW_PROXY_FOLDER ) {
2217                         EDL *drag_edl = collect_proxy(indexable);
2218                         if( drag_edl ) mwindow->session->drag_clips->append(drag_edl);
2219                         continue;
2220                 }
2221                 if( indexable ) {
2222                         mwindow->session->drag_assets->append(indexable);
2223                         continue;
2224                 }
2225                 if( result->edl ) {
2226                         mwindow->session->drag_clips->append(result->edl);
2227                         continue;
2228                 }
2229         }
2230 }
2231
2232 void AWindowGUI::copy_picons(AssetPicon *picon, ArrayList<BC_ListBoxItem*> *src)
2233 {
2234 // Remove current pointers
2235         ArrayList<BC_ListBoxItem*> *dst = displayed_assets;
2236         dst[0].remove_all();
2237         dst[1].remove_all_objects();
2238
2239         AWindowFolderSubItems *sub_items = picon ? picon->sub_items : 0;
2240         int folder = mwindow->edl->session->awindow_folder;
2241         BinFolder *bin_folder = folder < AWINDOW_USER_FOLDERS ? 0 :
2242                 mwindow->edl->get_folder(folder);
2243
2244 // Create new pointers
2245         for( int i = 0; i < src->total; i++ ) {
2246                 int visible = folder >= AW_CLIP_FOLDER ? 0 : 1;
2247                 picon = (AssetPicon*)src->values[i];
2248                 picon->sort_key = -1;
2249                 if( !visible && bin_folder ) {
2250                         Indexable *idxbl = bin_folder->is_clips ? (Indexable *)picon->edl :
2251                             picon->indexable ? picon->indexable :
2252                             picon->edl ? picon->edl->get_proxy_asset() : 0;
2253                         if( idxbl ) {
2254                                 picon->sort_key = mwindow->edl->folders.matches_indexable(folder, idxbl);
2255                                 if( picon->sort_key < 0 ) continue;
2256                                 visible = 1;
2257                         }
2258                 }
2259                 if( !visible && picon->indexable && picon->indexable->folder_no == folder )
2260                         visible = 1;
2261                 if( !visible && picon->edl && picon->edl->folder_no == folder )
2262                         visible = 1;
2263                 if( visible && sub_items ) {
2264                         if( !sub_items->matches(picon->get_text()) )
2265                                 visible = 0;
2266                 }
2267                 if( visible ) {
2268                         const char *text = search_text->get_text();
2269                         if( text && text[0] )
2270                                 visible = bstrcasestr(picon->get_text(), text) ? 1 : 0;
2271                 }
2272                 if( visible && picon->vicon && picon->vicon->hidden )
2273                         picon->vicon->hidden = 0;
2274                 if( visible ) {
2275                         BC_ListBoxItem *item2, *item1;
2276                         dst[0].append(item1 = picon);
2277                         if( picon->edl )
2278                                 dst[1].append(item2 = new BC_ListBoxItem(picon->edl->local_session->clip_notes));
2279                         else
2280                         if( picon->label )
2281                                 dst[1].append(item2 = new BC_ListBoxItem(picon->label->textstr));
2282                         else if( picon->comments_time ) {
2283                                 char date_time[BCSTRLEN];
2284                                 struct tm stm;  localtime_r(&picon->comments_time, &stm);
2285                                 sprintf(date_time,"%04d.%02d.%02d %02d:%02d:%02d @%0.2f %c%s",
2286                                          stm.tm_year+1900, stm.tm_mon+1, stm.tm_mday,
2287                                          stm.tm_hour, stm.tm_min, stm.tm_sec,
2288                                         picon->comments_rate, picon->comments_ffmt,
2289                                         picon->comments_type);
2290                                 dst[1].append(item2 = new BC_ListBoxItem(date_time));
2291                         }
2292                         else
2293                                 dst[1].append(item2 = new BC_ListBoxItem(""));
2294                         item1->set_autoplace_text(1);  item1->set_autoplace_icon(1);
2295                         item2->set_autoplace_text(1);  item2->set_autoplace_icon(1);
2296                 }
2297         }
2298 }
2299
2300 void AWindowGUI::sort_picons(ArrayList<BC_ListBoxItem*> *src)
2301 {
2302         int done = 0, changed = 0;
2303         while( !done ) {
2304                 done = 1;
2305                 for( int i=0; i<src->total-1; ++i ) {
2306                         AssetPicon *item1 = (AssetPicon *)src->values[i];
2307                         AssetPicon *item2 = (AssetPicon *)src->values[i + 1];
2308                         double v = item2->sort_key - item1->sort_key;
2309                         if( v > 0 ) continue;
2310                         if( v == 0 ) {
2311                                 const char *cp1 = item1->get_text();
2312                                 const char *bp1 = strrchr(cp1, '/');
2313                                 if( bp1 ) cp1 = bp1 + 1;
2314                                 const char *cp2 = item2->get_text();
2315                                 const char *bp2 = strrchr(cp2, '/');
2316                                 if( bp2 ) cp2 = bp2 + 1;
2317                                 if( strcmp(cp2, cp1) >= 0 ) continue;
2318                         }
2319                         src->values[i + 1] = item1;
2320                         src->values[i] = item2;
2321                         done = 0;  changed = 1;
2322                 }
2323         }
2324         if( changed ) {
2325                 for( int i=0; i<src->total; ++i ) {
2326                         AssetPicon *item = (AssetPicon *)src->values[i];
2327                         item->set_autoplace_icon(1);
2328                         item->set_autoplace_text(1);
2329                 }
2330         }
2331 }
2332
2333 void AWindowGUI::filter_displayed_assets()
2334 {
2335         //allow_iconlisting = 1;
2336         asset_titles[0] = C_("Title");
2337         asset_titles[1] = _("Comments");
2338         AssetPicon *picon = 0;
2339         int selected_folder = mwindow->edl->session->awindow_folder;
2340         // Ensure the current folder icon is highlighted
2341         for( int i = 0; i < folders.total; i++ ) {
2342                 AssetPicon *folder_item = (AssetPicon *)folders.values[i];
2343                 int selected = folder_item->foldernum == selected_folder ? 1 : 0;
2344                 folder_item->set_selected(selected);
2345                 if( selected ) picon = folder_item;
2346         }
2347
2348         ArrayList<BC_ListBoxItem*> *src = &assets;
2349         switch( selected_folder ) {
2350         case AW_AEFFECT_FOLDER:  src = &aeffects;  break;
2351         case AW_VEFFECT_FOLDER:  src = &veffects;  break;
2352         case AW_ATRANSITION_FOLDER:  src = &atransitions;  break;
2353         case AW_VTRANSITION_FOLDER:  src = &vtransitions;  break;
2354         case AW_LABEL_FOLDER:  src = &labellist;
2355                 asset_titles[0] = _("Time Stamps");
2356                 asset_titles[1] = C_("Title");
2357                 //allow_iconlisting = 0;
2358                 break;
2359         }
2360         copy_picons(picon, src);
2361 }
2362
2363
2364 void AWindowGUI::update_assets()
2365 {
2366         stop_vicon_drawing();
2367         folder_lock->lock("AWindowGUI::update_assets");
2368         update_folder_list();
2369         update_asset_list();
2370         labellist.remove_all_objects();
2371         create_label_folder();
2372
2373         if( displayed_folder != mwindow->edl->session->awindow_folder )
2374                 search_text->clear();
2375         vicon_thread->hide_vicons();
2376         filter_displayed_assets();
2377         folder_lock->unlock();
2378
2379         if( mwindow->edl->session->folderlist_format != folder_list->get_format() ) {
2380                 folder_list->update_format(mwindow->edl->session->folderlist_format, 0);
2381         }
2382         int folder_xposition = folder_list->get_xposition();
2383         int folder_yposition = folder_list->get_yposition();
2384         folder_list->update(&folders, 0, 0, 1, folder_xposition, folder_yposition, -1);
2385
2386         if( mwindow->edl->session->assetlist_format != asset_list->get_format() ) {
2387                 asset_list->update_format(mwindow->edl->session->assetlist_format, 0);
2388         }
2389         int asset_xposition = asset_list->get_xposition();
2390         int asset_yposition = asset_list->get_yposition();
2391         if( displayed_folder != mwindow->edl->session->awindow_folder ) {
2392                 displayed_folder = mwindow->edl->session->awindow_folder;
2393                 asset_xposition = asset_yposition = 0;
2394         }
2395         asset_list->update(displayed_assets, asset_titles,
2396                 mwindow->edl->session->asset_columns, ASSET_COLUMNS,
2397                 asset_xposition, asset_yposition, -1, 0);
2398         asset_list->center_selection();
2399
2400         flush();
2401         start_vicon_drawing();
2402         return;
2403 }
2404
2405 void AWindowGUI::update_effects()
2406 {
2407         aeffects.remove_all_objects();
2408         create_persistent_folder(&aeffects, 1, 0, 1, 0);
2409         veffects.remove_all_objects();
2410         create_persistent_folder(&veffects, 0, 1, 1, 0);
2411         atransitions.remove_all_objects();
2412         create_persistent_folder(&atransitions, 1, 0, 0, 1);
2413         vtransitions.remove_all_objects();
2414         create_persistent_folder(&vtransitions, 0, 1, 0, 1);
2415 }
2416
2417 int AWindowGUI::drag_motion()
2418 {
2419         if( get_hidden() ) return 0;
2420
2421         int result = 0;
2422         return result;
2423 }
2424
2425 int AWindowGUI::drag_stop()
2426 {
2427         if( get_hidden() ) return 0;
2428         return 0;
2429 }
2430
2431 Indexable* AWindowGUI::selected_asset()
2432 {
2433         AssetPicon *picon = (AssetPicon*)asset_list->get_selection(0, 0);
2434         return picon ? picon->indexable : 0;
2435 }
2436
2437 PluginServer* AWindowGUI::selected_plugin()
2438 {
2439         AssetPicon *picon = (AssetPicon*)asset_list->get_selection(0, 0);
2440         return picon ? picon->plugin : 0;
2441 }
2442
2443 AssetPicon* AWindowGUI::selected_folder()
2444 {
2445         AssetPicon *picon = (AssetPicon*)folder_list->get_selection(0, 0);
2446         return picon;
2447 }
2448
2449
2450
2451
2452
2453
2454
2455
2456 AWindowDivider::AWindowDivider(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2457  : BC_SubWindow(x, y, w, h)
2458 {
2459         this->mwindow = mwindow;
2460         this->gui = gui;
2461 }
2462 AWindowDivider::~AWindowDivider()
2463 {
2464 }
2465
2466 int AWindowDivider::button_press_event()
2467 {
2468         if( is_event_win() && cursor_inside() ) {
2469                 mwindow->session->current_operation = DRAG_PARTITION;
2470                 return 1;
2471         }
2472         return 0;
2473 }
2474
2475 int AWindowDivider::cursor_motion_event()
2476 {
2477         if( mwindow->session->current_operation == DRAG_PARTITION ) {
2478                 int wmin = 25;
2479                 int wmax = mwindow->session->awindow_w - mwindow->theme->adivider_w - wmin;
2480                 int fw = gui->get_relative_cursor_x();
2481                 if( fw > wmax ) fw = wmax;
2482                 if( fw < wmin ) fw = wmin;
2483                 mwindow->session->afolders_w = fw;
2484                 mwindow->theme->get_awindow_sizes(gui);
2485                 gui->reposition_objects();
2486                 gui->flush();
2487         }
2488         return 0;
2489 }
2490
2491 int AWindowDivider::button_release_event()
2492 {
2493         if( mwindow->session->current_operation == DRAG_PARTITION ) {
2494                 mwindow->session->current_operation = NO_OPERATION;
2495                 return 1;
2496         }
2497         return 0;
2498 }
2499
2500
2501
2502
2503
2504
2505 AWindowFolders::AWindowFolders(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2506  : BC_ListBox(x, y, w, h,
2507                 mwindow->edl->session->folderlist_format == FOLDERS_ICONS ?
2508                         LISTBOX_ICONS : LISTBOX_TEXT,
2509                 &gui->folders,    // Each column has an ArrayList of BC_ListBoxItems.
2510                 0,                // Titles for columns.  Set to 0 for no titles
2511                 0,                // width of each column
2512                 1,                // Total columns.
2513                 0,                // Pixel of top of window.
2514                 0,                // If this listbox is a popup window
2515                 LISTBOX_SINGLE,   // Select one item or multiple items
2516                 ICON_TOP,         // Position of icon relative to text of each item
2517                 1)                // Allow drags
2518 {
2519         this->mwindow = mwindow;
2520         this->gui = gui;
2521         set_drag_scroll(0);
2522         last_item0 = 0;
2523         last_item1 = 0;
2524 }
2525
2526 AWindowFolders::~AWindowFolders()
2527 {
2528 }
2529
2530 int AWindowFolders::selection_changed()
2531 {
2532         AWindowFolderItem *item0 = (AWindowFolderItem*)get_selection(0, 0);
2533         AWindowFolderItem *item1 = (AWindowFolderItem*)get_selection(0, 1);
2534 // prefer expanded entry
2535         AWindowFolderItem *item = item1 ? item1 : item0;
2536         if( item0 && item1 && last_item0 == item0 && last_item1 == item1 ) {
2537                 item1->set_selected(0);
2538                 item1 = 0;
2539                 item = item0;
2540         }
2541         last_item0 = item0;
2542         last_item1 = item1;
2543         if( item ) {
2544                 AssetPicon *picon = item->get_picon();
2545                 picon->sub_items = (AWindowFolderSubItems*)(!item->parent ? 0 : item);
2546
2547                 gui->stop_vicon_drawing();
2548
2549                 if( get_button_down() && get_buttonpress() == RIGHT_BUTTON ) {
2550                         gui->folderlist_menu->update_titles();
2551                         gui->folderlist_menu->activate_menu();
2552                 }
2553
2554                 mwindow->edl->session->awindow_folder = picon->foldernum;
2555                 gui->asset_list->draw_background();
2556                 gui->async_update_assets();
2557
2558                 gui->start_vicon_drawing();
2559         }
2560         return 1;
2561 }
2562
2563 int AWindowFolders::button_press_event()
2564 {
2565         int result = BC_ListBox::button_press_event();
2566
2567         if( !result ) {
2568                 if( get_buttonpress() == RIGHT_BUTTON &&
2569                     is_event_win() && cursor_inside() ) {
2570                         gui->folderlist_menu->update_titles();
2571                         gui->folderlist_menu->activate_menu();
2572                         result = 1;
2573                 }
2574         }
2575
2576         return result;
2577 }
2578
2579 int AWindowFolders::drag_stop()
2580 {
2581         int result = 0;
2582         if( get_hidden() ) return 0;
2583         if( mwindow->session->current_operation == DRAG_ASSET &&
2584             gui->folder_list->cursor_above() ) { // check user folder
2585                 int item_no = gui->folder_list->get_cursor_data_item_no();
2586                 AssetPicon *picon = (AssetPicon *)(item_no < 0 ? 0 : gui->folders[item_no]);
2587                 if( picon && picon->foldernum >= AWINDOW_USER_FOLDERS ) {
2588                         BinFolder *folder = mwindow->edl->get_folder(picon->foldernum);
2589                         ArrayList<Indexable *> *drags = folder->is_clips ?
2590                                 ((ArrayList<Indexable *> *)mwindow->session->drag_clips) :
2591                                 ((ArrayList<Indexable *> *)mwindow->session->drag_assets);
2592                         if( folder && drags && !folder->add_patterns(drags, shift_down()) )
2593                                 flicker(1,30);
2594                         mwindow->session->current_operation = ::NO_OPERATION;
2595                         result = 1;
2596                 }
2597         }
2598         return result;
2599 }
2600
2601 AWindowFolderSubItems::AWindowFolderSubItems(AWindowFolderItem *parent, const char *text)
2602  : AWindowFolderItem(text)
2603 {
2604         this->parent = parent;
2605 }
2606
2607 int AWindowFolders::load_expanders()
2608 {
2609         char expanders_path[BCTEXTLEN];
2610         mwindow->create_defaults_path(expanders_path, EXPANDERS_FILE);
2611         FILE *fp = fopen(expanders_path, "r");
2612         if( !fp ) {
2613                 snprintf(expanders_path, sizeof(expanders_path), "%s/%s",
2614                         File::get_cindat_path(), EXPANDERS_FILE);
2615                 fp = fopen(expanders_path, "r");
2616         }
2617
2618         if( !fp ) return 1;
2619         const char tab = '\t';
2620         char line[BCTEXTLEN];   line[0] = 0;
2621         AWindowFolderItem *item = 0, *parent;
2622         AWindowFolderSubItems *sub_items = 0;
2623         int k = 0;
2624         while( fgets(line,sizeof(line),fp) ) {
2625                 if( line[0] == '#' ) continue;
2626                 int i = strlen(line);
2627                 if( i > 0 && line[i-1] == '\n' ) line[--i] = 0;
2628                 if( i == 0 ) continue;
2629                 i = 0;
2630                 for( char *cp=line; *cp==tab; ++cp ) ++i;
2631                 if( i == 0 ) {
2632                         int i = gui->folders.size();
2633                         while( --i >= 0 ) {
2634                                 AssetPicon *folder = (AssetPicon *)gui->folders[i];
2635                                 if( !strcmp(folder->get_text(),_(line)) ) break;
2636                         }
2637                         item = (AWindowFolderItem*)(i >= 0 ? gui->folders[i] : 0);
2638                         sub_items = 0;
2639                         k = 0;
2640                         continue;
2641                 }
2642                 if( i > k+1 ) continue;
2643                 if( i == k+1 ) {
2644                         if( line[i] != '-' && sub_items ) {
2645                                 sub_items->names.append(cstrdup(_(&line[i])));
2646                                 continue;
2647                         }
2648                         parent = item;
2649                         k = i;
2650                 }
2651                 else {
2652                         while( i < k ) {
2653                                 item = item->parent;
2654                                 --k;
2655                         }
2656                         parent = item->parent;
2657                 }
2658                 ArrayList<BC_ListBoxItem*> *sublist = parent->get_sublist();
2659                 if( !sublist ) sublist = parent->new_sublist(1);
2660                 sub_items = new AWindowFolderSubItems(parent, &line[i]);
2661                 sublist->append(item = sub_items);
2662         }
2663         fclose(fp);
2664         return 0;
2665 }
2666
2667
2668 AWindowAssets::AWindowAssets(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2669  : BC_ListBox(x, y, w, h, !gui->allow_iconlisting ? LISTBOX_TEXT :
2670                 mwindow->edl->session->assetlist_format == ASSETS_ICONS ? LISTBOX_ICONS :
2671                 mwindow->edl->session->assetlist_format == ASSETS_ICONS_PACKED ? LISTBOX_ICONS_PACKED :
2672                 mwindow->edl->session->assetlist_format == ASSETS_ICON_LIST ? LISTBOX_ICON_LIST :
2673                         LISTBOX_TEXT,
2674                 &gui->assets,     // Each column has an ArrayList of BC_ListBoxItems.
2675                 gui->asset_titles,// Titles for columns.  Set to 0 for no titles
2676                 mwindow->edl->session->asset_columns, // width of each column
2677                 1,                // Total columns.
2678                 0,                // Pixel of top of window.
2679                 0,                // If this listbox is a popup window
2680                 LISTBOX_MULTIPLE, // Select one item or multiple items
2681                 ICON_TOP,         // Position of icon relative to text of each item
2682                 -1)               // Allow drags, require shift for scrolling
2683 {
2684         this->mwindow = mwindow;
2685         this->gui = gui;
2686         set_drag_scroll(0);
2687         set_scroll_stretch(1, 1);
2688 }
2689
2690 AWindowAssets::~AWindowAssets()
2691 {
2692 }
2693
2694 int AWindowAssets::button_press_event()
2695 {
2696         AssetVIconThread *avt = gui->vicon_thread;
2697         if( avt->draw_mode != ASSET_VIEW_NONE && is_event_win() ) {
2698                 int dir = 1, button = get_buttonpress();
2699                 switch( button ) {
2700                 case WHEEL_DOWN: dir = -1;  // fall thru
2701                 case WHEEL_UP: {
2702                         int x = get_cursor_x(), y = get_cursor_y();
2703                         if( avt->cursor_inside(x, y) && avt->view_win )
2704                                 avt->zoom_scale(dir);
2705                         return 1; }
2706                 }
2707         }
2708
2709         int result = BC_ListBox::button_press_event();
2710
2711         if( !result && get_buttonpress() == RIGHT_BUTTON &&
2712             is_event_win() && cursor_inside() ) {
2713                 BC_ListBox::deactivate_selection();
2714                 int folder = mwindow->edl->session->awindow_folder;
2715                 switch( folder ) {
2716                 case AW_AEFFECT_FOLDER:
2717                 case AW_VEFFECT_FOLDER:
2718                 case AW_ATRANSITION_FOLDER:
2719                 case AW_VTRANSITION_FOLDER:
2720                         gui->effectlist_menu->update();
2721                         gui->effectlist_menu->activate_menu();
2722                         break;
2723                 case AW_LABEL_FOLDER:
2724                         gui->labellist_menu->update();
2725                         gui->labellist_menu->activate_menu();
2726                         break;
2727                 case AW_CLIP_FOLDER:
2728                         gui->cliplist_menu->update();
2729                         gui->cliplist_menu->activate_menu();
2730                         break;
2731                 case AW_PROXY_FOLDER:
2732                         gui->proxylist_menu->update();
2733                         gui->proxylist_menu->activate_menu();
2734                         break;
2735                 default:
2736                 case AW_MEDIA_FOLDER: {
2737                         int shots =  folder==AW_MEDIA_FOLDER || folder>=AWINDOW_USER_FOLDERS;
2738                         gui->assetlist_menu->update_titles(shots);
2739                         gui->assetlist_menu->activate_menu();
2740                         break; }
2741                 }
2742                 result = 1;
2743         }
2744
2745         return result;
2746 }
2747
2748
2749 int AWindowAssets::handle_event()
2750 {
2751         AssetPicon *asset_picon = (AssetPicon *)get_selection(0, 0);
2752         if( !asset_picon ) return 0;
2753         Indexable *picon_idxbl = asset_picon->indexable;
2754         EDL *picon_edl = asset_picon->edl;
2755         int proxy = 0;
2756         VWindow *vwindow = 0;
2757         switch( mwindow->edl->session->awindow_folder ) {
2758         case AW_AEFFECT_FOLDER:
2759         case AW_VEFFECT_FOLDER:
2760         case AW_ATRANSITION_FOLDER:
2761         case AW_VTRANSITION_FOLDER: return 1;
2762         case AW_PROXY_FOLDER:
2763                 proxy = 1; // fall thru
2764         default:
2765                 if( mwindow->vwindows.size() > DEFAULT_VWINDOW )
2766                         vwindow = mwindow->vwindows.get(DEFAULT_VWINDOW);
2767                 break;
2768         }
2769         if( !vwindow || !vwindow->is_running() ) return 1;
2770         if( proxy && picon_idxbl ) {
2771                 picon_edl = gui->collect_proxy(picon_idxbl);
2772                 picon_idxbl = 0;
2773         }
2774
2775         if( picon_idxbl ) vwindow->change_source(picon_idxbl);
2776         else if( picon_edl ) vwindow->change_source(picon_edl);
2777         return 1;
2778 }
2779
2780 int AWindowAssets::selection_changed()
2781 {
2782 // Show popup window
2783         AssetPicon *item;
2784         int folder = mwindow->edl->session->awindow_folder;
2785         if( get_button_down() && get_buttonpress() == RIGHT_BUTTON &&
2786             (item = (AssetPicon*)get_selection(0, 0)) ) {
2787                 switch( folder ) {
2788                 case AW_AEFFECT_FOLDER:
2789                 case AW_VEFFECT_FOLDER:
2790                 case AW_ATRANSITION_FOLDER:
2791                 case AW_VTRANSITION_FOLDER:
2792                         gui->effectlist_menu->update();
2793                         gui->effectlist_menu->activate_menu();
2794                         break;
2795                 case AW_LABEL_FOLDER:
2796                         if( !item->label ) break;
2797                         gui->label_menu->activate_menu();
2798                         break;
2799                 case AW_CLIP_FOLDER:
2800                         if( !item->indexable && !item->edl ) break;
2801                         gui->clip_menu->update();
2802                         gui->clip_menu->activate_menu();
2803                         break;
2804                 case AW_PROXY_FOLDER:
2805                         if( !item->indexable && !item->edl ) break;
2806                         gui->proxy_menu->update();
2807                         gui->proxy_menu->activate_menu();
2808                         break;
2809                 default:
2810                         if( !item->indexable && !item->edl ) break;
2811                         gui->asset_menu->update();
2812                         gui->asset_menu->activate_menu();
2813                         break;
2814                 }
2815
2816                 deactivate_selection();
2817         }
2818         else if( get_button_down() && !gui->play_off &&
2819                  mwindow->edl->session->assetlist_format != ASSETS_TEXT ) {
2820                 item = (AssetPicon*)get_selection(0, 0);
2821                 if( item && !get_selection(0, 1) ) {
2822                         switch( folder ) {
2823                         case AW_MEDIA_FOLDER:
2824                         case AW_PROXY_FOLDER:
2825                         case AWINDOW_USER_FOLDERS:
2826                                 if( get_buttonpress() == LEFT_BUTTON ||
2827                                     get_buttonpress() == MIDDLE_BUTTON ) {
2828                                         AssetVIcon *vicon = 0;
2829                                         AssetVIconThread *avt = gui->vicon_thread;
2830                                         if( !avt->vicon && gui->vicon_drawing != AVICON_NO_PLAY )
2831                                                 vicon = item->vicon;
2832                                         int draw_mode = !vicon ?
2833                                                         ASSET_VIEW_NONE :
2834                                                 get_buttonpress() == MIDDLE_BUTTON ?
2835                                                         ASSET_VIEW_MEDIA_MAP : ASSET_VIEW_MEDIA;
2836                                         avt->set_view_popup(vicon, draw_mode);
2837                                 }
2838                                 break;
2839                         case AW_CLIP_FOLDER:
2840                                 if( get_buttonpress() == LEFT_BUTTON ) {
2841                                         AssetVIcon *vicon = 0;
2842                                         AssetVIconThread *avt = gui->vicon_thread;
2843                                         if( !avt->vicon && gui->vicon_drawing != AVICON_NO_PLAY )
2844                                                 vicon = item->vicon;
2845                                         gui->vicon_thread->set_view_popup(vicon, ASSET_VIEW_ICON);
2846                                 }
2847                                 break;
2848                         }
2849                 }
2850                 else {
2851                         gui->vicon_thread->set_view_popup(0);
2852                         if( gui->vicon_drawing != AVICON_FULL_PLAY )
2853                                 gui->stop_vicon_drawing();
2854                 }
2855         }
2856         return 1;
2857 }
2858
2859 void AWindowAssets::draw_background()
2860 {
2861         clear_box(0,0,get_w(),get_h(),get_bg_surface());
2862         set_color(BC_WindowBase::get_resources()->audiovideo_color);
2863         set_font(LARGEFONT);
2864         int folder = mwindow->edl->session->awindow_folder;
2865         const char *title = mwindow->edl->get_folder_name(folder);
2866         draw_text(get_w() - get_text_width(LARGEFONT, title) - 4, 30,
2867                 title, -1, get_bg_surface());
2868 }
2869
2870 int AWindowAssets::drag_start_event()
2871 {
2872         gui->vicon_thread->set_view_popup(0);
2873         int collect_pluginservers = 0;
2874         int collect_assets = 0, proxy = 0;
2875
2876         if( BC_ListBox::drag_start_event() ) {
2877                 switch( mwindow->edl->session->awindow_folder ) {
2878                 case AW_AEFFECT_FOLDER:
2879                         mwindow->session->current_operation = DRAG_AEFFECT;
2880                         collect_pluginservers = 1;
2881                         break;
2882                 case AW_VEFFECT_FOLDER:
2883                         mwindow->session->current_operation = DRAG_VEFFECT;
2884                         collect_pluginservers = 1;
2885                         break;
2886                 case AW_ATRANSITION_FOLDER:
2887                         mwindow->session->current_operation = DRAG_ATRANSITION;
2888                         collect_pluginservers = 1;
2889                         break;
2890                 case AW_VTRANSITION_FOLDER:
2891                         mwindow->session->current_operation = DRAG_VTRANSITION;
2892                         collect_pluginservers = 1;
2893                         break;
2894                 case AW_LABEL_FOLDER:
2895                         // do nothing!
2896                         break;
2897                 case AW_PROXY_FOLDER:
2898                         proxy = 1; // fall thru
2899                 case AW_MEDIA_FOLDER:
2900                 default:
2901                         mwindow->session->current_operation = DRAG_ASSET;
2902                         collect_assets = 1;
2903                         break;
2904                 }
2905
2906                 if( collect_pluginservers ) {
2907                         int i = 0;
2908                         mwindow->session->drag_pluginservers->remove_all();
2909                         while(1)
2910                         {
2911                                 AssetPicon *result = (AssetPicon*)get_selection(0, i++);
2912                                 if( !result ) break;
2913
2914                                 mwindow->session->drag_pluginservers->append(result->plugin);
2915                         }
2916                 }
2917
2918                 if( collect_assets ) {
2919                         gui->collect_assets(proxy);
2920                 }
2921
2922                 return 1;
2923         }
2924         return 0;
2925 }
2926
2927 int AWindowAssets::drag_motion_event()
2928 {
2929         BC_ListBox::drag_motion_event();
2930         unlock_window();
2931
2932         mwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2933         mwindow->gui->drag_motion();
2934         mwindow->gui->unlock_window();
2935
2936         for( int i = 0; i < mwindow->vwindows.size(); i++ ) {
2937                 VWindow *vwindow = mwindow->vwindows.get(i);
2938                 if( !vwindow->is_running() ) continue;
2939                 vwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2940                 vwindow->gui->drag_motion();
2941                 vwindow->gui->unlock_window();
2942         }
2943
2944         mwindow->cwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2945         mwindow->cwindow->gui->drag_motion();
2946         mwindow->cwindow->gui->unlock_window();
2947
2948         lock_window("AWindowAssets::drag_motion_event");
2949         if( mwindow->session->current_operation == DRAG_ASSET &&
2950             gui->folder_list->cursor_above() ) { // highlight user folder
2951                 BC_ListBoxItem *item = 0;
2952                 int item_no = gui->folder_list->get_cursor_data_item_no(&item);
2953                 if( item_no >= 0 ) {
2954                         AssetPicon *folder = (AssetPicon *)gui->folders[item_no];
2955                         if( folder->foldernum < AWINDOW_USER_FOLDERS ) item_no = -1;
2956                 }
2957                 if( item_no >= 0 )
2958                         item_no = gui->folder_list->item_to_index(&gui->folders, item);
2959                 int folder_xposition = gui->folder_list->get_xposition();
2960                 int folder_yposition = gui->folder_list->get_yposition();
2961                 gui->folder_list->update(&gui->folders, 0, 0, 1,
2962                         folder_xposition, folder_yposition, item_no, 0, 1);
2963         }
2964         return 0;
2965 }
2966
2967 int AWindowAssets::drag_stop_event()
2968 {
2969         int result = 0;
2970
2971         result = gui->drag_stop();
2972
2973         unlock_window();
2974
2975         if( !result ) {
2976                 mwindow->gui->lock_window("AWindowAssets::drag_stop_event");
2977                 result = mwindow->gui->drag_stop();
2978                 mwindow->gui->unlock_window();
2979         }
2980
2981         if( !result ) {
2982                 for( int i = 0; !result && i < mwindow->vwindows.size(); i++ ) {
2983                         VWindow *vwindow = mwindow->vwindows.get(i);
2984                         if( !vwindow ) continue;
2985                         if( !vwindow->is_running() ) continue;
2986                         if( vwindow->gui->is_hidden() ) continue;
2987                         vwindow->gui->lock_window("AWindowAssets::drag_stop_event");
2988                         if( vwindow->gui->cursor_above() &&
2989                             vwindow->gui->get_cursor_over_window() ) {
2990                                 result = vwindow->gui->drag_stop();
2991                         }
2992                         vwindow->gui->unlock_window();
2993                 }
2994         }
2995
2996         if( !result ) {
2997                 mwindow->cwindow->gui->lock_window("AWindowAssets::drag_stop_event");
2998                 result = mwindow->cwindow->gui->drag_stop();
2999                 mwindow->cwindow->gui->unlock_window();
3000         }
3001
3002         lock_window("AWindowAssets::drag_stop_event");
3003         if( !result ) {
3004                 result = gui->folder_list->drag_stop();
3005         }
3006
3007
3008         if( result )
3009                 get_drag_popup()->set_animation(0);
3010
3011         BC_ListBox::drag_stop_event();
3012 // since NO_OPERATION is also defined in listbox, we have to reach for global scope...
3013         mwindow->session->current_operation = ::NO_OPERATION;
3014
3015         return 1;
3016 }
3017
3018 int AWindowAssets::column_resize_event()
3019 {
3020         mwindow->edl->session->asset_columns[0] = get_column_width(0);
3021         mwindow->edl->session->asset_columns[1] = get_column_width(1);
3022         return 1;
3023 }
3024
3025 int AWindowAssets::cursor_enter_event()
3026 {
3027         int ret = BC_ListBox::cursor_enter_event();
3028         switch( gui->vicon_drawing ) {
3029         case AVICON_FULL_PLAY:
3030                 gui->start_vicon_drawing();
3031                 break;
3032         case AVICON_MOUSE_OVER:
3033         case AVICON_SRC_TARGET:
3034         case AVICON_NO_PLAY:
3035         default:
3036                 break;
3037         }
3038         return ret;
3039 }
3040
3041 int AWindowAssets::cursor_leave_event()
3042 {
3043         if( !is_event_win() ) return 0;
3044         if( !gui->vicon_thread->viewing )
3045                 gui->stop_vicon_drawing();
3046         return BC_ListBox::cursor_leave_event();
3047 }
3048
3049 int AWindowAssets::focus_out_event()
3050 {
3051         gui->stop_vicon_drawing();
3052         gui->vicon_thread->set_view_popup(0);
3053         return BC_ListBox::focus_out_event();
3054 }
3055
3056 void AWindowAssets::update_vicon_area()
3057 {
3058         int x0 = 0, x1 = get_w();
3059         int y0 = get_title_h();
3060         int y1 = get_h();
3061         if( is_highlighted() ) {
3062                 x0 += LISTBOX_BORDER;  x1 -= LISTBOX_BORDER;
3063                 y0 += LISTBOX_BORDER;  y1 -= LISTBOX_BORDER;
3064         }
3065         gui->vicon_thread->set_drawing_area(x0,y0, x1,y1);
3066 }
3067
3068 int AWindowAssets::mouse_over_event(int no)
3069 {
3070         AssetPicon *picon = no >= 0 && no < gui->displayed_assets[0].size() ?
3071                 (AssetPicon *)gui->displayed_assets[0][no] : 0;
3072         AssetVIcon *vicon = !picon ? 0 : picon->vicon;
3073         switch( gui->vicon_drawing ) {
3074         case AVICON_FULL_PLAY:
3075                 gui->vicon_thread->solo = 0;
3076                 if( vicon && gui->vicon_thread->viewing )
3077                         gui->vicon_thread->set_view_popup(vicon);
3078                 break;
3079         case AVICON_MOUSE_OVER:
3080                 if( !vicon ) break;
3081                 gui->vicon_thread->solo = vicon;
3082                 gui->start_vicon_drawing();
3083         // fall thru
3084         case AVICON_SRC_TARGET:
3085                 if( !vicon ) break;
3086                 if( gui->vicon_thread->viewing )
3087                         gui->vicon_thread->set_view_popup(vicon);
3088                 break;
3089         case AVICON_NO_PLAY:
3090         default:
3091                 break;
3092         }
3093         return 0;
3094 }
3095
3096
3097 AWindowSearchTextBox::AWindowSearchTextBox(AWindowSearchText *search_text, int x, int y, int w)
3098  : BC_TextBox(x, y, w, 1, "")
3099 {
3100         this->search_text = search_text;
3101 }
3102
3103 int AWindowSearchTextBox::handle_event()
3104 {
3105         return search_text->handle_event();
3106 }
3107
3108 AWindowSearchText::AWindowSearchText(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3109 {
3110         this->mwindow = mwindow;
3111         this->gui = gui;
3112         this->x = x;
3113         this->y = y;
3114 }
3115
3116 void AWindowSearchText::create_objects()
3117 {
3118         int x1 = x, y1 = y, margin = 10;
3119         gui->add_subwindow(text_title = new BC_Title(x1, y1, _("Search:")));
3120         x1 += text_title->get_w() + margin;
3121         int w1 = gui->get_w() - x1 - 2*margin;
3122         gui->add_subwindow(text_box = new AWindowSearchTextBox(this, x1, y1, w1));
3123 }
3124
3125 int AWindowSearchText::handle_event()
3126 {
3127         gui->async_update_assets();
3128         return 1;
3129 }
3130
3131 int AWindowSearchText::get_w()
3132 {
3133         return text_box->get_w() + text_title->get_w() + 10;
3134 }
3135
3136 int AWindowSearchText::get_h()
3137 {
3138         return bmax(text_box->get_h(),text_title->get_h());
3139 }
3140
3141 void AWindowSearchText::reposition_window(int x, int y, int w)
3142 {
3143         int x1 = x, y1 = y, margin = 10;
3144         text_title->reposition_window(x1, y1);
3145         x1 += text_title->get_w() + margin;
3146         int w1 = gui->get_w() - x1 - 2*margin;
3147         text_box->reposition_window(x1, y1, w1);
3148 }
3149
3150 const char *AWindowSearchText::get_text()
3151 {
3152         return text_box->get_text();
3153 }
3154
3155 void AWindowSearchText::clear()
3156 {
3157         text_box->update("");
3158 }
3159
3160 AWindowDeleteDisk::AWindowDeleteDisk(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3161  : BC_Button(x, y, mwindow->theme->deletedisk_data)
3162 {
3163         this->mwindow = mwindow;
3164         this->gui = gui;
3165         set_tooltip(_("Delete asset from disk"));
3166 }
3167
3168 int AWindowDeleteDisk::handle_event()
3169 {
3170         return 1;
3171 }
3172
3173 AWindowDeleteProject::AWindowDeleteProject(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3174  : BC_Button(x, y, mwindow->theme->deleteproject_data)
3175 {
3176         this->mwindow = mwindow;
3177         this->gui = gui;
3178         set_tooltip(_("Delete asset from project"));
3179 }
3180
3181 int AWindowDeleteProject::handle_event()
3182 {
3183         return 1;
3184 }
3185
3186 // AWindowInfo::AWindowInfo(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3187 //  : BC_Button(x, y, mwindow->theme->infoasset_data)
3188 // {
3189 //      this->mwindow = mwindow;
3190 //      this->gui = gui;
3191 //      set_tooltip(_("Edit information on asset"));
3192 // }
3193 // 
3194 // int AWindowInfo::handle_event()
3195 // {
3196 //      int cur_x, cur_y;
3197 //      gui->get_abs_cursor(cur_x, cur_y, 0);
3198 //      gui->awindow->asset_edit->edit_asset(gui->selected_asset(), cur_x, cur_y);
3199 //      return 1;
3200 // }
3201
3202 AWindowRedrawIndex::AWindowRedrawIndex(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3203  : BC_Button(x, y, mwindow->theme->redrawindex_data)
3204 {
3205         this->mwindow = mwindow;
3206         this->gui = gui;
3207         set_tooltip(_("Redraw index"));
3208 }
3209
3210 int AWindowRedrawIndex::handle_event()
3211 {
3212         return 1;
3213 }
3214
3215 AWindowPaste::AWindowPaste(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3216  : BC_Button(x, y, mwindow->theme->pasteasset_data)
3217 {
3218         this->mwindow = mwindow;
3219         this->gui = gui;
3220         set_tooltip(_("Paste asset on recordable tracks"));
3221 }
3222
3223 int AWindowPaste::handle_event()
3224 {
3225         return 1;
3226 }
3227
3228 AWindowAppend::AWindowAppend(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3229  : BC_Button(x, y, mwindow->theme->appendasset_data)
3230 {
3231         this->mwindow = mwindow;
3232         this->gui = gui;
3233         set_tooltip(_("Append asset in new tracks"));
3234 }
3235
3236 int AWindowAppend::handle_event()
3237 {
3238         return 1;
3239 }
3240
3241 AWindowView::AWindowView(MWindow *mwindow, AWindowGUI *gui, int x, int y)
3242  : BC_Button(x, y, mwindow->theme->viewasset_data)
3243 {
3244         this->mwindow = mwindow;
3245         this->gui = gui;
3246         set_tooltip(_("View asset"));
3247 }
3248
3249 int AWindowView::handle_event()
3250 {
3251         return 1;
3252 }
3253
3254 AddTools::AddTools(MWindow *mwindow, AWindowGUI *gui, int x, int y, const char *title)
3255  : BC_PopupMenu(x, y, title, -1, 0)
3256 {
3257         this->mwindow = mwindow;
3258         this->gui = gui;
3259 }
3260
3261 void AddTools::create_objects()
3262 {
3263         uint64_t vis = 0;
3264         add_item(new AddPluginItem(this, "ffmpeg", PLUGIN_FFMPEG_ID));
3265         vis |= 1 << PLUGIN_FFMPEG_ID;
3266         add_item(new AddPluginItem(this, "ladspa", PLUGIN_LADSPA_ID));
3267         vis |= 1 << PLUGIN_LADSPA_ID;
3268 #ifdef HAVE_LV2
3269         add_item(new AddPluginItem(this, "lv2", PLUGIN_LV2_ID));
3270         vis |= 1 << PLUGIN_LV2_ID;
3271 #endif
3272         for( int i=0; i<MWindow::plugindb->size(); ++i ) {
3273                 PluginServer *plugin = MWindow::plugindb->get(i);
3274                 if( !plugin->audio && !plugin->video ) continue;
3275                 int idx = plugin->dir_idx;
3276                 uint32_t msk = 1 << idx;
3277                 if( (msk & vis) != 0 ) continue;
3278                 vis |= msk;
3279                 char parent[BCTEXTLEN];
3280                 strcpy(parent, plugin->path);
3281                 char *bp = strrchr(parent, '/');
3282                 if( bp ) { *bp = 0;  bp = strrchr(parent, '/'); }
3283                 if( !bp ) bp = parent; else ++bp;
3284                 add_item(new AddPluginItem(this, bp, idx));
3285         }
3286 }
3287
3288 #if 0
3289 // plugin_dirs list from toplevel makefile include plugin_defs
3290 N_("ffmpeg")
3291 N_("ladspa")
3292 N_("lv2")
3293 N_("audio_tools")
3294 N_("audio_transitions")
3295 N_("blending")
3296 N_("colors")
3297 N_("exotic")
3298 N_("transforms")
3299 N_("tv_effects")
3300 N_("video_tools")
3301 N_("video_transitions")
3302 #endif
3303
3304 AddPluginItem::AddPluginItem(AddTools *menu, char const *text, int idx)
3305  : BC_MenuItem(_(text))
3306 {
3307         this->menu = menu;
3308         this->idx = idx;
3309         uint64_t msk = (uint64_t)1 << idx, vis = menu->gui->plugin_visibility;
3310         int chk = (msk & vis) ? 1 : 0;
3311         set_checked(chk);
3312 }
3313
3314 int AddPluginItem::handle_event()
3315 {
3316         int chk = get_checked() ^ 1;
3317         set_checked(chk);
3318         uint64_t msk = (uint64_t)1 << idx, vis = menu->gui->plugin_visibility;
3319         menu->gui->plugin_visibility = chk ? vis | msk : vis & ~msk;
3320         menu->gui->update_effects();
3321         menu->gui->save_defaults(menu->mwindow->defaults);
3322         menu->gui->async_update_assets();
3323         return 1;
3324 }
3325
3326 AVIconDrawingItem::AVIconDrawingItem(AVIconDrawing *avicon, const char *text, int id)
3327  : BC_MenuItem(text)
3328 {
3329         this->avicon = avicon;
3330         this->id = id;
3331 }
3332
3333 int AVIconDrawingItem::handle_event()
3334 {
3335         for( int i=0; i<AVICON_PLAY_MODES; ++i ) {
3336                 AVIconDrawingItem *item = (AVIconDrawingItem *) avicon->get_item(i);
3337                 item->set_checked(id == i);
3338         }
3339         AWindowGUI *agui = avicon->agui;
3340         agui->play_off = 0;
3341         avicon->set_text(get_text());
3342         agui->stop_vicon_drawing();
3343         agui->vicon_thread->set_view_popup(0);
3344         agui->vicon_thread->solo = 0;
3345         agui->vicon_drawing = id;
3346         agui->start_vicon_drawing();
3347         return 1;
3348 }
3349
3350 int AVIconDrawing::draw_face(int dx, int color)
3351 {
3352         int ret = BC_PopupMenu::draw_face(dx, color);
3353         if( agui->play_off && agui->vicon_drawing != AVICON_NO_PLAY ) {
3354                 int lx = get_margin(), ly = get_h()/2;
3355                 draw_line(lx,ly, get_w()-2*lx,ly);
3356         }
3357         return ret;
3358 }
3359
3360 AVIconDrawing::AVIconDrawing(AWindowGUI *agui, int x, int y, int w, const char *text)
3361  : BC_PopupMenu(x-w, y, w, text, 1, 0, 4)
3362 {
3363         this->agui = agui;
3364 }
3365
3366 AVIconDrawing::~AVIconDrawing()
3367 {
3368 }
3369
3370 void AVIconDrawing::create_objects()
3371 {
3372         AVIconDrawingItem *item;
3373         for( int i=0; i<AVICON_PLAY_MODES; ++i ) {
3374                 add_item(item = new AVIconDrawingItem(this, avicon_names[i], i));
3375                 item->set_checked(agui->vicon_drawing == i);
3376         }
3377 }
3378
3379 int AVIconDrawing::button_press_event()
3380 {
3381         if( !is_event_win() ) return 0;
3382         if( get_buttonpress() == MIDDLE_BUTTON ) {
3383                 agui->play_off = !agui->play_off ? 1 : 0;
3384                 draw_face(0, -1);
3385                 flash(1);
3386                 agui->start_vicon_drawing();
3387                 return 1;
3388         }
3389         return BC_PopupMenu::button_press_event();
3390 }
3391
3392 AWindowListFormat::AWindowListFormat(MWindow *mwindow, AWindowGUI *gui)
3393  : BC_MenuItem("","v",'v')
3394 {
3395         this->mwindow = mwindow;
3396         this->gui = gui;
3397 }
3398
3399 int AWindowListFormat::handle_event()
3400 {
3401         return gui->cycle_assetlist_format();
3402 }
3403
3404 void AWindowListFormat::update()
3405 {
3406         EDLSession *session = mwindow->edl->session;
3407         const char *text = 0;
3408         switch( session->assetlist_format ) {
3409         case ASSETS_TEXT:
3410                 text = _("Display icons");
3411                 break;
3412         case ASSETS_ICONS:
3413                 text = _("Display icons packed");
3414                 break;
3415         case ASSETS_ICONS_PACKED:
3416                 text = _("Display icon list");
3417                 break;
3418         case ASSETS_ICON_LIST:
3419                 text = _("Display text");
3420                 break;
3421         }
3422         set_text(text);
3423 }
3424
3425 AWindowListSort::AWindowListSort(MWindow *mwindow, AWindowGUI *gui)
3426  : BC_MenuItem(_("Sort items"))
3427 {
3428         this->mwindow = mwindow;
3429         this->gui = gui;
3430 }
3431
3432 int AWindowListSort::handle_event()
3433 {
3434         gui->sort_assets();
3435         return 1;
3436 }
3437
3438 AssetSelectUsedItem::AssetSelectUsedItem(AssetSelectUsed *select_used, const char *text, int action)
3439  : BC_MenuItem(text)
3440 {
3441         this->select_used = select_used;
3442         this->action = action;
3443 }
3444
3445 int AssetSelectUsedItem::handle_event()
3446 {
3447         MWindow *mwindow = select_used->mwindow;
3448         AWindowGUI *gui = select_used->gui;
3449         AWindowAssets *asset_list = gui->asset_list;
3450         ArrayList<BC_ListBoxItem*> *data = gui->displayed_assets;
3451
3452         switch( action ) {
3453         case SELECT_ALL:
3454         case SELECT_NONE:
3455                 asset_list->set_all_selected(data, action==SELECT_ALL ? 1 : 0);
3456                 break;
3457         case SELECT_USED:
3458         case SELECT_UNUSED:
3459                 asset_list->set_all_selected(data, 0);
3460                 for( int i = 0; i < data->total; i++ ) {
3461                         AssetPicon *picon = (AssetPicon*)data->values[i];
3462                         Indexable *idxbl = picon->indexable ? picon->indexable :
3463                             picon->edl ? picon->edl->get_proxy_asset() : 0;
3464                         int used = idxbl && mwindow->edl->in_use(idxbl) ? 1 : 0;
3465                         asset_list->set_selected(data, i, action==SELECT_USED ? used : !used);
3466                 }
3467                 break;
3468         }
3469
3470         int asset_xposition = asset_list->get_xposition();
3471         int asset_yposition = asset_list->get_yposition();
3472         asset_list->update(gui->displayed_assets, gui->asset_titles,
3473                 mwindow->edl->session->asset_columns, ASSET_COLUMNS,
3474                 asset_xposition, asset_yposition, -1, 0);
3475         asset_list->center_selection();
3476         return 1;
3477 }
3478
3479 AssetSelectUsed::AssetSelectUsed(MWindow *mwindow, AWindowGUI *gui)
3480  : BC_MenuItem(_("Select"))
3481 {
3482         this->mwindow = mwindow;
3483         this->gui = gui;
3484 }
3485