libui/redo/unix/entry.c

103 lines
2.3 KiB
C
Raw Normal View History

2015-06-11 18:07:06 -05:00
// 11 june 2015
#include "uipriv_unix.h"
struct entry {
uiEntry e;
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
};
uiDefineControlType(uiEntry, uiTypeEntry, struct entry)
2015-06-30 10:26:54 -05:00
static void onChanged(GtkEditable *editable, gpointer data)
{
struct entry *e = (struct entry *) data;
(*(e->onChanged))(uiEntry(e), e->onChangedData);
}
2015-06-11 18:07:06 -05:00
static uintptr_t entryHandle(uiControl *c)
{
struct entry *e = (struct entry *) c;
return (uintptr_t) (e->widget);
}
static void defaultOnChanged(uiEntry *e, void *data)
{
// do nothing
}
static char *entryText(uiEntry *ee)
{
struct entry *e = (struct entry *) ee;
2015-06-30 10:26:54 -05:00
return uiUnixStrdupText(gtk_entry_get_text(e->entry));
2015-06-11 18:07:06 -05:00
}
static void entrySetText(uiEntry *ee, const char *text)
{
struct entry *e = (struct entry *) ee;
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
}
static void entryOnChanged(uiEntry *ee, void (*f)(uiEntry *, void *), void *data)
{
struct entry *e = (struct entry *) ee;
e->onChanged = f;
e->onChangedData = data;
}
static int entryReadOnly(uiEntry *ee)
{
struct entry *e = (struct entry *) ee;
2015-06-30 10:26:54 -05:00
return gtk_editable_get_editable(e->editable) == FALSE;
2015-06-11 18:07:06 -05:00
}
static void entrySetReadOnly(uiEntry *ee, int readonly)
{
struct entry *e = (struct entry *) ee;
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)
{
struct entry *e;
e = (struct entry *) uiNewControl(uiTypeEntry());
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);
uiUnixMakeSingleWidgetControl(uiControl(e), 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);
2015-06-11 18:07:06 -05:00
e->onChanged = defaultOnChanged;
uiControl(e)->Handle = entryHandle;
uiEntry(e)->Text = entryText;
uiEntry(e)->SetText = entrySetText;
uiEntry(e)->OnChanged = entryOnChanged;
uiEntry(e)->ReadOnly = entryReadOnly;
uiEntry(e)->SetReadOnly = entrySetReadOnly;
return uiEntry(e);
}