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