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

Implement proper uiTableModel stamps for unix. #505

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions unix/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
typedef struct uiTableModelClass uiTableModelClass;
struct uiTableModel {
GObject parent_instance;
gint stamp;
uiTableModelHandler *mh;
};
struct uiTableModelClass {
Expand Down
55 changes: 26 additions & 29 deletions unix/tablemodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ static GType uiTableModel_get_column_type(GtkTreeModel *mm, gint index)
return G_TYPE_INVALID;
}

#define STAMP_GOOD 0x1234
#define STAMP_BAD 0x5678

static gboolean uiTableModel_get_iter(GtkTreeModel *mm, GtkTreeIter *iter, GtkTreePath *path)
{
uiTableModel *m = uiTableModel(mm);
Expand All @@ -67,21 +64,22 @@ static gboolean uiTableModel_get_iter(GtkTreeModel *mm, GtkTreeIter *iter, GtkTr
goto bad;
if (row >= uiprivTableModelNumRows(m))
goto bad;
iter->stamp = STAMP_GOOD;
iter->stamp = m->stamp;
iter->user_data = GINT_TO_POINTER(row);
return TRUE;
bad:
iter->stamp = STAMP_BAD;
iter->stamp = 0;
return FALSE;
}

// GtkListStore returns NULL on error; let's do that too
static GtkTreePath *uiTableModel_get_path(GtkTreeModel *mm, GtkTreeIter *iter)
{
uiTableModel *m = uiTableModel(mm);
gint row;

if (iter->stamp != STAMP_GOOD)
return NULL;
g_return_val_if_fail(iter->stamp == m->stamp, NULL);

row = GPOINTER_TO_INT(iter->user_data);
return gtk_tree_path_new_from_indices(row, -1);
}
Expand All @@ -95,8 +93,8 @@ static void uiTableModel_get_value(GtkTreeModel *mm, GtkTreeIter *iter, gint col
double r, g, b, a;
GdkRGBA rgba;

if (iter->stamp != STAMP_GOOD)
return;
g_return_if_fail(iter->stamp == m->stamp);

row = GPOINTER_TO_INT(iter->user_data);
tvalue = uiprivTableModelCellValue(m, row, column);
switch (uiprivTableModelColumnType(m, column)) {
Expand Down Expand Up @@ -138,12 +136,12 @@ static gboolean uiTableModel_iter_next(GtkTreeModel *mm, GtkTreeIter *iter)
uiTableModel *m = uiTableModel(mm);
gint row;

if (iter->stamp != STAMP_GOOD)
return FALSE;
g_return_val_if_fail(iter->stamp == m->stamp, FALSE);

row = GPOINTER_TO_INT(iter->user_data);
row++;
if (row >= uiprivTableModelNumRows(m)) {
iter->stamp = STAMP_BAD;
iter->stamp = 0;
return FALSE;
}
iter->user_data = GINT_TO_POINTER(row);
Expand All @@ -152,14 +150,15 @@ static gboolean uiTableModel_iter_next(GtkTreeModel *mm, GtkTreeIter *iter)

static gboolean uiTableModel_iter_previous(GtkTreeModel *mm, GtkTreeIter *iter)
{
uiTableModel *m = uiTableModel(mm);
gint row;

if (iter->stamp != STAMP_GOOD)
return FALSE;
g_return_val_if_fail(iter->stamp == m->stamp, FALSE);

row = GPOINTER_TO_INT(iter->user_data);
row--;
if (row < 0) {
iter->stamp = STAMP_BAD;
iter->stamp = 0;
return FALSE;
}
iter->user_data = GINT_TO_POINTER(row);
Expand Down Expand Up @@ -189,25 +188,20 @@ static gboolean uiTableModel_iter_nth_child(GtkTreeModel *mm, GtkTreeIter *iter,
{
uiTableModel *m = uiTableModel(mm);

if (iter->stamp != STAMP_GOOD)
if (parent != NULL || n < 0 || n >= uiprivTableModelNumRows(m)) {
iter->stamp = 0;
return FALSE;
if (parent != NULL)
goto bad;
if (n < 0)
goto bad;
if (n >= uiprivTableModelNumRows(m))
goto bad;
iter->stamp = STAMP_GOOD;
}

iter->stamp = m->stamp;
iter->user_data = GINT_TO_POINTER(n);

return TRUE;
bad:
iter->stamp = STAMP_BAD;
return FALSE;
}

gboolean uiTableModel_iter_parent(GtkTreeModel *mm, GtkTreeIter *iter, GtkTreeIter *child)
{
iter->stamp = STAMP_BAD;
iter->stamp = 0;
return FALSE;
}

Expand Down Expand Up @@ -240,6 +234,9 @@ uiTableModel *uiNewTableModel(uiTableModelHandler *mh)
uiTableModel *m;

m = uiTableModel(g_object_new(uiTableModelType, NULL));
while ((m->stamp = g_random_int()) == 0) {
//iter of 0 means invalid
}
m->mh = mh;
return m;
}
Expand All @@ -255,7 +252,7 @@ void uiTableModelRowInserted(uiTableModel *m, int newIndex)
GtkTreeIter iter;

path = gtk_tree_path_new_from_indices(newIndex, -1);
iter.stamp = STAMP_GOOD;
iter.stamp = m->stamp;
iter.user_data = GINT_TO_POINTER(newIndex);
gtk_tree_model_row_inserted(GTK_TREE_MODEL(m), path, &iter);
gtk_tree_path_free(path);
Expand All @@ -267,7 +264,7 @@ void uiTableModelRowChanged(uiTableModel *m, int index)
GtkTreeIter iter;

path = gtk_tree_path_new_from_indices(index, -1);
iter.stamp = STAMP_GOOD;
iter.stamp = m->stamp;
iter.user_data = GINT_TO_POINTER(index);
gtk_tree_model_row_changed(GTK_TREE_MODEL(m), path, &iter);
gtk_tree_path_free(path);
Expand Down