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