blob: 52e7fc48af9f488088785b8e216e05f6b22546fc [file] [log] [blame]
Namhyung Kimba47a142012-05-29 13:22:58 +09001#include "../util.h"
2#include "../../util/debug.h"
3#include "gtk.h"
4
Namhyung Kime078ba12012-05-29 13:23:02 +09005#include <string.h>
6
Namhyung Kimba47a142012-05-29 13:22:58 +09007
Namhyung Kim42ab68a2012-05-29 13:22:59 +09008struct perf_gtk_context *pgctx;
9
10struct 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
21int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)
22{
23 if (!perf_gtk__is_active_context(*ctx))
24 return -1;
25
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -030026 zfree(ctx);
Namhyung Kim42ab68a2012-05-29 13:22:59 +090027 return 0;
28}
29
Namhyung Kime078ba12012-05-29 13:23:02 +090030static 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
Ingo Molnar89fe8082013-09-30 12:07:11 +020055#ifdef HAVE_GTK_INFO_BAR_SUPPORT
Namhyung Kime078ba12012-05-29 13:23:02 +090056static 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
77static 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
105struct perf_error_ops perf_gtk_eops = {
106 .error = perf_gtk__error,
Ingo Molnar89fe8082013-09-30 12:07:11 +0200107#ifdef HAVE_GTK_INFO_BAR_SUPPORT
Namhyung Kime078ba12012-05-29 13:23:02 +0900108 .warning = perf_gtk__warning_info_bar,
109#else
110 .warning = perf_gtk__warning_statusbar,
111#endif
112};