From 9b3e57626dadd391f90a1ec1e489aa2b8ca599e7 Mon Sep 17 00:00:00 2001 From: Frederick Hornsey Date: Tue, 17 Apr 2018 20:00:46 -0500 Subject: [PATCH] Ability to remove entries --- Entry.c | 15 ++++++++++++ Entry.h | 5 ++++ launcher.c | 17 +++++++------ main_window.c | 66 +++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 84 insertions(+), 19 deletions(-) diff --git a/Entry.c b/Entry.c index 06d62d3..9d7b978 100644 --- a/Entry.c +++ b/Entry.c @@ -113,6 +113,21 @@ void Entries_append(Entries * entries, Entry * entry) { entries->size++; } +void Entries_remove(Entries * entries, Entry * entry) { + Node * prev = NULL; + for (Node * node = entries->head; node; node = node->next) { + if (node->entry == entry) { + if (prev) prev->next = node->next; + if (node == entries->head) entries->head = node->next; + if (node == entries->tail) entries->tail= prev; + entries->size--; + free(node); + return; + } + prev = node; + } +} + void Entries_delete(Entries * entries) { Node * next = entries->head; for (Node * node = next; node; node = next) { diff --git a/Entry.h b/Entry.h index 8b7c979..ce0d1c6 100644 --- a/Entry.h +++ b/Entry.h @@ -101,6 +101,11 @@ Entries * Entries_new(); */ void Entries_append(Entries * entries, Entry * entry); +/* + * Find and Remove entry for the Entries list + */ +void Entries_remove(Entries * entries, Entry * entry); + /* * Delete all the nodes and the Entry List, but NOT the Entries */ diff --git a/launcher.c b/launcher.c index 2581a6c..05081f2 100644 --- a/launcher.c +++ b/launcher.c @@ -162,15 +162,6 @@ void init_data() { Entries_sort(all_entries); } -void save_entries_if_changed() { - if (entries_changed) { - if (debug) printf("Entries Changed, Saving Entries\n"); - Entries_save(entries_file); - } else if (debug) { - printf("Entries not changed, not saving\n"); - } -} - int main(int argc, char * argv[]) { entries_changed = false; @@ -232,6 +223,14 @@ int main(int argc, char * argv[]) { int status = g_application_run(G_APPLICATION(app), argc, argv); g_object_unref(app); + // Save entries if changed + if (entries_changed) { + if (debug) printf("Entries Changed, Saving Entries\n"); + Entries_save(entries_file); + } else if (debug) { + if (debug) printf("Entries not changed, not saving\n"); + } + // Clean Up Data if (visable_entries != all_entries) Entries_delete(visable_entries); Entries_delete_all(all_entries); diff --git a/main_window.c b/main_window.c index f5b62d7..1ebf5b5 100644 --- a/main_window.c +++ b/main_window.c @@ -10,10 +10,17 @@ GtkWidget * layout; GtkWidget * filter; GtkWidget * scroll; GtkWidget * grid; -GtkWidget * menu; char * filter_string; +void entry_menu(Entry * entry); + +void hide_all_infobox() { + for (Node * n = visable_entries->head; n; n = n->next) { + gtk_widget_hide(n->entry->info_box); + } +} + void entry_click(GtkWidget * widget, GdkEventButton * event, gpointer data) { Entry * entry = (Entry *) data; if (debug) printf( @@ -24,11 +31,12 @@ void entry_click(GtkWidget * widget, GdkEventButton * event, gpointer data) { Entry_run(entry); break; case 3: // Right Mouse Button - gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL); + entry_menu(entry); break; default: break; } + hide_all_infobox(); } void add_entries_to_grid(Entries * entries) { @@ -85,6 +93,41 @@ bool entry_unhover(GtkWidget * widget, GdkEvent * event, gpointer data) { return false; } +bool entry_remove(gpointer data) { + Entry * entry = (Entry *) data; + GtkWidget * modal = gtk_dialog_new_with_buttons( + "Remove Game?", + GTK_WINDOW(window), + GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, + "Remove", GTK_RESPONSE_OK, + "Cancel", GTK_RESPONSE_CANCEL, + NULL + ); + + // Message + GtkWidget * content = gtk_dialog_get_content_area(GTK_DIALOG(modal)); + gtk_container_add(GTK_CONTAINER(content), gtk_label_new(g_strjoin( + "", "Are you sure you want to remove \"", entry->name, "\"?", NULL + ))); + + // Show Dialog + gtk_widget_show_all(modal); + GtkResponseType r = gtk_dialog_run(GTK_DIALOG(modal)); + + // Handle Results + if (r == GTK_RESPONSE_OK) { + if (debug) printf("Remove Entry %s: %s\n", entry->id, entry->name); + Entries_clear_container(GTK_CONTAINER(grid), visable_entries); + Entries_remove(all_entries, entry); + if (all_entries != visable_entries) Entries_remove(visable_entries, entry); + Entry_delete(entry); + entries_changed = true; + update_visable_entries(); + } + gtk_widget_destroy(modal); +} + + void init_entries_gui(Entries * entries) { for (Node * node = entries->head; node; node = node->next) { @@ -162,13 +205,13 @@ void init_entries_gui(Entries * entries) { } else { // Create Error Entry GUI char * error_message = g_strdup_printf( - "Could load image %s: \"%s\"\n", + "Could load image %s: \"%s\"", full_image_path, error->message ); g_error_free(error); - if (debug) fprintf(stderr, error_message); + if (debug) fprintf(stderr, "%s\n", error_message); GtkWidget * error_message_box = gtk_box_new( GTK_ORIENTATION_VERTICAL, 0); @@ -322,8 +365,8 @@ void set_sort_by(void * value) { update_visable_entries(); } -void init_menu() { - menu = gtk_menu_new(); +void entry_menu(Entry * entry) { + GtkWidget * menu = gtk_menu_new(); unsigned a = 0; unsigned b = 1; @@ -332,6 +375,10 @@ void init_menu() { gtk_menu_attach(GTK_MENU(menu), edit_item, 0, 1, a++, b++); GtkWidget * remove_item = gtk_menu_item_new_with_label("Remove Game"); + g_signal_connect_swapped( + G_OBJECT(remove_item), "activate", + G_CALLBACK(entry_remove), entry + ); gtk_menu_attach(GTK_MENU(menu), remove_item, 0, 1, a++, b++); GtkWidget * add_item = gtk_menu_item_new_with_label("Add Game(s)"); @@ -362,8 +409,10 @@ void init_menu() { gtk_menu_attach(GTK_MENU(menu), quit_item, 0, 1, a++, b++); g_signal_connect_swapped(G_OBJECT(quit_item), "activate", G_CALLBACK(quit), NULL); - gtk_menu_attach_to_widget(GTK_MENU(menu), window, NULL); + gtk_menu_attach_to_widget(GTK_MENU(menu), entry->fixed_widget, NULL); gtk_widget_show_all(menu); + + gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL); } void init_main_window(GtkApplication * app, gpointer user_data) { @@ -423,9 +472,6 @@ void init_main_window(GtkApplication * app, gpointer user_data) { visable_entries = all_entries; update_visable_entries(); - // Entry Context Menu - init_menu(); - // Show Window with Entries gtk_widget_show_all(grid);