-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathguiform.pas
executable file
·245 lines (218 loc) · 6.49 KB
/
guiform.pas
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
unit guiform;
{$mode objfpc}{$H+}
{$ifdef darwin}
{$modeswitch objectivec1}
{$endif}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
LCLIntf, LCLType, fphttpapp,
{$ifdef darwin}
{$ifdef lclcocoa}CocoaAll, CocoaWindows,{$endif}
{$endif}
{$ifdef lclwin32}
activex, windows,
{$endif}
webview;
{$ifdef lclwin32}
const
WM_APP_ACTIVATE = WM_APP + 1;
{$endif}
type
{ TForm1 }
TForm1 = class(TForm)
TopPanel: TPanel;
WebPanel: TPanel;
BtnWebViewSayHello: TButton;
BtnLazSayHello: TButton;
BtnExitProgram: TButton;
Timer1: TTimer;
procedure OnFormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure OnFormCreate(Sender: TObject);
procedure OnFormPaint(Sender: TObject);
procedure OnFormResize(Sender: TObject);
procedure OnFormShow(Sender: TObject);
procedure OnFormShowTimer(Sender: TObject);
procedure OnClickExitProgram(Sender: TObject);
procedure OnClickLazSayHello(Sender: TObject);
procedure OnClickWebViewSayHello(Sender: TObject);
private
wvHandle: PWebView;
isExiting: Boolean;
{$ifdef lclcocoa}
mainWC: TCocoaWindowContentDocument;
wvWin: NSWindow;
wvWinMask: Integer;
{$endif}
procedure CreateWebView;
procedure UpdateWebViewWindow;
public
property WebPanelControl: TPanel read WebPanel;
property WebviewHandle: PWebView read wvHandle;
end;
var
Form1: TForm1;
{$ifdef lclwin32}
ProxiedWndProc: WNDPROC;
{$endif}
implementation
{$R *.lfm}
{$ifdef lclwin32}
function WebViewWndProc(aHwnd: HWND; uMsg: UInt; WParam: wParam; lParam: LParam): LRESULT; stdcall;
var
r: RECTL;
aWindow: HWND;
begin
case uMsg of
WM_APP_ACTIVATE, WM_SIZE:
if Form1.WebViewHandle <> nil then
begin
Windows.GetClientRect(Form1.WebPanelControl.Handle, @r);
aWindow := HWND(PtrUInt(webview_get_native_handle(Form1.WebViewHandle, UI_Widget)));
MoveWindow(aWindow, r.top, r.left, r.right-r.left, r.bottom-r.top, true);
end
end;
result := CallWindowProc(ProxiedWndProc, aHwnd, uMsg, wParam, lParam);
end;
{$endif}
procedure SayHello(const seq: PAnsiChar; const req: PAnsiChar; arg: Pointer); cdecl;
const
s =
'var dc = document.getElementById("demo-content");' +
'var p = document.createElement("p");' +
'p.innerHTML = "\"Yo again!\" asynchronously injected by Javascript";' +
'dc.appendChild(p);';
begin
if Form1.webviewHandle <> nil then
begin
webview_eval(Form1.webviewHandle, PAnsiChar(s));
webview_return(Form1.webviewHandle, seq, WebView_Return_Ok, '{"result": "<p>\"Yo!\" returned by Pascal</p>"}');
end;
end;
procedure TForm1.OnFormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
if CloseAction = caFree then
OnClickExitProgram(Sender);
end;
procedure TForm1.OnFormCreate(Sender: TObject);
{$ifdef lclwin32}
var
comInitStatus: HRESULT;
{$endif}
begin
{$ifdef lclwin32}
ProxiedWndProc := Windows.WNDPROC(SetWindowLongPtr(Self.Handle, GWL_WNDPROC, PtrUInt(@WebViewWndProc)));
CoUninitialize;
comInitStatus := CoInitializeEx(nil, COINIT_APARTMENTTHREADED);
if (comInitStatus <> S_OK) and (comInitStatus <> S_FALSE) then
ShowMessage('webview COM init failed, program will not work correctly');
{$endif}
end;
procedure TForm1.OnFormPaint(Sender: TObject);
begin
{$ifdef lclcocoa}UpdateWebViewWindow;{$endif}
{$ifdef lclwin32}Forms.Application.ProcessMessages;{$endif}
end;
procedure TForm1.OnFormResize(Sender: TObject);
begin
{$ifdef lclcocoa}
if wvHandle <> nil then
webview_set_size(wvHandle, WebPanel.ClientWidth, WebPanel.ClientHeight, WebView_Hint_Fixed);
UpdateWebViewWindow;
{$endif}
end;
procedure TForm1.OnFormShow(Sender: TObject);
begin
Timer1.Interval := 1000;
Timer1.Enabled := true;
end;
procedure TForm1.OnFormShowTimer(Sender: TObject);
begin
Timer1.Enabled := false;
if not isExiting then
CreateWebView;
end;
procedure TForm1.OnClickExitProgram(Sender: TObject);
begin
isExiting := true;
Sleep(500);
fphttpapp.Application.Terminate;
if wvHandle <> nil then
webview_destroy(wvHandle);
WebPanel.Hide;
{$ifdef lclwin32}
CoUninitialize;
{$endif}
Halt;
end;
procedure TForm1.OnClickLazSayHello(Sender: TObject);
begin
Forms.Application.MessageBox('This is Lazarus GUI component in action', 'fpwebview LCL Embedded Demo');
end;
procedure TForm1.OnClickWebViewSayHello(Sender: TObject);
const
s =
'var dc = document.getElementById("demo-content");' +
'var p = document.createElement("p");' +
'p.innerHTML = "Lazarus says \"Yo!\"";' +
'dc.appendChild(p);';
begin
webview_eval(webviewHandle, PAnsiChar(s));
end;
procedure TForm1.CreateWebView;
var
phwnd: HWND;
{$ifdef lclcocoa}
aWC: TCocoaWindowContentDocument;
aRect: NSRect;
aView: NSView;
{$endif}
begin
if wvHandle <> nil then
exit;
WebPanel.HandleNeeded;
phwnd := WebPanel.Handle;
{$ifdef lclwin32}
wvHandle := webview_create(WebView_DevTools, @phwnd);
{$endif}
{$ifdef lclcocoa}
mainWC := TCocoaWindowContentDocument(self.Handle);
aRect.origin := mainWC.window.frame.origin;
aRect.size.width := WebPanel.ClientWidth;
aRect.size.height := WebPanel.ClientHeight;
aView := NSView.alloc.initWithFrame(aRect);
aWC := TCocoaWindowContentDocument(phwnd);
aWC.addSubView(aView);
wvWinMask := NSBorderlessWindowMask;
wvWin := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(
aRect,
wvWinMask,
NSBackingStoreBuffered,
false);
//wvWin.setContentView(aView); // Done by libwebview.
mainWC.window.addChildWindow_ordered(wvWin, NSWindowAbove);
wvHandle := webview_create(WebView_DevTools, Pointer(wvWin));
{$endif}
if wvHandle <> nil then
begin
webview_bind(wvHandle, PAnsiChar('HostSayHello'), @SayHello, nil);
webview_navigate(wvHandle, PAnsiChar('http://localhost:8000/'));
{$ifdef lclwin32}
PostMessage(Form1.Handle, WM_APP_ACTIVATE, 0, 0);
{$endif}
webview_run(wvHandle);
end
else
ShowMessage('webview creation failed, program will not work correctly.');
end;
procedure TForm1.UpdateWebViewWindow;
begin
{$ifdef lclcocoa}
if wvHandle <> nil then
begin
wvWin.setStyleMask(wvWinMask);
wvWin.setFrameOrigin(mainWC.window.frame.origin);
end;
{$endif}
end;
end.