1// Same copyright and license as the rest of the files in this project
2
3package gtk
4
5// #include <gtk/gtk.h>
6// #include "gtk.go.h"
7import "C"
8import (
9	"runtime"
10	"unsafe"
11
12	"github.com/gotk3/gotk3/gdk"
13	"github.com/gotk3/gotk3/glib"
14	"github.com/gotk3/gotk3/pango"
15)
16
17func init() {
18
19	tm := []glib.TypeMarshaler{
20		{glib.Type(C.gtk_text_window_type_get_type()), marshalTextWindowType},
21	}
22
23	glib.RegisterGValueMarshalers(tm)
24}
25
26// TextWindowType is a representation of GTK's GtkTextWindowType.
27type TextWindowType int
28
29const (
30	TEXT_WINDOW_PRIVATE TextWindowType = C.GTK_TEXT_WINDOW_PRIVATE
31	TEXT_WINDOW_WIDGET  TextWindowType = C.GTK_TEXT_WINDOW_WIDGET
32	TEXT_WINDOW_TEXT    TextWindowType = C.GTK_TEXT_WINDOW_TEXT
33	TEXT_WINDOW_LEFT    TextWindowType = C.GTK_TEXT_WINDOW_LEFT
34	TEXT_WINDOW_RIGHT   TextWindowType = C.GTK_TEXT_WINDOW_RIGHT
35	TEXT_WINDOW_TOP     TextWindowType = C.GTK_TEXT_WINDOW_TOP
36	TEXT_WINDOW_BOTTOM  TextWindowType = C.GTK_TEXT_WINDOW_BOTTOM
37)
38
39func marshalTextWindowType(p uintptr) (interface{}, error) {
40	c := C.g_value_get_enum((*C.GValue)(unsafe.Pointer(p)))
41	return TextWindowType(c), nil
42}
43
44/*
45 * GtkTextView
46 */
47
48// TextView is a representation of GTK's GtkTextView
49type TextView struct {
50	Container
51}
52
53// native returns a pointer to the underlying GtkTextView.
54func (v *TextView) native() *C.GtkTextView {
55	if v == nil || v.GObject == nil {
56		return nil
57	}
58	p := unsafe.Pointer(v.GObject)
59	return C.toGtkTextView(p)
60}
61
62func marshalTextView(p uintptr) (interface{}, error) {
63	c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
64	obj := glib.Take(unsafe.Pointer(c))
65	return wrapTextView(obj), nil
66}
67
68func wrapTextView(obj *glib.Object) *TextView {
69	if obj == nil {
70		return nil
71	}
72
73	return &TextView{Container{Widget{glib.InitiallyUnowned{obj}}}}
74}
75
76// TextViewNew is a wrapper around gtk_text_view_new().
77func TextViewNew() (*TextView, error) {
78	c := C.gtk_text_view_new()
79	if c == nil {
80		return nil, nilPtrErr
81	}
82	return wrapTextView(glib.Take(unsafe.Pointer(c))), nil
83}
84
85// TextViewNewWithBuffer is a wrapper around gtk_text_view_new_with_buffer().
86func TextViewNewWithBuffer(buf *TextBuffer) (*TextView, error) {
87	cbuf := buf.native()
88	c := C.gtk_text_view_new_with_buffer(cbuf)
89	return wrapTextView(glib.Take(unsafe.Pointer(c))), nil
90}
91
92// GetBuffer is a wrapper around gtk_text_view_get_buffer().
93func (v *TextView) GetBuffer() (*TextBuffer, error) {
94	c := C.gtk_text_view_get_buffer(v.native())
95	if c == nil {
96		return nil, nilPtrErr
97	}
98	return wrapTextBuffer(glib.Take(unsafe.Pointer(c))), nil
99}
100
101// SetBuffer is a wrapper around gtk_text_view_set_buffer().
102func (v *TextView) SetBuffer(buffer *TextBuffer) {
103	C.gtk_text_view_set_buffer(v.native(), buffer.native())
104}
105
106// SetEditable is a wrapper around gtk_text_view_set_editable().
107func (v *TextView) SetEditable(editable bool) {
108	C.gtk_text_view_set_editable(v.native(), gbool(editable))
109}
110
111// GetEditable is a wrapper around gtk_text_view_get_editable().
112func (v *TextView) GetEditable() bool {
113	c := C.gtk_text_view_get_editable(v.native())
114	return gobool(c)
115}
116
117// SetWrapMode is a wrapper around gtk_text_view_set_wrap_mode().
118func (v *TextView) SetWrapMode(wrapMode WrapMode) {
119	C.gtk_text_view_set_wrap_mode(v.native(), C.GtkWrapMode(wrapMode))
120}
121
122// GetWrapMode is a wrapper around gtk_text_view_get_wrap_mode().
123func (v *TextView) GetWrapMode() WrapMode {
124	return WrapMode(C.gtk_text_view_get_wrap_mode(v.native()))
125}
126
127// SetCursorVisible is a wrapper around gtk_text_view_set_cursor_visible().
128func (v *TextView) SetCursorVisible(visible bool) {
129	C.gtk_text_view_set_cursor_visible(v.native(), gbool(visible))
130}
131
132// GetCursorVisible is a wrapper around gtk_text_view_get_cursor_visible().
133func (v *TextView) GetCursorVisible() bool {
134	c := C.gtk_text_view_get_cursor_visible(v.native())
135	return gobool(c)
136}
137
138// SetOverwrite is a wrapper around gtk_text_view_set_overwrite().
139func (v *TextView) SetOverwrite(overwrite bool) {
140	C.gtk_text_view_set_overwrite(v.native(), gbool(overwrite))
141}
142
143// GetOverwrite is a wrapper around gtk_text_view_get_overwrite().
144func (v *TextView) GetOverwrite() bool {
145	c := C.gtk_text_view_get_overwrite(v.native())
146	return gobool(c)
147}
148
149// SetJustification is a wrapper around gtk_text_view_set_justification().
150func (v *TextView) SetJustification(justify Justification) {
151	C.gtk_text_view_set_justification(v.native(), C.GtkJustification(justify))
152}
153
154// GetJustification is a wrapper around gtk_text_view_get_justification().
155func (v *TextView) GetJustification() Justification {
156	c := C.gtk_text_view_get_justification(v.native())
157	return Justification(c)
158}
159
160// SetAcceptsTab is a wrapper around gtk_text_view_set_accepts_tab().
161func (v *TextView) SetAcceptsTab(acceptsTab bool) {
162	C.gtk_text_view_set_accepts_tab(v.native(), gbool(acceptsTab))
163}
164
165// GetAcceptsTab is a wrapper around gtk_text_view_get_accepts_tab().
166func (v *TextView) GetAcceptsTab() bool {
167	c := C.gtk_text_view_get_accepts_tab(v.native())
168	return gobool(c)
169}
170
171// SetPixelsAboveLines is a wrapper around gtk_text_view_set_pixels_above_lines().
172func (v *TextView) SetPixelsAboveLines(px int) {
173	C.gtk_text_view_set_pixels_above_lines(v.native(), C.gint(px))
174}
175
176// GetPixelsAboveLines is a wrapper around gtk_text_view_get_pixels_above_lines().
177func (v *TextView) GetPixelsAboveLines() int {
178	c := C.gtk_text_view_get_pixels_above_lines(v.native())
179	return int(c)
180}
181
182// SetPixelsBelowLines is a wrapper around gtk_text_view_set_pixels_below_lines().
183func (v *TextView) SetPixelsBelowLines(px int) {
184	C.gtk_text_view_set_pixels_below_lines(v.native(), C.gint(px))
185}
186
187// GetPixelsBelowLines is a wrapper around gtk_text_view_get_pixels_below_lines().
188func (v *TextView) GetPixelsBelowLines() int {
189	c := C.gtk_text_view_get_pixels_below_lines(v.native())
190	return int(c)
191}
192
193// SetPixelsInsideWrap is a wrapper around gtk_text_view_set_pixels_inside_wrap().
194func (v *TextView) SetPixelsInsideWrap(px int) {
195	C.gtk_text_view_set_pixels_inside_wrap(v.native(), C.gint(px))
196}
197
198// GetPixelsInsideWrap is a wrapper around gtk_text_view_get_pixels_inside_wrap().
199func (v *TextView) GetPixelsInsideWrap() int {
200	c := C.gtk_text_view_get_pixels_inside_wrap(v.native())
201	return int(c)
202}
203
204// SetLeftMargin is a wrapper around gtk_text_view_set_left_margin().
205func (v *TextView) SetLeftMargin(margin int) {
206	C.gtk_text_view_set_left_margin(v.native(), C.gint(margin))
207}
208
209// GetLeftMargin is a wrapper around gtk_text_view_get_left_margin().
210func (v *TextView) GetLeftMargin() int {
211	c := C.gtk_text_view_get_left_margin(v.native())
212	return int(c)
213}
214
215// SetRightMargin is a wrapper around gtk_text_view_set_right_margin().
216func (v *TextView) SetRightMargin(margin int) {
217	C.gtk_text_view_set_right_margin(v.native(), C.gint(margin))
218}
219
220// GetRightMargin is a wrapper around gtk_text_view_get_right_margin().
221func (v *TextView) GetRightMargin() int {
222	c := C.gtk_text_view_get_right_margin(v.native())
223	return int(c)
224}
225
226// SetIndent is a wrapper around gtk_text_view_set_indent().
227func (v *TextView) SetIndent(indent int) {
228	C.gtk_text_view_set_indent(v.native(), C.gint(indent))
229}
230
231// GetIndent is a wrapper around gtk_text_view_get_indent().
232func (v *TextView) GetIndent() int {
233	c := C.gtk_text_view_get_indent(v.native())
234	return int(c)
235}
236
237// SetTabs is a wrapper around gtk_text_view_set_tabs().
238func (v *TextView) SetTabs(tabs *pango.TabArray) {
239	C.gtk_text_view_set_tabs(v.native(), (*C.PangoTabArray)(unsafe.Pointer(tabs.Native())))
240}
241
242// GetTabs is a wrapper around gtk_text_view_get_tabs().
243func (v *TextView) GetTabs() (*pango.TabArray, error) {
244	c := C.gtk_text_view_get_tabs(v.native())
245	if c == nil {
246		return nil, nilPtrErr
247	}
248	ta := pango.WrapTabArray(uintptr(unsafe.Pointer(c)))
249	runtime.SetFinalizer(ta, (*pango.TabArray).Free)
250	return ta, nil
251}
252
253// SetInputHints is a wrapper around gtk_text_view_set_input_hints().
254func (v *TextView) SetInputHints(hints InputHints) {
255	C.gtk_text_view_set_input_hints(v.native(), C.GtkInputHints(hints))
256}
257
258// GetInputHints is a wrapper around gtk_text_view_get_input_hints().
259func (v *TextView) GetInputHints() InputHints {
260	c := C.gtk_text_view_get_input_hints(v.native())
261	return InputHints(c)
262}
263
264// SetInputPurpose is a wrapper around gtk_text_view_set_input_purpose().
265func (v *TextView) SetInputPurpose(purpose InputPurpose) {
266	C.gtk_text_view_set_input_purpose(v.native(),
267		C.GtkInputPurpose(purpose))
268}
269
270// GetInputPurpose is a wrapper around gtk_text_view_get_input_purpose().
271func (v *TextView) GetInputPurpose() InputPurpose {
272	c := C.gtk_text_view_get_input_purpose(v.native())
273	return InputPurpose(c)
274}
275
276// ScrollToMark is a wrapper around gtk_text_view_scroll_to_mark().
277func (v *TextView) ScrollToMark(mark *TextMark, within_margin float64, use_align bool, xalign, yalign float64) {
278	C.gtk_text_view_scroll_to_mark(v.native(), mark.native(), C.gdouble(within_margin), gbool(use_align), C.gdouble(xalign), C.gdouble(yalign))
279}
280
281// ScrollToIter is a wrapper around gtk_text_view_scroll_to_iter().
282func (v *TextView) ScrollToIter(iter *TextIter, within_margin float64, use_align bool, xalign, yalign float64) bool {
283	return gobool(C.gtk_text_view_scroll_to_iter(v.native(), iter.native(), C.gdouble(within_margin), gbool(use_align), C.gdouble(xalign), C.gdouble(yalign)))
284}
285
286// ScrollMarkOnscreen is a wrapper around gtk_text_view_scroll_mark_onscreen().
287func (v *TextView) ScrollMarkOnscreen(mark *TextMark) {
288	C.gtk_text_view_scroll_mark_onscreen(v.native(), mark.native())
289}
290
291// MoveMarkOnscreen is a wrapper around gtk_text_view_move_mark_onscreen().
292func (v *TextView) MoveMarkOnscreen(mark *TextMark) bool {
293	return gobool(C.gtk_text_view_move_mark_onscreen(v.native(), mark.native()))
294}
295
296// PlaceCursorOnscreen is a wrapper around gtk_text_view_place_cursor_onscreen().
297func (v *TextView) PlaceCursorOnscreen() bool {
298	return gobool(C.gtk_text_view_place_cursor_onscreen(v.native()))
299}
300
301// GetVisibleRect is a wrapper around gtk_text_view_get_visible_rect().
302func (v *TextView) GetVisibleRect() *gdk.Rectangle {
303	var rect C.GdkRectangle
304	C.gtk_text_view_get_visible_rect(v.native(), &rect)
305	return gdk.WrapRectangle(uintptr(unsafe.Pointer(&rect)))
306}
307
308// GetIterLocation is a wrapper around gtk_text_view_get_iter_location().
309func (v *TextView) GetIterLocation(iter *TextIter) *gdk.Rectangle {
310	var rect C.GdkRectangle
311	C.gtk_text_view_get_iter_location(v.native(), iter.native(), &rect)
312	return gdk.WrapRectangle(uintptr(unsafe.Pointer(&rect)))
313}
314
315// GetCursorLocations is a wrapper around gtk_text_view_get_cursor_locations().
316func (v *TextView) GetCursorLocations(iter *TextIter) (strong, weak *gdk.Rectangle) {
317	var strongRect, weakRect C.GdkRectangle
318	C.gtk_text_view_get_cursor_locations(v.native(), iter.native(), &strongRect, &weakRect)
319	return gdk.WrapRectangle(uintptr(unsafe.Pointer(&strongRect))), gdk.WrapRectangle(uintptr(unsafe.Pointer(&weakRect)))
320}
321
322// GetLineAtY is a wrapper around gtk_text_view_get_line_at_y().
323func (v *TextView) GetLineAtY(y int) (*TextIter, int) {
324	var iter TextIter
325	var line_top C.gint
326	iiter := (C.GtkTextIter)(iter)
327	C.gtk_text_view_get_line_at_y(v.native(), &iiter, C.gint(y), &line_top)
328	return &iter, int(line_top)
329}
330
331// GetLineYrange is a wrapper around gtk_text_view_get_line_yrange().
332func (v *TextView) GetLineYrange(iter *TextIter) (y, height int) {
333	var yx, heightx C.gint
334	C.gtk_text_view_get_line_yrange(v.native(), iter.native(), &yx, &heightx)
335	return int(yx), int(heightx)
336}
337
338// GetIterAtLocation is a wrapper around gtk_text_view_get_iter_at_location().
339func (v *TextView) GetIterAtLocation(x, y int) *TextIter {
340	var iter C.GtkTextIter
341	C.gtk_text_view_get_iter_at_location(v.native(), &iter, C.gint(x), C.gint(y))
342	return (*TextIter)(&iter)
343}
344
345// GetIterAtPosition is a wrapper around gtk_text_view_get_iter_at_position().
346func (v *TextView) GetIterAtPosition(x, y int) (*TextIter, int) {
347	var iter C.GtkTextIter
348	var trailing C.gint
349	C.gtk_text_view_get_iter_at_position(v.native(), &iter, &trailing, C.gint(x), C.gint(y))
350	return (*TextIter)(&iter), int(trailing)
351}
352
353// BufferToWindowCoords is a wrapper around gtk_text_view_buffer_to_window_coords().
354func (v *TextView) BufferToWindowCoords(win TextWindowType, buffer_x, buffer_y int) (window_x, window_y int) {
355	var wx, wy C.gint
356	C.gtk_text_view_buffer_to_window_coords(v.native(), C.GtkTextWindowType(win), C.gint(buffer_x), C.gint(buffer_y), &wx, &wy)
357	return int(wx), int(wy)
358}
359
360// WindowToBufferCoords is a wrapper around gtk_text_view_window_to_buffer_coords().
361func (v *TextView) WindowToBufferCoords(win TextWindowType, window_x, window_y int) (buffer_x, buffer_y int) {
362	var bx, by C.gint
363	C.gtk_text_view_window_to_buffer_coords(v.native(), C.GtkTextWindowType(win), C.gint(window_x), C.gint(window_y), &bx, &by)
364	return int(bx), int(by)
365}
366
367// GetWindow is a wrapper around gtk_text_view_get_window().
368func (v *TextView) GetWindow(win TextWindowType) *gdk.Window {
369	c := C.gtk_text_view_get_window(v.native(), C.GtkTextWindowType(win))
370	if c == nil {
371		return nil
372	}
373	return &gdk.Window{glib.Take(unsafe.Pointer(c))}
374}
375
376// GetWindowType is a wrapper around gtk_text_view_get_window_type().
377func (v *TextView) GetWindowType(w *gdk.Window) TextWindowType {
378	return TextWindowType(C.gtk_text_view_get_window_type(v.native(), (*C.GdkWindow)(unsafe.Pointer(w.Native()))))
379}
380
381// SetBorderWindowSize is a wrapper around gtk_text_view_set_border_window_size().
382func (v *TextView) SetBorderWindowSize(tp TextWindowType, size int) {
383	C.gtk_text_view_set_border_window_size(v.native(), C.GtkTextWindowType(tp), C.gint(size))
384}
385
386// GetBorderWindowSize is a wrapper around gtk_text_view_get_border_window_size().
387func (v *TextView) GetBorderWindowSize(tp TextWindowType) int {
388	return int(C.gtk_text_view_get_border_window_size(v.native(), C.GtkTextWindowType(tp)))
389}
390
391// ForwardDisplayLine is a wrapper around gtk_text_view_forward_display_line().
392func (v *TextView) ForwardDisplayLine(iter *TextIter) bool {
393	return gobool(C.gtk_text_view_forward_display_line(v.native(), iter.native()))
394}
395
396// BackwardDisplayLine is a wrapper around gtk_text_view_backward_display_line().
397func (v *TextView) BackwardDisplayLine(iter *TextIter) bool {
398	return gobool(C.gtk_text_view_backward_display_line(v.native(), iter.native()))
399}
400
401// ForwardDisplayLineEnd is a wrapper around gtk_text_view_forward_display_line_end().
402func (v *TextView) ForwardDisplayLineEnd(iter *TextIter) bool {
403	return gobool(C.gtk_text_view_forward_display_line_end(v.native(), iter.native()))
404}
405
406// BackwardDisplayLineStart is a wrapper around gtk_text_view_backward_display_line_start().
407func (v *TextView) BackwardDisplayLineStart(iter *TextIter) bool {
408	return gobool(C.gtk_text_view_backward_display_line_start(v.native(), iter.native()))
409}
410
411// StartsDisplayLine is a wrapper around gtk_text_view_starts_display_line().
412func (v *TextView) StartsDisplayLine(iter *TextIter) bool {
413	return gobool(C.gtk_text_view_starts_display_line(v.native(), iter.native()))
414}
415
416// MoveVisually is a wrapper around gtk_text_view_move_visually().
417func (v *TextView) MoveVisually(iter *TextIter, count int) bool {
418	return gobool(C.gtk_text_view_move_visually(v.native(), iter.native(), C.gint(count)))
419}
420
421// AddChildInWindow is a wrapper around gtk_text_view_add_child_in_window().
422func (v *TextView) AddChildInWindow(child IWidget, tp TextWindowType, xpos, ypos int) {
423	C.gtk_text_view_add_child_in_window(v.native(), child.toWidget(), C.GtkTextWindowType(tp), C.gint(xpos), C.gint(ypos))
424}
425
426// MoveChild is a wrapper around gtk_text_view_move_child().
427func (v *TextView) MoveChild(child IWidget, xpos, ypos int) {
428	C.gtk_text_view_move_child(v.native(), child.toWidget(), C.gint(xpos), C.gint(ypos))
429}
430
431// ImContextFilterKeypress is a wrapper around gtk_text_view_im_context_filter_keypress().
432func (v *TextView) ImContextFilterKeypress(event *gdk.EventKey) bool {
433	return gobool(C.gtk_text_view_im_context_filter_keypress(v.native(), (*C.GdkEventKey)(unsafe.Pointer(event.Native()))))
434}
435
436// ResetImContext is a wrapper around gtk_text_view_reset_im_context().
437func (v *TextView) ResetImContext() {
438	C.gtk_text_view_reset_im_context(v.native())
439}
440
441// AddChildAtAnchor is a wrapper around gtk_text_view_add_child_at_anchor().
442func (v *TextView) AddChildAtAnchor(child IWidget, anchor *TextChildAnchor) {
443	C.gtk_text_view_add_child_at_anchor(v.native(), child.toWidget(), anchor.native())
444}
445
446// TODO:
447// GtkTextAttributes * 	gtk_text_view_get_default_attributes () -- GtkTextAttributes
448// GtkTextViewLayer
449