この章では、簡単なテキストエディタを作ります。
package main
import (
"github.com/dontpanic92/wxGo/wx"
"io/ioutil"
"os"
)
var edited bool = false
var path string = ""
var name string = "untitled"
func main() {
app := wx.NewApp()
frame := wx.NewFrame(wx.NullWindow, -1, "Text Editor", wx.DefaultPosition, wx.DefaultSize)
panel := wx.NewPanel(frame, -1)
panel . SetBackgroundColour(wx.NewColour("white"))
textCtrl:= wx.NewTextCtrl(panel, -1, "", wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE)
vBox := wx.NewBoxSizer(wx.VERTICAL)
vBox . Add(textCtrl, 1, wx.ALL|wx.EXPAND, 10)
panel . SetSizer(vBox)
menuBar := wx.NewMenuBar()
fileMenu:= wx.NewMenu()
fileMenu. Append(wx.ID_NEW)
fileMenu. Append(wx.ID_OPEN)
fileMenu. Append(wx.ID_SAVE)
fileMenu. AppendSeparator()
fileMenu. Append(wx.ID_SAVEAS)
fileMenu. Append(wx.ID_DELETE)
fileMenu. AppendSeparator()
fileMenu. Append(wx.ID_EXIT)
menuBar . Append(fileMenu, "&File")
frame . SetMenuBar(menuBar)
/**** TEXT EDITED *****/
wx.Bind(frame, wx.EVT_TEXT, func(e wx.Event) {
edited = true
frame.SetTitle(name + "*")
}, textCtrl.GetId())
/**** NEW COMMAND *****/
wx.Bind(frame, wx.EVT_MENU, func(e wx.Event) {
saveSelect(textCtrl)
textCtrl.SetValue("")
name = "untitled"
frame.SetTitle(name)
edited = false
}, wx.ID_NEW)
/**** OPEN COMMAND ****/
wx.Bind(frame, wx.EVT_MENU, func(e wx.Event) {
if edited && !(name == "untitled" || name == "untitled*") {
saveSelect(textCtrl)
} else if edited {
saveQuestion(textCtrl)
}
fd := wx.NewFileDialog(wx.NullWindow, "", "", "", "*" , wx.FD_OPEN, wx.DefaultPosition, wx.DefaultSize)
if fd.ShowModal() == wx.ID_OK {
name = fd.GetFilename()
path = fd.GetPath()
data, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
textCtrl.SetValue(string(data))
frame.SetTitle(name)
edited = false
}
}, wx.ID_OPEN)
/**** SAVE COMMAND ****/
wx.Bind(frame, wx.EVT_MENU, func(e wx.Event) {
if edited && !(name == "untitled" || name == "untitled*") {
ioutil.WriteFile(path, []byte(textCtrl.GetValue()), os.ModePerm)
} else if edited {
saveDialog(textCtrl)
}
edited = false
frame.SetTitle(name)
}, wx.ID_SAVE)
/*** SAVEAS COMMAND ***/
wx.Bind(frame, wx.EVT_MENU, func(e wx.Event) {
saveDialog(textCtrl)
edited = false
frame.SetTitle(name)
}, wx.ID_SAVEAS)
/*** DELETE COMMAND ***/
wx.Bind(frame, wx.EVT_MENU, func(e wx.Event) {
textCtrl.SetValue("")
}, wx.ID_DELETE)
/**** QUIT COMMAND ****/
wx.Bind(frame, wx.EVT_MENU, func(e wx.Event) {
frame.Close()
}, wx.ID_EXIT)
/**** CLOSE WINDOW ****/
wx.Bind(frame, wx.EVT_CLOSE_WINDOW, func(e wx.Event) {
saveSelect(textCtrl)
frame.Destroy()
}, frame.GetId())
frame .Show()
app .MainLoop()
}
func saveSelect(textCtrl wx.TextCtrl) {
if edited && !(name == "untitled" || name == "untitled*") {
dial := wx.NewMessageDialog(wx.NullWindow, "Are you save this document?", "Question",
wx.YES_NO|wx.NO_DEFAULT|wx.ICON_QUESTION)
if dial.ShowModal() == wx.ID_YES {
ioutil.WriteFile(path, []byte(textCtrl.GetValue()), os.ModePerm)
}
} else if edited {
saveQuestion(textCtrl)
}
}
func saveQuestion(textCtrl wx.TextCtrl) {
dial := wx.NewMessageDialog(wx.NullWindow, "Are you save this document?", "Question",
wx.YES_NO|wx.NO_DEFAULT|wx.ICON_QUESTION)
if dial.ShowModal() == wx.ID_YES {
saveDialog(textCtrl)
}
}
func saveDialog(textCtrl wx.TextCtrl) {
fd := wx.NewFileDialog(wx.NullWindow, "", "", "HelloGo.txt", "*", wx.FD_SAVE, wx.DefaultPosition, wx.DefaultSize)
if fd.ShowModal() == wx.ID_OK {
name = fd.GetFilename()
path = fd.GetPath()
ioutil.WriteFile(path, []byte(textCtrl.GetValue()), os.ModePerm)
}
}
ターミナルで、textditor.go を保存したディレクトリに移動して、次のコードを実行してください。
go run texteditor.go
実行結果が表示されるのに、少し時間がかかります。
アプリケーションを終了するには、Quit メニューを選ぶか、ウィンドウを閉じます。