| 1 | /* InfoEditor.cpp |
| 2 | * |
| 3 | * Copyright (C) 2004-2011,2012,2013,2015,2017 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 2 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 "InfoEditor.h"cxx . . $ `<file>/sys/InfoEditor.h`/ |
| 20 | |
| 21 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (InfoEditor, TextEditor, 0); |
| 22 | |
| 23 | static InfoEditorcxx . . $ InfoEditor# theReferenceToTheOnlyInfoEditorcxx . . $ theReferenceToTheOnlyInfoEditor.; |
| 24 | |
| 25 | void structInfoEditorcxx . . $ structInfoEditor# :: v9_destroycxx . . $ structInfoEditor#v9_destroy(ced63f7c635d850d). () noexcept { |
| 26 | theReferenceToTheOnlyInfoEditorcxx . . $ theReferenceToTheOnlyInfoEditor. = nullptr; // undangle |
| 27 | InfoEditor_Parentcxx . . $ InfoEditor_Parent# :: v9_destroycxx . . $ structTextEditor#v9_destroy(ced63f7c635d850d). (); |
| 28 | } |
| 29 | |
| 30 | void structInfoEditorcxx . . $ structInfoEditor# :: v_clearcxx . . $ structInfoEditor#v_clear(49f6e7a06ebc5aa8). () { |
| 31 | Melder_clearInfocxx . . $ Melder_clearInfo(49f6e7a06ebc5aa8). (); |
| 32 | } |
| 33 | |
| 34 | InfoEditorcxx . . $ InfoEditor# InfoEditor_getTheReferenceToTheOnlyInstancecxx . . $ InfoEditor_getTheReferenceToTheOnlyInstance(1525cd43c71f6b0f). () { |
| 35 | if (! theReferenceToTheOnlyInfoEditorcxx . . $ theReferenceToTheOnlyInfoEditor.) { |
| 36 | autoInfoEditorcxx . . $ autoInfoEditor# editor = Thing_newcxx . . $ `sys/Thing.h:514:9`! (InfoEditor); |
| 37 | TextEditor_initcxx . . $ TextEditor_init(9ba39b4797eb5880). (editor.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), U""); |
| 38 | Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (editor.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), U"Praat Info"); |
| 39 | theReferenceToTheOnlyInfoEditorcxx . . $ theReferenceToTheOnlyInfoEditor. = editor.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 40 | editor.releaseToUsercxx . . $ autoSomeThing#releaseToUser(ced63f7c635d850d).(); |
| 41 | } |
| 42 | return theReferenceToTheOnlyInfoEditorcxx . . $ theReferenceToTheOnlyInfoEditor.; |
| 43 | } |
| 44 | |
| 45 | static void gui_informationcxx . . $ gui_information(ff00d524ab0355ec). (conststring32cxx . . $ conststring32# message) { |
| 46 | InfoEditorcxx . . $ InfoEditor# editor = InfoEditor_getTheReferenceToTheOnlyInstancecxx . . $ InfoEditor_getTheReferenceToTheOnlyInstance(1525cd43c71f6b0f). (); |
| 47 | #if cocoacxx . . $ `sys/Gui.h:35:10`! |
| 48 | /* |
| 49 | REPAIRED MEMORY LEAK |
| 50 | 2019-10-31 |
| 51 | As we may be called in long sequences outside of the event loop, |
| 52 | we may need to clean up. Without the autorelease pool, |
| 53 | the retainCount of the cocoaTextView will quickly rise, |
| 54 | as shown by running the following script: |
| 55 | for i to 1e6 |
| 56 | writeInfoLine: i |
| 57 | endfor |
| 58 | and waiting for Praat to crash after taking 3 GB of memory (macOS 10.15). |
| 59 | Also, if you stop before crashing (e.g. have the loop run to only 1e5), |
| 60 | then much of the memory will not be released at all. |
| 61 | */ |
| 62 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 63 | //Melder_casual (U"cocoaTextView retain count before: ", [editor -> textWidget -> d_cocoaTextView retainCount]); |
| 64 | #endif |
| 65 | GuiText_setStringcxx . . $ GuiText_setString(d44acbd60ea2b9be). (editor -> textWidgetcxx . . $ structTextEditor#textWidget., message, false); |
| 66 | GuiThing_showcxx . . $ GuiThing_show(20e88c25f0ba841). (editor -> windowFormcxx . . $ structEditor#windowForm.); |
| 67 | /* |
| 68 | Try to make sure that the invalidated text widget and the elements of the fronted window are |
| 69 | redrawn before the next event. |
| 70 | |
| 71 | The following Praat script can test this: |
| 72 | |
| 73 | writeInfoLine: "hoi" |
| 74 | for i to 100 |
| 75 | appendInfoLine: i |
| 76 | endfor |
| 77 | |
| 78 | The Info window should scroll continuously while the lines are added, |
| 79 | not just show the end result. |
| 80 | */ |
| 81 | #if cocoacxx . . $ `sys/Gui.h:35:10`! |
| 82 | /* |
| 83 | TODO: This code is very similar to that of GuiShell_drain (editor -> windowForm, false, false); |
| 84 | perhaps it can be merged at some point. |
| 85 | */ |
| 86 | [editor -> windowForm -> d_cocoaShell displayIfNeeded]; |
| 87 | /* |
| 88 | Just calling `displayIfNeeded` doesn't suffice for redrawing. |
| 89 | We have to poke the run loop a little, which can be achieved |
| 90 | by calling `nextEventMatchingMask` with `dequeue: YES`, |
| 91 | a combination that potentially progresses the run loop by one iteration. |
| 92 | This effect, even if not realized, forces pending redraws to be executed to the screen. |
| 93 | |
| 94 | Let's use the opportunity to throw away any mouse clicks and key strokes. |
| 95 | */ |
| 96 | NSEvent *nsEvent; |
| 97 | while ((nsEvent = [NSApp |
| 98 | nextEventMatchingMask: NSEventMaskAny |
| 99 | untilDate: [NSDate distantPast] |
| 100 | inMode: NSDefaultRunLoopMode |
| 101 | dequeue: YES]) != nullptr) |
| 102 | { |
| 103 | NSUInteger nsEventType = [nsEvent type]; |
| 104 | if (nsEventType == NSEventTypeKeyDown) |
| 105 | NSBeep(); |
| 106 | /* |
| 107 | Ignore all other events. |
| 108 | */ |
| 109 | } |
| 110 | [pool release]; |
| 111 | #endif |
| 112 | } |
| 113 | |
| 114 | void InfoEditor_injectInformationProccxx . . $ InfoEditor_injectInformationProc(49f6e7a06ebc5aa8). () { |
| 115 | Melder_setInformationProccxx . . $ Melder_setInformationProc(f441b548652834d9). (gui_informationcxx . . $ gui_information(ff00d524ab0355ec).); |
| 116 | } |
| 117 | |
| 118 | /* End of file InfoEditor.cpp */ |