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