libui/unix/combobox.c

83 lines
1.9 KiB
C
Raw Normal View History

2015-06-11 18:07:06 -05:00
// 11 june 2015
#include "uipriv_unix.h"
struct uiCombobox {
uiUnixControl c;
2015-06-11 18:07:06 -05:00
GtkWidget *widget;
GtkComboBox *combobox;
GtkComboBoxText *comboboxText;
void (*onSelected)(uiCombobox *, void *);
void *onSelectedData;
gulong onSelectedSignal;
2015-06-11 18:07:06 -05:00
};
uiUnixDefineControl(
uiCombobox, // type name
uiComboboxType // type function
)
2015-06-11 18:07:06 -05:00
// TODO this is triggered when editing an editable combobox's text
static void onChanged(GtkComboBox *cbox, gpointer data)
{
uiCombobox *c = uiCombobox(data);
(*(c->onSelected))(c, c->onSelectedData);
}
static void defaultOnSelected(uiCombobox *c, void *data)
{
// do nothing
}
void uiComboboxAppend(uiCombobox *c, const char *text)
2015-06-11 18:07:06 -05:00
{
2015-07-01 10:29:19 -05:00
gtk_combo_box_text_append(c->comboboxText, NULL, text);
2015-06-11 18:07:06 -05:00
}
intmax_t uiComboboxSelected(uiCombobox *c)
{
return gtk_combo_box_get_active(c->combobox);
}
void uiComboboxSetSelected(uiCombobox *c, intmax_t n)
{
// we need to inhibit sending of ::changed because this WILL send a ::changed otherwise
g_signal_handler_block(c->combobox, c->onSelectedSignal);
gtk_combo_box_set_active(c->combobox, n);
g_signal_handler_unblock(c->combobox, c->onSelectedSignal);
}
void uiComboboxOnSelected(uiCombobox *c, void (*f)(uiCombobox *c, void *data), void *data)
{
c->onSelected = f;
c->onSelectedData = data;
}
static uiCombobox *finishNewCombobox(GtkWidget *(*newfunc)(void))
2015-06-11 18:07:06 -05:00
{
uiCombobox *c;
2015-06-11 18:07:06 -05:00
c = (uiCombobox *) uiNewControl(uiComboboxType());
2015-06-11 18:07:06 -05:00
c->widget = (*newfunc)();
c->combobox = GTK_COMBO_BOX(c->widget);
c->comboboxText = GTK_COMBO_BOX_TEXT(c->widget);
2015-06-11 18:07:06 -05:00
c->onSelectedSignal = g_signal_connect(c->widget, "changed", G_CALLBACK(onChanged), c);
uiComboboxOnSelected(c, defaultOnSelected, NULL);
uiUnixFinishNewControl(c, uiCombobox);
2015-06-11 18:07:06 -05:00
return c;
2015-06-11 18:07:06 -05:00
}
uiCombobox *uiNewCombobox(void)
{
2015-06-14 18:58:00 -05:00
return finishNewCombobox(gtk_combo_box_text_new);
2015-06-11 18:07:06 -05:00
}
uiCombobox *uiNewEditableCombobox(void)
{
2015-06-14 18:58:00 -05:00
return finishNewCombobox(gtk_combo_box_text_new_with_entry);
2015-06-11 18:07:06 -05:00
}