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