gimpprogressbox.c 7.5 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpprogressbox.c
 * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
 *
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 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23
#include <gegl.h>
24 25
#include <gtk/gtk.h>

26
#include "libgimpmath/gimpmath.h"
27 28 29 30 31 32 33
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimpprogress.h"

#include "gimpprogressbox.h"
34
#include "gimpwidgets-utils.h"
35 36 37 38

#include "gimp-intl.h"


39
static void     gimp_progress_box_progress_iface_init (GimpProgressInterface *iface);
40

41
static void     gimp_progress_box_dispose            (GObject      *object);
42

43
static GimpProgress *
44
                gimp_progress_box_progress_start     (GimpProgress *progress,
45 46
                                                      gboolean      cancellable,
                                                      const gchar  *message);
47 48 49 50 51 52 53
static void     gimp_progress_box_progress_end       (GimpProgress *progress);
static gboolean gimp_progress_box_progress_is_active (GimpProgress *progress);
static void     gimp_progress_box_progress_set_text  (GimpProgress *progress,
                                                      const gchar  *message);
static void     gimp_progress_box_progress_set_value (GimpProgress *progress,
                                                      gdouble       percentage);
static gdouble  gimp_progress_box_progress_get_value (GimpProgress *progress);
54
static void     gimp_progress_box_progress_pulse     (GimpProgress *progress);
55 56


57
G_DEFINE_TYPE_WITH_CODE (GimpProgressBox, gimp_progress_box, GTK_TYPE_BOX,
58
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROGRESS,
59
                                                gimp_progress_box_progress_iface_init))
60

61 62
#define parent_class gimp_progress_box_parent_class

63 64 65 66

static void
gimp_progress_box_class_init (GimpProgressBoxClass *klass)
{
67
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
68

69
  object_class->dispose = gimp_progress_box_dispose;
70 71 72 73
}

static void
gimp_progress_box_init (GimpProgressBox *box)
74
{
75 76 77
  gtk_orientable_set_orientation (GTK_ORIENTABLE (box),
                                  GTK_ORIENTATION_VERTICAL);

Sven Neumann's avatar
Sven Neumann committed
78 79 80 81 82 83 84 85 86
  gtk_box_set_spacing (GTK_BOX (box), 6);

  box->progress = gtk_progress_bar_new ();
  gtk_widget_set_size_request (box->progress, 250, 20);
  gtk_box_pack_start (GTK_BOX (box), box->progress, FALSE, FALSE, 0);
  gtk_widget_show (box->progress);

  box->label = gtk_label_new ("");
  gtk_label_set_ellipsize (GTK_LABEL (box->label), PANGO_ELLIPSIZE_MIDDLE);
87
  gtk_label_set_xalign (GTK_LABEL (box->label), 0.0);
Sven Neumann's avatar
Sven Neumann committed
88 89 90 91 92
  gimp_label_set_attributes (GTK_LABEL (box->label),
                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
                             -1);
  gtk_box_pack_start (GTK_BOX (box), box->label, FALSE, FALSE, 0);
  gtk_widget_show (box->label);
93 94 95
}

static void
96
gimp_progress_box_progress_iface_init (GimpProgressInterface *iface)
97
{
98 99 100 101 102 103 104
  iface->start     = gimp_progress_box_progress_start;
  iface->end       = gimp_progress_box_progress_end;
  iface->is_active = gimp_progress_box_progress_is_active;
  iface->set_text  = gimp_progress_box_progress_set_text;
  iface->set_value = gimp_progress_box_progress_set_value;
  iface->get_value = gimp_progress_box_progress_get_value;
  iface->pulse     = gimp_progress_box_progress_pulse;
105 106
}

107
static void
108
gimp_progress_box_dispose (GObject *object)
109 110 111
{
  GimpProgressBox *box = GIMP_PROGRESS_BOX (object);

112
  G_OBJECT_CLASS (parent_class)->dispose (object);
113 114 115 116

  box->progress = NULL;
}

117 118
static GimpProgress *
gimp_progress_box_progress_start (GimpProgress *progress,
119 120
                                  gboolean      cancellable,
                                  const gchar  *message)
