| 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" |
| 20 | |
| 21 | Thing_implement (InfoEditor, TextEditor, 0); |
| 22 | |
| 23 | static InfoEditor theReferenceToTheOnlyInfoEditor; |
| 24 | |
| 25 | void structInfoEditor :: v9_destroy () noexcept { |
| 26 | theReferenceToTheOnlyInfoEditor = nullptr; // undangle |
| 27 | InfoEditor_Parent :: v9_destroy (); |
| 28 | } |
| 29 | |
| 30 | void structInfoEditor :: v_clear () { |
| 31 | Melder_clearInfo (); |
| 32 | } |
| 33 | |
| 34 | InfoEditor InfoEditor_getTheReferenceToTheOnlyInstance () { |
| 35 | if (! theReferenceToTheOnlyInfoEditor) { |
| 36 | autoInfoEditor editor = Thing_new (InfoEditor); |
| 37 | TextEditor_init (editor.get(), U""); |
| 38 | Thing_setName (editor.get(), U"Praat Info"); |
| 39 | theReferenceToTheOnlyInfoEditor = editor.get(); |
| 40 | editor.releaseToUser(); |
| 41 | } |
| 42 | return theReferenceToTheOnlyInfoEditor; |
| 43 | } |
| 44 | |
| 45 | static void gui_information (conststring32 message) { |
| 46 | InfoEditor editor = InfoEditor_getTheReferenceToTheOnlyInstance (); |
| 47 | #if cocoa |
| 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_setString (editor -> textWidget, message, false); |
| 66 | GuiThing_show (editor -> 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 cocoa |
| 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_injectInformationProc () { |
| 115 | Melder_setInformationProc (gui_information); |
| 116 | } |
| 117 | |
| 118 | /* End of file InfoEditor.cpp */ |