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