diff --git a/ui.h b/ui.h index 40aea949..b6be8358 100644 --- a/ui.h +++ b/ui.h @@ -1383,6 +1383,9 @@ struct uiTableParams { // If CellValue() for this column for any row returns NULL, that // row will also use the default background color. int RowBackgroundColorModelColumn; + // MultiSelect sets selection mode for the table. + // 0=single selection, 1=allow multiple rows to be selected + int MultiSelect; }; // uiTable is a uiControl that shows tabular data, allowing users to diff --git a/unix/table.c b/unix/table.c index d1e6fe60..f6b2328f 100644 --- a/unix/table.c +++ b/unix/table.c @@ -18,6 +18,8 @@ struct uiTable { // TODO document this properly GHashTable *indeterminatePositions; guint indeterminateTimer; + void (*onSelectionChanged)(uiTable *, void *); + void *onSelectionChangedData; }; // use the same size as GtkFileChooserWidget's treeview @@ -492,9 +494,28 @@ static void uiTableDestroy(uiControl *c) uiFreeControl(uiControl(t)); } +static void onChanged(GtkTreeSelection *sel, gpointer data) +{ + uiTable* t = uiTable(data); + (*(t->onSelectionChanged))(t, t->onSelectionChangedData); +} + +static void defaultOnSelectionChanged(uiTable *t, void *data) +{ + // do nothing +} + +void uiTableOnSelectionChanged(uiTable *t, void (*f)(uiTable *, void *), void *data) +{ + t->onSelectionChanged = f; + t->onSelectionChangedData = data; +} + uiTable *uiNewTable(uiTableParams *p) { uiTable *t; + GtkTreeSelection* sel; + GtkSelectionMode selMode; uiUnixNewControl(uiTable, t); @@ -518,5 +539,14 @@ uiTable *uiNewTable(uiTableParams *p) t->indeterminatePositions = g_hash_table_new_full(rowcolHash, rowcolEqual, uiprivFree, uiprivFree); + sel = gtk_tree_view_get_selection(t->tv); + selMode = GTK_SELECTION_SINGLE; + if (p->MultiSelect == 1) { + selMode = GTK_SELECTION_MULTIPLE; + } + gtk_tree_selection_set_mode(sel, selMode); + g_signal_connect(sel, "changed", G_CALLBACK(onChanged), t); + uiTableOnSelectionChanged(t, defaultOnSelectionChanged, NULL); + return t; }