aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/unix/multilineentry.c
blob: 09ffd4601430d080241cea960d8b4096ae09b708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 6 december 2015
#include "uipriv_unix.h"

struct uiMultilineEntry {
	uiUnixControl c;
	GtkWidget *widget;
	GtkContainer *scontainer;
	GtkScrolledWindow *sw;
	GtkWidget *textviewWidget;
	GtkTextView *textview;
	GtkTextBuffer *textbuf;
	void (*onChanged)(uiMultilineEntry *, void *);
	void *onChangedData;
	gulong onChangedSignal;
};

uiUnixControlAllDefaults(uiMultilineEntry)

static void onChanged(GtkTextBuffer *textbuf, gpointer data)
{
	uiMultilineEntry *e = uiMultilineEntry(data);

	(*(e->onChanged))(e, e->onChangedData);
}

static void defaultOnChanged(uiMultilineEntry *e, void *data)
{
	// do nothing
}

char *uiMultilineEntryText(uiMultilineEntry *e)
{
	GtkTextIter start, end;
	char *tret, *out;

	gtk_text_buffer_get_start_iter(e->textbuf, &start);
	gtk_text_buffer_get_end_iter(e->textbuf, &end);
	tret = gtk_text_buffer_get_text(e->textbuf, &start, &end, TRUE);
	// theoretically we could just return tret because uiUnixStrdupText() is just g_strdup(), but if that ever changes we can't, so let's do it this way to be safe
	out = uiUnixStrdupText(tret);
	g_free(tret);
	return out;
}

void uiMultilineEntrySetText(uiMultilineEntry *e, const char *text)
{
	// we need to inhibit sending of ::changed because this WILL send a ::changed otherwise
	g_signal_handler_block(e->textbuf, e->onChangedSignal);
	gtk_text_buffer_set_text(e->textbuf, text, -1);
	g_signal_handler_unblock(e->textbuf, e->onChangedSignal);
}

// TODO scroll to end?
void uiMultilineEntryAppend(uiMultilineEntry *e, const char *text)
{
	GtkTextIter end;

	gtk_text_buffer_get_end_iter(e->textbuf, &end);
	// we need to inhibit sending of ::changed because this WILL send a ::changed otherwise
	g_signal_handler_block(e->textbuf, e->onChangedSignal);
	gtk_text_buffer_insert(e->textbuf, &end, text, -1);
	g_signal_handler_unblock(e->textbuf, e->onChangedSignal);
}

void uiMultilineEntryOnChanged(uiMultilineEntry *e, void (*f)(uiMultilineEntry *e, void *data), void *data)
{
	e->onChanged = f;
	e->onChangedData = data;
}

int uiMultilineEntryReadOnly(uiMultilineEntry *e)
{
	return gtk_text_view_get_editable(e->textview) == FALSE;
}

void uiMultilineEntrySetReadOnly(uiMultilineEntry *e, int readonly)
{
	gboolean editable;

	editable = TRUE;
	if (readonly)
		editable = FALSE;
	gtk_text_view_set_editable(e->textview, editable);
}

static uiMultilineEntry *finishMultilineEntry(GtkPolicyType hpolicy, GtkWrapMode wrapMode)
{
	uiMultilineEntry *e;

	uiUnixNewControl(uiMultilineEntry, e);

	e->widget = gtk_scrolled_window_new(NULL, NULL);
	e->scontainer = GTK_CONTAINER(e->widget);
	e->sw = GTK_SCROLLED_WINDOW(e->widget);
	gtk_scrolled_window_set_policy(e->sw,
		hpolicy,
		GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type(e->sw, GTK_SHADOW_IN);

	e->textviewWidget = gtk_text_view_new();
	e->textview = GTK_TEXT_VIEW(e->textviewWidget);
	gtk_text_view_set_wrap_mode(e->textview, wrapMode);

	gtk_container_add(e->scontainer, e->textviewWidget);
	// and make the text view visible; only the scrolled window's visibility is controlled by libui
	gtk_widget_show(e->textviewWidget);

	e->textbuf = gtk_text_view_get_buffer(e->textview);

	e->onChangedSignal = g_signal_connect(e->textbuf, "changed", G_CALLBACK(onChanged), e);
	uiMultilineEntryOnChanged(e, defaultOnChanged, NULL);

	return e;
}

uiMultilineEntry *uiNewMultilineEntry(void)
{
	return finishMultilineEntry(GTK_POLICY_NEVER, GTK_WRAP_WORD);
}

uiMultilineEntry *uiNewNonWrappingMultilineEntry(void)
{
	return finishMultilineEntry(GTK_POLICY_AUTOMATIC, GTK_WRAP_NONE);
}