ui/cocoa/ListDelegate.m

changeset 109
c3dfcb8f0be7
equal deleted inserted replaced
108:77254bd6dccb 109:c3dfcb8f0be7
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2025 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 "ListDelegate.h"
30
31 @implementation ListDelegate
32
33 - (id)init:(NSTableView*) tableview obj:(UiObject*)obj {
34 _tableview = tableview;
35 _obj = obj;
36 return self;
37 }
38
39 - (void)activateEvent:(id)sender {
40 NSTableView *table = sender;
41 if(_onactivate) {
42 int row = (int)table.clickedRow;
43
44 UiListSelection sel;
45 sel.count = 1;
46 sel.rows = &row;
47
48 UiEvent event;
49 event.obj = _obj;
50 event.window = event.obj->window;
51 event.document = event.obj->ctx->document;
52 event.eventdata = &sel;
53 event.eventdatatype = UI_EVENT_DATA_LIST_SELECTION;
54 event.intval = row;
55 event.set = ui_get_setop();
56
57 _onactivate(&event, _onactivatedata);
58 }
59 }
60
61 - (void) tableViewSelectionDidChange:(NSNotification *) notification {
62 if(_onselection) {
63 UiListSelection sel = ui_tableview_selection(_tableview);
64
65 UiEvent event;
66 event.obj = _obj;
67 event.window = event.obj->window;
68 event.document = event.obj->ctx->document;
69 event.eventdata = &sel;
70 event.eventdatatype = UI_EVENT_DATA_LIST_SELECTION;
71 event.intval = 0;
72 event.set = ui_get_setop();
73
74 _onselection(&event, _onselectiondata);
75 }
76 }
77
78 @end
79
80 UiListSelection ui_tableview_selection(NSTableView *tableview) {
81 NSIndexSet *indexSet = tableview.selectedRowIndexes;
82 NSUInteger count = [indexSet count];
83
84 if(count == 0) {
85 return (UiListSelection){0, NULL};
86 }
87
88 int *rows = calloc(count, sizeof(int));
89
90 __block NSUInteger i = 0;
91 [indexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop) {
92 rows[i++] = (int)index;
93 }];
94
95 UiListSelection sel;
96 sel.count = (int)count;
97 sel.rows = rows;
98 return sel;
99 }

mercurial