util.c 2.3 KB

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