fixup for older distros + updated Preference/About messages
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / mwindow.C
index 8c0dd6c0049d1317bbe0aba96587e53d617589dd..2918e23e63e59e4035bb86e2b07059ea0536fd2e 100644 (file)
@@ -243,6 +243,7 @@ MWindow::MWindow()
        in_destructor = 0;
        speed_edl = 0;
        beeper = 0;
+       redraw_tracks = 0;
        shuttle = 0;
        wintv = 0;
        x10tv = 0;
@@ -265,9 +266,10 @@ MWindow::~MWindow()
 #ifdef HAVE_DVB
        gui->channel_info->stop();
 #endif
-       delete beeper;
-       delete create_bd;       create_bd = 0;
-       delete create_dvd;      create_dvd = 0;
+       delete beeper;          beeper = 0;
+       delete redraw_tracks;   redraw_tracks = 0;
+       delete create_bd;       create_bd = 0;
+       delete create_dvd;      create_dvd = 0;
 #ifdef HAVE_SHUTTLE
        delete shuttle;         shuttle = 0;
 #endif
@@ -294,6 +296,7 @@ MWindow::~MWindow()
        plugin_gui_lock->unlock();
        hide_keyframe_guis();
        clean_indexes();
+       clean_backups();
        save_defaults();
 // Give up and go to a movie
 //  cant run valgrind if this is used
@@ -301,6 +304,7 @@ MWindow::~MWindow()
        gui->del_keyboard_listener(
                (int (BC_WindowBase::*)(BC_WindowBase *))
                &MWindowGUI::keyboard_listener);
+       reset_caches(0);
 #if 0
 // release the hounds
        if( awindow && awindow->gui ) awindow->gui->close(0);
@@ -329,7 +333,6 @@ MWindow::~MWindow()
        gui->close(0);
        join();
 #endif
-       reset_caches();
        dead_plugins->remove_all_objects();
 // must delete theme before destroying plugindb
 //  theme destructor will be deleted by delete_plugins
@@ -347,8 +350,9 @@ MWindow::~MWindow()
        delete gui;             gui = 0;
        delete mainindexes;     mainindexes = 0;
        delete mainprogress;    mainprogress = 0;
-       delete audio_cache;     audio_cache = 0;  // delete the cache after the assets
-       delete video_cache;     video_cache = 0;  // delete the cache after the assets
+ // delete the caches after the assets
+       if( audio_cache ) { audio_cache->remove_user();  audio_cache = 0; }
+       if( video_cache ) { video_cache->remove_user();  video_cache = 0; }
        delete frame_cache;     frame_cache = 0;
        delete wave_cache;      wave_cache = 0;
        delete plugin_guis;     plugin_guis = 0;
@@ -503,6 +507,13 @@ void MWindow::init_defaults(BC_Hash* &defaults, char *config_path)
 
 void MWindow::check_language()
 {
+       char pref_locale[BCSTRLEN];
+       strcpy(pref_locale, DEFAULT_LOCALE);
+       defaults->get("LOCALE",pref_locale);
+// set LANGUAGE if pref locale != sys
+       if( strcmp(pref_locale, DEFAULT_LOCALE) )
+               setenv("LANGUAGE", pref_locale, 1);
+
        char curr_lang[BCTEXTLEN]; curr_lang[0] = 0;
        const char *env_lang = getenv("LANGUAGE");
        if( !env_lang ) env_lang = getenv("LC_ALL");
@@ -974,6 +985,58 @@ void MWindow::init_preferences()
        YUV::yuv.yuv_set_colors(preferences->yuv_color_space, preferences->yuv_color_range);
 }
 
+void MWindow::clean_backups()
+{
+    FileSystem fs;
+    int total_excess;
+    long oldest = 0;
+    int oldest_item = -1;
+    char string[BCTEXTLEN];
+
+// Delete extra backups
+    fs.set_filter("backup*.prev_*");
+    fs.complete_path(preferences->index_directory);
+    fs.update(preferences->index_directory);
+
+    // set to 50 for now
+    // total_excess = fs.dir_list.total - preferences->index_count;
+    total_excess = fs.dir_list.total - 50;
+    printf("Total excess of backups: %i \n", total_excess);
+
+//printf("MWindow::clean_backups 1 %d\n", fs.dir_list.total);
+
+    while(total_excess > 0)
+    {
+// Get oldest
+       for(int i = 0; i < fs.dir_list.total; i++)
+       {
+           fs.join_names(string, preferences->index_directory, fs.dir_list[i]->name);
+
+           if(i == 0 || fs.get_date(string) <= oldest)
+           {
+               oldest = fs.get_date(string);
+               oldest_item = i;
+           }
+       }
+
+       if(oldest_item >= 0)
+       {
+// Remove backup file
+           fs.join_names(string,
+               preferences->index_directory,
+               fs.dir_list[oldest_item]->name);
+//printf("MWindow::clean_backups 1 %s\n", string);
+           if(remove(string))
+               perror("delete_backups");
+           delete fs.dir_list[oldest_item];
+           fs.dir_list.remove_number(oldest_item);
+
+       }
+
+       total_excess--;
+    }
+}
+
 void MWindow::clean_indexes()
 {
        FileSystem fs;
@@ -1210,10 +1273,23 @@ ZWindow *MWindow::get_mixer(Mixer *&mixer)
        return zwindow;
 }
 
-void MWindow::del_mixer(ZWindow *zwindow)
+ZWindow *MWindow::get_mixer(int idx)
+{
+       ZWindow *zwindow = 0;
+       zwindows_lock->lock("MWindow::get_mixer");
+       for( int i=0; !zwindow && i<zwindows.size(); ++i ) {
+               ZWindow *zwdw = zwindows[i];
+               if( !zwdw->running() ) continue;
+               if( zwdw->idx != idx ) continue;
+               zwindow = zwindows[i];
+       }
+       zwindows_lock->unlock();
+       return zwindow;
+}
+
+void MWindow::close_mixer(ZWindow *zwindow)
 {
-       zwindows_lock->lock("MWindow::del_mixer 0");
-       edl->mixers.del_mixer(zwindow->idx);
+       zwindows_lock->lock("MWindow::close_mixer 0");
        if( session->selected_zwindow >= 0 ) {
                int i = zwindows.number_of(zwindow);
                if( i >= 0 && i < session->selected_zwindow )
@@ -1222,7 +1298,7 @@ void MWindow::del_mixer(ZWindow *zwindow)
                        session->selected_zwindow = -1;
        }
        zwindows_lock->unlock();
-       gui->lock_window("MWindow::del_mixer 1");
+       gui->lock_window("MWindow::close_mixer 1");
        gui->update_mixers(0, -1);
        gui->unlock_window();
 }
