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