]> Pileus Git - ~andy/gtk/blob - gtk/gtktearoffmenuitem.c
add vertical_padding style property.
[~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 "gtkmenu.h"
28 #include "gtktearoffmenuitem.h"
29
30 #define ARROW_SIZE 10
31 #define TEAR_LENGTH 5
32 #define BORDER_SPACING  3
33
34 static void gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass);
35 static void gtk_tearoff_menu_item_init       (GtkTearoffMenuItem      *tearoff_menu_item);
36 static void gtk_tearoff_menu_item_size_request (GtkWidget             *widget,
37                                                 GtkRequisition        *requisition);
38 static gint gtk_tearoff_menu_item_expose     (GtkWidget             *widget,
39                                               GdkEventExpose        *event);
40 static void gtk_tearoff_menu_item_activate   (GtkMenuItem           *menu_item);
41 static gint gtk_tearoff_menu_item_delete_cb  (GtkMenuItem           *menu_item,
42                                               GdkEventAny           *event);
43
44 GType
45 gtk_tearoff_menu_item_get_type (void)
46 {
47   static GType tearoff_menu_item_type = 0;
48
49   if (!tearoff_menu_item_type)
50     {
51       static const GTypeInfo tearoff_menu_item_info =
52       {
53         sizeof (GtkTearoffMenuItemClass),
54         NULL,           /* base_init */
55         NULL,           /* base_finalize */
56         (GClassInitFunc) gtk_tearoff_menu_item_class_init,
57         NULL,           /* class_finalize */
58         NULL,           /* class_data */
59         sizeof (GtkTearoffMenuItem),
60         0,              /* n_preallocs */
61         (GInstanceInitFunc) gtk_tearoff_menu_item_init,
62       };
63
64       tearoff_menu_item_type =
65         g_type_register_static (GTK_TYPE_MENU_ITEM, "GtkTearoffMenuItem",
66                                 &tearoff_menu_item_info, 0);
67     }
68
69   return tearoff_menu_item_type;
70 }
71
72 GtkWidget*
73 gtk_tearoff_menu_item_new (void)
74 {
75   return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
76 }
77
78 static void
79 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
80 {
81   GtkObjectClass *object_class;
82   GtkWidgetClass *widget_class;
83   GtkMenuItemClass *menu_item_class;
84
85   object_class = (GtkObjectClass*) klass;
86   widget_class = (GtkWidgetClass*) klass;
87   menu_item_class = (GtkMenuItemClass*) klass;
88
89   widget_class->expose_event = gtk_tearoff_menu_item_expose;
90   widget_class->size_request = gtk_tearoff_menu_item_size_request;
91
92   menu_item_class->activate = gtk_tearoff_menu_item_activate;
93 }
94
95 static void
96 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
97 {
98   tearoff_menu_item->torn_off = FALSE;
99 }
100
101 static void
102 gtk_tearoff_menu_item_size_request (GtkWidget      *widget,
103                                     GtkRequisition *requisition)
104 {
105   GtkTearoffMenuItem *tearoff;
106
107   tearoff = GTK_TEAROFF_MENU_ITEM (widget);
108   
109   requisition->width = (GTK_CONTAINER (widget)->border_width +
110                         widget->style->xthickness +
111                         BORDER_SPACING) * 2;
112   requisition->height = (GTK_CONTAINER (widget)->border_width +
113                          widget->style->ythickness) * 2;
114
115   if (tearoff->torn_off)
116     {
117       requisition->height += ARROW_SIZE;
118     }
119   else
120     {
121       requisition->height += widget->style->ythickness + 4;
122     }
123 }
124
125 static void
126 gtk_tearoff_menu_item_paint (GtkWidget   *widget,
127                              GdkRectangle *area)
128 {
129   GtkMenuItem *menu_item;
130   GtkTearoffMenuItem *tearoff_item;
131   GtkShadowType shadow_type;
132   gint width, height;
133   gint x, y;
134   gint right_max;
135   GtkArrowType arrow_type;
136   GtkTextDirection direction;
137   
138   if (GTK_WIDGET_DRAWABLE (widget))
139     {
140       menu_item = GTK_MENU_ITEM (widget);
141       tearoff_item = GTK_TEAROFF_MENU_ITEM (widget);
142
143       direction = gtk_widget_get_direction (widget);
144
145       x = widget->allocation.x + GTK_CONTAINER (menu_item)->border_width;
146       y = widget->allocation.y + GTK_CONTAINER (menu_item)->border_width;
147       width = widget->allocation.width - x * 2;
148       height = widget->allocation.height - y * 2;
149       right_max = x + width;
150
151       if (widget->state == GTK_STATE_PRELIGHT)
152         {
153           gint selected_shadow_type;
154           
155           gtk_widget_style_get (widget,
156                                 "selected_shadow_type", &selected_shadow_type,
157                                 NULL);
158           gtk_paint_box (widget->style,
159                          widget->window,
160                          GTK_STATE_PRELIGHT,
161                          selected_shadow_type,
162                          area, widget, "menuitem",
163                          x, y, width, height);
164         }
165       else
166         gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
167
168       if (tearoff_item->torn_off)
169         {
170           gint arrow_x;
171
172           if (widget->state == GTK_STATE_PRELIGHT)
173             shadow_type = GTK_SHADOW_IN;
174           else
175             shadow_type = GTK_SHADOW_OUT;
176
177           if (menu_item->toggle_size > ARROW_SIZE)
178             {
179               if (direction == GTK_TEXT_DIR_LTR) {
180                 arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
181                 arrow_type = GTK_ARROW_LEFT;
182               }
183               else {
184                 arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2; 
185                 arrow_type = GTK_ARROW_RIGHT;       
186               }
187               x += menu_item->toggle_size + BORDER_SPACING;
188             }
189           else
190             {
191               if (direction == GTK_TEXT_DIR_LTR) {
192                 arrow_x = ARROW_SIZE / 2;
193                 arrow_type = GTK_ARROW_LEFT;
194               }
195               else {
196                 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2; 
197                 arrow_type = GTK_ARROW_RIGHT;       
198               }
199               x += 2 * ARROW_SIZE;
200             }
201
202
203           gtk_paint_arrow (widget->style, widget->window,
204                            widget->state, shadow_type,
205                            NULL, widget, "tearoffmenuitem",
206                            arrow_type, FALSE,
207                            arrow_x, y + height / 2 - 5, 
208                            ARROW_SIZE, ARROW_SIZE);
209         }
210
211       while (x < right_max)
212         {
213           gint x1, x2;
214
215           if (direction == GTK_TEXT_DIR_LTR) {
216             x1 = x;
217             x2 = MIN (x + TEAR_LENGTH, right_max);
218           }
219           else {
220             x1 = right_max - x;
221             x2 = MAX (right_max - x - TEAR_LENGTH, 0);
222           }
223           
224           gtk_paint_hline (widget->style, widget->window, GTK_STATE_NORMAL,
225                            NULL, widget, "tearoffmenuitem",
226                            x1, x2, y + (height - widget->style->ythickness) / 2);
227           x += 2 * TEAR_LENGTH;
228         }
229     }
230 }
231
232 static gint
233 gtk_tearoff_menu_item_expose (GtkWidget      *widget,
234                             GdkEventExpose *event)
235 {
236   gtk_tearoff_menu_item_paint (widget, &event->area);
237
238   return FALSE;
239 }
240
241 static gint
242 gtk_tearoff_menu_item_delete_cb (GtkMenuItem *menu_item, GdkEventAny *event)
243 {
244   gtk_tearoff_menu_item_activate (menu_item);
245   return TRUE;
246 }
247
248 static void
249 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
250 {
251   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (menu_item);
252
253   tearoff_menu_item->torn_off = !tearoff_menu_item->torn_off;
254   gtk_widget_queue_resize (GTK_WIDGET (menu_item));
255
256   if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
257     {
258       GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
259       gboolean need_connect;
260       
261         need_connect = (tearoff_menu_item->torn_off && !menu->tearoff_window);
262
263         gtk_menu_set_tearoff_state (GTK_MENU (GTK_WIDGET (menu_item)->parent),
264                                     tearoff_menu_item->torn_off);
265
266         if (need_connect)
267           g_signal_connect_swapped (menu->tearoff_window,
268                                     "delete_event",
269                                     G_CALLBACK (gtk_tearoff_menu_item_delete_cb),
270                                     menu_item);
271     }
272 }
273