audio wave icons, viewer wave image, 7 lib updates, tweak file size
[goodguy/history.git] / cinelerra-5.1 / cinelerra / awindowgui.C
index 8b3bd1a2f07c36137171489cba3d8feac1c53457..688869130b686fecd21c32db35dc2897ac9d2119 100644 (file)
 #include "assetedit.h"
 #include "assetpopup.h"
 #include "assets.h"
+#include "audiodevice.h"
 #include "awindowgui.h"
 #include "awindow.h"
+#include "bccmodels.h"
 #include "bcsignals.h"
 #include "bchash.h"
 #include "cache.h"
-#include "bccmodels.h"
+#include "cstrdup.h"
+#include "clip.h"
 #include "clippopup.h"
 #include "cursors.h"
 #include "cwindowgui.h"
@@ -52,6 +55,7 @@
 #include "nestededls.h"
 #include "newfolder.h"
 #include "preferences.h"
+#include "samples.h"
 #include "theme.h"
 #include "vframe.h"
 #include "vicon.h"
@@ -95,23 +99,50 @@ 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) ) {
                        delete temp;  temp = 0;
                }
                if( !temp )
-                       temp = new VFrame(asset->width, asset->height, BC_RGB888);
+                       temp = new VFrame(0, -1, asset->width, asset->height, BC_RGB888, -1);
                int ww = picon->gui->vicon_thread->view_w;
                int hh = picon->gui->vicon_thread->view_h;
                while( seq_no >= images.size() ) {
                        file->set_layer(0);
                        int64_t pos = images.size() / picon->gui->vicon_thread->refresh_rate * frame_rate;
                        file->set_video_position(pos,0);
-                       file->read_frame(temp);
+                       if( file->read_frame(temp) ) temp->clear_frame();
                        add_image(temp, ww, hh, BC_RGB8);
                }
                mwindow->video_cache->check_in(asset);
@@ -136,6 +167,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)
@@ -209,6 +354,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;
@@ -220,6 +411,7 @@ void AssetPicon::reset()
        icon_vframe = 0;
        vicon = 0;
        in_use = 1;
+       mtime = 0;
        id = 0;
        persistent = 0;
 }
@@ -273,7 +465,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);
@@ -289,6 +481,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);
                                        }
 
@@ -296,35 +495,93 @@ void AssetPicon::create_objects()
                                else {
                                        gui->lock_window("AssetPicon::create_objects 2");
                                        icon = gui->video_icon;
-                                       icon_vframe = BC_WindowBase::get_resources()->type_to_icon[ICON_FILM];
+                                       icon_vframe = gui->video_vframe;
                                }
                        }
                        else {
                                icon = gui->video_icon;
-                               icon_vframe = BC_WindowBase::get_resources()->type_to_icon[ICON_FILM];
+                               icon_vframe = gui->video_vframe;
                        }
                }
                else
                if( asset->audio_data ) {
-                       icon = gui->audio_icon;
-                       icon_vframe = BC_WindowBase::get_resources()->type_to_icon[ICON_SOUND];
-               }
+                       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;
        }
        else
        if( indexable && !indexable->is_asset ) {
                icon = gui->video_icon;
-               icon_vframe = BC_WindowBase::get_resources()->type_to_icon[ICON_FILM];
+               icon_vframe = gui->video_vframe;
        }
        else
        if( edl ) {
                set_text(strcpy(name, edl->local_session->clip_title));
                icon = gui->clip_icon;
-               icon_vframe = mwindow->theme->get_image("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 )
@@ -372,9 +629,7 @@ void AssetPicon::create_objects()
                              mwindow->edl->session->frames_per_foot);
                set_text(name);
                icon = gui->label_icon;
-               icon_vframe = BC_WindowBase::get_resources()->type_to_icon[ICON_LABEL];
-               set_icon(icon);
-               set_icon_vframe(icon_vframe);
+               icon_vframe = gui->label_vframe;
        }
        if( !icon ) {
                icon = gui->file_icon;
@@ -435,9 +690,11 @@ AWindowGUI::AWindowGUI(MWindow *mwindow, AWindow *awindow)
        labellist_menu = 0;
        folderlist_menu = 0;
        temp_picon = 0;
+       search_text = 0;
        allow_iconlisting = 1;
        remove_plugin = 0;
        vicon_thread = 0;
+       vicon_audio = 0;
        vicon_drawing = 1;
        displayed_folder = AW_NO_FOLDER;
 }
