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