Debian Codegraph @

hiro/gtk/widget/combo-button.cpp

raw
1 #if defined(Hiro_ComboButton)
2
3 namespace hiro {
4
5 static auto ComboButton_change(GtkComboBox* comboBox, pComboButton* p) -> void { p->_updateSelected(); }
6
7 auto pComboButton::construct() -> void {
8 gtkListStore = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
9 gtkTreeModel = GTK_TREE_MODEL(gtkListStore);
10 gtkWidget = gtk_combo_box_new_with_model(gtkTreeModel);
11 gtkComboBox = GTK_COMBO_BOX(gtkWidget);
12
13 gtkCellIcon = gtk_cell_renderer_pixbuf_new();
14 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(gtkWidget), gtkCellIcon, false);
15 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(gtkWidget), gtkCellIcon, "pixbuf", 0, nullptr);
16 gtkCellText = gtk_cell_renderer_text_new();
17 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(gtkWidget), gtkCellText, true);
18 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(gtkWidget), gtkCellText, "text", 1, nullptr);
19
20 g_signal_connect(G_OBJECT(gtkWidget), "changed", G_CALLBACK(ComboButton_change), (gpointer)this);
21
22 pWidget::construct();
23 }
24
25 auto pComboButton::destruct() -> void {
26 gtk_widget_destroy(gtkWidget);
27 }
28
29 auto pComboButton::append(sComboButtonItem item) -> void {
30 }
31
32 auto pComboButton::minimumSize() const -> Size {
33 auto font = self().font(true);
34 s32 maximumWidth = 0;
35 for(auto& item : state().items) {
36 maximumWidth = max(maximumWidth,
37 (item->state.icon ? pFont::size(font, " ").height() + 2 : 0)
38 + pFont::size(font, item->state.text).width()
39 );
40 }
41 return {maximumWidth + 40, pFont::size(font, " ").height() + 12};
42 }
43
44 auto pComboButton::remove(sComboButtonItem item) -> void {
45 lock();
46 if(auto delegate = item->self()) {
47 gtk_list_store_remove(gtkListStore, &delegate->gtkIter);
48 if(gtk_combo_box_get_active(gtkComboBox) < 0) {
49 if(auto item = self().item(0)) item->setSelected();
50 }
51 }
52 unlock();
53 }
54
55 auto pComboButton::reset() -> void {
56 lock();
57 gtk_list_store_clear(gtkListStore);
58 unlock();
59 }
60
61 auto pComboButton::setFont(const Font& font) -> void {
62 pWidget::setFont(font);
63 auto fontDescription = pFont::create(font);
64 g_object_set(G_OBJECT(gtkCellText), "font-desc", fontDescription, nullptr);
65 }
66
67 auto pComboButton::_append(sComboButtonItem item) -> void {
68 lock();
69 if(auto self = item->self()) {
70 gtk_list_store_append(gtkListStore, &self->gtkIter);
71 self->setIcon(item->state.icon);
72 if(item->state.selected) self->setSelected();
73 self->setText(item->state.text);
74 }
75 if(gtk_combo_box_get_active(gtkComboBox) < 0) item->setSelected();
76 unlock();
77 }
78
79 auto pComboButton::_updateSelected() -> void {
80 for(auto& item : state().items) item->state.selected = false;
81 s32 selected = gtk_combo_box_get_active(gtkComboBox);
82 if(selected >= 0) {
83 if(auto item = self().item(selected)) {
84 item->state.selected = true;
85 if(!locked()) self().doChange();
86 }
87 }
88 }
89
90 }
91
92 #endif