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