-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCocoaHelpers.pas
154 lines (128 loc) · 5.02 KB
/
CocoaHelpers.pas
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
unit CocoaHelpers;
{
Unit of handy routines for use with Cocoa.
Author: Phil Hess.
Copyright: Copyright 2012 Phil Hess.
License: Modified LGPL. (see Free Pascal's rtl/COPYING.FPC).
This means you can link your code to this compiled unit (statically
in a standalone executable or dynamically in a library) without
releasing your code. Only changes to this unit need to be made
publicly available.
}
{$MODE Delphi}
{$modeswitch ObjectiveC1}
interface
uses
SysUtils,
CGBase,
{$IFDEF NoCocoaAll}
Foundation,
AppKit,
{$ELSE}
CocoaAll,
{$ENDIF}
NSHelpers;
procedure ErrorDlg(const ErrorMsg : string);
procedure DoErrorDlg(const ErrorMsg : string;
ParentWindow : NSWindow);
procedure DoConfirmDlg(const ConfirmMsg : string;
ParentWindow : NSWindow;
Delegate : id;
Selector : SEL);
procedure SetTableHeaderHeight(aTable : NSTableView;
newHeight : CGFloat;
backgroundColor : NSColor);
type
CustomTableHeaderCell = objcclass(NSTableHeaderCell)
function initTextCellWithCell(aCell : NSTableHeaderCell) : id; message 'initTextCellWithCell:';
procedure drawWithFrame_inView(cellFrame: NSRect; controlView_: NSView); override;
end;
implementation
procedure ErrorDlg(const ErrorMsg : string);
{Display error message modal dialog with OK button.}
var
Alert : NSAlert;
begin
Alert := NSAlert.alloc.init;
Alert.setInformativeText(StrToNSStr(ErrorMsg));
Alert.runModal;
Alert.release;
end;
procedure DoErrorDlg(const ErrorMsg : string;
ParentWindow : NSWindow);
{Display error message dialog with OK button as modal sheet.}
var
Alert : NSAlert;
begin
Alert := NSAlert.alloc.init;
Alert.setInformativeText(StrToNSStr(ErrorMSg));
Alert.beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo
(ParentWindow, nil, nil, nil); //assume don't need alertDidEndSelector method
end;
procedure DoConfirmDlg(const ConfirmMsg : string;
ParentWindow : NSWindow;
Delegate : id;
Selector : SEL);
{Display confirm dialog with OK and Cancel buttons as modal sheet.}
var
Alert : NSAlert;
begin
Alert := NSAlert.alloc.init;
Alert.setMessageText(NSSTR('Confirm'));
Alert.setInformativeText(StrToNSStr(ConfirmMsg));
Alert.setAlertStyle(NSInformationalAlertStyle);
Alert.addButtonWithTitle(NSSTR('OK')); {returnCode=NSAlertFirstButtonReturn}
Alert.addButtonWithTitle(NSSTR('Cancel')); {returnCode=NSAlertSecondButtonReturn}
Alert.beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo
(ParentWindow, Delegate, Selector, Alert);
end;
procedure SetTableHeaderHeight(aTable : NSTableView;
newHeight : CGFloat;
backgroundColor : NSColor);
var
ColIdx : Integer;
curCol : NSTableColumn;
begin
{First increase height of table's header view}
aTable.headerView.setFrameSize(
NSMakeSize(aTable.headerView.frame.size.width, newHeight));
{Now replace each column's header cell with a custom cell}
for ColIdx := 0 to aTable.tableColumns.count-1 do
begin
curCol := NSTableColumn(aTable.tableColumns.objectAtIndex(ColIdx));
curCol.setHeaderCell(CustomTableHeaderCell.alloc.initTextCellWithCell(
curCol.headerCell).autorelease);
curCol.headerCell.setBackgroundColor(backgroundColor);
curCol.headerCell.setDrawsBackground(True);
end;
{Finally, increase height of corner view to match header view}
aTable.setCornerView(
NSView.alloc.initWithFrame(
NSMakeRect(aTable.cornerView.frame.origin.x,
aTable.cornerView.frame.origin.y,
aTable.cornerView.frame.size.width,
aTable.headerView.frame.size.height)).autorelease);
end; {SetTableHeaderHeight}
function CustomTableHeaderCell.initTextCellWithCell(aCell : NSTableHeaderCell) : id;
{Initialize cell with specified cell's properties.}
begin
Result := initTextCell(aCell.stringValue);
NSTableHeaderCell(Result).setAlignment(aCell.alignment);
NSTableHeaderCell(Result).setFont(aCell.font);
end;
procedure CustomTableHeaderCell.drawWithFrame_inView(cellFrame: NSRect; controlView_: NSView);
var
borderFrame : NSRect;
interiorFrame : NSRect;
begin
borderFrame := NSMakeRect(cellFrame.origin.x-1, cellFrame.origin.y,
cellFrame.size.width, cellFrame.size.height+1);
interiorFrame := NSMakeRect(cellFrame.origin.x+1, cellFrame.origin.y+2,
cellFrame.size.width-3, cellFrame.size.height-2);
{So entire cell is filled with background color}
drawInteriorWithFrame_inView(cellFrame, controlView_);
NSDrawGroove(borderFrame, borderFrame);
{Use reduced frame so doesn't write over border}
drawInteriorWithFrame_inView(interiorFrame, controlView_);
end;
end.