progress.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_ui_progress__update(struct ui_progress *p)
  8. {
  9. double fraction = p->total ? 1.0 * p->curr / p->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(p->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, p->curr, p->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 void gtk_ui_progress__finish(void)
  32. {
  33. /* this will also destroy all of its children */
  34. gtk_widget_destroy(dialog);
  35. dialog = NULL;
  36. }
  37. static struct ui_progress_ops gtk_ui_progress__ops = {
  38. .update = gtk_ui_progress__update,
  39. .finish = gtk_ui_progress__finish,
  40. };
  41. void gtk_ui_progress__init(void)
  42. {
  43. ui_progress__ops = &gtk_ui_progress__ops;
  44. }