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 static inline wxChar
* copystring(const wxChar
* s
)
70 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
72 const float versionNo
= float(TEX2RTF_VERSION_NUMBER
);
74 TexChunk
*currentMember
= NULL
;
75 bool startedSections
= false;
76 wxChar
*contentsString
= NULL
;
77 bool suppressNameDecoration
= false;
78 bool OkToClose
= true;
80 unsigned long errorCount
= 0;
84 extern wxChar
*BigBuffer
;
85 extern wxChar
*TexFileRoot
;
86 extern wxChar
*TexBibName
; // Bibliography output file name
87 extern wxChar
*TexTmpBibName
; // Temporary bibliography output file name
88 extern wxList ColourTable
;
89 extern TexChunk
*TopLevel
;
92 wxHelpControllerBase
*HelpInstance
= NULL
;
96 static wxChar
*ipc_buffer
= NULL
;
97 static wxChar Tex2RTFLastStatus
[100];
98 Tex2RTFServer
*TheTex2RTFServer
= NULL
;
103 wxChar
*bulletFile
= NULL
;
105 FILE *Contents
= NULL
; // Contents page
106 FILE *Chapters
= NULL
; // Chapters (WinHelp RTF) or rest of file (linear RTF)
107 FILE *Sections
= NULL
;
108 FILE *Subsections
= NULL
;
109 FILE *Subsubsections
= NULL
;
111 FILE *WinHelpContentsFile
= NULL
;
115 wxChar
*MacroFile
= copystring(_T("tex2rtf.ini"));
117 wxChar
*FileRoot
= NULL
;
118 wxChar
*ContentsName
= NULL
; // Contents page from last time around
119 wxChar
*TmpContentsName
= NULL
; // Current contents page
120 wxChar
*TmpFrameContentsName
= NULL
; // Current frame contents page
121 wxChar
*WinHelpContentsFileName
= NULL
; // WinHelp .cnt file
122 wxChar
*RefFileName
= NULL
; // Reference file name
124 wxChar
*RTFCharset
= copystring(_T("ansi"));
127 int BufSize
= 100; // Size of buffer in K
133 void ShowOptions(void);
134 void ShowVersion(void);
136 wxChar wxTex2RTFBuffer
[1500];
139 IMPLEMENT_APP_CONSOLE(MyApp
)
141 wxMenuBar
*menuBar
= NULL
;
142 MyFrame
*frame
= NULL
;
143 // DECLARE_APP(MyApp)
147 // `Main program' equivalent, creating windows and returning main app frame
150 // Use default list of macros defined in tex2any.cc
151 DefineDefaultMacros();
152 AddMacroDef(ltHARDY
, _T("hardy"), 0);
154 FileRoot
= new wxChar
[300];
155 ContentsName
= new wxChar
[300];
156 TmpContentsName
= new wxChar
[300];
157 TmpFrameContentsName
= new wxChar
[300];
158 WinHelpContentsFileName
= new wxChar
[300];
159 RefFileName
= new wxChar
[300];
161 WX_CLEAR_LIST(wxList
,ColourTable
);
165 // Read input/output files
168 if (argv
[1][0] != _T('-'))
175 if (argv
[2][0] != _T('-'))
177 OutputFile
= argv
[2];
184 TexPathList
.Add(::wxGetCwd());
187 for (i
= n
; i
< argc
;)
189 if (wxStrcmp(argv
[i
], _T("-winhelp")) == 0)
192 convertMode
= TEX_RTF
;
196 else if (wxStrcmp(argv
[i
], _T("-interactive")) == 0)
199 isInteractive
= true;
202 else if (wxStrcmp(argv
[i
], _T("-sync")) == 0) // Don't yield
207 else if (wxStrcmp(argv
[i
], _T("-rtf")) == 0)
210 convertMode
= TEX_RTF
;
212 else if (wxStrcmp(argv
[i
], _T("-html")) == 0)
215 convertMode
= TEX_HTML
;
217 else if (wxStrcmp(argv
[i
], _T("-xlp")) == 0)
220 convertMode
= TEX_XLP
;
222 else if (wxStrcmp(argv
[i
], _T("-twice")) == 0)
227 else if (wxStrcmp(argv
[i
], _T("-macros")) == 0)
232 MacroFile
= copystring(argv
[i
]);
236 else if (wxStrcmp(argv
[i
], _T("-bufsize")) == 0)
241 BufSize
= wxAtoi(argv
[i
]);
245 else if (wxStrcmp(argv
[i
], _T("-charset")) == 0)
252 if (wxStrcmp(s
, _T("ansi")) == 0 || wxStrcmp(s
, _T("pc")) == 0 || wxStrcmp(s
, _T("mac")) == 0 ||
253 wxStrcmp(s
, _T("pca")) == 0)
254 RTFCharset
= copystring(s
);
257 OnError(_T("Incorrect argument for -charset"));
262 else if (wxStrcmp(argv
[i
], _T("-checkcurlybraces")) == 0)
265 checkCurlyBraces
= true;
267 else if (wxStrcmp(argv
[i
], _T("-checkcurleybraces")) == 0)
269 // Support the old, incorrectly spelled version of -checkcurlybraces
270 // so that old scripts which run tex2rtf -checkcurleybraces still work.
272 checkCurlyBraces
= true;
274 else if (wxStrcmp(argv
[i
], _T("-checksyntax")) == 0)
279 else if (wxStrcmp(argv
[i
], _T("-version")) == 0)
292 buf
.Printf(_T("Invalid switch %s.\n"), argv
[i
]);
304 if (InputFile
.empty() || OutputFile
.empty())
306 wxSTD cout
<< "Tex2RTF: input or output file is missing.\n";
312 if (!InputFile
.empty())
314 TexPathList
.EnsureFileAccessible(InputFile
);
316 if (InputFile
.empty() || OutputFile
.empty())
317 isInteractive
= true;
319 #if defined(__WXMSW__) && !defined(NO_GUI)
321 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
322 TheTex2RTFServer
= new Tex2RTFServer
;
323 TheTex2RTFServer
->Create(_T("TEX2RTF"));
326 TexInitialize(BufSize
);
327 ResetContentsLevels(0);
333 // Create the main frame window
334 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Tex2RTF"), wxDefaultPosition
, wxSize(400, 300));
336 frame
->CreateStatusBar(2);
337 #endif // wxUSE_STATUSBAR
340 // TODO: uncomment this when we have tex2rtf.xpm
341 frame
->SetIcon(wxICON(tex2rtf
));
343 if (!InputFile
.empty())
346 title
.Printf( _T("Tex2RTF [%s]"), wxFileNameFromPath(InputFile
).c_str());
347 frame
->SetTitle(title
);
351 wxMenu
*file_menu
= new wxMenu
;
352 file_menu
->Append(TEX_GO
, _T("&Go"), _T("Run converter"));
353 file_menu
->Append(TEX_SET_INPUT
, _T("Set &Input File"), _T("Set the LaTeX input file"));
354 file_menu
->Append(TEX_SET_OUTPUT
, _T("Set &Output File"), _T("Set the output file"));
355 file_menu
->AppendSeparator();
356 file_menu
->Append(TEX_VIEW_LATEX
, _T("View &LaTeX File"), _T("View the LaTeX input file"));
357 file_menu
->Append(TEX_VIEW_OUTPUT
, _T("View Output &File"), _T("View output file"));
358 file_menu
->Append(TEX_SAVE_FILE
, _T("&Save log file"), _T("Save displayed text into file"));
359 file_menu
->AppendSeparator();
360 file_menu
->Append(TEX_QUIT
, _T("E&xit"), _T("Exit Tex2RTF"));
362 wxMenu
*macro_menu
= new wxMenu
;
364 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, _T("&Load Custom Macros"), _T("Load custom LaTeX macro file"));
365 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, _T("View &Custom Macros"), _T("View custom LaTeX macros"));
367 wxMenu
*mode_menu
= new wxMenu
;
369 mode_menu
->Append(TEX_MODE_RTF
, _T("Output linear &RTF"), _T("Wordprocessor-compatible RTF"));
370 mode_menu
->Append(TEX_MODE_WINHELP
, _T("Output &WinHelp RTF"), _T("WinHelp-compatible RTF"));
371 mode_menu
->Append(TEX_MODE_HTML
, _T("Output &HTML"), _T("HTML World Wide Web hypertext file"));
372 mode_menu
->Append(TEX_MODE_XLP
, _T("Output &XLP"), _T("wxHelp hypertext help file"));
374 wxMenu
*options_menu
= new wxMenu
;
376 options_menu
->Append(TEX_OPTIONS_CURLY_BRACE
, _T("Curly brace matching"), _T("Checks for mismatched curly braces"),true);
377 options_menu
->Append(TEX_OPTIONS_SYNTAX_CHECKING
, _T("Syntax checking"), _T("Syntax checking for common errors"),true);
379 options_menu
->Check(TEX_OPTIONS_CURLY_BRACE
, checkCurlyBraces
);
380 options_menu
->Check(TEX_OPTIONS_SYNTAX_CHECKING
, checkSyntax
);
382 wxMenu
*help_menu
= new wxMenu
;
384 help_menu
->Append(TEX_HELP
, _T("&Help"), _T("Tex2RTF Contents Page"));
385 help_menu
->Append(TEX_ABOUT
, _T("&About Tex2RTF"), _T("About Tex2RTF"));
387 menuBar
= new wxMenuBar
;
388 menuBar
->Append(file_menu
, _T("&File"));
389 menuBar
->Append(macro_menu
, _T("&Macros"));
390 menuBar
->Append(mode_menu
, _T("&Conversion Mode"));
391 menuBar
->Append(options_menu
, _T("&Options"));
392 menuBar
->Append(help_menu
, _T("&Help"));
394 frame
->SetMenuBar(menuBar
);
395 frame
->textWindow
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_READONLY
|wxTE_MULTILINE
);
397 (*frame
->textWindow
) << _T("Welcome to Tex2RTF.\n");
401 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
402 HelpInstance
= new wxCHMHelpController
;
404 HelpInstance
= new wxHelpController
;
406 HelpInstance
->Initialize(_T("tex2rtf"));
410 * Read macro/initialisation file
414 wxString path
= TexPathList
.FindValidPath(MacroFile
);
416 ReadCustomMacros(path
);
419 wxString
inStr(_T("In "));
424 inStr
+= _T("WinHelp RTF");
426 inStr
+= _T("linear RTF");
438 inStr
+= _T("unknown");
441 inStr
+= _T(" mode.");
442 frame
->SetStatusText(inStr
, 1);
443 #endif // wxUSE_STATUSBAR
452 * Read macro/initialisation file
456 wxString path
= TexPathList
.FindValidPath(MacroFile
);
458 ReadCustomMacros(path
);
461 if ( rc
&& runTwice
)
468 OnExit(); // Do cleanup since OnExit won't be called now
477 wxNode
*node
= CustomMacroList
.GetFirst();
480 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
483 node
= CustomMacroList
.GetFirst();
485 MacroDefs
.BeginFind();
486 wxHashTable::Node
* mNode
= MacroDefs
.Next();
489 TexMacroDef
* def
= (TexMacroDef
*) mNode
->GetData();
491 mNode
= MacroDefs
.Next();
495 delete TheTex2RTFServer
;
510 delete currentArgData
;
511 currentArgData
= NULL
;
525 delete TexTmpBibName
;
526 TexTmpBibName
= NULL
;
540 delete TmpContentsName
;
541 TmpContentsName
= NULL
;
543 if (TmpFrameContentsName
)
545 delete TmpFrameContentsName
;
546 TmpFrameContentsName
= NULL
;
548 if (WinHelpContentsFileName
)
550 delete WinHelpContentsFileName
;
551 WinHelpContentsFileName
= NULL
;
575 delete [] BibliographyStyleString
;
576 delete [] DocumentStyleString
;
577 delete [] bitmapMethod
;
578 delete [] backgroundColourString
;
579 delete [] ContentsNameString
;
580 delete [] AbstractNameString
;
581 delete [] GlossaryNameString
;
582 delete [] ReferencesNameString
;
583 delete [] FiguresNameString
;
584 delete [] TablesNameString
;
585 delete [] FigureNameString
;
586 delete [] TableNameString
;
587 delete [] IndexNameString
;
588 delete [] ChapterNameString
;
589 delete [] SectionNameString
;
590 delete [] SubsectionNameString
;
591 delete [] SubsubsectionNameString
;
592 delete [] UpNameString
;
594 delete[] winHelpTitle
;
596 // TODO: this simulates zero-memory leaks!
597 // Otherwise there are just too many...
599 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
600 wxDebugContext::SetCheckpoint();
608 void ShowVersion(void)
611 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF version %.2f"), versionNo
);
615 void ShowOptions(void)
618 OnInform(_T("Usage: tex2rtf [input] [output] [switches]\n"));
619 OnInform(_T("where valid switches are"));
621 OnInform(_T(" -interactive"));
623 OnInform(_T(" -bufsize <size in K>"));
624 OnInform(_T(" -charset <pc | pca | ansi | mac> (default ansi)"));
625 OnInform(_T(" -twice"));
626 OnInform(_T(" -sync"));
627 OnInform(_T(" -checkcurlybraces"));
628 OnInform(_T(" -checksyntax"));
629 OnInform(_T(" -version"));
630 OnInform(_T(" -macros <filename>"));
631 OnInform(_T(" -winhelp"));
632 OnInform(_T(" -rtf"));
633 OnInform(_T(" -html"));
634 OnInform(_T(" -xlp\n"));
639 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
640 EVT_CLOSE(MyFrame::OnCloseWindow
)
641 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
642 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
643 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
644 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
645 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
646 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
647 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
648 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
649 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
650 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
651 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
652 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
653 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
654 EVT_MENU(TEX_OPTIONS_CURLY_BRACE
, MyFrame::OnOptionsCurlyBrace
)
655 EVT_MENU(TEX_OPTIONS_SYNTAX_CHECKING
, MyFrame::OnOptionsSyntaxChecking
)
656 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
657 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
660 // My frame constructor
661 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
662 wxFrame(frame
, id
, title
, pos
, size
)
665 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
667 if (!stopRunning
&& !OkToClose
)
679 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
685 void MyFrame::OnGo(wxCommandEvent
& WXUNUSED(event
))
689 menuBar
->EnableTop(0, false);
690 menuBar
->EnableTop(1, false);
691 menuBar
->EnableTop(2, false);
692 menuBar
->EnableTop(3, false);
700 SetStatusText(_T("Build aborted!"));
701 #endif // wxUSE_STATUSBAR
703 errBuf
.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount
);
708 if (runTwice
&& !stopRunning
)
713 menuBar
->EnableTop(0, true);
714 menuBar
->EnableTop(1, true);
715 menuBar
->EnableTop(2, true);
716 menuBar
->EnableTop(3, true);
719 void MyFrame::OnSetInput(wxCommandEvent
& WXUNUSED(event
))
721 ChooseInputFile(true);
724 void MyFrame::OnSetOutput(wxCommandEvent
& WXUNUSED(event
))
726 ChooseOutputFile(true);
729 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
732 wxString s
= wxFileSelector(_T("Save text to file"), wxEmptyString
, wxEmptyString
, _T("txt"), _T("*.txt"));
735 textWindow
->SaveFile(s
);
738 wxSnprintf(buf
, sizeof(buf
), _T("Saved text to %s"), (const wxChar
*) s
.c_str());
739 frame
->SetStatusText(buf
, 0);
740 #endif // wxUSE_STATUSBAR
742 #endif // wxUSE_FILEDLG
745 void MyFrame::OnViewOutput(wxCommandEvent
& WXUNUSED(event
))
748 if (!OutputFile
.empty() && wxFileExists(OutputFile
))
750 textWindow
->LoadFile(OutputFile
);
752 wxString
str(wxFileNameFromPath(OutputFile
));
753 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
754 frame
->SetTitle(buf
);
758 void MyFrame::OnViewLatex(wxCommandEvent
& WXUNUSED(event
))
761 if (!InputFile
.empty() && wxFileExists(InputFile
))
763 textWindow
->LoadFile(InputFile
);
765 wxString
str(wxFileNameFromPath(OutputFile
));
766 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
767 frame
->SetTitle(buf
);
771 void MyFrame::OnLoadMacros(wxCommandEvent
& WXUNUSED(event
))
775 wxString s
= wxFileSelector(_T("Choose custom macro file"), wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), _T("ini"), _T("*.ini"));
776 if (!s
.empty() && wxFileExists(s
))
778 MacroFile
= copystring(s
);
782 #endif // wxUSE_FILEDLG
785 void MyFrame::OnShowMacros(wxCommandEvent
& WXUNUSED(event
))
792 void MyFrame::OnModeRTF(wxCommandEvent
& WXUNUSED(event
))
794 convertMode
= TEX_RTF
;
796 InputFile
= wxEmptyString
;
797 OutputFile
= wxEmptyString
;
799 SetStatusText(_T("In linear RTF mode."), 1);
800 #endif // wxUSE_STATUSBAR
803 void MyFrame::OnModeWinHelp(wxCommandEvent
& WXUNUSED(event
))
805 convertMode
= TEX_RTF
;
807 InputFile
= wxEmptyString
;
808 OutputFile
= wxEmptyString
;
810 SetStatusText(_T("In WinHelp RTF mode."), 1);
811 #endif // wxUSE_STATUSBAR
814 void MyFrame::OnModeHTML(wxCommandEvent
& WXUNUSED(event
))
816 convertMode
= TEX_HTML
;
818 InputFile
= wxEmptyString
;
819 OutputFile
= wxEmptyString
;
821 SetStatusText(_T("In HTML mode."), 1);
822 #endif // wxUSE_STATUSBAR
825 void MyFrame::OnModeXLP(wxCommandEvent
& WXUNUSED(event
))
827 convertMode
= TEX_XLP
;
828 InputFile
= wxEmptyString
;
829 OutputFile
= wxEmptyString
;
831 SetStatusText(_T("In XLP mode."), 1);
832 #endif // wxUSE_STATUSBAR
835 void MyFrame::OnOptionsCurlyBrace(wxCommandEvent
& WXUNUSED(event
))
837 checkCurlyBraces
= !checkCurlyBraces
;
839 if (checkCurlyBraces
)
841 SetStatusText(_T("Checking curly braces: YES"), 1);
845 SetStatusText(_T("Checking curly braces: NO"), 1);
847 #endif // wxUSE_STATUSBAR
851 void MyFrame::OnOptionsSyntaxChecking(wxCommandEvent
& WXUNUSED(event
))
853 checkSyntax
= !checkSyntax
;
857 SetStatusText(_T("Checking syntax: YES"), 1);
861 SetStatusText(_T("Checking syntax: NO"), 1);
863 #endif // wxUSE_STATUSBAR
867 void MyFrame::OnHelp(wxCommandEvent
& WXUNUSED(event
))
870 HelpInstance
->LoadFile();
871 HelpInstance
->DisplayContents();
875 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
878 wxString platform
= wxGetOsDescription();
879 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());
880 wxMessageBox(buf
, _T("About Tex2RTF"));
883 void ChooseInputFile(bool force
)
886 if (force
|| InputFile
.empty())
888 wxString s
= wxFileSelector(_T("Choose LaTeX input file"), wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), _T("tex"), _T("*.tex"));
891 // Different file, so clear index entries.
893 ResetContentsLevels(0);
898 wxString str
= wxFileNameFromPath(InputFile
);
900 buf
.Printf(_T("Tex2RTF [%s]"), str
.c_str());
901 frame
->SetTitle(buf
);
902 OutputFile
= wxEmptyString
;
907 #endif // wxUSE_FILEDLG
910 void ChooseOutputFile(bool force
)
912 wxChar extensionBuf
[10];
914 wxStrcpy(wildBuf
, _T("*."));
916 if (!OutputFile
.empty())
917 path
= wxPathOnly(OutputFile
);
918 else if (!InputFile
.empty())
919 path
= wxPathOnly(InputFile
);
925 wxStrcpy(extensionBuf
, _T("rtf"));
926 wxStrcat(wildBuf
, _T("rtf"));
931 wxStrcpy(extensionBuf
, _T("xlp"));
932 wxStrcat(wildBuf
, _T("xlp"));
937 wxStrcpy(extensionBuf
, _T("html"));
938 wxStrcat(wildBuf
, _T("html"));
943 if (force
|| OutputFile
.empty())
945 wxString s
= wxFileSelector(_T("Choose output file"), path
, wxFileNameFromPath(OutputFile
),
946 extensionBuf
, wildBuf
);
952 #endif // wxUSE_FILEDLG
963 if (InputFile
.empty() || OutputFile
.empty() || stopRunning
)
970 wxString str
= wxFileNameFromPath(InputFile
);
972 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
973 frame
->SetTitle(buf
);
976 wxLongLong localTime
= wxGetLocalTimeMillis();
979 // Find extension-less filename
980 wxStrcpy(FileRoot
, OutputFile
.c_str());
981 StripExtension(FileRoot
);
983 if (truncateFilenames
&& convertMode
== TEX_HTML
)
985 // Truncate to five characters. This ensures that
986 // we can generate DOS filenames such as thing999. But 1000 files
987 // may not be enough, of course...
988 wxChar
* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
991 if(wxStrlen( sName
) > 5)
992 sName
[5] = '\0'; // that should do!
995 wxSnprintf(ContentsName
, 300, _T("%s.con"), FileRoot
);
996 wxSnprintf(TmpContentsName
, 300, _T("%s.cn1"), FileRoot
);
997 wxSnprintf(TmpFrameContentsName
, 300, _T("%s.frc"), FileRoot
);
998 wxSnprintf(WinHelpContentsFileName
, 300, _T("%s.cnt"), FileRoot
);
999 wxSnprintf(RefFileName
, 300, _T("%s.ref"), FileRoot
);
1001 TexPathList
.EnsureFileAccessible(InputFile
);
1004 wxString s
= TexPathList
.FindValidPath(_T("bullet.bmp"));
1007 wxString str
= wxFileNameFromPath(s
);
1008 bulletFile
= copystring(str
);
1012 if (wxFileExists(RefFileName
))
1013 ReadTexReferences(RefFileName
);
1015 bool success
= false;
1017 if (!InputFile
.empty() && !OutputFile
.empty())
1019 if (!wxFileExists(InputFile
))
1021 OnError(_T("Cannot open input file!"));
1025 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1029 buf
.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber
);
1030 frame
->SetStatusText(buf
);
1034 OnInform(_T("Reading LaTeX file..."));
1035 TexLoadFile(InputFile
);
1043 switch (convertMode
)
1064 OnInform(_T("*** Aborted by user."));
1066 stopRunning
= false;
1072 WriteTexReferences(RefFileName
);
1074 startedSections
= false;
1078 wxLongLong elapsed
= wxGetLocalTimeMillis() - localTime
;
1079 buf
.Printf(_T("Finished PASS #%d in %ld seconds.\n"), passNumber
, (long)(elapsed
.GetLo()/1000.0));
1084 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1091 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1092 frame
->SetStatusText(buf
);
1094 #endif // wxUSE_STATUSBAR
1096 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1100 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1101 OnInform(buf
.c_str());
1111 startedSections
= false;
1113 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1114 frame
->SetStatusText(_T("Aborted by user."));
1117 OnInform(_T("Sorry, unsuccessful."));
1122 void OnError(const wxString
& msg
)
1127 wxSTD cerr
<< "Error: " << msg
.mb_str() << "\n";
1130 if (isInteractive
&& frame
)
1132 (*frame
->textWindow
) << _T("Error: ") << msg
<< _T("\n");
1136 #if defined(__UNIX__)
1137 wxSTD cerr
<< "Error: " << msg
.mb_str() << "\n";
1139 #elif defined(__WXMSW__)
1148 void OnInform(const wxString
& msg
)
1151 wxSTD cout
<< msg
.mb_str() << "\n";
1154 if (isInteractive
&& frame
)
1156 (*frame
->textWindow
) << msg
<< _T("\n");
1160 #if defined(__UNIX__)
1161 wxSTD cout
<< msg
.mb_str() << "\n";
1163 #elif defined(__WXMSW__)
1175 void OnMacro(int macroId
, int no_args
, bool start
)
1177 switch (convertMode
)
1181 RTFOnMacro(macroId
, no_args
, start
);
1186 XLPOnMacro(macroId
, no_args
, start
);
1191 HTMLOnMacro(macroId
, no_args
, start
);
1197 bool OnArgument(int macroId
, int arg_no
, bool start
)
1199 switch (convertMode
)
1203 return RTFOnArgument(macroId
, arg_no
, start
);
1208 return XLPOnArgument(macroId
, arg_no
, start
);
1213 return HTMLOnArgument(macroId
, arg_no
, start
);
1223 #if defined(__WXMSW__) && !defined(NO_GUI)
1229 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
1231 if (topic
== _T("TEX2RTF"))
1234 ipc_buffer
= new wxChar
[1000];
1236 return new Tex2RTFConnection(ipc_buffer
, 4000);
1246 Tex2RTFConnection::Tex2RTFConnection(wxChar
*buf
, int size
):wxDDEConnection(buf
, size
)
1250 bool SplitCommand(wxChar
*data
, wxChar
*firstArg
, wxChar
*secondArg
)
1256 // Find first argument (command name)
1259 if (data
[i
] == ' ' || data
[i
] == 0)
1263 firstArg
[i
] = data
[i
];
1270 // Find second argument
1273 while (data
[i
] != 0)
1275 secondArg
[j
] = data
[i
];
1284 bool Tex2RTFConnection::OnExecute(const wxString
& WXUNUSED(topic
), wxChar
*data
, int WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1286 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1288 wxChar firstArg
[50];
1289 wxChar secondArg
[300];
1290 if (SplitCommand(data
, firstArg
, secondArg
))
1292 bool hasArg
= (wxStrlen(secondArg
) > 0);
1293 if (wxStrcmp(firstArg
, _T("INPUT")) == 0 && hasArg
)
1295 InputFile
= secondArg
;
1299 wxString str
= wxFileNameFromPath(InputFile
);
1300 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
1301 frame
->SetTitle(buf
);
1304 else if (wxStrcmp(firstArg
, _T("OUTPUT")) == 0 && hasArg
)
1306 OutputFile
= secondArg
;
1308 else if (wxStrcmp(firstArg
, _T("GO")) == 0)
1310 wxStrcpy(Tex2RTFLastStatus
, _T("WORKING"));
1312 wxStrcpy(Tex2RTFLastStatus
, _T("CONVERSION ERROR"));
1314 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1316 else if (wxStrcmp(firstArg
, _T("EXIT")) == 0)
1318 if (frame
) frame
->Close();
1320 else if (wxStrcmp(firstArg
, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg
, _T("ICONIZE")) == 0)
1323 frame
->Iconize(true);
1325 else if (wxStrcmp(firstArg
, _T("SHOW")) == 0 || wxStrcmp(firstArg
, _T("RESTORE")) == 0)
1329 frame
->Iconize(false);
1335 // Try for a setting
1336 wxStrcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, false));
1337 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1338 if (frame
&& wxStrcmp(firstArg
, _T("conversionMode")) == 0)
1341 wxStrcpy(buf
, _T("In "));
1343 if (winHelp
&& (convertMode
== TEX_RTF
))
1344 wxStrcat(buf
, _T("WinHelp RTF"));
1345 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1346 wxStrcat(buf
, _T("linear RTF"));
1347 else if (convertMode
== TEX_HTML
) wxStrcat(buf
, _T("HTML"));
1348 else if (convertMode
== TEX_XLP
) wxStrcat(buf
, _T("XLP"));
1349 wxStrcat(buf
, _T(" mode."));
1350 frame
->SetStatusText(buf
, 1);
1358 wxChar
*Tex2RTFConnection::OnRequest(const wxString
& WXUNUSED(topic
), const wxString
& WXUNUSED(item
), int *WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1360 return Tex2RTFLastStatus
;
1367 //void wxObject::Dump(wxSTD ostream& str)
1369 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1370 // str << GetClassInfo()->GetClassName();
1372 // str << "unknown object class";