This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathbrowser.lcb
More file actions
884 lines (669 loc) · 29.7 KB
/
Copy pathbrowser.lcb
File metadata and controls
884 lines (669 loc) · 29.7 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*
Copyright (C) 2015-2016 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/**
This widget displays web content within a native web browser view.
The browser widget can display HTML content generated in LiveCode, or
fetch content over the Internet. It supports JavaScript and allows
for interaction between LiveCode scripts and JavaScript code.
The [browser SVG icon](https://www.iconfinder.com/icons/309064/browser_globe_international_internet_web_world_icon)
is copyright © Ivan Boyko, and is licensed under the terms of the
[Creative Commons Attribution 3.0 Unported License](https://creativecommons.org/licenses/by/3.0/).
Name: browserDocumentLoadBegin
Type: message
Syntax: browserDocumentLoadBegin <pUrl>
Summary: Sent when a document begins loading in the browser.
Parameters:
pUrl (string): The URL of the loading document.
Description:
The <browserDocumentLoadBegin> message is sent to the widget's script
object when a new document begins to load in the browser. This will
happen whenever the browser navigates to a new page. The <pUrl>
parameter contains the URL of the loading document.
Name: browserDocumentLoadComplete
Type: message
Syntax: browserDocumentLoadComplete <pUrl>
Summary: Sent when a document has completed loading in the browser.
Parameters:
pUrl (string): The URL of the loaded document.
Description:
The <browserDocumentLoadComplete> message is sent to the widget's script
object when a new document has completed loading in the browser. The
<pUrl> parameter contains the URL of the loaded document.
Name: browserDocumentLoadFailed
Type: message
Syntax: browserDocumentLoadFailed <pUrl>, <pError>
Summary: Sent when a document has failed to load in the browser.
Parameters:
pUrl (string): The URL of the document.
pError (string): An error message describing the reason for the failure.
Description:
The <browserDocumentLoadFailed> message is sent to the widget's script
object when a new document has failed to load in the browser. The <pUrl>
parameter contains the URL of the document, and the <pError> parameter
gives the reason for the failure.
Name: browserFrameDocumentLoadBegin
Type: message
Syntax: browserFrameDocumentLoadBegin <pUrl>
Summary: Sent when a document begins loading in a frame of the browser.
Parameters:
pUrl (string): The URL of the loading document.
Description:
The <browserFrameDocumentLoadBegin> message is sent to the widget's
script object when a new document begins to load in a frame of the
browser. This will happen whenever the browser navigates to a new page
with multiple frames. The <pUrl> parameter contains the URL of the
loading document.
Name: browserFrameDocumentLoadComplete
Type: message
Syntax: browserFrameDocumentLoadComplete <pUrl>
Summary: Sent when a document has completed loading in a frame of the browser.
Parameters:
pUrl (string): The URL of the loaded document.
Description:
The <browserFrameDocumentLoadComplete> message is sent to the widget's
script object when a new document has completed loading in a frame of
the browser. The <pUrl> parameter contains the URL of the loaded
document.
Name: browserFrameDocumentLoadFailed
Type: message
Syntax: browserFrameDocumentLoadFailed <pUrl>, <pError>
Summary: Sent when a document has failed to load in a frame of the
browser.
Parameters:
pUrl (string): The URL of the document.
pError (string): An error message describing the reason for the failure.
Description:
The <browserFrameDocumentLoadFailed> message is sent to the widget's
script object when a new document has failed to load in a frame of the
browser. The <pUrl> parameter contains the URL of the document, and the
<pError> parameter gives the reason for the failure.
Name: browserNavigateBegin
Type: message
Syntax: browserNavigateBegin <pUrl>
Summary: Sent when the browser begins navigation to a new page.
Parameters:
pUrl (string): The URL of the new page.
Description:
The <browserNavigateBegin> message is sent to the widget's script object
when the browser begins navigation to a new page. This can be triggered
by launching a URL in the browser, or clicking a link within the
browser. The <pUrl> parameter contains the URL of the new page.
Name: browserNavigateComplete
Type: message
Syntax: browserNavigateComplete <pUrl>
Summary: Sent when the browser successfully navigates to a new page.
Parameters:
pUrl (string): The URL of the new page.
Description:
The <browserNavigateComplete> message is sent to the widget's script
object when the browser successfully navigates to a new page. The <pUrl>
parameter contains the URL of the new page.
Name: browserNavigateFailed
Type: message
Syntax: browserNavigateFailed <pUrl>, <pError>
Summary: Sent when the browser has failed to navigate to a new page.
Parameters:
pUrl (string): The URL of the document.
pError (string): An error message describing the reason for the failure.
Description:
The <browserNavigateFailed> message is sent to the widget's script
object when the browser has failed to navigate to a new page. The <pUrl>
parameter contains the URL of the new page, and the <pError> parameter
gives the reason for the failure.
Name: browserUnhandledLoadRequest
Type: message
Syntax: browserUnhandledLoadRequest <pUrl>
Summary: Sent when the browser is unable to handle a load request.
Parameters:
pUrl (string): The URL of the request.
Description:
The <browserUnhandledLoadRequest> message is sent to the widget's script
object when the browser is unable to load a URL, typically due to an
unrecognised URL scheme. The <pUrl> parameter contains the URL of the
unhandled request.
Name: browserProgressChanged
Type: message
Syntax: browserProgressChanged <pUrl>, <pProgress>
Summary: Sent when the load progress of the current document changes.
Parameters:
pUrl (string): The URL of the loading document.
pProgress (number): The percentage of the document loaded.
Description:
The <browserProgressChanged> message is sent to the widget's script
object when a the loading progress of the current document changes. The <pUrl>
parameter contains the URL of the loading document. The <pProgress> parameter
contains the percentage (between 0 and 100) of the document loaded.
*/
-- declaring extension as widget, followed by identifier
widget com.livecode.widget.browser
--
-- dependancy declarations
use com.livecode.foreign
use com.livecode.widget
use com.livecode.canvas
use com.livecode.engine
use com.livecode.extensions.libbrowser
use com.livecode.library.widgetutils
--
-- metadata
metadata title is "Browser"
metadata author is "LiveCode"
metadata version is "1.0.0"
metadata svgicon is "M21.7,12c0,5.4-4.4,9.7-9.7,9.7S2.3,17.4,2.3,12S6.6,2.3,12,2.3S21.7,6.6,21.7,12z M11.9,16.6c0-0.2-0.1-0.3-0.3-0.4 C11,16,10.4,16,9.9,15.5c-0.1-0.2-0.1-0.4-0.2-0.6c-0.2-0.2-0.7-0.3-1-0.4c-0.4,0-0.8,0-1.3,0c-0.2,0-0.5,0-0.7,0 c-0.3-0.1-0.5-0.5-0.7-0.8C6,13.6,6,13.4,5.8,13.4c-0.2-0.1-0.4,0.1-0.6,0c-0.1-0.1-0.1-0.2-0.1-0.3c0-0.3,0.2-0.6,0.4-0.8 c0.3-0.2,0.6,0.1,0.9,0.1c0.1,0,0.1,0,0.2,0.1C6.9,12.6,7,13,7,13.3c0,0.1,0,0.2,0,0.2c0,0.1,0.1,0.1,0.2,0.1c0.1-0.5,0.1-1,0.2-1.5 c0-0.6,0.6-1.2,1.1-1.4c0.2-0.1,0.3,0.1,0.5,0c0.6-0.2,2.1-0.8,1.8-1.6C10.6,8.4,10,7.7,9.2,7.8C9,7.9,8.9,8,8.7,8.1 C8.4,8.3,7.8,8.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5-0.7,0.7c-0.2,0.3-0.4,0.6-0.6,0.9c-0.1,0.1-0.2,0.3-0.1,0.4c0,0.1,0.1,0.1,0.2,0.1c0.2,0.1,0.3,0.1,0.5,0.2 c0.1,0,0.2,0.1,0.1,0.2c0,0,0,0.1-0.1,0.1c-0.5,0.5-1,0.9-1.5,1.4c-0.1,0.1-0.2,0.3-0.2,0.4c0,0.1,0.1,0.1,0.1,0.2 c0,0.1-0.1,0.1-0.2,0.2c-0.2,0.1-0.4,0.2-0.5,0.3c-0.1,0.2,0,0.5-0.1,0.7c-0.1,0.5-0.4,0.9-0.6,1.4c-0.2,0.3-0.3,0.6-0.5,0.9 c0,0.4-0.1,0.7,0.1,1c0.5,0.7,1.4,0.3,2.1,0.6c0.2,0.1,0.4,0.1,0.5,0.3c0.3,0.3,0.3,0.8,0.4,1.1c0.1,0.4,0.2,0.8,0.4,1.2 c0.1,0.5,0.3,1,0.4,1.4c0.9-0.7,1.7-1.5,2.3-2.5C19.9,16,20.2,15.2,20.5,14.4z"
metadata _ide is "true"
--
-- property declarations
/**
Name: URL
Type: property
Syntax:
set the URL of <widget> to <pUrl>
get the URL of <widget>
Summary: The URL displayed by the browser.
Parameters:
pUrl (string): A string specifying a URL.
Example:
-- Navigate to livecode.com by setting the url property,
-- keeping a copy of the previous URL
local tOldUrl
put the URL of widget "myBrowser" into tOldUrl
set the URL of widget "myBrowser" to "https://livecode.com"
Description:
The <URL> is the URL of the content be displayed in the browser.
>*Note:* The <URL> must be a standards-compliant URL. In particular,
> `file:` URLs must be of the form `file://<absolute path>`, and the
> path should be URL-encoded.
> See [RFC 1738, section 2.2](https://tools.ietf.org/html/rfc1738#section-2.2).
>*Note:* The <URL> reflects the currently displaying page in the
> browser, and will change when navigating to another page. Setting the
> htmlText will result in the <URL> being empty.
>*Note:* Setting the <URL> won't necessarily cause resources to be
> reloaded unless URLs of the resources explicitly change. However,
> you can force the browser widget to reload a page using
> `do "location.reload()" in widget "browser"` after setting the url.
>*Note:* Setting the <URL> to empty will clear the currently loaded
> page, resulting in a blank page being displayed and the <URL> and
> <htmlText> of the widget being empty.
References: htmlText (property)
*/
property URL get getUrl set setUrl
/**
Name: htmlText
Type: property
Syntax:
set the htmlText of <widget> to <pHtmlText>
get the htmlText of <widget>
Summary: The HTML text of the content displayed by the browser.
Parameters:
pHtmlText (string): A string containing HTML data to be displayed by the
browser.
Example:
-- Render a web page in the browser by specifying custom HTML
-- content
local tHTML
put "<html><head><title>My Page Title</title></head>" & \
"<body>My Page Contents</body></html>" into tHTML
set the htmlText of widget "myBrowser" to tHTML
Description:
The <htmlText> is the HTML representation of the content displayed in
the browser.
*Note:* When the <htmlText> has been set, the <URL> property will be
empty. When the <URL> property is not empty, the <htmlText> will
contain the source of the current URL displayed in the browser.
References: URL (property)
*/
property htmlText get getHtmlText set setHtmlText
metadata htmlText.editor is "com.livecode.pi.nowraptext"
metadata htmlText.user_visible is "false" -- bug 16927
metadata htmlText.label is "HTML text"
/**
Name: vScrollbar
Type: property
Syntax:
set the vScrollbar of <widget> to <pEnabled>
get the vScrollbar of <widget>
Summary: Controls the visibility of the browser's vertical scrollbar.
Value (boolean):
`true` if the vertical scrollbar should be enabled and displayed;
`false` otherwise.
Description:
Use the <vScrollbar> property to control the visibility of the browser's
vertical scrollbar
References: hScrollbar (property)
*/
property vScrollbar get getVScrollbar set setVScrollbar
metadata vScrollbar.editor is "com.livecode.pi.boolean"
metadata vScrollbar.default is "false"
metadata vScrollbar.label is "Vertical scrollbar"
/**
Name: hScrollbar
Type: property
Syntax:
set the hScrollbar of <widget> to <pEnabled>
get the hScrollbar of <widget>
Summary: Controls the visibility of the browser's horizontal scrollbar.
Value (boolean):
`true` if the horizontal scrollbar should be enabled and displayed;
`false` otherwise.
Description:
Use the <hScrollbar> property to control the visibility of the browser's
horizontal scrollbar
References: vScrollbar (property)
*/
property hScrollbar get getHScrollbar set setHScrollbar
metadata hScrollbar.editor is "com.livecode.pi.boolean"
metadata hScrollbar.default is "false"
metadata hScrollbar.label is "Horizontal scrollbar"
/**
Name: allowUserInteraction
Type: property
Syntax:
set the allowUserInteraction of <widget> to <allowInteraction>
get the allowUserInteraction of <widget>
Summary: Controls whether the browser responds to user interaction.
Value (boolean):
`true` if the browser should respond to user interaction;
`false` otherwise.
Description:
Use the <allowUserInteraction> property to control if the browser should respond
to user interaction.
*/
property allowUserInteraction get getAllowUserInteraction set setAllowUserInteraction
metadata allowUserInteraction is "com.livecode.pi.boolean"
metadata allowUserInteraction.default is "true"
metadata allowUserInteraction.label is "Allow user interaction"
/**
Name: isSecure
Type: property
Syntax:
get the isSecure of <widget>
Summary: Indicates if all resources in the current document have been loaded
through securely encrypted connections.
Value (boolean):
`true` if all resources in the current document have been loaded through
securely encrypted connections;
`false` otherwise.
Description:
Use the <isSecure> property to determine if all resources in the current
document have been loaded through securely encrypted connections. If the current
document is still loading, this will return false
*/
property isSecure get getIsSecure
metadata isSecure.user_visible is "false"
/**
Name: userAgent
Type: property
OS: mac,windows,linux,android,ios
Syntax:
set the userAgent of <widget> to <pUserAgent>
get the userAgent of <widget>
Summary: The identifier sent by the browser when fetching content from remote URLs.
Value (string): A suitable HTTP user agent string.
Example:
-- Set a custom User-Agent header. The remote web server may
-- be configured to deliver custom content for browsers using
-- this User-Agent.
set the userAgent of widget "myBrowser" to "myAppEmbeddedBrowser"
launch url "https://myexampleserver.com/content.html" in widget "myBrowser"
Description:
The <userAgent> is the identifier sent by the browser when fetching
content from remote HTTP servers.
The <userAgent> must conform to the requirements for the `User-Agent`
header described in the HTTP specification. See [RFC 2616, section
14.43](https://tools.ietf.org/html/rfc2616#section-14.43).
*/
property userAgent get getUserAgent set setUserAgent
metadata userAgent.editor is "com.livecode.pi.text"
metadata userAgent.label is "User agent"
/**
Name: javascriptHandlers
Type: property
Syntax:
set the javascriptHandlers of <widget> to <pHanderList>
get the javascriptHandlers of <widget>
Summary: A list of LiveCode handlers that are made available to JavaScript calls within the browser.
Value (string): A return-separated list of handler names.
Example:
-- Define a handler to respond to javascript calls.
on myJSHandler pMessage, pValue
-- Do appropriate actions here.
-- ...
end myJSHandler
-- Set up the browser javascript handler list
-- This code goes in a suitable setup handler
set the javascriptHandlers to "myJSHandler" & return & "myOtherJSHandler"
// Calling the handler from JavaScript within the browser
liveCode.myJSHandler("myMessage", 12345);
Description:
The <javascriptHandlers> is a list of LiveCode handlers that are made
available to JavaScript calls within the browser. The handlers will
appear as methods attached to a global `liveCode` object. You can call
these methods as you would any other JavaScript function and pass
whatever parameters you require.
>*Warning:* Setting the <javascriptHandlers> property gives JavaScript
running within the Web browser permission to execute parts of your
application through the handlers you choose to expose. If using this
feature, make sure that you have complete control over the webpages
which you load into the browser widget, and consider using HTTPS to
ensure that third-parties cannot inject malicious code into them.
*/
property javascriptHandlers get getJavaScriptHandlers set setJavaScriptHandlers
metadata javascriptHandlers.editor is "com.livecode.pi.text"
metadata javascriptHandlers.label is "JavaScript handlers"
--------------------------------------------------------------------------------
foreign handler MCHandlerGetFunctionPtr(in pHandler as any, out rFuncPtr as Pointer) returns CBool binds to "<builtin>"
--------------------------------------------------------------------------------
variable mOpened as Boolean
variable mBrowser as optional MCBrowserRef
variable mProperties as Array
--
variable mScriptObject as ScriptObject
--------------------------------------------------------------------------------
constant kPersistentProps is ["verticalScrollbarEnabled", "horizontalScrollbarEnabled", "allowNewWindows", "enableContextMenu", "userAgent", "javaScriptHandlers", "allowUserInteraction", "isSecure"]
--------------------------------------------------------------------------------
public handler OnLoad(in pProperties as Array)
put pProperties into mProperties
end handler
public handler OnSave(out rProperties as Array)
put mProperties into rProperties
end handler
----------
-- called when the widget is created
public handler OnCreate()
libbrowserInit()
put the empty array into mProperties
put false into mOpened
end handler
--
constant kOpenStateClosed is 0
constant kOpenStateOpen is 1
constant kOpenStateFailed is -1
variable mOpenState as Number
private unsafe handler InitBrowserView()
variable tParent as Pointer
// put my native window into tParent
MCWidgetGetMyStackNativeView(tParent)
variable tFactory as MCBrowserFactoryRef
if not MCBrowserFactoryGet(the empty string, tFactory) then
put kOpenStateFailed into mOpenState
throw "error getting browser factory"
end if
variable tDisplay as optional Pointer
MCWidgetGetMyStackNativeDisplay(tDisplay)
if not MCBrowserFactoryCreateBrowser(tFactory, tDisplay, tParent, mBrowser) then
put kOpenStateFailed into mOpenState
throw "error creating browser"
end if
put kOpenStateOpen into mOpenState
variable tBrowserView as Pointer
put MCBrowserGetNativeLayer(mBrowser) into tBrowserView
set my native layer to tBrowserView
// tell native layer that we can't render a snapshot of the browser view.
MCWidgetSetMyNativeLayerCanRenderToContext(false)
MCBrowserSetNavigationHandler(mBrowser, OnNavigationCallback, nothing)
MCBrowserSetJavaScriptHandler(mBrowser, OnJavaScriptCallback, nothing)
MCBrowserSetProgressHandler(mBrowser, OnProgressCallback, nothing)
applyProperties(mProperties)
end handler
private unsafe handler FinalizeBrowserView()
if mOpenState is not kOpenStateClosed then
set my native layer to nothing
if mBrowser is not nothing then
MCBrowserSetNavigationHandler(mBrowser, nothing, nothing)
MCBrowserSetJavaScriptHandler(mBrowser, nothing, nothing)
MCBrowserSetProgressHandler(mBrowser, nothing, nothing)
MCBrowserRelease(mBrowser)
put nothing into mBrowser
end if
put kOpenStateClosed into mOpenState
else
schedule timer in 0.1 seconds
end if
end handler
public handler OnTimer()
unsafe
FinalizeBrowserView()
end unsafe
end handler
--
public handler OnOpen()
// Keep a reference to this widget's script object so we can post to it from
// the callbacks (which don't have a target widget when called).
put my script object into mScriptObject
put true into mOpened
unsafe
InitBrowserView()
end unsafe
end handler
public handler OnClose()
unsafe
FinalizeBrowserView()
end unsafe
put false into mOpened
end handler
----------
private handler Capitalize(in pString as String) returns String
if pString is empty then
return the empty string
end if
put (the upper of char 1 of pString) into char 1 of pString
return pString
end handler
/* WORKAROUND - Need to retain foreign types in handler as conversion to those in the foreign handler type signature not yet implemented */
//private handler OnNavigationCallback(in pContext as optional Pointer, in pBrowser as MCBrowserRef, in pType as MCBrowserNavigationEventType, in pState as MCBrowserNavigationState, in pFrame as Boolean, in pUrl as String, in pError as optional String) returns nothing
private handler OnNavigationCallback(in pContext as optional Pointer, in pBrowser as MCBrowserRef, in pType as MCBrowserNavigationEventType, in pState as MCBrowserNavigationState, in pFrame as CBool, in pUrl as ZStringUTF8, in pError as optional ZStringUTF8) returns nothing
variable tType as String
put element (pType + 1) of kMCBrowserNavigationEventTypeMap into tType
variable tState as String
put element (pState + 1) of kMCBrowserNavigationStateMap into tState
if pState is kMCBrowserNavigationStateFailed then
log "Failed: %@" with [pError]
end if
/* Dispatch browser message */
variable tMessage as String
if tType is "navigate" and tState is "unhandled" then
put "browserUnhandledLoadRequest" into tMessage
else
if pFrame then
put "browserFrame" & Capitalize(tType) & Capitalize(tState) into tMessage
else
put "browser" & Capitalize(tType) & Capitalize(tState) into tMessage
end if
end if
variable tArgs as List
if pState is kMCBrowserNavigationStateFailed then
put [pUrl, pError] into tArgs
else
put [pUrl] into tArgs
end if
if pType is kMCBrowserNavigationEventTypeNavigate and pState is kMCBrowserNavigationStateBegin and not pFrame then
updateUrlProperty(pUrl)
end if
post tMessage to mScriptObject with tArgs
end handler
/* WORKAROUND - Need to retain foreign types in handler as conversion to those in the foreign handler type signature is not yet implemented */
//private handler OnJavaScriptCallback(in pContext as optional Pointer, in pBrowser as MCBrowserRef, in pHandler as String, in pParams as MCBrowserListRef) returns nothing
private handler OnJavaScriptCallback(in pContext as optional Pointer, in pBrowser as MCBrowserRef, in pHandler as ZStringUTF8, in pParams as MCBrowserListRef) returns nothing
variable tList
if not browserListToLCBList(pParams, tList) then
throw "error converting javascript callback parameters"
end if
post pHandler to mScriptObject with tList
end handler
private handler OnProgressCallback(in pContext as optional Pointer, in pBrowser as MCBrowserRef, in pUrl as ZStringUTF8, in pProgress as UInt32) returns nothing
post "browserProgressChanged" to mScriptObject with [pUrl, pProgress]
end handler
----------
private handler getProperty(in pProperty as String) returns any
if mBrowser is nothing then
if pProperty is among the keys of mProperties then
return mProperties[pProperty]
else
throw "can't access property: " & pProperty
end if
else
return browserGetProperty(mBrowser, pProperty)
end if
end handler
private handler setProperty(in pProperty as String, in pValue as any)
put pValue into mProperties[pProperty]
if mBrowser is not nothing then
/* UNCHECKED */ browserSetProperty(mBrowser, pProperty, pValue)
end if
end handler
--
private handler clearBrowser()
setHtmlText(the empty string)
end handler
private handler getUrl() returns String
if "url" is among the keys of mProperties then
return mProperties["url"]
else
return the empty string
end if
end handler
private handler setUrl(in pUrl as String)
if pUrl is empty then
clearBrowser()
else
updateUrlProperty(pUrl)
if mBrowser is not nothing then
unsafe
if not MCBrowserGoToURL(mBrowser, pUrl) then
throw "error launching url " & pUrl
end if
end unsafe
end if
end if
end handler
private handler updateUrlProperty(pUrl as String)
put pUrl into mProperties["url"]
if "htmlText" is among the keys of mProperties then
delete mProperties["htmlText"]
end if
end handler
--
private handler getHtmlText() returns String
if "htmlText" is among the keys of mProperties then
return mProperties["htmlText"]
else
return getProperty("htmlText")
end if
end handler
private handler setHtmlText(in pHtmlText as String)
updateHtmlTextProperty(pHtmlText)
setProperty("htmlText", pHtmlText)
end handler
private handler updateHtmlTextProperty(in pHtmlText as String)
put pHtmlText into mProperties["htmlText"]
if "url" is among the keys of mProperties then
delete mProperties["url"]
end if
end handler
--
private handler getUserAgent() returns String
return getProperty("userAgent")
end handler
private handler setUserAgent(in pUserAgent as String)
setProperty("userAgent", pUserAgent)
end handler
--
private handler getJavaScriptHandlers() returns String
return getProperty("javaScriptHandlers")
end handler
private handler setJavaScriptHandlers(in pJavaScriptHandlers as String)
setProperty("javaScriptHandlers", pJavaScriptHandlers)
end handler
--
private handler getVScrollbar() returns Boolean
return getProperty("verticalScrollbarEnabled")
end handler
private handler setVScrollbar(in pVScrollbar as Boolean)
setProperty("verticalScrollbarEnabled", pVScrollbar)
end handler
--
private handler getHScrollbar() returns Boolean
return getProperty("horizontalScrollbarEnabled")
end handler
private handler setHScrollbar(in pHScrollbar as Boolean)
setProperty("horizontalScrollbarEnabled", pHScrollbar)
end handler
--
private handler getAllowUserInteraction() returns Boolean
return getProperty("allowUserInteraction")
end handler
private handler setAllowUserInteraction(in pAllowUserInteraction as Boolean)
setProperty("allowUserInteraction", pAllowUserInteraction)
end handler
--
private handler getIsSecure() returns Boolean
return getProperty("isSecure")
end handler
----------
private handler applyProperties(in pProperties as Array)
variable tKey
repeat for each element tKey in kPersistentProps
if tKey is among the keys of pProperties then
setProperty(tKey, pProperties[tKey])
end if
end repeat
if "url" is among the keys of pProperties then
setUrl(pProperties["url"])
else if "htmlText" is among the keys of pProperties then
setHtmlText(pProperties["htmltext"])
end if
end handler
----------
public handler OnLaunchUrl(in pUrl as String)
setUrl(pUrl)
end handler
public handler OnGoBack()
if mBrowser is not nothing then
unsafe
if not MCBrowserGoBack(mBrowser) then
throw "error going back"
end if
end unsafe
end if
end handler
public handler OnGoForward()
if mBrowser is not nothing then
unsafe
if not MCBrowserGoForward(mBrowser) then
throw "error going forward"
end if
end unsafe
end if
end handler
public handler OnDo(in pScript as String)
browserEvaluateJavaScript(pScript)
end handler
private handler browserEvaluateJavaScript(in pScript as String) returns String
if mBrowser is not nothing then
variable tResult as String
unsafe
if not MCBrowserEvaluateJavaScript(mBrowser, pScript, tResult) then
throw "error evaluating javascript"
end if
end unsafe
return tResult
end if
return the empty string
end handler
----------
constant kSvgIcon is "M21.7,12c0,5.4-4.4,9.7-9.7,9.7S2.3,17.4,2.3,12S6.6,2.3,12,2.3S21.7,6.6,21.7,12z M11.9,16.6c0-0.2-0.1-0.3-0.3-0.4 C11,16,10.4,16,9.9,15.5c-0.1-0.2-0.1-0.4-0.2-0.6c-0.2-0.2-0.7-0.3-1-0.4c-0.4,0-0.8,0-1.3,0c-0.2,0-0.5,0-0.7,0 c-0.3-0.1-0.5-0.5-0.7-0.8C6,13.6,6,13.4,5.8,13.4c-0.2-0.1-0.4,0.1-0.6,0c-0.1-0.1-0.1-0.2-0.1-0.3c0-0.3,0.2-0.6,0.4-0.8 c0.3-0.2,0.6,0.1,0.9,0.1c0.1,0,0.1,0,0.2,0.1C6.9,12.6,7,13,7,13.3c0,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0.2,1C12,7.4,12.1,7.4,12.1,7.4z M20.5,14.4c0.1-0.2,0.1-0.5,0.2-0.7 c0.1-0.5,0.1-1,0.1-1.5c0-1-0.1-2-0.4-2.9C20.2,9,20.1,8.7,20,8.4c-0.2-0.5-0.5-1-0.9-1.4c-0.4-0.5-0.9-1.9-1.8-1.5 c-0.3,0.1-0.5,0.5-0.7,0.7c-0.2,0.3-0.4,0.6-0.6,0.9c-0.1,0.1-0.2,0.3-0.1,0.4c0,0.1,0.1,0.1,0.2,0.1c0.2,0.1,0.3,0.1,0.5,0.2 c0.1,0,0.2,0.1,0.1,0.2c0,0,0,0.1-0.1,0.1c-0.5,0.5-1,0.9-1.5,1.4c-0.1,0.1-0.2,0.3-0.2,0.4c0,0.1,0.1,0.1,0.1,0.2 c0,0.1-0.1,0.1-0.2,0.2c-0.2,0.1-0.4,0.2-0.5,0.3c-0.1,0.2,0,0.5-0.1,0.7c-0.1,0.5-0.4,0.9-0.6,1.4c-0.2,0.3-0.3,0.6-0.5,0.9 c0,0.4-0.1,0.7,0.1,1c0.5,0.7,1.4,0.3,2.1,0.6c0.2,0.1,0.4,0.1,0.5,0.3c0.3,0.3,0.3,0.8,0.4,1.1c0.1,0.4,0.2,0.8,0.4,1.2 c0.1,0.5,0.3,1,0.4,1.4c0.9-0.7,1.7-1.5,2.3-2.5C19.9,16,20.2,15.2,20.5,14.4z"
public handler OnPaint() returns nothing
paintPlaceholderImage(this canvas, my bounds, kSvgIcon, my name, nothing, nothing)
end handler
----------
end widget