rework undo compression, add shift viewer overwr/copy/clip/splice, fix paste edl...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / awindowgui.C
index a50f21a2280ddea161a53c28a9413449be399269..ea21a02cd70256b67222eee0d9c128eb679c3a99 100644 (file)
  *
  */
 
+#include "arender.h"
 #include "asset.h"
 #include "assetedit.h"
 #include "assetpopup.h"
 #include "assets.h"
+#include "audiodevice.h"
 #include "awindowgui.h"
 #include "awindow.h"
 #include "bccmodels.h"
@@ -31,6 +33,7 @@
 #include "cache.h"
 #include "cstrdup.h"
 #include "clip.h"
+#include "clipedls.h"
 #include "clippopup.h"
 #include "cursors.h"
 #include "cwindowgui.h"
 #include "mainsession.h"
 #include "mwindowgui.h"
 #include "mwindow.h"
-#include "nestededls.h"
 #include "newfolder.h"
 #include "preferences.h"
+#include "renderengine.h"
+#include "samples.h"
 #include "theme.h"
+#include "tracks.h"
+#include "transportque.h"
 #include "vframe.h"
 #include "vicon.h"
+#include "vrender.h"
 #include "vwindowgui.h"
 #include "vwindow.h"
 
@@ -97,9 +104,36 @@ AssetVIcon::~AssetVIcon()
 
 VFrame *AssetVIcon::frame()
 {
+       Asset *asset = (Asset *)picon->indexable;
+       if( !asset->video_data && audio_data && audio_size && length > 0 ) {
+               if( !temp ) temp = new VFrame(0, -1, vw, vh, BC_RGB888, -1);
+               temp->clear_frame();
+               int sample_rate = asset->get_sample_rate();
+               int64_t audio_samples = asset->get_audio_samples();
+               double duration = (double)audio_samples / sample_rate;
+               picon->draw_hue_bar(temp, duration);
+               int secs = length / frame_rate + 0.5;
+               if( !secs ) secs = 1;
+               int bfrsz = VICON_SAMPLE_RATE;
+               int samples = audio_size/sizeof(vicon_audio_t);
+               double line_pos = (double)seq_no/(length-1);
+               int audio_pos = samples * line_pos * (secs-1)/secs;
+               vicon_audio_t *audio_data = ((vicon_audio_t *)this->audio_data) + audio_pos;
+               static const int mx = (1<<(8*sizeof(*audio_data)-1)) - 1;
+               double data[bfrsz], sample_scale = 1./mx;
+               int len = samples - audio_pos;
+               if( len > bfrsz ) len = bfrsz;
+               int i = 0;
+               while( i < len ) data[i++] = *audio_data++ * sample_scale;
+               while( i < bfrsz ) data[i++] = 0;
+               picon->draw_wave(temp, data, bfrsz, RED, GREEN);
+               int x = (vw-1) * line_pos;
+               temp->pixel_rgb = RED;
+               temp->draw_line(x,0, x,vh);
+               return temp;
+       }
        if( seq_no >= images.size() ) {
                MWindow *mwindow = picon->mwindow;
-               Asset *asset = (Asset *)picon->indexable;
                File *file = mwindow->video_cache->check_out(asset, mwindow->edl, 1);
                if( !file ) return 0;
                if( temp && (temp->get_w() != asset->width || temp->get_h() != asset->height) ) {
@@ -138,6 +172,120 @@ int AssetVIcon::get_vy()
        return lbox->get_item_y(picon) + lbox->get_title_h();
 }
 
+void AssetVIcon::load_audio()
+{
+       MWindow *mwindow = picon->mwindow;
+       Asset *asset = (Asset *)picon->indexable;
+       File *file = mwindow->audio_cache->check_out(asset, mwindow->edl, 1);
+       int channels = asset->get_audio_channels();
+       if( channels > 2 ) channels = 2;
+       int sample_rate = asset->get_sample_rate();
+       int bfrsz = sample_rate;
+       Samples samples(bfrsz);
+       double time_scale = (double)sample_rate / VICON_SAMPLE_RATE;
+       vicon_audio_t *audio_data = (vicon_audio_t *)this->audio_data;
+       static const int mx = (1<<(8*sizeof(*audio_data)-1)) - 1;
+       double sample_scale = (double)mx / channels;
+       int audio_pos = 0, audio_len = audio_size/sizeof(vicon_audio_t);
+       while( audio_pos < audio_len ) {
+               int64_t pos = audio_pos * time_scale;
+               for( int ch=0; ch<channels; ++ch ) {
+                       file->set_channel(ch);
+                       file->set_audio_position(pos);
+                       file->read_samples(&samples, bfrsz);
+                       double *data = samples.get_data();
+                       for( int64_t k=audio_pos; k<audio_len; ++k ) {
+                               int i = k * time_scale - pos;
+                               if( i >= bfrsz ) break;
+                               int v = audio_data[k] + data[i] * sample_scale;
+                               audio_data[k] = CLIP(v, -mx,mx);
+                       }
+               }
+               audio_pos = (pos + bfrsz) / time_scale;
+       }
+       mwindow->audio_cache->check_in(asset);
+}
+
+
+AssetVIconAudio::AssetVIconAudio(AWindowGUI *gui)
+ : Thread(1, 0, 0)
+{
+       this->gui = gui;
+       audio = new AudioDevice(gui->mwindow);
+       interrupted = 0;
+       vicon = 0;
+}
+AssetVIconAudio::~AssetVIconAudio()
+{
+       delete audio;
+}
+
+void AssetVIconAudio::run()
+{
+       int channels = 2;
+       int64_t bfrsz = VICON_SAMPLE_RATE;
+       MWindow *mwindow = gui->mwindow;
+       EDL *edl = mwindow->edl;
+       EDLSession *session = edl->session;
+       AudioOutConfig *aconfig = session->playback_config->aconfig;
+       audio->open_output(aconfig, VICON_SAMPLE_RATE, bfrsz, channels, 0);
+       audio->start_playback();
+       double out0[bfrsz], out1[bfrsz], *out[2] = { out0, out1 };
+       vicon_audio_t *audio_data = (vicon_audio_t *)vicon->audio_data;
+       static const int mx = (1<<(8*sizeof(*audio_data)-1)) - 1;
+
+       int audio_len = vicon->audio_size/sizeof(vicon_audio_t);
+       while( !interrupted ) {
+               int len = audio_len - audio_pos;
+               if( len <= 0 ) break;
+               if( len > bfrsz ) len = bfrsz;
+               int k = audio_pos;
+               for( int i=0; i<len; ++i,++k )
+                       out0[i] = out1[i] = (double)audio_data[k] / mx;
+               audio_pos = k;
+               audio->write_buffer(out, channels, len);
+       }
+
+       if( !interrupted )
+               audio->set_last_buffer();
+       audio->stop_audio(interrupted ? 0 : 1);
+       audio->close_all();
+}
+
+void AssetVIconAudio::start(AssetVIcon *vicon)
+{
+       if( running() ) return;
+       interrupted = 0;
+       audio_pos = 0;
+       this->vicon = vicon;
+       Thread::start();
+}
+
+void AssetVIconAudio::stop(int wait)
+{
+       if( running() && !interrupted ) {
+               interrupted = 1;
+               audio->stop_audio(wait);
+       }
+       Thread::join();
+       if( vicon ) {
+               vicon->playing_audio = 0;
+               vicon = 0;
+       }
+}
+
+void AssetVIcon::start_audio()
+{
+       picon->gui->vicon_audio->stop(0);
+       playing_audio = 1;
+       picon->gui->vicon_audio->start(this);
+}
+
+void AssetVIcon::stop_audio()
+{
+       picon->gui->vicon_audio->stop(0);
+       playing_audio = 0;
+}
 
 AssetPicon::AssetPicon(MWindow *mwindow,
        AWindowGUI *gui, Indexable *indexable)
@@ -211,6 +359,52 @@ AssetPicon::~AssetPicon()
        }
 }
 
