1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpprogressdialog.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 "libgimpwidgets/gimpwidgets.h"
27 
28 #include "widgets-types.h"
29 
30 #include "core/gimpprogress.h"
31 
32 #include "gimpprogressbox.h"
33 #include "gimpprogressdialog.h"
34 
35 #include "gimp-intl.h"
36 
37 
38 #define PROGRESS_DIALOG_WIDTH  400
39 
40 
41 static void    gimp_progress_dialog_progress_iface_init (GimpProgressInterface *iface);
42 
43 static void     gimp_progress_dialog_response           (GtkDialog    *dialog,
44                                                          gint          response_id);
45 
46 static GimpProgress *
47                 gimp_progress_dialog_progress_start     (GimpProgress *progress,
48                                                          gboolean      cancellable,
49                                                          const gchar  *message);
50 static void     gimp_progress_dialog_progress_end       (GimpProgress *progress);
51 static gboolean gimp_progress_dialog_progress_is_active (GimpProgress *progress);
52 static void     gimp_progress_dialog_progress_set_text  (GimpProgress *progress,
53                                                          const gchar  *message);
54 static void     gimp_progress_dialog_progress_set_value (GimpProgress *progress,
55                                                          gdouble       percentage);
56 static gdouble  gimp_progress_dialog_progress_get_value (GimpProgress *progress);
57 static void     gimp_progress_dialog_progress_pulse     (GimpProgress *progress);
58 
59 
G_DEFINE_TYPE_WITH_CODE(GimpProgressDialog,gimp_progress_dialog,GIMP_TYPE_DIALOG,G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROGRESS,gimp_progress_dialog_progress_iface_init))60 G_DEFINE_TYPE_WITH_CODE (GimpProgressDialog, gimp_progress_dialog,
61                          GIMP_TYPE_DIALOG,
62                          G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROGRESS,
63                                                 gimp_progress_dialog_progress_iface_init))
64 
65 #define parent_class gimp_progress_dialog_parent_class
66 
67 
68 static void
69 gimp_progress_dialog_class_init (GimpProgressDialogClass *klass)
70 {
71   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
72 
73   dialog_class->response = gimp_progress_dialog_response;
74 }
75 
76 static void
gimp_progress_dialog_init(GimpProgressDialog * dialog)77 gimp_progress_dialog_init (GimpProgressDialog *dialog)
78 {
79   GtkWidget *content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
80 
81   dialog->box = gimp_progress_box_new ();
82   gtk_container_set_border_width (GTK_CONTAINER (dialog->box), 12);
83   gtk_box_pack_start (GTK_BOX (content_area), dialog->box, TRUE, TRUE, 0);
84   gtk_widget_show (dialog->box);
85 
86   g_signal_connect (dialog->box, "destroy",
87                     G_CALLBACK (gtk_widget_destroyed),
88                     &dialog->box);
89 
90   gtk_dialog_add_button (GTK_DIALOG (dialog),
91                          _("_Cancel"), GTK_RESPONSE_CANCEL);
92   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
93 
94   gtk_widget_set_size_request (GTK_WIDGET (dialog), PROGRESS_DIALOG_WIDTH, -1);
95 }
96 
97 static void
gimp_progress_dialog_progress_iface_init(GimpProgressInterface * iface)98 gimp_progress_dialog_progress_iface_init (GimpProgressInterface *iface)
99 {
100   iface->start     = gimp_progress_dialog_progress_start;
101   iface->end       = gimp_progress_dialog_progress_end;
102   iface->is_active = gimp_progress_dialog_progress_is_active;
103   iface->set_text  = gimp_progress_dialog_progress_set_text;
104   iface->set_value = gimp_progress_dialog_progress_set_value;
105   iface->get_value = gimp_progress_dialog_progress_get_value;
106   iface->pulse     = gimp_progress_dialog_progress_pulse;
107 }
108 
109 static void
gimp_progress_dialog_response(GtkDialog * dialog,gint response_id)110 gimp_progress_dialog_response (GtkDialog *dialog,
111                                gint       response_id)
112 {
113   GimpProgressDialog *progress_dialog = GIMP_PROGRESS_DIALOG (dialog);
114 
115   if (GIMP_PROGRESS_BOX (progress_dialog->box)->cancellable)
116     gimp_progress_cancel (GIMP_PROGRESS (dialog));
117 }
118 
119 static GimpProgress *
gimp_progress_dialog_progress_start(GimpProgress * progress,gboolean cancellable,const gchar * message)120 gimp_progress_dialog_progress_start (GimpProgress *progress,
121                                      gboolean      cancellable,
122                                      const gchar  *message)
123 {
124   GimpProgressDialog *dialog = GIMP_PROGRESS_DIALOG (progress);
125 
126   if (! dialog->box)
127     return NULL;
128 
129   if (gimp_progress_start (GIMP_PROGRESS (dialog->box), cancellable,
130                            "%s", message))
131     {
132       gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
133                                          GTK_RESPONSE_CANCEL, cancellable);
134 
135       gtk_window_present (GTK_WINDOW (dialog));
136 
137       return progress;
138     }
139 
140   return NULL;
141 }
142 
143 static void
gimp_progress_dialog_progress_end(GimpProgress * progress)144 gimp_progress_dialog_progress_end (GimpProgress *progress)
145 {
146   GimpProgressDialog *dialog = GIMP_PROGRESS_DIALOG (progress);
147 
148   if (! dialog->box)
149     return;
150 
151   if (GIMP_PROGRESS_BOX (dialog->box)->active)
152     {
153       gimp_progress_end (GIMP_PROGRESS (dialog->box));
154 
155       gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
156                                          GTK_RESPONSE_CANCEL, FALSE);
157 
158       gtk_widget_hide (GTK_WIDGET (dialog));
159     }
160 }
161 
162 static gboolean
gimp_progress_dialog_progress_is_active(GimpProgress * progress)163 gimp_progress_dialog_progress_is_active (GimpProgress *progress)
164 {
165   GimpProgressDialog *dialog = GIMP_PROGRESS_DIALOG (progress);
166 
167   if (! dialog->box)
168     return FALSE;
169 
170   return gimp_progress_is_active (GIMP_PROGRESS (dialog->box));
171 }
172 
173 static void
gimp_progress_dialog_progress_set_text(GimpProgress * progress,const gchar * message)174 gimp_progress_dialog_progress_set_text (GimpProgress *progress,
175                                         const gchar  *message)
176 {
177   GimpProgressDialog *dialog = GIMP_PROGRESS_DIALOG (progress);
178 
179   if (! dialog->box)
180     return;
181 
182   gimp_progress_set_text_literal (GIMP_PROGRESS (dialog->box), message);
183 }
184 
185 static void
gimp_progress_dialog_progress_set_value(GimpProgress * progress,gdouble percentage)186 gimp_progress_dialog_progress_set_value (GimpProgress *progress,
187                                          gdouble       percentage)
188 {
189   GimpProgressDialog *dialog = GIMP_PROGRESS_DIALOG (progress);
190 
191   if (! dialog->box)
192     return;
193 
194   gimp_progress_set_value (GIMP_PROGRESS (dialog->box), percentage);
195 }
196 
197 static gdouble
gimp_progress_dialog_progress_get_value(GimpProgress * progress)198 gimp_progress_dialog_progress_get_value (GimpProgress *progress)
199 {
200   GimpProgressDialog *dialog = GIMP_PROGRESS_DIALOG (progress);
201 
202   if (! dialog->box)
203     return 0.0;
204 
205   return gimp_progress_get_value (GIMP_PROGRESS (dialog->box));
206 }
207 
208 static void
gimp_progress_dialog_progress_pulse(GimpProgress * progress)209 gimp_progress_dialog_progress_pulse (GimpProgress *progress)
210 {
211   GimpProgressDialog *dialog = GIMP_PROGRESS_DIALOG (progress);
212 
213   if (! dialog->box)
214     return;
215 
216   gimp_progress_pulse (GIMP_PROGRESS (dialog->box));
217 }
218 
219 GtkWidget *
gimp_progress_dialog_new(void)220 gimp_progress_dialog_new (void)
221 {
222   return g_object_new (GIMP_TYPE_PROGRESS_DIALOG,
223                        "title",             _("Progress"),
224                        "role",              "progress",
225                        "skip-taskbar-hint", TRUE,
226                        "skip-pager-hint",   TRUE,
227                        "resizable",         FALSE,
228                        "focus-on-map",      FALSE,
229                        "window-position",   GTK_WIN_POS_CENTER,
230                        NULL);
231 }
232