add select all/none, layout shortcuts from all guis, docs tweaks + cv->gg
[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         case KEY_F1:
1492         case KEY_F2:
1493         case KEY_F3:
1494         case KEY_F4:
1495                 if( shift_down() && ctrl_down() ) {
1496                         resend_event(mwindow->gui);
1497                         return 1;
1498                 }
1499                 break;
1500         }
1501         return 0;
1502 }
1503
1504
1505
1506 int AWindowGUI::create_custom_xatoms()
1507 {
1508         UpdateAssetsXAtom = create_xatom("CWINDOWGUI_UPDATE_ASSETS");
1509         return 0;
1510 }
1511 int AWindowGUI::receive_custom_xatoms(xatom_event *event)
1512 {
1513         if( event->message_type == UpdateAssetsXAtom ) {
1514                 update_assets();
1515                 return 1;
1516         }
1517         return 0;
1518 }
1519
1520 void AWindowGUI::async_update_assets()
1521 {
1522         xatom_event event;
1523         event.message_type = UpdateAssetsXAtom;
1524         send_custom_xatom(&event);
1525 }
1526
1527
1528 void AWindowGUI::update_folder_list()
1529 {
1530         for( int i = 0; i < folders.total; i++ ) {
1531                 AssetPicon *picon = (AssetPicon*)folders.values[i];
1532                 picon->in_use = 0;
1533         }
1534
1535 // Search assets for folders
1536         for( int i = 0; i < mwindow->edl->folders.total; i++ ) {
1537                 BinFolder *bin_folder = mwindow->edl->folders[i];
1538                 int exists = 0;
1539
1540                 for( int j = 0; j < folders.total; j++ ) {
1541                         AssetPicon *picon = (AssetPicon*)folders[j];
1542                         if( !strcasecmp(picon->get_text(), bin_folder->title) ) {
1543                                 exists = 1;
1544                                 picon->in_use = 1;
1545                                 break;
1546                         }
1547                 }
1548
1549                 if( !exists ) {
1550                         const char *title = bin_folder->title;
1551                         int folder = bin_folder->awindow_folder;
1552                         AssetPicon *picon = new AssetPicon(mwindow, this, folder, title);
1553                         picon->create_objects();
1554                         folders.append(picon);
1555                 }
1556         }
1557
1558 // Delete unused non-persistent folders
1559         int do_autoplace = 0;
1560         for( int i=folders.total; --i>=0; ) {
1561                 AssetPicon *picon = (AssetPicon*)folders.values[i];
1562                 if( !picon->in_use && !picon->persistent ) {
1563                         delete picon;
1564                         folders.remove_number(i);
1565                         do_autoplace = 1;
1566                 }
1567         }
1568         if( do_autoplace )
1569                 folder_list->set_autoplacement(&folders, 0, 1);
1570 }
1571
1572 void AWindowGUI::create_persistent_folder(ArrayList<BC_ListBoxItem*> *output,
1573         int do_audio, int do_video, int is_realtime, int is_transition)
1574 {
1575         ArrayList<PluginServer*> plugin_list;
1576 // Get pointers to plugindb entries
1577         mwindow->search_plugindb(do_audio, do_video, is_realtime, is_transition,
1578                         0, plugin_list);
1579
1580         for( int i = 0; i < plugin_list.total; i++ ) {
1581                 PluginServer *server = plugin_list.values[i];
1582                 int visible = plugin_visibility & (1<<server->dir_idx);
1583                 if( !visible ) continue;
1584 // Create new listitem
1585                 AssetPicon *picon = new AssetPicon(mwindow, this, server);
1586                 picon->create_objects();
1587                 output->append(picon);
1588         }
1589 }
1590
1591 void AWindowGUI::create_label_folder()
1592 {
1593         Label *current;
1594         for( current = mwindow->edl->labels->first; current; current = NEXT ) {
1595                 AssetPicon *picon = new AssetPicon(mwindow, this, current);
1596                 picon->create_objects();
1597                 labellist.append(picon);
1598         }
1599 }
1600
1601
1602 void AWindowGUI::update_asset_list()
1603 {
1604         ArrayList<AssetPicon *> new_assets;
1605         for( int i = 0; i < assets.total; i++ ) {
1606                 AssetPicon *picon = (AssetPicon*)assets.values[i];
1607                 picon->in_use = 0;
1608         }
1609
1610         mwindow->gui->lock_window("AWindowGUI::update_asset_list");
1611 // Synchronize EDL clips
1612         for( int i=0; i<mwindow->edl->clips.size(); ++i ) {
1613                 int exists = 0;
1614
1615 // Look for clip in existing listitems
1616                 for( int j = 0; j < assets.total && !exists; j++ ) {
1617                         AssetPicon *picon = (AssetPicon*)assets.values[j];
1618
1619                         if( picon->id == mwindow->edl->clips[i]->id ) {
1620                                 picon->edl = mwindow->edl->clips[i];
1621                                 picon->set_text(mwindow->edl->clips[i]->local_session->clip_title);
1622                                 exists = 1;
1623                                 picon->in_use = 1;
1624                         }
1625                 }
1626
1627 // Create new listitem
1628                 if( !exists ) {
1629                         AssetPicon *picon = new AssetPicon(mwindow,
1630                                 this, mwindow->edl->clips[i]);
1631                         new_assets.append(picon);
1632                 }
1633         }
1634
1635 // Synchronize EDL assets
1636         for( Asset *current=mwindow->edl->assets->first; current; current=NEXT ) {
1637                 int exists = 0;
1638
1639 // Look for asset in existing listitems
1640                 for( int j = 0; j < assets.total && !exists; j++ ) {
1641                         AssetPicon *picon = (AssetPicon*)assets.values[j];
1642
1643                         if( picon->id == current->id ) {
1644                                 picon->indexable = current;
1645                                 picon->in_use = 1;
1646                                 exists = 1;
1647                         }
1648                 }
1649
1650 // Create new listitem
1651                 if( !exists ) {
1652                         AssetPicon *picon = new AssetPicon(mwindow,
1653                                 this, current);
1654                         new_assets.append(picon);
1655                 }
1656         }
1657
1658 // Synchronize nested EDLs
1659         for( int i=0; i<mwindow->edl->nested_edls.size(); ++i ) {
1660                 int exists = 0;
1661                 EDL *nested_edl = mwindow->edl->nested_edls[i];
1662
1663 // Look for asset in existing listitems
1664                 for( int j=0; j<assets.total && !exists; ++j ) {
1665                         AssetPicon *picon = (AssetPicon*)assets.values[j];
1666
1667                         if( picon->id == nested_edl->id ) {
1668                                 picon->indexable = nested_edl;
1669                                 picon->in_use = 1;
1670                                 exists = 1;
1671                         }
1672                 }
1673
1674 // Create new listitem
1675                 if( !exists ) {
1676                         AssetPicon *picon = new AssetPicon(mwindow,
1677                                 this, (Indexable*)nested_edl);
1678                         new_assets.append(picon);
1679                 }
1680         }
1681         mwindow->gui->unlock_window();
1682
1683         for( int i=0; i<new_assets.size(); ++i ) {
1684                 AssetPicon *picon = new_assets[i];
1685                 picon->create_objects();
1686                 if( picon->indexable )
1687                         picon->foldernum = AW_MEDIA_FOLDER;
1688                 else if( picon->edl )
1689                         picon->foldernum = AW_CLIP_FOLDER;
1690                 assets.append(picon);
1691         }
1692
1693         mwindow->gui->lock_window();
1694         mwindow->gui->default_message();
1695         mwindow->gui->unlock_window();
1696
1697         for( int i = assets.size() - 1; i >= 0; i-- ) {
1698                 AssetPicon *picon = (AssetPicon*)assets.get(i);
1699                 if( !picon->in_use ) {
1700                         delete picon;
1701                         assets.remove_number(i);
1702                         continue;
1703                 }
1704                 if( picon->indexable && picon->indexable->is_asset ) {
1705                         struct stat st;
1706                         picon->comments_time = !stat(picon->indexable->path, &st) ?
1707                                 st.st_mtime : 0;
1708                 }
1709         }
1710 }
1711
1712 void AWindowGUI::update_picon(Indexable *indexable)
1713 {
1714         VIcon *vicon = 0;
1715         for( int i = 0; i < assets.total; i++ ) {
1716                 AssetPicon *picon = (AssetPicon*)assets.values[i];
1717                 if( picon->indexable == indexable ||
1718                     picon->edl == (EDL *)indexable ) {
1719                         char name[BCTEXTLEN];
1720                         FileSystem fs;
1721                         fs.extract_name(name, indexable->path);
1722                         picon->set_text(name);
1723                         vicon = picon->vicon;
1724                         break;
1725                 }
1726         }
1727         if( vicon ) {
1728                 stop_vicon_drawing();
1729                 vicon->clear_images();
1730                 vicon->reset(indexable->get_frame_rate());
1731                 start_vicon_drawing();
1732         }
1733 }
1734
1735 void AWindowGUI::sort_assets()
1736 {
1737         folder_lock->lock("AWindowGUI::sort_assets");
1738         switch( mwindow->edl->session->awindow_folder ) {
1739         case AW_AEFFECT_FOLDER:
1740                 sort_picons(&aeffects);
1741                 break;
1742         case AW_VEFFECT_FOLDER:
1743                 sort_picons(&veffects);
1744                 break;
1745         case AW_ATRANSITION_FOLDER:
1746                 sort_picons(&atransitions);
1747                 break;
1748         case AW_VTRANSITION_FOLDER:
1749                 sort_picons(&vtransitions);
1750                 break;
1751         case AW_LABEL_FOLDER:
1752                 sort_picons(&labellist);
1753                 break;
1754         default:
1755                 sort_picons(&assets);
1756                 break;
1757         }
1758 // reset xyposition
1759         asset_list->update_format(asset_list->get_format(), 0);
1760         folder_lock->unlock();
1761         update_assets();
1762 }
1763
1764 void AWindowGUI::sort_folders()
1765 {
1766         folder_lock->lock("AWindowGUI::update_assets");
1767 //      folder_list->collapse_recursive(&folders, 0);
1768         folder_list->set_autoplacement(&folders, 0, 1);
1769         sort_picons(&folders);
1770         folder_list->update_format(folder_list->get_format(), 0);
1771         folder_lock->unlock();
1772         update_assets();
1773 }
1774
1775 EDL *AWindowGUI::collect_proxy(Indexable *indexable)
1776 {
1777         Asset *proxy_asset = (Asset *)indexable;
1778         char path[BCTEXTLEN];
1779         int proxy_scale = mwindow->edl->session->proxy_scale;
1780         ProxyRender::from_proxy_path(path, proxy_asset, proxy_scale);
1781         Asset *unproxy_asset = mwindow->edl->assets->get_asset(path);
1782         if( !unproxy_asset || !unproxy_asset->layers ) return 0;
1783 // make a clip from proxy video tracks and unproxy audio tracks
1784         EDL *proxy_edl = new EDL(mwindow->edl);
1785         proxy_edl->create_objects();
1786         proxy_edl->set_path(proxy_asset->path);
1787         FileSystem fs;  fs.extract_name(path, proxy_asset->path);
1788         strcpy(proxy_edl->local_session->clip_title, path);
1789         strcpy(proxy_edl->local_session->clip_notes, _("Proxy clip"));
1790         proxy_edl->session->video_tracks = proxy_asset->layers;
1791         proxy_edl->session->audio_tracks = unproxy_asset->channels;
1792         proxy_edl->create_default_tracks();
1793         double length = proxy_asset->frame_rate > 0 ?
1794                 ( proxy_asset->video_length >= 0 ?
1795                         ( proxy_asset->video_length / proxy_asset->frame_rate ) :
1796                         ( proxy_edl->session->si_useduration ?
1797                                 proxy_edl->session->si_duration :
1798                                 1.0 / proxy_asset->frame_rate ) ) :
1799                 1.0 / proxy_edl->session->frame_rate;
1800         Track *current = proxy_edl->tracks->first;
1801         for( int vtrack=0; current; current=NEXT ) {
1802                 if( current->data_type != TRACK_VIDEO ) continue;
1803                 current->insert_asset(proxy_asset, 0, length, 0, vtrack++);
1804         }
1805         length = (double)unproxy_asset->audio_length / unproxy_asset->sample_rate;
1806         current = proxy_edl->tracks->first;
1807         for( int atrack=0; current; current=NEXT ) {
1808                 if( current->data_type != TRACK_AUDIO ) continue;
1809                 current->insert_asset(unproxy_asset, 0, length, 0, atrack++);
1810         }
1811         proxy_edl->folder_no = AW_PROXY_FOLDER;
1812         return proxy_edl;
1813 }
1814
1815
1816 void AWindowGUI::collect_assets(int proxy)
1817 {
1818         mwindow->session->drag_assets->remove_all();
1819         mwindow->session->drag_clips->remove_all();
1820         int i = 0;  AssetPicon *result;
1821         while( (result = (AssetPicon*)asset_list->get_selection(0, i++)) != 0 ) {
1822                 Indexable *indexable = result->indexable;
1823                 if( proxy && indexable && indexable->is_asset &&
1824                     indexable->folder_no == AW_PROXY_FOLDER ) {
1825                         EDL *drag_edl = collect_proxy(indexable);
1826                         if( drag_edl ) mwindow->session->drag_clips->append(drag_edl);
1827                         continue;
1828                 }
1829                 if( indexable ) {
1830                         mwindow->session->drag_assets->append(indexable);
1831                         continue;
1832                 }
1833                 if( result->edl ) {
1834                         mwindow->session->drag_clips->append(result->edl);
1835                         continue;
1836                 }
1837         }
1838 }
1839
1840 void AWindowGUI::copy_picons(AssetPicon *picon, ArrayList<BC_ListBoxItem*> *src)
1841 {
1842 // Remove current pointers
1843         ArrayList<BC_ListBoxItem*> *dst = displayed_assets;
1844         dst[0].remove_all();
1845         dst[1].remove_all_objects();
1846
1847         AWindowFolderSubItems *sub_items = picon ? picon->sub_items : 0;
1848         int folder = mwindow->edl->session->awindow_folder;
1849         BinFolder *bin_folder = folder < AWINDOW_USER_FOLDERS ? 0 :
1850                 mwindow->edl->get_folder(folder);
1851
1852 // Create new pointers
1853         for( int i = 0; i < src->total; i++ ) {
1854                 int visible = folder >= AW_CLIP_FOLDER ? 0 : 1;
1855                 picon = (AssetPicon*)src->values[i];
1856                 picon->sort_key = -1;
1857                 if( !visible && bin_folder ) {
1858                         Indexable *idxbl = bin_folder->is_clips ? (Indexable *)picon->edl :
1859                             picon->indexable ? picon->indexable :
1860                             picon->edl ? picon->edl->get_proxy_asset() : 0;
1861                         if( idxbl ) {
1862                                 picon->sort_key = mwindow->edl->folders.matches_indexable(folder, idxbl);
1863                                 if( picon->sort_key < 0 ) continue;
1864                                 visible = 1;
1865                         }
1866                 }
1867                 if( !visible && picon->indexable && picon->indexable->folder_no == folder )
1868                         visible = 1;
1869                 if( !visible && picon->edl && picon->edl->folder_no == folder )
1870                         visible = 1;
1871                 if( visible && sub_items ) {
1872                         if( !sub_items->matches(picon->get_text()) )
1873                                 visible = 0;
1874                 }
1875                 if( visible ) {
1876                         const char *text = search_text->get_text();
1877                         if( text && text[0] )
1878                                 visible = bstrcasestr(picon->get_text(), text) ? 1 : 0;
1879                 }
1880                 if( visible && picon->vicon && picon->vicon->hidden )
1881                         picon->vicon->hidden = 0;
1882                 if( visible ) {
1883                         BC_ListBoxItem *item2, *item1;
1884                         dst[0].append(item1 = picon);
1885                         if( picon->edl )
1886                                 dst[1].append(item2 = new BC_ListBoxItem(picon->edl->local_session->clip_notes));
1887                         else
1888                         if( picon->label )
1889                                 dst[1].append(item2 = new BC_ListBoxItem(picon->label->textstr));
1890                         else if( picon->comments_time ) {
1891                                 char date_time[BCSTRLEN];
1892                                 struct tm stm;  localtime_r(&picon->comments_time, &stm);
1893                                 sprintf(date_time,"%04d.%02d.%02d %02d:%02d:%02d",
1894                                          stm.tm_year+1900, stm.tm_mon+1, stm.tm_mday,
1895                                          stm.tm_hour, stm.tm_min, stm.tm_sec);
1896                                 dst[1].append(item2 = new BC_ListBoxItem(date_time));
1897                         }
1898                         else
1899                                 dst[1].append(item2 = new BC_ListBoxItem(""));
1900                         item1->set_autoplace_text(1);  item1->set_autoplace_icon(1);
1901                         item2->set_autoplace_text(1);  item2->set_autoplace_icon(1);
1902                 }
1903         }
1904 }
1905
1906 void AWindowGUI::sort_picons(ArrayList<BC_ListBoxItem*> *src)
1907 {
1908         int done = 0, changed = 0;
1909         while( !done ) {
1910                 done = 1;
1911                 for( int i=0; i<src->total-1; ++i ) {
1912                         AssetPicon *item1 = (AssetPicon *)src->values[i];
1913                         AssetPicon *item2 = (AssetPicon *)src->values[i + 1];
1914                         double v = item2->sort_key - item1->sort_key;
1915                         if( v > 0 ) continue;
1916                         if( v == 0 ) {
1917                                 const char *cp1 = item1->get_text();
1918                                 const char *bp1 = strrchr(cp1, '/');
1919                                 if( bp1 ) cp1 = bp1 + 1;
1920                                 const char *cp2 = item2->get_text();
1921                                 const char *bp2 = strrchr(cp2, '/');
1922                                 if( bp2 ) cp2 = bp2 + 1;
1923                                 if( strcmp(cp2, cp1) >= 0 ) continue;
1924                         }
1925                         src->values[i + 1] = item1;
1926                         src->values[i] = item2;
1927                         done = 0;  changed = 1;
1928                 }
1929         }
1930         if( changed ) {
1931                 for( int i=0; i<src->total; ++i ) {
1932                         AssetPicon *item = (AssetPicon *)src->values[i];
1933                         item->set_autoplace_icon(1);
1934                         item->set_autoplace_text(1);
1935                 }
1936         }
1937 }
1938
1939 void AWindowGUI::filter_displayed_assets()
1940 {
1941         //allow_iconlisting = 1;
1942         asset_titles[0] = C_("Title");
1943         asset_titles[1] = _("Comments");
1944         AssetPicon *picon = 0;
1945         int selected_folder = mwindow->edl->session->awindow_folder;
1946         // Ensure the current folder icon is highlighted
1947         for( int i = 0; i < folders.total; i++ ) {
1948                 AssetPicon *folder_item = (AssetPicon *)folders.values[i];
1949                 int selected = folder_item->foldernum == selected_folder ? 1 : 0;
1950                 folder_item->set_selected(selected);
1951                 if( selected ) picon = folder_item;
1952         }
1953
1954         ArrayList<BC_ListBoxItem*> *src = &assets;
1955         switch( selected_folder ) {
1956         case AW_AEFFECT_FOLDER:  src = &aeffects;  break;
1957         case AW_VEFFECT_FOLDER:  src = &veffects;  break;
1958         case AW_ATRANSITION_FOLDER:  src = &atransitions;  break;
1959         case AW_VTRANSITION_FOLDER:  src = &vtransitions;  break;
1960         case AW_LABEL_FOLDER:  src = &labellist;
1961                 asset_titles[0] = _("Time Stamps");
1962                 asset_titles[1] = C_("Title");
1963                 //allow_iconlisting = 0;
1964                 break;
1965         }
1966         copy_picons(picon, src);
1967 }
1968
1969
1970 void AWindowGUI::update_assets()
1971 {
1972         stop_vicon_drawing();
1973         folder_lock->lock("AWindowGUI::update_assets");
1974         update_folder_list();
1975         update_asset_list();
1976         labellist.remove_all_objects();
1977         create_label_folder();
1978
1979         if( displayed_folder != mwindow->edl->session->awindow_folder )
1980                 search_text->clear();
1981         vicon_thread->hide_vicons();
1982         filter_displayed_assets();
1983         folder_lock->unlock();
1984
1985         if( mwindow->edl->session->folderlist_format != folder_list->get_format() ) {
1986                 folder_list->update_format(mwindow->edl->session->folderlist_format, 0);
1987         }
1988         int folder_xposition = folder_list->get_xposition();
1989         int folder_yposition = folder_list->get_yposition();
1990         folder_list->update(&folders, 0, 0, 1, folder_xposition, folder_yposition, -1);
1991
1992         if( mwindow->edl->session->assetlist_format != asset_list->get_format() ) {
1993                 asset_list->update_format(mwindow->edl->session->assetlist_format, 0);
1994         }
1995         int asset_xposition = asset_list->get_xposition();
1996         int asset_yposition = asset_list->get_yposition();
1997         if( displayed_folder != mwindow->edl->session->awindow_folder ) {
1998                 displayed_folder = mwindow->edl->session->awindow_folder;
1999                 asset_xposition = asset_yposition = 0;
2000         }
2001         asset_list->update(displayed_assets, asset_titles,
2002                 mwindow->edl->session->asset_columns, ASSET_COLUMNS,
2003                 asset_xposition, asset_yposition, -1, 0);
2004         asset_list->center_selection();
2005
2006         flush();
2007         start_vicon_drawing();
2008         return;
2009 }
2010
2011 void AWindowGUI::update_effects()
2012 {
2013         aeffects.remove_all_objects();
2014         create_persistent_folder(&aeffects, 1, 0, 1, 0);
2015         veffects.remove_all_objects();
2016         create_persistent_folder(&veffects, 0, 1, 1, 0);
2017         atransitions.remove_all_objects();
2018         create_persistent_folder(&atransitions, 1, 0, 0, 1);
2019         vtransitions.remove_all_objects();
2020         create_persistent_folder(&vtransitions, 0, 1, 0, 1);
2021 }
2022
2023 int AWindowGUI::drag_motion()
2024 {
2025         if( get_hidden() ) return 0;
2026
2027         int result = 0;
2028         return result;
2029 }
2030
2031 int AWindowGUI::drag_stop()
2032 {
2033         if( get_hidden() ) return 0;
2034         return 0;
2035 }
2036
2037 Indexable* AWindowGUI::selected_asset()
2038 {
2039         AssetPicon *picon = (AssetPicon*)asset_list->get_selection(0, 0);
2040         return picon ? picon->indexable : 0;
2041 }
2042
2043 PluginServer* AWindowGUI::selected_plugin()
2044 {
2045         AssetPicon *picon = (AssetPicon*)asset_list->get_selection(0, 0);
2046         return picon ? picon->plugin : 0;
2047 }
2048
2049 AssetPicon* AWindowGUI::selected_folder()
2050 {
2051         AssetPicon *picon = (AssetPicon*)folder_list->get_selection(0, 0);
2052         return picon;
2053 }
2054
2055
2056
2057
2058
2059
2060
2061
2062 AWindowDivider::AWindowDivider(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2063  : BC_SubWindow(x, y, w, h)
2064 {
2065         this->mwindow = mwindow;
2066         this->gui = gui;
2067 }
2068 AWindowDivider::~AWindowDivider()
2069 {
2070 }
2071
2072 int AWindowDivider::button_press_event()
2073 {
2074         if( is_event_win() && cursor_inside() ) {
2075                 mwindow->session->current_operation = DRAG_PARTITION;
2076                 return 1;
2077         }
2078         return 0;
2079 }
2080
2081 int AWindowDivider::cursor_motion_event()
2082 {
2083         if( mwindow->session->current_operation == DRAG_PARTITION ) {
2084                 int wmin = 25;
2085                 int wmax = mwindow->session->awindow_w - mwindow->theme->adivider_w - wmin;
2086                 int fw = gui->get_relative_cursor_x();
2087                 if( fw > wmax ) fw = wmax;
2088                 if( fw < wmin ) fw = wmin;
2089                 mwindow->session->afolders_w = fw;
2090                 mwindow->theme->get_awindow_sizes(gui);
2091                 gui->reposition_objects();
2092                 gui->flush();
2093         }
2094         return 0;
2095 }
2096
2097 int AWindowDivider::button_release_event()
2098 {
2099         if( mwindow->session->current_operation == DRAG_PARTITION ) {
2100                 mwindow->session->current_operation = NO_OPERATION;
2101                 return 1;
2102         }
2103         return 0;
2104 }
2105
2106
2107
2108
2109
2110
2111 AWindowFolders::AWindowFolders(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2112  : BC_ListBox(x, y, w, h,
2113                 mwindow->edl->session->folderlist_format == FOLDERS_ICONS ?
2114                         LISTBOX_ICONS : LISTBOX_TEXT,
2115                 &gui->folders,    // Each column has an ArrayList of BC_ListBoxItems.
2116                 0,                // Titles for columns.  Set to 0 for no titles
2117                 0,                // width of each column
2118                 1,                // Total columns.
2119                 0,                // Pixel of top of window.
2120                 0,                // If this listbox is a popup window
2121                 LISTBOX_SINGLE,   // Select one item or multiple items
2122                 ICON_TOP,         // Position of icon relative to text of each item
2123                 1)                // Allow drags
2124 {
2125         this->mwindow = mwindow;
2126         this->gui = gui;
2127         set_drag_scroll(0);
2128         last_item0 = 0;
2129         last_item1 = 0;
2130 }
2131
2132 AWindowFolders::~AWindowFolders()
2133 {
2134 }
2135
2136 int AWindowFolders::selection_changed()
2137 {
2138         AWindowFolderItem *item0 = (AWindowFolderItem*)get_selection(0, 0);
2139         AWindowFolderItem *item1 = (AWindowFolderItem*)get_selection(0, 1);
2140 // prefer expanded entry
2141         AWindowFolderItem *item = item1 ? item1 : item0;
2142         if( item0 && item1 && last_item0 == item0 && last_item1 == item1 ) {
2143                 item1->set_selected(0);
2144                 item1 = 0;
2145                 item = item0;
2146         }
2147         last_item0 = item0;
2148         last_item1 = item1;
2149         if( item ) {
2150                 AssetPicon *picon = item->get_picon();
2151                 picon->sub_items = (AWindowFolderSubItems*)(!item->parent ? 0 : item);
2152
2153                 gui->stop_vicon_drawing();
2154
2155                 if( get_button_down() && get_buttonpress() == 3 ) {
2156                         gui->folderlist_menu->update_titles();
2157                         gui->folderlist_menu->activate_menu();
2158                 }
2159
2160                 mwindow->edl->session->awindow_folder = picon->foldernum;
2161                 gui->asset_list->draw_background();
2162                 gui->async_update_assets();
2163
2164                 gui->start_vicon_drawing();
2165         }
2166         return 1;
2167 }
2168
2169 int AWindowFolders::button_press_event()
2170 {
2171         int result = 0;
2172
2173         result = BC_ListBox::button_press_event();
2174
2175         if( !result ) {
2176                 if( get_buttonpress() == 3 && is_event_win() && cursor_inside() ) {
2177                         gui->folderlist_menu->update_titles();
2178                         gui->folderlist_menu->activate_menu();
2179                         result = 1;
2180                 }
2181         }
2182
2183
2184         return result;
2185 }
2186
2187 int AWindowFolders::drag_stop()
2188 {
2189         int result = 0;
2190         if( get_hidden() ) return 0;
2191         if( mwindow->session->current_operation == DRAG_ASSET &&
2192             gui->folder_list->cursor_above() ) { // check user folder
2193                 int item_no = gui->folder_list->get_cursor_data_item_no();
2194                 AssetPicon *picon = (AssetPicon *)(item_no < 0 ? 0 : gui->folders[item_no]);
2195                 if( picon && picon->foldernum >= AWINDOW_USER_FOLDERS ) {
2196                         BinFolder *folder = mwindow->edl->get_folder(picon->foldernum);
2197                         ArrayList<Indexable *> *drags = folder->is_clips ?
2198                                 ((ArrayList<Indexable *> *)mwindow->session->drag_clips) :
2199                                 ((ArrayList<Indexable *> *)mwindow->session->drag_assets);
2200                         if( folder && drags && !folder->add_patterns(drags, shift_down()) )
2201                                 flicker(1,30);
2202                         mwindow->session->current_operation = ::NO_OPERATION;
2203                         result = 1;
2204                 }
2205         }
2206         return result;
2207 }
2208
2209 AWindowFolderSubItems::AWindowFolderSubItems(AWindowFolderItem *parent, const char *text)
2210  : AWindowFolderItem(text)
2211 {
2212         this->parent = parent;
2213 }
2214
2215 int AWindowFolders::load_expanders()
2216 {
2217         char expanders_path[BCTEXTLEN];
2218         mwindow->create_defaults_path(expanders_path, EXPANDERS_FILE);
2219         FILE *fp = fopen(expanders_path, "r");
2220         if( !fp ) {
2221                 snprintf(expanders_path, sizeof(expanders_path), "%s/%s",
2222                         File::get_cindat_path(), EXPANDERS_FILE);
2223                 fp = fopen(expanders_path, "r");
2224         }
2225
2226         if( !fp ) return 1;
2227         const char tab = '\t';
2228         char line[BCTEXTLEN];   line[0] = 0;
2229         AWindowFolderItem *item = 0, *parent;
2230         AWindowFolderSubItems *sub_items = 0;
2231         int k = 0;
2232         while( fgets(line,sizeof(line),fp) ) {
2233                 if( line[0] == '#' ) continue;
2234                 int i = strlen(line);
2235                 if( i > 0 && line[i-1] == '\n' ) line[--i] = 0;
2236                 if( i == 0 ) continue;
2237                 i = 0;
2238                 for( char *cp=line; *cp==tab; ++cp ) ++i;
2239                 if( i == 0 ) {
2240                         int i = gui->folders.size();
2241                         while( --i >= 0 ) {
2242                                 AssetPicon *folder = (AssetPicon *)gui->folders[i];
2243                                 if( !strcmp(folder->get_text(),_(line)) ) break;
2244                         }
2245                         item = (AWindowFolderItem*)(i >= 0 ? gui->folders[i] : 0);
2246                         sub_items = 0;
2247                         k = 0;
2248                         continue;
2249                 }
2250                 if( i > k+1 ) continue;
2251                 if( i == k+1 ) {
2252                         if( line[i] != '-' && sub_items ) {
2253                                 sub_items->names.append(cstrdup(_(&line[i])));
2254                                 continue;
2255                         }
2256                         parent = item;
2257                         k = i;
2258                 }
2259                 else {
2260                         while( i < k ) {
2261                                 item = item->parent;
2262                                 --k;
2263                         }
2264                         parent = item->parent;
2265                 }
2266                 ArrayList<BC_ListBoxItem*> *sublist = parent->get_sublist();
2267                 if( !sublist ) sublist = parent->new_sublist(1);
2268                 sub_items = new AWindowFolderSubItems(parent, &line[i]);
2269                 sublist->append(item = sub_items);
2270         }
2271         fclose(fp);
2272         return 0;
2273 }
2274
2275
2276 AWindowAssets::AWindowAssets(MWindow *mwindow, AWindowGUI *gui, int x, int y, int w, int h)
2277  : BC_ListBox(x, y, w, h, !gui->allow_iconlisting ? LISTBOX_TEXT :
2278                 mwindow->edl->session->assetlist_format == ASSETS_ICONS ? LISTBOX_ICONS :
2279                 mwindow->edl->session->assetlist_format == ASSETS_ICONS_PACKED ? LISTBOX_ICONS_PACKED :
2280                 mwindow->edl->session->assetlist_format == ASSETS_ICON_LIST ? LISTBOX_ICON_LIST :
2281                         LISTBOX_TEXT,
2282                 &gui->assets,     // Each column has an ArrayList of BC_ListBoxItems.
2283                 gui->asset_titles,// Titles for columns.  Set to 0 for no titles
2284                 mwindow->edl->session->asset_columns, // width of each column
2285                 1,                // Total columns.
2286                 0,                // Pixel of top of window.
2287                 0,                // If this listbox is a popup window
2288                 LISTBOX_MULTIPLE, // Select one item or multiple items
2289                 ICON_TOP,         // Position of icon relative to text of each item
2290                 -1)               // Allow drags, require shift for scrolling
2291 {
2292         this->mwindow = mwindow;
2293         this->gui = gui;
2294         set_drag_scroll(0);
2295         set_scroll_stretch(1, 1);
2296 }
2297
2298 AWindowAssets::~AWindowAssets()
2299 {
2300 }
2301
2302 int AWindowAssets::button_press_event()
2303 {
2304         int result = 0;
2305
2306         result = BC_ListBox::button_press_event();
2307
2308         if( !result && get_buttonpress() == 3 && is_event_win() && cursor_inside() ) {
2309                 BC_ListBox::deactivate_selection();
2310                 int folder = mwindow->edl->session->awindow_folder;
2311                 switch( folder ) {
2312                 case AW_AEFFECT_FOLDER:
2313                 case AW_VEFFECT_FOLDER:
2314                 case AW_ATRANSITION_FOLDER:
2315                 case AW_VTRANSITION_FOLDER:
2316                         gui->effectlist_menu->update();
2317                         gui->effectlist_menu->activate_menu();
2318                         break;
2319                 case AW_LABEL_FOLDER:
2320                         gui->labellist_menu->update();
2321                         gui->labellist_menu->activate_menu();
2322                         break;
2323                 case AW_CLIP_FOLDER:
2324                         gui->cliplist_menu->update();
2325                         gui->cliplist_menu->activate_menu();
2326                         break;
2327                 case AW_PROXY_FOLDER:
2328                         gui->proxylist_menu->update();
2329                         gui->proxylist_menu->activate_menu();
2330                         break;
2331                 default:
2332                 case AW_MEDIA_FOLDER: {
2333                         int shots =  folder==AW_MEDIA_FOLDER || folder>=AWINDOW_USER_FOLDERS;
2334                         gui->assetlist_menu->update_titles(shots);
2335                         gui->assetlist_menu->activate_menu();
2336                         break; }
2337                 }
2338                 result = 1;
2339         }
2340
2341         return result;
2342 }
2343
2344
2345 int AWindowAssets::handle_event()
2346 {
2347         AssetPicon *asset_picon = (AssetPicon *)get_selection(0, 0);
2348         if( !asset_picon ) return 0;
2349         Indexable *picon_idxbl = asset_picon->indexable;
2350         EDL *picon_edl = asset_picon->edl;
2351         int proxy = 0;
2352         VWindow *vwindow = 0;
2353         switch( mwindow->edl->session->awindow_folder ) {
2354         case AW_AEFFECT_FOLDER:
2355         case AW_VEFFECT_FOLDER:
2356         case AW_ATRANSITION_FOLDER:
2357         case AW_VTRANSITION_FOLDER: return 1;
2358         case AW_PROXY_FOLDER:
2359                 proxy = 1; // fall thru
2360         default:
2361                 if( mwindow->vwindows.size() > DEFAULT_VWINDOW )
2362                         vwindow = mwindow->vwindows.get(DEFAULT_VWINDOW);
2363                 break;
2364         }
2365         if( !vwindow || !vwindow->is_running() ) return 1;
2366         if( proxy && picon_idxbl ) {
2367                 picon_edl = gui->collect_proxy(picon_idxbl);
2368                 picon_idxbl = 0;
2369         }
2370
2371         if( picon_idxbl ) vwindow->change_source(picon_idxbl);
2372         else if( picon_edl ) vwindow->change_source(picon_edl);
2373         return 1;
2374 }
2375
2376 int AWindowAssets::selection_changed()
2377 {
2378 // Show popup window
2379         AssetPicon *item;
2380         if( get_button_down() && get_buttonpress() == 3 &&
2381             (item = (AssetPicon*)get_selection(0, 0)) ) {
2382                 int folder = mwindow->edl->session->awindow_folder;
2383                 switch( folder ) {
2384                 case AW_AEFFECT_FOLDER:
2385                 case AW_VEFFECT_FOLDER:
2386                 case AW_ATRANSITION_FOLDER:
2387                 case AW_VTRANSITION_FOLDER:
2388                         gui->effectlist_menu->update();
2389                         gui->effectlist_menu->activate_menu();
2390                         break;
2391                 case AW_LABEL_FOLDER:
2392                         if( !item->label ) break;
2393                         gui->label_menu->activate_menu();
2394                         break;
2395                 case AW_CLIP_FOLDER:
2396                         if( !item->indexable && !item->edl ) break;
2397                         gui->clip_menu->update();
2398                         gui->clip_menu->activate_menu();
2399                         break;
2400                 case AW_PROXY_FOLDER:
2401                         if( !item->indexable && !item->edl ) break;
2402                         gui->proxy_menu->update();
2403                         gui->proxy_menu->activate_menu();
2404                         break;
2405                 default:
2406                         if( !item->indexable && !item->edl ) break;
2407                         gui->asset_menu->update();
2408                         gui->asset_menu->activate_menu();
2409                         break;
2410                 }
2411
2412                 deactivate_selection();
2413         }
2414         else if( gui->vicon_drawing && get_button_down() && get_buttonpress() == 1 &&
2415                  ( mwindow->edl->session->awindow_folder == AW_MEDIA_FOLDER ||
2416                    mwindow->edl->session->awindow_folder == AW_PROXY_FOLDER ||
2417                    mwindow->edl->session->awindow_folder >= AWINDOW_USER_FOLDERS ) &&
2418                    (item = (AssetPicon*)get_selection(0, 0)) != 0 ) {
2419                 VIcon *vicon = 0;
2420                 if( !gui->vicon_thread->vicon  ) {
2421                         vicon = item->vicon;
2422                 }
2423                 gui->vicon_thread->set_view_popup(vicon);
2424         }
2425         return 1;
2426 }
2427
2428 void AWindowAssets::draw_background()
2429 {
2430         clear_box(0,0,get_w(),get_h(),get_bg_surface());
2431         set_color(BC_WindowBase::get_resources()->audiovideo_color);
2432         set_font(LARGEFONT);
2433         int folder = mwindow->edl->session->awindow_folder;
2434         const char *title = mwindow->edl->get_folder_name(folder);
2435         draw_text(get_w() - get_text_width(LARGEFONT, title) - 4, 30,
2436                 title, -1, get_bg_surface());
2437 }
2438
2439 int AWindowAssets::drag_start_event()
2440 {
2441         int collect_pluginservers = 0;
2442         int collect_assets = 0, proxy = 0;
2443
2444         if( BC_ListBox::drag_start_event() ) {
2445                 switch( mwindow->edl->session->awindow_folder ) {
2446                 case AW_AEFFECT_FOLDER:
2447                         mwindow->session->current_operation = DRAG_AEFFECT;
2448                         collect_pluginservers = 1;
2449                         break;
2450                 case AW_VEFFECT_FOLDER:
2451                         mwindow->session->current_operation = DRAG_VEFFECT;
2452                         collect_pluginservers = 1;
2453                         break;
2454                 case AW_ATRANSITION_FOLDER:
2455                         mwindow->session->current_operation = DRAG_ATRANSITION;
2456                         collect_pluginservers = 1;
2457                         break;
2458                 case AW_VTRANSITION_FOLDER:
2459                         mwindow->session->current_operation = DRAG_VTRANSITION;
2460                         collect_pluginservers = 1;
2461                         break;
2462                 case AW_LABEL_FOLDER:
2463                         // do nothing!
2464                         break;
2465                 case AW_PROXY_FOLDER:
2466                         proxy = 1; // fall thru
2467                 case AW_MEDIA_FOLDER:
2468                 default:
2469                         mwindow->session->current_operation = DRAG_ASSET;
2470                         collect_assets = 1;
2471                         break;
2472                 }
2473
2474                 if( collect_pluginservers ) {
2475                         int i = 0;
2476                         mwindow->session->drag_pluginservers->remove_all();
2477                         while(1)
2478                         {
2479                                 AssetPicon *result = (AssetPicon*)get_selection(0, i++);
2480                                 if( !result ) break;
2481
2482                                 mwindow->session->drag_pluginservers->append(result->plugin);
2483                         }
2484                 }
2485
2486                 if( collect_assets ) {
2487                         gui->collect_assets(proxy);
2488                 }
2489
2490                 return 1;
2491         }
2492         return 0;
2493 }
2494
2495 int AWindowAssets::drag_motion_event()
2496 {
2497         BC_ListBox::drag_motion_event();
2498         unlock_window();
2499
2500         mwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2501         mwindow->gui->drag_motion();
2502         mwindow->gui->unlock_window();
2503
2504         for( int i = 0; i < mwindow->vwindows.size(); i++ ) {
2505                 VWindow *vwindow = mwindow->vwindows.get(i);
2506                 if( !vwindow->is_running() ) continue;
2507                 vwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2508                 vwindow->gui->drag_motion();
2509                 vwindow->gui->unlock_window();
2510         }
2511
2512         mwindow->cwindow->gui->lock_window("AWindowAssets::drag_motion_event");
2513         mwindow->cwindow->gui->drag_motion();
2514         mwindow->cwindow->gui->unlock_window();
2515
2516         lock_window("AWindowAssets::drag_motion_event");
2517         if( mwindow->session->current_operation == DRAG_ASSET &&
2518             gui->folder_list->cursor_above() ) { // highlight user folder
2519                 BC_ListBoxItem *item = 0;
2520                 int item_no = gui->folder_list->get_cursor_data_item_no(&item);
2521                 if( item_no >= 0 ) {
2522                         AssetPicon *folder = (AssetPicon *)gui->folders[item_no];
2523                         if( folder->foldernum < AWINDOW_USER_FOLDERS ) item_no = -1;
2524                 }
2525                 if( item_no >= 0 )
2526                         item_no = gui->folder_list->item_to_index(&gui->folders, item);
2527                 int folder_xposition = gui->folder_list->get_xposition();
2528                 int folder_yposition = gui->folder_list->get_yposition();
2529                 gui->folder_list->update(&gui->folders, 0, 0, 1,
2530                         folder_xposition, folder_yposition, item_no, 0, 1);
2531         }
2532         return 0;
2533 }
2534
2535 int AWindowAssets::drag_stop_event()
2536 {
2537         int result = 0;
2538
2539         result = gui->drag_stop();
2540
2541         unlock_window();
2542
2543         if( !result ) {
2544                 mwindow->gui->lock_window("AWindowAssets::drag_stop_event");
2545                 result = mwindow->gui->drag_stop();
2546                 mwindow->gui->unlock_window();
2547         }
2548
2549         if( !result ) {
2550                 for( int i = 0; !result && i < mwindow->vwindows.size(); i++ ) {
2551                         VWindow *vwindow = mwindow->vwindows.get(i);
2552                         if( !vwindow ) continue;
2553                         if( !vwindow->is_running() ) continue;
2554                         if( vwindow->gui->is_hidden() ) continue;
2555                         vwindow->gui->lock_window("AWindowAssets::drag_stop_event");
2556                         if( vwindow->gui->cursor_above() &&
2557                             vwindow->gui->get_cursor_over_window() ) {
2558                                 result = vwindow->gui->drag_stop();
2559                         }
2560                         vwindow->gui->unlock_window();
2561                 }
2562         }
2563
2564         if( !result ) {
2565                 mwindow->cwindow->gui->lock_window("AWindowAssets::drag_stop_event");
2566                 result = mwindow->cwindow->gui->drag_stop();
2567                 mwindow->cwindow->gui->unlock_window();
2568         }
2569
2570         lock_window("AWindowAssets::drag_stop_event");
2571         if( !result ) {
2572                 result = gui->folder_list->drag_stop();
2573         }
2574
2575
2576         if( result )
2577                 get_drag_popup()->set_animation(0);
2578
2579         BC_ListBox::drag_stop_event();
2580 // since NO_OPERATION is also defined in listbox, we have to reach for global scope...
2581         mwindow->session->current_operation = ::NO_OPERATION;
2582
2583         return 1;
2584 }
2585
2586 int AWindowAssets::column_resize_event()
2587 {
2588         mwindow->edl->session->asset_columns[0] = get_column_width(0);
2589         mwindow->edl->session->asset_columns[1] = get_column_width(1);
2590         return 1;
2591 }
2592
2593 int AWindowAssets::focus_in_event()
2594 {
2595         int ret = BC_ListBox::focus_in_event();
2596         gui->start_vicon_drawing();
2597         return ret;
2598 }
2599
2600 int AWindowAssets::focus_out_event()
2601 {
2602         gui->stop_vicon_drawing();
2603         return BC_ListBox::focus_out_event();
2604 }
2605
2606 int AWindowAssets::cursor_enter_event()
2607 {
2608         int ret = BC_ListBox::cursor_enter_event();
2609         gui->start_vicon_drawing();
2610         return ret;
2611 }
2612
2613 int AWindowAssets::cursor_leave_event()
2614 {
2615         gui->stop_vicon_drawing();
2616         return BC_ListBox::cursor_leave_event();
2617 }
2618
2619 void AWindowAssets::update_vicon_area()
2620 {
2621         int x0 = 0, x1 = get_w();
2622         int y0 = get_title_h();
2623         int y1 = get_h();
2624         if( is_highlighted() ) {
2625                 x0 += LISTBOX_BORDER;  x1 -= LISTBOX_BORDER;
2626                 y0 += LISTBOX_BORDER;  y1 -= LISTBOX_BORDER;
2627         }
2628         gui->vicon_thread->set_drawing_area(x0,y0, x1,y1);
2629 }
2630
2631 int AWindowAssets::mouse_over_event(int no)
2632 {
2633         if( gui->vicon_thread->viewing &&
2634             no >= 0 && no < gui->displayed_assets[0].size() ) {
2635                 AssetPicon *picon = (AssetPicon *)gui->displayed_assets[0][no];
2636                 VIcon *vicon = picon->vicon;
2637                 picon->gui->vicon_thread->set_view_popup(vicon);
2638         }
2639         return 0;
2640 }
2641
2642
2643 AWindowSearchTextBox::AWindowSearchTextBox(AWindowSearchText *search_text, int x, int y, int w)
2644  : BC_TextBox(x, y, w, 1, "")
2645 {
2646         this->search_text = search_text;
2647 }
2648
2649 int AWindowSearchTextBox::handle_event()
2650 {
2651         return search_text->handle_event();
2652 }
2653
2654 AWindowSearchText::AWindowSearchText(MWindow *mwindow, AWindowGUI *gui, int x, int y)
2655 {
2656         this->mwindow = mwindow;
2657         this->gui = gui;
2658         this->x = x;
2659         this->y = y;
2660 }
2661
2662 void AWindowSearchText::create_objects()
2663 {
2664         int x1 = x, y1 = y, margin = 10;
2665         gui->add_subwindow(text_title = new BC_Title(x1, y1, _("Search:")));
2666         x1 += text_title->get_w() + margin;
2667         int w1 = gui->get_w() - x1 - 2*margin;
2668         gui->add_subwindow(text_box = new AWindowSearchTextBox(this, x1, y1, w1));
2669 }
2670
2671 int AWindowSearchText::handle_event()
2672 {
2673         gui->async_update_assets();
2674         return 1;
2675 }
2676
2677 int AWindowSearchText::get_w()
2678 {
2679         return text_box->get_w() + text_title->get_w() + 10;
2680 }
2681
2682 int AWindowSearchText::get_h()
2683 {
2684         return bmax(text_box->get_h(),text_title->get_h());
2685 }
2686
2687 void AWindowSearchText::reposition_window(int x, int y, int w)
2688 {
2689         int x1 = x, y1 = y, margin = 10;
2690         text_title->reposition_window(x1, y1);
2691         x1 += text_title->get_w() + margin;
2692         int w1 = gui->get_w() - x1 - 2*margin;
2693         text_box->reposition_window(x1, y1, w1);
2694 }
2695
2696 const char *AWindowSearchText::get_text()
2697 {
2698         return text_box->get_text();
2699 }
2700
2701 void AWindowSearchText::clear()
2702 {
2703         text_box->update("");
2704 }
2705
2706 AWindowDeleteDisk::AWindowDeleteDisk(MWindow *mwindow, AWindowGUI *gui, int x, int y)
2707  : BC_Button(x, y, mwindow->theme->deletedisk_data)
2708 {
2709         this->mwindow = mwindow;
2710         this->gui = gui;
2711         set_tooltip(_("Delete asset from disk"));
2712 }
2713
2714 int AWindowDeleteDisk::handle_event()
2715 {
2716         return 1;
2717 }
2718
2719 AWindowDeleteProject::AWindowDeleteProject(MWindow *mwindow, AWindowGUI *gui, int x, int y)
2720  : BC_Button(x, y, mwindow->theme->deleteproject_data)
2721 {
2722         this->mwindow = mwindow;
2723         this->gui = gui;
2724         set_tooltip(_("Delete asset from project"));
2725 }
2726
2727 int AWindowDeleteProject::handle_event()
2728 {
2729         return 1;
2730 }
2731
2732 // AWindowInfo::AWindowInfo(MWindow *mwindow, AWindowGUI *gui, int x, int y)
2733 //  : BC_Button(x, y, mwindow->theme->infoasset_data)
2734 // {
2735 //      this->mwindow = mwindow;
2736 //      this->gui = gui;
2737 //      set_tooltip(_("Edit information on asset"));
2738 // }
2739 // 
2740 // int AWindowInfo::handle_event()
2741 // {
2742 //      int cur_x, cur_y;
2743 //      gui->get_abs_cursor(cur_x, cur_y, 0);
2744 //      gui->awindow->asset_edit->edit_asset(gui->selected_asset(), cur_x, cur_y);
2745 //      return 1;
2746 // }
2747
2748 AWindowRedrawIndex::AWindowRedrawIndex(MWindow *mwindow, AWindowGUI *gui, int x, int y)
2749  : BC_Button(x, y, mwindow->theme->redrawindex_data)
2750 {
2751         this->mwindow = mwindow;
2752         this->gui = gui;
2753         set_tooltip(_("Redraw index"));
2754 }
2755
2756 int AWindowRedrawIndex::handle_event()
2757 {
2758         return 1;
2759 }
2760
2761 AWindowPaste::AWindowPaste(MWindow *mwindow, AWindowGUI *gui, int x, int y)
2762  : BC_Button(x, y, mwindow->theme->pasteasset_data)
2763 {
2764         this->mwindow = mwindow;
2765         this->gui = gui;
2766         set_tooltip(_("Paste asset on recordable tracks"));
2767 }
2768
2769 int AWindowPaste::handle_event()
2770 {
2771         return 1;
2772 }
2773
2774 AWindowAppend::AWindowAppend(MWindow *mwindow, AWindowGUI *gui, int x, int y)
2775  : BC_Button(x, y, mwindow->theme->appendasset_data)
2776 {
2777         this->mwindow = mwindow;
2778         this->gui = gui;
2779         set_tooltip(_("Append asset in new tracks"));
2780 }
2781
2782 int AWindowAppend::handle_event()
2783 {
2784         return 1;
2785 }
2786
2787 AWindowView::AWindowView(MWindow *mwindow, AWindowGUI *gui, int x, int y)
2788  : BC_Button(x, y, mwindow->theme->viewasset_data)
2789 {
2790         this->mwindow = mwindow;
2791         this->gui = gui;
2792         set_tooltip(_("View asset"));
2793 }
2794
2795 int AWindowView::handle_event()
2796 {
2797         return 1;
2798 }
2799
2800 AddTools::AddTools(MWindow *mwindow, AWindowGUI *gui, int x, int y, const char *title)
2801  : BC_PopupMenu(x, y, BC_Title::calculate_w(gui, title, MEDIUMFONT)+8, title, -1, 0, 4)
2802 {
2803         this->mwindow = mwindow;
2804         this->gui = gui;
2805 }
2806
2807 void AddTools::create_objects()
2808 {
2809         uint64_t vis = 0;
2810         add_item(new AddPluginItem(this, "ffmpeg", PLUGIN_FFMPEG_ID));
2811         vis |= 1 << PLUGIN_FFMPEG_ID;
2812         add_item(new AddPluginItem(this, "ladspa", PLUGIN_LADSPA_ID));
2813         vis |= 1 << PLUGIN_LADSPA_ID;
2814 #ifdef HAVE_LV2
2815         add_item(new AddPluginItem(this, "lv2", PLUGIN_LV2_ID));
2816         vis |= 1 << PLUGIN_LV2_ID;
2817 #endif
2818         for( int i=0; i<MWindow::plugindb->size(); ++i ) {
2819                 PluginServer *plugin = MWindow::plugindb->get(i);
2820                 if( !plugin->audio && !plugin->video ) continue;
2821                 int idx = plugin->dir_idx;
2822                 uint32_t msk = 1 << idx;
2823                 if( (msk & vis) != 0 ) continue;
2824                 vis |= msk;
2825                 char parent[BCTEXTLEN];
2826                 strcpy(parent, plugin->path);
2827                 char *bp = strrchr(parent, '/');
2828                 if( bp ) { *bp = 0;  bp = strrchr(parent, '/'); }
2829                 if( !bp ) bp = parent; else ++bp;
2830                 add_item(new AddPluginItem(this, bp, idx));
2831         }
2832 }
2833
2834 #if 0
2835 // plugin_dirs list from toplevel makefile include plugin_defs
2836 N_("ffmpeg")
2837 N_("ladspa")
2838 N_("lv2")
2839 N_("audio_tools")
2840 N_("audio_transitions")
2841 N_("blending")
2842 N_("colors")
2843 N_("exotic")
2844 N_("transforms")
2845 N_("tv_effects")
2846 N_("video_tools")
2847 N_("video_transitions")
2848 #endif
2849
2850 AddPluginItem::AddPluginItem(AddTools *menu, char const *text, int idx)
2851  : BC_MenuItem(_(text))
2852 {
2853         this->menu = menu;
2854         this->idx = idx;
2855         uint64_t msk = (uint64_t)1 << idx, vis = menu->gui->plugin_visibility;
2856         int chk = (msk & vis) ? 1 : 0;
2857         set_checked(chk);
2858 }
2859
2860 int AddPluginItem::handle_event()
2861 {
2862         int chk = get_checked() ^ 1;
2863         set_checked(chk);
2864         uint64_t msk = (uint64_t)1 << idx, vis = menu->gui->plugin_visibility;
2865         menu->gui->plugin_visibility = chk ? vis | msk : vis & ~msk;
2866         menu->gui->update_effects();
2867         menu->gui->save_defaults(menu->mwindow->defaults);
2868         menu->gui->async_update_assets();
2869         return 1;
2870 }
2871
2872 AVIconDrawing::AVIconDrawing(AWindowGUI *agui, int x, int y, VFrame **images)
2873  : BC_Toggle(x, y, images, agui->vicon_drawing)
2874 {
2875         this->agui = agui;
2876         set_tooltip(_("Preview"));
2877 }
2878
2879 void AVIconDrawing::calculate_geometry(AWindowGUI *agui, VFrame **images, int *ww, int *hh)
2880 {
2881         int text_line = -1, toggle_x = -1, toggle_y = -1;
2882         int text_x = -1, text_y = -1, text_w = -1, text_h = -1;
2883         BC_Toggle::calculate_extents(agui, images, 1,
2884                 &text_line, ww, hh, &toggle_x, &toggle_y,
2885                 &text_x, &text_y, &text_w, &text_h, "", MEDIUMFONT);
2886 }
2887
2888 AVIconDrawing::~AVIconDrawing()
2889 {
2890 }
2891
2892 int AVIconDrawing::handle_event()
2893 {
2894         agui->vicon_drawing = get_value();
2895         if( agui->vicon_drawing )
2896                 agui->start_vicon_drawing();
2897         else
2898                 agui->stop_vicon_drawing();
2899         return 1;
2900 }
2901
2902
2903 AWindowListFormat::AWindowListFormat(MWindow *mwindow, AWindowGUI *gui)
2904  : BC_MenuItem("","v",'v')
2905 {
2906         this->mwindow = mwindow;
2907         this->gui = gui;
2908 }
2909
2910 int AWindowListFormat::handle_event()
2911 {
2912         return gui->cycle_assetlist_format();
2913 }
2914
2915 void AWindowListFormat::update()
2916 {
2917         EDLSession *session = mwindow->edl->session;
2918         const char *text = 0;
2919         switch( session->assetlist_format ) {
2920         case ASSETS_TEXT:
2921                 text = _("Display icons");
2922                 break;
2923         case ASSETS_ICONS:
2924                 text = _("Display icons packed");
2925                 break;
2926         case ASSETS_ICONS_PACKED:
2927                 text = _("Display icon list");
2928                 break;
2929         case ASSETS_ICON_LIST:
2930                 text = _("Display text");
2931                 break;
2932         }
2933         set_text(text);
2934 }
2935
2936 AWindowListSort::AWindowListSort(MWindow *mwindow, AWindowGUI *gui)
2937  : BC_MenuItem(_("Sort items"))
2938 {
2939         this->mwindow = mwindow;
2940         this->gui = gui;
2941 }
2942
2943 int AWindowListSort::handle_event()
2944 {
2945         gui->sort_assets();
2946         return 1;
2947 }
2948
2949 AssetSelectUsedItem::AssetSelectUsedItem(AssetSelectUsed *select_used, const char *text, int action)
2950  : BC_MenuItem(text)
2951 {
2952         this->select_used = select_used;
2953         this->action = action;
2954 }
2955
2956 int AssetSelectUsedItem::handle_event()
2957 {
2958         MWindow *mwindow = select_used->mwindow;
2959         AWindowGUI *gui = select_used->gui;
2960         AWindowAssets *asset_list = gui->asset_list;
2961         ArrayList<BC_ListBoxItem*> *data = gui->displayed_assets;
2962
2963         switch( action ) {
2964         case SELECT_ALL:
2965         case SELECT_NONE:
2966                 asset_list->set_all_selected(data, action==SELECT_ALL ? 1 : 0);
2967                 break;
2968         case SELECT_USED:
2969         case SELECT_UNUSED:
2970                 asset_list->set_all_selected(data, 0);
2971                 for( int i = 0; i < data->total; i++ ) {
2972                         AssetPicon *picon = (AssetPicon*)data->values[i];
2973                         Indexable *idxbl = picon->indexable ? picon->indexable :
2974                             picon->edl ? picon->edl->get_proxy_asset() : 0;
2975                         int used = idxbl && mwindow->edl->in_use(idxbl) ? 1 : 0;
2976                         asset_list->set_selected(data, i, action==SELECT_USED ? used : !used);
2977                 }
2978                 break;
2979         }
2980
2981         int asset_xposition = asset_list->get_xposition();
2982         int asset_yposition = asset_list->get_yposition();
2983         asset_list->update(gui->displayed_assets, gui->asset_titles,
2984                 mwindow->edl->session->asset_columns, ASSET_COLUMNS,
2985                 asset_xposition, asset_yposition, -1, 0);
2986         asset_list->center_selection();
2987         return 1;
2988 }
2989
2990 AssetSelectUsed::AssetSelectUsed(MWindow *mwindow, AWindowGUI *gui)
2991  : BC_MenuItem(_("Select"))
2992 {
2993         this->mwindow = mwindow;
2994         this->gui = gui;
2995 }
2996