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