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