1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/*
* Copyright © 2023 Benjamin Otte
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#include "config.h"
#include "gtklistitembaseprivate.h"
#include "gtkaccessible.h"
typedef struct _GtkListItemBasePrivate GtkListItemBasePrivate;
struct _GtkListItemBasePrivate
{
GObject *item;
guint position;
gboolean selected;
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkListItemBase, gtk_list_item_base, GTK_TYPE_WIDGET)
static void
gtk_list_item_base_default_update (GtkListItemBase *self,
guint position,
gpointer item,
gboolean selected)
{
GtkListItemBasePrivate *priv = gtk_list_item_base_get_instance_private (self);
g_set_object (&priv->item, item);
priv->position = position;
priv->selected = selected;
}
static void
gtk_list_item_base_dispose (GObject *object)
{
GtkListItemBase *self = GTK_LIST_ITEM_BASE (object);
GtkListItemBasePrivate *priv = gtk_list_item_base_get_instance_private (self);
g_clear_object (&priv->item);
G_OBJECT_CLASS (gtk_list_item_base_parent_class)->dispose (object);
}
static void
gtk_list_item_base_class_init (GtkListItemBaseClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
klass->update = gtk_list_item_base_default_update;
gobject_class->dispose = gtk_list_item_base_dispose;
}
static void
gtk_list_item_base_init (GtkListItemBase *self)
{
}
void
gtk_list_item_base_update (GtkListItemBase *self,
guint position,
gpointer item,
gboolean selected)
{
GtkListItemBasePrivate *priv = gtk_list_item_base_get_instance_private (self);
gboolean was_selected;
if (priv->position == position &&
priv->item == item &&
priv->selected == selected)
return;
was_selected = priv->selected;
GTK_LIST_ITEM_BASE_GET_CLASS (self)->update (self, position, item, selected);
/* don't look at selected variable, it's not reentrancy safe */
if (was_selected != priv->selected)
{
if (priv->selected)
gtk_widget_set_state_flags (GTK_WIDGET (self), GTK_STATE_FLAG_SELECTED, FALSE);
else
gtk_widget_unset_state_flags (GTK_WIDGET (self), GTK_STATE_FLAG_SELECTED);
}
/* We're updating the a11y state at least once because of the side-effects, subsequent same state filtering is done on the a11y layer. */
gtk_accessible_update_state (GTK_ACCESSIBLE (self),
GTK_ACCESSIBLE_STATE_SELECTED, priv->selected,
-1);
}
guint
gtk_list_item_base_get_position (GtkListItemBase *self)
{
GtkListItemBasePrivate *priv = gtk_list_item_base_get_instance_private (self);
return priv->position;
}
gpointer
gtk_list_item_base_get_item (GtkListItemBase *self)
{
GtkListItemBasePrivate *priv = gtk_list_item_base_get_instance_private (self);
return priv->item;
}
gboolean
gtk_list_item_base_get_selected (GtkListItemBase *self)
{
GtkListItemBasePrivate *priv = gtk_list_item_base_get_instance_private (self);
return priv->selected;
}
|