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