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