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 - (BOOL)tableView:(NSTableView *)tableview isGroupRow:(NSInteger)row { |
|
137 return NO; |
|
138 } |
|
139 |
|
140 @end |
|
141 |
|
142 |
|
143 UIWIDGET ui_table(UiObject *obj, UiList *model, UiModelInfo *modelinfo) { |
|
144 UiContainer *ct = uic_get_current_container(obj); |
|
145 NSRect frame = ct->getframe(ct); |
|
146 |
|
147 NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:frame]; |
|
148 [scrollview setHasVerticalScroller:YES]; |
|
149 //[scrollvew setHasHorizontalScroller:YES]; |
|
150 [scrollview setBorderType:NSNoBorder]; |
|
151 //[scrollview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; |
|
152 |
|
153 NSTableView *tableview = [[NSTableView alloc]initWithFrame:frame]; |
|
154 [scrollview setDocumentView:tableview]; |
|
155 [tableview setAllowsMultipleSelection: YES]; |
|
156 //[tableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList]; |
|
157 |
|
158 // add columns |
|
159 for(int i=0;i<modelinfo->columns;i++) { |
|
160 NSString *cid = [[NSString alloc]initWithFormat: @"%d", i]; |
|
161 NSTableColumn *column = [[NSTableColumn alloc]initWithIdentifier:cid]; |
|
162 |
|
163 NSString *title = [[NSString alloc]initWithUTF8String: modelinfo->titles[i]]; |
|
164 [[column headerCell] setStringValue:title]; |
|
165 |
|
166 [tableview addTableColumn:column]; |
|
167 } |
|
168 |
|
169 UiTableDataSource *source = [[UiTableDataSource alloc]initWithData:model modelInfo:modelinfo]; |
|
170 [tableview setDataSource:source]; |
|
171 [tableview setDelegate:source]; |
|
172 |
|
173 [tableview setDoubleAction:@selector(handleDoubleAction:)]; |
|
174 [tableview setTarget:source]; |
|
175 |
|
176 |
|
177 ct->add(ct, scrollview); |
|
178 return scrollview; |
|
179 } |
|
180 |
|
181 |
|
182 UIWIDGET ui_listview_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
|
183 UiContainer *ct = uic_get_current_container(obj); |
|
184 NSRect frame = ct->getframe(ct); |
|
185 |
|
186 NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:frame]; |
|
187 [scrollview setHasVerticalScroller:YES]; |
|
188 |
|
189 [scrollview setBorderType:NSNoBorder]; |
|
190 |
|
191 NSTableView *tableview = [[NSTableView alloc]initWithFrame:frame]; |
|
192 [scrollview setDocumentView:tableview]; |
|
193 [tableview setAllowsMultipleSelection: NO]; |
|
194 |
|
195 // add single column |
|
196 NSTableColumn *column = [[NSTableColumn alloc]initWithIdentifier:@"c"]; |
|
197 [tableview addTableColumn:column]; |
|
198 |
|
199 // create model info |
|
200 UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, -1); |
|
201 |
|
202 // add source |
|
203 UiTableDataSource *source = [[UiTableDataSource alloc]initWithData:list->list modelInfo:modelinfo]; |
|
204 |
|
205 [tableview setDataSource:source]; |
|
206 [tableview setDelegate:source]; |
|
207 |
|
208 [tableview setDoubleAction:@selector(handleDoubleAction:)]; |
|
209 [tableview setTarget:source]; |
|
210 |
|
211 ct->add(ct, scrollview); |
|
212 return scrollview; |
|
213 } |
|
214 |
|
215 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
|
216 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr)); |
|
217 listptr->list = list; |
|
218 return ui_listview_var(obj, listptr, getvalue, f, udata); |
|
219 } |
|
220 |
|
221 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
|
222 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST); |
|
223 if(var) { |
|
224 UiListVar *value = var->value; |
|
225 return ui_listview_var(obj, value->listptr, getvalue, f, udata); |
|
226 } else { |
|
227 // TODO: error |
|
228 } |
|
229 return NULL; |
|
230 } |
|
231 |
|
232 |
|
233 // TODO: motif code duplicate |
|
234 char* ui_type_to_string(UiModelType type, void *data, BOOL *free) { |
|
235 switch(type) { |
|
236 case UI_STRING: *free = FALSE; return data; |
|
237 case UI_INTEGER: { |
|
238 *free = TRUE; |
|
239 int *val = data; |
|
240 sstr_t str = ucx_asprintf(ucx_default_allocator(), "%d", *val); |
|
241 return str.ptr; |
|
242 } |
|
243 } |
|
244 *free = FALSE; |
|
245 return NULL; |
|
246 } |
|