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
|
{
*****************************************************************************
* CocoaWSCheckLst.pp *
* --------------- *
* *
* *
*****************************************************************************
*****************************************************************************
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 CocoaWSCheckLst;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
interface
uses
// Libs
MacOSAll, CocoaAll, Classes, sysutils,
// LCL
Controls, StdCtrls, CheckLst, LCLType,
// Widgetset
WSCheckLst, WSLCLClasses,
// LCL Cocoa
CocoaWSCommon, CocoaPrivate, CocoaUtils, CocoaWSStdCtrls, CocoaTables, CocoaGDIObjects,
CocoaScrollers
,LCLMessageGlue;
type
{ TCocoaCheckStringList }
TCocoaCheckStringList = class(TCocoaStringList)
protected
procedure ExchangeItems(Index1, Index2: Integer); override;
public
ChkState : array of SInt8;
procedure InsertItem(Index: Integer; const S: string; O: TObject); override;
procedure Delete(Index: Integer); override;
procedure Clear; override;
end;
{ TLCLCheckboxListCallback }
TLCLCheckboxListCallback = class(TLCLListBoxCallback, IListViewCallback)
protected
function AllocStrings(ATable: NSTableView): TCocoaStringList; override;
public
checklist: TCustomCheckListBox;
constructor Create(AOwner: NSObject; ATarget: TWinControl; AHandleView: NSView); override;
function GetItemCheckedAt(ARow, ACol: Integer; var CheckState: Integer): Boolean; override;
procedure SetItemCheckedAt(ARow, ACol: Integer; CheckState: Integer); override;
function GetCheckState(Index: Integer; var AState: Integer): Boolean;
function SetCheckState(Index: Integer; AState: Integer; InvalidateCocoa: Boolean = true): Boolean;
end;
{ TCocoaWSCustomCheckListBox }
TCocoaWSCustomCheckListBox = class(TWSCustomCheckListBox)
published
class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
class function GetState(const ACheckListBox: TCustomCheckListBox; const AIndex: integer): TCheckBoxState; override;
class procedure SetState(const ACheckListBox: TCustomCheckListBox; const AIndex: integer; const AState: TCheckBoxState); override;
end;
function CtrlToCheckList(ctrl: TWinControl; out tbl: TCocoaTableListView; out cb: TLCLCheckboxListCallback): Boolean;
implementation
function CtrlToCheckList(ctrl: TWinControl; out tbl: TCocoaTableListView; out cb: TLCLCheckboxListCallback): Boolean;
begin
Result := Assigned(ctrl) and (ctrl.HandleAllocated) and (ctrl.Handle <> 0);
if not Result then begin
tbl := nil;
cb := nil;
Exit;
end;
tbl:=TCocoaTableListView(NSSCrollView(ctrl.Handle).documentView);
Result := Assigned(tbl);
if Result then
cb := TLCLCheckboxListCallback(tbl.lclGetCallback.GetCallbackObject)
else
cb := nil;
end;
{ TCocoaCheckStringList }
procedure TCocoaCheckStringList.ExchangeItems(Index1, Index2: Integer);
var
t : Integer;
begin
inherited ExchangeItems(Index1, Index2);
t := ChkState[Index1];
ChkState[Index1] := ChkState[Index2];
ChkState[Index2] := t;
end;
procedure TCocoaCheckStringList.InsertItem(Index: Integer; const S: string;
O: TObject);
var
cnt : integer;
sz : integer;
begin
cnt := Count;
inherited InsertItem(Index, S, O);
if length(ChkState)<Capacity then
SetLength(ChkState, Capacity);
sz := (cnt - Index) * sizeof(SInt8);
if sz>0 then System.Move(ChkState[Index], ChkState[Index+1], sz);
ChkState[Index] := 0;
end;
procedure TCocoaCheckStringList.Delete(Index: Integer);
var
sz : Integer;
begin
inherited Delete(Index);
sz := (Count - Index) * sizeof(SInt8);
if (sz>0) and (Index < Count) then
System.Move(ChkState[Index+1], ChkState[Index], sz);
end;
procedure TCocoaCheckStringList.Clear;
begin
inherited Clear;
SetLength(ChkState, 0);
end;
{ TLCLCheckboxListCallback }
function TLCLCheckboxListCallback.AllocStrings(ATable: NSTableView): TCocoaStringList;
begin
Result:=TCocoaCheckStringList.Create(ATable);
end;
constructor TLCLCheckboxListCallback.Create(AOwner: NSObject; ATarget: TWinControl; AHandleView: NSView);
begin
inherited Create(AOwner, ATarget, AHandleView);
if ATarget is TCustomCheckListBox then
checklist := TCustomCheckListBox(ATarget);
end;
function TLCLCheckboxListCallback.GetItemCheckedAt(ARow, ACol: Integer;
var CheckState: Integer): Boolean;
begin
Result := GetCheckState(Arow, CheckState);
end;
procedure TLCLCheckboxListCallback.SetItemCheckedAt(ARow, ACol: Integer;
CheckState: Integer);
var
changed : Boolean;
begin
changed := SetCheckState(ARow, CheckState, false); // returns true, if changed!s
if changed then LCLSendChangedMsg(Target, ARow);
end;
function TLCLCheckboxListCallback.GetCheckState(Index: Integer; var AState: Integer): Boolean;
var
chkstr : TCocoaCheckStringList;
begin
Result := Assigned(strings) and (Index>=0) and (Index<strings.Count);
if Result then
begin
chkstr := TCocoaCheckStringList(strings);
AState := chkstr.ChkState[Index];
end
else
ASTate := 0;
end;
function TLCLCheckboxListCallback.SetCheckState(Index: Integer; AState: Integer;
InvalidateCocoa: Boolean = true): Boolean;
var
chkstr : TCocoaCheckStringList;
begin
Result := Assigned(Strings) and (Index>=0) and (Index<strings.Count);
if not Result then Exit;
chkstr := TCocoaCheckStringList(strings);
Result := chkstr.ChkState[Index] <> AState;
if Result then
begin
chkstr.ChkState[Index] := AState;
if InvalidateCocoa and Assigned(listview) then
listview.reloadDataForRow_column(Index, 0);
end;
end;
{ TCocoaWSCustomCheckListBox }
{------------------------------------------------------------------------------
Method: TCocoaWSCustomCheckListBox.CreateHandle
Params: AWinControl - LCL control
AParams - Creation parameters
Returns: Handle to the control in Cocoa interface
Creates new check list box in Cocoa interface with the specified parameters
------------------------------------------------------------------------------}
class function TCocoaWSCustomCheckListBox.CreateHandle(
const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle;
var
list: TCocoaTableListView;
scroll: TCocoaScrollView;
begin
list := AllocCocoaTableListView.lclInitWithCreateParams(AParams);
if not Assigned(list) then
begin
Result := 0;
Exit;
end;
list.callback := TLCLCheckboxListCallback.CreateWithView(list, AWinControl);
list.lclSetFirstColumCheckboxes(true);
//list.list := TCocoaStringList.Create(list);
list.addTableColumn(NSTableColumn.alloc.init);
list.setHeaderView(nil);
list.setDataSource(list);
list.setDelegate(list);
list.readOnly := true;
//todo:
//list.AllowMixedState := TCustomCheckListBox(AWinControl).AllowGrayed;
list.isOwnerDraw := TCustomCheckListBox(AWinControl).Style in [lbOwnerDrawFixed, lbOwnerDrawVariable];
scroll := EmbedInScrollView(list);
if not Assigned(scroll) then
begin
list.dealloc;
Result := 0;
Exit;
end;
scroll.callback := list.callback;
scroll.setHasVerticalScroller(true);
scroll.setAutohidesScrollers(true);
ScrollViewSetBorderStyle(scroll, TCustomCheckListBox(AWinControl).BorderStyle);
UpdateFocusRing(list, TCustomCheckListBox(AWinControl).BorderStyle);
Result := TLCLIntfHandle(scroll);
end;
{------------------------------------------------------------------------------
Method: TCocoaWSCustomCheckListBox.GetState
Params: ACustomCheckListBox - LCL custom check list box
AIndex - Item index
Returns: If the specified item in check list box in Cocoa interface is
checked, grayed or unchecked
------------------------------------------------------------------------------}
class function TCocoaWSCustomCheckListBox.GetState(
const ACheckListBox: TCustomCheckListBox; const AIndex: integer): TCheckBoxState;
var
tbl: TCocoaTableListView;
cb : TLCLCheckboxListCallback;
cocoaSt: Integer;
begin
if not CtrlToCheckList(ACheckListBox, tbl, cb) then begin
Result := cbUnchecked;
Exit;
end;
if cb.GetCheckState(AIndex, cocoaSt) then
case cocoaSt of
NSOnState : Result := cbChecked;
NSMixedState : Result := cbGrayed;
else
Result := cbUnchecked;
end
else
Result := cbUnchecked;
end;
{------------------------------------------------------------------------------
Method: TCocoaWSCustomCheckListBox.SetState
Params: ACustomCheckListBox - LCL custom check list box
AIndex - Item index to change checked value
AChecked - New checked value
Changes checked value of item with the specified index of check list box in
Cocoa interface
------------------------------------------------------------------------------}
class procedure TCocoaWSCustomCheckListBox.SetState(
const ACheckListBox: TCustomCheckListBox; const AIndex: integer;
const AState: TCheckBoxState);
var
tbl: TCocoaTableListView;
cb : TLCLCheckboxListCallback;
cocoaSt: Integer;
begin
if not CtrlToCheckList(ACheckListBox, tbl, cb) then Exit;
case AState of
cbChecked: cocoaSt := NSOnState;
cbGrayed: cocoaSt := NSMixedState;
else
cocoaSt := NSOffState;
end;
cb.SetCheckState(AIndex, cocoaSt, true);
end;
end.
|