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