Skip to content

Commit

Permalink
Ability to remove entries
Browse files Browse the repository at this point in the history
  • Loading branch information
iguessthislldo committed Apr 18, 2018
1 parent 57bd104 commit 9b3e576
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 19 deletions.
15 changes: 15 additions & 0 deletions Entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions Entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
17 changes: 8 additions & 9 deletions launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
66 changes: 56 additions & 10 deletions main_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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)");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 9b3e576

Please sign in to comment.