Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logview.go #657

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 15 additions & 45 deletions examples/logview/logview.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,45 @@
// license that can be found in the LICENSE file.
package main

import (
"errors"
"syscall"
"unsafe"
)

import (
"github.com/lxn/walk"
"github.com/lxn/win"
)

type LogView struct {
walk.WidgetBase
walk.TextEdit
logChan chan string
maxSize int
}

const (
TEM_APPENDTEXT = win.WM_USER + 6
)

func NewLogView(parent walk.Container) (*LogView, error) {
lc := make(chan string, 1024)
lv := &LogView{logChan: lc}

if err := walk.InitWidget(
lv,
lv := &LogView{
logChan: make(chan string, 1024),
maxSize: 0x7FFF0000,
}
if err := walk.InitWidget(lv,
parent,
"EDIT",
win.WS_TABSTOP|win.WS_VISIBLE|win.WS_VSCROLL|win.ES_MULTILINE|win.ES_WANTRETURN,
win.WS_EX_CLIENTEDGE); err != nil {
return nil, err
}
lv.setReadOnly(true)
lv.SendMessage(win.EM_SETLIMITTEXT, 4294967295, 0)
lv.TextEdit.SetReadOnly(true)
lv.TextEdit.SetMaxLength(lv.maxSize)
return lv, nil
}

func (*LogView) CreateLayoutItem(ctx *walk.LayoutContext) walk.LayoutItem {
return walk.NewGreedyLayoutItem()
}

func (lv *LogView) setTextSelection(start, end int) {
lv.SendMessage(win.EM_SETSEL, uintptr(start), uintptr(end))
}

func (lv *LogView) textLength() int {
return int(lv.SendMessage(0x000E, uintptr(0), uintptr(0)))
}

func (lv *LogView) AppendText(value string) {
textLength := lv.textLength()
lv.setTextSelection(textLength, textLength)
lv.SendMessage(win.EM_REPLACESEL, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))))
}

func (lv *LogView) setReadOnly(readOnly bool) error {
if 0 == lv.SendMessage(win.EM_SETREADONLY, uintptr(win.BoolToBOOL(readOnly)), 0) {
return errors.New("fail to call EM_SETREADONLY")
textLength := lv.TextEdit.TextLength()
if textLength+len(value) < lv.maxSize {
lv.TextEdit.AppendText(value)
} else {
lv.TextEdit.SetText(value)
}

return nil
}

func (lv *LogView) PostAppendText(value string) {
Expand All @@ -78,20 +56,12 @@ func (lv *LogView) Write(p []byte) (int, error) {

func (lv *LogView) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_GETDLGCODE:
if wParam == win.VK_RETURN {
return win.DLGC_WANTALLKEYS
}

return win.DLGC_HASSETSEL | win.DLGC_WANTARROWS | win.DLGC_WANTCHARS
case TEM_APPENDTEXT:
select {
case value := <-lv.logChan:
lv.AppendText(value)
default:
return 0
}
}

return lv.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
return lv.TextEdit.WndProc(hwnd, msg, wParam, lParam)
}
17 changes: 15 additions & 2 deletions icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,25 +246,29 @@ func (i *Icon) handleForDPIWithError(dpi int) (win.HICON, error) {

var hInst win.HINSTANCE
var name *uint16
var flags uint32
if i.filePath != "" {
absFilePath, err := filepath.Abs(i.filePath)
if err != nil {
return 0, err
}

flags |= win.LR_LOADFROMFILE
name = syscall.StringToUTF16Ptr(absFilePath)
} else {
if !i.isStock {
if hInst = win.GetModuleHandle(nil); hInst == 0 {
return 0, lastError("GetModuleHandle")
}
} else {
flags |= win.LR_SHARED
}

name = i.res
}

var size Size
if i.size96dpi.Width == 0 || i.size96dpi.Height == 0 {
flags |= win.LR_DEFAULTSIZE
size = SizeFrom96DPI(defaultIconSize(), dpi)
} else {
size = SizeFrom96DPI(i.size96dpi, dpi)
Expand All @@ -291,7 +295,16 @@ func (i *Icon) handleForDPIWithError(dpi int) (win.HICON, error) {
int32(size.Height),
&hIcon))
if hr < 0 || hIcon == 0 {
return 0, lastError("LoadIconWithScaleDown")
hIcon = win.HICON(win.LoadImage(
hInst,
name,
win.IMAGE_ICON,
int32(size.Width),
int32(size.Height),
flags))
if hIcon == 0 {
return 0, lastError("LoadIconWithScaleDown & LoadImage")
}
}
}

Expand Down