libui/unix/entry.c

82 lines
1.7 KiB
C
Raw Normal View History

2015-06-11 18:07:06 -05:00
// 11 june 2015
#include "uipriv_unix.h"
struct uiEntry {
uiUnixControl c;
2015-06-11 18:07:06 -05:00
GtkWidget *widget;
2015-06-27 18:46:11 -05:00
GtkEntry *entry;
GtkEditable *editable;
2015-06-11 18:07:06 -05:00
void (*onChanged)(uiEntry *, void *);
void *onChangedData;
2015-06-30 10:26:54 -05:00
gulong onChangedSignal;
2015-06-11 18:07:06 -05:00
};
uiUnixDefineControl(
uiEntry // type name
)
2015-06-11 18:07:06 -05:00
2015-06-30 10:26:54 -05:00
static void onChanged(GtkEditable *editable, gpointer data)
{
2015-08-27 22:00:34 -05:00
uiEntry *e = uiEntry(data);
2015-06-30 10:26:54 -05:00
2015-08-27 22:00:34 -05:00
(*(e->onChanged))(e, e->onChangedData);
2015-06-30 10:26:54 -05:00
}
2015-06-11 18:07:06 -05:00
static void defaultOnChanged(uiEntry *e, void *data)
{
// do nothing
}
char *uiEntryText(uiEntry *e)
2015-06-11 18:07:06 -05:00
{
2015-06-30 10:26:54 -05:00
return uiUnixStrdupText(gtk_entry_get_text(e->entry));
2015-06-11 18:07:06 -05:00
}
void uiEntrySetText(uiEntry *e, const char *text)
2015-06-11 18:07:06 -05:00
{
2015-06-30 10:26:54 -05:00
// we need to inhibit sending of ::changed because this WILL send a ::changed otherwise
g_signal_handler_block(e->editable, e->onChangedSignal);
gtk_entry_set_text(e->entry, text);
g_signal_handler_unblock(e->editable, e->onChangedSignal);
2015-06-11 18:07:06 -05:00
// don't queue the control for resize; entry sizes are independent of their contents
}
void uiEntryOnChanged(uiEntry *e, void (*f)(uiEntry *, void *), void *data)
2015-06-11 18:07:06 -05:00
{
e->onChanged = f;
e->onChangedData = data;
}
int uiEntryReadOnly(uiEntry *e)
2015-06-11 18:07:06 -05:00
{
2015-06-30 10:26:54 -05:00
return gtk_editable_get_editable(e->editable) == FALSE;
2015-06-11 18:07:06 -05:00
}
void uiEntrySetReadOnly(uiEntry *e, int readonly)
2015-06-11 18:07:06 -05:00
{
2015-06-30 10:26:54 -05:00
gboolean editable;
2015-06-11 18:07:06 -05:00
2015-06-30 10:26:54 -05:00
editable = TRUE;
if (readonly)
editable = FALSE;
gtk_editable_set_editable(e->editable, editable);
2015-06-11 18:07:06 -05:00
}
uiEntry *uiNewEntry(void)
{
uiEntry *e;
2015-06-11 18:07:06 -05:00
e = (uiEntry *) uiNewControl(uiEntryType());
2015-06-11 18:07:06 -05:00
2015-06-27 18:46:11 -05:00
e->widget = gtk_entry_new();
e->entry = GTK_ENTRY(e->widget);
e->editable = GTK_EDITABLE(e->widget);
2015-06-11 18:07:06 -05:00
2015-06-30 10:26:54 -05:00
e->onChangedSignal = g_signal_connect(e->widget, "changed", G_CALLBACK(onChanged), e);
uiEntryOnChanged(e, defaultOnChanged, NULL);
2015-06-11 18:07:06 -05:00
uiUnixFinishNewControl(e, uiEntry);
2015-06-11 18:07:06 -05:00
return e;
2015-06-11 18:07:06 -05:00
}