Debian Codegraph @

sys/ManPage.cpp

raw
1 /* ManPage.cpp
2 *
3 * Copyright (C) 1996-2011,2016,2023-2026 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "ManPage.h"
20 #include "praat.h"
21
22 #include "enums_getText.h"
23 #include "ManPage_enums.h"
24 #include "enums_getValue.h"
25 #include "ManPage_enums.h"
26
27 Thing_implement (ManPage, Thing, 0);
28
29 static autoMelderString *manualInfoProc_string;
30 static void manualInfoProc (conststring32 infoText) {
31 MelderString_copy (manualInfoProc_string, infoText);
32 // FIXME: this overrides a growing info buffer, which is an O(N^2) algorithm if in a loop
33 }
34
35 void ManPage_runAllChunksToCache (ManPage me, Interpreter optionalInterpreterReference,
36 const kGraphics_font font, const double fontSize,
37 PraatApplication praatApplication, PraatObjects praatObjects, PraatPicture praatPicture,
38 MelderFolder rootDirectory
39 ) {
40 theCurrentPraatApplication = praatApplication;
41 theCurrentPraatApplication -> batch = true; // prevent creation of editor windows
42 theCurrentPraatApplication -> topShell = theForegroundPraatApplication. topShell; // needed for UiForm_create () in dialogs
43 theCurrentPraatObjects = praatObjects;
44 Melder_assert (praatObjects -> n == 0);
45 Melder_assert (praatObjects -> totalSelection == 0);
46 Melder_assert (praatObjects -> totalBeingCreated == 0);
47 praatObjects -> sequentialUniqueIdOfLatestObjectInList = 0;
48 theCurrentPraatPicture = praatPicture;
49
50 void praat_actions_show (); // TODO: integrate this better
51 praat_actions_show (); // we have to set the `executable` flags to false, in the global variable `theActions`
52
53 #if 1
54 autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); // BUG: overwrites existing stack, which is optionalInterpreterReference's owner
55 interpreterStack -> interpreters [1] = Interpreter_createFromEnvironment (interpreterStack.get(), Editor (nullptr), MelderFile (nullptr));
56 #else
57 Interpreter interpreterReference;
58 autoInterpreterStack interpreterStack;
59 if (optionalInterpreterReference) {
60 interpreterReference = optionalInterpreterReference;
61 } else {
62 interpreterStack = InterpreterStack_create (Editor (nullptr));
63 interpreterStack -> interpreters [1] = Interpreter_createFromEnvironment (interpreterStack.get(), Editor (nullptr));
64 interpreterReference = interpreterStack -> interpreters [1].get();
65 }
66 #endif
67
68 /*
69 When this page is drawn for the first time,
70 all the script parts have to be run,
71 so that the outputs of drawing and info can be cached.
72 */
73 integer chunkNumber = 0;
74 bool anErrorHasOccurred = false;
75 autostring32 theErrorThatOccurred;
76 integer errorChunk = 0;
77 for (integer ipar = 1; ipar <= my paragraphs.size; ipar ++) {
78 ManPage_Paragraph paragraph = & my paragraphs [ipar];
79 if (paragraph -> type != kManPage_type::SCRIPT)
80 continue;
81 chunkNumber += 1;
82 if (paragraph -> cacheGraphics)
83 break; // don't run the chunks again
84 /*
85 Divert info text from Info window to Manual window.
86 */
87 autoMelderSetInformationProc divert (manualInfoProc);
88 manualInfoProc_string = & paragraph -> cacheInfo;
89 MelderInfo_open ();
90 /*
91 Divert graphics from Picture window to Manual window.
92 */
93 paragraph -> cacheGraphics = Graphics_create_screen (nullptr, nullptr, 100);
94 Graphics_startRecording (paragraph -> cacheGraphics.get());
95 Graphics_setFont (paragraph -> cacheGraphics.get(), font);
96 Graphics_setFontStyle (paragraph -> cacheGraphics.get(), 0);
97 Graphics_setFontSize (paragraph -> cacheGraphics.get(), fontSize);
98 const double true_width_inches = paragraph -> width * ( paragraph -> width < 0.0 ? -1.0 : fontSize / 12.0 );
99 const double true_height_inches = paragraph -> height * ( paragraph -> height < 0.0 ? -1.0 : fontSize / 12.0 );
100 Graphics_setWrapWidth (paragraph -> cacheGraphics.get(), 0.0);
101 integer x1DCold, x2DCold, y1DCold, y2DCold;
102 Graphics_inqWsViewport (paragraph -> cacheGraphics.get(), & x1DCold, & x2DCold, & y1DCold, & y2DCold);
103 double x1NDCold, x2NDCold, y1NDCold, y2NDCold;
104 Graphics_inqWsWindow (paragraph -> cacheGraphics.get(), & x1NDCold, & x2NDCold, & y1NDCold, & y2NDCold);
105
106 theCurrentPraatPicture -> graphics = paragraph -> cacheGraphics.get(); // has to draw into HyperPage rather than Picture window
107 theCurrentPraatPicture -> font = font;
108 theCurrentPraatPicture -> fontSize = fontSize;
109 theCurrentPraatPicture -> lineType = Graphics_DRAWN;
110 theCurrentPraatPicture -> lineWidth = 1.0;
111 theCurrentPraatPicture -> arrowSize = 1.0;
112 theCurrentPraatPicture -> speckleSize = 1.0;
113 theCurrentPraatPicture -> colour = Melder_BLACK;
114 #if 1
115 theCurrentPraatPicture -> x1NDC = 0.0;
116 theCurrentPraatPicture -> x2NDC = true_width_inches;
117 theCurrentPraatPicture -> y1NDC = 0.0;
118 theCurrentPraatPicture -> y2NDC = true_height_inches;
119
120 Graphics_setViewport (paragraph -> cacheGraphics.get(),
121 theCurrentPraatPicture -> x1NDC, theCurrentPraatPicture -> x2NDC, theCurrentPraatPicture -> y1NDC, theCurrentPraatPicture -> y2NDC);
122 #endif
123 Graphics_setWindow (paragraph -> cacheGraphics.get(), 0.0, 1.0, 0.0, 1.0);
124 #if 1
125 integer x1DC, y1DC, x2DC, y2DC;
126 Graphics_WCtoDC (paragraph -> cacheGraphics.get(), 0.0, 0.0, & x1DC, & y2DC);
127 Graphics_WCtoDC (paragraph -> cacheGraphics.get(), 1.0, 1.0, & x2DC, & y1DC);
128 Graphics_resetWsViewport (paragraph -> cacheGraphics.get(), x1DC, x2DC, y1DC, y2DC);
129 Graphics_setWsWindow (paragraph -> cacheGraphics.get(), 0, paragraph -> width, 0, paragraph -> height);
130 #endif
131 theCurrentPraatPicture -> x1NDC = 0.0;
132 theCurrentPraatPicture -> x2NDC = paragraph -> width;
133 theCurrentPraatPicture -> y1NDC = 0.0;
134 theCurrentPraatPicture -> y2NDC = paragraph -> height;
135 Graphics_setViewport (paragraph -> cacheGraphics.get(),
136 theCurrentPraatPicture -> x1NDC, theCurrentPraatPicture -> x2NDC, theCurrentPraatPicture -> y1NDC, theCurrentPraatPicture -> y2NDC);
137 if (anErrorHasOccurred) {
138 trace (U"Chunk ", chunkNumber, U" not run, because of an earlier error.");
139 MelderInfo_writeLine (U"\\#{**ERROR** This code chunk was not run,}\n because an error occurred in an earlier chunk.");
140 MelderInfo_close ();
141 } else {
142 autoMelderProgressOff progress;
143 autoMelderWarningOff nowarn;
144 autoMelderSaveCurrentFolder saveFolder;
145 if (! MelderFolder_isNull (rootDirectory))
146 Melder_setCurrentFolder (rootDirectory);
147 try {
148 autoMelderString program;
149 MelderString_append (& program, paragraph -> text);
150 for (integer jpar = 1; jpar <= my paragraphs.size; jpar ++) if (jpar != ipar) {
151 ManPage_Paragraph otherParagraph = & my paragraphs [jpar];
152 if (otherParagraph -> type == kManPage_type::SCRIPT)
153 if (Melder_startsWith (otherParagraph -> text, U"\tprocedure ") && Melder_endsWith (otherParagraph -> text, U"\tendproc\n"))
154 MelderString_append (& program, otherParagraph -> text);
155 }
156 //Interpreter_rememberNotebook (interpreterStack -> interpreters [1], <#MelderFile notebookFile#>, false);
157 interpreterStack -> runDown (autoInterpreter(), Melder_dup (program.string), chunkNumber > 1);
158 //interpreterStack -> runDown (interpreterStack -> interpreters [1], Melder_dup (program.string), chunkNumber > 1);
159 } catch (MelderError) {
160 anErrorHasOccurred = true;
161 errorChunk = chunkNumber;
162 theErrorThatOccurred = Melder_dup (Melder_getError ());
163 trace (U"Error in chunk ", chunkNumber, U".");
164 Melder_clearError ();
165 MelderInfo_writeLine (U"\\#{**AN ERROR OCCURRED IN THIS CODE CHUNK:**\n", theErrorThatOccurred.get());
166 MelderInfo_close ();
167 }
168 }
169 #if 0
170 Graphics_setLineType (paragraph -> cacheGraphics.get(), Graphics_DRAWN);
171 Graphics_setLineWidth (paragraph -> cacheGraphics.get(), 1.0);
172 Graphics_setArrowSize (paragraph -> cacheGraphics.get(), 1.0);
173 Graphics_setSpeckleSize (paragraph -> cacheGraphics.get(), 1.0);
174 Graphics_setColour (paragraph -> cacheGraphics.get(), Melder_BLACK);
175 #endif
176
177 #if 0
178 Graphics_resetWsViewport (paragraph -> cacheGraphics.get(), x1DCold, x2DCold, y1DCold, y2DCold);
179 Graphics_setWsWindow (paragraph -> cacheGraphics.get(), x1NDCold, x2NDCold, y1NDCold, y2NDCold);
180 Graphics_setViewport (paragraph -> cacheGraphics.get(), 0.0, 1.0, 0.0, 1.0);
181 Graphics_setWindow (paragraph -> cacheGraphics.get(), 0.0, 1.0, 0.0, 1.0);
182 Graphics_setTextAlignment (paragraph -> cacheGraphics.get(), Graphics_LEFT, Graphics_BOTTOM);
183 Graphics_stopRecording (paragraph -> cacheGraphics.get());
184 #endif
185 }
186 theCurrentPraatApplication = & theForegroundPraatApplication;
187 theCurrentPraatObjects = & theForegroundPraatObjects;
188 theCurrentPraatPicture = & theForegroundPraatPicture;
189 if (anErrorHasOccurred)
190 Melder_flushError (U"Error in code chunk ", errorChunk, U".\n", theErrorThatOccurred.get());
191 praatObjects -> reset();
192 }
193
194 /* End of file ManPage.cpp */