Debian Codegraph @

hiro/gtk/widget/icon-view.cpp

raw
1 #if defined(Hiro_IconView)
2
3 namespace hiro {
4
5 static auto IconView_activate(GtkIconView* iconView, GtkTreePath* path, pIconView* p) -> void {
6 if(!p->locked()) p->self().doActivate();
7 }
8
9 static auto IconView_buttonEvent(GtkTreeView* treeView, GdkEventButton* event, pIconView* p) -> s32 {
10 if(event->type == GDK_BUTTON_RELEASE && event->button == 3) {
11 if(!p->locked()) p->self().doContext();
12 return false;
13 }
14
15 return false;
16 }
17
18 static auto IconView_change(GtkIconView* iconView, pIconView* p) -> void {
19 p->_updateSelected();
20 }
21
22 auto pIconView::construct() -> void {
23 gtkWidget = gtk_scrolled_window_new(0, 0);
24 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(gtkWidget), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
25 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(gtkWidget), GTK_SHADOW_ETCHED_IN);
26
27 store = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
28 subWidget = gtk_icon_view_new_with_model(GTK_TREE_MODEL(store));
29 gtk_icon_view_set_pixbuf_column(GTK_ICON_VIEW(subWidget), 0);
30 gtk_icon_view_set_text_column(GTK_ICON_VIEW(subWidget), 1);
31 gtk_icon_view_set_reorderable(GTK_ICON_VIEW(subWidget), false);
32 gtk_icon_view_set_margin(GTK_ICON_VIEW(subWidget), 0);
33 gtk_icon_view_set_spacing(GTK_ICON_VIEW(subWidget), 0);
34 gtk_icon_view_set_column_spacing(GTK_ICON_VIEW(subWidget), 0);
35 gtk_icon_view_set_row_spacing(GTK_ICON_VIEW(subWidget), 0);
36 gtk_icon_view_set_item_padding(GTK_ICON_VIEW(subWidget), 0);
37 gtk_container_add(GTK_CONTAINER(gtkWidget), subWidget);
38
39 gtk_widget_show(subWidget);
40
41 setBackgroundColor(state().backgroundColor);
42 setBatchable(state().batchable);
43 setFlow(state().flow);
44 setForegroundColor(state().foregroundColor);
45 setOrientation(state().orientation);
46 for(auto position : range(self().itemCount())) {
47 auto& item = state().items[position];
48 append(item);
49 }
50 _updateSelected();
51
52 g_signal_connect(G_OBJECT(subWidget), "button-press-event", G_CALLBACK(IconView_buttonEvent), (gpointer)this);
53 g_signal_connect(G_OBJECT(subWidget), "button-release-event", G_CALLBACK(IconView_buttonEvent), (gpointer)this);
54 g_signal_connect(G_OBJECT(subWidget), "item-activated", G_CALLBACK(IconView_activate), (gpointer)this);
55 g_signal_connect(G_OBJECT(subWidget), "selection-changed", G_CALLBACK(IconView_change), (gpointer)this);
56
57 pWidget::construct();
58 }
59
60 auto pIconView::destruct() -> void {
61 gtk_widget_destroy(subWidget);
62 gtk_widget_destroy(gtkWidget);
63 }
64
65 auto pIconView::append(sIconViewItem item) -> void {
66 GtkTreeIter iter;
67 gtk_list_store_append(store, &iter);
68 setItemIcon(item->offset(), item->state.icon);
69 setItemSelected(item->offset(), item->state.selected);
70 setItemText(item->offset(), item->state.text);
71 }
72
73 auto pIconView::remove(sIconViewItem item) -> void {
74 lock();
75 GtkTreeIter iter;
76 if(gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(store), &iter, string{item->offset()})) {
77 gtk_list_store_remove(store, &iter);
78 }
79 _updateSelected();
80 unlock();
81 }
82
83 auto pIconView::reset() -> void {
84 lock();
85 gtk_list_store_clear(store);
86 _updateSelected();
87 unlock();
88 }
89
90 auto pIconView::setBackgroundColor(Color color) -> void {
91 GdkColor gdkColor = CreateColor(color);
92 gtk_widget_modify_base(subWidget, GTK_STATE_NORMAL, color ? &gdkColor : nullptr);
93 }
94
95 auto pIconView::setBatchable(bool batchable) -> void {
96 gtk_icon_view_set_selection_mode(GTK_ICON_VIEW(subWidget),
97 batchable ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE
98 );
99 }
100
101 auto pIconView::setFlow(Orientation flow) -> void {
102 //GTK+ does not support vertical flow ... the closest we can get is a horizontal flow with only one column
103 if(flow == Orientation::Horizontal) {
104 gtk_icon_view_set_columns(GTK_ICON_VIEW(subWidget), -1);
105 gtk_icon_view_set_item_width(GTK_ICON_VIEW(subWidget), -1);
106 } else {
107 gtk_icon_view_set_columns(GTK_ICON_VIEW(subWidget), 1);
108 gtk_icon_view_set_item_width(GTK_ICON_VIEW(subWidget), max(128, pSizable::state().geometry.width() - 64));
109 }
110 }
111
112 auto pIconView::setForegroundColor(Color color) -> void {
113 GdkColor gdkColor = CreateColor(color);
114 gtk_widget_modify_text(subWidget, GTK_STATE_NORMAL, color ? &gdkColor : nullptr);
115 }
116
117 auto pIconView::setGeometry(Geometry geometry) -> void {
118 pWidget::setGeometry(geometry);
119 if(state().flow == Orientation::Vertical) {
120 gtk_icon_view_set_item_width(GTK_ICON_VIEW(subWidget), max(128, pSizable::state().geometry.width() - 64));
121 }
122 }
123
124 auto pIconView::setItemIcon(u32 position, const image& icon) -> void {
125 if(position >= self().itemCount()) return;
126 GtkTreeIter iter;
127 if(gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(store), &iter, string{position})) {
128 if(icon) {
129 GdkPixbuf* pixbuf = CreatePixbuf(icon);
130 gtk_list_store_set(store, &iter, 0, pixbuf, -1);
131 } else {
132 gtk_list_store_set(store, &iter, 0, nullptr, -1);
133 }
134 }
135 }
136
137 auto pIconView::setItemSelected(u32 position, bool selected) -> void {
138 if(position >= self().itemCount()) return;
139 lock();
140 GtkTreePath* path = gtk_tree_path_new_from_string(string{position});
141 if(selected) {
142 gtk_icon_view_select_path(GTK_ICON_VIEW(subWidget), path);
143 } else {
144 gtk_icon_view_unselect_path(GTK_ICON_VIEW(subWidget), path);
145 }
146 gtk_tree_path_free(path);
147 _updateSelected();
148 unlock();
149 }
150
151 auto pIconView::setItemSelected(const std::vector<s32>& selections) -> void {
152 lock();
153 setItemSelectedNone();
154 for(auto& position : selections) setItemSelected(position, true);
155 _updateSelected();
156 unlock();
157 }
158
159 auto pIconView::setItemSelectedAll() -> void {
160 lock();
161 gtk_icon_view_select_all(GTK_ICON_VIEW(subWidget));
162 _updateSelected();
163 unlock();
164 }
165
166 auto pIconView::setItemSelectedNone() -> void {
167 lock();
168 gtk_icon_view_unselect_all(GTK_ICON_VIEW(subWidget));
169 _updateSelected();
170 unlock();
171 }
172
173 auto pIconView::setItemText(u32 position, const string& text) -> void {
174 if(position >= self().itemCount()) return;
175 GtkTreeIter iter;
176 if(gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(store), &iter, string{position})) {
177 gtk_list_store_set(store, &iter, 1, (const char*)text, -1);
178 }
179 }
180
181 auto pIconView::setOrientation(Orientation orientation) -> void {
182 gtk_icon_view_set_item_orientation(GTK_ICON_VIEW(subWidget),
183 orientation == Orientation::Horizontal ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL
184 );
185 }
186
187 auto pIconView::_updateSelected() -> void {
188 std::vector<u32> selected;
189
190 GList* list = gtk_icon_view_get_selected_items(GTK_ICON_VIEW(subWidget));
191 GList* p = list;
192
193 while(p) {
194 auto path = (GtkTreePath*)p->data;
195 char* pathString = gtk_tree_path_to_string(path);
196 u32 position = toNatural(pathString);
197 g_free(pathString);
198 selected.push_back(position);
199 p = p->next;
200 }
201
202 g_list_foreach(list, (GFunc)gtk_tree_path_free, nullptr);
203 g_list_free(list);
204
205 bool identical = selected.size() == currentSelection.size();
206 if(identical) {
207 for(u32 n = 0; n < selected.size(); n++) {
208 if(selected[n] != currentSelection[n]) {
209 identical = false;
210 break;
211 }
212 }
213 }
214 if(identical) return;
215
216 currentSelection = selected;
217 for(auto& item : state().items) item->state.selected = false;
218 for(auto& position : currentSelection) {
219 if(position >= self().itemCount()) continue;
220 state().items[position]->state.selected = true;
221 }
222
223 if(!locked()) self().doChange();
224 }
225
226 }
227
228 #endif