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