@@ -1297,11 +1373,11 @@ void MWindow::handle_mixers(EDL *edl, int command, int wait_tracking,
                        k = mixer->mixer_ids.size();
                        while( --k >= 0 && track->get_mixer_id() != mixer->mixer_ids[k] );
                        if( k >= 0 ) {
-                               track->record = 1;
+                               track->armed = 1;
                                track->play = track->data_type == TRACK_VIDEO ? 1 : 0;
                        }
                        else
-                               track->record = track->play = 0;
+                               track->armed = track->play = 0;
                }
                zwindow->change_source(mixer_edl);
                zwindow->handle_mixer(command, 0,
@@ -1356,7 +1432,7 @@ ZWindow *MWindow::create_mixer(Indexable *indexable, double position)
        Mixer *mixer = 0;
        ZWindow *zwindow = get_mixer(mixer);
        while( track ) {
-               track->play = track->record = 0;
+               track->play = track->armed = 0;
                if( track->data_type == TRACK_VIDEO ) {
                        sprintf(track->title, _("Mixer %d"), zwindow->idx);
                }
@@ -1446,6 +1522,27 @@ int MWindow::select_zwindow(ZWindow *zwindow)
 
 void MWindow::tile_mixers()
 {
+       int x1 = session->tile_mixers_x;
+       int y1 = session->tile_mixers_y;
+       int x2 = x1 + session->tile_mixers_w;
+       int y2 = y1 + session->tile_mixers_h;
+       tile_mixers(x1, y1, x2, y2);
+}
+
+void MWindow::tile_mixers(int x1, int y1, int x2, int y2)
+{
+       if( x1 == x2 || y1 == y2 ) {
+// use top left quadrent
+               int sw = gui->get_screen_w(1, -1);
+               int sh = gui->get_screen_w(1, -1);
+               x1 = 1;     y1 = 1;
+               x2 = sw/2;  y2 = sh/2;
+       }
+       else {
+               if( x1 > x2 ) { int t = x1;  x1 = x2;  x2 = t; }
+               if( y1 > y2 ) { int t = y1;  y1 = y2;  y2 = t; }
+       }
+       int ow = edl->session->output_w, oh = edl->session->output_h;
        int nz = 0;
        for( int i=0; i<zwindows.size(); ++i ) {
                ZWindow *zwindow = zwindows[i];
@@ -1453,34 +1550,63 @@ void MWindow::tile_mixers()
                ++nz;
        }
        if( !nz ) return;
-       int zn = ceil(sqrt(nz));
-       int x1 = 1 + gui->get_x(), x2 = cwindow->gui->get_x();
-       int y1 = 1, y2 = gui->get_y();
-       int rw = gui->get_root_w(0), rh = gui->get_root_h(0);
-       if( x1 < 0 ) x1 = 0;
-       if( y1 < 0 ) y1 = 0;
-       if( x2 > rw ) x2 = rw;
-       if( y2 > rh ) y2 = rh;
-       int dx = x2 - x1, dy = y2 - y1;
-       int zw = dx / zn;
        int lt = BC_DisplayInfo::get_left_border();
        int top = BC_DisplayInfo::get_top_border();
        int bw = lt + BC_DisplayInfo::get_right_border();  // borders
        int bh = top + BC_DisplayInfo::get_bottom_border();
-       int zx = 0, zy = 0;  // window origins
        int mw = xS(10+10), mh = yS(10+10); // canvas margins
-       int rsz = 0, n = 0, dz = 0;
-       int ow = edl->session->output_w, oh = edl->session->output_h;
+       int dx = x2 - x1, dy = y2 - y1;
+       int64_t sz = dx * dy, best_r = sz;
+       int bx = 1, by = nz;
+       for( int nx=1; nx<=nz; ++nx ) {
+               int ny = ceil((double)nz / nx);
+               int zw = dx / nx;
+               int ww = zw - bw;
+               int hh = (ww - mw) * oh / ow + mh;
+               int zh = hh + bh;
+               int64_t za = zw*nx * zh*ny;
+               int64_t r = sz - za;
+               if( r < 0 ) continue;
+               if( r >= best_r ) continue;
+               best_r = r;
+               bx = nx;  by = ny;
+       }
+       for( int ny=1; ny<=nz; ++ny ) {
+               int nx = ceil((double)nz / ny);
+               int zh = dy / ny;
+               int hh = zh - bh;
+               int ww = (hh - mh) * ow / oh + mw;
+               int zw = ww + bw;
+               int64_t za = zw*nx * zh*ny;
+               int64_t r = sz - za;
+               if( r < 0 ) continue;
+               if( r >= best_r ) continue;
+               best_r = r;
+               bx = nx;  by = ny;
+       }
+       int zw, zh, ww, hh;
+       if( bx < by ) {
+               zh = dy / by;
+               hh = zh - bh;
+               ww = (hh - mh) * ow / oh + mw;
+               zw = ww + bw;
+       }
+       else {
+               zw = dx / bx;
+               ww = zw - bw;
+               hh = (ww - mw) * oh / ow + mh;
+               zh = hh + bh;
+       }
+
+       int zx = 0, zy = 0;  // window origins
+       int n = 0;
        for( int i=0; i<zwindows.size(); ++i ) {
                ZWindow *zwindow = zwindows[i];
                if( zwindow->idx < 0 ) continue;
-               int ww = zw - bw, hh = (ww - mw) * oh / ow + mh, zh = hh + bh;
-               if( rsz < hh ) rsz = hh;
                int xx = zx + x1, yy = zy + y1;
                int mx = x2 - zw, my = y2 - zh;
                if( xx > mx ) xx = mx;
                if( yy > my ) yy = my;
-               xx += lt + xS(dz);  yy += top + yS(dz);
                zwindow->reposition(xx,yy, ww,hh);
                if( zwindow->running() ) {
                        ZWindowGUI *gui = (ZWindowGUI *)zwindow->get_gui();
@@ -1488,17 +1614,24 @@ void MWindow::tile_mixers()
                        gui->BC_WindowBase::reposition_window(xx,yy, ww,hh);
                        gui->unlock_window();
                }
-               if( ++n >= zn ) {
-                       n = 0;  rsz += bh;
-                       if( (zy += rsz) > (dy - rsz) ) dz += 10;
-                       rsz = 0;
-                       zx = 0;
+               if( ++n >= bx ) {
+                       zx = 0;  zy += zh;
+                       n = 0;
                }
                else
                        zx += zw;
        }
 }
 
+void MWindow::set_gang_tracks(int v)
+{
+       edl->local_session->gang_tracks = v;
+       sync_parameters(CHANGE_PARAMS);
+       gui->update(1, 1, 0, 0, 1, 0, 0);
+       gui->flush();
+}
+
+
 void MWindow::init_cache()
 {
        audio_cache = new CICache(preferences);
@@ -1769,7 +1902,7 @@ int MWindow::put_commercial()
        //check it
        for(Track *track=tracks->first; track && !errmsg; track=track->next) {
                if( track->data_type != TRACK_VIDEO ) continue;
-               if( !track->record ) continue;
+               if( !track->armed ) continue;
                if( count > 0 ) { errmsg = _("multiple video tracks"); break; }
                ++count;
                int64_t units_start = track->to_units(start,0);
@@ -1789,7 +1922,7 @@ int MWindow::put_commercial()
        //run it
        for(Track *track=tracks->first; track && !errmsg; track=track->next) {
                if( track->data_type != TRACK_VIDEO ) continue;
-               if( !track->record ) continue;
+               if( !track->armed ) continue;
                int64_t units_start = track->to_units(start,0);
                int64_t units_end = track->to_units(end,0);
                Edits *edits = track->edits;
@@ -2260,6 +2393,8 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
 // Start examining next batch of index files
        if(got_indexes) mainindexes->start_build();
 
+// reload renderengine edl or the plugin guis will flip out
+       sync_parameters(CHANGE_ALL);
 // Open plugin GUIs
        show_plugins();
 
@@ -2268,6 +2403,7 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
              load_mode == LOADMODE_REPLACE_CONCATENATE ) &&
            (ftype != FILE_IS_XML || edl_mode != LOADMODE_EDL_CLIP) ) {
                select_asset(0, 0);
+               edl->session->proxy_state = PROXY_INACTIVE;
                edl->session->proxy_scale = 1;
                edl->session->proxy_disabled_scale = 1;
                edl->session->proxy_use_scaler = 0;
@@ -2285,8 +2421,8 @@ if(debug) printf("MWindow::load_filenames %d\n", __LINE__);
                goto_start();
        }
 
-       if( ( edl->session->proxy_auto_scale && edl->session->proxy_scale != 1 ) &&
-           ( load_mode != LOADMODE_REPLACE && load_mode != LOADMODE_REPLACE_CONCATENATE ) ) {
+       if( edl->session->proxy_state != PROXY_INACTIVE && edl->session->proxy_auto_scale &&
+           load_mode != LOADMODE_REPLACE && load_mode != LOADMODE_REPLACE_CONCATENATE ) {
                ArrayList<Indexable *> orig_idxbls;
                for( int i=0; i<new_assets.size(); ++i )
                        orig_idxbls.append(new_assets.get(i));
@@ -2391,8 +2527,7 @@ int MWindow::render_proxy(ArrayList<Indexable *> &new_idxbls)
 int MWindow::enable_proxy()
 {
        int ret = 0;
-       if( edl->session->proxy_scale == 1 &&
-           edl->session->proxy_disabled_scale != 1 ) {
+       if( edl->session->proxy_state == PROXY_DISABLED ) {
                int new_scale = edl->session->proxy_disabled_scale;
                int new_use_scaler = edl->session->proxy_use_scaler;
                Asset *asset = new Asset;
@@ -2405,6 +2540,7 @@ int MWindow::enable_proxy()
                        beep(2000., 1.5, gain);
                }
                edl->session->proxy_disabled_scale = 1;
+               edl->session->proxy_state = PROXY_ACTIVE;
                gui->lock_window("MWindow::to_proxy");
                update_project(LOADMODE_REPLACE);
                gui->unlock_window();
@@ -2414,15 +2550,15 @@ int MWindow::enable_proxy()
 
 int MWindow::disable_proxy()
 {
-       if( edl->session->proxy_scale != 1 &&
-           edl->session->proxy_disabled_scale == 1 ) {
-               int old_scale = edl->session->proxy_scale, new_scale = 1;
+       if( edl->session->proxy_state == PROXY_ACTIVE ) {
+               int old_scale = edl->session->proxy_scale, new_scale = 0;
                int new_use_scaler = edl->session->proxy_use_scaler;
                Asset *asset = new Asset;
                asset->format = FILE_FFMPEG;
                asset->load_defaults(defaults, "PROXY_", 1, 1, 0, 0, 0);
                to_proxy(asset, new_scale, new_use_scaler);
                asset->remove_user();
+               edl->session->proxy_state = PROXY_DISABLED;
                edl->session->proxy_disabled_scale = old_scale;
                gui->lock_window("MWindow::to_proxy");
                update_project(LOADMODE_REPLACE);
@@ -2439,7 +2575,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
        edl->Garbage::add_user();
        save_backup();
        undo_before(_("proxy"), this);
-       int asset_scale = new_scale == 1 ? 0 :
+       int asset_scale = !new_scale ? 0 :
                        !new_use_scaler ? 1 : new_scale;
        ProxyRender proxy_render(this, asset, asset_scale);
 
@@ -2447,7 +2583,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
 // remove all session proxy assets at the at the current proxy_scale
        int proxy_scale = edl->session->proxy_scale;
 
-       if( proxy_scale > 1 ) {
+       if( edl->session->proxy_state == PROXY_ACTIVE ) {
                Asset *orig_asset = edl->assets->first;
                for( ; orig_asset; orig_asset=orig_asset->next ) {
                        char new_path[BCTEXTLEN];
@@ -2469,8 +2605,6 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                for( int i=0,n=edl->nested_edls.size(); i<n; ++i ) {
                        EDL *orig_nested = edl->nested_edls[i];
                        char new_path[BCTEXTLEN];
-                       if( !ProxyRender::from_proxy_path(new_path, orig_nested, proxy_scale) )
-                               continue;
                        proxy_render.to_proxy_path(new_path, orig_nested, proxy_scale);
 // test if proxy asset was already added to proxy_assets
                        int got_it = 0;
@@ -2488,7 +2622,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                }
 
 // convert from the proxy assets to the original assets
-               edl->set_proxy(1, 0, &proxy_assets, &orig_idxbls);
+               edl->set_proxy(0, 0, &proxy_assets, &orig_idxbls);
 
 // remove the references
                for( int i=0; i<proxy_assets.size(); ++i ) {
@@ -2509,7 +2643,7 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
        confirm_paths.set_array_delete();
 
 // convert to new size if not original size
-       if( new_scale != 1 ) {
+       if( new_scale ) {
                FileSystem fs;
                Asset *orig = edl->assets->first;
                for( ; orig; orig=orig->next ) {
@@ -2537,7 +2671,10 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                                proxy_render.add_needed(orig_nested, proxy);
                        }
                }
+               edl->session->proxy_state = PROXY_ACTIVE;
        }
+       else
+               edl->session->proxy_state = PROXY_INACTIVE;
 
        int result = 0;
 // test for existing files
@@ -2546,13 +2683,13 @@ int MWindow::to_proxy(Asset *asset, int new_scale, int new_use_scaler)
                confirm_paths.remove_all_objects();
        }
 
-       if( !result )
+       if( !result && new_scale )
                result = proxy_render.create_needed_proxies(new_scale);
 
-       if( !result ) // resize project
+       if( !result ) // resize project
                edl->set_proxy(new_scale, new_use_scaler,
                        &proxy_render.orig_idxbls, &proxy_render.orig_proxies);
-
+       }
        undo_after(_("proxy"), LOAD_ALL);
        edl->Garbage::remove_user();
        restart_brender();
@@ -2649,12 +2786,12 @@ void MWindow::create_objects(int want_gui,
        init_3d();
 
        if(debug) PRINT_TRACE
-       show_splash();
 
        if(debug) PRINT_TRACE
        default_standard = default_std();
        init_defaults(defaults, config_path);
        check_language();
+       show_splash();
        init_preferences();
        if(splash_window)
                splash_window->update_status(_("Initializing Plugins"));
@@ -2888,7 +3025,7 @@ void MWindow::run()
        run_lock->unlock();
 }
 
-void MWindow::show_vwindow()
+void MWindow::show_vwindow(int raise)
 {
        int total_running = 0;
        session->show_vwindow = 1;
@@ -2898,23 +3035,40 @@ void MWindow::show_vwindow()
        for(int j = 0; j < vwindows.size(); j++) {
                VWindow *vwindow = vwindows[j];
                if( !vwindow->is_running() ) continue;
+               total_running++;
+               if( !raise && !vwindow->gui->is_hidden() ) continue;
                vwindow->gui->lock_window("MWindow::show_vwindow");
                vwindow->gui->show_window(0);
                vwindow->gui->raise_window();
                vwindow->gui->flush();
                vwindow->gui->unlock_window();
-               total_running++;
        }
-
 // If no windows visible
-       if(!total_running)
-       {
+       if( !total_running )
                get_viewer(1, DEFAULT_VWINDOW);
-       }
 
        gui->mainmenu->show_vwindow->set_checked(1);
 }
 
+
+void MWindow::hide_vwindow(int raise)
+{
+       session->show_vwindow = 0;
+       int total_running = 0;
+
+       for(int j = 0; j < vwindows.size(); j++) {
+               VWindow *vwindow = vwindows[j];
+               if( !vwindow->is_running() ) continue;
+               total_running++;
+               if( !raise && !vwindow->gui->is_hidden() ) continue;
+               vwindow->gui->lock_window("MWindow::show_vwindow");
+               vwindow->gui->hide_window(0);
+               vwindow->gui->unlock_window();
+       }
+       gui->mainmenu->show_vwindow->set_checked(0);
+}
+
+
 void MWindow::show_awindow()
 {
        session->show_awindow = 1;
@@ -2926,6 +3080,17 @@ void MWindow::show_awindow()
        gui->mainmenu->show_awindow->set_checked(1);
 }
 
+void MWindow::hide_awindow()
+{
+       session->show_awindow = 0;
+
+       awindow->gui->lock_window("MWindow::show_awindow");
+       awindow->gui->hide_window();
+       awindow->gui->unlock_window();
+       gui->mainmenu->show_awindow->set_checked(0);
+}
+
+
 char *MWindow::get_cwindow_display()
 {
        char *x11_host = screens < 2 || session->window_config == 0 ?
@@ -2937,9 +3102,22 @@ void MWindow::show_cwindow()
 {
        session->show_cwindow = 1;
        cwindow->show_window();
+       cwindow->gui->tool_panel->raise_tool();
        gui->mainmenu->show_cwindow->set_checked(1);
 }
 
+
+void MWindow::hide_cwindow()
+{
+       session->show_cwindow = 0;
+
+       cwindow->gui->lock_window("MWindow::show_cwindow");
+       cwindow->gui->hide_window();
+       cwindow->gui->unlock_window();
+       gui->mainmenu->show_cwindow->set_checked(0);
+}
+
+
 void MWindow::show_gwindow()
 {
        session->show_gwindow = 1;
@@ -2952,6 +3130,7 @@ void MWindow::show_gwindow()
 
        gui->mainmenu->show_gwindow->set_checked(1);
 }
+
 void MWindow::hide_gwindow()
 {
        session->show_gwindow = 0;
@@ -2959,6 +3138,7 @@ void MWindow::hide_gwindow()
        gwindow->gui->lock_window("MWindow::show_gwindow");
        gwindow->gui->hide_window();
        gwindow->gui->unlock_window();
+       gui->mainmenu->show_gwindow->set_checked(0);
 }
 
 void MWindow::show_lwindow()
@@ -2972,8 +3152,20 @@ void MWindow::show_lwindow()
        gui->mainmenu->show_lwindow->set_checked(1);
 }
 
+void MWindow::hide_lwindow()
+{
+       session->show_lwindow = 0;
+
+       lwindow->gui->lock_window("MWindow::show_lwindow");
+       lwindow->gui->hide_window();
+       lwindow->gui->unlock_window();
+       gui->mainmenu->show_lwindow->set_checked(0);
+}
+
+
 void MWindow::restore_windows()
 {
+       gui->unlock_window();
        if( !session->show_vwindow ) {
                for( int i=0, n=vwindows.size(); i<n; ++i ) {
                        VWindow *vwindow = vwindows[i];
@@ -2983,8 +3175,8 @@ void MWindow::restore_windows()
                        vwindow->gui->unlock_window();
                }
        }
-       else
-               show_vwindow();
+       else 
+               show_vwindow(0);
 
        if( !session->show_awindow && !awindow->gui->is_hidden() ) {
                awindow->gui->lock_window("MWindow::restore_windows");
@@ -3000,7 +3192,7 @@ void MWindow::restore_windows()
                cwindow->gui->unlock_window();
        }
        else if( session->show_cwindow && cwindow->gui->is_hidden() )
-               cwindow->show_window();
+               show_cwindow();
 
        if( !session->show_gwindow && !gwindow->gui->is_hidden() ) {
                gwindow->gui->lock_window("MWindow::restore_windows");
@@ -3018,6 +3210,8 @@ void MWindow::restore_windows()
        else if( session->show_lwindow && lwindow->gui->is_hidden() )
                show_lwindow();
 
+       tile_mixers();
+       gui->lock_window("MWindow::restore_windows");
        gui->focus();
 }
 
@@ -3404,9 +3598,10 @@ int MWindow::get_hash_color(Edit *edit)
                (Indexable*)edit->asset : (Indexable*)edit->nested_edl;
        if( !idxbl ) return 0;
        char path[BCTEXTLEN];
-       if( !edit->asset || edit->track->data_type != TRACK_VIDEO ||
-           edl->session->proxy_scale == 1 ||
-           ProxyRender::from_proxy_path(path, idxbl, edl->session->proxy_scale) )
+// map proxy colors to unproxy colors
+       if( edl->session->proxy_state != PROXY_ACTIVE ||
+           !edit->asset || edit->track->data_type != TRACK_VIDEO ||
+           ProxyRender::from_proxy_path(path, (Asset*)idxbl, edl->session->proxy_scale) )
                strcpy(path, idxbl->path);
        char *cp = strrchr(path, '/');
        cp = !cp ? path : cp+1;
@@ -3707,8 +3902,8 @@ void MWindow::update_project(int load_mode)
 
        if( load_mode == LOADMODE_REPLACE ||
            load_mode == LOADMODE_REPLACE_CONCATENATE ) {
-               delete gui->keyvalue_popup;
-               gui->keyvalue_popup = 0;
+               edl->session->timecode_offset = 0;
+               gui->close_keyvalue_popup();
                gui->load_panes();
        }
 
@@ -3750,6 +3945,7 @@ void MWindow::update_project(int load_mode)
 
                for( int i=0; i<edl->mixers.size(); ++i ) {
                        Mixer *mixer = edl->mixers[i];
+                       if( !mixer->show ) continue;
                        ZWindow *zwindow = get_mixer(mixer);
                        zwindow->set_title(mixer->title);
                        zwindow->start();
@@ -3806,6 +4002,7 @@ void MWindow::stack_push(EDL *new_edl, Indexable *idxbl)
                StackItem &item = stack.append();
                item.edl = edl;
                item.new_edl = new_edl;
+               item.duration = new_edl->tracks->total_length();
                item.undo = undo;
                item.idxbl = idxbl;
                item.mtime = 0;
@@ -3838,17 +4035,44 @@ void MWindow::stack_pop()
 // session edl replaced, overwrite and save clip data
        if( item.new_edl != edl )
                item.new_edl->overwrite_clip(edl);
+       Indexable *idxbl = item.idxbl;
+       if( idxbl && idxbl->is_asset && item.mtime ) {
+               Asset *asset = (Asset *)idxbl;
+               if( asset->format == FILE_REF ) {
+                       char *path = asset->path;
+                       struct stat st;
+                       if( stat(path, &st) || item.mtime == st.st_mtime ) {
+                               int cw = xS(250), ch = yS(150), px, py;
+                               gui->get_pop_cursor(px, py);
+                               px -= cw/2;  py -= ch/2;
+                               ConfirmRefWindow confirm(this, path, px, py, cw, ch);
+                               confirm.create_objects();
+                               int result = confirm.run_window();
+                               if( !result ) {
+                                       FileXML file;
+                                       item.new_edl->save_xml(&file, path);
+                                       file.terminate_string();
+                                       if(file.write_to_file(path))
+                                               eprintf(_("Cant write FileREF: %s"), path);
+                               }
+                       }
+               }
+       }
        edl->remove_user();
        edl = item.edl;
        delete undo;
        undo = item.undo;
-       Indexable *idxbl = item.idxbl;
-       int64_t mtime = item.mtime;
        stack.remove();
        if( idxbl ) {
+// resize the indexable edits if the new_edl duration changed
+               double duration = item.new_edl->tracks->total_length();
+               double dt = duration - item.duration;
+               if( fabs(dt) > 1e-4 )
+                       edl->tracks->update_idxbl_length(idxbl->id, dt);
                gui->unlock_window();
+               gui->resource_thread->close_indexable(idxbl);
                remove_from_caches(idxbl);
-               remove_indexfile(idxbl);
+               IndexFile::delete_index_files(preferences, idxbl);
                mainindexes->add_indexable(idxbl);
                mainindexes->start_build();
                awindow->gui->async_update_assets();
@@ -3859,17 +4083,6 @@ void MWindow::stack_pop()
        undo_after(_("open edl"), LOAD_ALL);
        show_plugins();
        gui->stack_button->update();
-       if( mtime && idxbl && idxbl->is_asset ) {
-               struct stat st;
-               Asset *asset = (Asset *)idxbl;
-               if( asset->format == FILE_REF && !stat(asset->path, &st) &&
-                   item.mtime == st.st_mtime ) {
-                       char text[BCTEXTLEN];
-                       snprintf(text, sizeof(text),
-                                _("Warning: Asset not updated: %s"), asset->path);
-                       show_warning(&preferences->warn_stack, text);
-               }
-       }
 }
 
 int MWindow::save(EDL *edl, char *filename, int stat)
@@ -3913,6 +4126,7 @@ int MWindow::save(int save_as)
        for( int i=stack.size(); --i>=0;  ) {
                StackItem &item = stack[i];
                Indexable *idxbl = item.idxbl;
+               if( !idxbl ) continue;
                if( idxbl->is_asset ) {
                        Asset *asset = (Asset *)idxbl;
                        if( asset->format == FILE_REF ) {
@@ -3920,8 +4134,8 @@ int MWindow::save(int save_as)
                                        return 1;
                        }
                }
-               else if( item.new_edl != item.idxbl )
-                       item.new_edl->overwrite_clip((EDL*)item.idxbl);
+               else if( item.new_edl != idxbl )
+                       item.new_edl->overwrite_clip((EDL*)idxbl);
        }
        EDL *new_edl = stack.size() ? stack[0].edl : edl;
        save(new_edl, path, 1);
@@ -3950,6 +4164,7 @@ void MWindow::clip_to_media()
                return;
        }
        undo_before();
+       awindow->gui->stop_vicon_drawing();
        int clips_total = session->drag_clips->total;
        for( int i=0; i<clips_total; ++i ) {
                EDL *clip = session->drag_clips->values[i];
@@ -3963,7 +4178,7 @@ void MWindow::clip_to_media()
                char *bp = strrchr(clip->local_session->clip_title, '/');
                bp = bp ? bp+1 : clip->local_session->clip_title;
                cp += snprintf(cp, ep-cp, "%s", bp);
-               EDL *nested = edl->new_nested_edl(clip, path);
+               EDL *nested = edl->new_nested_clip(clip, path);
                edl->clips.remove(clip);
                clip->remove_user();
                mainindexes->add_indexable(nested);
@@ -4035,18 +4250,18 @@ void MWindow::update_preferences(Preferences *prefs)
        if( prefs != preferences )
                preferences->copy_from(prefs);
        if( cwindow->playback_engine )
-               cwindow->playback_engine->preferences->copy_from(prefs);
+               cwindow->playback_engine->update_preferences(prefs);
        for(int i = 0; i < vwindows.size(); i++) {
                VWindow *vwindow = vwindows[i];
                if( !vwindow->is_running() ) continue;
                if( vwindow->playback_engine )
-                       vwindow->playback_engine->preferences->copy_from(prefs);
+                       vwindow->playback_engine->update_preferences(prefs);
        }
        for(int i = 0; i < zwindows.size(); i++) {
                ZWindow *zwindow = zwindows[i];
                if( !zwindow->is_running() ) continue;
                if( zwindow->zgui->playback_engine )
-                       zwindow->zgui->playback_engine->preferences->copy_from(prefs);
+                       zwindow->zgui->playback_engine->update_preferences(prefs);
        }
 }
 
@@ -4065,11 +4280,8 @@ void MWindow::update_vwindow()
 void MWindow::remove_indexfile(Indexable *indexable)
 {
        if( !indexable->is_asset ) return;
-       Asset *asset = (Asset *)indexable;
 // Erase file
-       IndexFile::delete_index(preferences, asset, ".toc");
-       IndexFile::delete_index(preferences, asset, ".idx");
-       IndexFile::delete_index(preferences, asset, ".mkr");
+       IndexFile::delete_index_files(preferences, indexable);
 }
 
 void MWindow::rebuild_indices()
@@ -4111,6 +4323,25 @@ void MWindow::get_backup_path(char *path, int len)
        cp += snprintf(cp, ep-cp, idx ? BACKUPn_FILE : BACKUP_FILE, idx);
 }
 
+void MWindow::create_timestamped_copy_from_previous_backup(char *previouspath)
+{
+  if (previouspath == NULL) return;
+  char backup_path[BCTEXTLEN];
+  backup_path[0] = 0;
+  time_t now = time(NULL);
+  struct tm* currenttime = localtime(&now);
+  snprintf(backup_path, sizeof(backup_path), 
+      "%s/%s_%d%.2d%.2d_%.2d%.2d%.2d",
+      File::get_config_path(), BACKUP_FILE1, 
+      currenttime->tm_year + 1900,
+      currenttime->tm_mon + 1,
+      currenttime->tm_mday,
+      currenttime->tm_hour,
+      currenttime->tm_min,
+      currenttime->tm_sec);
+       rename(previouspath, backup_path);
+}
+
 void MWindow::save_backup()
 {
        FileXML file;
@@ -4121,6 +4352,8 @@ void MWindow::save_backup()
        snprintf(backup_path1, sizeof(backup_path1), "%s/%s",
                File::get_config_path(), BACKUP_FILE1);
        get_backup_path(backup_path, sizeof(backup_path));
+       if( preferences->ongoing_backups )
+               create_timestamped_copy_from_previous_backup(backup_path1);
        rename(backup_path, backup_path1);
        edl->save_xml(&file, backup_path);
        file.terminate_string();
@@ -4389,11 +4622,14 @@ static inline int gcd(int m, int n)
 int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
 {
        w = 1;  h = 1;
+       double ar;
+       
        if(!width || !height) return 1;
        if( width == 720 && (height == 480 || height == 576) ) {
                w = 4;  h = 3;  return 0; // for NTSC and PAL
        }
-       double ar = (double)width / height;
+       
+       ar = (double)width / height;
 // square-ish pixels
        if( EQUIV(ar, 1.0000) ) return 0;
        if( EQUIV(ar, 1.3333) ) { w = 4;  h = 3;  return 0; }
@@ -4401,6 +4637,8 @@ int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
        if( EQUIV(ar, 2.1111) ) { w = 19; h = 9;  return 0; }
        if( EQUIV(ar, 2.2222) ) { w = 20; h = 9;  return 0; }
        if( EQUIV(ar, 2.3333) ) { w = 21; h = 9;  return 0; }
+       if( EQUIV(ar, 2.37037) ) { w = 64; h = 27;  return 0; }
+
        int ww = width, hh = height;
        // numerator, denominator must be under mx
        int mx = 255, n = gcd(ww, hh);
@@ -4424,31 +4662,29 @@ int MWindow::create_aspect_ratio(float &w, float &h, int width, int height)
        return 0;
 }
 
-void MWindow::reset_caches()
+void MWindow::reset_caches(int locked)
 {
-       frame_cache->remove_all();
-       wave_cache->remove_all();
-       audio_cache->remove_all();
-       video_cache->remove_all();
-       if( cwindow->playback_engine ) {
-               if( cwindow->playback_engine->audio_cache )
-                       cwindow->playback_engine->audio_cache->remove_all();
-               if( cwindow->playback_engine->video_cache )
-                       cwindow->playback_engine->video_cache->remove_all();
-       }
+       if( locked ) gui->unlock_window();
+       awindow->gui->stop_vicon_drawing(1);
+       if( cwindow->playback_engine )
+               cwindow->playback_engine->create_cache();
        for(int i = 0; i < vwindows.size(); i++) {
                VWindow *vwindow = vwindows[i];
                if( !vwindow->is_running() ) continue;
                if( !vwindow->playback_engine ) continue;
-               if( vwindow->playback_engine->audio_cache )
-                       vwindow->playback_engine->audio_cache->remove_all();
-               if( vwindow->playback_engine->video_cache )
-                       vwindow->playback_engine->video_cache->remove_all();
+               vwindow->playback_engine->create_cache();
        }
+       gui->lock_window("MWindow::reset_caches");
+       frame_cache->remove_all();
+       wave_cache->remove_all();
+       audio_cache->remove_all();
+       video_cache->remove_all();
+       if( !locked ) gui->unlock_window();
 }
 
 void MWindow::remove_from_caches(Indexable *idxbl)
 {
+       awindow->gui->stop_vicon_drawing(1);
        frame_cache->remove_item(idxbl);
        wave_cache->remove_item(idxbl);
        if( gui->render_engine &&
@@ -4456,11 +4692,7 @@ void MWindow::remove_from_caches(Indexable *idxbl)
                delete gui->render_engine;
                gui->render_engine = 0;
        }
-       if( gui->resource_thread->render_engine_id == idxbl->id ) {
-               gui->resource_thread->render_engine_id = -1;
-               delete gui->resource_thread->render_engine;
-               gui->resource_thread->render_engine = 0;
-       }
+       gui->resource_thread->close_indexable(idxbl);
        if( !idxbl->is_asset ) return;
        Asset *asset = (Asset *)idxbl;
        audio_cache->delete_entry(asset);
@@ -4486,12 +4718,13 @@ void MWindow::remove_from_caches(Indexable *idxbl)
                if( zwindow->zgui->playback_engine->video_cache )
                        zwindow->zgui->playback_engine->video_cache->delete_entry(asset);
        }
+       awindow->gui->start_vicon_drawing();
 }
 
 void MWindow::remove_assets_from_project(int push_undo, int redraw, int delete_indexes,
                ArrayList<Indexable*> *drag_assets, ArrayList<EDL*> *drag_clips)
 {
-       awindow->gui->close_view_popup();
+       awindow->gui->stop_vicon_drawing(1);
 
 // Remove from VWindow.
        if( drag_clips ) {
@@ -4646,6 +4879,14 @@ void MWindow::dump_exe(FILE *fp)
        fprintf(fp, "\n");
 }
 
+void MWindow::dump_caches(FILE *fp)
+{
+       fprintf(fp, "audio cache: ");
+       audio_cache->dump(fp);
+       fprintf(fp, "video cache: ");
+       video_cache->dump(fp);
+}
+
 void MWindow::trap_hook(FILE *fp, void *vp)
 {
        MWindow *mwindow = (MWindow *)vp;
@@ -4657,6 +4898,8 @@ void MWindow::trap_hook(FILE *fp, void *vp)
        mwindow->dump_undo(fp);
        fprintf(fp, "\nEXE: %s\n", AboutPrefs::build_timestamp);
        mwindow->dump_exe(fp);
+       fprintf(fp, "\nCACHES:\n");
+       mwindow->dump_caches(fp);
 }
 
 
@@ -4727,31 +4970,30 @@ int MWindow::interrupt_indexes()
 
 void MWindow::next_time_format()
 {
-       switch(edl->session->time_format)
-       {
-               case TIME_HMS: edl->session->time_format = TIME_HMSF; break;
-               case TIME_HMSF: edl->session->time_format = TIME_SAMPLES; break;
-               case TIME_SAMPLES: edl->session->time_format = TIME_SAMPLES_HEX; break;
-               case TIME_SAMPLES_HEX: edl->session->time_format = TIME_FRAMES; break;
-               case TIME_FRAMES: edl->session->time_format = TIME_FEET_FRAMES; break;
-               case TIME_FEET_FRAMES: edl->session->time_format = TIME_SECONDS; break;
-               case TIME_SECONDS: edl->session->time_format = TIME_HMS; break;
+       switch( edl->session->time_format ) {
+       case TIME_HMS:          edl->session->time_format = TIME_HMSF;         break;
+       case TIME_HMSF:         edl->session->time_format = TIME_TIMECODE;     break;
+       case TIME_TIMECODE:     edl->session->time_format = TIME_FRAMES;       break;
+       case TIME_FRAMES:       edl->session->time_format = TIME_SAMPLES;      break;
+       case TIME_SAMPLES:      edl->session->time_format = TIME_SAMPLES_HEX;  break;
+       case TIME_SAMPLES_HEX:  edl->session->time_format = TIME_SECONDS;      break;
+       case TIME_SECONDS:      edl->session->time_format = TIME_FEET_FRAMES;  break;
+       case TIME_FEET_FRAMES:  edl->session->time_format = TIME_HMS;          break;
        }
-
        time_format_common();
 }
 
 void MWindow::prev_time_format()
 {
-       switch(edl->session->time_format)
-       {
-               case TIME_HMS: edl->session->time_format = TIME_SECONDS; break;
-               case TIME_SECONDS: edl->session->time_format = TIME_FEET_FRAMES; break;
-               case TIME_FEET_FRAMES: edl->session->time_format = TIME_FRAMES; break;
-               case TIME_FRAMES: edl->session->time_format = TIME_SAMPLES_HEX; break;
-               case TIME_SAMPLES_HEX: edl->session->time_format = TIME_SAMPLES; break;
-               case TIME_SAMPLES: edl->session->time_format = TIME_HMSF; break;
-               case TIME_HMSF: edl->session->time_format = TIME_HMS; break;
+       switch( edl->session->time_format ) {
+       case TIME_HMS:          edl->session->time_format = TIME_FEET_FRAMES;  break;
+       case TIME_HMSF:         edl->session->time_format = TIME_HMS;          break;
+       case TIME_TIMECODE:     edl->session->time_format = TIME_HMSF;         break;
+       case TIME_FRAMES:       edl->session->time_format = TIME_TIMECODE;     break;
+       case TIME_SAMPLES:      edl->session->time_format = TIME_FRAMES;       break;
+       case TIME_SAMPLES_HEX:  edl->session->time_format = TIME_SAMPLES;      break;
+       case TIME_SECONDS:      edl->session->time_format = TIME_SAMPLES_HEX;  break;
+       case TIME_FEET_FRAMES:  edl->session->time_format = TIME_SECONDS;      break;
        }
 
        time_format_common();
@@ -4940,12 +5182,26 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                session->output_w = width;
                session->output_h = height;
                session->frame_rate = framerate;
+               session->interlace_mode = asset->interlace_mode;
                // not, asset->actual_width/actual_height
                asset->width = session->output_w;
                asset->height = session->output_h;
                asset->frame_rate = session->frame_rate;
+               
                create_aspect_ratio(session->aspect_w, session->aspect_h,
                        session->output_w, session->output_h);
+       float ar = asset->aspect_ratio;
+       if (ar) {
+       //printf ("Aspect ratio from asset: %f \n", ar);
+       if( EQUIV(ar, 1.3333) ) { session->aspect_w = 4;  session->aspect_h = 3;  }
+       if( EQUIV(ar, 1.7777) ) { session->aspect_w = 16; session->aspect_h = 9;   }
+       if( EQUIV(ar, 2.1111) ) { session->aspect_w = 19; session->aspect_h = 9;  }
+       if( EQUIV(ar, 2.2222) ) { session->aspect_w = 20; session->aspect_h = 9;   }
+       if( EQUIV(ar, 2.3333) ) { session->aspect_w = 21; session->aspect_h = 9;   }
+       if( EQUIV(ar, 2.370370) ) { session->aspect_w = 64; session->aspect_h = 27; }
+       }
+                       
+                       
                Track *track = edl->tracks->first;
                for( Track *next_track=0; track; track=next_track ) {
                        next_track = track->next;
@@ -4965,7 +5221,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                                track->track_h = edl->session->output_h;
                        }
                        else if( delete_tracks )
-                               edl->tracks->delete_track(track);
+                               edl->tracks->delete_track(track, 0);
                }
                edl->retrack();
                edl->resample(old_framerate, session->frame_rate, TRACK_VIDEO);
@@ -5001,7 +5257,7 @@ int MWindow::select_asset(Asset *asset, int vstream, int astream, int delete_tra
                                                delete edit;
                                }
                                if( !track->edits->first )
-                                       edl->tracks->delete_track(track);
+                                       edl->tracks->delete_track(track, 0);
                        }
                }
                edl->rechannel();
@@ -5097,3 +5353,58 @@ int MWindow::get_cpus()
        return get_cpus(edl->session->output_w, edl->session->output_h);
 }
 
+void MWindow::draw_trackmovement()
+{
+       if( !redraw_tracks )
+               redraw_tracks = new DrawTrackMovement(this);
+       redraw_tracks->start();
+}
+
+DrawTrackMovement::DrawTrackMovement(MWindow *mwindow)
+ : Thread(1, 0, 0)
+{
+       this->mwindow = mwindow;
+}
+DrawTrackMovement::~DrawTrackMovement()
+{
+       join();
+}
+
+void DrawTrackMovement::run()
+{
+       mwindow->gui->lock_window("DrawTrackMovement::run");
+       mwindow->edl->tracks->update_y_pixels(mwindow->theme);
+       mwindow->gui->draw_trackmovement();
+       mwindow->gui->unlock_window();
+}
+
+
+ConfirmRefWindow::ConfirmRefWindow(MWindow *mwindow, char *path,
+               int px, int py, int cw, int ch)
+ : BC_Window(_(PROGRAM_NAME ": Confirm update"), px, py, cw, ch, cw, ch)
+{
+       this->mwindow = mwindow;
+       this->path = path;
+}
+
+ConfirmRefWindow::~ConfirmRefWindow()
+{
+}
+
+void ConfirmRefWindow::create_objects()
+{
+       lock_window("ConfirmRefWindow::create_objects()");
+       int x = xS(10), y = yS(10), pad = yS(5);
+       BC_Title *title;
+       add_subwindow(title = new BC_Title(x, y, _("FileREF not updated:")));
+       y += title->get_h() + pad;
+       BC_TextBox *text_box;
+       add_subwindow(text_box = new BC_TextBox(x,y, get_w()-2*x, 1, path));
+       y += text_box->get_h() + 2*pad;
+       add_subwindow(title = new BC_Title(x, y, _("Save file ref changes?")));
+       add_subwindow(new BC_OKButton(this));
+       add_subwindow(new BC_CancelButton(this));
+       show_window();
+       unlock_window();
+}
+