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)
311 buf
.Printf(_T("Invalid switch %s.\n"), argv
[i
]);
312 OnError((wxChar
*)buf
.c_str());
322 #if defined(__WXMSW__) && !defined(NO_GUI)
324 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
325 TheTex2RTFServer
= new Tex2RTFServer
;
326 TheTex2RTFServer
->Create(_T("TEX2RTF"));
329 TexInitialize(BufSize
);
330 ResetContentsLevels(0);
336 // Create the main frame window
337 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Tex2RTF"), wxDefaultPosition
, wxSize(400, 300));
339 frame
->CreateStatusBar(2);
340 #endif // wxUSE_STATUSBAR
343 // TODO: uncomment this when we have tex2rtf.xpm
344 frame
->SetIcon(wxICON(tex2rtf
));
346 if (!InputFile
.empty())
349 title
.Printf( _T("Tex2RTF [%s]"), wxFileNameFromPath(InputFile
).c_str());
350 frame
->SetTitle(title
);
354 wxMenu
*file_menu
= new wxMenu
;
355 file_menu
->Append(TEX_GO
, _T("&Go"), _T("Run converter"));
356 file_menu
->Append(TEX_SET_INPUT
, _T("Set &Input File"), _T("Set the LaTeX input file"));
357 file_menu
->Append(TEX_SET_OUTPUT
, _T("Set &Output File"), _T("Set the output file"));
358 file_menu
->AppendSeparator();
359 file_menu
->Append(TEX_VIEW_LATEX
, _T("View &LaTeX File"), _T("View the LaTeX input file"));
360 file_menu
->Append(TEX_VIEW_OUTPUT
, _T("View Output &File"), _T("View output file"));
361 file_menu
->Append(TEX_SAVE_FILE
, _T("&Save log file"), _T("Save displayed text into file"));
362 file_menu
->AppendSeparator();
363 file_menu
->Append(TEX_QUIT
, _T("E&xit"), _T("Exit Tex2RTF"));
365 wxMenu
*macro_menu
= new wxMenu
;
367 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, _T("&Load Custom Macros"), _T("Load custom LaTeX macro file"));
368 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, _T("View &Custom Macros"), _T("View custom LaTeX macros"));
370 wxMenu
*mode_menu
= new wxMenu
;
372 mode_menu
->Append(TEX_MODE_RTF
, _T("Output linear &RTF"), _T("Wordprocessor-compatible RTF"));
373 mode_menu
->Append(TEX_MODE_WINHELP
, _T("Output &WinHelp RTF"), _T("WinHelp-compatible RTF"));
374 mode_menu
->Append(TEX_MODE_HTML
, _T("Output &HTML"), _T("HTML World Wide Web hypertext file"));
375 mode_menu
->Append(TEX_MODE_XLP
, _T("Output &XLP"), _T("wxHelp hypertext help file"));
377 wxMenu
*options_menu
= new wxMenu
;
379 options_menu
->Append(TEX_OPTIONS_CURLY_BRACE
, _T("Curly brace matching"), _T("Checks for mismatched curly braces"),true);
380 options_menu
->Append(TEX_OPTIONS_SYNTAX_CHECKING
, _T("Syntax checking"), _T("Syntax checking for common errors"),true);
382 options_menu
->Check(TEX_OPTIONS_CURLY_BRACE
, checkCurlyBraces
);
383 options_menu
->Check(TEX_OPTIONS_SYNTAX_CHECKING
, checkSyntax
);
385 wxMenu
*help_menu
= new wxMenu
;
387 help_menu
->Append(TEX_HELP
, _T("&Help"), _T("Tex2RTF Contents Page"));
388 help_menu
->Append(TEX_ABOUT
, _T("&About Tex2RTF"), _T("About Tex2RTF"));
390 menuBar
= new wxMenuBar
;
391 menuBar
->Append(file_menu
, _T("&File"));
392 menuBar
->Append(macro_menu
, _T("&Macros"));
393 menuBar
->Append(mode_menu
, _T("&Conversion Mode"));
394 menuBar
->Append(options_menu
, _T("&Options"));
395 menuBar
->Append(help_menu
, _T("&Help"));
397 frame
->SetMenuBar(menuBar
);
398 frame
->textWindow
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_READONLY
|wxTE_MULTILINE
);
400 (*frame
->textWindow
) << _T("Welcome to Tex2RTF.\n");
404 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
405 HelpInstance
= new wxCHMHelpController
;
407 HelpInstance
= new wxHelpController
;
409 HelpInstance
->Initialize(_T("tex2rtf"));
413 * Read macro/initialisation file
417 wxString path
= TexPathList
.FindValidPath(MacroFile
);
419 ReadCustomMacros(path
);
422 wxString
inStr(_T("In "));
427 inStr
+= _T("WinHelp RTF");
429 inStr
+= _T("linear RTF");
441 inStr
+= _T("unknown");
444 inStr
+= _T(" mode.");
445 frame
->SetStatusText(inStr
, 1);
446 #endif // wxUSE_STATUSBAR
455 * Read macro/initialisation file
459 wxString path
= TexPathList
.FindValidPath(MacroFile
);
461 ReadCustomMacros(path
);
464 if ( rc
&& runTwice
)
471 OnExit(); // Do cleanup since OnExit won't be called now
480 wxNode
*node
= CustomMacroList
.GetFirst();
483 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
486 node
= CustomMacroList
.GetFirst();
488 MacroDefs
.BeginFind();
489 wxHashTable::Node
* mNode
= MacroDefs
.Next();
492 TexMacroDef
* def
= (TexMacroDef
*) mNode
->GetData();
494 mNode
= MacroDefs
.Next();
498 delete TheTex2RTFServer
;
513 delete currentArgData
;
514 currentArgData
= NULL
;
528 delete TexTmpBibName
;
529 TexTmpBibName
= NULL
;
543 delete TmpContentsName
;
544 TmpContentsName
= NULL
;
546 if (TmpFrameContentsName
)
548 delete TmpFrameContentsName
;
549 TmpFrameContentsName
= NULL
;
551 if (WinHelpContentsFileName
)
553 delete WinHelpContentsFileName
;
554 WinHelpContentsFileName
= NULL
;
578 delete [] BibliographyStyleString
;
579 delete [] DocumentStyleString
;
580 delete [] bitmapMethod
;
581 delete [] backgroundColourString
;
582 delete [] ContentsNameString
;
583 delete [] AbstractNameString
;
584 delete [] GlossaryNameString
;
585 delete [] ReferencesNameString
;
586 delete [] FiguresNameString
;
587 delete [] TablesNameString
;
588 delete [] FigureNameString
;
589 delete [] TableNameString
;
590 delete [] IndexNameString
;
591 delete [] ChapterNameString
;
592 delete [] SectionNameString
;
593 delete [] SubsectionNameString
;
594 delete [] SubsubsectionNameString
;
595 delete [] UpNameString
;
597 delete[] winHelpTitle
;
599 // TODO: this simulates zero-memory leaks!
600 // Otherwise there are just too many...
602 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
603 wxDebugContext::SetCheckpoint();
611 void ShowVersion(void)
614 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF version %.2f"), versionNo
);
618 void ShowOptions(void)
621 OnInform(_T("Usage: tex2rtf [input] [output] [switches]\n"));
622 OnInform(_T("where valid switches are"));
624 OnInform(_T(" -interactive"));
626 OnInform(_T(" -bufsize <size in K>"));
627 OnInform(_T(" -charset <pc | pca | ansi | mac> (default ansi)"));
628 OnInform(_T(" -twice"));
629 OnInform(_T(" -sync"));
630 OnInform(_T(" -checkcurlybraces"));
631 OnInform(_T(" -checksyntax"));
632 OnInform(_T(" -version"));
633 OnInform(_T(" -macros <filename>"));
634 OnInform(_T(" -winhelp"));
635 OnInform(_T(" -rtf"));
636 OnInform(_T(" -html"));
637 OnInform(_T(" -xlp\n"));
642 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
643 EVT_CLOSE(MyFrame::OnCloseWindow
)
644 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
645 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
646 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
647 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
648 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
649 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
650 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
651 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
652 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
653 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
654 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
655 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
656 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
657 EVT_MENU(TEX_OPTIONS_CURLY_BRACE
, MyFrame::OnOptionsCurlyBrace
)
658 EVT_MENU(TEX_OPTIONS_SYNTAX_CHECKING
, MyFrame::OnOptionsSyntaxChecking
)
659 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
660 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
663 // My frame constructor
664 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
665 wxFrame(frame
, id
, title
, pos
, size
)
668 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
670 if (!stopRunning
&& !OkToClose
)
682 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
688 void MyFrame::OnGo(wxCommandEvent
& WXUNUSED(event
))
692 menuBar
->EnableTop(0, false);
693 menuBar
->EnableTop(1, false);
694 menuBar
->EnableTop(2, false);
695 menuBar
->EnableTop(3, false);
703 SetStatusText(_T("Build aborted!"));
704 #endif // wxUSE_STATUSBAR
706 errBuf
.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount
);
707 OnInform((wxChar
*)errBuf
.c_str());
711 if (runTwice
&& !stopRunning
)
716 menuBar
->EnableTop(0, true);
717 menuBar
->EnableTop(1, true);
718 menuBar
->EnableTop(2, true);
719 menuBar
->EnableTop(3, true);
722 void MyFrame::OnSetInput(wxCommandEvent
& WXUNUSED(event
))
724 ChooseInputFile(true);
727 void MyFrame::OnSetOutput(wxCommandEvent
& WXUNUSED(event
))
729 ChooseOutputFile(true);
732 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
735 wxString s
= wxFileSelector(_T("Save text to file"), wxEmptyString
, wxEmptyString
, _T("txt"), _T("*.txt"));
738 textWindow
->SaveFile(s
);
741 wxSnprintf(buf
, sizeof(buf
), _T("Saved text to %s"), (const wxChar
*) s
.c_str());
742 frame
->SetStatusText(buf
, 0);
743 #endif // wxUSE_STATUSBAR
745 #endif // wxUSE_FILEDLG
748 void MyFrame::OnViewOutput(wxCommandEvent
& WXUNUSED(event
))
751 if (!OutputFile
.empty() && wxFileExists(OutputFile
))
753 textWindow
->LoadFile(OutputFile
);
755 wxString
str(wxFileNameFromPath(OutputFile
));
756 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
757 frame
->SetTitle(buf
);
761 void MyFrame::OnViewLatex(wxCommandEvent
& WXUNUSED(event
))
764 if (!InputFile
.empty() && wxFileExists(InputFile
))
766 textWindow
->LoadFile(InputFile
);
768 wxString
str(wxFileNameFromPath(OutputFile
));
769 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
770 frame
->SetTitle(buf
);
774 void MyFrame::OnLoadMacros(wxCommandEvent
& WXUNUSED(event
))
778 wxString s
= wxFileSelector(_T("Choose custom macro file"), wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), _T("ini"), _T("*.ini"));
779 if (!s
.empty() && wxFileExists(s
))
781 MacroFile
= copystring(s
);
785 #endif // wxUSE_FILEDLG
788 void MyFrame::OnShowMacros(wxCommandEvent
& WXUNUSED(event
))
795 void MyFrame::OnModeRTF(wxCommandEvent
& WXUNUSED(event
))
797 convertMode
= TEX_RTF
;
799 InputFile
= wxEmptyString
;
800 OutputFile
= wxEmptyString
;
802 SetStatusText(_T("In linear RTF mode."), 1);
803 #endif // wxUSE_STATUSBAR
806 void MyFrame::OnModeWinHelp(wxCommandEvent
& WXUNUSED(event
))
808 convertMode
= TEX_RTF
;
810 InputFile
= wxEmptyString
;
811 OutputFile
= wxEmptyString
;
813 SetStatusText(_T("In WinHelp RTF mode."), 1);
814 #endif // wxUSE_STATUSBAR
817 void MyFrame::OnModeHTML(wxCommandEvent
& WXUNUSED(event
))
819 convertMode
= TEX_HTML
;
821 InputFile
= wxEmptyString
;
822 OutputFile
= wxEmptyString
;
824 SetStatusText(_T("In HTML mode."), 1);
825 #endif // wxUSE_STATUSBAR
828 void MyFrame::OnModeXLP(wxCommandEvent
& WXUNUSED(event
))
830 convertMode
= TEX_XLP
;
831 InputFile
= wxEmptyString
;
832 OutputFile
= wxEmptyString
;
834 SetStatusText(_T("In XLP mode."), 1);
835 #endif // wxUSE_STATUSBAR
838 void MyFrame::OnOptionsCurlyBrace(wxCommandEvent
& WXUNUSED(event
))
840 checkCurlyBraces
= !checkCurlyBraces
;
842 if (checkCurlyBraces
)
844 SetStatusText(_T("Checking curly braces: YES"), 1);
848 SetStatusText(_T("Checking curly braces: NO"), 1);
850 #endif // wxUSE_STATUSBAR
854 void MyFrame::OnOptionsSyntaxChecking(wxCommandEvent
& WXUNUSED(event
))
856 checkSyntax
= !checkSyntax
;
860 SetStatusText(_T("Checking syntax: YES"), 1);
864 SetStatusText(_T("Checking syntax: NO"), 1);
866 #endif // wxUSE_STATUSBAR
870 void MyFrame::OnHelp(wxCommandEvent
& WXUNUSED(event
))
873 HelpInstance
->LoadFile();
874 HelpInstance
->DisplayContents();
878 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
881 wxString platform
= wxGetOsDescription();
882 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());
883 wxMessageBox(buf
, _T("About Tex2RTF"));
886 void ChooseInputFile(bool force
)
889 if (force
|| InputFile
.empty())
891 wxString s
= wxFileSelector(_T("Choose LaTeX input file"), wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), _T("tex"), _T("*.tex"));
894 // Different file, so clear index entries.
896 ResetContentsLevels(0);
901 wxString str
= wxFileNameFromPath(InputFile
);
903 buf
.Printf(_T("Tex2RTF [%s]"), str
.c_str());
904 frame
->SetTitle((wxChar
*)buf
.c_str());
905 OutputFile
= wxEmptyString
;
910 #endif // wxUSE_FILEDLG
913 void ChooseOutputFile(bool force
)
915 wxChar extensionBuf
[10];
917 wxStrcpy(wildBuf
, _T("*."));
919 if (!OutputFile
.empty())
920 path
= wxPathOnly(OutputFile
);
921 else if (!InputFile
.empty())
922 path
= wxPathOnly(InputFile
);
928 wxStrcpy(extensionBuf
, _T("rtf"));
929 wxStrcat(wildBuf
, _T("rtf"));
934 wxStrcpy(extensionBuf
, _T("xlp"));
935 wxStrcat(wildBuf
, _T("xlp"));
940 wxStrcpy(extensionBuf
, _T("html"));
941 wxStrcat(wildBuf
, _T("html"));
946 if (force
|| OutputFile
.empty())
948 wxString s
= wxFileSelector(_T("Choose output file"), path
, wxFileNameFromPath(OutputFile
),
949 extensionBuf
, wildBuf
);
955 #endif // wxUSE_FILEDLG
966 if (InputFile
.empty() || OutputFile
.empty() || stopRunning
)
973 wxString str
= wxFileNameFromPath(InputFile
);
975 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
976 frame
->SetTitle(buf
);
979 wxLongLong localTime
= wxGetLocalTimeMillis();
982 // Find extension-less filename
983 wxStrcpy(FileRoot
, OutputFile
.c_str());
984 StripExtension(FileRoot
);
986 if (truncateFilenames
&& convertMode
== TEX_HTML
)
988 // Truncate to five characters. This ensures that
989 // we can generate DOS filenames such as thing999. But 1000 files
990 // may not be enough, of course...
991 wxChar
* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
994 if(wxStrlen( sName
) > 5)
995 sName
[5] = '\0'; // that should do!
998 wxSnprintf(ContentsName
, 300, _T("%s.con"), FileRoot
);
999 wxSnprintf(TmpContentsName
, 300, _T("%s.cn1"), FileRoot
);
1000 wxSnprintf(TmpFrameContentsName
, 300, _T("%s.frc"), FileRoot
);
1001 wxSnprintf(WinHelpContentsFileName
, 300, _T("%s.cnt"), FileRoot
);
1002 wxSnprintf(RefFileName
, 300, _T("%s.ref"), FileRoot
);
1004 TexPathList
.EnsureFileAccessible(InputFile
);
1007 wxString s
= TexPathList
.FindValidPath(_T("bullet.bmp"));
1010 wxString str
= wxFileNameFromPath(s
);
1011 bulletFile
= copystring(str
);
1015 if (wxFileExists(RefFileName
))
1016 ReadTexReferences(RefFileName
);
1018 bool success
= false;
1020 if (!InputFile
.empty() && !OutputFile
.empty())
1022 if (!wxFileExists(InputFile
))
1024 OnError(_T("Cannot open input file!"));
1028 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1032 buf
.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber
);
1033 frame
->SetStatusText((wxChar
*)buf
.c_str());
1037 OnInform(_T("Reading LaTeX file..."));
1038 TexLoadFile(InputFile
);
1046 switch (convertMode
)
1067 OnInform(_T("*** Aborted by user."));
1069 stopRunning
= false;
1075 WriteTexReferences(RefFileName
);
1077 startedSections
= false;
1081 wxLongLong elapsed
= wxGetLocalTimeMillis() - localTime
;
1082 buf
.Printf(_T("Finished PASS #%d in %ld seconds.\n"), passNumber
, (long)(elapsed
.GetLo()/1000.0));
1083 OnInform((wxChar
*)buf
.c_str());
1087 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1088 OnInform((wxChar
*)buf
.c_str());
1094 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1095 frame
->SetStatusText((wxChar
*)buf
.c_str());
1097 #endif // wxUSE_STATUSBAR
1099 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1100 OnInform((wxChar
*)buf
.c_str());
1103 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1104 OnInform((wxChar
*)buf
.c_str());
1114 startedSections
= false;
1116 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1117 frame
->SetStatusText(_T("Aborted by user."));
1120 OnInform(_T("Sorry, unsuccessful."));
1125 void OnError(const wxChar
*msg
)
1127 wxString msg_string
= msg
;
1131 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1134 if (isInteractive
&& frame
)
1136 (*frame
->textWindow
) << _T("Error: ") << msg
<< _T("\n");
1140 #if defined(__UNIX__)
1141 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1143 #elif defined(__WXMSW__)
1152 void OnInform(const wxChar
*msg
)
1154 wxString msg_string
= msg
;
1156 wxSTD cout
<< msg_string
.mb_str() << "\n";
1159 if (isInteractive
&& frame
)
1161 (*frame
->textWindow
) << msg
<< _T("\n");
1165 #if defined(__UNIX__)
1166 wxSTD cout
<< msg_string
.mb_str() << "\n";
1168 #elif defined(__WXMSW__)
1180 void OnMacro(int macroId
, int no_args
, bool start
)
1182 switch (convertMode
)
1186 RTFOnMacro(macroId
, no_args
, start
);
1191 XLPOnMacro(macroId
, no_args
, start
);
1196 HTMLOnMacro(macroId
, no_args
, start
);
1202 bool OnArgument(int macroId
, int arg_no
, bool start
)
1204 switch (convertMode
)
1208 return RTFOnArgument(macroId
, arg_no
, start
);
1213 return XLPOnArgument(macroId
, arg_no
, start
);
1218 return HTMLOnArgument(macroId
, arg_no
, start
);
1228 #if defined(__WXMSW__) && !defined(NO_GUI)
1234 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
1236 if (topic
== _T("TEX2RTF"))
1239 ipc_buffer
= new wxChar
[1000];
1241 return new Tex2RTFConnection(ipc_buffer
, 4000);
1251 Tex2RTFConnection::Tex2RTFConnection(wxChar
*buf
, int size
):wxDDEConnection(buf
, size
)
1255 bool SplitCommand(wxChar
*data
, wxChar
*firstArg
, wxChar
*secondArg
)
1261 // Find first argument (command name)
1264 if (data
[i
] == ' ' || data
[i
] == 0)
1268 firstArg
[i
] = data
[i
];
1275 // Find second argument
1278 while (data
[i
] != 0)
1280 secondArg
[j
] = data
[i
];
1289 bool Tex2RTFConnection::OnExecute(const wxString
& WXUNUSED(topic
), wxChar
*data
, int WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1291 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1293 wxChar firstArg
[50];
1294 wxChar secondArg
[300];
1295 if (SplitCommand(data
, firstArg
, secondArg
))
1297 bool hasArg
= (wxStrlen(secondArg
) > 0);
1298 if (wxStrcmp(firstArg
, _T("INPUT")) == 0 && hasArg
)
1300 InputFile
= secondArg
;
1304 wxString str
= wxFileNameFromPath(InputFile
);
1305 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
1306 frame
->SetTitle(buf
);
1309 else if (wxStrcmp(firstArg
, _T("OUTPUT")) == 0 && hasArg
)
1311 OutputFile
= secondArg
;
1313 else if (wxStrcmp(firstArg
, _T("GO")) == 0)
1315 wxStrcpy(Tex2RTFLastStatus
, _T("WORKING"));
1317 wxStrcpy(Tex2RTFLastStatus
, _T("CONVERSION ERROR"));
1319 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1321 else if (wxStrcmp(firstArg
, _T("EXIT")) == 0)
1323 if (frame
) frame
->Close();
1325 else if (wxStrcmp(firstArg
, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg
, _T("ICONIZE")) == 0)
1328 frame
->Iconize(true);
1330 else if (wxStrcmp(firstArg
, _T("SHOW")) == 0 || wxStrcmp(firstArg
, _T("RESTORE")) == 0)
1334 frame
->Iconize(false);
1340 // Try for a setting
1341 wxStrcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, false));
1342 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1343 if (frame
&& wxStrcmp(firstArg
, _T("conversionMode")) == 0)
1346 wxStrcpy(buf
, _T("In "));
1348 if (winHelp
&& (convertMode
== TEX_RTF
))
1349 wxStrcat(buf
, _T("WinHelp RTF"));
1350 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1351 wxStrcat(buf
, _T("linear RTF"));
1352 else if (convertMode
== TEX_HTML
) wxStrcat(buf
, _T("HTML"));
1353 else if (convertMode
== TEX_XLP
) wxStrcat(buf
, _T("XLP"));
1354 wxStrcat(buf
, _T(" mode."));
1355 frame
->SetStatusText(buf
, 1);
1363 wxChar
*Tex2RTFConnection::OnRequest(const wxString
& WXUNUSED(topic
), const wxString
& WXUNUSED(item
), int *WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1365 return Tex2RTFLastStatus
;
1372 //void wxObject::Dump(wxSTD ostream& str)
1374 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1375 // str << GetClassInfo()->GetClassName();
1377 // str << "unknown object class";