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