gimpprogressbar.c 6.25 KB
Newer Older
1 2 3 4 5 6
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpprogressbar.c
 * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16 17
 *
 * 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
18
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21 22 23 24 25
 */

#include "config.h"

#include <gtk/gtk.h>

26 27 28 29 30 31 32 33
#ifdef GDK_WINDOWING_WIN32
#include <gdk/gdkwin32.h>
#endif

#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif

34 35 36 37 38 39 40
#include "gimpuitypes.h"

#include "gimp.h"

#include "gimpprogressbar.h"


41 42 43 44 45 46 47 48 49 50
/**
 * SECTION: gimpprogressbar
 * @title: GimpProgressBar
 * @short_description: A widget providing a progress bar.
 *
 * A widget providing a progress bar that automatically redirects any
 * progress calls to itself.
 **/


51
static void     gimp_progress_bar_dispose    (GObject     *object);
52

53 54 55 56 57 58 59 60 61 62
static void     gimp_progress_bar_start      (const gchar *message,
                                              gboolean     cancelable,
                                              gpointer     user_data);
static void     gimp_progress_bar_end        (gpointer     user_data);
static void     gimp_progress_bar_set_text   (const gchar *message,
                                              gpointer     user_data);
static void     gimp_progress_bar_set_value  (gdouble      percentage,
                                              gpointer     user_data);
static void     gimp_progress_bar_pulse      (gpointer     user_data);
static guint32  gimp_progress_bar_get_window (gpointer     user_data);
63 64


65
G_DEFINE_TYPE (GimpProgressBar, gimp_progress_bar, GTK_TYPE_PROGRESS_BAR)
66

67
#define parent_class gimp_progress_bar_parent_class
68 69 70 71 72


static void
gimp_progress_bar_class_init (GimpProgressBarClass *klass)
{
73
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
74

75
  object_class->dispose = gimp_progress_bar_dispose;
76 77 78 79 80
}

static void
gimp_progress_bar_init (GimpProgressBar *bar)
{
81 82
  GimpProgressVtable vtable = { 0, };

83
  gtk_progress_bar_set_text (GTK_PROGRESS_BAR (bar), " ");
Sven Neumann's avatar
Sven Neumann committed
84
  gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (bar), PANGO_ELLIPSIZE_END);
85

86 87 88 89 90 91 92 93
  vtable.start      = gimp_progress_bar_start;
  vtable.end        = gimp_progress_bar_end;
  vtable.set_text   = gimp_progress_bar_set_text;
  vtable.set_value  = gimp_progress_bar_set_value;
  vtable.pulse      = gimp_progress_bar_pulse;
  vtable.get_window = gimp_progress_bar_get_window;

  bar->progress_callback = gimp_progress_install_vtable (&vtable, bar);
94 95 96
}

static void
97
gimp_progress_bar_dispose (GObject *object)
98 99 100 101 102 103 104 105 106
{
  GimpProgressBar *bar = GIMP_PROGRESS_BAR (object);

  if (bar->progress_callback)
    {
      gimp_progress_uninstall (bar->progress_callback);
      bar->progress_callback = NULL;
    }

107
  G_OBJECT_CLASS (parent_class)->dispose (object);
108 109 110 111 112 113 114 115 116 117 118 119
}

static void
gimp_progress_bar_start (const gchar *message,
                         gboolean     cancelable,
                         gpointer     user_data)
{
  GimpProgressBar *bar = GIMP_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);

120
  if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
121 122
    while (gtk_events_pending ())
      gtk_main_iteration ();
123 124 125 126 127 128 129 130 131 132
}

static void
gimp_progress_bar_end (gpointer user_data)
{
  GimpProgressBar *bar = GIMP_PROGRESS_BAR (user_data);

  gtk_progress_bar_set_text (GTK_PROGRESS_BAR (bar), " ");
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), 0.0);

133
  if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
134 135
    while (gtk_events_pending ())
      gtk_main_iteration ();
136 137 138 139 140 141 142 143 144 145
}

static void
gimp_progress_bar_set_text (const gchar *message,
                            gpointer     user_data)
{
  GimpProgressBar *bar = GIMP_PROGRESS_BAR (user_data);

  gtk_progress_bar_set_text (GTK_PROGRESS_BAR (bar), message ? message : " ");

146
  if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
147 148
    while (gtk_events_pending ())
      gtk_main_iteration ();
149 150 151 152 153 154 155 156
}

static void
gimp_progress_bar_set_value (gdouble  percentage,
                             gpointer user_data)
{
  GimpProgressBar *bar = GIMP_PROGRESS_BAR (user_data);

157 158 159 160
  if (percentage >= 0.0)
    gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), percentage);
  else
    gtk_progress_bar_pulse (GTK_PROGRESS_BAR (bar));
161

162
  if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
163 164
    while (gtk_events_pending ())
      gtk_main_iteration ();
165 166
}

167 168 169 170 171 172 173
static void
gimp_progress_bar_pulse (gpointer user_data)
{
  GimpProgressBar *bar = GIMP_PROGRESS_BAR (user_data);

  gtk_progress_bar_pulse (GTK_PROGRESS_BAR (bar));

174
  if (gtk_widget_is_drawable (GTK_WIDGET (bar)))
175 176
    while (gtk_events_pending ())
      gtk_main_iteration ();
177 178
}

179 180
static guint32
gimp_window_get_native_id (GtkWindow *window)
181 182 183 184 185 186 187 188 189 190
{
  g_return_val_if_fail (GTK_IS_WINDOW (window), 0);

#ifdef GDK_NATIVE_WINDOW_POINTER
#ifdef __GNUC__
#warning gimp_window_get_native() unimplementable for the target windowing system
#endif
#endif

#ifdef GDK_WINDOWING_WIN32
191
  if (window && gtk_widget_get_realized (GTK_WIDGET (window)))
192
    return GDK_WINDOW_HWND (gtk_widget_get_window (GTK_WIDGET (window)));
193 194 195
#endif

#ifdef GDK_WINDOWING_X11
196
  if (window && gtk_widget_get_realized (GTK_WIDGET (window)))
197
    return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
198 199 200 201 202 203 204 205 206 207 208 209 210 211
#endif

  return 0;
}

static guint32
gimp_progress_bar_get_window (gpointer user_data)
{
  GimpProgressBar *bar = GIMP_PROGRESS_BAR (user_data);
  GtkWidget       *toplevel;

  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (bar));

  if (GTK_IS_WINDOW (toplevel))
212
    return gimp_window_get_native_id (GTK_WINDOW (toplevel));
213 214 215 216

  return 0;
}

217 218 219 220 221 222 223
/**
 * gimp_progress_bar_new:
 *
 * Creates a new #GimpProgressBar widget.
 *
 * Return value: the new widget.
 *
224
 * Since: 2.2
225 226 227 228 229 230
 **/
GtkWidget *
gimp_progress_bar_new (void)
{
  return g_object_new (GIMP_TYPE_PROGRESS_BAR, NULL);
}