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