+void AssetPicon::draw_hue_bar(VFrame *frame, double duration)
+{
+       float t = duration > 1 ? (log(duration) / log(3600.f)) : 0;
+       if( t > 1 ) t = 1;
+       float h = 300 * t, s = 1., v = 1.;
+       float r, g, b; // duration, 0..1hr == hue red..magenta
+       HSV::hsv_to_rgb(r,g,b, h,s,v);
+       int ih = frame->get_h()/8, iw = frame->get_w();
+       int ir = r * 256;  CLAMP(ir, 0,255);
+       int ig = g * 256;  CLAMP(ig, 0,255);
+       int ib = b * 256;  CLAMP(ib, 0,255);
+       unsigned char **rows = frame->get_rows();
+       for( int y=0; y<ih; ++y ) {
+               unsigned char *rp = rows[y];
+               for( int x=0; x<iw; rp+=3,++x ) {
+                       rp[0] = ir;  rp[1] = ig;  rp[2] = ib;
+               }
+       }
+}
+
+void AssetPicon::draw_wave(VFrame *frame, double *dp, int len, int base_color, int line_color)
+{
+       int w = frame->get_w(), h = frame->get_h();
+       int h1 = h-1, h2 = h/2, y = h2;
+       int rgb_color = frame->pixel_rgb;
+       for( int x=0,x1=0,x2=0; x<w; ++x,x1=x2 ) {
+               double min = *dp, max = min;
+               x2 = (len * (x+1))/w;
+               for( int i=x1; i<x2; ++i ) {
+                       double value = *dp++;
+                       if( value < min ) min = value;
+                       if( value > max ) max = value;
+               }
+               int ctr = (min + max) / 2;
+               int y0 = (int)(h2 - ctr*h2);  CLAMP(y0, 0,h1);
+               int y1 = (int)(h2 - min*h2);  CLAMP(y1, 0,h1);
+               int y2 = (int)(h2 - max*h2);  CLAMP(y2, 0,h1);
+               frame->pixel_rgb = line_color;
+               frame->draw_line(x,y1, x,y2);
+               frame->pixel_rgb = base_color;
+               frame->draw_line(x,y, x,y0);
+               y = y0;
+       }
+       frame->pixel_rgb = rgb_color;
+}
+
 void AssetPicon::reset()
 {
        plugin = 0;
@@ -227,6 +421,33 @@ void AssetPicon::reset()
        persistent = 0;
 }
 
