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