121 122 123
{
  GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);

124 125 126
  if (! box->progress)
    return NULL;

127 128 129 130 131 132 133
  if (! box->active)
    {
      GtkProgressBar *bar = GTK_PROGRESS_BAR (box->progress);

      gtk_label_set_text (GTK_LABEL (box->label), message);
      gtk_progress_bar_set_fraction (bar, 0.0);

134 135 136
      box->active      = TRUE;
      box->cancellable = cancellable;
      box->value       = 0.0;
137

138
      if (gtk_widget_is_drawable (box->progress))
139 140
        gdk_window_process_updates (gtk_widget_get_window (box->progress),
                                    TRUE);
Sven Neumann's avatar
Sven Neumann committed
141

142 143 144 145 146 147 148 149 150
      return progress;
    }

  return NULL;
}

static void
gimp_progress_box_progress_end (GimpProgress *progress)
{
151
  if (gimp_progress_box_progress_is_active (progress))
152
    {
153 154
      GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
      GtkProgressBar  *bar = GTK_PROGRESS_BAR (box->progress);
155 156 157 158

      gtk_label_set_text (GTK_LABEL (box->label), "");
      gtk_progress_bar_set_fraction (bar, 0.0);

159 160 161
      box->active      = FALSE;
      box->cancellable = FALSE;
      box->value       = 0.0;
162 163 164
    }
}

165 166 167 168 169
static gboolean
gimp_progress_box_progress_is_active (GimpProgress *progress)
{
  GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);

170
  return (box->progress && box->active);
171 172
}

173 174 175 176
static void
gimp_progress_box_progress_set_text (GimpProgress *progress,
                                     const gchar  *message)
{
177
  if (gimp_progress_box_progress_is_active (progress))
178
    {
179 180
      GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);

181
      gtk_label_set_text (GTK_LABEL (box->label), message);
Sven Neumann's avatar
Sven Neumann committed
182

183
      if (gtk_widget_is_drawable (box->progress))
184 185
        gdk_window_process_updates (gtk_widget_get_window (box->progress),
                                    TRUE);
186 187 188 189 190 191 192
    }
}

static void
gimp_progress_box_progress_set_value (GimpProgress *progress,
                                      gdouble       percentage)
{
193
  if (gimp_progress_box_progress_is_active (progress))
194
    {
195 196
      GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
      GtkProgressBar  *bar = GTK_PROGRESS_BAR (box->progress);
197 198 199
      GtkAllocation    allocation;

      gtk_widget_get_allocation (GTK_WIDGET (bar), &allocation);
200

201
      box->value = percentage;
202

203
      /* only update the progress bar if this causes a visible change */
204
      if (fabs (allocation.width *
205 206 207 208
                (percentage - gtk_progress_bar_get_fraction (bar))) > 1.0)
        {
          gtk_progress_bar_set_fraction (bar, box->value);

209
          gimp_widget_flush_expose (box->progress);
210
        }
211 212 213 214 215 216
    }
}

static gdouble
gimp_progress_box_progress_get_value (GimpProgress *progress)
{
217 218 219 220
  if (gimp_progress_box_progress_is_active (progress))
    {
      return GIMP_PROGRESS_BOX (progress)->value;
    }
221 222 223 224

  return 0.0;
}

225 226 227
static void
gimp_progress_box_progress_pulse (GimpProgress *progress)
{
228
  if (gimp_progress_box_progress_is_active (progress))
229
    {
230 231
      GimpProgressBox *box = GIMP_PROGRESS_BOX (progress);
      GtkProgressBar  *bar = GTK_PROGRESS_BAR (box->progress);
232 233 234

      gtk_progress_bar_pulse (bar);

235
      if (gtk_widget_is_drawable (box->progress))
236 237
        gdk_window_process_updates (gtk_widget_get_window (box->progress),
                                    TRUE);
238 239 240
    }
}

241 242 243
GtkWidget *
gimp_progress_box_new (void)
{
Sven Neumann's avatar
Sven Neumann committed
244
  return g_object_new (GIMP_TYPE_PROGRESS_BOX, NULL);
245
}