1/*
2 * Copyright © 2019 Benjamin Otte
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Benjamin Otte <otte@gnome.org>
18 */
19
20#include "config.h"
21
22#include "gtkcolumnlistitemfactoryprivate.h"
23
24#include "gtkboxlayout.h"
25#include "gtkcolumnviewcolumnprivate.h"
26#include "gtkcolumnviewlayoutprivate.h"
27#include "gtklistitemfactoryprivate.h"
28#include "gtklistitemprivate.h"
29
30struct _GtkColumnListItemFactory
31{
32 GtkListItemFactory parent_instance;
33
34 GtkColumnView *view; /* no reference, the view references us */
35};
36
37struct _GtkColumnListItemFactoryClass
38{
39 GtkListItemFactoryClass parent_class;
40};
41
42G_DEFINE_TYPE (GtkColumnListItemFactory, gtk_column_list_item_factory, GTK_TYPE_LIST_ITEM_FACTORY)
43
44static void
45gtk_column_list_item_factory_setup (GtkListItemFactory *factory,
46 GtkListItemWidget *widget,
47 GtkListItem *list_item)
48{
49 GtkColumnListItemFactory *self = GTK_COLUMN_LIST_ITEM_FACTORY (factory);
50 GListModel *columns;
51 guint i;
52
53 /* FIXME: evil */
54 gtk_widget_set_layout_manager (GTK_WIDGET (widget),
55 layout_manager: gtk_column_view_layout_new (view: self->view));
56
57 GTK_LIST_ITEM_FACTORY_CLASS (gtk_column_list_item_factory_parent_class)->setup (factory, widget, list_item);
58
59 columns = gtk_column_view_get_columns (self: self->view);
60
61 for (i = 0; i < g_list_model_get_n_items (list: columns); i++)
62 {
63 GtkColumnViewColumn *column = g_list_model_get_item (list: columns, position: i);
64
65 gtk_column_list_item_factory_add_column (factory: self,
66 list_item: list_item->owner,
67 column,
68 FALSE);
69
70 g_object_unref (object: column);
71 }
72}
73
74static void
75gtk_column_list_item_factory_teardown (GtkListItemFactory *factory,
76 GtkListItemWidget *widget,
77 GtkListItem *list_item)
78{
79 GtkWidget *child;
80
81 GTK_LIST_ITEM_FACTORY_CLASS (gtk_column_list_item_factory_parent_class)->teardown (factory, widget, list_item);
82
83 while ((child = gtk_widget_get_first_child (GTK_WIDGET (widget))))
84 {
85 gtk_list_item_widget_remove_child (GTK_LIST_ITEM_WIDGET (widget), child);
86 }
87}
88
89static void
90gtk_column_list_item_factory_update (GtkListItemFactory *factory,
91 GtkListItemWidget *widget,
92 GtkListItem *list_item,
93 guint position,
94 gpointer item,
95 gboolean selected)
96{
97 GtkWidget *child;
98
99 GTK_LIST_ITEM_FACTORY_CLASS (gtk_column_list_item_factory_parent_class)->update (factory, widget, list_item, position, item, selected);
100
101 for (child = gtk_widget_get_first_child (GTK_WIDGET (widget));
102 child;
103 child = gtk_widget_get_next_sibling (widget: child))
104 {
105 gtk_list_item_widget_update (GTK_LIST_ITEM_WIDGET (child), position, item, selected);
106 }
107}
108
109static void
110gtk_column_list_item_factory_class_init (GtkColumnListItemFactoryClass *klass)
111{
112 GtkListItemFactoryClass *factory_class = GTK_LIST_ITEM_FACTORY_CLASS (klass);
113
114 factory_class->setup = gtk_column_list_item_factory_setup;
115 factory_class->teardown = gtk_column_list_item_factory_teardown;
116 factory_class->update = gtk_column_list_item_factory_update;
117}
118
119static void
120gtk_column_list_item_factory_init (GtkColumnListItemFactory *self)
121{
122}
123
124GtkColumnListItemFactory *
125gtk_column_list_item_factory_new (GtkColumnView *view)
126{
127 GtkColumnListItemFactory *result;
128
129 result = g_object_new (GTK_TYPE_COLUMN_LIST_ITEM_FACTORY, NULL);
130
131 result->view = view;
132
133 return result;
134}
135
136void
137gtk_column_list_item_factory_add_column (GtkColumnListItemFactory *factory,
138 GtkListItemWidget *list_item,
139 GtkColumnViewColumn *column,
140 gboolean check_bind)
141{
142 GtkWidget *cell;
143
144 cell = gtk_column_view_cell_new (column);
145 gtk_list_item_widget_add_child (GTK_LIST_ITEM_WIDGET (list_item), GTK_WIDGET (cell));
146 gtk_list_item_widget_update (GTK_LIST_ITEM_WIDGET (cell),
147 position: gtk_list_item_widget_get_position (self: list_item),
148 item: gtk_list_item_widget_get_item (self: list_item),
149 selected: gtk_list_item_widget_get_selected (self: list_item));
150}
151

source code of gtk/gtk/gtkcolumnlistitemfactory.c