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