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