Debian Codegraph @

phoenix/cocoa/widget/list-view.cpp

raw
1 @implementation CocoaListView : NSScrollView
2
3 -(id) initWith:(phoenix::ListView&)listViewReference {
4 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
5 listView = &listViewReference;
6 content = [[CocoaListViewContent alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
7
8 [self setDocumentView:content];
9 [self setBorderType:NSBezelBorder];
10 [self setHasVerticalScroller:YES];
11
12 [content setDataSource:self];
13 [content setDelegate:self];
14 [content setTarget:self];
15 [content setDoubleAction:@selector(doubleAction:)];
16
17 [content setAllowsColumnReordering:NO];
18 [content setAllowsColumnResizing:YES];
19 [content setAllowsColumnSelection:NO];
20 [content setAllowsEmptySelection:YES];
21 [content setAllowsMultipleSelection:NO];
22 [content setColumnAutoresizingStyle:NSTableViewLastColumnOnlyAutoresizingStyle];
23
24 font = nil;
25 [self setFont:nil];
26 }
27 return self;
28 }
29
30 -(void) dealloc {
31 [content release];
32 [font release];
33 [super dealloc];
34 }
35
36 -(CocoaListViewContent*) content {
37 return content;
38 }
39
40 -(NSFont*) font {
41 return font;
42 }
43
44 -(void) setFont:(NSFont*)fontPointer {
45 if(!fontPointer) fontPointer = [NSFont systemFontOfSize:12];
46 [fontPointer retain];
47 if(font) [font release];
48 font = fontPointer;
49
50 unsigned fontHeight = phoenix::pFont::size(font, " ").height;
51 [content setFont:font];
52 [content setRowHeight:fontHeight];
53 [self reloadColumns];
54 }
55
56 -(void) reloadColumns {
57 while([[content tableColumns] count]) {
58 [content removeTableColumn:[[content tableColumns] lastObject]];
59 }
60
61 if(listView->state.checkable) {
62 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:@"check"];
63 NSTableHeaderCell *headerCell = [[NSTableHeaderCell alloc] initTextCell:@""];
64 NSButtonCell *dataCell = [[NSButtonCell alloc] initTextCell:@""];
65
66 [dataCell setButtonType:NSSwitchButton];
67 [dataCell setControlSize:NSSmallControlSize];
68 [dataCell setRefusesFirstResponder:YES];
69
70 [tableColumn setResizingMask:NSTableColumnNoResizing];
71 [tableColumn setHeaderCell:headerCell];
72 [tableColumn setDataCell:dataCell];
73 [tableColumn setWidth:20.0];
74
75 [content addTableColumn:tableColumn];
76 }
77
78 lstring headers = listView->state.headerText;
79 if(headers.size() == 0) headers.append("");
80 [content setUsesAlternatingRowBackgroundColors:headers.size() >= 2];
81
82 for(unsigned column = 0; column < headers.size(); column++) {
83 NSTableColumn* tableColumn = [[NSTableColumn alloc] initWithIdentifier:[[NSNumber numberWithInteger:column] stringValue]];
84 NSTableHeaderCell* headerCell = [[NSTableHeaderCell alloc] initTextCell:[NSString stringWithUTF8String:headers(column)]];
85 CocoaListViewCell* dataCell = [[CocoaListViewCell alloc] initTextCell:@""];
86
87 [dataCell setEditable:NO];
88
89 [tableColumn setResizingMask:NSTableColumnAutoresizingMask | NSTableColumnUserResizingMask];
90 [tableColumn setHeaderCell:headerCell];
91 [tableColumn setDataCell:dataCell];
92
93 [content addTableColumn:tableColumn];
94 }
95 }
96
97 -(NSInteger) numberOfRowsInTableView:(NSTableView*)table {
98 return listView->state.text.size();
99 }
100
101 -(id) tableView:(NSTableView*)table objectValueForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
102 if([[tableColumn identifier] isEqualToString:@"check"]) {
103 auto checked = listView->state.checked(row) ? NSOnState : NSOffState;
104 return [NSNumber numberWithInteger:checked];
105 }
106
107 NSInteger column = [[tableColumn identifier] integerValue];
108 unsigned height = [table rowHeight];
109
110 NSString* text = [NSString stringWithUTF8String:listView->state.text(row)(column)];
111 NSImage* image = NSMakeImage(listView->state.image(row)(column), height, height);
112
113 if(image) return @{ @"text":text, @"image":image };
114 return @{ @"text":text };
115 }
116
117 -(BOOL) tableView:(NSTableView*)table shouldShowCellExpansionForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
118 return NO;
119 }
120
121 -(NSString*) tableView:(NSTableView*)table toolTipForCell:(NSCell*)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation {
122 return nil;
123 }
124
125 -(void) tableView:(NSTableView*)table setObjectValue:(id)object forTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
126 if([[tableColumn identifier] isEqualToString:@"check"]) {
127 listView->state.checked(row) = [object integerValue] != NSOffState;
128 if(listView->onToggle) listView->onToggle(row);
129 }
130 }
131
132 -(void) tableView:(NSTableView*)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
133 [cell setFont:[self font]];
134 }
135
136 -(void) tableViewSelectionDidChange:(NSNotification*)notification {
137 listView->state.selected = true;
138 listView->state.selection = [content selectedRow];
139 if(listView->onChange) listView->onChange();
140 }
141
142 -(IBAction) activate:(id)sender {
143 if(listView->onActivate) listView->onActivate();
144 }
145
146 -(IBAction) doubleAction:(id)sender {
147 if([content clickedRow] >= 0) {
148 [self activate:self];
149 }
150 }
151
152 @end
153
154 @implementation CocoaListViewContent : NSTableView
155
156 -(void) keyDown:(NSEvent*)event {
157 auto character = [[event characters] characterAtIndex:0];
158 if(character == NSEnterCharacter || character == NSCarriageReturnCharacter) {
159 if([self selectedRow] >= 0) {
160 [[self delegate] activate:self];
161 return;
162 }
163 }
164
165 [super keyDown:event];
166 }
167
168 @end
169
170 @implementation CocoaListViewCell : NSTextFieldCell
171
172 //used by type-ahead
173 -(NSString*) stringValue {
174 return [[self objectValue] objectForKey:@"text"];
175 }
176
177 -(void) drawWithFrame:(NSRect)frame inView:(NSView*)view {
178 NSString* text = [[self objectValue] objectForKey:@"text"];
179 NSImage* image = [[self objectValue] objectForKey:@"image"];
180 unsigned textDisplacement = 0;
181
182 if(image) {
183 [[NSGraphicsContext currentContext] saveGraphicsState];
184
185 NSRect targetRect = NSMakeRect(frame.origin.x, frame.origin.y, frame.size.height, frame.size.height);
186 NSRect sourceRect = NSMakeRect(0, 0, [image size].width, [image size].height);
187 [image drawInRect:targetRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
188
189 [[NSGraphicsContext currentContext] restoreGraphicsState];
190 textDisplacement = frame.size.height + 2;
191 }
192
193 NSRect textRect = NSMakeRect(
194 frame.origin.x + textDisplacement, frame.origin.y,
195 frame.size.width - textDisplacement, frame.size.height
196 );
197
198 NSColor* textColor = [self isHighlighted]
199 ? [NSColor alternateSelectedControlTextColor]
200 : [NSColor textColor];
201
202 [text drawInRect:textRect withAttributes:@{
203 NSForegroundColorAttributeName:textColor,
204 NSFontAttributeName:[self font]
205 }];
206 }
207
208 @end
209
210 namespace phoenix {
211
212 void pListView::append(const lstring& text) {
213 @autoreleasepool {
214 [[cocoaView content] reloadData];
215 }
216 }
217
218 void pListView::autoSizeColumns() {
219 @autoreleasepool {
220 if(listView.state.checkable) {
221 NSTableColumn* tableColumn = [[cocoaView content] tableColumnWithIdentifier:@"check"];
222 [tableColumn setWidth:20.0];
223 }
224
225 unsigned height = [[cocoaView content] rowHeight];
226 for(unsigned column = 0; column < max(1u, listView.state.headerText.size()); column++) {
227 NSTableColumn* tableColumn = [[cocoaView content] tableColumnWithIdentifier:[[NSNumber numberWithInteger:column] stringValue]];
228 unsigned minimumWidth = pFont::size([[tableColumn headerCell] font], listView.state.headerText(column)).width + 4;
229 for(unsigned row = 0; row < listView.state.text.size(); row++) {
230 unsigned width = pFont::size([cocoaView font], listView.state.text(row)(column)).width + 2;
231 if(listView.state.image(row)(height).empty() == false) width += height + 2;
232 if(width > minimumWidth) minimumWidth = width;
233 }
234 [tableColumn setWidth:minimumWidth];
235 }
236
237 [[cocoaView content] sizeLastColumnToFit];
238 }
239 }
240
241 void pListView::remove(unsigned selection) {
242 @autoreleasepool {
243 [[cocoaView content] reloadData];
244 }
245 }
246
247 void pListView::reset() {
248 @autoreleasepool {
249 [[cocoaView content] reloadData];
250 }
251 }
252
253 void pListView::setCheckable(bool checkable) {
254 @autoreleasepool {
255 [cocoaView reloadColumns];
256 }
257 }
258
259 void pListView::setChecked(unsigned selection, bool checked) {
260 @autoreleasepool {
261 [[cocoaView content] reloadData];
262 }
263 }
264
265 void pListView::setFont(string font) {
266 @autoreleasepool {
267 [cocoaView setFont:pFont::cocoaFont(font)];
268 }
269 }
270
271 void pListView::setHeaderText(const lstring& text) {
272 @autoreleasepool {
273 [cocoaView reloadColumns];
274 }
275 }
276
277 void pListView::setHeaderVisible(bool visible) {
278 @autoreleasepool {
279 if(visible) {
280 [[cocoaView content] setHeaderView:[[[NSTableHeaderView alloc] init] autorelease]];
281 } else {
282 [[cocoaView content] setHeaderView:nil];
283 }
284 }
285 }
286
287 void pListView::setImage(unsigned selection, unsigned position, const image& image) {
288 @autoreleasepool {
289 [[cocoaView content] reloadData];
290 }
291 }
292
293 void pListView::setSelected(bool selected) {
294 @autoreleasepool {
295 if(selected == false) {
296 [[cocoaView content] deselectAll:nil];
297 }
298 }
299 }
300
301 void pListView::setSelection(unsigned selection) {
302 @autoreleasepool {
303 [[cocoaView content] selectRowIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(selection, 1)] byExtendingSelection:NO];
304 }
305 }
306
307 void pListView::setText(unsigned selection, unsigned position, const string text) {
308 @autoreleasepool {
309 [[cocoaView content] reloadData];
310 }
311 }
312
313 void pListView::constructor() {
314 @autoreleasepool {
315 cocoaView = cocoaListView = [[CocoaListView alloc] initWith:listView];
316 setHeaderVisible(listView.state.headerVisible);
317 setHeaderText(listView.state.headerText);
318 }
319 }
320
321 void pListView::destructor() {
322 @autoreleasepool {
323 [cocoaView release];
324 }
325 }
326
327 }