Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new API functions for table header clicks and sort indicators. #513

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions darwin/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct uiTable {
uiprivScrollViewData *d;
int backgroundColumn;
uiTableModel *m;
void (*headerOnClicked)(uiTable *, int, void *);
void *headerOnClickedData;
};

// tablecolumn.m
Expand Down
49 changes: 49 additions & 0 deletions darwin/table.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ @interface uiprivTableView : NSTableView {
uiTableModel *uiprivM;
}
- (id)initWithFrame:(NSRect)r uiprivT:(uiTable *)t uiprivM:(uiTableModel *)m;
- (uiTable *)uiTable;
@end

@implementation uiprivTableView
Expand All @@ -30,6 +31,11 @@ - (id)initWithFrame:(NSRect)r uiprivT:(uiTable *)t uiprivM:(uiTableModel *)m
return self;
}

- (uiTable *)uiTable
{
return self->uiprivT;
}

// TODO is this correct for overflow scrolling?
static void setBackgroundColor(uiprivTableView *t, NSTableRowView *rv, NSInteger row)
{
Expand Down Expand Up @@ -88,6 +94,12 @@ - (void)tableView:(NSTableView *)tv didAddRowView:(NSTableRowView *)rv forRow:(N
setBackgroundColor((uiprivTableView *) tv, rv, row);
}

- (void)tableView:(uiprivTableView *)tv didClickTableColumn:(NSTableColumn *) tc
{
uiTable *t = [tv uiTable];
t->headerOnClicked(t, [[tc identifier] intValue], t->headerOnClickedData);
}

@end

uiTableModel *uiNewTableModel(uiTableModelHandler *mh)
Expand Down Expand Up @@ -170,6 +182,17 @@ static void uiTableDestroy(uiControl *c)
uiFreeControl(uiControl(t));
}

void uiTableHeaderOnClicked(uiTable *t, void (*f)(uiTable *, int, void *), void *data)
{
t->headerOnClicked = f;
t->headerOnClickedData = data;
}

static void defaultHeaderOnClicked(uiTable *table, int column, void *data)
{
// do nothing
}

uiTable *uiNewTable(uiTableParams *p)
{
uiTable *t;
Expand Down Expand Up @@ -209,10 +232,36 @@ static void uiTableDestroy(uiControl *c)
sp.VScroll = YES;
t->sv = uiprivMkScrollView(&sp, &(t->d));

uiTableHeaderOnClicked(t, defaultHeaderOnClicked, NULL);

// TODO WHY DOES THIS REMOVE ALL GRAPHICAL GLITCHES?
// I got the idea from http://jwilling.com/blog/optimized-nstableview-scrolling/ but that was on an unrelated problem I didn't seem to have (although I have small-ish tables to start with)
// I don't get layer-backing... am I supposed to layer-back EVERYTHING manually? I need to check Interface Builder again...
[t->sv setWantsLayer:YES];

return t;
}

uiSort uiTableHeaderSortIndicator(uiTable *t, int lcol)
{
NSTableColumn *tc = [t->tv tableColumnWithIdentifier:[@(lcol) stringValue]];
NSString *si = [[t->tv indicatorImageInTableColumn:tc] name];
if ([si isEqualToString:@"NSAscendingSortIndicator"])
return uiSortAscending;
else if ([si isEqualToString:@"NSDescendingSortIndicator"])
return uiSortDescending;
return uiSortNone;
}

