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 for (i
= n
; i
< argc
;)
191 if (wxStrcmp(argv
[i
], _T("-winhelp")) == 0)
194 convertMode
= TEX_RTF
;
198 else if (wxStrcmp(argv
[i
], _T("-interactive")) == 0)
201 isInteractive
= true;
204 else if (wxStrcmp(argv
[i
], _T("-sync")) == 0) // Don't yield
209 else if (wxStrcmp(argv
[i
], _T("-rtf")) == 0)
212 convertMode
= TEX_RTF
;
214 else if (wxStrcmp(argv
[i
], _T("-html")) == 0)
217 convertMode
= TEX_HTML
;
219 else if (wxStrcmp(argv
[i
], _T("-xlp")) == 0)
222 convertMode
= TEX_XLP
;
224 else if (wxStrcmp(argv
[i
], _T("-twice")) == 0)
229 else if (wxStrcmp(argv
[i
], _T("-macros")) == 0)
234 MacroFile
= copystring(argv
[i
]);
238 else if (wxStrcmp(argv
[i
], _T("-bufsize")) == 0)
243 BufSize
= wxAtoi(argv
[i
]);
247 else if (wxStrcmp(argv
[i
], _T("-charset")) == 0)
254 if (wxStrcmp(s
, _T("ansi")) == 0 || wxStrcmp(s
, _T("pc")) == 0 || wxStrcmp(s
, _T("mac")) == 0 ||
255 wxStrcmp(s
, _T("pca")) == 0)
256 RTFCharset
= copystring(s
);
259 OnError(_T("Incorrect argument for -charset"));
264 else if (wxStrcmp(argv
[i
], _T("-checkcurlybraces")) == 0)
267 checkCurlyBraces
= true;
269 else if (wxStrcmp(argv
[i
], _T("-checkcurleybraces")) == 0)
271 // Support the old, incorrectly spelled version of -checkcurlybraces
272 // so that old scripts which run tex2rtf -checkcurleybraces still work.
274 checkCurlyBraces
= true;
276 else if (wxStrcmp(argv
[i
], _T("-checksyntax")) == 0)
281 else if (wxStrcmp(argv
[i
], _T("-version")) == 0)
294 buf
.Printf(_T("Invalid switch %s.\n"), argv
[i
]);
295 OnError((wxChar
*)buf
.c_str());
306 if (InputFile
.empty() || OutputFile
.empty())
308 wxSTD cout
<< "Tex2RTF: input or output file is missing.\n";
314 if (!InputFile
.empty())
316 TexPathList
.EnsureFileAccessible(InputFile
);
318 if (InputFile
.empty() || OutputFile
.empty())
319 isInteractive
= true;
321 #if defined(__WXMSW__) && !defined(NO_GUI)
323 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
324 TheTex2RTFServer
= new Tex2RTFServer
;
325 TheTex2RTFServer
->Create(_T("TEX2RTF"));
328 TexInitialize(BufSize
);
329 ResetContentsLevels(0);
335 // Create the main frame window
336 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Tex2RTF"), wxDefaultPosition
, wxSize(400, 300));
338 frame
->CreateStatusBar(2);
339 #endif // wxUSE_STATUSBAR
342 // TODO: uncomment this when we have tex2rtf.xpm
343 frame
->SetIcon(wxICON(tex2rtf
));
345 if (!InputFile
.empty())
348 title
.Printf( _T("Tex2RTF [%s]"), wxFileNameFromPath(InputFile
).c_str());
349 frame
->SetTitle(title
);
353 wxMenu
*file_menu
= new wxMenu
;
354 file_menu
->Append(TEX_GO
, _T("&Go"), _T("Run converter"));
355 file_menu
->Append(TEX_SET_INPUT
, _T("Set &Input File"), _T("Set the LaTeX input file"));
356 file_menu
->Append(TEX_SET_OUTPUT
, _T("Set &Output File"), _T("Set the output file"));
357 file_menu
->AppendSeparator();
358 file_menu
->Append(TEX_VIEW_LATEX
, _T("View &LaTeX File"), _T("View the LaTeX input file"));
359 file_menu
->Append(TEX_VIEW_OUTPUT
, _T("View Output &File"), _T("View output file"));
360 file_menu
->Append(TEX_SAVE_FILE
, _T("&Save log file"), _T("Save displayed text into file"));
361 file_menu
->AppendSeparator();
362 file_menu
->Append(TEX_QUIT
, _T("E&xit"), _T("Exit Tex2RTF"));
364 wxMenu
*macro_menu
= new wxMenu
;
366 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, _T("&Load Custom Macros"), _T("Load custom LaTeX macro file"));
367 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, _T("View &Custom Macros"), _T("View custom LaTeX macros"));
369 wxMenu
*mode_menu
= new wxMenu
;
371 mode_menu
->Append(TEX_MODE_RTF
, _T("Output linear &RTF"), _T("Wordprocessor-compatible RTF"));
372 mode_menu
->Append(TEX_MODE_WINHELP
, _T("Output &WinHelp RTF"), _T("WinHelp-compatible RTF"));
373 mode_menu
->Append(TEX_MODE_HTML
, _T("Output &HTML"), _T("HTML World Wide Web hypertext file"));
374 mode_menu
->Append(TEX_MODE_XLP
, _T("Output &XLP"), _T("wxHelp hypertext help file"));
376 wxMenu
*options_menu
= new wxMenu
;
378 options_menu
->Append(TEX_OPTIONS_CURLY_BRACE
, _T("Curly brace matching"), _T("Checks for mismatched curly braces"),true);
379 options_menu
->Append(TEX_OPTIONS_SYNTAX_CHECKING
, _T("Syntax checking"), _T("Syntax checking for common errors"),true);
381 options_menu
->Check(TEX_OPTIONS_CURLY_BRACE
, checkCurlyBraces
);
382 options_menu
->Check(TEX_OPTIONS_SYNTAX_CHECKING
, checkSyntax
);
384 wxMenu
*help_menu
= new wxMenu
;
386 help_menu
->Append(TEX_HELP
, _T("&Help"), _T("Tex2RTF Contents Page"));
387 help_menu
->Append(TEX_ABOUT
, _T("&About Tex2RTF"), _T("About Tex2RTF"));
389 menuBar
= new wxMenuBar
;
390 menuBar
->Append(file_menu
, _T("&File"));
391 menuBar
->Append(macro_menu
, _T("&Macros"));
392 menuBar
->Append(mode_menu
, _T("&Conversion Mode"));
393 menuBar
->Append(options_menu
, _T("&Options"));
394 menuBar
->Append(help_menu
, _T("&Help"));
396 frame
->SetMenuBar(menuBar
);
397 frame
->textWindow
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_READONLY
|wxTE_MULTILINE
);
399 (*frame
->textWindow
) << _T("Welcome to Tex2RTF.\n");
403 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
404 HelpInstance
= new wxCHMHelpController
;
406 HelpInstance
= new wxHelpController
;
408 HelpInstance
->Initialize(_T("tex2rtf"));
412 * Read macro/initialisation file
416 wxString path
= TexPathList
.FindValidPath(MacroFile
);
418 ReadCustomMacros(path
);
421 wxString
inStr(_T("In "));
426 inStr
+= _T("WinHelp RTF");
428 inStr
+= _T("linear RTF");
440 inStr
+= _T("unknown");
443 inStr
+= _T(" mode.");
444 frame
->SetStatusText(inStr
, 1);
445 #endif // wxUSE_STATUSBAR
454 * Read macro/initialisation file
458 wxString path
= TexPathList
.FindValidPath(MacroFile
);
460 ReadCustomMacros(path
);
463 if ( rc
&& runTwice
)
470 OnExit(); // Do cleanup since OnExit won't be called now
479 wxNode
*node
= CustomMacroList
.GetFirst();
482 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
485 node
= CustomMacroList
.GetFirst();
487 MacroDefs
.BeginFind();
488 wxHashTable::Node
* mNode
= MacroDefs
.Next();
491 TexMacroDef
* def
= (TexMacroDef
*) mNode
->GetData();
493 mNode
= MacroDefs
.Next();
497 delete TheTex2RTFServer
;
512 delete currentArgData
;
513 currentArgData
= NULL
;
527 delete TexTmpBibName
;
528 TexTmpBibName
= NULL
;
542 delete TmpContentsName
;
543 TmpContentsName
= NULL
;
545 if (TmpFrameContentsName
)
547 delete TmpFrameContentsName
;
548 TmpFrameContentsName
= NULL
;
550 if (WinHelpContentsFileName
)
552 delete WinHelpContentsFileName
;
553 WinHelpContentsFileName
= NULL
;
577 delete [] BibliographyStyleString
;
578 delete [] DocumentStyleString
;
579 delete [] bitmapMethod
;
580 delete [] backgroundColourString
;
581 delete [] ContentsNameString
;
582 delete [] AbstractNameString
;
583 delete [] GlossaryNameString
;
584 delete [] ReferencesNameString
;
585 delete [] FiguresNameString
;
586 delete [] TablesNameString
;
587 delete [] FigureNameString
;
588 delete [] TableNameString
;
589 delete [] IndexNameString
;
590 delete [] ChapterNameString
;
591 delete [] SectionNameString
;
592 delete [] SubsectionNameString
;
593 delete [] SubsubsectionNameString
;
594 delete [] UpNameString
;
596 delete[] winHelpTitle
;
598 // TODO: this simulates zero-memory leaks!
599 // Otherwise there are just too many...
601 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
602 wxDebugContext::SetCheckpoint();
610 void ShowVersion(void)
613 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF version %.2f"), versionNo
);
617 void ShowOptions(void)
620 OnInform(_T("Usage: tex2rtf [input] [output] [switches]\n"));
621 OnInform(_T("where valid switches are"));
623 OnInform(_T(" -interactive"));
625 OnInform(_T(" -bufsize <size in K>"));
626 OnInform(_T(" -charset <pc | pca | ansi | mac> (default ansi)"));
627 OnInform(_T(" -twice"));
628 OnInform(_T(" -sync"));
629 OnInform(_T(" -checkcurlybraces"));
630 OnInform(_T(" -checksyntax"));
631 OnInform(_T(" -version"));
632 OnInform(_T(" -macros <filename>"));
633 OnInform(_T(" -winhelp"));
634 OnInform(_T(" -rtf"));
635 OnInform(_T(" -html"));
636 OnInform(_T(" -xlp\n"));
641 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
642 EVT_CLOSE(MyFrame::OnCloseWindow
)
643 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
644 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
645 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
646 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
647 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
648 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
649 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
650 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
651 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
652 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
653 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
654 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
655 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
656 EVT_MENU(TEX_OPTIONS_CURLY_BRACE
, MyFrame::OnOptionsCurlyBrace
)
657 EVT_MENU(TEX_OPTIONS_SYNTAX_CHECKING
, MyFrame::OnOptionsSyntaxChecking
)
658 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
659 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
662 // My frame constructor
663 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
664 wxFrame(frame
, id
, title
, pos
, size
)
667 void MyFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
669 if (!stopRunning
&& !OkToClose
)
681 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
687 void MyFrame::OnGo(wxCommandEvent
& WXUNUSED(event
))
691 menuBar
->EnableTop(0, false);
692 menuBar
->EnableTop(1, false);
693 menuBar
->EnableTop(2, false);
694 menuBar
->EnableTop(3, false);
702 SetStatusText(_T("Build aborted!"));
703 #endif // wxUSE_STATUSBAR
705 errBuf
.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount
);
706 OnInform((wxChar
*)errBuf
.c_str());
710 if (runTwice
&& !stopRunning
)
715 menuBar
->EnableTop(0, true);
716 menuBar
->EnableTop(1, true);
717 menuBar
->EnableTop(2, true);
718 menuBar
->EnableTop(3, true);
721 void MyFrame::OnSetInput(wxCommandEvent
& WXUNUSED(event
))
723 ChooseInputFile(true);
726 void MyFrame::OnSetOutput(wxCommandEvent
& WXUNUSED(event
))
728 ChooseOutputFile(true);
731 void MyFrame::OnSaveFile(wxCommandEvent
& WXUNUSED(event
))
734 wxString s
= wxFileSelector(_T("Save text to file"), wxEmptyString
, wxEmptyString
, _T("txt"), _T("*.txt"));
737 textWindow
->SaveFile(s
);
740 wxSnprintf(buf
, sizeof(buf
), _T("Saved text to %s"), (const wxChar
*) s
.c_str());
741 frame
->SetStatusText(buf
, 0);
742 #endif // wxUSE_STATUSBAR
744 #endif // wxUSE_FILEDLG
747 void MyFrame::OnViewOutput(wxCommandEvent
& WXUNUSED(event
))
750 if (!OutputFile
.empty() && wxFileExists(OutputFile
))
752 textWindow
->LoadFile(OutputFile
);
754 wxString
str(wxFileNameFromPath(OutputFile
));
755 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
756 frame
->SetTitle(buf
);
760 void MyFrame::OnViewLatex(wxCommandEvent
& WXUNUSED(event
))
763 if (!InputFile
.empty() && wxFileExists(InputFile
))
765 textWindow
->LoadFile(InputFile
);
767 wxString
str(wxFileNameFromPath(OutputFile
));
768 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
.c_str());
769 frame
->SetTitle(buf
);
773 void MyFrame::OnLoadMacros(wxCommandEvent
& WXUNUSED(event
))
777 wxString s
= wxFileSelector(_T("Choose custom macro file"), wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), _T("ini"), _T("*.ini"));
778 if (!s
.empty() && wxFileExists(s
))
780 MacroFile
= copystring(s
);
784 #endif // wxUSE_FILEDLG
787 void MyFrame::OnShowMacros(wxCommandEvent
& WXUNUSED(event
))
794 void MyFrame::OnModeRTF(wxCommandEvent
& WXUNUSED(event
))
796 convertMode
= TEX_RTF
;
798 InputFile
= wxEmptyString
;
799 OutputFile
= wxEmptyString
;
801 SetStatusText(_T("In linear RTF mode."), 1);
802 #endif // wxUSE_STATUSBAR
805 void MyFrame::OnModeWinHelp(wxCommandEvent
& WXUNUSED(event
))
807 convertMode
= TEX_RTF
;
809 InputFile
= wxEmptyString
;
810 OutputFile
= wxEmptyString
;
812 SetStatusText(_T("In WinHelp RTF mode."), 1);
813 #endif // wxUSE_STATUSBAR
816 void MyFrame::OnModeHTML(wxCommandEvent
& WXUNUSED(event
))
818 convertMode
= TEX_HTML
;
820 InputFile
= wxEmptyString
;
821 OutputFile
= wxEmptyString
;
823 SetStatusText(_T("In HTML mode."), 1);
824 #endif // wxUSE_STATUSBAR
827 void MyFrame::OnModeXLP(wxCommandEvent
& WXUNUSED(event
))
829 convertMode
= TEX_XLP
;
830 InputFile
= wxEmptyString
;
831 OutputFile
= wxEmptyString
;
833 SetStatusText(_T("In XLP mode."), 1);
834 #endif // wxUSE_STATUSBAR
837 void MyFrame::OnOptionsCurlyBrace(wxCommandEvent
& WXUNUSED(event
))
839 checkCurlyBraces
= !checkCurlyBraces
;
841 if (checkCurlyBraces
)
843 SetStatusText(_T("Checking curly braces: YES"), 1);
847 SetStatusText(_T("Checking curly braces: NO"), 1);
849 #endif // wxUSE_STATUSBAR
853 void MyFrame::OnOptionsSyntaxChecking(wxCommandEvent
& WXUNUSED(event
))
855 checkSyntax
= !checkSyntax
;
859 SetStatusText(_T("Checking syntax: YES"), 1);
863 SetStatusText(_T("Checking syntax: NO"), 1);
865 #endif // wxUSE_STATUSBAR
869 void MyFrame::OnHelp(wxCommandEvent
& WXUNUSED(event
))
872 HelpInstance
->LoadFile();
873 HelpInstance
->DisplayContents();
877 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
880 wxString platform
= wxGetOsDescription();
881 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());
882 wxMessageBox(buf
, _T("About Tex2RTF"));
885 void ChooseInputFile(bool force
)
888 if (force
|| InputFile
.empty())
890 wxString s
= wxFileSelector(_T("Choose LaTeX input file"), wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), _T("tex"), _T("*.tex"));
893 // Different file, so clear index entries.
895 ResetContentsLevels(0);
900 wxString str
= wxFileNameFromPath(InputFile
);
902 buf
.Printf(_T("Tex2RTF [%s]"), str
.c_str());
903 frame
->SetTitle((wxChar
*)buf
.c_str());
904 OutputFile
= wxEmptyString
;
909 #endif // wxUSE_FILEDLG
912 void ChooseOutputFile(bool force
)
914 wxChar extensionBuf
[10];
916 wxStrcpy(wildBuf
, _T("*."));
918 if (!OutputFile
.empty())
919 path
= wxPathOnly(OutputFile
);
920 else if (!InputFile
.empty())
921 path
= wxPathOnly(InputFile
);
927 wxStrcpy(extensionBuf
, _T("rtf"));
928 wxStrcat(wildBuf
, _T("rtf"));
933 wxStrcpy(extensionBuf
, _T("xlp"));
934 wxStrcat(wildBuf
, _T("xlp"));
939 wxStrcpy(extensionBuf
, _T("html"));
940 wxStrcat(wildBuf
, _T("html"));
945 if (force
|| OutputFile
.empty())
947 wxString s
= wxFileSelector(_T("Choose output file"), path
, wxFileNameFromPath(OutputFile
),
948 extensionBuf
, wildBuf
);
954 #endif // wxUSE_FILEDLG
965 if (InputFile
.empty() || OutputFile
.empty() || stopRunning
)
972 wxString str
= wxFileNameFromPath(InputFile
);
974 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
975 frame
->SetTitle(buf
);
978 wxLongLong localTime
= wxGetLocalTimeMillis();
981 // Find extension-less filename
982 wxStrcpy(FileRoot
, OutputFile
.c_str());
983 StripExtension(FileRoot
);
985 if (truncateFilenames
&& convertMode
== TEX_HTML
)
987 // Truncate to five characters. This ensures that
988 // we can generate DOS filenames such as thing999. But 1000 files
989 // may not be enough, of course...
990 wxChar
* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
993 if(wxStrlen( sName
) > 5)
994 sName
[5] = '\0'; // that should do!
997 wxSnprintf(ContentsName
, 300, _T("%s.con"), FileRoot
);
998 wxSnprintf(TmpContentsName
, 300, _T("%s.cn1"), FileRoot
);
999 wxSnprintf(TmpFrameContentsName
, 300, _T("%s.frc"), FileRoot
);
1000 wxSnprintf(WinHelpContentsFileName
, 300, _T("%s.cnt"), FileRoot
);
1001 wxSnprintf(RefFileName
, 300, _T("%s.ref"), FileRoot
);
1003 TexPathList
.EnsureFileAccessible(InputFile
);
1006 wxString s
= TexPathList
.FindValidPath(_T("bullet.bmp"));
1009 wxString str
= wxFileNameFromPath(s
);
1010 bulletFile
= copystring(str
);
1014 if (wxFileExists(RefFileName
))
1015 ReadTexReferences(RefFileName
);
1017 bool success
= false;
1019 if (!InputFile
.empty() && !OutputFile
.empty())
1021 if (!wxFileExists(InputFile
))
1023 OnError(_T("Cannot open input file!"));
1027 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1031 buf
.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber
);
1032 frame
->SetStatusText((wxChar
*)buf
.c_str());
1036 OnInform(_T("Reading LaTeX file..."));
1037 TexLoadFile(InputFile
);
1045 switch (convertMode
)
1066 OnInform(_T("*** Aborted by user."));
1068 stopRunning
= false;
1074 WriteTexReferences(RefFileName
);
1076 startedSections
= false;
1080 wxLongLong elapsed
= wxGetLocalTimeMillis() - localTime
;
1081 buf
.Printf(_T("Finished PASS #%d in %ld seconds.\n"), passNumber
, (long)(elapsed
.GetLo()/1000.0));
1082 OnInform((wxChar
*)buf
.c_str());
1086 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1087 OnInform((wxChar
*)buf
.c_str());
1093 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1094 frame
->SetStatusText((wxChar
*)buf
.c_str());
1096 #endif // wxUSE_STATUSBAR
1098 buf
.Printf(_T("Done, %d %s."), passNumber
, (passNumber
> 1) ? _T("passes") : _T("pass"));
1099 OnInform((wxChar
*)buf
.c_str());
1102 buf
.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount
);
1103 OnInform((wxChar
*)buf
.c_str());
1113 startedSections
= false;
1115 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1116 frame
->SetStatusText(_T("Aborted by user."));
1119 OnInform(_T("Sorry, unsuccessful."));
1124 void OnError(const wxChar
*msg
)
1126 wxString msg_string
= msg
;
1130 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1133 if (isInteractive
&& frame
)
1135 (*frame
->textWindow
) << _T("Error: ") << msg
<< _T("\n");
1139 #if defined(__UNIX__)
1140 wxSTD cerr
<< "Error: " << msg_string
.mb_str() << "\n";
1142 #elif defined(__WXMSW__)
1151 void OnInform(const wxChar
*msg
)
1153 wxString msg_string
= msg
;
1155 wxSTD cout
<< msg_string
.mb_str() << "\n";
1158 if (isInteractive
&& frame
)
1160 (*frame
->textWindow
) << msg
<< _T("\n");
1164 #if defined(__UNIX__)
1165 wxSTD cout
<< msg_string
.mb_str() << "\n";
1167 #elif defined(__WXMSW__)
1179 void OnMacro(int macroId
, int no_args
, bool start
)
1181 switch (convertMode
)
1185 RTFOnMacro(macroId
, no_args
, start
);
1190 XLPOnMacro(macroId
, no_args
, start
);
1195 HTMLOnMacro(macroId
, no_args
, start
);
1201 bool OnArgument(int macroId
, int arg_no
, bool start
)
1203 switch (convertMode
)
1207 return RTFOnArgument(macroId
, arg_no
, start
);
1212 return XLPOnArgument(macroId
, arg_no
, start
);
1217 return HTMLOnArgument(macroId
, arg_no
, start
);
1227 #if defined(__WXMSW__) && !defined(NO_GUI)
1233 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
1235 if (topic
== _T("TEX2RTF"))
1238 ipc_buffer
= new wxChar
[1000];
1240 return new Tex2RTFConnection(ipc_buffer
, 4000);
1250 Tex2RTFConnection::Tex2RTFConnection(wxChar
*buf
, int size
):wxDDEConnection(buf
, size
)
1254 bool SplitCommand(wxChar
*data
, wxChar
*firstArg
, wxChar
*secondArg
)
1260 // Find first argument (command name)
1263 if (data
[i
] == ' ' || data
[i
] == 0)
1267 firstArg
[i
] = data
[i
];
1274 // Find second argument
1277 while (data
[i
] != 0)
1279 secondArg
[j
] = data
[i
];
1288 bool Tex2RTFConnection::OnExecute(const wxString
& WXUNUSED(topic
), wxChar
*data
, int WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1290 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1292 wxChar firstArg
[50];
1293 wxChar secondArg
[300];
1294 if (SplitCommand(data
, firstArg
, secondArg
))
1296 bool hasArg
= (wxStrlen(secondArg
) > 0);
1297 if (wxStrcmp(firstArg
, _T("INPUT")) == 0 && hasArg
)
1299 InputFile
= secondArg
;
1303 wxString str
= wxFileNameFromPath(InputFile
);
1304 wxSnprintf(buf
, sizeof(buf
), _T("Tex2RTF [%s]"), (const wxChar
*) str
);
1305 frame
->SetTitle(buf
);
1308 else if (wxStrcmp(firstArg
, _T("OUTPUT")) == 0 && hasArg
)
1310 OutputFile
= secondArg
;
1312 else if (wxStrcmp(firstArg
, _T("GO")) == 0)
1314 wxStrcpy(Tex2RTFLastStatus
, _T("WORKING"));
1316 wxStrcpy(Tex2RTFLastStatus
, _T("CONVERSION ERROR"));
1318 wxStrcpy(Tex2RTFLastStatus
, _T("OK"));
1320 else if (wxStrcmp(firstArg
, _T("EXIT")) == 0)
1322 if (frame
) frame
->Close();
1324 else if (wxStrcmp(firstArg
, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg
, _T("ICONIZE")) == 0)
1327 frame
->Iconize(true);
1329 else if (wxStrcmp(firstArg
, _T("SHOW")) == 0 || wxStrcmp(firstArg
, _T("RESTORE")) == 0)
1333 frame
->Iconize(false);
1339 // Try for a setting
1340 wxStrcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, false));
1341 #if !defined(NO_GUI) && wxUSE_STATUSBAR
1342 if (frame
&& wxStrcmp(firstArg
, _T("conversionMode")) == 0)
1345 wxStrcpy(buf
, _T("In "));
1347 if (winHelp
&& (convertMode
== TEX_RTF
))
1348 wxStrcat(buf
, _T("WinHelp RTF"));
1349 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1350 wxStrcat(buf
, _T("linear RTF"));
1351 else if (convertMode
== TEX_HTML
) wxStrcat(buf
, _T("HTML"));
1352 else if (convertMode
== TEX_XLP
) wxStrcat(buf
, _T("XLP"));
1353 wxStrcat(buf
, _T(" mode."));
1354 frame
->SetStatusText(buf
, 1);
1362 wxChar
*Tex2RTFConnection::OnRequest(const wxString
& WXUNUSED(topic
), const wxString
& WXUNUSED(item
), int *WXUNUSED(size
), wxIPCFormat
WXUNUSED(format
))
1364 return Tex2RTFLastStatus
;
1371 //void wxObject::Dump(wxSTD ostream& str)
1373 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1374 // str << GetClassInfo()->GetClassName();
1376 // str << "unknown object class";