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