void uiTableHeaderSetSortIndicator(uiTable *t, int lcol, uiSort order)
{
NSTableColumn *tc = [t->tv tableColumnWithIdentifier:[@(lcol) stringValue]];
NSImage *img;
if (order == uiSortAscending)
img = [NSImage imageNamed:@"NSAscendingSortIndicator"];
else if (order == uiSortDescending)
img = [NSImage imageNamed:@"NSDescendingSortIndicator"];
else
img = nil;
[t->tv setIndicatorImage:img inTableColumn:tc];
}
28 changes: 21 additions & 7 deletions darwin/tablecolumn.m
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ void uiTableAppendTextColumn(uiTable *t, const char *name, int textModelColumn,
struct textColumnCreateParams p;
uiprivTableColumn *col;
NSString *str;
NSString *ident;

memset(&p, 0, sizeof (struct textColumnCreateParams));
p.t = t;
Expand All @@ -597,8 +598,9 @@ void uiTableAppendTextColumn(uiTable *t, const char *name, int textModelColumn,
else
p.textParams = uiprivDefaultTextColumnOptionalParams;

ident = [@([[t->tv tableColumns] count]) stringValue];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:ident params:&p];
str = [NSString stringWithUTF8String:name];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:str params:&p];
[col setTitle:str];
[t->tv addTableColumn:col];
}
Expand All @@ -608,6 +610,7 @@ void uiTableAppendImageColumn(uiTable *t, const char *name, int imageModelColumn
struct textColumnCreateParams p;
uiprivTableColumn *col;
NSString *str;
NSString *ident;

memset(&p, 0, sizeof (struct textColumnCreateParams));
p.t = t;
Expand All @@ -616,8 +619,9 @@ void uiTableAppendImageColumn(uiTable *t, const char *name, int imageModelColumn
p.makeImageView = YES;
p.imageModelColumn = imageModelColumn;

ident = [@([[t->tv tableColumns] count]) stringValue];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:ident params:&p];
str = [NSString stringWithUTF8String:name];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:str params:&p];
[col setTitle:str];
[t->tv addTableColumn:col];
}
Expand All @@ -627,6 +631,7 @@ void uiTableAppendImageTextColumn(uiTable *t, const char *name, int imageModelCo
struct textColumnCreateParams p;
uiprivTableColumn *col;
NSString *str;
NSString *ident;

memset(&p, 0, sizeof (struct textColumnCreateParams));
p.t = t;
Expand All @@ -643,8 +648,9 @@ void uiTableAppendImageTextColumn(uiTable *t, const char *name, int imageModelCo
p.makeImageView = YES;
p.imageModelColumn = imageModelColumn;

ident = [@([[t->tv tableColumns] count]) stringValue];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:ident params:&p];
str = [NSString stringWithUTF8String:name];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:str params:&p];
[col setTitle:str];
[t->tv addTableColumn:col];
}
Expand All @@ -654,6 +660,7 @@ void uiTableAppendCheckboxColumn(uiTable *t, const char *name, int checkboxModel
struct textColumnCreateParams p;
uiprivTableColumn *col;
NSString *str;
NSString *ident;

memset(&p, 0, sizeof (struct textColumnCreateParams));
p.t = t;
Expand All @@ -663,8 +670,9 @@ void uiTableAppendCheckboxColumn(uiTable *t, const char *name, int checkboxModel
p.checkboxModelColumn = checkboxModelColumn;
p.checkboxEditableModelColumn = checkboxEditableModelColumn;

ident = [@([[t->tv tableColumns] count]) stringValue];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:ident params:&p];
str = [NSString stringWithUTF8String:name];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:str params:&p];
[col setTitle:str];
[t->tv addTableColumn:col];
}
Expand All @@ -674,6 +682,7 @@ void uiTableAppendCheckboxTextColumn(uiTable *t, const char *name, int checkboxM
struct textColumnCreateParams p;
uiprivTableColumn *col;
NSString *str;
NSString *ident;

memset(&p, 0, sizeof (struct textColumnCreateParams));
p.t = t;
Expand All @@ -691,8 +700,9 @@ void uiTableAppendCheckboxTextColumn(uiTable *t, const char *name, int checkboxM
p.checkboxModelColumn = checkboxModelColumn;
p.checkboxEditableModelColumn = checkboxEditableModelColumn;

ident = [@([[t->tv tableColumns] count]) stringValue];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:ident params:&p];
str = [NSString stringWithUTF8String:name];
col = [[uiprivTextImageCheckboxTableColumn alloc] initWithIdentifier:str params:&p];
[col setTitle:str];
[t->tv addTableColumn:col];
}
Expand All @@ -701,9 +711,11 @@ void uiTableAppendProgressBarColumn(uiTable *t, const char *name, int progressMo
{
uiprivTableColumn *col;
NSString *str;
NSString *ident;

ident = [@([[t->tv tableColumns] count]) stringValue];
col = [[uiprivProgressBarTableColumn alloc] initWithIdentifier:ident table:t model:t->m modelColumn:progressModelColumn];
str = [NSString stringWithUTF8String:name];
col = [[uiprivProgressBarTableColumn alloc] initWithIdentifier:str table:t model:t->m modelColumn:progressModelColumn];
[col setTitle:str];
[t->tv addTableColumn:col];
}
Expand All @@ -712,9 +724,11 @@ void uiTableAppendButtonColumn(uiTable *t, const char *name, int buttonModelColu
{
uiprivTableColumn *col;
NSString *str;
NSString *ident;

ident = [@([[t->tv tableColumns] count]) stringValue];
col = [[uiprivButtonTableColumn alloc] initWithIdentifier:ident table:t model:t->m modelColumn:buttonModelColumn editableColumn:buttonClickableModelColumn];
str = [NSString stringWithUTF8String:name];
col = [[uiprivButtonTableColumn alloc] initWithIdentifier:str table:t model:t->m modelColumn:buttonModelColumn editableColumn:buttonClickableModelColumn];
[col setTitle:str];
[t->tv addTableColumn:col];
}
17 changes: 17 additions & 0 deletions test/page16.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ static void modelSetCellValue(uiTableModelHandler *mh, uiTableModel *m, int row,

static uiTableModel *m;

static void headerOnClicked(uiTable *t, int col, void *data)
{
static int prev = 0;

if (prev != col)
uiTableHeaderSetSortIndicator(t, prev, uiSortNone);

if (uiTableHeaderSortIndicator(t, col) == uiSortAscending)
uiTableHeaderSetSortIndicator(t, col, uiSortDescending);
else
uiTableHeaderSetSortIndicator(t, col, uiSortAscending);

prev = col;
}

uiBox *makePage16(void)
{
uiBox *page16;
Expand Down Expand Up @@ -152,6 +167,8 @@ uiBox *makePage16(void)
uiTableAppendProgressBarColumn(t, "Progress Bar",
8);

uiTableHeaderOnClicked(t, headerOnClicked, NULL);

return page16;
}

Expand Down
22 changes: 22 additions & 0 deletions ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,12 @@ _UI_EXTERN uiTableValue *uiNewTableValueColor(double r, double g, double b, doub
// TODO define whether all this, for both uiTableValue and uiAttribute, is undefined behavior or a caught error
_UI_EXTERN void uiTableValueColor(const uiTableValue *v, double *r, double *g, double *b, double *a);

_UI_ENUM(uiSort) {
uiSortNone,
uiSortAscending,
uiSortDescending
};

// uiTableModel is an object that provides the data for a uiTable.
// This data is returned via methods you provide in the
// uiTableModelHandler struct.
Expand Down Expand Up @@ -1458,6 +1464,22 @@ _UI_EXTERN void uiTableAppendButtonColumn(uiTable *t,
// uiNewTable() creates a new uiTable with the specified parameters.
_UI_EXTERN uiTable *uiNewTable(uiTableParams *params);

// uiTableHeaderSetSortIndicator() sets the sort indicator of the table
// header to display an appropriate arrow on the column header
_UI_EXTERN void uiTableHeaderSetSortIndicator(uiTable *t,
int column,
uiSort order);

// uiTableHeaderSortIndicator returns the sort indicator of the specified
// column
_UI_EXTERN uiSort uiTableHeaderSortIndicator(uiTable *t, int column);

// uiTableHeaderOnClicked() sets a callback function to be called
// when a table column header is clicked
_UI_EXTERN void uiTableHeaderOnClicked(uiTable *t,
void (*f)(uiTable *table, int column, void *data),
void *data);

#ifdef __cplusplus
}
#endif
Expand Down
59 changes: 59 additions & 0 deletions unix/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct uiTable {
// TODO document this properly
GHashTable *indeterminatePositions;
guint indeterminateTimer;
void (*headerOnClicked)(uiTable *, int, void *);
void *headerOnClickedData;
};

// use the same size as GtkFileChooserWidget's treeview
Expand Down Expand Up @@ -327,13 +329,68 @@ static void buttonColumnClicked(GtkCellRenderer *r, gchar *pathstr, gpointer dat
onEdited(p->m, p->modelColumn, pathstr, NULL, NULL);
}

uiSort uiTableHeaderSortIndicator(uiTable *t, int lcol)
{
GtkTreeViewColumn *c = gtk_tree_view_get_column(t->tv, lcol);

if (c == NULL || gtk_tree_view_column_get_sort_indicator(c) == FALSE)
return uiSortNone;

if (gtk_tree_view_column_get_sort_order(c) == GTK_SORT_ASCENDING)
return uiSortAscending;
else
return uiSortDescending;
}

void uiTableHeaderSetSortIndicator(uiTable *t, int lcol, uiSort order)
{
GtkTreeViewColumn *c = gtk_tree_view_get_column(t->tv, lcol);

if (c == NULL)
return;

if (order == uiSortNone) {
gtk_tree_view_column_set_sort_indicator(c, FALSE);
return;
}

gtk_tree_view_column_set_sort_indicator(c, TRUE);
if (order == uiSortAscending)
gtk_tree_view_column_set_sort_order(c, GTK_SORT_ASCENDING);
else
gtk_tree_view_column_set_sort_order(c, GTK_SORT_DESCENDING);
}

void uiTableHeaderOnClicked(uiTable *t, void (*f)(uiTable *, int, void *), void *data)
{
t->headerOnClicked = f;
t->headerOnClickedData = data;
}

static void defaultHeaderOnClicked(uiTable *table, int column, void *data)
{
// do nothing
}

static void headerOnClicked(GtkTreeViewColumn *c, gpointer data)
{
guint i;
uiTable *t = uiTable(data);

for (i = 0; i < gtk_tree_view_get_n_columns(t->tv); ++i)
if (gtk_tree_view_get_column(t->tv, i) == c)
t->headerOnClicked(t, i, t->headerOnClickedData);
}

static GtkTreeViewColumn *addColumn(uiTable *t, const char *name)
{
GtkTreeViewColumn *c;

c = gtk_tree_view_column_new();
gtk_tree_view_column_set_resizable(c, TRUE);
gtk_tree_view_column_set_title(c, name);
gtk_tree_view_column_set_clickable(c, 1);
g_signal_connect(c, "clicked", G_CALLBACK(headerOnClicked), t);
gtk_tree_view_append_column(t->tv, c);
return c;
}
Expand Down Expand Up @@ -520,5 +577,7 @@ uiTable *uiNewTable(uiTableParams *p)
t->indeterminatePositions = g_hash_table_new_full(rowcolHash, rowcolEqual,
uiprivFree, uiprivFree);

uiTableHeaderOnClicked(t, defaultHeaderOnClicked, NULL);

return t;
}
Loading