]> Pileus Git - ~andy/gtk/blob - gtk/gtktearoffmenuitem.c
dc151ceed77918ecce73780bc09fb231c34da454
[~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 #undef GTK_DISABLE_DEPRECATED
30
31 #include "gtkmenuprivate.h"
32 #include "gtkmenuitemprivate.h"
33 #include "gtktearoffmenuitem.h"
34 #include "gtkintl.h"
35
36
37 /**
38  * SECTION:gtktearoffmenuitem
39  * @Short_description: A menu item used to tear off and reattach its menu
40  * @Title: GtkTearoffMenuItem
41  * @See_also: #GtkMenu
42  *
43  * A #GtkTearoffMenuItem is a special #GtkMenuItem which is used to
44  * tear off and reattach its menu.
45  *
46  * When its menu is shown normally, the #GtkTearoffMenuItem is drawn as a
47  * dotted line indicating that the menu can be torn off.  Activating it
48  * causes its menu to be torn off and displayed in its own window
49  * as a tearoff menu.
50  *
51  * When its menu is shown as a tearoff menu, the #GtkTearoffMenuItem is drawn
52  * as a dotted line which has a left pointing arrow graphic indicating that
53  * the tearoff menu can be reattached.  Activating it will erase the tearoff
54  * menu window.
55  *
56  * <note>#GtkTearoffMenuItem is deprecated and should not be used in newly
57  * written code. Menus are not meant to be torn around.</note>
58  */
59
60
61 #define ARROW_SIZE 10
62 #define TEAR_LENGTH 5
63 #define BORDER_SPACING  3
64
65 struct _GtkTearoffMenuItemPrivate
66 {
67   guint torn_off : 1;
68 };
69
70 static void gtk_tearoff_menu_item_get_preferred_width  (GtkWidget      *widget,
71                                                         gint           *minimum,
72                                                         gint           *natural);
73 static void gtk_tearoff_menu_item_get_preferred_height (GtkWidget      *widget,
74                                                         gint           *minimum,
75                                                         gint           *natural);
76 static gboolean gtk_tearoff_menu_item_draw             (GtkWidget      *widget,
77                                                         cairo_t        *cr);
78 static void gtk_tearoff_menu_item_activate             (GtkMenuItem    *menu_item);
79 static void gtk_tearoff_menu_item_parent_set           (GtkWidget      *widget,
80                                                         GtkWidget      *previous);
81
82 G_DEFINE_TYPE (GtkTearoffMenuItem, gtk_tearoff_menu_item, GTK_TYPE_MENU_ITEM)
83
84 /**
85  * gtk_tearoff_menu_item_new:
86  *
87  * Creates a new #GtkTearoffMenuItem.
88  *
89  * Returns: a new #GtkTearoffMenuItem.
90  *
91  * Deprecated: 3.4: #GtkTearoffMenuItem is deprecated and should not be
92  *     used in newly written code.
93  */
94 GtkWidget*
95 gtk_tearoff_menu_item_new (void)
96 {
97   return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
98 }
99
100 static void
101 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
102 {
103   GtkWidgetClass *widget_class;
104   GtkMenuItemClass *menu_item_class;
105
106   widget_class = (GtkWidgetClass*) klass;
107   menu_item_class = (GtkMenuItemClass*) klass;
108
109   widget_class->draw = gtk_tearoff_menu_item_draw;
110   widget_class->get_preferred_width = gtk_tearoff_menu_item_get_preferred_width;
111   widget_class->get_preferred_height = gtk_tearoff_menu_item_get_preferred_height;
112   widget_class->parent_set = gtk_tearoff_menu_item_parent_set;
113
114   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_TEAR_OFF_MENU_ITEM);
115
116   menu_item_class->activate = gtk_tearoff_menu_item_activate;
117
118   g_type_class_add_private (klass, sizeof (GtkTearoffMenuItemPrivate));
119 }
120
121 static void
122 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
123 {
124   GtkTearoffMenuItemPrivate *priv;
125
126   tearoff_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (tearoff_menu_item,
127                                                          GTK_TYPE_TEAROFF_MENU_ITEM,
128                                                          GtkTearoffMenuItemPrivate);
129   priv = tearoff_menu_item->priv;
130
131   priv->torn_off = FALSE;
132 }
133
134 static void
135 gtk_tearoff_menu_item_get_preferred_width (GtkWidget      *widget,
136                                            gint           *minimum,
137                                            gint           *natural)
138 {
139   GtkStyleContext *context;
140   guint border_width;
141   GtkBorder padding;
142   GtkStateFlags state;
143
144   context = gtk_widget_get_style_context (widget);
145   state = gtk_widget_get_state_flags (widget);
146
147   gtk_style_context_get_padding (context, state, &padding);
148   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
149
150   *minimum = *natural = (border_width + BORDER_SPACING) * 2 + padding.left + padding.right;
151 }
152
153 static void
154 gtk_tearoff_menu_item_get_preferred_height (GtkWidget      *widget,
155                                             gint           *minimum,
156                                             gint           *natural)
157 {
158   GtkStyleContext *context;
159   GtkBorder padding;
160   GtkStateFlags state;
161   GtkWidget *parent;
162   guint border_width;
163
164   context = gtk_widget_get_style_context (widget);
165   state = gtk_widget_get_state_flags (widget);
166
167   gtk_style_context_get_padding (context, state, &padding);
168   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
169
170   *minimum = *natural = (border_width * 2) + padding.top + padding.bottom;
171
172   parent = gtk_widget_get_parent (widget);
173   if (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off)
174     {
175       *minimum += ARROW_SIZE;
176       *natural += ARROW_SIZE;
177     }
178   else
179     {
180       *minimum += padding.top + 4;
181       *natural += padding.top + 4;
182     }
183 }
184
185 static gboolean
186 gtk_tearoff_menu_item_draw (GtkWidget *widget,
187                             cairo_t   *cr)
188 {
189   GtkMenuItem *menu_item;
190   GtkStateFlags state;
191   GtkStyleContext *context;
192   GtkBorder padding;
193   gint x, y, width, height;
194   gint right_max;
195   guint border_width;
196   GtkTextDirection direction;
197   GtkWidget *parent;
198   gdouble angle;
199
200   menu_item = GTK_MENU_ITEM (widget);
201   context = gtk_widget_get_style_context (widget);
202   direction = gtk_widget_get_direction (widget);
203   state = gtk_widget_get_state_flags (widget);
204
205   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_item));
206   x = border_width;
207   y = border_width;
208   width = gtk_widget_get_allocated_width (widget) - border_width * 2;
209   height = gtk_widget_get_allocated_height (widget) - border_width * 2;
210   right_max = x + width;
211
212   gtk_style_context_save (context);
213   gtk_style_context_set_state (context, state);
214   gtk_style_context_get_padding (context, state, &padding);
215
216   if (state & GTK_STATE_FLAG_PRELIGHT)
217     {
218       gtk_render_background (context, cr, x, y, width, height);
219       gtk_render_frame (context, cr, x, y, width, height);
220     }
221
222   parent = gtk_widget_get_parent (widget);
223   if (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off)
224     {
225       gint arrow_x;
226
227       if (menu_item->priv->toggle_size > ARROW_SIZE)
228         {
229           if (direction == GTK_TEXT_DIR_LTR)
230             {
231               arrow_x = x + (menu_item->priv->toggle_size - ARROW_SIZE)/2;
232               angle = (3 * G_PI) / 2;
233             }
234           else
235             {
236               arrow_x = x + width - menu_item->priv->toggle_size + (menu_item->priv->toggle_size - ARROW_SIZE)/2;
237               angle = G_PI / 2;
238             }
239           x += menu_item->priv->toggle_size + BORDER_SPACING;
240         }
241       else
242         {
243           if (direction == GTK_TEXT_DIR_LTR)
244             {
245               arrow_x = ARROW_SIZE / 2;
246               angle = (3 * G_PI) / 2;
247             }
248           else
249             {
250               arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2;
251               angle = G_PI / 2;
252             }
253           x += 2 * ARROW_SIZE;
254         }
255
256       gtk_render_arrow (context, cr, angle,
257                         arrow_x, height / 2 - 5,
258                         ARROW_SIZE);
259     }
260
261   while (x < right_max)
262     {
263       gint x1, x2;
264
265       if (direction == GTK_TEXT_DIR_LTR)
266         {
267           x1 = x;
268           x2 = MIN (x + TEAR_LENGTH, right_max);
269         }
270       else
271         {
272           x1 = right_max - x;
273           x2 = MAX (right_max - x - TEAR_LENGTH, 0);
274         }
275
276       gtk_render_line (context, cr,
277                        x1, y + (height - padding.bottom) / 2,
278                        x2, y + (height - padding.bottom) / 2);
279       x += 2 * TEAR_LENGTH;
280     }
281
282   gtk_style_context_restore (context);
283
284   return FALSE;
285 }
286
287 static void
288 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
289 {
290   GtkWidget *parent;
291
292   parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
293   if (GTK_IS_MENU (parent))
294     {
295       GtkMenu *menu = GTK_MENU (parent);
296
297       gtk_widget_queue_resize (GTK_WIDGET (menu_item));
298       gtk_menu_set_tearoff_state (GTK_MENU (parent),
299                                   !menu->priv->torn_off);
300     }
301 }
302
303 static void
304 tearoff_state_changed (GtkMenu            *menu,
305                        GParamSpec         *pspec,
306                        gpointer            data)
307 {
308   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (data);
309   GtkTearoffMenuItemPrivate *priv = tearoff_menu_item->priv;
310
311   priv->torn_off = gtk_menu_get_tearoff_state (menu);
312 }
313
314 static void
315 gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
316                                   GtkWidget *previous)
317 {
318   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
319   GtkTearoffMenuItemPrivate *priv = tearoff_menu_item->priv;
320   GtkMenu *menu;
321   GtkWidget *parent;
322
323   parent = gtk_widget_get_parent (widget);
324   menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
325
326   if (previous)
327     g_signal_handlers_disconnect_by_func (previous,
328                                           tearoff_state_changed,
329                                           tearoff_menu_item);
330
331   if (menu)
332     {
333       priv->torn_off = gtk_menu_get_tearoff_state (menu);
334       g_signal_connect (menu, "notify::tearoff-state",
335                         G_CALLBACK (tearoff_state_changed),
336                         tearoff_menu_item);
337     }
338 }