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