@@ -454,6 +711,7 @@ AWindowGUI::~AWindowGUI()
        displayed_assets[1].remove_all_objects();
 
        delete vicon_thread;
+       delete vicon_audio;
        delete newfolder_thread;
 
        delete asset_menu;
@@ -464,6 +722,7 @@ AWindowGUI::~AWindowGUI()
        delete cliplist_menu;
        delete labellist_menu;
        delete folderlist_menu;
+       delete search_text;
        delete temp_picon;
        delete remove_plugin;
 
@@ -613,12 +872,17 @@ void AWindowGUI::create_objects()
        mwindow->theme->get_awindow_sizes(this);
        load_defaults(mwindow->defaults);
 
-       add_subwindow(asset_list = new AWindowAssets(mwindow, this,
-               mwindow->theme->alist_x, mwindow->theme->alist_y,
-               mwindow->theme->alist_w, mwindow->theme->alist_h));
+       int x1 = mwindow->theme->alist_x, y1 = mwindow->theme->alist_y;
+       int w1 = mwindow->theme->alist_w, h1 = mwindow->theme->alist_h;
+       search_text = new AWindowSearchText(mwindow, this, x1, y1+5);
+       search_text->create_objects();
+       int dy = search_text->get_h() + 10;
+       y1 += dy;  h1 -= dy;
+       add_subwindow(asset_list = new AWindowAssets(mwindow, this, x1, y1, w1, h1));
 
        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,
@@ -713,9 +977,12 @@ int AWindowGUI::translation_event()
 
 void AWindowGUI::reposition_objects()
 {
-       asset_list->reposition_window(
-               mwindow->theme->alist_x, mwindow->theme->alist_y,
-               mwindow->theme->alist_w, mwindow->theme->alist_h);
+       int x1 = mwindow->theme->alist_x, y1 = mwindow->theme->alist_y;
+       int w1 = mwindow->theme->alist_w, h1 = mwindow->theme->alist_h;
+       search_text->reposition_window(x1, y1+5, w1);
+       int dy = search_text->get_h() + 10;
+       y1 += dy;  h1 -= dy;
+       asset_list->reposition_window(x1, y1, w1, h1);
        divider->reposition_window(
                mwindow->theme->adivider_x, mwindow->theme->adivider_y,
                mwindow->theme->adivider_w, mwindow->theme->adivider_h);
@@ -827,6 +1094,7 @@ void AWindowRemovePlugin::handle_close_event(int result)
 {
        if( !result ) {
                printf(_("remove %s\n"), plugin->path);
+               awindow->gui->lock_window("AWindowRemovePlugin::handle_close_event");
                ArrayList<BC_ListBoxItem*> *folder =
                        plugin->audio ? plugin->transition ?
                                &awindow->gui->atransitions :
@@ -836,14 +1104,14 @@ void AWindowRemovePlugin::handle_close_event(int result)
                                &awindow->gui->veffects :
                        0;
                if( folder ) remove_plugin(plugin, *folder);
+               MWindow *mwindow = awindow->mwindow;
+               awindow->gui->unlock_window();
                char plugin_path[BCTEXTLEN];
                strcpy(plugin_path, plugin->path);
-               MWindow *mwindow = awindow->mwindow;
                mwindow->plugindb->remove(plugin);
                remove(plugin_path);
                char index_path[BCTEXTLEN];
-               snprintf(index_path, sizeof(index_path), "%s/%s",
-                       mwindow->preferences->plugin_dir, PLUGIN_FILE);
+               mwindow->create_defaults_path(index_path, PLUGIN_FILE);
                remove(index_path);
                char picon_path[BCTEXTLEN];
                FileSystem fs;
@@ -935,7 +1203,6 @@ void AWindowGUI::async_update_assets()
 
 void AWindowGUI::update_folder_list()
 {
-       stop_vicon_drawing();
        for( int i = 0; i < folders.total; i++ ) {
                AssetPicon *picon = (AssetPicon*)folders.values[i];
                picon->in_use = 0;
@@ -973,8 +1240,6 @@ void AWindowGUI::update_folder_list()
                        folders.remove_number(i);
                }
        }
-
-       start_vicon_drawing();
 }
 
 void AWindowGUI::create_persistent_folder(ArrayList<BC_ListBoxItem*> *output,
@@ -1101,7 +1366,11 @@ void AWindowGUI::update_asset_list()
                if( !picon->in_use ) {
                        delete picon;
                        assets.remove_number(i);
+                       continue;
                }
+               if( !picon->indexable || !picon->indexable->is_asset ) continue;
+               struct stat st;
+               picon->mtime = !stat(picon->indexable->path, &st) ? st.st_mtime : 0;
        }
 }
 
@@ -1128,7 +1397,7 @@ void AWindowGUI::update_picon(Indexable *indexable)
        }
 }
 