+void AssetPicon::open_render_engine(EDL *edl, int is_audio)
+{
+       TransportCommand command;
+       command.command = is_audio ? NORMAL_FWD : CURRENT_FRAME;
+       command.get_edl()->copy_all(edl);
+       command.change_type = CHANGE_ALL;
+       command.realtime = 0;
+       render_engine = new RenderEngine(0, mwindow->preferences, 0, 0);
+       render_engine->set_vcache(mwindow->video_cache);
+       render_engine->set_acache(mwindow->audio_cache);
+       render_engine->arm_command(&command);
+}
+void AssetPicon::close_render_engine()
+{
+       delete render_engine;  render_engine = 0;
+}
+void AssetPicon::render_video(int64_t pos, VFrame *vfrm)
+{
+       if( !render_engine || !render_engine->vrender ) return;
+       render_engine->vrender->process_buffer(vfrm, pos, 0);
+}
+void AssetPicon::render_audio(int64_t pos, Samples **samples, int len)
+{
+       if( !render_engine || !render_engine->arender ) return;
+       render_engine->arender->process_buffer(samples, len, pos);
+}
+
 void AssetPicon::create_objects()
 {
        FileSystem fs;
@@ -239,6 +460,10 @@ void AssetPicon::create_objects()
                fs.extract_name(name, indexable->path);
                set_text(name);
        }
