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 static void gtk_tearoff_menu_item_size_request (GtkWidget *widget,
38 GtkRequisition *requisition);
39 static gint gtk_tearoff_menu_item_expose (GtkWidget *widget,
40 GdkEventExpose *event);
41 static void gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item);
42 static void gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
45 G_DEFINE_TYPE (GtkTearoffMenuItem, gtk_tearoff_menu_item, GTK_TYPE_MENU_ITEM)
48 gtk_tearoff_menu_item_new (void)
50 return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
54 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
56 GtkWidgetClass *widget_class;
57 GtkMenuItemClass *menu_item_class;
59 widget_class = (GtkWidgetClass*) klass;
60 menu_item_class = (GtkMenuItemClass*) klass;
62 widget_class->expose_event = gtk_tearoff_menu_item_expose;
63 widget_class->size_request = gtk_tearoff_menu_item_size_request;
64 widget_class->parent_set = gtk_tearoff_menu_item_parent_set;
66 menu_item_class->activate = gtk_tearoff_menu_item_activate;
70 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
72 tearoff_menu_item->torn_off = FALSE;
76 gtk_tearoff_menu_item_size_request (GtkWidget *widget,
77 GtkRequisition *requisition)
81 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
82 requisition->width = (border_width + widget->style->xthickness + BORDER_SPACING) * 2;
83 requisition->height = (border_width + widget->style->ythickness) * 2;
85 if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
87 requisition->height += ARROW_SIZE;
91 requisition->height += widget->style->ythickness + 4;
96 gtk_tearoff_menu_item_paint (GtkWidget *widget,
99 GtkMenuItem *menu_item;
100 GtkShadowType shadow_type;
105 GtkArrowType arrow_type;
106 GtkTextDirection direction;
108 if (gtk_widget_is_drawable (widget))
110 menu_item = GTK_MENU_ITEM (widget);
112 direction = gtk_widget_get_direction (widget);
114 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_item));
115 x = widget->allocation.x + border_width;
116 y = widget->allocation.y + border_width;
117 width = widget->allocation.width - border_width * 2;
118 height = widget->allocation.height - border_width * 2;
119 right_max = x + width;
121 if (widget->state == GTK_STATE_PRELIGHT)
123 gint selected_shadow_type;
125 gtk_widget_style_get (widget,
126 "selected-shadow-type", &selected_shadow_type,
128 gtk_paint_box (widget->style,
131 selected_shadow_type,
132 area, widget, "menuitem",
133 x, y, width, height);
136 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
138 if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
142 if (widget->state == GTK_STATE_PRELIGHT)
143 shadow_type = GTK_SHADOW_IN;
145 shadow_type = GTK_SHADOW_OUT;
147 if (menu_item->toggle_size > ARROW_SIZE)
149 if (direction == GTK_TEXT_DIR_LTR) {
150 arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
151 arrow_type = GTK_ARROW_LEFT;
154 arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2;
155 arrow_type = GTK_ARROW_RIGHT;
157 x += menu_item->toggle_size + BORDER_SPACING;
161 if (direction == GTK_TEXT_DIR_LTR) {
162 arrow_x = ARROW_SIZE / 2;
163 arrow_type = GTK_ARROW_LEFT;
166 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2;
167 arrow_type = GTK_ARROW_RIGHT;
173 gtk_paint_arrow (widget->style, widget->window,
174 widget->state, shadow_type,
175 NULL, widget, "tearoffmenuitem",
177 arrow_x, y + height / 2 - 5,
178 ARROW_SIZE, ARROW_SIZE);
181 while (x < right_max)
185 if (direction == GTK_TEXT_DIR_LTR) {
187 x2 = MIN (x + TEAR_LENGTH, right_max);
191 x2 = MAX (right_max - x - TEAR_LENGTH, 0);
194 gtk_paint_hline (widget->style, widget->window, GTK_STATE_NORMAL,
195 NULL, widget, "tearoffmenuitem",
196 x1, x2, y + (height - widget->style->ythickness) / 2);
197 x += 2 * TEAR_LENGTH;
203 gtk_tearoff_menu_item_expose (GtkWidget *widget,
204 GdkEventExpose *event)
206 gtk_tearoff_menu_item_paint (widget, &event->area);
212 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
214 if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
216 GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
218 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
219 gtk_menu_set_tearoff_state (GTK_MENU (GTK_WIDGET (menu_item)->parent),
225 tearoff_state_changed (GtkMenu *menu,
229 GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (data);
231 tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
235 gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
238 GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
239 GtkMenu *menu = GTK_IS_MENU (widget->parent) ? GTK_MENU (widget->parent) : NULL;
242 g_signal_handlers_disconnect_by_func (previous,
243 tearoff_state_changed,
248 tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
249 g_signal_connect (menu, "notify::tearoff-state",
250 G_CALLBACK (tearoff_state_changed),