From d3421b1cd49eadf06a48ddb2ed7d9390d75bec86 Mon Sep 17 00:00:00 2001 From: Armando Rivera Date: Mon, 2 Aug 2021 20:58:29 -0400 Subject: [PATCH] Added initial NSTableview support --- src/cocoa/NSTableview.nim | 7 +++ src/cocoa/widgets/col.h | 29 +++++++++ src/cocoa/widgets/tableview.m | 115 ++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/cocoa/NSTableview.nim create mode 100644 src/cocoa/widgets/tableview.m diff --git a/src/cocoa/NSTableview.nim b/src/cocoa/NSTableview.nim new file mode 100644 index 0000000..893380d --- /dev/null +++ b/src/cocoa/NSTableview.nim @@ -0,0 +1,7 @@ +{.compile: "widgets/tableview.m".} + +import NSFunctions + +proc newTableview*(parent: ID, left, top, width, height: cint): ID {.cdecl, importc: "createTableView".} +proc addColumn*(parent: ID, name: cstring) {.cdecl, importc: "newTableColumn".} +proc addRow*(parent: ID, person: cstring) {.cdecl, importc: "addRow".} \ No newline at end of file diff --git a/src/cocoa/widgets/col.h b/src/cocoa/widgets/col.h index 5bbfa0d..9a729c0 100644 --- a/src/cocoa/widgets/col.h +++ b/src/cocoa/widgets/col.h @@ -64,6 +64,8 @@ void Example(void); */ + typedef void(*EVENT_ACTION)(id, const char*); + enum { akNone, akRight, @@ -133,13 +135,38 @@ - (void)selected:(id)sender; @end + @interface CocoaTableView : NSScrollView + { + NSTableView *tbl; + NSTableColumn *tblc; + NSTableColumn *tbld; + NSMutableArray *db; + NSMutableDictionary *dict; + + + int cnt; + } + @property (retain) NSTableView *tbl; + @property (retain) NSMutableArray *db; + @property (retain) NSMutableDictionary *dict; + + + - (id)initWithFrame:(NSRect)frame; + - (void)doubleclicked:(id)sender; + - (void)selected:(id)sender; + @end + @interface CocoaButton: NSButton{ ACTION buttonAction; + EVENT_ACTION eventAction; } @property ACTION buttonAction; + @property EVENT_ACTION eventAction; - (void) click:(id)sender; + - (void)mouseEntered:(NSEvent *)theEvent; + - (void)mouseExited:(NSEvent *)theEvent; @end @interface RadioButton: NSButton{ @@ -593,6 +620,8 @@ id createTabView(id parent, const char *label, int x, int y, int width, int height); void addTab(id parent, const char *name); id getTab(id parent, const char *name); + + void eventAction(id widget, EVENT_ACTION callback); #ifdef __cplusplus } diff --git a/src/cocoa/widgets/tableview.m b/src/cocoa/widgets/tableview.m new file mode 100644 index 0000000..1879d54 --- /dev/null +++ b/src/cocoa/widgets/tableview.m @@ -0,0 +1,115 @@ +#import + +#import "col.h" + +#ifndef NSSTR + #define NSSTR(txt) [NSString stringWithUTF8String:txt] +#endif + + + +@implementation CocoaTableView + +@synthesize tbl; +@synthesize db; +@synthesize dict; + +- (id)initWithFrame:(NSRect)frame { + self = [super initWithFrame:frame]; + db = [NSMutableArray new]; + dict = [NSMutableDictionary new]; + + if (self) { + + tbl = [[NSTableView alloc] initWithFrame: NSMakeRect(5,40,335,185)]; + [tbl setGridStyleMask: NSTableViewGridNone]; + [tbl setAllowsColumnSelection: YES]; + [tbl setAllowsColumnReordering: YES]; + [tbl setAllowsEmptySelection: NO]; + [tbl setAllowsMultipleSelection: NO]; + [tbl setColumnAutoresizingStyle: NSTableViewUniformColumnAutoresizingStyle]; + + [tbl setDataSource: self]; + [tbl setDelegate: self]; + [tbl setTarget:self]; + [tbl setDoubleAction:@selector(doubleclicked:)]; + + [self setHasHorizontalScroller: YES]; + [self setHasVerticalScroller: YES]; + [self setDocumentView: tbl]; + [self setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; + + + [tbl reloadData]; + + cnt = 0; + } + return self; +} + +- (void)doubleclicked:(id)sender +{ + // int rowIndex = [sender selectedRow]; +} + +- (void)selected:(id)sender +{ + // [tbl selectRowIndexes:[NSIndexSet indexSetWithIndex:item->row] byExtendingSelection:NO]; +} + +-(void)tableViewSelectionDidChange:(NSNotification *)notification +{ + NSInteger row = [tbl selectedRow]; + +} + +- (long)numberOfRowsInTableView:(NSTableView *)aTableView +{ + return db.count; + // return dict.count; +} + +-(id)tableView:(NSTableView *)tableView + objectValueForTableColumn:(NSTableColumn *)tableColumn + row:(NSInteger)row +{ + // NSString *columnIdentifier = [tableColumn identifier]; + + return [self.db[row] valueForKey: [tableColumn identifier]]; + // return [self.dict valueForKey: [tableColumn identifier]]; + +} + + +@end + + +id createTableView(id parent,int l, int t, int w, int h){ + id self = [[[CocoaTableView alloc] initWithFrame:NSMakeRect( l, t, w, h )] autorelease]; + addToParent(parent, self); + return self; +} + +void addRow(id parent, const char *person) { + NSArray *tmpArray = [NSSTR(person) componentsSeparatedByString: @"|"]; + NSMutableDictionary *myDic = [NSMutableDictionary new]; + NSArray *columns = [[parent tbl] tableColumns]; + + for (int i = 0; i < tmpArray.count; i++) { + NSString *ident = [[parent tbl] tableColumns][i].identifier; + [myDic setObject: tmpArray[i] forKey: ident]; + } + + [[parent db] addObject: myDic]; + + [[parent tbl] reloadData]; +} + +void newTableColumn(id parent, const char *name) { + NSString *title = [NSString stringWithUTF8String: name]; + id tCol = [[NSTableColumn alloc] initWithIdentifier: [title lowercaseString]]; + [tCol setMinWidth: 200]; + [tCol setTitle: title]; + + [[parent tbl] addTableColumn: tCol]; +} \ No newline at end of file