summaryrefslogtreecommitdiff
path: root/listbox_darwin.m
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-05-13 23:35:04 -0400
committerPietro Gagliardi <[email protected]>2014-05-13 23:35:04 -0400
commit58b07a83eaf296d31523a30288705bf820a4c9d7 (patch)
tree67df2674fd9f23944db78b8e666bdeeb47906dc8 /listbox_darwin.m
parentb4cbccb40244587bbc4ed46980b2acd75a4ba23b (diff)
Migrated listbox_darwin.go to using Objective-C directly instead of through the runtime. Due to linking issues on Mac OS X 10.6, I haven't made this live yet; I need to repair my 10.6 setup before I can actually test. This won't be done before midnight, so preemptive commit. The new listbox_darwin.go is lbnew.
Diffstat (limited to 'listbox_darwin.m')
-rw-r--r--listbox_darwin.m142
1 files changed, 142 insertions, 0 deletions
diff --git a/listbox_darwin.m b/listbox_darwin.m
new file mode 100644
index 0000000..0a5ba97
--- /dev/null
+++ b/listbox_darwin.m
@@ -0,0 +1,142 @@
+// 13 may 2014
+
+#include "objc_darwin.h"
+#include "listbox_darwin.h"
+#include <Foundation/NSDictionary.h>
+#include <AppKit/NSArrayController.h>
+#include <AppKit/NSTableColumn.h>
+#include <AppKit/NSTableView.h>
+#include <Foundation/NSIndexSet.h>
+
+#define to(T, x) ((T *) (x))
+#define toNSMutableDictionary(x) to(NSMutableDictionary, (x))
+#define toNSArrayController(x) to(NSArrayController, (x))
+#define toNSTableColumn(x) to(NSTableColumn, (x))
+#define toNSTableView(x) to(NSTableView, (x))
+#define toNSIndexSet(x) to(NSIndexSet, (x))
+
+#define toNSInteger(x) ((NSInteger) (x))
+#define fromNSInteger(x) ((intptr_t) (x))
+#define toNSUInteger(x) ((NSUInteger) (x))
+#define fromNSUInteger(x) ((uintptr_t) (x))
+
+id toListboxItem(id key, id value)
+{
+ return [NSMutableDictionary dictionaryWithObject:value forKey:key];
+}
+
+id fromListboxItem(id item, id key)
+{
+ return [toNSMutableDictionary(item) objectForKey:key];
+}
+
+id newListboxArray(void)
+{
+ NSArrayController *ac;
+
+ ac = [NSArrayController new];
+ [ac setAutomaticallyRearrangesObjects:NO];
+ return ac;
+}
+
+void listboxArrayAppend(id ac, id item)
+{
+ [toNSArrayController(ac) addObject:item];
+}
+
+void listboxArrayInsertBefore(id ac, id item, uintptr_t before)
+{
+ [toNSArrayController(ac) insertObject:item atArrangedObjectIndex:toNSUInteger(before)];
+}
+
+void listboxArrayDelete(id ac, uintptr_t index)
+{
+ [toNSArrayController(ac) removeObjectAtArrangedObjectIndex:toNSUInteger(index)];
+}
+
+id listboxArrayItemAt(id ac, uintptr_t index)
+{
+ NSArrayController *array;
+
+ array = toNSArrayController(ac);
+ return [[array arrangedObjects] objectAtIndex:toNSUInteger(index)];
+}
+
+void bindListboxArray(id tableColumn, id bindwhat, id ac, id keyPath)
+{
+ [toNSTableColumn(tableColumn) bind:bindwhat
+ toObject:ac
+ withKeyPath:keyPath
+ options:nil]; // no options
+}
+
+id boundListboxArray(id tableColumn, id boundwhat)
+{
+ return [[toNSTableColumn(tableColumn) infoForBinding:boundwhat]
+ objectForKey:NSObservedObjectKey];
+}
+
+id makeListboxTableColumn(id identifier)
+{
+ NSTableColumn *column;
+ NSCell *dataCell;
+
+ column = [[NSTableColumn alloc] initWithIdentifier:identifier];
+ [column setEditable:NO];
+ // to set the font for each item, we set the font of the "data cell", which is more aptly called the "cell template"
+ dataCell = [column dataCell];
+ // TODO pull the one from sysdata_darwin.m
+ objc_setFont(dataCell, NSRegularControlSize);
+ [column setDataCell:dataCell];
+ // TODO other properties?
+ return column;
+}
+
+id listboxTableColumn(id listbox, id identifier)
+{
+ return [toNSTableView(listbox) tableColumnWithIdentifier:identifier];
+}
+
+id makeListbox(id tableColumn, BOOL multisel)
+{
+ NSTableView *listbox;
+
+ listbox = [[NSTableView alloc]
+ initWithFrame:NSMakeRect(0, 0, 100, 100)];
+ [listbox addTableColumn:tableColumn];
+ [listbox setAllowsMultipleSelection:multisel];
+ [listbox setAllowsEmptySelection:YES];
+ [listbox setHeaderView:nil];
+ // TODO other prperties?
+ return listbox;
+}
+
+id listboxSelectedRowIndexes(id listbox)
+{
+ return [toNSTableView(listbox) selectedRowIndexes];
+}
+
+uintptr_t listboxIndexesCount(id indexes)
+{
+ return fromNSUInteger([toNSIndexSet(indexes) count]);
+}
+
+uintptr_t listboxIndexesFirst(id indexes)
+{
+ return fromNSUInteger([toNSIndexSet(indexes) firstIndex]);
+}
+
+uintptr_t listboxIndexesNext(id indexes, uintptr_t prev)
+{
+ return fromNSUInteger([toNSIndexSet(indexes) indexGreaterThanIndex:toNSUInteger(prev)]);
+}
+
+intptr_t listboxLen(id listbox)
+{
+ return fromNSInteger([toNSTableView(listbox) numberOfRows]);
+}
+
+void listboxDeselectAll(id listbox)
+{
+ [toNSTableView(listbox) deselectAll:listbox];
+}