]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubutton.c
Improve GtkMenuButton menu positioning
[~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 *menu;
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_MENU,
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_MENU:
189         gtk_menu_button_set_menu (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_MENU:
216         g_value_set_object (value, priv->menu);
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->menu)
240     gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
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   GtkRequisition menu_req;
253   GtkTextDirection direction;
254   GdkRectangle monitor;
255   gint monitor_num;
256   GdkScreen *screen;
257   GdkWindow *window;
258   GtkAllocation allocation, arrow_allocation;
259   GtkAlign align;
260
261   gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
262                                  &menu_req, NULL);
263
264   align = gtk_widget_get_halign (GTK_WIDGET (priv->menu));
265   direction = gtk_widget_get_direction (widget);
266   window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
267
268   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
269   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
270   if (monitor_num < 0)
271     monitor_num = 0;
272   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
273
274   gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation);
275   gtk_widget_get_allocation (widget, &arrow_allocation);
276
277   gdk_window_get_origin (window, x, y);
278   *x += allocation.x;
279   *y += allocation.y;
280
281   /* treat the default align value like START */
282   if (align == GTK_ALIGN_FILL)
283     align = GTK_ALIGN_START;
284
285   if (align == GTK_ALIGN_CENTER)
286     *x -= (menu_req.width - allocation.width) / 2;
287   else if ((align == GTK_ALIGN_START && direction == GTK_TEXT_DIR_LTR) ||
288            (align == GTK_ALIGN_END && direction == GTK_TEXT_DIR_RTL))
289     *x += MAX (allocation.width - menu_req.width, 0);
290   else if (menu_req.width > allocation.width)
291     *x -= menu_req.width - allocation.width;
292
293   if (priv->arrow_type == GTK_ARROW_UP && *y - menu_req.height >= monitor.y)
294     {
295       *y -= menu_req.height;
296     }
297   else
298     {
299       if ((*y + arrow_allocation.height + menu_req.height) <= monitor.y + monitor.height)
300         *y += arrow_allocation.height;
301       else if ((*y - menu_req.height) >= monitor.y)
302         *y -= menu_req.height;
303       else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
304         *y += arrow_allocation.height;
305       else
306         *y -= menu_req.height;
307     }
308
309   *push_in = FALSE;
310 }
311
312 static void
313 menu_position_side_func (GtkMenu       *menu,
314                          gint          *x,
315                          gint          *y,
316                          gboolean      *push_in,
317                          GtkMenuButton *menu_button)
318 {
319   GtkMenuButtonPrivate *priv = menu_button->priv;
320   GtkAllocation allocation;
321   GtkWidget *widget = GTK_WIDGET (menu_button);
322   GtkRequisition menu_req;
323   GdkRectangle monitor;
324   gint monitor_num;
325   GdkScreen *screen;
326   GdkWindow *window;
327   GtkAlign align;
328
329   gtk_widget_get_preferred_size (GTK_WIDGET (priv->menu),
330                                  &menu_req, NULL);
331
332   window = gtk_widget_get_window (widget);
333
334   align = gtk_widget_get_valign (GTK_WIDGET (menu));
335   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
336   monitor_num = gdk_screen_get_monitor_at_window (screen, window);
337   if (monitor_num < 0)
338     monitor_num = 0;
339   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
340
341   gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (menu_button)), x, y);
342
343   gtk_widget_get_allocation (widget, &allocation);
344
345   if (priv->arrow_type == GTK_ARROW_RIGHT)
346     {
347       if (*x + allocation.width + menu_req.width <= monitor.x + monitor.width)
348         *x += allocation.width;
349       else
350         *x -= menu_req.width;
351     }
352   else
353     {
354       if (*x - menu_req.width >= monitor.x)
355         *x -= menu_req.width;
356       else
357         *x += allocation.width;
358     }
359
360   /* treat the default align value like START */
361   if (align == GTK_ALIGN_FILL)
362     align = GTK_ALIGN_START;
363
364   if (align == GTK_ALIGN_CENTER)
365     *y -= (menu_req.height - allocation.height) / 2;
366   else if (align == GTK_ALIGN_END)
367     *y -= menu_req.height - allocation.height;
368
369   *push_in = FALSE;
370 }
371
372 static void
373 popup_menu (GtkMenuButton  *menu_button,
374             GdkEventButton *event)
375 {
376   GtkMenuButtonPrivate *priv = menu_button->priv;
377   GtkMenuPositionFunc func;
378
379   if (priv->func)
380     priv->func (priv->user_data);
381
382   if (!priv->menu)
383     return;
384
385   switch (priv->arrow_type)
386     {
387       case GTK_ARROW_LEFT:
388       case GTK_ARROW_RIGHT:
389         func = (GtkMenuPositionFunc) menu_position_side_func;
390         break;
391       default:
392         func = (GtkMenuPositionFunc) menu_position_up_down_func;
393         break;
394   }
395
396   gtk_menu_popup_for_device (GTK_MENU (priv->menu),
397                              event ? event->device : NULL,
398                              NULL, NULL,
399                              func,
400                              GTK_WIDGET (menu_button),
401                              NULL,
402                              event ? event->button : 0,
403                              event ? event->time : gtk_get_current_event_time ());
404 }
405
406 static void
407 gtk_menu_button_toggled (GtkToggleButton *button)
408 {
409   GtkMenuButton *menu_button = GTK_MENU_BUTTON (button);
410   GtkMenuButtonPrivate *priv = menu_button->priv;
411
412   if (!priv->menu)
413     return;
414
415   if (gtk_toggle_button_get_active (button) &&
416       !gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
417     {
418       /* we get here only when the menu is activated by a key
419        * press, so that we can select the first menu item
420        */
421       popup_menu (menu_button, NULL);
422       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
423     }
424 }
425
426 static gboolean
427 gtk_menu_button_button_press_event (GtkWidget      *widget,
428                                     GdkEventButton *event)
429 {
430   if (event->button == GDK_BUTTON_PRIMARY)
431     {
432       popup_menu (GTK_MENU_BUTTON (widget), event);
433       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
434
435       return TRUE;
436     }
437
438   return GTK_WIDGET_CLASS (gtk_menu_button_parent_class)->button_press_event (widget, event);
439 }
440
441 static void
442 gtk_menu_button_class_init (GtkMenuButtonClass *klass)
443 {
444   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
445   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
446   GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);
447
448   g_type_class_add_private (klass, sizeof (GtkMenuButtonPrivate));
449
450   gobject_class->set_property = gtk_menu_button_set_property;
451   gobject_class->get_property = gtk_menu_button_get_property;
452   gobject_class->dispose = gtk_menu_button_dispose;
453
454   widget_class->state_flags_changed = gtk_menu_button_state_flags_changed;
455   widget_class->button_press_event = gtk_menu_button_button_press_event;
456
457   toggle_button_class->toggled = gtk_menu_button_toggled;
458
459   /**
460    * GtkMenuButton:menu:
461    *
462    * The #GtkMenu that will be popped up when the button is clicked.
463    *
464    * Since: 3.6
465    */
466   g_object_class_install_property (gobject_class,
467                                    PROP_MENU,
468                                    g_param_spec_object ("menu",
469                                                         P_("menu"),
470                                                         P_("The dropdown menu."),
471                                                         GTK_TYPE_MENU,
472                                                         G_PARAM_READWRITE));
473   /**
474    * GtkMenuButton:menu-model:
475    *
476    * The #GMenuModel from which the menu to pop up will be created.
477    * See gtk_menu_button_set_menu_model() for the interaction with the
478    * #GtkMenuButton:menu property.
479    *
480    * Since: 3.6
481    */
482   g_object_class_install_property (gobject_class,
483                                    PROP_MODEL,
484                                    g_param_spec_object ("menu-model",
485                                                         P_("menu-model"),
486                                                         P_("The dropdown menu's model."),
487                                                         G_TYPE_MENU_MODEL,
488                                                         G_PARAM_READWRITE));
489   /**
490    * GtkMenuButton:align-widget:
491    *
492    * The #GtkWidget to use to align the popup menu with.
493    *
494    * Since: 3.6
495    */
496   g_object_class_install_property (gobject_class,
497                                    PROP_ALIGN_WIDGET,
498                                    g_param_spec_object ("align-widget",
499                                                         P_("align-widget"),
500                                                         P_("The parent widget which the menu should align with."),
501                                                         GTK_TYPE_CONTAINER,
502                                                         G_PARAM_READWRITE));
503   /**
504    * GtkMenuButton:direction:
505    *
506    * The #GtkArrowType representing the direction in which the
507    * menu will be popped out.
508    *
509    * Since: 3.6
510    */
511   g_object_class_install_property (gobject_class,
512                                    PROP_DIRECTION,
513                                    g_param_spec_enum ("direction",
514                                                       P_("direction"),
515                                                       P_("The direction the arrow should point."),
516                                                       GTK_TYPE_ARROW_TYPE,
517                                                       GTK_ARROW_DOWN,
518                                                       G_PARAM_READWRITE));
519 }
520
521 static void
522 add_arrow (GtkMenuButton *menu_button)
523 {
524   GtkWidget *arrow;
525
526   arrow = gtk_arrow_new (menu_button->priv->arrow_type, GTK_SHADOW_NONE);
527   gtk_container_add (GTK_CONTAINER (menu_button), arrow);
528   gtk_widget_show (arrow);
529   menu_button->priv->arrow_widget = arrow;
530 }
531
532 static void
533 gtk_menu_button_init (GtkMenuButton *menu_button)
534 {
535   GtkMenuButtonPrivate *priv;
536
537   priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_button, GTK_TYPE_MENU_BUTTON, GtkMenuButtonPrivate);
538   menu_button->priv = priv;
539   priv->arrow_type = GTK_ARROW_DOWN;
540
541   add_arrow (menu_button);
542
543   gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
544 }
545
546 /**
547  * gtk_menu_button_new:
548  *
549  * Creates a new #GtkMenuButton widget with downwards-pointing
550  * arrow as the only child. You can replace the child widget
551  * with another #GtkWidget should you wish to.
552  *
553  * Returns: The newly created #GtkMenuButton widget.
554  *
555  * Since: 3.6
556  */
557 GtkWidget *
558 gtk_menu_button_new (void)
559 {
560   return g_object_new (GTK_TYPE_MENU_BUTTON, NULL);
561 }
562
563 /* Callback for the "deactivate" signal on the pop-up menu.
564  * This is used so that we unset the state of the toggle button
565  * when the pop-up menu disappears.
566  */
567 static int
568 menu_deactivate_cb (GtkMenuShell  *menu_shell,
569                     GtkMenuButton *menu_button)
570 {
571   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button), FALSE);
572
573   return TRUE;
574 }
575
576 static void
577 menu_detacher (GtkWidget *widget,
578                GtkMenu   *menu)
579 {
580   GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (widget)->priv;
581
582   g_return_if_fail (priv->menu == (GtkWidget *) menu);
583
584   priv->menu = NULL;
585 }
586
587 /* This function is used in GtkMenuToolButton, the call back will
588  * be called when GtkMenuToolButton would have emitted the "show-menu"
589  * signal.
590  */
591 void
592 _gtk_menu_button_set_menu_with_func (GtkMenuButton                 *menu_button,
593                                      GtkWidget                     *menu,
594                                      GtkMenuButtonShowMenuCallback  func,
595                                      gpointer                       user_data)
596 {
597   GtkMenuButtonPrivate *priv;
598
599   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
600   g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
601
602   priv = menu_button->priv;
603   priv->func = func;
604   priv->user_data = user_data;
605
606   if (priv->menu == GTK_WIDGET (menu))
607     return;
608
609   if (priv->menu)
610     {
611       if (gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
612         gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
613     }
614
615   if (priv->menu)
616     {
617       g_signal_handlers_disconnect_by_func (priv->menu,
618                                             menu_deactivate_cb,
619                                             menu_button);
620       gtk_menu_detach (GTK_MENU (priv->menu));
621     }
622
623   priv->menu = menu;
624
625   if (priv->menu)
626     {
627       gtk_menu_attach_to_widget (GTK_MENU (priv->menu), GTK_WIDGET (menu_button),
628                                  menu_detacher);
629
630       gtk_widget_set_sensitive (GTK_WIDGET (menu_button), TRUE);
631
632       g_signal_connect (priv->menu, "deactivate",
633                         G_CALLBACK (menu_deactivate_cb), menu_button);
634     }
635   else
636     {
637       gtk_widget_set_sensitive (GTK_WIDGET (menu_button), FALSE);
638     }
639
640   g_object_notify (G_OBJECT (menu_button), "menu");
641   g_object_notify (G_OBJECT (menu_button), "menu-model");
642 }
643
644 /**
645  * gtk_menu_button_set_menu:
646  * @menu_button: a #GtkMenuButton
647  * @menu: (allow-none): a #GtkMenu
648  *
649  * Sets the #GtkMenu that will be popped up when the button is clicked,
650  * or %NULL to disable the button. If #GtkMenuButton:menu-model is set,
651  * it will be set to %NULL.
652  *
653  * Since: 3.6
654  */
655 void
656 gtk_menu_button_set_menu (GtkMenuButton *menu_button,
657                           GtkWidget     *menu)
658 {
659   GtkMenuButtonPrivate *priv;
660
661   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
662   g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);
663
664   priv = menu_button->priv;
665   g_clear_object (&priv->model);
666
667   _gtk_menu_button_set_menu_with_func (menu_button, menu, NULL, NULL);
668 }
669
670 /**
671  * gtk_menu_button_get_menu:
672  * @menu_button: a #GtkMenuButton
673  *
674  * Returns the #GtkMenu that pops out of the button.
675  *
676  * Returns: (transfer none): a #GtkMenu or %NULL.
677  *
678  * Since: 3.6
679  */
680 GtkMenu *
681 gtk_menu_button_get_menu (GtkMenuButton *menu_button)
682 {
683   g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
684
685   return GTK_MENU (menu_button->priv->menu);
686 }
687
688 /**
689  * gtk_menu_button_set_menu_model:
690  * @menu_button: a #GtkMenuButton
691  * @menu_model: (allow-none): a #GMenuModel
692  *
693  * Sets the #GMenuModel from which the #GtkMenuButton:menu property will be
694  * filled in, or %NULL to disable the button.
695  *
696  * The #GtkMenu will be created with gtk_menu_new_from_model(), so actions
697  * will be connected as documented there.
698  *
699  * If you #GtkMenuButton:menu * is already set, then its content will be lost
700  * and replaced by our newly created #GtkMenu.
701  *
702  * Since: 3.6
703  */
704 void
705 gtk_menu_button_set_menu_model (GtkMenuButton *menu_button,
706                                 GMenuModel    *menu_model)
707 {
708   GtkMenuButtonPrivate *priv;
709   GtkWidget *menu;
710
711   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
712   g_return_if_fail (G_IS_MENU_MODEL (menu_model) || menu_model == NULL);
713
714   priv = menu_button->priv;
715   g_clear_object (&priv->model);
716
717   if (menu_model == NULL)
718     {
719       gtk_menu_button_set_menu (menu_button, NULL);
720       return;
721     }
722
723   priv->model = g_object_ref (menu_model);
724   menu = gtk_menu_new_from_model (menu_model);
725   gtk_widget_show_all (menu);
726   gtk_menu_button_set_menu (menu_button, menu);
727 }
728
729 /**
730  * gtk_menu_button_get_menu_model:
731  * @menu_button: a #GtkMenuButton
732  *
733  * Returns the #GMenuModel used to generate the menu.
734  *
735  * Returns: (transfer none): a #GMenuModel or %NULL.
736  *
737  * Since: 3.6
738  */
739 GMenuModel *
740 gtk_menu_button_get_menu_model (GtkMenuButton *menu_button)
741 {
742   g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
743
744   return menu_button->priv->model;
745 }
746
747 /**
748  * gtk_menu_button_set_align_widget:
749  * @menu_button: a #GtkMenuButton
750  * @align_widget: (allow-none): a #GtkWidget
751  *
752  * Sets the #GtkWidget to use to line the menu with when popped up. Note that
753  * the @align_widget must contain the #GtkMenuButton itself.
754  *
755  * Setting it to %NULL means that the popup menu will be aligned with the
756  * button itself.
757  *
758  * Since: 3.6
759  */
760 void
761 gtk_menu_button_set_align_widget (GtkMenuButton *menu_button,
762                                   GtkWidget     *align_widget)
763 {
764   GtkMenuButtonPrivate *priv;
765
766   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
767   g_return_if_fail (align_widget == NULL || gtk_widget_is_ancestor (GTK_WIDGET (menu_button), align_widget));
768
769   priv = menu_button->priv;
770   if (priv->align_widget == align_widget)
771     return;
772
773   priv->align_widget = align_widget;
774
775   if (priv->align_widget)
776     g_object_add_weak_pointer (G_OBJECT (priv->align_widget), (gpointer *) &priv->align_widget);
777
778   g_object_notify (G_OBJECT (menu_button), "align-widget");
779 }
780
781 /**
782  * gtk_menu_button_get_align_widget:
783  * @menu_button: a #GtkMenuButton
784  *
785  * Returns the parent #GtkWidget to use to line up with menu.
786  *
787  * Returns: (transfer none): a #GtkWidget value or %NULL.
788  *
789  * Since: 3.6
790  */
791 GtkWidget *
792 gtk_menu_button_get_align_widget (GtkMenuButton *menu_button)
793 {
794   g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), NULL);
795
796   return menu_button->priv->align_widget;
797 }
798
799 /**
800  * gtk_menu_button_set_direction:
801  * @menu_button: a #GtkMenuButton
802  * @direction: a #GtkArrowType
803  *
804  * Sets the direction in which the menu will be popped up, as
805  * well as changing the arrow's direction. The child will not
806  * be changed to an arrow if it was customized.
807  *
808  * If the menu when popped out would have collided with screen edges,
809  * we will do our best to keep it inside the screen and fully visible.
810  *
811  * If you pass GTK_ARROW_NONE for a @direction, the menu will behave
812  * as if you passed GTK_ARROW_DOWN (although you won't see any arrows).
813  *
814  * Since: 3.6
815  */
816 void
817 gtk_menu_button_set_direction (GtkMenuButton *menu_button,
818                                GtkArrowType   direction)
819 {
820   GtkMenuButtonPrivate *priv = menu_button->priv;
821   GtkWidget *child;
822
823   g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
824
825   if (priv->arrow_type == direction)
826     return;
827
828   priv->arrow_type = direction;
829
830   /* Is it custom content? We don't change that */
831   child = gtk_bin_get_child (GTK_BIN (menu_button));
832   if (priv->arrow_widget != child)
833     return;
834
835   gtk_arrow_set (GTK_ARROW (child), priv->arrow_type, GTK_SHADOW_NONE);
836 }
837
838 /**
839  * gtk_menu_button_get_direction:
840  * @menu_button: a #GtkMenuButton
841  *
842  * Returns the direction the menu will be pointing at when popped up.
843  *
844  * Returns: a #GtkArrowType value.
845  *
846  * Since: 3.6
847  */
848 GtkArrowType
849 gtk_menu_button_get_direction (GtkMenuButton *menu_button)
850 {
851   g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), GTK_ARROW_DOWN);
852
853   return menu_button->priv->arrow_type;
854 }
855
856 static void
857 gtk_menu_button_dispose (GObject *object)
858 {
859   GtkMenuButtonPrivate *priv = GTK_MENU_BUTTON (object)->priv;
860
861   if (priv->menu)
862     {
863       g_signal_handlers_disconnect_by_func (priv->menu,
864                                             menu_deactivate_cb,
865                                             object);
866       gtk_menu_detach (GTK_MENU (priv->menu));
867     }
868
869   g_clear_object (&priv->model);
870
871   G_OBJECT_CLASS (gtk_menu_button_parent_class)->dispose (object);
872 }