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