1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Converts Latex to linear/WinHelp RTF, HTML, wxHelp.
4 // Author: Julian Smart
5 // Modified by: Wlodzimiez ABX Skiba 2003/2004 Unicode support
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
20 #if defined(__WXMSW__)
21 #include "wx/msw/wrapwin.h"
27 #include "wx/textctrl.h"
28 #include "wx/filedlg.h"
29 #include "wx/msgdlg.h"
39 #include "wx/cshelp.h"
40 #include "wx/helphtml.h"
42 #include "wx/msw/helpchm.h"
44 #include "wx/html/helpctrl.h"
65 #if (defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXX11__) || defined(__WXMGL__)) && !defined(NO_GUI)
66 #include "tex2rtf.xpm"
69 #if !WXWIN_COMPATIBILITY_2_4
70 static inline wxChar
* copystring(const wxChar
* s
)
71 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
74 const float versionNo
= float(TEX2RTF_VERSION_NUMBER
);
76 TexChunk
*currentMember
= NULL
;
77 bool startedSections
= false;
78 wxChar
*contentsString
= NULL
;
79 bool suppressNameDecoration
= false;
80 bool OkToClose
= true;
82 unsigned long errorCount
= 0;
86 extern wxChar
*BigBuffer
;
87 extern wxChar
*TexFileRoot
;
88 extern wxChar
*TexBibName
; // Bibliography output file name
89 extern wxChar
*TexTmpBibName
; // Temporary bibliography output file name
90 extern wxList ColourTable
;
91 extern TexChunk
*TopLevel
;
94 wxHelpControllerBase
*HelpInstance
= NULL
;
98 static wxChar
*ipc_buffer
= NULL
;
99 static wxChar Tex2RTFLastStatus
[100];
100 Tex2RTFServer
*TheTex2RTFServer
= NULL
;
105 wxChar
*bulletFile
= NULL
;
107 FILE *Contents
= NULL
; // Contents page
108 FILE *Chapters
= NULL
; // Chapters (WinHelp RTF) or rest of file (linear RTF)
109 FILE *Sections
= NULL
;
110 FILE *Subsections
= NULL
;
111 FILE *Subsubsections
= NULL
;
113 FILE *WinHelpContentsFile
= NULL
;
117 wxChar
*MacroFile
= copystring(_T("tex2rtf.ini"));
119 wxChar
*FileRoot
= NULL
;
120 wxChar
*ContentsName
= NULL
; // Contents page from last time around
121 wxChar
*TmpContentsName
= NULL
; // Current contents page
122 wxChar
*TmpFrameContentsName
= NULL
; // Current frame contents page
123 wxChar
*WinHelpContentsFileName
= NULL
; // WinHelp .cnt file
124 wxChar
*RefFileName
= NULL
; // Reference file name
126 wxChar
*RTFCharset
= copystring(_T("ansi"));
129 int BufSize
= 100; // Size of buffer in K
135 void ShowOptions(void);
136 void ShowVersion(void);
138 wxChar wxTex2RTFBuffer
[1500];
141 IMPLEMENT_APP_CONSOLE(MyApp
)
143 wxMenuBar
*menuBar
= NULL
;
144 MyFrame
*frame
= NULL
;
145 // DECLARE_APP(MyApp)
149 // `Main program' equivalent, creating windows and returning main app frame
152 // Use default list of macros defined in tex2any.cc
153 DefineDefaultMacros();
154 AddMacroDef(ltHARDY
, _T("hardy"), 0);
156 FileRoot
= new wxChar
[300];
157 ContentsName
= new wxChar
[300];
158 TmpContentsName
= new wxChar
[300];
159 TmpFrameContentsName
= new wxChar
[300];
160 WinHelpContentsFileName
= new wxChar
[300];
161 RefFileName
= new wxChar
[300];
163 WX_CLEAR_LIST(wxList
,ColourTable
);
167 // Read input/output files
170 if (argv
[1][0] != _T('-'))
177 if (argv
[2][0] != _T('-'))
179 OutputFile
= argv
[2];
186 TexPathList
.Add(::wxGetCwd());
189 if (InputFile
.empty() || OutputFile
.empty())
191 wxSTD cout
<< "Tex2RTF: input or output file is missing.\n";
197 if (!InputFile
.empty())
199 TexPathList
.EnsureFileAccessible(InputFile
);
201 if (InputFile
.empty() || OutputFile
.empty())
202 isInteractive
= true;
205 for (i
= n
; i
< argc
;)
207 if (wxStrcmp(argv
[i
], _T("-winhelp")) == 0)
210 convertMode
= TEX_RTF
;
214 else if (wxStrcmp(argv
[i
], _T("-interactive")) == 0)
217 isInteractive
= true;
220 else if (wxStrcmp(argv
[i
], _T("-sync")) == 0) // Don't yield
225 else if (wxStrcmp(argv
[i
], _T("-rtf")) == 0)
228 convertMode
= TEX_RTF
;
230 else if (wxStrcmp(argv
[i
], _T("-html")) == 0)
233 convertMode
= TEX_HTML
;
235 else if (wxStrcmp(argv
[i
], _T("-xlp")) == 0)
238 convertMode
= TEX_XLP
;
240 else if (wxStrcmp(argv
[i
], _T("-twice")) == 0)
245 else if (wxStrcmp(argv
[i
], _T("-macros")) == 0)
250 MacroFile
= copystring(argv
[i
]);
254 else if (wxStrcmp(argv
[i
], _T("-bufsize")) == 0)
259 BufSize
= wxAtoi(argv
[i
]);
263 else if (wxStrcmp(argv
[i
], _T("-charset")) == 0)
270 if (wxStrcmp(s
, _T("ansi")) == 0 || wxStrcmp(s
, _T("pc")) == 0 || wxStrcmp(s
, _T("mac")) == 0 ||
271 wxStrcmp(s
, _T("pca")) == 0)
272 RTFCharset
= copystring(s
);
275 OnError(_T("Incorrect argument for -charset"));
280 else if (wxStrcmp(argv
[i
], _T("-checkcurlybraces")) == 0)
283 checkCurlyBraces
= true;
285 else if (wxStrcmp(argv
[i
], _T("-checkcurleybraces")) == 0)
287 // Support the old, incorrectly spelled version of -checkcurlybraces
288 // so that old scripts which run tex2rtf -checkcurleybraces still work.
290 checkCurlyBraces
= true;
292 else if (wxStrcmp(argv
[i
], _T("-checksyntax")) == 0)
297 else if (wxStrcmp(argv
[i
], _T("-version")) == 0)
305 buf
.Printf(_T("Invalid switch %s.\n"), argv
[i
]);
306 OnError((wxChar
*)buf
.c_str());
316 #if defined(__WXMSW__) && !defined(NO_GUI)
318 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
319 TheTex2RTFServer
= new Tex2RTFServer
;
320 TheTex2RTFServer
->Create(_T("TEX2RTF"));
323 TexInitialize(BufSize
);
324 ResetContentsLevels(0);
330 // Create the main frame window
331 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Tex2RTF"), wxDefaultPosition
, wxSize(400, 300));
333 frame
->CreateStatusBar(2);
334 #endif // wxUSE_STATUSBAR
337 // TODO: uncomment this when we have tex2rtf.xpm
338 frame
->SetIcon(wxICON(tex2rtf
));
340 if (!InputFile
.empty())
343 title
.Printf( _T("Tex2RTF [%s]"), wxFileNameFromPath(InputFile
).c_str());
344 frame
->SetTitle(title
);
348 wxMenu
*file_menu
= new wxMenu
;
349 file_menu
->Append(TEX_GO
, _T("&Go"), _T("Run converter"));
350 file_menu
->Append(TEX_SET_INPUT
, _T("Set &Input File"), _T("Set the LaTeX input file"));
351 file_menu
->Append(TEX_SET_OUTPUT
, _T("Set &Output File"), _T("Set the output file"));
352 file_menu
->AppendSeparator();
353 file_menu
->Append(TEX_VIEW_LATEX
, _T("View &LaTeX File"), _T("View the LaTeX input file"));
354 file_menu
->Append(TEX_VIEW_OUTPUT
, _T("View Output &File"), _T("View output file"));
355 file_menu
->Append(TEX_SAVE_FILE
, _T("&Save log file"), _T("Save displayed text into file"));
356 file_menu
->AppendSeparator();
357 file_menu
->Append(TEX_QUIT
, _T("E&xit"), _T("Exit Tex2RTF"));
359 wxMenu
*macro_menu
= new wxMenu
;
361 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, _T("&Load Custom Macros"), _T("Load custom LaTeX macro file"));
362 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, _T("View &Custom Macros"), _T("View custom LaTeX macros"));
364 wxMenu
*mode_menu
= new wxMenu
;
366 mode_menu
->Append(TEX_MODE_RTF
, _T("Output linear &RTF"), _T("Wordprocessor-compatible RTF"));
367 mode_menu
->Append(TEX_MODE_WINHELP
, _T("Output &WinHelp RTF"), _T("WinHelp-compatible RTF"));
368 mode_menu
->Append(TEX_MODE_HTML
, _T("Output &HTML"), _T("HTML World Wide Web hypertext file"));
369 mode_menu
->Append(TEX_MODE_XLP
, _T("Output &XLP"), _T("wxHelp hypertext help file"));
371 wxMenu
*options_menu
= new wxMenu
;
373 options_menu
->Append(TEX_OPTIONS_CURLY_BRACE
, _T("Curly brace matching"), _T("Checks for mismatched curly braces"),true);
374 options_menu
->Append(TEX_OPTIONS_SYNTAX_CHECKING
, _T("Syntax checking"), _T("Syntax checking for common errors"),true);
376 options_menu
->Check(TEX_OPTIONS_CURLY_BRACE
, checkCurlyBraces
);
377 options_menu
->Check(TEX_OPTIONS_SYNTAX_CHECKING
, checkSyntax
);
379 wxMenu
*help_menu
= new wxMenu
;
381 help_menu
->Append(TEX_HELP
, _T("&Help"), _T("Tex2RTF Contents Page"));
382 help_menu
->Append(TEX_ABOUT
, _T("&About Tex2RTF"), _T("About Tex2RTF"));
384 menuBar
= new wxMenuBar
;
385 menuBar
->Append(file_menu
, _T("&File"));
386 menuBar
->Append(macro_menu
, _T("&Macros"));
387 menuBar
->Append(mode_menu
, _T("&Conversion Mode"));
388 menuBar
->Append(options_menu
, _T("&Options"));
389 menuBar
->Append(help_menu
, _T("&Help"));
391 frame
->SetMenuBar(menuBar
);
392 frame
->textWindow
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_READONLY
|wxTE_MULTILINE
);
394 (*frame
->textWindow
) << _T("Welcome to Tex2RTF.\n");
398 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
399 HelpInstance
= new wxCHMHelpController
;
401 HelpInstance
= new wxHelpController
;
403 HelpInstance
->Initialize(_T("tex2rtf"));
407 * Read macro/initialisation file
411 wxString path
= TexPathList
.FindValidPath(MacroFile
);
413 ReadCustomMacros(path
);
416 wxString
inStr(_T("In "));
421 inStr
+= _T("WinHelp RTF");
423 inStr
+= _T("linear RTF");
435 inStr
+= _T("unknown");
438 inStr
+= _T(" mode.");
439 frame
->SetStatusText(inStr
, 1);
440 #endif // wxUSE_STATUSBAR
449 * Read macro/initialisation file
453 wxString path
= TexPathList
.FindValidPath(MacroFile
);
455 ReadCustomMacros(path
);
458 if ( rc
&& runTwice
)
465 OnExit(); // Do cleanup since OnExit won't be called now
474 wxNode
*node
= CustomMacroList
.GetFirst();
477 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
480 node
= CustomMacroList
.GetFirst();
482 MacroDefs
.BeginFind();
483 wxHashTable::Node
* mNode
= MacroDefs
.Next();
486 TexMacroDef
* def
= (TexMacroDef
*) mNode
->GetData();
488 mNode
= MacroDefs
.Next();
492 delete TheTex2RTFServer
;
507 delete currentArgData
;
508 currentArgData
= NULL
;
522 delete TexTmpBibName
;
523 TexTmpBibName
= NULL
;
537 delete TmpContentsName
;
538 TmpContentsName
= NULL
;
540 if (TmpFrameContentsName
)
542 delete TmpFrameContentsName
;
543 TmpFrameContentsName
= NULL
;
545 if (WinHelpContentsFileName
)
547 delete WinHelpContentsFileName
;
548 WinHelpContentsFileName
= NULL
;
572 delete [] BibliographyStyleString
;
573 delete [] DocumentStyleString
;
574 delete [] bitmapMethod
;
575 delete [] backgroundColourString
;
576 delete [] ContentsNameString
;
577 delete [] AbstractNameString
;
578 delete [] GlossaryNameString
;
579 delete [] ReferencesNameString
;
580 delete [] FiguresNameString
;
581 delete [] TablesNameString
;
582 delete [] FigureNameString
;
583 delete [] TableNameString
;
584 delete [] IndexNameString
;
585 delete [] ChapterNameString
;
586 delete [] SectionNameString
;
587 delete [] SubsectionNameString
;
588 delete [] SubsubsectionNameString
;
589 delete [] UpNameString
;
591 delete[] winHelpTitle
;
593 // TODO: this simulates zero-memory leaks!
594 // Otherwise there are just too many...
596 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
597 wxDebugContext::SetCheckpoint();
605 void ShowVersion(void)
608 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF version %.2f"), versionNo
);
612 void ShowOptions(void)
615 OnInform(_T("Usage: tex2rtf [input] [output] [switches]\n"));
616 OnInform(_T("where valid switches are"));
618 OnInform(_T(" -interactive"));
620 OnInform(_T(" -bufsize <size in K>"));
621 OnInform(_T(" -charset <pc | pca | ansi | mac> (default ansi)"));
622 OnInform(_T(" -twice"));
623 OnInform(_T(" -sync"));
624 OnInform(_T(" -checkcurlybraces"));
625 OnInform(_T(" -checksyntax"));
626 OnInform(_T(" -macros <filename>"));
627 OnInform(_T(" -winhelp"));
628 OnInform(_T(" -rtf"));
629 OnInform(_T(" -html"));
630 OnInform(_T(" -xlp\n"));
635 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
636 EVT_CLOSE(MyFrame::OnCloseWindow
)
637 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
638 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
639 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
640 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
641 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
642 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
643 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
644 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
645 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
646 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
647 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
648 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
649 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
650 EVT_MENU(TEX_OPTIONS_CURLY_BRACE
, MyFrame::OnOptionsCurlyBrace
)
651 EVT_MENU(TEX_OPTIONS_SYNTAX_CHECKING
, MyFrame::OnOptionsSyntaxChecking
)
652 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
653 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
656 // My frame constructor
657 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
658 wxFrame(frame
, id
, title
, pos
, size
)
661 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
663 if (!stopRunning
&& !OkToClose
)
675 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
681 void MyFrame::OnGo(wxCommandEvent
& WXUNUSED(event
))
685 menuBar
->EnableTop(0, false);
686 menuBar
->EnableTop(1, false);
687 menuBar
->EnableTop(2, false);
688 menuBar
->EnableTop(3, false);
696 SetStatusText(_T("Build aborted!"));
697 #endif // wxUSE_STATUSBAR
699 errBuf
.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount
);
700 OnInform((wxChar
*)errBuf
.c_str());
704 if (runTwice
&& !stopRunning
)
709 menuBar
->EnableTop(0, true);
710 menuBar
->EnableTop(1, true);
711 menuBar
->EnableTop(2, true);
712 menuBar
->EnableTop(3, true);
715 void MyFrame::OnSetInput(wxCommandEvent
& WXUNUSED(event
))
717 ChooseInputFile(true);
720 void MyFrame::OnSetOutput(wxCommandEvent
& WXUNUSED(event
))
722 ChooseOutputFile(true);
725 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
728 wxString s
= wxFileSelector(_T("Save text to file"), wxEmptyString
, wxEmptyString
, _T("txt"), _T("*.txt"));
731 textWindow
->SaveFile(s
);
734 wxSnprintf(buf
, sizeof(buf
), _T("Saved text to %s"), (const wxChar
*) s
.c_str());
735 frame
->SetStatusText(buf
, 0);
736 #endif // wxUSE_STATUSBAR
738 #endif // wxUSE_FILEDLG
741 void MyFrame::OnViewOutput(wxCommandEvent
& WXUNUSED(event
))
744 if (!OutputFile
.empty() && wxFileExists(OutputFile
))
746 textWindow
->LoadFile(OutputFile
);
748 wxString
str(wxFileNameFromPath(OutputFile
));
749 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
750 frame
->SetTitle(buf
);
754 void MyFrame::OnViewLatex(wxCommandEvent
& WXUNUSED(event
))
757 if (!InputFile
.empty() && wxFileExists(InputFile
))
759 textWindow
->LoadFile(InputFile
);
761 wxString
str(wxFileNameFromPath(OutputFile
));
762 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
763 frame
->SetTitle(buf
);
767 void MyFrame::OnLoadMacros(wxCommandEvent
& WXUNUSED(event
))
771 wxString s
= wxFileSelector(_T("Choose custom macro file"), wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), _T("ini"), _T("*.ini"));
772 if (!s
.empty() && wxFileExists(s
))
774 MacroFile
= copystring(s
);
778 #endif // wxUSE_FILEDLG
781 void MyFrame::OnShowMacros(wxCommandEvent
& WXUNUSED(event
))
788 void MyFrame::OnModeRTF(wxCommandEvent
& WXUNUSED(event
))
790 convertMode
= TEX_RTF
;
792 InputFile
= wxEmptyString
;
793 OutputFile
= wxEmptyString
;
795 SetStatusText(_T("In linear RTF mode."), 1);
796 #endif // wxUSE_STATUSBAR
799 void MyFrame::OnModeWinHelp(wxCommandEvent
& WXUNUSED(event
))
801 convertMode
= TEX_RTF
;
803 InputFile
= wxEmptyString
;
804 OutputFile
= wxEmptyString
;
806 SetStatusText(_T("In WinHelp RTF mode."), 1);
807 #endif // wxUSE_STATUSBAR
810 void MyFrame::OnModeHTML(wxCommandEvent
& WXUNUSED(event
))
812 convertMode
= TEX_HTML
;
814 InputFile
= wxEmptyString
;
815 OutputFile
= wxEmptyString
;
817 SetStatusText(_T("In HTML mode."), 1);
818 #endif // wxUSE_STATUSBAR
821 void MyFrame::OnModeXLP(wxCommandEvent
& WXUNUSED(event
))
823 convertMode
= TEX_XLP
;
824 InputFile
= wxEmptyString
;
825 OutputFile
= wxEmptyString
;
827 SetStatusText(_T("In XLP mode."), 1);
828 #endif // wxUSE_STATUSBAR
831 void MyFrame::OnOptionsCurlyBrace(wxCommandEvent
& WXUNUSED(event
))
833 checkCurlyBraces
= !checkCurlyBraces
;
835 if (checkCurlyBraces
)
837 SetStatusText(_T("Checking curly braces: YES"), 1);
841 SetStatusText(_T("Checking curly braces: NO"), 1);
843 #endif // wxUSE_STATUSBAR
847 void MyFrame::OnOptionsSyntaxChecking(wxCommandEvent
& WXUNUSED(event
))
849 checkSyntax
= !checkSyntax
;
853 SetStatusText(_T("Checking syntax: YES"), 1);
857 SetStatusText(_T("Checking syntax: NO"), 1);
859 #endif // wxUSE_STATUSBAR
863 void MyFrame::OnHelp(wxCommandEvent
& WXUNUSED(event
))
866 HelpInstance
->LoadFile();
867 HelpInstance
->DisplayContents();
871 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
874 wxString platform
= wxGetOsDescription();
875 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF Version %.2f %s\nLaTeX to RTF, WinHelp, and HTML Conversion\n\n(c) Julian Smart, George Tasker and others, 1999-2005"), versionNo
, platform
.c_str());
876 wxMessageBox(buf
, _T("About Tex2RTF"));
879 void ChooseInputFile(bool force
)
882 if (force
|| InputFile
.empty())
884 wxString s
= wxFileSelector(_T("Choose LaTeX input file"), wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), _T("tex"), _T("*.tex"));
887 // Different file, so clear index entries.
889 ResetContentsLevels(0);
894 wxString str
= wxFileNameFromPath(InputFile
);
896 buf
.Printf(_T("Tex2RTF [%s]"), str
.c_str());
897 frame
->SetTitle((wxChar
*)buf
.c_str());
898 OutputFile
= wxEmptyString
;
903 #endif // wxUSE_FILEDLG
906 void ChooseOutputFile(bool force
)
908 wxChar extensionBuf
[10];
910 wxStrcpy(wildBuf
, _T("*."));
912 if (!OutputFile
.empty())
913 path
= wxPathOnly(OutputFile
);
914 else if (!InputFile
.empty())
915 path
= wxPathOnly(InputFile
);
921 wxStrcpy(extensionBuf
, _T("rtf"));
922 wxStrcat(wildBuf
, _T("rtf"));
927 wxStrcpy(extensionBuf
, _T("xlp"));
928 wxStrcat(wildBuf
, _T("xlp"));
933 wxStrcpy(extensionBuf
, _T("html"));
934 wxStrcat(wildBuf
, _T("html"));
939 if (force
|| OutputFile
.empty())
941 wxString s
= wxFileSelector(_T("Choose output file"), path
, wxFileNameFromPath(OutputFile
),
942 extensionBuf
, wildBuf
);
948 #endif // wxUSE_FILEDLG
959 if (InputFile
.empty() || OutputFile
.empty() || stopRunning
)
966 wxString str
= wxFileNameFromPath(InputFile
);
968 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
969 frame
->SetTitle(buf
);
972 wxLongLong localTime
= wxGetLocalTimeMillis();
975 // Find extension-less filename
976 wxStrcpy(FileRoot
, OutputFile
.c_str());
977 StripExtension(FileRoot
);
979 if (truncateFilenames
&& convertMode
== TEX_HTML
)
981 // Truncate to five characters. This ensures that
982 // we can generate DOS filenames such as thing999. But 1000 files
983 // may not be enough, of course...
984 wxChar
* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
987 if(wxStrlen( sName
) > 5)
988 sName
[5] = '\0'; // that should do!
991 wxSnprintf(ContentsName
, 300, _T("%s.con"), FileRoot
);
992 wxSnprintf(TmpContentsName
, 300, _T("%s.cn1"), FileRoot
);
993 wxSnprintf(TmpFrameContentsName
, 300, _T("%s.frc"), FileRoot
);
994 wxSnprintf(WinHelpContentsFileName
, 300, _T("%s.cnt"), FileRoot
);
995 wxSnprintf(RefFileName
, 300, _T("%s.ref"), FileRoot
);
997 TexPathList
.EnsureFileAccessible(InputFile
);
1000 wxString s
= TexPathList
.FindValidPath(_T("bullet.bmp"));
1003 wxString str
= wxFileNameFromPath(s
);
1004 bulletFile
= copystring(str
);
1008 if (wxFileExists(RefFileName
))
1009 ReadTexReferences(RefFileName
);
1011 bool success
= false;
1013 if (!InputFile
.empty() && !OutputFile
.empty())
1015 if (!wxFileExists(InputFile
))
1017 OnError(_T("Cannot open input file!"));
1021 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1025 buf
.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber
);
1026 frame
->SetStatusText((wxChar
*)buf
.c_str());
1030 OnInform(_T("Reading LaTeX file..."));
1031 TexLoadFile(InputFile
);
1039 switch (convertMode
)
1060 OnInform(_T("*** Aborted by user."));
1062 stopRunning
= false;
1068 WriteTexReferences(RefFileName
);
1070 startedSections
= false;
1074 wxLongLong elapsed
= wxGetLocalTimeMillis() - localTime
;
1075 buf
.Printf(_T("Finished PASS #%d in %ld seconds.\n"), passNumber
, (long)(elapsed
.GetLo()/1000.0));
1076 OnInform((wxChar
*)buf
.c_str());
1080 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1081 OnInform((wxChar
*)buf
.c_str());
1087 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1088 frame
->SetStatusText((wxChar
*)buf
.c_str());
1090 #endif // wxUSE_STATUSBAR
1092 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1093 OnInform((wxChar
*)buf
.c_str());
1096 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1097 OnInform((wxChar
*)buf
.c_str());
1107 startedSections
= false;
1109 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1110 frame
->SetStatusText(_T("Aborted by user."));
1113 OnInform(_T("Sorry, unsuccessful."));
1118 void OnError(const wxChar
*msg
)
1120 wxString msg_string
= msg
;
1124 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1127 if (isInteractive
&& frame
)
1129 (*frame
->textWindow
) << _T("Error: ") << msg
<< _T("\n");
1133 #if defined(__UNIX__)
1134 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1136 #elif defined(__WXMSW__)
1145 void OnInform(const wxChar
*msg
)
1147 wxString msg_string
= msg
;
1149 wxSTD cout
<< msg_string
.mb_str() << "\n";
1152 if (isInteractive
&& frame
)
1154 (*frame
->textWindow
) << msg
<< _T("\n");
1158 #if defined(__UNIX__)
1159 wxSTD cout
<< msg_string
.mb_str() << "\n";
1161 #elif defined(__WXMSW__)
1173 void OnMacro(int macroId
, int no_args
, bool start
)
1175 switch (convertMode
)
1179 RTFOnMacro(macroId
, no_args
, start
);
1184 XLPOnMacro(macroId
, no_args
, start
);
1189 HTMLOnMacro(macroId
, no_args
, start
);
1195 bool OnArgument(int macroId
, int arg_no
, bool start
)
1197 switch (convertMode
)
1201 return RTFOnArgument(macroId
, arg_no
, start
);
1206 return XLPOnArgument(macroId
, arg_no
, start
);
1211 return HTMLOnArgument(macroId
, arg_no
, start
);
1221 #if defined(__WXMSW__) && !defined(NO_GUI)
1227 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
1229 if (topic
== _T("TEX2RTF"))
1232 ipc_buffer
= new wxChar
[1000];
1234 return new Tex2RTFConnection(ipc_buffer
, 4000);
1244 Tex2RTFConnection::Tex2RTFConnection(wxChar
*buf
, int size
):wxDDEConnection(buf
, size
)
1248 bool SplitCommand(wxChar
*data
, wxChar
*firstArg
, wxChar
*secondArg
)
1254 // Find first argument (command name)
1257 if (data
[i
] == ' ' || data
[i
] == 0)
1261 firstArg
[i
] = data
[i
];
1268 // Find second argument
1271 while (data
[i
] != 0)
1273 secondArg
[j
] = data
[i
];
1282 bool Tex2RTFConnection::OnExecute(const wxString
& WXUNUSED(topic
), wxChar
*data
, int WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1284 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1286 wxChar firstArg
[50];
1287 wxChar secondArg
[300];
1288 if (SplitCommand(data
, firstArg
, secondArg
))
1290 bool hasArg
= (wxStrlen(secondArg
) > 0);
1291 if (wxStrcmp(firstArg
, _T("INPUT")) == 0 && hasArg
)
1293 InputFile
= secondArg
;
1297 wxString str
= wxFileNameFromPath(InputFile
);
1298 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
1299 frame
->SetTitle(buf
);
1302 else if (wxStrcmp(firstArg
, _T("OUTPUT")) == 0 && hasArg
)
1304 OutputFile
= secondArg
;
1306 else if (wxStrcmp(firstArg
, _T("GO")) == 0)
1308 wxStrcpy(Tex2RTFLastStatus
, _T("WORKING"));
1310 wxStrcpy(Tex2RTFLastStatus
, _T("CONVERSION ERROR"));
1312 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1314 else if (wxStrcmp(firstArg
, _T("EXIT")) == 0)
1316 if (frame
) frame
->Close();
1318 else if (wxStrcmp(firstArg
, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg
, _T("ICONIZE")) == 0)
1321 frame
->Iconize(true);
1323 else if (wxStrcmp(firstArg
, _T("SHOW")) == 0 || wxStrcmp(firstArg
, _T("RESTORE")) == 0)
1327 frame
->Iconize(false);
1333 // Try for a setting
1334 wxStrcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, false));
1335 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1336 if (frame
&& wxStrcmp(firstArg
, _T("conversionMode")) == 0)
1339 wxStrcpy(buf
, _T("In "));
1341 if (winHelp
&& (convertMode
== TEX_RTF
))
1342 wxStrcat(buf
, _T("WinHelp RTF"));
1343 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1344 wxStrcat(buf
, _T("linear RTF"));
1345 else if (convertMode
== TEX_HTML
) wxStrcat(buf
, _T("HTML"));
1346 else if (convertMode
== TEX_XLP
) wxStrcat(buf
, _T("XLP"));
1347 wxStrcat(buf
, _T(" mode."));
1348 frame
->SetStatusText(buf
, 1);
1356 wxChar
*Tex2RTFConnection::OnRequest(const wxString
& WXUNUSED(topic
), const wxString
& WXUNUSED(item
), int *WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1358 return Tex2RTFLastStatus
;
1365 //void wxObject::Dump(wxSTD ostream& str)
1367 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1368 // str << GetClassInfo()->GetClassName();
1370 // str << "unknown object class";