forked from ying32/govcl
/
MainFormImpl.go
138 lines (116 loc) · 2.74 KB
/
MainFormImpl.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
// 在这里写你的事件
package main
import (
"bytes"
"fmt"
"net/http"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/types"
)
//::private::
type TMainFormFields struct {
webview *vcl.TMiniWebview
isLoaded bool
}
const (
webAddr = ":58988"
)
func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
f.webview = vcl.NewMiniWebview(f)
f.webview.SetParent(f.PnlWebview)
f.webview.SetAlign(types.AlClient)
f.webview.Navigate("about:blank")
f.setToolBtnClickEvent()
http.HandleFunc("/", f.httpHandleFunc)
go func() {
http.ListenAndServe(webAddr, nil)
}()
}
func (f *TMainForm) onToolBtnClick(sender vcl.IObject) {
btn := vcl.AsButton(sender)
setSelText := func(s string) {
f.MemoEditor.SetSelText(fmt.Sprintf("%s%s%s", s, f.MemoEditor.SelText(), s))
}
setSelText2 := func(s string) {
f.MemoEditor.SetSelText(fmt.Sprintf("%s %s", s, f.MemoEditor.SelText()))
}
switch btn.Tag() {
case 1:
f.MemoEditor.Undo()
case 2:
case 3:
setSelText("**")
case 4:
setSelText("~~")
case 5:
setSelText("*")
case 6:
setSelText2(">")
case 10:
setSelText2("#")
case 11:
setSelText2("##")
case 12:
setSelText2("###")
case 13:
setSelText2("####")
case 14:
setSelText2("#####")
case 15:
setSelText2("######")
case 16:
setSelText2("-")
case 17:
setSelText2("1.")
case 18:
setSelText2("\r\n------\r\n")
case 19:
f.MemoEditor.SetSelText(fmt.Sprintf("[%s]()", f.MemoEditor.SelText()))
case 20:
f.MemoEditor.SetSelText(fmt.Sprintf("[%s][]", f.MemoEditor.SelText()))
case 21:
f.MemoEditor.SetSelText(fmt.Sprintf("![%s]()", f.MemoEditor.SelText()))
case 22:
setSelText("\r\n```\r\n")
}
}
func (f *TMainForm) setToolBtnClickEvent() {
var i int32
for i = 0; i < f.ToolBar1.ControlCount(); i++ {
ctl := f.ToolBar1.Controls(i)
if ctl.Is().ToolButton() {
btn := vcl.AsToolButton(ctl)
if btn.Style() == types.TbsButton {
btn.SetOnClick(f.onToolBtnClick)
}
}
}
}
func (f *TMainForm) httpHandleFunc(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "text/html;charset=utf-8")
var buf *bytes.Buffer
vcl.ThreadSync(func() {
buf = makeHtmlPage(map[string]interface{}{"MdData": f.MemoEditor.Text()})
})
if buf != nil {
writer.Write(buf.Bytes())
}
}
func (f *TMainForm) OnFormDestroy(sender vcl.IObject) {
}
func (f *TMainForm) OnFormCloseQuery(sender vcl.IObject, canClose *bool) {
}
func (f *TMainForm) OnFormResize(sender vcl.IObject) {
if f.PnlWebview != nil && f.PnlEditor != nil {
wHalf := (f.Width() - f.Splitter1.Width()) / 2
f.PnlWebview.SetWidth(wHalf)
}
}
func (f *TMainForm) OnMemoEditorChange(sender vcl.IObject) {
if !f.isLoaded {
f.webview.Navigate("http://127.0.0.1" + webAddr)
} else {
f.webview.Refresh()
}
f.MemoEditor.SetFocus()
}