Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / swindow.C
index 770556c64a99f942f8ae500869d8974c284f8c10..18f6abaaf3dfe79f5c28f8278a12fa2d67d18e1d 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * CINELERRA
+ * Copyright (C) 2016-2020 William Morrow
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
 
 #include "bchash.h"
 #include "bctimer.h"
@@ -48,7 +67,7 @@ int SWindowOK::button_press_event()
 
 int SWindowOK::keypress_event()
 {
-       return 0;
+       return context_help_check_and_show();
 }
 
 
@@ -129,7 +148,7 @@ int SWindowSaveFile::handle_event()
                sw_gui->save_spumux_data();
        }
        else {
-               eprintf(_("script microdvd file path required"));
+               eprintf(_("script file path required"));
        }
        return 1;
 }
@@ -150,6 +169,9 @@ void SWindowGUI::create_objects()
        add_subwindow(load_file = new SWindowLoadFile(this, x1, y1));
        x1 += load_file->get_w() + 2*xpad;
        add_subwindow(save_file = new SWindowSaveFile(this, x1, y1));
+       x1 += save_file->get_w() + 2*xpad;
+       add_subwindow(save_format = new SWindowSaveFormat(this, x1, y1));
+       save_format->create_objects();
        y += max(load_path->get_h(), load_file->get_h()) + ypad;
        x1 = x + ypad, y1 = y;
        BC_Title *title1, *title2;
@@ -276,6 +298,9 @@ SWindowGUI::SWindowGUI(SWindow *swindow, int x, int y, int w, int h)
        blank_line = 0;
        text_font = MEDIUMFONT;
        text_rowsz = get_text_ascent(text_font)+1 + get_text_descent(text_font)+1;
+       sub_format = SUB_FORMAT_SRT;
+// *** CONTEXT_HELP ***
+       context_help_set_keyword("Subtitles");
 }
 
 SWindowGUI::~SWindowGUI()
@@ -339,6 +364,7 @@ void SWindowGUI::load_defaults()
        defaults->get("SUBTTL_SCRIPT_PATH", script_path);
        script_entry_no = defaults->get("SUBTTL_SCRIPT_ENTRY_NO", script_entry_no);
        script_text_no = defaults->get("SUBTTL_SCRIPT_TEXT_NO", script_text_no);
+       sub_format = defaults->get("SUBTTL_SCRIPT_FORMAT", sub_format);
 }
 
 void SWindowGUI::save_defaults()
@@ -347,6 +373,7 @@ void SWindowGUI::save_defaults()
        defaults->update("SUBTTL_SCRIPT_PATH", script_path);
        defaults->update("SUBTTL_SCRIPT_ENTRY_NO", script_entry_no);
        defaults->update("SUBTTL_SCRIPT_TEXT_NO", script_text_no);
+       defaults->update("SUBTTL_SCRIPT_FORMAT", sub_format);
 }
 
 void SWindowGUI::set_script_pos(int64_t entry_no, int text_no)
