Last active
May 27, 2017 17:44
-
-
Save alucky0707/6199515 to your computer and use it in GitHub Desktop.
WALKのサンプル。テキストエリアの文字列を検索する。 ref. http://qiita.com/alucky0707/items/b066ccd2ff8517cf79fb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> | |
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/> | |
<dependency> | |
<dependentAssembly> | |
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/> | |
</dependentAssembly> | |
</dependency> | |
</assembly> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
//WALK関連のライブラリ | |
import ( | |
"github.com/lxn/walk" | |
. "github.com/lxn/walk/declarative" | |
) | |
//その他標準ライブラリ | |
import ( | |
"fmt" | |
"log" | |
"strings" | |
) | |
func main() { | |
mw := &MyMainWindow {} | |
if _, err := (MainWindow { | |
AssignTo: &mw.MainWindow, | |
Title: "SearchBox", | |
MinSize: Size {300, 400}, | |
Layout: VBox {}, | |
Children: []Widget { | |
GroupBox { | |
Layout: HBox {}, | |
Children: []Widget { | |
LineEdit { | |
AssignTo: &mw.searchBox, | |
}, | |
PushButton { | |
Text: "検索", | |
OnClicked: mw.clicked, | |
}, | |
}, | |
}, | |
TextEdit { | |
AssignTo: &mw.textArea, | |
}, | |
ListBox { | |
AssignTo: &mw.results, | |
Row: 5, | |
}, | |
}, | |
}.Run()); err != nil { | |
log.Fatal(err) | |
} | |
} | |
//structにまとめることで、グローバル変数を作らない | |
type MyMainWindow struct { | |
*walk.MainWindow | |
searchBox *walk.LineEdit | |
textArea *walk.TextEdit | |
results *walk.ListBox | |
} | |
func (mw *MyMainWindow) clicked() { | |
word := mw.searchBox.Text() | |
text := mw.textArea.Text() | |
model := []string {} | |
for _, i := range search(text, word) { | |
model = append(model, fmt.Sprintf("%d文字目に発見", i)) | |
} | |
log.Print(model) | |
mw.results.SetModel(model) | |
} | |
//textからwordを検索して、位置をUnicode単位で返す | |
func search(text ,word string) (result []int) { | |
result = []int {} | |
i := 0 | |
for j, _ := range text { | |
if strings.HasPrefix(text[j:], word) { | |
log.Print(i) | |
result = append(result, i) | |
} | |
i += 1 | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment