Compare commits

..

No commits in common. "6937d246999341f90562e8b9159c91966d0bd64d" and "c7ffcf68ed15d9cc3b4d3886287cb5ad938c0e4c" have entirely different histories.

3 changed files with 69 additions and 61 deletions

View File

@ -337,23 +337,26 @@ GtkWindow *create_main_window()
{ {
GtkBuilder *main_builder; GtkBuilder *main_builder;
GtkTreeView *cell_tree; GtkTreeView *cell_tree;
GtkTreeStore *cell_store;
GtkWidget *listbox; GtkWidget *listbox;
GtkWidget *conv_button; GtkWidget *conv_button;
GtkWidget *search_entry; GtkWidget *search_entry;
static GList *gds_libs; static GList *gds_libs;
static struct open_button_data open_data; static struct open_button_data open_data;
static struct convert_button_data conv_data; static struct convert_button_data conv_data;
struct tree_stores *cell_selector_stores;
main_builder = gtk_builder_new_from_resource("/main.glade"); main_builder = gtk_builder_new_from_resource("/main.glade");
gtk_builder_connect_signals(main_builder, NULL); gtk_builder_connect_signals(main_builder, NULL);
cell_tree = GTK_TREE_VIEW(gtk_builder_get_object(main_builder, "cell-tree")); cell_tree = GTK_TREE_VIEW(gtk_builder_get_object(main_builder, "cell-tree"));
search_entry = GTK_WIDGET(gtk_builder_get_object(main_builder, "cell-search")); search_entry = GTK_WIDGET(gtk_builder_get_object(main_builder, "cell-search"));
open_data.search_entry = GTK_SEARCH_ENTRY(search_entry); open_data.search_entry = GTK_SEARCH_ENTRY(search_entry);
cell_selector_stores = setup_cell_selector(cell_tree, GTK_ENTRY(search_entry)); cell_store = setup_cell_selector(cell_tree, GTK_ENTRY(search_entry));
open_data.cell_store = cell_selector_stores->base_store;
open_data.cell_store = cell_store;
open_data.list_ptr = &gds_libs; open_data.list_ptr = &gds_libs;
open_data.main_window = GTK_WINDOW(gtk_builder_get_object(main_builder, "main-window")); open_data.main_window = GTK_WINDOW(gtk_builder_get_object(main_builder, "main-window"));
g_signal_connect(GTK_WIDGET(gtk_builder_get_object(main_builder, "button-load-gds")), g_signal_connect(GTK_WIDGET(gtk_builder_get_object(main_builder, "button-load-gds")),

View File

@ -61,84 +61,88 @@ static gboolean tree_sel_func(GtkTreeSelection *selection,
} }
/** /**
* @brief cell_store_filter_visible_func Decides whether an element of the tree model @p model is visible. * @brief tree_sel_search_func
* @param model Tree model * @param model Tree model
* @param iter Current element / iter in Model to check * @param column Column id
* @param data Data. Set to static stores variable * @param key Search key
* @return TRUE if visible, else FALSE * @param iter Iterator
* @note TODO: Maybe implement Damerau-Levenshtein distance matching * @param search_data User data. In this case always NULL
* @return returns false (!) if matches
*/ */
static gboolean cell_store_filter_visible_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer data) static gboolean tree_sel_search_func(GtkTreeModel *model, gint column, const gchar *key, GtkTreeIter *iter, gpointer *search_data)
{ {
struct tree_stores *stores = (struct tree_stores *)data; (void)search_data;
(void)column;
struct gds_cell *cell; struct gds_cell *cell;
struct gds_library *lib; struct gds_library *lib;
gboolean result = FALSE; GtkTreePath *path;
const char *search_string; GtkTreeIter lib_iter;
char *lib_str = NULL, *cell_str = NULL, *div_str = NULL, *key_copy;
gboolean result = TRUE;
if (!model || !iter || !stores) gtk_tree_model_get(model, iter, CELL_SEL_CELL, &cell, -1);
goto exit_filter; path = gtk_tree_model_get_path(model, iter);
gtk_tree_model_get(model, iter, CELL_SEL_CELL, &cell, CELL_SEL_LIBRARY, &lib, -1);
if (lib) {
result = TRUE;
goto exit_filter;
}
/* Libraries not selectable */
if (!cell) if (!cell)
goto exit_filter; return TRUE;
search_string = gtk_entry_get_text(stores->search_entry); if (!gtk_tree_path_up(path)) {
gtk_tree_path_free(path);
/* Show all, if field is empty */ printf("Cell without parent library found during search! Somethings really wrong!!!\n");
if (!strlen(search_string)) return TRUE;
result = TRUE;
if (strstr(cell->name, search_string))
result = TRUE;
gtk_tree_view_expand_all(stores->base_tree_view);
exit_filter:
return result;
} }
static void change_filter(GtkWidget *entry, gpointer data) /* Find name of parent library */
{ gtk_tree_model_get_iter(model, &lib_iter, path);
struct tree_stores *stores = (struct tree_stores *)data; gtk_tree_model_get(model, &lib_iter, CELL_SEL_LIBRARY, &lib, -1);
gtk_tree_model_filter_refilter(stores->filter); 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 * @brief Setup a GtkTreeView with the necessary columns
* @param view Tree view to set up * @param view Tree view to set up
* @param search_entry Entry field for search * @param search_entry Entry field for search
* @return Tree stores for storing data inside the GtkTreeView * @return TreeStore for storing data inside the GtkTreeView
*/ */
struct tree_stores *setup_cell_selector(GtkTreeView* view, GtkEntry *search_entry) GtkTreeStore *setup_cell_selector(GtkTreeView* view, GtkEntry *search_entry)
{ {
static struct tree_stores stores; GtkTreeStore *cell_store;
GtkCellRenderer *render_dates; GtkCellRenderer *render_dates;
GtkCellRenderer *render_cell; GtkCellRenderer *render_cell;
GtkCellRenderer *render_lib; GtkCellRenderer *render_lib;
GtkTreeViewColumn *column; GtkTreeViewColumn *column;
stores.base_tree_view = view; 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);
stores.search_entry = search_entry; gtk_tree_view_set_model(view, GTK_TREE_MODEL(cell_store));
stores.base_store = gtk_tree_store_new(CELL_SEL_COLUMN_COUNT, G_TYPE_POINTER, G_TYPE_POINTER, GDK_TYPE_RGBA, G_TYPE_STRING, G_TYPE_STRING);
/* Searching */
if (search_entry) {
stores.filter = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(GTK_TREE_MODEL(stores.base_store), NULL));
gtk_tree_model_filter_set_visible_func (stores.filter,
(GtkTreeModelFilterVisibleFunc)cell_store_filter_visible_func,
&stores, NULL);
g_signal_connect(GTK_SEARCH_ENTRY(search_entry), "search-changed", G_CALLBACK(change_filter), &stores);
}
gtk_tree_view_set_model(view, GTK_TREE_MODEL(stores.filter));
render_dates = gtk_cell_renderer_text_new(); render_dates = gtk_cell_renderer_text_new();
render_cell = lib_cell_renderer_new(); render_cell = lib_cell_renderer_new();
@ -161,6 +165,14 @@ struct tree_stores *setup_cell_selector(GtkTreeView* view, GtkEntry *search_entr
* This prevents selecting a library */ * This prevents selecting a library */
gtk_tree_selection_set_select_function(gtk_tree_view_get_selection(view), tree_sel_func, NULL, NULL); gtk_tree_selection_set_select_function(gtk_tree_view_get_selection(view), tree_sel_func, NULL, NULL);
return &stores; /* 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

@ -43,14 +43,7 @@ enum cell_store_columns {
CELL_SEL_COLUMN_COUNT /**< Not a column. Used to determine count of coumns **/ CELL_SEL_COLUMN_COUNT /**< Not a column. Used to determine count of coumns **/
}; };
struct tree_stores { GtkTreeStore *setup_cell_selector(GtkTreeView* view, GtkEntry *search_entry);
GtkTreeView *base_tree_view;
GtkTreeStore *base_store;
GtkTreeModelFilter *filter;
GtkEntry *search_entry;
};
struct tree_stores *setup_cell_selector(GtkTreeView* view, GtkEntry *search_entry);
#endif /* __TREE_STORE_H__ */ #endif /* __TREE_STORE_H__ */