]> git.saurik.com Git - wxWidgets.git/blob - utils/tex2rtf/src/tex2rtf.cpp
jconfig.h uses configures results
[wxWidgets.git] / utils / tex2rtf / src / tex2rtf.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tex2rtf.cpp
3 // Purpose: Converts Latex to linear/WinHelp RTF, HTML, wxHelp.
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 7.9.93
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #ifndef NO_GUI
28 #include <wx/help.h>
29 #include <wx/timer.h>
30 #endif
31
32 #ifdef NO_GUI
33 #if wxUSE_IOSTREAMH
34 #include <iostream.h>
35 #include <fstream.h>
36 #else
37 #include <iostream>
38 #include <fstream>
39 #endif
40 #endif
41
42 #include <ctype.h>
43 #include <stdlib.h>
44 #include "tex2any.h"
45 #include "tex2rtf.h"
46 #include "rtfutils.h"
47
48 #if (defined(__WXGTK__) || defined(__WXMOTIF__)) && !defined(NO_GUI)
49 #include "tex2rtf.xpm"
50 #endif
51
52 const float versionNo = 2.0;
53
54 TexChunk *currentMember = NULL;
55 bool startedSections = FALSE;
56 char *contentsString = NULL;
57 bool suppressNameDecoration = FALSE;
58 bool OkToClose = TRUE;
59 int passNumber = 1;
60
61 #ifndef NO_GUI
62 wxHelpController *HelpInstance = NULL;
63
64 #ifdef __WXMSW__
65 static char *ipc_buffer = NULL;
66 static char Tex2RTFLastStatus[100];
67 Tex2RTFServer *TheTex2RTFServer = NULL;
68 #endif
69 #endif
70
71 char *bulletFile = NULL;
72
73 FILE *Contents = NULL; // Contents page
74 FILE *Chapters = NULL; // Chapters (WinHelp RTF) or rest of file (linear RTF)
75 FILE *Sections = NULL;
76 FILE *Subsections = NULL;
77 FILE *Subsubsections = NULL;
78 FILE *Popups = NULL;
79 FILE *WinHelpContentsFile = NULL;
80
81 char *InputFile = NULL;
82 char *OutputFile = NULL;
83 char *MacroFile = copystring("tex2rtf.ini");
84
85 char *FileRoot = NULL;
86 char *ContentsName = NULL; // Contents page from last time around
87 char *TmpContentsName = NULL; // Current contents page
88 char *TmpFrameContentsName = NULL; // Current frame contents page
89 char *WinHelpContentsFileName = NULL; // WinHelp .cnt file
90 char *RefName = NULL; // Reference file name
91
92 char *RTFCharset = copystring("ansi");
93
94 #ifdef __WXMSW__
95 int BufSize = 100; // Size of buffer in K
96 #else
97 int BufSize = 500;
98 #endif
99
100 bool Go(void);
101 void ShowOptions(void);
102
103 #ifdef NO_GUI
104
105 extern char *wxBuffer; // we must init it, otherwise tex2rtf will crash
106
107 int main(int argc, char **argv)
108 #else
109 wxMenuBar *menuBar = NULL;
110 MyFrame *frame = NULL;
111
112 // DECLARE_APP(MyApp)
113 IMPLEMENT_APP(MyApp)
114
115 // `Main program' equivalent, creating windows and returning main app frame
116 bool MyApp::OnInit()
117 #endif
118 {
119 // Use default list of macros defined in tex2any.cc
120 DefineDefaultMacros();
121 AddMacroDef(ltHARDY, "hardy", 0);
122
123 FileRoot = new char[300];
124 ContentsName = new char[300];
125 TmpContentsName = new char[300];
126 TmpFrameContentsName = new char[300];
127 WinHelpContentsFileName = new char[300];
128 RefName = new char[300];
129
130 int n = 1;
131
132 // Read input/output files
133 if (argc > 1)
134 {
135 if (argv[1][0] != '-')
136 {
137 InputFile = argv[1];
138 n ++;
139
140 if (argc > 2)
141 {
142 if (argv[2][0] != '-')
143 {
144 OutputFile = argv[2];
145 n ++;
146 }
147 }
148 }
149 }
150
151 #ifdef NO_GUI
152 wxBuffer = new char[1500];
153 // this is done in wxApp, but NO_GUI version doesn't call it :-(
154
155 if (!InputFile || !OutputFile)
156 {
157 cout << "Tex2RTF: input or output file is missing.\n";
158 ShowOptions();
159 exit(1);
160 }
161
162 #endif
163 if (InputFile)
164 {
165 TexPathList.EnsureFileAccessible(InputFile);
166 }
167 if (!InputFile || !OutputFile)
168 isInteractive = TRUE;
169
170 for (int i = n; i < argc;)
171 {
172 if (strcmp(argv[i], "-winhelp") == 0)
173 {
174 i ++;
175 convertMode = TEX_RTF;
176 winHelp = TRUE;
177 }
178 #ifndef NO_GUI
179 else if (strcmp(argv[i], "-interactive") == 0)
180 {
181 i ++;
182 isInteractive = TRUE;
183 }
184 #endif
185 else if (strcmp(argv[i], "-sync") == 0) // Don't yield
186 {
187 i ++;
188 isSync = TRUE;
189 }
190 else if (strcmp(argv[i], "-rtf") == 0)
191 {
192 i ++;
193 convertMode = TEX_RTF;
194 }
195 else if (strcmp(argv[i], "-html") == 0)
196 {
197 i ++;
198 convertMode = TEX_HTML;
199 }
200 else if (strcmp(argv[i], "-xlp") == 0)
201 {
202 i ++;
203 convertMode = TEX_XLP;
204 }
205 else if (strcmp(argv[i], "-twice") == 0)
206 {
207 i ++;
208 runTwice = TRUE;
209 }
210 else if (strcmp(argv[i], "-macros") == 0)
211 {
212 i ++;
213 if (i < argc)
214 {
215 MacroFile = copystring(argv[i]);
216 i ++;
217 }
218 }
219 else if (strcmp(argv[i], "-bufsize") == 0)
220 {
221 i ++;
222 if (i < argc)
223 {
224 BufSize = atoi(argv[i]);
225 i ++;
226 }
227 }
228 else if (strcmp(argv[i], "-charset") == 0)
229 {
230 i ++;
231 if (i < argc)
232 {
233 char *s = argv[i];
234 i ++;
235 if (strcmp(s, "ansi") == 0 || strcmp(s, "pc") == 0 || strcmp(s, "mac") == 0 ||
236 strcmp(s, "pca") == 0)
237 RTFCharset = copystring(s);
238 else
239 {
240 OnError("Incorrect argument for -charset");
241 }
242 }
243 }
244 else
245 {
246 char buf[100];
247 sprintf(buf, "Invalid switch %s.\n", argv[i]);
248 OnError(buf);
249 i++;
250 #ifdef NO_GUI
251 ShowOptions();
252 exit(1);
253 #endif
254 }
255 }
256
257 #if defined(__WXMSW__) && !defined(NO_GUI)
258 wxDDEInitialize();
259 Tex2RTFLastStatus[0] = 0; // DDE connection return value
260 TheTex2RTFServer = new Tex2RTFServer;
261 TheTex2RTFServer->Create("TEX2RTF");
262 #endif
263
264 #if defined(__WXMSW__) && defined(__WIN16__)
265 // Limit to max Windows array size
266 if (BufSize > 64) BufSize = 64;
267 #endif
268
269 TexInitialize(BufSize);
270 ResetContentsLevels(0);
271
272 #ifndef NO_GUI
273
274 if (isInteractive)
275 {
276 char buf[100];
277
278 // Create the main frame window
279 frame = new MyFrame(NULL, -1, "Tex2RTF", wxPoint(-1, -1), wxSize(400, 300));
280 frame->CreateStatusBar(2);
281
282 // Give it an icon
283 // TODO: uncomment this when we have tex2rtf.xpm
284 frame->SetIcon(wxICON(tex2rtf));
285
286 if (InputFile)
287 {
288 sprintf(buf, "Tex2RTF [%s]", FileNameFromPath(InputFile));
289 frame->SetTitle(buf);
290 }
291
292 // Make a menubar
293 wxMenu *file_menu = new wxMenu;
294 file_menu->Append(TEX_GO, "&Go", "Run converter");
295 file_menu->Append(TEX_SET_INPUT, "Set &Input File", "Set the LaTeX input file");
296 file_menu->Append(TEX_SET_OUTPUT, "Set &Output File", "Set the output file");
297 file_menu->AppendSeparator();
298 file_menu->Append(TEX_VIEW_LATEX, "View &LaTeX File", "View the LaTeX input file");
299 file_menu->Append(TEX_VIEW_OUTPUT, "View Output &File", "View output file");
300 file_menu->Append(TEX_SAVE_FILE, "&Save log file", "Save displayed text into file");
301 file_menu->AppendSeparator();
302 file_menu->Append(TEX_QUIT, "E&xit", "Exit Tex2RTF");
303
304 wxMenu *macro_menu = new wxMenu;
305
306 macro_menu->Append(TEX_LOAD_CUSTOM_MACROS, "&Load Custom Macros", "Load custom LaTeX macro file");
307 macro_menu->Append(TEX_VIEW_CUSTOM_MACROS, "View &Custom Macros", "View custom LaTeX macros");
308
309 wxMenu *mode_menu = new wxMenu;
310
311 mode_menu->Append(TEX_MODE_RTF, "Output linear &RTF", "Wordprocessor-compatible RTF");
312 mode_menu->Append(TEX_MODE_WINHELP, "Output &WinHelp RTF", "WinHelp-compatible RTF");
313 mode_menu->Append(TEX_MODE_HTML, "Output &HTML", "HTML World Wide Web hypertext file");
314 mode_menu->Append(TEX_MODE_XLP, "Output &XLP", "wxHelp hypertext help file");
315
316 wxMenu *help_menu = new wxMenu;
317
318 help_menu->Append(TEX_HELP, "&Help", "Tex2RTF Contents Page");
319 help_menu->Append(TEX_ABOUT, "&About Tex2RTF", "About Tex2RTF");
320
321 menuBar = new wxMenuBar;
322 menuBar->Append(file_menu, "&File");
323 menuBar->Append(macro_menu, "&Macros");
324 menuBar->Append(mode_menu, "&Conversion Mode");
325 menuBar->Append(help_menu, "&Help");
326
327 frame->SetMenuBar(menuBar);
328 frame->textWindow = new wxTextCtrl(frame, -1, "", wxPoint(-1, -1), wxSize(-1, -1), wxTE_READONLY|wxTE_MULTILINE);
329
330 (*frame->textWindow) << "Welcome to Julian Smart's LaTeX to RTF converter.\n";
331 // ShowOptions();
332
333 HelpInstance = new wxHelpController();
334 HelpInstance->Initialize("tex2rtf");
335
336 /*
337 * Read macro/initialisation file
338 *
339 */
340
341 wxString path;
342 if ((path = TexPathList.FindValidPath(MacroFile)) != "")
343 ReadCustomMacros((char*) (const char*) path);
344
345 strcpy(buf, "In ");
346
347 if (winHelp && (convertMode == TEX_RTF))
348 strcat(buf, "WinHelp RTF");
349 else if (!winHelp && (convertMode == TEX_RTF))
350 strcat(buf, "linear RTF");
351 else if (convertMode == TEX_HTML) strcat(buf, "HTML");
352 else if (convertMode == TEX_XLP) strcat(buf, "XLP");
353 strcat(buf, " mode.");
354 frame->SetStatusText(buf, 1);
355
356 frame->Show(TRUE);
357 return TRUE;
358 }
359 else
360 #endif // NO_GUI
361 {
362 /*
363 * Read macro/initialisation file
364 *
365 */
366
367 wxString path;
368 if ((path = TexPathList.FindValidPath(MacroFile)) != "")
369 ReadCustomMacros((char*) (const char*) path);
370
371 Go();
372 if (runTwice) Go();
373 #ifdef NO_GUI
374 return 0;
375 #else
376 return NULL;
377 #endif
378 }
379
380 #ifndef NO_GUI
381 // Return the main frame window
382 return TRUE;
383 #else
384 delete[] wxBuffer;
385 return FALSE;
386 #endif
387 }
388
389 #ifndef NO_GUI
390 int MyApp::OnExit()
391 {
392 wxNode *node = CustomMacroList.First();
393 while (node)
394 {
395 CustomMacro *macro = (CustomMacro *)node->Data();
396 delete macro;
397 delete node;
398 node = CustomMacroList.First();
399 }
400 MacroDefs.BeginFind();
401 node = MacroDefs.Next();
402 while (node)
403 {
404 TexMacroDef* def = (TexMacroDef*) node->Data();
405 delete def;
406 node = MacroDefs.Next();
407 }
408 MacroDefs.Clear();
409 #ifdef __WXMSW__
410 delete TheTex2RTFServer;
411 wxDDECleanUp();
412 #endif
413 delete HelpInstance;
414
415 // TODO: this simulates zero-memory leaks!
416 // Otherwise there are just too many...
417 #ifndef __WXGTK__
418 wxDebugContext::SetCheckpoint();
419 #endif
420
421 return 0;
422 }
423 #endif
424 void ShowOptions(void)
425 {
426 char buf[100];
427 sprintf(buf, "Tex2RTF version %.2f", versionNo);
428 OnInform(buf);
429 OnInform("Usage: tex2rtf [input] [output] [switches]\n");
430 OnInform("where valid switches are");
431 OnInform(" -interactive");
432 OnInform(" -bufsize <size in K>");
433 OnInform(" -charset <pc | pca | ansi | mac> (default ansi)");
434 OnInform(" -twice");
435 OnInform(" -sync");
436 OnInform(" -macros <filename>");
437 OnInform(" -winhelp");
438 OnInform(" -rtf");
439 OnInform(" -html");
440 OnInform(" -xlp\n");
441 }
442
443 #ifndef NO_GUI
444
445 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
446 EVT_CLOSE(MyFrame::OnCloseWindow)
447 EVT_MENU(TEX_QUIT, MyFrame::OnExit)
448 EVT_MENU(TEX_GO, MyFrame::OnGo)
449 EVT_MENU(TEX_SET_INPUT, MyFrame::OnSetInput)
450 EVT_MENU(TEX_SET_OUTPUT, MyFrame::OnSetOutput)
451 EVT_MENU(TEX_SAVE_FILE, MyFrame::OnSaveFile)
452 EVT_MENU(TEX_VIEW_LATEX, MyFrame::OnViewLatex)
453 EVT_MENU(TEX_VIEW_OUTPUT, MyFrame::OnViewOutput)
454 EVT_MENU(TEX_VIEW_CUSTOM_MACROS, MyFrame::OnShowMacros)
455 EVT_MENU(TEX_LOAD_CUSTOM_MACROS, MyFrame::OnLoadMacros)
456 EVT_MENU(TEX_MODE_RTF, MyFrame::OnModeRTF)
457 EVT_MENU(TEX_MODE_WINHELP, MyFrame::OnModeWinHelp)
458 EVT_MENU(TEX_MODE_HTML, MyFrame::OnModeHTML)
459 EVT_MENU(TEX_MODE_XLP, MyFrame::OnModeXLP)
460 EVT_MENU(TEX_HELP, MyFrame::OnHelp)
461 EVT_MENU(TEX_ABOUT, MyFrame::OnAbout)
462 END_EVENT_TABLE()
463
464 // My frame constructor
465 MyFrame::MyFrame(wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size):
466 wxFrame(frame, id, title, pos, size)
467 {}
468
469 void MyFrame::OnCloseWindow(wxCloseEvent& event)
470 {
471 if (!stopRunning && !OkToClose)
472 {
473 stopRunning = TRUE;
474 runTwice = FALSE;
475 return;
476 }
477 else if (OkToClose)
478 {
479 this->Destroy();
480 }
481 }
482
483 void MyFrame::OnExit(wxCommandEvent& event)
484 {
485 this->Destroy();
486 }
487
488 void MyFrame::OnGo(wxCommandEvent& event)
489 {
490 menuBar->EnableTop(0, FALSE);
491 menuBar->EnableTop(1, FALSE);
492 menuBar->EnableTop(2, FALSE);
493 menuBar->EnableTop(3, FALSE);
494 textWindow->Clear();
495 Tex2RTFYield(TRUE);
496 Go();
497
498 if (runTwice)
499 {
500 Tex2RTFYield(TRUE);
501 Go();
502 }
503 menuBar->EnableTop(0, TRUE);
504 menuBar->EnableTop(1, TRUE);
505 menuBar->EnableTop(2, TRUE);
506 menuBar->EnableTop(3, TRUE);
507 }
508
509 void MyFrame::OnSetInput(wxCommandEvent& event)
510 {
511 ChooseInputFile(TRUE);
512 }
513
514 void MyFrame::OnSetOutput(wxCommandEvent& event)
515 {
516 ChooseOutputFile(TRUE);
517 }
518
519 void MyFrame::OnSaveFile(wxCommandEvent& event)
520 {
521 wxString s = wxFileSelector("Save text to file", "", "", "txt", "*.txt");
522 if (s != "")
523 {
524 textWindow->SaveFile(s);
525 char buf[350];
526 sprintf(buf, "Saved text to %s", (const char*) s);
527 frame->SetStatusText(buf, 0);
528 }
529 }
530
531 void MyFrame::OnViewOutput(wxCommandEvent& event)
532 {
533 ChooseOutputFile();
534 if (OutputFile && wxFileExists(OutputFile))
535 {
536 textWindow->LoadFile(OutputFile);
537 char buf[300];
538 wxString str(wxFileNameFromPath(OutputFile));
539 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
540 frame->SetTitle(buf);
541 }
542 }
543
544 void MyFrame::OnViewLatex(wxCommandEvent& event)
545 {
546 ChooseInputFile();
547 if (InputFile && wxFileExists(InputFile))
548 {
549 textWindow->LoadFile(InputFile);
550 char buf[300];
551 wxString str(wxFileNameFromPath(OutputFile));
552 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
553 frame->SetTitle(buf);
554 }
555 }
556
557 void MyFrame::OnLoadMacros(wxCommandEvent& event)
558 {
559 textWindow->Clear();
560 wxString s = wxFileSelector("Choose custom macro file", wxPathOnly(MacroFile), wxFileNameFromPath(MacroFile), "ini", "*.ini");
561 if (s != "" && wxFileExists(s))
562 {
563 MacroFile = copystring(s);
564 ReadCustomMacros((char*) (const char*) s);
565 ShowCustomMacros();
566 }
567 }
568
569 void MyFrame::OnShowMacros(wxCommandEvent& event)
570 {
571 textWindow->Clear();
572 Tex2RTFYield(TRUE);
573 ShowCustomMacros();
574 }
575
576 void MyFrame::OnModeRTF(wxCommandEvent& event)
577 {
578 convertMode = TEX_RTF;
579 winHelp = FALSE;
580 InputFile = NULL;
581 OutputFile = NULL;
582 SetStatusText("In linear RTF mode.", 1);
583 }
584
585 void MyFrame::OnModeWinHelp(wxCommandEvent& event)
586 {
587 convertMode = TEX_RTF;
588 winHelp = TRUE;
589 InputFile = NULL;
590 OutputFile = NULL;
591 SetStatusText("In WinHelp RTF mode.", 1);
592 }
593
594 void MyFrame::OnModeHTML(wxCommandEvent& event)
595 {
596 convertMode = TEX_HTML;
597 winHelp = FALSE;
598 InputFile = NULL;
599 OutputFile = NULL;
600 SetStatusText("In HTML mode.", 1);
601 }
602
603 void MyFrame::OnModeXLP(wxCommandEvent& event)
604 {
605 convertMode = TEX_XLP;
606 InputFile = NULL;
607 OutputFile = NULL;
608 SetStatusText("In XLP mode.", 1);
609 }
610
611 void MyFrame::OnHelp(wxCommandEvent& event)
612 {
613 HelpInstance->LoadFile();
614 HelpInstance->DisplayContents();
615 }
616
617 void MyFrame::OnAbout(wxCommandEvent& event)
618 {
619 char buf[300];
620 #ifdef __WIN32__
621 char *platform = " (32-bit)";
622 #else
623 #ifdef __WXMSW__
624 char *platform = " (16-bit)";
625 #else
626 char *platform = "";
627 #endif
628 #endif
629 sprintf(buf, "Tex2RTF Version %.2f%s\nLaTeX to RTF, WinHelp, HTML and wxHelp Conversion\n\n(c) Julian Smart 1999", versionNo, platform);
630 wxMessageBox(buf, "About Tex2RTF");
631 }
632
633 void ChooseInputFile(bool force)
634 {
635 if (force || !InputFile)
636 {
637 wxString s = wxFileSelector("Choose LaTeX input file", wxPathOnly(InputFile), wxFileNameFromPath(InputFile), "tex", "*.tex");
638 if (s != "")
639 {
640 // Different file, so clear index entries.
641 ClearKeyWordTable();
642 ResetContentsLevels(0);
643 passNumber = 1;
644 char buf[300];
645 InputFile = copystring(s);
646 wxString str = wxFileNameFromPath(InputFile);
647 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
648 frame->SetTitle(buf);
649 OutputFile = NULL;
650 }
651 }
652 }
653
654 void ChooseOutputFile(bool force)
655 {
656 char extensionBuf[10];
657 char wildBuf[10];
658 strcpy(wildBuf, "*.");
659 wxString path;
660 if (OutputFile)
661 path = wxPathOnly(OutputFile);
662 else if (InputFile)
663 path = wxPathOnly(InputFile);
664
665 switch (convertMode)
666 {
667 case TEX_RTF:
668 {
669 strcpy(extensionBuf, "rtf");
670 strcat(wildBuf, "rtf");
671 break;
672 }
673 case TEX_XLP:
674 {
675 strcpy(extensionBuf, "xlp");
676 strcat(wildBuf, "xlp");
677 break;
678 }
679 case TEX_HTML:
680 {
681 #if defined(__WXMSW__) && defined(__WIN16__)
682 strcpy(extensionBuf, "htm");
683 strcat(wildBuf, "htm");
684 #else
685 strcpy(extensionBuf, "html");
686 strcat(wildBuf, "html");
687 #endif
688 break;
689 }
690 }
691 if (force || !OutputFile)
692 {
693 wxString s = wxFileSelector("Choose output file", path, wxFileNameFromPath(OutputFile),
694 extensionBuf, wildBuf);
695 if (s != "")
696 OutputFile = copystring(s);
697 }
698 }
699 #endif
700
701 bool Go(void)
702 {
703 #ifndef NO_GUI
704 ChooseInputFile();
705 ChooseOutputFile();
706 #endif
707
708 if (!InputFile || !OutputFile)
709 return FALSE;
710
711 #ifndef NO_GUI
712 if (isInteractive)
713 {
714 char buf[300];
715 wxString str = wxFileNameFromPath(InputFile);
716
717 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
718 frame->SetTitle(buf);
719 }
720
721 wxStartTimer();
722 #endif
723
724 // Find extension-less filename
725 strcpy(FileRoot, OutputFile);
726 StripExtension(FileRoot);
727
728 if (truncateFilenames && convertMode == TEX_HTML)
729 {
730 // Truncate to five characters. This ensures that
731 // we can generate DOS filenames such as thing999. But 1000 files
732 // may not be enough, of course...
733 char* sName = wxFileNameFromPath( FileRoot); // this Julian's method is non-destructive reference
734
735 if(sName)
736 if(strlen( sName) > 5)
737 sName[5] = '\0'; // that should do!
738 }
739
740 sprintf(ContentsName, "%s.con", FileRoot);
741 sprintf(TmpContentsName, "%s.cn1", FileRoot);
742 sprintf(TmpFrameContentsName, "%s.frc", FileRoot);
743 sprintf(WinHelpContentsFileName, "%s.cnt", FileRoot);
744 sprintf(RefName, "%s.ref", FileRoot);
745
746 TexPathList.EnsureFileAccessible(InputFile);
747 if (!bulletFile)
748 {
749 wxString s = TexPathList.FindValidPath("bullet.bmp");
750 if (s != "")
751 {
752 wxString str = wxFileNameFromPath(s);
753 bulletFile = copystring(str);
754 }
755 }
756
757 if (wxFileExists(RefName))
758 ReadTexReferences(RefName);
759
760 bool success = FALSE;
761
762 if (InputFile && OutputFile)
763 {
764 if (!FileExists(InputFile))
765 {
766 OnError("Cannot open input file!");
767 TexCleanUp();
768 return FALSE;
769 }
770 #ifndef NO_GUI
771 if (isInteractive)
772 {
773 char buf[50];
774 sprintf(buf, "Working, pass %d...", passNumber);
775 frame->SetStatusText(buf);
776 }
777 #endif
778 OkToClose = FALSE;
779 OnInform("Reading LaTeX file...");
780 TexLoadFile(InputFile);
781
782 switch (convertMode)
783 {
784 case TEX_RTF:
785 {
786 success = RTFGo();
787 break;
788 }
789 case TEX_XLP:
790 {
791 success = XLPGo();
792 break;
793 }
794 case TEX_HTML:
795 {
796 success = HTMLGo();
797 break;
798 }
799 }
800 }
801 if (stopRunning)
802 {
803 OnInform("*** Aborted by user.");
804 success = FALSE;
805 stopRunning = FALSE;
806 }
807
808 if (success)
809 {
810 WriteTexReferences(RefName);
811 TexCleanUp();
812 startedSections = FALSE;
813
814 char buf[100];
815 #ifndef NO_GUI
816 long tim = wxGetElapsedTime();
817 sprintf(buf, "Finished in %ld seconds.", (long)(tim/1000.0));
818 OnInform(buf);
819 if (isInteractive)
820 {
821 sprintf(buf, "Done, %d %s.", passNumber, (passNumber > 1) ? "passes" : "pass");
822 frame->SetStatusText(buf);
823 }
824 #else
825 sprintf(buf, "Done, %d %s.", passNumber, (passNumber > 1) ? "passes" : "pass");
826 OnInform(buf);
827 #endif
828 passNumber ++;
829 OkToClose = TRUE;
830 return TRUE;
831 }
832
833 TexCleanUp();
834 startedSections = FALSE;
835
836 OnInform("Sorry, unsuccessful.");
837 OkToClose = TRUE;
838 return FALSE;
839 }
840
841 void OnError(char *msg)
842 {
843 #ifdef NO_GUI
844 cerr << "Error: " << msg << "\n";
845 cerr.flush();
846 #else
847 if (isInteractive)
848 (*frame->textWindow) << "Error: " << msg << "\n";
849 else
850 #ifdef __UNIX__
851 {
852 cerr << "Error: " << msg << "\n";
853 cerr.flush();
854 }
855 #endif
856 #ifdef __WXMSW__
857 wxError(msg);
858 #endif
859 Tex2RTFYield(TRUE);
860 #endif // NO_GUI
861 }
862
863 void OnInform(char *msg)
864 {
865 #ifdef NO_GUI
866 cout << msg << "\n";
867 cout.flush();
868 #else
869 if (isInteractive)
870 (*frame->textWindow) << msg << "\n";
871 else
872 #ifdef __WXMSW__
873 {
874 cout << msg << "\n";
875 cout.flush();
876 }
877 #endif
878 #ifdef __WXMSW__
879 {}
880 #endif
881 if (isInteractive)
882 {
883 Tex2RTFYield(TRUE);
884 }
885 #endif // NO_GUI
886 }
887
888 void OnMacro(int macroId, int no_args, bool start)
889 {
890 switch (convertMode)
891 {
892 case TEX_RTF:
893 {
894 RTFOnMacro(macroId, no_args, start);
895 break;
896 }
897 case TEX_XLP:
898 {
899 XLPOnMacro(macroId, no_args, start);
900 break;
901 }
902 case TEX_HTML:
903 {
904 HTMLOnMacro(macroId, no_args, start);
905 break;
906 }
907 }
908 }
909
910 bool OnArgument(int macroId, int arg_no, bool start)
911 {
912 switch (convertMode)
913 {
914 case TEX_RTF:
915 {
916 return RTFOnArgument(macroId, arg_no, start);
917 break;
918 }
919 case TEX_XLP:
920 {
921 return XLPOnArgument(macroId, arg_no, start);
922 break;
923 }
924 case TEX_HTML:
925 {
926 return HTMLOnArgument(macroId, arg_no, start);
927 break;
928 }
929 }
930 return TRUE;
931 }
932
933 /*
934 * DDE Stuff
935 */
936 #if defined(__WXMSW__) && !defined(NO_GUI)
937
938 /*
939 * Server
940 */
941
942 wxConnectionBase *Tex2RTFServer::OnAcceptConnection(const wxString& topic)
943 {
944 if (topic == "TEX2RTF")
945 {
946 if (!ipc_buffer)
947 ipc_buffer = new char[1000];
948
949 return new Tex2RTFConnection(ipc_buffer, 4000);
950 }
951 else
952 return NULL;
953 }
954
955 /*
956 * Connection
957 */
958
959 Tex2RTFConnection::Tex2RTFConnection(char *buf, int size):wxDDEConnection(buf, size)
960 {
961 }
962
963 Tex2RTFConnection::~Tex2RTFConnection(void)
964 {
965 }
966
967 bool SplitCommand(char *data, char *firstArg, char *secondArg)
968 {
969 firstArg[0] = 0;
970 secondArg[0] = 0;
971 int i = 0;
972 int len = strlen(data);
973 bool stop = FALSE;
974 // Find first argument (command name)
975 while (!stop)
976 {
977 if (data[i] == ' ' || data[i] == 0)
978 stop = TRUE;
979 else
980 {
981 firstArg[i] = data[i];
982 i ++;
983 }
984 }
985 firstArg[i] = 0;
986 if (data[i] == ' ')
987 {
988 // Find second argument
989 i ++;
990 int j = 0;
991 while (data[i] != 0)
992 {
993 secondArg[j] = data[i];
994 i ++;
995 j ++;
996 }
997 secondArg[j] = 0;
998 }
999 return TRUE;
1000 }
1001
1002 bool Tex2RTFConnection::OnExecute(const wxString& topic, char *data, int size, int format)
1003 {
1004 strcpy(Tex2RTFLastStatus, "OK");
1005
1006 char firstArg[50];
1007 char secondArg[300];
1008 if (SplitCommand(data, firstArg, secondArg))
1009 {
1010 bool hasArg = (strlen(secondArg) > 0);
1011 if (strcmp(firstArg, "INPUT") == 0 && hasArg)
1012 {
1013 if (InputFile) delete[] InputFile;
1014 InputFile = copystring(secondArg);
1015 if (frame)
1016 {
1017 char buf[100];
1018 wxString str = wxFileNameFromPath(InputFile);
1019 sprintf(buf, "Tex2RTF [%s]", (const char*) str);
1020 frame->SetTitle(buf);
1021 }
1022 }
1023 else if (strcmp(firstArg, "OUTPUT") == 0 && hasArg)
1024 {
1025 if (OutputFile) delete[] OutputFile;
1026 OutputFile = copystring(secondArg);
1027 }
1028 else if (strcmp(firstArg, "GO") == 0)
1029 {
1030 strcpy(Tex2RTFLastStatus, "WORKING");
1031 if (!Go())
1032 strcpy(Tex2RTFLastStatus, "CONVERSION ERROR");
1033 else
1034 strcpy(Tex2RTFLastStatus, "OK");
1035 }
1036 else if (strcmp(firstArg, "EXIT") == 0)
1037 {
1038 if (frame) frame->Close();
1039 }
1040 else if (strcmp(firstArg, "MINIMIZE") == 0 || strcmp(firstArg, "ICONIZE") == 0)
1041 {
1042 if (frame)
1043 frame->Iconize(TRUE);
1044 }
1045 else if (strcmp(firstArg, "SHOW") == 0 || strcmp(firstArg, "RESTORE") == 0)
1046 {
1047 if (frame)
1048 {
1049 frame->Iconize(FALSE);
1050 frame->Show(TRUE);
1051 }
1052 }
1053 else
1054 {
1055 // Try for a setting
1056 strcpy(Tex2RTFLastStatus, RegisterSetting(firstArg, secondArg, FALSE));
1057 #ifndef NO_GUI
1058 if (frame && strcmp(firstArg, "conversionMode") == 0)
1059 {
1060 char buf[100];
1061 strcpy(buf, "In ");
1062
1063 if (winHelp && (convertMode == TEX_RTF))
1064 strcat(buf, "WinHelp RTF");
1065 else if (!winHelp && (convertMode == TEX_RTF))
1066 strcat(buf, "linear RTF");
1067 else if (convertMode == TEX_HTML) strcat(buf, "HTML");
1068 else if (convertMode == TEX_XLP) strcat(buf, "XLP");
1069 strcat(buf, " mode.");
1070 frame->SetStatusText(buf, 1);
1071 }
1072 #endif
1073 }
1074 }
1075 return TRUE;
1076 }
1077
1078 char *Tex2RTFConnection::OnRequest(const wxString& topic, const wxString& item, int *size, int format)
1079 {
1080 return Tex2RTFLastStatus;
1081 }
1082
1083 #endif
1084
1085 #ifndef NO_GUI
1086 #ifndef __WXGTK__
1087 //void wxObject::Dump(ostream& str)
1088 //{
1089 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1090 // str << GetClassInfo()->GetClassName();
1091 // else
1092 // str << "unknown object class";
1093 //}
1094 #endif
1095 #endif