18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <inttypes.h>
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "gtk.h"
58c2ecf20Sopenharmony_ci#include "../progress.h"
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_cistatic GtkWidget *dialog;
88c2ecf20Sopenharmony_cistatic GtkWidget *progress;
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic void gtk_ui_progress__update(struct ui_progress *p)
118c2ecf20Sopenharmony_ci{
128c2ecf20Sopenharmony_ci	double fraction = p->total ? 1.0 * p->curr / p->total : 0.0;
138c2ecf20Sopenharmony_ci	char buf[1024];
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	if (dialog == NULL) {
168c2ecf20Sopenharmony_ci		GtkWidget *vbox = gtk_vbox_new(TRUE, 5);
178c2ecf20Sopenharmony_ci		GtkWidget *label = gtk_label_new(p->title);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci		dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
208c2ecf20Sopenharmony_ci		progress = gtk_progress_bar_new();
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci		gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, FALSE, 3);
238c2ecf20Sopenharmony_ci		gtk_box_pack_start(GTK_BOX(vbox), progress, TRUE, TRUE, 3);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci		gtk_container_add(GTK_CONTAINER(dialog), vbox);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci		gtk_window_set_title(GTK_WINDOW(dialog), "perf");
288c2ecf20Sopenharmony_ci		gtk_window_resize(GTK_WINDOW(dialog), 300, 80);
298c2ecf20Sopenharmony_ci		gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci		gtk_widget_show_all(dialog);
328c2ecf20Sopenharmony_ci	}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), fraction);
358c2ecf20Sopenharmony_ci	snprintf(buf, sizeof(buf), "%"PRIu64" / %"PRIu64, p->curr, p->total);
368c2ecf20Sopenharmony_ci	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), buf);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* we didn't call gtk_main yet, so do it manually */
398c2ecf20Sopenharmony_ci	while (gtk_events_pending())
408c2ecf20Sopenharmony_ci		gtk_main_iteration();
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void gtk_ui_progress__finish(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	/* this will also destroy all of its children */
468c2ecf20Sopenharmony_ci	gtk_widget_destroy(dialog);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	dialog = NULL;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic struct ui_progress_ops gtk_ui_progress__ops = {
528c2ecf20Sopenharmony_ci	.update		= gtk_ui_progress__update,
538c2ecf20Sopenharmony_ci	.finish		= gtk_ui_progress__finish,
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_civoid gtk_ui_progress__init(void)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	ui_progress__ops = &gtk_ui_progress__ops;
598c2ecf20Sopenharmony_ci}
60