]> git.saurik.com Git - wxWidgets.git/blame - utils/tex2rtf/src/tex2rtf.cpp
our build also depends on bc
[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
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
70static inline wxChar* copystring(const wxChar* s)
71 { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); }
72#endif
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
JS
294 buf.Printf(_T("Invalid switch %s.\n"), argv[i]);
295 OnError((wxChar *)buf.c_str());
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
JS
705 errBuf.Printf(_T("\nErrors encountered during this pass: %lu\n"), errorCount);
706 OnInform((wxChar *)errBuf.c_str());
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());
903 frame->SetTitle((wxChar *)buf.c_str());
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
JS
1031 buf.Printf(_T("Working, pass %d...Click CLOSE to abort"), passNumber);
1032 frame->SetStatusText((wxChar *)buf.c_str());
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));
6c155d33 1082 OnInform((wxChar *)buf.c_str());
63ad540d
GT
1083
1084 if (errorCount)
1085 {
6c155d33
JS
1086 buf.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount);
1087 OnInform((wxChar *)buf.c_str());
63ad540d
GT
1088 }
1089
67a99992 1090#if wxUSE_STATUSBAR
9a29912f
JS
1091 if (isInteractive)
1092 {
6c155d33
JS
1093 buf.Printf(_T("Done, %d %s."), passNumber, (passNumber > 1) ? _T("passes") : _T("pass"));
1094 frame->SetStatusText((wxChar *)buf.c_str());
9a29912f 1095 }
67a99992 1096#endif // wxUSE_STATUSBAR
9a29912f 1097#else
6c155d33
JS
1098 buf.Printf(_T("Done, %d %s."), passNumber, (passNumber > 1) ? _T("passes") : _T("pass"));
1099 OnInform((wxChar *)buf.c_str());
63ad540d
GT
1100 if (errorCount)
1101 {
6c155d33
JS
1102 buf.Printf(_T("Errors encountered during this pass: %lu\n"), errorCount);
1103 OnInform((wxChar *)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
6c155d33 1124void OnError(const wxChar *msg)
9a29912f 1125{
878094ac
WS
1126 wxString msg_string = msg;
1127 errorCount++;
63ad540d 1128
9a29912f 1129#ifdef NO_GUI
b63b07a8 1130 wxSTD cerr << "Error: " << msg_string.mb_str() << "\n";
219f4a6d 1131 wxSTD cerr.flush();
878094ac
WS
1132#else
1133 if (isInteractive && frame)
1134 {
1135 (*frame->textWindow) << _T("Error: ") << msg << _T("\n");
1136 }
1137 else
1138 {
1139#if defined(__UNIX__)
1140 wxSTD cerr << "Error: " << msg_string.mb_str() << "\n";
1141 wxSTD cerr.flush();
1142#elif defined(__WXMSW__)
1143 wxLogError(msg);
9a29912f 1144#endif
878094ac 1145 }
63ad540d 1146
878094ac 1147 Tex2RTFYield(true);
9a29912f
JS
1148#endif // NO_GUI
1149}
1150
6c155d33 1151void OnInform(const wxChar *msg)
9a29912f 1152{
878094ac 1153 wxString msg_string = msg;
9a29912f 1154#ifdef NO_GUI
b63b07a8 1155 wxSTD cout << msg_string.mb_str() << "\n";
219f4a6d 1156 wxSTD cout.flush();
878094ac
WS
1157#else
1158 if (isInteractive && frame)
1159 {
1160 (*frame->textWindow) << msg << _T("\n");
1161 }
1162 else
1163 {
1164#if defined(__UNIX__)
1165 wxSTD cout << msg_string.mb_str() << "\n";
1166 wxSTD cout.flush();
1167#elif defined(__WXMSW__)
1168 wxLogInfo(msg);
9a29912f 1169#endif
878094ac
WS
1170 }
1171
1172 if (isInteractive)
1173 {
1174 Tex2RTFYield(true);
1175 }
9a29912f
JS
1176#endif // NO_GUI
1177}
1178
1179void OnMacro(int macroId, int no_args, bool start)
1180{
1181 switch (convertMode)
1182 {
1183 case TEX_RTF:
1184 {
1185 RTFOnMacro(macroId, no_args, start);
1186 break;
1187 }
1188 case TEX_XLP:
1189 {
1190 XLPOnMacro(macroId, no_args, start);
1191 break;
1192 }
1193 case TEX_HTML:
1194 {
1195 HTMLOnMacro(macroId, no_args, start);
1196 break;
1197 }
1198 }
1199}
1200
1201bool OnArgument(int macroId, int arg_no, bool start)
1202{
1203 switch (convertMode)
1204 {
1205 case TEX_RTF:
1206 {
1207 return RTFOnArgument(macroId, arg_no, start);
6c155d33 1208 // break;
9a29912f
JS
1209 }
1210 case TEX_XLP:
1211 {
1212 return XLPOnArgument(macroId, arg_no, start);
6c155d33 1213 // break;
9a29912f
JS
1214 }
1215 case TEX_HTML:
1216 {
1217 return HTMLOnArgument(macroId, arg_no, start);
6c155d33 1218 // break;
9a29912f
JS
1219 }
1220 }
b63b07a8 1221 return true;
9a29912f
JS
1222}
1223
1224/*
1225 * DDE Stuff
1226 */
1227#if defined(__WXMSW__) && !defined(NO_GUI)
1228
1229/*
1230 * Server
1231 */
1232
1233wxConnectionBase *Tex2RTFServer::OnAcceptConnection(const wxString& topic)
1234{
6c155d33 1235 if (topic == _T("TEX2RTF"))
9a29912f
JS
1236 {
1237 if (!ipc_buffer)
6c155d33 1238 ipc_buffer = new wxChar[1000];
ffaaaacb 1239
9a29912f
JS
1240 return new Tex2RTFConnection(ipc_buffer, 4000);
1241 }
1242 else
1243 return NULL;
1244}
1245
1246 /*
1247 * Connection
1248 */
ffaaaacb 1249
6c155d33 1250Tex2RTFConnection::Tex2RTFConnection(wxChar *buf, int size):wxDDEConnection(buf, size)
9a29912f
JS
1251{
1252}
1253
6c155d33 1254bool SplitCommand(wxChar *data, wxChar *firstArg, wxChar *secondArg)
9a29912f
JS
1255{
1256 firstArg[0] = 0;
1257 secondArg[0] = 0;
1258 int i = 0;
b63b07a8 1259 bool stop = false;
9a29912f
JS
1260 // Find first argument (command name)
1261 while (!stop)
1262 {
1263 if (data[i] == ' ' || data[i] == 0)
b63b07a8 1264 stop = true;
9a29912f
JS
1265 else
1266 {
1267 firstArg[i] = data[i];
1268 i ++;
1269 }
1270 }
1271 firstArg[i] = 0;
1272 if (data[i] == ' ')
1273 {
1274 // Find second argument
1275 i ++;
1276 int j = 0;
1277 while (data[i] != 0)
1278 {
1279 secondArg[j] = data[i];
1280 i ++;
1281 j ++;
1282 }
1283 secondArg[j] = 0;
1284 }
b63b07a8 1285 return true;
9a29912f
JS
1286}
1287
6c155d33 1288bool Tex2RTFConnection::OnExecute(const wxString& WXUNUSED(topic), wxChar *data, int WXUNUSED(size), wxIPCFormat WXUNUSED(format))
9a29912f 1289{
6c155d33 1290 wxStrcpy(Tex2RTFLastStatus, _T("OK"));
9a29912f 1291
6c155d33
JS
1292 wxChar firstArg[50];
1293 wxChar secondArg[300];
9a29912f
JS
1294 if (SplitCommand(data, firstArg, secondArg))
1295 {
6c155d33
JS
1296 bool hasArg = (wxStrlen(secondArg) > 0);
1297 if (wxStrcmp(firstArg, _T("INPUT")) == 0 && hasArg)
9a29912f 1298 {
5c66e5b2
WS
1299 InputFile = secondArg;
1300 if (frame)
1301 {
1302 wxChar buf[100];
1303 wxString str = wxFileNameFromPath(InputFile);
1304 wxSnprintf(buf, sizeof(buf), _T("Tex2RTF [%s]"), (const wxChar*) str);
1305 frame->SetTitle(buf);
1306 }
9a29912f 1307 }
6c155d33 1308 else if (wxStrcmp(firstArg, _T("OUTPUT")) == 0 && hasArg)
9a29912f 1309 {
5c66e5b2 1310 OutputFile = secondArg;
9a29912f 1311 }
6c155d33 1312 else if (wxStrcmp(firstArg, _T("GO")) == 0)
9a29912f 1313 {
6c155d33 1314 wxStrcpy(Tex2RTFLastStatus, _T("WORKING"));
9a29912f 1315 if (!Go())
6c155d33 1316 wxStrcpy(Tex2RTFLastStatus, _T("CONVERSION ERROR"));
9a29912f 1317 else
6c155d33 1318 wxStrcpy(Tex2RTFLastStatus, _T("OK"));
9a29912f 1319 }
6c155d33 1320 else if (wxStrcmp(firstArg, _T("EXIT")) == 0)
9a29912f 1321 {
e3065973 1322 if (frame) frame->Close();
9a29912f 1323 }
6c155d33 1324 else if (wxStrcmp(firstArg, _T("MINIMIZE")) == 0 || wxStrcmp(firstArg, _T("ICONIZE")) == 0)
9a29912f
JS
1325 {
1326 if (frame)
b63b07a8 1327 frame->Iconize(true);
9a29912f 1328 }
6c155d33 1329 else if (wxStrcmp(firstArg, _T("SHOW")) == 0 || wxStrcmp(firstArg, _T("RESTORE")) == 0)
9a29912f
JS
1330 {
1331 if (frame)
1332 {
b63b07a8
RL
1333 frame->Iconize(false);
1334 frame->Show(true);
9a29912f
JS
1335 }
1336 }
1337 else
1338 {
1339 // Try for a setting
b63b07a8 1340 wxStrcpy(Tex2RTFLastStatus, RegisterSetting(firstArg, secondArg, false));
67a99992 1341#if !defined(NO_GUI) && wxUSE_STATUSBAR
6c155d33 1342 if (frame && wxStrcmp(firstArg, _T("conversionMode")) == 0)
9a29912f 1343 {
6c155d33
JS
1344 wxChar buf[100];
1345 wxStrcpy(buf, _T("In "));
9a29912f
JS
1346
1347 if (winHelp && (convertMode == TEX_RTF))
6c155d33 1348 wxStrcat(buf, _T("WinHelp RTF"));
9a29912f 1349 else if (!winHelp && (convertMode == TEX_RTF))
6c155d33
JS
1350 wxStrcat(buf, _T("linear RTF"));
1351 else if (convertMode == TEX_HTML) wxStrcat(buf, _T("HTML"));
1352 else if (convertMode == TEX_XLP) wxStrcat(buf, _T("XLP"));
1353 wxStrcat(buf, _T(" mode."));
9a29912f
JS
1354 frame->SetStatusText(buf, 1);
1355 }
1356#endif
1357 }
1358 }
b63b07a8 1359 return true;
9a29912f
JS
1360}
1361
6c155d33 1362wxChar *Tex2RTFConnection::OnRequest(const wxString& WXUNUSED(topic), const wxString& WXUNUSED(item), int *WXUNUSED(size), wxIPCFormat WXUNUSED(format))
9a29912f
JS
1363{
1364 return Tex2RTFLastStatus;
1365}
1366
1367#endif
1368
c4c794e1 1369#ifndef NO_GUI
17867d61 1370#ifndef __WXGTK__
dd107c50 1371//void wxObject::Dump(wxSTD ostream& str)
7cf496bf
RS
1372//{
1373// if (GetClassInfo() && GetClassInfo()->GetClassName())
1374// str << GetClassInfo()->GetClassName();
1375// else
1376// str << "unknown object class";
1377//}
1378#endif
17867d61 1379#endif