michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "progressui.h" michael@0: #include "readstrings.h" michael@0: #include "errors.h" michael@0: michael@0: #define TIMER_INTERVAL 100 michael@0: michael@0: static float sProgressVal; // between 0 and 100 michael@0: static gboolean sQuit = FALSE; michael@0: static gboolean sEnableUI; michael@0: static guint sTimerID; michael@0: michael@0: static GtkWidget *sWin; michael@0: static GtkWidget *sLabel; michael@0: static GtkWidget *sProgressBar; michael@0: michael@0: static const char *sProgramPath; michael@0: michael@0: static gboolean michael@0: UpdateDialog(gpointer data) michael@0: { michael@0: if (sQuit) michael@0: { michael@0: gtk_widget_hide(sWin); michael@0: gtk_main_quit(); michael@0: } michael@0: michael@0: float progress = sProgressVal; michael@0: michael@0: gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(sProgressBar), michael@0: progress / 100.0); michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: static gboolean michael@0: OnDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data) michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: int michael@0: InitProgressUI(int *pargc, char ***pargv) michael@0: { michael@0: sProgramPath = (*pargv)[0]; michael@0: michael@0: sEnableUI = gtk_init_check(pargc, pargv); michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: ShowProgressUI() michael@0: { michael@0: if (!sEnableUI) michael@0: return -1; michael@0: michael@0: // Only show the Progress UI if the process is taking a significant amount of michael@0: // time where a significant amount of time is defined as .5 seconds after michael@0: // ShowProgressUI is called sProgress is less than 70. michael@0: usleep(500000); michael@0: michael@0: if (sQuit || sProgressVal > 70.0f) michael@0: return 0; michael@0: michael@0: char ini_path[PATH_MAX]; michael@0: snprintf(ini_path, sizeof(ini_path), "%s.ini", sProgramPath); michael@0: michael@0: StringTable strings; michael@0: if (ReadStrings(ini_path, &strings) != OK) michael@0: return -1; michael@0: michael@0: sWin = gtk_window_new(GTK_WINDOW_TOPLEVEL); michael@0: if (!sWin) michael@0: return -1; michael@0: michael@0: static GdkPixbuf *pixbuf; michael@0: char icon_path[PATH_MAX]; michael@0: snprintf(icon_path, sizeof(icon_path), "%s.png", sProgramPath); michael@0: michael@0: g_signal_connect(G_OBJECT(sWin), "delete_event", michael@0: G_CALLBACK(OnDeleteEvent), nullptr); michael@0: michael@0: gtk_window_set_title(GTK_WINDOW(sWin), strings.title); michael@0: gtk_window_set_type_hint(GTK_WINDOW(sWin), GDK_WINDOW_TYPE_HINT_DIALOG); michael@0: gtk_window_set_position(GTK_WINDOW(sWin), GTK_WIN_POS_CENTER_ALWAYS); michael@0: gtk_window_set_resizable(GTK_WINDOW(sWin), FALSE); michael@0: gtk_window_set_decorated(GTK_WINDOW(sWin), TRUE); michael@0: gtk_window_set_deletable(GTK_WINDOW(sWin),FALSE); michael@0: pixbuf = gdk_pixbuf_new_from_file (icon_path, nullptr); michael@0: gtk_window_set_icon(GTK_WINDOW(sWin), pixbuf); michael@0: g_object_unref(pixbuf); michael@0: michael@0: GtkWidget *vbox = gtk_vbox_new(TRUE, 6); michael@0: sLabel = gtk_label_new(strings.info); michael@0: gtk_misc_set_alignment(GTK_MISC(sLabel), 0.0f, 0.0f); michael@0: sProgressBar = gtk_progress_bar_new(); michael@0: michael@0: gtk_box_pack_start(GTK_BOX(vbox), sLabel, FALSE, FALSE, 0); michael@0: gtk_box_pack_start(GTK_BOX(vbox), sProgressBar, TRUE, TRUE, 0); michael@0: michael@0: sTimerID = g_timeout_add(TIMER_INTERVAL, UpdateDialog, nullptr); michael@0: michael@0: gtk_container_set_border_width(GTK_CONTAINER(sWin), 10); michael@0: gtk_container_add(GTK_CONTAINER(sWin), vbox); michael@0: gtk_widget_show_all(sWin); michael@0: michael@0: gtk_main(); michael@0: return 0; michael@0: } michael@0: michael@0: // Called on a background thread michael@0: void michael@0: QuitProgressUI() michael@0: { michael@0: sQuit = TRUE; michael@0: } michael@0: michael@0: // Called on a background thread michael@0: void michael@0: UpdateProgressUI(float progress) michael@0: { michael@0: sProgressVal = progress; // 32-bit writes are atomic michael@0: }