PIKApp/app/core/pikaprogress.c

269 lines
6.4 KiB
C
Raw Normal View History

2023-09-26 00:35:21 +02:00
/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* pikaprogress.c
* Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gio/gio.h>
#include <gegl.h>
#include "core-types.h"
#include "pika.h"
#include "pikaprogress.h"
#include "pika-intl.h"
enum
{
CANCEL,
LAST_SIGNAL
};
G_DEFINE_INTERFACE (PikaProgress, pika_progress, G_TYPE_OBJECT)
static guint progress_signals[LAST_SIGNAL] = { 0 };
/* private functions */
static void
pika_progress_default_init (PikaProgressInterface *progress_iface)
{
progress_signals[CANCEL] =
g_signal_new ("cancel",
G_TYPE_FROM_INTERFACE (progress_iface),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (PikaProgressInterface, cancel),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
}
/* public functions */
PikaProgress *
pika_progress_start (PikaProgress *progress,
gboolean cancellable,
const gchar *format,
...)
{
PikaProgressInterface *progress_iface;
g_return_val_if_fail (PIKA_IS_PROGRESS (progress), NULL);
g_return_val_if_fail (format != NULL, NULL);
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->start)
{
PikaProgress *ret;
va_list args;
gchar *text;
va_start (args, format);
text = g_strdup_vprintf (format, args);
va_end (args);
ret = progress_iface->start (progress, cancellable, text);
g_free (text);
return ret;
}
return NULL;
}
void
pika_progress_end (PikaProgress *progress)
{
PikaProgressInterface *progress_iface;
g_return_if_fail (PIKA_IS_PROGRESS (progress));
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->end)
progress_iface->end (progress);
}
gboolean
pika_progress_is_active (PikaProgress *progress)
{
PikaProgressInterface *progress_iface;
g_return_val_if_fail (PIKA_IS_PROGRESS (progress), FALSE);
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->is_active)
return progress_iface->is_active (progress);
return FALSE;
}
void
pika_progress_set_text (PikaProgress *progress,
const gchar *format,
...)
{
va_list args;
gchar *message;
g_return_if_fail (PIKA_IS_PROGRESS (progress));
g_return_if_fail (format != NULL);
va_start (args, format);
message = g_strdup_vprintf (format, args);
va_end (args);
pika_progress_set_text_literal (progress, message);
g_free (message);
}
void
pika_progress_set_text_literal (PikaProgress *progress,
const gchar *message)
{
PikaProgressInterface *progress_iface;
g_return_if_fail (PIKA_IS_PROGRESS (progress));
g_return_if_fail (message != NULL);
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->set_text)
progress_iface->set_text (progress, message);
}
void
pika_progress_set_value (PikaProgress *progress,
gdouble percentage)
{
PikaProgressInterface *progress_iface;
g_return_if_fail (PIKA_IS_PROGRESS (progress));
percentage = CLAMP (percentage, 0.0, 1.0);
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->set_value)
progress_iface->set_value (progress, percentage);
}
gdouble
pika_progress_get_value (PikaProgress *progress)
{
PikaProgressInterface *progress_iface;
g_return_val_if_fail (PIKA_IS_PROGRESS (progress), 0.0);
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->get_value)
return progress_iface->get_value (progress);
return 0.0;
}
void
pika_progress_pulse (PikaProgress *progress)
{
PikaProgressInterface *progress_iface;
g_return_if_fail (PIKA_IS_PROGRESS (progress));
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->pulse)
progress_iface->pulse (progress);
}
guint32
pika_progress_get_window_id (PikaProgress *progress)
{
PikaProgressInterface *progress_iface;
g_return_val_if_fail (PIKA_IS_PROGRESS (progress), 0);
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->get_window_id)
return progress_iface->get_window_id (progress);
return 0;
}
gboolean
pika_progress_message (PikaProgress *progress,
Pika *pika,
PikaMessageSeverity severity,
const gchar *domain,
const gchar *message)
{
PikaProgressInterface *progress_iface;
g_return_val_if_fail (PIKA_IS_PROGRESS (progress), FALSE);
g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
g_return_val_if_fail (domain != NULL, FALSE);
g_return_val_if_fail (message != NULL, FALSE);
progress_iface = PIKA_PROGRESS_GET_IFACE (progress);
if (progress_iface->message)
return progress_iface->message (progress, pika, severity, domain, message);
return FALSE;
}
void
pika_progress_cancel (PikaProgress *progress)
{
g_return_if_fail (PIKA_IS_PROGRESS (progress));
g_signal_emit (progress, progress_signals[CANCEL], 0);
}
void
pika_progress_update_and_flush (gint min,
gint max,
gint current,
gpointer data)
{
pika_progress_set_value (PIKA_PROGRESS (data),
(gdouble) (current - min) / (gdouble) (max - min));
while (g_main_context_pending (NULL))
g_main_context_iteration (NULL, TRUE);
}