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