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