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