[qwaq] Implement a very simple table view class

It's nowhere near as sophisticated as NSTableView, but this is text, not
graphics.
This commit is contained in:
Bill Currie 2020-03-31 10:55:39 +09:00
parent e15f2097df
commit a1f67b5d6f
3 changed files with 166 additions and 0 deletions

View file

@ -41,6 +41,7 @@ qwaq_app_dat_src= \
ui/proxyview.r \
ui/rect.r \
ui/scrollbar.r \
ui/tableview.r \
ui/textcontext.r \
ui/titlebar.r \
ui/view.r \

View file

@ -0,0 +1,44 @@
#ifndef __qwaq_ui_tableview_h
#define __qwaq_ui_tableview_h
#include "ui/view.h"
@class DrawBuffer;
@class TableView;
@class TableViewColumn;
@class Array;
@protocol TableViewDataSource
-(int)numberOfRows:(TableView *)tableview;
-(View *)tableView:(TableView *)tableView
forColumn:(TableViewColumn *)column
row:(int)row;
-retain;
-release;
@end
@interface TableViewColumn : Object
{
string name;
int width;
}
+(TableViewColumn *)named:(string)name;
+(TableViewColumn *)named:(string)name width:(int)width;
-(string)name;
-(int)width;
@end
@interface TableView : View
{
Array *columns;
DrawBuffer *buffer;
int columns_dirty;
id<TableViewDataSource> dataSource;
Point base;
}
+(TableView *)withRect:(Rect)rect;
-addColumn:(TableViewColumn *)column;
-setDataSource:(id<TableViewDataSource>)dataSource;
@end
#endif//__qwaq_ui_tableview_h

121
ruamoko/qwaq/ui/tableview.r Normal file
View file

@ -0,0 +1,121 @@
#include <Array.h>
#include "ui/listener.h"
#include "ui/tableview.h"
@implementation TableViewColumn
-initWithName:(string)name width:(int)width
{
if (!(self = [super init])) {
return nil;
}
self.name = name;
self.width = width;
return self;
}
+(TableViewColumn *)named:(string)name
{
return [[[self alloc] initWithName:name width:-1] autorelease];
}
+(TableViewColumn *)named:(string)name width:(int)width
{
return [[[self alloc] initWithName:name width:width] autorelease];
}
-(string)name
{
return name;
}
-(int)width
{
return width;
}
-setWidth:(int)width
{
self.width = width;
return self;
}
@end
@implementation TableView
-initWithRect:(Rect)rect
{
if (!(self = [super initWithRect:rect])) {
return nil;
}
options = ofCanFocus | ofRelativeEvents;
columns = [[Array array] retain];
buffer = [[DrawBuffer buffer:size] retain];
[onViewScrolled addListener:self :@selector(onScroll:)];
return self;
}
-(void)dealloc
{
[columns release];
[buffer release];
[dataSource release];
}
+(TableView *)withRect:(Rect)rect
{
return [[[self alloc] initWithRect:rect] autorelease];
}
-addColumn:(TableViewColumn *)column
{
[columns addObject:column];
columns_dirty = 1;
return self;
}
-setDataSource:(id<TableViewDataSource>)dataSource
{
self.dataSource = [dataSource retain];
return self;
}
-resize:(Extent)delta
{
Extent size = self.size;
[super resize:delta];
[buffer resizeTo:self.size];
return self;
}
-draw
{
View *cell;
TableViewColumn *col;
[super draw];
int numCols = [columns count];
int numRows = [dataSource numberOfRows:self];
[buffer clear];
for (int y = 0; y < ylen; y++) {
for (int i = 0, x = 0; i < numCols; i++) {
int row = base.y + y;
if (row >= numRows) {
break;
}
col = [columns objectAtIndex:i];
cell = [dataSource tableView:self forColumn:col row:row];
[[[cell setContext:buffer] moveTo:{x, y}] draw];
x += [col width];
}
}
[textContext blitFromBuffer:buffer to:pos from:[buffer rect]];
return self;
}
-(void)onScroll:(id)sender
{
if (base.y != scroll.y) {
base.y = scroll.y;
[self redraw];
}
}
@end