gtkstatusbar.c 14.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * GtkStatusbar Copyright (C) 1998 Shawn T. Amundson
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 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
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18
 */

19
/*
20
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
21 22
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
23
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24 25
 */

26
#include "config.h"
27

28 29
#include "gtkstatusbar.h"

30 31
#include "gtkframe.h"
#include "gtklabel.h"
32
#include "gtkmarshalers.h"
Owen Taylor's avatar
Owen Taylor committed
33
#include "gtkwindow.h"
34
#include "gtkprivate.h"
35
#include "gtkintl.h"
36
#include "gtkorientable.h"
37
#include "gtktypebuiltins.h"
38
#include "a11y/gtkstatusbaraccessible.h"
39

40 41 42 43 44 45 46 47 48 49 50 51
/**
 * SECTION:gtkstatusbar
 * @title: GtkStatusbar
 * @short_description: Report messages of minor importance to the user
 *
 * A #GtkStatusbar is usually placed along the bottom of an application's
 * main #GtkWindow. It may provide a regular commentary of the application's
 * status (as is usually the case in a web browser, for example), or may be
 * used to simply output a message when the status changes, (when an upload
 * is complete in an FTP client, for example).
 *
 * Status bars in GTK+ maintain a stack of messages. The message at
52
 * the top of the each bar’s stack is the one that will currently be displayed.
53
 *
54
 * Any messages added to a statusbar’s stack must specify a
55
 * context id that is used to uniquely identify
56 57 58 59 60 61 62 63 64 65 66 67
 * the source of a message. This context id can be generated by
 * gtk_statusbar_get_context_id(), given a message and the statusbar that
 * it will be added to. Note that messages are stored in a stack, and when
 * choosing which message to display, the stack structure is adhered to,
 * regardless of the context identifier of a message.
 *
 * One could say that a statusbar maintains one stack of messages for
 * display purposes, but allows multiple message producers to maintain
 * sub-stacks of the messages they produced (via context ids).
 *
 * Status bars are created using gtk_statusbar_new().
 *
68
 * Messages are added to the bar’s stack with gtk_statusbar_push().
69 70 71 72 73
 *
 * The message at the top of the stack can be removed using
 * gtk_statusbar_pop(). A message can be removed from anywhere in the
 * stack if its message id was recorded at the time it was added. This
 * is done using gtk_statusbar_remove().
74 75 76 77
 *
 * # CSS node
 *
 * GtkStatusbar has a single CSS node with name statusbar.
78
 */
79

80 81
typedef struct _GtkStatusbarMsg GtkStatusbarMsg;

82
struct _GtkStatusbarPrivate
83 84 85
{
  GtkWidget     *frame;
  GtkWidget     *label;
86
  GtkWidget     *message_area;
87 88 89 90 91 92 93 94 95

  GSList        *messages;
  GSList        *keys;

  guint          seq_context_id;
  guint          seq_message_id;
};


96 97 98 99 100 101
struct _GtkStatusbarMsg
{
  gchar *text;
  guint context_id;
  guint message_id;
};
102 103 104 105 106 107 108 109

enum
{
  SIGNAL_TEXT_PUSHED,
  SIGNAL_TEXT_POPPED,
  SIGNAL_LAST
};

110 111 112
static void     gtk_statusbar_update            (GtkStatusbar      *statusbar,
						 guint              context_id,
						 const gchar       *text);
113
static void     gtk_statusbar_destroy           (GtkWidget         *widget);
Owen Taylor's avatar
Owen Taylor committed
114

115
static guint              statusbar_signals[SIGNAL_LAST] = { 0 };
116

117
G_DEFINE_TYPE_WITH_PRIVATE (GtkStatusbar, gtk_statusbar, GTK_TYPE_BOX)
118 119 120 121

