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