table-view.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. @end
  125. @implementation CocoaTableViewCell : NSCell
  126. -(id) initWith:(hiro::mTableView&)tableViewReference {
  127. if(self = [super initTextCell:@""]) {
  128. tableView = &tableViewReference;
  129. buttonCell = [[NSButtonCell alloc] initTextCell:@""];
  130. [buttonCell setButtonType:NSSwitchButton];
  131. [buttonCell setControlSize:NSSmallControlSize];
  132. [buttonCell setRefusesFirstResponder:YES];
  133. [buttonCell setTarget:self];
  134. }
  135. return self;
  136. }
  137. //used by type-ahead
  138. -(NSString*) stringValue {
  139. return [[self objectValue] objectForKey:@"text"];
  140. }
  141. -(void) drawWithFrame:(NSRect)frame inView:(NSView*)view {
  142. if(auto tableViewItem = tableView->item([view rowAtPoint:frame.origin])) {
  143. if(auto tableViewCell = tableViewItem->cell([view columnAtPoint:frame.origin])) {
  144. NSColor* backgroundColor = nil;
  145. if([self isHighlighted]) backgroundColor = [NSColor alternateSelectedControlColor];
  146. else if(!tableView->enabled(true)) backgroundColor = [NSColor controlBackgroundColor];
  147. else if(auto color = tableViewCell->state.backgroundColor) backgroundColor = NSMakeColor(color);
  148. else backgroundColor = [NSColor controlBackgroundColor];
  149. [backgroundColor set];
  150. [NSBezierPath fillRect:frame];
  151. if(tableViewCell->state.checkable) {
  152. [buttonCell setHighlighted:YES];
  153. [buttonCell setState:(tableViewCell->state.checked ? NSOnState : NSOffState)];
  154. [buttonCell drawWithFrame:frame inView:view];
  155. frame.origin.x += frame.size.height + 2;
  156. frame.size.width -= frame.size.height + 2;
  157. }
  158. if(tableViewCell->state.icon) {
  159. NSImage* image = NSMakeImage(tableViewCell->state.icon, frame.size.height, frame.size.height);
  160. [[NSGraphicsContext currentContext] saveGraphicsState];
  161. NSRect targetRect = NSMakeRect(frame.origin.x, frame.origin.y, frame.size.height, frame.size.height);
  162. NSRect sourceRect = NSMakeRect(0, 0, [image size].width, [image size].height);
  163. [image drawInRect:targetRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
  164. [[NSGraphicsContext currentContext] restoreGraphicsState];
  165. frame.origin.x += frame.size.height + 2;
  166. frame.size.width -= frame.size.height + 2;
  167. }
  168. if(tableViewCell->state.text) {
  169. NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  170. paragraphStyle.alignment = NSTextAlignmentCenter;
  171. if(tableViewCell->state.alignment.horizontal() < 0.333) paragraphStyle.alignment = NSTextAlignmentLeft;
  172. if(tableViewCell->state.alignment.horizontal() > 0.666) paragraphStyle.alignment = NSTextAlignmentRight;
  173. NSColor* foregroundColor = nil;
  174. if([self isHighlighted]) foregroundColor = [NSColor alternateSelectedControlTextColor];
  175. else if(!tableView->enabled(true)) foregroundColor = [NSColor disabledControlTextColor];
  176. else if(auto color = tableViewCell->state.foregroundColor) foregroundColor = NSMakeColor(color);
  177. else foregroundColor = [NSColor textColor];
  178. NSString* text = [NSString stringWithUTF8String:tableViewCell->state.text];
  179. [text drawInRect:frame withAttributes:@{
  180. NSBackgroundColorAttributeName:backgroundColor,
  181. NSForegroundColorAttributeName:foregroundColor,
  182. NSFontAttributeName:hiro::pFont::create(tableViewCell->font(true)),
  183. NSParagraphStyleAttributeName:paragraphStyle
  184. }];
  185. }
  186. }
  187. }
  188. }
  189. //needed to trigger trackMouse events
  190. -(NSUInteger) hitTestForEvent:(NSEvent*)event inRect:(NSRect)frame ofView:(NSView*)view {
  191. NSUInteger hitTest = [super hitTestForEvent:event inRect:frame ofView:view];
  192. NSPoint point = [view convertPoint:[event locationInWindow] fromView:nil];
  193. NSRect rect = NSMakeRect(frame.origin.x, frame.origin.y, frame.size.height, frame.size.height);
  194. if(NSMouseInRect(point, rect, [view isFlipped])) {
  195. hitTest |= NSCellHitTrackableArea;
  196. }
  197. return hitTest;
  198. }
  199. //I am unable to get startTrackingAt:, continueTracking:, stopTracking: to work
  200. //so instead, I have to run a modal loop on events until the mouse button is released
  201. -(BOOL) trackMouse:(NSEvent*)event inRect:(NSRect)frame ofView:(NSView*)view untilMouseUp:(BOOL)flag {
  202. if([event type] == NSLeftMouseDown) {
  203. NSWindow* window = [view window];
  204. NSEvent* nextEvent;
  205. while((nextEvent = [window nextEventMatchingMask:(NSLeftMouseDragged | NSLeftMouseUp)])) {
  206. if([nextEvent type] == NSLeftMouseUp) {
  207. NSPoint point = [view convertPoint:[nextEvent locationInWindow] fromView:nil];
  208. NSRect rect = NSMakeRect(frame.origin.x, frame.origin.y, frame.size.height, frame.size.height);
  209. if(NSMouseInRect(point, rect, [view isFlipped])) {
  210. if(auto tableViewItem = tableView->item([view rowAtPoint:point])) {
  211. if(auto tableViewCell = tableViewItem->cell([view columnAtPoint:point])) {
  212. tableViewCell->state.checked = !tableViewCell->state.checked;
  213. tableView->doToggle(tableViewCell->instance);
  214. }
  215. }
  216. }
  217. break;
  218. }
  219. }
  220. }
  221. return YES;
  222. }
  223. +(BOOL) prefersTrackingUntilMouseUp {
  224. return YES;
  225. }
  226. @end
  227. namespace hiro {
  228. auto pTableView::construct() -> void {
  229. @autoreleasepool {
  230. cocoaView = cocoaTableView = [[CocoaTableView alloc] initWith:self()];
  231. pWidget::construct();
  232. setAlignment(state().alignment);
  233. setBackgroundColor(state().backgroundColor);
  234. setBatchable(state().batchable);
  235. setBordered(state().bordered);
  236. setFont(self().font(true));
  237. setForegroundColor(state().foregroundColor);
  238. setHeadered(state().headered);
  239. setSortable(state().sortable);
  240. }
  241. }
  242. auto pTableView::destruct() -> void {
  243. @autoreleasepool {
  244. [cocoaView removeFromSuperview];
  245. [cocoaView release];
  246. }
  247. }
  248. auto pTableView::append(sTableViewColumn column) -> void {
  249. @autoreleasepool {
  250. [cocoaView reloadColumns];
  251. resizeColumns();
  252. }
  253. }
  254. auto pTableView::append(sTableViewItem item) -> void {
  255. @autoreleasepool {
  256. [[cocoaView content] reloadData];
  257. }
  258. }
  259. auto pTableView::remove(sTableViewColumn column) -> void {
  260. @autoreleasepool {
  261. [cocoaView reloadColumns];
  262. resizeColumns();
  263. }
  264. }
  265. auto pTableView::remove(sTableViewItem item) -> void {
  266. @autoreleasepool {
  267. [[cocoaView content] reloadData];
  268. }
  269. }
  270. auto pTableView::resizeColumns() -> void {
  271. @autoreleasepool {
  272. vector<int> widths;
  273. int minimumWidth = 0;
  274. int expandable = 0;
  275. for(uint column : range(self().columnCount())) {
  276. int width = _width(column);
  277. widths.append(width);
  278. minimumWidth += width;
  279. if(state().columns[column]->expandable()) expandable++;
  280. }
  281. int maximumWidth = self().geometry().width() - 18; //include margin for vertical scroll bar
  282. int expandWidth = 0;
  283. if(expandable && maximumWidth > minimumWidth) {
  284. expandWidth = (maximumWidth - minimumWidth) / expandable;
  285. }
  286. for(uint column : range(self().columnCount())) {
  287. if(auto self = state().columns[column]->self()) {
  288. int width = widths[column];
  289. if(self->state().expandable) width += expandWidth;
  290. NSTableColumn* tableColumn = [[cocoaView content] tableColumnWithIdentifier:[[NSNumber numberWithInteger:column] stringValue]];
  291. [tableColumn setWidth:width];
  292. }
  293. }
  294. }
  295. }
  296. auto pTableView::setAlignment(Alignment alignment) -> void {
  297. }
  298. auto pTableView::setBackgroundColor(Color color) -> void {
  299. }
  300. auto pTableView::setBatchable(bool batchable) -> void {
  301. @autoreleasepool {
  302. [[cocoaView content] setAllowsMultipleSelection:(batchable ? YES : NO)];
  303. }
  304. }
  305. auto pTableView::setBordered(bool bordered) -> void {
  306. }
  307. auto pTableView::setEnabled(bool enabled) -> void {
  308. pWidget::setEnabled(enabled);
  309. @autoreleasepool {
  310. [[cocoaView content] setEnabled:enabled];
  311. }
  312. }
  313. auto pTableView::setFont(const Font& font) -> void {
  314. @autoreleasepool {
  315. [cocoaView setFont:pFont::create(font)];
  316. }
  317. }
  318. auto pTableView::setForegroundColor(Color color) -> void {
  319. }
  320. auto pTableView::setHeadered(bool headered) -> void {
  321. @autoreleasepool {
  322. if(headered) {
  323. [[cocoaView content] setHeaderView:[[[NSTableHeaderView alloc] init] autorelease]];
  324. } else {
  325. [[cocoaView content] setHeaderView:nil];
  326. }
  327. }
  328. }
  329. auto pTableView::setSortable(bool sortable) -> void {
  330. //TODO
  331. }
  332. auto pTableView::_cellWidth(uint row, uint column) -> uint {
  333. uint width = 8;
  334. if(auto pTableViewItem = self().item(row)) {
  335. if(auto pTableViewCell = pTableViewItem->cell(column)) {
  336. if(pTableViewCell->state.checkable) {
  337. width += 24;
  338. }
  339. if(auto& icon = pTableViewCell->state.icon) {
  340. width += icon.width() + 2;
  341. }
  342. if(auto& text = pTableViewCell->state.text) {
  343. width += pFont::size(pTableViewCell->font(true), text).width();
  344. }
  345. }
  346. }
  347. return width;
  348. }
  349. auto pTableView::_columnWidth(uint column_) -> uint {
  350. uint width = 8;
  351. if(auto column = self().column(column_)) {
  352. if(auto& icon = column->state.icon) {
  353. width += icon.width() + 2;
  354. }
  355. if(auto& text = column->state.text) {
  356. width += pFont::size(column->font(true), text).width();
  357. }
  358. if(column->state.sorting != Sort::None) {
  359. width += 16;
  360. }
  361. }
  362. return width;
  363. }
  364. auto pTableView::_width(uint column) -> uint {
  365. if(auto width = self().column(column).width()) return width;
  366. uint width = 1;
  367. if(!self().column(column).visible()) return width;
  368. if(state().headered) width = max(width, _columnWidth(column));
  369. for(auto row : range(state().items.size())) {
  370. width = max(width, _cellWidth(row, column));
  371. }
  372. return width;
  373. }
  374. /*
  375. auto pTableView::setSelected(bool selected) -> void {
  376. @autoreleasepool {
  377. if(selected == false) {
  378. [[cocoaView content] deselectAll:nil];
  379. }
  380. }
  381. }
  382. auto pTableView::setSelection(unsigned selection) -> void {
  383. @autoreleasepool {
  384. [[cocoaView content] selectRowIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(selection, 1)] byExtendingSelection:NO];
  385. }
  386. }
  387. */
  388. }
  389. #endif