| 1 | /* Manual.cpp |
| 2 | * |
| 3 | * Copyright (C) 1996-2024,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 "Manual.h" |
| 20 | #include "Printer.h" |
| 21 | #include "machine.h" |
| 22 | #include "site.h" |
| 23 | #include "EditorM.h" |
| 24 | #include "praat_script.h" |
| 25 | #include "praatP.h" |
| 26 | |
| 27 | Thing_implement (Manual, HyperPage, 0); |
| 28 | |
| 29 | void structManual :: v9_destroy () noexcept { |
| 30 | if (our ownManPages) |
| 31 | forget_nozero (our data()); |
| 32 | Manual_Parent :: v9_destroy (); |
| 33 | } |
| 34 | |
| 35 | #define SEARCH_PAGE 0 |
| 36 | |
| 37 | static void menu_cb_writeOneToHtmlFile (Manual me, EDITOR_ARGS) { |
| 38 | EDITOR_FORM_SAVE (U"Save as HTML file", nullptr) |
| 39 | autoMelderString buffer; |
| 40 | MelderString_copy (& buffer, my manPages() -> pages.at [my visiblePageNumber] -> title.get()); |
| 41 | char32 *p = buffer.string; |
| 42 | while (*p) { |
| 43 | if (! isalnum ((int) *p) && *p != U'_') |
| 44 | *p = U'_'; |
| 45 | p ++; |
| 46 | } |
| 47 | MelderString_append (& buffer, U".html"); |
| 48 | Melder_sprint (defaultName,300, buffer.string); |
| 49 | EDITOR_DO_SAVE |
| 50 | ManPages_writeOneToHtmlFile (my manPages(), nullptr, my visiblePageNumber, file); |
| 51 | EDITOR_END |
| 52 | } |
| 53 | |
| 54 | static void menu_cb_writeAllToHtmlFolder (Manual me, EDITOR_ARGS) { |
| 55 | EDITOR_FORM (U"Save all pages as HTML files", nullptr) |
| 56 | FOLDER (folder, U"Folder", U"") |
| 57 | EDITOR_OK |
| 58 | SET_STRING (folder, MelderFolder_peekPath (& my rootDirectory)) |
| 59 | EDITOR_DO |
| 60 | ManPages_writeAllToHtmlDir (my manPages(), nullptr, folder); |
| 61 | EDITOR_END |
| 62 | } |
| 63 | |
| 64 | static void menu_cb_searchForPageList (Manual me, EDITOR_ARGS) { |
| 65 | EDITOR_FORM (U"Search for page", nullptr) |
| 66 | LIST (page, U"Page", ManPages_getTitles (my manPages()), 1) |
| 67 | EDITOR_OK |
| 68 | EDITOR_DO |
| 69 | HyperPage_goToPage_number (me, page); |
| 70 | EDITOR_END |
| 71 | } |
| 72 | |
| 73 | static void Manual_runAllChunksToCache (Manual me, ManPage page) { |
| 74 | if (! my praatApplication) |
| 75 | my praatApplication = Melder_calloc_f (structPraatApplication, 1); |
| 76 | if (! my praatObjects) |
| 77 | my praatObjects = Melder_calloc_f (structPraatObjects, 1); |
| 78 | if (! my praatPicture) |
| 79 | my praatPicture = Melder_calloc_f (structPraatPicture, 1); |
| 80 | my praatApplication -> manPages = my manPages(); |
| 81 | ManPage_runAllChunksToCache (page, my optionalInterpreterReference, my instancePref_font(), my instancePref_fontSize(), |
| 82 | my praatApplication, |
| 83 | my praatObjects, |
| 84 | my praatPicture, |
| 85 | & my rootDirectory |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | void structManual :: v_draw () { |
| 90 | //TRACE |
| 91 | if (our visiblePageNumber == SEARCH_PAGE) { |
| 92 | HyperPage_pageTitle (this, U"Best matches"); |
| 93 | HyperPage_intro (this, U"The best matches to your query seem to be:"); |
| 94 | for (int i = 1; i <= our numberOfMatches; i ++) { |
| 95 | char32 link [300]; |
| 96 | const ManPage page = our manPages() -> pages.at [matches [i]]; |
| 97 | Melder_sprint (link,300, U"• @@", page -> title.get()); |
| 98 | HyperPage_listItem (this, link); |
| 99 | } |
| 100 | return; |
| 101 | } |
| 102 | const ManPage page = our manPages() -> pages.at [our visiblePageNumber]; |
| 103 | HyperPage_pageTitle (this, page -> title.get()); // TODO: check appropriateness of file name |
| 104 | integer chunkNumber = 0; |
| 105 | for (integer ipar = 1; ipar <= page -> paragraphs.size; ipar ++) { |
| 106 | ManPage_Paragraph paragraph = & page -> paragraphs [ipar]; |
| 107 | switch (paragraph -> type) { |
| 108 | case kManPage_type::INTRO: HyperPage_intro (this, paragraph -> text); break; |
| 109 | case kManPage_type::ENTRY: HyperPage_entry (this, paragraph -> text); break; |
| 110 | case kManPage_type::NORMAL: HyperPage_paragraph (this, paragraph -> text); break; |
| 111 | case kManPage_type::LIST_ITEM: HyperPage_listItem (this, paragraph -> text); break; |
| 112 | case kManPage_type::TERM: HyperPage_listTag (this, paragraph -> text); break; |
| 113 | case kManPage_type::DEFINITION: HyperPage_definition (this, paragraph -> text); break; |
| 114 | case kManPage_type::CODE: HyperPage_code (this, paragraph -> text); break; |
| 115 | case kManPage_type::PROTOTYPE: HyperPage_prototype (this, paragraph -> text); break; |
| 116 | case kManPage_type::EQUATION: HyperPage_formula (this, paragraph -> text); break; |
| 117 | case kManPage_type::PICTURE: HyperPage_picture (this, paragraph -> width, |
| 118 | paragraph -> height, paragraph -> draw); break; |
| 119 | case kManPage_type::SCRIPT: |
| 120 | ++ chunkNumber; |
| 121 | trace (U"Drawing chunk ", chunkNumber); |
| 122 | HyperPage_script (this, paragraph -> width, paragraph -> height, |
| 123 | paragraph -> text, paragraph -> cacheGraphics.get(), paragraph -> cacheInfo.string); break; |
| 124 | case kManPage_type::LIST_ITEM1: HyperPage_listItem1 (this, paragraph -> text); break; |
| 125 | case kManPage_type::LIST_ITEM2: HyperPage_listItem2 (this, paragraph -> text); break; |
| 126 | case kManPage_type::LIST_ITEM3: HyperPage_listItem3 (this, paragraph -> text); break; |
| 127 | case kManPage_type::TERM1: HyperPage_listTag1 (this, paragraph -> text); break; |
| 128 | case kManPage_type::TERM2: HyperPage_listTag2 (this, paragraph -> text); break; |
| 129 | case kManPage_type::TERM3: HyperPage_listTag3 (this, paragraph -> text); break; |
| 130 | case kManPage_type::DEFINITION1: HyperPage_definition1 (this, paragraph -> text); break; |
| 131 | case kManPage_type::DEFINITION2: HyperPage_definition2 (this, paragraph -> text); break; |
| 132 | case kManPage_type::DEFINITION3: HyperPage_definition3 (this, paragraph -> text); break; |
| 133 | case kManPage_type::CODE1: HyperPage_code1 (this, paragraph -> text); break; |
| 134 | case kManPage_type::CODE2: HyperPage_code2 (this, paragraph -> text); break; |
| 135 | case kManPage_type::CODE3: HyperPage_code3 (this, paragraph -> text); break; |
| 136 | case kManPage_type::CODE4: HyperPage_code4 (this, paragraph -> text); break; |
| 137 | case kManPage_type::CODE5: HyperPage_code5 (this, paragraph -> text); break; |
| 138 | case kManPage_type::CAPTION: HyperPage_caption (this, paragraph -> text); break; |
| 139 | case kManPage_type::QUOTE: HyperPage_quote (this, paragraph -> text); break; |
| 140 | case kManPage_type::QUOTE1: HyperPage_quote1 (this, paragraph -> text); break; |
| 141 | case kManPage_type::QUOTE2: HyperPage_quote2 (this, paragraph -> text); break; |
| 142 | case kManPage_type::QUOTE3: HyperPage_quote3 (this, paragraph -> text); break; |
| 143 | case kManPage_type::SUBHEADER: HyperPage_subheader (this, paragraph -> text); break; |
| 144 | default: break; |
| 145 | } |
| 146 | } |
| 147 | if (ManPages_uniqueLinksHither (our manPages(), our visiblePageNumber)) { |
| 148 | integer ilink, jlink; |
| 149 | bool goAhead = true; |
| 150 | if (page -> paragraphs.size > 0) { |
| 151 | conststring32 text = page -> paragraphs [page -> paragraphs.size]. text; |
| 152 | if (! text || text [0] == U'\0' || text [Melder_length (text) - 1] != U':') { |
| 153 | if (our printing && our suppressLinksHither) |
| 154 | goAhead = false; |
| 155 | else |
| 156 | HyperPage_entry (this, U"Links to this page"); |
| 157 | } |
| 158 | } |
| 159 | if (goAhead) for (ilink = 1; ilink <= page -> linksHither.size; ilink ++) { |
| 160 | const integer link = page -> linksHither [ilink]; |
| 161 | bool alreadyShown = false; |
| 162 | for (jlink = 1; jlink <= page -> linksThither.size; jlink ++) |
| 163 | if (page -> linksThither [jlink] == link) |
| 164 | alreadyShown = true; |
| 165 | if (! alreadyShown) { |
| 166 | conststring32 title = our manPages() -> pages.at [page -> linksHither [ilink]] -> title.get(); |
| 167 | char32 linkText [304]; |
| 168 | Melder_sprint (linkText, 304, U"@@", title, U"@"); |
| 169 | HyperPage_listItem (this, linkText); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | if (! our printing && page -> signature) { |
| 174 | HyperPage_any (this, U"", our instancePref_font(), our instancePref_fontSize(), 0, 0.0, |
| 175 | 0.0, 0.0, 0.1, 0.1, HyperPage_ADD_BORDER); |
| 176 | HyperPage_any (this, page -> signature.get(), our instancePref_font(), our instancePref_fontSize(), Graphics_ITALIC, 0.0, |
| 177 | 0.03, 0.0, 0.1, 0.0, 0); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /********** PRINTING **********/ |
| 182 | |
| 183 | static void print (void *void_me, Graphics graphics) { |
| 184 | iam (Manual); |
| 185 | const integer numberOfPages = my manPages() -> pages.size, saveVisiblePageNumber = my visiblePageNumber; |
| 186 | my ps = graphics; |
| 187 | Graphics_setDollarSignIsCode (my ps, true); // for manuals |
| 188 | Graphics_setBackquoteIsVerbatim (my ps, true); // for notebooks |
| 189 | Graphics_setAtSignIsLink (my ps, true); |
| 190 | my printing = true; |
| 191 | HyperPage_initSheetOfPaper ((HyperPage) me); |
| 192 | for (integer ipage = 1; ipage <= numberOfPages; ipage ++) { |
| 193 | ManPage page = my manPages() -> pages.at [ipage]; |
| 194 | if (! my printPagesStartingWith || Melder_startsWith (page -> title.get(), my printPagesStartingWith)) { |
| 195 | my visiblePageNumber = ipage; |
| 196 | my optionalCurrentPageTitle = Melder_dup_f (page -> title.get()); |
| 197 | my v_goToPage_number (ipage); |
| 198 | my v_draw (); |
| 199 | my v_goToPage_number (saveVisiblePageNumber); |
| 200 | } |
| 201 | } |
| 202 | my printing = false; |
| 203 | my printPagesStartingWith = nullptr; |
| 204 | } |
| 205 | |
| 206 | static void menu_cb_printRange (Manual me, EDITOR_ARGS) { |
| 207 | EDITOR_FORM (U"Print range", nullptr) |
| 208 | SENTENCE (leftOrInsideHeader, U"Left or inside header", U"") |
| 209 | SENTENCE (middleHeader, U"Middle header", U"") |
| 210 | SENTENCE (rightOrOutsideHeader, U"Right or outside header", U"Manual") |
| 211 | SENTENCE (leftOrInsideFooter, U"Left or inside footer", U"") |
| 212 | SENTENCE (middleFooter, U"Middle footer", U"") |
| 213 | SENTENCE (rightOrOutsideFooter, U"Right or outside footer", U"") |
| 214 | BOOLEAN (mirrorEvenOddHeaders, U"Mirror even/odd headers", true) |
| 215 | TEXTFIELD (printAllPagesWhoseTitleStartsWith, U"Print all pages whose title starts with", U"Intro", 2) |
| 216 | INTEGER (firstPageNumber, U"First page number", U"1") |
| 217 | BOOLEAN (suppressLinksToThisPage, U"Suppress \"Links to this page\"", false) |
| 218 | EDITOR_OK |
| 219 | SET_STRING (leftOrInsideHeader, date_STR().get()) |
| 220 | SET_STRING (rightOrOutsideHeader, my name.get()) |
| 221 | if (my d_printingPageNumber) |
| 222 | SET_INTEGER (firstPageNumber, my d_printingPageNumber + 1) |
| 223 | if (my visiblePageNumber >= 1 && my visiblePageNumber <= my manPages() -> pages.size) { |
| 224 | ManPage page = my manPages() -> pages.at [my visiblePageNumber]; |
| 225 | SET_STRING (printAllPagesWhoseTitleStartsWith, page -> title.get()); |
| 226 | } |
| 227 | EDITOR_DO |
| 228 | my insideHeader = leftOrInsideHeader; |
| 229 | my middleHeader = middleHeader; |
| 230 | my outsideHeader = rightOrOutsideHeader; |
| 231 | my insideFooter = leftOrInsideFooter; |
| 232 | my middleFooter = middleFooter; |
| 233 | my outsideFooter = rightOrOutsideFooter; |
| 234 | my mirror = mirrorEvenOddHeaders; |
| 235 | my printPagesStartingWith = printAllPagesWhoseTitleStartsWith; |
| 236 | my d_printingPageNumber = firstPageNumber; |
| 237 | my suppressLinksHither = suppressLinksToThisPage; |
| 238 | Printer_print (print, me); |
| 239 | EDITOR_END |
| 240 | } |
| 241 | |
| 242 | /********** SEARCHING **********/ |
| 243 | |
| 244 | static double searchToken (ManPages me, integer ipage, conststring32 token) { |
| 245 | double goodness = 0.0; |
| 246 | ManPage page = my pages.at [ipage]; |
| 247 | if (! token [0]) |
| 248 | return 1.0; |
| 249 | /* |
| 250 | Try to find a match in the title, case-insensitively. |
| 251 | */ |
| 252 | static MelderString buffer; |
| 253 | MelderString_copy (& buffer, page -> title.get()); |
| 254 | for (char32 *p = & buffer.string [0]; *p != U'\0'; p ++) |
| 255 | *p = Melder_toLowerCase (*p); |
| 256 | if (str32str (buffer.string, token)) { |
| 257 | goodness += 300.0; // lots of points for a match in the title! |
| 258 | if (str32equ (buffer.string, token)) |
| 259 | goodness += 10000.0; // even more points for an exact match! |
| 260 | } |
| 261 | /* |
| 262 | Try to find a match in the paragraphs, case-insensitively. |
| 263 | */ |
| 264 | for (integer ipar = 1; ipar <= page -> paragraphs.size; ipar ++) { |
| 265 | ManPage_Paragraph par = & page -> paragraphs [ipar]; |
| 266 | if (par -> text) { |
| 267 | char32 *ptoken; |
| 268 | MelderString_copy (& buffer, par -> text); |
| 269 | for (char32 *p = & buffer.string [0]; *p != '\0'; p ++) |
| 270 | *p = Melder_toLowerCase (*p); |
| 271 | ptoken = str32str (buffer.string, token); |
| 272 | if (ptoken) { |
| 273 | goodness += 10.0; // ten points for every paragraph with a match! |
| 274 | if (str32str (ptoken + Melder_length (token), token)) |
| 275 | goodness += 1.0; // one point for every second occurrence in a paragraph! |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | return goodness; |
| 280 | } |
| 281 | |
| 282 | static void search (Manual me, conststring32 query) { |
| 283 | const integer numberOfPages = my manPages() -> pages.size; |
| 284 | static MelderString searchText; |
| 285 | MelderString_copy (& searchText, query); |
| 286 | for (char32 *p = & searchText.string [0]; *p != U'\0'; p ++) { |
| 287 | if (*p == U'\n') |
| 288 | *p = U' '; |
| 289 | *p = Melder_toLowerCase (*p); |
| 290 | } |
| 291 | static autoVEC goodnessOfMatch; |
| 292 | if (NUMisEmpty (goodnessOfMatch.get())) |
| 293 | goodnessOfMatch = zero_VEC (numberOfPages); |
| 294 | for (integer ipage = 1; ipage <= numberOfPages; ipage ++) { |
| 295 | char32 *token = searchText.string; |
| 296 | goodnessOfMatch [ipage] = 1.0; |
| 297 | for (;;) { |
| 298 | char32 *space = str32chr (token, U' '); |
| 299 | if (space) |
| 300 | *space = U'\0'; |
| 301 | goodnessOfMatch [ipage] *= searchToken (my manPages(), ipage, token); |
| 302 | if (! space) |
| 303 | break; |
| 304 | *space = U' '; // restore |
| 305 | token = space + 1; |
| 306 | } |
| 307 | } |
| 308 | /* |
| 309 | Find the 20 best matches. |
| 310 | */ |
| 311 | my numberOfMatches = 0; |
| 312 | for (integer imatch = 1; imatch <= 20; imatch ++) { |
| 313 | integer imax = 0; |
| 314 | double max = 0.0; |
| 315 | for (integer ipage = 1; ipage <= numberOfPages; ipage ++) { |
| 316 | if (goodnessOfMatch [ipage] > max) { |
| 317 | max = goodnessOfMatch [ipage]; |
| 318 | imax = ipage; |
| 319 | } |
| 320 | } |
| 321 | if (! imax) break; |
| 322 | my matches [++ my numberOfMatches] = imax; |
| 323 | goodnessOfMatch [imax] = 0.0; // skip next time |
| 324 | } |
| 325 | HyperPage_goToPage_number (me, SEARCH_PAGE); |
| 326 | } |
| 327 | |
| 328 | void Manual_search (Manual me, conststring32 query) { |
| 329 | GuiText_setString (my searchText, query); |
| 330 | search (me, query); |
| 331 | } |
| 332 | |
| 333 | static void gui_button_cb_home (Manual me, GuiButtonEvent /* event */) { |
| 334 | integer iHome = ManPages_lookUp (my manPages(), U"Intro"); |
| 335 | HyperPage_goToPage_number (me, iHome ? iHome : 1); |
| 336 | } |
| 337 | |
| 338 | static void gui_button_cb_record (Manual me, GuiButtonEvent /* event */) { |
| 339 | ManPage optionalManPage = ( my visiblePageNumber < 1 ? nullptr : my manPages() -> pages.at [my visiblePageNumber] ); |
| 340 | GuiThing_setSensitive (my recordButton, false); |
| 341 | GuiThing_setSensitive (my playButton, false); |
| 342 | GuiThing_setSensitive (my publishButton, false); |
| 343 | #if defined (_WIN32) |
| 344 | GdiFlush (); |
| 345 | #endif |
| 346 | if (! Melder_record (optionalManPage ? optionalManPage -> recordingTime : 1.0)) |
| 347 | Melder_flushError (); |
| 348 | GuiThing_setSensitive (my recordButton, true); |
| 349 | GuiThing_setSensitive (my playButton, true); |
| 350 | GuiThing_setSensitive (my publishButton, true); |
| 351 | } |
| 352 | |
| 353 | static void gui_button_cb_play (Manual me, GuiButtonEvent /* event */) { |
| 354 | GuiThing_setSensitive (my recordButton, false); |
| 355 | GuiThing_setSensitive (my playButton, false); |
| 356 | GuiThing_setSensitive (my publishButton, false); |
| 357 | #if defined (_WIN32) |
| 358 | GdiFlush (); |
| 359 | #endif |
| 360 | Melder_play (); |
| 361 | GuiThing_setSensitive (my recordButton, true); |
| 362 | GuiThing_setSensitive (my playButton, true); |
| 363 | GuiThing_setSensitive (my publishButton, true); |
| 364 | } |
| 365 | |
| 366 | static void gui_button_cb_publish (Manual /* me */, GuiButtonEvent /* event */) { |
| 367 | Melder_publishPlayed (); |
| 368 | } |
| 369 | |
| 370 | static void do_search (Manual me) { |
| 371 | autostring32 query = GuiText_getString (my searchText); |
| 372 | search (me, query.get()); |
| 373 | } |
| 374 | |
| 375 | static void gui_button_cb_search (Manual me, GuiButtonEvent /* event */) { |
| 376 | do_search (me); |
| 377 | } |
| 378 | |
| 379 | void structManual :: v_createChildren () { |
| 380 | const bool hasRecordingButtons = ( our manPages() -> dynamic && our manPages() -> pages.at [1] -> recordingTime > 0.0 ); |
| 381 | our d_hasExtraRowOfTools = hasRecordingButtons; |
| 382 | Manual_Parent :: v_createChildren (); |
| 383 | #if defined (macintosh) |
| 384 | #define STRING_SPACING 8 |
| 385 | #else |
| 386 | #define STRING_SPACING 2 |
| 387 | #endif |
| 388 | const int height = Machine_getTextHeight (), y = Machine_getMenuBarBottom () + 4; |
| 389 | our homeButton = GuiButton_createShown (our windowForm, 104, 168, y, y + height, |
| 390 | U"Home", gui_button_cb_home, this, 0); |
| 391 | if (hasRecordingButtons) { |
| 392 | our recordButton = GuiButton_createShown (our windowForm, 4, 79, y+height+8, y+height+8 + height, |
| 393 | U"Record", gui_button_cb_record, this, 0); |
| 394 | our playButton = GuiButton_createShown (our windowForm, 85, 160, y+height+8, y+height+8 + height, |
| 395 | U"Play", gui_button_cb_play, this, 0); |
| 396 | our publishButton = GuiButton_createShown (our windowForm, 166, 166 + 175, y+height+8, y+height+8 + height, |
| 397 | U"Copy last played to list", gui_button_cb_publish, this, 0); |
| 398 | } |
| 399 | GuiButton_createShown (our windowForm, 274, 274 + 69, y, y + height, |
| 400 | U"Search:", gui_button_cb_search, this, GuiButton_DEFAULT); |
| 401 | our searchText = GuiText_createShown (our windowForm, 274+69 + STRING_SPACING, 452 + STRING_SPACING - 2, y, y + Gui_TEXTFIELD_HEIGHT, 0); |
| 402 | } |
| 403 | |
| 404 | static void menu_cb_help (Manual me, EDITOR_ARGS) { HyperPage_goToPage (me, U"Manual"); } |
| 405 | |
| 406 | void structManual :: v_createMenus () { |
| 407 | Manual_Parent :: v_createMenus (); |
| 408 | |
| 409 | Editor_addCommand (this, U"File", U"Print manual...", 0, menu_cb_printRange); |
| 410 | Editor_addCommand (this, U"File", U"Save page as HTML file...", 'S', menu_cb_writeOneToHtmlFile); |
| 411 | Editor_addCommand (this, U"File", U"Save manual to HTML folder...", 0, menu_cb_writeAllToHtmlFolder); |
| 412 | Editor_addCommand (this, U"File", U"Save manual to HTML directory...", GuiMenu_DEPRECATED_2020, menu_cb_writeAllToHtmlFolder); |
| 413 | Editor_addCommand (this, U"File", U"-- close --", 0, nullptr); |
| 414 | |
| 415 | Editor_addCommand (this, U"Go to", U"Search for page (list)...", 0, menu_cb_searchForPageList); |
| 416 | } |
| 417 | |
| 418 | void structManual :: v_createMenuItems_help (EditorMenu menu) { |
| 419 | Manual_Parent :: v_createMenuItems_help (menu); |
| 420 | EditorMenu_addCommand (menu, U"Manual help", '?', menu_cb_help); |
| 421 | } |
| 422 | |
| 423 | void structManual :: v_defaultHeaders (EditorCommand cmd) { |
| 424 | Manual me = (Manual) cmd -> d_editor; |
| 425 | if (my visiblePageNumber > 0) { |
| 426 | const ManPage page = our manPages() -> pages.at [my visiblePageNumber]; |
| 427 | SET_STRING (my outsideHeader, page -> title.get()) |
| 428 | SET_STRING (my insideFooter, page -> signature.get()) |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | integer structManual :: v_getNumberOfPages () { |
| 433 | return our manPages() -> pages.size; |
| 434 | } |
| 435 | |
| 436 | integer structManual :: v_getCurrentPageNumber () { |
| 437 | return our visiblePageNumber > 0 ? our visiblePageNumber : 1; |
| 438 | } |
| 439 | |
| 440 | void structManual :: v_goToPage_number (integer goToPageNumber) { |
| 441 | if (goToPageNumber < 1 || goToPageNumber > our manPages() -> pages.size) { |
| 442 | if (goToPageNumber == SEARCH_PAGE) { |
| 443 | our visiblePageNumber = SEARCH_PAGE; |
| 444 | our optionalCurrentPageTitle. reset(); |
| 445 | return; |
| 446 | } else Melder_throw (U"Page ", goToPageNumber, U" not found."); |
| 447 | } |
| 448 | our visiblePageNumber = goToPageNumber; |
| 449 | ManPage page = our manPages() -> pages.at [our visiblePageNumber]; |
| 450 | our optionalCurrentPageTitle = Melder_dup_f (page -> title.get()); |
| 451 | Melder_assert (our optionalCurrentPageTitle); |
| 452 | our manPages() -> invalidateCache (); |
| 453 | Manual_runAllChunksToCache (this, page); |
| 454 | } |
| 455 | |
| 456 | int structManual :: v_goToPage (conststring32 title) { |
| 457 | if (! title) { |
| 458 | our v_goToPage_number (SEARCH_PAGE); |
| 459 | return 1; |
| 460 | } |
| 461 | Melder_assert (title); |
| 462 | if (title [0] == U'\\' && title [1] == U'F' && title [2] == U'I') { |
| 463 | structMelderFile file { }; |
| 464 | MelderFolder_relativePathToFile (& our manPages() -> rootDirectory, title + 3, & file); |
| 465 | Melder_recordFromFile (& file); |
| 466 | return -1; |
| 467 | } else if (title [0] == U'\\' && title [1] == U'S' && title [2] == U'C') { |
| 468 | static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); |
| 469 | autoMelderSetCurrentFolder saveFolder (& our manPages() -> rootDirectory); |
| 470 | autoPraatBackground background; |
| 471 | try { |
| 472 | autostring32 fileNameWithArguments = Melder_dup (title + 3); |
| 473 | praat_executeScriptFromFileNameWithArguments (fileNameWithArguments.get()); |
| 474 | } catch (MelderError) { |
| 475 | Melder_flushError (); |
| 476 | } |
| 477 | return 0; |
| 478 | } else { |
| 479 | const integer i = ManPages_lookUp (our manPages(), title); |
| 480 | if (i == 0) { |
| 481 | if (title [0] == U'`') |
| 482 | Melder_throw (U"Page ", title, U" not found."); |
| 483 | else |
| 484 | Melder_throw (U"Page “", title, U"” not found."); |
| 485 | } |
| 486 | our v_goToPage_number (i); |
| 487 | return 1; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | autoManual Manual_create (conststring32 openingPageTitle, Interpreter optionalInterpreterReference, |
| 492 | ManPages manPages, bool ownManPages, bool backquoteIsVerbatim) |
| 493 | { |
| 494 | Melder_assert (openingPageTitle); |
| 495 | try { |
| 496 | autoManual me = Thing_new (Manual); |
| 497 | const integer lookUpPageNumber = ManPages_lookUp (manPages, openingPageTitle); |
| 498 | if (lookUpPageNumber == 0) |
| 499 | Melder_throw (U"Page “", openingPageTitle, U"” not found."); |
| 500 | my visiblePageNumber = lookUpPageNumber; |
| 501 | my optionalCurrentPageTitle = Melder_dup_f (openingPageTitle); |
| 502 | /* |
| 503 | The title of the window is the title of the whole manual, not the title of the page. |
| 504 | If the first page has a title that starts with "-", then that is the title; |
| 505 | otherwise, the title is just "Praat Manual". |
| 506 | */ |
| 507 | char32 windowTitle [101]; |
| 508 | if (manPages -> pages.at [1] -> title [0] == U'-') { |
| 509 | Melder_sprint (windowTitle,101, & manPages -> pages.at [1] -> title [1]); |
| 510 | const integer windowTitleLength = Melder_length (windowTitle); |
| 511 | if (windowTitleLength > 0 && windowTitle [windowTitleLength - 1] == U'-') |
| 512 | windowTitle [windowTitleLength - 1] = U'\0'; |
| 513 | } else { |
| 514 | Melder_sprint (windowTitle,101, U"Praat Manual"); |
| 515 | } |
| 516 | my ownManPages = ownManPages; |
| 517 | HyperPage_init1 (me.get(), windowTitle, manPages, backquoteIsVerbatim); |
| 518 | MelderFolder_copy (& manPages -> rootDirectory, & my rootDirectory); |
| 519 | my history [0]. page = Melder_dup_f (openingPageTitle); // BAD |
| 520 | /* |
| 521 | Cache the output of the opening page. |
| 522 | */ |
| 523 | ManPage openingPage = manPages -> pages.at [my visiblePageNumber]; |
| 524 | my optionalInterpreterReference = optionalInterpreterReference; |
| 525 | Manual_runAllChunksToCache (me.get(), openingPage); |
| 526 | |
| 527 | HyperPage_init2 (me.get(), windowTitle, manPages); |
| 528 | return me; |
| 529 | } catch (MelderError) { |
| 530 | Melder_throw (U"Manual window not created."); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /* End of file Manual.cpp */ |