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