PIKApp/libpika/pikaprogressbar.c

230 lines
6.6 KiB
C
Raw Normal View History

2023-09-26 00:35:21 +02:00
/* LIBPIKA - The PIKA Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* pikaprogressbar.c
* Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
*
* 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
* Lesser 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"
2023-10-30 23:55:30 +01:00
#include <gegl.h>
2023-09-26 00:35:21 +02:00
#include <gtk/gtk.h>
#ifdef GDK_WINDOWING_WIN32
#include <gdk/gdkwin32.h>
#endif
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/gdkwayland.h>
#endif
2023-10-30 23:55:30 +01:00
#include "libpikawidgets/pikawidgets.h"
2023-09-26 00:35:21 +02:00
#include "pikauitypes.h"
#include "pika.h"
#include "pikaprogressbar.h"
/**
* SECTION: pikaprogressbar
* @title: PikaProgressBar
* @short_description: A widget providing a progress bar.
*
* A widget providing a progress bar that automatically redirects any
* progress calls to itself.
**/
2023-10-30 23:55:30 +01:00
typedef struct _PikaProgressBarPrivate
{
GBytes *window_handle;
} PikaProgressBarPrivate;
static void pika_progress_bar_dispose (GObject *object);
2023-09-26 00:35:21 +02:00
2023-10-30 23:55:30 +01:00
static void pika_progress_bar_start (const gchar *message,
gboolean cancelable,
gpointer user_data);
static void pika_progress_bar_end (gpointer user_data);
static void pika_progress_bar_set_text (const gchar *message,
gpointer user_data);
static void pika_progress_bar_set_value (gdouble percentage,
gpointer user_data);
static void pika_progress_bar_pulse (gpointer user_data);
static GBytes * pika_progress_bar_get_window_handle (gpointer user_data);
2023-09-26 00:35:21 +02:00
2023-10-30 23:55:30 +01:00
G_DEFINE_TYPE_WITH_PRIVATE (PikaProgressBar, pika_progress_bar, GTK_TYPE_PROGRESS_BAR)
2023-09-26 00:35:21 +02:00
#define parent_class pika_progress_bar_parent_class
static void
pika_progress_bar_class_init (PikaProgressBarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = pika_progress_bar_dispose;
}
static void
pika_progress_bar_init (PikaProgressBar *bar)
{
2023-10-30 23:55:30 +01:00
PikaProgressVtable vtable = { 0, };
PikaProgressBarPrivate *priv = pika_progress_bar_get_instance_private (bar);
2023-09-26 00:35:21 +02:00
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (bar), " ");
gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (bar), PANGO_ELLIPSIZE_END);
2023-10-30 23:55:30 +01:00
vtable.start = pika_progress_bar_start;
vtable.end = pika_progress_bar_end;
vtable.set_text = pika_progress_bar_set_text;
vtable.set_value = pika_progress_bar_set_value;
vtable.pulse = pika_progress_bar_pulse;
vtable.get_window_handle = pika_progress_bar_get_window_handle;
2023-09-26 00:35:21 +02:00
bar->progress_callback = pika_progress_install_vtable (&vtable, bar, NULL);
2023-10-30 23:55:30 +01:00
pika_widget_set_native_handle (GTK_WIDGET (bar), &priv->window_handle);
2023-09-26 00:35:21 +02:00
}
static void
pika_progress_bar_dispose (GObject *object)
{
2023-10-30 23:55:30 +01:00
PikaProgressBar *bar = PIKA_PROGRESS_BAR (object);
PikaProgressBarPrivate *priv = pika_progress_bar_get_instance_private (bar);
2023-09-26 00:35:21 +02:00
if (bar->progress_callback)
{
pika_progress_uninstall (bar->progress_callback);
bar->progress_callback = NULL;
}
2023-10-30 23:55:30 +01:00
if (priv->window_handle != NULL)
{
#ifdef GDK_WINDOWING_WAYLAND
if (GDK_IS_WAYLAND_DISPLAY (gdk_display_get_default ()) &&
/* The GdkWindow is likely already destroyed. */
gtk_widget_get_window (GTK_WIDGET (bar)) != NULL)
gdk_wayland_window_unexport_handle (gtk_widget_get_window (GTK_WIDGET (bar)));
#endif
g_clear_pointer (&priv->window_handle, g_bytes_unref);
}
2023-09-26 00:35:21 +02:00
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
pika_progress_bar_start (const gchar *message,
gboolean cancelable,
gpointer user_data)
{
PikaProgressBar *bar = PIKA_PROGRESS_BAR (user_data);
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (bar), message ? message : " ");
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), 0.0);
if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
while (gtk_events_pending ())
gtk_main_iteration ();
}
static void
pika_progress_bar_end (gpointer user_data)
{
PikaProgressBar *bar = PIKA_PROGRESS_BAR (user_data);
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (bar), " ");
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), 0.0);
if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
while (gtk_events_pending ())
gtk_main_iteration ();
}
static void
pika_progress_bar_set_text (const gchar *message,
gpointer user_data)
{
PikaProgressBar *bar = PIKA_PROGRESS_BAR (user_data);
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (bar), message ? message : " ");
if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
while (gtk_events_pending ())
gtk_main_iteration ();
}
static void
pika_progress_bar_set_value (gdouble percentage,
gpointer user_data)
{
PikaProgressBar *bar = PIKA_PROGRESS_BAR (user_data);
if (percentage >= 0.0)
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), percentage);
else
gtk_progress_bar_pulse (GTK_PROGRESS_BAR (bar));
if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
while (gtk_events_pending ())
gtk_main_iteration ();
}
static void
pika_progress_bar_pulse (gpointer user_data)
{
PikaProgressBar *bar = PIKA_PROGRESS_BAR (user_data);
gtk_progress_bar_pulse (GTK_PROGRESS_BAR (bar));
if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
while (gtk_events_pending ())
gtk_main_iteration ();
}
2023-10-30 23:55:30 +01:00
static GBytes *
pika_progress_bar_get_window_handle (gpointer user_data)
2023-09-26 00:35:21 +02:00
{
2023-10-30 23:55:30 +01:00
PikaProgressBar *bar = PIKA_PROGRESS_BAR (user_data);
PikaProgressBarPrivate *priv = pika_progress_bar_get_instance_private (bar);
2023-09-26 00:35:21 +02:00
2023-10-30 23:55:30 +01:00
return g_bytes_ref (priv->window_handle);
2023-09-26 00:35:21 +02:00
}
/**
* pika_progress_bar_new:
*
* Creates a new #PikaProgressBar widget.
*
* Returns: the new widget.
*
* Since: 2.2
**/
GtkWidget *
pika_progress_bar_new (void)
{
return g_object_new (PIKA_TYPE_PROGRESS_BAR, NULL);
}