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