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