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