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