1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * GtkStatusbar Copyright (C) 1998 Shawn T. Amundson
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * 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
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 /*
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 */
27
28 #include "config.h"
29 #include "gtkframe.h"
30 #include "gtklabel.h"
31 #include "gtkmarshalers.h"
32 #include "gtkstatusbar.h"
33 #include "gtkwindow.h"
34 #include "gtkprivate.h"
35 #include "gtkintl.h"
36 #include "gtkbuildable.h"
37 #include "gtkalias.h"
38
39 typedef struct _GtkStatusbarMsg GtkStatusbarMsg;
40
41 struct _GtkStatusbarMsg
42 {
43 gchar *text;
44 guint context_id;
45 guint message_id;
46 };
47
48 enum
49 {
50 SIGNAL_TEXT_PUSHED,
51 SIGNAL_TEXT_POPPED,
52 SIGNAL_LAST
53 };
54
55 enum
56 {
57 PROP_0,
58 PROP_HAS_RESIZE_GRIP
59 };
60
61 static void gtk_statusbar_buildable_interface_init (GtkBuildableIface *iface);
62 static GObject *gtk_statusbar_buildable_get_internal_child (GtkBuildable *buildable,
63 GtkBuilder *builder,
64 const gchar *childname);
65 static void gtk_statusbar_destroy (GtkObject *object);
66 static void gtk_statusbar_update (GtkStatusbar *statusbar,
67 guint context_id,
68 const gchar *text);
69 static void gtk_statusbar_size_allocate (GtkWidget *widget,
70 GtkAllocation *allocation);
71 static void gtk_statusbar_realize (GtkWidget *widget);
72 static void gtk_statusbar_unrealize (GtkWidget *widget);
73 static void gtk_statusbar_map (GtkWidget *widget);
74 static void gtk_statusbar_unmap (GtkWidget *widget);
75 static gboolean gtk_statusbar_button_press (GtkWidget *widget,
76 GdkEventButton *event);
77 static gboolean gtk_statusbar_expose_event (GtkWidget *widget,
78 GdkEventExpose *event);
79 static void gtk_statusbar_size_request (GtkWidget *widget,
80 GtkRequisition *requisition);
81 static void gtk_statusbar_size_allocate (GtkWidget *widget,
82 GtkAllocation *allocation);
83 static void gtk_statusbar_direction_changed (GtkWidget *widget,
84 GtkTextDirection prev_dir);
85 static void gtk_statusbar_state_changed (GtkWidget *widget,
86 GtkStateType previous_state);
87 static void gtk_statusbar_create_window (GtkStatusbar *statusbar);
88 static void gtk_statusbar_destroy_window (GtkStatusbar *statusbar);
89 static void gtk_statusbar_get_property (GObject *object,
90 guint prop_id,
91 GValue *value,
92 GParamSpec *pspec);
93 static void gtk_statusbar_set_property (GObject *object,
94 guint prop_id,
95 const GValue *value,
96 GParamSpec *pspec);
97 static void label_selectable_changed (GtkWidget *label,
98 GParamSpec *pspec,
99 gpointer data);
100
101
102 static guint statusbar_signals[SIGNAL_LAST] = { 0 };
103
104 G_DEFINE_TYPE_WITH_CODE (GtkStatusbar, gtk_statusbar, GTK_TYPE_HBOX,
105 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
106 gtk_statusbar_buildable_interface_init));
107
108 static void
gtk_statusbar_class_init(GtkStatusbarClass * class)109 gtk_statusbar_class_init (GtkStatusbarClass *class)
110 {
111 GObjectClass *gobject_class;
112 GtkObjectClass *object_class;
113 GtkWidgetClass *widget_class;
114
115 gobject_class = (GObjectClass *) class;
116 object_class = (GtkObjectClass *) class;
117 widget_class = (GtkWidgetClass *) class;
118
119 gobject_class->set_property = gtk_statusbar_set_property;
120 gobject_class->get_property = gtk_statusbar_get_property;
121
122 object_class->destroy = gtk_statusbar_destroy;
123
124 widget_class->realize = gtk_statusbar_realize;
125 widget_class->unrealize = gtk_statusbar_unrealize;
126 widget_class->map = gtk_statusbar_map;
127 widget_class->unmap = gtk_statusbar_unmap;
128 widget_class->button_press_event = gtk_statusbar_button_press;
129 widget_class->expose_event = gtk_statusbar_expose_event;
130 widget_class->size_request = gtk_statusbar_size_request;
131 widget_class->size_allocate = gtk_statusbar_size_allocate;
132 widget_class->direction_changed = gtk_statusbar_direction_changed;
133 widget_class->state_changed = gtk_statusbar_state_changed;
134
135 class->text_pushed = gtk_statusbar_update;
136 class->text_popped = gtk_statusbar_update;
137
138 /**
139 * GtkStatusbar:has-resize-grip:
140 *
141 * Whether the statusbar has a grip for resizing the toplevel window.
142 *
143 * Since: 2.4
144 */
145 g_object_class_install_property (gobject_class,
146 PROP_HAS_RESIZE_GRIP,
147 g_param_spec_boolean ("has-resize-grip",
148 P_("Has Resize Grip"),
149 P_("Whether the statusbar has a grip for resizing the toplevel"),
150 TRUE,
151 GTK_PARAM_READWRITE));
152
153 /**
154 * GtkStatusbar::text-pushed:
155 * @statusbar: the object which received the signal.
156 * @context_id: the context id of the relevant message/statusbar.
157 * @text: the message that was pushed.
158 *
159 * Is emitted whenever a new message gets pushed onto a statusbar's stack.
160 */
161 statusbar_signals[SIGNAL_TEXT_PUSHED] =
162 g_signal_new (I_("text-pushed"),
163 G_OBJECT_CLASS_TYPE (class),
164 G_SIGNAL_RUN_LAST,
165 G_STRUCT_OFFSET (GtkStatusbarClass, text_pushed),
166 NULL, NULL,
167 _gtk_marshal_VOID__UINT_STRING,
168 G_TYPE_NONE, 2,
169 G_TYPE_UINT,
170 G_TYPE_STRING);
171
172 /**
173 * GtkStatusbar::text-popped:
174 * @statusbar: the object which received the signal.
175 * @context_id: the context id of the relevant message/statusbar.
176 * @text: the message that was just popped.
177 *
178 * Is emitted whenever a new message is popped off a statusbar's stack.
179 */
180 statusbar_signals[SIGNAL_TEXT_POPPED] =
181 g_signal_new (I_("text-popped"),
182 G_OBJECT_CLASS_TYPE (class),
183 G_SIGNAL_RUN_LAST,
184 G_STRUCT_OFFSET (GtkStatusbarClass, text_popped),
185 NULL, NULL,
186 _gtk_marshal_VOID__UINT_STRING,
187 G_TYPE_NONE, 2,
188 G_TYPE_UINT,
189 G_TYPE_STRING);
190
191 gtk_widget_class_install_style_property (widget_class,
192 g_param_spec_enum ("shadow-type",
193 P_("Shadow type"),
194 P_("Style of bevel around the statusbar text"),
195 GTK_TYPE_SHADOW_TYPE,
196 GTK_SHADOW_IN,
197 GTK_PARAM_READABLE));
198 }
199
200 static void
gtk_statusbar_init(GtkStatusbar * statusbar)201 gtk_statusbar_init (GtkStatusbar *statusbar)
202 {
203 GtkBox *box;
204 GtkWidget *message_area;
205 GtkShadowType shadow_type;
206
207 box = GTK_BOX (statusbar);
208
209 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), TRUE);
210
211 box->spacing = 2;
212 box->homogeneous = FALSE;
213
214 statusbar->has_resize_grip = TRUE;
215
216 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
217
218 statusbar->frame = gtk_frame_new (NULL);
219 gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
220 gtk_box_pack_start (box, statusbar->frame, TRUE, TRUE, 0);
221 gtk_widget_show (statusbar->frame);
222
223 message_area = gtk_hbox_new (FALSE, 4);
224 gtk_container_add (GTK_CONTAINER (statusbar->frame), message_area);
225 gtk_widget_show (message_area);
226
227 statusbar->label = gtk_label_new ("");
228 gtk_label_set_single_line_mode (GTK_LABEL (statusbar->label), TRUE);
229 gtk_misc_set_alignment (GTK_MISC (statusbar->label), 0.0, 0.5);
230 g_signal_connect (statusbar->label, "notify::selectable",
231 G_CALLBACK (label_selectable_changed), statusbar);
232 gtk_label_set_ellipsize (GTK_LABEL (statusbar->label), PANGO_ELLIPSIZE_END);
233 gtk_container_add (GTK_CONTAINER (message_area), statusbar->label);
234 gtk_widget_show (statusbar->label);
235
236 statusbar->seq_context_id = 1;
237 statusbar->seq_message_id = 1;
238 statusbar->messages = NULL;
239 statusbar->keys = NULL;
240 }
241
242 static GtkBuildableIface *parent_buildable_iface;
243
244 static void
gtk_statusbar_buildable_interface_init(GtkBuildableIface * iface)245 gtk_statusbar_buildable_interface_init (GtkBuildableIface *iface)
246 {
247 parent_buildable_iface = g_type_interface_peek_parent (iface);
248 iface->get_internal_child = gtk_statusbar_buildable_get_internal_child;
249 }
250
251 static GObject *
gtk_statusbar_buildable_get_internal_child(GtkBuildable * buildable,GtkBuilder * builder,const gchar * childname)252 gtk_statusbar_buildable_get_internal_child (GtkBuildable *buildable,
253 GtkBuilder *builder,
254 const gchar *childname)
255 {
256 if (strcmp (childname, "message_area") == 0)
257 return G_OBJECT (gtk_bin_get_child (GTK_BIN (GTK_STATUSBAR (buildable)->frame)));
258
259 return parent_buildable_iface->get_internal_child (buildable,
260 builder,
261 childname);
262 }
263
264 /**
265 * gtk_statusbar_new:
266 *
267 * Creates a new #GtkStatusbar ready for messages.
268 *
269 * Returns: the new #GtkStatusbar
270 */
271 GtkWidget*
gtk_statusbar_new(void)272 gtk_statusbar_new (void)
273 {
274 return g_object_new (GTK_TYPE_STATUSBAR, NULL);
275 }
276
277 static void
gtk_statusbar_update(GtkStatusbar * statusbar,guint context_id,const gchar * text)278 gtk_statusbar_update (GtkStatusbar *statusbar,
279 guint context_id,
280 const gchar *text)
281 {
282 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
283
284 if (!text)
285 text = "";
286
287 gtk_label_set_text (GTK_LABEL (statusbar->label), text);
288 }
289
290 /**
291 * gtk_statusbar_get_context_id:
292 * @statusbar: a #GtkStatusbar
293 * @context_description: textual description of what context
294 * the new message is being used in
295 *
296 * Returns a new context identifier, given a description
297 * of the actual context. Note that the description is
298 * <emphasis>not</emphasis> shown in the UI.
299 *
300 * Returns: an integer id
301 */
302 guint
gtk_statusbar_get_context_id(GtkStatusbar * statusbar,const gchar * context_description)303 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
304 const gchar *context_description)
305 {
306 gchar *string;
307 guint id;
308
309 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
310 g_return_val_if_fail (context_description != NULL, 0);
311
312 /* we need to preserve namespaces on object datas */
313 string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
314
315 id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (statusbar), string));
316 if (id == 0)
317 {
318 id = statusbar->seq_context_id++;
319 g_object_set_data_full (G_OBJECT (statusbar), string, GUINT_TO_POINTER (id), NULL);
320 statusbar->keys = g_slist_prepend (statusbar->keys, string);
321 }
322 else
323 g_free (string);
324
325 return id;
326 }
327
328 /**
329 * gtk_statusbar_push:
330 * @statusbar: a #GtkStatusbar
331 * @context_id: the message's context id, as returned by
332 * gtk_statusbar_get_context_id()
333 * @text: the message to add to the statusbar
334 *
335 * Pushes a new message onto a statusbar's stack.
336 *
337 * Returns: a message id that can be used with
338 * gtk_statusbar_remove().
339 */
340 guint
gtk_statusbar_push(GtkStatusbar * statusbar,guint context_id,const gchar * text)341 gtk_statusbar_push (GtkStatusbar *statusbar,
342 guint context_id,
343 const gchar *text)
344 {
345 GtkStatusbarMsg *msg;
346
347 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
348 g_return_val_if_fail (text != NULL, 0);
349
350 msg = g_slice_new (GtkStatusbarMsg);
351 msg->text = g_strdup (text);
352 msg->context_id = context_id;
353 msg->message_id = statusbar->seq_message_id++;
354
355 statusbar->messages = g_slist_prepend (statusbar->messages, msg);
356
357 g_signal_emit (statusbar,
358 statusbar_signals[SIGNAL_TEXT_PUSHED],
359 0,
360 msg->context_id,
361 msg->text);
362
363 return msg->message_id;
364 }
365
366 /**
367 * gtk_statusbar_pop:
368 * @statusbar: a #GtkStatusBar
369 * @context_id: a context identifier
370 *
371 * Removes the first message in the #GtkStatusBar's stack
372 * with the given context id.
373 *
374 * Note that this may not change the displayed message, if
375 * the message at the top of the stack has a different
376 * context id.
377 */
378 void
gtk_statusbar_pop(GtkStatusbar * statusbar,guint context_id)379 gtk_statusbar_pop (GtkStatusbar *statusbar,
380 guint context_id)
381 {
382 GtkStatusbarMsg *msg;
383
384 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
385
386 if (statusbar->messages)
387 {
388 GSList *list;
389
390 for (list = statusbar->messages; list; list = list->next)
391 {
392 msg = list->data;
393
394 if (msg->context_id == context_id)
395 {
396 statusbar->messages = g_slist_remove_link (statusbar->messages,
397 list);
398 g_free (msg->text);
399 g_slice_free (GtkStatusbarMsg, msg);
400 g_slist_free_1 (list);
401 break;
402 }
403 }
404 }
405
406 msg = statusbar->messages ? statusbar->messages->data : NULL;
407
408 g_signal_emit (statusbar,
409 statusbar_signals[SIGNAL_TEXT_POPPED],
410 0,
411 (guint) (msg ? msg->context_id : 0),
412 msg ? msg->text : NULL);
413 }
414
415 /**
416 * gtk_statusbar_remove:
417 * @statusbar: a #GtkStatusBar
418 * @context_id: a context identifier
419 * @message_id: a message identifier, as returned by gtk_statusbar_push()
420 *
421 * Forces the removal of a message from a statusbar's stack.
422 * The exact @context_id and @message_id must be specified.
423 */
424 void
gtk_statusbar_remove(GtkStatusbar * statusbar,guint context_id,guint message_id)425 gtk_statusbar_remove (GtkStatusbar *statusbar,
426 guint context_id,
427 guint message_id)
428 {
429 GtkStatusbarMsg *msg;
430
431 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
432 g_return_if_fail (message_id > 0);
433
434 msg = statusbar->messages ? statusbar->messages->data : NULL;
435 if (msg)
436 {
437 GSList *list;
438
439 /* care about signal emission if the topmost item is removed */
440 if (msg->context_id == context_id &&
441 msg->message_id == message_id)
442 {
443 gtk_statusbar_pop (statusbar, context_id);
444 return;
445 }
446
447 for (list = statusbar->messages; list; list = list->next)
448 {
449 msg = list->data;
450
451 if (msg->context_id == context_id &&
452 msg->message_id == message_id)
453 {
454 statusbar->messages = g_slist_remove_link (statusbar->messages, list);
455 g_free (msg->text);
456 g_slice_free (GtkStatusbarMsg, msg);
457 g_slist_free_1 (list);
458
459 break;
460 }
461 }
462 }
463 }
464
465 /**
466 * gtk_statusbar_remove_all:
467 * @statusbar: a #GtkStatusBar
468 * @context_id: a context identifier
469 *
470 * Forces the removal of all messages from a statusbar's
471 * stack with the exact @context_id.
472 *
473 * Since: 2.22
474 */
475 void
gtk_statusbar_remove_all(GtkStatusbar * statusbar,guint context_id)476 gtk_statusbar_remove_all (GtkStatusbar *statusbar,
477 guint context_id)
478 {
479 GtkStatusbarMsg *msg;
480 GSList *prev, *list;
481
482 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
483
484 if (statusbar->messages == NULL)
485 return;
486
487 msg = statusbar->messages->data;
488
489 /* care about signal emission if the topmost item is removed */
490 if (msg->context_id == context_id)
491 {
492 gtk_statusbar_pop (statusbar, context_id);
493
494 prev = NULL;
495 list = statusbar->messages;
496 }
497 else
498 {
499 prev = statusbar->messages;
500 list = prev->next;
501 }
502
503 while (list != NULL)
504 {
505 msg = list->data;
506
507 if (msg->context_id == context_id)
508 {
509 if (prev == NULL)
510 statusbar->messages = list->next;
511 else
512 prev->next = list->next;
513
514 g_free (msg->text);
515 g_slice_free (GtkStatusbarMsg, msg);
516 g_slist_free_1 (list);
517
518 if (prev == NULL)
519 prev = statusbar->messages;
520
521 if (prev)
522 list = prev->next;
523 else
524 list = NULL;
525 }
526 else
527 {
528 prev = list;
529 list = prev->next;
530 }
531 }
532 }
533
534 /**
535 * gtk_statusbar_set_has_resize_grip:
536 * @statusbar: a #GtkStatusBar
537 * @setting: %TRUE to have a resize grip
538 *
539 * Sets whether the statusbar has a resize grip.
540 * %TRUE by default.
541 */
542 void
gtk_statusbar_set_has_resize_grip(GtkStatusbar * statusbar,gboolean setting)543 gtk_statusbar_set_has_resize_grip (GtkStatusbar *statusbar,
544 gboolean setting)
545 {
546 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
547
548 setting = setting != FALSE;
549
550 if (setting != statusbar->has_resize_grip)
551 {
552 statusbar->has_resize_grip = setting;
553 gtk_widget_queue_resize (statusbar->label);
554 gtk_widget_queue_draw (GTK_WIDGET (statusbar));
555
556 if (gtk_widget_get_realized (GTK_WIDGET (statusbar)))
557 {
558 if (statusbar->has_resize_grip && statusbar->grip_window == NULL)
559 {
560 gtk_statusbar_create_window (statusbar);
561 if (gtk_widget_get_mapped (GTK_WIDGET (statusbar)))
562 gdk_window_show (statusbar->grip_window);
563 }
564 else if (!statusbar->has_resize_grip && statusbar->grip_window != NULL)
565 gtk_statusbar_destroy_window (statusbar);
566 }
567
568 g_object_notify (G_OBJECT (statusbar), "has-resize-grip");
569 }
570 }
571
572 /**
573 * gtk_statusbar_get_has_resize_grip:
574 * @statusbar: a #GtkStatusBar
575 *
576 * Returns whether the statusbar has a resize grip.
577 *
578 * Returns: %TRUE if the statusbar has a resize grip.
579 */
580 gboolean
gtk_statusbar_get_has_resize_grip(GtkStatusbar * statusbar)581 gtk_statusbar_get_has_resize_grip (GtkStatusbar *statusbar)
582 {
583 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), FALSE);
584
585 return statusbar->has_resize_grip;
586 }
587
588 /**
589 * gtk_statusbar_get_message_area:
590 * @statusbar: a #GtkStatusBar
591 *
592 * Retrieves the box containing the label widget.
593 *
594 * Returns: (transfer none): a #GtkBox
595 *
596 * Since: 2.20
597 */
598 GtkWidget*
gtk_statusbar_get_message_area(GtkStatusbar * statusbar)599 gtk_statusbar_get_message_area (GtkStatusbar *statusbar)
600 {
601 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), NULL);
602
603 return gtk_bin_get_child (GTK_BIN (statusbar->frame));
604 }
605
606 static void
gtk_statusbar_destroy(GtkObject * object)607 gtk_statusbar_destroy (GtkObject *object)
608 {
609 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
610 GSList *list;
611
612 for (list = statusbar->messages; list; list = list->next)
613 {
614 GtkStatusbarMsg *msg;
615
616 msg = list->data;
617 g_free (msg->text);
618 g_slice_free (GtkStatusbarMsg, msg);
619 }
620 g_slist_free (statusbar->messages);
621 statusbar->messages = NULL;
622
623 for (list = statusbar->keys; list; list = list->next)
624 g_free (list->data);
625 g_slist_free (statusbar->keys);
626 statusbar->keys = NULL;
627
628 GTK_OBJECT_CLASS (gtk_statusbar_parent_class)->destroy (object);
629 }
630
631 static void
gtk_statusbar_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)632 gtk_statusbar_set_property (GObject *object,
633 guint prop_id,
634 const GValue *value,
635 GParamSpec *pspec)
636 {
637 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
638
639 switch (prop_id)
640 {
641 case PROP_HAS_RESIZE_GRIP:
642 gtk_statusbar_set_has_resize_grip (statusbar, g_value_get_boolean (value));
643 break;
644 default:
645 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
646 break;
647 }
648 }
649
650 static void
gtk_statusbar_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)651 gtk_statusbar_get_property (GObject *object,
652 guint prop_id,
653 GValue *value,
654 GParamSpec *pspec)
655 {
656 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
657
658 switch (prop_id)
659 {
660 case PROP_HAS_RESIZE_GRIP:
661 g_value_set_boolean (value, statusbar->has_resize_grip);
662 break;
663 default:
664 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
665 break;
666 }
667 }
668
669 static GdkWindowEdge
get_grip_edge(GtkStatusbar * statusbar)670 get_grip_edge (GtkStatusbar *statusbar)
671 {
672 GtkWidget *widget = GTK_WIDGET (statusbar);
673
674 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
675 return GDK_WINDOW_EDGE_SOUTH_EAST;
676 else
677 return GDK_WINDOW_EDGE_SOUTH_WEST;
678 }
679
680 static void
get_grip_rect(GtkStatusbar * statusbar,GdkRectangle * rect)681 get_grip_rect (GtkStatusbar *statusbar,
682 GdkRectangle *rect)
683 {
684 GtkWidget *widget;
685 gint w, h;
686
687 widget = GTK_WIDGET (statusbar);
688
689 /* These are in effect the max/default size of the grip. */
690 w = 18;
691 h = 18;
692
693 if (w > widget->allocation.width)
694 w = widget->allocation.width;
695
696 if (h > widget->allocation.height - widget->style->ythickness)
697 h = widget->allocation.height - widget->style->ythickness;
698
699 rect->width = w;
700 rect->height = h;
701 rect->y = widget->allocation.y + widget->allocation.height - h;
702
703 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
704 rect->x = widget->allocation.x + widget->allocation.width - w;
705 else
706 rect->x = widget->allocation.x + widget->style->xthickness;
707 }
708
709 static void
set_grip_cursor(GtkStatusbar * statusbar)710 set_grip_cursor (GtkStatusbar *statusbar)
711 {
712 if (statusbar->has_resize_grip && statusbar->grip_window != NULL)
713 {
714 GtkWidget *widget = GTK_WIDGET (statusbar);
715 GdkDisplay *display = gtk_widget_get_display (widget);
716 GdkCursorType cursor_type;
717 GdkCursor *cursor;
718
719 if (gtk_widget_is_sensitive (widget))
720 {
721 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
722 cursor_type = GDK_BOTTOM_RIGHT_CORNER;
723 else
724 cursor_type = GDK_BOTTOM_LEFT_CORNER;
725
726 cursor = gdk_cursor_new_for_display (display, cursor_type);
727 gdk_window_set_cursor (statusbar->grip_window, cursor);
728 gdk_cursor_unref (cursor);
729 }
730 else
731 gdk_window_set_cursor (statusbar->grip_window, NULL);
732 }
733 }
734
735 static void
gtk_statusbar_create_window(GtkStatusbar * statusbar)736 gtk_statusbar_create_window (GtkStatusbar *statusbar)
737 {
738 GtkWidget *widget;
739 GdkWindowAttr attributes;
740 gint attributes_mask;
741 GdkRectangle rect;
742
743 widget = GTK_WIDGET (statusbar);
744
745 g_return_if_fail (gtk_widget_get_realized (widget));
746 g_return_if_fail (statusbar->has_resize_grip);
747
748 get_grip_rect (statusbar, &rect);
749
750 attributes.x = rect.x;
751 attributes.y = rect.y;
752 attributes.width = rect.width;
753 attributes.height = rect.height;
754 attributes.window_type = GDK_WINDOW_CHILD;
755 attributes.wclass = GDK_INPUT_ONLY;
756 attributes.event_mask = gtk_widget_get_events (widget) |
757 GDK_BUTTON_PRESS_MASK;
758
759 attributes_mask = GDK_WA_X | GDK_WA_Y;
760
761 statusbar->grip_window = gdk_window_new (widget->window,
762 &attributes, attributes_mask);
763
764 gdk_window_set_user_data (statusbar->grip_window, widget);
765
766 gdk_window_raise (statusbar->grip_window);
767
768 set_grip_cursor (statusbar);
769 }
770
771 static void
gtk_statusbar_direction_changed(GtkWidget * widget,GtkTextDirection prev_dir)772 gtk_statusbar_direction_changed (GtkWidget *widget,
773 GtkTextDirection prev_dir)
774 {
775 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
776
777 set_grip_cursor (statusbar);
778 }
779
780 static void
gtk_statusbar_state_changed(GtkWidget * widget,GtkStateType previous_state)781 gtk_statusbar_state_changed (GtkWidget *widget,
782 GtkStateType previous_state)
783 {
784 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
785
786 set_grip_cursor (statusbar);
787 }
788
789 static void
gtk_statusbar_destroy_window(GtkStatusbar * statusbar)790 gtk_statusbar_destroy_window (GtkStatusbar *statusbar)
791 {
792 gdk_window_set_user_data (statusbar->grip_window, NULL);
793 gdk_window_destroy (statusbar->grip_window);
794 statusbar->grip_window = NULL;
795 }
796
797 static void
gtk_statusbar_realize(GtkWidget * widget)798 gtk_statusbar_realize (GtkWidget *widget)
799 {
800 GtkStatusbar *statusbar;
801
802 statusbar = GTK_STATUSBAR (widget);
803
804 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->realize (widget);
805
806 if (statusbar->has_resize_grip)
807 gtk_statusbar_create_window (statusbar);
808 }
809
810 static void
gtk_statusbar_unrealize(GtkWidget * widget)811 gtk_statusbar_unrealize (GtkWidget *widget)
812 {
813 GtkStatusbar *statusbar;
814
815 statusbar = GTK_STATUSBAR (widget);
816
817 if (statusbar->grip_window)
818 gtk_statusbar_destroy_window (statusbar);
819
820 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unrealize (widget);
821 }
822
823 static void
gtk_statusbar_map(GtkWidget * widget)824 gtk_statusbar_map (GtkWidget *widget)
825 {
826 GtkStatusbar *statusbar;
827
828 statusbar = GTK_STATUSBAR (widget);
829
830 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->map (widget);
831
832 if (statusbar->grip_window)
833 gdk_window_show (statusbar->grip_window);
834 }
835
836 static void
gtk_statusbar_unmap(GtkWidget * widget)837 gtk_statusbar_unmap (GtkWidget *widget)
838 {
839 GtkStatusbar *statusbar;
840
841 statusbar = GTK_STATUSBAR (widget);
842
843 if (statusbar->grip_window)
844 gdk_window_hide (statusbar->grip_window);
845
846 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unmap (widget);
847 }
848
849 static gboolean
gtk_statusbar_button_press(GtkWidget * widget,GdkEventButton * event)850 gtk_statusbar_button_press (GtkWidget *widget,
851 GdkEventButton *event)
852 {
853 GtkStatusbar *statusbar;
854 GtkWidget *ancestor;
855 GdkWindowEdge edge;
856
857 statusbar = GTK_STATUSBAR (widget);
858
859 if (!statusbar->has_resize_grip ||
860 event->type != GDK_BUTTON_PRESS ||
861 event->window != statusbar->grip_window)
862 return FALSE;
863
864 ancestor = gtk_widget_get_toplevel (widget);
865
866 if (!GTK_IS_WINDOW (ancestor))
867 return FALSE;
868
869 edge = get_grip_edge (statusbar);
870
871 if (event->button == 1)
872 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
873 edge,
874 event->button,
875 event->x_root, event->y_root,
876 event->time);
877 else if (event->button == 2)
878 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
879 event->button,
880 event->x_root, event->y_root,
881 event->time);
882 else
883 return FALSE;
884
885 return TRUE;
886 }
887
888 static gboolean
gtk_statusbar_expose_event(GtkWidget * widget,GdkEventExpose * event)889 gtk_statusbar_expose_event (GtkWidget *widget,
890 GdkEventExpose *event)
891 {
892 GtkStatusbar *statusbar;
893 GdkRectangle rect;
894
895 statusbar = GTK_STATUSBAR (widget);
896
897 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->expose_event (widget, event);
898
899 if (statusbar->has_resize_grip)
900 {
901 GdkWindowEdge edge;
902
903 edge = get_grip_edge (statusbar);
904
905 get_grip_rect (statusbar, &rect);
906
907 gtk_paint_resize_grip (widget->style,
908 widget->window,
909 gtk_widget_get_state (widget),
910 &event->area,
911 widget,
912 "statusbar",
913 edge,
914 rect.x, rect.y,
915 /* don't draw grip over the frame, though you
916 * can click on the frame.
917 */
918 rect.width - widget->style->xthickness,
919 rect.height - widget->style->ythickness);
920 }
921
922 return FALSE;
923 }
924
925 static void
gtk_statusbar_size_request(GtkWidget * widget,GtkRequisition * requisition)926 gtk_statusbar_size_request (GtkWidget *widget,
927 GtkRequisition *requisition)
928 {
929 GtkStatusbar *statusbar;
930 GtkShadowType shadow_type;
931
932 statusbar = GTK_STATUSBAR (widget);
933
934 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
935 gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
936
937 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_request (widget, requisition);
938 }
939
940 /* look for extra children between the frame containing
941 * the label and where we want to draw the resize grip
942 */
943 static gboolean
has_extra_children(GtkStatusbar * statusbar)944 has_extra_children (GtkStatusbar *statusbar)
945 {
946 GList *l;
947 GtkBoxChild *child, *frame;
948
949 /* If the internal frame has been modified assume we have extra children */
950 if (gtk_bin_get_child (GTK_BIN (statusbar->frame)) != statusbar->label)
951 return TRUE;
952
953 frame = NULL;
954 for (l = GTK_BOX (statusbar)->children; l; l = l->next)
955 {
956 frame = l->data;
957
958 if (frame->widget == statusbar->frame)
959 break;
960 }
961
962 for (l = l->next; l; l = l->next)
963 {
964 child = l->data;
965
966 if (!gtk_widget_get_visible (child->widget))
967 continue;
968
969 if (frame->pack == GTK_PACK_START || child->pack == GTK_PACK_END)
970 return TRUE;
971 }
972
973 return FALSE;
974 }
975
976 static void
gtk_statusbar_size_allocate(GtkWidget * widget,GtkAllocation * allocation)977 gtk_statusbar_size_allocate (GtkWidget *widget,
978 GtkAllocation *allocation)
979 {
980 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
981 gboolean extra_children = FALSE;
982 GdkRectangle rect;
983
984 if (statusbar->has_resize_grip)
985 {
986 get_grip_rect (statusbar, &rect);
987
988 extra_children = has_extra_children (statusbar);
989
990 /* If there are extra children, we don't want them to occupy
991 * the space where we draw the resize grip, so we temporarily
992 * shrink the allocation.
993 * If there are no extra children, we want the frame to get
994 * the full allocation, and we fix up the allocation of the
995 * label afterwards to make room for the grip.
996 */
997 if (extra_children)
998 {
999 allocation->width -= rect.width;
1000 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1001 allocation->x += rect.width;
1002 }
1003 }
1004
1005 /* chain up normally */
1006 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_allocate (widget, allocation);
1007
1008 if (statusbar->has_resize_grip)
1009 {
1010 if (extra_children)
1011 {
1012 allocation->width += rect.width;
1013 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1014 allocation->x -= rect.width;
1015
1016 widget->allocation = *allocation;
1017 }
1018 else
1019 {
1020 GtkWidget *child;
1021
1022 /* Use the frame's child instead of statusbar->label directly, in case
1023 * the label has been replaced by a container as the frame's child
1024 * (and the label reparented into that container).
1025 */
1026 child = gtk_bin_get_child (GTK_BIN (statusbar->frame));
1027
1028 if (child->allocation.width + rect.width > statusbar->frame->allocation.width)
1029 {
1030 /* shrink the label to make room for the grip */
1031 *allocation = child->allocation;
1032 allocation->width = MAX (1, allocation->width - rect.width);
1033 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1034 allocation->x += child->allocation.width - allocation->width;
1035
1036 gtk_widget_size_allocate (child, allocation);
1037 }
1038 }
1039
1040 if (statusbar->grip_window)
1041 {
1042 get_grip_rect (statusbar, &rect);
1043
1044 gdk_window_raise (statusbar->grip_window);
1045 gdk_window_move_resize (statusbar->grip_window,
1046 rect.x, rect.y,
1047 rect.width, rect.height);
1048 }
1049
1050 }
1051 }
1052
1053 static void
label_selectable_changed(GtkWidget * label,GParamSpec * pspec,gpointer data)1054 label_selectable_changed (GtkWidget *label,
1055 GParamSpec *pspec,
1056 gpointer data)
1057 {
1058 GtkStatusbar *statusbar = GTK_STATUSBAR (data);
1059
1060 if (statusbar &&
1061 statusbar->has_resize_grip && statusbar->grip_window)
1062 gdk_window_raise (statusbar->grip_window);
1063 }
1064
1065 #define __GTK_STATUSBAR_C__
1066 #include "gtkaliasdef.c"
1067