+       else if( edl ) {
+               set_text(strcpy(name, edl->local_session->clip_title));
+               set_text(name);
+       }
 
        if( indexable && indexable->is_asset ) {
                Asset *asset = (Asset*)indexable;
@@ -276,7 +501,7 @@ void AssetPicon::create_objects()
                                        file->read_frame(gui->temp_picon);
                                        mwindow->video_cache->check_in(asset);
 
-                                       gui->lock_window("AssetPicon::create_objects 1");
+                                       gui->lock_window("AssetPicon::create_objects 0");
                                        icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
                                        icon->draw_vframe(gui->temp_picon,
                                                0, 0, pixmap_w, pixmap_h, 0, 0);
@@ -292,6 +517,13 @@ void AssetPicon::create_objects()
                                                if( secs > 5 ) secs = 5;
                                                int64_t length = secs * gui->vicon_thread->refresh_rate;
                                                vicon = new AssetVIcon(this, pixmap_w, pixmap_h, framerate, length);
+                                               if( asset->audio_data && secs > 0 ) {
+                                                       gui->unlock_window();
+                                                       int audio_len = VICON_SAMPLE_RATE*secs+0.5;
+                                                       vicon->init_audio(audio_len*sizeof(vicon_audio_t));
+                                                       vicon->load_audio();
+                                                       gui->lock_window("AssetPicon::create_objects 1");
+                                               }
                                                gui->vicon_thread->add_vicon(vicon);
                                        }
 
@@ -309,8 +541,65 @@ void AssetPicon::create_objects()
                }
                else
                if( asset->audio_data ) {
-                       icon = gui->audio_icon;
-                       icon_vframe = gui->audio_vframe;
+                       if( mwindow->preferences->use_thumbnails ) {
+                               gui->unlock_window();
+                               File *file = mwindow->audio_cache->check_out(asset,
+                                       mwindow->edl,
+                                       1);
+                               if( file ) {
+                                       pixmap_w = pixmap_h * 16/9;
+                                       icon_vframe = new VFrame(0,
+                                               -1, pixmap_w, pixmap_h, BC_RGB888, -1);
+                                       { char string[BCTEXTLEN];
+                                       sprintf(string, _("Reading %s"), name);
+                                       mwindow->gui->lock_window("AssetPicon::create_objects 3");
+                                       mwindow->gui->show_message(string);
+                                       mwindow->gui->unlock_window(); }
+                                       int sample_rate = asset->get_sample_rate();
+                                       int channels = asset->get_audio_channels();
+                                       if( channels > 2 ) channels = 2;
+                                       int64_t audio_samples = asset->get_audio_samples();
+                                       double duration = (double)audio_samples / sample_rate;
+                                       draw_hue_bar(icon_vframe, duration);
+                                       int bfrsz = sample_rate;
+                                       Samples samples(bfrsz);
+                                       static int line_colors[2] = { GREEN, YELLOW };
+                                       static int base_colors[2] = { RED, PINK };
+                                       for( int i=channels; --i>=0; ) {
+                                               file->set_channel(i);
+                                               file->set_audio_position(0);
+                                               file->read_samples(&samples, bfrsz);
+                                               draw_wave(icon_vframe, samples.get_data(), bfrsz,
+                                                       base_colors[i], line_colors[i]);
+                                       }
+                                       mwindow->audio_cache->check_in(asset);
+                                       if( asset->awindow_folder == AW_MEDIA_FOLDER ) {
+                                               double secs = duration;
+                                               if( secs > 5 ) secs = 5;
+                                               double refresh_rate = gui->vicon_thread->refresh_rate;
+                                               int64_t length = secs * refresh_rate;
+                                               vicon = new AssetVIcon(this, pixmap_w, pixmap_h, refresh_rate, length);
+                                               int audio_len = VICON_SAMPLE_RATE*secs+0.5;
+                                               vicon->init_audio(audio_len*sizeof(vicon_audio_t));
+                                               vicon->load_audio();
+                                               gui->vicon_thread->add_vicon(vicon);
+                                       }
+                                       gui->lock_window("AssetPicon::create_objects 4");
+                                       icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
+                                       icon->draw_vframe(icon_vframe,
+                                               0, 0, pixmap_w, pixmap_h, 0, 0);
+                               }
+                               else {
+                                       gui->lock_window("AssetPicon::create_objects 5");
+                                       icon = gui->audio_icon;
+                                       icon_vframe = gui->audio_vframe;
+                               }
+                       }
+                       else {
+                               icon = gui->audio_icon;
+                               icon_vframe = gui->audio_vframe;
+                       }
+
                }
                struct stat st;
                mtime = !stat(asset->path, &st) ? st.st_mtime : 0;
