undo fixes, viewer change_source rework, cleanup
[goodguy/history.git] / cinelerra-5.1 / cinelerra / awindowgui.C
index ec03790af2e61e640fa64a9ecaaa3864ef1e2066..6c640328cede9d75f341acc32df6b8dc1f0b87a1 100644 (file)
@@ -54,6 +54,7 @@
 #include "nestededls.h"
 #include "newfolder.h"
 #include "preferences.h"
+#include "samples.h"
 #include "theme.h"
 #include "vframe.h"
 #include "vicon.h"
@@ -106,14 +107,14 @@ VFrame *AssetVIcon::frame()
                        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);
@@ -211,6 +212,32 @@ AssetPicon::~AssetPicon()
        }
 }
 
+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;
@@ -222,6 +249,7 @@ void AssetPicon::reset()
        icon_vframe = 0;
        vicon = 0;
        in_use = 1;
+       mtime = 0;
        id = 0;
        persistent = 0;
 }
@@ -308,10 +336,72 @@ 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 length = asset->get_audio_samples();
+                                       double duration = (double)length / sample_rate;
+                                       float t = duration > 1 ? (logf(duration) / logf(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 = icon_vframe->get_h()/8, iw = icon_vframe->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 = icon_vframe->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;
+                                               }
+                                       }
+                                       length = sample_rate;
+                                       Samples samples(length);
+                                       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, length);
+                                               draw_wave(icon_vframe, samples.get_data(), length,
+                                                       base_colors[i], line_colors[i]);
+                                       }
+                                       mwindow->audio_cache->check_in(asset);
+                                       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 ) {
@@ -322,11 +412,11 @@ void AssetPicon::create_objects()
        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 )
@@ -1108,7 +1198,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;
        }
 }
 
@@ -1135,7 +1229,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:
@@ -1154,7 +1248,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);
@@ -1207,6 +1301,14 @@ 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);  item1->set_autoplace_icon(1);
@@ -1215,26 +1317,29 @@ void AWindowGUI::copy_picons(ArrayList<BC_ListBoxItem*> *dst,
        }
 }
 
-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);
+               }
+       }
 }
 
 
@@ -1543,7 +1648,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;
                }
@@ -1568,12 +1673,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;
 }
 
@@ -2122,7 +2225,7 @@ AWindowListSort::AWindowListSort(MWindow *mwindow, AWindowGUI *gui)
 
 int AWindowListSort::handle_event()
 {
-       gui->sort_assets();
+       gui->sort_assets(0);
        return 1;
 }