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