2013-04-21 11:51:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "gtkstackswitcher.h"
|
2013-09-26 03:52:39 +00:00
|
|
|
#include "gtkradiobutton.h"
|
|
|
|
#include "gtklabel.h"
|
2016-03-13 05:11:52 +00:00
|
|
|
#include "gtkdnd.h"
|
2016-03-25 19:52:22 +00:00
|
|
|
#include "gtkdragdest.h"
|
2013-09-26 03:52:39 +00:00
|
|
|
#include "gtkorientable.h"
|
2013-04-21 11:51:14 +00:00
|
|
|
#include "gtkprivate.h"
|
|
|
|
#include "gtkintl.h"
|
2017-06-28 06:19:35 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
2017-11-15 19:20:40 +00:00
|
|
|
#include "gtktypebuiltins.h"
|
2018-02-25 10:29:33 +00:00
|
|
|
#include "gtkimage.h"
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-04-21 15:05:38 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gtkstackswitcher
|
2013-04-22 14:23:56 +00:00
|
|
|
* @Short_description: A controller for GtkStack
|
2013-04-21 15:05:38 +00:00
|
|
|
* @Title: GtkStackSwitcher
|
|
|
|
* @See_also: #GtkStack
|
|
|
|
*
|
|
|
|
* The GtkStackSwitcher widget acts as a controller for a
|
|
|
|
* #GtkStack; it shows a row of buttons to switch between
|
|
|
|
* the various pages of the associated stack widget.
|
|
|
|
*
|
|
|
|
* All the content for the buttons comes from the child properties
|
2017-10-10 09:19:53 +00:00
|
|
|
* of the #GtkStack; the button visibility in a #GtkStackSwitcher
|
|
|
|
* widget is controlled by the visibility of the child in the
|
|
|
|
* #GtkStack.
|
2013-04-21 15:05:38 +00:00
|
|
|
*
|
|
|
|
* It is possible to associate multiple #GtkStackSwitcher widgets
|
2017-10-10 09:19:53 +00:00
|
|
|
* with the same #GtkStack widget.
|
2013-04-21 15:05:38 +00:00
|
|
|
*
|
|
|
|
* The GtkStackSwitcher widget was added in 3.10.
|
2015-11-04 04:24:49 +00:00
|
|
|
*
|
|
|
|
* # CSS nodes
|
|
|
|
*
|
2015-11-15 22:11:48 +00:00
|
|
|
* GtkStackSwitcher has a single CSS node named stackswitcher and
|
|
|
|
* style class .stack-switcher.
|
2015-11-05 13:17:49 +00:00
|
|
|
*
|
|
|
|
* When circumstances require it, GtkStackSwitcher adds the
|
|
|
|
* .needs-attention style class to the widgets representing the
|
|
|
|
* stack pages.
|
2013-04-21 15:05:38 +00:00
|
|
|
*/
|
|
|
|
|
2016-03-13 05:11:52 +00:00
|
|
|
#define TIMEOUT_EXPAND 500
|
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
typedef struct _GtkStackSwitcherPrivate GtkStackSwitcherPrivate;
|
2013-04-21 11:51:14 +00:00
|
|
|
struct _GtkStackSwitcherPrivate
|
|
|
|
{
|
|
|
|
GtkStack *stack;
|
|
|
|
GHashTable *buttons;
|
2017-11-15 19:20:40 +00:00
|
|
|
GtkIconSize icon_size;
|
2013-04-21 11:51:14 +00:00
|
|
|
gboolean in_child_changed;
|
2016-03-13 05:11:52 +00:00
|
|
|
GtkWidget *switch_button;
|
|
|
|
guint switch_timer;
|
2013-04-21 11:51:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
2015-11-17 05:38:22 +00:00
|
|
|
PROP_ICON_SIZE,
|
2013-04-21 11:51:14 +00:00
|
|
|
PROP_STACK
|
|
|
|
};
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkStackSwitcher, gtk_stack_switcher, GTK_TYPE_BOX)
|
2013-04-21 11:51:14 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_stack_switcher_init (GtkStackSwitcher *switcher)
|
|
|
|
{
|
|
|
|
GtkStyleContext *context;
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
2016-03-13 05:11:52 +00:00
|
|
|
gtk_widget_set_has_window (GTK_WIDGET (switcher), FALSE);
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
priv = gtk_stack_switcher_get_instance_private (switcher);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2017-11-15 02:14:47 +00:00
|
|
|
priv->icon_size = GTK_ICON_SIZE_INHERIT;
|
2013-04-21 11:51:14 +00:00
|
|
|
priv->stack = NULL;
|
|
|
|
priv->buttons = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
|
|
|
|
|
|
context = gtk_widget_get_style_context (GTK_WIDGET (switcher));
|
2013-08-30 20:32:42 +00:00
|
|
|
gtk_style_context_add_class (context, "stack-switcher");
|
2013-04-21 11:51:14 +00:00
|
|
|
gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
|
|
|
|
|
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (switcher), GTK_ORIENTATION_HORIZONTAL);
|
2016-03-13 05:11:52 +00:00
|
|
|
|
2017-11-14 21:32:23 +00:00
|
|
|
gtk_drag_dest_set (GTK_WIDGET (switcher), 0, NULL, 0);
|
2016-03-13 05:11:52 +00:00
|
|
|
gtk_drag_dest_set_track_motion (GTK_WIDGET (switcher), TRUE);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_button_clicked (GtkWidget *widget,
|
|
|
|
GtkStackSwitcher *self)
|
|
|
|
{
|
|
|
|
GtkWidget *child;
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
if (!priv->in_child_changed)
|
2013-04-21 11:51:14 +00:00
|
|
|
{
|
|
|
|
child = g_object_get_data (G_OBJECT (widget), "stack-child");
|
2013-09-22 03:36:05 +00:00
|
|
|
gtk_stack_set_visible_child (priv->stack, child);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rebuild_child (GtkWidget *self,
|
|
|
|
const gchar *icon_name,
|
2015-11-17 05:38:22 +00:00
|
|
|
const gchar *title,
|
|
|
|
gint icon_size)
|
2013-04-21 11:51:14 +00:00
|
|
|
{
|
|
|
|
GtkStyleContext *context;
|
|
|
|
GtkWidget *button_child;
|
|
|
|
|
|
|
|
button_child = gtk_bin_get_child (GTK_BIN (self));
|
|
|
|
if (button_child != NULL)
|
|
|
|
gtk_widget_destroy (button_child);
|
|
|
|
|
|
|
|
button_child = NULL;
|
|
|
|
context = gtk_widget_get_style_context (GTK_WIDGET (self));
|
|
|
|
|
|
|
|
if (icon_name != NULL)
|
|
|
|
{
|
2017-11-15 00:43:13 +00:00
|
|
|
button_child = gtk_image_new_from_icon_name (icon_name);
|
2013-04-21 11:51:14 +00:00
|
|
|
if (title != NULL)
|
|
|
|
gtk_widget_set_tooltip_text (GTK_WIDGET (self), title);
|
|
|
|
|
|
|
|
gtk_style_context_remove_class (context, "text-button");
|
|
|
|
gtk_style_context_add_class (context, "image-button");
|
|
|
|
}
|
|
|
|
else if (title != NULL)
|
|
|
|
{
|
|
|
|
button_child = gtk_label_new (title);
|
|
|
|
|
2013-04-25 00:10:05 +00:00
|
|
|
gtk_widget_set_tooltip_text (GTK_WIDGET (self), NULL);
|
|
|
|
|
2013-04-21 11:51:14 +00:00
|
|
|
gtk_style_context_remove_class (context, "image-button");
|
|
|
|
gtk_style_context_add_class (context, "text-button");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (button_child)
|
|
|
|
{
|
2014-07-16 17:35:29 +00:00
|
|
|
gtk_widget_set_halign (GTK_WIDGET (button_child), GTK_ALIGN_CENTER);
|
2013-04-21 11:51:14 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (self), button_child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 20:32:42 +00:00
|
|
|
static void
|
2013-09-26 03:52:39 +00:00
|
|
|
update_needs_attention (GtkWidget *widget, GtkWidget *button, gpointer data)
|
2013-08-30 20:32:42 +00:00
|
|
|
{
|
|
|
|
GtkContainer *container;
|
|
|
|
gboolean needs_attention;
|
|
|
|
GtkStyleContext *context;
|
|
|
|
|
|
|
|
container = GTK_CONTAINER (data);
|
|
|
|
gtk_container_child_get (container, widget,
|
|
|
|
"needs-attention", &needs_attention,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
context = gtk_widget_get_style_context (button);
|
2014-07-17 01:08:27 +00:00
|
|
|
if (needs_attention)
|
2013-08-30 20:32:42 +00:00
|
|
|
gtk_style_context_add_class (context, GTK_STYLE_CLASS_NEEDS_ATTENTION);
|
|
|
|
else
|
|
|
|
gtk_style_context_remove_class (context, GTK_STYLE_CLASS_NEEDS_ATTENTION);
|
|
|
|
}
|
|
|
|
|
2013-04-21 11:51:14 +00:00
|
|
|
static void
|
|
|
|
update_button (GtkStackSwitcher *self,
|
|
|
|
GtkWidget *widget,
|
|
|
|
GtkWidget *button)
|
|
|
|
{
|
|
|
|
gchar *title;
|
|
|
|
gchar *icon_name;
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
|
2013-04-21 11:51:14 +00:00
|
|
|
"title", &title,
|
|
|
|
"icon-name", &icon_name,
|
|
|
|
NULL);
|
|
|
|
|
2015-11-17 05:38:22 +00:00
|
|
|
rebuild_child (button, icon_name, title, priv->icon_size);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-10-17 20:05:05 +00:00
|
|
|
gtk_widget_set_visible (button, gtk_widget_get_visible (widget) && (title != NULL || icon_name != NULL));
|
2013-04-21 11:51:14 +00:00
|
|
|
|
|
|
|
g_free (title);
|
|
|
|
g_free (icon_name);
|
2013-08-30 20:32:42 +00:00
|
|
|
|
|
|
|
update_needs_attention (widget, button, priv->stack);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-10-17 20:05:05 +00:00
|
|
|
on_title_icon_visible_updated (GtkWidget *widget,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GtkStackSwitcher *self)
|
2013-04-21 11:51:14 +00:00
|
|
|
{
|
|
|
|
GtkWidget *button;
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
button = g_hash_table_lookup (priv->buttons, widget);
|
2013-04-21 11:51:14 +00:00
|
|
|
update_button (self, widget, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_position_updated (GtkWidget *widget,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GtkStackSwitcher *self)
|
|
|
|
{
|
|
|
|
GtkWidget *button;
|
|
|
|
gint position;
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
button = g_hash_table_lookup (priv->buttons, widget);
|
|
|
|
|
|
|
|
gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
|
2013-04-21 11:51:14 +00:00
|
|
|
"position", &position,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gtk_box_reorder_child (GTK_BOX (self), button, position);
|
|
|
|
}
|
|
|
|
|
2013-08-30 20:32:42 +00:00
|
|
|
static void
|
|
|
|
on_needs_attention_updated (GtkWidget *widget,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GtkStackSwitcher *self)
|
|
|
|
{
|
|
|
|
GtkWidget *button;
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
|
|
|
|
|
|
|
button = g_hash_table_lookup (priv->buttons, widget);
|
|
|
|
update_button (self, widget, button);
|
|
|
|
}
|
|
|
|
|
2016-03-13 05:11:52 +00:00
|
|
|
static void
|
|
|
|
remove_switch_timer (GtkStackSwitcher *self)
|
|
|
|
{
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
|
|
|
|
|
|
|
if (priv->switch_timer)
|
|
|
|
{
|
|
|
|
g_source_remove (priv->switch_timer);
|
|
|
|
priv->switch_timer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_stack_switcher_switch_timeout (gpointer data)
|
|
|
|
{
|
|
|
|
GtkStackSwitcher *self = data;
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
GtkWidget *button;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
|
|
|
|
|
|
|
priv->switch_timer = 0;
|
|
|
|
|
|
|
|
button = priv->switch_button;
|
|
|
|
priv->switch_button = NULL;
|
|
|
|
|
|
|
|
if (button)
|
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_stack_switcher_drag_motion (GtkWidget *widget,
|
|
|
|
GdkDragContext *context,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
guint time)
|
|
|
|
{
|
|
|
|
GtkStackSwitcher *self = GTK_STACK_SWITCHER (widget);
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
GtkWidget *button;
|
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer value;
|
|
|
|
gboolean retval = FALSE;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
|
|
|
|
|
|
|
button = NULL;
|
|
|
|
g_hash_table_iter_init (&iter, priv->buttons);
|
|
|
|
while (g_hash_table_iter_next (&iter, NULL, &value))
|
|
|
|
{
|
2017-06-28 06:19:35 +00:00
|
|
|
GdkRectangle allocation;
|
|
|
|
|
|
|
|
gtk_widget_get_outer_allocation (GTK_WIDGET (value), &allocation);
|
|
|
|
if (gdk_rectangle_contains_point (&allocation, (int)x, (int)y))
|
2016-03-13 05:11:52 +00:00
|
|
|
{
|
|
|
|
button = GTK_WIDGET (value);
|
|
|
|
retval = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (button != priv->switch_button)
|
|
|
|
remove_switch_timer (self);
|
|
|
|
|
|
|
|
priv->switch_button = button;
|
|
|
|
|
|
|
|
if (button && !priv->switch_timer)
|
|
|
|
{
|
2018-02-02 14:51:47 +00:00
|
|
|
priv->switch_timer = g_timeout_add (TIMEOUT_EXPAND,
|
|
|
|
gtk_stack_switcher_switch_timeout,
|
|
|
|
self);
|
2016-03-13 05:11:52 +00:00
|
|
|
g_source_set_name_by_id (priv->switch_timer, "[gtk+] gtk_stack_switcher_switch_timeout");
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_stack_switcher_drag_leave (GtkWidget *widget,
|
|
|
|
GdkDragContext *context,
|
|
|
|
guint time)
|
|
|
|
{
|
|
|
|
GtkStackSwitcher *self = GTK_STACK_SWITCHER (widget);
|
|
|
|
|
|
|
|
remove_switch_timer (self);
|
|
|
|
}
|
|
|
|
|
2013-04-21 11:51:14 +00:00
|
|
|
static void
|
2014-09-12 00:42:12 +00:00
|
|
|
add_child (GtkWidget *widget,
|
|
|
|
GtkStackSwitcher *self)
|
2013-04-21 11:51:14 +00:00
|
|
|
{
|
|
|
|
GtkWidget *button;
|
|
|
|
GList *group;
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
|
|
|
button = gtk_radio_button_new (NULL);
|
2016-03-13 05:11:52 +00:00
|
|
|
|
2015-10-23 20:13:30 +00:00
|
|
|
gtk_widget_set_focus_on_click (button, FALSE);
|
2017-01-27 11:02:47 +00:00
|
|
|
gtk_check_button_set_draw_indicator (GTK_CHECK_BUTTON (button), FALSE);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
|
|
|
update_button (self, widget, button);
|
|
|
|
|
|
|
|
group = gtk_container_get_children (GTK_CONTAINER (self));
|
|
|
|
if (group != NULL)
|
|
|
|
{
|
|
|
|
gtk_radio_button_join_group (GTK_RADIO_BUTTON (button), GTK_RADIO_BUTTON (group->data));
|
|
|
|
g_list_free (group);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (self), button);
|
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (button), "stack-child", widget);
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (on_button_clicked), self);
|
2013-10-17 20:05:05 +00:00
|
|
|
g_signal_connect (widget, "notify::visible", G_CALLBACK (on_title_icon_visible_updated), self);
|
|
|
|
g_signal_connect (widget, "child-notify::title", G_CALLBACK (on_title_icon_visible_updated), self);
|
|
|
|
g_signal_connect (widget, "child-notify::icon-name", G_CALLBACK (on_title_icon_visible_updated), self);
|
2013-04-21 11:51:14 +00:00
|
|
|
g_signal_connect (widget, "child-notify::position", G_CALLBACK (on_position_updated), self);
|
2013-08-30 20:32:42 +00:00
|
|
|
g_signal_connect (widget, "child-notify::needs-attention", G_CALLBACK (on_needs_attention_updated), self);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
g_hash_table_insert (priv->buttons, widget, button);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-12 00:42:12 +00:00
|
|
|
remove_child (GtkWidget *widget,
|
|
|
|
GtkStackSwitcher *self)
|
2013-04-21 11:51:14 +00:00
|
|
|
{
|
2014-09-12 00:42:12 +00:00
|
|
|
GtkWidget *button;
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (widget, on_title_icon_visible_updated, self);
|
|
|
|
g_signal_handlers_disconnect_by_func (widget, on_position_updated, self);
|
|
|
|
g_signal_handlers_disconnect_by_func (widget, on_needs_attention_updated, self);
|
|
|
|
|
|
|
|
button = g_hash_table_lookup (priv->buttons, widget);
|
|
|
|
gtk_container_remove (GTK_CONTAINER (self), button);
|
|
|
|
g_hash_table_remove (priv->buttons, widget);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
populate_switcher (GtkStackSwitcher *self)
|
|
|
|
{
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
2014-10-07 03:06:09 +00:00
|
|
|
GtkWidget *widget, *button;
|
2013-09-22 03:36:05 +00:00
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
2014-09-12 00:42:12 +00:00
|
|
|
gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)add_child, self);
|
2014-10-07 03:06:09 +00:00
|
|
|
|
|
|
|
widget = gtk_stack_get_visible_child (priv->stack);
|
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
button = g_hash_table_lookup (priv->buttons, widget);
|
2015-05-06 14:53:43 +00:00
|
|
|
priv->in_child_changed = TRUE;
|
2014-10-07 03:06:09 +00:00
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
|
2015-05-06 14:53:43 +00:00
|
|
|
priv->in_child_changed = FALSE;
|
2014-10-07 03:06:09 +00:00
|
|
|
}
|
2014-09-12 00:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_switcher (GtkStackSwitcher *self)
|
|
|
|
{
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
|
|
|
gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)remove_child, self);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_child_changed (GtkWidget *widget,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GtkStackSwitcher *self)
|
|
|
|
{
|
|
|
|
GtkWidget *child;
|
|
|
|
GtkWidget *button;
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (self);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
|
|
|
child = gtk_stack_get_visible_child (GTK_STACK (widget));
|
2013-09-22 03:36:05 +00:00
|
|
|
button = g_hash_table_lookup (priv->buttons, child);
|
2013-04-21 11:51:14 +00:00
|
|
|
if (button != NULL)
|
|
|
|
{
|
2013-09-22 03:36:05 +00:00
|
|
|
priv->in_child_changed = TRUE;
|
2013-04-21 11:51:14 +00:00
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
|
2013-09-22 03:36:05 +00:00
|
|
|
priv->in_child_changed = FALSE;
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_stack_child_added (GtkContainer *container,
|
|
|
|
GtkWidget *widget,
|
|
|
|
GtkStackSwitcher *self)
|
|
|
|
{
|
2014-09-12 00:42:12 +00:00
|
|
|
add_child (widget, self);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_stack_child_removed (GtkContainer *container,
|
|
|
|
GtkWidget *widget,
|
|
|
|
GtkStackSwitcher *self)
|
|
|
|
{
|
2014-09-12 00:42:12 +00:00
|
|
|
remove_child (widget, self);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
disconnect_stack_signals (GtkStackSwitcher *switcher)
|
|
|
|
{
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
priv = gtk_stack_switcher_get_instance_private (switcher);
|
2013-04-21 11:51:14 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_added, switcher);
|
|
|
|
g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_removed, switcher);
|
|
|
|
g_signal_handlers_disconnect_by_func (priv->stack, on_child_changed, switcher);
|
|
|
|
g_signal_handlers_disconnect_by_func (priv->stack, disconnect_stack_signals, switcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
connect_stack_signals (GtkStackSwitcher *switcher)
|
|
|
|
{
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
priv = gtk_stack_switcher_get_instance_private (switcher);
|
2013-04-21 11:51:14 +00:00
|
|
|
g_signal_connect_after (priv->stack, "add",
|
|
|
|
G_CALLBACK (on_stack_child_added), switcher);
|
|
|
|
g_signal_connect_after (priv->stack, "remove",
|
|
|
|
G_CALLBACK (on_stack_child_removed), switcher);
|
|
|
|
g_signal_connect (priv->stack, "notify::visible-child",
|
|
|
|
G_CALLBACK (on_child_changed), switcher);
|
|
|
|
g_signal_connect_swapped (priv->stack, "destroy",
|
|
|
|
G_CALLBACK (disconnect_stack_signals), switcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_stack_switcher_set_stack:
|
|
|
|
* @switcher: a #GtkStackSwitcher
|
|
|
|
* @stack: (allow-none): a #GtkStack
|
|
|
|
*
|
|
|
|
* Sets the stack to control.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_stack_switcher_set_stack (GtkStackSwitcher *switcher,
|
|
|
|
GtkStack *stack)
|
|
|
|
{
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_STACK_SWITCHER (switcher));
|
2013-09-22 03:36:05 +00:00
|
|
|
g_return_if_fail (GTK_IS_STACK (stack) || stack == NULL);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
priv = gtk_stack_switcher_get_instance_private (switcher);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
|
|
|
if (priv->stack == stack)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (priv->stack)
|
|
|
|
{
|
|
|
|
disconnect_stack_signals (switcher);
|
|
|
|
clear_switcher (switcher);
|
|
|
|
g_clear_object (&priv->stack);
|
|
|
|
}
|
|
|
|
if (stack)
|
|
|
|
{
|
|
|
|
priv->stack = g_object_ref (stack);
|
|
|
|
populate_switcher (switcher);
|
|
|
|
connect_stack_signals (switcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (switcher));
|
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (switcher), "stack");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_stack_switcher_get_stack:
|
|
|
|
* @switcher: a #GtkStackSwitcher
|
|
|
|
*
|
2013-04-21 15:05:38 +00:00
|
|
|
* Retrieves the stack.
|
|
|
|
* See gtk_stack_switcher_set_stack().
|
2013-04-21 11:51:14 +00:00
|
|
|
*
|
2015-12-28 20:14:08 +00:00
|
|
|
* Returns: (nullable) (transfer none): the stack, or %NULL if
|
2013-04-21 11:51:14 +00:00
|
|
|
* none has been set explicitly.
|
|
|
|
*/
|
|
|
|
GtkStack *
|
|
|
|
gtk_stack_switcher_get_stack (GtkStackSwitcher *switcher)
|
|
|
|
{
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
2013-04-21 11:51:14 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_STACK_SWITCHER (switcher), NULL);
|
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
priv = gtk_stack_switcher_get_instance_private (switcher);
|
|
|
|
return priv->stack;
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 05:38:22 +00:00
|
|
|
static void
|
|
|
|
gtk_stack_switcher_set_icon_size (GtkStackSwitcher *switcher,
|
2017-11-15 19:20:40 +00:00
|
|
|
GtkIconSize icon_size)
|
2015-11-17 05:38:22 +00:00
|
|
|
{
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_STACK_SWITCHER (switcher));
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (switcher);
|
|
|
|
|
|
|
|
if (icon_size != priv->icon_size)
|
|
|
|
{
|
|
|
|
priv->icon_size = icon_size;
|
|
|
|
|
|
|
|
if (priv->stack != NULL)
|
|
|
|
{
|
|
|
|
clear_switcher (switcher);
|
|
|
|
populate_switcher (switcher);
|
|
|
|
}
|
2015-11-19 23:08:30 +00:00
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (switcher), "icon-size");
|
2015-11-17 05:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-21 11:51:14 +00:00
|
|
|
static void
|
|
|
|
gtk_stack_switcher_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);
|
2013-09-22 03:36:05 +00:00
|
|
|
GtkStackSwitcherPrivate *priv;
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2013-09-22 03:36:05 +00:00
|
|
|
priv = gtk_stack_switcher_get_instance_private (switcher);
|
2013-04-21 11:51:14 +00:00
|
|
|
switch (prop_id)
|
|
|
|
{
|
2015-11-17 05:38:22 +00:00
|
|
|
case PROP_ICON_SIZE:
|
2017-11-15 19:20:40 +00:00
|
|
|
g_value_set_enum (value, priv->icon_size);
|
2015-11-17 05:38:22 +00:00
|
|
|
break;
|
|
|
|
|
2013-04-21 11:51:14 +00:00
|
|
|
case PROP_STACK:
|
|
|
|
g_value_set_object (value, priv->stack);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_stack_switcher_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2015-11-17 05:38:22 +00:00
|
|
|
case PROP_ICON_SIZE:
|
2017-11-15 19:20:40 +00:00
|
|
|
gtk_stack_switcher_set_icon_size (switcher, g_value_get_enum (value));
|
2015-11-17 05:38:22 +00:00
|
|
|
break;
|
|
|
|
|
2013-04-21 11:51:14 +00:00
|
|
|
case PROP_STACK:
|
|
|
|
gtk_stack_switcher_set_stack (switcher, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_stack_switcher_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);
|
|
|
|
|
2016-03-13 05:11:52 +00:00
|
|
|
remove_switch_timer (switcher);
|
2013-04-21 11:51:14 +00:00
|
|
|
gtk_stack_switcher_set_stack (switcher, NULL);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_stack_switcher_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2014-03-29 22:14:51 +00:00
|
|
|
static void
|
|
|
|
gtk_stack_switcher_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);
|
|
|
|
GtkStackSwitcherPrivate *priv;
|
|
|
|
|
|
|
|
priv = gtk_stack_switcher_get_instance_private (switcher);
|
|
|
|
|
|
|
|
g_hash_table_destroy (priv->buttons);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_stack_switcher_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2013-04-21 11:51:14 +00:00
|
|
|
static void
|
|
|
|
gtk_stack_switcher_class_init (GtkStackSwitcherClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
2015-11-04 04:24:49 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
|
2013-04-21 11:51:14 +00:00
|
|
|
|
|
|
|
object_class->get_property = gtk_stack_switcher_get_property;
|
|
|
|
object_class->set_property = gtk_stack_switcher_set_property;
|
|
|
|
object_class->dispose = gtk_stack_switcher_dispose;
|
2014-03-29 22:14:51 +00:00
|
|
|
object_class->finalize = gtk_stack_switcher_finalize;
|
2013-04-21 11:51:14 +00:00
|
|
|
|
2016-03-13 05:11:52 +00:00
|
|
|
widget_class->drag_motion = gtk_stack_switcher_drag_motion;
|
|
|
|
widget_class->drag_leave = gtk_stack_switcher_drag_leave;
|
2015-11-17 05:38:22 +00:00
|
|
|
/**
|
|
|
|
* GtkStackSwitcher:icon-size:
|
|
|
|
*
|
|
|
|
* Use the "icon-size" property to change the size of the image displayed
|
|
|
|
* when a #GtkStackSwitcher is displaying icons.
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_ICON_SIZE,
|
2017-11-15 19:20:40 +00:00
|
|
|
g_param_spec_enum ("icon-size",
|
|
|
|
P_("Icon Size"),
|
|
|
|
P_("Symbolic size to use for named icon"),
|
|
|
|
GTK_TYPE_ICON_SIZE,
|
|
|
|
GTK_ICON_SIZE_INHERIT,
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY | GTK_PARAM_READWRITE));
|
2015-11-17 05:38:22 +00:00
|
|
|
|
2013-04-21 11:51:14 +00:00
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_STACK,
|
|
|
|
g_param_spec_object ("stack",
|
|
|
|
P_("Stack"),
|
|
|
|
P_("Stack"),
|
|
|
|
GTK_TYPE_STACK,
|
|
|
|
GTK_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT));
|
2015-11-04 04:24:49 +00:00
|
|
|
|
2017-11-18 03:49:57 +00:00
|
|
|
gtk_widget_class_set_css_name (widget_class, I_("stackswitcher"));
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|
|
|
|
|
2013-04-21 15:05:38 +00:00
|
|
|
/**
|
|
|
|
* gtk_stack_switcher_new:
|
|
|
|
*
|
|
|
|
* Create a new #GtkStackSwitcher.
|
|
|
|
*
|
2014-02-19 23:49:43 +00:00
|
|
|
* Returns: a new #GtkStackSwitcher.
|
2013-04-21 15:05:38 +00:00
|
|
|
*/
|
2013-04-21 11:51:14 +00:00
|
|
|
GtkWidget *
|
|
|
|
gtk_stack_switcher_new (void)
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
return g_object_new (GTK_TYPE_STACK_SWITCHER, NULL);
|
2013-04-21 11:51:14 +00:00
|
|
|
}
|