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);
328 // Create the main frame window
329 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Tex2RTF"), wxDefaultPosition
, wxSize(400, 300));
331 frame
->CreateStatusBar(2);
332 #endif // wxUSE_STATUSBAR
335 // TODO: uncomment this when we have tex2rtf.xpm
336 frame
->SetIcon(wxICON(tex2rtf
));
338 if (!InputFile
.empty())
340 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), wxFileNameFromPath(InputFile
));
341 frame
->SetTitle(buf
);
345 wxMenu
*file_menu
= new wxMenu
;
346 file_menu
->Append(TEX_GO
, _T("&Go"), _T("Run converter"));
347 file_menu
->Append(TEX_SET_INPUT
, _T("Set &Input File"), _T("Set the LaTeX input file"));
348 file_menu
->Append(TEX_SET_OUTPUT
, _T("Set &Output File"), _T("Set the output file"));
349 file_menu
->AppendSeparator();
350 file_menu
->Append(TEX_VIEW_LATEX
, _T("View &LaTeX File"), _T("View the LaTeX input file"));
351 file_menu
->Append(TEX_VIEW_OUTPUT
, _T("View Output &File"), _T("View output file"));
352 file_menu
->Append(TEX_SAVE_FILE
, _T("&Save log file"), _T("Save displayed text into file"));
353 file_menu
->AppendSeparator();
354 file_menu
->Append(TEX_QUIT
, _T("E&xit"), _T("Exit Tex2RTF"));
356 wxMenu
*macro_menu
= new wxMenu
;
358 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, _T("&Load Custom Macros"), _T("Load custom LaTeX macro file"));
359 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, _T("View &Custom Macros"), _T("View custom LaTeX macros"));
361 wxMenu
*mode_menu
= new wxMenu
;
363 mode_menu
->Append(TEX_MODE_RTF
, _T("Output linear &RTF"), _T("Wordprocessor-compatible RTF"));
364 mode_menu
->Append(TEX_MODE_WINHELP
, _T("Output &WinHelp RTF"), _T("WinHelp-compatible RTF"));
365 mode_menu
->Append(TEX_MODE_HTML
, _T("Output &HTML"), _T("HTML World Wide Web hypertext file"));
366 mode_menu
->Append(TEX_MODE_XLP
, _T("Output &XLP"), _T("wxHelp hypertext help file"));
368 wxMenu
*options_menu
= new wxMenu
;
370 options_menu
->Append(TEX_OPTIONS_CURLY_BRACE
, _T("Curly brace matching"), _T("Checks for mismatched curly braces"),true);
371 options_menu
->Append(TEX_OPTIONS_SYNTAX_CHECKING
, _T("Syntax checking"), _T("Syntax checking for common errors"),true);
373 options_menu
->Check(TEX_OPTIONS_CURLY_BRACE
, checkCurlyBraces
);
374 options_menu
->Check(TEX_OPTIONS_SYNTAX_CHECKING
, checkSyntax
);
376 wxMenu
*help_menu
= new wxMenu
;
378 help_menu
->Append(TEX_HELP
, _T("&Help"), _T("Tex2RTF Contents Page"));
379 help_menu
->Append(TEX_ABOUT
, _T("&About Tex2RTF"), _T("About Tex2RTF"));
381 menuBar
= new wxMenuBar
;
382 menuBar
->Append(file_menu
, _T("&File"));
383 menuBar
->Append(macro_menu
, _T("&Macros"));
384 menuBar
->Append(mode_menu
, _T("&Conversion Mode"));
385 menuBar
->Append(options_menu
, _T("&Options"));
386 menuBar
->Append(help_menu
, _T("&Help"));
388 frame
->SetMenuBar(menuBar
);
389 frame
->textWindow
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_READONLY
|wxTE_MULTILINE
);
391 (*frame
->textWindow
) << _T("Welcome to Tex2RTF.\n");
395 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
396 HelpInstance
= new wxCHMHelpController
;
398 HelpInstance
= new wxHelpController
;
400 HelpInstance
->Initialize(_T("tex2rtf"));
404 * Read macro/initialisation file
408 wxString path
= TexPathList
.FindValidPath(MacroFile
);
410 ReadCustomMacros((wxChar
*)path
.c_str());
413 wxStrcpy(buf
, _T("In "));
417 wxStrcat(buf
, (winHelp
) ? _T("WinHelp RTF") : _T("linear RTF"));
420 wxStrcat(buf
, _T("HTML"));
423 wxStrcat(buf
, _T("XLP"));
426 wxStrcat(buf
, _T("unknown"));
429 wxStrcat(buf
, _T(" mode."));
430 frame
->SetStatusText(buf
, 1);
431 #endif // wxUSE_STATUSBAR
440 * Read macro/initialisation file
444 wxString path
= TexPathList
.FindValidPath(MacroFile
);
446 ReadCustomMacros((wxChar
*)path
.c_str());
456 OnExit(); // Do cleanup since OnExit won't be called now
465 wxNode
*node
= CustomMacroList
.GetFirst();
468 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
471 node
= CustomMacroList
.GetFirst();
473 MacroDefs
.BeginFind();
474 wxHashTable::Node
* mNode
= MacroDefs
.Next();
477 TexMacroDef
* def
= (TexMacroDef
*) mNode
->GetData();
479 mNode
= MacroDefs
.Next();
483 delete TheTex2RTFServer
;
498 delete currentArgData
;
499 currentArgData
= NULL
;
513 delete TexTmpBibName
;
514 TexTmpBibName
= NULL
;
528 delete TmpContentsName
;
529 TmpContentsName
= NULL
;
531 if (TmpFrameContentsName
)
533 delete TmpFrameContentsName
;
534 TmpFrameContentsName
= NULL
;
536 if (WinHelpContentsFileName
)
538 delete WinHelpContentsFileName
;
539 WinHelpContentsFileName
= NULL
;
563 delete [] BibliographyStyleString
;
564 delete [] DocumentStyleString
;
565 delete [] bitmapMethod
;
566 delete [] backgroundColourString
;
567 delete [] ContentsNameString
;
568 delete [] AbstractNameString
;
569 delete [] GlossaryNameString
;
570 delete [] ReferencesNameString
;
571 delete [] FiguresNameString
;
572 delete [] TablesNameString
;
573 delete [] FigureNameString
;
574 delete [] TableNameString
;
575 delete [] IndexNameString
;
576 delete [] ChapterNameString
;
577 delete [] SectionNameString
;
578 delete [] SubsectionNameString
;
579 delete [] SubsubsectionNameString
;
580 delete [] UpNameString
;
582 delete[] winHelpTitle
;
584 // TODO: this simulates zero-memory leaks!
585 // Otherwise there are just too many...
587 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
588 wxDebugContext::SetCheckpoint();
595 void ShowOptions(void)
598 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF version %.2f"), versionNo
);
600 OnInform(_T("Usage: tex2rtf [input] [output] [switches]\n"));
601 OnInform(_T("where valid switches are"));
603 OnInform(_T(" -interactive"));
605 OnInform(_T(" -bufsize <size in K>"));
606 OnInform(_T(" -charset <pc | pca | ansi | mac> (default ansi)"));
607 OnInform(_T(" -twice"));
608 OnInform(_T(" -sync"));
609 OnInform(_T(" -checkcurlybraces"));
610 OnInform(_T(" -checksyntax"));
611 OnInform(_T(" -macros <filename>"));
612 OnInform(_T(" -winhelp"));
613 OnInform(_T(" -rtf"));
614 OnInform(_T(" -html"));
615 OnInform(_T(" -xlp\n"));
620 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
621 EVT_CLOSE(MyFrame::OnCloseWindow
)
622 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
623 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
624 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
625 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
626 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
627 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
628 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
629 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
630 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
631 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
632 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
633 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
634 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
635 EVT_MENU(TEX_OPTIONS_CURLY_BRACE
, MyFrame::OnOptionsCurlyBrace
)
636 EVT_MENU(TEX_OPTIONS_SYNTAX_CHECKING
, MyFrame::OnOptionsSyntaxChecking
)
637 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
638 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
641 // My frame constructor
642 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
643 wxFrame(frame
, id
, title
, pos
, size
)
646 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
648 if (!stopRunning
&& !OkToClose
)
660 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
666 void MyFrame::OnGo(wxCommandEvent
& WXUNUSED(event
))
670 menuBar
->EnableTop(0, false);
671 menuBar
->EnableTop(1, false);
672 menuBar
->EnableTop(2, false);
673 menuBar
->EnableTop(3, false);
681 SetStatusText(_T("Build aborted!"));
682 #endif // wxUSE_STATUSBAR
684 errBuf
.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount
);
685 OnInform((wxChar
*)errBuf
.c_str());
689 if (runTwice
&& !stopRunning
)
694 menuBar
->EnableTop(0, true);
695 menuBar
->EnableTop(1, true);
696 menuBar
->EnableTop(2, true);
697 menuBar
->EnableTop(3, true);
700 void MyFrame::OnSetInput(wxCommandEvent
& WXUNUSED(event
))
702 ChooseInputFile(true);
705 void MyFrame::OnSetOutput(wxCommandEvent
& WXUNUSED(event
))
707 ChooseOutputFile(true);
710 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
713 wxString s
= wxFileSelector(_T("Save text to file"), wxEmptyString
, wxEmptyString
, _T("txt"), _T("*.txt"));
716 textWindow
->SaveFile(s
);
719 wxSnprintf(buf
, sizeof(buf
), _T("Saved text to %s"), (const wxChar
*) s
.c_str());
720 frame
->SetStatusText(buf
, 0);
721 #endif // wxUSE_STATUSBAR
723 #endif // wxUSE_FILEDLG
726 void MyFrame::OnViewOutput(wxCommandEvent
& WXUNUSED(event
))
729 if (!OutputFile
.empty() && wxFileExists(OutputFile
))
731 textWindow
->LoadFile(OutputFile
);
733 wxString
str(wxFileNameFromPath(OutputFile
));
734 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
735 frame
->SetTitle(buf
);
739 void MyFrame::OnViewLatex(wxCommandEvent
& WXUNUSED(event
))
742 if (!InputFile
.empty() && wxFileExists(InputFile
))
744 textWindow
->LoadFile(InputFile
);
746 wxString
str(wxFileNameFromPath(OutputFile
));
747 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
748 frame
->SetTitle(buf
);
752 void MyFrame::OnLoadMacros(wxCommandEvent
& WXUNUSED(event
))
756 wxString s
= wxFileSelector(_T("Choose custom macro file"), wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), _T("ini"), _T("*.ini"));
757 if (!s
.empty() && wxFileExists(s
))
759 MacroFile
= copystring(s
);
760 ReadCustomMacros((wxChar
*)s
.c_str());
763 #endif // wxUSE_FILEDLG
766 void MyFrame::OnShowMacros(wxCommandEvent
& WXUNUSED(event
))
773 void MyFrame::OnModeRTF(wxCommandEvent
& WXUNUSED(event
))
775 convertMode
= TEX_RTF
;
777 InputFile
= wxEmptyString
;
778 OutputFile
= wxEmptyString
;
780 SetStatusText(_T("In linear RTF mode."), 1);
781 #endif // wxUSE_STATUSBAR
784 void MyFrame::OnModeWinHelp(wxCommandEvent
& WXUNUSED(event
))
786 convertMode
= TEX_RTF
;
788 InputFile
= wxEmptyString
;
789 OutputFile
= wxEmptyString
;
791 SetStatusText(_T("In WinHelp RTF mode."), 1);
792 #endif // wxUSE_STATUSBAR
795 void MyFrame::OnModeHTML(wxCommandEvent
& WXUNUSED(event
))
797 convertMode
= TEX_HTML
;
799 InputFile
= wxEmptyString
;
800 OutputFile
= wxEmptyString
;
802 SetStatusText(_T("In HTML mode."), 1);
803 #endif // wxUSE_STATUSBAR
806 void MyFrame::OnModeXLP(wxCommandEvent
& WXUNUSED(event
))
808 convertMode
= TEX_XLP
;
809 InputFile
= wxEmptyString
;
810 OutputFile
= wxEmptyString
;
812 SetStatusText(_T("In XLP mode."), 1);
813 #endif // wxUSE_STATUSBAR
816 void MyFrame::OnOptionsCurlyBrace(wxCommandEvent
& WXUNUSED(event
))
818 checkCurlyBraces
= !checkCurlyBraces
;
820 if (checkCurlyBraces
)
822 SetStatusText(_T("Checking curly braces: YES"), 1);
826 SetStatusText(_T("Checking curly braces: NO"), 1);
828 #endif // wxUSE_STATUSBAR
832 void MyFrame::OnOptionsSyntaxChecking(wxCommandEvent
& WXUNUSED(event
))
834 checkSyntax
= !checkSyntax
;
838 SetStatusText(_T("Checking syntax: YES"), 1);
842 SetStatusText(_T("Checking syntax: NO"), 1);
844 #endif // wxUSE_STATUSBAR
848 void MyFrame::OnHelp(wxCommandEvent
& WXUNUSED(event
))
851 HelpInstance
->LoadFile();
852 HelpInstance
->DisplayContents();
856 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
859 wxString platform
= wxGetOsDescription();
860 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());
861 wxMessageBox(buf
, _T("About Tex2RTF"));
864 void ChooseInputFile(bool force
)
867 if (force
|| InputFile
.empty())
869 wxString s
= wxFileSelector(_T("Choose LaTeX input file"), wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), _T("tex"), _T("*.tex"));
872 // Different file, so clear index entries.
874 ResetContentsLevels(0);
879 wxString str
= wxFileNameFromPath(InputFile
);
881 buf
.Printf(_T("Tex2RTF [%s]"), str
.c_str());
882 frame
->SetTitle((wxChar
*)buf
.c_str());
883 OutputFile
= wxEmptyString
;
888 #endif // wxUSE_FILEDLG
891 void ChooseOutputFile(bool force
)
893 wxChar extensionBuf
[10];
895 wxStrcpy(wildBuf
, _T("*."));
897 if (!OutputFile
.empty())
898 path
= wxPathOnly(OutputFile
);
899 else if (!InputFile
.empty())
900 path
= wxPathOnly(InputFile
);
906 wxStrcpy(extensionBuf
, _T("rtf"));
907 wxStrcat(wildBuf
, _T("rtf"));
912 wxStrcpy(extensionBuf
, _T("xlp"));
913 wxStrcat(wildBuf
, _T("xlp"));
918 wxStrcpy(extensionBuf
, _T("html"));
919 wxStrcat(wildBuf
, _T("html"));
924 if (force
|| OutputFile
.empty())
926 wxString s
= wxFileSelector(_T("Choose output file"), path
, wxFileNameFromPath(OutputFile
),
927 extensionBuf
, wildBuf
);
933 #endif // wxUSE_FILEDLG
944 if (InputFile
.empty() || OutputFile
.empty() || stopRunning
)
951 wxString str
= wxFileNameFromPath(InputFile
);
953 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
954 frame
->SetTitle(buf
);
960 // Find extension-less filename
961 wxStrcpy(FileRoot
, OutputFile
.c_str());
962 StripExtension(FileRoot
);
964 if (truncateFilenames
&& convertMode
== TEX_HTML
)
966 // Truncate to five characters. This ensures that
967 // we can generate DOS filenames such as thing999. But 1000 files
968 // may not be enough, of course...
969 wxChar
* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
972 if(wxStrlen( sName
) > 5)
973 sName
[5] = '\0'; // that should do!
976 wxSnprintf(ContentsName
, 300, _T("%s.con"), FileRoot
);
977 wxSnprintf(TmpContentsName
, 300, _T("%s.cn1"), FileRoot
);
978 wxSnprintf(TmpFrameContentsName
, 300, _T("%s.frc"), FileRoot
);
979 wxSnprintf(WinHelpContentsFileName
, 300, _T("%s.cnt"), FileRoot
);
980 wxSnprintf(RefFileName
, 300, _T("%s.ref"), FileRoot
);
982 TexPathList
.EnsureFileAccessible(InputFile
);
985 wxString s
= TexPathList
.FindValidPath(_T("bullet.bmp"));
988 wxString str
= wxFileNameFromPath(s
);
989 bulletFile
= copystring(str
);
993 if (wxFileExists(RefFileName
))
994 ReadTexReferences(RefFileName
);
996 bool success
= false;
998 if (!InputFile
.empty() && !OutputFile
.empty())
1000 if (!wxFileExists(InputFile
))
1002 OnError(_T("Cannot open input file!"));
1006 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1010 buf
.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber
);
1011 frame
->SetStatusText((wxChar
*)buf
.c_str());
1015 OnInform(_T("Reading LaTeX file..."));
1016 TexLoadFile(InputFile
);
1024 switch (convertMode
)
1045 OnInform(_T("*** Aborted by user."));
1047 stopRunning
= false;
1053 WriteTexReferences(RefFileName
);
1055 startedSections
= false;
1059 long tim
= wxGetElapsedTime();
1060 buf
.Printf(_T("Finished PASS #%d in %ld seconds.\n"), passNumber
, (long)(tim
/1000.0));
1061 OnInform((wxChar
*)buf
.c_str());
1065 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1066 OnInform((wxChar
*)buf
.c_str());
1072 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1073 frame
->SetStatusText((wxChar
*)buf
.c_str());
1075 #endif // wxUSE_STATUSBAR
1077 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1078 OnInform((wxChar
*)buf
.c_str());
1081 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1082 OnInform((wxChar
*)buf
.c_str());
1092 startedSections
= false;
1094 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1095 frame
->SetStatusText(_T("Aborted by user."));
1098 OnInform(_T("Sorry, unsuccessful."));
1103 void OnError(const wxChar
*msg
)
1105 wxString msg_string
= msg
;
1109 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1112 if (isInteractive
&& frame
)
1114 (*frame
->textWindow
) << _T("Error: ") << msg
<< _T("\n");
1118 #if defined(__UNIX__)
1119 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1121 #elif defined(__WXMSW__)
1130 void OnInform(const wxChar
*msg
)
1132 wxString msg_string
= msg
;
1134 wxSTD cout
<< msg_string
.mb_str() << "\n";
1137 if (isInteractive
&& frame
)
1139 (*frame
->textWindow
) << msg
<< _T("\n");
1143 #if defined(__UNIX__)
1144 wxSTD cout
<< msg_string
.mb_str() << "\n";
1146 #elif defined(__WXMSW__)
1158 void OnMacro(int macroId
, int no_args
, bool start
)
1160 switch (convertMode
)
1164 RTFOnMacro(macroId
, no_args
, start
);
1169 XLPOnMacro(macroId
, no_args
, start
);
1174 HTMLOnMacro(macroId
, no_args
, start
);
1180 bool OnArgument(int macroId
, int arg_no
, bool start
)
1182 switch (convertMode
)
1186 return RTFOnArgument(macroId
, arg_no
, start
);
1191 return XLPOnArgument(macroId
, arg_no
, start
);
1196 return HTMLOnArgument(macroId
, arg_no
, start
);
1206 #if defined(__WXMSW__) && !defined(NO_GUI)
1212 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
1214 if (topic
== _T("TEX2RTF"))
1217 ipc_buffer
= new wxChar
[1000];
1219 return new Tex2RTFConnection(ipc_buffer
, 4000);
1229 Tex2RTFConnection::Tex2RTFConnection(wxChar
*buf
, int size
):wxDDEConnection(buf
, size
)
1233 bool SplitCommand(wxChar
*data
, wxChar
*firstArg
, wxChar
*secondArg
)
1239 // Find first argument (command name)
1242 if (data
[i
] == ' ' || data
[i
] == 0)
1246 firstArg
[i
] = data
[i
];
1253 // Find second argument
1256 while (data
[i
] != 0)
1258 secondArg
[j
] = data
[i
];
1267 bool Tex2RTFConnection::OnExecute(const wxString
& WXUNUSED(topic
), wxChar
*data
, int WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1269 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1271 wxChar firstArg
[50];
1272 wxChar secondArg
[300];
1273 if (SplitCommand(data
, firstArg
, secondArg
))
1275 bool hasArg
= (wxStrlen(secondArg
) > 0);
1276 if (wxStrcmp(firstArg
, _T("INPUT")) == 0 && hasArg
)
1278 InputFile
= secondArg
;
1282 wxString str
= wxFileNameFromPath(InputFile
);
1283 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
1284 frame
->SetTitle(buf
);
1287 else if (wxStrcmp(firstArg
, _T("OUTPUT")) == 0 && hasArg
)
1289 OutputFile
= secondArg
;
1291 else if (wxStrcmp(firstArg
, _T("GO")) == 0)
1293 wxStrcpy(Tex2RTFLastStatus
, _T("WORKING"));
1295 wxStrcpy(Tex2RTFLastStatus
, _T("CONVERSION ERROR"));
1297 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1299 else if (wxStrcmp(firstArg
, _T("EXIT")) == 0)
1301 if (frame
) frame
->Close();
1303 else if (wxStrcmp(firstArg
, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg
, _T("ICONIZE")) == 0)
1306 frame
->Iconize(true);
1308 else if (wxStrcmp(firstArg
, _T("SHOW")) == 0 || wxStrcmp(firstArg
, _T("RESTORE")) == 0)
1312 frame
->Iconize(false);
1318 // Try for a setting
1319 wxStrcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, false));
1320 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1321 if (frame
&& wxStrcmp(firstArg
, _T("conversionMode")) == 0)
1324 wxStrcpy(buf
, _T("In "));
1326 if (winHelp
&& (convertMode
== TEX_RTF
))
1327 wxStrcat(buf
, _T("WinHelp RTF"));
1328 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1329 wxStrcat(buf
, _T("linear RTF"));
1330 else if (convertMode
== TEX_HTML
) wxStrcat(buf
, _T("HTML"));
1331 else if (convertMode
== TEX_XLP
) wxStrcat(buf
, _T("XLP"));
1332 wxStrcat(buf
, _T(" mode."));
1333 frame
->SetStatusText(buf
, 1);
1341 wxChar
*Tex2RTFConnection::OnRequest(const wxString
& WXUNUSED(topic
), const wxString
& WXUNUSED(item
), int *WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1343 return Tex2RTFLastStatus
;
1350 //void wxObject::Dump(wxSTD ostream& str)
1352 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1353 // str << GetClassInfo()->GetClassName();
1355 // str << "unknown object class";