static void
gtk_statusbar_class_init (GtkStatusbarClass *class)
{
122
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
123

124
  widget_class->destroy = gtk_statusbar_destroy;
125

126 127
  class->text_pushed = gtk_statusbar_update;
  class->text_popped = gtk_statusbar_update;
128

129
  /**
130
   * GtkStatusbar::text-pushed:
131 132 133 134
   * @statusbar: the object which received the signal
   * @context_id: the context id of the relevant message/statusbar
   * @text: the message that was pushed
   *
135 136
   * Is emitted whenever a new message gets pushed onto a statusbar's stack.
   */
137
  statusbar_signals[SIGNAL_TEXT_PUSHED] =
138
    g_signal_new (I_("text-pushed"),
Manish Singh's avatar
Manish Singh committed
139 140 141 142 143 144 145 146
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkStatusbarClass, text_pushed),
		  NULL, NULL,
		  _gtk_marshal_VOID__UINT_STRING,
		  G_TYPE_NONE, 2,
		  G_TYPE_UINT,
		  G_TYPE_STRING);
147 148 149

  /**
   * GtkStatusbar::text-popped:
150 151 152
   * @statusbar: the object which received the signal
   * @context_id: the context id of the relevant message/statusbar
   * @text: the message that was just popped
153 154 155
   *
   * Is emitted whenever a new message is popped off a statusbar's stack.
   */
156
  statusbar_signals[SIGNAL_TEXT_POPPED] =
157
    g_signal_new (I_("text-popped"),
Manish Singh's avatar
Manish Singh committed
158 159 160 161 162 163 164 165
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkStatusbarClass, text_popped),
		  NULL, NULL,
		  _gtk_marshal_VOID__UINT_STRING,
		  G_TYPE_NONE, 2,
		  G_TYPE_UINT,
		  G_TYPE_STRING);
166

167 168 169 170 171 172 173 174
  /**
   * GtkStatusbar:shadow-type:
   *
   * The style of the bevel around the statusbar text.
   *
   * Deprecated: 3.20: Use CSS properties to determine the appearance,
   *    the value of this style property is ignored.
   */
175
  gtk_widget_class_install_style_property (widget_class,
176
                                           g_param_spec_enum ("shadow-type",
177 178
                                                              P_("Shadow type"),
                                                              P_("Style of bevel around the statusbar text"),
179 180
                                                              GTK_TYPE_SHADOW_TYPE,
                                                              GTK_SHADOW_IN,
181
                                                              GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
182

183 184
  /* Bind class to template
   */
185
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkstatusbar.ui");
186 187 188
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkStatusbar, message_area);
  gtk_widget_class_bind_template_child_private (widget_class, GtkStatusbar, frame);
  gtk_widget_class_bind_template_child_private (widget_class, GtkStatusbar, label);
189 190

  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_STATUSBAR_ACCESSIBLE);
191
  gtk_widget_class_set_css_name (widget_class, "statusbar");
192 193 194 195 196
}

static void
gtk_statusbar_init (GtkStatusbar *statusbar)
{
197
  GtkStatusbarPrivate *priv;
198

199
  statusbar->priv = gtk_statusbar_get_instance_private (statusbar);
200
  priv = statusbar->priv;
201

202 203 204 205
  priv->seq_context_id = 1;
  priv->seq_message_id = 1;
  priv->messages = NULL;
  priv->keys = NULL;
206

207 208
  gtk_widget_init_template (GTK_WIDGET (statusbar));
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (statusbar), TRUE);
209 210
}

211 212 213 214 215 216 217
/**
 * gtk_statusbar_new:
 *
 * Creates a new #GtkStatusbar ready for messages.
 *
 * Returns: the new #GtkStatusbar
 */
218
GtkWidget* 
219
gtk_statusbar_new (void)
220
{
Manish Singh's avatar
Manish Singh committed
221
  return g_object_new (GTK_TYPE_STATUSBAR, NULL);
222 223 224 225
}

static void
gtk_statusbar_update (GtkStatusbar *statusbar,
226
		      guint	    context_id,
227 228
		      const gchar  *text)
{
229
  GtkStatusbarPrivate *priv;
230

231
  g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
232

233 234
  priv = statusbar->priv;

235 236
  if (!text)
    text = "";
237

238
  gtk_label_set_text (GTK_LABEL (priv->label), text);
239 240
}

241 242 243 244 245 246 247 248
/**
 * gtk_statusbar_get_context_id:
 * @statusbar: a #GtkStatusbar
 * @context_description: textual description of what context 
 *                       the new message is being used in
 *
 * Returns a new context identifier, given a description 
 * of the actual context. Note that the description is 
249
 * not shown in the UI.
250 251 252
 *
 * Returns: an integer id
 */
