Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 1 | #include <locale.h> |
| 2 | #include <malloc.h> |
| 3 | #include <string.h> |
| 4 | #include <stdarg.h> |
| 5 | |
| 6 | #include <gtk/gtk.h> |
| 7 | |
| 8 | #include "gfio.h" |
| 9 | #include "gerror.h" |
| 10 | |
| 11 | static void on_info_bar_response(GtkWidget *widget, gint response, |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 12 | gpointer data) |
Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 13 | { |
| 14 | struct gui *ui = (struct gui *) data; |
| 15 | |
| 16 | if (response == GTK_RESPONSE_OK) { |
| 17 | gtk_widget_destroy(widget); |
| 18 | ui->error_info_bar = NULL; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | static void report_error(struct gui_entry *ge, GError *error) |
| 23 | { |
| 24 | struct gui *ui = ge->ui; |
| 25 | |
| 26 | if (ui->error_info_bar == NULL) { |
Jens Axboe | cab2440 | 2013-04-18 05:22:37 -0700 | [diff] [blame] | 27 | GtkWidget *container; |
| 28 | |
Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 29 | ui->error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK, |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 30 | GTK_RESPONSE_OK, NULL); |
Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 31 | g_signal_connect(ui->error_info_bar, "response", G_CALLBACK(on_info_bar_response), ui); |
| 32 | gtk_info_bar_set_message_type(GTK_INFO_BAR(ui->error_info_bar), |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 33 | GTK_MESSAGE_ERROR); |
| 34 | |
Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 35 | ui->error_label = gtk_label_new(error->message); |
Jens Axboe | cab2440 | 2013-04-18 05:22:37 -0700 | [diff] [blame] | 36 | container = gtk_info_bar_get_content_area(GTK_INFO_BAR(ui->error_info_bar)); |
Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 37 | gtk_container_add(GTK_CONTAINER(container), ui->error_label); |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 38 | |
Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 39 | gtk_box_pack_start(GTK_BOX(ui->vbox), ui->error_info_bar, FALSE, FALSE, 0); |
| 40 | gtk_widget_show_all(ui->vbox); |
| 41 | } else { |
| 42 | char buffer[256]; |
| 43 | snprintf(buffer, sizeof(buffer), "Failed to open file."); |
Jens Axboe | f762cef | 2012-03-26 13:51:11 +0200 | [diff] [blame] | 44 | gtk_label_set_text(GTK_LABEL(ui->error_label), buffer); |
Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | void gfio_report_error(struct gui_entry *ge, const char *format, ...) |
| 49 | { |
| 50 | va_list args; |
| 51 | GError *error; |
| 52 | |
| 53 | va_start(args, format); |
| 54 | error = g_error_new_valist(g_quark_from_string("fio"), 1, format, args); |
| 55 | va_end(args); |
| 56 | |
| 57 | report_error(ge, error); |
| 58 | g_error_free(error); |
| 59 | } |
| 60 | |
Jens Axboe | 1252d8f | 2012-03-21 11:13:31 +0100 | [diff] [blame] | 61 | void gfio_report_info(struct gui *ui, const char *title, const char *message) |
| 62 | { |
| 63 | GtkWidget *dialog, *content, *label; |
Jens Axboe | 4166658 | 2012-03-21 10:25:29 +0100 | [diff] [blame] | 64 | |
Jens Axboe | 1252d8f | 2012-03-21 11:13:31 +0100 | [diff] [blame] | 65 | dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(ui->window), |
| 66 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
| 67 | GTK_STOCK_OK, GTK_RESPONSE_OK, NULL); |
| 68 | |
| 69 | content = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); |
| 70 | label = gtk_label_new(message); |
| 71 | gtk_container_add(GTK_CONTAINER(content), label); |
| 72 | gtk_widget_show_all(dialog); |
| 73 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); |
| 74 | gtk_dialog_run(GTK_DIALOG(dialog)); |
| 75 | gtk_widget_destroy(dialog); |
| 76 | } |