1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
29 #include "gtkmenuprivate.h"
30 #include "gtkmenuitemprivate.h"
31 #include "gtktearoffmenuitem.h"
36 #define BORDER_SPACING 3
38 struct _GtkTearoffMenuItemPrivate
43 static void gtk_tearoff_menu_item_get_preferred_width (GtkWidget *widget,
46 static void gtk_tearoff_menu_item_get_preferred_height (GtkWidget *widget,
49 static gboolean gtk_tearoff_menu_item_draw (GtkWidget *widget,
51 static void gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item);
52 static void gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
55 G_DEFINE_TYPE (GtkTearoffMenuItem, gtk_tearoff_menu_item, GTK_TYPE_MENU_ITEM)
58 gtk_tearoff_menu_item_new (void)
60 return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
64 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
66 GtkWidgetClass *widget_class;
67 GtkMenuItemClass *menu_item_class;
69 widget_class = (GtkWidgetClass*) klass;
70 menu_item_class = (GtkMenuItemClass*) klass;
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;
77 menu_item_class->activate = gtk_tearoff_menu_item_activate;
79 g_type_class_add_private (klass, sizeof (GtkTearoffMenuItemPrivate));
83 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
85 GtkTearoffMenuItemPrivate *priv;
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;
92 priv->torn_off = FALSE;
96 gtk_tearoff_menu_item_get_preferred_width (GtkWidget *widget,
100 GtkStyleContext *context;
105 context = gtk_widget_get_style_context (widget);
106 state = gtk_widget_get_state_flags (widget);
108 gtk_style_context_get_padding (context, state, &padding);
109 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
111 *minimum = *natural = (border_width + BORDER_SPACING) * 2 + padding.left + padding.right;
115 gtk_tearoff_menu_item_get_preferred_height (GtkWidget *widget,
119 GtkStyleContext *context;
125 context = gtk_widget_get_style_context (widget);
126 state = gtk_widget_get_state_flags (widget);
128 gtk_style_context_get_padding (context, state, &padding);
129 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
131 *minimum = *natural = (border_width * 2) + padding.top + padding.bottom;
133 parent = gtk_widget_get_parent (widget);
134 if (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off)
136 *minimum += ARROW_SIZE;
137 *natural += ARROW_SIZE;
141 *minimum += padding.top + 4;
142 *natural += padding.top + 4;
147 gtk_tearoff_menu_item_draw (GtkWidget *widget,
150 GtkMenuItem *menu_item;
152 GtkStyleContext *context;
154 gint x, y, width, height;
157 GtkTextDirection direction;
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);
166 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_item));
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;
173 gtk_style_context_save (context);
174 gtk_style_context_set_state (context, state);
175 gtk_style_context_get_padding (context, state, &padding);
177 if (state & GTK_STATE_FLAG_PRELIGHT)
179 gtk_render_background (context, cr, x, y, width, height);
180 gtk_render_frame (context, cr, x, y, width, height);
183 parent = gtk_widget_get_parent (widget);
184 if (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off)
188 if (menu_item->priv->toggle_size > ARROW_SIZE)
190 if (direction == GTK_TEXT_DIR_LTR)
192 arrow_x = x + (menu_item->priv->toggle_size - ARROW_SIZE)/2;
193 angle = (3 * G_PI) / 2;
197 arrow_x = x + width - menu_item->priv->toggle_size + (menu_item->priv->toggle_size - ARROW_SIZE)/2;
200 x += menu_item->priv->toggle_size + BORDER_SPACING;
204 if (direction == GTK_TEXT_DIR_LTR)
206 arrow_x = ARROW_SIZE / 2;
207 angle = (3 * G_PI) / 2;
211 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2;
217 gtk_render_arrow (context, cr, angle,
218 arrow_x, height / 2 - 5,
222 while (x < right_max)
226 if (direction == GTK_TEXT_DIR_LTR)
229 x2 = MIN (x + TEAR_LENGTH, right_max);
234 x2 = MAX (right_max - x - TEAR_LENGTH, 0);
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;
243 gtk_style_context_restore (context);
249 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
253 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
254 if (GTK_IS_MENU (parent))
256 GtkMenu *menu = GTK_MENU (parent);
258 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
259 gtk_menu_set_tearoff_state (GTK_MENU (parent),
260 !menu->priv->torn_off);
265 tearoff_state_changed (GtkMenu *menu,
269 GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (data);
270 GtkTearoffMenuItemPrivate *priv = tearoff_menu_item->priv;
272 priv->torn_off = gtk_menu_get_tearoff_state (menu);
276 gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
279 GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
280 GtkTearoffMenuItemPrivate *priv = tearoff_menu_item->priv;
284 parent = gtk_widget_get_parent (widget);
285 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
288 g_signal_handlers_disconnect_by_func (previous,
289 tearoff_state_changed,
294 priv->torn_off = gtk_menu_get_tearoff_state (menu);
295 g_signal_connect (menu, "notify::tearoff-state",
296 G_CALLBACK (tearoff_state_changed),