]> Pileus Git - ~andy/gtk/blob - gtk/gtktearoffmenuitem.c
Use accessor functions to acces GtkContainer
[~andy/gtk] / gtk / gtktearoffmenuitem.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include "gtkmenu.h"
30 #include "gtktearoffmenuitem.h"
31 #include "gtkintl.h"
32
33 #define ARROW_SIZE 10
34 #define TEAR_LENGTH 5
35 #define BORDER_SPACING  3
36
37 static void gtk_tearoff_menu_item_size_request (GtkWidget             *widget,
38                                                 GtkRequisition        *requisition);
39 static gint gtk_tearoff_menu_item_expose     (GtkWidget             *widget,
40                                               GdkEventExpose        *event);
41 static void gtk_tearoff_menu_item_activate   (GtkMenuItem           *menu_item);
42 static void gtk_tearoff_menu_item_parent_set (GtkWidget             *widget,
43                                               GtkWidget             *previous);
44
45 G_DEFINE_TYPE (GtkTearoffMenuItem, gtk_tearoff_menu_item, GTK_TYPE_MENU_ITEM)
46
47 GtkWidget*
48 gtk_tearoff_menu_item_new (void)
49 {
50   return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
51 }
52
53 static void
54 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
55 {
56   GtkWidgetClass *widget_class;
57   GtkMenuItemClass *menu_item_class;
58
59   widget_class = (GtkWidgetClass*) klass;
60   menu_item_class = (GtkMenuItemClass*) klass;
61
62   widget_class->expose_event = gtk_tearoff_menu_item_expose;
63   widget_class->size_request = gtk_tearoff_menu_item_size_request;
64   widget_class->parent_set = gtk_tearoff_menu_item_parent_set;
65
66   menu_item_class->activate = gtk_tearoff_menu_item_activate;
67 }
68
69 static void
70 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
71 {
72   tearoff_menu_item->torn_off = FALSE;
73 }
74
75 static void
76 gtk_tearoff_menu_item_size_request (GtkWidget      *widget,
77                                     GtkRequisition *requisition)
78 {
79   guint border_width;
80
81   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
82   requisition->width = (border_width + widget->style->xthickness + BORDER_SPACING) * 2;
83   requisition->height = (border_width + widget->style->ythickness) * 2;
84
85   if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
86     {
87       requisition->height += ARROW_SIZE;
88     }
89   else
90     {
91       requisition->height += widget->style->ythickness + 4;
92     }
93 }
94
95 static void
96 gtk_tearoff_menu_item_paint (GtkWidget   *widget,
97                              GdkRectangle *area)
98 {
99   GtkMenuItem *menu_item;
100   GtkShadowType shadow_type;
101   gint width, height;
102   gint x, y;
103   gint right_max;
104   guint border_width;
105   GtkArrowType arrow_type;
106   GtkTextDirection direction;
107   
108   if (gtk_widget_is_drawable (widget))
109     {
110       menu_item = GTK_MENU_ITEM (widget);
111
112       direction = gtk_widget_get_direction (widget);
113
114       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_item));
115       x = widget->allocation.x + border_width;
116       y = widget->allocation.y + border_width;
117       width = widget->allocation.width - border_width * 2;
118       height = widget->allocation.height - border_width * 2;
119       right_max = x + width;
120
121       if (widget->state == GTK_STATE_PRELIGHT)
122         {
123           gint selected_shadow_type;
124           
125           gtk_widget_style_get (widget,
126                                 "selected-shadow-type", &selected_shadow_type,
127                                 NULL);
128           gtk_paint_box (widget->style,
129                          widget->window,
130                          GTK_STATE_PRELIGHT,
131                          selected_shadow_type,
132                          area, widget, "menuitem",
133                          x, y, width, height);
134         }
135       else
136         gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
137
138       if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
139         {
140           gint arrow_x;
141
142           if (widget->state == GTK_STATE_PRELIGHT)
143             shadow_type = GTK_SHADOW_IN;
144           else
145             shadow_type = GTK_SHADOW_OUT;
146
147           if (menu_item->toggle_size > ARROW_SIZE)
148             {
149               if (direction == GTK_TEXT_DIR_LTR) {
150                 arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
151                 arrow_type = GTK_ARROW_LEFT;
152               }
153               else {
154                 arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2; 
155                 arrow_type = GTK_ARROW_RIGHT;       
156               }
157               x += menu_item->toggle_size + BORDER_SPACING;
158             }
159           else
160             {
161               if (direction == GTK_TEXT_DIR_LTR) {
162                 arrow_x = ARROW_SIZE / 2;
163                 arrow_type = GTK_ARROW_LEFT;
164               }
165               else {
166                 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2; 
167                 arrow_type = GTK_ARROW_RIGHT;       
168               }
169               x += 2 * ARROW_SIZE;
170             }
171
172
173           gtk_paint_arrow (widget->style, widget->window,
174                            widget->state, shadow_type,
175                            NULL, widget, "tearoffmenuitem",
176                            arrow_type, FALSE,
177                            arrow_x, y + height / 2 - 5, 
178                            ARROW_SIZE, ARROW_SIZE);
179         }
180
181       while (x < right_max)
182         {
183           gint x1, x2;
184
185           if (direction == GTK_TEXT_DIR_LTR) {
186             x1 = x;
187             x2 = MIN (x + TEAR_LENGTH, right_max);
188           }
189           else {
190             x1 = right_max - x;
191             x2 = MAX (right_max - x - TEAR_LENGTH, 0);
192           }
193           
194           gtk_paint_hline (widget->style, widget->window, GTK_STATE_NORMAL,
195                            NULL, widget, "tearoffmenuitem",
196                            x1, x2, y + (height - widget->style->ythickness) / 2);
197           x += 2 * TEAR_LENGTH;
198         }
199     }
200 }
201
202 static gint
203 gtk_tearoff_menu_item_expose (GtkWidget      *widget,
204                             GdkEventExpose *event)
205 {
206   gtk_tearoff_menu_item_paint (widget, &event->area);
207
208   return FALSE;
209 }
210
211 static void
212 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
213 {
214   if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
215     {
216       GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
217       
218       gtk_widget_queue_resize (GTK_WIDGET (menu_item));
219       gtk_menu_set_tearoff_state (GTK_MENU (GTK_WIDGET (menu_item)->parent),
220                                   !menu->torn_off);
221     }
222 }
223
224 static void
225 tearoff_state_changed (GtkMenu            *menu,
226                        GParamSpec         *pspec,
227                        gpointer            data)
228 {
229   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (data);
230
231   tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
232 }
233
234 static void
235 gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
236                                   GtkWidget *previous)
237 {
238   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
239   GtkMenu *menu = GTK_IS_MENU (widget->parent) ? GTK_MENU (widget->parent) : NULL;
240
241   if (previous)
242     g_signal_handlers_disconnect_by_func (previous, 
243                                           tearoff_state_changed, 
244                                           tearoff_menu_item);
245   
246   if (menu)
247     {
248       tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
249       g_signal_connect (menu, "notify::tearoff-state", 
250                         G_CALLBACK (tearoff_state_changed), 
251                         tearoff_menu_item);
252     }  
253 }