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/.
30 #include "gtktearoffmenuitem.h"
35 #define BORDER_SPACING 3
37 struct _GtkTearoffMenuItemPrivate
42 static void gtk_tearoff_menu_item_size_request (GtkWidget *widget,
43 GtkRequisition *requisition);
44 static gboolean gtk_tearoff_menu_item_draw (GtkWidget *widget,
46 static void gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item);
47 static void gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
50 G_DEFINE_TYPE (GtkTearoffMenuItem, gtk_tearoff_menu_item, GTK_TYPE_MENU_ITEM)
53 gtk_tearoff_menu_item_new (void)
55 return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
59 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
61 GtkWidgetClass *widget_class;
62 GtkMenuItemClass *menu_item_class;
64 widget_class = (GtkWidgetClass*) klass;
65 menu_item_class = (GtkMenuItemClass*) klass;
67 widget_class->draw = gtk_tearoff_menu_item_draw;
68 widget_class->size_request = gtk_tearoff_menu_item_size_request;
69 widget_class->parent_set = gtk_tearoff_menu_item_parent_set;
71 menu_item_class->activate = gtk_tearoff_menu_item_activate;
73 g_type_class_add_private (klass, sizeof (GtkTearoffMenuItemPrivate));
77 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
79 GtkTearoffMenuItemPrivate *priv;
81 tearoff_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (tearoff_menu_item,
82 GTK_TYPE_TEAROFF_MENU_ITEM,
83 GtkTearoffMenuItemPrivate);
84 priv = tearoff_menu_item->priv;
86 priv->torn_off = FALSE;
90 gtk_tearoff_menu_item_size_request (GtkWidget *widget,
91 GtkRequisition *requisition)
97 style = gtk_widget_get_style (widget);
99 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
100 requisition->width = (border_width + style->xthickness + BORDER_SPACING) * 2;
101 requisition->height = (border_width + style->ythickness) * 2;
103 parent = gtk_widget_get_parent (widget);
104 if (GTK_IS_MENU (parent) && GTK_MENU (parent)->torn_off)
106 requisition->height += ARROW_SIZE;
110 requisition->height += style->ythickness + 4;
115 gtk_tearoff_menu_item_draw (GtkWidget *widget,
118 GtkMenuItem *menu_item;
119 GtkShadowType shadow_type;
122 gint x, y, width, height;
125 GtkArrowType arrow_type;
126 GtkTextDirection direction;
130 menu_item = GTK_MENU_ITEM (widget);
131 style = gtk_widget_get_style (widget);
132 window = gtk_widget_get_window (widget);
133 direction = gtk_widget_get_direction (widget);
134 state = gtk_widget_get_state (widget);
136 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_item));
139 width = gtk_widget_get_allocated_width (widget) - border_width * 2;
140 height = gtk_widget_get_allocated_height (widget) - border_width * 2;
141 right_max = x + width;
143 if (state == GTK_STATE_PRELIGHT)
145 gint selected_shadow_type;
147 gtk_widget_style_get (widget,
148 "selected-shadow-type", &selected_shadow_type,
150 gtk_paint_box (style,
153 selected_shadow_type,
155 x, y, width, height);
158 parent = gtk_widget_get_parent (widget);
159 if (GTK_IS_MENU (parent) && GTK_MENU (parent)->torn_off)
163 if (state == GTK_STATE_PRELIGHT)
164 shadow_type = GTK_SHADOW_IN;
166 shadow_type = GTK_SHADOW_OUT;
168 if (menu_item->toggle_size > ARROW_SIZE)
170 if (direction == GTK_TEXT_DIR_LTR) {
171 arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
172 arrow_type = GTK_ARROW_LEFT;
175 arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2;
176 arrow_type = GTK_ARROW_RIGHT;
178 x += menu_item->toggle_size + BORDER_SPACING;
182 if (direction == GTK_TEXT_DIR_LTR) {
183 arrow_x = ARROW_SIZE / 2;
184 arrow_type = GTK_ARROW_LEFT;
187 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2;
188 arrow_type = GTK_ARROW_RIGHT;
194 gtk_paint_arrow (style, cr,
196 widget, "tearoffmenuitem",
198 arrow_x, y + height / 2 - 5,
199 ARROW_SIZE, ARROW_SIZE);
202 while (x < right_max)
206 if (direction == GTK_TEXT_DIR_LTR) {
208 x2 = MIN (x + TEAR_LENGTH, right_max);
212 x2 = MAX (right_max - x - TEAR_LENGTH, 0);
215 gtk_paint_hline (style, cr, GTK_STATE_NORMAL,
216 widget, "tearoffmenuitem",
217 x1, x2, y + (height - style->ythickness) / 2);
218 x += 2 * TEAR_LENGTH;
225 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
229 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
230 if (GTK_IS_MENU (parent))
232 GtkMenu *menu = GTK_MENU (parent);
234 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
235 gtk_menu_set_tearoff_state (GTK_MENU (parent),
241 tearoff_state_changed (GtkMenu *menu,
245 GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (data);
246 GtkTearoffMenuItemPrivate *priv = tearoff_menu_item->priv;
248 priv->torn_off = gtk_menu_get_tearoff_state (menu);
252 gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
255 GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
256 GtkTearoffMenuItemPrivate *priv = tearoff_menu_item->priv;
260 parent = gtk_widget_get_parent (widget);
261 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
264 g_signal_handlers_disconnect_by_func (previous,
265 tearoff_state_changed,
270 priv->torn_off = gtk_menu_get_tearoff_state (menu);
271 g_signal_connect (menu, "notify::tearoff-state",
272 G_CALLBACK (tearoff_state_changed),