fltk2 0.1.7

Rust bindings for the FLTK GUI library
Documentation
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
use crate::app::{init::init_all, init::is_initialized, widget::windows};
use crate::prelude::*;
use fltk_sys::fl;
use std::{mem, os::raw, panic, thread, time};

/// Runs the event loop
/// # Errors
/// Returns `FailedToRun`, this is fatal to the app
pub fn run() -> Result<(), FltkError> {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        if !crate::app::is_ui_thread() {
            return Err(FltkError::Internal(FltkErrorKind::FailedToRun));
        }
        match fl::Fl_run() {
            0 => Ok(()),
            _ => Err(FltkError::Internal(FltkErrorKind::FailedToRun)),
        }
    }
}

/// Enable locks. This is called automatically in the beginning of the app initialization
pub fn enable_locks() -> Result<(), FltkError> {
    lock()?;
    Ok(())
}

/// Locks the main UI thread
/// # Errors
/// Returns `FailedToLock` if locking is unsupported. This is fatal to the app
pub fn lock() -> Result<(), FltkError> {
    unsafe {
        match fl::Fl_lock() {
            0 => Ok(()),
            _ => Err(FltkError::Internal(FltkErrorKind::FailedToLock)),
        }
    }
}

/// Unlocks the main UI thread
pub fn unlock() {
    unsafe {
        fl::Fl_unlock();
    }
}

/// Trigger event loop handling in the main thread
pub fn awake() {
    unsafe { fl::Fl_awake() }
}

/// Registers a function that will be called by the main thread during the next message handling cycle
pub fn awake_callback<F: FnMut() + 'static>(cb: F) {
    unsafe {
        unsafe extern "C" fn shim(data: *mut raw::c_void) {
            unsafe {
                let mut a: Box<Box<dyn FnMut()>> = Box::from_raw(data as *mut Box<dyn FnMut()>);
                let f: &mut dyn FnMut() = &mut **a;
                let _ = panic::catch_unwind(panic::AssertUnwindSafe(f));
            }
        }
        let a: *mut Box<dyn FnMut()> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: fl::Fl_Awake_Handler = Some(shim);
        fl::Fl_awake_callback(callback, data);
    }
}

/// Starts waiting for events.
/// Calls to redraw within wait require an explicit sleep
pub fn wait() -> bool {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        assert!(crate::app::is_ui_thread());
        fl::Fl_wait() != 0
    }
}

/// Put the thread to sleep for `dur` seconds
pub fn sleep(dur: f64) {
    let dur = dur * 1000.;
    thread::sleep(time::Duration::from_millis(dur as u64));
}

/// Waits a maximum of `dur` seconds or until "something happens".
/// Returns true if an event happened (always true on windows).
/// Returns false if nothing happened.
/// # Errors
/// Can error out on X11 system if interrupted by a signal
pub fn wait_for(dur: f64) -> Result<bool, FltkError> {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        if !crate::app::is_ui_thread() {
            return Err(FltkError::Internal(FltkErrorKind::FailedToRun));
        }
        match fl::Fl_wait_for(dur) as i32 {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(FltkError::Unknown(String::from(
                "The event loop was probably interrupted by an OS signal!",
            ))),
        }
    }
}

/// Returns whether a quit signal was sent
pub fn should_program_quit() -> bool {
    unsafe { fl::Fl_should_program_quit() != 0 }
}

/// Determines whether a program should quit
pub fn program_should_quit(flag: bool) {
    unsafe { fl::Fl_program_should_quit(i32::from(flag)) }
}

/// Calling this during a big calculation will keep the screen up to date and the interface responsive.
pub fn check() -> bool {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        assert!(crate::app::is_ui_thread());
        fl::Fl_check() != 0
    }
}

/// This is similar to `app::check()` except this does not call `app::flush()` or any callbacks,
/// which is useful if your program is in a state where such callbacks are illegal.
pub fn ready() -> bool {
    unsafe {
        if !is_initialized() {
            init_all();
        }
        assert!(crate::app::is_ui_thread());
        fl::Fl_ready() != 0
    }
}

