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