ui/cocoa/tree.m

changeset 47
97792f44d919
child 50
22b5adeb371f
equal deleted inserted replaced
46:4a5e0b9b6992 47:97792f44d919
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2012 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #import <stdio.h>
30 #import <stdlib.h>
31
32 #import "tree.h"
33 #import "container.h"
34 #import "window.h"
35 #import "../common/context.h"
36 #import "../../ucx/utils.h"
37
38 @implementation UiTableDataSource
39
40 - (id)initWithData:(UiList*)list modelInfo:(UiModelInfo*)modelinfo {
41 data = list;
42 info = modelinfo;
43 lastSelection = NULL;
44 return self;
45 }
46
47 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableview {
48 return data->count(data);
49 }
50
51 - (id)tableView: (NSTableView*)tableview
52 objectValueForTableColumn:(NSTableColumn*)column
53 row:(NSInteger)row
54 {
55 int column_index = [[column identifier]intValue];
56
57 void *row_data = data->get(data, row);
58 void *cell_data = info->getvalue(row_data, column_index);
59
60 BOOL f = false;
61 char *str = ui_type_to_string(info->types[column_index], cell_data, &f);
62 NSString *s = [[NSString alloc]initWithUTF8String:str];
63 return s;
64 }
65
66 - (void)tableView:(NSTableView *)tableview
67 setObjectValue:(id)object
68 forTableColumn:(NSTableColumn *)column
69 row:(NSInteger)row
70 {
71 int column_index = [[column identifier]intValue];
72
73 // TODO
74 }
75
76 - (void)tableViewSelectionDidChange:(NSNotification *)notification {
77 NSTableView *tableview = (NSTableView*)notification.object;
78
79 // create selection object
80 UiListSelection *selection = malloc(sizeof(UiListSelection));
81 selection->count = [tableview numberOfSelectedRows];
82
83 selection->rows = calloc(selection->count, sizeof(int));
84 NSIndexSet *indices = [tableview selectedRowIndexes];
85 NSUInteger index = [indices firstIndex];
86 int i=0;
87 while (index!=NSNotFound) {
88 selection->rows[i] = index;
89 index = [indices indexGreaterThanIndex:index];
90 i++;
91 }
92
93 // create event object
94 UiEvent event;
95 NSWindow *activeWindow = [NSApp keyWindow];
96 if([activeWindow class] == [UiCocoaWindow class]) {
97 event.obj = [(UiCocoaWindow*)activeWindow object];
98 event.window = event.obj->window;
99 event.document = event.obj->ctx->document;
100 } else {
101 event.window = NULL;
102 event.document = NULL;
103 }
104 event.eventdata = selection;
105 event.intval = selection->count == 0 ? -1 : selection->rows[0];
106
107 // callback
108 info->selection(&event, info->userdata);
109
110 // cleanup
111 if(lastSelection) {
112 free(lastSelection->rows);
113 free(lastSelection);
114 }
115 lastSelection = selection;
116 }
117
118 - (void)handleDoubleAction:(id)sender {
119 // create event object
120 UiEvent event;
121 NSWindow *activeWindow = [NSApp keyWindow];
122 if([activeWindow class] == [UiCocoaWindow class]) {
123 event.obj = [(UiCocoaWindow*)activeWindow object];
124 event.window = event.obj->window;
125 event.document = event.obj->ctx->document;
126 } else {
127 event.window = NULL;
128 event.document = NULL;
129 }
130 event.eventdata = lastSelection;
131 event.intval = lastSelection->count == 0 ? -1 : lastSelection->rows[0];
132
133 info->activate(&event, info->userdata);
134 }
135
136 @end
137
138
139 UIWIDGET ui_table(UiObject *obj, UiList *model, UiModelInfo *modelinfo) {
140 UiContainer *ct = uic_get_current_container(obj);
141 NSRect frame = ct->getframe(ct);
142
143 NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:frame];
144 [scrollview setHasVerticalScroller:YES];
145 //[scrollvew setHasHorizontalScroller:YES];
146 [scrollview setBorderType:NSNoBorder];
147 //[scrollview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
148
149 NSTableView *tableview = [[NSTableView alloc]initWithFrame:frame];
150 [scrollview setDocumentView:tableview];
151 [tableview setAllowsMultipleSelection: YES];
152
153 // add columns
154 for(int i=0;i<modelinfo->columns;i++) {
155 NSString *cid = [[NSString alloc]initWithFormat: @"%d", i];
156 NSTableColumn *column = [[NSTableColumn alloc]initWithIdentifier:cid];
157
158 NSString *title = [[NSString alloc]initWithUTF8String: modelinfo->titles[i]];
159 [[column headerCell] setStringValue:title];
160
161 [tableview addTableColumn:column];
162 }
163
164 UiTableDataSource *source = [[UiTableDataSource alloc]initWithData:model modelInfo:modelinfo];
165 [tableview setDataSource:source];
166 [tableview setDelegate:source];
167
168 [tableview setDoubleAction:@selector(handleDoubleAction:)];
169 [tableview setTarget:source];
170
171
172 ct->add(ct, scrollview);
173 return scrollview;
174 }
175
176
177 // TODO: motif code duplicate
178 char* ui_type_to_string(UiModelType type, void *data, BOOL *free) {
179 switch(type) {
180 case UI_STRING: *free = FALSE; return data;
181 case UI_INTEGER: {
182 *free = TRUE;
183 int *val = data;
184 sstr_t str = ucx_asprintf(ucx_default_allocator(), "%d", *val);
185 return str.ptr;
186 }
187 }
188 *free = FALSE;
189 return NULL;
190 }

mercurial