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