WIP: simplifications dans builddir/demos/gtk-demo/tree_store.c

This commit is contained in:
Jean Sirmai 2023-12-13 11:53:11 +01:00
parent 3e52de8a09
commit 0c2f313a3b
Signed by: jean
GPG Key ID: FB3115C340E057E3
1 changed files with 56 additions and 238 deletions

View File

@ -31,24 +31,18 @@ typedef struct _TreeItem TreeItem;
struct _TreeItem
{
const char *label;
gboolean alex;
gboolean havoc;
gboolean tim;
gboolean owen;
gboolean dave;
gboolean world_holiday;
gboolean AA;
gboolean BB;
gboolean CC;
TreeItem *children;
};
/* columns */
enum
{
HOLIDAY_NAME_COLUMN = 0,
ALEX_COLUMN,
HAVOC_COLUMN,
TIM_COLUMN,
OWEN_COLUMN,
DAVE_COLUMN,
COLUMN_0,
COLUMN_1,
COLUMN_2,
VISIBLE_COLUMN,
WORLD_COLUMN,
@ -56,122 +50,36 @@ enum
};
/* tree data */
static TreeItem january[] =
static TreeItem item_name_0[] =
{
{"New Years Day", TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, NULL },
{"Presidential Inauguration", FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, NULL },
{"Martin Luther King Jr. day", FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, NULL },
{"A", TRUE, TRUE, TRUE, NULL },
{"B", FALSE, TRUE, FALSE, NULL },
{"C", FALSE, TRUE, FALSE, NULL },
{ NULL }
};
static TreeItem february[] =
static TreeItem item_name_1[] =
{
{ "Presidents' Day", FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, NULL },
{ "Groundhog Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Valentine's Day", FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, NULL },
{ "A", FALSE, TRUE, FALSE, NULL },
{ "B", FALSE, FALSE, FALSE, NULL },
{ "C", FALSE, FALSE, TRUE, NULL },
{ "D", FALSE, TRUE, TRUE, NULL },
{ NULL }
};
static TreeItem march[] =
static TreeItem item_name_2[] =
{
{ "National Tree Planting Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "St Patrick's Day", FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, NULL },
{ "A", FALSE, FALSE, FALSE, NULL },
{ "B", FALSE, FALSE, TRUE, NULL },
{ NULL }
};
static TreeItem april[] =
{
{ "April Fools' Day", FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, NULL },
{ "Army Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Earth Day", FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, NULL },
{ "Administrative Professionals' Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ NULL }
};
static TreeItem may[] =
{
{ "Nurses' Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "National Day of Prayer", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Mothers' Day", FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, NULL },
{ "Armed Forces Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Memorial Day", TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, NULL },
{ NULL }
};
static TreeItem june[] =
{
{ "June Fathers' Day", FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, NULL },
{ "Juneteenth (Liberation Day)", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Flag Day", FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, NULL },
{ NULL }
};
static TreeItem july[] =
{
{ "Parents' Day", FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, NULL },
{ "Independence Day", FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, NULL },
{ NULL }
};
static TreeItem august[] =
{
{ "Air Force Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Coast Guard Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Friendship Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ NULL }
};
static TreeItem september[] =
{
{ "Grandparents' Day", FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, NULL },
{ "Citizenship Day or Constitution Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Labor Day", TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, NULL },
{ NULL }
};
static TreeItem october[] =
{
{ "National Children's Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Bosses' Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Sweetest Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Mother-in-Law's Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Navy Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Columbus Day", FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, NULL },
{ "Halloween", FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, NULL },
{ NULL }
};
static TreeItem november[] =
{
{ "Marine Corps Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Veterans' Day", TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, NULL },
{ "Thanksgiving", FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, NULL },
{ NULL }
};
static TreeItem december[] =
{
{ "Pearl Harbor Remembrance Day", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ "Christmas", TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, NULL },
{ "Kwanzaa", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL },
{ NULL }
};
static TreeItem toplevel[] =
{
{"January", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, january},
{"February", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, february},
{"March", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, march},
{"April", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, april},
{"May", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, may},
{"June", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, june},
{"July", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, july},
{"August", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, august},
{"September", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, september},
{"October", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, october},
{"November", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, november},
{"December", FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, december},
{NULL}
{"IT-0", FALSE, FALSE, FALSE, item_name_0},
{"IT-1", FALSE, FALSE, FALSE, item_name_1},
{"IT-2", FALSE, FALSE, FALSE, item_name_2},
{ NULL }
};
@ -180,7 +88,7 @@ create_model (void)
{
GtkTreeStore *model;
GtkTreeIter iter;
TreeItem *month = toplevel;
TreeItem *item_0 = toplevel;
/* create tree store */
model = gtk_tree_store_new (NUM_COLUMNS,
@ -188,49 +96,40 @@ create_model (void)
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN);
/* add data to the tree store */
while (month->label)
while (item_0->label)
{
TreeItem *holiday = month->children;
TreeItem *item_1 = item_0->children;
gtk_tree_store_append (model, &iter, NULL);
gtk_tree_store_set (model, &iter,
HOLIDAY_NAME_COLUMN, month->label,
ALEX_COLUMN, FALSE,
HAVOC_COLUMN, FALSE,
TIM_COLUMN, FALSE,
OWEN_COLUMN, FALSE,
DAVE_COLUMN, FALSE,
COLUMN_0, item_0->label,
COLUMN_1, FALSE,
COLUMN_2, FALSE,
VISIBLE_COLUMN, FALSE,
WORLD_COLUMN, FALSE,
-1);
/* add children */
while (holiday->label)
while (item_1->label)
{
GtkTreeIter child_iter;
gtk_tree_store_append (model, &child_iter, &iter);
gtk_tree_store_set (model, &child_iter,
HOLIDAY_NAME_COLUMN, holiday->label,
ALEX_COLUMN, holiday->alex,
HAVOC_COLUMN, holiday->havoc,
TIM_COLUMN, holiday->tim,
OWEN_COLUMN, holiday->owen,
DAVE_COLUMN, holiday->dave,
COLUMN_0, item_1->label,
COLUMN_1, item_1->AA,
COLUMN_2, item_1->BB,
VISIBLE_COLUMN, TRUE,
WORLD_COLUMN, holiday->world_holiday,
WORLD_COLUMN, item_1->CC,
-1);
holiday++;
item_1++;
}
month++;
item_0++;
}
return GTK_TREE_MODEL (model);
@ -270,127 +169,46 @@ add_columns (GtkTreeView *treeview)
{
int col_offset;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
// GtkTreeViewColumn *column;
GtkTreeModel *model = gtk_tree_view_get_model (treeview);
/* column for holiday names */
/* column for item_1 names */
renderer = gtk_cell_renderer_text_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, "Holiday",
renderer, "text",
HOLIDAY_NAME_COLUMN,
NULL);
column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "Col 0", renderer, "text",
COLUMN_0, NULL);
/* column = */ gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
// gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* alex column */
/* AA column */
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set_data (G_OBJECT (renderer), "column", (int *)ALEX_COLUMN);
g_object_set_data (G_OBJECT (renderer), "column", (int *)COLUMN_1);
g_signal_connect (renderer, "toggled", G_CALLBACK (item_toggled), model);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, "Alex",
renderer,
"active",
ALEX_COLUMN,
"visible",
VISIBLE_COLUMN,
"activatable",
WORLD_COLUMN, NULL);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "Col 1", renderer, "active",
COLUMN_1, "visible", VISIBLE_COLUMN, "activatable", WORLD_COLUMN, NULL);
column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column),
GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* column = */ gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
// gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column), GTK_TREE_VIEW_COLUMN_FIXED);
// gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* havoc column */
/* BB column */
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set_data (G_OBJECT (renderer), "column", (int *)HAVOC_COLUMN);
g_object_set_data (G_OBJECT (renderer), "column", (int *)COLUMN_2);
g_signal_connect (renderer, "toggled", G_CALLBACK (item_toggled), model);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, "Havoc",
renderer,
"active",
HAVOC_COLUMN,
"visible",
VISIBLE_COLUMN,
NULL);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview), -1, "Col 2", renderer, "active",
COLUMN_2, "visible", VISIBLE_COLUMN, NULL);
column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column),
GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* column = */ gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
// gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column), GTK_TREE_VIEW_COLUMN_FIXED);
// gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* tim column */
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set_data (G_OBJECT (renderer), "column", (int *)TIM_COLUMN);
g_signal_connect (renderer, "toggled", G_CALLBACK (item_toggled), model);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, "Tim",
renderer,
"active",
TIM_COLUMN,
"visible",
VISIBLE_COLUMN,
"activatable",
WORLD_COLUMN, NULL);
column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column),
GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* owen column */
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set_data (G_OBJECT (renderer), "column", (int *)OWEN_COLUMN);
g_signal_connect (renderer, "toggled", G_CALLBACK (item_toggled), model);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, "Owen",
renderer,
"active",
OWEN_COLUMN,
"visible",
VISIBLE_COLUMN,
NULL);
column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column),
GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
/* dave column */
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set_data (G_OBJECT (renderer), "column", (int *)DAVE_COLUMN);
g_signal_connect (renderer, "toggled", G_CALLBACK (item_toggled), model);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, "Dave",
renderer,
"active",
DAVE_COLUMN,
"visible",
VISIBLE_COLUMN,
NULL);
column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column),
GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
}
GtkWidget *
@ -420,7 +238,7 @@ do_tree_store (GtkWidget *do_widget)
gtk_window_set_child (GTK_WINDOW (window), vbox);
gtk_box_append (GTK_BOX (vbox),
gtk_label_new ("Jonathan's Holiday Card Planning Sheet"));
gtk_label_new ("Learning GTK trees"));
sw = gtk_scrolled_window_new ();
gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);