@@ -428,7 +455,7 @@ int SWindowGUI::update_selection()
        Edit *edit = 0;
        Tracks *tracks = edl->tracks;
        for( Track *track=tracks->first; track && !edit; track=track->next ) {
-               if( !track->record ) continue;
+               if( !track->is_armed() ) continue;
                if( track->data_type != TRACK_SUBTITLE ) continue;
                int64_t pos = track->to_units(position,0);
                edit = track->edits->editof(pos, PLAY_FORWARD, 0);
@@ -448,7 +475,7 @@ int MWindow::paste_subtitle_text(char *text, double start, double end)
        Tracks *tracks = edl->tracks;
        for( Track *track=tracks->first; track; track=track->next ) {
                if( track->data_type != TRACK_SUBTITLE ) continue;
-               if( !track->record ) continue;
+               if( !track->is_armed() ) continue;
                int64_t start_i = track->to_units(start, 0);
                int64_t end_i = track->to_units(end, 1);
                track->edits->clear(start_i,end_i);
@@ -838,35 +865,107 @@ void SWindowGUI::save_spumux_data()
        Tracks *tracks = swindow->mwindow->edl->tracks;
        for( Track *track=tracks->first; track; track=track->next ) {
                if( track->data_type != TRACK_SUBTITLE ) continue;
-               if( !track->record ) continue;
+               if( !track->is_armed() ) continue;
                char *cp = track_title, *ep = cp+sizeof(track_title)-6;
                for( const char *bp=track->title; cp<ep && *bp!=0; ) {
-                       int b = butf8(bp), c = !iswalnum(b) ? '_' : b;
-                       butf8(c, cp);
+                       int wch = butf8(bp); // iswalnum(wch) broken by MS port
+                       if( !( (wch >= 'A' && wch <= 'Z') ||
+                              (wch >= 'a' && wch <= 'z') ||
+                              (wch >= '0' && wch <= '9') ) ) wch = '_';
+                       butf8(wch, cp);
+               }
+               const char *sfx = "";
+               switch( sub_format ) {
+               case SUB_FORMAT_SRT:   sfx = ".srt";   break;
+               case SUB_FORMAT_RIP:   sfx = ".sub";   break;
+               case SUB_FORMAT_UDVD:  sfx = ".udvd";  break;
                }
                *cp = 0;
-               snprintf(ext,len,"-%s.udvd",track_title);
+               snprintf(ext,len,"-%s%s",track_title, sfx);
                FILE *fp = fopen(filename, "w");
                if( !fp ) {
                        eprintf(_("Unable to open %s:\n%m"), filename);
                        continue;
                }
-               int64_t start = 0;
+               switch( sub_format ) {
+               case SUB_FORMAT_RIP:
+                       fprintf(fp,"[SUBTITLE]\n"
+                               "[COLF]&HFFFFFF,[SIZE]12,[FONT]Times New Roman\n");
+                       break;
+               }
+               int64_t start = 0;  int count = 0;
                for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
                        SEdit *sedit = (SEdit *)edit;
-                       if( sedit->length > 0 ) {
-                               int64_t end = start + sedit->length;
-                               char *text = sedit->get_text();
-                               if( *text ) {
+                       if( !sedit->length ) continue;
+                       int64_t end = start + sedit->length;
+                       double st = sedit->track->from_units(start);
+                       int shr = st/3600; st -= shr*3600;
+                       int smn = st/60;   st -= smn*60;
+                       int ssc = st;      st -= ssc;
+                       int sms = st*1000;
+                       double et = sedit->track->from_units(end);
+                       int ehr = et/3600; et -= ehr*3600;
+                       int emn = et/60;   et -= emn*60;
+                       int esc = et;      et -= esc;
+                       int ems = et*1000;
+                       char *text = sedit->get_text();
+                       if( *text ) {
+                               ++count;
+                               switch( sub_format ) {
+                               case SUB_FORMAT_SRT:
+                                       fprintf(fp, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n%s\n\n",
+                                                count, shr, smn, ssc, sms, ehr, emn, esc, ems, text);
+                                       break;
+                               case SUB_FORMAT_RIP:
+                                       fprintf(fp, "%02d:%02d:%02d.%02d,%02d:%02d:%02d.%02d\n%s\n\n",
+                                                shr, smn, ssc, sms/10, ehr, emn, esc, ems/10, text);
+                                       break;
+                               case SUB_FORMAT_UDVD:
                                        fprintf(fp, "{%jd}{%jd}%s\n", start, end-1, text);
+                                       break;
                                }
-                               start = end;
                        }
+                       start = end;
                }
                fclose(fp);
        }
 }
 
+SWindowItemFormat::SWindowItemFormat(SWindowSaveFormat *save_format,
+               const char *text, int id)
+ : BC_MenuItem(text)
+{
+       this->save_format = save_format;
+       this->id = id;
+}
+
+int SWindowItemFormat::handle_event()
+{
+       save_format->sw_gui->sub_format = id;
+       save_format->update_toggles();
+       return 1;
+}
+
+SWindowSaveFormat::SWindowSaveFormat(SWindowGUI *sw_gui, int x, int y)
+ : BC_PopupMenu(x, y, _("Format"))
+{
+       this->sw_gui = sw_gui;
+}
+
+void SWindowSaveFormat::create_objects()
+{
+       add_item(srt = new SWindowItemFormat(this, _("SRT"), SUB_FORMAT_SRT));
+       add_item(rip = new SWindowItemFormat(this, _("SUB"), SUB_FORMAT_RIP));
+       add_item(udvd = new SWindowItemFormat(this, _("UDVD"), SUB_FORMAT_UDVD));
+       update_toggles();
+}
+
+void SWindowSaveFormat::update_toggles()
+{
+       srt->set_checked(sw_gui->sub_format == SUB_FORMAT_SRT);
+       rip->set_checked(sw_gui->sub_format == SUB_FORMAT_RIP);
+       udvd->set_checked(sw_gui->sub_format == SUB_FORMAT_UDVD);
+}
 
 
 SWindow::SWindow(MWindow *mwindow)