gimpmessagebox.c 13.9 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpmessagebox.c
 * Copyright (C) 2004 Sven Neumann <sven@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 "libgimpbase/gimpbase.h"
27 28 29 30 31 32
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "gimpmessagebox.h"

33 34
#include "gimp-intl.h"

35 36 37 38 39 40 41 42 43 44

#define GIMP_MESSAGE_BOX_SPACING  12

enum
{
  PROP_0,
  PROP_STOCK_ID
};


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
static void   gimp_message_box_constructed      (GObject        *object);
static void   gimp_message_box_dispose          (GObject        *object);
static void   gimp_message_box_finalize         (GObject        *object);
static void   gimp_message_box_set_property     (GObject        *object,
                                                 guint           property_id,
                                                 const GValue   *value,
                                                 GParamSpec     *pspec);
static void   gimp_message_box_get_property     (GObject        *object,
                                                 guint           property_id,
                                                 GValue         *value,
                                                 GParamSpec     *pspec);

static void   gimp_message_box_forall           (GtkContainer   *container,
                                                 gboolean        include_internals,
                                                 GtkCallback     callback,
                                                 gpointer        callback_data);

static void   gimp_message_box_size_request     (GtkWidget      *widget,
                                                 GtkRequisition *requisition);
static void   gimp_message_box_size_allocate    (GtkWidget      *widget,
                                                 GtkAllocation  *allocation);

static void   gimp_message_box_set_label_text   (GimpMessageBox *box,
                                                 gint            n,
                                                 const gchar    *format,
                                                 va_list         args) G_GNUC_PRINTF (3, 0);
static void   gimp_message_box_set_label_markup (GimpMessageBox *box,
                                                 gint            n,
                                                 const gchar    *format,
                                                 va_list         args) G_GNUC_PRINTF (3, 0);
75 76


77
G_DEFINE_TYPE (GimpMessageBox, gimp_message_box, GTK_TYPE_BOX)
78

79
#define parent_class gimp_message_box_parent_class
80 81 82 83 84


static void
gimp_message_box_class_init (GimpMessageBoxClass *klass)
{
85 86 87
  GObjectClass      *object_class    = G_OBJECT_CLASS (klass);
  GtkWidgetClass    *widget_class    = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
88

89
  object_class->constructed   = gimp_message_box_constructed;
90 91
  object_class->dispose       = gimp_message_box_dispose;
  object_class->finalize      = gimp_message_box_finalize;
92 93 94 95 96 97 98 99 100 101 102 103
  object_class->set_property  = gimp_message_box_set_property;
  object_class->get_property  = gimp_message_box_get_property;


  widget_class->size_request  = gimp_message_box_size_request;
  widget_class->size_allocate = gimp_message_box_size_allocate;

  container_class->forall     = gimp_message_box_forall;

  g_object_class_install_property (object_class, PROP_STOCK_ID,
                                   g_param_spec_string ("stock-id", NULL, NULL,
                                                        NULL,
104
                                                        GIMP_PARAM_READWRITE |
105 106 107 108 109 110
                                                        G_PARAM_CONSTRUCT_ONLY));
}

static void
gimp_message_box_init (GimpMessageBox *box)
{
111 112
  gint i;

113 114 115
  gtk_orientable_set_orientation (GTK_ORIENTABLE (box),
                                  GTK_ORIENTATION_VERTICAL);

116
  gtk_box_set_spacing (GTK_BOX (box), 12);
117
  gtk_container_set_border_width (GTK_CONTAINER (box), 12);
118

119 120
  /*  Unset the focus chain to keep the labels from being in the focus
   *  chain.  Users of GimpMessageBox that add focusable widgets should
121
   *  either unset the focus chain or (better) explicitly set one.
122 123 124
   */
  gtk_container_set_focus_chain (GTK_CONTAINER (box), NULL);

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  for (i = 0; i < 2; i++)
    {
      GtkWidget *label = g_object_new (GTK_TYPE_LABEL,
                                       "wrap",       TRUE,
                                       "selectable", TRUE,
                                       "xalign",     0.0,
                                       "yalign",     0.5,
                                       NULL);

      if (i == 0)
        gimp_label_set_attributes (GTK_LABEL (label),
                                   PANGO_ATTR_SCALE,  PANGO_SCALE_LARGE,
                                   PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD,
                                   -1);

      gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);

      box->label[i] = label;
    }
