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