1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * gimpprogressbox.c
5 * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <gegl.h>
24 #include <gtk/gtk.h>
25
26 #include "libgimpmath/gimpmath.h"
27 #include "libgimpwidgets/gimpwidgets.h"
28
29 #include "widgets-types.h"
30
31 #include "core/gimpprogress.h"
32
33 #include "gimpprogressbox.h"
34 #include "gimpwidgets-utils.h"
35
36 #include "gimp-intl.h"
37
38
39 static void gimp_progress_box_progress_iface_init (GimpProgressInterface *iface);
40
41 static void gimp_progress_box_dispose (GObject *object);
42
43 static GimpProgress *
44 gimp_progress_box_progress_start (GimpProgress *progress,
45 gboolean cancellable,
46 const gchar *message);
47 static void gimp_progress_box_progress_end (GimpProgress *progress);
48 static gboolean gimp_progress_box_progress_is_active (GimpProgress *progress);
49 static void gimp_progress_box_progress_set_text (GimpProgress *progress,
50 const gchar *message);
51 static void gimp_progress_box_progress_set_value (GimpProgress *progress,
52 gdouble percentage);
53 static gdouble gimp_progress_box_progress_get_value (GimpProgress *progress);
54 static void gimp_progress_box_progress_pulse (GimpProgress *progress);
55
56
G_DEFINE_TYPE_WITH_CODE(GimpProgressBox,gimp_progress_box,GTK_TYPE_BOX,G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROGRESS,gimp_progress_box_progress_iface_init))57 G_DEFINE_TYPE_WITH_CODE (GimpProgressBox, gimp_progress_box, GTK_TYPE_BOX,
58 G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROGRESS,
59 gimp_progress_box_progress_iface_init))
60
61 #define parent_class gimp_progress_box_parent_class
62
63
64 static void
65 gimp_progress_box_class_init (GimpProgressBoxClass *klass)
66 {
67 GObjectClass *object_class = G_OBJECT_CLASS (klass);
68
69 object_class->dispose = gimp_progress_box_dispose;
70 }
71
72 static void
gimp_progress_box_init(GimpProgressBox * box)73 gimp_progress_box_init (GimpProgressBox *box)
74 {
75 gtk_orientable_set_orientation (GTK_ORIENTABLE (box),
76 GTK_ORIENTATION_VERTICAL);
77
78 gtk_box_set_spacing (GTK_BOX (box), 6);
79
80 box->progress = gtk_progress_bar_new ();
81 gtk_widget_set_size_request (box->progress, 250, 20);
82 gtk_box_pack_start (GTK_BOX (box), box->progress, FALSE, FALSE, 0);
83 gtk_widget_show (box->progress);
84
85 box->label = gtk_label_new ("");
86 gtk_label_set_ellipsize (GTK_LABEL (box->label), PANGO_ELLIPSIZE_MIDDLE);
87 gtk_label_set_xalign (GTK_LABEL (box->label), 0.0);
88 gimp_label_set_attributes (GTK_LABEL (box->label),
89 PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
90 -1);
91 gtk_box_pack_start (GTK_BOX (box), box->label, FALSE, FALSE, 0);
92 gtk_widget_show (box->label);
93 }
94
95 static void
gimp_progress_box_progress_iface_init(GimpProgressInterface * iface)96 gimp_progress_box_progress_iface_init (GimpProgressInterface *iface)
97 {
98 iface->start = gimp_progress_box_progress_start;
99 iface->end = gimp_progress_box_progress_end;
100 iface->is_active = gimp_progress_box_progress_is_active;
101 iface->set_text = gimp_progress_box_progress_set_text;
102 iface->set_value = gimp_progress_box_progress_set_value;
103 iface->get_value = gimp_progress_box_progress_get_value;
104 iface->pulse = gimp_progress_box_progress_pulse;
105 }
106
107 static void
gimp_progress_box_dispose(GObject * object)108 gimp_progress_box_dispose (GObject *object)
109 {
110 GimpProgressBox *box = GIMP_PROGRESS_BOX (object);
111
112 G_OBJECT_CLASS (parent_class)->dispose (object);
113
114 box->progress = NULL;
115 }
116
117 static GimpProgress *
gimp_progress_box_progress_start(GimpProgress * progress,gboolean cancellable,const gchar * message)118 gimp_progress_box_progress_start (GimpProgress *progress,
119 gboolean cancellable,
120 const gchar *message)
121 {
122 GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
123
124 if (! box->progress)
125 return NULL;
126
127 if (! box->active)
128 {
129 GtkProgressBar *bar = GTK_PROGRESS_BAR (box->progress);
130
131 gtk_label_set_text (GTK_LABEL (box->label), message);
132 gtk_progress_bar_set_fraction (bar, 0.0);
133
134 box->active = TRUE;
135 box->cancellable = cancellable;
136 box->value = 0.0;
137
138 if (gtk_widget_is_drawable (box->progress))
139 gdk_window_process_updates (gtk_widget_get_window (box->progress),
140 TRUE);
141
142 return progress;
143 }
144
145 return NULL;
146 }
147
148 static void
gimp_progress_box_progress_end(GimpProgress * progress)149 gimp_progress_box_progress_end (GimpProgress *progress)
150 {
151 if (gimp_progress_box_progress_is_active (progress))
152 {
153 GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
154 GtkProgressBar *bar = GTK_PROGRESS_BAR (box->progress);
155
156 gtk_label_set_text (GTK_LABEL (box->label), "");
157 gtk_progress_bar_set_fraction (bar, 0.0);
158
159 box->active = FALSE;
160 box->cancellable = FALSE;
161 box->value = 0.0;
162 }
163 }
164
165 static gboolean
gimp_progress_box_progress_is_active(GimpProgress * progress)166 gimp_progress_box_progress_is_active (GimpProgress *progress)
167 {
168 GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
169
170 return (box->progress && box->active);
171 }
172
173 static void
gimp_progress_box_progress_set_text(GimpProgress * progress,const gchar * message)174 gimp_progress_box_progress_set_text (GimpProgress *progress,
175 const gchar *message)
176 {
177 if (gimp_progress_box_progress_is_active (progress))
178 {
179 GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
180
181 gtk_label_set_text (GTK_LABEL (box->label), message);
182
183 if (gtk_widget_is_drawable (box->progress))
184 gdk_window_process_updates (gtk_widget_get_window (box->progress),
185 TRUE);
186 }
187 }
188
189 static void
gimp_progress_box_progress_set_value(GimpProgress * progress,gdouble percentage)190 gimp_progress_box_progress_set_value (GimpProgress *progress,
191 gdouble percentage)
192 {
193 if (gimp_progress_box_progress_is_active (progress))
194 {
195 GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
196 GtkProgressBar *bar = GTK_PROGRESS_BAR (box->progress);
197 GtkAllocation allocation;
198
199 gtk_widget_get_allocation (GTK_WIDGET (bar), &allocation);
200
201 box->value = percentage;
202
203 /* only update the progress bar if this causes a visible change */
204 if (fabs (allocation.width *
205 (percentage - gtk_progress_bar_get_fraction (bar))) > 1.0)
206 {
207 gtk_progress_bar_set_fraction (bar, box->value);
208
209 gimp_widget_flush_expose (box->progress);
210 }
211 }
212 }
213
214 static gdouble
gimp_progress_box_progress_get_value(GimpProgress * progress)215 gimp_progress_box_progress_get_value (GimpProgress *progress)
216 {
217 if (gimp_progress_box_progress_is_active (progress))
218 {
219 return GIMP_PROGRESS_BOX (progress)->value;
220 }
221
222 return 0.0;
223 }
224
225 static void
gimp_progress_box_progress_pulse(GimpProgress * progress)226 gimp_progress_box_progress_pulse (GimpProgress *progress)
227 {
228 if (gimp_progress_box_progress_is_active (progress))
229 {
230 GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
231 GtkProgressBar *bar = GTK_PROGRESS_BAR (box->progress);
232
233 gtk_progress_bar_pulse (bar);
234
235 if (gtk_widget_is_drawable (box->progress))
236 gdk_window_process_updates (gtk_widget_get_window (box->progress),
237 TRUE);
238 }
239 }
240
241 GtkWidget *
gimp_progress_box_new(void)242 gimp_progress_box_new (void)
243 {
244 return g_object_new (GIMP_TYPE_PROGRESS_BOX, NULL);
245 }
246