| 1 |
#if defined(Hiro_ComboEdit) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
static auto ComboEdit_activate(GtkComboBox* comboBox, pComboEdit* p) -> void { p->self().doActivate(); } |
| 6 |
static auto ComboEdit_change(GtkComboBox* comboBox, pComboEdit* p) -> void { p->_updateText(); } |
| 7 |
|
| 8 |
auto pComboEdit::construct() -> void { |
| 9 |
gtkListStore = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING); |
| 10 |
gtkTreeModel = GTK_TREE_MODEL(gtkListStore); |
| 11 |
gtkWidget = gtk_combo_box_new_with_model_and_entry(gtkTreeModel); |
| 12 |
gtkComboBox = GTK_COMBO_BOX(gtkWidget); |
| 13 |
|
| 14 |
gtk_cell_layout_clear(GTK_CELL_LAYOUT(gtkWidget)); |
| 15 |
gtkCellIcon = gtk_cell_renderer_pixbuf_new(); |
| 16 |
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(gtkWidget), gtkCellIcon, false); |
| 17 |
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(gtkWidget), gtkCellIcon, "pixbuf", 0, nullptr); |
| 18 |
gtkCellText = gtk_cell_renderer_text_new(); |
| 19 |
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(gtkWidget), gtkCellText, true); |
| 20 |
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(gtkWidget), gtkCellText, "text", 1, nullptr); |
| 21 |
|
| 22 |
//we must call gtk_combo_box_set_entry_text_column(1) in order for GtkComboBoxEntry to pull text |
| 23 |
//however, this API call throws a Gtk-CRITICAL assertion that column 1 is not a cell renderer |
| 24 |
//this is, however, incorrect. it *is* a renderer, and the API call works as expected |
| 25 |
//so in order to suppress the error message, we redirect stderr to /dev/null temporarily |
| 26 |
#if !defined(PLATFORM_WINDOWS) |
| 27 |
s32 stdErr = dup(STDERR_FILENO); |
| 28 |
s32 devNull = open("/dev/null", 0); |
| 29 |
dup2(devNull, STDERR_FILENO); |
| 30 |
#endif |
| 31 |
gtk_combo_box_set_entry_text_column(gtkComboBox, 1); |
| 32 |
#if !defined(PLATFORM_WINDOWS) |
| 33 |
dup2(stdErr, STDERR_FILENO); |
| 34 |
close(devNull); |
| 35 |
close(stdErr); |
| 36 |
#endif |
| 37 |
|
| 38 |
setBackgroundColor(state().backgroundColor); |
| 39 |
setEditable(state().editable); |
| 40 |
setForegroundColor(state().foregroundColor); |
| 41 |
for(auto& item : state().items) append(item); |
| 42 |
|
| 43 |
g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN(gtkComboBox))), "activate", G_CALLBACK(ComboEdit_activate), (gpointer)this); |
| 44 |
g_signal_connect(G_OBJECT(gtkWidget), "changed", G_CALLBACK(ComboEdit_change), (gpointer)this); |
| 45 |
|
| 46 |
pWidget::construct(); |
| 47 |
} |
| 48 |
|
| 49 |
auto pComboEdit::destruct() -> void { |
| 50 |
gtk_widget_destroy(gtkWidget); |
| 51 |
} |
| 52 |
|
| 53 |
auto pComboEdit::append(sComboEditItem item) -> void { |
| 54 |
lock(); |
| 55 |
if(auto self = item->self()) { |
| 56 |
gtk_list_store_append(gtkListStore, &self->gtkIter); |
| 57 |
self->setIcon(item->state.icon); |
| 58 |
self->setText(item->state.text); |
| 59 |
} |
| 60 |
unlock(); |
| 61 |
} |
| 62 |
|
| 63 |
auto pComboEdit::minimumSize() const -> Size { |
| 64 |
auto font = self().font(true); |
| 65 |
s32 maximumWidth = 0; |
| 66 |
for(auto& item : state().items) { |
| 67 |
maximumWidth = max(maximumWidth, |
| 68 |
(item->state.icon ? pFont::size(font, " ").height() + 2 : 0) |
| 69 |
+ pFont::size(font, item->state.text).width() |
| 70 |
); |
| 71 |
} |
| 72 |
return {maximumWidth + 40, pFont::size(font, " ").height() + 12}; |
| 73 |
} |
| 74 |
|
| 75 |
auto pComboEdit::remove(sComboEditItem item) -> void { |
| 76 |
lock(); |
| 77 |
if(auto delegate = item->self()) { |
| 78 |
gtk_list_store_remove(gtkListStore, &delegate->gtkIter); |
| 79 |
} |
| 80 |
unlock(); |
| 81 |
} |
| 82 |
|
| 83 |
auto pComboEdit::reset() -> void { |
| 84 |
lock(); |
| 85 |
gtk_list_store_clear(gtkListStore); |
| 86 |
unlock(); |
| 87 |
} |
| 88 |
|
| 89 |
auto pComboEdit::setBackgroundColor(Color color) -> void { |
| 90 |
GdkColor gdkColor = CreateColor(color); |
| 91 |
gtk_widget_modify_base(gtk_bin_get_child(GTK_BIN(gtkComboBox)), GTK_STATE_NORMAL, color ? &gdkColor : nullptr); |
| 92 |
} |
| 93 |
|
| 94 |
auto pComboEdit::setEditable(bool editable) -> void { |
| 95 |
gtk_editable_set_editable(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(gtkComboBox))), editable); |
| 96 |
} |
| 97 |
|
| 98 |
auto pComboEdit::setForegroundColor(Color color) -> void { |
| 99 |
GdkColor gdkColor = CreateColor(color); |
| 100 |
gtk_widget_modify_text(gtk_bin_get_child(GTK_BIN(gtkComboBox)), GTK_STATE_NORMAL, color ? &gdkColor : nullptr); |
| 101 |
} |
| 102 |
|
| 103 |
auto pComboEdit::setFont(const Font& font) -> void { |
| 104 |
pWidget::setFont(font); |
| 105 |
auto fontDescription = pFont::create(font); |
| 106 |
g_object_set(G_OBJECT(gtkCellText), "font-desc", fontDescription, nullptr); |
| 107 |
} |
| 108 |
|
| 109 |
auto pComboEdit::setText(const string& text) -> void { |
| 110 |
lock(); |
| 111 |
gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(gtkComboBox))), text); |
| 112 |
unlock(); |
| 113 |
} |
| 114 |
|
| 115 |
auto pComboEdit::_updateText() -> void { |
| 116 |
string text = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(gtkComboBox)))); |
| 117 |
if(text == state().text) return; |
| 118 |
state().text = text; |
| 119 |
if(!locked()) self().doChange(); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
#endif |