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