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
|
unit CocoaDatePicker;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
interface
uses
Classes, SysUtils,
CocoaAll, CocoaUtils, CocoaPrivate, cocoa_extra;
type
IDatePickerCallback = interface(ICommonCallback)
Procedure MouseBtnUp;
end;
{ TCocoaDatePicker }
TCocoaDatePicker = objcclass(NSDatePicker)
public
callback: IDatePickerCallback;
autoResize: boolean;
retainAspectRatio: boolean;
function lclGetCallback: ICommonCallback; override;
procedure mouseDown(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
function acceptsFirstResponder: LCLObjCBoolean; override;
procedure setFrame(aframe: NSRect); override;
end;
implementation
procedure TCocoaDatePicker.mouseDown(event: NSEvent);
Var
oldDate, newDate: TDateTime;
begin
if assigned(callback) then
begin
// Save Date BEFORE mouse click/down event
oldDate:= NSDateToDateTime(Self.dateValue);
if not callback.MouseUpDownEvent(event) then
// Without this, Cocoa will not update our NSDatePicker date...
inherited mouseDown(event);
// After mouse event, has our date changed
newDate:= NSDateToDateTime(Self.dateValue);
if oldDate <> newDate then
callback.SendOnChange;
// This also calls OnClick....
callback.MouseBtnUp;
end;
end;
procedure TCocoaDatePicker.mouseMoved(event: NSEvent);
begin
if not callback.MouseMove(event) then
inherited mouseMoved(event);
end;
function TCocoaDatePicker.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := True;
end;
function TCocoaDatePicker.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaDatePicker.setFrame(aframe: NSRect);
var
fsz : NSSize;
sz : NSSize;
rt : double;
begin
inherited setFrame(aframe);
if not autoResize then Exit;
fsz:=fittingSize;
if (fsz.width=0) or (fsz.height=0) then Exit;
sz:=frame.size;
//don't resize if too small already
if (sz.width<fsz.width) or (sz.height<fsz.height) then Exit;
if retainAspectRatio and (fsz.height>0) and (sz.height<>0) then
begin
rt:=fsz.width/fsz.height;
fsz.width:=fsz.width * sz.width / (sz.height*rt);
end;
setBoundsSize(fsz);
end;
end.
|