253 254 255 256
guint
gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
			      const gchar  *context_description)
{
257
  GtkStatusbarPrivate *priv;
258
  gchar *string;
259
  guint id;
260 261 262 263
  
  g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
  g_return_val_if_fail (context_description != NULL, 0);

264 265
  priv = statusbar->priv;

266 267 268
  /* we need to preserve namespaces on object datas */
  string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);

269 270
  id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (statusbar), string));
  if (id == 0)
271
    {
272
      id = priv->seq_context_id++;
273
      g_object_set_data_full (G_OBJECT (statusbar), string, GUINT_TO_POINTER (id), NULL);
274
      priv->keys = g_slist_prepend (priv->keys, string);
275 276 277 278
    }
  else
    g_free (string);

279
  return id;
280 281
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static GtkStatusbarMsg *
gtk_statusbar_msg_create (GtkStatusbar *statusbar,
		          guint         context_id,
		          const gchar  *text)
{
  GtkStatusbarMsg *msg;

  msg = g_slice_new (GtkStatusbarMsg);
  msg->text = g_strdup (text);
  msg->context_id = context_id;
  msg->message_id = statusbar->priv->seq_message_id++;

  return msg;
}

static void
gtk_statusbar_msg_free (GtkStatusbarMsg *msg)
{
  g_free (msg->text);
  g_slice_free (GtkStatusbarMsg, msg);
}

304 305 306
/**
 * gtk_statusbar_push:
 * @statusbar: a #GtkStatusbar
307
 * @context_id: the message’s context id, as returned by
308 309 310
 *              gtk_statusbar_get_context_id()
 * @text: the message to add to the statusbar
 * 
311
 * Pushes a new message onto a statusbar’s stack.
312 313 314 315
 *
 * Returns: a message id that can be used with 
 *          gtk_statusbar_remove().
 */
316 317
guint
gtk_statusbar_push (GtkStatusbar *statusbar,
318
		    guint	  context_id,
319
		    const gchar  *text)
320
{
321
  GtkStatusbarPrivate *priv;
322
  GtkStatusbarMsg *msg;
323 324 325

  g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
  g_return_val_if_fail (text != NULL, 0);
326

327 328
  priv = statusbar->priv;

329
  msg = gtk_statusbar_msg_create (statusbar, context_id, text);
330
  priv->messages = g_slist_prepend (priv->messages, msg);
331

Manish Singh's avatar
Manish Singh committed
332 333 334 335 336
  g_signal_emit (statusbar,
		 statusbar_signals[SIGNAL_TEXT_PUSHED],
		 0,
		 msg->context_id,
		 msg->text);
337

338
  return msg->message_id;
339 340
}

341 342
/**
 * gtk_statusbar_pop:
343
 * @statusbar: a #GtkStatusbar
344 345
 * @context_id: a context identifier
 * 
346
 * Removes the first message in the #GtkStatusbar’s stack
347 348 349 350 351 352
 * with the given context id. 
 *
 * Note that this may not change the displayed message, if 
 * the message at the top of the stack has a different 
 * context id.
 */
353
void
354 355
gtk_statusbar_pop (GtkStatusbar *statusbar,
		   guint	 context_id)
356
{
357
  GtkStatusbarPrivate *priv;
358
  GtkStatusbarMsg *msg;
359

360
  g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
361

362 363 364
  priv = statusbar->priv;

  if (priv->messages)
365
    {
366
      GSList *list;
367

368
      for (list = priv->messages; list; list = list->next)
369 370 371 372 373
	{
	  msg = list->data;

	  if (msg->context_id == context_id)
	    {
374 375
	      priv->messages = g_slist_remove_link (priv->messages, list);
	      gtk_statusbar_msg_free (msg);
376 377 378 379
	      g_slist_free_1 (list);
	      break;
	    }
	}
380 381
    }

382
  msg = priv->messages ? priv->messages->data : NULL;
383

Manish Singh's avatar
Manish Singh committed
384 385 386 387 388
  g_signal_emit (statusbar,
		 statusbar_signals[SIGNAL_TEXT_POPPED],
		 0,
		 (guint) (msg ? msg->context_id : 0),
		 msg ? msg->text : NULL);
389 390
}

