Debian Codegraph @

hiro/gtk/widget/tab-frame.cpp

raw
1 #if defined(Hiro_TabFrame)
2
3 namespace hiro {
4
5 static auto TabFrame_change(GtkNotebook* notebook, GtkWidget* page, u32 position, pTabFrame* p) -> void {
6 for(auto& item : p->state().items) item->state.selected = false;
7 if(auto item = p->self().item(position)) item->state.selected = true;
8
9 p->_synchronizeLayout();
10 if(!p->locked()) p->self().doChange();
11 }
12
13 static auto TabFrame_close(GtkButton* button, pTabFrame* p) -> void {
14 maybe<u32> position;
15 for(auto n : range(p->tabs.size())) {
16 if(button == (GtkButton*)p->tabs[n].close) {
17 position = n;
18 break;
19 }
20 }
21 if(position) {
22 if(!p->locked()) p->self().doClose(p->self().item(*position));
23 }
24 }
25
26 static auto TabFrame_move(GtkNotebook* notebook, GtkWidget* page, u32 moveTo, pTabFrame* p) -> void {
27 u32 position = gtk_notebook_get_current_page(notebook);
28 for(auto& item : p->state().items) item->state.selected = false;
29 if(auto item = p->self().item(position)) item->state.selected = true;
30
31 maybe<u32> moveFrom;
32 for(auto n : range(p->tabs.size())) {
33 if(page == p->tabs[n].child) {
34 moveFrom = n;
35 break;
36 }
37 }
38 if(moveFrom) {
39 auto item = p->state().items[*moveFrom];
40 p->state().items.erase(p->state().items.begin() + *moveFrom);
41 p->state().items.insert(p->state().items.begin() + moveTo, item);
42 auto tab = p->tabs[*moveFrom];
43 p->tabs.erase(p->tabs.begin() + *moveFrom);
44 p->tabs.insert(p->tabs.begin() + moveTo, tab);
45 if(!p->locked()) p->self().doMove(p->self().item(*moveFrom), p->self().item(moveTo));
46 }
47 }
48
49 auto pTabFrame::construct() -> void {
50 gtkWidget = gtk_notebook_new();
51 gtk_notebook_set_show_border(GTK_NOTEBOOK(gtkWidget), false);
52 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(gtkWidget), GTK_POS_TOP);
53
54 tabs.clear(); //todo: memory leak, need to release each tab
55 for(auto& item : state().items) append(item);
56 setNavigation(state().navigation);
57
58 g_signal_connect(G_OBJECT(gtkWidget), "page-reordered", G_CALLBACK(TabFrame_move), (gpointer)this);
59 g_signal_connect(G_OBJECT(gtkWidget), "switch-page", G_CALLBACK(TabFrame_change), (gpointer)this);
60
61 pWidget::construct();
62 }
63
64 auto pTabFrame::destruct() -> void {
65 gtk_widget_destroy(gtkWidget);
66 }
67
68 auto pTabFrame::append(sTabFrameItem item) -> void {
69 lock();
70 setFont(self().font(true));
71 setItemMovable(item->offset(), item->movable());
72 if(item->selected()) setItemSelected(item->offset());
73 _synchronizeTab(tabs.size() - 1);
74 setGeometry(self().geometry());
75 unlock();
76 }
77
78 auto pTabFrame::container(mWidget& widget) -> GtkWidget* {
79 //TabFrame holds multiple TabFrameItem controls
80 //each TabFrameItem has its own GtkWindow; plus its own layout
81 //we need to recurse up from the widget to its topmost layout before the TabFrameItem
82 //once we know the topmost layout, we search through all TabFrameItems for a match
83 mObject* object = &widget;
84 while(object) {
85 if(object->parentTabFrameItem()) break;
86 if(auto parent = object->parent()) { object = parent; continue; }
87 break;
88 }
89
90 u32 position = 0;
91 for(auto& item : state().items) {
92 if(item->state.sizable.get() == object) return tabs[position].child;
93 position++;
94 }
95
96 return nullptr;
97 }
98
99 auto pTabFrame::remove(sTabFrameItem item) -> void {
100 lock();
101 //if we are removing the current tab, we have to select another tab manually
102 if(item->offset() == gtk_notebook_get_current_page(GTK_NOTEBOOK(gtkWidget))) {
103 //the new tab will be the one after this one
104 u32 displacement = 1;
105 //... unless it's the last tab, in which case it's the one before it
106 if(item->offset() == self().itemCount() - 1) displacement = -1;
107 //... unless there are no tabs left, in which case nothing is selected
108 if(self().itemCount() > 1) {
109 setItemSelected(item->offset() + displacement);
110 }
111 }
112 tabs.erase(tabs.begin() + item->offset());
113 gtk_notebook_remove_page(GTK_NOTEBOOK(gtkWidget), item->offset());
114
115 u32 position = gtk_notebook_get_current_page(GTK_NOTEBOOK(gtkWidget));
116 for(auto& item : state().items) item->state.selected = false;
117 if(auto item = self().item(position)) item->state.selected = true;
118 unlock();
119 }
120
121 auto pTabFrame::setFont(const Font& font) -> void {
122 for(auto n : range(tabs.size())) {
123 pFont::setFont(tabs[n].title, font);
124 if(auto sizable = state().items[n]->state.sizable) {
125 if(auto self = sizable->self()) {
126 self->setFont(sizable->font(true));
127 }
128 }
129 }
130 }
131
132 auto pTabFrame::setGeometry(Geometry geometry) -> void {
133 pWidget::setGeometry(geometry);
134 geometry.setPosition();
135 if(state().navigation == Navigation::Top || state().navigation == Navigation::Bottom) {
136 geometry.setWidth(geometry.width() - 6);
137 geometry.setHeight(geometry.height() - (15 + _tabHeight()));
138 } else {
139 geometry.setWidth(geometry.width() - (17 + _tabWidth()));
140 geometry.setHeight(geometry.height() - 6);
141 }
142 for(auto& item : state().items) {
143 if(item->state.sizable) item->state.sizable->setGeometry(geometry);
144 }
145 }
146
147 auto pTabFrame::setItemClosable(u32 position, bool closable) -> void {
148 _synchronizeTab(position);
149 }
150
151 auto pTabFrame::setItemIcon(u32 position, const image& icon) -> void {
152 _synchronizeTab(position);
153 }
154
155 auto pTabFrame::setItemMovable(u32 position, bool movable) -> void {
156 lock();
157 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gtkWidget), tabs[position].child, movable);
158 unlock();
159 }
160
161 auto pTabFrame::setItemSelected(u32 position) -> void {
162 lock();
163 gtk_notebook_set_current_page(GTK_NOTEBOOK(gtkWidget), position);
164 unlock();
165 }
166
167 auto pTabFrame::setItemSizable(u32 position, sSizable sizable) -> void {
168 //if(layout->self()) layout->self()->setParent();
169 }
170
171 auto pTabFrame::setItemText(u32 position, const string& text) -> void {
172 _synchronizeTab(position);
173 }
174
175 auto pTabFrame::setNavigation(Navigation navigation) -> void {
176 GtkPositionType type;
177 switch(navigation) { default:
178 case Navigation::Top: type = GTK_POS_TOP; break;
179 case Navigation::Bottom: type = GTK_POS_BOTTOM; break;
180 case Navigation::Left: type = GTK_POS_LEFT; break;
181 case Navigation::Right: type = GTK_POS_RIGHT; break;
182 }
183 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(gtkWidget), type);
184 setGeometry(self().geometry());
185 }
186
187 //called by pTabFrameItem::construct(), before pTabFrame::append()
188 //tab needs to be created early, so that pTabFrameItem child widgets calling pWidget::container() can find the tab
189 auto pTabFrame::_append() -> void {
190 lock();
191 Tab tab;
192 tab.child = gtk_fixed_new();
193 #if HIRO_GTK==2
194 tab.container = gtk_hbox_new(false, 0);
195 #elif HIRO_GTK==3
196 tab.container = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
197 #endif
198 tab.image = gtk_image_new();
199 tab.title = gtk_label_new("");
200 gtk_misc_set_alignment(GTK_MISC(tab.title), 0.0, 0.5);
201 tab.close = gtk_button_new_with_label("\u00d7"); //Unicode multiplication sign (looks better than 'X')
202 gtk_button_set_focus_on_click(GTK_BUTTON(tab.close), false);
203 gtk_button_set_relief(GTK_BUTTON(tab.close), GTK_RELIEF_NONE);
204 pFont::setFont(tab.close, Font("sans", 9).setBold());
205 auto color = CreateColor({255, 0, 0});
206 gtk_widget_modify_fg(gtk_bin_get_child(GTK_BIN(tab.close)), GTK_STATE_PRELIGHT, &color);
207 tabs.push_back(tab);
208
209 gtk_widget_show(tab.child);
210 gtk_widget_show(tab.container);
211 gtk_widget_show(tab.image);
212 gtk_widget_show(tab.title);
213 gtk_widget_show(tab.close);
214 gtk_box_pack_start(GTK_BOX(tab.container), tab.image, false, false, 0);
215 gtk_box_pack_start(GTK_BOX(tab.container), tab.title, true, true, 0);
216 gtk_box_pack_start(GTK_BOX(tab.container), tab.close, false, false, 0);
217
218 g_signal_connect(G_OBJECT(tab.close), "clicked", G_CALLBACK(TabFrame_close), (gpointer)this);
219 gtk_notebook_append_page(GTK_NOTEBOOK(gtkWidget), tab.child, tab.container);
220 unlock();
221 }
222
223 auto pTabFrame::_synchronizeLayout() -> void {
224 for(auto& item : state().items) {
225 if(auto& sizable = item->state.sizable) {
226 if(auto self = sizable->self()) {
227 self->setVisible(sizable->visible() && item->selected());
228 }
229 }
230 }
231 }
232
233 auto pTabFrame::_synchronizeTab(u32 position) -> void {
234 auto& item = state().items[position];
235 auto& tab = tabs[position];
236 gtk_widget_set_visible(tab.close, item->closable());
237 if(auto& icon = item->state.icon) {
238 u32 size = pFont::size(self().font(true), " ").height();
239 auto pixbuf = CreatePixbuf(icon, true);
240 gtk_image_set_from_pixbuf(GTK_IMAGE(tab.image), pixbuf);
241 } else {
242 gtk_image_clear(GTK_IMAGE(tab.image));
243 }
244 string text = {
245 item->state.icon && item->state.text ? " " : "",
246 item->state.text,
247 item->state.text && item->state.closable ? " " : ""
248 };
249 gtk_label_set_text(GTK_LABEL(tab.title), text);
250 }
251
252 //compute the height of the tallest tab for child layout geometry calculations
253 auto pTabFrame::_tabHeight() -> u32 {
254 s32 height = 1;
255
256 for(auto n : range(self().itemCount())) {
257 GtkAllocation imageAllocation, titleAllocation, closeAllocation;
258 gtk_widget_get_allocation(tabs[n].image, &imageAllocation);
259 gtk_widget_get_allocation(tabs[n].title, &titleAllocation);
260 gtk_widget_get_allocation(tabs[n].close, &closeAllocation);
261 height = max(height, imageAllocation.height);
262 height = max(height, titleAllocation.height);
263 if(!state().items[n]->closable()) continue;
264 height = max(height, closeAllocation.height);
265 }
266
267 return height;
268 }
269
270 auto pTabFrame::_tabWidth() -> u32 {
271 s32 width = 1;
272
273 for(auto n : range(self().itemCount())) {
274 GtkAllocation imageAllocation, titleAllocation, closeAllocation;
275 gtk_widget_get_allocation(tabs[n].image, &imageAllocation);
276 gtk_widget_get_allocation(tabs[n].title, &titleAllocation);
277 gtk_widget_get_allocation(tabs[n].close, &closeAllocation);
278 width = max(width, imageAllocation.width + titleAllocation.width +
279 (state().items[n]->closable() ? closeAllocation.width : 0)
280 );
281 }
282
283 return width;
284 }
285
286 }
287
288 #endif