Debian Codegraph @

phoenix/gtk/widget/combo-button.cpp

raw
1 namespace phoenix {
2
3 static void ComboButton_change(ComboButton* self) {
4 if(!self->p.locked) {
5 self->state.selection = gtk_combo_box_get_active(GTK_COMBO_BOX(self->p.gtkWidget));
6 if(self->onChange) self->onChange();
7 }
8 }
9
10 void pComboButton::append(string text) {
11 gtk_combo_box_append_text(GTK_COMBO_BOX(gtkWidget), text);
12 if(itemCounter++ == 0) comboButton.setSelection(0);
13 }
14
15 Size pComboButton::minimumSize() {
16 unsigned maximumWidth = 0;
17 for(auto& item : comboButton.state.text) maximumWidth = max(maximumWidth, pFont::size(widget.state.font, item).width);
18
19 Size size = pFont::size(widget.state.font, " ");
20 return {maximumWidth + 44, size.height + 12};
21 }
22
23 void pComboButton::remove(unsigned selection) {
24 locked = true;
25 gtk_combo_box_remove_text(GTK_COMBO_BOX(gtkWidget), selection);
26 itemCounter--;
27 locked = false;
28
29 //when removing the actively selected item, reset the selection to the first entry
30 if(selection == comboButton.state.selection) comboButton.setSelection(0);
31 }
32
33 void pComboButton::reset() {
34 locked = true;
35 gtk_list_store_clear(GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(gtkWidget))));
36 itemCounter = 0;
37 locked = false;
38 }
39
40 void pComboButton::setSelection(unsigned selection) {
41 locked = true;
42 gtk_combo_box_set_active(GTK_COMBO_BOX(gtkWidget), selection);
43 locked = false;
44 }
45
46 void pComboButton::setText(unsigned selection, string text) {
47 locked = true;
48 gtk_combo_box_remove_text(GTK_COMBO_BOX(gtkWidget), selection);
49 gtk_combo_box_insert_text(GTK_COMBO_BOX(gtkWidget), selection, text);
50 gtk_combo_box_set_active(GTK_COMBO_BOX(gtkWidget), comboButton.state.selection);
51 locked = false;
52 }
53
54 void pComboButton::constructor() {
55 itemCounter = 0;
56 gtkWidget = gtk_combo_box_new_text();
57 g_signal_connect_swapped(G_OBJECT(gtkWidget), "changed", G_CALLBACK(ComboButton_change), (gpointer)&comboButton);
58
59 locked = true;
60 for(auto& text : comboButton.state.text) append(text);
61 locked = false;
62 setSelection(comboButton.state.selection);
63 }
64
65 void pComboButton::destructor() {
66 gtk_widget_destroy(gtkWidget);
67 }
68
69 void pComboButton::orphan() {
70 destructor();
71 constructor();
72 }
73
74 }