]> Pileus Git - ~andy/gtk/blob - gtk/gtktearoffmenuitem.c
Practically everything changed.
[~andy/gtk] / gtk / gtktearoffmenuitem.c
1 /* GTK - The GTK+ 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_size_request (GtkWidget             *widget,
39                                                 GtkRequisition        *requisition);
40 static gint gtk_tearoff_menu_item_expose     (GtkWidget             *widget,
41                                               GdkEventExpose        *event);
42 static void gtk_tearoff_menu_item_activate   (GtkMenuItem           *menu_item);
43 static void gtk_tearoff_menu_item_parent_set (GtkWidget             *widget,
44                                               GtkWidget             *previous);
45
46 G_DEFINE_TYPE (GtkTearoffMenuItem, gtk_tearoff_menu_item, GTK_TYPE_MENU_ITEM)
47
48 GtkWidget*
49 gtk_tearoff_menu_item_new (void)
50 {
51   return g_object_new (GTK_TYPE_TEAROFF_MENU_ITEM, NULL);
52 }
53
54 static void
55 gtk_tearoff_menu_item_class_init (GtkTearoffMenuItemClass *klass)
56 {
57   GtkWidgetClass *widget_class;
58   GtkMenuItemClass *menu_item_class;
59
60   widget_class = (GtkWidgetClass*) klass;
61   menu_item_class = (GtkMenuItemClass*) klass;
62
63   widget_class->expose_event = gtk_tearoff_menu_item_expose;
64   widget_class->size_request = gtk_tearoff_menu_item_size_request;
65   widget_class->parent_set = gtk_tearoff_menu_item_parent_set;
66
67   menu_item_class->activate = gtk_tearoff_menu_item_activate;
68 }
69
70 static void
71 gtk_tearoff_menu_item_init (GtkTearoffMenuItem *tearoff_menu_item)
72 {
73   tearoff_menu_item->torn_off = FALSE;
74 }
75
76 static void
77 gtk_tearoff_menu_item_size_request (GtkWidget      *widget,
78                                     GtkRequisition *requisition)
79 {
80   requisition->width = (GTK_CONTAINER (widget)->border_width +
81                         widget->style->xthickness +
82                         BORDER_SPACING) * 2;
83   requisition->height = (GTK_CONTAINER (widget)->border_width +
84                          widget->style->ythickness) * 2;
85
86   if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
87     {
88       requisition->height += ARROW_SIZE;
89     }
90   else
91     {
92       requisition->height += widget->style->ythickness + 4;
93     }
94 }
95
96 static void
97 gtk_tearoff_menu_item_paint (GtkWidget   *widget,
98                              GdkRectangle *area)
99 {
100   GtkMenuItem *menu_item;
101   GtkShadowType shadow_type;
102   gint width, height;
103   gint x, y;
104   gint right_max;
105   GtkArrowType arrow_type;
106   GtkTextDirection direction;
107   
108   if (GTK_WIDGET_DRAWABLE (widget))
109     {
110       menu_item = GTK_MENU_ITEM (widget);
111
112       direction = gtk_widget_get_direction (widget);
113
114       x = widget->allocation.x + GTK_CONTAINER (menu_item)->border_width;
115       y = widget->allocation.y + GTK_CONTAINER (menu_item)->border_width;
116       width = widget->allocation.width - GTK_CONTAINER (menu_item)->border_width * 2;
117       height = widget->allocation.height - GTK_CONTAINER (menu_item)->border_width * 2;
118       right_max = x + width;
119
120       if (widget->state == GTK_STATE_PRELIGHT)
121         {
122           gint selected_shadow_type;
123           
124           gtk_widget_style_get (widget,
125                                 "selected-shadow-type", &selected_shadow_type,
126                                 NULL);
127           gtk_paint_box (widget->style,
128                          widget->window,
129                          GTK_STATE_PRELIGHT,
130                          selected_shadow_type,
131                          area, widget, "menuitem",
132                          x, y, width, height);
133         }
134       else
135         gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
136
137       if (GTK_IS_MENU (widget->parent) && GTK_MENU (widget->parent)->torn_off)
138         {
139           gint arrow_x;
140
141           if (widget->state == GTK_STATE_PRELIGHT)
142             shadow_type = GTK_SHADOW_IN;
143           else
144             shadow_type = GTK_SHADOW_OUT;
145
146           if (menu_item->toggle_size > ARROW_SIZE)
147             {
148               if (direction == GTK_TEXT_DIR_LTR) {
149                 arrow_x = x + (menu_item->toggle_size - ARROW_SIZE)/2;
150                 arrow_type = GTK_ARROW_LEFT;
151               }
152               else {
153                 arrow_x = x + width - menu_item->toggle_size + (menu_item->toggle_size - ARROW_SIZE)/2; 
154                 arrow_type = GTK_ARROW_RIGHT;       
155               }
156               x += menu_item->toggle_size + BORDER_SPACING;
157             }
158           else
159             {
160               if (direction == GTK_TEXT_DIR_LTR) {
161                 arrow_x = ARROW_SIZE / 2;
162                 arrow_type = GTK_ARROW_LEFT;
163               }
164               else {
165                 arrow_x = x + width - 2 * ARROW_SIZE + ARROW_SIZE / 2; 
166                 arrow_type = GTK_ARROW_RIGHT;       
167               }
168               x += 2 * ARROW_SIZE;
169             }
170
171
172           gtk_paint_arrow (widget->style, widget->window,
173                            widget->state, shadow_type,
174                            NULL, widget, "tearoffmenuitem",
175                            arrow_type, FALSE,
176                            arrow_x, y + height / 2 - 5, 
177                            ARROW_SIZE, ARROW_SIZE);
178         }
179
180       while (x < right_max)
181         {
182           gint x1, x2;
183
184           if (direction == GTK_TEXT_DIR_LTR) {
185             x1 = x;
186             x2 = MIN (x + TEAR_LENGTH, right_max);
187           }
188           else {
189             x1 = right_max - x;
190             x2 = MAX (right_max - x - TEAR_LENGTH, 0);
191           }
192           
193           gtk_paint_hline (widget->style, widget->window, GTK_STATE_NORMAL,
194                            NULL, widget, "tearoffmenuitem",
195                            x1, x2, y + (height - widget->style->ythickness) / 2);
196           x += 2 * TEAR_LENGTH;
197         }
198     }
199 }
200
201 static gint
202 gtk_tearoff_menu_item_expose (GtkWidget      *widget,
203                             GdkEventExpose *event)
204 {
205   gtk_tearoff_menu_item_paint (widget, &event->area);
206
207   return FALSE;
208 }
209
210 static void
211 gtk_tearoff_menu_item_activate (GtkMenuItem *menu_item)
212 {
213   if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
214     {
215       GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
216       
217       gtk_widget_queue_resize (GTK_WIDGET (menu_item));
218       gtk_menu_set_tearoff_state (GTK_MENU (GTK_WIDGET (menu_item)->parent),
219                                   !menu->torn_off);
220     }
221 }
222
223 static void
224 tearoff_state_changed (GtkMenu            *menu,
225                        GParamSpec         *pspec,
226                        gpointer            data)
227 {
228   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (data);
229
230   tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
231 }
232
233 static void
234 gtk_tearoff_menu_item_parent_set (GtkWidget *widget,
235                                   GtkWidget *previous)
236 {
237   GtkTearoffMenuItem *tearoff_menu_item = GTK_TEAROFF_MENU_ITEM (widget);
238   GtkMenu *menu = GTK_IS_MENU (widget->parent) ? GTK_MENU (widget->parent) : NULL;
239
240   if (previous)
241     g_signal_handlers_disconnect_by_func (previous, 
242                                           tearoff_state_changed, 
243                                           tearoff_menu_item);
244   
245   if (menu)
246     {
247       tearoff_menu_item->torn_off = gtk_menu_get_tearoff_state (menu);
248       g_signal_connect (menu, "notify::tearoff-state", 
249                         G_CALLBACK (tearoff_state_changed), 
250                         tearoff_menu_item);
251     }  
252 }
253
254 #define __GTK_TEAROFF_MENU_ITEM_C__
255 #include "gtkaliasdef.c"