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