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