Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 220 lines (173 sloc) 5.839 kb
40bf19d @lxn Add WebView widget and start of a web browser example
authored
1 // Copyright 2010 The Walk Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
8711334 @lxn Move contents of high-level packages into walk package
authored
5 package walk
40bf19d @lxn Add WebView widget and start of a web browser example
authored
6
7 import (
8 "syscall"
9 "unsafe"
10 )
11
5e60627 @lxn Remove winapi sub-package and use github.com/lxn/go-winapi instead
authored
12 import . "github.com/lxn/go-winapi"
40bf19d @lxn Add WebView widget and start of a web browser example
authored
13
14 const webViewWindowClass = `\o/ Walk_WebView_Class \o/`
15
f87ab9d @lxn Refactor widget construction and window procedures plus some other ch…
authored
16 var webViewWindowClassRegistered bool
40bf19d @lxn Add WebView widget and start of a web browser example
authored
17
18 type WebView struct {
1d8db13 @lxn Rename Widget struct to WidgetBase
authored
19 WidgetBase
40bf19d @lxn Add WebView widget and start of a web browser example
authored
20 clientSite webViewIOleClientSite
21 browserObject *IOleObject
22 }
23
8f5a117 @lxn Run go fix; apply additional manual fixes
authored
24 func NewWebView(parent Container) (*WebView, error) {
f87ab9d @lxn Refactor widget construction and window procedures plus some other ch…
authored
25 ensureRegisteredWindowClass(webViewWindowClass, &webViewWindowClassRegistered)
40bf19d @lxn Add WebView widget and start of a web browser example
authored
26
27 wv := &WebView{
28 clientSite: webViewIOleClientSite{
29 IOleClientSite: IOleClientSite{
30 LpVtbl: webViewIOleClientSiteVtbl,
31 },
32 inPlaceSite: webViewIOleInPlaceSite{
33 IOleInPlaceSite: IOleInPlaceSite{
34 LpVtbl: webViewIOleInPlaceSiteVtbl,
35 },
36 inPlaceFrame: webViewIOleInPlaceFrame{
37 IOleInPlaceFrame: IOleInPlaceFrame{
38 LpVtbl: webViewIOleInPlaceFrameVtbl,
39 },
40 },
41 },
42 docHostUIHandler: webViewIDocHostUIHandler{
43 IDocHostUIHandler: IDocHostUIHandler{
44 LpVtbl: webViewIDocHostUIHandlerVtbl,
45 },
46 },
ab3fe8b @lxn Attempt to add event handling support for WebView (crashing, again ca…
authored
47 webBrowserEvents2: webViewDWebBrowserEvents2{
48 DWebBrowserEvents2: DWebBrowserEvents2{
49 LpVtbl: webViewDWebBrowserEvents2Vtbl,
50 },
51 },
40bf19d @lxn Add WebView widget and start of a web browser example
authored
52 },
53 }
54
f87ab9d @lxn Refactor widget construction and window procedures plus some other ch…
authored
55 if err := initChildWidget(
56 wv,
57 parent,
58 webViewWindowClass,
59 WS_VISIBLE,
60 0); err != nil {
61 return nil, err
40bf19d @lxn Add WebView widget and start of a web browser example
authored
62 }
63
64 wv.clientSite.inPlaceSite.inPlaceFrame.webView = wv
65
66 succeeded := false
67
68 defer func() {
69 if !succeeded {
70 wv.Dispose()
71 }
72 }()
73
74 var classFactoryPtr unsafe.Pointer
75 if hr := CoGetClassObject(&CLSID_WebBrowser, CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER, nil, &IID_IClassFactory, &classFactoryPtr); FAILED(hr) {
76 return nil, errorFromHRESULT("CoGetClassObject", hr)
77 }
78 classFactory := (*IClassFactory)(classFactoryPtr)
79 defer classFactory.Release()
80
81 var browserObjectPtr unsafe.Pointer
82 if hr := classFactory.CreateInstance(nil, &IID_IOleObject, &browserObjectPtr); FAILED(hr) {
83 return nil, errorFromHRESULT("IClassFactory.CreateInstance", hr)
84 }
85 browserObject := (*IOleObject)(browserObjectPtr)
86
87 wv.browserObject = browserObject
88
89 if hr := browserObject.SetClientSite((*IOleClientSite)(unsafe.Pointer(&wv.clientSite))); FAILED(hr) {
90 return nil, errorFromHRESULT("IOleObject.SetClientSite", hr)
91 }
92
93 if hr := browserObject.SetHostNames(syscall.StringToUTF16Ptr("Walk.WebView"), nil); FAILED(hr) {
94 return nil, errorFromHRESULT("IOleObject.SetHostNames", hr)
95 }
96
97 if hr := OleSetContainedObject((*IUnknown)(unsafe.Pointer(browserObject)), true); FAILED(hr) {
98 return nil, errorFromHRESULT("OleSetContainedObject", hr)
99 }
100
101 var rect RECT
f87ab9d @lxn Refactor widget construction and window procedures plus some other ch…
authored
102 GetClientRect(wv.hWnd, &rect)
40bf19d @lxn Add WebView widget and start of a web browser example
authored
103
f87ab9d @lxn Refactor widget construction and window procedures plus some other ch…
authored
104 if hr := browserObject.DoVerb(OLEIVERB_SHOW, nil, (*IOleClientSite)(unsafe.Pointer(&wv.clientSite)), -1, wv.hWnd, &rect); FAILED(hr) {
40bf19d @lxn Add WebView widget and start of a web browser example
authored
105 return nil, errorFromHRESULT("IOleObject.DoVerb", hr)
106 }
107
b08d86a @lxn Changes for release 2011-02-01, WebView broken again
authored
108 // FIXME: Reactivate after fixing crash
109 /* var cpcPtr unsafe.Pointer
110 if hr := browserObject.QueryInterface(&IID_IConnectionPointContainer, &cpcPtr); FAILED(hr) {
111 return nil, errorFromHRESULT("IOleObject.QueryInterface(IID_IConnectionPointContainer)", hr)
112 }
113 cpc := (*IConnectionPointContainer)(cpcPtr)
114 defer cpc.Release()
40bf19d @lxn Add WebView widget and start of a web browser example
authored
115
b08d86a @lxn Changes for release 2011-02-01, WebView broken again
authored
116 var cp *IConnectionPoint
117 if hr := cpc.FindConnectionPoint(&DIID_DWebBrowserEvents2, &cp); FAILED(hr) {
118 return nil, errorFromHRESULT("IConnectionPointContainer.FindConnectionPoint(DIID_DWebBrowserEvents2)", hr)
119 }
120 defer cp.Release()
ab3fe8b @lxn Attempt to add event handling support for WebView (crashing, again ca…
authored
121
b08d86a @lxn Changes for release 2011-02-01, WebView broken again
authored
122 var cookie uint
123 if hr := cp.Advise(unsafe.Pointer(&wv.clientSite.webBrowserEvents2), &cookie); FAILED(hr) {
124 return nil, errorFromHRESULT("IConnectionPoint.Advise", hr)
125 }*/
ab3fe8b @lxn Attempt to add event handling support for WebView (crashing, again ca…
authored
126
127 wv.onResize()
128
40bf19d @lxn Add WebView widget and start of a web browser example
authored
129 succeeded = true
130
131 return wv, nil
132 }
133
134 func (wv *WebView) Dispose() {
135 if wv.browserObject != nil {
136 wv.browserObject.Close(OLECLOSE_NOSAVE)
137 wv.browserObject.Release()
138
139 wv.browserObject = nil
140 }
141
1d8db13 @lxn Rename Widget struct to WidgetBase
authored
142 wv.WidgetBase.Dispose()
40bf19d @lxn Add WebView widget and start of a web browser example
authored
143 }
144
c47bf1f @lxn Revert LayoutFlags change
authored
145 func (*WebView) LayoutFlags() LayoutFlags {
f6ee1c3 @lxn Add greedy layout flags + several layout fixes
authored
146 return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
40bf19d @lxn Add WebView widget and start of a web browser example
authored
147 }
148
be8d275 @lxn More layout changes; Splitter now uses a simpler layout instead of Bo…
authored
149 func (*WebView) SizeHint() Size {
8711334 @lxn Move contents of high-level packages into walk package
authored
150 return Size{100, 100}
40bf19d @lxn Add WebView widget and start of a web browser example
authored
151 }
152
8f5a117 @lxn Run go fix; apply additional manual fixes
authored
153 func (wv *WebView) URL() (url string, err error) {
154 err = wv.withWebBrowser2(func(webBrowser2 *IWebBrowser2) error {
40bf19d @lxn Add WebView widget and start of a web browser example
authored
155 var urlBstr *uint16 /*BSTR*/
156 if hr := webBrowser2.Get_LocationURL(&urlBstr); FAILED(hr) {
157 return errorFromHRESULT("IWebBrowser2.Get_LocationURL", hr)
158 }
159 defer SysFreeString(urlBstr)
160
161 url = BSTRToString(urlBstr)
162
163 return nil
164 })
165
166 return
167 }
168
8f5a117 @lxn Run go fix; apply additional manual fixes
authored
169 func (wv *WebView) SetURL(url string) error {
170 return wv.withWebBrowser2(func(webBrowser2 *IWebBrowser2) error {
40bf19d @lxn Add WebView widget and start of a web browser example
authored
171 urlBstr := StringToVariantBSTR(url)
172 flags := IntToVariantI4(0)
173 targetFrameName := StringToVariantBSTR("_self")
174
175 if hr := webBrowser2.Navigate2(urlBstr, flags, targetFrameName, nil, nil); FAILED(hr) {
176 return errorFromHRESULT("IWebBrowser2.Navigate2", hr)
177 }
178
179 return nil
180 })
181 }
182
8f5a117 @lxn Run go fix; apply additional manual fixes
authored
183 func (wv *WebView) withWebBrowser2(f func(webBrowser2 *IWebBrowser2) error) error {
40bf19d @lxn Add WebView widget and start of a web browser example
authored
184 var webBrowser2Ptr unsafe.Pointer
185 if hr := wv.browserObject.QueryInterface(&IID_IWebBrowser2, &webBrowser2Ptr); FAILED(hr) {
186 return errorFromHRESULT("IOleObject.QueryInterface", hr)
187 }
188 webBrowser2 := (*IWebBrowser2)(webBrowser2Ptr)
189 defer webBrowser2.Release()
190
191 return f(webBrowser2)
192 }
193
194 func (wv *WebView) onResize() {
195 // FIXME: handle error?
8f5a117 @lxn Run go fix; apply additional manual fixes
authored
196 wv.withWebBrowser2(func(webBrowser2 *IWebBrowser2) error {
ae309d4 @lxn Remove superfluous error return values from many Widget getter methods.
authored
197 bounds := wv.ClientBounds()
40bf19d @lxn Add WebView widget and start of a web browser example
authored
198
199 webBrowser2.Put_Left(0)
200 webBrowser2.Put_Top(0)
b0dd455 @lxn Change [u]int to [u]int32 in winapi and adjust walk as required.
authored
201 webBrowser2.Put_Width(int32(bounds.Width))
202 webBrowser2.Put_Height(int32(bounds.Height))
40bf19d @lxn Add WebView widget and start of a web browser example
authored
203
204 return nil
205 })
206 }
207
b0dd455 @lxn Change [u]int to [u]int32 in winapi and adjust walk as required.
authored
208 func (wv *WebView) wndProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr {
b08d86a @lxn Changes for release 2011-02-01, WebView broken again
authored
209 switch msg {
40bf19d @lxn Add WebView widget and start of a web browser example
authored
210 case WM_SIZE, WM_SIZING:
f87ab9d @lxn Refactor widget construction and window procedures plus some other ch…
authored
211 if wv.clientSite.inPlaceSite.inPlaceFrame.webView == nil {
212 break
213 }
214
40bf19d @lxn Add WebView widget and start of a web browser example
authored
215 wv.onResize()
216 }
217
f87ab9d @lxn Refactor widget construction and window procedures plus some other ch…
authored
218 return wv.WidgetBase.wndProc(hwnd, msg, wParam, lParam)
40bf19d @lxn Add WebView widget and start of a web browser example
authored
219 }
Something went wrong with that request. Please try again.