util.c 2.3 KB

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