|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include <stdio.h> |
|
8 #include <gtk/gtk.h> |
|
9 #include <unistd.h> |
|
10 #include "progressui.h" |
|
11 #include "readstrings.h" |
|
12 #include "errors.h" |
|
13 |
|
14 #define TIMER_INTERVAL 100 |
|
15 |
|
16 static float sProgressVal; // between 0 and 100 |
|
17 static gboolean sQuit = FALSE; |
|
18 static gboolean sEnableUI; |
|
19 static guint sTimerID; |
|
20 |
|
21 static GtkWidget *sWin; |
|
22 static GtkWidget *sLabel; |
|
23 static GtkWidget *sProgressBar; |
|
24 |
|
25 static const char *sProgramPath; |
|
26 |
|
27 static gboolean |
|
28 UpdateDialog(gpointer data) |
|
29 { |
|
30 if (sQuit) |
|
31 { |
|
32 gtk_widget_hide(sWin); |
|
33 gtk_main_quit(); |
|
34 } |
|
35 |
|
36 float progress = sProgressVal; |
|
37 |
|
38 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(sProgressBar), |
|
39 progress / 100.0); |
|
40 |
|
41 return TRUE; |
|
42 } |
|
43 |
|
44 static gboolean |
|
45 OnDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data) |
|
46 { |
|
47 return TRUE; |
|
48 } |
|
49 |
|
50 int |
|
51 InitProgressUI(int *pargc, char ***pargv) |
|
52 { |
|
53 sProgramPath = (*pargv)[0]; |
|
54 |
|
55 sEnableUI = gtk_init_check(pargc, pargv); |
|
56 return 0; |
|
57 } |
|
58 |
|
59 int |
|
60 ShowProgressUI() |
|
61 { |
|
62 if (!sEnableUI) |
|
63 return -1; |
|
64 |
|
65 // Only show the Progress UI if the process is taking a significant amount of |
|
66 // time where a significant amount of time is defined as .5 seconds after |
|
67 // ShowProgressUI is called sProgress is less than 70. |
|
68 usleep(500000); |
|
69 |
|
70 if (sQuit || sProgressVal > 70.0f) |
|
71 return 0; |
|
72 |
|
73 char ini_path[PATH_MAX]; |
|
74 snprintf(ini_path, sizeof(ini_path), "%s.ini", sProgramPath); |
|
75 |
|
76 StringTable strings; |
|
77 if (ReadStrings(ini_path, &strings) != OK) |
|
78 return -1; |
|
79 |
|
80 sWin = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
|
81 if (!sWin) |
|
82 return -1; |
|
83 |
|
84 static GdkPixbuf *pixbuf; |
|
85 char icon_path[PATH_MAX]; |
|
86 snprintf(icon_path, sizeof(icon_path), "%s.png", sProgramPath); |
|
87 |
|
88 g_signal_connect(G_OBJECT(sWin), "delete_event", |
|
89 G_CALLBACK(OnDeleteEvent), nullptr); |
|
90 |
|
91 gtk_window_set_title(GTK_WINDOW(sWin), strings.title); |
|
92 gtk_window_set_type_hint(GTK_WINDOW(sWin), GDK_WINDOW_TYPE_HINT_DIALOG); |
|
93 gtk_window_set_position(GTK_WINDOW(sWin), GTK_WIN_POS_CENTER_ALWAYS); |
|
94 gtk_window_set_resizable(GTK_WINDOW(sWin), FALSE); |
|
95 gtk_window_set_decorated(GTK_WINDOW(sWin), TRUE); |
|
96 gtk_window_set_deletable(GTK_WINDOW(sWin),FALSE); |
|
97 pixbuf = gdk_pixbuf_new_from_file (icon_path, nullptr); |
|
98 gtk_window_set_icon(GTK_WINDOW(sWin), pixbuf); |
|
99 g_object_unref(pixbuf); |
|
100 |
|
101 GtkWidget *vbox = gtk_vbox_new(TRUE, 6); |
|
102 sLabel = gtk_label_new(strings.info); |
|
103 gtk_misc_set_alignment(GTK_MISC(sLabel), 0.0f, 0.0f); |
|
104 sProgressBar = gtk_progress_bar_new(); |
|
105 |
|
106 gtk_box_pack_start(GTK_BOX(vbox), sLabel, FALSE, FALSE, 0); |
|
107 gtk_box_pack_start(GTK_BOX(vbox), sProgressBar, TRUE, TRUE, 0); |
|
108 |
|
109 sTimerID = g_timeout_add(TIMER_INTERVAL, UpdateDialog, nullptr); |
|
110 |
|
111 gtk_container_set_border_width(GTK_CONTAINER(sWin), 10); |
|
112 gtk_container_add(GTK_CONTAINER(sWin), vbox); |
|
113 gtk_widget_show_all(sWin); |
|
114 |
|
115 gtk_main(); |
|
116 return 0; |
|
117 } |
|
118 |
|
119 // Called on a background thread |
|
120 void |
|
121 QuitProgressUI() |
|
122 { |
|
123 sQuit = TRUE; |
|
124 } |
|
125 |
|
126 // Called on a background thread |
|
127 void |
|
128 UpdateProgressUI(float progress) |
|
129 { |
|
130 sProgressVal = progress; // 32-bit writes are atomic |
|
131 } |