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 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
24 #if defined(__WXMSW__)
25 #include "wx/msw/wrapwin.h"
31 #include "wx/textctrl.h"
32 #include "wx/filedlg.h"
33 #include "wx/msgdlg.h"
43 #include "wx/cshelp.h"
44 #include "wx/helphtml.h"
46 #include "wx/msw/helpchm.h"
48 #include "wx/html/helpctrl.h"
69 #if (defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXX11__) || defined(__WXMGL__)) && !defined(NO_GUI)
70 #include "tex2rtf.xpm"
73 #if !WXWIN_COMPATIBILITY_2_4
74 static inline wxChar
* copystring(const wxChar
* s
)
75 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
78 const float versionNo
= float(TEX2RTF_VERSION_NUMBER
);
80 TexChunk
*currentMember
= NULL
;
81 bool startedSections
= false;
82 wxChar
*contentsString
= NULL
;
83 bool suppressNameDecoration
= false;
84 bool OkToClose
= true;
86 unsigned long errorCount
= 0;
90 extern wxChar
*BigBuffer
;
91 extern wxChar
*TexFileRoot
;
92 extern wxChar
*TexBibName
; // Bibliography output file name
93 extern wxChar
*TexTmpBibName
; // Temporary bibliography output file name
94 extern wxList ColourTable
;
95 extern TexChunk
*TopLevel
;
98 wxHelpControllerBase
*HelpInstance
= NULL
;
102 static wxChar
*ipc_buffer
= NULL
;
103 static wxChar Tex2RTFLastStatus
[100];
104 Tex2RTFServer
*TheTex2RTFServer
= NULL
;
109 wxChar
*bulletFile
= NULL
;
111 FILE *Contents
= NULL
; // Contents page
112 FILE *Chapters
= NULL
; // Chapters (WinHelp RTF) or rest of file (linear RTF)
113 FILE *Sections
= NULL
;
114 FILE *Subsections
= NULL
;
115 FILE *Subsubsections
= NULL
;
117 FILE *WinHelpContentsFile
= NULL
;
121 wxChar
*MacroFile
= copystring(_T("tex2rtf.ini"));
123 wxChar
*FileRoot
= NULL
;
124 wxChar
*ContentsName
= NULL
; // Contents page from last time around
125 wxChar
*TmpContentsName
= NULL
; // Current contents page
126 wxChar
*TmpFrameContentsName
= NULL
; // Current frame contents page
127 wxChar
*WinHelpContentsFileName
= NULL
; // WinHelp .cnt file
128 wxChar
*RefFileName
= NULL
; // Reference file name
130 wxChar
*RTFCharset
= copystring(_T("ansi"));
133 int BufSize
= 100; // Size of buffer in K
139 void ShowOptions(void);
141 wxChar wxTex2RTFBuffer
[1500];
144 IMPLEMENT_APP_CONSOLE(MyApp
)
146 wxMenuBar
*menuBar
= NULL
;
147 MyFrame
*frame
= NULL
;
148 // DECLARE_APP(MyApp)
152 // `Main program' equivalent, creating windows and returning main app frame
155 // Use default list of macros defined in tex2any.cc
156 DefineDefaultMacros();
157 AddMacroDef(ltHARDY
, _T("hardy"), 0);
159 FileRoot
= new wxChar
[300];
160 ContentsName
= new wxChar
[300];
161 TmpContentsName
= new wxChar
[300];
162 TmpFrameContentsName
= new wxChar
[300];
163 WinHelpContentsFileName
= new wxChar
[300];
164 RefFileName
= new wxChar
[300];
166 WX_CLEAR_LIST(wxList
,ColourTable
);
170 // Read input/output files
173 if (argv
[1][0] != _T('-'))
180 if (argv
[2][0] != _T('-'))
182 OutputFile
= argv
[2];
190 if (InputFile
.empty() || OutputFile
.empty())
192 wxSTD cout
<< "Tex2RTF: input or output file is missing.\n";
198 if (!InputFile
.empty())
200 TexPathList
.EnsureFileAccessible(InputFile
);
202 if (InputFile
.empty() || OutputFile
.empty())
203 isInteractive
= true;
206 for (i
= n
; i
< argc
;)
208 if (wxStrcmp(argv
[i
], _T("-winhelp")) == 0)
211 convertMode
= TEX_RTF
;
215 else if (wxStrcmp(argv
[i
], _T("-interactive")) == 0)
218 isInteractive
= true;
221 else if (wxStrcmp(argv
[i
], _T("-sync")) == 0) // Don't yield
226 else if (wxStrcmp(argv
[i
], _T("-rtf")) == 0)
229 convertMode
= TEX_RTF
;
231 else if (wxStrcmp(argv
[i
], _T("-html")) == 0)
234 convertMode
= TEX_HTML
;
236 else if (wxStrcmp(argv
[i
], _T("-xlp")) == 0)
239 convertMode
= TEX_XLP
;
241 else if (wxStrcmp(argv
[i
], _T("-twice")) == 0)
246 else if (wxStrcmp(argv
[i
], _T("-macros")) == 0)
251 MacroFile
= copystring(argv
[i
]);
255 else if (wxStrcmp(argv
[i
], _T("-bufsize")) == 0)
260 BufSize
= wxAtoi(argv
[i
]);
264 else if (wxStrcmp(argv
[i
], _T("-charset")) == 0)
271 if (wxStrcmp(s
, _T("ansi")) == 0 || wxStrcmp(s
, _T("pc")) == 0 || wxStrcmp(s
, _T("mac")) == 0 ||
272 wxStrcmp(s
, _T("pca")) == 0)
273 RTFCharset
= copystring(s
);
276 OnError(_T("Incorrect argument for -charset"));
281 else if (wxStrcmp(argv
[i
], _T("-checkcurlybraces")) == 0)
284 checkCurlyBraces
= true;
286 else if (wxStrcmp(argv
[i
], _T("-checkcurleybraces")) == 0)
288 // Support the old, incorrectly spelled version of -checkcurlybraces
289 // so that old scripts which run tex2rtf -checkcurleybraces still work.
291 checkCurlyBraces
= true;
293 else if (wxStrcmp(argv
[i
], _T("-checksyntax")) == 0)
301 buf
.Printf(_T("Invalid switch %s.\n"), argv
[i
]);
302 OnError((wxChar
*)buf
.c_str());
312 #if defined(__WXMSW__) && !defined(NO_GUI)
314 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
315 TheTex2RTFServer
= new Tex2RTFServer
;
316 TheTex2RTFServer
->Create(_T("TEX2RTF"));
319 TexInitialize(BufSize
);
320 ResetContentsLevels(0);
326 // Create the main frame window
327 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Tex2RTF"), wxDefaultPosition
, wxSize(400, 300));
329 frame
->CreateStatusBar(2);
330 #endif // wxUSE_STATUSBAR
333 // TODO: uncomment this when we have tex2rtf.xpm
334 frame
->SetIcon(wxICON(tex2rtf
));
336 if (!InputFile
.empty())
339 title
.Printf( _T("Tex2RTF [%s]"), wxFileNameFromPath(InputFile
).c_str());
340 frame
->SetTitle(title
);
344 wxMenu
*file_menu
= new wxMenu
;
345 file_menu
->Append(TEX_GO
, _T("&Go"), _T("Run converter"));
346 file_menu
->Append(TEX_SET_INPUT
, _T("Set &Input File"), _T("Set the LaTeX input file"));
347 file_menu
->Append(TEX_SET_OUTPUT
, _T("Set &Output File"), _T("Set the output file"));
348 file_menu
->AppendSeparator();
349 file_menu
->Append(TEX_VIEW_LATEX
, _T("View &LaTeX File"), _T("View the LaTeX input file"));
350 file_menu
->Append(TEX_VIEW_OUTPUT
, _T("View Output &File"), _T("View output file"));
351 file_menu
->Append(TEX_SAVE_FILE
, _T("&Save log file"), _T("Save displayed text into file"));
352 file_menu
->AppendSeparator();
353 file_menu
->Append(TEX_QUIT
, _T("E&xit"), _T("Exit Tex2RTF"));
355 wxMenu
*macro_menu
= new wxMenu
;
357 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, _T("&Load Custom Macros"), _T("Load custom LaTeX macro file"));
358 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, _T("View &Custom Macros"), _T("View custom LaTeX macros"));
360 wxMenu
*mode_menu
= new wxMenu
;
362 mode_menu
->Append(TEX_MODE_RTF
, _T("Output linear &RTF"), _T("Wordprocessor-compatible RTF"));
363 mode_menu
->Append(TEX_MODE_WINHELP
, _T("Output &WinHelp RTF"), _T("WinHelp-compatible RTF"));
364 mode_menu
->Append(TEX_MODE_HTML
, _T("Output &HTML"), _T("HTML World Wide Web hypertext file"));
365 mode_menu
->Append(TEX_MODE_XLP
, _T("Output &XLP"), _T("wxHelp hypertext help file"));
367 wxMenu
*options_menu
= new wxMenu
;
369 options_menu
->Append(TEX_OPTIONS_CURLY_BRACE
, _T("Curly brace matching"), _T("Checks for mismatched curly braces"),true);
370 options_menu
->Append(TEX_OPTIONS_SYNTAX_CHECKING
, _T("Syntax checking"), _T("Syntax checking for common errors"),true);
372 options_menu
->Check(TEX_OPTIONS_CURLY_BRACE
, checkCurlyBraces
);
373 options_menu
->Check(TEX_OPTIONS_SYNTAX_CHECKING
, checkSyntax
);
375 wxMenu
*help_menu
= new wxMenu
;
377 help_menu
->Append(TEX_HELP
, _T("&Help"), _T("Tex2RTF Contents Page"));
378 help_menu
->Append(TEX_ABOUT
, _T("&About Tex2RTF"), _T("About Tex2RTF"));
380 menuBar
= new wxMenuBar
;
381 menuBar
->Append(file_menu
, _T("&File"));
382 menuBar
->Append(macro_menu
, _T("&Macros"));
383 menuBar
->Append(mode_menu
, _T("&Conversion Mode"));
384 menuBar
->Append(options_menu
, _T("&Options"));
385 menuBar
->Append(help_menu
, _T("&Help"));
387 frame
->SetMenuBar(menuBar
);
388 frame
->textWindow
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_READONLY
|wxTE_MULTILINE
);
390 (*frame
->textWindow
) << _T("Welcome to Tex2RTF.\n");
394 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
395 HelpInstance
= new wxCHMHelpController
;
397 HelpInstance
= new wxHelpController
;
399 HelpInstance
->Initialize(_T("tex2rtf"));
403 * Read macro/initialisation file
407 wxString path
= TexPathList
.FindValidPath(MacroFile
);
409 ReadCustomMacros((wxChar
*)path
.c_str());
412 wxString
inStr(_T("In "));
417 inStr
+= _T("WinHelp RTF");
419 inStr
+= _T("linear RTF");
431 inStr
+= _T("unknown");
434 inStr
+= _T(" mode.");
435 frame
->SetStatusText(inStr
, 1);
436 #endif // wxUSE_STATUSBAR
445 * Read macro/initialisation file
449 wxString path
= TexPathList
.FindValidPath(MacroFile
);
451 ReadCustomMacros((wxChar
*)path
.c_str());
461 OnExit(); // Do cleanup since OnExit won't be called now
470 wxNode
*node
= CustomMacroList
.GetFirst();
473 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
476 node
= CustomMacroList
.GetFirst();
478 MacroDefs
.BeginFind();
479 wxHashTable::Node
* mNode
= MacroDefs
.Next();
482 TexMacroDef
* def
= (TexMacroDef
*) mNode
->GetData();
484 mNode
= MacroDefs
.Next();
488 delete TheTex2RTFServer
;
503 delete currentArgData
;
504 currentArgData
= NULL
;
518 delete TexTmpBibName
;
519 TexTmpBibName
= NULL
;
533 delete TmpContentsName
;
534 TmpContentsName
= NULL
;
536 if (TmpFrameContentsName
)
538 delete TmpFrameContentsName
;
539 TmpFrameContentsName
= NULL
;
541 if (WinHelpContentsFileName
)
543 delete WinHelpContentsFileName
;
544 WinHelpContentsFileName
= NULL
;
568 delete [] BibliographyStyleString
;
569 delete [] DocumentStyleString
;
570 delete [] bitmapMethod
;
571 delete [] backgroundColourString
;
572 delete [] ContentsNameString
;
573 delete [] AbstractNameString
;
574 delete [] GlossaryNameString
;
575 delete [] ReferencesNameString
;
576 delete [] FiguresNameString
;
577 delete [] TablesNameString
;
578 delete [] FigureNameString
;
579 delete [] TableNameString
;
580 delete [] IndexNameString
;
581 delete [] ChapterNameString
;
582 delete [] SectionNameString
;
583 delete [] SubsectionNameString
;
584 delete [] SubsubsectionNameString
;
585 delete [] UpNameString
;
587 delete[] winHelpTitle
;
589 // TODO: this simulates zero-memory leaks!
590 // Otherwise there are just too many...
592 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
593 wxDebugContext::SetCheckpoint();
600 void ShowOptions(void)
603 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF version %.2f"), versionNo
);
605 OnInform(_T("Usage: tex2rtf [input] [output] [switches]\n"));
606 OnInform(_T("where valid switches are"));
608 OnInform(_T(" -interactive"));
610 OnInform(_T(" -bufsize <size in K>"));
611 OnInform(_T(" -charset <pc | pca | ansi | mac> (default ansi)"));
612 OnInform(_T(" -twice"));
613 OnInform(_T(" -sync"));
614 OnInform(_T(" -checkcurlybraces"));
615 OnInform(_T(" -checksyntax"));
616 OnInform(_T(" -macros <filename>"));
617 OnInform(_T(" -winhelp"));
618 OnInform(_T(" -rtf"));
619 OnInform(_T(" -html"));
620 OnInform(_T(" -xlp\n"));
625 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
626 EVT_CLOSE(MyFrame::OnCloseWindow
)
627 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
628 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
629 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
630 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
631 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
632 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
633 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
634 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
635 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
636 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
637 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
638 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
639 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
640 EVT_MENU(TEX_OPTIONS_CURLY_BRACE
, MyFrame::OnOptionsCurlyBrace
)
641 EVT_MENU(TEX_OPTIONS_SYNTAX_CHECKING
, MyFrame::OnOptionsSyntaxChecking
)
642 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
643 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
646 // My frame constructor
647 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
648 wxFrame(frame
, id
, title
, pos
, size
)
651 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
653 if (!stopRunning
&& !OkToClose
)
665 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
671 void MyFrame::OnGo(wxCommandEvent
& WXUNUSED(event
))
675 menuBar
->EnableTop(0, false);
676 menuBar
->EnableTop(1, false);
677 menuBar
->EnableTop(2, false);
678 menuBar
->EnableTop(3, false);
686 SetStatusText(_T("Build aborted!"));
687 #endif // wxUSE_STATUSBAR
689 errBuf
.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount
);
690 OnInform((wxChar
*)errBuf
.c_str());
694 if (runTwice
&& !stopRunning
)
699 menuBar
->EnableTop(0, true);
700 menuBar
->EnableTop(1, true);
701 menuBar
->EnableTop(2, true);
702 menuBar
->EnableTop(3, true);
705 void MyFrame::OnSetInput(wxCommandEvent
& WXUNUSED(event
))
707 ChooseInputFile(true);
710 void MyFrame::OnSetOutput(wxCommandEvent
& WXUNUSED(event
))
712 ChooseOutputFile(true);
715 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
718 wxString s
= wxFileSelector(_T("Save text to file"), wxEmptyString
, wxEmptyString
, _T("txt"), _T("*.txt"));
721 textWindow
->SaveFile(s
);
724 wxSnprintf(buf
, sizeof(buf
), _T("Saved text to %s"), (const wxChar
*) s
.c_str());
725 frame
->SetStatusText(buf
, 0);
726 #endif // wxUSE_STATUSBAR
728 #endif // wxUSE_FILEDLG
731 void MyFrame::OnViewOutput(wxCommandEvent
& WXUNUSED(event
))
734 if (!OutputFile
.empty() && wxFileExists(OutputFile
))
736 textWindow
->LoadFile(OutputFile
);
738 wxString
str(wxFileNameFromPath(OutputFile
));
739 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
740 frame
->SetTitle(buf
);
744 void MyFrame::OnViewLatex(wxCommandEvent
& WXUNUSED(event
))
747 if (!InputFile
.empty() && wxFileExists(InputFile
))
749 textWindow
->LoadFile(InputFile
);
751 wxString
str(wxFileNameFromPath(OutputFile
));
752 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
753 frame
->SetTitle(buf
);
757 void MyFrame::OnLoadMacros(wxCommandEvent
& WXUNUSED(event
))
761 wxString s
= wxFileSelector(_T("Choose custom macro file"), wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), _T("ini"), _T("*.ini"));
762 if (!s
.empty() && wxFileExists(s
))
764 MacroFile
= copystring(s
);
765 ReadCustomMacros((wxChar
*)s
.c_str());
768 #endif // wxUSE_FILEDLG
771 void MyFrame::OnShowMacros(wxCommandEvent
& WXUNUSED(event
))
778 void MyFrame::OnModeRTF(wxCommandEvent
& WXUNUSED(event
))
780 convertMode
= TEX_RTF
;
782 InputFile
= wxEmptyString
;
783 OutputFile
= wxEmptyString
;
785 SetStatusText(_T("In linear RTF mode."), 1);
786 #endif // wxUSE_STATUSBAR
789 void MyFrame::OnModeWinHelp(wxCommandEvent
& WXUNUSED(event
))
791 convertMode
= TEX_RTF
;
793 InputFile
= wxEmptyString
;
794 OutputFile
= wxEmptyString
;
796 SetStatusText(_T("In WinHelp RTF mode."), 1);
797 #endif // wxUSE_STATUSBAR
800 void MyFrame::OnModeHTML(wxCommandEvent
& WXUNUSED(event
))
802 convertMode
= TEX_HTML
;
804 InputFile
= wxEmptyString
;
805 OutputFile
= wxEmptyString
;
807 SetStatusText(_T("In HTML mode."), 1);
808 #endif // wxUSE_STATUSBAR
811 void MyFrame::OnModeXLP(wxCommandEvent
& WXUNUSED(event
))
813 convertMode
= TEX_XLP
;
814 InputFile
= wxEmptyString
;
815 OutputFile
= wxEmptyString
;
817 SetStatusText(_T("In XLP mode."), 1);
818 #endif // wxUSE_STATUSBAR
821 void MyFrame::OnOptionsCurlyBrace(wxCommandEvent
& WXUNUSED(event
))
823 checkCurlyBraces
= !checkCurlyBraces
;
825 if (checkCurlyBraces
)
827 SetStatusText(_T("Checking curly braces: YES"), 1);
831 SetStatusText(_T("Checking curly braces: NO"), 1);
833 #endif // wxUSE_STATUSBAR
837 void MyFrame::OnOptionsSyntaxChecking(wxCommandEvent
& WXUNUSED(event
))
839 checkSyntax
= !checkSyntax
;
843 SetStatusText(_T("Checking syntax: YES"), 1);
847 SetStatusText(_T("Checking syntax: NO"), 1);
849 #endif // wxUSE_STATUSBAR
853 void MyFrame::OnHelp(wxCommandEvent
& WXUNUSED(event
))
856 HelpInstance
->LoadFile();
857 HelpInstance
->DisplayContents();
861 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
864 wxString platform
= wxGetOsDescription();
865 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());
866 wxMessageBox(buf
, _T("About Tex2RTF"));
869 void ChooseInputFile(bool force
)
872 if (force
|| InputFile
.empty())
874 wxString s
= wxFileSelector(_T("Choose LaTeX input file"), wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), _T("tex"), _T("*.tex"));
877 // Different file, so clear index entries.
879 ResetContentsLevels(0);
884 wxString str
= wxFileNameFromPath(InputFile
);
886 buf
.Printf(_T("Tex2RTF [%s]"), str
.c_str());
887 frame
->SetTitle((wxChar
*)buf
.c_str());
888 OutputFile
= wxEmptyString
;
893 #endif // wxUSE_FILEDLG
896 void ChooseOutputFile(bool force
)
898 wxChar extensionBuf
[10];
900 wxStrcpy(wildBuf
, _T("*."));
902 if (!OutputFile
.empty())
903 path
= wxPathOnly(OutputFile
);
904 else if (!InputFile
.empty())
905 path
= wxPathOnly(InputFile
);
911 wxStrcpy(extensionBuf
, _T("rtf"));
912 wxStrcat(wildBuf
, _T("rtf"));
917 wxStrcpy(extensionBuf
, _T("xlp"));
918 wxStrcat(wildBuf
, _T("xlp"));
923 wxStrcpy(extensionBuf
, _T("html"));
924 wxStrcat(wildBuf
, _T("html"));
929 if (force
|| OutputFile
.empty())
931 wxString s
= wxFileSelector(_T("Choose output file"), path
, wxFileNameFromPath(OutputFile
),
932 extensionBuf
, wildBuf
);
938 #endif // wxUSE_FILEDLG
949 if (InputFile
.empty() || OutputFile
.empty() || stopRunning
)
956 wxString str
= wxFileNameFromPath(InputFile
);
958 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
959 frame
->SetTitle(buf
);
965 // Find extension-less filename
966 wxStrcpy(FileRoot
, OutputFile
.c_str());
967 StripExtension(FileRoot
);
969 if (truncateFilenames
&& convertMode
== TEX_HTML
)
971 // Truncate to five characters. This ensures that
972 // we can generate DOS filenames such as thing999. But 1000 files
973 // may not be enough, of course...
974 wxChar
* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
977 if(wxStrlen( sName
) > 5)
978 sName
[5] = '\0'; // that should do!
981 wxSnprintf(ContentsName
, 300, _T("%s.con"), FileRoot
);
982 wxSnprintf(TmpContentsName
, 300, _T("%s.cn1"), FileRoot
);
983 wxSnprintf(TmpFrameContentsName
, 300, _T("%s.frc"), FileRoot
);
984 wxSnprintf(WinHelpContentsFileName
, 300, _T("%s.cnt"), FileRoot
);
985 wxSnprintf(RefFileName
, 300, _T("%s.ref"), FileRoot
);
987 TexPathList
.EnsureFileAccessible(InputFile
);
990 wxString s
= TexPathList
.FindValidPath(_T("bullet.bmp"));
993 wxString str
= wxFileNameFromPath(s
);
994 bulletFile
= copystring(str
);
998 if (wxFileExists(RefFileName
))
999 ReadTexReferences(RefFileName
);
1001 bool success
= false;
1003 if (!InputFile
.empty() && !OutputFile
.empty())
1005 if (!wxFileExists(InputFile
))
1007 OnError(_T("Cannot open input file!"));
1011 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1015 buf
.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber
);
1016 frame
->SetStatusText((wxChar
*)buf
.c_str());
1020 OnInform(_T("Reading LaTeX file..."));
1021 TexLoadFile(InputFile
);
1029 switch (convertMode
)
1050 OnInform(_T("*** Aborted by user."));
1052 stopRunning
= false;
1058 WriteTexReferences(RefFileName
);
1060 startedSections
= false;
1064 long tim
= wxGetElapsedTime();
1065 buf
.Printf(_T("Finished PASS #%d in %ld seconds.\n"), passNumber
, (long)(tim
/1000.0));
1066 OnInform((wxChar
*)buf
.c_str());
1070 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1071 OnInform((wxChar
*)buf
.c_str());
1077 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1078 frame
->SetStatusText((wxChar
*)buf
.c_str());
1080 #endif // wxUSE_STATUSBAR
1082 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1083 OnInform((wxChar
*)buf
.c_str());
1086 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1087 OnInform((wxChar
*)buf
.c_str());
1097 startedSections
= false;
1099 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1100 frame
->SetStatusText(_T("Aborted by user."));
1103 OnInform(_T("Sorry, unsuccessful."));
1108 void OnError(const wxChar
*msg
)
1110 wxString msg_string
= msg
;
1114 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1117 if (isInteractive
&& frame
)
1119 (*frame
->textWindow
) << _T("Error: ") << msg
<< _T("\n");
1123 #if defined(__UNIX__)
1124 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1126 #elif defined(__WXMSW__)
1135 void OnInform(const wxChar
*msg
)
1137 wxString msg_string
= msg
;
1139 wxSTD cout
<< msg_string
.mb_str() << "\n";
1142 if (isInteractive
&& frame
)
1144 (*frame
->textWindow
) << msg
<< _T("\n");
1148 #if defined(__UNIX__)
1149 wxSTD cout
<< msg_string
.mb_str() << "\n";
1151 #elif defined(__WXMSW__)
1163 void OnMacro(int macroId
, int no_args
, bool start
)
1165 switch (convertMode
)
1169 RTFOnMacro(macroId
, no_args
, start
);
1174 XLPOnMacro(macroId
, no_args
, start
);
1179 HTMLOnMacro(macroId
, no_args
, start
);
1185 bool OnArgument(int macroId
, int arg_no
, bool start
)
1187 switch (convertMode
)
1191 return RTFOnArgument(macroId
, arg_no
, start
);
1196 return XLPOnArgument(macroId
, arg_no
, start
);
1201 return HTMLOnArgument(macroId
, arg_no
, start
);
1211 #if defined(__WXMSW__) && !defined(NO_GUI)
1217 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
1219 if (topic
== _T("TEX2RTF"))
1222 ipc_buffer
= new wxChar
[1000];
1224 return new Tex2RTFConnection(ipc_buffer
, 4000);
1234 Tex2RTFConnection::Tex2RTFConnection(wxChar
*buf
, int size
):wxDDEConnection(buf
, size
)
1238 bool SplitCommand(wxChar
*data
, wxChar
*firstArg
, wxChar
*secondArg
)
1244 // Find first argument (command name)
1247 if (data
[i
] == ' ' || data
[i
] == 0)
1251 firstArg
[i
] = data
[i
];
1258 // Find second argument
1261 while (data
[i
] != 0)
1263 secondArg
[j
] = data
[i
];
1272 bool Tex2RTFConnection::OnExecute(const wxString
& WXUNUSED(topic
), wxChar
*data
, int WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1274 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1276 wxChar firstArg
[50];
1277 wxChar secondArg
[300];
1278 if (SplitCommand(data
, firstArg
, secondArg
))
1280 bool hasArg
= (wxStrlen(secondArg
) > 0);
1281 if (wxStrcmp(firstArg
, _T("INPUT")) == 0 && hasArg
)
1283 InputFile
= secondArg
;
1287 wxString str
= wxFileNameFromPath(InputFile
);
1288 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
1289 frame
->SetTitle(buf
);
1292 else if (wxStrcmp(firstArg
, _T("OUTPUT")) == 0 && hasArg
)
1294 OutputFile
= secondArg
;
1296 else if (wxStrcmp(firstArg
, _T("GO")) == 0)
1298 wxStrcpy(Tex2RTFLastStatus
, _T("WORKING"));
1300 wxStrcpy(Tex2RTFLastStatus
, _T("CONVERSION ERROR"));
1302 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1304 else if (wxStrcmp(firstArg
, _T("EXIT")) == 0)
1306 if (frame
) frame
->Close();
1308 else if (wxStrcmp(firstArg
, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg
, _T("ICONIZE")) == 0)
1311 frame
->Iconize(true);
1313 else if (wxStrcmp(firstArg
, _T("SHOW")) == 0 || wxStrcmp(firstArg
, _T("RESTORE")) == 0)
1317 frame
->Iconize(false);
1323 // Try for a setting
1324 wxStrcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, false));
1325 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1326 if (frame
&& wxStrcmp(firstArg
, _T("conversionMode")) == 0)
1329 wxStrcpy(buf
, _T("In "));
1331 if (winHelp
&& (convertMode
== TEX_RTF
))
1332 wxStrcat(buf
, _T("WinHelp RTF"));
1333 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1334 wxStrcat(buf
, _T("linear RTF"));
1335 else if (convertMode
== TEX_HTML
) wxStrcat(buf
, _T("HTML"));
1336 else if (convertMode
== TEX_XLP
) wxStrcat(buf
, _T("XLP"));
1337 wxStrcat(buf
, _T(" mode."));
1338 frame
->SetStatusText(buf
, 1);
1346 wxChar
*Tex2RTFConnection::OnRequest(const wxString
& WXUNUSED(topic
), const wxString
& WXUNUSED(item
), int *WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1348 return Tex2RTFLastStatus
;
1355 //void wxObject::Dump(wxSTD ostream& str)
1357 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1358 // str << GetClassInfo()->GetClassName();
1360 // str << "unknown object class";