-void AWindowGUI::sort_assets()
+void AWindowGUI::sort_assets(int use_mtime)
 {
        switch( mwindow->edl->session->awindow_folder ) {
        case AW_AEFFECT_FOLDER:
@@ -1147,7 +1416,7 @@ void AWindowGUI::sort_assets()
                sort_picons(&labellist);
                break;
        default:
-               sort_picons(&assets);
+               sort_picons(&assets, use_mtime);
        }
 // reset xyposition
        asset_list->update_format(asset_list->get_format(), 0);
@@ -1189,6 +1458,10 @@ void AWindowGUI::copy_picons(ArrayList<BC_ListBoxItem*> *dst,
                if( folder < 0 ||
                    (picon->indexable && picon->indexable->awindow_folder == folder) ||
                    (picon->edl && picon->edl->local_session->awindow_folder == folder) ) {
+                       const char *text = search_text->get_text();
+                       int hidden = text && text[0] && !bstrcasestr(picon->get_text(), text);
+                       if( picon->vicon ) picon->vicon->hidden = hidden;
+                       if( hidden ) continue;
                        BC_ListBoxItem *item2, *item1;
                        dst[0].append(item1 = picon);
                        if( picon->edl )
@@ -1196,34 +1469,45 @@ void AWindowGUI::copy_picons(ArrayList<BC_ListBoxItem*> *dst,
                        else
                        if( picon->label && picon->label->textstr )
                                dst[1].append(item2 = new BC_ListBoxItem(picon->label->textstr));
+                       else if( picon->mtime ) {
+                               char date_time[BCSTRLEN];
+                               struct tm stm;  localtime_r(&picon->mtime, &stm);
+                               sprintf(date_time,"%04d.%02d.%02d %02d:%02d:%02d",
+                                        stm.tm_year+1900, stm.tm_mon+1, stm.tm_mday,
+                                        stm.tm_hour, stm.tm_min, stm.tm_sec);
+                               dst[1].append(item2 = new BC_ListBoxItem(date_time));
+                       }
                        else
                                dst[1].append(item2 = new BC_ListBoxItem(""));
-                       item1->set_autoplace_text(1);
-                       item2->set_autoplace_text(1);
+                       item1->set_autoplace_text(1);  item1->set_autoplace_icon(1);
+                       item2->set_autoplace_text(1);  item2->set_autoplace_icon(1);
                }
        }
 }
 
-void AWindowGUI::sort_picons(ArrayList<BC_ListBoxItem*> *src)
+void AWindowGUI::sort_picons(ArrayList<BC_ListBoxItem*> *src, int use_mtime)
 {
-       int done = 0;
-       while(!done)
-       {
+       int done = 0, changed = 0;
+       while( !done ) {
                done = 1;
-               for( int i = 0; i < src->total - 1; i++ ) {
-                       BC_ListBoxItem *item1 = src->values[i];
-                       BC_ListBoxItem *item2 = src->values[i + 1];
-                       item1->set_autoplace_icon(1);
-                       item2->set_autoplace_icon(1);
-                       item1->set_autoplace_text(1);
-                       item2->set_autoplace_text(1);
-                       if( strcmp(item1->get_text(), item2->get_text()) > 0 ) {
+               for( int i=0; i<src->total-1; ++i ) {
+                       AssetPicon *item1 = (AssetPicon *)src->values[i];
+                       AssetPicon *item2 = (AssetPicon *)src->values[i + 1];
+                       if( use_mtime ? item1->mtime > item2->mtime :
+                           strcmp(item1->get_text(), item2->get_text()) > 0 ) {
                                src->values[i + 1] = item1;
                                src->values[i] = item2;
-                               done = 0;
+                               done = 0;  changed = 1;
                        }
                }
        }
+       if( changed ) {
+               for( int i=0; i<src->total; ++i ) {
+                       AssetPicon *item = (AssetPicon *)src->values[i];
+                       item->set_autoplace_icon(1);
+                       item->set_autoplace_text(1);
+               }
+       }
 }
 
 
@@ -1269,10 +1553,14 @@ void AWindowGUI::filter_displayed_assets()
 
 void AWindowGUI::update_assets()
 {
+       stop_vicon_drawing();
        update_folder_list();
        update_asset_list();
        labellist.remove_all_objects();
        create_label_folder();
+
+       if( displayed_folder != mwindow->edl->session->awindow_folder )
+               search_text->clear();
        filter_displayed_assets();
 
        if( mwindow->edl->session->folderlist_format != folder_list->get_format() ) {
@@ -1297,6 +1585,7 @@ void AWindowGUI::update_assets()
        asset_list->center_selection();
 
        flush();
+       start_vicon_drawing();
        return;
 }
 
@@ -1527,7 +1816,7 @@ int AWindowAssets::button_press_event()
                        break;
                case AW_MEDIA_FOLDER:
                case AW_PROXY_FOLDER:
-                       gui->assetlist_menu->update_titles();
+                       gui->assetlist_menu->update_titles(folder==AW_MEDIA_FOLDER);
                        gui->assetlist_menu->activate_menu();
                        break;
                }
@@ -1552,12 +1841,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;
 }
 
@@ -1595,7 +1882,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 ) {
@@ -1734,11 +2022,13 @@ int AWindowAssets::drag_stop_event()
 
        lock_window("AWindowAssets::drag_stop_event");
 
-       if( result ) get_drag_popup()->set_animation(0);
+       if( result )
+               get_drag_popup()->set_animation(0);
 
        BC_ListBox::drag_stop_event();
-       mwindow->session->current_operation = ::NO_OPERATION; // since NO_OPERATION is also defined in listbox, we have to reach for global scope...
-       return 0;
+// since NO_OPERATION is also defined in listbox, we have to reach for global scope...
+       mwindow->session->current_operation = ::NO_OPERATION;
+       return 1;
 }
 
 int AWindowAssets::column_resize_event()
@@ -1760,16 +2050,68 @@ int AWindowAssets::focus_out_event()
        return BC_ListBox::focus_out_event();
 }
 
+AWindowSearchTextBox::AWindowSearchTextBox(AWindowSearchText *search_text, int x, int y, int w)
+ : BC_TextBox(x, y, w, 1, "")
+{
+       this->search_text = search_text;
+}
 
+int AWindowSearchTextBox::handle_event()
+{
+       return search_text->handle_event();
+}
 
+AWindowSearchText::AWindowSearchText(MWindow *mwindow, AWindowGUI *gui, int x, int y)
+{
+       this->mwindow = mwindow;
+       this->gui = gui;
+       this->x = x;
+       this->y = y;
+}
 
+void AWindowSearchText::create_objects()
+{
+       int x1 = x, y1 = y, margin = 10;
+       gui->add_subwindow(text_title = new BC_Title(x1, y1, _("Search:")));
+       x1 += text_title->get_w() + margin;
+       int w1 = gui->get_w() - x1 - 2*margin;
+       gui->add_subwindow(text_box = new AWindowSearchTextBox(this, x1, y1, w1));
+}
 
+int AWindowSearchText::handle_event()
+{
+       gui->async_update_assets();
+       return 1;
+}
 
+int AWindowSearchText::get_w()
+{
+       return text_box->get_w() + text_title->get_w() + 10;
+}
 
+int AWindowSearchText::get_h()
+{
+       return bmax(text_box->get_h(),text_title->get_h());
+}
 
+void AWindowSearchText::reposition_window(int x, int y, int w)
+{
+       int x1 = x, y1 = y, margin = 10;
+       text_title->reposition_window(x1, y1);
+       x1 += text_title->get_w() + margin;
+       int w1 = gui->get_w() - x1 - 2*margin;
+       text_box->reposition_window(x1, y1, w1);
+}
 
+const char *AWindowSearchText::get_text()
+{
+       return text_box->get_text();
+}
 
-
+void AWindowSearchText::clear()
+{
+       text_box->update("");
+}
 
 AWindowNewFolder::AWindowNewFolder(MWindow *mwindow, AWindowGUI *gui, int x, int y)
  : BC_Button(x, y, mwindow->theme->newbin_data)
@@ -2052,7 +2394,7 @@ AWindowListSort::AWindowListSort(MWindow *mwindow, AWindowGUI *gui)
 
 int AWindowListSort::handle_event()
 {
-       gui->sort_assets();
+       gui->sort_assets(0);
        return 1;
 }