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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
use ::{_API};
use capi::sctypes::*;
use value::Value;
pub use capi::scdom::{SCDOM_RESULT, HELEMENT, SET_ELEMENT_HTML, ELEMENT_AREAS};
use capi::scbehavior::{CLICK_REASON, BEHAVIOR_EVENTS, BEHAVIOR_EVENT_PARAMS};
pub use dom::event::EventHandler;
pub use dom::event::EventReason;
pub type Result<T> = ::std::result::Result<T, SCDOM_RESULT>;
macro_rules! HELEMENT {
() => { ::std::ptr::null_mut() }
}
macro_rules! ok_or {
($rv:expr, $ok:ident) => {
if $ok == SCDOM_RESULT::OK {
Ok($rv)
} else {
Err($ok)
}
};
($rv:expr, $ok:ident, $skip_not_handled:expr) => {
if $ok == SCDOM_RESULT::OK || ($ok == $skip_not_handled) {
Ok($rv)
} else {
Err($ok)
}
};
}
trait ElementVisitor {
fn on_element(&mut self, el: Element) -> bool;
fn result(&self) -> Vec<Element>;
}
#[derive(Default)]
struct FindFirstElement {
all: Vec<Element>,
}
impl ElementVisitor for FindFirstElement {
fn on_element(&mut self, el: Element) -> bool {
self.all.push(el);
return true;
}
fn result(&self) -> Vec<Element> {
self.all.clone()
}
}
#[derive(Default)]
struct FindAllElements {
all: Vec<Element>,
}
impl ElementVisitor for FindAllElements {
fn on_element(&mut self, el: Element) -> bool {
self.all.push(el);
return false;
}
fn result(&self) -> Vec<Element> {
self.all.clone()
}
}
#[derive(PartialEq)]
pub struct Element {
he: HELEMENT,
}
unsafe impl Send for Element {}
unsafe impl Sync for Element {}
impl Element {
pub fn from(he: HELEMENT) -> Element {
Element { he: Element::use_or(he) }
}
pub fn create(tag: &str) -> Result<Element> {
let mut e = Element { he: HELEMENT!() };
let (tag,_) = s2u!(tag);
let text = 0 as LPCWSTR;
let ok = (_API.SciterCreateElement)(tag.as_ptr(), text, &mut e.he);
ok_or!(e, ok)
}
pub fn with_parent(tag: &str, parent: &mut Element) -> Result<Element> {
let mut e = Element { he: HELEMENT!() };
let (tag,_) = s2u!(tag);
let text = 0 as LPCWSTR;
(_API.SciterCreateElement)(tag.as_ptr(), text, &mut e.he);
let ok = parent.append(&e);
ok.map(|_| e)
}
#[deprecated(since="0.5.0", note="please use `Element::with_parent()` instead.")]
pub fn create_at(tag: &str, parent: &mut Element) -> Result<Element> {
Element::with_parent(tag, parent)
}
pub fn with_text(tag: &str, text: &str) -> Result<Element> {
let mut e = Element { he: HELEMENT!() };
let (tag,_) = s2u!(tag);
let (text,_) = s2w!(text);
let ok = (_API.SciterCreateElement)(tag.as_ptr(), text.as_ptr(), &mut e.he);
ok_or!(e, ok)
}
pub fn with_type(tag: &str, el_type: &str) -> Result<Element> {
let mut e = Element { he: HELEMENT!() };
let (tag,_) = s2u!(tag);
let text = 0 as LPCWSTR;
let ok = (_API.SciterCreateElement)(tag.as_ptr(), text, &mut e.he);
if ok == SCDOM_RESULT::OK {
let r = e.set_attribute("type", el_type);
r.map(|_| e)
} else {
Err(ok)
}
}
pub fn from_window(hwnd: HWINDOW) -> Result<Element> {
let mut p = HELEMENT!();
let ok = (_API.SciterGetRootElement)(hwnd, &mut p);
ok_or!(Element::from(p), ok)
}
pub fn from_focus(hwnd: HWINDOW) -> Result<Element> {
let mut p = HELEMENT!();
let ok = (_API.SciterGetFocusElement)(hwnd, &mut p);
ok_or!(Element::from(p), ok)
}
pub fn from_highlighted(hwnd: HWINDOW) -> Result<Element> {
let mut p = HELEMENT!();
let ok = (_API.SciterGetHighlightedElement)(hwnd, &mut p);
ok_or!(Element::from(p), ok)
}
pub fn from_point(hwnd: HWINDOW, pt: POINT) -> Result<Element> {
let mut p = HELEMENT!();
let ok = (_API.SciterFindElement)(hwnd, pt, &mut p);
ok_or!(Element::from(p), ok)
}
pub fn from_uid(hwnd: HWINDOW, uid: u32) -> Result<Element> {
let mut p = HELEMENT!();
let ok = (_API.SciterGetElementByUID)(hwnd, uid, &mut p);
ok_or!(Element::from(p), ok)
}
fn use_or(he: HELEMENT) -> HELEMENT {
let ok = (_API.Sciter_UseElement)(he);
if ok == SCDOM_RESULT::OK {
he
} else {
HELEMENT!()
}
}
pub fn as_ptr(&self) -> HELEMENT {
self.he
}
pub fn get_uid(&self) -> u32 {
let mut n = 0;
(_API.SciterGetElementUID)(self.he, &mut n);
return n;
}
pub fn get_tag(&self) -> String {
let mut s = String::new();
(_API.SciterGetElementTypeCB)(self.he, store_astr, &mut s as *mut String as LPVOID);
return s;
}
pub fn get_text(&self) -> String {
let mut s = String::new();
(_API.SciterGetElementTextCB)(self.he, store_wstr, &mut s as *mut String as LPVOID);
return s;
}
pub fn set_text(&mut self, text: &str) -> Result<()> {
let (s,n) = s2w!(text);
let ok = (_API.SciterSetElementText)(self.he, s.as_ptr(), n);
ok_or!((), ok)
}
pub fn get_html(&self, with_outer_html: bool) -> Vec<u8> {
let mut s = Vec::new();
(_API.SciterGetElementHtmlCB)(self.he, with_outer_html as BOOL, store_bstr, &mut s as *mut Vec<u8> as LPVOID);
return s;
}
pub fn set_html(&mut self, html: &[u8], how: Option<SET_ELEMENT_HTML>) -> Result<()> {
if html.is_empty() {
return self.clear();
}
let ok = (_API.SciterSetElementHtml)(self.he, html.as_ptr(), html.len() as UINT, how.unwrap_or(SET_ELEMENT_HTML::SIH_REPLACE_CONTENT) as UINT);
ok_or!((), ok)
}
pub fn get_value(&self) -> Value {
let mut rv = Value::new();
(_API.SciterGetValue)(self.he, rv.as_ptr());
return rv;
}
pub fn set_value<T: Into<Value>>(&mut self, val: T) -> Result<()> {
let ok = (_API.SciterSetValue)(self.he, val.into().as_cptr());
ok_or!((), ok)
}
pub fn get_hwnd(&self, for_root: bool) -> HWINDOW {
let mut hwnd: HWINDOW = ::std::ptr::null_mut();
(_API.SciterGetElementHwnd)(self.he, &mut hwnd as *mut HWINDOW, for_root as BOOL);
return hwnd;
}
pub fn get_location(&self, kind: u32) -> Result<RECT> {
let mut rc = RECT::default();
let ok = (_API.SciterGetElementLocation)(self.he, &mut rc as *mut _, kind as u32);
ok_or!(rc, ok)
}
pub fn send_event(&self, code: BEHAVIOR_EVENTS, reason: Option<CLICK_REASON>, source: Option<HELEMENT>) -> Result<bool> {
let mut handled = false as BOOL;
let r = reason.unwrap_or(CLICK_REASON::SYNTHESIZED);
let s = source.unwrap_or(self.he);
let ok = (_API.SciterSendEvent)(self.he, code as u32, s, r as u32, &mut handled);
ok_or!(handled != 0, ok)
}
pub fn post_event(&self, code: BEHAVIOR_EVENTS, reason: Option<CLICK_REASON>, source: Option<HELEMENT>) -> Result<()> {
let r = reason.unwrap_or(CLICK_REASON::SYNTHESIZED);
let s = source.unwrap_or(self.he);
let ok = (_API.SciterPostEvent)(self.he, code as u32, s, r as u32);
ok_or!((), ok)
}
pub fn fire_event(&self, code: BEHAVIOR_EVENTS, reason: Option<CLICK_REASON>, source: Option<HELEMENT>, post: bool, data: Option<Value>) -> Result<bool> {
let mut handled = false as BOOL;
let mut params = BEHAVIOR_EVENT_PARAMS {
cmd: code as UINT,
reason: reason.unwrap_or(CLICK_REASON::SYNTHESIZED) as UINT_PTR,
he: source.unwrap_or(self.he),
heTarget: self.he,
data: Default::default(),
};
if data.is_some() {
data.unwrap().pack_to(&mut params.data);
}
let ok = (_API.SciterFireEvent)(¶ms, post as BOOL, &mut handled);
ok_or!(handled != 0, ok)
}
pub fn fire_event_params(evt: &BEHAVIOR_EVENT_PARAMS, post: bool) -> Result<bool> {
let mut handled = false as BOOL;
let ok = (_API.SciterFireEvent)(evt, post as BOOL, &mut handled);
ok_or!(handled != 0, ok)
}
pub fn eval_script(&self, script: &str) -> Result<Value> {
let mut rv = Value::new();
let (s,n) = s2w!(script);
let ok = (_API.SciterEvalElementScript)(self.he, s.as_ptr(), n, rv.as_ptr());
return ok_or!(rv, ok, SCDOM_RESULT::OPERATION_FAILED);
}
pub fn call_function(&self, name: &str, args: &[Value]) -> Result<Value> {
let mut rv = Value::new();
let (name,_) = s2u!(name);
let argv = Value::pack_args(args);
let ok = (_API.SciterCallScriptingFunction)(self.he, name.as_ptr(), argv.as_ptr(), argv.len() as UINT, rv.as_ptr());
return ok_or!(rv, ok, SCDOM_RESULT::OPERATION_FAILED);
}
pub fn call_method(&self, name: &str, args: &[Value]) -> Result<Value> {
let mut rv = Value::new();
let (name,_) = s2u!(name);
let argv = Value::pack_args(args);
let ok = (_API.SciterCallScriptingMethod)(self.he, name.as_ptr(), argv.as_ptr(), argv.len() as UINT, rv.as_ptr());
return ok_or!(rv, ok, SCDOM_RESULT::OPERATION_FAILED);
}
pub fn attribute_count(&self) -> usize {
let mut n = 0u32;
(_API.SciterGetAttributeCount)(self.he, &mut n);
return n as usize;
}
pub fn attribute_name(&self, index: usize) -> String {
let mut s = String::new();
(_API.SciterGetNthAttributeNameCB)(self.he, index as UINT, store_astr, &mut s as *mut String as LPVOID);
return s;
}
pub fn attribute(&self, index: usize) -> String {
let mut s = String::new();
(_API.SciterGetNthAttributeValueCB)(self.he, index as UINT, store_wstr, &mut s as *mut String as LPVOID);
return s;
}
pub fn get_attribute(&self, name: &str) -> Option<String> {
let mut s = String::new();
let (name,_) = s2u!(name);
let ok = (_API.SciterGetAttributeByNameCB)(self.he, name.as_ptr(), store_wstr, &mut s as *mut String as LPVOID);
match ok {
SCDOM_RESULT::OK => Some(s),
_ => None,
}
}
pub fn set_attribute(&mut self, name: &str, value: &str) -> Result<()> {
let (name,_) = s2u!(name);
let (value,_) = s2w!(value);
let ok = (_API.SciterSetAttributeByName)(self.he, name.as_ptr(), value.as_ptr());
ok_or!((), ok)
}
pub fn remove_attribute(&mut self, name: &str) -> Result<()> {
let (name,_) = s2u!(name);
let value = ::std::ptr::null();
let ok = (_API.SciterSetAttributeByName)(self.he, name.as_ptr(), value);
ok_or!((), ok)
}
pub fn toggle_attribute(&mut self, name: &str, isset: bool, value: Option<&str>) -> Result<()> {
if isset {
self.set_attribute(name, value.unwrap())
} else {
self.remove_attribute(name)
}
}
pub fn clear_attributes(&mut self) -> Result<()> {
let ok = (_API.SciterClearAttributes)(self.he);
ok_or!((), ok)
}
pub fn get_style_attribute(&self, name: &str) -> String {
let mut s = String::new();
let (name,_) = s2u!(name);
(_API.SciterGetStyleAttributeCB)(self.he, name.as_ptr(), store_wstr, &mut s as *mut String as LPVOID);
return s;
}
pub fn set_style_attribute(&mut self, name: &str, value: &str) -> Result<()> {
let (name,_) = s2u!(name);
let (value,_) = s2w!(value);
let ok = (_API.SciterSetStyleAttribute)(self.he, name.as_ptr(), value.as_ptr());
ok_or!((), ok)
}
pub fn index(&self) -> usize {
let mut n = 0u32;
(_API.SciterGetElementIndex)(self.he, &mut n as *mut UINT);
return n as usize;
}
pub fn root(&self) -> Element {
if let Some(dad) = self.parent() {
dad.root()
} else {
self.clone()
}
}
pub fn parent(&self) -> Option<Element> {
let mut p = HELEMENT!();
(_API.SciterGetParentElement)(self.he, &mut p);
if p.is_null() {
None
} else {
Some(Element::from(p))
}
}
pub fn first_sibling(&self) -> Option<Element> {
if let Some(dad) = self.parent() {
let count = dad.len();
if count > 0 {
return dad.child(0);
}
}
None
}
pub fn last_sibling(&self) -> Option<Element> {
if let Some(dad) = self.parent() {
let count = dad.len();
if count > 0 {
return dad.child(count - 1);
}
}
None
}
pub fn next_sibling(&self) -> Option<Element> {
let idx = self.index() + 1;
if let Some(dad) = self.parent() {
let count = dad.len();
if idx < count {
return dad.child(idx);
}
}
None
}
pub fn prev_sibling(&self) -> Option<Element> {
let idx = self.index();
if let Some(dad) = self.parent() {
let count = dad.len();
if idx > 0 && (idx - 1) < count {
return dad.child(idx - 1);
}
}
None
}
pub fn first_child(&self) -> Option<Element> {
return self.child(0);
}
pub fn last_child(&self) -> Option<Element> {
let count = self.len();
if count > 0 {
return self.child(count - 1);
}
None
}
pub fn get(&self, index: usize) -> Option<Element> {
return self.child(index);
}
pub fn children(&self) -> Children {
Children {
base: self,
index: 0,
count: self.children_count(),
}
}
pub fn child(&self, index: usize) -> Option<Element> {
let mut p = HELEMENT!();
let ok = (_API.SciterGetNthChild)(self.he, index as UINT, &mut p);
match ok {
SCDOM_RESULT::OK => Some(Element::from(p)),
_ => None
}
}
pub fn children_count(&self) -> usize {
let mut n = 0u32;
(_API.SciterGetChildrenCount)(self.he, &mut n as *mut UINT);
return n as usize;
}
pub fn len(&self) -> usize {
return self.children_count();
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn clear(&mut self) -> Result<()> {
let ok = (_API.SciterSetElementText)(self.he, ::std::ptr::null(), 0);
ok_or!((), ok)
}
pub fn clone_element(&self) -> Element {
let mut e = Element { he: HELEMENT!() };
(_API.SciterCloneElement)(self.he, &mut e.he);
return e;
}
pub fn insert(&mut self, index: usize, child: &Element) -> Result<()> {
let ok = (_API.SciterInsertElement)(child.he, self.he, index as UINT);
ok_or!((), ok)
}
pub fn append(&mut self, child: &Element) -> Result<()> {
self.insert(0x7FFF_FFFF, child)
}
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
pub fn push(&mut self, element: Element) {
self.append(&element).expect("Could not append element.");
}
pub fn pop(&mut self) -> Option<Element> {
let count = self.len();
if count > 0 {
if let Some(mut child) = self.get(count - 1) {
child.detach().expect("Could not detach element.");
return Some(child);
}
}
return None;
}
pub fn detach(&mut self) -> Result<()> {
let ok = (_API.SciterDetachElement)(self.he);
ok_or!((), ok)
}
pub fn destroy(&mut self) -> Result<()> {
let mut p = HELEMENT!();
::std::mem::swap(&mut self.he, &mut p);
let ok = (_API.SciterDeleteElement)(p);
ok_or!((), ok)
}
pub fn swap(&mut self, other: &mut Element) -> Result<()> {
let ok = (_API.SciterSwapElements)(self.he, other.he);
ok_or!((), ok)
}
pub fn test(&self, selector: &str) -> bool {
let mut p = HELEMENT!();
let (s,_) = s2u!(selector);
(_API.SciterSelectParent)(self.he, s.as_ptr(), 1, &mut p);
return !p.is_null();
}
fn select_elements<T: ElementVisitor>(&self, selector: &str, callback: T) -> Result<Vec<Element>> {
use ::capi::schandler::NativeHandler;
extern "system" fn inner<T: ElementVisitor>(he: HELEMENT, param: LPVOID) -> BOOL {
let obj = NativeHandler::get_data::<T>(¶m);
let e = Element::from(he);
let stop = obj.on_element(e);
return stop as BOOL;
}
let (s,_) = s2u!(selector);
let handler = NativeHandler::from(callback);
let ok = (_API.SciterSelectElements)(self.he, s.as_ptr(), inner::<T>, handler.as_mut_ptr());
if ok != SCDOM_RESULT::OK {
return Err(ok);
}
let obj = handler.as_ref::<T>();
return Ok(obj.result());
}
pub fn find_nearest_parent(&self, selector: &str) -> Result<Option<Element>> {
let mut p = HELEMENT!();
let (s,_) = s2u!(selector);
let ok = (_API.SciterSelectParent)(self.he, s.as_ptr(), 0, &mut p);
if ok != SCDOM_RESULT::OK {
return Err(ok);
}
if p.is_null() { Ok(None) } else { Ok(Some(Element::from(p))) }
}
pub fn find_first(&self, selector: &str) -> Result<Option<Element>> {
let cb = FindFirstElement::default();
let all = self.select_elements(selector, cb);
all.map(|mut x| { x.pop() })
}
pub fn find_all(&self, selector: &str) -> Result<Option<Vec<Element>>> {
let cb = FindAllElements::default();
let all = self.select_elements(selector, cb);
all.map(Some)
}
pub fn update(&self, render_now: bool) -> Result<()> {
let ok = (_API.SciterUpdateElement)(self.he, render_now as BOOL);
ok_or!((), ok)
}
pub fn start_timer(&self, period_ms: u32, timer_id: u64) -> Result<()> {
let ok = (_API.SciterSetTimer)(self.he, period_ms as UINT, timer_id as ::capi::sctypes::UINT_PTR);
ok_or!((), ok)
}
pub fn stop_timer(&self, timer_id: u64) -> Result<()> {
if !self.he.is_null() {
let ok = (_API.SciterSetTimer)(self.he, 0 as UINT, timer_id as ::capi::sctypes::UINT_PTR);
ok_or!((), ok)
} else {
Ok(())
}
}
pub fn attach_handler<Handler: EventHandler>(&mut self, handler: Handler) -> Result<u64> {
let boxed = Box::new(handler);
let ptr = Box::into_raw(boxed);
let token = ptr as usize as u64;
let ok = (_API.SciterAttachEventHandler)(self.he, ::eventhandler::_event_handler_proc::<Handler>, ptr as LPVOID);
ok_or!(token, ok)
}
pub fn detach_handler<Handler: EventHandler>(&mut self, token: u64) -> Result<()> {
let ptr = token as usize as *mut Handler;
let ok = (_API.SciterDetachEventHandler)(self.he, ::eventhandler::_event_handler_proc::<Handler>, ptr as LPVOID);
ok_or!((), ok)
}
}
impl Drop for Element {
fn drop(&mut self) {
(_API.Sciter_UnuseElement)(self.he);
self.he = HELEMENT!();
}
}
impl Clone for Element {
fn clone(&self) -> Self {
Element::from(self.he)
}
}
impl ::std::fmt::Display for Element {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
if self.he.is_null() {
return f.write_str("None");
}
let (t,n,i,c) = (self.get_attribute("type"), self.get_attribute("name"), self.get_attribute("id"), self.get_attribute("class"));
let tag = self.get_tag();
try!(f.write_str(&tag));
if i.is_some() {
try!(write!(f, "#{}", i.unwrap()));
}
if c.is_some() {
try!(write!(f, ".{}", c.unwrap()));
}
if t.is_some() {
try!(write!(f, "|{}", t.unwrap()));
}
if n.is_some() {
try!(write!(f, "({})", n.unwrap()));
}
return Ok(());
}
}
impl ::std::fmt::Debug for Element {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{{{}:{:?}}}", self, self.he)
}
}
pub struct Children<'a> {
base: &'a Element,
index: usize,
count: usize,
}
impl<'a> ::std::iter::Iterator for Children<'a> {
type Item = Element;
fn next(&mut self) -> Option<Element> {
if self.index < self.count {
let pos = self.index;
self.index += 1;
self.base.child(pos)
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remain = self.count - self.index;
(remain, Some(remain))
}
fn count(self) -> usize {
self.count
}
}
impl<'a> ::std::iter::DoubleEndedIterator for Children<'a> {
fn next_back(&mut self) -> Option<Element> {
if self.index == self.count || self.count == 0 {
None
} else {
self.count -= 1;
self.base.child(self.count)
}
}
}
impl<'a> ::std::iter::IntoIterator for &'a Element {
type Item = Element;
type IntoIter = Children<'a>;
fn into_iter(self) -> Children<'a> {
self.children()
}
}
use ::utf;
extern "system" fn store_wstr(szstr: LPCWSTR, str_length: UINT, param: LPVOID) {
let s = utf::w2sn(szstr, str_length as usize);
let out = param as *mut String;
unsafe { *out = s };
}
extern "system" fn store_astr(szstr: LPCSTR, str_length: UINT, param: LPVOID) {
let s = utf::u2sn(szstr, str_length as usize);
let out = param as *mut String;
unsafe { *out = s };
}
extern "system" fn store_bstr(szstr: LPCBYTE, str_length: UINT, param: LPVOID) {
let s = unsafe { ::std::slice::from_raw_parts(szstr, str_length as usize) };
let pout = param as *mut Vec<u8>;
let out = unsafe {&mut *pout};
out.extend_from_slice(s);
}
pub mod event {
pub use capi::scbehavior::{CLICK_REASON, EVENT_GROUPS, EDIT_CHANGED_REASON, BEHAVIOR_EVENTS, PHASE_MASK};
use capi::sctypes::*;
use capi::scdom::HELEMENT;
use value::Value;
pub fn default_events() -> EVENT_GROUPS {
return EVENT_GROUPS::HANDLE_BEHAVIOR_EVENT | EVENT_GROUPS::HANDLE_SCRIPTING_METHOD_CALL;
}
#[derive(Debug)]
pub enum EventReason {
General(CLICK_REASON),
EditChanged(EDIT_CHANGED_REASON),
VideoBind(LPVOID),
}
#[allow(unused_variables)]
pub trait EventHandler {
fn get_subscription(&mut self) -> Option<EVENT_GROUPS> {
return Some(default_events());
}
fn attached(&mut self, root: HELEMENT) {}
fn detached(&mut self, root: HELEMENT) {}
fn document_complete(&mut self, root: HELEMENT, target: HELEMENT) {}
fn document_close(&mut self, root: HELEMENT, target: HELEMENT) {}
fn on_script_call(&mut self, root: HELEMENT, name: &str, args: &[Value]) -> Option<Value> {
return self.dispatch_script_call(root, name, args);
}
fn dispatch_script_call(&mut self, root: HELEMENT, name: &str, args: &[Value]) -> Option<Value> {
return None;
}
fn on_event(&mut self, root: HELEMENT, source: HELEMENT, target: HELEMENT, code: BEHAVIOR_EVENTS, phase: PHASE_MASK, reason: EventReason) -> bool {
return false;
}
fn on_timer(&mut self, root: HELEMENT, timer_id: u64) -> bool { return false; }
}
}