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