1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
|
{
/***************************************************************************
CocoaInt.pas - CocoaInterface Object
----------------------------------------
Initial Revision : Mon August 6th CST 2004
***************************************************************************/
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit CocoaInt;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
{$include cocoadefines.inc}
interface
uses
// rtl+ftl
Types, Classes, SysUtils, Math, contnrs,
// carbon bindings
MacOSAll,
// interfacebase
LCLPlatformDef, InterfaceBase, GraphType,
// private
CocoaAll, CocoaPrivate, CocoaUtils, CocoaGDIObjects,
cocoa_extra, CocoaWSMenus, CocoaWSForms, CocoaWindows, CocoaScrollers,
CocoaWSClipboard, CocoaTextEdits, CocoaWSCommon,
// LCL
LCLStrConsts, LMessages, LCLMessageGlue, LCLProc, LCLIntf, LCLType,
Controls, Forms, Themes, Menus,
IntfGraphics, Graphics, CocoaWSFactory;
type
{ TCocoaTimerObject }
TCocoaTimerObject = objcclass(NSObject)
func: TWSTimerProc;
procedure timerEvent; message 'timerEvent';
class function newWithFunc(afunc: TWSTimerProc): TCocoaTimerObject; message 'newWithFunc:';
end;
TAppDelegate = objcclass(NSObject, NSApplicationDelegateProtocol)
procedure application_openFiles(sender: NSApplication; filenames: NSArray);
procedure applicationDidHide(notification: NSNotification);
procedure applicationDidUnhide(notification: NSNotification);
procedure applicationDidBecomeActive(notification: NSNotification);
procedure applicationDidResignActive(notification: NSNotification);
procedure applicationDidChangeScreenParameters(notification: NSNotification);
end;
{ TCocoaApplication }
TCocoaApplication = objcclass(NSApplication)
aloop : TApplicationMainLoop;
isrun : Boolean;
modals : NSMutableDictionary;
procedure dealloc; override;
function isRunning: LCLObjCBoolean; override;
procedure run; override;
procedure sendEvent(theEvent: NSEvent); override;
function nextEventMatchingMask_untilDate_inMode_dequeue(mask: NSUInteger; expiration: NSDate; mode: NSString; deqFlag: LCLObjCBoolean): NSEvent; override;
function runModalForWindow(theWindow: NSWindow): NSInteger; override;
end;
{ TModalSession }
TModalSession = class(TObject)
window : NSWindow;
sess : NSModalSession;
// recording menu state for the modality stack
// there's no limitation for a modal window to have its own menu
// if it override the mainMenu, we still need the information
// to restore the previous state of the mainmenu
prevMenuEnabled: Boolean;
cocoaMenu : NSMenu;
lclMenu : TMenu;
constructor Create(awin: NSWindow; asess: NSModalSession;
APrevMenuEnabled: Boolean;
amainmenu: NSMenu; ALCL: TMenu);
end;
{ TCocoaWidgetSet }
TCocoaWidgetSet = class(TWidgetSet)
private
FTerminating: Boolean;
FNSApp: NSApplication;
FNSApp_Delegate: TAppDelegate;
FCurrentCursor: HCursor;
FCaptureControl: HWND;
protected
FStockNullBrush: HBRUSH;
FStockBlackBrush: HBRUSH;
FStockLtGrayBrush: HBRUSH;
FStockGrayBrush: HBRUSH;
FStockDkGrayBrush: HBRUSH;
FStockWhiteBrush: HBRUSH;
FStockNullPen: HPEN;
FStockBlackPen: HPEN;
FStockWhitePen: HPEN;
FStockSystemFont: HFONT;
FStockFixedFont: HFONT;
FSysColorBrushes: array[0..MAX_SYS_COLORS] of HBrush;
// Sandboxing
SandboxingOn: Boolean;
fClipboard: TCocoaWSClipboard;
// Clipboard
procedure SyncClipboard();
function PromptUser(const DialogCaption, DialogMessage: String;
DialogType: longint; Buttons: PLongint; ButtonCount, DefaultIndex,
EscapeResult: Longint): Longint; override;
function GetAppHandle: THandle; override;
function CreateThemeServices: TThemeServices; override;
procedure SendCheckSynchronizeMessage;
procedure OnWakeMainThread(Sender: TObject);
procedure DoSetMainMenu(AMenu: NSMenu; ALCLMenu: TMenu);
public
// modal session
CurModalForm: NSWindow;
Modals : TList;
MainMenuEnabled: Boolean; // the latest main menu status
PrevMenu : NSMenu;
PrevLCLMenu : TMenu;
CurLCLMenu: TMenu;
PrevMenuEnabled: Boolean; // previous mainmenu status
constructor Create; override;
destructor Destroy; override;
function LCLPlatform: TLCLPlatform; override;
procedure AppInit(var ScreenInfo: TScreenInfo); override;
procedure AppRun(const ALoop: TApplicationMainLoop); override;
procedure AppWaitMessage; override;
procedure AppProcessMessages; override;
procedure AppTerminate; override;
procedure AppMinimize; override;
procedure AppRestore; override;
procedure AppBringToFront; override;
procedure AppSetIcon(const Small, Big: HICON); override;
procedure AppSetTitle(const ATitle: string); override;
function GetLCLCapability(ACapability: TLCLCapability): PtrUInt; override;
function CreateTimer(Interval: integer; TimerFunc: TWSTimerProc): THandle; override;
function DestroyTimer(TimerHandle: THandle): boolean; override;
function NewUserEventInfo(Handle: HWND; Msg: Cardinal; wParam: WParam; lParam: LParam): NSMutableDictionary;
function PrepareUserEvent(Handle: HWND; Info: NSDictionary; NeedsResult: Boolean): NSEvent;
procedure InitStockItems;
procedure FreeStockItems;
procedure FreeSysColorBrushes;
procedure SetMainMenu(const AMenu: HMENU; const ALCLMenu: TMenu);
function StartModal(awin: NSWindow; hasMenu: Boolean): Boolean;
procedure EndModal(awin: NSWindow);
function isModalSession: Boolean;
{todo:}
function DCGetPixel(CanvasHandle: HDC; X, Y: integer): TGraphicsColor; override;
procedure DCSetPixel(CanvasHandle: HDC; X, Y: integer; AColor: TGraphicsColor); override;
procedure DCRedraw(CanvasHandle: HDC); override;
procedure DCSetAntialiasing(CanvasHandle: HDC; AEnabled: Boolean); override;
procedure SetDesigning(AComponent: TComponent); override;
function RawImage_DescriptionFromCocoaBitmap(out ADesc: TRawImageDescription; ABitmap: TCocoaBitmap): Boolean;
function RawImage_FromCocoaBitmap(out ARawImage: TRawImage; ABitmap, AMask: TCocoaBitmap; ARect: PRect = nil): Boolean;
function RawImage_DescriptionToBitmapType(ADesc: TRawImageDescription; out bmpType: TCocoaBitmapType): Boolean;
function GetImagePixelData(AImage: CGImageRef; out bitmapByteCount: PtrUInt): Pointer;
class function Create32BitAlphaBitmap(ABitmap, AMask: TCocoaBitmap): TCocoaBitmap;
property NSApp: NSApplication read FNSApp;
property CurrentCursor: HCursor read FCurrentCursor write FCurrentCursor;
property CaptureControl: HWND read FCaptureControl;
// the winapi compatibility methods
{$I cocoawinapih.inc}
// the extra LCL interface methods
{$I cocoalclintfh.inc}
end;
var
CocoaWidgetSet: TCocoaWidgetSet;
CocoaBasePPI : Integer = 96; // for compatiblity with LCL 1.8 release. The macOS base is 72ppi
MainPool : NSAutoreleasePool = nil;
ColorToolTip : TColorRef = $C9FCF9; // default = macosx10.4 yellow color. (See InitInternals below)
// it's likely the tooltip color will change in future.
// Thus the variable is left public, so a user of LCL
// would be able to initialize it properly on start
function CocoaScrollBarSetScrollInfo(bar: TCocoaScrollBar; const ScrollInfo: TScrollInfo): Integer;
function CocoaScrollBarGetScrollInfo(bar: TCocoaScrollBar; var ScrollInfo: TScrollInfo): Boolean;
procedure NSScrollerGetScrollInfo(docSz, pageSz: CGFloat; rl: NSSCroller; Var ScrollInfo: TScrollInfo);
procedure NSScrollViewGetScrollInfo(sc: NSScrollView; BarFlag: Integer; Var ScrollInfo: TScrollInfo);
procedure NSScrollerSetScrollInfo(docSz, pageSz: CGFloat; rl: NSSCroller; const ScrollInfo: TScrollInfo);
procedure NSScrollViewSetScrollPos(sc: NSScrollView; BarFlag: Integer; const ScrollInfo: TScrollInfo);
function CocoaPromptUser(const DialogCaption, DialogMessage: String;
DialogType: longint; Buttons: PLongint; ButtonCount, DefaultIndex,
EscapeResult: Longint;
sheetOfWindow: NSWindow = nil): Longint;
implementation
// NSCursor doesn't support any wait cursor, so we need to use a non-native one
// Not supporting it at all would result in crashes in Screen.Cursor := crHourGlass;
{$R ../../cursor_hourglass.res}
uses
dl,dynlibs,
CocoaCaret,
CocoaThemes;
function CocoaScrollBarSetScrollInfo(bar: TCocoaScrollBar; const ScrollInfo: TScrollInfo): Integer;
var
pg : Integer;
mn : Integer;
mx : Integer;
dl : Integer;
begin
if not Assigned(bar) then
begin
Result := 0;
Exit;
end;
if ScrollInfo.fMask and SIF_PAGE>0 then
begin
pg:=ScrollInfo.nPage;
end
else pg:=bar.pageInt;
if ScrollInfo.fMask and SIF_RANGE>0 then
begin
mn:=ScrollInfo.nMin;
mx:=ScrollInfo.nMax;
end
else
begin
mn:=bar.minInt;
mx:=bar.maxInt;
end;
dl:=mx-mn;
{$ifdef BOOLFIX}
bar.setEnabled_(Ord(dl<>0));
{$else}
bar.SetEnabled(dl<>0);
{$endif}
// if changed page or range, the knob changes
if ScrollInfo.fMask and (SIF_RANGE or SIF_PAGE)>0 then
begin
if dl<>0 then
bar.setKnobProportion(pg/dl)
else
bar.setKnobProportion(1);
bar.pageInt:=pg;
bar.minInt:=mn;
bar.maxInt:=mx;
end;
if ScrollInfo.fMask and SIF_POS > 0 then
bar.lclSetPos( ScrollInfo.nPos );
Result:=bar.lclPos;
end;
function CocoaScrollBarGetScrollInfo(bar: TCocoaScrollBar; var ScrollInfo: TScrollInfo): Boolean;
var
l : integer;
begin
Result:=Assigned(bar);
if not Result then Exit;
FillChar(ScrollInfo, sizeof(ScrollInfo), 0);
ScrollInfo.cbSize:=sizeof(ScrollInfo);
ScrollInfo.fMask:=SIF_ALL;
ScrollInfo.nMin:=bar.minInt;
ScrollInfo.nMax:=bar.maxInt;
ScrollInfo.nPage:=bar.pageInt;
ScrollInfo.nPos:=bar.lclPos;
ScrollInfo.nTrackPos:=ScrollInfo.nPos;
Result:=true;
end;
procedure NSScrollerGetScrollInfo(docSz, pageSz: CGFloat; rl: NSSCroller; Var ScrollInfo: TScrollInfo);
begin
ScrollInfo.cbSize:=sizeof(ScrollInfo);
ScrollInfo.fMask:=SIF_ALL;
ScrollInfo.nPos:=round(rl.floatValue*(docSz-pageSz));
ScrollInfo.nTrackPos:=ScrollInfo.nPos;
ScrollInfo.nMin:=0;
ScrollInfo.nMax:=round(docSz);
ScrollInfo.nPage:=round(rl.knobProportion*docSz);
end;
procedure NSScrollViewGetScrollInfo(sc: NSScrollView; BarFlag: Integer; Var ScrollInfo: TScrollInfo);
var
ns : NSView;
vr : NSRect;
begin
ns:=sc.documentView;
if not Assigned(ns) then begin
FillChar(ScrollInfo, sizeof(ScrollInfo),0);
ScrollInfo.cbSize:=sizeof(ScrollInfo);
Exit;
end;
vr:=sc.documentVisibleRect;
if BarFlag = SB_Vert then
NSScrollerGetScrollInfo(ns.frame.size.height, vr.size.height, sc.verticalScroller, ScrollInfo)
else
NSScrollerGetScrollInfo(ns.frame.size.width, vr.size.width, sc.horizontalScroller, ScrollInfo);
end;
procedure NSScrollerSetScrollInfo(docSz, pageSz: CGFloat; rl: NSSCroller; const ScrollInfo: TScrollInfo);
var
sz : CGFloat;
begin
if ScrollInfo.fMask and SIF_POS>0 then begin
sz:=docSz-pageSz;
if sz=0 then rl.setFloatValue(0)
else rl.setFloatValue(ScrollInfo.nPos/sz);
end;
if ScrollInfo.fMask and SIF_PAGE>0 then begin
sz:=docSz-pageSz;
if sz=0 then rl.setKnobProportion(1)
else rl.setKnobProportion(1/sz);
end;
end;
procedure NSScrollViewSetScrollPos(sc: NSScrollView; BarFlag: Integer; const ScrollInfo: TScrollInfo);
var
ns : NSView;
vr : NSRect;
begin
ns:=sc.documentView;
if not Assigned(ns) then Exit;
vr:=sc.documentVisibleRect;
if BarFlag = SB_Vert then
begin
//NSScrollerSetScrollInfo(ns.frame.size.height, sc.verticalScroller, ScrollInfo)
if not sc.documentView.isFlipped then
vr.origin.y := sc.documentView.frame.size.height - ScrollInfo.nPos - vr.size.Height
else
vr.origin.y := ScrollInfo.nPos;
end
else
begin
//NSScrollerSetScrollInfo(ns.frame.size.width, sc.horizontalScroller, ScrollInfo);
vr.origin.x:=ScrollInfo.nPos;
end;
ns.scrollRectToVisible(vr);
end;
{ TModalSession }
constructor TModalSession.Create(awin: NSWindow; asess: NSModalSession;
APrevMenuEnabled: Boolean; amainmenu: NSMenu; ALCL: TMenu);
begin
inherited Create;
window := awin;
sess := asess;
prevMenuEnabled := APrevMenuEnabled;
cocoaMenu := amainmenu;
lclMenu := alcl;
end;
{ TCocoaApplication }
procedure TCocoaApplication.dealloc;
begin
if Assigned(modals) then modals.release;
inherited dealloc;
end;
function TCocoaApplication.isRunning: LCLObjCBoolean;
begin
Result:=isrun;
end;
procedure TCocoaApplication.run;
begin
isrun:=true;
aloop();
end;
procedure ForwardMouseMove(app: NSApplication; theEvent: NSEvent);
var
w : NSWindow;
kw : NSWindow;
ev : NSEvent;
p : NSPoint;
wfr : NSRect;
begin
kw := app.keyWindow;
// mouse move was consumed by the focused window
if Assigned(kw) and NSPointInRect( theEvent.mouseLocation, kw.frame) then
Exit;
for w in app.windows do
begin
if w = kw then Continue;
if not w.isVisible then Continue;
// todo: check for enabled windows? modal windows?
wfr := w.frame;
if not NSPointInRect( theEvent.mouseLocation, wfr) then Continue;
p := theEvent.mouseLocation;
p.x := p.x - w.frame.origin.x;
p.y := p.y - w.frame.origin.y;
ev := NSEvent.mouseEventWithType_location_modifierFlags_timestamp_windowNumber_context_eventNumber_clickCount_pressure(
theEvent.type_,
p,
theEvent.modifierFlags,
theEvent.timestamp,
w.windowNumber,
theEvent.context,
theEvent.eventNumber,
theEvent.clickCount,
theEvent.pressure
);
w.sendEvent(ev);
end;
end;
procedure TCocoaApplication.sendEvent(theEvent: NSEvent);
begin
// https://stackoverflow.com/questions/4001565/missing-keyup-events-on-meaningful-key-combinations-e-g-select-till-beginning
if (theEvent.type_ = NSKeyUp) and
((theEvent.modifierFlags and NSCommandKeyMask) = NSCommandKeyMask)
then
self.keyWindow.sendEvent(theEvent);
inherited sendEvent(theEvent);
if (theEvent.type_ = NSMouseMoved) then ForwardMouseMove(Self, theEvent);
end;
function isMouseMoveEvent(tp: NSEventType): Boolean; inline;
begin
Result := (tp = NSMouseMoved)
or (tp = NSLeftMouseDragged)
or (tp = NSRightMouseDragged)
or (tp = NSOtherMouseDragged);
end;
function TCocoaApplication.nextEventMatchingMask_untilDate_inMode_dequeue(
mask: NSUInteger; expiration: NSDate; mode: NSString; deqFlag: LCLObjCBoolean
): NSEvent;
var
cb : ICommonCallback;
begin
{$ifdef BOOLFIX}
Result:=inherited nextEventMatchingMask_untilDate_inMode_dequeue_(mask,
expiration, mode, Ord(deqFlag));
{$else}
Result:=inherited nextEventMatchingMask_untilDate_inMode_dequeue(mask,
expiration, mode, deqFlag);
{$endif}
if Assigned(Result)
and ((mode = NSEventTrackingRunLoopMode) or mode.isEqualToString(NSEventTrackingRunLoopMode))
and Assigned(TrackedControl)
then
begin
if Result.type_ = NSLeftMouseUp then
begin
//todo: send callback!
TrackedControl := nil;
end
else
if isMouseMoveEvent(Result.type_) then
begin
cb := TrackedControl.lclGetCallback;
if Assigned(cb) then cb.MouseMove(Result);
end;
end;
end;
function TCocoaApplication.runModalForWindow(theWindow: NSWindow): NSInteger;
begin
ApplicationWillShowModal;
Result:=inherited runModalForWindow(theWindow);
end;
// the implementation of the utility methods
{$I cocoaobject.inc}
// the implementation of the winapi compatibility methods
{$I cocoawinapi.inc}
// the implementation of the extra LCL interface methods
{$I cocoalclintf.inc}
procedure InternalInit;
begin
// MacOSX 10.6 reports a lot of warnings during initialization process
// adding the autorelease pool for the whole Cocoa widgetset
MainPool := NSAutoreleasePool.alloc.init;
// Apple doesn't provide any reasonable way of aquiring tooltip bk color
// todo: The tooltip color could be different depending on "light" or "dark"
// mode selected in the system. Thus actual Theme Drawing should be used
// and implemeneted.
if NSAppKitVersionNumber >= NSAppKitVersionNumber10_10 then
ColorTooltip := $EDEDED;
end;
procedure InternalFinal;
begin
if Assigned(MainPool) then MainPool.release;
end;
procedure TCocoaWidgetSet.DoSetMainMenu(AMenu: NSMenu; ALCLMenu: TMenu);
var
i: Integer;
lCurItem: TMenuItem;
lMenuObj: NSObject;
lNSMenu: NSMenu absolute AMenu;
begin
if Assigned(PrevMenu) then PrevMenu.release;
PrevMenu := NSApplication(NSApp).mainMenu;
PrevMenu.retain;
PrevLCLMenu := CurLCLMenu;
NSApp.setMainMenu(lNSMenu);
CurLCLMenu := ALCLMenu;
if (ALCLMenu = nil) or not ALCLMenu.HandleAllocated then Exit;
// Find the Apple menu, if the user provided any by setting the Caption to
// Some older docs say we should use setAppleMenu to obtain the Services/Hide/Quit items,
// but its now private and in 10.10 it doesn't seam to do anything
// NSApp.setAppleMenu(NSMenu(lMenuObj));
for i := 0 to ALCLMenu.Items.Count-1 do
begin
lCurItem := ALCLMenu.Items.Items[i];
if not lNSMenu.isKindOfClass_(TCocoaMenu) then Break;
if not lCurItem.HandleAllocated then Continue;
lMenuObj := NSObject(lCurItem.Handle);
if not lMenuObj.isKindOfClass_(TCocoaMenuItem) then Continue;
if TCocoaMenuItem(lMenuObj).isValidAppleMenu() then
begin
TCocoaMenu(lNSMenu).overrideAppleMenu(TCocoaMenuItem(lMenuObj));
Break;
end;
end;
end;
procedure TCocoaWidgetSet.SetMainMenu(const AMenu: HMENU; const ALCLMenu: TMenu);
begin
if AMenu<>0 then
begin
DoSetMainMenu(NSMenu(AMenu), ALCLMenu);
PrevMenuEnabled := MainMenuEnabled;
MainMenuEnabled := true;
ToggleAppMenu(true);
//if not Assigned(ACustomForm.Menu) then ToggleAppMenu(false);
// for modal windows work around bug, but doesn't work :(
{$ifdef COCOA_USE_NATIVE_MODAL}
{if CurModalForm <> nil then
for i := 0 to lNSMenu.numberOfItems()-1 do
begin
lNSMenu.itemAtIndex(i).setTarget(TCocoaWSCustomForm.GetWindowFromHandle(CurModalForm));
end;}
{$endif}
end;
end;
function TCocoaWidgetSet.StartModal(awin: NSWindow; hasMenu: Boolean): Boolean;
var
sess : NSModalSession;
lvl : NSInteger;
begin
Result := false;
if not Assigned(awin) then Exit;
lvl := awin.level;
sess := NSApplication(NSApp).beginModalSessionForWindow(awin);
if not Assigned(sess) then Exit;
// beginModalSession "configures" the modality and potentially is changing window level
awin.setLevel(lvl);
if not Assigned(Modals) then Modals := TList.Create;
// If a modal menu has it's menu, then SetMainMenu has already been called
// (Show is called for modal windows prior to ShowModal. Show triggers Activate and Active is doing MainMenu)
if not hasMenu then begin
Modals.Add( TModalSession.Create(awin, sess, MainMenuEnabled, NSApplication(NSApp).mainMenu, CurLCLMenu));
MainMenuEnabled := false;
ToggleAppMenu(false); // modal menu doesn't have a window, disabling it
end else
// if modal window has its own menu, then the prior window is rescord in "Prev" fields
Modals.Add( TModalSession.Create(awin, sess, PrevMenuEnabled, PrevMenu, PrevLCLMenu));
Result := true;
end;
procedure TCocoaWidgetSet.EndModal(awin: NSWindow);
var
ms : TModalSession;
begin
if not Assigned(Modals) or (Modals.Count = 0) then Exit;
ms := TModalSession(Modals[Modals.Count-1]);
if (ms.window <> awin) then Exit;
NSApplication(NSApp).endModalSession(ms.sess);
// restoring the menu status that was before the modality
DoSetMainMenu(ms.cocoaMenu, ms.lclMenu);
PrevMenuEnabled := MainMenuEnabled;
MainMenuEnabled := ms.prevMenuEnabled;
ToggleAppMenu(ms.prevMenuEnabled); // modal menu doesn't have a window, disabling it
ms.Free;
Modals.Delete(Modals.Count-1);
end;
function TCocoaWidgetSet.isModalSession: Boolean;
begin
Result := Assigned(Modals) and (Modals.Count > 0);
end;
initialization
// {$I Cocoaimages.lrs}
InternalInit;
finalization
InternalFinal;
end.
|