]> Pileus Git - ~andy/gtk/blob - gtk/gtktearoffmenuitem.c
Put the torn_off flag back into the GtkTearoffMenuItem struct, since it is
[~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 #include "gtkmenu.h"
29 #include "gtktearoffmenuitem.h"
30
31 #define ARROW_SIZE 10
32 #define TEAR_LENGTH 5
33 #define BORDER_SPACING  3
34
35 static void gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass);
36 static void gtk_tearoff_menu_item_init       (GtkTearoffMenuItem      *tearoff_menu_item);
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 GType
46 gtk_tearoff_menu_item_get_type (void)
47 {
48   static GType tearoff_menu_item_type = 0;
49
50   if (!tearoff_menu_item_type)
51     {
52       static const GTypeInfo tearoff_menu_item_info =
53       {
54         sizeof (GtkTearoffMenuItemClass),
55         NULL,           /* base_init */
56         NULL,           /* base_finalize */
57         (GClassInitFunc) gtk_tearoff_menu_item_class_init,
58         NULL,           /* class_finalize */
59         NULL,           /* class_data */
60         sizeof (GtkTearoffMenuItem),
61         0,              /* n_preallocs */
62         (GInstanceInitFunc) gtk_tearoff_menu_item_init,
63       };
64
65       tearoff_menu_item_type =
66         g_type_register_static (GTK_TYPE_MENU_ITEM, "GtkTearoffMenuItem",
67                                 &tearoff_menu_item_info, 0);
68     }
69
70   return tearoff_menu_item_type;
71 }
72
73 GtkWidget*
74 gtk_tearoff_menu_item_new (void)
75 {
76   return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
77 }
78
79 static void
80 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
81 {
82   GtkObjectClass *object_class;
83   GtkWidgetClass *widget_class;
84   GtkMenuItemClass *menu_item_class;
85
86   object_class = (GtkObjectClass*) klass;
87   widget_class = (GtkWidgetClass*) klass;
88   menu_item_class = (GtkMenuItemClass*) klass;
89
90   widget_class->expose_event = gtk_tearoff_menu_item_expose;
91   widget_class->size_request = gtk_tearoff_menu_item_size_request;
92   widget_class->parent_set = gtk_tearoff_menu_item_parent_set;
93
94   menu_item_class->activate = gtk_tearoff_menu_item_activate;
95 }
96
97 static void
98 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
99 {
100   tearoff_menu_item->torn_off = FALSE;
101 }
102
103 static void
104 gtk_tearoff_menu_item_size_request (GtkWidget      *widget,
105                                     GtkRequisition *requisition)
106 {
107   GtkTearoffMenuItem *tearoff;
108   
109   tearoff = GTK_TEAROFF_MENU_ITEM (widget);
110   
111   requisition->width = (GTK_CONTAINER (widget)->border_width +
112                         widget->style->xthickness +
113                         BORDER_SPACING) * 2;
114   requisition->height = (GTK_CONTAINER (widget)->border_width +
115                          widget->style->ythickness) * 2;
116
117   if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
118     {
119       requisition->height += ARROW_SIZE;
120     }
121   else
122     {
123       requisition->height += widget->style->ythickness + 4;
124     }
125 }
126
127 static void
128 gtk_tearoff_menu_item_paint (GtkWidget   *widget,
129                              GdkRectangle *area)
130 {
131   GtkMenuItem *menu_item;
132   GtkTearoffMenuItem *tearoff_item;
133   GtkShadowType shadow_type;
134   gint width, height;
135   gint x, y;
136   gint right_max;
137   GtkArrowType arrow_type;
138   GtkTextDirection direction;
139   
140   if (GTK_WIDGET_DRAWABLE (widget))
141     {
142       menu_item = GTK_MENU_ITEM (widget);
143       tearoff_item = GTK_TEAROFF_MENU_ITEM (widget);
144
145       direction = gtk_widget_get_direction (widget);
146
147       x = widget->allocation.x + GTK_CONTAINER (menu_item)->border_width;
148       y = widget->allocation.y + GTK_CONTAINER (menu_item)->border_width;
149       width = widget->allocation.width - GTK_CONTAINER (menu_item)->border_width * 2;
150       height = widget->allocation.height - GTK_CONTAINER (menu_item)->border_width * 2;
151       right_max = x + width;
152
153       if (widget->state == GTK_STATE_PRELIGHT)
154         {
155           gint selected_shadow_type;
156           
157           gtk_widget_style_get (widget,
158                                 "selected_shadow_type", &selected_shadow_type,
159                                 NULL);
160           gtk_paint_box (widget->style,
161                          widget->window,
162                          GTK_STATE_PRELIGHT,
163                          selected_shadow_type,
164                          area, widget, "menuitem",
165                          x, y, width, height);
166         }
167       else
168         gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
169
170       if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
171         {
172           gint arrow_x;
173
174           if (widget->state == GTK_STATE_PRELIGHT)
175             shadow_type = GTK_SHADOW_IN;
176           else
177             shadow_type = GTK_SHADOW_OUT;
178
179           if (menu_item->toggle_size > ARROW_SIZE)
180             {
181               if (direction == GTK_TEXT_DIR_LTR) {
182                 arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
183                 arrow_type = GTK_ARROW_LEFT;
184               }
185               else {
186                 arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2; 
187                 arrow_type = GTK_ARROW_RIGHT;       
188               }
189               x += menu_item->toggle_size + BORDER_SPACING;
190             }
191           else
192             {
193               if (direction == GTK_TEXT_DIR_LTR) {
194                 arrow_x = ARROW_SIZE / 2;
195                 arrow_type = GTK_ARROW_LEFT;
196               }
197               else {
198                 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2; 
199                 arrow_type = GTK_ARROW_RIGHT;       
200               }
201               x += 2 * ARROW_SIZE;
202             }
203
204
205           gtk_paint_arrow (widget->style, widget->window,
206                            widget->state, shadow_type,
207                            NULL, widget, "tearoffmenuitem",
208                            arrow_type, FALSE,
209                            arrow_x, y + height / 2 - 5, 
210                            ARROW_SIZE, ARROW_SIZE);
211         }
212
213       while (x < right_max)
214         {
215           gint x1, x2;
216
217           if (direction == GTK_TEXT_DIR_LTR) {
218             x1 = x;
219             x2 = MIN (x + TEAR_LENGTH, right_max);
220           }
221           else {
222             x1 = right_max - x;
223             x2 = MAX (right_max - x - TEAR_LENGTH, 0);
224           }
225           
226           gtk_paint_hline (widget->style, widget->window, GTK_STATE_NORMAL,
227                            NULL, widget, "tearoffmenuitem",
228                            x1, x2, y + (height - widget->style->ythickness) / 2);
229           x += 2 * TEAR_LENGTH;
230         }
231     }
232 }
233
234 static gint
235 gtk_tearoff_menu_item_expose (GtkWidget      *widget,
236                             GdkEventExpose *event)
237 {
238   gtk_tearoff_menu_item_paint (widget, &event->area);
239
240   return FALSE;
241 }
242
243 static void
244 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
245 {
246   if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
247     {
248       GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
249       
250       gtk_menu_set_tearoff_state (GTK_MENU (GTK_WIDGET (menu_item)->parent),
251                                   !menu->torn_off);
252     }
253
254   gtk_widget_queue_resize (GTK_WIDGET (menu_item));
255 }
256
257 static void
258 tearoff_state_changed (GtkMenu            *menu,
259                        GtkTearoffMenuItem *tearoff_menu_item)
260 {
261   tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
262 }
263
264 static void
265 gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
266                                   GtkWidget *previous)
267 {
268   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
269   GtkMenu *menu = GTK_IS_MENU (widget->parent) ? GTK_MENU (widget->parent) : NULL;
270
271   if (previous)
272     g_signal_handlers_disconnect_by_func (previous, 
273                                           tearoff_state_changed, 
274                                           tearoff_menu_item);
275   
276   if (menu)
277     {
278       tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
279       g_signal_connect (menu, "notify::tearoff-state", 
280                         G_CALLBACK (tearoff_state_changed), 
281                         tearoff_menu_item);
282     }  
283 }