@@ -322,13 +611,122 @@ void AssetPicon::create_objects()
        }
        else
        if( edl ) {
-               set_text(strcpy(name, edl->local_session->clip_title));
-               icon = gui->clip_icon;
-               icon_vframe = gui->clip_vframe;
+               if( edl->tracks->playable_video_tracks() ) {
+                       if( mwindow->preferences->use_thumbnails ) {
+                               gui->unlock_window();
+                               char clip_icon_path[BCTEXTLEN];
+                               char *clip_icon = edl->local_session->clip_icon;
+                               if( clip_icon[0] ) {
+                                       snprintf(clip_icon_path, sizeof(clip_icon_path),
+                                               "%s/%s", File::get_config_path(), clip_icon);
+                                       icon_vframe = VFramePng::vframe_png(clip_icon_path);
+                               }
+                               if( !icon_vframe ) {
+//printf("render clip: %s\n", name);
+                                       int edl_h = edl->get_h(), edl_w = edl->get_w();
+                                       int height = edl_h > 0 ? edl_h : 1;
+                                       int width = edl_w > 0 ? edl_w : 1;
+                                       int color_model = edl->session->color_model;
+                                       pixmap_w = pixmap_h * width / height;
+
+                                       if( gui->temp_picon &&
+                                           (gui->temp_picon->get_color_model() != color_model ||
+                                            gui->temp_picon->get_w() != width ||
+                                            gui->temp_picon->get_h() != height) ) {
+                                               delete gui->temp_picon;  gui->temp_picon = 0;
+                                       }
+
+                                       if( !gui->temp_picon ) {
+                                               gui->temp_picon = new VFrame(0, -1,
+                                                       width, height, color_model, -1);
+                                       }
+                                       char string[BCTEXTLEN];
+                                       sprintf(string, _("Rendering %s"), name);
+                                       mwindow->gui->lock_window("AssetPicon::create_objects");
+                                       mwindow->gui->show_message(string);
+                                       mwindow->gui->unlock_window();
+                                       open_render_engine(edl, 0);
+                                       render_video(0, gui->temp_picon);
+                                       close_render_engine();
+                                       gui->lock_window("AssetPicon::create_objects 0");
+                                       icon_vframe = new VFrame(0,
+                                               -1, pixmap_w, pixmap_h, BC_RGB888, -1);
+                                       icon_vframe->transfer_from(gui->temp_picon);
+                                       if( clip_icon[0] ) icon_vframe->write_png(clip_icon_path);
+                               }
+                               else {
+                                       pixmap_w = icon_vframe->get_w();
+                                       pixmap_h = icon_vframe->get_h();
+                               }
+                               icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
+                               icon->draw_vframe(icon_vframe,
+                                       0, 0, pixmap_w, pixmap_h, 0, 0);
+                       }
+                       else {
+                               icon = gui->clip_icon;
+                               icon_vframe = gui->clip_vframe;
+                       }
+               }
+               else
+               if( edl->tracks->playable_audio_tracks() ) {
+                       if( mwindow->preferences->use_thumbnails ) {
+                               gui->unlock_window();
+                               char clip_icon_path[BCTEXTLEN];
+                               char *clip_icon = edl->local_session->clip_icon;
+                               if( clip_icon[0] ) {
+                                       snprintf(clip_icon_path, sizeof(clip_icon_path),
+                                               "%s/%s", File::get_config_path(), clip_icon);
+                                       icon_vframe = VFramePng::vframe_png(clip_icon_path);
+                               }
+                               if( !icon_vframe ) {
+                                       pixmap_w = pixmap_h * 16/9;
+                                       icon_vframe = new VFrame(0,
+                                               -1, pixmap_w, pixmap_h, BC_RGB888, -1);
+                                       char string[BCTEXTLEN];
+                                       sprintf(string, _("Rendering %s"), name);
+                                       mwindow->gui->lock_window("AssetPicon::create_objects 3");
+                                       mwindow->gui->show_message(string);
+                                       mwindow->gui->unlock_window();
+                                       int sample_rate = edl->get_sample_rate();
+                                       int channels = edl->get_audio_channels();
+                                       if( channels > 2 ) channels = 2;
+                                       int64_t audio_samples = edl->get_audio_samples();
+                                       double duration = (double)audio_samples / sample_rate;
+                                       draw_hue_bar(icon_vframe, duration);
+                                       Samples *samples[MAX_CHANNELS];
+                                       int bfrsz = sample_rate;
+                                       for( int i=0; i<MAX_CHANNELS; ++i )
+                                               samples[i] = i<channels ? new Samples(bfrsz) : 0;
+                                       open_render_engine(edl, 1);
+                                       render_audio(0, samples, bfrsz);
+                                       close_render_engine();
+                                       gui->lock_window("AssetPicon::create_objects 4");
+                                       static int line_colors[2] = { GREEN, YELLOW };
+                                       static int base_colors[2] = { RED, PINK };
+                                       for( int i=channels; --i>=0; ) {
+                                               draw_wave(icon_vframe, samples[i]->get_data(), bfrsz,
+                                                       base_colors[i], line_colors[i]);
+                                       }
+                                       for( int i=0; i<channels; ++i ) delete samples[i];
+                                       if( clip_icon[0] ) icon_vframe->write_png(clip_icon_path);
+                               }
+                               else {
+                                       pixmap_w = icon_vframe->get_w();
+                                       pixmap_h = icon_vframe->get_h();
+                               }
+                               icon = new BC_Pixmap(gui, pixmap_w, pixmap_h);
+                               icon->draw_vframe(icon_vframe,
+                                       0, 0, pixmap_w, pixmap_h, 0, 0);
+                       }
+                       else {
+                               icon = gui->clip_icon;
+                               icon_vframe = gui->clip_vframe;
+                       }
+               }
        }
        else
        if( plugin ) {
-               strcpy(name,  plugin->title);
+               strcpy(name, _(plugin->title));
                set_text(name);
                icon_vframe = plugin->get_picon();
                if( icon_vframe )
@@ -441,6 +839,7 @@ AWindowGUI::AWindowGUI(MWindow *mwindow, AWindow *awindow)
        allow_iconlisting = 1;
        remove_plugin = 0;
        vicon_thread = 0;
+       vicon_audio = 0;
        vicon_drawing = 1;
        displayed_folder = AW_NO_FOLDER;
 }
