progress.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <inttypes.h>
  2. #include "gtk.h"
  3. #include "../progress.h"
  4. #include "util.h"
  5. static GtkWidget *dialog;
  6. static GtkWidget *progress;
  7. static void gtk_progress_update(u64 curr, u64 total, const char *title)
  8. {
  9. double fraction = total ? 1.0 * curr / total : 0.0;
  10. char buf[1024];
  11. if (dialog == NULL) {
  12. GtkWidget *vbox = gtk_vbox_new(TRUE, 5);
  13. GtkWidget *label = gtk_label_new(title);
  14. dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  15. progress = gtk_progress_bar_new();
  16. gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, FALSE, 3);
  17. gtk_box_pack_start(GTK_BOX(vbox), progress, TRUE, TRUE, 3);
  18. gtk_container_add(GTK_CONTAINER(dialog), vbox);
  19. gtk_window_set_title(GTK_WINDOW(dialog), "perf");
  20. gtk_window_resize(GTK_WINDOW(dialog), 300, 80);
  21. gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
  22. gtk_widget_show_all(dialog);
  23. }
  24. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), fraction);
  25. snprintf(buf, sizeof(buf), "%"PRIu64" / %"PRIu64, curr, total);
  26. gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), buf);
  27. /* we didn't call gtk_main yet, so do it manually */
  28. while (gtk_events_pending())
  29. gtk_main_iteration();
  30. }
  31. static struct ui_progress gtk_progress_fns = {
  32. .update = gtk_progress_update,
  33. };
  34. void perf_gtk__init_progress(void)
  35. {
  36. progress_fns = &gtk_progress_fns;
  37. }