-
Notifications
You must be signed in to change notification settings - Fork 3
/
MyPrintViewOutline.swift
executable file
·305 lines (225 loc) · 10.1 KB
/
MyPrintViewOutline.swift
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
//
// MyPrintView.swift
// PDFGeneratorTableView
//
// Created by thierryH24 on 02/09/2018.
// Copyright © 2018 thierryH24. All rights reserved.
//
import AppKit
class MyPrintViewOutline: NSView
{
weak var tableToPrint: NSOutlineView?
var header = ""
var attributes: [NSAttributedString.Key : Any] = [:]
var listFont = NSFont(name: "Helvetica", size: 10.0)
var headerHeight : CGFloat = 0.0
var footerHeight : CGFloat = 0.0
var lineHeight : CGFloat = 0.0
var entryHeight : CGFloat = 0.0
var pageRect = NSRect.zero
var linesPerPage = 0
var currentPage = 0
var rightMargin : CGFloat = 20.0
let bottomMargin : CGFloat = 40.0
var leftMargin : CGFloat = 0
var topMargin : CGFloat = 0
var numberOfRows = 0
var numberOfRowsByPage = 0
let margin: CGFloat = 20
var widthQuotient : CGFloat = 0
override var printJobTitle : String {
return ""
}
init(tableView tableToPrint: NSOutlineView?, andHeader header: String?) {
// Initialize with dummy frame
super.init(frame: NSMakeRect(0, 0, 700, 700))
self.tableToPrint = tableToPrint
self.header = header!
listFont = NSFont(name: "Helvetica", size: 10.0)
lineHeight = listFont!.boundingRectForFont.size.height
entryHeight = listFont!.capHeight * 2.5
headerHeight = margin + entryHeight
footerHeight = margin
leftMargin = pageRect.origin.x + margin
topMargin = pageRect.origin.y + headerHeight
numberOfRows = tableToPrint!.numberOfRows
attributes = [.font: listFont!]
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: Pagination
override func knowsPageRange(_ range: NSRangePointer) -> Bool {
let printInfo = NSPrintOperation.current?.printInfo
pageRect = (printInfo?.imageablePageBounds)!
var newFrame = NSRect.zero
newFrame.size = printInfo?.paperSize ?? CGSize.zero
self.frame = newFrame
// Number of lines per page
linesPerPage = Int((pageRect.size.height - headerHeight - footerHeight) / entryHeight - CGFloat(1))
// Number of full pages
var noPages: Int = numberOfRows / linesPerPage
// Rest of lines on last page
if numberOfRows % linesPerPage > 0 {
noPages += 1
}
range.pointee.location = 1
range.pointee.length = noPages
return true
}
override func rectForPage(_ page: Int) -> NSRect {
currentPage = page - 1
return pageRect
}
override var pageHeader: NSAttributedString {
return NSAttributedString(string: header)
}
// MARK: Drawing
// Origin top left
override var isFlipped: Bool {
return true
}
// This is where the drawing takes place
override func draw(_ dirtyRect: NSRect) {
NSBezierPath.defaultLineWidth = 0.1
var originalWidth: CGFloat = 0
for column in tableToPrint!.tableColumns {
originalWidth += column.width
}
widthQuotient = (pageRect.size.width - margin) / originalWidth
let inset = (entryHeight - lineHeight - 1.0) / 2.0
// Column titles
var horizontalOffset: CGFloat = 0
for column in tableToPrint!.tableColumns {
if column.isHidden == true {
continue
}
let headerRect = NSMakeRect(
leftMargin + horizontalOffset,
topMargin,
widthQuotient * column.width,
entryHeight)
horizontalOffset += widthQuotient * column.width
let bpath = NSBezierPath(rect: headerRect)
NSColor.red.setFill()
bpath.fill()
NSColor.blue.set()
bpath.stroke()
bpath.close()
let columnTitle = column.title
columnTitle.draw(in: NSInsetRect(headerRect, inset, inset), withAttributes: attributes)
}
let firstEntryOfPage = currentPage * linesPerPage
let lastEntryOfPage = ((currentPage + 1) * linesPerPage) > tableToPrint!.numberOfRows ? tableToPrint!.numberOfRows : ((currentPage + 1) * linesPerPage)
numberOfRowsByPage = lastEntryOfPage - firstEntryOfPage
for i in 0..<( lastEntryOfPage - firstEntryOfPage) {
let row = firstEntryOfPage + i
var horizontalOffset: CGFloat = 0
var numCol = 0
var offsetX : CGFloat = 16
for column in tableToPrint!.tableColumns {
if column.isHidden == true {
continue
}
var valueAsStr = ""
// Header ???
if let tableCellView = tableToPrint?.view(atColumn: numCol, row: row, makeIfNecessary: true) as? KSHeaderCellView {
// draw triangle
let rectDis = NSMakeRect( leftMargin + horizontalOffset, topMargin + entryHeight * CGFloat(i + 1) + 4, 8 , 8)
let center = CGPoint(x: rectDis.midX, y: rectDis.midY)
let side = rectDis.width
let bezierPathDis = trianglePath(center: center, side: side)
bezierPathDis.stroke()
bezierPathDis.fill()
bezierPathDis.close()
// Now we can finally draw the entry
valueAsStr = (tableCellView.textField?.stringValue)!
attributes[.foregroundColor] = (tableCellView.textField?.textColor)!
let fillColor = tableCellView.fillColor
let rect = NSMakeRect(
leftMargin + horizontalOffset + 10 ,
topMargin + entryHeight * CGFloat(i + 1),
(pageRect.size.width - margin - 10 ) ,
entryHeight)
horizontalOffset += widthQuotient * column.width
let bezierPath = NSBezierPath(rect: rect)
fillColor.set()
bezierPath.fill()
NSColor.lightGray.set()
bezierPath.stroke()
bezierPath.close()
let stringRect = NSInsetRect(rect, inset, inset)
valueAsStr.draw(in: stringRect, withAttributes: attributes)
}
else
if let tableCellView = tableToPrint?.view(atColumn: numCol, row: row, makeIfNecessary: true) as? NSTableCellView {
valueAsStr = (tableCellView.textField?.stringValue)!
attributes[.foregroundColor] = (tableCellView.textField?.textColor)!
let rect = NSMakeRect(
leftMargin + horizontalOffset + offsetX,
topMargin + entryHeight * CGFloat(i + 1),
widthQuotient * column.width - 2,
entryHeight)
horizontalOffset += widthQuotient * column.width
let stringRect = NSInsetRect(rect, inset, inset)
valueAsStr.draw(in: stringRect, withAttributes: attributes)
if offsetX == 16 {
offsetX = 2
}
}
numCol += 1
}
drawVerticalGrids()
drawHorizontalGrids()
}
}
func trianglePath(center: CGPoint, side: CGFloat) -> NSBezierPath {
let path = NSBezierPath()
let startX = center.x - side / 2
let startY = center.y - side / 2
path.move(to: CGPoint(x: startX, y: startY))
path.line(to: CGPoint(x: startX + side, y: startY ))
path.line(to: CGPoint(x: startX + side/2, y: startY + side))
path.close()
return path
}
func drawVerticalGrids() {
let columns = tableToPrint!.tableColumns
var offsetX : CGFloat = 0.0
var fromPoint = CGPoint(x : leftMargin, y: topMargin + entryHeight )
var toPoint = CGPoint(x : leftMargin, y: topMargin + entryHeight * CGFloat(numberOfRowsByPage + 1 ))
drawLine(fromPoint, toPoint: toPoint)
for i in 0..<columns.count {
offsetX += widthQuotient * columns[i].width
//draw the vertical lines
fromPoint = NSMakePoint(
leftMargin + offsetX ,
topMargin + entryHeight )
toPoint = NSMakePoint(
leftMargin + offsetX ,
topMargin + entryHeight * CGFloat(numberOfRowsByPage + 1 ) )
drawLine(fromPoint, toPoint: toPoint)
}
}
func drawHorizontalGrids() {
for i in 0...numberOfRowsByPage {
let fromPoint = NSMakePoint(
leftMargin ,
topMargin + entryHeight + (CGFloat(i) * entryHeight)
)
let toPoint = NSMakePoint(
leftMargin + pageRect.size.width - rightMargin,
topMargin + entryHeight + (CGFloat(i) * entryHeight)
)
drawLine(fromPoint, toPoint: toPoint)
}
}
func drawLine( _ fromPoint:NSPoint, toPoint:NSPoint){
let path = NSBezierPath()
NSColor.gray.set()
path.move(to: fromPoint)
path.line(to: toPoint)
path.lineWidth = 0.5
path.stroke()
}
}