-
Notifications
You must be signed in to change notification settings - Fork 267
/
sciter.go
2439 lines (2157 loc) · 89.5 KB
/
sciter.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
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This package provides a Golang bindings of Sciter using cgo.
// Using go sciter you must have the platform specified sciter dynamic library
// downloaded from sciter-sdk,
// the library itself is rather small (under 5MB, less than 2MB when upxed) .
package sciter
/*
#cgo CFLAGS: -g -std=c11 -Iinclude -DPLAIN_API_ONLY -D_BSD_SOURCE -D_DEFAULT_SOURCE
#cgo linux LDFLAGS: -ldl
#cgo linux pkg-config: gtk+-3.0
#include "sciter-x.h"
extern SBOOL SC_CALLBACK SciterElementCallback_cgo(HELEMENT he, LPVOID param);
extern VOID SC_CALLBACK LPCSTR_RECEIVER_cgo( LPCSTR str, UINT str_length, LPVOID param );
extern VOID SC_CALLBACK LPCWSTR_RECEIVER_cgo( LPCWSTR str, UINT str_length, LPVOID param );
extern VOID SC_CALLBACK LPCBYTE_RECEIVER_cgo( LPCBYTE bytes, UINT num_bytes, LPVOID param );
extern SBOOL SC_CALLBACK ElementEventProc_cgo(LPVOID tag, HELEMENT he, UINT evtg, LPVOID prms );
extern UINT SC_CALLBACK SciterHostCallback_cgo( LPSCITER_CALLBACK_NOTIFICATION pns, LPVOID callbackParam );
// native functor
extern VOID NATIVE_FUNCTOR_INVOKE_cgo( VOID* tag, UINT argc, const VALUE* argv, VALUE* retval);
extern VOID NATIVE_FUNCTOR_RELEASE_cgo( VOID* tag );
// cmp
extern INT SC_CALLBACK ELEMENT_COMPARATOR_cgo( HELEMENT he1, HELEMENT he2, LPVOID param );
// ValueEnumElements
extern SBOOL SC_CALLBACK KeyValueCallback_cgo(LPVOID param, const VALUE* pkey, const VALUE* pval );
extern const char * SCITER_DLL_PATH;
extern HSARCHIVE SCAPI SciterOpenArchive (LPCBYTE archiveData, UINT archiveDataLength);
extern SBOOL SCAPI SciterGetArchiveItem (HSARCHIVE harc, LPCWSTR path, LPCBYTE* pdata, UINT* pdataLength);
extern SBOOL SCAPI SciterCloseArchive (HSARCHIVE harc);
*/
import "C"
import (
"fmt"
"log"
"runtime"
"strings"
"unsafe"
)
type Sciter struct {
hwnd C.HWINDOW
// because it live with the PROGRAM
callbacks map[*CallbackHandler]struct{}
// map scripting function name to NativeFunctor
*eventMapper
// sciter archive
har C.HSARCHIVE
}
var (
BAD_HWINDOW C.HWINDOW = nil
)
func Wrap(hwnd C.HWINDOW) *Sciter {
s := &Sciter{
hwnd: hwnd,
callbacks: make(map[*CallbackHandler]struct{}),
}
return s
}
func ClassName() string {
cname := (*uint16)(unsafe.Pointer(C.SciterClassName()))
return Utf16ToString(cname)
}
func Version(major bool) uint {
var v C.UINT
if major {
v = C.SciterVersion(C.SBOOL(1))
} else {
v = C.SciterVersion(C.SBOOL(0))
}
return uint(v)
}
func VersionAsString() string {
major := Version(true)
minor := Version(false)
return fmt.Sprintf("0x%x %x", major, minor)
}
func (s *Sciter) GetHwnd() C.HWINDOW {
return s.hwnd
}
func SetDLL(dir string) {
C.SCITER_DLL_PATH = C.CString(dir)
}
//This function is used in response to SCN_LOAD_DATA request.
//
// \param[in] hwnd \b HWINDOW, Sciter window handle.
// \param[in] uri \b LPCWSTR, URI of the data requested by Sciter.
// \param[in] data \b LPBYTE, pointer to data buffer.
// \param[in] dataLength \b UINT, length of the data in bytes.
// \return \b BOOL, TRUE if Sciter accepts the data or \c FALSE if error occured
// (for example this function was called outside of #SCN_LOAD_DATA request).
//
// \warning If used, call of this function MUST be done ONLY while handling
// SCN_LOAD_DATA request and in the same thread. For asynchronous resource loading
// use SciterDataReadyAsync
func (s *Sciter) DataReady(uri string, data []byte) bool {
var pData C.LPCBYTE
if len(data) > 0 {
pData = (C.LPCBYTE)(unsafe.Pointer(&data[0]))
}
ret := C.SciterDataReady(s.hwnd, StringToWcharPtr(uri), pData, C.UINT(len(data)))
if ret == 0 {
return false
}
// mark the data to prevent gc
loadedUri[uri] = data
return true
}
var (
loadedUri = map[string][]byte{}
)
//SciterDataReadyAsync (HWINDOW hwnd,LPCWSTR uri, LPCBYTE data, UINT dataLength, LPVOID requestId) ;//{ return SAPI()->SciterDataReadyAsync (hwnd,uri, data, dataLength, requestId); }
//Use this function outside of SCN_LOAD_DATA request. This function is needed when you
// you have your own http client implemented in your application.
//
// \param[in] hwnd \b HWINDOW, Sciter window handle.
// \param[in] uri \b LPCWSTR, URI of the data requested by Sciter.
// \param[in] data \b LPBYTE, pointer to data buffer.
// \param[in] requestId \b LPVOID, SCN_LOAD_DATA requestId.
// \return \b BOOL, TRUE if Sciter accepts the data or \c FALSE if error occured
func (s *Sciter) DataReadyAsync(uri string, data []byte, requestId C.HREQUEST) bool {
// args
var pData C.LPCBYTE
if len(data) > 0 {
pData = (C.LPCBYTE)(unsafe.Pointer(&data[0]))
}
curi := StringToWcharPtr(uri)
cdataLength := C.UINT(len(data))
crequestId := C.LPVOID(requestId)
// cgo call
ret := C.SciterDataReadyAsync(s.hwnd, curi, pData, cdataLength, crequestId)
if ret == 0 {
return false
}
return true
}
// BOOL SciterLoadFile (HWINDOW hWndSciter, LPCWSTR filename) ;//{ return SAPI()->SciterLoadFile (hWndSciter,filename); }
func (s *Sciter) LoadFile(filename string) error {
ret := C.SciterLoadFile(s.hwnd, StringToWcharPtr(filename))
if ret == 0 {
return fmt.Errorf("LoadFile with: %s failed", filename)
}
return nil
}
// BOOL SciterLoadHtml (HWINDOW hWndSciter, LPCBYTE html, UINT htmlSize, LPCWSTR baseUrl) ;//{ return SAPI()->SciterLoadHtml (hWndSciter,html,htmlSize,baseUrl); }
//Load HTML from in memory buffer with base.
//
// \param[in] hWndSciter \b HWINDOW, Sciter window handle.
// \param[in] html \b LPCBYTE, Address of HTML to load.
// \param[in] htmlSize \b UINT, Length of the array pointed by html parameter.
// \param[in] baseUrl \b LPCWSTR, base URL. All relative links will be resolved against
// this URL.
// \return \b BOOL, \c TRUE if the text was parsed and loaded successfully, FALSE otherwise.
func (s *Sciter) LoadHtml(html, baseUrl string) error {
// args
chtml := (C.LPCBYTE)(unsafe.Pointer(StringToBytePtr(html)))
csize := C.UINT(len(html))
cbaseUrl := StringToWcharPtr(baseUrl)
// cgo call
ret := C.SciterLoadHtml(s.hwnd, chtml, csize, cbaseUrl)
if ret == 0 {
return fmt.Errorf("LoadHtml with: %s failed", string(html))
}
return nil
}
// typedef UINT SC_CALLBACK SciterHostCallback( LPSCITER_CALLBACK_NOTIFICATION pns, LPVOID callbackParam );
//export goSciterHostCallback
func goSciterHostCallback(ph unsafe.Pointer, callbackParam unsafe.Pointer) int {
phdr := (*SciterCallbackNotification)(ph)
handler := globalCallbackHandlers[int(uintptr(callbackParam))]
switch phdr.Code {
case SC_LOAD_DATA:
if handler.OnLoadData != nil {
return handler.OnLoadData((*ScnLoadData)(unsafe.Pointer(phdr)))
}
case SC_DATA_LOADED:
if handler.OnDataLoaded != nil {
return handler.OnDataLoaded((*ScnDataLoaded)(unsafe.Pointer(phdr)))
}
case SC_ATTACH_BEHAVIOR:
params := (*ScnAttachBehavior)(unsafe.Pointer(phdr))
key := params.BehaviorName()
if behavior, exists := handler.Behaviors[key]; exists {
// Increment the reference count for this behavior
if refCount, exists := behaviors[behavior]; exists {
behaviors[behavior] = refCount + 1
} else {
behaviors[behavior] = 1
}
el := WrapElement(params.Element())
el.attachBehavior(behavior)
} else {
log.Printf("No such behavior <%s> found", key)
}
case SC_ENGINE_DESTROYED:
if handler.OnEngineDestroyed != nil {
return handler.OnEngineDestroyed()
}
case SC_POSTED_NOTIFICATION:
if handler.OnPostedNotification != nil {
return handler.OnPostedNotification((*ScnPostedNotification)(unsafe.Pointer(phdr)))
}
case SC_GRAPHICS_CRITICAL_FAILURE:
if handler.OnGraphicsCriticalFailure != nil {
return handler.OnGraphicsCriticalFailure()
}
case SC_KEYBOARD_REQUEST:
if handler.OnKeyboardRequest != nil {
return handler.OnKeyboardRequest((*ScnKeyboardRequest)(unsafe.Pointer(phdr)))
}
case SC_INVALIDATE_RECT:
if handler.OnInvalidateRect != nil {
return handler.OnInvalidateRect((*ScnInvalidateRect)(unsafe.Pointer(phdr)))
}
}
return 0
}
var (
sciterhostcallback = C.LPSciterHostCallback(unsafe.Pointer(C.SciterHostCallback_cgo))
globalCallbackHandlers = make([]*CallbackHandler, 0)
)
// VOID SciterSetCallback (HWINDOW hWndSciter, LPSciterHostCallback cb, LPVOID cbParam)
//Set \link #SCITER_NOTIFY() notification callback function \endlink.
//
// \param[in] hWndSciter \b HWINDOW, Sciter window handle.
// \param[in] cb \b SCITER_NOTIFY*, \link #SCITER_NOTIFY() callback function \endlink.
// \param[in] cbParam \b LPVOID, parameter that will be passed to \link #SCITER_NOTIFY() callback function \endlink as vParam paramter.
func (s *Sciter) SetCallback(handler *CallbackHandler) {
if _, ok := s.callbacks[handler]; ok {
return
}
s.callbacks[handler] = struct{}{}
// args
globalCallbackHandlers = append(globalCallbackHandlers, handler)
idx := len(globalCallbackHandlers) - 1
cparam := C.LPVOID(unsafe.Pointer(uintptr(idx)))
// cgo call
C.SciterSetCallback(s.hwnd, sciterhostcallback, cparam)
}
// BOOL SciterSetMasterCSS (LPCBYTE utf8, UINT numBytes)
//Set Master style sheet.
//
// \param[in] utf8 \b LPCBYTE, start of CSS buffer.
// \param[in] numBytes \b UINT, number of bytes in utf8.
func SetMasterCSS(data string) (ok bool) {
// args
cutf8 := C.LPCBYTE(unsafe.Pointer(StringToBytePtr(data)))
clen := C.UINT(len(data))
// cgo call
r := C.SciterSetMasterCSS(cutf8, clen)
if r == 0 {
return false
}
return true
}
// BOOL SciterAppendMasterCSS (LPCBYTE utf8, UINT numBytes)
//Append Master style sheet.
//
// \param[in] utf8 \b LPCBYTE, start of CSS buffer.
// \param[in] numBytes \b UINT, number of bytes in utf8.
func AppendMasterCSS(data string) (ok bool) {
// args
cutf8 := C.LPCBYTE(unsafe.Pointer(StringToBytePtr(data)))
clen := C.UINT(len(data))
// cgo call
r := C.SciterAppendMasterCSS(cutf8, clen)
if r == 0 {
return false
}
return true
}
// BOOL SciterSetCSS (HWINDOW hWndSciter, LPCBYTE utf8, UINT numBytes, LPCWSTR baseUrl, LPCWSTR mediaType)
//Set (reset) style sheet of current document.
//Will reset styles for all elements according to given CSS (utf8)
//
// \param[in] hWndSciter \b HWINDOW, Sciter window handle.
// \param[in] utf8 \b LPCBYTE, start of CSS buffer.
// \param[in] numBytes \b UINT, number of bytes in utf8.
func (s *Sciter) SetCSS(css, baseUrl, mediaType string) (ok bool) {
// args
ccss := C.LPCBYTE(unsafe.Pointer(StringToBytePtr(css)))
clen := C.UINT(len(css))
cbaseUrl := StringToWcharPtr(baseUrl)
cmediaType := StringToWcharPtr(mediaType)
// cgo call
r := C.SciterSetCSS(s.hwnd, ccss, clen, cbaseUrl, cmediaType)
if r == 0 {
return false
}
return true
}
// BOOL SciterSetMediaType (HWINDOW hWndSciter, LPCWSTR mediaType)
//Set media type of this sciter instance.
//
// \param[in] hWndSciter \b HWINDOW, Sciter window handle.
// \param[in] mediaType \b LPCWSTR, media type name.
//
// For example media type can be "handheld", "projection", "screen", "screen-hires", etc.
// By default sciter window has "screen" media type.
//
// Media type name is used while loading and parsing style sheets in the engine so
// you should call this function *before* loading document in it.
func (s *Sciter) SetMediaType(mediaType string) (ok bool) {
// args
cmediaType := C.LPCWSTR(unsafe.Pointer(StringToWcharPtr(mediaType)))
// cgo call
r := C.SciterSetMediaType(s.hwnd, cmediaType)
if r == 0 {
return false
}
return true
}
// BOOL SciterSetMediaVars (HWINDOW hWndSciter, const SCITER_VALUE *mediaVars)
// UINT SciterGetMinWidth (HWINDOW hWndSciter)
func (s *Sciter) MinWidth() int {
r := C.SciterGetMinWidth(s.hwnd)
return int(r)
}
// UINT SciterGetMinHeight (HWINDOW hWndSciter, UINT width)
func (s *Sciter) MinHeight(width int) int {
// args
cwidth := C.UINT(width)
// cgo call
r := C.SciterGetMinHeight(s.hwnd, cwidth)
return int(r)
}
// BOOL SciterCall (HWINDOW hWnd, LPCSTR functionName, UINT argc, const SCITER_VALUE* argv, SCITER_VALUE* retval)
// To call global function defined in script using its full name (may include name of namespaces where it resides)
func (s *Sciter) Call(functionName string, args ...*Value) (retval *Value, err error) {
retval = NewValue()
argc := len(args)
argv := make([]Value, argc)
for i := 0; i < argc; i++ {
argv[i].Copy(args[i]) // Make copy that is not garbage collected
defer argv[i].finalize()
}
// args
funcName := C.CString(functionName)
defer C.free(unsafe.Pointer(funcName))
cfn := C.LPCSTR(unsafe.Pointer(funcName))
cargc := C.UINT(argc)
var cargv *C.SCITER_VALUE
if len(argv) > 0 {
cargv = (*C.SCITER_VALUE)(unsafe.Pointer(&argv[0]))
}
cretval := (*C.SCITER_VALUE)(unsafe.Pointer(retval))
// cgo call
r := C.SciterCall(s.hwnd, cfn, cargc, cargv, cretval)
if r == 0 {
err = fmt.Errorf("SciterCall failed with function: %s", functionName)
}
return
}
// BOOL SciterEval ( HWINDOW hwnd, LPCWSTR script, UINT scriptLength, SCITER_VALUE* pretval)
// To evaluate arbitrary script in context of current document loaded into the window
func (s *Sciter) Eval(script string) (retval *Value, ok bool) {
retval = NewValue()
// args
u16, err := Utf16FromString(script)
if err != nil {
return nil, false
}
cscriptLength := C.UINT(len(u16) - 1)
cscript := C.LPCWSTR(unsafe.Pointer(&u16[0]))
cretval := (*C.SCITER_VALUE)(unsafe.Pointer(retval))
// cgo call
r := C.SciterEval(s.hwnd, cscript, cscriptLength, cretval)
if r == 0 {
ok = false
} else {
ok = true
}
return
}
// VOID SciterUpdateWindow(HWINDOW hwnd)
func (s *Sciter) UpdateWindow() {
C.SciterUpdateWindow(s.hwnd)
}
// #ifdef WINDOWS
// BOOL SciterTranslateMessage (MSG* lpMsg) ;//{ return SAPI()->SciterTranslateMessage (lpMsg); }
// #endif
// BOOL SciterSetOption (HWINDOW hWnd, UINT option, UINT_PTR value )
func (s *Sciter) SetOption(option Sciter_RT_OPTIONS, value uint) (ok bool) {
// args
coption := C.UINT(option)
cvalue := C.UINT_PTR(value)
// cgo call
r := C.SciterSetOption(s.hwnd, coption, cvalue)
if r == 0 {
return false
}
return true
}
// BOOL SciterSetOption (NULL, UINT option, UINT_PTR value )
func SetOption(option Sciter_RT_OPTIONS, value uint) (ok bool) {
coption := C.UINT(option)
cvalue := C.UINT_PTR(value)
hwnd := BAD_HWINDOW
r := C.SciterSetOption(hwnd, coption, cvalue)
if r == 0 {
return false
}
return true
}
// VOID SciterGetPPI (HWINDOW hWndSciter, UINT* px, UINT* py) ;// { SAPI()->SciterGetPPI (hWndSciter,px,py); }
// BOOL SciterGetViewExpando ( HWINDOW hwnd, VALUE* pval ) ;//{ return SAPI()->SciterGetViewExpando ( hwnd, pval ); }
// #ifdef WINDOWS
// BOOL SciterRenderD2D (HWINDOW hWndSciter, ID2D1RenderTarget* prt) ;//{ return SAPI()->SciterRenderD2D (hWndSciter,prt); }
// BOOL SciterD2DFactory (ID2D1Factory ** ppf) ;//{ return SAPI()->SciterD2DFactory (ppf); }
// BOOL SciterDWFactory (IDWriteFactory ** ppf) ;//{ return SAPI()->SciterDWFactory (ppf); }
// #endif
// BOOL SciterGraphicsCaps (LPUINT pcaps) ;//{ return SAPI()->SciterGraphicsCaps (pcaps); }
// BOOL SciterSetHomeURL (HWINDOW hWndSciter, LPCWSTR baseUrl)
// Set sciter home url.
// home url is used for resolving sciter: urls
// If you will set it like SciterSetHomeURL(hwnd,"http://sciter.com/modules/")
// then <script src="sciter:lib/root-extender.tis"> will load
// root-extender.tis from http://sciter.com/modules/lib/root-extender.tis
//
// \param[in] hWndSciter \b HWINDOW, Sciter window handle.
// \param[in] baseUrl \b LPCWSTR, URL of sciter home.
func (s *Sciter) SetHomeURL(baseUrl string) (ok bool) {
// args
cbaseUrl := C.LPCWSTR(unsafe.Pointer(StringToWcharPtr(baseUrl)))
// cgo call
r := C.SciterSetHomeURL(s.hwnd, cbaseUrl)
if r == 0 {
return false
}
return true
}
// Open data blob of the provided compressed Sciter archive.
func (s *Sciter) OpenArchive(data []byte) {
s.har = C.SciterOpenArchive((*C.BYTE)(&data[0]), C.UINT(len(data)))
}
// Get an archive item referenced by \c uri.
//
// Usually it is passed to \c Sciter.DataReady().
func (s *Sciter) GetArchiveItem(uri string) []byte {
var data C.LPCBYTE
var length C.UINT
cdata := (*C.LPCBYTE)(&data)
r := C.SciterGetArchiveItem(s.har, StringToWcharPtr(uri), cdata, &length)
if r == 0 {
return nil
}
ret := ByteCPtrToBytes(data, length)
return ret
}
// Close the archive.
func (s *Sciter) CloseArchive() {
C.SciterCloseArchive(s.har)
s.har = C.HSARCHIVE(nil)
}
// Register `this://app/` URLs to be loaded from the given Sciter archive.
//
// Pack resources using `packfolder` tool:
//
// `$ packfolder res_folder res_packed.go -v resource_name -go`
//
// Usage:
//
//```
// win.SetResourceArchive(resource_name)
// win.LoadFile("this://app//index.htm")
//```
func (s *Sciter) SetResourceArchive(data []byte) {
// register `this://app/` schema
callback := &CallbackHandler{
OnLoadData: func(params *ScnLoadData) int {
if strings.HasPrefix(params.Uri(), "this://app/") {
// load resource starting with our schema
url := params.Uri()[11:]
fileData := s.GetArchiveItem(url)
if fileData != nil {
// use loaded resource
s.DataReady(url, fileData)
} else {
// failed to load
log.Println("error: failed to load " + params.Uri())
// but fallback to Sciter anyway
}
}
return LOAD_OK
},
}
s.OpenArchive(data)
s.SetCallback(callback)
}
// #if defined(OSX)
// HWINDOW SciterCreateNSView ( LPRECT frame ) ;//{ return SAPI()->SciterCreateNSView ( frame ); }
// #endif
// HWINDOW SciterCreateWindow ( UINT creationFlags,LPRECT frame, SciterWindowDelegate* delegate, LPVOID delegateParam, HWINDOW parent);
// #if defined(OSX)
// HWINDOW SCAPI SciterCreateNSView( LPRECT frame ); // returns NSView*
// typedef LPVOID SciterWindowDelegate; // Obj-C id, NSWindowDelegate and NSResponder
// #elif defined(WINDOWS)
// typedef LRESULT SC_CALLBACK SciterWindowDelegate(HWINDOW hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LPVOID pParam, BOOL* handled);
// #elif defined(LINUX)
// typedef LPVOID SciterWindowDelegate;
// #endif
// type HELEMENT C.HELEMENT
var (
BAD_HELEMENT = C.HELEMENT(unsafe.Pointer(uintptr(0)))
)
var (
elementHandlerMap = map[*Element]map[*EventHandler]struct{}{}
)
// Represents a single DOM element, owns and manages a Handle
type Element struct {
handle C.HELEMENT
err error
// defaultHandler *EventHandler
*eventMapper
}
// Wrap C.HELEMENT to a go side *Element, doing Sciter_UseElement/Sciter_UnuseElement automatically
func WrapElement(he C.HELEMENT) *Element {
e := &Element{
handle: he,
}
e.use()
runtime.SetFinalizer(e, (*Element).finalize)
return e
}
// SCDOM_RESULT Sciter_UseElement(HELEMENT he) ;//{ return SAPI()->Sciter_UseElement(he); }
func (e *Element) use() error {
r := C.Sciter_UseElement(e.handle)
return wrapDomResult(r, "Sciter_UseElement")
}
// SCDOM_RESULT Sciter_UnuseElement(HELEMENT he) ;//{ return SAPI()->Sciter_UnuseElement(he); }
//Marks DOM object as unused (a.k.a. Release).
//
// UnUse would be called automatically when released by Go Runtime
// for *Element created by WrapElement(...)
//
// Get handle of every element's child element.
// \param[in] he \b #HELEMENT
// \return \b #SCDOM_RESULT SCAPI
//
// Application should call this function when it does not need element's
// handle anymore.
// \sa #Sciter_UseElement()
func (e *Element) unUse() error {
r := C.Sciter_UnuseElement(e.handle)
return wrapDomResult(r, "Sciter_UnuseElement")
}
// finalize() only happens when *Element is no longer used
// but the existence of elementHandlerMap makes that
// elements with EventHandler attached would not be gc collected
func (e *Element) finalize() {
// Detach handlers
// log.Printf("finalizing el 0x%x", e)
// only detach when element does have handlers
if handlersmap, ok := elementHandlerMap[e]; ok {
for handler, _ := range handlersmap {
e.DetachEventHandler(handler)
}
}
// Release the underlying sciter handle
e.unUse()
e.handle = BAD_HELEMENT
}
// SCDOM_RESULT SciterGetRootElement(HWINDOW hwnd, HELEMENT *phe) ;//{ return SAPI()->SciterGetRootElement(hwnd, phe); }
//Get root DOM element of HTML document.
// \param[in] hwnd \b HWINDOW, Sciter window for which you need to get root
// element
// \param[out ] phe \b #HELEMENT*, variable to receive root element
// \return \b #SCDOM_RESULT SCAPI
//
// Root DOM object is always a 'HTML' element of the document.
func (s *Sciter) GetRootElement() (*Element, error) {
var he C.HELEMENT
r := C.SciterGetRootElement(s.hwnd, &he)
return WrapElement(he), wrapDomResult(r, "SciterGetRootElement")
}
// SCDOM_RESULT SciterGetFocusElement(HWINDOW hwnd, HELEMENT *phe) ;//{ return SAPI()->SciterGetFocusElement(hwnd, phe); }
//Get focused DOM element of HTML document.
// \param[in] hwnd \b HWINDOW, Sciter window for which you need to get focus
// element
// \param[out ] phe \b #HELEMENT*, variable to receive focus element
// \return \b #SCDOM_RESULT SCAPI
//
// phe can have null value (0).
//
// COMMENT: To set focus on element use SciterSetElementState(STATE_FOCUS,0)
func (s *Sciter) GetFocusElement() (*Element, error) {
var he C.HELEMENT
r := C.SciterGetFocusElement(s.hwnd, &he)
return WrapElement(he), wrapDomResult(r, "SciterGetFocusElement")
}
// SCDOM_RESULT SciterFindElement(HWINDOW hwnd, POINT pt, HELEMENT* phe) ;//{ return SAPI()->SciterFindElement(hwnd,pt,phe); }
// Find DOM element by coordinate.
// \param[in] hwnd \b HWINDOW, Sciter window for which you need to find
// elementz
// \param[in] pt \b POINT, coordinates, window client area relative.
// \param[out ] phe \b #HELEMENT*, variable to receive found element handle.
// \return \b #SCDOM_RESULT SCAPI
//
// If element was not found then *phe will be set to zero.
func (s *Sciter) FindElement(pt Point) (*Element, error) {
var he C.HELEMENT
cpt := *(*C.POINT)(unsafe.Pointer(&pt))
r := C.SciterFindElement(s.hwnd, cpt, &he)
return WrapElement(he), wrapDomResult(r, "SciterFindElement")
}
// SCDOM_RESULT SciterGetChildrenCount(HELEMENT he, UINT* count) ;//{ return SAPI()->SciterGetChildrenCount(he, count); }
// Get number of child elements.
// \param[in] he \b #HELEMENT, element which child elements you need to count
// \param[out] count \b UINT*, variable to receive number of child elements
// \return \b #SCDOM_RESULT SCAPI
//
// \par Example:
// for paragraph defined as
// \verbatim <p>Hello <b>wonderfull</b> world!</p> \endverbatim
// count will be set to 1 as the paragraph has only one sub element:
// \verbatim <b>wonderfull</b> \endverbatim
func (e *Element) ChildrenCount() (int, error) {
var count C.UINT
r := C.SciterGetChildrenCount(e.handle, &count)
return int(count), wrapDomResult(r, "SciterGetChildrenCount")
}
// SCDOM_RESULT SciterGetNthChild(HELEMENT he, UINT n, HELEMENT* phe) ;//{ return SAPI()->SciterGetNthChild(he,n,phe); }
// Get handle of every element's child element.
// \param[in] he \b #HELEMENT
// \param[in] n \b UINT, number of the child element
// \param[out] phe \b #HELEMENT*, variable to receive handle of the child
// element
// \return \b #SCDOM_RESULT SCAPI
//
// \par Example:
// for paragraph defined as
// \verbatim <p>Hello <b>wonderfull</b> world!</p> \endverbatim
// *phe will be equal to handle of <b> element:
// \verbatim <b>wonderfull</b> \endverbatim
func (e *Element) NthChild(n int) (*Element, error) {
var he C.HELEMENT
r := C.SciterGetNthChild(e.handle, C.UINT(n), &he)
return WrapElement(he), wrapDomResult(r, "SciterGetNthChild")
}
// SCDOM_RESULT SciterGetParentElement(HELEMENT he, HELEMENT* p_parent_he) ;//{ return SAPI()->SciterGetParentElement(he,p_parent_he); }
// Get parent element.
func (e *Element) ParentElement() (*Element, error) {
var he C.HELEMENT
r := C.SciterGetParentElement(e.handle, &he)
return WrapElement(he), wrapDomResult(r, "SciterGetParentElement")
}
// inline VOID SC_CALLBACK _LPCBYTE2ASTRING( LPCBYTE bytes, UINT num_bytes, LPVOID param )
// {
// sciter::astring* s = (sciter::astring*)param;
// *s = sciter::astring((const char*)bytes,num_bytes);
// }
// var _LPCSTR_RECEIVER = syscall.NewCallback(func(bs *byte, n uint, param uintptr) int {
// s := (*string)(unsafe.Pointer(param))
// *s = bytePtrToString(bs)
// return 0
// })
// inline VOID SC_CALLBACK _LPCWSTR2STRING( LPCWSTR str, UINT str_length, LPVOID param )
// {
// sciter::string* s = (sciter::string*)param;
// *s = sciter::string(str,str_length);
// }
// var _LPCWSTR_RECEIVER = syscall.NewCallback(func(bs *uint16, n uint, param uintptr) int {
// s := (*string)(unsafe.Pointer(param))
// *s = utf16ToString(bs)
// return 0
// })
// typedef VOID SC_CALLBACK LPCBYTE_RECEIVER( LPCBYTE bytes, UINT num_bytes, LPVOID param );
//export goLPCBYTE_RECEIVER
func goLPCBYTE_RECEIVER(bs *byte, n uint, param unsafe.Pointer) int {
r := BytePtrToBytes(bs, n)
*(*[]byte)(param) = r
return 0
}
// typedef VOID SC_CALLBACK LPCWSTR_RECEIVER( LPCWSTR str, UINT str_length, LPVOID param );
//export goLPCWSTR_RECEIVER
func goLPCWSTR_RECEIVER(bs *uint16, n uint, param unsafe.Pointer) int {
s := (*string)(param)
*s = Utf16ToString(bs)
return 0
}
// typedef VOID SC_CALLBACK LPCSTR_RECEIVER( LPCSTR str, UINT str_length, LPVOID param );
//export goLPCSTR_RECEIVER
func goLPCSTR_RECEIVER(bs C.LPCSTR, n uint, param unsafe.Pointer) int {
s := (*string)(param)
*s = C.GoString(bs)
return 0
}
var (
lpcbyte_receiver = (*C.LPCBYTE_RECEIVER)(unsafe.Pointer(C.LPCBYTE_RECEIVER_cgo))
lpcwstr_receiver = (*C.LPCWSTR_RECEIVER)(unsafe.Pointer(C.LPCWSTR_RECEIVER_cgo))
lpcstr_receiver = (*C.LPCSTR_RECEIVER)(unsafe.Pointer(C.LPCSTR_RECEIVER_cgo))
)
// SCDOM_RESULT SciterGetElementHtmlCB(HELEMENT he, BOOL outer, LPCBYTE_RECEIVER* rcv, LPVOID rcv_param) ;//{ return SAPI()->SciterGetElementHtmlCB( he, outer, rcv, rcv_param); }
//Get html representation of the element.
// \param[in] he \b #HELEMENT
// \param[in] outer \b BOOL, if TRUE will retunr outer HTML otherwise inner.
// \param[in] rcv \b pointer to function receiving UTF8 encoded HTML.
// \param[in] rcv_param \b parameter that passed to rcv as it is.
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) Html(outer bool) (string, error) {
var bs []byte
// args
couter := C.SBOOL(C.FALSE)
if outer {
couter = C.SBOOL(C.TRUE)
}
cparam := C.LPVOID(unsafe.Pointer(&bs))
// cgo call
r := C.SciterGetElementHtmlCB(e.handle, couter, lpcbyte_receiver, cparam)
str := string(bs)
return str, wrapDomResult(r, "SciterGetElementHtmlCB")
}
// SCDOM_RESULT SciterGetElementTextCB(HELEMENT he, LPCWSTR_RECEIVER* rcv, LPVOID rcv_param)
// Get inner text of the element as LPCWSTR (utf16 words).
// \param[in] he \b #HELEMENT
// \param[in] rcv \b pointer to the function receiving UTF16 encoded plain text
// \param[in] rcv_param \b param passed that passed to LPCWSTR_RECEIVER "as is"
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) Text() (string, error) {
var str string
// args
cparam := C.LPVOID(unsafe.Pointer(&str))
// cgo call
r := C.SciterGetElementTextCB(e.handle, lpcwstr_receiver, cparam)
return str, wrapDomResult(r, "SciterGetElementTextCB")
}
// SCDOM_RESULT SciterSetElementText(HELEMENT he, LPCWSTR utf16, UINT length)
//Set inner text of the element from LPCWSTR buffer (utf16 words).
// \param[in] he \b #HELEMENT
// \param[in] utf16words \b pointer, UTF16 encoded plain text
// \param[in] length \b UINT, number of words in utf16words sequence
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) SetText(text string) error {
// args
u16, err := Utf16FromString(text)
if err != nil {
return err
}
clength := C.UINT(len(u16) - 1)
ctext := C.LPCWSTR(unsafe.Pointer(&u16[0]))
// cgo call
r := C.SciterSetElementText(e.handle, ctext, clength)
return wrapDomResult(r, "SciterSetElementText")
}
// SCDOM_RESULT SciterGetAttributeCount(HELEMENT he, LPUINT p_count) ;//{ return SAPI()->SciterGetAttributeCount(he, p_count); }
//Get number of element's attributes.
// \param[in] he \b #HELEMENT
// \param[out] p_count \b LPUINT, variable to receive number of element
// attributes.
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) AttrCount() (int, error) {
var count C.UINT
// cgo call
r := C.SciterGetAttributeCount(e.handle, &count)
return int(count), wrapDomResult(r, "SciterGetAttributeCount")
}
// SCDOM_RESULT SciterGetNthAttributeNameCB(HELEMENT he, UINT n, LPCSTR_RECEIVER* rcv, LPVOID rcv_param) ;//{ return SAPI()->SciterGetNthAttributeNameCB(he,n,rcv,rcv_param); }
func (e *Element) NthAttrName(n int) (name string, err error) {
// args
cn := C.UINT(n)
cparam := C.LPVOID(unsafe.Pointer(&name))
// cgo call
r := C.SciterGetNthAttributeNameCB(e.handle, cn, lpcstr_receiver, cparam)
err = wrapDomResult(r, "SciterGetNthAttributeNameCB")
return
}
// SCDOM_RESULT SciterGetNthAttributeValueCB(HELEMENT he, UINT n, LPCWSTR_RECEIVER* rcv, LPVOID rcv_param) ;//{ return SAPI()->SciterGetNthAttributeValueCB(he, n, rcv, rcv_param); }
//Get value of any element's attribute by attribute's number.
// \param[in] he \b #HELEMENT
// \param[in] n \b UINT, number of desired attribute
// \param[out] p_name \b LPCSTR*, will be set to address of the string
// containing attribute name
// \param[out] p_value \b LPCWSTR*, will be set to address of the string
// containing attribute value
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) NthAttr(n int) (value string, err error) {
// args
cn := C.UINT(n)
cparam := C.LPVOID(unsafe.Pointer(&value))
// cgo call
r := C.SciterGetNthAttributeValueCB(e.handle, cn, lpcwstr_receiver, cparam)
err = wrapDomResult(r, "SciterGetNthAttributeValueCB")
return
}
// SCDOM_RESULT SciterGetAttributeByNameCB(HELEMENT he, LPCSTR name, LPCWSTR_RECEIVER* rcv, LPVOID rcv_param) ;//{ return SAPI()->SciterGetAttributeByNameCB(he,name,rcv,rcv_param); }
//Get value of any element's attribute by name.
// \param[in] he \b #HELEMENT
// \param[in] name \b LPCSTR, attribute name
// \param[out] p_value \b LPCWSTR*, will be set to address of the string
// containing attribute value
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) Attr(name string) (string, error) {
var str string
// args
cname := C.CString(name)
crcv := (*C.LPCWSTR_RECEIVER)(unsafe.Pointer(C.LPCWSTR_RECEIVER_cgo))
cparam := C.LPVOID(unsafe.Pointer(&str))
// cgo call
r := C.SciterGetAttributeByNameCB(e.handle, cname, crcv, cparam)
C.free(unsafe.Pointer(cname))
return str, wrapDomResult(r, "SciterGetAttributeByNameCB")
}
// SCDOM_RESULT SciterSetAttributeByName(HELEMENT he, LPCSTR name, LPCWSTR value) ;//{ return SAPI()->SciterSetAttributeByName(he,name,value); }
//Set attribute's value.
// \param[in] he \b #HELEMENT
// \param[in] name \b LPCSTR, attribute name
// \param[in] value \b LPCWSTR, new attribute value or 0 if you want to remove attribute.
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) SetAttr(name, val string) error {
// args
cname := C.CString(name)
cval := C.LPCWSTR(StringToWcharPtr(val))
// cgo call
r := C.SciterSetAttributeByName(e.handle, cname, cval)
C.free(unsafe.Pointer(cname))
return wrapDomResult(r, "SciterSetAttributeByName")
}
// SCDOM_RESULT SciterClearAttributes(HELEMENT he)
//Remove all attributes from the element.
// \param[in] he \b #HELEMENT
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) ClearAttr() error {
return wrapDomResult(C.SciterClearAttributes(e.handle), "SciterClearAttributes")
}
// SCDOM_RESULT SciterGetElementIndex(HELEMENT he, LPUINT p_index)
//Get element index.
// \param[in] he \b #HELEMENT
// \param[out] p_index \b LPUINT, variable to receive number of the element
// among parent element's subelements.
// \return \b #SCDOM_RESULT SCAPI
func (e *Element) Index() (idx int, err error) {
// args
var cidx C.UINT
// cgo call
r := C.SciterGetElementIndex(e.handle, &cidx)
idx = int(cidx)
err = wrapDomResult(r, "SciterGetElementIndex")
return
}
// SCDOM_RESULT SciterGetElementType(HELEMENT he, LPCSTR* p_type)
// SCDOM_RESULT SciterGetElementTypeCB(HELEMENT he, LPCSTR_RECEIVER* rcv, LPVOID rcv_param)
// Get element's type.
func (e *Element) Type() (string, error) {
var str string
// args
cparam := C.LPVOID(unsafe.Pointer(&str))
// cgo call
r := C.SciterGetElementTypeCB(e.handle, lpcstr_receiver, cparam)
return str, wrapDomResult(r, "SciterGetElementTypeCB")
}
// SCDOM_RESULT SciterGetStyleAttributeCB(HELEMENT he, LPCSTR name, LPCWSTR_RECEIVER* rcv, LPVOID rcv_param) ;//{ return SAPI()->SciterGetStyleAttributeCB(he,name,rcv,rcv_param); }
//Get element's style attribute.
// \param[in] he \b #HELEMENT
// \param[in] name \b LPCSTR, name of the style attribute
// \param[in] rcv \b pointer to the function receiving UTF16 encoded plain text
// \param[in] rcv_param \b param passed that passed to LPCWSTR_RECEIVER "as is"
//
// Style attributes are those that are set using css. E.g. "font-face: arial" or "display: block".
//
// \sa #SciterSetStyleAttribute()
func (e *Element) Style(name string) (string, error) {
var str string
// args
cname := C.CString(name)
crcv := (*C.LPCWSTR_RECEIVER)(unsafe.Pointer(C.LPCWSTR_RECEIVER_cgo))
cparam := C.LPVOID(unsafe.Pointer(&str))
// cgo call
r := C.SciterGetStyleAttributeCB(e.handle, cname, crcv, cparam)
C.free(unsafe.Pointer(cname))
return str, wrapDomResult(r, "SciterGetStyleAttributeCB")
}
// SCDOM_RESULT SciterSetStyleAttribute(HELEMENT he, LPCSTR name, LPCWSTR value) ;//{ return SAPI()->SciterSetStyleAttribute(he,name,value); }
func (e *Element) SetStyle(name, val string) error {
// args
cname := C.CString(name)
cval := C.LPCWSTR(StringToWcharPtr(val))
// cgo call
r := C.SciterSetStyleAttribute(e.handle, cname, cval)
C.free(unsafe.Pointer(cname))
return wrapDomResult(r, "SciterSetStyleAttribute")
}
// SCDOM_RESULT SciterGetElementLocation(HELEMENT he, LPRECT p_location, UINT areas /*ELEMENT_AREAS*/) ;//{ return SAPI()->SciterGetElementLocation(he,p_location,areas); }
// SCDOM_RESULT SciterScrollToView(HELEMENT he, UINT SciterScrollFlags) ;//{ return SAPI()->SciterScrollToView(he,SciterScrollFlags); }
// Scroll to view.
func (e *Element) ScrollToView(flag SCITER_SCROLL_FLAGS) error {