1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpprogress.c
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gio/gio.h>
#include "libgimpbase/gimpbase.h"
#include "gimpprogress.h"
#include "gimp.h"
typedef struct
{
gchar *progress_callback;
GimpProgressVtable vtable;
gpointer data;
GDestroyNotify data_destroy;
} GimpProgressData;
/* local function prototypes */
static void gimp_progress_data_free (GimpProgressData *data);
static GimpValueArray * gimp_temp_progress_run (GimpProcedure *procedure,
GimpProcedureConfig *config,
gpointer run_data);
/* private variables */
static gdouble gimp_progress_current = 0.0;
static const gdouble gimp_progress_step = (1.0 / 256.0);
static const gint64 gimp_progress_delay = 50000; /* 50 millisecond */
/* public functions */
/**
* gimp_progress_install_vtable:
* @vtable: a pointer to a @GimpProgressVtable.
* @user_data: a pointer that is passed as user_data to all vtable
* functions.
* @user_data_destroy: (nullable): destroy function for @user_data, or %NULL.
*
* Returns: the name of the temporary procedure that's been installed
*
* Since: 2.4
**/
const gchar *
gimp_progress_install_vtable (const GimpProgressVtable *vtable,
gpointer user_data,
GDestroyNotify user_data_destroy)
{
GimpPlugIn *plug_in;
GimpProcedure *procedure;
gchar *progress_callback;
GimpProgressData *progress_data;
g_return_val_if_fail (vtable != NULL, NULL);
g_return_val_if_fail (vtable->start != NULL, NULL);
g_return_val_if_fail (vtable->end != NULL, NULL);
g_return_val_if_fail (vtable->set_text != NULL, NULL);
g_return_val_if_fail (vtable->set_value != NULL, NULL);
plug_in = gimp_get_plug_in ();
progress_callback = gimp_pdb_temp_procedure_name (gimp_get_pdb ());
progress_data = g_slice_new0 (GimpProgressData);
progress_data->progress_callback = progress_callback;
progress_data->vtable.start = vtable->start;
progress_data->vtable.end = vtable->end;
progress_data->vtable.set_text = vtable->set_text;
progress_data->vtable.set_value = vtable->set_value;
progress_data->vtable.pulse = vtable->pulse;
progress_data->vtable.get_window_handle = vtable->get_window_handle;
progress_data->data = user_data;
progress_data->data_destroy = user_data_destroy;
procedure = gimp_procedure_new (plug_in,
progress_callback,
GIMP_PDB_PROC_TYPE_TEMPORARY,
gimp_temp_progress_run,
progress_data,
(GDestroyNotify)
gimp_progress_data_free);
gimp_procedure_add_enum_argument (procedure, "command",
"Command",
"The progress command",
GIMP_TYPE_PROGRESS_COMMAND,
GIMP_PROGRESS_COMMAND_START,
G_PARAM_READWRITE);
gimp_procedure_add_string_argument (procedure, "text",
"Text",
"The progress text",
NULL,
G_PARAM_READWRITE);
gimp_procedure_add_double_argument (procedure, "value",
"Value",
"The progress value",
0.0, 1.0, 0.0,
G_PARAM_READWRITE);
gimp_procedure_add_double_return_value (procedure, "value",
"Value",
"The progress value",
0.0, 1.0, 0.0,
G_PARAM_READWRITE);
gimp_plug_in_add_temp_procedure (plug_in, procedure);
g_object_unref (procedure);
if (_gimp_progress_install (progress_callback))
{
/* Allow callbacks to be watched */
gimp_plug_in_persistent_enable (plug_in);
return progress_callback;
}
gimp_plug_in_remove_temp_procedure (plug_in, progress_callback);
return NULL;
}
/**
* gimp_progress_uninstall:
* @progress_callback: the name of the temporary procedure to uninstall
*
* Uninstalls a temporary progress procedure that was installed using
* gimp_progress_install().
*
* Since: 2.2
**/
void
gimp_progress_uninstall (const gchar *progress_callback)
{
GimpPlugIn *plug_in = gimp_get_plug_in ();
g_return_if_fail (progress_callback != NULL);
gimp_plug_in_remove_temp_procedure (plug_in, progress_callback);
}
/**
* gimp_progress_init:
* @message: Message to use in the progress dialog.
*
* Initializes the progress bar for the current plug-in.
*
* Initializes the progress bar for the current plug-in. It is only
* valid to call this procedure from a plug-in.
*
* Returns: TRUE on success.
*/
gboolean
gimp_progress_init (const gchar *message)
{
GimpDisplay *display = gimp_default_display ();
gboolean success;
gimp_progress_current = 0.0;
success = _gimp_progress_init (message, display);
return success;
}
/**
* gimp_progress_init_printf:
* @format: a standard printf() format string
* @...: arguments for @format
*
* Initializes the progress bar for the current plug-in.
*
* Initializes the progress bar for the current plug-in. It is only
* valid to call this procedure from a plug-in.
*
* Returns: %TRUE on success.
*
* Since: 2.4
**/
gboolean
gimp_progress_init_printf (const gchar *format,
...)
{
gchar *text;
gboolean retval;
va_list args;
g_return_val_if_fail (format != NULL, FALSE);
va_start (args, format);
text = g_strdup_vprintf (format, args);
va_end (args);
retval = gimp_progress_init (text);
g_free (text);
return retval;
}
/**
* gimp_progress_set_text_printf:
* @format: a standard printf() format string
* @...: arguments for @format
*
* Changes the text in the progress bar for the current plug-in.
*
* This function changes the text in the progress bar for the current
* plug-in. Unlike gimp_progress_init() it does not change the
* displayed value.
*
* Returns: %TRUE on success.
*
* Since: 2.4
**/
gboolean
gimp_progress_set_text_printf (const gchar *format,
...)
{
gchar *text;
gboolean retval;
va_list args;
g_return_val_if_fail (format != NULL, FALSE);
va_start (args, format);
text = g_strdup_vprintf (format, args);
va_end (args);
retval = gimp_progress_set_text (text);
g_free (text);
return retval;
}
/**
* gimp_progress_update:
* @percentage: Percentage of progress completed (in the range from 0.0
* to 1.0).
*
* Updates the progress bar for the current plug-in.
*
* The library will handle over-updating by possibly dropping silently
* some updates when they happen too close next to each other (either
* time-wise or step-wise).
* The caller does not have to take care of this aspect of progression
* and can focus on computing relevant progression steps.
*
* Returns: TRUE on success.
*/
gboolean
gimp_progress_update (gdouble percentage)
{
static gint64 last_update = 0;
gboolean changed;
if (percentage <= 0.0)
{
changed = (gimp_progress_current != 0.0);
percentage = 0.0;
}
else if (percentage >= 1.0)
{
changed = (gimp_progress_current != 1.0);
percentage = 1.0;
}
else
{
if (last_update == 0 ||
g_get_monotonic_time () - last_update >= gimp_progress_delay)
{
/* If the progression step is too small, better not show it. */
changed =
(fabs (gimp_progress_current - percentage) > gimp_progress_step);
}
else
{
/* Too many changes in a short time interval. */
changed = FALSE;
}
}
/* Suppress the update if the change was only marginal or progression
* update happens too often. This is not an error, it is just
* unneeded to overload the GUI with constant updates.
*/
if (! changed)
return TRUE;
gimp_progress_current = percentage;
last_update = g_get_monotonic_time ();
return _gimp_progress_update (gimp_progress_current);
}
/* private functions */
static void
gimp_progress_data_free (GimpProgressData *data)
{
_gimp_progress_uninstall (data->progress_callback);
g_free (data->progress_callback);
if (data->data_destroy)
data->data_destroy (data->data);
g_slice_free (GimpProgressData, data);
}
static GimpValueArray *
gimp_temp_progress_run (GimpProcedure *procedure,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpProgressData *progress_data = run_data;
GimpProgressCommand command;
gchar *text;
gdouble value;
g_object_get (config,
"command", &command,
"text", &text,
"value", &value,
NULL);
switch (command)
{
case GIMP_PROGRESS_COMMAND_START:
progress_data->vtable.start (text, value != 0.0,
progress_data->data);
break;
case GIMP_PROGRESS_COMMAND_END:
progress_data->vtable.end (progress_data->data);
break;
case GIMP_PROGRESS_COMMAND_SET_TEXT:
progress_data->vtable.set_text (text, progress_data->data);
break;
case GIMP_PROGRESS_COMMAND_SET_VALUE:
progress_data->vtable.set_value (value, progress_data->data);
break;
case GIMP_PROGRESS_COMMAND_PULSE:
if (progress_data->vtable.pulse)
progress_data->vtable.pulse (progress_data->data);
else
progress_data->vtable.set_value (-1, progress_data->data);
break;
case GIMP_PROGRESS_COMMAND_GET_WINDOW:
{
GimpValueArray *return_vals;
GBytes *window_handle = NULL;
if (progress_data->vtable.get_window_handle)
window_handle = progress_data->vtable.get_window_handle (progress_data->data);
return_vals = gimp_procedure_new_return_values (procedure,
GIMP_PDB_SUCCESS,
NULL);
GIMP_VALUES_SET_BYTES (return_vals, 1, window_handle);
g_bytes_unref (window_handle);
g_free (text);
return return_vals;
}
break;
default:
g_free (text);
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CALLING_ERROR,
NULL);
}
g_free (text);
return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
}
|