| 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 CastleWindow.BackendFileDialog. |
| 18 |
|
| 19 |
Lots of inspiration from LCL Cocoa widgetset code. |
| 20 |
} |
| 21 |
|
| 22 |
function TCastleWindow.BackendFileDialog(const Title: string; var FileName: string; |
| 23 |
OpenDialog: boolean; FileFilters: TFileFilterList): boolean; |
| 24 |
|
| 25 |
{ Implementation based on LCL TCocoaWSFileDialog.ShowModal } |
| 26 |
|
| 27 |
var |
| 28 |
openDlg: NSOpenPanel; |
| 29 |
saveDlg: NSSavePanel; |
| 30 |
InitName, InitDir: string; |
| 31 |
|
| 32 |
// filter accessory view |
| 33 |
// accessoryView: NSView; |
| 34 |
// lFilter: TCocoaFilterComboBox; |
| 35 |
|
| 36 |
(* |
| 37 |
// setup panel and its accessory view |
| 38 |
procedure CreateAccessoryView(AOpenOwner: NSOpenPanel; ASaveOwner: NSSavePanel); |
| 39 |
const |
| 40 |
INT_MIN_ACCESSORYVIEW_WIDTH = 300; |
| 41 |
OFS_HRZ = 10; |
| 42 |
var |
| 43 |
lRect: NSRect; |
| 44 |
lText: NSTextField; |
| 45 |
lTextStr: NSString; |
| 46 |
lDialogView: NSView; |
| 47 |
lAccessoryWidth: Integer = INT_MIN_ACCESSORYVIEW_WIDTH; |
| 48 |
w: Integer; |
| 49 |
nw: Integer; |
| 50 |
fw: Integer; |
| 51 |
begin |
| 52 |
// check if the accessory is necessary |
| 53 |
if FileDialog.Filter = '' then Exit; |
| 54 |
|
| 55 |
// try to obtain the dialog size |
| 56 |
lDialogView := NSView(ASaveOwner.contentView); |
| 57 |
if lDialogView <> nil then |
| 58 |
begin |
| 59 |
if (NSAppkitVersionNumber >= NSAppKitVersionNumber11_0) then |
| 60 |
// starting with Big Sur, the dialog retains the last openned size |
| 61 |
// causing the width to be increased on every openning of the dialog |
| 62 |
// we'd simply force the lAccessoryWidth to start with the minimum width |
| 63 |
lAccessoryWidth := INT_MIN_ACCESSORYVIEW_WIDTH |
| 64 |
else if lDialogView.frame.size.width > INT_MIN_ACCESSORYVIEW_WIDTH then |
| 65 |
lAccessoryWidth := Round(lDialogView.frame.size.width); |
| 66 |
end; |
| 67 |
lRect := GetNSRect(0, 0, lAccessoryWidth, 30); |
| 68 |
accessoryView := NSView.alloc.initWithFrame(lRect); |
| 69 |
|
| 70 |
// "Format:" label |
| 71 |
lText := NSTextField.alloc.initWithFrame(NSNullRect); |
| 72 |
{$ifdef BOOLFIX} |
| 73 |
lText.setBezeled_(Ord(False)); |
| 74 |
lText.setDrawsBackground_(Ord(False)); |
| 75 |
lText.setEditable_(Ord(False)); |
| 76 |
lText.setSelectable_(Ord(False)); |
| 77 |
{$else} |
| 78 |
lText.setBezeled(False); |
| 79 |
lText.setDrawsBackground(False); |
| 80 |
lText.setEditable(False); |
| 81 |
lText.setSelectable(False); |
| 82 |
{$endif} |
| 83 |
lTextStr := NSStringUTF8(rsMacOSFileFormat); |
| 84 |
lText.setStringValue(lTextStr); |
| 85 |
lText.setFont(NSFont.systemFontOfSize(NSFont.systemFontSizeForControlSize(NSRegularControlSize))); |
| 86 |
lText.sizeToFit; |
| 87 |
|
| 88 |
// Combobox |
| 89 |
lFilter := TCocoaFilterComboBox.alloc.initWithFrame(NSNullRect); |
| 90 |
lFilter.IsOpenDialog := AOpenOwner <> nil; |
| 91 |
if lFilter.IsOpenDialog then |
| 92 |
lFilter.DialogHandle := AOpenOwner |
| 93 |
else |
| 94 |
lFilter.DialogHandle := ASaveOwner; |
| 95 |
lFilter.Owner := FileDialog; |
| 96 |
lFilter.setTarget(lFilter); |
| 97 |
lFilter.setAction(objcselector('comboboxAction:')); |
| 98 |
lFilter.updateFilterList(); |
| 99 |
if FileDialog.FilterIndex <= 0 then |
| 100 |
lFilter.lastSelectedItemIndex := 0 |
| 101 |
else |
| 102 |
lFilter.lastSelectedItemIndex := FileDialog.FilterIndex-1; |
| 103 |
lFilter.lastSelectedItemIndex := lFilter.setDialogFilter(lFilter.lastSelectedItemIndex); |
| 104 |
lFilter.sizeToFit; |
| 105 |
lFilter.setAutoresizingMask(NSViewWidthSizable); |
| 106 |
if FileDialog.FilterIndex>0 then |
| 107 |
lFilter.selectItemAtIndex(FileDialog.FilterIndex-1); |
| 108 |
|
| 109 |
// Trying to put controls into the center of the Acc-view |
| 110 |
// Label must fit in full. Whatever is left is for filter |
| 111 |
w:=lAccessoryWidth - OFS_HRZ - OFS_HRZ; |
| 112 |
fw:=Round(lFilter.frame.size.width); |
| 113 |
nw:=Round(lText.frame.size.width + fw + OFS_HRZ); |
| 114 |
if nw>w then begin |
| 115 |
dec(fw, nw-w); |
| 116 |
nw:=w; |
| 117 |
end; |
| 118 |
|
| 119 |
lText.setFrame( NSMakeRect( |
| 120 |
Round((w-nw) / 2+OFS_HRZ) |
| 121 |
, 0 |
| 122 |
, lText.frame.size.width |
| 123 |
, lFilter.frame.size.height |
| 124 |
)); |
| 125 |
|
| 126 |
lFilter.setFrame( NSMakeRect( |
| 127 |
lText.frame.origin.x+lText.frame.size.width+OFS_HRZ |
| 128 |
,4 |
| 129 |
,fw |
| 130 |
,lFilter.frame.size.height |
| 131 |
)); |
| 132 |
|
| 133 |
accessoryView.addSubview(lText.autorelease); |
| 134 |
accessoryView.addSubview(lFilter.autorelease); |
| 135 |
|
| 136 |
lFilter.setAutoresizingMask(NSViewWidthSizable); |
| 137 |
|
| 138 |
lFilter.DialogHandle.setAccessoryView(accessoryView.autorelease); |
| 139 |
lFilter.DialogHandle.setDelegate(lFilter); |
| 140 |
end; |
| 141 |
*) |
| 142 |
|
| 143 |
begin |
| 144 |
InitName := ExtractFileName(FileName); |
| 145 |
InitDir := ExtractFileDir(FileName); |
| 146 |
|
| 147 |
// TODO: FileFilters |
| 148 |
// Adapt LCL code? Or resign -- Cocoa doesn't have such filters natively. |
| 149 |
// lFilter := nil; |
| 150 |
|
| 151 |
if OpenDialog then |
| 152 |
begin |
| 153 |
openDlg := NSOpenPanel.openPanel; |
| 154 |
openDlg.setAllowsMultipleSelection(false); |
| 155 |
|
| 156 |
{ to choose only directory we would do this: |
| 157 |
openDlg.setCanChooseDirectories(true); |
| 158 |
openDlg.setCanChooseFiles(false); |
| 159 |
} |
| 160 |
openDlg.setCanChooseFiles(true); |
| 161 |
openDlg.setCanChooseDirectories(false); |
| 162 |
|
| 163 |
// that's correct, NSOpenPanel descends from NSSavePanel and we use it |
| 164 |
saveDlg := openDlg; |
| 165 |
end else |
| 166 |
begin |
| 167 |
saveDlg := NSSavePanel.savePanel; |
| 168 |
saveDlg.setCanCreateDirectories(true); |
| 169 |
saveDlg.setNameFieldStringValue(NSSTR(InitName)); |
| 170 |
openDlg := nil; |
| 171 |
end; |
| 172 |
|
| 173 |
saveDlg.setAccessoryView(nil); |
| 174 |
saveDlg.retain; // LCL comment: this is for OSX 10.6 (and we don't use ARC either) |
| 175 |
saveDlg.setTitle(NSSTR(Title)); |
| 176 |
saveDlg.setDirectoryURL(NSURL.fileURLWithPath(NSSTR(InitDir))); |
| 177 |
|
| 178 |
MenuForcefullyDisabled := true; |
| 179 |
try |
| 180 |
Result := saveDlg.runModal = NSModalResponseOK; |
| 181 |
if Result then |
| 182 |
FileName := saveDlg.URL.path.UTF8String; |
| 183 |
|
| 184 |
// release everything |
| 185 |
saveDlg.release; |
| 186 |
finally |
| 187 |
MenuForcefullyDisabled := false; |
| 188 |
end; |
| 189 |
|
| 190 |
ref.makeKeyAndOrderFront(nil); |
| 191 |
end; |