Added initial NSTableview support

This commit is contained in:
Armando Rivera 2021-08-02 20:58:29 -04:00
parent c945a6cb86
commit d3421b1cd4
3 changed files with 151 additions and 0 deletions

View File

@ -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".}

View File

@ -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 <NSTableViewDataSource, NSTableViewDelegate>
{
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
}

View File

@ -0,0 +1,115 @@
#import <Cocoa/Cocoa.h>
#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];
}