/
htmlayout.go
168 lines (145 loc) · 4.15 KB
/
htmlayout.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
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package htmlayout
import (
"errors"
"github.com/Archs/go-htmlayout"
"github.com/lxn/walk"
"github.com/lxn/win"
// "syscall"
// "unsafe"
)
// const (
// htmlayoutClassName = "walkHtmLayoutCLassName"
// )
var (
hwnd2Widget = make(map[win.HWND]walk.Widget)
)
type HtmLayout struct {
walk.WidgetBase
pageUrlChangedPublisher walk.EventPublisher
pageContentChangedPublisher walk.EventPublisher
// htmlPath to load
pageUrl string
// html as string
pageContent string
}
func newHtmLayout(parent walk.Container) (*HtmLayout, error) {
de := new(HtmLayout)
if err := walk.InitWidget(
de,
parent,
// htmlayoutClassName,
gohl.GetClassName(),
win.WS_CHILDWINDOW|win.WS_OVERLAPPEDWINDOW|win.WS_CLIPSIBLINGS,
0); err != nil {
return nil, err
}
hwnd2Widget[de.Handle()] = de
// println("post", win.PostMessage(de.Handle(), win.WM_CREATE, 0, 0))
// go func() {
// win.ShowWindow(de.Handle(), win.SW_SHOW)
// win.UpdateWindow(de.Handle())
// var msg win.MSG
// for win.GetMessage(&msg, 0, 0, 0) > 0 {
// win.TranslateMessage(&msg)
// win.DispatchMessage(&msg)
// }
// }()
de.MustRegisterProperty("PageUrl", walk.NewProperty(
func() interface{} {
return de.pageUrl
},
func(v interface{}) error {
de.pageUrl = v.(string)
return nil
},
de.pageUrlChangedPublisher.Event()))
de.MustRegisterProperty("PageContent", walk.NewProperty(
func() interface{} {
return de.pageContent
},
func(v interface{}) error {
de.pageContent = v.(string)
return nil
},
de.pageContentChangedPublisher.Event()))
return de, nil
}
func NewHtmLayout(parent walk.Container, url string) (*HtmLayout, error) {
de, err := newHtmLayout(parent)
if err != nil {
return nil, err
}
de.pageUrl = url
gohl.EnableDebug()
return de, nil
}
func NewHtmLayoutWithContent(parent walk.Container, html string) (*HtmLayout, error) {
w, err := newHtmLayout(parent)
if err != nil {
return nil, err
}
w.pageContent = html
return w, nil
}
func (de *HtmLayout) MinSizeHint() walk.Size {
return walk.Size{400, 300}
}
func (de *HtmLayout) SizeHint() walk.Size {
return de.MinSizeHint()
}
func newError(msg string) error {
return errors.New(msg)
}
func (de *HtmLayout) PageUrlChanged() *walk.Event {
return de.pageUrlChangedPublisher.Event()
}
func (de *HtmLayout) PageContentChanged() *walk.Event {
return de.pageContentChangedPublisher.Event()
}
func (de *HtmLayout) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
// htmlayout handle the msg first
// ret, handled := gohl.ProcNoDefault(hwnd, msg, wParam, lParam)
// println("procNoDefault:", handled, msg)
// if handled {
// return uintptr(ret)
// }
// begin default message loop
switch msg {
case win.WM_CREATE: // this would not be called
println("WM_CREATE loading", "a.html", hwnd, de.Handle())
if err := gohl.LoadFile(hwnd, "a.html"); err != nil {
println("gohl.LoadFile failed:", err.Error())
}
}
return de.WindowBase.WndProc(hwnd, msg, wParam, lParam)
}
// func wndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
// w, ok := hwnd2Widget[hwnd]
// if !ok {
// return win.DefWindowProc(hwnd, msg, wParam, lParam)
// }
// return w.WndProc(hwnd, msg, wParam, lParam)
// }
// func init() {
// var wc win.WNDCLASSEX
// wc.CbSize = uint32(unsafe.Sizeof(wc))
// wc.Style = win.CS_HREDRAW | win.CS_VREDRAW
// wc.LpfnWndProc = syscall.NewCallback(wndProc)
// wc.CbClsExtra = 0
// wc.CbWndExtra = 0
// wc.HInstance = win.GetModuleHandle(nil)
// wc.HbrBackground = win.GetSysColorBrush(win.COLOR_WINDOWFRAME)
// wc.LpszMenuName = syscall.StringToUTF16Ptr("")
// wc.LpszClassName = syscall.StringToUTF16Ptr(htmlayoutClassName)
// wc.HIconSm = win.LoadIcon(0, win.MAKEINTRESOURCE(win.IDI_APPLICATION))
// wc.HIcon = win.LoadIcon(0, win.MAKEINTRESOURCE(win.IDI_APPLICATION))
// wc.HCursor = win.LoadCursor(0, win.MAKEINTRESOURCE(win.IDC_ARROW))
// atom := win.RegisterClassEx(&wc)
// if atom == 0 {
// panic("Registering Class Failed:")
// }
// // walk.MustRegisterWindowClass(htmlayoutClassName)
// }