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
use ::{_API};
use capi::sctypes::*;
use capi::screquest::HREQUEST;
use capi::schandler::NativeHandler;
use dom::event::EventHandler;
use eventhandler::*;
use value::{Value};
pub use capi::scdef::{SCITER_RT_OPTIONS, SCRIPT_RUNTIME_FEATURES, GFX_LAYER};
pub use capi::scdef::{LOAD_RESULT, SCN_LOAD_DATA, SCN_DATA_LOADED, SCN_ATTACH_BEHAVIOR, OUTPUT_SUBSYTEMS, OUTPUT_SEVERITY};
pub type Result<T> = ::std::result::Result<T, ()>;
macro_rules! ok_or {
($ok:ident) => {
if $ok != 0 {
Ok(())
} else {
Err(())
}
};
($ok:ident, $rv:expr) => {
if $ok != 0 {
Ok($rv)
} else {
Err(())
}
};
($ok:ident, $rv:expr, $err:expr) => {
if $ok != 0 {
Ok($rv)
} else {
Err($err)
}
};
}
#[allow(unused_variables)]
pub trait HostHandler {
fn on_data_load(&mut self, pnm: &mut SCN_LOAD_DATA) -> LOAD_RESULT { return LOAD_RESULT::LOAD_DEFAULT; }
fn on_data_loaded(&mut self, pnm: & SCN_DATA_LOADED) { }
fn on_attach_behavior(&mut self, pnm: &mut SCN_ATTACH_BEHAVIOR) -> bool { return false; }
fn on_engine_destroyed(&mut self) { }
fn on_graphics_critical_failure(&mut self) { }
fn on_debug_output(&mut self, subsystem: OUTPUT_SUBSYTEMS, severity: OUTPUT_SEVERITY, message: &str) {
if message.len() > 0 {
println!("{:?}:{:?}: {}", severity, subsystem, message);
}
}
fn data_ready(&mut self, hwnd: HWINDOW, uri: &str, data: &[u8], request_id: Option<HREQUEST>) {
let (s,_) = s2w!(uri);
match request_id {
Some(req) => {
(_API.SciterDataReadyAsync)(hwnd, s.as_ptr(), data.as_ptr(), data.len() as UINT, req)
},
None => {
(_API.SciterDataReady)(hwnd, s.as_ptr(), data.as_ptr(), data.len() as UINT)
},
};
}
}
struct DefaultHandler;
impl HostHandler for DefaultHandler {
}
use std::cell::{Cell, RefCell};
pub struct Host {
hwnd: Cell<HWINDOW>,
handler: RefCell<NativeHandler>,
}
impl Host {
pub fn attach(hwnd: HWINDOW) -> Host {
let host = Host { hwnd: Cell::new(hwnd), handler: RefCell::new(NativeHandler::default()) };
host.setup_callback(hwnd, DefaultHandler);
return host;
}
pub fn setup_callback<T: HostHandler>(&self, hwnd: HWINDOW, handler: T) {
*self.handler.borrow_mut() = NativeHandler::from(handler);
self.hwnd.set(hwnd);
self.enable_debug(true);
(_API.SciterSetCallback)(hwnd, _on_handle_notification::<T>, self.handler.borrow().as_mut_ptr());
(_API.SciterSetupDebugOutput)(0 as HWINDOW, self.handler.borrow().as_mut_ptr(), _on_debug_notification::<T>);
}
pub fn attach_handler<T: EventHandler>(&self, handler: T) {
let hwnd = self.get_hwnd();
let boxed = Box::new( WindowHandler { hwnd: hwnd, handler: handler } );
let ptr = Box::into_raw(boxed);
(_API.SciterWindowAttachEventHandler)(hwnd, _event_handler_window_proc::<T>, ptr as LPVOID, ::dom::event::default_events() as UINT);
}
pub fn enable_debug(&self, enable: bool) {
let hwnd = 0 as HWINDOW;
(_API.SciterSetOption)(hwnd, SCITER_RT_OPTIONS::SCITER_SET_DEBUG_MODE, enable as UINT_PTR);
}
pub fn get_hwnd(&self) -> HWINDOW {
self.hwnd.get()
}
pub fn get_root(&self) -> Option<::dom::Element> {
::dom::Element::from_window(self.hwnd.get()).ok()
}
pub fn load_file(&self, uri: &str) {
let (s,_) = s2w!(uri);
(_API.SciterLoadFile)(self.hwnd.get(), s.as_ptr());
}
pub fn load_html(&self, html: &[u8], uri: Option<&str>) {
match uri {
Some(uri) => {
let (s,_) = s2w!(uri);
(_API.SciterLoadHtml)(self.hwnd.get(), html.as_ptr(), html.len() as UINT, s.as_ptr())
},
None => {
(_API.SciterLoadHtml)(self.hwnd.get(), html.as_ptr(), html.len() as UINT, 0 as LPCWSTR)
}
};
}
pub fn data_ready(&self, uri: &str, data: &[u8]) {
let (s,_) = s2w!(uri);
(_API.SciterDataReady)(self.hwnd.get(), s.as_ptr(), data.as_ptr(), data.len() as UINT);
}
pub fn data_ready_async(&self, uri: &str, data: &[u8], request_id: Option<HREQUEST>) {
let (s,_) = s2w!(uri);
let req = request_id.unwrap_or(::std::ptr::null_mut());
(_API.SciterDataReadyAsync)(self.hwnd.get(), s.as_ptr(), data.as_ptr(), data.len() as UINT, req);
}
pub fn eval_script(&self, script: &str) -> ::std::result::Result<Value, Value> {
let (s,n) = s2w!(script);
let mut rv = Value::new();
let ok = (_API.SciterEval)(self.hwnd.get(), s.as_ptr(), n, rv.as_ptr());
ok_or!(ok, rv, rv)
}
pub fn call_function(&self, name: &str, args: &[Value]) -> ::std::result::Result<Value, Value> {
let mut rv = Value::new();
let (s,_) = s2u!(name);
let argv = Value::pack_args(args);
let ok = (_API.SciterCall)(self.hwnd.get(), s.as_ptr(), argv.len() as UINT, argv.as_ptr(), rv.as_ptr());
ok_or!(ok, rv, rv)
}
pub fn set_option(&self, option: SCITER_RT_OPTIONS, value: usize) -> Result<()> {
let ok = (_API.SciterSetOption)(self.hwnd.get(), option, value as UINT_PTR);
ok_or!(ok)
}
pub fn set_home_url(&self, url: &str) -> Result<()> {
let (s,_) = s2w!(url);
let ok = (_API.SciterSetHomeURL)(self.hwnd.get(), s.as_ptr());
ok_or!(ok)
}
pub fn set_media_type(&self, media_type: &str) -> Result<()> {
let (s,_) = s2w!(media_type);
let ok = (_API.SciterSetMediaType)(self.hwnd.get(), s.as_ptr());
ok_or!(ok)
}
pub fn set_media_vars(&self, media: Value) -> Result<()> {
let ok = (_API.SciterSetMediaVars)(self.hwnd.get(), media.as_cptr());
ok_or!(ok)
}
pub fn set_master_css(&self, css: &str, append: bool) -> Result<()> {
let (s,_) = s2u!(css);
let b = s.as_bytes();
let n = b.len() as UINT;
let ok = if append {
(_API.SciterAppendMasterCSS)(b.as_ptr(), n)
} else {
(_API.SciterSetMasterCSS)(b.as_ptr(), n)
};
ok_or!(ok)
}
pub fn set_window_css(&self, css: &str, base_url: &str, media_type: &str) -> Result<()> {
let (s,_) = s2u!(css);
let (url,_) = s2w!(base_url);
let (media,_) = s2w!(media_type);
let b = s.as_bytes();
let n = b.len() as UINT;
let ok = (_API.SciterSetCSS)(self.hwnd.get(), b.as_ptr(), n, url.as_ptr(), media.as_ptr());
ok_or!(ok)
}
}
extern "system" fn _on_handle_notification<T: HostHandler>(pnm: *mut ::capi::scdef::SCITER_CALLBACK_NOTIFICATION, param: LPVOID) -> UINT
{
use capi::scdef::{SCITER_NOTIFICATION, SCITER_CALLBACK_NOTIFICATION};
let mut boxed = NativeHandler::from_mut_ptr3(param);
let nm: &mut SCITER_CALLBACK_NOTIFICATION = unsafe { &mut *pnm };
let code: SCITER_NOTIFICATION = unsafe { ::std::mem::transmute(nm.code) };
let result: UINT = match code {
SCITER_NOTIFICATION::SC_LOAD_DATA => {
let me = boxed.as_mut::<T>();
let scnm = pnm as *mut SCN_LOAD_DATA;
let re = me.on_data_load(unsafe { &mut *scnm} );
re as UINT
},
SCITER_NOTIFICATION::SC_DATA_LOADED => {
let me = boxed.as_mut::<T>();
let scnm = pnm as *mut SCN_DATA_LOADED;
me.on_data_loaded(unsafe { &mut *scnm} );
0 as UINT
},
SCITER_NOTIFICATION::SC_ATTACH_BEHAVIOR => {
let me = boxed.as_mut::<T>();
let scnm = pnm as *mut SCN_ATTACH_BEHAVIOR;
let re = me.on_attach_behavior(unsafe { &mut *scnm} );
re as UINT
},
SCITER_NOTIFICATION::SC_ENGINE_DESTROYED => {
let me = boxed.as_mut::<T>();
me.on_engine_destroyed();
0 as UINT
},
SCITER_NOTIFICATION::SC_GRAPHICS_CRITICAL_FAILURE => {
let me = boxed.as_mut::<T>();
me.on_engine_destroyed();
0 as UINT
},
_ => 0,
};
return result;
}
extern "system" fn _on_debug_notification<T: HostHandler>(param: LPVOID, subsystem: OUTPUT_SUBSYTEMS, severity: OUTPUT_SEVERITY,
text: LPCWSTR, _text_length: UINT)
{
let mut boxed = NativeHandler::from_mut_ptr3(param);
{
let me = boxed.as_mut::<T>();
let message = ::utf::w2s(text).replace("\r", "\n");
me.on_debug_output(subsystem, severity, message.trim_right());
}
}