144 145 146

  box->repeat   = 0;
  box->label[2] = NULL;
147 148
}

149 150
static void
gimp_message_box_constructed (GObject *object)
151
{
152
  GimpMessageBox *box = GIMP_MESSAGE_BOX (object);
153

154
  G_OBJECT_CLASS (parent_class)->constructed (object);
155 156 157 158 159 160 161 162

  if (box->stock_id)
    {
      gtk_widget_push_composite_child ();
      box->image = gtk_image_new_from_stock (box->stock_id,
                                             GTK_ICON_SIZE_DIALOG);
      gtk_widget_pop_composite_child ();

Sven Neumann's avatar
Sven Neumann committed
163
      gtk_misc_set_alignment (GTK_MISC (box->image), 0.0, 0.0);
164 165 166 167 168
      gtk_widget_set_parent (box->image, GTK_WIDGET (box));
      gtk_widget_show (box->image);
    }
}

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
static void
gimp_message_box_dispose (GObject *object)
{
  GimpMessageBox *box = GIMP_MESSAGE_BOX (object);

  if (box->image)
    {
      gtk_widget_unparent (box->image);
      box->image = NULL;
    }

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gimp_message_box_finalize (GObject *object)
{
  GimpMessageBox *box = GIMP_MESSAGE_BOX (object);

  if (box->stock_id)
    {
      g_free (box->stock_id);
      box->stock_id = NULL;
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

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
static void
gimp_message_box_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GimpMessageBox *box = GIMP_MESSAGE_BOX (object);

  switch (property_id)
    {
    case PROP_STOCK_ID:
      box->stock_id = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_message_box_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GimpMessageBox *box = GIMP_MESSAGE_BOX (object);

  switch (property_id)
    {
    case PROP_STOCK_ID:
      g_value_set_string (value, box->stock_id);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_message_box_size_request (GtkWidget      *widget,
                               GtkRequisition *requisition)
{
  GimpMessageBox *box = GIMP_MESSAGE_BOX (widget);

  GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);

243
  if (box->image && gtk_widget_get_visible (box->image))
244 245
    {
      GtkRequisition  child_requisition;
246
      gint            border_width;
247 248 249

      gtk_widget_size_request (box->image, &child_requisition);

250 251
      border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

252 253 254
      requisition->width  += child_requisition.width + GIMP_MESSAGE_BOX_SPACING;
      requisition->height = MAX (requisition->height,
                                 child_requisition.height +
255
                                 2 * border_width);
256 257 258 259 260 261 262 263 264 265
    }
}

static void
gimp_message_box_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
{
  GimpMessageBox *box       = GIMP_MESSAGE_BOX (widget);
  GtkContainer   *container = GTK_CONTAINER (widget);
  gint            width     = 0;
266 267 268
  gboolean        rtl;

  rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
269

270
  if (box->image && gtk_widget_get_visible (box->image))
271 272 273
    {
      GtkRequisition  child_requisition;
      GtkAllocation   child_allocation;
274
      gint            border_width;
275 276 277 278
      gint            height;

      gtk_widget_size_request (box->image, &child_requisition);

279 280 281
      border_width = gtk_container_get_border_width (container);

      width  = MIN (allocation->width - 2 * border_width,
282
                    child_requisition.width + GIMP_MESSAGE_BOX_SPACING);
283 284
      width  = MAX (1, width);

285
      height = allocation->height - 2 * border_width;
286 287
      height = MAX (1, height);

288
      if (rtl)
289 290
        child_allocation.x  = (allocation->width -
                               border_width      -
291 292
                               child_requisition.width);
      else
293
        child_allocation.x  = allocation->x + border_width;
294

295
      child_allocation.y      = allocation->y + border_width;
296 297 298 299 300 301
      child_allocation.width  = width;
      child_allocation.height = height;

      gtk_widget_size_allocate (box->image, &child_allocation);
    }

302 303
  allocation->x     += rtl ? 0 : width;
  allocation->width -= width;
304 305 306

  GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);

307
  allocation->x      -= rtl ? 0 : width;
308 309
  allocation->width  += width;

310
  gtk_widget_set_allocation (widget, allocation);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
}

static void
gimp_message_box_forall (GtkContainer *container,
                         gboolean      include_internals,
                         GtkCallback   callback,
                         gpointer      callback_data)
{
  if (include_internals)
    {
      GimpMessageBox *box = GIMP_MESSAGE_BOX (container);

      if (box->image)
        (* callback) (box->image, callback_data);
    }

  GTK_CONTAINER_CLASS (parent_class)->forall (container, include_internals,
                                              callback, callback_data);
}

331 332 333 334 335 336 337 338 339 340
static void
gimp_message_box_set_label_text (GimpMessageBox *box,
                                 gint            n,
                                 const gchar    *format,
                                 va_list         args)
{
  GtkWidget *label = box->label[n];

  if (format)
    {
Sven Neumann's avatar
Sven Neumann committed
341 342
      gchar *text = g_strdup_vprintf (format, args);
      gchar *utf8 = gimp_any_to_utf8 (text, -1, "Cannot convert text to utf8.");
343

Sven Neumann's avatar
Sven Neumann committed
344
      gtk_label_set_text (GTK_LABEL (label), utf8);
345 346
      gtk_widget_show (label);

Sven Neumann's avatar
Sven Neumann committed
347
      g_free (utf8);
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
      g_free (text);
    }
  else
    {
      gtk_widget_hide (label);
      gtk_label_set_text (GTK_LABEL (label), NULL);
    }
}

static void
gimp_message_box_set_label_markup (GimpMessageBox *box,
                                   gint            n,
                                   const gchar    *format,
                                   va_list         args)
{
  GtkWidget *label = box->label[n];

  if (format)
    {
      gchar *text = g_markup_vprintf_escaped (format, args);

      gtk_label_set_markup (GTK_LABEL (label), text);
      gtk_widget_show (label);

      g_free (text);
    }
  else
    {
      gtk_widget_hide (label);
      gtk_label_set_text (GTK_LABEL (label), NULL);
    }
}

381 382 383
/*  public functions  */

GtkWidget *
384
gimp_message_box_new (const gchar *stock_id)
385 386
{
  return g_object_new (GIMP_TYPE_MESSAGE_BOX,
387
                       "stock-id",  stock_id,
388 389
                       NULL);
}
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

void
gimp_message_box_set_primary_text (GimpMessageBox *box,
                                   const gchar    *format,
                                   ...)
{
  va_list args;

  g_return_if_fail (GIMP_IS_MESSAGE_BOX (box));

  va_start (args, format);
  gimp_message_box_set_label_text (box, 0, format, args);
  va_end (args);
}

void
gimp_message_box_set_text (GimpMessageBox *box,
                           const gchar    *format,
                           ...)
{
  va_list args;

  g_return_if_fail (GIMP_IS_MESSAGE_BOX (box));

  va_start (args, format);
  gimp_message_box_set_label_text (box, 1, format, args);
  va_end (args);
}

void
gimp_message_box_set_markup (GimpMessageBox *box,
                             const gchar    *format,
                             ...)
{
  va_list args;

  g_return_if_fail (GIMP_IS_MESSAGE_BOX (box));

  va_start (args, format);
  gimp_message_box_set_label_markup (box, 1,format, args);
  va_end (args);
}
432 433 434 435 436 437 438 439 440 441

gint
gimp_message_box_repeat (GimpMessageBox *box)
{
  gchar *message;

  g_return_val_if_fail (GIMP_IS_MESSAGE_BOX (box), 0);

  box->repeat++;

442 443 444 445
  message = g_strdup_printf (ngettext ("Message repeated once.",
                                       "Message repeated %d times.",
                                       box->repeat),
                             box->repeat);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

  if (box->label[2])
    {
      gtk_label_set_text (GTK_LABEL (box->label[2]), message);
    }
  else
    {
      GtkWidget *label = box->label[2] = gtk_label_new (message);

      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 1.0);
      gimp_label_set_attributes (GTK_LABEL (label),
                                 PANGO_ATTR_STYLE, PANGO_STYLE_OBLIQUE,
                                 -1);
      gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
      gtk_widget_show (label);
    }

  g_free (message);

  return box->repeat;
}