The example “TextEdit MBS” shows how to use NSUndomanagerMBS for a CocoaControlMBS based text area. However, the control is deprecated. Is there an updated example for a text area?
Well, you could just port it over to either regular TextView or NSTextViewControlMBS control:
dim textView as NSTextViewMBS = textArea1.NSTextViewMBS
dim u as NSUndoManagerMBS = textView.undoManager
u.undo
and of course enable it in the open event for the TextArea:
Dim textView As NSTextViewMBS = textArea1.NSTextViewMBS
textView.allowsUndo = True
Thanks!
@Christian_Schmitz : can you give some hints on how to use the grouping levels?
As test I want to start a new grouping level if the key is a space a return or a “.”. According to the docs I should be able to use grouping level to check if I’m in a group or not. But I only get grouping level as 0. Therefore, every key down makes a new grouping level. Which is not what I want:
if Key = EndOfLine or Key = " " or Key = "." then
if UndoManager.groupingLevel = 0 then
UndoManager.beginUndoGrouping
else
UndoManager.endUndoGrouping
end if
else
if UndoManager.groupingLevel = 0 then
UndoManager.beginUndoGrouping
end if
end if
Return False
And me the Grobmotoriker - of course - makes a nice NSInternalInconsistencyException with the above code and doing an undo:
Exception Name: NSInternalInconsistencyException
Description: _endUndoGroupRemovingIfEmpty:: NSUndoManager 0x600002e6ac40 is in invalid state, endUndoGrouping called with no matching begin
User Info: (null)
0 CoreFoundation 0x0000000181108570 __exceptionPreprocess + 176
1 libobjc.A.dylib 0x0000000180bf9eb4 objc_exception_throw + 60
2 Foundation 0x00000001821ef7ec -[NSUndoManager _postCheckpointNotification] + 0
3 Foundation 0x00000001821b87b8 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 192
4 AppKit 0x0000000184ca8540 -[NSApplication _handleEvent:] + 84
5 AppKit 0x00000001848613cc -[NSApplication run] + 512
6 XojoFramework 0x0000000105cdc600 RuntimeRun + 60
7 test.debug 0x00000001045705c0 REALbasic._RuntimeRun + 28
8 test.debug 0x000000010475e374 _Main + 712
9 test.debug 0x000000010475d7a4 main + 36
10 dyld 0x0000000180c350e0 start + 2360
macOS Sonoma 14.3 with MBS from 6.11.2023.