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