Implement first draft of cell search

This commit is contained in:
2019-02-04 20:02:10 +01:00
parent 9f2544ee94
commit 8306c34292
4 changed files with 134 additions and 33 deletions

View File

@@ -60,12 +60,79 @@ static gboolean tree_sel_func(GtkTreeSelection *selection,
return FALSE;
}
/**
* @brief tree_sel_search_func
* @param model Tree model
* @param column Column id
* @param key Search key
* @param iter Iterator
* @param search_data User data. In this case always NULL
* @return returns false (!) if matches
*/
static gboolean tree_sel_search_func(GtkTreeModel *model, gint column, const gchar *key, GtkTreeIter *iter, gpointer *search_data)
{
(void)search_data;
(void)column;
struct gds_cell *cell;
struct gds_library *lib;
GtkTreePath *path;
GtkTreeIter lib_iter;
char *lib_str = NULL, *cell_str = NULL, *div_str = NULL, *key_copy;
gboolean result = TRUE;
gtk_tree_model_get(model, iter, CELL_SEL_CELL, &cell, -1);
path = gtk_tree_model_get_path(model, iter);
/* Libraries not selectable */
if (!cell)
return TRUE;
if (!gtk_tree_path_up(path)) {
gtk_tree_path_free(path);
printf("Cell without parent library found during search! Somethings really wrong!!!\n");
return TRUE;
}
/* Find name of parent library */
gtk_tree_model_get_iter(model, &lib_iter, path);
gtk_tree_model_get(model, &lib_iter, CELL_SEL_LIBRARY, &lib, -1);
gtk_tree_path_free(path);
if (!lib) {
printf("Parent library invalid!\n");
return TRUE;
}
/* Search for Library/Cell division operator */
key_copy = strdup(key);
if (!key_copy)
goto abort_search;
if ((div_str = strstr(key_copy, ":"))) {
lib_str = key_copy;
*div_str = 0x00;
cell_str = div_str + 1;
if (!strncmp(lib_str, lib->name, sizeof(lib->name))) {
if (!strncmp(cell_str, cell->name, sizeof(cell->name)))
result = FALSE;
}
} else {
result = (strncmp(key, cell->name, sizeof(cell->name)) ? TRUE : FALSE);
}
abort_search:
return result;
}
/**
* @brief Setup a GtkTreeView with the necessary columns
* @param view Tree view to set up
* @param search_entry Entry field for search
* @return TreeStore for storing data inside the GtkTreeView
*/
GtkTreeStore *setup_cell_selector(GtkTreeView* view)
GtkTreeStore *setup_cell_selector(GtkTreeView* view, GtkEntry *search_entry)
{
GtkTreeStore *cell_store;
@@ -73,40 +140,19 @@ GtkTreeStore *setup_cell_selector(GtkTreeView* view)
GtkCellRenderer *render_cell;
GtkCellRenderer *render_lib;
GtkTreeViewColumn *column;
GdkRGBA cell_text_color;
GValue val = G_VALUE_INIT;
cell_store = gtk_tree_store_new(CELL_SEL_COLUMN_COUNT, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING);
cell_store = gtk_tree_store_new(CELL_SEL_COLUMN_COUNT, G_TYPE_POINTER, G_TYPE_POINTER, GDK_TYPE_RGBA, G_TYPE_STRING, G_TYPE_STRING);
gtk_tree_view_set_model(view, GTK_TREE_MODEL(cell_store));
render_dates = gtk_cell_renderer_text_new();
render_cell = lib_cell_renderer_new();
render_lib = lib_cell_renderer_new();
/* Set foreground color for cell column */
cell_text_color.alpha = 1;
cell_text_color.red = (double)61.0/(double)255.0;
cell_text_color.green = (double)152.0/(double)255.0;
cell_text_color.blue = 0.0;
g_value_init(&val, G_TYPE_BOOLEAN);
g_value_set_boolean(&val, TRUE);
g_object_set_property(G_OBJECT(render_cell), "foreground-set", &val);
g_value_unset(&val);
g_value_init(&val, GDK_TYPE_RGBA);
g_value_set_boxed(&val, &cell_text_color);
g_object_set_property(G_OBJECT(render_cell), "foreground-rgba", &val);
g_value_unset(&val);
column = gtk_tree_view_column_new_with_attributes("Library", render_lib, "gds-lib", CELL_SEL_LIBRARY, NULL);
gtk_tree_view_append_column(view, column);
/* Cell color: #3D9801 */
column = gtk_tree_view_column_new_with_attributes("Cell", render_cell, "gds-cell", CELL_SEL_CELL, NULL);
column = gtk_tree_view_column_new_with_attributes("Cell", render_cell, "gds-cell", CELL_SEL_CELL, "foreground-rgba", CELL_SEL_CELL_COLOR, NULL);
gtk_tree_view_append_column(view, column);
column = gtk_tree_view_column_new_with_attributes("Mod. Date", render_dates, "text", CELL_SEL_MODDATE, NULL);
@@ -119,6 +165,14 @@ GtkTreeStore *setup_cell_selector(GtkTreeView* view)
* This prevents selecting a library */
gtk_tree_selection_set_select_function(gtk_tree_view_get_selection(view), tree_sel_func, NULL, NULL);
/* Searching */
if (search_entry) {
gtk_tree_view_set_enable_search(view, TRUE);
gtk_tree_view_set_search_column(view, CELL_SEL_CELL);
gtk_tree_view_set_search_equal_func(view, (GtkTreeViewSearchEqualFunc)tree_sel_search_func, NULL, NULL);
gtk_tree_view_set_search_entry(view, search_entry);
}
return cell_store;
}
/** @} */

View File

@@ -37,12 +37,13 @@
enum cell_store_columns {
CELL_SEL_LIBRARY = 0,
CELL_SEL_CELL,
CELL_SEL_CELL_COLOR, /**< Cell column color */
CELL_SEL_MODDATE,
CELL_SEL_ACCESSDATE,
CELL_SEL_COLUMN_COUNT /**< Not a column. Used to determine count of coumns **/
};
GtkTreeStore *setup_cell_selector(GtkTreeView* view);
GtkTreeStore *setup_cell_selector(GtkTreeView* view, GtkEntry *search_entry);
#endif /* __TREE_STORE_H__ */