Debian Codegraph @

hiro/cocoa/widget/table-view.cpp

raw
1 #if defined(Hiro_TableView)
2
3 @implementation CocoaTableView
4
5 -(id) initWith:(hiro::mTableView&)tableViewReference {
6 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
7 tableView = &tableViewReference;
8 content = [[CocoaTableViewContent alloc] initWith:tableViewReference];
9
10 [self setDocumentView:content];
11 [self setBorderType:NSBezelBorder];
12 [self setHasVerticalScroller:YES];
13
14 [content setDataSource:self];
15 [content setDelegate:self];
16 [content setTarget:self];
17 [content setDoubleAction:@selector(doubleAction:)];
18
19 [content setAllowsColumnReordering:NO];
20 [content setAllowsColumnResizing:YES];
21 [content setAllowsColumnSelection:NO];
22 [content setAllowsEmptySelection:YES];
23 [content setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
24
25 font = nil;
26 [self setFont:nil];
27 }
28 return self;
29 }
30
31 -(void) setUsesSidebarStyle:(bool)usesSidebarStyle {
32 content.selectionHighlightStyle = usesSidebarStyle? NSTableViewSelectionHighlightStyleSourceList : NSTableViewSelectionHighlightStyleRegular;
33 self.borderType = usesSidebarStyle? NSNoBorder : NSBezelBorder;
34 }
35
36 -(bool) usesSidebarStyle {
37 return content.selectionHighlightStyle == NSTableViewSelectionHighlightStyleSourceList;
38 }
39
40 -(CocoaTableViewContent*) content {
41 return content;
42 }
43
44 -(NSFont*) font {
45 return font;
46 }
47
48 -(void) setFont:(NSFont*)fontPointer {
49 if(!fontPointer) fontPointer = [NSFont systemFontOfSize:12];
50 font = fontPointer;
51
52 u32 fontHeight = hiro::pFont::size(font, " ").height();
53 [content setFont:font];
54 [content setRowHeight:fontHeight + 4];
55 [self reloadColumns];
56 tableView->resizeColumns();
57 }
58
59 -(void) reloadColumns {
60 while([[content tableColumns] count]) {
61 [content removeTableColumn:[[content tableColumns] lastObject]];
62 }
63
64 for(auto& tableViewColumn : tableView->state.columns) {
65 auto column = tableViewColumn->offset();
66
67 NSTableColumn* tableColumn = [[NSTableColumn alloc] initWithIdentifier:[[NSNumber numberWithInteger:column] stringValue]];
68 NSTableHeaderCell* headerCell = [[NSTableHeaderCell alloc] initTextCell:[NSString stringWithUTF8String:tableViewColumn->state.text]];
69 CocoaTableViewCell* dataCell = [[CocoaTableViewCell alloc] initWith:*tableView];
70
71 [dataCell setEditable:NO];
72
73 [tableColumn setResizingMask:NSTableColumnAutoresizingMask | NSTableColumnUserResizingMask];
74 [tableColumn setHeaderCell:headerCell];
75 [tableColumn setDataCell:dataCell];
76
77 [content addTableColumn:tableColumn];
78 }
79 }
80
81 -(NSInteger) numberOfRowsInTableView:(NSTableView*)table {
82 return tableView->state.items.size();
83 }
84
85 -(id) tableView:(NSTableView*)table objectValueForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
86 if(auto tableViewItem = tableView->item(row)) {
87 if(auto tableViewCell = tableViewItem->cell([[tableColumn identifier] integerValue])) {
88 NSString* text = [NSString stringWithUTF8String:tableViewCell->state.text];
89 return @{ @"text":text }; //used by type-ahead
90 }
91 }
92 return @{};
93 }
94
95 -(BOOL) tableView:(NSTableView*)table shouldShowCellExpansionForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
96 return NO;
97 }
98
99 -(NSString*) tableView:(NSTableView*)table toolTipForCell:(NSCell*)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation {
100 return nil;
101 }
102
103 -(void) tableView:(NSTableView*)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
104 [cell setFont:[self font]];
105 }
106
107 -(void) tableViewSelectionDidChange:(NSNotification*)notification {
108 if(tableView->self()->locked()) return;
109 for(auto& tableViewItem : tableView->state.items) {
110 tableViewItem->state.selected = tableViewItem->offset() == [content selectedRow];
111 }
112 tableView->doChange();
113 }
114
115 -(IBAction) doubleAction:(id)sender {
116 s32 row = [content clickedRow];
117 if(row >= 0 && row < tableView->state.items.size()) {
118 s32 column = [content clickedColumn];
119 if(column >= 0 && column < tableView->state.columns.size()) {
120 auto item = tableView->state.items[row];
121 auto cell = item->cell(column);
122 tableView->doActivate(cell);
123 }
124 }
125 }
126
127 @end
128
129 @implementation CocoaTableViewContent : NSTableView
130
131 -(id) initWith:(hiro::mTableView&)tableViewReference {
132 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
133 tableView = &tableViewReference;
134 }
135 return self;
136 }
137
138 -(void) reloadData {
139 //acquire lock to prevent tableViewSelectionDidChange from invoking the onChange callback
140 auto lock = tableView->self()->acquire();
141 [super reloadData];
142 }
143
144 -(void) keyDown:(NSEvent*)event {
145 auto character = [[event characters] characterAtIndex:0];
146 if(character == NSEnterCharacter || character == NSCarriageReturnCharacter) {
147 s32 row = [self selectedRow];
148 if(row >= 0 && row < tableView->state.items.size()) {
149 s32 column = max(0, [self selectedColumn]); //can be -1?
150 if(column >= 0 && column < tableView->state.columns.size()) {
151 auto item = tableView->state.items[row];
152 auto cell = item->cell(column);
153 tableView->doActivate(cell);
154 }
155 }
156 }
157
158 [super keyDown:event];
159 }
160
161 -(NSMenu*) menuForEvent:(NSEvent*)event {
162 NSPoint localPoint = [self convertPoint:event.locationInWindow fromView:nil];
163 NSInteger row = [self rowAtPoint:localPoint];
164 NSInteger column = [self columnAtPoint:localPoint];
165
166 if (row < 0 || row >= tableView->state.items.size()) {
167 return nil;
168 }
169
170 if (row >= 0 && ![self isRowSelected:row]) {
171 [self selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
172 }
173
174 if(column >= 0 && column < tableView->state.columns.size()) {
175 auto item = tableView->state.items[row];
176 auto cell = item->cell(column);
177 tableView->doContext(cell);
178 }
179
180 return nil;
181 }
182
183 - (void)drawRect:(NSRect)dirtyRect
184 {
185 [super drawRect:dirtyRect];
186 if(((CocoaTableView*)self.enclosingScrollView).usesSidebarStyle) {
187 NSRect frame = self.bounds;
188 frame.origin.x += frame.size.width - 1;
189 frame.size.width = 1;
190 if(@available(macOS 10.14, *)) [[NSColor separatorColor] set];
191 else if (@available(macOS 10.10, *)) [[NSColor secondaryLabelColor] set];
192 else [[NSColor shadowColor] set];
193 [NSBezierPath fillRect:frame];
194 }
195 }
196
197 @end
198
199 @implementation CocoaTableViewCell : NSCell
200
201 -(id) initWith:(hiro::mTableView&)tableViewReference {
202 if(self = [super initTextCell:@""]) {
203 tableView = &tableViewReference;
204 buttonCell = [[NSButtonCell alloc] initTextCell:@""];
205 [buttonCell setButtonType:NSButtonTypeSwitch];
206 [buttonCell setControlSize:NSControlSizeSmall];
207 [buttonCell setRefusesFirstResponder:YES];
208 [buttonCell setTarget:self];
209 textCell = [[NSTextFieldCell alloc] init];
210 }
211 return self;
212 }
213
214 //used by type-ahead
215 -(NSString*) stringValue {
216 return [[self objectValue] objectForKey:@"text"];
217 }
218
219 -(void) drawWithFrame:(NSRect)frame inView:(NSView*)_view {
220 NSTableView* view = (NSTableView*)_view;
221 if(auto tableViewItem = tableView->item([view rowAtPoint:frame.origin])) {
222 if(auto tableViewCell = tableViewItem->cell([view columnAtPoint:frame.origin])) {
223 if(tableViewCell->state.checkable) {
224 [buttonCell setHighlighted:YES];
225 [buttonCell setState:(tableViewCell->state.checked ? NSControlStateValueOn : NSControlStateValueOff)];
226 [buttonCell drawWithFrame:frame inView:view];
227 frame.origin.x += frame.size.height + 2;
228 frame.size.width -= frame.size.height + 2;
229 }
230
231 if(tableViewCell->state.icon) {
232 NSImage* image = NSMakeImage(tableViewCell->state.icon,
233 tableViewCell->state.icon.width(),
234 tableViewCell->state.icon.height());
235 [[NSGraphicsContext currentContext] saveGraphicsState];
236 NSRect targetRect = NSMakeRect(frame.origin.x + 2, frame.origin.y + (frame.size.height - image.size.height) / 2,
237 image.size.width, image.size.height);
238 NSRect sourceRect = NSMakeRect(0, 0, image.size.width, image.size.height);
239 [image drawInRect:targetRect fromRect:sourceRect operation:NSCompositingOperationSourceOver fraction:1.0 respectFlipped:YES hints:nil];
240 [[NSGraphicsContext currentContext] restoreGraphicsState];
241 frame.origin.x += image.size.width + 4;
242 frame.size.width -= image.size.width + 4;
243 }
244
245 if(tableViewCell->state.text) {
246 textCell.stringValue = @((const char*)tableViewCell->state.text);
247 textCell.alignment = NSTextAlignmentCenter;
248 textCell.backgroundStyle = self.backgroundStyle;
249 textCell.font = hiro::pFont::create(tableViewCell->font(true));
250 if(tableViewCell->state.alignment.horizontal() < 0.333) textCell.alignment = NSTextAlignmentLeft;
251 if(tableViewCell->state.alignment.horizontal() > 0.666) textCell.alignment = NSTextAlignmentRight;
252 textCell.textColor = nil;
253 if(![self isHighlighted] && tableView->enabled(true)) {
254 auto systemColor = tableViewCell->state.foregroundSystemColor;
255 if(systemColor != hiro::SystemColor::None) textCell.textColor = NSMakeColor(systemColor);
256 else if(auto color = tableViewCell->state.foregroundColor) textCell.textColor = NSMakeColor(color);
257 }
258 [textCell drawWithFrame:frame inView:view];
259 }
260 }
261 }
262 }
263
264 //needed to trigger trackMouse events
265 -(NSUInteger) hitTestForEvent:(NSEvent*)event inRect:(NSRect)frame ofView:(NSView*)view {
266 NSUInteger hitTest = [super hitTestForEvent:event inRect:frame ofView:view];
267 NSPoint point = [view convertPoint:[event locationInWindow] fromView:nil];
268 NSRect rect = NSMakeRect(frame.origin.x, frame.origin.y, frame.size.height, frame.size.height);
269 if(NSMouseInRect(point, rect, [view isFlipped])) {
270 hitTest |= NSCellHitTrackableArea;
271 }
272 return hitTest;
273 }
274
275 //I am unable to get startTrackingAt:, continueTracking:, stopTracking: to work
276 //so instead, I have to run a modal loop on events until the mouse button is released
277 -(BOOL) trackMouse:(NSEvent*)event inRect:(NSRect)frame ofView:(NSView*)_view untilMouseUp:(BOOL)flag {
278 NSTableView* view = (NSTableView*)_view;
279 if([event type] == NSEventTypeLeftMouseDown) {
280 NSWindow* window = [view window];
281 NSEvent* nextEvent;
282 while((nextEvent = [window nextEventMatchingMask:(NSEventTypeLeftMouseDragged | NSEventTypeLeftMouseUp)])) {
283 if([nextEvent type] == NSEventTypeLeftMouseUp) {
284 NSPoint point = [view convertPoint:[nextEvent locationInWindow] fromView:nil];
285 NSRect rect = NSMakeRect(frame.origin.x, frame.origin.y, frame.size.height, frame.size.height);
286 if(NSMouseInRect(point, rect, [view isFlipped])) {
287 if(auto tableViewItem = tableView->item([view rowAtPoint:point])) {
288 if(auto tableViewCell = tableViewItem->cell([view columnAtPoint:point])) {
289 tableViewCell->state.checked = !tableViewCell->state.checked;
290 tableView->doToggle(tableViewCell);
291 }
292 }
293 }
294 break;
295 }
296 }
297 }
298 return YES;
299 }
300
301 +(BOOL) prefersTrackingUntilMouseUp {
302 return YES;
303 }
304
305 @end
306
307 namespace hiro {
308
309 auto pTableView::construct() -> void {
310 cocoaView = cocoaTableView = [[CocoaTableView alloc] initWith:self()];
311 pWidget::construct();
312
313 setAlignment(state().alignment);
314 setBackgroundColor(state().backgroundColor);
315 setBatchable(state().batchable);
316 setBordered(state().bordered);
317 setFont(self().font(true));
318 setForegroundColor(state().foregroundColor);
319 setHeadered(state().headered);
320 setSortable(state().sortable);
321 }
322
323 auto pTableView::destruct() -> void {
324 [cocoaView removeFromSuperview];
325 }
326
327 auto pTableView::append(sTableViewColumn column) -> void {
328 [(CocoaTableView*)cocoaView reloadColumns];
329 resizeColumns();
330 }
331
332 auto pTableView::append(sTableViewItem item) -> void {
333 [[(CocoaTableView*)cocoaView content] reloadData];
334 }
335
336 auto pTableView::remove(sTableViewColumn column) -> void {
337 [(CocoaTableView*)cocoaView reloadColumns];
338 resizeColumns();
339 }
340
341 auto pTableView::remove(sTableViewItem item) -> void {
342 [[(CocoaTableView*)cocoaView content] reloadData];
343 }
344
345 auto pTableView::resizeColumns() -> void {
346 std::vector<s32> widths;
347 s32 minimumWidth = 0;
348 s32 expandable = 0;
349 for(u32 column : range(self().columnCount())) {
350 s32 width = _width(column);
351 widths.push_back(width);
352 minimumWidth += width;
353 if(state().columns[column]->expandable()) expandable++;
354 }
355
356 s32 maximumWidth = self().geometry().width() - 18; //include margin for vertical scroll bar
357 s32 expandWidth = 0;
358 if(expandable && maximumWidth > minimumWidth) {
359 expandWidth = (maximumWidth - minimumWidth) / expandable;
360 }
361
362 for(u32 column : range(self().columnCount())) {
363 if(auto self = state().columns[column]->self()) {
364 s32 width = widths[column];
365 if(self->state().expandable) width += expandWidth;
366 NSTableColumn* tableColumn = [[(CocoaTableView*)cocoaView content] tableColumnWithIdentifier:[[NSNumber numberWithInteger:column] stringValue]];
367 [tableColumn setWidth:width];
368 }
369 }
370 }
371
372 auto pTableView::setAlignment(Alignment alignment) -> void {
373 }
374
375 auto pTableView::setBackgroundColor(Color color) -> void {
376 }
377
378 auto pTableView::setBatchable(bool batchable) -> void {
379 [[(CocoaTableView*)cocoaView content] setAllowsMultipleSelection:(batchable ? YES : NO)];
380 }
381
382 auto pTableView::setBordered(bool bordered) -> void {
383 }
384
385 auto pTableView::setEnabled(bool enabled) -> void {
386 pWidget::setEnabled(enabled);
387 [[(CocoaTableView*)cocoaView content] setEnabled:enabled];
388 }
389
390 auto pTableView::setFont(const Font& font) -> void {
391 [(CocoaTableView*)cocoaView setFont:pFont::create(font)];
392 }
393
394 auto pTableView::setForegroundColor(Color color) -> void {
395 }
396
397 auto pTableView::setHeadered(bool headered) -> void {
398 if(headered) {
399 [[(CocoaTableView*)cocoaView content] setHeaderView:[[NSTableHeaderView alloc] init]];
400 } else {
401 [[(CocoaTableView*)cocoaView content] setHeaderView:nil];
402 }
403 }
404
405 auto pTableView::setSortable(bool sortable) -> void {
406 //TODO
407 }
408
409 auto pTableView::setUsesSidebarStyle(bool usesSidebarStyle) -> void {
410 ((CocoaTableView*)cocoaView).usesSidebarStyle = usesSidebarStyle;
411 }
412
413 auto pTableView::_cellWidth(u32 row, u32 column) -> u32 {
414 u32 width = 8;
415 if(auto pTableViewItem = self().item(row)) {
416 if(auto pTableViewCell = pTableViewItem->cell(column)) {
417 if(pTableViewCell->state.checkable) {
418 width += 24;
419 }
420 if(auto& icon = pTableViewCell->state.icon) {
421 width += icon.width() + 2;
422 }
423 if(auto& text = pTableViewCell->state.text) {
424 width += pFont::size(pTableViewCell->font(true), text).width();
425 }
426 }
427 }
428 return width;
429 }
430
431 auto pTableView::_columnWidth(u32 column_) -> u32 {
432 u32 width = 8;
433 if(auto column = self().column(column_)) {
434 if(auto& icon = column->state.icon) {
435 width += icon.width() + 2;
436 }
437 if(auto& text = column->state.text) {
438 width += pFont::size(column->font(true), text).width();
439 }
440 if(column->state.sorting != Sort::None) {
441 width += 16;
442 }
443 }
444 return width;
445 }
446
447 auto pTableView::_width(u32 column) -> u32 {
448 if(auto width = self().column(column).width()) return width;
449 u32 width = 1;
450 if(!self().column(column).visible()) return width;
451 if(state().headered) width = max(width, _columnWidth(column));
452 for(auto row : range(state().items.size())) {
453 width = max(width, _cellWidth(row, column));
454 }
455 return width;
456 }
457
458 /*
459 auto pTableView::setSelected(bool selected) -> void {
460 if(selected == false) {
461 [[cocoaView content] deselectAll:nil];
462 }
463 }
464
465 auto pTableView::setSelection(u32 selection) -> void {
466 [[cocoaView content] selectRowIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(selection, 1)] byExtendingSelection:NO];
467 }
468 */
469
470 }
471
472 #endif