]> Pileus Git - ~andy/gtk/blob - gtk/gtkstatusbar.c
Set MATH_LIB to empty also on Win32.
[~andy/gtk] / gtk / gtkstatusbar.c
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 "gtkframe.h"
29 #include "gtklabel.h"
30 #include "gtkmarshalers.h"
31 #include "gtksignal.h"
32 #include "gtkstatusbar.h"
33 #include "gtkwindow.h"
34 #include "gtkintl.h"
35
36 typedef struct _GtkStatusbarMsg GtkStatusbarMsg;
37
38 struct _GtkStatusbarMsg
39 {
40   gchar *text;
41   guint context_id;
42   guint message_id;
43 };
44
45 enum
46 {
47   SIGNAL_TEXT_PUSHED,
48   SIGNAL_TEXT_POPPED,
49   SIGNAL_LAST
50 };
51
52 static void     gtk_statusbar_class_init     (GtkStatusbarClass *class);
53 static void     gtk_statusbar_init           (GtkStatusbar      *statusbar);
54 static void     gtk_statusbar_destroy        (GtkObject         *object);
55 static void     gtk_statusbar_update         (GtkStatusbar      *statusbar,
56                                               guint              context_id,
57                                               const gchar       *text);
58 static void     gtk_statusbar_size_allocate  (GtkWidget         *widget,
59                                               GtkAllocation     *allocation);
60 static void     gtk_statusbar_realize        (GtkWidget         *widget);
61 static void     gtk_statusbar_unrealize      (GtkWidget         *widget);
62 static void     gtk_statusbar_map            (GtkWidget         *widget);
63 static void     gtk_statusbar_unmap          (GtkWidget         *widget);
64 static gboolean gtk_statusbar_button_press   (GtkWidget         *widget,
65                                               GdkEventButton    *event);
66 static gboolean gtk_statusbar_expose_event   (GtkWidget         *widget,
67                                               GdkEventExpose    *event);
68 static void     gtk_statusbar_size_request   (GtkWidget         *widget,
69                                               GtkRequisition    *requisition);
70 static void     gtk_statusbar_size_allocate  (GtkWidget         *widget,
71                                               GtkAllocation     *allocation);
72 static void     gtk_statusbar_create_window  (GtkStatusbar      *statusbar);
73 static void     gtk_statusbar_destroy_window (GtkStatusbar      *statusbar);
74
75 static GtkContainerClass *parent_class;
76 static guint              statusbar_signals[SIGNAL_LAST] = { 0 };
77
78 GtkType      
79 gtk_statusbar_get_type (void)
80 {
81   static GtkType statusbar_type = 0;
82
83   if (!statusbar_type)
84     {
85       static const GtkTypeInfo statusbar_info =
86       {
87         "GtkStatusbar",
88         sizeof (GtkStatusbar),
89         sizeof (GtkStatusbarClass),
90         (GtkClassInitFunc) gtk_statusbar_class_init,
91         (GtkObjectInitFunc) gtk_statusbar_init,
92         /* reserved_1 */ NULL,
93         /* reserved_2 */ NULL,
94         (GtkClassInitFunc) NULL,
95       };
96
97       statusbar_type = gtk_type_unique (GTK_TYPE_HBOX, &statusbar_info);
98     }
99
100   return statusbar_type;
101 }
102
103 static void
104 gtk_statusbar_class_init (GtkStatusbarClass *class)
105 {
106   GtkObjectClass *object_class;
107   GtkWidgetClass *widget_class;
108   GtkContainerClass *container_class;
109
110   object_class = (GtkObjectClass *) class;
111   widget_class = (GtkWidgetClass *) class;
112   container_class = (GtkContainerClass *) class;
113
114   parent_class = gtk_type_class (GTK_TYPE_HBOX);
115   
116   object_class->destroy = gtk_statusbar_destroy;
117
118   widget_class->realize = gtk_statusbar_realize;
119   widget_class->unrealize = gtk_statusbar_unrealize;
120   widget_class->map = gtk_statusbar_map;
121   widget_class->unmap = gtk_statusbar_unmap;
122   
123   widget_class->button_press_event = gtk_statusbar_button_press;
124   widget_class->expose_event = gtk_statusbar_expose_event;
125
126   widget_class->size_request = gtk_statusbar_size_request;
127   widget_class->size_allocate = gtk_statusbar_size_allocate;
128   
129   class->messages_mem_chunk = g_mem_chunk_new ("GtkStatusBar messages mem chunk",
130                                                sizeof (GtkStatusbarMsg),
131                                                sizeof (GtkStatusbarMsg) * 64,
132                                                G_ALLOC_AND_FREE);
133
134   class->text_pushed = gtk_statusbar_update;
135   class->text_popped = gtk_statusbar_update;
136   
137   statusbar_signals[SIGNAL_TEXT_PUSHED] =
138     gtk_signal_new ("text_pushed",
139                     GTK_RUN_LAST,
140                     GTK_CLASS_TYPE (object_class),
141                     GTK_SIGNAL_OFFSET (GtkStatusbarClass, text_pushed),
142                     _gtk_marshal_VOID__UINT_STRING,
143                     GTK_TYPE_NONE, 2,
144                     GTK_TYPE_UINT,
145                     GTK_TYPE_STRING);
146   statusbar_signals[SIGNAL_TEXT_POPPED] =
147     gtk_signal_new ("text_popped",
148                     GTK_RUN_LAST,
149                     GTK_CLASS_TYPE (object_class),
150                     GTK_SIGNAL_OFFSET (GtkStatusbarClass, text_popped),
151                     _gtk_marshal_VOID__UINT_STRING,
152                     GTK_TYPE_NONE, 2,
153                     GTK_TYPE_UINT,
154                     GTK_TYPE_STRING);
155
156   gtk_widget_class_install_style_property (widget_class,
157                                            g_param_spec_enum ("shadow_type",
158                                                               _("Shadow type"),
159                                                               _("Style of bevel around the statusbar text"),
160                                                               GTK_TYPE_SHADOW_TYPE,
161                                                               GTK_SHADOW_IN,
162                                                               G_PARAM_READABLE));
163 }
164
165 static void
166 gtk_statusbar_init (GtkStatusbar *statusbar)
167 {
168   GtkBox *box;
169   GtkShadowType shadow_type;
170   
171   box = GTK_BOX (statusbar);
172
173   box->spacing = 2;
174   box->homogeneous = FALSE;
175
176   statusbar->has_resize_grip = TRUE;
177
178   gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow_type", &shadow_type, NULL);
179   
180   statusbar->frame = gtk_frame_new (NULL);
181   gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
182   gtk_box_pack_start (box, statusbar->frame, TRUE, TRUE, 0);
183   gtk_widget_show (statusbar->frame);
184
185   statusbar->label = gtk_label_new ("");
186   gtk_misc_set_alignment (GTK_MISC (statusbar->label), 0.0, 0.0);
187   /* don't expand the size request for the label; if we
188    * do that then toplevels weirdly resize
189    */
190   gtk_widget_set_usize (statusbar->label, 1, -1);
191   gtk_container_add (GTK_CONTAINER (statusbar->frame), statusbar->label);
192   gtk_widget_show (statusbar->label);
193
194   statusbar->seq_context_id = 1;
195   statusbar->seq_message_id = 1;
196   statusbar->messages = NULL;
197   statusbar->keys = NULL;
198 }
199
200 GtkWidget* 
201 gtk_statusbar_new (void)
202 {
203   return gtk_type_new (GTK_TYPE_STATUSBAR);
204 }
205
206 static void
207 gtk_statusbar_update (GtkStatusbar *statusbar,
208                       guint         context_id,
209                       const gchar  *text)
210 {
211   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
212
213   if (!text)
214     text = "";
215
216   gtk_label_set_text (GTK_LABEL (statusbar->label), text);
217 }
218
219 guint
220 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
221                               const gchar  *context_description)
222 {
223   gchar *string;
224   guint *id;
225   
226   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
227   g_return_val_if_fail (context_description != NULL, 0);
228
229   /* we need to preserve namespaces on object datas */
230   string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
231
232   id = gtk_object_get_data (GTK_OBJECT (statusbar), string);
233   if (!id)
234     {
235       id = g_new (guint, 1);
236       *id = statusbar->seq_context_id++;
237       gtk_object_set_data_full (GTK_OBJECT (statusbar), string, id, (GtkDestroyNotify) g_free);
238       statusbar->keys = g_slist_prepend (statusbar->keys, string);
239     }
240   else
241     g_free (string);
242
243   return *id;
244 }
245
246 guint
247 gtk_statusbar_push (GtkStatusbar *statusbar,
248                     guint         context_id,
249                     const gchar  *text)
250 {
251   GtkStatusbarMsg *msg;
252   GtkStatusbarClass *class;
253
254   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
255   g_return_val_if_fail (text != NULL, 0);
256
257   class = GTK_STATUSBAR_GET_CLASS (statusbar);
258   msg = g_chunk_new (GtkStatusbarMsg, class->messages_mem_chunk);
259   msg->text = g_strdup (text);
260   msg->context_id = context_id;
261   msg->message_id = statusbar->seq_message_id++;
262
263   statusbar->messages = g_slist_prepend (statusbar->messages, msg);
264
265   gtk_signal_emit (GTK_OBJECT (statusbar),
266                    statusbar_signals[SIGNAL_TEXT_PUSHED],
267                    msg->context_id,
268                    msg->text);
269
270   return msg->message_id;
271 }
272
273 void
274 gtk_statusbar_pop (GtkStatusbar *statusbar,
275                    guint         context_id)
276 {
277   GtkStatusbarMsg *msg;
278
279   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
280
281   if (statusbar->messages)
282     {
283       GSList *list;
284
285       for (list = statusbar->messages; list; list = list->next)
286         {
287           msg = list->data;
288
289           if (msg->context_id == context_id)
290             {
291               GtkStatusbarClass *class;
292
293               class = GTK_STATUSBAR_GET_CLASS (statusbar);
294
295               statusbar->messages = g_slist_remove_link (statusbar->messages,
296                                                          list);
297               g_free (msg->text);
298               g_mem_chunk_free (class->messages_mem_chunk, msg);
299               g_slist_free_1 (list);
300               break;
301             }
302         }
303     }
304
305   msg = statusbar->messages ? statusbar->messages->data : NULL;
306
307   gtk_signal_emit (GTK_OBJECT (statusbar),
308                    statusbar_signals[SIGNAL_TEXT_POPPED],
309                    (guint) (msg ? msg->context_id : 0),
310                    msg ? msg->text : NULL);
311 }
312
313 void
314 gtk_statusbar_remove (GtkStatusbar *statusbar,
315                       guint        context_id,
316                       guint        message_id)
317 {
318   GtkStatusbarMsg *msg;
319
320   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
321   g_return_if_fail (message_id > 0);
322
323   msg = statusbar->messages ? statusbar->messages->data : NULL;
324   if (msg)
325     {
326       GSList *list;
327
328       /* care about signal emission if the topmost item is removed */
329       if (msg->context_id == context_id &&
330           msg->message_id == message_id)
331         {
332           gtk_statusbar_pop (statusbar, context_id);
333           return;
334         }
335       
336       for (list = statusbar->messages; list; list = list->next)
337         {
338           msg = list->data;
339           
340           if (msg->context_id == context_id &&
341               msg->message_id == message_id)
342             {
343               GtkStatusbarClass *class;
344               
345               class = GTK_STATUSBAR_GET_CLASS (statusbar);
346               statusbar->messages = g_slist_remove_link (statusbar->messages, list);
347               g_free (msg->text);
348               g_mem_chunk_free (class->messages_mem_chunk, msg);
349               g_slist_free_1 (list);
350               
351               break;
352             }
353         }
354     }
355 }
356
357 void
358 gtk_statusbar_set_has_resize_grip (GtkStatusbar *statusbar,
359                                    gboolean      setting)
360 {
361   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
362
363   setting = setting != FALSE;
364
365   if (setting != statusbar->has_resize_grip)
366     {
367       statusbar->has_resize_grip = setting;
368       gtk_widget_queue_draw (GTK_WIDGET (statusbar));
369
370       if (GTK_WIDGET_REALIZED (statusbar))
371         {
372           if (statusbar->has_resize_grip && statusbar->grip_window == NULL)
373             gtk_statusbar_create_window (statusbar);
374           else if (!statusbar->has_resize_grip && statusbar->grip_window != NULL)
375             gtk_statusbar_destroy_window (statusbar);
376         }
377     }
378 }
379
380 gboolean
381 gtk_statusbar_get_has_resize_grip (GtkStatusbar *statusbar)
382 {
383   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), FALSE);
384
385   return statusbar->has_resize_grip;
386 }
387
388 static void
389 gtk_statusbar_destroy (GtkObject *object)
390 {
391   GtkStatusbar *statusbar;
392   GtkStatusbarClass *class;
393   GSList *list;
394
395   g_return_if_fail (GTK_IS_STATUSBAR (object));
396
397   statusbar = GTK_STATUSBAR (object);
398   class = GTK_STATUSBAR_GET_CLASS (statusbar);
399
400   for (list = statusbar->messages; list; list = list->next)
401     {
402       GtkStatusbarMsg *msg;
403
404       msg = list->data;
405       g_free (msg->text);
406       g_mem_chunk_free (class->messages_mem_chunk, msg);
407     }
408   g_slist_free (statusbar->messages);
409   statusbar->messages = NULL;
410
411   for (list = statusbar->keys; list; list = list->next)
412     g_free (list->data);
413   g_slist_free (statusbar->keys);
414   statusbar->keys = NULL;
415
416   GTK_OBJECT_CLASS (parent_class)->destroy (object);
417 }
418
419 static void
420 get_grip_rect (GtkStatusbar *statusbar,
421                GdkRectangle *rect)
422 {
423   GtkWidget *widget;
424   gint w, h;
425   
426   widget = GTK_WIDGET (statusbar);
427
428   /* These are in effect the max/default size of the grip. */
429   w = 18;
430   h = 18;
431
432   if (w > (widget->allocation.width))
433     w = widget->allocation.width;
434
435   if (h > (widget->allocation.height - widget->style->ythickness))
436     h = widget->allocation.height - widget->style->ythickness;
437   
438   rect->x = widget->allocation.x + widget->allocation.width - w;
439   rect->y = widget->allocation.y + widget->allocation.height - h;
440   rect->width = w;
441   rect->height = h;
442 }
443
444 static void
445 gtk_statusbar_create_window (GtkStatusbar *statusbar)
446 {
447   GtkWidget *widget;
448   GdkWindowAttr attributes;
449   gint attributes_mask;
450   GdkRectangle rect;
451   
452   g_return_if_fail (GTK_WIDGET_REALIZED (statusbar));
453   g_return_if_fail (statusbar->has_resize_grip);
454   
455   widget = GTK_WIDGET (statusbar);
456
457   get_grip_rect (statusbar, &rect);
458
459   attributes.x = rect.x;
460   attributes.y = rect.y;
461   attributes.width = rect.width;
462   attributes.height = rect.height;
463   attributes.window_type = GDK_WINDOW_CHILD;
464   attributes.wclass = GDK_INPUT_ONLY;
465   attributes.event_mask = gtk_widget_get_events (widget) |
466     GDK_BUTTON_PRESS_MASK;
467
468   attributes_mask = GDK_WA_X | GDK_WA_Y;
469
470   statusbar->grip_window = gdk_window_new (widget->window,
471                                            &attributes, attributes_mask);
472   gdk_window_set_user_data (statusbar->grip_window, widget);
473 }
474
475 static void
476 gtk_statusbar_destroy_window (GtkStatusbar *statusbar)
477 {
478   gdk_window_set_user_data (statusbar->grip_window, NULL);
479   gdk_window_destroy (statusbar->grip_window);
480   statusbar->grip_window = NULL;
481 }
482
483 static void
484 gtk_statusbar_realize (GtkWidget *widget)
485 {
486   GtkStatusbar *statusbar;
487
488   statusbar = GTK_STATUSBAR (widget);
489   
490   (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);
491
492   if (statusbar->has_resize_grip)
493     gtk_statusbar_create_window (statusbar);
494 }
495
496 static void
497 gtk_statusbar_unrealize (GtkWidget *widget)
498 {
499   GtkStatusbar *statusbar;
500
501   statusbar = GTK_STATUSBAR (widget);
502
503   if (statusbar->grip_window)
504     gtk_statusbar_destroy_window (statusbar);
505   
506   (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
507 }
508
509 static void
510 gtk_statusbar_map (GtkWidget *widget)
511 {
512   GtkStatusbar *statusbar;
513
514   statusbar = GTK_STATUSBAR (widget);
515   
516   (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
517   
518   if (statusbar->grip_window)
519     gdk_window_show (statusbar->grip_window);
520 }
521
522 static void
523 gtk_statusbar_unmap (GtkWidget *widget)
524 {
525   GtkStatusbar *statusbar;
526
527   statusbar = GTK_STATUSBAR (widget);
528
529   if (statusbar->grip_window)
530     gdk_window_hide (statusbar->grip_window);
531   
532   (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
533 }
534
535 static gboolean
536 gtk_statusbar_button_press (GtkWidget      *widget,
537                             GdkEventButton *event)
538 {
539   GtkStatusbar *statusbar;
540   GtkWidget *ancestor;
541   
542   statusbar = GTK_STATUSBAR (widget);
543   
544   if (!statusbar->has_resize_grip)
545     return FALSE;
546   
547   ancestor = gtk_widget_get_toplevel (widget);
548
549   if (!GTK_IS_WINDOW (ancestor))
550     return FALSE;
551
552   if (event->button == 1)
553     gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
554                                   GDK_WINDOW_EDGE_SOUTH_EAST,
555                                   event->button,
556                                   event->x_root, event->y_root,
557                                   event->time);
558   else if (event->button == 2)
559     gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
560                                 event->button,
561                                 event->x_root, event->y_root,
562                                 event->time);
563   else
564     return FALSE;
565   
566   return TRUE;
567 }
568
569 static gboolean
570 gtk_statusbar_expose_event (GtkWidget      *widget,
571                             GdkEventExpose *event)
572 {
573   GtkStatusbar *statusbar;
574   GdkRectangle rect;
575   
576   statusbar = GTK_STATUSBAR (widget);
577
578   GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
579
580   if (statusbar->has_resize_grip)
581     {
582       get_grip_rect (statusbar, &rect);
583
584       gtk_paint_resize_grip (widget->style,
585                              widget->window,
586                              GTK_WIDGET_STATE (widget),
587                              NULL,
588                              widget,
589                              "statusbar",
590                              GDK_WINDOW_EDGE_SOUTH_EAST,
591                              rect.x, rect.y,
592                              /* don't draw grip over the frame, though you
593                               * can click on the frame.
594                               */
595                              rect.width - widget->style->xthickness,
596                              rect.height - widget->style->ythickness);
597     }
598
599   return FALSE;
600 }
601
602 static void
603 gtk_statusbar_size_request   (GtkWidget      *widget,
604                               GtkRequisition *requisition)
605 {
606   GtkStatusbar *statusbar;
607   GtkShadowType shadow_type;
608   
609   statusbar = GTK_STATUSBAR (widget);
610
611   gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow_type", &shadow_type, NULL);  
612   gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
613   
614   GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
615
616   if (statusbar->has_resize_grip)
617     {
618       GdkRectangle rect;
619
620       /* x, y in the grip rect depend on size allocation, but
621        * w, h do not so this is OK
622        */
623       get_grip_rect (statusbar, &rect);
624       
625       requisition->width += rect.width;
626       requisition->height = MAX (requisition->height, rect.height);
627     }
628 }
629
630 static void
631 gtk_statusbar_size_allocate  (GtkWidget     *widget,
632                               GtkAllocation *allocation)
633 {
634   GtkStatusbar *statusbar;
635   
636   statusbar = GTK_STATUSBAR (widget);
637
638   if (statusbar->has_resize_grip)
639     {
640       GdkRectangle rect;
641       GtkRequisition saved_req;
642       
643       widget->allocation = *allocation; /* get_grip_rect needs this info */
644       get_grip_rect (statusbar, &rect);
645   
646       if (statusbar->grip_window)
647         gdk_window_move_resize (statusbar->grip_window,
648                                 rect.x, rect.y,
649                                 rect.width, rect.height);
650       
651       /* enter the bad hack zone */      
652       saved_req = widget->requisition;
653       widget->requisition.width -= rect.width; /* HBox::size_allocate needs this */
654       if (widget->requisition.width < 0)
655         widget->requisition.width = 0;
656       GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
657       widget->requisition = saved_req;
658     }
659   else
660     {
661       /* chain up normally */
662       GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
663     }
664 }