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);
137 wxChar wxTex2RTFBuffer
[1500];
140 IMPLEMENT_APP_CONSOLE(MyApp
)
142 wxMenuBar
*menuBar
= NULL
;
143 MyFrame
*frame
= NULL
;
144 // DECLARE_APP(MyApp)
148 // `Main program' equivalent, creating windows and returning main app frame
151 // Use default list of macros defined in tex2any.cc
152 DefineDefaultMacros();
153 AddMacroDef(ltHARDY
, _T("hardy"), 0);
155 FileRoot
= new wxChar
[300];
156 ContentsName
= new wxChar
[300];
157 TmpContentsName
= new wxChar
[300];
158 TmpFrameContentsName
= new wxChar
[300];
159 WinHelpContentsFileName
= new wxChar
[300];
160 RefFileName
= new wxChar
[300];
162 WX_CLEAR_LIST(wxList
,ColourTable
);
166 // Read input/output files
169 if (argv
[1][0] != _T('-'))
176 if (argv
[2][0] != _T('-'))
178 OutputFile
= argv
[2];
185 TexPathList
.Add(::wxGetCwd());
188 if (InputFile
.empty() || OutputFile
.empty())
190 wxSTD cout
<< "Tex2RTF: input or output file is missing.\n";
196 if (!InputFile
.empty())
198 TexPathList
.EnsureFileAccessible(InputFile
);
200 if (InputFile
.empty() || OutputFile
.empty())
201 isInteractive
= true;
204 for (i
= n
; i
< argc
;)
206 if (wxStrcmp(argv
[i
], _T("-winhelp")) == 0)
209 convertMode
= TEX_RTF
;
213 else if (wxStrcmp(argv
[i
], _T("-interactive")) == 0)
216 isInteractive
= true;
219 else if (wxStrcmp(argv
[i
], _T("-sync")) == 0) // Don't yield
224 else if (wxStrcmp(argv
[i
], _T("-rtf")) == 0)
227 convertMode
= TEX_RTF
;
229 else if (wxStrcmp(argv
[i
], _T("-html")) == 0)
232 convertMode
= TEX_HTML
;
234 else if (wxStrcmp(argv
[i
], _T("-xlp")) == 0)
237 convertMode
= TEX_XLP
;
239 else if (wxStrcmp(argv
[i
], _T("-twice")) == 0)
244 else if (wxStrcmp(argv
[i
], _T("-macros")) == 0)
249 MacroFile
= copystring(argv
[i
]);
253 else if (wxStrcmp(argv
[i
], _T("-bufsize")) == 0)
258 BufSize
= wxAtoi(argv
[i
]);
262 else if (wxStrcmp(argv
[i
], _T("-charset")) == 0)
269 if (wxStrcmp(s
, _T("ansi")) == 0 || wxStrcmp(s
, _T("pc")) == 0 || wxStrcmp(s
, _T("mac")) == 0 ||
270 wxStrcmp(s
, _T("pca")) == 0)
271 RTFCharset
= copystring(s
);
274 OnError(_T("Incorrect argument for -charset"));
279 else if (wxStrcmp(argv
[i
], _T("-checkcurlybraces")) == 0)
282 checkCurlyBraces
= true;
284 else if (wxStrcmp(argv
[i
], _T("-checkcurleybraces")) == 0)
286 // Support the old, incorrectly spelled version of -checkcurlybraces
287 // so that old scripts which run tex2rtf -checkcurleybraces still work.
289 checkCurlyBraces
= true;
291 else if (wxStrcmp(argv
[i
], _T("-checksyntax")) == 0)
299 buf
.Printf(_T("Invalid switch %s.\n"), argv
[i
]);
300 OnError((wxChar
*)buf
.c_str());
310 #if defined(__WXMSW__) && !defined(NO_GUI)
312 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
313 TheTex2RTFServer
= new Tex2RTFServer
;
314 TheTex2RTFServer
->Create(_T("TEX2RTF"));
317 TexInitialize(BufSize
);
318 ResetContentsLevels(0);
324 // Create the main frame window
325 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Tex2RTF"), wxDefaultPosition
, wxSize(400, 300));
327 frame
->CreateStatusBar(2);
328 #endif // wxUSE_STATUSBAR
331 // TODO: uncomment this when we have tex2rtf.xpm
332 frame
->SetIcon(wxICON(tex2rtf
));
334 if (!InputFile
.empty())
337 title
.Printf( _T("Tex2RTF [%s]"), wxFileNameFromPath(InputFile
).c_str());
338 frame
->SetTitle(title
);
342 wxMenu
*file_menu
= new wxMenu
;
343 file_menu
->Append(TEX_GO
, _T("&Go"), _T("Run converter"));
344 file_menu
->Append(TEX_SET_INPUT
, _T("Set &Input File"), _T("Set the LaTeX input file"));
345 file_menu
->Append(TEX_SET_OUTPUT
, _T("Set &Output File"), _T("Set the output file"));
346 file_menu
->AppendSeparator();
347 file_menu
->Append(TEX_VIEW_LATEX
, _T("View &LaTeX File"), _T("View the LaTeX input file"));
348 file_menu
->Append(TEX_VIEW_OUTPUT
, _T("View Output &File"), _T("View output file"));
349 file_menu
->Append(TEX_SAVE_FILE
, _T("&Save log file"), _T("Save displayed text into file"));
350 file_menu
->AppendSeparator();
351 file_menu
->Append(TEX_QUIT
, _T("E&xit"), _T("Exit Tex2RTF"));
353 wxMenu
*macro_menu
= new wxMenu
;
355 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, _T("&Load Custom Macros"), _T("Load custom LaTeX macro file"));
356 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, _T("View &Custom Macros"), _T("View custom LaTeX macros"));
358 wxMenu
*mode_menu
= new wxMenu
;
360 mode_menu
->Append(TEX_MODE_RTF
, _T("Output linear &RTF"), _T("Wordprocessor-compatible RTF"));
361 mode_menu
->Append(TEX_MODE_WINHELP
, _T("Output &WinHelp RTF"), _T("WinHelp-compatible RTF"));
362 mode_menu
->Append(TEX_MODE_HTML
, _T("Output &HTML"), _T("HTML World Wide Web hypertext file"));
363 mode_menu
->Append(TEX_MODE_XLP
, _T("Output &XLP"), _T("wxHelp hypertext help file"));
365 wxMenu
*options_menu
= new wxMenu
;
367 options_menu
->Append(TEX_OPTIONS_CURLY_BRACE
, _T("Curly brace matching"), _T("Checks for mismatched curly braces"),true);
368 options_menu
->Append(TEX_OPTIONS_SYNTAX_CHECKING
, _T("Syntax checking"), _T("Syntax checking for common errors"),true);
370 options_menu
->Check(TEX_OPTIONS_CURLY_BRACE
, checkCurlyBraces
);
371 options_menu
->Check(TEX_OPTIONS_SYNTAX_CHECKING
, checkSyntax
);
373 wxMenu
*help_menu
= new wxMenu
;
375 help_menu
->Append(TEX_HELP
, _T("&Help"), _T("Tex2RTF Contents Page"));
376 help_menu
->Append(TEX_ABOUT
, _T("&About Tex2RTF"), _T("About Tex2RTF"));
378 menuBar
= new wxMenuBar
;
379 menuBar
->Append(file_menu
, _T("&File"));
380 menuBar
->Append(macro_menu
, _T("&Macros"));
381 menuBar
->Append(mode_menu
, _T("&Conversion Mode"));
382 menuBar
->Append(options_menu
, _T("&Options"));
383 menuBar
->Append(help_menu
, _T("&Help"));
385 frame
->SetMenuBar(menuBar
);
386 frame
->textWindow
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_READONLY
|wxTE_MULTILINE
);
388 (*frame
->textWindow
) << _T("Welcome to Tex2RTF.\n");
392 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
393 HelpInstance
= new wxCHMHelpController
;
395 HelpInstance
= new wxHelpController
;
397 HelpInstance
->Initialize(_T("tex2rtf"));
401 * Read macro/initialisation file
405 wxString path
= TexPathList
.FindValidPath(MacroFile
);
407 ReadCustomMacros(path
);
410 wxString
inStr(_T("In "));
415 inStr
+= _T("WinHelp RTF");
417 inStr
+= _T("linear RTF");
429 inStr
+= _T("unknown");
432 inStr
+= _T(" mode.");
433 frame
->SetStatusText(inStr
, 1);
434 #endif // wxUSE_STATUSBAR
443 * Read macro/initialisation file
447 wxString path
= TexPathList
.FindValidPath(MacroFile
);
449 ReadCustomMacros(path
);
452 if ( rc
&& runTwice
)
459 OnExit(); // Do cleanup since OnExit won't be called now
468 wxNode
*node
= CustomMacroList
.GetFirst();
471 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
474 node
= CustomMacroList
.GetFirst();
476 MacroDefs
.BeginFind();
477 wxHashTable::Node
* mNode
= MacroDefs
.Next();
480 TexMacroDef
* def
= (TexMacroDef
*) mNode
->GetData();
482 mNode
= MacroDefs
.Next();
486 delete TheTex2RTFServer
;
501 delete currentArgData
;
502 currentArgData
= NULL
;
516 delete TexTmpBibName
;
517 TexTmpBibName
= NULL
;
531 delete TmpContentsName
;
532 TmpContentsName
= NULL
;
534 if (TmpFrameContentsName
)
536 delete TmpFrameContentsName
;
537 TmpFrameContentsName
= NULL
;
539 if (WinHelpContentsFileName
)
541 delete WinHelpContentsFileName
;
542 WinHelpContentsFileName
= NULL
;
566 delete [] BibliographyStyleString
;
567 delete [] DocumentStyleString
;
568 delete [] bitmapMethod
;
569 delete [] backgroundColourString
;
570 delete [] ContentsNameString
;
571 delete [] AbstractNameString
;
572 delete [] GlossaryNameString
;
573 delete [] ReferencesNameString
;
574 delete [] FiguresNameString
;
575 delete [] TablesNameString
;
576 delete [] FigureNameString
;
577 delete [] TableNameString
;
578 delete [] IndexNameString
;
579 delete [] ChapterNameString
;
580 delete [] SectionNameString
;
581 delete [] SubsectionNameString
;
582 delete [] SubsubsectionNameString
;
583 delete [] UpNameString
;
585 delete[] winHelpTitle
;
587 // TODO: this simulates zero-memory leaks!
588 // Otherwise there are just too many...
590 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
591 wxDebugContext::SetCheckpoint();
598 void ShowOptions(void)
601 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF version %.2f"), versionNo
);
603 OnInform(_T("Usage: tex2rtf [input] [output] [switches]\n"));
604 OnInform(_T("where valid switches are"));
606 OnInform(_T(" -interactive"));
608 OnInform(_T(" -bufsize <size in K>"));
609 OnInform(_T(" -charset <pc | pca | ansi | mac> (default ansi)"));
610 OnInform(_T(" -twice"));
611 OnInform(_T(" -sync"));
612 OnInform(_T(" -checkcurlybraces"));
613 OnInform(_T(" -checksyntax"));
614 OnInform(_T(" -macros <filename>"));
615 OnInform(_T(" -winhelp"));
616 OnInform(_T(" -rtf"));
617 OnInform(_T(" -html"));
618 OnInform(_T(" -xlp\n"));
623 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
624 EVT_CLOSE(MyFrame::OnCloseWindow
)
625 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
626 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
627 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
628 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
629 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
630 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
631 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
632 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
633 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
634 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
635 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
636 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
637 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
638 EVT_MENU(TEX_OPTIONS_CURLY_BRACE
, MyFrame::OnOptionsCurlyBrace
)
639 EVT_MENU(TEX_OPTIONS_SYNTAX_CHECKING
, MyFrame::OnOptionsSyntaxChecking
)
640 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
641 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
644 // My frame constructor
645 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
646 wxFrame(frame
, id
, title
, pos
, size
)
649 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
651 if (!stopRunning
&& !OkToClose
)
663 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
669 void MyFrame::OnGo(wxCommandEvent
& WXUNUSED(event
))
673 menuBar
->EnableTop(0, false);
674 menuBar
->EnableTop(1, false);
675 menuBar
->EnableTop(2, false);
676 menuBar
->EnableTop(3, false);
684 SetStatusText(_T("Build aborted!"));
685 #endif // wxUSE_STATUSBAR
687 errBuf
.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount
);
688 OnInform((wxChar
*)errBuf
.c_str());
692 if (runTwice
&& !stopRunning
)
697 menuBar
->EnableTop(0, true);
698 menuBar
->EnableTop(1, true);
699 menuBar
->EnableTop(2, true);
700 menuBar
->EnableTop(3, true);
703 void MyFrame::OnSetInput(wxCommandEvent
& WXUNUSED(event
))
705 ChooseInputFile(true);
708 void MyFrame::OnSetOutput(wxCommandEvent
& WXUNUSED(event
))
710 ChooseOutputFile(true);
713 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
716 wxString s
= wxFileSelector(_T("Save text to file"), wxEmptyString
, wxEmptyString
, _T("txt"), _T("*.txt"));
719 textWindow
->SaveFile(s
);
722 wxSnprintf(buf
, sizeof(buf
), _T("Saved text to %s"), (const wxChar
*) s
.c_str());
723 frame
->SetStatusText(buf
, 0);
724 #endif // wxUSE_STATUSBAR
726 #endif // wxUSE_FILEDLG
729 void MyFrame::OnViewOutput(wxCommandEvent
& WXUNUSED(event
))
732 if (!OutputFile
.empty() && wxFileExists(OutputFile
))
734 textWindow
->LoadFile(OutputFile
);
736 wxString
str(wxFileNameFromPath(OutputFile
));
737 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
738 frame
->SetTitle(buf
);
742 void MyFrame::OnViewLatex(wxCommandEvent
& WXUNUSED(event
))
745 if (!InputFile
.empty() && wxFileExists(InputFile
))
747 textWindow
->LoadFile(InputFile
);
749 wxString
str(wxFileNameFromPath(OutputFile
));
750 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
751 frame
->SetTitle(buf
);
755 void MyFrame::OnLoadMacros(wxCommandEvent
& WXUNUSED(event
))
759 wxString s
= wxFileSelector(_T("Choose custom macro file"), wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), _T("ini"), _T("*.ini"));
760 if (!s
.empty() && wxFileExists(s
))
762 MacroFile
= copystring(s
);
766 #endif // wxUSE_FILEDLG
769 void MyFrame::OnShowMacros(wxCommandEvent
& WXUNUSED(event
))
776 void MyFrame::OnModeRTF(wxCommandEvent
& WXUNUSED(event
))
778 convertMode
= TEX_RTF
;
780 InputFile
= wxEmptyString
;
781 OutputFile
= wxEmptyString
;
783 SetStatusText(_T("In linear RTF mode."), 1);
784 #endif // wxUSE_STATUSBAR
787 void MyFrame::OnModeWinHelp(wxCommandEvent
& WXUNUSED(event
))
789 convertMode
= TEX_RTF
;
791 InputFile
= wxEmptyString
;
792 OutputFile
= wxEmptyString
;
794 SetStatusText(_T("In WinHelp RTF mode."), 1);
795 #endif // wxUSE_STATUSBAR
798 void MyFrame::OnModeHTML(wxCommandEvent
& WXUNUSED(event
))
800 convertMode
= TEX_HTML
;
802 InputFile
= wxEmptyString
;
803 OutputFile
= wxEmptyString
;
805 SetStatusText(_T("In HTML mode."), 1);
806 #endif // wxUSE_STATUSBAR
809 void MyFrame::OnModeXLP(wxCommandEvent
& WXUNUSED(event
))
811 convertMode
= TEX_XLP
;
812 InputFile
= wxEmptyString
;
813 OutputFile
= wxEmptyString
;
815 SetStatusText(_T("In XLP mode."), 1);
816 #endif // wxUSE_STATUSBAR
819 void MyFrame::OnOptionsCurlyBrace(wxCommandEvent
& WXUNUSED(event
))
821 checkCurlyBraces
= !checkCurlyBraces
;
823 if (checkCurlyBraces
)
825 SetStatusText(_T("Checking curly braces: YES"), 1);
829 SetStatusText(_T("Checking curly braces: NO"), 1);
831 #endif // wxUSE_STATUSBAR
835 void MyFrame::OnOptionsSyntaxChecking(wxCommandEvent
& WXUNUSED(event
))
837 checkSyntax
= !checkSyntax
;
841 SetStatusText(_T("Checking syntax: YES"), 1);
845 SetStatusText(_T("Checking syntax: NO"), 1);
847 #endif // wxUSE_STATUSBAR
851 void MyFrame::OnHelp(wxCommandEvent
& WXUNUSED(event
))
854 HelpInstance
->LoadFile();
855 HelpInstance
->DisplayContents();
859 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
862 wxString platform
= wxGetOsDescription();
863 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());
864 wxMessageBox(buf
, _T("About Tex2RTF"));
867 void ChooseInputFile(bool force
)
870 if (force
|| InputFile
.empty())
872 wxString s
= wxFileSelector(_T("Choose LaTeX input file"), wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), _T("tex"), _T("*.tex"));
875 // Different file, so clear index entries.
877 ResetContentsLevels(0);
882 wxString str
= wxFileNameFromPath(InputFile
);
884 buf
.Printf(_T("Tex2RTF [%s]"), str
.c_str());
885 frame
->SetTitle((wxChar
*)buf
.c_str());
886 OutputFile
= wxEmptyString
;
891 #endif // wxUSE_FILEDLG
894 void ChooseOutputFile(bool force
)
896 wxChar extensionBuf
[10];
898 wxStrcpy(wildBuf
, _T("*."));
900 if (!OutputFile
.empty())
901 path
= wxPathOnly(OutputFile
);
902 else if (!InputFile
.empty())
903 path
= wxPathOnly(InputFile
);
909 wxStrcpy(extensionBuf
, _T("rtf"));
910 wxStrcat(wildBuf
, _T("rtf"));
915 wxStrcpy(extensionBuf
, _T("xlp"));
916 wxStrcat(wildBuf
, _T("xlp"));
921 wxStrcpy(extensionBuf
, _T("html"));
922 wxStrcat(wildBuf
, _T("html"));
927 if (force
|| OutputFile
.empty())
929 wxString s
= wxFileSelector(_T("Choose output file"), path
, wxFileNameFromPath(OutputFile
),
930 extensionBuf
, wildBuf
);
936 #endif // wxUSE_FILEDLG
947 if (InputFile
.empty() || OutputFile
.empty() || stopRunning
)
954 wxString str
= wxFileNameFromPath(InputFile
);
956 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
957 frame
->SetTitle(buf
);
960 wxLongLong localTime
= wxGetLocalTimeMillis();
963 // Find extension-less filename
964 wxStrcpy(FileRoot
, OutputFile
.c_str());
965 StripExtension(FileRoot
);
967 if (truncateFilenames
&& convertMode
== TEX_HTML
)
969 // Truncate to five characters. This ensures that
970 // we can generate DOS filenames such as thing999. But 1000 files
971 // may not be enough, of course...
972 wxChar
* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
975 if(wxStrlen( sName
) > 5)
976 sName
[5] = '\0'; // that should do!
979 wxSnprintf(ContentsName
, 300, _T("%s.con"), FileRoot
);
980 wxSnprintf(TmpContentsName
, 300, _T("%s.cn1"), FileRoot
);
981 wxSnprintf(TmpFrameContentsName
, 300, _T("%s.frc"), FileRoot
);
982 wxSnprintf(WinHelpContentsFileName
, 300, _T("%s.cnt"), FileRoot
);
983 wxSnprintf(RefFileName
, 300, _T("%s.ref"), FileRoot
);
985 TexPathList
.EnsureFileAccessible(InputFile
);
988 wxString s
= TexPathList
.FindValidPath(_T("bullet.bmp"));
991 wxString str
= wxFileNameFromPath(s
);
992 bulletFile
= copystring(str
);
996 if (wxFileExists(RefFileName
))
997 ReadTexReferences(RefFileName
);
999 bool success
= false;
1001 if (!InputFile
.empty() && !OutputFile
.empty())
1003 if (!wxFileExists(InputFile
))
1005 OnError(_T("Cannot open input file!"));
1009 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1013 buf
.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber
);
1014 frame
->SetStatusText((wxChar
*)buf
.c_str());
1018 OnInform(_T("Reading LaTeX file..."));
1019 TexLoadFile(InputFile
);
1027 switch (convertMode
)
1048 OnInform(_T("*** Aborted by user."));
1050 stopRunning
= false;
1056 WriteTexReferences(RefFileName
);
1058 startedSections
= false;
1062 wxLongLong elapsed
= wxGetLocalTimeMillis() - localTime
;
1063 buf
.Printf(_T("Finished PASS #%d in %ld seconds.\n"), passNumber
, (long)(elapsed
.GetLo()/1000.0));
1064 OnInform((wxChar
*)buf
.c_str());
1068 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1069 OnInform((wxChar
*)buf
.c_str());
1075 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1076 frame
->SetStatusText((wxChar
*)buf
.c_str());
1078 #endif // wxUSE_STATUSBAR
1080 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1081 OnInform((wxChar
*)buf
.c_str());
1084 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1085 OnInform((wxChar
*)buf
.c_str());
1095 startedSections
= false;
1097 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1098 frame
->SetStatusText(_T("Aborted by user."));
1101 OnInform(_T("Sorry, unsuccessful."));
1106 void OnError(const wxChar
*msg
)
1108 wxString msg_string
= msg
;
1112 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1115 if (isInteractive
&& frame
)
1117 (*frame
->textWindow
) << _T("Error: ") << msg
<< _T("\n");
1121 #if defined(__UNIX__)
1122 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1124 #elif defined(__WXMSW__)
1133 void OnInform(const wxChar
*msg
)
1135 wxString msg_string
= msg
;
1137 wxSTD cout
<< msg_string
.mb_str() << "\n";
1140 if (isInteractive
&& frame
)
1142 (*frame
->textWindow
) << msg
<< _T("\n");
1146 #if defined(__UNIX__)
1147 wxSTD cout
<< msg_string
.mb_str() << "\n";
1149 #elif defined(__WXMSW__)
1161 void OnMacro(int macroId
, int no_args
, bool start
)
1163 switch (convertMode
)
1167 RTFOnMacro(macroId
, no_args
, start
);
1172 XLPOnMacro(macroId
, no_args
, start
);
1177 HTMLOnMacro(macroId
, no_args
, start
);
1183 bool OnArgument(int macroId
, int arg_no
, bool start
)
1185 switch (convertMode
)
1189 return RTFOnArgument(macroId
, arg_no
, start
);
1194 return XLPOnArgument(macroId
, arg_no
, start
);
1199 return HTMLOnArgument(macroId
, arg_no
, start
);
1209 #if defined(__WXMSW__) && !defined(NO_GUI)
1215 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
1217 if (topic
== _T("TEX2RTF"))
1220 ipc_buffer
= new wxChar
[1000];
1222 return new Tex2RTFConnection(ipc_buffer
, 4000);
1232 Tex2RTFConnection::Tex2RTFConnection(wxChar
*buf
, int size
):wxDDEConnection(buf
, size
)
1236 bool SplitCommand(wxChar
*data
, wxChar
*firstArg
, wxChar
*secondArg
)
1242 // Find first argument (command name)
1245 if (data
[i
] == ' ' || data
[i
] == 0)
1249 firstArg
[i
] = data
[i
];
1256 // Find second argument
1259 while (data
[i
] != 0)
1261 secondArg
[j
] = data
[i
];
1270 bool Tex2RTFConnection::OnExecute(const wxString
& WXUNUSED(topic
), wxChar
*data
, int WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1272 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1274 wxChar firstArg
[50];
1275 wxChar secondArg
[300];
1276 if (SplitCommand(data
, firstArg
, secondArg
))
1278 bool hasArg
= (wxStrlen(secondArg
) > 0);
1279 if (wxStrcmp(firstArg
, _T("INPUT")) == 0 && hasArg
)
1281 InputFile
= secondArg
;
1285 wxString str
= wxFileNameFromPath(InputFile
);
1286 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
1287 frame
->SetTitle(buf
);
1290 else if (wxStrcmp(firstArg
, _T("OUTPUT")) == 0 && hasArg
)
1292 OutputFile
= secondArg
;
1294 else if (wxStrcmp(firstArg
, _T("GO")) == 0)
1296 wxStrcpy(Tex2RTFLastStatus
, _T("WORKING"));
1298 wxStrcpy(Tex2RTFLastStatus
, _T("CONVERSION ERROR"));
1300 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1302 else if (wxStrcmp(firstArg
, _T("EXIT")) == 0)
1304 if (frame
) frame
->Close();
1306 else if (wxStrcmp(firstArg
, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg
, _T("ICONIZE")) == 0)
1309 frame
->Iconize(true);
1311 else if (wxStrcmp(firstArg
, _T("SHOW")) == 0 || wxStrcmp(firstArg
, _T("RESTORE")) == 0)
1315 frame
->Iconize(false);
1321 // Try for a setting
1322 wxStrcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, false));
1323 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1324 if (frame
&& wxStrcmp(firstArg
, _T("conversionMode")) == 0)
1327 wxStrcpy(buf
, _T("In "));
1329 if (winHelp
&& (convertMode
== TEX_RTF
))
1330 wxStrcat(buf
, _T("WinHelp RTF"));
1331 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1332 wxStrcat(buf
, _T("linear RTF"));
1333 else if (convertMode
== TEX_HTML
) wxStrcat(buf
, _T("HTML"));
1334 else if (convertMode
== TEX_XLP
) wxStrcat(buf
, _T("XLP"));
1335 wxStrcat(buf
, _T(" mode."));
1336 frame
->SetStatusText(buf
, 1);
1344 wxChar
*Tex2RTFConnection::OnRequest(const wxString
& WXUNUSED(topic
), const wxString
& WXUNUSED(item
), int *WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1346 return Tex2RTFLastStatus
;
1353 //void wxObject::Dump(wxSTD ostream& str)
1355 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1356 // str << GetClassInfo()->GetClassName();
1358 // str << "unknown object class";