391 392
/**
 * gtk_statusbar_remove:
393
 * @statusbar: a #GtkStatusbar
394 395 396
 * @context_id: a context identifier
 * @message_id: a message identifier, as returned by gtk_statusbar_push()
 *
397
 * Forces the removal of a message from a statusbar’s stack. 
398 399
 * The exact @context_id and @message_id must be specified.
 */
400
void
401 402 403
gtk_statusbar_remove (GtkStatusbar *statusbar,
		      guint	   context_id,
		      guint        message_id)
404
{
405
  GtkStatusbarPrivate *priv;
406
  GtkStatusbarMsg *msg;
407

408
  g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
409
  g_return_if_fail (message_id > 0);
410

411 412 413
  priv = statusbar->priv;

  msg = priv->messages ? priv->messages->data : NULL;
414 415
  if (msg)
    {
416 417 418 419 420
      GSList *list;

      /* care about signal emission if the topmost item is removed */
      if (msg->context_id == context_id &&
	  msg->message_id == message_id)
421
	{
422
	  gtk_statusbar_pop (statusbar, context_id);
423 424 425
	  return;
	}
      
426
      for (list = priv->messages; list; list = list->next)
427 428 429
	{
	  msg = list->data;
	  
430 431
	  if (msg->context_id == context_id &&
	      msg->message_id == message_id)
432
	    {
433
	      priv->messages = g_slist_remove_link (priv->messages, list);
434
	      gtk_statusbar_msg_free (msg);
435
	      g_slist_free_1 (list);
436 437 438 439
	      
	      break;
	    }
	}
440 441 442
    }
}

443 444
/**
 * gtk_statusbar_remove_all:
445
 * @statusbar: a #GtkStatusbar
446 447 448 449 450 451 452 453 454 455 456
 * @context_id: a context identifier
 *
 * Forces the removal of all messages from a statusbar's
 * stack with the exact @context_id.
 *
 * Since: 2.22
 */
void
gtk_statusbar_remove_all (GtkStatusbar *statusbar,
                          guint         context_id)
{
457
  GtkStatusbarPrivate *priv;
458 459 460 461 462
  GtkStatusbarMsg *msg;
  GSList *prev, *list;

  g_return_if_fail (GTK_IS_STATUSBAR (statusbar));

463 464 465
  priv = statusbar->priv;

  if (priv->messages == NULL)
466 467
    return;

468 469 470 471 472 473 474
  /* We special-case the topmost message at the bottom of this
   * function:
   * If we need to pop it, we have to update various state and we want
   * an up-to-date list of remaining messages in that case.
   */
  prev = priv->messages;
  list = prev->next;
475 476 477 478 479 480 481

  while (list != NULL)
    {
      msg = list->data;

      if (msg->context_id == context_id)
        {
482
          prev->next = list->next;
483

484
          gtk_statusbar_msg_free (msg);
485 486
          g_slist_free_1 (list);

487
          list = prev->next;
488 489 490 491 492 493 494
        }
      else
        {
          prev = list;
          list = prev->next;
        }
    }
495 496 497 498 499 500 501

  /* Treat topmost message here */
  msg = priv->messages->data;
  if (msg->context_id == context_id)
    {
      gtk_statusbar_pop (statusbar, context_id);
    }
502 503
}

504 505
/**
 * gtk_statusbar_get_message_area:
506
 * @statusbar: a #GtkStatusbar
507 508 509
 *
 * Retrieves the box containing the label widget.
 *
510
 * Returns: (type Gtk.Box) (transfer none): a #GtkBox
511 512 513 514 515 516
 *
 * Since: 2.20
 */
GtkWidget*
gtk_statusbar_get_message_area (GtkStatusbar *statusbar)
{
517
  GtkStatusbarPrivate *priv;
518

519 520
  g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), NULL);

521 522
  priv = statusbar->priv;

523
  return priv->message_area;
524 525
}

526
static void
527
gtk_statusbar_destroy (GtkWidget *widget)
528
{
529
  GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
530
  GtkStatusbarPrivate *priv = statusbar->priv;
531

532
  g_slist_free_full (priv->messages, (GDestroyNotify) gtk_statusbar_msg_free);
533
  priv->messages = NULL;
534

535
  g_slist_free_full (priv->keys, g_free);
536
  priv->keys = NULL;
537

538
  GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->destroy (widget);
539
}