-
Notifications
You must be signed in to change notification settings - Fork 1
/
textView.go
207 lines (153 loc) · 3.82 KB
/
textView.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
type textView struct {
app *app
GTKtextView *gtk.TextView
}
func newTextView(app *app) *textView {
tv, err := gtk.TextViewNew()
if err != nil {
log.Fatal("failed setting up gtk textview: ", err)
}
adjv, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0)
adjh, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0)
scrolled, err := gtk.ScrolledWindowNew(adjv, adjh)
if err != nil {
log.Fatal("failed setting up gtk scroll: ", err)
}
scrolled.SetHExpand(true)
scrolled.SetVExpand(true)
scrolled.SetPolicy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scrolled.Add(tv)
app.grid.Add(scrolled)
tv.SetMonospace(true)
target, err := gtk.TargetEntryNew("text/uri-list", gtk.TargetFlags(0), 0)
if err != nil {
log.Fatal("failed creating target for textView", err)
}
tv.DragDestSet(gtk.DEST_DEFAULT_ALL, []gtk.TargetEntry{*target}, gdk.ACTION_COPY)
return &textView{
app: app,
GTKtextView: tv,
}
}
func (t *textView) SetFont(font string, size int64) error {
styleContext, err := t.GTKtextView.GetStyleContext()
if err != nil {
return err
}
cssProvider, err := gtk.CssProviderNew()
if err != nil {
return err
}
css := `
textview {
padding-top: 2px;
padding-left: 2px;
font-family: "` + font + `", "Lucida Console";
font-size: ` + strconv.FormatInt(size, 10) + `pt;
}
`
err = cssProvider.LoadFromData(css)
if err != nil {
return err
}
// Get the default screen for the GTK application.
screen, err := styleContext.GetScreen()
if err != nil {
return err
}
// Add the CSS provider to the screen's style context.
gtk.AddProviderForScreen(screen, cssProvider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
t.GTKtextView.ShowAll()
fmt.Printf("setting font: '%s' %d\n", font, size)
return nil
}
func (t *textView) SetText(text string) {
b, _ := t.GTKtextView.GetBuffer()
b.SetText(text)
}
func (t *textView) WrapText(wrap bool) {
if wrap {
t.GTKtextView.SetWrapMode(gtk.WRAP_WORD)
} else {
t.GTKtextView.SetWrapMode(gtk.WRAP_NONE)
}
}
func (t *textView) Copy() {
b, _ := t.GTKtextView.GetBuffer()
if !t.GTKtextView.IsFocus() || !b.GetHasSelection() {
return
}
t.GTKtextView.Emit("copy-clipboard", glib.TYPE_NONE)
}
func (t *textView) Cut() {
b, _ := t.GTKtextView.GetBuffer()
if !t.GTKtextView.IsFocus() || !b.GetHasSelection() {
return
}
t.GTKtextView.Emit("cut-clipboard", glib.TYPE_NONE)
}
func (t *textView) Paste() {
if !t.GTKtextView.IsFocus() {
return
}
t.GTKtextView.Emit("paste-clipboard", glib.TYPE_NONE)
}
func (t *textView) SelectAll() {
if !t.GTKtextView.IsFocus() {
return
}
t.GTKtextView.Emit("select-all", glib.TYPE_NONE)
}
func (t *textView) Backspace() {
if !t.GTKtextView.IsFocus() {
return
}
t.GTKtextView.Emit("backspace", glib.TYPE_NONE)
}
func (t *textView) LoadSource(filename string) (err error) {
src, err := os.ReadFile(filename)
if err != nil {
return
}
buff, err := t.GTKtextView.GetBuffer()
if err != nil {
return
}
t.Clear()
buff.Insert(buff.GetStartIter(), string(src))
return
}
func (t *textView) SaveSource(filename string) error {
buff, _ := t.GTKtextView.GetBuffer()
// TODO: Add a write file error
source, err := buff.GetText(buff.GetStartIter(), buff.GetEndIter(), true)
if err != nil {
return err
}
err = os.WriteFile(filename, []byte(source), 0666)
return err
}
func (t *textView) Clear() {
buff, _ := t.GTKtextView.GetBuffer()
buff.Delete(buff.GetStartIter(), buff.GetEndIter())
}
func (t *textView) InsertTimestamp() {
timestamp := time.Now().Format("1:04 PM 02/01/2006")
buff, _ := t.GTKtextView.GetBuffer()
buff.InsertAtCursor(timestamp)
}
func (t *textView) GoToLine(i int) {
buff, _ := t.GTKtextView.GetBuffer()
buff.PlaceCursor(buff.GetIterAtLine(i))
}