1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a753579cSNamhyung Kim #include <inttypes.h>
3a753579cSNamhyung Kim
4a753579cSNamhyung Kim #include "gtk.h"
5a753579cSNamhyung Kim #include "../progress.h"
6a753579cSNamhyung Kim
7a753579cSNamhyung Kim static GtkWidget *dialog;
8a753579cSNamhyung Kim static GtkWidget *progress;
9a753579cSNamhyung Kim
gtk_ui_progress__update(struct ui_progress * p)104d3001fdSArnaldo Carvalho de Melo static void gtk_ui_progress__update(struct ui_progress *p)
11a753579cSNamhyung Kim {
124d3001fdSArnaldo Carvalho de Melo double fraction = p->total ? 1.0 * p->curr / p->total : 0.0;
13a753579cSNamhyung Kim char buf[1024];
14a753579cSNamhyung Kim
15a753579cSNamhyung Kim if (dialog == NULL) {
16a753579cSNamhyung Kim GtkWidget *vbox = gtk_vbox_new(TRUE, 5);
174d3001fdSArnaldo Carvalho de Melo GtkWidget *label = gtk_label_new(p->title);
18a753579cSNamhyung Kim
19a753579cSNamhyung Kim dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
20a753579cSNamhyung Kim progress = gtk_progress_bar_new();
21a753579cSNamhyung Kim
22a753579cSNamhyung Kim gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, FALSE, 3);
23a753579cSNamhyung Kim gtk_box_pack_start(GTK_BOX(vbox), progress, TRUE, TRUE, 3);
24a753579cSNamhyung Kim
25a753579cSNamhyung Kim gtk_container_add(GTK_CONTAINER(dialog), vbox);
26a753579cSNamhyung Kim
27a753579cSNamhyung Kim gtk_window_set_title(GTK_WINDOW(dialog), "perf");
28a753579cSNamhyung Kim gtk_window_resize(GTK_WINDOW(dialog), 300, 80);
29a753579cSNamhyung Kim gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
30a753579cSNamhyung Kim
31a753579cSNamhyung Kim gtk_widget_show_all(dialog);
32a753579cSNamhyung Kim }
33a753579cSNamhyung Kim
34a753579cSNamhyung Kim gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), fraction);
354d3001fdSArnaldo Carvalho de Melo snprintf(buf, sizeof(buf), "%"PRIu64" / %"PRIu64, p->curr, p->total);
36a753579cSNamhyung Kim gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), buf);
37a753579cSNamhyung Kim
38a753579cSNamhyung Kim /* we didn't call gtk_main yet, so do it manually */
39a753579cSNamhyung Kim while (gtk_events_pending())
40a753579cSNamhyung Kim gtk_main_iteration();
41a753579cSNamhyung Kim }
42a753579cSNamhyung Kim
gtk_ui_progress__finish(void)434779a2e9SArnaldo Carvalho de Melo static void gtk_ui_progress__finish(void)
44a5580f3eSNamhyung Kim {
45a5580f3eSNamhyung Kim /* this will also destroy all of its children */
46a5580f3eSNamhyung Kim gtk_widget_destroy(dialog);
47a5580f3eSNamhyung Kim
48a5580f3eSNamhyung Kim dialog = NULL;
49a5580f3eSNamhyung Kim }
50a5580f3eSNamhyung Kim
514779a2e9SArnaldo Carvalho de Melo static struct ui_progress_ops gtk_ui_progress__ops = {
524779a2e9SArnaldo Carvalho de Melo .update = gtk_ui_progress__update,
534779a2e9SArnaldo Carvalho de Melo .finish = gtk_ui_progress__finish,
54a753579cSNamhyung Kim };
55a753579cSNamhyung Kim
gtk_ui_progress__init(void)564779a2e9SArnaldo Carvalho de Melo void gtk_ui_progress__init(void)
57a753579cSNamhyung Kim {
584779a2e9SArnaldo Carvalho de Melo ui_progress__ops = >k_ui_progress__ops;
59a753579cSNamhyung Kim }
60