Added search and processing for the recent workspace selector

This commit is contained in:
Markus Koch 2017-02-05 16:28:26 +01:00
parent f8d3bb8730
commit 54a7814e0b
2 changed files with 106 additions and 28 deletions

View File

@ -1,6 +1,45 @@
#include "mainwindow.h"
#include "mdiary.h"
enum {
WSCOL_LABEL = 0,
WSCOL_PATH,
WSCOL_COUNT
};
static gboolean mainWindow_workspace_entry_visible(GtkTreeModel *model,
GtkTreeIter *iter,
struct mainWindow *mainWindow)
{
GRegex *regex;
GMatchInfo *match_info;
gchar *searchString;
gchar *temp;
gboolean ret = FALSE;
searchString = gtk_entry_get_text(GTK_ENTRY(mainWindow->workspaceSearch));
if (g_strcmp0(searchString, "")) {
regex = g_regex_new(searchString, G_REGEX_CASELESS, 0, NULL);
gtk_tree_model_get(model, iter, WSCOL_PATH, &temp, -1);
if (g_regex_match(regex, temp, 0, &match_info) && g_match_info_matches(match_info))
ret = TRUE;
g_free(temp);
} else {
ret = TRUE;
}
return ret;
}
void mainWindow_workspace_search_changed(GtkWidget *widget, gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
gtk_tree_model_filter_refilter(mainWindow->workspaceListFiltered);
}
/**
* @brief mainWindow_list_entry_visible checks whether the entry should be displayed.
* @param model N/A
@ -8,7 +47,9 @@
* @param mainWindow struct mainWindow *
* @return True when it should displayed, else false.
*/
static gboolean mainWindow_list_entry_visible(GtkTreeModel *model, GtkTreeIter *iter, struct mainWindow *mainWindow)
static gboolean mainWindow_list_entry_visible(GtkTreeModel *model,
GtkTreeIter *iter,
struct mainWindow *mainWindow)
{
GRegex *regex;
GMatchInfo *match_info;
@ -102,6 +143,33 @@ static gint mainWindow_sort_date_compare_func(GtkTreeModel *model,
return -1;
}
static void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path)
{
GtkTreeIter iter;
GRegex *regex;
GMatchInfo *match_info;
gchar *temp;
regex = g_regex_new("(.*)\\/(.*)", G_REGEX_RAW, 0, NULL);
if (g_regex_match(regex, path, 0, &match_info) &&
g_regex_get_capture_count(regex) > 1 &&
g_match_info_matches(match_info)) {
g_match_info_fetch(match_info, 1);
temp = g_strdup_printf("<b>%s</b>\n<span weight=\"ultralight\">%s</span>",
g_match_info_fetch(match_info, 2),
g_match_info_fetch(match_info, 1));
gtk_list_store_append(mainWindow->workspaceListStore, &iter);
gtk_list_store_set(mainWindow->workspaceListStore, &iter,
WSCOL_LABEL, temp,
WSCOL_PATH, path,
-1);
g_free(temp);
}
g_regex_unref(regex);
}
/**
* @brief mainWindow_wschooser_configure_treeView configures the GtkTreeView for workspace selection
* @param mainWindow
@ -111,34 +179,28 @@ static void mainWindow_configure_workspaceTreeView(struct mainWindow *mainWindow
GtkCellRenderer *renderer;
GtkTreeViewColumn *col;
mainWindow->workspaceListStore = gtk_list_store_new(1, G_TYPE_STRING);
mainWindow->workspaceListStore = gtk_list_store_new(WSCOL_COUNT,
G_TYPE_STRING,
G_TYPE_STRING);
/*
* TODO:
* gtk_tree_model_filter_set_visible_func(mainWindow->workspaceListStore,
* (GtkTreeModelFilterVisibleFunc) mainWindow_list_entry_visible,
* mainWindow, NULL);
*/
mainWindow->workspaceListFiltered = GTK_TREE_MODEL_FILTER(
gtk_tree_model_filter_new(
GTK_TREE_MODEL(mainWindow->workspaceListStore),
NULL));
gtk_tree_view_set_model(mainWindow->workspaceListView, GTK_TREE_MODEL(mainWindow->workspaceListStore));
gtk_tree_model_filter_set_visible_func(mainWindow->workspaceListFiltered,
(GtkTreeModelFilterVisibleFunc) mainWindow_workspace_entry_visible,
mainWindow, NULL);
gtk_tree_view_set_model(mainWindow->workspaceListView, GTK_TREE_MODEL(mainWindow->workspaceListFiltered));
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(mainWindow->workspaceListView, -1, "", renderer,
"markup", 0, NULL);
/* Test entry for debugging */
GtkTreeIter iter;
gtk_list_store_append(mainWindow->workspaceListStore, &iter);
gtk_list_store_set(mainWindow->workspaceListStore, &iter,
0, "<b>testworkspace</b>\n\n<span weight=\"ultralight\">/home/user/Documents</span>",
-1);
gtk_list_store_append(mainWindow->workspaceListStore, &iter);
gtk_list_store_set(mainWindow->workspaceListStore, &iter,
0, "<b>testworkspace</b>\n\n<span weight=\"ultralight\">/home/user/Documents</span>",
-1);
"markup", WSCOL_LABEL, NULL);
/* Test only */
mainWindow_add_recent_workspace(mainWindow, "/home/user/Document/diary");
mainWindow_add_recent_workspace(mainWindow, "/home/user/Document/workfoo");
}
/**
@ -293,6 +355,10 @@ static void mainWindow_connect_signals(struct mainWindow *mainWindow)
"row-activated",
(GCallback) mainWindow_workspaceListClicked,
mainWindow);
g_signal_connect(mainWindow->workspaceSearch,
"changed",
(GCallback) mainWindow_workspace_search_changed,
mainWindow);
}
/**
@ -337,7 +403,7 @@ struct mainWindow *mainWindow_new()
mainWindow->textBuffer = GTK_TEXT_BUFFER(gtk_builder_get_object(builder, "entryTextBuffer"));
mainWindow->buttonOtherWorkspace = GTK_WIDGET(gtk_builder_get_object(builder, "buttonOtherWorkspace"));
mainWindow->searchWorkspace = GTK_WIDGET(gtk_builder_get_object(builder, "searchWorkspace"));
mainWindow->workspaceSearch = GTK_WIDGET(gtk_builder_get_object(builder, "searchWorkspace"));
mainWindow->popoverWorkspace = GTK_WIDGET(gtk_builder_get_object(builder, "popoverWorkspace"));
mainWindow->workspaceListView = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeWorkspace"));
mainWindow->workspaceListSelection = GTK_TREE_SELECTION(gtk_builder_get_object(builder,
@ -585,12 +651,22 @@ void mainWindow_dateIconPress(GtkWidget *widget, gint icon_pos, gint event, gpoi
}
void mainWindow_workspaceListClicked(GtkWidget *widget,
GtkTreePath *path,
GtkTreePath *tree_path,
GtkTreeViewColumn *column,
gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
GtkTreeModel *model;
GtkTreeIter iter;
GtkTreeSelection *selection;
gchar *path;
gtk_popover_popdown(GTK_POPOVER(mainWindow->popoverWorkspace));
g_print("TODO: Workspace list clicked.\n");
selection = gtk_tree_view_get_selection(mainWindow->workspaceListView);
if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
gtk_tree_model_get(model, &iter, WSCOL_PATH, &path, -1);
g_print("TODO: Switch workspace to %s\n", path);
g_free(path);
}
}

View File

@ -41,11 +41,12 @@ struct mainWindow {
} filterSettings;
GtkWidget *buttonOtherWorkspace;
GtkWidget *searchWorkspace;
GtkWidget *workspaceSearch;
GtkWidget *popoverWorkspace;
GtkTreeView *workspaceListView;
GtkTreeSelection *workspaceListSelection;
GtkListStore *workspaceListStore;
GtkTreeModelFilter *workspaceListFiltered;
};
struct mainWindow *mainWindow_new();
@ -55,13 +56,14 @@ void mainWindow_set_meta_information(struct mainWindow *mainWindow, guint time_e
void mainWindow_showPopover(GtkWidget *widget, GdkEvent *event, gpointer user_data);
void mainWindow_otherWorkspaceClicked(GtkWidget *widget, gint event, gpointer user_data);
void mainWindow_workspaceListClicked(GtkWidget *widget,
GtkTreePath *path,
GtkTreePath *tree_path,
GtkTreeViewColumn *column,
gpointer user_data);
void mainWindow_clearSearch(GtkWidget *widget, gpointer user_data);
void mainWindow_calendarSelected(GtkWidget *widget, gpointer user_data);
void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data);
void mainWindow_filterChanged(GtkWidget *widget, gpointer user_data);
void mainWindow_workspace_search_changed(GtkWidget *widget, gpointer user_data);
void mainWindow_checkDate(GtkWidget *widget, gint event, gpointer user_data);
void mainWindow_dateIconPress(GtkWidget *widget, gint icon_pos, gint event, gpointer user_data);