]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubutton.c
[l10n] Updated German translation
[~andy/gtk] / gtk / gtkmenubutton.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2003 Ricardo Fernandez Pascual
4  * Copyright (C) 2004 Paolo Borelli
5  * Copyright (C) 2012 Bastien Nocera
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /**
22  * SECTION:gtkmenubutton
23  * @short_description: A widget that shows a menu when clicked on
24  * @title: GtkMenuButton
25  *
26  * The #GtkMenuButton widget is used to display a menu when clicked on.
27  * This menu can be provided either as a #GtkMenu, or an abstract #GMenuModel.
28  *
29  * The #GtkMenuButton widget can hold any valid child widget. That is, it
30  * can hold almost any other standard #GtkWidget. The most commonly used
31  * child is the provided #GtkArrow.
32  *
33  * The positioning of the menu is determined by the #GtkMenuButton:direction
34  * property of the menu button and the #GtkWidget:halign or #GtkWidget:valign
35  * properties of the menu. For example, when the direction is %GTK_ARROW_DOWN
36  * and the horizontal alignment is %GTK_ALIGN_START, the menu will be
37  * positioned below the button, with the starting edge (depending on the
38  * text direction) of the menu aligned with the starting edge of the button.
39  * If there is not enough space below the button, the menu is popped up above
40  * the button instead. If the alignment would move part of the menu offscreen,
41  * it is 'pushed in'.
42  *
43  * <informaltable>
44  *   <tgroup cols="4">
45  *     <tbody>
46  *       <row>
47  *         <entry></entry>
48  *         <entry>halign = start</entry>
49  *         <entry>halign = center</entry>
50  *         <entry>halign = end</entry>
51  *       </row>
52  *     <row>
53  *       <entry>direction = down</entry>
54  *       <entry>
55  *         <inlinemediaobject>
56  *           <imageobject><imagedata fileref="down-start.png" format="PNG"/></imageobject>
57  *         </inlinemediaobject>
58  *       </entry>
59  *       <entry>
60  *         <inlinemediaobject>
61  *           <imageobject><imagedata fileref="down-center.png" format="PNG"/></imageobject>
62  *         </inlinemediaobject>
63  *       </entry>
64  *       <entry>
65  *         <inlinemediaobject>
66  *           <imageobject><imagedata fileref="down-end.png" format="PNG"/></imageobject>
67  *         </inlinemediaobject>
68  *       </entry>
69  *     </row>
70  *     <row>
71  *       <entry>direction = up</entry>
72  *       <entry>
73  *         <inlinemediaobject>
74  *           <imageobject><imagedata fileref="up-start.png" format="PNG"/></imageobject>
75  *         </inlinemediaobject>
76  *       </entry>
77  *       <entry>
78  *         <inlinemediaobject>
79  *           <imageobject><imagedata fileref="up-center.png" format="PNG"/></imageobject>
80  *         </inlinemediaobject>
81  *       </entry>
82  *       <entry>
83  *         <inlinemediaobject>
84  *           <imageobject><imagedata fileref="up-end.png" format="PNG"/></imageobject>
85  *         </inlinemediaobject>
86  *       </entry>
87  *      </row>
88  *     </tbody>
89  *   </tgroup>
90  * </informaltable>
91  * <informaltable>
92  *   <tgroup cols="3">
93  *     <tbody>
94  *       <row>
95  *         <entry></entry>
96  *         <entry>direction = left</entry>
97  *         <entry>direction = right</entry>
98  *       </row>
99  *     <row>
100  *       <entry>valign = start</entry>
101  *       <entry>
102  *         <inlinemediaobject>
103  *           <imageobject><imagedata fileref="left-start.png" format="PNG"/></imageobject>
104  *         </inlinemediaobject>
105  *       </entry>
106  *       <entry>
107  *         <inlinemediaobject>
108  *           <imageobject><imagedata fileref="right-start.png" format="PNG"/></imageobject>
109  *         </inlinemediaobject>
110  *       </entry>
111  *     </row>
112  *     <row>
113  *       <entry>valign = center</entry>
114  *       <entry>
115  *         <inlinemediaobject>
116  *           <imageobject><imagedata fileref="left-center.png" format="PNG"/></imageobject>
117  *         </inlinemediaobject>
118  *       </entry>
119  *       <entry>
120  *         <inlinemediaobject>
121  *           <imageobject><imagedata fileref="right-center.png" format="PNG"/></imageobject>
122  *         </inlinemediaobject>
123  *       </entry>
124  *     </row>
125  *     <row>
126  *       <entry>valign = end</entry>
127  *       <entry>
128  *         <inlinemediaobject>
129  *           <imageobject><imagedata fileref="left-end.png" format="PNG"/></imageobject>
130  *         </inlinemediaobject>
131  *       </entry>
132  *       <entry>
133  *         <inlinemediaobject>
134  *           <imageobject><imagedata fileref="right-end.png" format="PNG"/></imageobject>
135  *         </inlinemediaobject>
136  *       </entry>
137  *      </row>
138  *     </tbody>
139  *   </tgroup>
140  * </informaltable>
141  */
142
143 #include "config.h"
144
145 #include "gtkmenubutton.h"
146 #include "gtkmenubuttonprivate.h"
147 #include "gtkarrow.h"
148
149 #include "gtkprivate.h"
150 #include "gtkintl.h"
151
152 struct _GtkMenuButtonPrivate
153 {
154   GtkWidget *popup;
155   GMenuModel *model;
156
157   GtkMenuButtonShowMenuCallback func;
158   gpointer user_data;
159
160   GtkArrowType arrow_type;
161   GtkWidget *align_widget;
162   gpointer arrow_widget;
163 };
164
165 enum
166 {
167   PROP_0,
168   PROP_POPUP,
169   PROP_MODEL,
170   PROP_ALIGN_WIDGET,
171   PROP_DIRECTION
172 };
173
174 G_DEFINE_TYPE(GtkMenuButton, gtk_menu_button, GTK_TYPE_TOGGLE_BUTTON)
175
176 static void gtk_menu_button_dispose (GObject *object);
177
178 static void
179 gtk_menu_button_set_property (GObject      *object,
180                               guint         property_id,
181                               const GValue *value,
182                               GParamSpec   *pspec)
183 {
184   GtkMenuButton *self = GTK_MENU_BUTTON (object);
185
186   switch (property_id)
187     {
188       case PROP_POPUP:
189         gtk_menu_button_set_popup (self, g_value_get_object (value));
190         break;
191       case PROP_MODEL:
192         gtk_menu_button_set_menu_model (self, g_value_get_object (value));
193         break;
194       case PROP_ALIGN_WIDGET:
195         gtk_menu_button_set_align_widget (self, g_value_get_object (value));
196         break;
197       case PROP_DIRECTION:
198         gtk_menu_button_set_direction (self, g_value_get_enum (value));
199         break;
200       default:
201         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
202     }
203 }
204
205 static void
206 gtk_menu_button_get_property (GObject    *object,
207                               guint       property_id,
208                               GValue     *value,
209                               GParamSpec *pspec)
210 {
211   GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;
212
213   switch (property_id)
214     {
215       case PROP_POPUP:
216         g_value_set_object (value, priv->popup);
217         break;
218       case PROP_MODEL:
219         g_value_set_object (value, priv->model);
220         break;
221       case PROP_ALIGN_WIDGET:
222         g_value_set_object (value, priv->align_widget);
223         break;
224       case PROP_DIRECTION:
225         g_value_set_enum (value, priv->arrow_type);
226         break;
227       default:
228         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
229     }
230 }
231
232 static void
233 gtk_menu_button_state_flags_changed (GtkWidget    *widget,
234                                      GtkStateFlags previous_state_flags)
235 {
236   GtkMenuButton *button = GTK_MENU_BUTTON (widget);
237   GtkMenuButtonPrivate *priv = button->priv;
238
239   if (!gtk_widget_is_sensitive (widget) && priv->popup)
240     gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup));
241 }
242
243 static void
244 menu_position_up_down_func (GtkMenu       *menu,
245                             gint          *x,
246                             gint          *y,
247                             gboolean      *push_in,
248                             GtkMenuButton *menu_button)
249 {
250   GtkMenuButtonPrivate *priv = menu_button->priv;
251   GtkWidget *widget = GTK_WIDGET (menu_button);
252   GtkWidget *toplevel;
253   GtkRequisition menu_req;
254   GtkTextDirection direction;
255   GdkRectangle monitor;
256   gint monitor_num;
257   GdkScreen *screen;
258   GdkWindow *window;
259   GtkAllocation allocation, arrow_allocation;
260   GtkAlign align;
261
262   /* In the common case the menu button is showing a dropdown menu, set the
263    * corresponding type hint on the toplevel, so the WM can omit the top side
264    * of the shadows.
265    */
266   if (priv->arrow_type == GTK_ARROW_DOWN)
267     {
268       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (priv->popup));
269       gtk_window_set_type_hint (GTK_WINDOW (toplevel), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
270     }
271
272   gtk_widget_get_preferred_size (GTK_WIDGET (priv->popup),
273                                  &menu_req, NULL);
274
275   align = gtk_widget_get_halign (GTK_WIDGET (priv->popup));
276   direction = gtk_widget_get_direction (widget);
277   window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
278
279   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
280   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
281   if (monitor_num < 0)
282     monitor_num = 0;
283   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
284
285   gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation);
286   gtk_widget_get_allocation (widget, &arrow_allocation);
287
288   gdk_window_get_origin (window, x, y);
289   *x += allocation.x;
290   *y += allocation.y;
291
292   /* treat the default align value like START */
293   if (align == GTK_ALIGN_FILL)
294     align = GTK_ALIGN_START;
295
296   if (align == GTK_ALIGN_CENTER)
297     *x -= (menu_req.width - allocation.width) / 2;
298   else if ((align == GTK_ALIGN_START && direction == GTK_TEXT_DIR_LTR) ||
299            (align == GTK_ALIGN_END && direction == GTK_TEXT_DIR_RTL))
300     *x += MAX (allocation.width - menu_req.width, 0);
301   else if (menu_req.width > allocation.width)
302     *x -= menu_req.width - allocation.width;
303
304   if (priv->arrow_type == GTK_ARROW_UP && *y - menu_req.height >= monitor.y)
305     {
306       *y -= menu_req.height;
307     }
308   else
309     {
310       if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
311         *y += arrow_allocation.height;
312       else if ((*y - menu_req.height) >= monitor.y)
313         *y -= menu_req.height;
314       else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
315         *y += arrow_allocation.height;
316       else
317         *y -= menu_req.height;
318     }
319
320   *push_in = FALSE;
321 }
322
323 static void
324 menu_position_side_func (GtkMenu       *menu,
325                          gint          *x,
326                          gint          *y,
327                          gboolean      *push_in,
328                          GtkMenuButton *menu_button)
329 {
330   GtkMenuButtonPrivate *priv = menu_button->priv;
331   GtkAllocation allocation;
332   GtkWidget *widget = GTK_WIDGET (menu_button);
333   GtkRequisition menu_req;
334   GdkRectangle monitor;
335   gint monitor_num;
336   GdkScreen *screen;
337   GdkWindow *window;
338   GtkAlign align;
339   GtkTextDirection direction;
340
341   gtk_widget_get_preferred_size (GTK_WIDGET (priv->popup),
342                                  &menu_req, NULL);
343
344   window = gtk_widget_get_window (widget);
345
346   direction = gtk_widget_get_direction (widget);
347   align = gtk_widget_get_valign (GTK_WIDGET (menu));
348   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
349   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
350   if (monitor_num < 0)
351     monitor_num = 0;
352   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
353
354   gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (menu_button)), x, y);
355
356   gtk_widget_get_allocation (widget, &allocation);
357
358   if ((priv->arrow_type == GTK_ARROW_RIGHT && direction == GTK_TEXT_DIR_LTR) ||
359       (priv->arrow_type == GTK_ARROW_LEFT && direction == GTK_TEXT_DIR_RTL))
360
361     {
362       if (*x + allocation.width + menu_req.width <= monitor.x + monitor.width)
363         *x += allocation.width;
364       else
365         *x -= menu_req.width;
366     }
367   else
368     {
369       if (*x - menu_req.width >= monitor.x)
370         *x -= menu_req.width;
371       else
372         *x += allocation.width;
373     }
374
375   /* treat the default align value like START */
376   if (align == GTK_ALIGN_FILL)
377     align = GTK_ALIGN_START;
378
379   if (align == GTK_ALIGN_CENTER)
380     *y -= (menu_req.height - allocation.height) / 2;
381   else if (align == GTK_ALIGN_END)
382     *y -= menu_req.height - allocation.height;
383
384   *push_in = FALSE;
385 }
386
387 static void
388 popup_menu (GtkMenuButton  *menu_button,
389             GdkEventButton *event)
390 {
391   GtkMenuButtonPrivate *priv = menu_button->priv;
392   GtkMenuPositionFunc func;
393
394   if (priv->func)
395     priv->func (priv->user_data);
396
397   if (!priv->popup)
398     return;
399
400   switch (priv->arrow_type)
401     {
402       case GTK_ARROW_LEFT:
403       case GTK_ARROW_RIGHT:
404         func = (GtkMenuPositionFunc) menu_position_side_func;
405         break;
406       default:
407         func = (GtkMenuPositionFunc) menu_position_up_down_func;
408         break;
409   }
410
411   gtk_menu_popup_for_device (GTK_MENU (priv->popup),
412                              event ? event->device : NULL,
413                              NULL, NULL,
414                              func,
415                              GTK_WIDGET (menu_button),
416                              NULL,
417                              event ? event->button : 0,
418                              event ? event->time : gtk_get_current_event_time ());
419 }
420
421 static void
422 gtk_menu_button_toggled (GtkToggleButton *button)
423 {
424   GtkMenuButton *menu_button = GTK_MENU_BUTTON (button);
425   GtkMenuButtonPrivate *priv = menu_button->priv;
426
427   if (!priv->popup)
428     return;
429
430   if (gtk_toggle_button_get_active (button) &&
431       !gtk_widget_get_visible (GTK_WIDGET (priv->popup)))
432     {
433       /* we get here only when the menu is activated by a key
434        * press, so that we can select the first menu item
435        */
436       popup_menu (menu_button, NULL);
437       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup), FALSE);
438     }
439 }
440
441 static gboolean
442 gtk_menu_button_button_press_event (GtkWidget      *widget,
443                                     GdkEventButton *event)
444 {
445   if (event->button == GDK_BUTTON_PRIMARY)
446     {
447       popup_menu (GTK_MENU_BUTTON (widget), event);
448       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
449
450       return TRUE;
451     }
452
453   return GTK_WIDGET_CLASS (gtk_menu_button_parent_class)->button_press_event (widget, event);
454 }
455
456 static void
457 gtk_menu_button_add (GtkContainer *container,
458                      GtkWidget    *child)
459 {
460   GtkMenuButton *button = GTK_MENU_BUTTON (container);
461
462   if (button->priv->arrow_widget)
463     gtk_container_remove (container, button->priv->arrow_widget);
464
465   GTK_CONTAINER_CLASS (gtk_menu_button_parent_class)->add (container, child);
466 }
467
468 static void
469 gtk_menu_button_remove (GtkContainer *container,
470                         GtkWidget    *child)
471 {
472   GtkMenuButton *button = GTK_MENU_BUTTON (container);
473
474   if (child == button->priv->arrow_widget)
475     button->priv->arrow_widget = NULL;
476
477   GTK_CONTAINER_CLASS (gtk_menu_button_parent_class)->remove (container, child);
478 }
479
480 static void
481 gtk_menu_button_class_init (GtkMenuButtonClass *klass)
482 {
483   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
484   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
485   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
486   GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);
487
488   g_type_class_add_private (klass, sizeof (GtkMenuButtonPrivate));
489
490   gobject_class->set_property = gtk_menu_button_set_property;
491   gobject_class->get_property = gtk_menu_button_get_property;
492   gobject_class->dispose = gtk_menu_button_dispose;
493
494   widget_class->state_flags_changed = gtk_menu_button_state_flags_changed;
495   widget_class->button_press_event = gtk_menu_button_button_press_event;
496
497   container_class->add = gtk_menu_button_add;
498   container_class->remove = gtk_menu_button_remove;
499
500   toggle_button_class->toggled = gtk_menu_button_toggled;
501
502   /**
503    * GtkMenuButton:popup:
504    *
505    * The #GtkMenu that will be popped up when the button is clicked.
506    *
507    * Since: 3.6
508    */
509   g_object_class_install_property (gobject_class,
510                                    PROP_POPUP,
511                                    g_param_spec_object ("popup",
512                                                         P_("popup"),
513                                                         P_("The dropdown menu."),
514                                                         GTK_TYPE_MENU,
515                                                         G_PARAM_READWRITE));
516    /**
517    * GtkMenuButton:menu:
518    *
519    * The #GtkMenu that will be popped up when the button is clicked.
520    * This property has been renamed to "popup".  "menu" will be
521    * removed before 3.6.0.
522    *
523    * Since: 3.6
524    */
525   g_object_class_install_property (gobject_class,
526                                    PROP_POPUP, /* [sic] */
527                                    g_param_spec_object ("menu",
528                                                         P_("menu"),
529                                                         P_("The dropdown menu."),
530                                                         GTK_TYPE_MENU,
531                                                         G_PARAM_DEPRECATED | G_PARAM_READWRITE));
532   /**
533    * GtkMenuButton:menu-model:
534    *
535    * The #GMenuModel from which the menu to pop up will be created.
536    * See gtk_menu_button_set_menu_model() for the interaction with the
537    * #GtkMenuButton:menu property.
538    *
539    * Since: 3.6
540    */
541   g_object_class_install_property (gobject_class,
542                                    PROP_MODEL,
543                                    g_param_spec_object ("menu-model",
544                                                         P_("menu-model"),
545                                                         P_("The dropdown menu's model."),
546                                                         G_TYPE_MENU_MODEL,
547                                                         G_PARAM_READWRITE));
548   /**
549    * GtkMenuButton:align-widget:
550    *
551    * The #GtkWidget to use to align the popup menu with.
552    *
553    * Since: 3.6
554    */
555   g_object_class_install_property (gobject_class,
556                                    PROP_ALIGN_WIDGET,
557                                    g_param_spec_object ("align-widget",
558                                                         P_("align-widget"),
559                                                         P_("The parent widget which the menu should align with."),
560                                                         GTK_TYPE_CONTAINER,
561                                                         G_PARAM_READWRITE));
562   /**
563    * GtkMenuButton:direction:
564    *
565    * The #GtkArrowType representing the direction in which the
566    * menu will be popped out.
567    *
568    * Since: 3.6
569    */
570   g_object_class_install_property (gobject_class,
571                                    PROP_DIRECTION,
572                                    g_param_spec_enum ("direction",
573                                                       P_("direction"),
574                                                       P_("The direction the arrow should point."),
575                                                       GTK_TYPE_ARROW_TYPE,
576                                                       GTK_ARROW_DOWN,
577                                                       G_PARAM_READWRITE));
578 }
579
580 static void
581 add_arrow (GtkMenuButton *menu_button)
582 {
583   GtkWidget *arrow;
584
585   arrow = gtk_arrow_new (menu_button->priv->arrow_type, GTK_SHADOW_NONE);
586   gtk_container_add (GTK_CONTAINER (menu_button), arrow);
587   gtk_widget_show (arrow);
588   menu_button->priv->arrow_widget = arrow;
589 }
590
591 static void
592 gtk_menu_button_init (GtkMenuButton *menu_button)
593 {
594   GtkMenuButtonPrivate *priv;
595
596   priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_button, GTK_TYPE_MENU_BUTTON, GtkMenuButtonPrivate);
597   menu_button->priv = priv;
598   priv->arrow_type = GTK_ARROW_DOWN;
599
600   add_arrow (menu_button);
601
602   gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
603 }
604
605 /**
606  * gtk_menu_button_new:
607  *
608  * Creates a new #GtkMenuButton widget with downwards-pointing
609  * arrow as the only child. You can replace the child widget
610  * with another #GtkWidget should you wish to.
611  *
612  * Returns: The newly created #GtkMenuButton widget.
613  *
614  * Since: 3.6
615  */
616 GtkWidget *
617 gtk_menu_button_new (void)
618 {
619   return g_object_new (GTK_TYPE_MENU_BUTTON, NULL);
620 }
621
622 /* Callback for the "deactivate" signal on the pop-up menu.
623  * This is used so that we unset the state of the toggle button
624  * when the pop-up menu disappears.
625  */
626 static int
627 menu_deactivate_cb (GtkMenuShell  *menu_shell,
628                     GtkMenuButton *menu_button)
629 {
630   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);
631
632   return TRUE;
633 }
634
635 static void
636 menu_detacher (GtkWidget *widget,
637                GtkMenu   *menu)
638 {
639   GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (widget)->priv;
640
641   g_return_if_fail (priv->popup == (GtkWidget *) menu);
642
643   priv->popup = NULL;
644 }
645
646 /* This function is used in GtkMenuToolButton, the call back will
647  * be called when GtkMenuToolButton would have emitted the "show-menu"
648  * signal.
649  */
650 void
651 _gtk_menu_button_set_popup_with_func (GtkMenuButton                 *menu_button,
652                                       GtkWidget                     *menu,
653                                       GtkMenuButtonShowMenuCallback  func,
654                                       gpointer                       user_data)
655 {
656   GtkMenuButtonPrivate *priv;
657
658   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
659   g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
660
661   priv = menu_button->priv;
662   priv->func = func;
663   priv->user_data = user_data;
664
665   if (priv->popup == GTK_WIDGET (menu))
666     return;
667
668   if (priv->popup)
669     {
670       if (gtk_widget_get_visible (GTK_WIDGET (priv->popup)))
671         gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup));
672     }
673
674   if (priv->popup)
675     {
676       g_signal_handlers_disconnect_by_func (priv->popup,
677                                             menu_deactivate_cb,
678                                             menu_button);
679       gtk_menu_detach (GTK_MENU (priv->popup));
680     }
681
682   priv->popup = menu;
683
684   if (priv->popup)
685     {
686       gtk_menu_attach_to_widget (GTK_MENU (priv->popup), GTK_WIDGET (menu_button),
687                                  menu_detacher);
688
689       gtk_widget_set_sensitive (GTK_WIDGET (menu_button), TRUE);
690
691       g_signal_connect (priv->popup, "deactivate",
692                         G_CALLBACK (menu_deactivate_cb), menu_button);
693     }
694   else
695     {
696       gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
697     }
698
699   g_object_notify (G_OBJECT (menu_button), "menu");
700   g_object_notify (G_OBJECT (menu_button), "menu-model");
701 }
702
703 /**
704  * gtk_menu_button_set_popup:
705  * @menu_button: a #GtkMenuButton
706  * @popup: (allow-none): a #GtkMenu
707  *
708  * Sets the #GtkMenu that will be popped up when the button is clicked,
709  * or %NULL to disable the button. If #GtkMenuButton:menu-model is set,
710  * it will be set to %NULL.
711  *
712  * Since: 3.6
713  */
714 void
715 gtk_menu_button_set_popup (GtkMenuButton *menu_button,
716                            GtkWidget     *popup)
717 {
718   GtkMenuButtonPrivate *priv;
719
720   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
721   g_return_if_fail (GTK_IS_MENU (popup) || popup == NULL);
722
723   priv = menu_button->priv;
724   g_clear_object (&priv->model);
725
726   _gtk_menu_button_set_popup_with_func (menu_button, popup, NULL, NULL);
727 }
728
729 /**
730  * gtk_menu_button_get_popup:
731  * @menu_button: a #GtkMenuButton
732  *
733  * Returns the #GtkMenu that pops out of the button.
734  *
735  * Returns: (transfer none): a #GtkMenu or %NULL.
736  *
737  * Since: 3.6
738  */
739 GtkMenu *
740 gtk_menu_button_get_popup (GtkMenuButton *menu_button)
741 {
742   g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
743
744   return GTK_MENU (menu_button->priv->popup);
745 }
746
747 void
748 gtk_menu_button_set_menu (GtkMenuButton *menu_button,
749                           GtkWidget     *menu)
750 {
751   gtk_menu_button_set_popup (menu_button, menu);
752 }
753
754 GtkMenu *
755 gtk_menu_button_get_menu (GtkMenuButton *menu_button)
756 {
757   return gtk_menu_button_get_popup (menu_button);
758 }
759
760 /**
761  * gtk_menu_button_set_menu_model:
762  * @menu_button: a #GtkMenuButton
763  * @menu_model: (allow-none): a #GMenuModel
764  *
765  * Sets the #GMenuModel from which the #GtkMenuButton:menu property will be
766  * filled in, or %NULL to disable the button.
767  *
768  * The #GtkMenu will be created with gtk_menu_new_from_model(), so actions
769  * will be connected as documented there.
770  *
771  * If you #GtkMenuButton:menu * is already set, then its content will be lost
772  * and replaced by our newly created #GtkMenu.
773  *
774  * Since: 3.6
775  */
776 void
777 gtk_menu_button_set_menu_model (GtkMenuButton *menu_button,
778                                 GMenuModel    *menu_model)
779 {
780   GtkMenuButtonPrivate *priv;
781   GtkWidget *menu;
782
783   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
784   g_return_if_fail (G_IS_MENU_MODEL (menu_model) || menu_model == NULL);
785
786   priv = menu_button->priv;
787   g_clear_object (&priv->model);
788
789   if (menu_model == NULL)
790     {
791       gtk_menu_button_set_popup (menu_button, NULL);
792       return;
793     }
794
795   priv->model = g_object_ref (menu_model);
796   menu = gtk_menu_new_from_model (menu_model);
797   gtk_widget_show_all (menu);
798   gtk_menu_button_set_popup (menu_button, menu);
799 }
800
801 /**
802  * gtk_menu_button_get_menu_model:
803  * @menu_button: a #GtkMenuButton
804  *
805  * Returns the #GMenuModel used to generate the menu.
806  *
807  * Returns: (transfer none): a #GMenuModel or %NULL.
808  *
809  * Since: 3.6
810  */
811 GMenuModel *
812 gtk_menu_button_get_menu_model (GtkMenuButton *menu_button)
813 {
814   g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
815
816   return menu_button->priv->model;
817 }
818
819 /**
820  * gtk_menu_button_set_align_widget:
821  * @menu_button: a #GtkMenuButton
822  * @align_widget: (allow-none): a #GtkWidget
823  *
824  * Sets the #GtkWidget to use to line the menu with when popped up. Note that
825  * the @align_widget must contain the #GtkMenuButton itself.
826  *
827  * Setting it to %NULL means that the popup menu will be aligned with the
828  * button itself.
829  *
830  * Since: 3.6
831  */
832 void
833 gtk_menu_button_set_align_widget (GtkMenuButton *menu_button,
834                                   GtkWidget     *align_widget)
835 {
836   GtkMenuButtonPrivate *priv;
837
838   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
839   g_return_if_fail (align_widget == NULL || gtk_widget_is_ancestor (GTK_WIDGET (menu_button), align_widget));
840
841   priv = menu_button->priv;
842   if (priv->align_widget == align_widget)
843     return;
844
845   priv->align_widget = align_widget;
846
847   if (priv->align_widget)
848     g_object_add_weak_pointer (G_OBJECT (priv->align_widget), (gpointer *) &priv->align_widget);
849
850   g_object_notify (G_OBJECT (menu_button), "align-widget");
851 }
852
853 /**
854  * gtk_menu_button_get_align_widget:
855  * @menu_button: a #GtkMenuButton
856  *
857  * Returns the parent #GtkWidget to use to line up with menu.
858  *
859  * Returns: (transfer none): a #GtkWidget value or %NULL.
860  *
861  * Since: 3.6
862  */
863 GtkWidget *
864 gtk_menu_button_get_align_widget (GtkMenuButton *menu_button)
865 {
866   g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
867
868   return menu_button->priv->align_widget;
869 }
870
871 /**
872  * gtk_menu_button_set_direction:
873  * @menu_button: a #GtkMenuButton
874  * @direction: a #GtkArrowType
875  *
876  * Sets the direction in which the menu will be popped up, as
877  * well as changing the arrow's direction. The child will not
878  * be changed to an arrow if it was customized.
879  *
880  * If the menu when popped out would have collided with screen edges,
881  * we will do our best to keep it inside the screen and fully visible.
882  *
883  * If you pass GTK_ARROW_NONE for a @direction, the menu will behave
884  * as if you passed GTK_ARROW_DOWN (although you won't see any arrows).
885  *
886  * Since: 3.6
887  */
888 void
889 gtk_menu_button_set_direction (GtkMenuButton *menu_button,
890                                GtkArrowType   direction)
891 {
892   GtkMenuButtonPrivate *priv = menu_button->priv;
893   GtkWidget *child;
894
895   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
896
897   if (priv->arrow_type == direction)
898     return;
899
900   priv->arrow_type = direction;
901
902   /* Is it custom content? We don't change that */
903   child = gtk_bin_get_child (GTK_BIN (menu_button));
904   if (priv->arrow_widget != child)
905     return;
906
907   gtk_arrow_set (GTK_ARROW (child), priv->arrow_type, GTK_SHADOW_NONE);
908 }
909
910 /**
911  * gtk_menu_button_get_direction:
912  * @menu_button: a #GtkMenuButton
913  *
914  * Returns the direction the menu will be pointing at when popped up.
915  *
916  * Returns: a #GtkArrowType value.
917  *
918  * Since: 3.6
919  */
920 GtkArrowType
921 gtk_menu_button_get_direction (GtkMenuButton *menu_button)
922 {
923   g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), GTK_ARROW_DOWN);
924
925   return menu_button->priv->arrow_type;
926 }
927
928 static void
929 gtk_menu_button_dispose (GObject *object)
930 {
931   GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;
932
933   if (priv->popup)
934     {
935       g_signal_handlers_disconnect_by_func (priv->popup,
936                                             menu_deactivate_cb,
937                                             object);
938       gtk_menu_detach (GTK_MENU (priv->popup));
939     }
940
941   g_clear_object (&priv->model);
942
943   G_OBJECT_CLASS (gtk_menu_button_parent_class)->dispose (object);
944 }