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