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