table-view.cpp 15 KB

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