Add default widget handling to popovers

Add a new API, gtk_popover_set_default_widget, that can be
used to make a widget act as default while the popover is
shown. This is useful in dialog-like popovers.

http://bugzilla.gnome.org/show_bug.cgi?id=747664
This commit is contained in:
Matthias Clasen 2015-04-17 00:10:06 -04:00
parent 3682c761ed
commit e1c1fc59e5
3 changed files with 77 additions and 0 deletions

View File

@ -8062,6 +8062,8 @@ gtk_popover_set_modal
gtk_popover_get_modal
gtk_popover_set_transitions_enabled
gtk_popover_get_transitions_enabled
gtk_popover_set_default_widget
gtk_popover_get_default_widget
<SUBSECTION Standard>
GTK_TYPE_POPOVER
GTK_IS_POPOVER

View File

@ -134,6 +134,8 @@ struct _GtkPopoverPrivate
GtkWidget *widget;
GtkWindow *window;
GtkWidget *prev_focus_widget;
GtkWidget *default_widget;
GtkWidget *prev_default;
GtkScrollable *parent_scrollable;
GtkAdjustment *vadj;
GtkAdjustment *hadj;
@ -577,10 +579,17 @@ gtk_popover_set_state (GtkPopover *popover,
static void
gtk_popover_map (GtkWidget *widget)
{
GtkPopoverPrivate *priv = GTK_POPOVER (widget)->priv;
priv->prev_default = gtk_window_get_default_widget (priv->window);
g_object_ref (priv->prev_default);
GTK_WIDGET_CLASS (gtk_popover_parent_class)->map (widget);
gdk_window_show (gtk_widget_get_window (widget));
gtk_popover_update_position (GTK_POPOVER (widget));
gtk_window_set_default (priv->window, priv->default_widget);
}
static void
@ -592,6 +601,9 @@ gtk_popover_unmap (GtkWidget *widget)
gdk_window_hide (gtk_widget_get_window (widget));
GTK_WIDGET_CLASS (gtk_popover_parent_class)->unmap (widget);
gtk_window_set_default (priv->window, priv->prev_default);
g_clear_object (&priv->prev_default);
}
static void
@ -2357,3 +2369,60 @@ gtk_popover_new_from_model (GtkWidget *relative_to,
return popover;
}
/**
* gtk_popover_set_default_widget:
* @popover: a #GtkPopover
* @widget: (allow-none): the new default widget, or %NULL
*
* Sets the widget that should be set as default widget while
* the popover is shown (see gtk_window_set_default()). #GtkPopover
* remembers the previous default widget and reestablishes it
* when the popover is dismissed.
*
* Since: 3.18
*/
void
gtk_popover_set_default_widget (GtkPopover *popover,
GtkWidget *widget)
{
GtkPopoverPrivate *priv = popover->priv;
g_return_if_fail (GTK_IS_POPOVER (popover));
g_return_if_fail (widget == NULL || gtk_widget_get_can_default (widget));
if (priv->default_widget == widget)
return;
if (priv->default_widget)
g_object_unref (priv->default_widget);
priv->default_widget = widget;
if (priv->default_widget)
g_object_ref (priv->default_widget);
if (gtk_widget_get_mapped (GTK_WIDGET (popover)))
gtk_window_set_default (priv->window, priv->default_widget);
}
/**
* gtk_popover_get_default_widget:
* @popover: a #GtkPopover
*
* Gets the widget that should be set as the default while
* the popover is shown.
*
* Returns: (transfer none): the default widget, or %NULL if there is none
*
* Since: 3.18
*/
GtkWidget *
gtk_popover_get_default_widget (GtkPopover *popover)
{
GtkPopoverPrivate *priv = popover->priv;
g_return_if_fail (GTK_IS_POPOVER (popover));
return priv->default_widget;
}

View File

@ -103,6 +103,12 @@ void gtk_popover_set_transitions_enabled (GtkPopover *popover,
GDK_AVAILABLE_IN_3_16
gboolean gtk_popover_get_transitions_enabled (GtkPopover *popover);
GDK_AVAILABLE_IN_3_18
void gtk_popover_set_default_widget (GtkPopover *popover,
GtkWidget *widget);
GDK_AVAILABLE_IN_3_18
GtkWidget * gtk_popover_get_default_widget (GtkPopover *popover);
G_END_DECLS
#endif /* __GTK_POPOVER_H__ */