progress.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <inttypes.h>
  3. #include "gtk.h"
  4. #include "../progress.h"
  5. #include "util.h"
  6. static GtkWidget *dialog;
  7. static GtkWidget *progress;
  8. static void gtk_ui_progress__update(struct ui_progress *p)
  9. {
  10. double fraction = p->total ? 1.0 * p->curr / p->total : 0.0;
  11. char buf[1024];
  12. if (dialog == NULL) {
  13. GtkWidget *vbox = gtk_vbox_new(TRUE, 5);
  14. GtkWidget *label = gtk_label_new(p->title);
  15. dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  16. progress = gtk_progress_bar_new();
  17. gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, FALSE, 3);
  18. gtk_box_pack_start(GTK_BOX(vbox), progress, TRUE, TRUE, 3);
  19. gtk_container_add(GTK_CONTAINER(dialog), vbox);
  20. gtk_window_set_title(GTK_WINDOW(dialog), "perf");
  21. gtk_window_resize(GTK_WINDOW(dialog), 300, 80);
  22. gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
  23. gtk_widget_show_all(dialog);
  24. }
  25. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), fraction);
  26. snprintf(buf, sizeof(buf), "%"PRIu64" / %"PRIu64, p->curr, p->total);
  27. gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), buf);
  28. /* we didn't call gtk_main yet, so do it manually */
  29. while (gtk_events_pending())
  30. gtk_main_iteration();
  31. }
  32. static void gtk_ui_progress__finish(void)
  33. {
  34. /* this will also destroy all of its children */
  35. gtk_widget_destroy(dialog);
  36. dialog = NULL;
  37. }
  38. static struct ui_progress_ops gtk_ui_progress__ops = {
  39. .update = gtk_ui_progress__update,
  40. .finish = gtk_ui_progress__finish,
  41. };
  42. void gtk_ui_progress__init(void)
  43. {
  44. ui_progress__ops = &gtk_ui_progress__ops;
  45. }