This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
/
sciter-x-dom.hpp
1378 lines (1192 loc) · 39.2 KB
/
sciter-x-dom.hpp
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
/*
* The Sciter Engine of Terra Informatica Software, Inc.
* http://sciter.com
*
* The code and information provided "as-is" without
* warranty of any kind, either expressed or implied.
*
* (C) 2003-2015, Terra Informatica Software, Inc.
*/
/*
* DOM access methods, C++ interface, sciter::dom namespace
*/
#ifndef __sciter_dom_hpp__
#define __sciter_dom_hpp__
#pragma warning(disable:4786) //identifier was truncated...
#pragma warning(disable:4100) //unreferenced formal parameter
#include "sciter-x-dom.h"
#include <algorithm>
#include <vector>
/**sciter namespace.*/
namespace sciter
{
/**dom namespace.*/
namespace dom
{
/**callback structure.
* Used with #sciter::dom::element::select() function.
**/
struct callback
{
/**Is called for every element that match criteria specified when calling to #sciter::dom::element::select() function.*/
virtual bool on_element(HELEMENT he) = 0;
};
class element;
/**DOM node - element, comment, text */
class node
{
HNODE hn;
protected:
void use(HNODE h) { hn = ( SciterNodeAddRef(h) == SCDOM_OK)? h: 0; }
void unuse() { if(hn) SciterNodeRelease(hn); hn = 0; }
void set(HNODE h) { unuse(); use(h); }
public:
node(): hn(0) {}
node(HNODE h) { use(h); }
node(HELEMENT h);
node(const element& el);
node(const node& n) { use(n.hn); }
~node() { unuse(); }
operator HNODE() const { return hn; }
operator bool() const { return hn != 0; }
node& operator = (HNODE h) { set(h); return *this; }
node& operator = (HELEMENT h);
node& operator = (const node& n) { set(n.hn); return *this; }
node& operator = (const element& n);
bool operator == (const node& rs ) const { return hn == rs.hn; }
bool operator == (HNODE rs ) const { return hn == rs; }
bool operator != (const node& rs ) const { return hn != rs.hn; }
bool operator != (HNODE rs ) const { return hn != rs; }
bool is_valid() const { return hn != 0; }
HELEMENT parent( ) const;
HNODE next_sibling() const; // sibling
HNODE prev_sibling() const; // sibling
HNODE first_child() const; // child
HNODE last_child() const; // child
unsigned int children_count() const;
HNODE operator[](unsigned int idx) const;
HNODE operator[](int idx) const { return this->operator[](unsigned(idx)); }
bool is_text() const;
bool is_comment() const;
bool is_element() const;
HELEMENT to_element() const;
// get/set characters of text/comment node
sciter::string text() const; // get text as sciter::string (utf16)
void text(LPCWSTR text, UINT textLength) const; // set text
void text(const sciter::string& s) { text(s.c_str(),UINT(s.length())); }
void remove(); // remove it from the DOM and release reference
void detach(); // detach it from the DOM and keep reference
static node make_text_node(LPCWSTR text, UINT textLength);
static node make_comment_node(LPCWSTR text, UINT textLength);
void append(HNODE hn); // as a last child node
void prepend(HNODE hn); // as a first child node
void insert_before(HNODE hn); // as a previous sibling
void insert_after(HNODE hn); // as a next sibling
// fetch DOM node reference from SCITER_VALUE envelope
static node from_value(const SCITER_VALUE& v) {
HNODE hn = 0;
SCDOM_RESULT r = SciterNodeUnwrap(&v, &hn);
assert(r == SCDOM_OK); (void)r;
return node(hn);
}
// wrap DOM node reference into sciter::value envelope
SCITER_VALUE to_value() const {
SCITER_VALUE v;
SCDOM_RESULT r = SciterNodeWrap(&v, hn);
assert(r == SCDOM_OK); (void)r;
return v;
}
};
/**DOM element.*/
class element
{
protected:
HELEMENT he;
void use(HELEMENT h) { he = ( Sciter_UseElement(h) == SCDOM_OK)? h: 0; }
void unuse() { if(he) Sciter_UnuseElement(he); he = 0; }
void set(HELEMENT h) { unuse(); use(h); }
public:
/**Construct \c undefined element .
**/
element(): he(0) { }
/**Construct \c element from existing element handle.
* \param h \b #HELEMENT
**/
element(HELEMENT h) { use(h); }
/**Copy constructor;
* \param e \b #element
**/
element(const element& e) { use(e.he); }
element(const node& e);
operator bool() const { return he != 0; }
operator HELEMENT() const { return he; }
operator HNODE() const {
HNODE hn = 0;
SciterNodeCastFromElement(he,&hn); // always succeedes
return hn;
}
/**Destructor.*/
~element() { unuse(); }
/**Assign \c element an \c #HELEMENT
* \param h \b #HELEMENT
* \return \b #element&
**/
element& operator = (HELEMENT h) { set(h); return *this; }
/**Assign \c element another \c #element
* \param e \b #element
* \return \b #element&
**/
element& operator = (const element& e) { set(e.he); return *this; }
/**Test equality of this and another \c #element's
* \param rs \b const \b #element
* \return \b bool, true if elements are equal, false otherwise
**/
bool operator == (const element& rs ) const { return he == rs.he; }
bool operator == (HELEMENT rs ) const { return he == rs; }
/**Test equality of this and another \c #element's
* \param rs \b const \b #element
* \return \b bool, true if elements are not equal, false otherwise
**/
bool operator != (const element& rs ) const { return he != rs.he; }
bool operator != (HELEMENT rs ) const { return he != rs; }
/**Test whether element is valid.
* \return \b bool, true if element is valid, false otherwise
**/
bool is_valid() const { return he != 0; }
/**Get number of child elements.
* \return \b int, number of child elements
**/
unsigned int children_count() const
{
UINT count = 0;
SciterGetChildrenCount(he, &count);
return count;
}
/**Get Nth child element.
* \param index \b unsigned \b int, number of the child element
* \return \b #HELEMENT, child element handle
**/
HELEMENT child( unsigned int index ) const
{
HELEMENT child = 0;
SciterGetNthChild(he, index, &child);
return child;
}
/**Get parent element.
* \return \b #HELEMENT, handle of the parent element
**/
HELEMENT parent( ) const
{
HELEMENT hparent = 0;
SciterGetParentElement(he, &hparent);
return hparent;
}
/**Get index of this element in its parent collection.
* \return \b unsigned \b int, index of this element in its parent collection
**/
unsigned int index( ) const
{
UINT index = 0;
SciterGetElementIndex(he, &index);
return index;
}
/**Get number of the attributes.
* \return \b unsigned \b int, number of the attributes
**/
unsigned int get_attribute_count( ) const
{
UINT n = 0;
SciterGetAttributeCount(he, &n);
return n;
}
/**Get attribute value by its index.
* \param n \b unsigned \b int, number of the attribute
* \return \b const \b WCHAR*, value of the n-th attribute
**/
sciter::string get_attribute( unsigned int n ) const
{
sciter::string s;
SCDOM_RESULT r = SciterGetNthAttributeValueCB(he, n, &_LPCWSTR2STRING, &s);
assert(r == SCDOM_OK); (void)r;
return s;
}
/**Get attribute name by its index.
* \param n \b unsigned \b int, number of the attribute
* \return \b const \b char*, name of the n-th attribute
**/
sciter::astring get_attribute_name( unsigned int n ) const
{
sciter::astring s;
SCDOM_RESULT r = SciterGetNthAttributeNameCB(he, n, &_LPCSTR2ASTRING, &s);
assert(r == SCDOM_OK); (void)r;
return s;
}
/**Get attribute value by name.
* \param name \b const \b char*, name of the attribute
* \return \b sciter::string, value of the n-th attribute
**/
sciter::string get_attribute( const char* name, const WCHAR* def_value = 0 ) const
{
sciter::string s;
SCDOM_RESULT r = SciterGetAttributeByNameCB(he, name, &_LPCWSTR2STRING, &s);
if(r == SCDOM_OK_NOT_HANDLED && def_value)
return sciter::string(def_value);
return s;
}
/**Add or replace attribute.
* \param name \b const \b char*, name of the attribute
* \param value \b const \b WCHAR*, name of the attribute
**/
void set_attribute( const char* name, const WCHAR* value )
{
SciterSetAttributeByName(he, name, value);
}
/**Get attribute integer value by name.
* \param name \b const \b char*, name of the attribute
* \return \b int , value of the attribute
**/
int get_attribute_int( const char* name, int def_val = 0 ) const
{
sciter::string txt = get_attribute(name);
if(txt.length() == 0) return def_val;
aux::wchars wc = aux::chars_of(txt);
return aux::to_int(wc);
}
/**Remove attribute.
* \param name \b const \b char*, name of the attribute
**/
void remove_attribute( const char* name )
{
SciterSetAttributeByName(he, name, 0);
}
/**Get style attribute of the element by its name.
* \param name \b const \b char*, name of the style attribute, e.g. "background-color"
* \return \b const \b WCHAR*, value of the style attribute
**/
sciter::string get_style_attribute( const char* name ) const
{
sciter::string s;
SciterGetStyleAttributeCB(he, name,&_LPCWSTR2STRING, &s);
return s;
}
/**Set style attribute.
* \param name \b const \b char*, name of the style attribute
* \param value \b const \b WCHAR*, value of the style attribute
*
* \par Example:
* \code e.set_style_attribute("background-color", L"red"); \endcode
**/
void set_style_attribute( const char* name, const WCHAR* value ) const
{
SciterSetStyleAttribute(he, name, value);
}
/**Get root DOM element of the Sciter document.
* \param hSciterWnd \b HWINDOW, Sciter window
* \return \b #HELEMENT, root element
* \see also \b #root
**/
static HELEMENT root_element(HWINDOW hSciterWnd)
{
HELEMENT h = 0;
SciterGetRootElement(hSciterWnd,&h);
return h;
}
/**Get focus DOM element of the Sciter document.
* \param hSciterWnd \b HWINDOW, Sciter window
* \return \b #HELEMENT, focus element
*
* COMMENT: to set focus use: set_state(STATE_FOCUS)
*
**/
static HELEMENT focus_element(HWINDOW hSciterWnd)
{
HELEMENT h = 0;
SciterGetFocusElement(hSciterWnd,&h);
return h;
}
/**Find DOM element of the Sciter document by coordinates.
* \param hSciterWnd \b HWINDOW, Sciter window
* \param clientPt \b POINT, coordinates.
* \return \b #HELEMENT, found element handle or zero
**/
static HELEMENT find_element(HWINDOW hSciterWnd, POINT clientPt)
{
HELEMENT h = 0;
SciterFindElement(hSciterWnd, clientPt, &h);
return h;
}
/**Set mouse capture.
* After call to this function all mouse events will be targeted to this element.
* To remove mouse capture call #sciter::dom::element::release_capture().
**/
void set_capture() { SciterSetCapture(he); }
/**Release mouse capture.
* Mouse capture can be set with #element:set_capture()
**/
void release_capture() { SciterReleaseCapture(he); }
inline static SBOOL SC_CALLBACK callback_func( HELEMENT he, LPVOID param )
{
callback *pcall = (callback *)param;
return (SBOOL)pcall->on_element(he); // SBOOL(true) - stop enumeration
}
inline void select_elements( callback *pcall,
const char* selectors // CSS selectors, comma separated list
) const
{
SciterSelectElements( he, selectors, callback_func, pcall);
}
/**Get element by id.
* \param id \b char*, value of the "id" attribute.
* \return \b #HELEMENT, handle of the first element with the "id" attribute equal to given.
**/
HELEMENT get_element_by_id(const char* id) const
{
if(!id) return 0;
return find_first( "[id='%s']", id );
}
HELEMENT get_element_by_id(const WCHAR* id) const
{
if(!id) return 0;
return find_first( "[id='%S']", id );
}
/**Apply changes and refresh element area in its window.
* \param[in] render_now \b bool, if true element will be redrawn immediately.
**/
void update( bool render_now = false ) const
{
SciterUpdateElement(he, (SBOOL)render_now);
}
void refresh( RECT rc ) const
{
SciterRefreshElementArea(he, rc);
}
void refresh( ) const
{
RECT rc = get_location(SELF_RELATIVE | CONTENT_BOX);
refresh( rc );
}
/**Get next sibling element.
* \return \b #HELEMENT, handle of the next sibling element if it exists or 0 otherwise
**/
HELEMENT next_sibling() const
{
unsigned int idx = index() + 1;
element pel = parent();
if(!pel.is_valid())
return 0;
if( idx >= pel.children_count() )
return 0;
return pel.child(idx);
}
/**Get previous sibling element.
* \return \b #HELEMENT, handle of previous sibling element if it exists or 0 otherwise
**/
HELEMENT prev_sibling() const
{
int idx = static_cast<int>(index()) - 1;
element pel = parent();
if(!pel.is_valid())
return 0;
if( idx < 0 )
return 0;
return pel.child(static_cast<unsigned>(idx));
}
/**Get first sibling element.
* \return \b #HELEMENT, handle of the first sibling element if it exists or 0 otherwise
**/
HELEMENT first_sibling() const
{
element pel = parent();
if(!pel.is_valid())
return 0;
return pel.child(0);
}
/**Get last sibling element.
* \return \b #HELEMENT, handle of last sibling element if it exists or 0 otherwise
**/
HELEMENT last_sibling() const
{
element pel = parent();
if(!pel.is_valid())
return 0;
return pel.child(pel.children_count() - 1);
}
/**Get root of the element
* \return \b #HELEMENT, handle of document root element (html)
**/
HELEMENT root() const
{
element pel = parent();
if(pel.is_valid())
return pel.root();
return he;
}
/**Get bounding rectangle of the element.
* \param root_relative \b bool, if true function returns location of the
* element relative to Sciter window, otherwise the location is given
* relative to first scrollable container.
* \return \b RECT, bounding rectangle of the element.
**/
RECT get_location(unsigned int area = ROOT_RELATIVE | CONTENT_BOX) const
{
RECT rc = {0,0,0,0};
SciterGetElementLocation(he,&rc, area);
return rc;
}
/** Test if point is inside shape rectangle of the element.
client_pt - client rect relative point
**/
bool is_inside( POINT client_pt ) const
{
RECT rc = get_location(ROOT_RELATIVE | BORDER_BOX);
return client_pt.x >= rc.left
&& client_pt.x < rc.right
&& client_pt.y >= rc.top
&& client_pt.y < rc.bottom;
}
/**Scroll this element to view.
**/
void scroll_to_view(bool toTopOfView = false, bool smooth = false)
{
UINT flags = 0;
if(toTopOfView) flags |= SCROLL_TO_TOP;
if(smooth) flags |= SCROLL_SMOOTH;
SciterScrollToView(he, flags );
}
void get_scroll_info(POINT& scroll_pos, RECT& view_rect, SIZE& content_size)
{
SCDOM_RESULT r = SciterGetScrollInfo(he, &scroll_pos, &view_rect, &content_size);
assert(r == SCDOM_OK); (void)r;
}
void set_scroll_pos(POINT scroll_pos)
{
SCDOM_RESULT r = SciterSetScrollPos(he, scroll_pos, true);
assert(r == SCDOM_OK); (void)r;
}
/** get min-intrinsic and max-intrinsic widths of the element. */
void get_intrinsic_widths(int& min_width,int& max_width)
{
SCDOM_RESULT r = SciterGetElementIntrinsicWidths(he, &min_width, &max_width);
assert(r == SCDOM_OK); (void)r;
}
/** get min-intrinsic height of the element calculated for forWidth. */
void get_intrinsic_height(int for_width, int& min_height)
{
SCDOM_RESULT r = SciterGetElementIntrinsicHeight(he, for_width, &min_height);
assert(r == SCDOM_OK); (void)r;
}
/**Get element's tag name.
* \return \b sciter::astring, tag name of the element
*
* \par Example:
* For <div> tag function will return "div".
**/
sciter::astring get_element_type() const
{
sciter::astring s;
SciterGetElementTypeCB(he, &_LPCSTR2ASTRING, &s);
return s;
}
// alias
sciter::astring get_tag() const { return get_element_type(); }
/**Get HWINDOW of containing window.
* \param root_window \b bool, handle of which window to get:
* - true - Sciter window
* - false - nearest windowed parent element.
* \return \b HWINDOW
**/
HWINDOW get_element_hwnd(bool root_window) const
{
HWINDOW hwnd = 0;
SciterGetElementHwnd(he,&hwnd, (SBOOL)root_window);
return hwnd;
}
void attach_hwnd(HWINDOW child)
{
SCDOM_RESULT r = SciterAttachHwndToElement(he,child);
assert( r == SCDOM_OK ); (void)r;
}
/**Get element UID - identifier suitable for storage.
* \return \b UID
**/
UINT get_element_uid() const
{
UINT uid = 0;
SciterGetElementUID(he,&uid);
return uid;
}
/**Get element handle by its UID.
* \param hSciterWnd \b HWINDOW, Sciter window
* \param uid \b UINT, uid of the element
* \return \b #HELEMENT, handle of element with the given uid or 0 if not found
**/
static HELEMENT element_by_uid(HWINDOW hSciterWnd, UINT uid)
{
HELEMENT h = 0;
SciterGetElementByUID(hSciterWnd, uid,&h);
return h;
}
/**Combine given URL with URL of the document element belongs to.
* \param[in, out] inOutURL \b LPWSTR, at input this buffer contains
* zero-terminated URL to be combined, after function call it contains
* zero-terminated combined URL
* \param bufferSize \b UINT, size of the buffer pointed by \c inOutURL
**/
void combine_url(LPWSTR inOutURL, UINT bufferSize) const
{
SciterCombineURL(he,inOutURL,bufferSize);
}
#ifdef CPP11
sciter::string combine_url(const sciter::string& relative_url) const
{
WCHAR buffer[4096] = {0};
//wcsncpy_s(buffer,relative_url,min(4096,relative_url.length()));
std::size_t length = relative_url.length() < 4095u ? relative_url.length(): 4095u ;
std::copy_n(relative_url.cbegin(), length, buffer);
buffer[length] = 0;
SciterCombineURL(he,buffer,4096);
return sciter::string(buffer);
}
#endif
/**Set inner or outer html of the element.
* \param html \b const \b unsigned \b char*, UTF-8 encoded string containing html text
* \param html_length \b size_t, length in bytes of \c html
* \param where \b int, possible values are:
* - SIH_REPLACE_CONTENT - replace content of the element
* - SIH_INSERT_AT_START - insert html before first child of the element
* - SIH_APPEND_AFTER_LAST - insert html after last child of the element
**/
void set_html( const unsigned char* html, size_t html_length, int where = SIH_REPLACE_CONTENT)
{
if(html == 0 || html_length == 0)
clear();
else
{
SCDOM_RESULT r = SciterSetElementHtml(he, html, UINT(html_length), where);
assert(r == SCDOM_OK); (void)r;
}
}
// html as utf8 bytes sequence
sciter::astring
get_html( bool outer = true) const
{
sciter::astring s;
SCDOM_RESULT r = SciterGetElementHtmlCB(he, SBOOL(outer), &_LPCBYTE2ASTRING,&s);
assert(r == SCDOM_OK); (void)r;
return s;
}
// get text as sciter::string (utf16)
sciter::string text() const
{
sciter::string s;
SCDOM_RESULT r = SciterGetElementTextCB(he, &_LPCWSTR2STRING, &s);
assert(r == SCDOM_OK); (void)r;
return s;
}
void set_text(const WCHAR* utf16, size_t utf16_length)
{
SCDOM_RESULT r = SciterSetElementText(he, utf16, UINT(utf16_length));
assert(r == SCDOM_OK); (void)r;
}
void set_text(const WCHAR* t)
{
assert(t);
if( t ) set_text( t, str_length(t) );
}
void clear() // clears content of the element
{
SCDOM_RESULT r = SciterSetElementText(he, 0, 0);
assert(r == SCDOM_OK); (void)r;
}
HELEMENT find_first( const char* selector, ... ) const
{
char buffer[2049]; buffer[0]=0;
va_list args;
va_start ( args, selector );
vsnprintf( buffer, sizeof(buffer), selector, args );
va_end ( args );
find_first_callback find_first;
select_elements( &find_first, buffer); // find first element satisfying given CSS selector
//assert(find_first.hfound);
return find_first.hfound;
}
void find_all( callback* cb, const char* selector, ... ) const
{
char buffer[2049]; buffer[0]=0;
va_list args;
va_start ( args, selector );
vsnprintf( buffer, sizeof(buffer), selector, args );
va_end ( args );
select_elements( cb, buffer); // find all elements satisfying given CSS selector
//assert(find_first.hfound);
}
#ifdef CPP11
std::vector<sciter::dom::element>
find_all(const char* selector, ...) const
{
struct each_callback : public sciter::dom::callback
{
std::vector<sciter::dom::element> elements;
virtual bool on_element(HELEMENT he) {
elements.push_back(sciter::dom::element(he));
return false; // no stop
}
};
each_callback cb;
this->find_all(&cb, selector);
return cb.elements;
}
#endif
// will find first parent satisfying given css selector(s)
HELEMENT find_nearest_parent(const char* selector, ...) const
{
char buffer[2049]; buffer[0]=0;
va_list args;
va_start ( args, selector );
vsnprintf( buffer, sizeof(buffer), selector, args );
va_end ( args );
HELEMENT heFound = 0;
SCDOM_RESULT r = SciterSelectParent(he, buffer, 0, &heFound);
assert(r == SCDOM_OK); (void)r;
return heFound;
}
// test this element against CSS selector(s)
bool test(const char* selector, ...) const
{
char buffer[2049]; buffer[0]=0;
va_list args;
va_start ( args, selector );
vsnprintf( buffer, sizeof(buffer), selector, args );
va_end ( args );
HELEMENT heFound = 0;
SCDOM_RESULT r = SciterSelectParent(he, buffer, 1, &heFound);
assert(r == SCDOM_OK); (void)r;
return heFound != 0;
}
/**Get UI state bits of the element as set of ELEMENT_STATE_BITS
**/
unsigned int get_state() const
{
UINT state = 0;
SCDOM_RESULT r = SciterGetElementState(he,&state);
assert(r == SCDOM_OK); (void)r;
return state; /*ELEMENT_STATE_BITS*/
}
/**Checks if particular UI state bits are set in the element.
**/
bool get_state(/*ELEMENT_STATE_BITS*/ unsigned int bits) const
{
UINT state = 0;
SCDOM_RESULT r = SciterGetElementState(he,&state);
assert(r == SCDOM_OK); (void)r;
return (state & bits) != 0;
}
/**Set UI state of the element with optional view update.
**/
void set_state(
/*ELEMENT_STATE_BITS*/ unsigned int bitsToSet,
/*ELEMENT_STATE_BITS*/ unsigned int bitsToClear = 0, bool update = true )
{
SCDOM_RESULT r = SciterSetElementState(he,bitsToSet,bitsToClear, SBOOL(update));
assert(r == SCDOM_OK); (void)r;
}
/** "deeply enabled" **/
bool enabled()
{
SBOOL b = false;
SCDOM_RESULT r = SciterIsElementEnabled(he,&b);
assert(r == SCDOM_OK); (void)r;
return b != 0;
}
/** "deeply visible" **/
bool visible()
{
SBOOL b = false;
SCDOM_RESULT r = SciterIsElementVisible(he,&b);
assert(r == SCDOM_OK); (void)r;
return b != 0;
}
void start_timer(unsigned int ms, void* timer_id = 0)
{
SCDOM_RESULT r = SciterSetTimer(he,ms,UINT_PTR(timer_id));
assert(r == SCDOM_OK); (void)r;
}
void stop_timer(void* timer_id = 0)
{
if(he)
{
SCDOM_RESULT r = SciterSetTimer(he,0,UINT_PTR(timer_id));
assert(r == SCDOM_OK); (void)r;
}
}
/** create brand new element with text (optional).
Example:
element div = element::create("div");
- will create DIV element,
element opt = element::create("option",L"Europe");
- will create OPTION element with text "Europe" in it.
**/
static element create(const char* tagname, const WCHAR* text = 0)
{
element e(0);
SCDOM_RESULT r = SciterCreateElement( tagname, text, &e.he ); // don't need 'use' here, as it is already "addrefed"
assert(r == SCDOM_OK); (void)r;
return e;
}
/** create brand new copy of this element. Element will be created disconected.
You need to call insert to inject it in some container.
Example:
element select = ...;
element option1 = ...;
element option2 = option1.clone();
select.insert(option2, option1.index() + 1);
- will create copy of option1 element (option2) and insert it after option1,
**/
element clone()
{
element e(0);
SCDOM_RESULT r = SciterCloneElement( he, &e.he ); // don't need 'use' here, as it is already "addrefed"
assert(r == SCDOM_OK); (void)r;
return e;
}
/** Insert element e at \i index position of this element.
**/
void insert( const element& e, unsigned int index )
{
SCDOM_RESULT r = SciterInsertElement( e.he, this->he, index );
assert(r == SCDOM_OK); (void)r;
}
/** Append element e as last child of this element.
**/
void append( const element& e ) { insert(e,0x7FFFFFFF); }
/** detach - remove this element from its parent
**/
void detach()
{
SCDOM_RESULT r = SciterDetachElement( he );
assert(r == SCDOM_OK); (void)r;
}
/** destroy - remove this element from its parent and destroy all behaviors
**/
void destroy()
{
HELEMENT t = he; he = 0;
SCDOM_RESULT r = SciterDeleteElement( t );
assert(r == SCDOM_OK); (void)r;
}
/** swap two elements in the DOM
**/
void swap(HELEMENT with)
{
SCDOM_RESULT r = SciterSwapElements(he, with);
assert(r == SCDOM_OK); (void)r;
}
/** traverse event - send it by sinking/bubbling on the
* parent/child chain of this element
**/
bool send_event(unsigned int event_code, uintptr_t reason = 0, HELEMENT heSource = 0)
{
SBOOL handled = false;
SCDOM_RESULT r = SciterSendEvent(he, event_code, heSource? heSource: he, reason, &handled);
assert(r == SCDOM_OK); (void)r;
return handled != 0;
}
/** post event - post it in the queue for later sinking/bubbling on the
* parent/child chain of this element.
* method returns immediately
**/
void post_event(unsigned int event_code, uintptr_t reason = 0, HELEMENT heSource = 0)
{
SCDOM_RESULT r = SciterPostEvent(he, event_code, heSource? heSource: he, reason);
assert(r == SCDOM_OK); (void)r;
}
bool fire_event(const BEHAVIOR_EVENT_PARAMS& evt, bool post = true)
{
SBOOL handled = false;
SCDOM_RESULT r = SciterFireEvent(&evt, post, &handled);
assert(r == SCDOM_OK); (void)r;
return handled != 0;
}
/** call method, invokes method in all event handlers attached to the element
**/
bool call_behavior_method(METHOD_PARAMS* p)
{
if(!is_valid())
return false;
return SciterCallBehaviorMethod(he,p) == SCDOM_OK;
}
void load_html(const WCHAR* url, HELEMENT initiator = 0)
{
load_data(url,RT_DATA_HTML, initiator);
}
void load_data(const WCHAR* url, UINT dataType, HELEMENT initiator = 0)
{
SCDOM_RESULT r = SciterRequestElementData(he,url, dataType, initiator);
assert(r == SCDOM_OK); (void)r;
}
struct comparator
{
virtual int compare(const sciter::dom::element& e1, const sciter::dom::element& e2) = 0;
static INT SC_CALLBACK scmp( HELEMENT he1, HELEMENT he2, LPVOID param )
{
sciter::dom::element::comparator* self =
static_cast<sciter::dom::element::comparator*>(param);
sciter::dom::element e1 = he1;
sciter::dom::element e2 = he2;
return self->compare( e1,e2 );
}
};
/** reorders children of the element using sorting order defined by cmp
**/
void sort( comparator& cmp, int start = 0, int end = -1 )
{
if (end == -1)
end = children_count();
SCDOM_RESULT r = SciterSortElements(he, start, end, &comparator::scmp, &cmp);
assert(r == SCDOM_OK); (void)r;
}
// "manually" attach event_handler proc to the DOM element
void attach_event_handler(event_handler* p_event_handler )
{
SciterAttachEventHandler(he, &event_handler_raw::element_proc, static_cast<event_handler_raw*>(p_event_handler));
}
void detach_event_handler(event_handler* p_event_handler )
{
SciterDetachEventHandler(he, &event_handler_raw::element_proc, static_cast<event_handler_raw*>(p_event_handler));
}
// call scripting method attached to the element (directly or through scripting behavior)
// Example, script:
// var elem = ...
// elem.foo = function() {...}
// Native code: