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