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
|
{ $Id: $}
{ --------------------------------------------
cocoabuttons.pas - Cocoa internal classes
--------------------------------------------
This unit contains the private classhierarchy for the Cocoa implemetations
*****************************************************************************
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 CocoaButtons;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
{$interfaces corba}
{$include cocoadefines.inc}
interface
uses
// rtl+ftl
Types, Classes, SysUtils,
CGGeometry,
// Libs
MacOSAll, CocoaAll, CocoaUtils, CocoaGDIObjects,
cocoa_extra, CocoaPrivate
// LCL
,Graphics;
const
// these heights were received from Xcode interface builder,
// where the height cannot be changed for a button control the actual size
// of the button (the difference between top pixel and bottom pixel,
// is less than frame size also
PUSHBTN_REG_HEIGHT = 20;
PUSHBTN_SMALL_HEIGHT = 17;
PUSHBTN_MINI_HEIGHT = 14;
type
{ IButtonCallback }
IButtonCallback = interface(ICommonCallback)
procedure ButtonClick;
procedure GetAllowMixedState(var allowed: Boolean);
end;
{ TCocoaButton }
TCocoaButton = objcclass(NSButton)
protected
procedure actionButtonClick(sender: NSObject); message 'actionButtonClick:';
procedure boundsDidChange(sender: NSNotification); message 'boundsDidChange:';
procedure frameDidChange(sender: NSNotification); message 'frameDidChange:';
public
callback: IButtonCallback;
Glyph: TBitmap;
smallHeight: integer;
miniHeight: integer;
adjustFontToControlSize: Boolean;
procedure dealloc; override;
function initWithFrame(frameRect: NSRect): id; override;
function acceptsFirstResponder: LCLObjCBoolean; override;
procedure drawRect(dirtyRect: NSRect); override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
// keyboard
procedure keyDown(event: NSEvent); override;
function performKeyEquivalent(event: NSEvent): LCLObjCBoolean; override;
// mouse
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseEntered(event: NSEvent); override;
procedure mouseExited(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
procedure resetCursorRects; override;
// lcl overrides
procedure lclSetFrame(const r: TRect); override;
procedure lclCheckMixedAllowance; message 'lclCheckMixedAllowance';
function lclGetFrameToLayoutDelta: TRect; override;
// cocoa
procedure setState(astate: NSInteger); override;
end;
IStepperCallback = interface(ICommonCallback)
procedure BeforeChange(var Allowed: Boolean);
procedure Change(NewValue: Double; isUpPressed: Boolean; var Allowed: Boolean);
procedure UpdownClick(isUpPressed: Boolean);
end;
{ TCocoaStepper }
TCocoaStepper = objcclass(NSStepper)
callback: IStepperCallback;
lastValue: Double;
procedure stepperAction(sender: NSObject); message 'stepperAction:';
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
end;
implementation
{ TCocoaStepper }
procedure TCocoaStepper.stepperAction(sender: NSObject);
var
newval : Double;
allowChange : Boolean;
updownpress : Boolean;
begin
newval := doubleValue;
allowChange := true;
updownpress := newval > lastValue;
if Assigned(callback) then begin
callback.BeforeChange(allowChange);
callback.Change(newval, updownpress, allowChange);
end;
if not allowChange then
setDoubleValue(lastValue)
else
lastValue := doubleValue;
if Allowchange and Assigned(callback) then callback.UpdownClick(updownpress);
end;
procedure TCocoaStepper.mouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
begin
inherited mouseDown(event);
if Assigned(Callback) then
callback.MouseUpDownEvent(event, true);
end;
end;
procedure TCocoaStepper.mouseUp(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited mouseUp(event);
end;
procedure TCocoaStepper.rightMouseDown(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaStepper.rightMouseUp(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaStepper.otherMouseDown(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaStepper.otherMouseUp(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaStepper.mouseDragged(event: NSEvent);
begin
if not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaStepper.mouseMoved(event: NSEvent);
begin
if not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
{ TCocoaButton }
procedure TCocoaButton.lclSetFrame(const r: TRect);
var
lBtnHeight, lDiff: Integer;
lRoundBtnSize: NSSize;
begin
// NSTexturedRoundedBezelStyle should be the preferred style, but it has a fixed height!
// fittingSize is 10.7+
{ if respondsToSelector(objcselector('fittingSize')) then
begin
lBtnHeight := r.Bottom - r.Top;
lRoundBtnSize := fittingSize();
lDiff := Abs(Round(lRoundBtnSize.Height) - lBtnHeight);
if lDiff < 4 then // this nr of pixels maximum size difference is arbitrary and we could choose another number
setBezelStyle(NSTexturedRoundedBezelStyle)
else
setBezelStyle(NSTexturedSquareBezelStyle);
end
else
setBezelStyle(NSTexturedSquareBezelStyle);
}
if (miniHeight<>0) or (smallHeight<>0) then
SetNSControlSize(Self,r.Bottom-r.Top,miniHeight, smallHeight, adjustFontToControlSize);
inherited lclSetFrame(r);
end;
procedure TCocoaButton.lclCheckMixedAllowance;
var
allowed : Boolean;
begin
if allowsMixedState and Assigned(callback) then begin
allowed := false;
callback.GetAllowMixedState(allowed);
if not allowed then begin
// "mixed" should be following by "On" state
// lclCheckMixedAllowance is called prior to changing
// the state. So the state needs to be switched to "Off"
// so it could be then switched to "On" by Cocoa
if state = NSMixedState then
inherited setState(NSOffState);
{$ifdef BOOLFIX}
setAllowsMixedState_(Ord(false));
{$else}
setAllowsMixedState(false);
{$endif}
end;
end;
end;
function TCocoaButton.lclGetFrameToLayoutDelta: TRect;
begin
case bezelStyle of
NSPushOnPushOffButton:
begin
// todo: on 10.7 or later there's a special API for that!
// The data is received from 10.6 Interface Builder
case NSCell(Self.Cell).controlSize of
NSSmallControlSize: begin
Result.Left := 5;
Result.Top := 4;
Result.Right := -5;
Result.Bottom := -7;
end;
NSMiniControlSize: begin
Result.Left := 1;
Result.Top := 0;
Result.Right := -1;
Result.Bottom := -2;
end;
else
// NSRegularControlSize
Result.Left := 6;
Result.Top := 4;
Result.Right := -6;
Result.Bottom := -8;
end;
end;
else
Result := inherited lclGetFrameToLayoutDelta;
end;
end;
procedure TCocoaButton.setState(astate: NSInteger);
var
ch : Boolean;
begin
ch := astate<>state;
inherited setState(astate);
if Assigned(callback) and ch then callback.SendOnChange;
end;
procedure TCocoaButton.actionButtonClick(sender: NSObject);
begin
// this is the action handler of button
if Assigned(callback) then
callback.ButtonClick;
end;
procedure TCocoaButton.boundsDidChange(sender: NSNotification);
begin
if Assigned(callback) then
callback.boundsDidChange(self);
end;
procedure TCocoaButton.frameDidChange(sender: NSNotification);
begin
if Assigned(callback) then
callback.frameDidChange(self);
end;
procedure TCocoaButton.dealloc;
begin
if Assigned(Glyph) then
FreeAndNil(Glyph);
inherited dealloc;
end;
function TCocoaButton.initWithFrame(frameRect: NSRect): id;
begin
Result := inherited initWithFrame(frameRect);
if Assigned(Result) then
begin
setTarget(Self);
setAction(objcselector('actionButtonClick:'));
// todo: find a way to release notifications below
// NSNotificationCenter.defaultCenter.addObserver_selector_name_object(Self, objcselector('boundsDidChange:'), NSViewBoundsDidChangeNotification, Result);
// NSNotificationCenter.defaultCenter.addObserver_selector_name_object(Self, objcselector('frameDidChange:'), NSViewFrameDidChangeNotification, Result);
// Result.setPostsBoundsChangedNotifications(True);
// Result.setPostsFrameChangedNotifications(True);
end;
end;
function TCocoaButton.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := True;
end;
procedure TCocoaButton.drawRect(dirtyRect: NSRect);
var ctx: NSGraphicsContext;
begin
inherited drawRect(dirtyRect);
if CheckMainThread and Assigned(callback) then
callback.Draw(NSGraphicsContext.currentContext, bounds, dirtyRect);
end;
function TCocoaButton.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaButton.lclClearCallback;
begin
callback := nil;
end;
procedure TCocoaButton.keyDown(event: NSEvent);
begin
if event.keyCode = kVK_Space then
lclCheckMixedAllowance;
inherited keyDown(event);
end;
function TCocoaButton.performKeyEquivalent(event: NSEvent): LCLObjCBoolean;
begin
// "Return" is a keyEquivalent for a "default" button"
// LCL provides its own mechanism for handling default buttons
if (keyEquivalent.length = 1) and (keyEquivalentModifierMask = 0) and
(keyEquivalent.characterAtIndex(0) = NSCarriageReturnCharacter) then
Result := False
else
Result := inherited performKeyEquivalent(event);
end;
procedure TCocoaButton.mouseUp(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited mouseUp(event);
end;
procedure TCocoaButton.rightMouseDown(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaButton.rightMouseUp(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaButton.otherMouseDown(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaButton.otherMouseUp(event: NSEvent);
begin
if not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaButton.resetCursorRects;
begin
if not callback.resetCursorRects then
inherited resetCursorRects;
end;
procedure TCocoaButton.mouseDown(event: NSEvent);
begin
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
begin
lclCheckMixedAllowance;
// We need to call the inherited regardless of the result of the call to
// MouseUpDownEvent otherwise mouse clicks don't work, see bug 30131
inherited mouseDown(event);
if Assigned(callback) then
callback.MouseUpDownEvent(event, true);
end;
end;
procedure TCocoaButton.mouseDragged(event: NSEvent);
begin
if not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaButton.mouseEntered(event: NSEvent);
begin
inherited mouseEntered(event);
end;
procedure TCocoaButton.mouseExited(event: NSEvent);
begin
inherited mouseExited(event);
end;
procedure TCocoaButton.mouseMoved(event: NSEvent);
begin
if not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
end.
|