From 1cbe2444cc6c0c8da9e730561914986506d83770 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 2 Apr 2012 19:19:01 -0700 Subject: [PATCH] Add the ugliest 'delete dive' model ever This interface works the same way the "edit dive" menu item does: it's a text entry meny item on the dive text entries (ie buddy/divemaster/notes sections). Except you pick the "Delete" entry rather than the "Edit" entry. It kind of works, but it really is a pretty horrible interface. I'll need to add a top-level dive menu entry for just deleting all selected dives instead. And it would be good to be able to get a drop-down menu from the divelist instead of having to do it from the dive text entries, which is just insane. But that requires gtk work. I'm not quite ready to get back into that. Thus the "exact same insane interface as the explicit 'Edit' mode". Signed-off-by: Linus Torvalds --- dive.h | 1 + info.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- parse-xml.c | 23 +++++++++++++++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/dive.h b/dive.h index 39f2037..89f7901 100644 --- a/dive.h +++ b/dive.h @@ -299,6 +299,7 @@ extern time_t utc_mktime(struct tm *tm); extern struct dive *alloc_dive(void); extern void record_dive(struct dive *dive); +extern void delete_dive(struct dive *dive); extern struct sample *prepare_sample(struct dive **divep); extern void finish_sample(struct dive *dive); diff --git a/info.c b/info.c index 6a4a296..f1afc05 100644 --- a/info.c +++ b/info.c @@ -100,19 +100,58 @@ void show_dive_info(struct dive *dive) dive && dive->notes ? dive->notes : "", -1); } +static int delete_dive_info(struct dive *dive) +{ + int success; + GtkWidget *dialog; + + if (!dive) + return 0; + + dialog = gtk_dialog_new_with_buttons("Delete Dive", + GTK_WINDOW(main_window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, + GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, + NULL); + + gtk_widget_show_all(dialog); + success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT; + if (success) { + delete_dive(dive); + mark_divelist_changed(TRUE); + dive_list_update_dives(); + } + + gtk_widget_destroy(dialog); + + return success; +} + static void info_menu_edit_cb(GtkMenuItem *menuitem, gpointer user_data) { edit_dive_info(current_dive); } -static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data) +static void info_menu_delete_cb(GtkMenuItem *menuitem, gpointer user_data) { - GtkWidget *item = gtk_menu_item_new_with_label("Edit"); - g_signal_connect(item, "activate", G_CALLBACK(info_menu_edit_cb), NULL); + delete_dive_info(current_dive); +} + +static void add_menu_item(GtkMenu *menu, const char *label, void (*cb)(GtkMenuItem *, gpointer)) +{ + GtkWidget *item = gtk_menu_item_new_with_label(label); + g_signal_connect(item, "activate", G_CALLBACK(cb), NULL); gtk_widget_show(item); /* Yes, really */ gtk_menu_prepend(menu, item); } +static void populate_popup_cb(GtkTextView *entry, GtkMenu *menu, gpointer user_data) +{ + add_menu_item(menu, "Delete", info_menu_delete_cb); + add_menu_item(menu, "Edit", info_menu_edit_cb); +} + static GtkEntry *text_value(GtkWidget *box, const char *label) { GtkWidget *widget; diff --git a/parse-xml.c b/parse-xml.c index e920a11..552786b 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -39,6 +39,29 @@ void record_dive(struct dive *dive) dive_table.nr = nr+1; } +/* + * Remove a dive from the dive_table array + */ +void delete_dive(struct dive *dive) +{ + int nr = dive_table.nr, i; + struct dive **dives = dive_table.dives; + + /* + * Stupid. We know the dive table is sorted by date, + * we could do a binary lookup. Sue me. + */ + for (i = 0; i < nr; i++) { + struct dive *d = dives[i]; + if (d != dive) + continue; + memmove(dives+i, dives+i+1, sizeof(struct dive *)*(nr-i-1)); + dives[nr] = NULL; + dive_table.nr = nr-1; + break; + } +} + static void start_match(const char *type, const char *name, char *buffer) { if (verbose > 2) -- 2.43.0