1 #include "../util.h" 2 #include "../../util/debug.h" 3 #include "gtk.h" 4 5 #include <string.h> 6 7 8 struct perf_gtk_context *pgctx; 9 10 struct perf_gtk_context *perf_gtk__activate_context(GtkWidget *window) 11 { 12 struct perf_gtk_context *ctx; 13 14 ctx = malloc(sizeof(*pgctx)); 15 if (ctx) 16 ctx->main_window = window; 17 18 return ctx; 19 } 20 21 int perf_gtk__deactivate_context(struct perf_gtk_context **ctx) 22 { 23 if (!perf_gtk__is_active_context(*ctx)) 24 return -1; 25 26 zfree(ctx); 27 return 0; 28 } 29 30 static int perf_gtk__error(const char *format, va_list args) 31 { 32 char *msg; 33 GtkWidget *dialog; 34 35 if (!perf_gtk__is_active_context(pgctx) || 36 vasprintf(&msg, format, args) < 0) { 37 fprintf(stderr, "Error:\n"); 38 vfprintf(stderr, format, args); 39 fprintf(stderr, "\n"); 40 return -1; 41 } 42 43 dialog = gtk_message_dialog_new_with_markup(GTK_WINDOW(pgctx->main_window), 44 GTK_DIALOG_DESTROY_WITH_PARENT, 45 GTK_MESSAGE_ERROR, 46 GTK_BUTTONS_CLOSE, 47 "<b>Error</b>\n\n%s", msg); 48 gtk_dialog_run(GTK_DIALOG(dialog)); 49 50 gtk_widget_destroy(dialog); 51 free(msg); 52 return 0; 53 } 54 55 #ifdef HAVE_GTK_INFO_BAR_SUPPORT 56 static int perf_gtk__warning_info_bar(const char *format, va_list args) 57 { 58 char *msg; 59 60 if (!perf_gtk__is_active_context(pgctx) || 61 vasprintf(&msg, format, args) < 0) { 62 fprintf(stderr, "Warning:\n"); 63 vfprintf(stderr, format, args); 64 fprintf(stderr, "\n"); 65 return -1; 66 } 67 68 gtk_label_set_text(GTK_LABEL(pgctx->message_label), msg); 69 gtk_info_bar_set_message_type(GTK_INFO_BAR(pgctx->info_bar), 70 GTK_MESSAGE_WARNING); 71 gtk_widget_show(pgctx->info_bar); 72 73 free(msg); 74 return 0; 75 } 76 #else 77 static int perf_gtk__warning_statusbar(const char *format, va_list args) 78 { 79 char *msg, *p; 80 81 if (!perf_gtk__is_active_context(pgctx) || 82 vasprintf(&msg, format, args) < 0) { 83 fprintf(stderr, "Warning:\n"); 84 vfprintf(stderr, format, args); 85 fprintf(stderr, "\n"); 86 return -1; 87 } 88 89 gtk_statusbar_pop(GTK_STATUSBAR(pgctx->statbar), 90 pgctx->statbar_ctx_id); 91 92 /* Only first line can be displayed */ 93 p = strchr(msg, '\n'); 94 if (p) 95 *p = '\0'; 96 97 gtk_statusbar_push(GTK_STATUSBAR(pgctx->statbar), 98 pgctx->statbar_ctx_id, msg); 99 100 free(msg); 101 return 0; 102 } 103 #endif 104 105 struct perf_error_ops perf_gtk_eops = { 106 .error = perf_gtk__error, 107 #ifdef HAVE_GTK_INFO_BAR_SUPPORT 108 .warning = perf_gtk__warning_info_bar, 109 #else 110 .warning = perf_gtk__warning_statusbar, 111 #endif 112 }; 113