/// Quit the app
pub fn quit() {
    if let Some(wins) = windows() {
        for mut i in wins {
            if i.shown() {
                i.hide();
            }
        }
    }
}

/// Handle object for interacting with idle callbacks
pub type IdleHandle = *mut ();

unsafe extern "C" fn idle_shim(data: *mut raw::c_void) {
    unsafe {
        let a: *mut Box<dyn FnMut(IdleHandle)> = data as *mut Box<dyn FnMut(IdleHandle)>;
        let f: &mut dyn FnMut(IdleHandle) = &mut **a;
        let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| (*f)(data as _)));
    }
}

/// Add an idle callback to run within the event loop.
/// This function returns a handle that can be used for future interaction with the callback.
/// Calls to [`WidgetExt::redraw`](`crate::prelude::WidgetExt::redraw`) within the callback require an explicit sleep
pub fn add_idle<F: FnMut(IdleHandle) + 'static>(cb: F) -> IdleHandle {
    unsafe {
        let a: *mut Box<dyn FnMut(IdleHandle)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_add_idle(callback, data);

        data as _
    }
}

/// Remove the idle function associated with the handle
pub fn remove_idle(handle: IdleHandle) {
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_remove_idle(callback, data);
    }
}

/// Checks whether the idle function, associated with the handle, is installed
pub fn has_idle(handle: IdleHandle) -> bool {
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_has_idle(callback, data) != 0
    }
}

/// Handle object for interacting with check callbacks
pub type CheckHandle = *mut ();

/// Add a check callback to run within the event loop.
/// This function returns a handle that can be used for future interaction with the callback.
pub fn add_check<F: FnMut(CheckHandle) + 'static>(cb: F) -> CheckHandle {
    unsafe {
        let a: *mut Box<dyn FnMut(CheckHandle)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_add_check(callback, data);

        data as _
    }
}

/// Remove the check function associated with the handle
pub fn remove_check(handle: CheckHandle) {
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_remove_check(callback, data);
    }
}

/// Checks whether the check function, associated with the handle, is installed
pub fn has_check(handle: CheckHandle) -> bool {
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(idle_shim);
        fl::Fl_has_check(callback, data) != 0
    }
}

unsafe extern "C" fn clipboard_notify_shim(source: i32, data: *mut raw::c_void) {
    unsafe {
        let a: *mut Box<dyn FnMut(i32)> = data as *mut Box<dyn FnMut(i32)>;
        let f: &mut dyn FnMut(i32) = &mut **a;
        let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| (*f)(source)));
    }
}

/// Register a callback whenever there is a change to the selection buffer or the clipboard.
/// The clipboard is source 1 and the selection buffer is source 0.
/// A callback via closure cannot be removed!
pub fn add_clipboard_notify<F: FnMut(i32) + 'static>(cb: F) {
    unsafe {
        let a: *mut Box<dyn FnMut(i32)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(source: i32, arg1: *mut raw::c_void)> =
            Some(clipboard_notify_shim);
        fl::Fl_add_clipboard_notify(callback, data);
    }
}

/// Stop calling the specified callback when there are changes to the selection
/// buffer or the clipboard.
/// The clipboard is source 1 and the selection buffer is source 0
pub fn remove_clipboard_notify() {
    unsafe {
        let callback: Option<unsafe extern "C" fn(source: i32, arg1: *mut raw::c_void)> =
            Some(clipboard_notify_shim);
        fl::Fl_remove_clipboard_notify(callback);
    }
}

/// Handle object for interacting with timeouts
pub type TimeoutHandle = *mut ();

unsafe extern "C" fn timeout_shim(data: *mut raw::c_void) {
    unsafe {
        let a: *mut Box<dyn FnMut(TimeoutHandle)> = data as *mut Box<dyn FnMut(TimeoutHandle)>;
        let f: &mut dyn FnMut(TimeoutHandle) = &mut **a;
        let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| (*f)(data as _)));
    }
}

