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