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 function to set a table row double click callback. #516

Open
wants to merge 1 commit 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 (*onRowDoubleClicked)(uiTable *, int, void *);
void *onRowDoubleClickedData;
};

// tablecolumn.m
Expand Down
26 changes: 26 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;
- (void)onDoubleAction:(id)sender;
@end

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

- (void)onDoubleAction:(id)sender
{
uiTable *t = self->uiprivT;
NSInteger row = [self clickedRow];

if (row < 0)
return;

(*(t->onRowDoubleClicked))(t, row, t->onRowDoubleClickedData);
}

// TODO is this correct for overflow scrolling?
static void setBackgroundColor(uiprivTableView *t, NSTableRowView *rv, NSInteger row)
{
Expand Down Expand Up @@ -170,6 +182,17 @@ static void uiTableDestroy(uiControl *c)
uiFreeControl(uiControl(t));
}

static void defaultOnRowDoubleClicked(uiTable *table, int row, void *data)
{
// do nothing
}

void uiTableOnRowDoubleClicked(uiTable *t, void (*f)(uiTable *, int, void *), void *data)
{
t->onRowDoubleClicked = f;
t->onRowDoubleClickedData = data;
}

uiTable *uiNewTable(uiTableParams *p)
{
uiTable *t;
Expand Down Expand Up @@ -198,6 +221,9 @@ static void uiTableDestroy(uiControl *c)
[t->tv setAllowsTypeSelect:YES];
// TODO floatsGroupRows — do we even allow group rows?

uiTableOnRowDoubleClicked(t, defaultOnRowDoubleClicked, NULL);
[t->tv setDoubleAction: @selector(onDoubleAction:)];

memset(&sp, 0, sizeof (uiprivScrollViewCreateParams));
sp.DocumentView = t->tv;
// this is what Interface Builder sets it to
Expand Down
7 changes: 7 additions & 0 deletions test/page16.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ static void modelSetCellValue(uiTableModelHandler *mh, uiTableModel *m, int row,
checkStates[row] = uiTableValueInt(val);
}

static void onRowDoubleClicked(uiTable *table, int row, void *data)
{
printf("Double clicked row %d\n", row);
}

static uiTableModel *m;

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

uiTableOnRowDoubleClicked(t, onRowDoubleClicked, NULL);

return page16;
}

Expand Down
6 changes: 6 additions & 0 deletions ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,12 @@ _UI_EXTERN void uiTableAppendButtonColumn(uiTable *t,
int buttonModelColumn,
int buttonClickableModelColumn);

// uiTableOnRowDoubleClicked sets a callback to be called when the user
// double clicks a table row.
_UI_EXTERN void uiTableOnRowDoubleClicked(uiTable *t,
void (*f)(uiTable *t, int row, void *data),
void *data);

// uiNewTable() creates a new uiTable with the specified parameters.
_UI_EXTERN uiTable *uiNewTable(uiTableParams *params);

Expand Down
24 changes: 24 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 (*onRowDoubleClicked)(uiTable *, int, void *);
void *onRowDoubleClickedData;
};

// use the same size as GtkFileChooserWidget's treeview
Expand Down Expand Up @@ -494,6 +496,25 @@ static void uiTableDestroy(uiControl *c)
uiFreeControl(uiControl(t));
}

static void onRowDoubleClicked(GtkTreeView *tv, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
{
uiTable *t = uiTable(data);
gint row = gtk_tree_path_get_indices(path)[0];

(*(t->onRowDoubleClicked))(t, row, t->onRowDoubleClickedData);
}

static void defaultOnRowDoubleClicked(uiTable *table, int row, void *data)
{
// do nothing
}

void uiTableOnRowDoubleClicked(uiTable *t, void (*f)(uiTable *, int, void *), void *data)
{
t->onRowDoubleClicked = f;
t->onRowDoubleClickedData = data;
}

uiTable *uiNewTable(uiTableParams *p)
{
uiTable *t;
Expand All @@ -511,7 +532,10 @@ uiTable *uiNewTable(uiTableParams *p)

t->treeWidget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(t->model));
t->tv = GTK_TREE_VIEW(t->treeWidget);

// TODO set up t->tv
uiTableOnRowDoubleClicked(t, defaultOnRowDoubleClicked, NULL);
g_signal_connect(t->tv, "row-activated", G_CALLBACK(onRowDoubleClicked), t);

gtk_container_add(t->scontainer, t->treeWidget);
// and make the tree view visible; only the scrolled window's visibility is controlled by libui
Expand Down
22 changes: 22 additions & 0 deletions windows/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ void uiTableModelRowDeleted(uiTableModel *m, int oldIndex)
}
}

static void defaultOnRowDoubleClicked(uiTable *table, int row, void *data)
{
// do nothing
}

void uiTableOnRowDoubleClicked(uiTable *t, void (*f)(uiTable *, int, void *), void *data)
{
t->onRowDoubleClicked = f;
t->onRowDoubleClickedData = data;
}

uiTableModelHandler *uiprivTableModelHandler(uiTableModel *m)
{
return m->mh;
Expand Down Expand Up @@ -278,6 +289,15 @@ static BOOL onWM_NOTIFY(uiControl *c, HWND hwnd, NMHDR *nmhdr, LRESULT *lResult)
}
return TRUE;
#endif
case NM_DBLCLK:
{
LVHITTESTINFO ht = {};
ht.pt = ((NMITEMACTIVATE *)nmhdr)->ptAction;
if (ListView_SubItemHitTest(t->hwnd, &ht) == -1)
return FALSE;
(*(t->onRowDoubleClicked))(t, ht.iItem, t->onRowDoubleClickedData);
return TRUE;
}
case LVN_ITEMCHANGED:
{
NMLISTVIEW *nm = (NMLISTVIEW *) nmhdr;
Expand Down Expand Up @@ -518,5 +538,7 @@ uiTable *uiNewTable(uiTableParams *p)
if (SetWindowSubclass(t->hwnd, tableSubProc, 0, (DWORD_PTR) t) == FALSE)
logLastError(L"SetWindowSubclass()");

uiTableOnRowDoubleClicked(t, defaultOnRowDoubleClicked, NULL);

return t;
}
2 changes: 2 additions & 0 deletions windows/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct uiTable {
HWND edit;
int editedItem;
int editedSubitem;
void (*onRowDoubleClicked)(uiTable *, int, void *);
void *onRowDoubleClickedData;
};
extern int uiprivTableProgress(uiTable *t, int item, int subitem, int modelColumn, LONG *pos);

Expand Down