/**
    Adds a one-shot timeout callback. The timeout duration `tm` is indicated in seconds
    This function returns a handle that can be use for future interaction with the timeout
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let callback = |_handle| {
            println!("FIRED");
        };

        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        let _handle = app::add_timeout(1.0, callback);
        app.run().unwrap();
    }
    ```
*/
pub fn add_timeout<F: FnMut(TimeoutHandle) + 'static>(tm: f64, cb: F) -> TimeoutHandle {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let a: *mut Box<dyn FnMut(TimeoutHandle)> = Box::into_raw(Box::new(Box::new(cb)));
        let data: *mut raw::c_void = a as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(timeout_shim);
        fl::Fl_add_timeout(tm, callback, data);

        data as _
    }
}

/**
    Repeats the timeout callback, associated with the hadle, from the expiration of the previous timeout.
    You may only call this method inside a timeout callback.
    The timeout duration `tm` is indicated in seconds
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let callback = |handle| {
            println!("TICK");
            app::repeat_timeout(1.0, handle);
        };

        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        app::add_timeout(1.0, callback);
        app.run().unwrap();
    }
    ```
*/
pub fn repeat_timeout(tm: f64, handle: TimeoutHandle) {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(timeout_shim);
        fl::Fl_repeat_timeout(tm, callback, data);
    }
}

/**
    Removes the timeout callback associated with the handle
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let callback = |handle| {
            println!("FIRED");
        };

        let app = app::App::default();
        let mut wind = window::Window::new(100, 100, 400, 300, "");
        wind.show();
        let handle = app::add_timeout(1.0, callback);
        app::remove_timeout(handle);
        app.run().unwrap();
    }
    ```
*/
pub fn remove_timeout(handle: TimeoutHandle) {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(timeout_shim);
        fl::Fl_remove_timeout(callback, data);
    }
}

/// Check whether the timeout, associated with the handle, is installed
pub fn has_timeout(handle: TimeoutHandle) -> bool {
    assert!(crate::app::is_ui_thread());
    unsafe {
        let data: *mut raw::c_void = handle as *mut raw::c_void;
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> = Some(timeout_shim);
        fl::Fl_has_timeout(callback, data) != 0
    }
}

#[doc(hidden)]
pub fn add_raw_timeout<T>(tm: f64, cb: fn(*mut T), data: *mut T) {
    unsafe {
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        let data: *mut raw::c_void = data as *mut raw::c_void;
        fl::Fl_add_timeout(tm, callback, data);
    }
}

#[doc(hidden)]
pub fn repeat_raw_timeout<T>(tm: f64, cb: fn(*mut T), data: *mut T) {
    unsafe {
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        let data: *mut raw::c_void = data as *mut raw::c_void;
        fl::Fl_repeat_timeout(tm, callback, data);
    }
}

#[doc(hidden)]
pub fn remove_raw_timeout<T>(cb: fn(*mut T), data: *mut T) {
    unsafe {
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        let data: *mut raw::c_void = data as *mut raw::c_void;
        fl::Fl_remove_timeout(callback, data);
    }
}

#[doc(hidden)]
pub fn has_raw_timeout<T>(cb: fn(*mut T), data: *mut T) -> bool {
    unsafe {
        let callback: Option<unsafe extern "C" fn(arg1: *mut raw::c_void)> =
            Some(mem::transmute(cb));
        let data: *mut raw::c_void = data as *mut raw::c_void;
        fl::Fl_has_timeout(callback, data) != 0
    }
}

/// Add a system handler
/// # Safety
/// FLTK makes no assurances regarding handling by the system handler
pub unsafe fn add_system_handler(
    cb: Option<unsafe extern "C" fn(*mut raw::c_void, *mut raw::c_void) -> i32>,
    data: *mut raw::c_void,
) {
    unsafe {
        assert!(crate::app::is_ui_thread());
        fl::Fl_add_system_handler(cb, data);
    }
}

/// Add a system handler
/// # Safety
/// FLTK makes no assurances regarding handling by the system handler
pub unsafe fn remove_system_handler(
    cb: Option<unsafe extern "C" fn(*mut raw::c_void, *mut raw::c_void) -> i32>,
) {
    unsafe {
        assert!(crate::app::is_ui_thread());
        fl::Fl_remove_system_handler(cb);
    }
}