| 1 |
{%MainUnit castlewindow.pas} |
| 2 |
{ |
| 3 |
Copyright 2022-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Implementation of TCastleWindow.MessageOK, MessageYesNo (Cocoa alerts) |
| 18 |
|
| 19 |
Lots of inspiration from LCL Cocoa widgetset code. |
| 20 |
} |
| 21 |
|
| 22 |
var |
| 23 |
AlertEnded: Boolean; |
| 24 |
AlertResultCode: NSModalResponse; |
| 25 |
|
| 26 |
procedure AlertCompletionHandler(ResultCode: NSModalResponse); |
| 27 |
{cdecl; No need to change calling convention, looking at examples} |
| 28 |
begin |
| 29 |
AlertEnded := true; |
| 30 |
AlertResultCode := ResultCode; |
| 31 |
end; |
| 32 |
|
| 33 |
type |
| 34 |
TAlertCompletionHandler = reference to procedure(ResultCode: NSModalResponse); cdecl; cblock; |
| 35 |
|
| 36 |
function TCastleWindow.CocoaPromptUser(const DialogCaption: String; |
| 37 |
const DialogMessage: String; |
| 38 |
const AlertStyle: NSAlertStyle; |
| 39 |
const ButtonCaptions: array of String; |
| 40 |
const ButtonResults: array of NSInteger): NSInteger; |
| 41 |
var |
| 42 |
anAlert: NSAlert; |
| 43 |
informativeText: NSString; |
| 44 |
messageText: NSString; |
| 45 |
I: Integer; |
| 46 |
aButton: NSButton; |
| 47 |
begin |
| 48 |
Assert(ref <> nil); |
| 49 |
Assert(High(ButtonCaptions) = High(ButtonResults)); |
| 50 |
Assert(High(ButtonCaptions) >= 0); |
| 51 |
|
| 52 |
Result := -1; |
| 53 |
AnAlert := NSAlert.alloc.init; |
| 54 |
try |
| 55 |
informativeText := NSSTR(DialogMessage); |
| 56 |
messageText := NSSTR(DialogCaption); |
| 57 |
anAlert.setAlertStyle(AlertStyle); |
| 58 |
anAlert.setInformativeText(informativeText); |
| 59 |
anAlert.setMessageText(messageText); |
| 60 |
|
| 61 |
for I := Low(ButtonCaptions) to High(ButtonCaptions) do |
| 62 |
begin |
| 63 |
aButton := anAlert.addButtonWithTitle(NSSTR(ButtonCaptions[I])); |
| 64 |
aButton.setKeyEquivalentModifierMask(0); |
| 65 |
|
| 66 |
// call aButton.setKeyEquivalent |
| 67 |
if (ButtonCaptions[I] = 'OK') or |
| 68 |
(ButtonCaptions[I] = 'Yes') then |
| 69 |
aButton.setKeyEquivalent(NSSTR(#13)) |
| 70 |
else |
| 71 |
if ButtonCaptions[I] = 'No' then |
| 72 |
aButton.setKeyEquivalent(NSSTR(#27)) |
| 73 |
else |
| 74 |
// By default, the first button is the default button. If in our |
| 75 |
// case this should not be the case, remove the default status |
| 76 |
// from the first button. |
| 77 |
aButton.setKeyEquivalent(NSSTR('')); |
| 78 |
|
| 79 |
aButton.setTag(ButtonResults[I]); |
| 80 |
end; |
| 81 |
|
| 82 |
AlertResultCode := -1; |
| 83 |
AlertEnded := false; |
| 84 |
|
| 85 |
{ Using beginSheetModalForWindow_completionHandler with callback |
| 86 |
based on https://wiki.freepascal.org/macOS_NSAlert |
| 87 |
|
| 88 |
This requires casting AlertCompletionHandler to "cblock", see |
| 89 |
https://forum.lazarus.freepascal.org/index.php?topic=57724.0 |
| 90 |
The OpaqueCBlocks predated FPC support for cblocks. So they're basically untyped parameters used as placeholders for cblock parameters. |
| 91 |
https://wiki.freepascal.org/FPC_New_Features_3.2.0#Support_for_interfacing_with_C_blocks_functionality |
| 92 |
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/tests/test/tblock1.pp |
| 93 |
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/tests/test/tblock2.pp |
| 94 |
} |
| 95 |
|
| 96 |
anAlert.beginSheetModalForWindow_completionHandler(ref, |
| 97 |
TAlertCompletionHandler(@AlertCompletionHandler)); |
| 98 |
|
| 99 |
{ Using MenuForcefullyDisabled is important here, as we run |
| 100 |
Application.ProcessMessage and in general process all events as usual |
| 101 |
underneath the message box. } |
| 102 |
MenuForcefullyDisabled := true; |
| 103 |
try |
| 104 |
while not AlertEnded do |
| 105 |
if not Application.ProcessMessage(true, true) then |
| 106 |
Break; |
| 107 |
finally |
| 108 |
MenuForcefullyDisabled := false; |
| 109 |
end; |
| 110 |
|
| 111 |
if AlertEnded then |
| 112 |
Result := AlertResultCode; |
| 113 |
finally |
| 114 |
AnAlert.release; |
| 115 |
end; |
| 116 |
|
| 117 |
{ Make the ref again receive keys, otherwise closing the dialog |
| 118 |
with key makes weird experience -- further key presses will not be send |
| 119 |
to the application window you're looking at. } |
| 120 |
ref.makeKeyAndOrderFront(nil); |
| 121 |
end; |
| 122 |
|
| 123 |
const |
| 124 |
CaptionFromMessageType: array [TWindowMessageType] of String = ( |
| 125 |
'Information', |
| 126 |
'Warning', |
| 127 |
'Question', |
| 128 |
'Error', |
| 129 |
'Message' // mtOther |
| 130 |
); |
| 131 |
AlertStyleFromMessageType: array [TWindowMessageType] of NSAlertStyle = ( |
| 132 |
NSInformationalAlertStyle, // mtInfo |
| 133 |
NSWarningAlertStyle, // mtWarning |
| 134 |
NSInformationalAlertStyle, // mtQuestion |
| 135 |
NSCriticalAlertStyle, // mtError |
| 136 |
NSInformationalAlertStyle // mtOther |
| 137 |
); |
| 138 |
|
| 139 |
procedure TCastleWindow.MessageOK(const S: String; const MessageType: TWindowMessageType); |
| 140 |
begin |
| 141 |
CocoaPromptUser(CaptionFromMessageType[MessageType], S, |
| 142 |
AlertStyleFromMessageType[MessageType], ['OK'], [0]); |
| 143 |
end; |
| 144 |
|
| 145 |
function TCastleWindow.MessageYesNo(const S: String; |
| 146 |
const MessageType: TWindowMessageType): boolean; |
| 147 |
begin |
| 148 |
Result := CocoaPromptUser(CaptionFromMessageType[MessageType], S, |
| 149 |
AlertStyleFromMessageType[MessageType], ['Yes', 'No'], [0, 1]) = 0; |
| 150 |
end; |