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"
36 #define BORDER_SPACING 3
38 static void gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass);
39 static void gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item);
40 static void gtk_tearoff_menu_item_size_request (GtkWidget *widget,
41 GtkRequisition *requisition);
42 static gint gtk_tearoff_menu_item_expose (GtkWidget *widget,
43 GdkEventExpose *event);
44 static void gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item);
45 static void gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
49 gtk_tearoff_menu_item_get_type (void)
51 static GType tearoff_menu_item_type = 0;
53 if (!tearoff_menu_item_type)
55 static const GTypeInfo tearoff_menu_item_info =
57 sizeof (GtkTearoffMenuItemClass),
59 NULL, /* base_finalize */
60 (GClassInitFunc) gtk_tearoff_menu_item_class_init,
61 NULL, /* class_finalize */
62 NULL, /* class_data */
63 sizeof (GtkTearoffMenuItem),
65 (GInstanceInitFunc) gtk_tearoff_menu_item_init,
68 tearoff_menu_item_type =
69 g_type_register_static (GTK_TYPE_MENU_ITEM, I_("GtkTearoffMenuItem"),
70 &tearoff_menu_item_info, 0);
73 return tearoff_menu_item_type;
77 gtk_tearoff_menu_item_new (void)
79 return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
83 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
85 GtkWidgetClass *widget_class;
86 GtkMenuItemClass *menu_item_class;
88 widget_class = (GtkWidgetClass*) klass;
89 menu_item_class = (GtkMenuItemClass*) klass;
91 widget_class->expose_event = gtk_tearoff_menu_item_expose;
92 widget_class->size_request = gtk_tearoff_menu_item_size_request;
93 widget_class->parent_set = gtk_tearoff_menu_item_parent_set;
95 menu_item_class->activate = gtk_tearoff_menu_item_activate;
99 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
101 tearoff_menu_item->torn_off = FALSE;
105 gtk_tearoff_menu_item_size_request (GtkWidget *widget,
106 GtkRequisition *requisition)
108 requisition->width = (GTK_CONTAINER (widget)->border_width +
109 widget->style->xthickness +
111 requisition->height = (GTK_CONTAINER (widget)->border_width +
112 widget->style->ythickness) * 2;
114 if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
116 requisition->height += ARROW_SIZE;
120 requisition->height += widget->style->ythickness + 4;
125 gtk_tearoff_menu_item_paint (GtkWidget *widget,
128 GtkMenuItem *menu_item;
129 GtkShadowType shadow_type;
133 GtkArrowType arrow_type;
134 GtkTextDirection direction;
136 if (GTK_WIDGET_DRAWABLE (widget))
138 menu_item = GTK_MENU_ITEM (widget);
140 direction = gtk_widget_get_direction (widget);
142 x = widget->allocation.x + GTK_CONTAINER (menu_item)->border_width;
143 y = widget->allocation.y + GTK_CONTAINER (menu_item)->border_width;
144 width = widget->allocation.width - GTK_CONTAINER (menu_item)->border_width * 2;
145 height = widget->allocation.height - GTK_CONTAINER (menu_item)->border_width * 2;
146 right_max = x + width;
148 if (widget->state == GTK_STATE_PRELIGHT)
150 gint selected_shadow_type;
152 gtk_widget_style_get (widget,
153 "selected-shadow-type", &selected_shadow_type,
155 gtk_paint_box (widget->style,
158 selected_shadow_type,
159 area, widget, "menuitem",
160 x, y, width, height);
163 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
165 if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
169 if (widget->state == GTK_STATE_PRELIGHT)
170 shadow_type = GTK_SHADOW_IN;
172 shadow_type = GTK_SHADOW_OUT;
174 if (menu_item->toggle_size > ARROW_SIZE)
176 if (direction == GTK_TEXT_DIR_LTR) {
177 arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
178 arrow_type = GTK_ARROW_LEFT;
181 arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2;
182 arrow_type = GTK_ARROW_RIGHT;
184 x += menu_item->toggle_size + BORDER_SPACING;
188 if (direction == GTK_TEXT_DIR_LTR) {
189 arrow_x = ARROW_SIZE / 2;
190 arrow_type = GTK_ARROW_LEFT;
193 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2;
194 arrow_type = GTK_ARROW_RIGHT;
200 gtk_paint_arrow (widget->style, widget->window,
201 widget->state, shadow_type,
202 NULL, widget, "tearoffmenuitem",
204 arrow_x, y + height / 2 - 5,
205 ARROW_SIZE, ARROW_SIZE);
208 while (x < right_max)
212 if (direction == GTK_TEXT_DIR_LTR) {
214 x2 = MIN (x + TEAR_LENGTH, right_max);
218 x2 = MAX (right_max - x - TEAR_LENGTH, 0);
221 gtk_paint_hline (widget->style, widget->window, GTK_STATE_NORMAL,
222 NULL, widget, "tearoffmenuitem",
223 x1, x2, y + (height - widget->style->ythickness) / 2);
224 x += 2 * TEAR_LENGTH;
230 gtk_tearoff_menu_item_expose (GtkWidget *widget,
231 GdkEventExpose *event)
233 gtk_tearoff_menu_item_paint (widget, &event->area);
239 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
241 if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
243 GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
245 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
246 gtk_menu_set_tearoff_state (GTK_MENU (GTK_WIDGET (menu_item)->parent),
252 tearoff_state_changed (GtkMenu *menu,
256 GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (data);
258 tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
262 gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
265 GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
266 GtkMenu *menu = GTK_IS_MENU (widget->parent) ? GTK_MENU (widget->parent) : NULL;
269 g_signal_handlers_disconnect_by_func (previous,
270 tearoff_state_changed,
275 tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
276 g_signal_connect (menu, "notify::tearoff-state",
277 G_CALLBACK (tearoff_state_changed),
282 #define __GTK_TEAROFF_MENU_ITEM_C__
283 #include "gtkaliasdef.c"