]>
Commit | Line | Data |
---|---|---|
9a29912f JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: tex2rtf.cpp | |
3 | // Purpose: Converts Latex to linear/WinHelp RTF, HTML, wxHelp. | |
4 | // Author: Julian Smart | |
b63b07a8 RL |
5 | // Modified by: Wlodzimiez ABX Skiba 2003/2004 Unicode support |
6 | // Ron Lee | |
9a29912f JS |
7 | // Created: 7.9.93 |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) Julian Smart | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifdef __GNUG__ | |
14 | #pragma implementation | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
c5565f8c WS |
24 | #if defined(__WXMSW__) |
25 | #include "wx/msw/wrapwin.h" | |
26 | #endif | |
27 | ||
9a29912f | 28 | #ifndef WX_PRECOMP |
ea172e69 MB |
29 | #ifndef NO_GUI |
30 | #include "wx/menu.h" | |
31 | #include "wx/textctrl.h" | |
32 | #include "wx/filedlg.h" | |
33 | #include "wx/msgdlg.h" | |
516a5722 | 34 | #include "wx/icon.h" |
ea172e69 | 35 | #endif |
9a29912f JS |
36 | #endif |
37 | ||
38 | #ifndef NO_GUI | |
e5486aff WS |
39 | #include "wx/timer.h" |
40 | #include "wx/help.h" | |
41 | #include "wx/cshelp.h" | |
42 | #include "wx/helphtml.h" | |
43 | #ifdef __WXMSW__ | |
44 | #include "wx/msw/helpchm.h" | |
45 | #else | |
46 | #include "wx/html/helpctrl.h" | |
47 | #endif | |
48 | #endif // !NO_GUI | |
9a29912f | 49 | |
9a29912f JS |
50 | #if wxUSE_IOSTREAMH |
51 | #include <iostream.h> | |
52 | #include <fstream.h> | |
53 | #else | |
54 | #include <iostream> | |
55 | #include <fstream> | |
56 | #endif | |
9a29912f JS |
57 | |
58 | #include <ctype.h> | |
59 | #include <stdlib.h> | |
60 | #include "tex2any.h" | |
61 | #include "tex2rtf.h" | |
62 | #include "rtfutils.h" | |
56d34922 | 63 | #include "symbols.h" |
9a29912f | 64 | |
7658b9eb | 65 | #if (defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXX11__)) && !defined(NO_GUI) |
9a29912f JS |
66 | #include "tex2rtf.xpm" |
67 | #endif | |
68 | ||
ea172e69 MB |
69 | #if !WXWIN_COMPATIBILITY_2_4 |
70 | static inline wxChar* copystring(const wxChar* s) | |
71 | { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); } | |
72 | #endif | |
73 | ||
b3ced905 | 74 | const float versionNo = float(TEX2RTF_VERSION_NUMBER); |
9a29912f JS |
75 | |
76 | TexChunk *currentMember = NULL; | |
b63b07a8 | 77 | bool startedSections = false; |
6c155d33 | 78 | wxChar *contentsString = NULL; |
b63b07a8 RL |
79 | bool suppressNameDecoration = false; |
80 | bool OkToClose = true; | |
9a29912f | 81 | int passNumber = 1; |
6a205442 | 82 | unsigned long errorCount = 0; |
9a29912f JS |
83 | |
84 | #ifndef NO_GUI | |
ffaaaacb | 85 | |
6c155d33 JS |
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 | |
71599f0a GT |
90 | extern wxList ColourTable; |
91 | extern TexChunk *TopLevel; | |
3924dd22 | 92 | |
ffaaaacb | 93 | #if wxUSE_HELP |
e5486aff | 94 | wxHelpControllerBase *HelpInstance = NULL; |
ffaaaacb | 95 | #endif // wxUSE_HELP |
9a29912f JS |
96 | |
97 | #ifdef __WXMSW__ | |
6c155d33 JS |
98 | static wxChar *ipc_buffer = NULL; |
99 | static wxChar Tex2RTFLastStatus[100]; | |
9a29912f | 100 | Tex2RTFServer *TheTex2RTFServer = NULL; |
e5486aff WS |
101 | #endif // __WXMSW__ |
102 | ||
103 | #endif // !NO_GUI | |
9a29912f | 104 | |
6c155d33 | 105 | wxChar *bulletFile = NULL; |
9a29912f JS |
106 | |
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; | |
112 | FILE *Popups = NULL; | |
113 | FILE *WinHelpContentsFile = NULL; | |
114 | ||
6c155d33 JS |
115 | wxChar *InputFile = NULL; |
116 | wxChar *OutputFile = NULL; | |
117 | wxChar *MacroFile = copystring(_T("tex2rtf.ini")); | |
9a29912f | 118 | |
6c155d33 JS |
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 | |
9a29912f | 125 | |
6c155d33 | 126 | wxChar *RTFCharset = copystring(_T("ansi")); |
9a29912f JS |
127 | |
128 | #ifdef __WXMSW__ | |
129 | int BufSize = 100; // Size of buffer in K | |
130 | #else | |
131 | int BufSize = 500; | |
132 | #endif | |
133 | ||
134 | bool Go(void); | |
135 | void ShowOptions(void); | |
136 | ||
6c155d33 | 137 | wxChar wxTex2RTFBuffer[1500]; |
c4c794e1 | 138 | |
aed3314d | 139 | #ifdef NO_GUI |
b63b07a8 | 140 | IMPLEMENT_APP_CONSOLE(MyApp) |
9a29912f | 141 | #else |
b63b07a8 RL |
142 | wxMenuBar *menuBar = NULL; |
143 | MyFrame *frame = NULL; | |
144 | // DECLARE_APP(MyApp) | |
145 | IMPLEMENT_APP(MyApp) | |
146 | #endif | |
9a29912f JS |
147 | |
148 | // `Main program' equivalent, creating windows and returning main app frame | |
149 | bool MyApp::OnInit() | |
9a29912f JS |
150 | { |
151 | // Use default list of macros defined in tex2any.cc | |
152 | DefineDefaultMacros(); | |
6c155d33 | 153 | AddMacroDef(ltHARDY, _T("hardy"), 0); |
9a29912f | 154 | |
6c155d33 JS |
155 | FileRoot = new wxChar[300]; |
156 | ContentsName = new wxChar[300]; | |
157 | TmpContentsName = new wxChar[300]; | |
158 | TmpFrameContentsName = new wxChar[300]; | |
159 | WinHelpContentsFileName = new wxChar[300]; | |
160 | RefFileName = new wxChar[300]; | |
9a29912f | 161 | |
22b636aa | 162 | WX_CLEAR_LIST(wxList,ColourTable); |
71599f0a | 163 | |
9a29912f | 164 | int n = 1; |
ffaaaacb | 165 | |
9a29912f JS |
166 | // Read input/output files |
167 | if (argc > 1) | |
168 | { | |
169 | if (argv[1][0] != '-') | |
170 | { | |
171 | InputFile = argv[1]; | |
172 | n ++; | |
173 | ||
174 | if (argc > 2) | |
175 | { | |
176 | if (argv[2][0] != '-') | |
177 | { | |
178 | OutputFile = argv[2]; | |
179 | n ++; | |
180 | } | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | #ifdef NO_GUI | |
186 | if (!InputFile || !OutputFile) | |
187 | { | |
219f4a6d | 188 | wxSTD cout << "Tex2RTF: input or output file is missing.\n"; |
9a29912f JS |
189 | ShowOptions(); |
190 | exit(1); | |
191 | } | |
192 | #endif | |
b63b07a8 | 193 | |
9a29912f JS |
194 | if (InputFile) |
195 | { | |
196 | TexPathList.EnsureFileAccessible(InputFile); | |
197 | } | |
198 | if (!InputFile || !OutputFile) | |
b63b07a8 | 199 | isInteractive = true; |
9a29912f | 200 | |
f6bcfd97 BP |
201 | int i; |
202 | for (i = n; i < argc;) | |
9a29912f | 203 | { |
6c155d33 | 204 | if (wxStrcmp(argv[i], _T("-winhelp")) == 0) |
9a29912f JS |
205 | { |
206 | i ++; | |
207 | convertMode = TEX_RTF; | |
b63b07a8 | 208 | winHelp = true; |
9a29912f JS |
209 | } |
210 | #ifndef NO_GUI | |
6c155d33 | 211 | else if (wxStrcmp(argv[i], _T("-interactive")) == 0) |
9a29912f JS |
212 | { |
213 | i ++; | |
b63b07a8 | 214 | isInteractive = true; |
9a29912f JS |
215 | } |
216 | #endif | |
6c155d33 | 217 | else if (wxStrcmp(argv[i], _T("-sync")) == 0) // Don't yield |
9a29912f JS |
218 | { |
219 | i ++; | |
b63b07a8 | 220 | isSync = true; |
9a29912f | 221 | } |
6c155d33 | 222 | else if (wxStrcmp(argv[i], _T("-rtf")) == 0) |
9a29912f JS |
223 | { |
224 | i ++; | |
225 | convertMode = TEX_RTF; | |
226 | } | |
6c155d33 | 227 | else if (wxStrcmp(argv[i], _T("-html")) == 0) |
9a29912f JS |
228 | { |
229 | i ++; | |
230 | convertMode = TEX_HTML; | |
231 | } | |
6c155d33 | 232 | else if (wxStrcmp(argv[i], _T("-xlp")) == 0) |
9a29912f JS |
233 | { |
234 | i ++; | |
235 | convertMode = TEX_XLP; | |
236 | } | |
6c155d33 | 237 | else if (wxStrcmp(argv[i], _T("-twice")) == 0) |
9a29912f JS |
238 | { |
239 | i ++; | |
b63b07a8 | 240 | runTwice = true; |
9a29912f | 241 | } |
6c155d33 | 242 | else if (wxStrcmp(argv[i], _T("-macros")) == 0) |
9a29912f JS |
243 | { |
244 | i ++; | |
245 | if (i < argc) | |
246 | { | |
247 | MacroFile = copystring(argv[i]); | |
248 | i ++; | |
249 | } | |
250 | } | |
6c155d33 | 251 | else if (wxStrcmp(argv[i], _T("-bufsize")) == 0) |
9a29912f JS |
252 | { |
253 | i ++; | |
254 | if (i < argc) | |
255 | { | |
6c155d33 | 256 | BufSize = wxAtoi(argv[i]); |
9a29912f JS |
257 | i ++; |
258 | } | |
259 | } | |
6c155d33 | 260 | else if (wxStrcmp(argv[i], _T("-charset")) == 0) |
9a29912f JS |
261 | { |
262 | i ++; | |
263 | if (i < argc) | |
264 | { | |
6c155d33 | 265 | wxChar *s = argv[i]; |
9a29912f | 266 | i ++; |
6c155d33 JS |
267 | if (wxStrcmp(s, _T("ansi")) == 0 || wxStrcmp(s, _T("pc")) == 0 || wxStrcmp(s, _T("mac")) == 0 || |
268 | wxStrcmp(s, _T("pca")) == 0) | |
9a29912f JS |
269 | RTFCharset = copystring(s); |
270 | else | |
271 | { | |
6c155d33 | 272 | OnError(_T("Incorrect argument for -charset")); |
b63b07a8 | 273 | return false; |
9a29912f JS |
274 | } |
275 | } | |
276 | } | |
88fd7006 VS |
277 | else if (wxStrcmp(argv[i], _T("-checkcurlybraces")) == 0) |
278 | { | |
279 | i ++; | |
280 | checkCurlyBraces = true; | |
281 | } | |
6c155d33 | 282 | else if (wxStrcmp(argv[i], _T("-checkcurleybraces")) == 0) |
fad535ee | 283 | { |
88fd7006 VS |
284 | // Support the old, incorrectly spelled version of -checkcurlybraces |
285 | // so that old scripts which run tex2rtf -checkcurleybraces still work. | |
fad535ee | 286 | i ++; |
88fd7006 | 287 | checkCurlyBraces = true; |
fad535ee | 288 | } |
6c155d33 | 289 | else if (wxStrcmp(argv[i], _T("-checksyntax")) == 0) |
fad535ee GT |
290 | { |
291 | i ++; | |
b63b07a8 | 292 | checkSyntax = true; |
fad535ee | 293 | } |
9a29912f JS |
294 | else |
295 | { | |
63ad540d | 296 | wxString buf; |
6c155d33 JS |
297 | buf.Printf(_T("Invalid switch %s.\n"), argv[i]); |
298 | OnError((wxChar *)buf.c_str()); | |
9a29912f JS |
299 | #ifdef NO_GUI |
300 | ShowOptions(); | |
301 | exit(1); | |
5b7ab938 | 302 | #else |
b63b07a8 | 303 | return false; |
5b7ab938 | 304 | #endif |
9a29912f JS |
305 | } |
306 | } | |
307 | ||
308 | #if defined(__WXMSW__) && !defined(NO_GUI) | |
309 | wxDDEInitialize(); | |
310 | Tex2RTFLastStatus[0] = 0; // DDE connection return value | |
311 | TheTex2RTFServer = new Tex2RTFServer; | |
6c155d33 | 312 | TheTex2RTFServer->Create(_T("TEX2RTF")); |
9a29912f JS |
313 | #endif |
314 | ||
9a29912f JS |
315 | TexInitialize(BufSize); |
316 | ResetContentsLevels(0); | |
317 | ||
318 | #ifndef NO_GUI | |
319 | ||
320 | if (isInteractive) | |
321 | { | |
6c155d33 | 322 | wxChar buf[100]; |
9a29912f JS |
323 | |
324 | // Create the main frame window | |
b63b07a8 | 325 | frame = new MyFrame(NULL, wxID_ANY, _T("Tex2RTF"), wxDefaultPosition, wxSize(400, 300)); |
67a99992 | 326 | #if wxUSE_STATUSBAR |
9a29912f | 327 | frame->CreateStatusBar(2); |
67a99992 | 328 | #endif // wxUSE_STATUSBAR |
9a29912f JS |
329 | |
330 | // Give it an icon | |
331 | // TODO: uncomment this when we have tex2rtf.xpm | |
332 | frame->SetIcon(wxICON(tex2rtf)); | |
333 | ||
334 | if (InputFile) | |
335 | { | |
b63b07a8 | 336 | wxSnprintf(buf, sizeof(buf), _T("Tex2RTF [%s]"), wxFileNameFromPath(InputFile)); |
9a29912f JS |
337 | frame->SetTitle(buf); |
338 | } | |
339 | ||
340 | // Make a menubar | |
341 | wxMenu *file_menu = new wxMenu; | |
6c155d33 JS |
342 | file_menu->Append(TEX_GO, _T("&Go"), _T("Run converter")); |
343 | file_menu->Append(TEX_SET_INPUT, _T("Set &Input File"), _T("Set the LaTeX input file")); | |
344 | file_menu->Append(TEX_SET_OUTPUT, _T("Set &Output File"), _T("Set the output file")); | |
9a29912f | 345 | file_menu->AppendSeparator(); |
6c155d33 JS |
346 | file_menu->Append(TEX_VIEW_LATEX, _T("View &LaTeX File"), _T("View the LaTeX input file")); |
347 | file_menu->Append(TEX_VIEW_OUTPUT, _T("View Output &File"), _T("View output file")); | |
348 | file_menu->Append(TEX_SAVE_FILE, _T("&Save log file"), _T("Save displayed text into file")); | |
9a29912f | 349 | file_menu->AppendSeparator(); |
6c155d33 | 350 | file_menu->Append(TEX_QUIT, _T("E&xit"), _T("Exit Tex2RTF")); |
9a29912f JS |
351 | |
352 | wxMenu *macro_menu = new wxMenu; | |
353 | ||
6c155d33 JS |
354 | macro_menu->Append(TEX_LOAD_CUSTOM_MACROS, _T("&Load Custom Macros"), _T("Load custom LaTeX macro file")); |
355 | macro_menu->Append(TEX_VIEW_CUSTOM_MACROS, _T("View &Custom Macros"), _T("View custom LaTeX macros")); | |
9a29912f JS |
356 | |
357 | wxMenu *mode_menu = new wxMenu; | |
358 | ||
6c155d33 JS |
359 | mode_menu->Append(TEX_MODE_RTF, _T("Output linear &RTF"), _T("Wordprocessor-compatible RTF")); |
360 | mode_menu->Append(TEX_MODE_WINHELP, _T("Output &WinHelp RTF"), _T("WinHelp-compatible RTF")); | |
361 | mode_menu->Append(TEX_MODE_HTML, _T("Output &HTML"), _T("HTML World Wide Web hypertext file")); | |
362 | mode_menu->Append(TEX_MODE_XLP, _T("Output &XLP"), _T("wxHelp hypertext help file")); | |
9a29912f | 363 | |
fad535ee GT |
364 | wxMenu *options_menu = new wxMenu; |
365 | ||
88fd7006 | 366 | options_menu->Append(TEX_OPTIONS_CURLY_BRACE, _T("Curly brace matching"), _T("Checks for mismatched curly braces"),true); |
b63b07a8 | 367 | options_menu->Append(TEX_OPTIONS_SYNTAX_CHECKING, _T("Syntax checking"), _T("Syntax checking for common errors"),true); |
fad535ee | 368 | |
88fd7006 | 369 | options_menu->Check(TEX_OPTIONS_CURLY_BRACE, checkCurlyBraces); |
fad535ee GT |
370 | options_menu->Check(TEX_OPTIONS_SYNTAX_CHECKING, checkSyntax); |
371 | ||
9a29912f JS |
372 | wxMenu *help_menu = new wxMenu; |
373 | ||
6c155d33 JS |
374 | help_menu->Append(TEX_HELP, _T("&Help"), _T("Tex2RTF Contents Page")); |
375 | help_menu->Append(TEX_ABOUT, _T("&About Tex2RTF"), _T("About Tex2RTF")); | |
9a29912f JS |
376 | |
377 | menuBar = new wxMenuBar; | |
6c155d33 JS |
378 | menuBar->Append(file_menu, _T("&File")); |
379 | menuBar->Append(macro_menu, _T("&Macros")); | |
380 | menuBar->Append(mode_menu, _T("&Conversion Mode")); | |
381 | menuBar->Append(options_menu, _T("&Options")); | |
382 | menuBar->Append(help_menu, _T("&Help")); | |
9a29912f JS |
383 | |
384 | frame->SetMenuBar(menuBar); | |
b63b07a8 | 385 | frame->textWindow = new wxTextCtrl(frame, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY|wxTE_MULTILINE); |
9a29912f | 386 | |
6c155d33 | 387 | (*frame->textWindow) << _T("Welcome to Tex2RTF.\n"); |
ffaaaacb | 388 | // ShowOptions(); |
9a29912f | 389 | |
ffaaaacb | 390 | #if wxUSE_HELP |
e5486aff WS |
391 | #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__) |
392 | HelpInstance = new wxCHMHelpController; | |
393 | #else | |
394 | HelpInstance = new wxHtmlHelpController; | |
395 | #endif | |
6c155d33 | 396 | HelpInstance->Initialize(_T("tex2rtf")); |
ffaaaacb | 397 | #endif // wxUSE_HELP |
9a29912f JS |
398 | |
399 | /* | |
400 | * Read macro/initialisation file | |
401 | * | |
402 | */ | |
ffaaaacb | 403 | |
6c155d33 | 404 | wxString path = TexPathList.FindValidPath(MacroFile); |
489f6cf7 | 405 | if (!path.empty()) |
6c155d33 | 406 | ReadCustomMacros((wxChar *)path.c_str()); |
9a29912f | 407 | |
67a99992 | 408 | #if wxUSE_STATUSBAR |
6c155d33 | 409 | wxStrcpy(buf, _T("In ")); |
67a99992 WS |
410 | switch (convertMode) |
411 | { | |
412 | case TEX_RTF: | |
f8bd8dd5 | 413 | wxStrcat(buf, (winHelp) ? _T("WinHelp RTF") : _T("linear RTF")); |
67a99992 WS |
414 | break; |
415 | case TEX_HTML: | |
416 | wxStrcat(buf, _T("HTML")); | |
417 | break; | |
418 | case TEX_XLP: | |
419 | wxStrcat(buf, _T("XLP")); | |
420 | break; | |
421 | default: | |
422 | wxStrcat(buf, _T("unknown")); | |
423 | break; | |
424 | } | |
425 | wxStrcat(buf, _T(" mode.")); | |
9a29912f | 426 | frame->SetStatusText(buf, 1); |
67a99992 | 427 | #endif // wxUSE_STATUSBAR |
ffaaaacb | 428 | |
b63b07a8 RL |
429 | frame->Show(true); |
430 | return true; | |
9a29912f JS |
431 | } |
432 | else | |
433 | #endif // NO_GUI | |
434 | { | |
435 | /* | |
436 | * Read macro/initialisation file | |
437 | * | |
438 | */ | |
ffaaaacb | 439 | |
6c155d33 | 440 | wxString path = TexPathList.FindValidPath(MacroFile); |
489f6cf7 | 441 | if (!path.empty()) |
6c155d33 | 442 | ReadCustomMacros((wxChar*)path.c_str()); |
9a29912f JS |
443 | |
444 | Go(); | |
254a2129 | 445 | if (runTwice) |
3924dd22 GT |
446 | { |
447 | Go(); | |
448 | } | |
9a29912f | 449 | #ifdef NO_GUI |
b63b07a8 | 450 | return true; |
9a29912f | 451 | #else |
00827094 | 452 | OnExit(); // Do cleanup since OnExit won't be called now |
b63b07a8 | 453 | return false; |
9a29912f JS |
454 | #endif |
455 | } | |
9a29912f JS |
456 | } |
457 | ||
c4c794e1 | 458 | #ifndef NO_GUI |
9a29912f JS |
459 | int MyApp::OnExit() |
460 | { | |
ddc4f3b5 | 461 | wxNode *node = CustomMacroList.GetFirst(); |
9a29912f JS |
462 | while (node) |
463 | { | |
ddc4f3b5 | 464 | CustomMacro *macro = (CustomMacro *)node->GetData(); |
9a29912f JS |
465 | delete macro; |
466 | delete node; | |
ddc4f3b5 | 467 | node = CustomMacroList.GetFirst(); |
9a29912f JS |
468 | } |
469 | MacroDefs.BeginFind(); | |
f6fe5318 MB |
470 | wxHashTable::Node* mNode = MacroDefs.Next(); |
471 | while (mNode) | |
9a29912f | 472 | { |
f6fe5318 | 473 | TexMacroDef* def = (TexMacroDef*) mNode->GetData(); |
9a29912f | 474 | delete def; |
f6fe5318 | 475 | mNode = MacroDefs.Next(); |
9a29912f JS |
476 | } |
477 | MacroDefs.Clear(); | |
478 | #ifdef __WXMSW__ | |
479 | delete TheTex2RTFServer; | |
23016fdd | 480 | wxDDECleanUp(); |
9a29912f | 481 | #endif |
ffaaaacb VZ |
482 | |
483 | #if wxUSE_HELP | |
9a29912f | 484 | delete HelpInstance; |
ffaaaacb | 485 | #endif // wxUSE_HELP |
9a29912f | 486 | |
71599f0a GT |
487 | if (BigBuffer) |
488 | { | |
489 | delete BigBuffer; | |
490 | BigBuffer = NULL; | |
491 | } | |
492 | if (currentArgData) | |
493 | { | |
494 | delete currentArgData; | |
495 | currentArgData = NULL; | |
496 | } | |
497 | if (TexFileRoot) | |
498 | { | |
499 | delete TexFileRoot; | |
500 | TexFileRoot = NULL; | |
501 | } | |
502 | if (TexBibName) | |
503 | { | |
504 | delete TexBibName; | |
505 | TexBibName = NULL; | |
506 | } | |
507 | if (TexTmpBibName) | |
508 | { | |
509 | delete TexTmpBibName; | |
510 | TexTmpBibName = NULL; | |
511 | } | |
512 | if (FileRoot) | |
513 | { | |
514 | delete FileRoot; | |
515 | FileRoot = NULL; | |
516 | } | |
517 | if (ContentsName) | |
518 | { | |
519 | delete ContentsName; | |
520 | ContentsName = NULL; | |
521 | } | |
522 | if (TmpContentsName) | |
523 | { | |
524 | delete TmpContentsName; | |
525 | TmpContentsName = NULL; | |
526 | } | |
527 | if (TmpFrameContentsName) | |
528 | { | |
529 | delete TmpFrameContentsName; | |
530 | TmpFrameContentsName = NULL; | |
531 | } | |
532 | if (WinHelpContentsFileName) | |
533 | { | |
534 | delete WinHelpContentsFileName; | |
535 | WinHelpContentsFileName = NULL; | |
536 | } | |
ad781c23 | 537 | if (RefFileName) |
71599f0a | 538 | { |
ad781c23 GD |
539 | delete RefFileName; |
540 | RefFileName = NULL; | |
71599f0a GT |
541 | } |
542 | if (TopLevel) | |
543 | { | |
544 | delete TopLevel; | |
545 | TopLevel = NULL; | |
546 | } | |
547 | if (MacroFile) | |
548 | { | |
549 | delete MacroFile; | |
550 | MacroFile = NULL; | |
551 | } | |
552 | if (RTFCharset) | |
553 | { | |
554 | delete RTFCharset; | |
555 | RTFCharset = NULL; | |
556 | } | |
557 | ||
3924dd22 GT |
558 | delete [] PageStyle; |
559 | delete [] BibliographyStyleString; | |
560 | delete [] DocumentStyleString; | |
561 | delete [] bitmapMethod; | |
562 | delete [] backgroundColourString; | |
563 | delete [] ContentsNameString; | |
564 | delete [] AbstractNameString; | |
565 | delete [] GlossaryNameString; | |
566 | delete [] ReferencesNameString; | |
567 | delete [] FiguresNameString; | |
568 | delete [] TablesNameString; | |
569 | delete [] FigureNameString; | |
570 | delete [] TableNameString; | |
571 | delete [] IndexNameString; | |
572 | delete [] ChapterNameString; | |
573 | delete [] SectionNameString; | |
574 | delete [] SubsectionNameString; | |
575 | delete [] SubsubsectionNameString; | |
576 | delete [] UpNameString; | |
577 | if (winHelpTitle) | |
578 | delete[] winHelpTitle; | |
579 | ||
9a29912f JS |
580 | // TODO: this simulates zero-memory leaks! |
581 | // Otherwise there are just too many... | |
12a44087 | 582 | #ifndef __WXGTK__ |
fad535ee | 583 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
9a29912f | 584 | wxDebugContext::SetCheckpoint(); |
fad535ee | 585 | #endif |
12a44087 | 586 | #endif |
9a29912f JS |
587 | |
588 | return 0; | |
589 | } | |
c4c794e1 | 590 | #endif |
9a29912f JS |
591 | void ShowOptions(void) |
592 | { | |
6c155d33 | 593 | wxChar buf[100]; |
b63b07a8 | 594 | wxSnprintf(buf, sizeof(buf), _T("Tex2RTF version %.2f"), versionNo); |
9a29912f | 595 | OnInform(buf); |
6c155d33 JS |
596 | OnInform(_T("Usage: tex2rtf [input] [output] [switches]\n")); |
597 | OnInform(_T("where valid switches are")); | |
610c6ded | 598 | #ifndef NO_GUI |
6c155d33 | 599 | OnInform(_T(" -interactive")); |
610c6ded | 600 | #endif |
6c155d33 JS |
601 | OnInform(_T(" -bufsize <size in K>")); |
602 | OnInform(_T(" -charset <pc | pca | ansi | mac> (default ansi)")); | |
603 | OnInform(_T(" -twice")); | |
604 | OnInform(_T(" -sync")); | |
88fd7006 | 605 | OnInform(_T(" -checkcurlybraces")); |
6c155d33 JS |
606 | OnInform(_T(" -checksyntax")); |
607 | OnInform(_T(" -macros <filename>")); | |
608 | OnInform(_T(" -winhelp")); | |
609 | OnInform(_T(" -rtf")); | |
610 | OnInform(_T(" -html")); | |
611 | OnInform(_T(" -xlp\n")); | |
9a29912f JS |
612 | } |
613 | ||
614 | #ifndef NO_GUI | |
615 | ||
616 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
617 | EVT_CLOSE(MyFrame::OnCloseWindow) | |
618 | EVT_MENU(TEX_QUIT, MyFrame::OnExit) | |
619 | EVT_MENU(TEX_GO, MyFrame::OnGo) | |
620 | EVT_MENU(TEX_SET_INPUT, MyFrame::OnSetInput) | |
621 | EVT_MENU(TEX_SET_OUTPUT, MyFrame::OnSetOutput) | |
622 | EVT_MENU(TEX_SAVE_FILE, MyFrame::OnSaveFile) | |
623 | EVT_MENU(TEX_VIEW_LATEX, MyFrame::OnViewLatex) | |
624 | EVT_MENU(TEX_VIEW_OUTPUT, MyFrame::OnViewOutput) | |
625 | EVT_MENU(TEX_VIEW_CUSTOM_MACROS, MyFrame::OnShowMacros) | |
626 | EVT_MENU(TEX_LOAD_CUSTOM_MACROS, MyFrame::OnLoadMacros) | |
627 | EVT_MENU(TEX_MODE_RTF, MyFrame::OnModeRTF) | |
628 | EVT_MENU(TEX_MODE_WINHELP, MyFrame::OnModeWinHelp) | |
629 | EVT_MENU(TEX_MODE_HTML, MyFrame::OnModeHTML) | |
630 | EVT_MENU(TEX_MODE_XLP, MyFrame::OnModeXLP) | |
88fd7006 | 631 | EVT_MENU(TEX_OPTIONS_CURLY_BRACE, MyFrame::OnOptionsCurlyBrace) |
fad535ee | 632 | EVT_MENU(TEX_OPTIONS_SYNTAX_CHECKING, MyFrame::OnOptionsSyntaxChecking) |
9a29912f JS |
633 | EVT_MENU(TEX_HELP, MyFrame::OnHelp) |
634 | EVT_MENU(TEX_ABOUT, MyFrame::OnAbout) | |
635 | END_EVENT_TABLE() | |
636 | ||
637 | // My frame constructor | |
638 | MyFrame::MyFrame(wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size): | |
639 | wxFrame(frame, id, title, pos, size) | |
640 | {} | |
641 | ||
6c155d33 | 642 | void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
9a29912f JS |
643 | { |
644 | if (!stopRunning && !OkToClose) | |
645 | { | |
b63b07a8 RL |
646 | stopRunning = true; |
647 | runTwice = false; | |
9a29912f JS |
648 | return; |
649 | } | |
650 | else if (OkToClose) | |
651 | { | |
9a29912f JS |
652 | this->Destroy(); |
653 | } | |
654 | } | |
655 | ||
6c155d33 | 656 | void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) |
9a29912f | 657 | { |
71599f0a GT |
658 | Close(); |
659 | // this->Destroy(); | |
9a29912f JS |
660 | } |
661 | ||
6c155d33 | 662 | void MyFrame::OnGo(wxCommandEvent& WXUNUSED(event)) |
9a29912f | 663 | { |
f6bcfd97 | 664 | passNumber = 1; |
63ad540d | 665 | errorCount = 0; |
b63b07a8 RL |
666 | menuBar->EnableTop(0, false); |
667 | menuBar->EnableTop(1, false); | |
668 | menuBar->EnableTop(2, false); | |
669 | menuBar->EnableTop(3, false); | |
9a29912f | 670 | textWindow->Clear(); |
b63b07a8 | 671 | Tex2RTFYield(true); |
9a29912f JS |
672 | Go(); |
673 | ||
04b9c5bb GT |
674 | if (stopRunning) |
675 | { | |
67a99992 | 676 | #if wxUSE_STATUSBAR |
6c155d33 | 677 | SetStatusText(_T("Build aborted!")); |
67a99992 | 678 | #endif // wxUSE_STATUSBAR |
04b9c5bb | 679 | wxString errBuf; |
6c155d33 JS |
680 | errBuf.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount); |
681 | OnInform((wxChar *)errBuf.c_str()); | |
04b9c5bb GT |
682 | } |
683 | ||
684 | ||
685 | if (runTwice && !stopRunning) | |
9a29912f | 686 | { |
b63b07a8 | 687 | Tex2RTFYield(true); |
9a29912f JS |
688 | Go(); |
689 | } | |
b63b07a8 RL |
690 | menuBar->EnableTop(0, true); |
691 | menuBar->EnableTop(1, true); | |
692 | menuBar->EnableTop(2, true); | |
693 | menuBar->EnableTop(3, true); | |
9a29912f JS |
694 | } |
695 | ||
6c155d33 | 696 | void MyFrame::OnSetInput(wxCommandEvent& WXUNUSED(event)) |
9a29912f | 697 | { |
b63b07a8 | 698 | ChooseInputFile(true); |
9a29912f JS |
699 | } |
700 | ||
6c155d33 | 701 | void MyFrame::OnSetOutput(wxCommandEvent& WXUNUSED(event)) |
9a29912f | 702 | { |
b63b07a8 | 703 | ChooseOutputFile(true); |
9a29912f JS |
704 | } |
705 | ||
6c155d33 | 706 | void MyFrame::OnSaveFile(wxCommandEvent& WXUNUSED(event)) |
9a29912f | 707 | { |
6c155d33 JS |
708 | wxString s = wxFileSelector(_T("Save text to file"), _T(""), _T(""), _T("txt"), _T("*.txt")); |
709 | if (s != _T("")) | |
9a29912f JS |
710 | { |
711 | textWindow->SaveFile(s); | |
67a99992 | 712 | #if wxUSE_STATUSBAR |
6c155d33 | 713 | wxChar buf[350]; |
b63b07a8 | 714 | wxSnprintf(buf, sizeof(buf), _T("Saved text to %s"), (const wxChar*) s.c_str()); |
9a29912f | 715 | frame->SetStatusText(buf, 0); |
67a99992 | 716 | #endif // wxUSE_STATUSBAR |
9a29912f JS |
717 | } |
718 | } | |
719 | ||
6c155d33 | 720 | void MyFrame::OnViewOutput(wxCommandEvent& WXUNUSED(event)) |
9a29912f JS |
721 | { |
722 | ChooseOutputFile(); | |
723 | if (OutputFile && wxFileExists(OutputFile)) | |
724 | { | |
725 | textWindow->LoadFile(OutputFile); | |
6c155d33 | 726 | wxChar buf[300]; |
9a29912f | 727 | wxString str(wxFileNameFromPath(OutputFile)); |
b63b07a8 | 728 | wxSnprintf(buf, sizeof(buf), _T("Tex2RTF [%s]"), (const wxChar*) str.c_str()); |
9a29912f JS |
729 | frame->SetTitle(buf); |
730 | } | |
731 | } | |
732 | ||
6c155d33 | 733 | void MyFrame::OnViewLatex(wxCommandEvent& WXUNUSED(event)) |
9a29912f JS |
734 | { |
735 | ChooseInputFile(); | |
736 | if (InputFile && wxFileExists(InputFile)) | |
737 | { | |
738 | textWindow->LoadFile(InputFile); | |
6c155d33 | 739 | wxChar buf[300]; |
9a29912f | 740 | wxString str(wxFileNameFromPath(OutputFile)); |
b63b07a8 | 741 | wxSnprintf(buf, sizeof(buf), _T("Tex2RTF [%s]"), (const wxChar*) str.c_str()); |
9a29912f JS |
742 | frame->SetTitle(buf); |
743 | } | |
744 | } | |
745 | ||
6c155d33 | 746 | void MyFrame::OnLoadMacros(wxCommandEvent& WXUNUSED(event)) |
9a29912f JS |
747 | { |
748 | textWindow->Clear(); | |
6c155d33 | 749 | wxString s = wxFileSelector(_T("Choose custom macro file"), wxPathOnly(MacroFile), wxFileNameFromPath(MacroFile), _T("ini"), _T("*.ini")); |
489f6cf7 | 750 | if (!s.empty() && wxFileExists(s)) |
9a29912f JS |
751 | { |
752 | MacroFile = copystring(s); | |
6c155d33 | 753 | ReadCustomMacros((wxChar *)s.c_str()); |
9a29912f JS |
754 | ShowCustomMacros(); |
755 | } | |
756 | } | |
757 | ||
6c155d33 | 758 | void MyFrame::OnShowMacros(wxCommandEvent& WXUNUSED(event)) |
9a29912f JS |
759 | { |
760 | textWindow->Clear(); | |
b63b07a8 | 761 | Tex2RTFYield(true); |
9a29912f JS |
762 | ShowCustomMacros(); |
763 | } | |
764 | ||
6c155d33 | 765 | void MyFrame::OnModeRTF(wxCommandEvent& WXUNUSED(event)) |
9a29912f JS |
766 | { |
767 | convertMode = TEX_RTF; | |
b63b07a8 | 768 | winHelp = false; |
9a29912f JS |
769 | InputFile = NULL; |
770 | OutputFile = NULL; | |
67a99992 | 771 | #if wxUSE_STATUSBAR |
6c155d33 | 772 | SetStatusText(_T("In linear RTF mode."), 1); |
67a99992 | 773 | #endif // wxUSE_STATUSBAR |
9a29912f JS |
774 | } |
775 | ||
6c155d33 | 776 | void MyFrame::OnModeWinHelp(wxCommandEvent& WXUNUSED(event)) |
9a29912f JS |
777 | { |
778 | convertMode = TEX_RTF; | |
b63b07a8 | 779 | winHelp = true; |
9a29912f JS |
780 | InputFile = NULL; |
781 | OutputFile = NULL; | |
67a99992 | 782 | #if wxUSE_STATUSBAR |
6c155d33 | 783 | SetStatusText(_T("In WinHelp RTF mode."), 1); |
67a99992 | 784 | #endif // wxUSE_STATUSBAR |
9a29912f JS |
785 | } |
786 | ||
6c155d33 | 787 | void MyFrame::OnModeHTML(wxCommandEvent& WXUNUSED(event)) |
9a29912f JS |
788 | { |
789 | convertMode = TEX_HTML; | |
b63b07a8 | 790 | winHelp = false; |
9a29912f JS |
791 | InputFile = NULL; |
792 | OutputFile = NULL; | |
67a99992 | 793 | #if wxUSE_STATUSBAR |
6c155d33 | 794 | SetStatusText(_T("In HTML mode."), 1); |
67a99992 | 795 | #endif // wxUSE_STATUSBAR |
9a29912f JS |
796 | } |
797 | ||
6c155d33 | 798 | void MyFrame::OnModeXLP(wxCommandEvent& WXUNUSED(event)) |
9a29912f JS |
799 | { |
800 | convertMode = TEX_XLP; | |
801 | InputFile = NULL; | |
802 | OutputFile = NULL; | |
67a99992 | 803 | #if wxUSE_STATUSBAR |
6c155d33 | 804 | SetStatusText(_T("In XLP mode."), 1); |
67a99992 | 805 | #endif // wxUSE_STATUSBAR |
9a29912f JS |
806 | } |
807 | ||
88fd7006 | 808 | void MyFrame::OnOptionsCurlyBrace(wxCommandEvent& WXUNUSED(event)) |
fad535ee | 809 | { |
88fd7006 | 810 | checkCurlyBraces = !checkCurlyBraces; |
67a99992 | 811 | #if wxUSE_STATUSBAR |
88fd7006 | 812 | if (checkCurlyBraces) |
fad535ee | 813 | { |
88fd7006 | 814 | SetStatusText(_T("Checking curly braces: YES"), 1); |
fad535ee GT |
815 | } |
816 | else | |
817 | { | |
88fd7006 | 818 | SetStatusText(_T("Checking curly braces: NO"), 1); |
fad535ee | 819 | } |
67a99992 | 820 | #endif // wxUSE_STATUSBAR |
fad535ee GT |
821 | } |
822 | ||
823 | ||
6c155d33 | 824 | void MyFrame::OnOptionsSyntaxChecking(wxCommandEvent& WXUNUSED(event)) |
fad535ee GT |
825 | { |
826 | checkSyntax = !checkSyntax; | |
67a99992 | 827 | #if wxUSE_STATUSBAR |
fad535ee GT |
828 | if (checkSyntax) |
829 | { | |
6c155d33 | 830 | SetStatusText(_T("Checking syntax: YES"), 1); |
fad535ee GT |
831 | } |
832 | else | |
833 | { | |
6c155d33 | 834 | SetStatusText(_T("Checking syntax: NO"), 1); |
fad535ee | 835 | } |
67a99992 | 836 | #endif // wxUSE_STATUSBAR |
fad535ee GT |
837 | } |
838 | ||
839 | ||
6c155d33 | 840 | void MyFrame::OnHelp(wxCommandEvent& WXUNUSED(event)) |
9a29912f | 841 | { |
ffaaaacb | 842 | #if wxUSE_HELP |
9a29912f JS |
843 | HelpInstance->LoadFile(); |
844 | HelpInstance->DisplayContents(); | |
ffaaaacb | 845 | #endif // wxUSE_HELP |
9a29912f JS |
846 | } |
847 | ||
6c155d33 | 848 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
9a29912f | 849 | { |
6c155d33 | 850 | wxChar buf[300]; |
9a29912f | 851 | #ifdef __WIN32__ |
6c155d33 | 852 | wxChar *platform = _T(" (32-bit)"); |
9a29912f JS |
853 | #else |
854 | #ifdef __WXMSW__ | |
6c155d33 | 855 | wxChar *platform = _T(" (16-bit)"); |
9a29912f | 856 | #else |
6c155d33 | 857 | wxChar *platform = _T(""); |
9a29912f JS |
858 | #endif |
859 | #endif | |
b63b07a8 | 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-2002"), versionNo, platform); |
6c155d33 | 861 | wxMessageBox(buf, _T("About Tex2RTF")); |
9a29912f JS |
862 | } |
863 | ||
864 | void ChooseInputFile(bool force) | |
865 | { | |
866 | if (force || !InputFile) | |
867 | { | |
6c155d33 | 868 | wxString s = wxFileSelector(_T("Choose LaTeX input file"), wxPathOnly(InputFile), wxFileNameFromPath(InputFile), _T("tex"), _T("*.tex")); |
489f6cf7 | 869 | if (!s.empty()) |
9a29912f JS |
870 | { |
871 | // Different file, so clear index entries. | |
872 | ClearKeyWordTable(); | |
873 | ResetContentsLevels(0); | |
874 | passNumber = 1; | |
63ad540d GT |
875 | errorCount = 0; |
876 | ||
9a29912f JS |
877 | InputFile = copystring(s); |
878 | wxString str = wxFileNameFromPath(InputFile); | |
63ad540d | 879 | wxString buf; |
6c155d33 JS |
880 | buf.Printf(_T("Tex2RTF [%s]"), str.c_str()); |
881 | frame->SetTitle((wxChar *)buf.c_str()); | |
9a29912f JS |
882 | OutputFile = NULL; |
883 | } | |
884 | } | |
885 | } | |
886 | ||
887 | void ChooseOutputFile(bool force) | |
888 | { | |
6c155d33 JS |
889 | wxChar extensionBuf[10]; |
890 | wxChar wildBuf[10]; | |
891 | wxStrcpy(wildBuf, _T("*.")); | |
aed0ed3c | 892 | wxString path; |
9a29912f JS |
893 | if (OutputFile) |
894 | path = wxPathOnly(OutputFile); | |
895 | else if (InputFile) | |
896 | path = wxPathOnly(InputFile); | |
ffaaaacb | 897 | |
9a29912f JS |
898 | switch (convertMode) |
899 | { | |
900 | case TEX_RTF: | |
901 | { | |
6c155d33 JS |
902 | wxStrcpy(extensionBuf, _T("rtf")); |
903 | wxStrcat(wildBuf, _T("rtf")); | |
9a29912f JS |
904 | break; |
905 | } | |
906 | case TEX_XLP: | |
907 | { | |
6c155d33 JS |
908 | wxStrcpy(extensionBuf, _T("xlp")); |
909 | wxStrcat(wildBuf, _T("xlp")); | |
9a29912f JS |
910 | break; |
911 | } | |
912 | case TEX_HTML: | |
913 | { | |
6c155d33 JS |
914 | wxStrcpy(extensionBuf, _T("html")); |
915 | wxStrcat(wildBuf, _T("html")); | |
9a29912f JS |
916 | break; |
917 | } | |
918 | } | |
919 | if (force || !OutputFile) | |
920 | { | |
6c155d33 | 921 | wxString s = wxFileSelector(_T("Choose output file"), path, wxFileNameFromPath(OutputFile), |
9a29912f | 922 | extensionBuf, wildBuf); |
489f6cf7 | 923 | if (!s.empty()) |
9a29912f JS |
924 | OutputFile = copystring(s); |
925 | } | |
926 | } | |
927 | #endif | |
928 | ||
929 | bool Go(void) | |
930 | { | |
931 | #ifndef NO_GUI | |
932 | ChooseInputFile(); | |
933 | ChooseOutputFile(); | |
934 | #endif | |
935 | ||
04b9c5bb | 936 | if (!InputFile || !OutputFile || stopRunning) |
b63b07a8 | 937 | return false; |
9a29912f JS |
938 | |
939 | #ifndef NO_GUI | |
940 | if (isInteractive) | |
941 | { | |
6c155d33 | 942 | wxChar buf[300]; |
9a29912f JS |
943 | wxString str = wxFileNameFromPath(InputFile); |
944 | ||
b63b07a8 | 945 | wxSnprintf(buf, sizeof(buf), _T("Tex2RTF [%s]"), (const wxChar*) str); |
9a29912f JS |
946 | frame->SetTitle(buf); |
947 | } | |
948 | ||
949 | wxStartTimer(); | |
950 | #endif | |
951 | ||
952 | // Find extension-less filename | |
6c155d33 | 953 | wxStrcpy(FileRoot, OutputFile); |
9a29912f JS |
954 | StripExtension(FileRoot); |
955 | ||
956 | if (truncateFilenames && convertMode == TEX_HTML) | |
957 | { | |
958 | // Truncate to five characters. This ensures that | |
959 | // we can generate DOS filenames such as thing999. But 1000 files | |
960 | // may not be enough, of course... | |
6c155d33 | 961 | wxChar* sName = wxFileNameFromPath( FileRoot); // this Julian's method is non-destructive reference |
9a29912f JS |
962 | |
963 | if(sName) | |
6c155d33 | 964 | if(wxStrlen( sName) > 5) |
9a29912f JS |
965 | sName[5] = '\0'; // that should do! |
966 | } | |
ffaaaacb | 967 | |
b63b07a8 RL |
968 | wxSnprintf(ContentsName, 300, _T("%s.con"), FileRoot); |
969 | wxSnprintf(TmpContentsName, 300, _T("%s.cn1"), FileRoot); | |
970 | wxSnprintf(TmpFrameContentsName, 300, _T("%s.frc"), FileRoot); | |
971 | wxSnprintf(WinHelpContentsFileName, 300, _T("%s.cnt"), FileRoot); | |
972 | wxSnprintf(RefFileName, 300, _T("%s.ref"), FileRoot); | |
9a29912f JS |
973 | |
974 | TexPathList.EnsureFileAccessible(InputFile); | |
975 | if (!bulletFile) | |
976 | { | |
6c155d33 | 977 | wxString s = TexPathList.FindValidPath(_T("bullet.bmp")); |
489f6cf7 | 978 | if (!s.empty()) |
9a29912f JS |
979 | { |
980 | wxString str = wxFileNameFromPath(s); | |
981 | bulletFile = copystring(str); | |
982 | } | |
983 | } | |
984 | ||
ad781c23 GD |
985 | if (wxFileExists(RefFileName)) |
986 | ReadTexReferences(RefFileName); | |
ffaaaacb | 987 | |
b63b07a8 | 988 | bool success = false; |
9a29912f JS |
989 | |
990 | if (InputFile && OutputFile) | |
991 | { | |
2b5f62a0 | 992 | if (!wxFileExists(InputFile)) |
9a29912f | 993 | { |
6c155d33 | 994 | OnError(_T("Cannot open input file!")); |
9a29912f | 995 | TexCleanUp(); |
b63b07a8 | 996 | return false; |
9a29912f | 997 | } |
67a99992 | 998 | #if !defined(NO_GUI) && wxUSE_STATUSBAR |
9a29912f JS |
999 | if (isInteractive) |
1000 | { | |
63ad540d | 1001 | wxString buf; |
6c155d33 JS |
1002 | buf.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber); |
1003 | frame->SetStatusText((wxChar *)buf.c_str()); | |
9a29912f JS |
1004 | } |
1005 | #endif | |
b63b07a8 | 1006 | OkToClose = false; |
6c155d33 | 1007 | OnInform(_T("Reading LaTeX file...")); |
9a29912f JS |
1008 | TexLoadFile(InputFile); |
1009 | ||
04b9c5bb | 1010 | if (stopRunning) |
e4941e3d | 1011 | { |
b63b07a8 RL |
1012 | OkToClose = true; |
1013 | return false; | |
e4941e3d | 1014 | } |
04b9c5bb | 1015 | |
9a29912f JS |
1016 | switch (convertMode) |
1017 | { | |
1018 | case TEX_RTF: | |
1019 | { | |
1020 | success = RTFGo(); | |
1021 | break; | |
1022 | } | |
1023 | case TEX_XLP: | |
1024 | { | |
1025 | success = XLPGo(); | |
1026 | break; | |
1027 | } | |
1028 | case TEX_HTML: | |
1029 | { | |
1030 | success = HTMLGo(); | |
1031 | break; | |
1032 | } | |
1033 | } | |
1034 | } | |
1035 | if (stopRunning) | |
1036 | { | |
6c155d33 | 1037 | OnInform(_T("*** Aborted by user.")); |
b63b07a8 RL |
1038 | success = false; |
1039 | stopRunning = false; | |
1040 | OkToClose = true; | |
9a29912f JS |
1041 | } |
1042 | ||
1043 | if (success) | |
1044 | { | |
ad781c23 | 1045 | WriteTexReferences(RefFileName); |
9a29912f | 1046 | TexCleanUp(); |
b63b07a8 | 1047 | startedSections = false; |
9a29912f | 1048 | |
63ad540d | 1049 | wxString buf; |
9a29912f JS |
1050 | #ifndef NO_GUI |
1051 | long tim = wxGetElapsedTime(); | |
6c155d33 JS |
1052 | buf.Printf(_T("Finished PASS #%d in %ld seconds.\n"), passNumber, (long)(tim/1000.0)); |
1053 | OnInform((wxChar *)buf.c_str()); | |
63ad540d GT |
1054 | |
1055 | if (errorCount) | |
1056 | { | |
6c155d33 JS |
1057 | buf.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount); |
1058 | OnInform((wxChar *)buf.c_str()); | |
63ad540d GT |
1059 | } |
1060 | ||
67a99992 | 1061 | #if wxUSE_STATUSBAR |
9a29912f JS |
1062 | if (isInteractive) |
1063 | { | |
6c155d33 JS |
1064 | buf.Printf(_T("Done, %d %s."), passNumber, (passNumber > 1) ? _T("passes") : _T("pass")); |
1065 | frame->SetStatusText((wxChar *)buf.c_str()); | |
9a29912f | 1066 | } |
67a99992 | 1067 | #endif // wxUSE_STATUSBAR |
9a29912f | 1068 | #else |
6c155d33 JS |
1069 | buf.Printf(_T("Done, %d %s."), passNumber, (passNumber > 1) ? _T("passes") : _T("pass")); |
1070 | OnInform((wxChar *)buf.c_str()); | |
63ad540d GT |
1071 | if (errorCount) |
1072 | { | |
6c155d33 JS |
1073 | buf.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount); |
1074 | OnInform((wxChar *)buf.c_str()); | |
63ad540d | 1075 | } |
9a29912f JS |
1076 | #endif |
1077 | passNumber ++; | |
63ad540d | 1078 | errorCount = 0; |
b63b07a8 RL |
1079 | OkToClose = true; |
1080 | return true; | |
9a29912f JS |
1081 | } |
1082 | ||
1083 | TexCleanUp(); | |
b63b07a8 | 1084 | startedSections = false; |
9a29912f | 1085 | |
67a99992 | 1086 | #if !defined(NO_GUI) && wxUSE_STATUSBAR |
6c155d33 | 1087 | frame->SetStatusText(_T("Aborted by user.")); |
8efab359 | 1088 | #endif // GUI |
fad535ee | 1089 | |
6c155d33 | 1090 | OnInform(_T("Sorry, unsuccessful.")); |
b63b07a8 RL |
1091 | OkToClose = true; |
1092 | return false; | |
9a29912f JS |
1093 | } |
1094 | ||
6c155d33 | 1095 | void OnError(const wxChar *msg) |
9a29912f | 1096 | { |
b63b07a8 | 1097 | wxString msg_string = msg; |
63ad540d GT |
1098 | errorCount++; |
1099 | ||
9a29912f | 1100 | #ifdef NO_GUI |
b63b07a8 | 1101 | wxSTD cerr << "Error: " << msg_string.mb_str() << "\n"; |
219f4a6d | 1102 | wxSTD cerr.flush(); |
9a29912f | 1103 | #else |
3f8e5072 | 1104 | if (isInteractive && frame) |
6c155d33 | 1105 | (*frame->textWindow) << _T("Error: ") << msg << _T("\n"); |
9a29912f JS |
1106 | else |
1107 | #ifdef __UNIX__ | |
1108 | { | |
b63b07a8 | 1109 | wxSTD cerr << "Error: " << msg_string.mb_str() << "\n"; |
219f4a6d | 1110 | wxSTD cerr.flush(); |
9a29912f JS |
1111 | } |
1112 | #endif | |
63ad540d | 1113 | |
9a29912f | 1114 | #ifdef __WXMSW__ |
11611a62 | 1115 | wxLogError(msg); |
9a29912f | 1116 | #endif |
b63b07a8 | 1117 | Tex2RTFYield(true); |
9a29912f JS |
1118 | #endif // NO_GUI |
1119 | } | |
1120 | ||
6c155d33 | 1121 | void OnInform(const wxChar *msg) |
9a29912f | 1122 | { |
b63b07a8 | 1123 | wxString msg_string = msg; |
9a29912f | 1124 | #ifdef NO_GUI |
b63b07a8 | 1125 | wxSTD cout << msg_string.mb_str() << "\n"; |
219f4a6d | 1126 | wxSTD cout.flush(); |
9a29912f | 1127 | #else |
3f8e5072 | 1128 | if (isInteractive && frame) |
6c155d33 | 1129 | (*frame->textWindow) << msg << _T("\n"); |
fad535ee GT |
1130 | /* This whole block of code is just wrong I think. It would behave |
1131 | completely wrong under anything other than MSW due to the ELSE | |
4fe30bce | 1132 | with no statement, and the cout calls would fail under MSW, as |
254a2129 | 1133 | the code in this block is compiled if !NO_GUI This code has been |
4fe30bce | 1134 | here since v1.1 of this file too. - gt |
9a29912f JS |
1135 | else |
1136 | #ifdef __WXMSW__ | |
1137 | { | |
b63b07a8 | 1138 | wxSTD cout << msg_string.mb_str() << "\n"; |
219f4a6d | 1139 | wxSTD cout.flush(); |
9a29912f JS |
1140 | } |
1141 | #endif | |
1142 | #ifdef __WXMSW__ | |
1143 | {} | |
1144 | #endif | |
fad535ee | 1145 | */ |
9a29912f JS |
1146 | if (isInteractive) |
1147 | { | |
b63b07a8 | 1148 | Tex2RTFYield(true); |
9a29912f JS |
1149 | } |
1150 | #endif // NO_GUI | |
1151 | } | |
1152 | ||
1153 | void OnMacro(int macroId, int no_args, bool start) | |
1154 | { | |
1155 | switch (convertMode) | |
1156 | { | |
1157 | case TEX_RTF: | |
1158 | { | |
1159 | RTFOnMacro(macroId, no_args, start); | |
1160 | break; | |
1161 | } | |
1162 | case TEX_XLP: | |
1163 | { | |
1164 | XLPOnMacro(macroId, no_args, start); | |
1165 | break; | |
1166 | } | |
1167 | case TEX_HTML: | |
1168 | { | |
1169 | HTMLOnMacro(macroId, no_args, start); | |
1170 | break; | |
1171 | } | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | bool OnArgument(int macroId, int arg_no, bool start) | |
1176 | { | |
1177 | switch (convertMode) | |
1178 | { | |
1179 | case TEX_RTF: | |
1180 | { | |
1181 | return RTFOnArgument(macroId, arg_no, start); | |
6c155d33 | 1182 | // break; |
9a29912f JS |
1183 | } |
1184 | case TEX_XLP: | |
1185 | { | |
1186 | return XLPOnArgument(macroId, arg_no, start); | |
6c155d33 | 1187 | // break; |
9a29912f JS |
1188 | } |
1189 | case TEX_HTML: | |
1190 | { | |
1191 | return HTMLOnArgument(macroId, arg_no, start); | |
6c155d33 | 1192 | // break; |
9a29912f JS |
1193 | } |
1194 | } | |
b63b07a8 | 1195 | return true; |
9a29912f JS |
1196 | } |
1197 | ||
1198 | /* | |
1199 | * DDE Stuff | |
1200 | */ | |
1201 | #if defined(__WXMSW__) && !defined(NO_GUI) | |
1202 | ||
1203 | /* | |
1204 | * Server | |
1205 | */ | |
1206 | ||
1207 | wxConnectionBase *Tex2RTFServer::OnAcceptConnection(const wxString& topic) | |
1208 | { | |
6c155d33 | 1209 | if (topic == _T("TEX2RTF")) |
9a29912f JS |
1210 | { |
1211 | if (!ipc_buffer) | |
6c155d33 | 1212 | ipc_buffer = new wxChar[1000]; |
ffaaaacb | 1213 | |
9a29912f JS |
1214 | return new Tex2RTFConnection(ipc_buffer, 4000); |
1215 | } | |
1216 | else | |
1217 | return NULL; | |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * Connection | |
1222 | */ | |
ffaaaacb | 1223 | |
6c155d33 | 1224 | Tex2RTFConnection::Tex2RTFConnection(wxChar *buf, int size):wxDDEConnection(buf, size) |
9a29912f JS |
1225 | { |
1226 | } | |
1227 | ||
6c155d33 | 1228 | bool SplitCommand(wxChar *data, wxChar *firstArg, wxChar *secondArg) |
9a29912f JS |
1229 | { |
1230 | firstArg[0] = 0; | |
1231 | secondArg[0] = 0; | |
1232 | int i = 0; | |
b63b07a8 | 1233 | bool stop = false; |
9a29912f JS |
1234 | // Find first argument (command name) |
1235 | while (!stop) | |
1236 | { | |
1237 | if (data[i] == ' ' || data[i] == 0) | |
b63b07a8 | 1238 | stop = true; |
9a29912f JS |
1239 | else |
1240 | { | |
1241 | firstArg[i] = data[i]; | |
1242 | i ++; | |
1243 | } | |
1244 | } | |
1245 | firstArg[i] = 0; | |
1246 | if (data[i] == ' ') | |
1247 | { | |
1248 | // Find second argument | |
1249 | i ++; | |
1250 | int j = 0; | |
1251 | while (data[i] != 0) | |
1252 | { | |
1253 | secondArg[j] = data[i]; | |
1254 | i ++; | |
1255 | j ++; | |
1256 | } | |
1257 | secondArg[j] = 0; | |
1258 | } | |
b63b07a8 | 1259 | return true; |
9a29912f JS |
1260 | } |
1261 | ||
6c155d33 | 1262 | bool Tex2RTFConnection::OnExecute(const wxString& WXUNUSED(topic), wxChar *data, int WXUNUSED(size), wxIPCFormat WXUNUSED(format)) |
9a29912f | 1263 | { |
6c155d33 | 1264 | wxStrcpy(Tex2RTFLastStatus, _T("OK")); |
9a29912f | 1265 | |
6c155d33 JS |
1266 | wxChar firstArg[50]; |
1267 | wxChar secondArg[300]; | |
9a29912f JS |
1268 | if (SplitCommand(data, firstArg, secondArg)) |
1269 | { | |
6c155d33 JS |
1270 | bool hasArg = (wxStrlen(secondArg) > 0); |
1271 | if (wxStrcmp(firstArg, _T("INPUT")) == 0 && hasArg) | |
9a29912f JS |
1272 | { |
1273 | if (InputFile) delete[] InputFile; | |
1274 | InputFile = copystring(secondArg); | |
1275 | if (frame) | |
1276 | { | |
6c155d33 | 1277 | wxChar buf[100]; |
9a29912f | 1278 | wxString str = wxFileNameFromPath(InputFile); |
b63b07a8 | 1279 | wxSnprintf(buf, sizeof(buf), _T("Tex2RTF [%s]"), (const wxChar*) str); |
9a29912f JS |
1280 | frame->SetTitle(buf); |
1281 | } | |
1282 | } | |
6c155d33 | 1283 | else if (wxStrcmp(firstArg, _T("OUTPUT")) == 0 && hasArg) |
9a29912f JS |
1284 | { |
1285 | if (OutputFile) delete[] OutputFile; | |
1286 | OutputFile = copystring(secondArg); | |
1287 | } | |
6c155d33 | 1288 | else if (wxStrcmp(firstArg, _T("GO")) == 0) |
9a29912f | 1289 | { |
6c155d33 | 1290 | wxStrcpy(Tex2RTFLastStatus, _T("WORKING")); |
9a29912f | 1291 | if (!Go()) |
6c155d33 | 1292 | wxStrcpy(Tex2RTFLastStatus, _T("CONVERSION ERROR")); |
9a29912f | 1293 | else |
6c155d33 | 1294 | wxStrcpy(Tex2RTFLastStatus, _T("OK")); |
9a29912f | 1295 | } |
6c155d33 | 1296 | else if (wxStrcmp(firstArg, _T("EXIT")) == 0) |
9a29912f | 1297 | { |
e3065973 | 1298 | if (frame) frame->Close(); |
9a29912f | 1299 | } |
6c155d33 | 1300 | else if (wxStrcmp(firstArg, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg, _T("ICONIZE")) == 0) |
9a29912f JS |
1301 | { |
1302 | if (frame) | |
b63b07a8 | 1303 | frame->Iconize(true); |
9a29912f | 1304 | } |
6c155d33 | 1305 | else if (wxStrcmp(firstArg, _T("SHOW")) == 0 || wxStrcmp(firstArg, _T("RESTORE")) == 0) |
9a29912f JS |
1306 | { |
1307 | if (frame) | |
1308 | { | |
b63b07a8 RL |
1309 | frame->Iconize(false); |
1310 | frame->Show(true); | |
9a29912f JS |
1311 | } |
1312 | } | |
1313 | else | |
1314 | { | |
1315 | // Try for a setting | |
b63b07a8 | 1316 | wxStrcpy(Tex2RTFLastStatus, RegisterSetting(firstArg, secondArg, false)); |
67a99992 | 1317 | #if !defined(NO_GUI) && wxUSE_STATUSBAR |
6c155d33 | 1318 | if (frame && wxStrcmp(firstArg, _T("conversionMode")) == 0) |
9a29912f | 1319 | { |
6c155d33 JS |
1320 | wxChar buf[100]; |
1321 | wxStrcpy(buf, _T("In ")); | |
9a29912f JS |
1322 | |
1323 | if (winHelp && (convertMode == TEX_RTF)) | |
6c155d33 | 1324 | wxStrcat(buf, _T("WinHelp RTF")); |
9a29912f | 1325 | else if (!winHelp && (convertMode == TEX_RTF)) |
6c155d33 JS |
1326 | wxStrcat(buf, _T("linear RTF")); |
1327 | else if (convertMode == TEX_HTML) wxStrcat(buf, _T("HTML")); | |
1328 | else if (convertMode == TEX_XLP) wxStrcat(buf, _T("XLP")); | |
1329 | wxStrcat(buf, _T(" mode.")); | |
9a29912f JS |
1330 | frame->SetStatusText(buf, 1); |
1331 | } | |
1332 | #endif | |
1333 | } | |
1334 | } | |
b63b07a8 | 1335 | return true; |
9a29912f JS |
1336 | } |
1337 | ||
6c155d33 | 1338 | wxChar *Tex2RTFConnection::OnRequest(const wxString& WXUNUSED(topic), const wxString& WXUNUSED(item), int *WXUNUSED(size), wxIPCFormat WXUNUSED(format)) |
9a29912f JS |
1339 | { |
1340 | return Tex2RTFLastStatus; | |
1341 | } | |
1342 | ||
1343 | #endif | |
1344 | ||
c4c794e1 | 1345 | #ifndef NO_GUI |
17867d61 | 1346 | #ifndef __WXGTK__ |
dd107c50 | 1347 | //void wxObject::Dump(wxSTD ostream& str) |
7cf496bf RS |
1348 | //{ |
1349 | // if (GetClassInfo() && GetClassInfo()->GetClassName()) | |
1350 | // str << GetClassInfo()->GetClassName(); | |
1351 | // else | |
1352 | // str << "unknown object class"; | |
1353 | //} | |
1354 | #endif | |
17867d61 | 1355 | #endif |