blob: 303673818b05fad7dbe5044eb5f233d6f14fdcb5 [file] [log] [blame]
Jens Axboe41666582012-03-21 10:25:29 +01001#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
11static void on_info_bar_response(GtkWidget *widget, gint response,
Jens Axboe3c3ed072012-03-27 09:12:39 +020012 gpointer data)
Jens Axboe41666582012-03-21 10:25:29 +010013{
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
22static void report_error(struct gui_entry *ge, GError *error)
23{
24 struct gui *ui = ge->ui;
25
26 if (ui->error_info_bar == NULL) {
27 ui->error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
Jens Axboe3c3ed072012-03-27 09:12:39 +020028 GTK_RESPONSE_OK, NULL);
Jens Axboe41666582012-03-21 10:25:29 +010029 g_signal_connect(ui->error_info_bar, "response", G_CALLBACK(on_info_bar_response), ui);
30 gtk_info_bar_set_message_type(GTK_INFO_BAR(ui->error_info_bar),
Jens Axboe3c3ed072012-03-27 09:12:39 +020031 GTK_MESSAGE_ERROR);
32
Jens Axboe41666582012-03-21 10:25:29 +010033 ui->error_label = gtk_label_new(error->message);
34 GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(ui->error_info_bar));
35 gtk_container_add(GTK_CONTAINER(container), ui->error_label);
Jens Axboe3c3ed072012-03-27 09:12:39 +020036
Jens Axboe41666582012-03-21 10:25:29 +010037 gtk_box_pack_start(GTK_BOX(ui->vbox), ui->error_info_bar, FALSE, FALSE, 0);
38 gtk_widget_show_all(ui->vbox);
39 } else {
40 char buffer[256];
41 snprintf(buffer, sizeof(buffer), "Failed to open file.");
Jens Axboef762cef2012-03-26 13:51:11 +020042 gtk_label_set_text(GTK_LABEL(ui->error_label), buffer);
Jens Axboe41666582012-03-21 10:25:29 +010043 }
44}
45
46void gfio_report_error(struct gui_entry *ge, const char *format, ...)
47{
48 va_list args;
49 GError *error;
50
51 va_start(args, format);
52 error = g_error_new_valist(g_quark_from_string("fio"), 1, format, args);
53 va_end(args);
54
55 report_error(ge, error);
56 g_error_free(error);
57}
58
Jens Axboe1252d8f2012-03-21 11:13:31 +010059void gfio_report_info(struct gui *ui, const char *title, const char *message)
60{
61 GtkWidget *dialog, *content, *label;
Jens Axboe41666582012-03-21 10:25:29 +010062
Jens Axboe1252d8f2012-03-21 11:13:31 +010063 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(ui->window),
64 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
65 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
66
67 content = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
68 label = gtk_label_new(message);
69 gtk_container_add(GTK_CONTAINER(content), label);
70 gtk_widget_show_all(dialog);
71 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
72 gtk_dialog_run(GTK_DIALOG(dialog));
73 gtk_widget_destroy(dialog);
74}