@@ -457,6 +856,7 @@ AWindowGUI::~AWindowGUI()
        displayed_assets[1].remove_all_objects();
 
        delete vicon_thread;
+       delete vicon_audio;
        delete newfolder_thread;
 
        delete asset_menu;
@@ -627,6 +1027,7 @@ void AWindowGUI::create_objects()
 
        vicon_thread = new VIconThread(asset_list);
        vicon_thread->start();
+       vicon_audio = new AssetVIconAudio(this);
 
        add_subwindow(divider = new AWindowDivider(mwindow, this,
                mwindow->theme->adivider_x, mwindow->theme->adivider_y,
@@ -1024,16 +1425,16 @@ void AWindowGUI::update_asset_list()
        }
 
 // Synchronize EDL clips
-       for( int i = 0; i < mwindow->edl->clips.total; i++ ) {
+       for( int i=0; i<mwindow->edl->clips.size(); ++i ) {
                int exists = 0;
 
 // Look for clip in existing listitems
                for( int j = 0; j < assets.total && !exists; j++ ) {
                        AssetPicon *picon = (AssetPicon*)assets.values[j];
 
-                       if( picon->id == mwindow->edl->clips.values[i]->id ) {
-                               picon->edl = mwindow->edl->clips.values[i];
-                               picon->set_text(mwindow->edl->clips.values[i]->local_session->clip_title);
+                       if( picon->id == mwindow->edl->clips[i]->id ) {
+                               picon->edl = mwindow->edl->clips[i];
+                               picon->set_text(mwindow->edl->clips[i]->local_session->clip_title);
                                exists = 1;
                                picon->in_use = 1;
                        }
@@ -1042,16 +1443,14 @@ void AWindowGUI::update_asset_list()
 // Create new listitem
                if( !exists ) {
                        AssetPicon *picon = new AssetPicon(mwindow,
-                               this, mwindow->edl->clips.values[i]);
+                               this, mwindow->edl->clips[i]);
                        picon->create_objects();
                        assets.append(picon);
                }
        }
 
 // Synchronize EDL assets
-       for( Asset *current = mwindow->edl->assets->first;
-               current;
-               current = NEXT ) {
+       for( Asset *current=mwindow->edl->assets->first; current; current=NEXT ) {
                int exists = 0;
 
 // Look for asset in existing listitems
@@ -1060,9 +1459,8 @@ void AWindowGUI::update_asset_list()
 
                        if( picon->id == current->id ) {
                                picon->indexable = current;
-                               exists = 1;
                                picon->in_use = 1;
-                               break;
+                               exists = 1;
                        }
                }
 
@@ -1080,26 +1478,25 @@ void AWindowGUI::update_asset_list()
        mwindow->gui->unlock_window();
 
 // Synchronize nested EDLs
-       for( int i = 0; i < mwindow->edl->nested_edls->size(); i++ ) {
+       for( int i=0; i<mwindow->edl->nested_edls.size(); ++i ) {
                int exists = 0;
-               Indexable *indexable = mwindow->edl->nested_edls->get(i);
+               EDL *nested_edl = mwindow->edl->nested_edls[i];
 
 // Look for asset in existing listitems
-               for( int j = 0; j < assets.total && !exists; j++ ) {
+               for( int j=0; j<assets.total && !exists; ++j ) {
                        AssetPicon *picon = (AssetPicon*)assets.values[j];
 
-                       if( picon->id == indexable->id ) {
-                               picon->indexable = indexable;
-                               exists = 1;
+                       if( picon->id == nested_edl->id ) {
+                               picon->indexable = nested_edl;
                                picon->in_use = 1;
-                               break;
+                               exists = 1;
                        }
                }
 
 // Create new listitem
                if( !exists ) {
                        AssetPicon *picon = new AssetPicon(mwindow,
-                               this, indexable);
+                               this, (Indexable*)nested_edl);
                        picon->create_objects();
                        assets.append(picon);
                }
@@ -1585,12 +1982,10 @@ int AWindowAssets::handle_event()
                mwindow->vwindows.get(DEFAULT_VWINDOW) : 0;
        if( !vwindow || !vwindow->is_running() ) return 1;
 
-       vwindow->gui->lock_window("AWindowAssets::handle_event");
        if( asset_picon->indexable )
                vwindow->change_source(asset_picon->indexable);
        else if( asset_picon->edl )
                vwindow->change_source(asset_picon->edl);
-       vwindow->gui->unlock_window();
        return 1;
 }
 
@@ -1628,7 +2023,8 @@ int AWindowAssets::selection_changed()
                BC_ListBox::deactivate_selection();
                return 1;
        }
-       else if( get_button_down() && get_buttonpress() == 1 &&
+       else if( gui->vicon_drawing &&
+                get_button_down() && get_buttonpress() == 1 &&
                 (item = (AssetPicon*)get_selection(0, 0)) ) {
                VIcon *vicon = 0;
                if( !gui->vicon_thread->viewing ) {