]> Pileus Git - ~andy/gtk/blob - gtk/gtktearoffmenuitem.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~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 gint gtk_tearoff_menu_item_delete_cb  (GtkMenuItem           *menu_item,
43                                               GdkEventAny           *event);
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
93   menu_item_class->activate = gtk_tearoff_menu_item_activate;
94 }
95
96 static void
97 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
98 {
99   tearoff_menu_item->torn_off = FALSE;
100 }
101
102 static void
103 gtk_tearoff_menu_item_size_request (GtkWidget      *widget,
104                                     GtkRequisition *requisition)
105 {
106   GtkTearoffMenuItem *tearoff;
107
108   tearoff = GTK_TEAROFF_MENU_ITEM (widget);
109   
110   requisition->width = (GTK_CONTAINER (widget)->border_width +
111                         widget->style->xthickness +
112                         BORDER_SPACING) * 2;
113   requisition->height = (GTK_CONTAINER (widget)->border_width +
114                          widget->style->ythickness) * 2;
115
116   if (tearoff->torn_off)
117     {
118       requisition->height += ARROW_SIZE;
119     }
120   else
121     {
122       requisition->height += widget->style->ythickness + 4;
123     }
124 }
125
126 static void
127 gtk_tearoff_menu_item_paint (GtkWidget   *widget,
128                              GdkRectangle *area)
129 {
130   GtkMenuItem *menu_item;
131   GtkTearoffMenuItem *tearoff_item;
132   GtkShadowType shadow_type;
133   gint width, height;
134   gint x, y;
135   gint right_max;
136   GtkArrowType arrow_type;
137   GtkTextDirection direction;
138   
139   if (GTK_WIDGET_DRAWABLE (widget))
140     {
141       menu_item = GTK_MENU_ITEM (widget);
142       tearoff_item = GTK_TEAROFF_MENU_ITEM (widget);
143
144       direction = gtk_widget_get_direction (widget);
145
146       x = widget->allocation.x + GTK_CONTAINER (menu_item)->border_width;
147       y = widget->allocation.y + GTK_CONTAINER (menu_item)->border_width;
148       width = widget->allocation.width - GTK_CONTAINER (menu_item)->border_width * 2;
149       height = widget->allocation.height - GTK_CONTAINER (menu_item)->border_width * 2;
150       right_max = x + width;
151
152       if (widget->state == GTK_STATE_PRELIGHT)
153         {
154           gint selected_shadow_type;
155           
156           gtk_widget_style_get (widget,
157                                 "selected_shadow_type", &selected_shadow_type,
158                                 NULL);
159           gtk_paint_box (widget->style,
160                          widget->window,
161                          GTK_STATE_PRELIGHT,
162                          selected_shadow_type,
163                          area, widget, "menuitem",
164                          x, y, width, height);
165         }
166       else
167         gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
168
169       if (tearoff_item->torn_off)
170         {
171           gint arrow_x;
172
173           if (widget->state == GTK_STATE_PRELIGHT)
174             shadow_type = GTK_SHADOW_IN;
175           else
176             shadow_type = GTK_SHADOW_OUT;
177
178           if (menu_item->toggle_size > ARROW_SIZE)
179             {
180               if (direction == GTK_TEXT_DIR_LTR) {
181                 arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
182                 arrow_type = GTK_ARROW_LEFT;
183               }
184               else {
185                 arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2; 
186                 arrow_type = GTK_ARROW_RIGHT;       
187               }
188               x += menu_item->toggle_size + BORDER_SPACING;
189             }
190           else
191             {
192               if (direction == GTK_TEXT_DIR_LTR) {
193                 arrow_x = ARROW_SIZE / 2;
194                 arrow_type = GTK_ARROW_LEFT;
195               }
196               else {
197                 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2; 
198                 arrow_type = GTK_ARROW_RIGHT;       
199               }
200               x += 2 * ARROW_SIZE;
201             }
202
203
204           gtk_paint_arrow (widget->style, widget->window,
205                            widget->state, shadow_type,
206                            NULL, widget, "tearoffmenuitem",
207                            arrow_type, FALSE,
208                            arrow_x, y + height / 2 - 5, 
209                            ARROW_SIZE, ARROW_SIZE);
210         }
211
212       while (x < right_max)
213         {
214           gint x1, x2;
215
216           if (direction == GTK_TEXT_DIR_LTR) {
217             x1 = x;
218             x2 = MIN (x + TEAR_LENGTH, right_max);
219           }
220           else {
221             x1 = right_max - x;
222             x2 = MAX (right_max - x - TEAR_LENGTH, 0);
223           }
224           
225           gtk_paint_hline (widget->style, widget->window, GTK_STATE_NORMAL,
226                            NULL, widget, "tearoffmenuitem",
227                            x1, x2, y + (height - widget->style->ythickness) / 2);
228           x += 2 * TEAR_LENGTH;
229         }
230     }
231 }
232
233 static gint
234 gtk_tearoff_menu_item_expose (GtkWidget      *widget,
235                             GdkEventExpose *event)
236 {
237   gtk_tearoff_menu_item_paint (widget, &event->area);
238
239   return FALSE;
240 }
241
242 static gint
243 gtk_tearoff_menu_item_delete_cb (GtkMenuItem *menu_item, GdkEventAny *event)
244 {
245   gtk_tearoff_menu_item_activate (menu_item);
246   return TRUE;
247 }
248
249 static void
250 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
251 {
252   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (menu_item);
253
254   tearoff_menu_item->torn_off = !tearoff_menu_item->torn_off;
255   gtk_widget_queue_resize (GTK_WIDGET (menu_item));
256
257   if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
258     {
259       GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
260       gboolean need_connect;
261       
262         need_connect = (tearoff_menu_item->torn_off && !menu->tearoff_window);
263
264         gtk_menu_set_tearoff_state (GTK_MENU (GTK_WIDGET (menu_item)->parent),
265                                     tearoff_menu_item->torn_off);
266
267         if (need_connect)
268           g_signal_connect_swapped (menu->tearoff_window,
269                                     "delete_event",
270                                     G_CALLBACK (gtk_tearoff_menu_item_delete_cb),
271                                     menu_item);
272     }
273 }
274