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
use crate::enums::{Align, Color, ColorDepth, Cursor, Font, FrameType, Shortcut};
use crate::image::RgbImage;
use crate::prelude::*;
use crate::utils::FlString;
use fltk_sys::draw::*;
use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw;
#[derive(Copy, Clone, Debug)]
pub struct Coord<T: Copy>(pub T, pub T);
bitflags! {
pub struct LineStyle: i32 {
const Solid = 0;
const Dash = 1;
const Dot =2;
const DashDot = 3;
const DashDotDot =4;
const CapFlat = 100;
const CapRound = 200;
const CapSquare = 300;
const JoinMiter = 1000;
const JoinRound = 2000;
const JoinBevel = 3000;
}
}
pub type Region = *mut raw::c_void;
#[derive(Debug)]
pub struct Offscreen {
inner: *mut raw::c_void,
}
unsafe impl Sync for Offscreen {}
unsafe impl Send for Offscreen {}
impl Offscreen {
pub fn new(w: i32, h: i32) -> Option<Offscreen> {
unsafe {
let x = Fl_create_offscreen(w, h);
if x.is_null() {
None
} else {
Some(Offscreen { inner: x })
}
}
}
pub unsafe fn uninit() -> Offscreen {
Offscreen {
inner: std::ptr::null_mut(),
}
}
pub fn begin(&self) {
assert!(!self.inner.is_null());
unsafe { Fl_begin_offscreen(self.inner) }
}
pub fn end(&self) {
assert!(!self.inner.is_null());
unsafe { Fl_end_offscreen() }
}
pub fn copy(&self, x: i32, y: i32, w: i32, h: i32, src_x: i32, src_y: i32) {
assert!(!self.inner.is_null());
unsafe { Fl_copy_offscreen(x, y, w, h, self.inner, src_x, src_y) }
}
pub fn rescale(&mut self) {
assert!(!self.inner.is_null());
unsafe { Fl_rescale_offscreen(self.inner) }
}
pub fn is_valid(&self) -> bool {
assert!(!self.inner.is_null());
!self.inner.is_null()
}
pub unsafe fn shallow_copy(&self) -> Offscreen {
assert!(!self.inner.is_null());
Offscreen { inner: self.inner }
}
}
impl Drop for Offscreen {
fn drop(&mut self) {
unsafe { Fl_delete_offscreen(self.inner) }
}
}
pub fn show_colormap(old_color: Color) -> Color {
unsafe { mem::transmute(Fl_show_colormap(old_color.bits() as u32)) }
}
pub fn set_color_rgb(r: u8, g: u8, b: u8) {
unsafe { Fl_set_color_rgb(r, g, b) }
}
pub fn get_color() -> Color {
unsafe { mem::transmute(Fl_get_color()) }
}
pub fn draw_line(x1: i32, y1: i32, x2: i32, y2: i32) {
unsafe {
Fl_line(x1, y1, x2, y2);
}
}
pub fn draw_line2(pos1: Coord<i32>, pos2: Coord<i32>, pos3: Coord<i32>) {
unsafe { Fl_line2(pos1.0, pos1.1, pos2.0, pos2.1, pos3.0, pos3.1) }
}
pub fn draw_point(x: i32, y: i32) {
unsafe { Fl_point(x, y) }
}
pub fn draw_point2(pos: Coord<i32>) {
unsafe { Fl_point(pos.0, pos.1) }
}
pub fn draw_rect(x: i32, y: i32, w: i32, h: i32) {
unsafe { Fl_rect(x, y, w, h) }
}
pub fn draw_rect_with_color(x: i32, y: i32, w: i32, h: i32, color: Color) {
unsafe { Fl_rect_with_color(x, y, w, h, color.bits() as u32) }
}
pub fn draw_loop(x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32) {
unsafe {
Fl_loop(x1, y1, x2, y2, x3, y3);
}
}
pub fn draw_loop2(pos1: Coord<i32>, pos2: Coord<i32>, pos3: Coord<i32>) {
unsafe { Fl_loop(pos1.0, pos1.1, pos2.0, pos2.1, pos3.0, pos3.1) }
}
pub fn draw_loop3(pos1: Coord<i32>, pos2: Coord<i32>, pos3: Coord<i32>, pos4: Coord<i32>) {
unsafe {
Fl_loop2(
pos1.0, pos1.1, pos2.0, pos2.1, pos3.0, pos3.1, pos4.0, pos4.1,
)
}
}
pub fn draw_rect_fill(x: i32, y: i32, w: i32, h: i32, color: Color) {
unsafe { Fl_rectf_with_color(x, y, w, h, color.bits() as u32) }
}
pub fn draw_focus_rect(x: i32, y: i32, w: i32, h: i32) {
unsafe { Fl_focus_rect(x, y, w, h) }
}
pub fn set_draw_hex_color(color: u32) {
let (r, g, b) = crate::utils::hex2rgb(color);
unsafe { Fl_set_color_rgb(r, g, b) }
}
pub fn set_draw_rgb_color(r: u8, g: u8, b: u8) {
unsafe { Fl_set_color_rgb(r, g, b) }
}
pub fn set_draw_color(color: Color) {
unsafe { Fl_set_color_int(color.bits() as u32) }
}
pub fn draw_circle(x: f64, y: f64, r: f64) {
unsafe {
Fl_circle(x, y, r);
}
}
pub fn draw_arc(x: i32, y: i32, width: i32, height: i32, a: f64, b: f64) {
unsafe {
Fl_arc(x, y, width, height, a, b);
}
}
pub fn draw_arc2(x: f64, y: f64, r: f64, start: f64, end: f64) {
unsafe { Fl_arc2(x, y, r, start, end) }
}
pub fn draw_pie(x: i32, y: i32, width: i32, height: i32, a: f64, b: f64) {
unsafe {
Fl_pie(x, y, width, height, a, b);
}
}
pub fn set_line_style(style: LineStyle, width: i32) {
unsafe {
Fl_line_style(
style.bits(),
width,
std::ptr::null_mut() as *mut std::os::raw::c_char,
);
}
}
pub fn push_clip(x: i32, y: i32, w: i32, h: i32) {
unsafe {
Fl_push_clip(x, y, w, h);
}
}
pub fn pop_clip() {
unsafe {
Fl_pop_clip();
}
}
pub fn set_clip_region(r: Region) {
assert!(!r.is_null());
unsafe { Fl_set_clip_region(r) }
}
pub fn clip_region() -> Region {
unsafe {
let ptr = Fl_clip_region();
assert!(!ptr.is_null());
ptr
}
}
pub fn push_no_clip() {
unsafe { Fl_push_no_clip() }
}
pub fn not_clipped(x: i32, y: i32, w: i32, h: i32) -> bool {
unsafe { Fl_not_clipped(x, y, w, h) != 0 }
}
pub fn restore_clip() {
unsafe { Fl_restore_clip() }
}
pub fn transform_x(x: f64, y: f64) -> f64 {
unsafe { Fl_transform_x(x, y) }
}
pub fn transform_y(x: f64, y: f64) -> f64 {
unsafe { Fl_transform_y(x, y) }
}
pub fn transform_dx(x: f64, y: f64) -> f64 {
unsafe { Fl_transform_dx(x, y) }
}
pub fn transform_dy(x: f64, y: f64) -> f64 {
unsafe { Fl_transform_dy(x, y) }
}
pub fn transformed_vertex(xf: f64, yf: f64) {
unsafe { Fl_transformed_vertex(xf, yf) }
}
pub fn draw_rectf(x: i32, y: i32, w: i32, h: i32) {
unsafe { Fl_rectf(x, y, w, h) }
}
pub fn draw_rectf_with_rgb(
x: i32,
y: i32,
width: i32,
height: i32,
color_r: u8,
color_g: u8,
color_b: u8,
) {
unsafe { Fl_rectf_with_rgb(x, y, width, height, color_r, color_g, color_b) }
}
pub fn draw_polygon(x: i32, y: i32, x1: i32, y1: i32, x2: i32, y2: i32) {
unsafe { Fl_polygon(x, y, x1, y1, x2, y2) }
}
pub fn draw_polygon2(pos1: Coord<i32>, pos2: Coord<i32>, pos3: Coord<i32>) {
unsafe { Fl_polygon(pos1.0, pos1.1, pos2.0, pos2.1, pos3.0, pos3.1) }
}
pub fn draw_polygon3(pos1: Coord<i32>, pos2: Coord<i32>, pos3: Coord<i32>, pos4: Coord<i32>) {
unsafe {
Fl_polygon2(
pos1.0, pos1.1, pos2.0, pos2.1, pos3.0, pos3.1, pos4.0, pos4.1,
)
}
}
pub fn draw_curve(pos1: Coord<f64>, pos2: Coord<f64>, pos3: Coord<f64>, pos4: Coord<f64>) {
unsafe {
Fl_curve(
pos1.0, pos1.1, pos2.0, pos2.1, pos3.0, pos3.1, pos4.0, pos4.1,
)
}
}
pub fn draw_xyline(x: i32, y: i32, x1: i32) {
unsafe { Fl_xyline(x, y, x1) }
}
pub fn draw_xyline2(x: i32, y: i32, x1: i32, y2: i32) {
unsafe { Fl_xyline2(x, y, x1, y2) }
}
pub fn draw_xyline3(x: i32, y: i32, x1: i32, y2: i32, x3: i32) {
unsafe { Fl_xyline3(x, y, x1, y2, x3) }
}
pub fn draw_yxline(x: i32, y: i32, y1: i32) {
unsafe { Fl_yxline(x, y, y1) }
}
pub fn draw_yxline2(x: i32, y: i32, y1: i32, x2: i32) {
unsafe { Fl_yxline2(x, y, y1, x2) }
}
pub fn draw_yxline3(x: i32, y: i32, y1: i32, x2: i32, y3: i32) {
unsafe { Fl_yxline3(x, y, y1, x2, y3) }
}
pub fn push_matrix() {
unsafe { Fl_push_matrix() }
}
pub fn pop_matrix() {
unsafe { Fl_pop_matrix() }
}
pub fn scale_xy(x: f64, y: f64) {
unsafe { Fl_scale(x, y) }
}
pub fn scale_x(x: f64) {
unsafe { Fl_scale2(x) }
}
pub fn translate(x: f64, y: f64) {
unsafe { Fl_translate(x, y) }
}
pub fn rotate(d: f64) {
unsafe { Fl_rotate(d) }
}
pub fn mult_matrix(val_a: f64, val_b: f64, val_c: f64, val_d: f64, x: f64, y: f64) {
unsafe { Fl_mult_matrix(val_a, val_b, val_c, val_d, x, y) }
}
pub fn begin_points() {
unsafe { Fl_begin_points() }
}
pub fn begin_line() {
unsafe { Fl_begin_line() }
}
pub fn begin_loop() {
unsafe { Fl_begin_loop() }
}
pub fn begin_polygon() {
unsafe { Fl_begin_polygon() }
}
pub fn vertex(x: f64, y: f64) {
unsafe { Fl_vertex(x, y) }
}
pub fn end_points() {
unsafe { Fl_end_points() }
}
pub fn end_line() {
unsafe { Fl_end_line() }
}
pub fn end_loop() {
unsafe { Fl_end_loop() }
}
pub fn end_polygon() {
unsafe { Fl_end_polygon() }
}
pub fn begin_complex_polygon() {
unsafe { Fl_begin_complex_polygon() }
}
pub fn gap() {
unsafe { Fl_gap() }
}
pub fn end_complex_polygon() {
unsafe { Fl_end_complex_polygon() }
}
pub fn set_font(face: Font, fsize: i32) {
unsafe { Fl_set_draw_font(face.bits() as i32, fsize as i32) }
}
pub fn font() -> Font {
unsafe { mem::transmute(Fl_font()) }
}
pub fn size() -> i32 {
unsafe { Fl_size() as i32 }
}
pub fn height() -> i32 {
unsafe { Fl_height() as i32 }
}
pub fn set_height(font: Font, size: i32) {
unsafe {
Fl_set_height(font.bits() as i32, size as i32);
}
}
pub fn descent() -> i32 {
unsafe { Fl_descent() }
}
pub fn width(txt: &str) -> f64 {
let txt = CString::safe_new(txt);
unsafe { Fl_width(txt.as_ptr()) }
}
pub fn width2(txt: &str, n: i32) -> f64 {
let txt = CString::safe_new(txt);
unsafe { Fl_width2(txt.as_ptr(), n) }
}
pub fn measure(txt: &str, draw_symbols: bool) -> (i32, i32) {
let txt = CString::safe_new(txt);
let mut x = 0;
let mut y = 0;
unsafe {
Fl_measure(txt.as_ptr(), &mut x, &mut y, draw_symbols as i32);
}
(x, y)
}
pub fn char_width(c: char) -> f64 {
unsafe { Fl_width3(c as u32) }
}
pub fn latin1_to_local(txt: &str, n: i32) -> String {
let txt = CString::safe_new(txt);
unsafe {
let x = Fl_latin1_to_local(txt.as_ptr(), n);
assert!(!x.is_null());
CStr::from_ptr(x as *mut raw::c_char)
.to_string_lossy()
.to_string()
}
}
pub fn local_to_latin1(txt: &str, n: i32) -> String {
let txt = CString::safe_new(txt);
unsafe {
let x = Fl_local_to_latin1(txt.as_ptr(), n);
assert!(!x.is_null());
CStr::from_ptr(x as *mut raw::c_char)
.to_string_lossy()
.to_string()
}
}
pub fn draw_text(txt: &str, x: i32, y: i32) {
let txt = CString::safe_new(txt);
unsafe { Fl_draw(txt.as_ptr(), x, y) }
}
pub fn draw_text2(string: &str, x: i32, y: i32, width: i32, height: i32, align: Align) {
let s = CString::safe_new(string);
unsafe { Fl_draw_text2(s.as_ptr(), x, y, width, height, align.bits() as i32) }
}
pub fn draw_text_angled(angle: i32, txt: &str, x: i32, y: i32) {
let txt = CString::safe_new(txt);
unsafe { Fl_draw2(angle, txt.as_ptr(), x, y) }
}
pub fn rtl_draw(txt: &str, x: i32, y: i32) {
let len = txt.len() as i32;
let txt = CString::safe_new(txt);
unsafe { Fl_rtl_draw(txt.as_ptr(), len, x, y) }
}
pub fn draw_frame(string: &str, x: i32, y: i32, width: i32, height: i32) {
let s = CString::safe_new(string);
unsafe { Fl_frame(s.as_ptr(), x, y, width, height) }
}
pub fn draw_frame2(string: &str, x: i32, y: i32, width: i32, height: i32) {
let s = CString::safe_new(string);
unsafe { Fl_frame2(s.as_ptr(), x, y, width, height) }
}
pub fn draw_box(box_type: FrameType, x: i32, y: i32, w: i32, h: i32, color: Color) {
unsafe { Fl_draw_box(box_type as i32, x, y, w, h, color.bits() as u32) }
}
pub fn can_do_alpha_blending() -> bool {
unsafe { Fl_can_do_alpha_blending() != 0 }
}
pub fn shortcut_label(shortcut: Shortcut) -> String {
unsafe {
let x = Fl_shortcut_label(shortcut.bits() as u32);
assert!(!x.is_null());
CStr::from_ptr(x as *mut raw::c_char)
.to_string_lossy()
.to_string()
}
}
pub fn overlay_rect(x: i32, y: i32, w: i32, h: i32) {
unsafe { Fl_overlay_rect(x, y, w, h) }
}
pub fn overlay_clear() {
unsafe { Fl_overlay_clear() }
}
pub fn set_cursor(cursor: Cursor) {
unsafe { Fl_set_cursor(cursor as i32) }
}
pub fn set_cursor_with_color(cursor: Cursor, fg: Color, bg: Color) {
unsafe { Fl_set_cursor2(cursor as i32, fg.bits() as i32, bg.bits() as i32) }
}
pub fn set_status(x: i32, y: i32, w: i32, h: i32) {
unsafe { Fl_set_status(x, y, w, h) }
}
pub fn set_spot<Win: WindowExt>(font: Font, size: i32, x: i32, y: i32, w: i32, h: i32, win: &Win) {
unsafe {
assert!(!win.was_deleted());
Fl_set_spot(
font.bits() as i32,
size as i32,
x,
y,
w,
h,
win.as_widget_ptr() as *mut raw::c_void,
)
}
}
pub fn reset_spot() {
unsafe { Fl_reset_spot() }
}
pub fn capture_window<Win: WindowExt>(win: &mut Win) -> Result<RgbImage, FltkError> {
assert!(!win.was_deleted());
let cp = win.width() * win.height() * 3;
win.show();
unsafe {
let x = Fl_read_image(std::ptr::null_mut(), 0, 0, win.width(), win.height(), 0);
if x.is_null() {
Err(FltkError::Internal(FltkErrorKind::FailedOperation))
} else {
let x = std::slice::from_raw_parts(x, cp as usize);
Ok(RgbImage::new(
x,
win.width(),
win.height(),
ColorDepth::Rgb8,
)?)
}
}
}
pub fn draw_rgba<'a, T: WidgetBase>(wid: &'a mut T, fb: &'a [u8]) -> Result<(), FltkError> {
let width = wid.width();
let height = wid.height();
let mut img = crate::image::RgbImage::new(fb, width, height, ColorDepth::Rgba8)?;
wid.draw(move |s| {
let x = s.x();
let y = s.y();
let w = s.width();
let h = s.height();
img.scale(w, h, false, true);
img.draw(x, y, w, h);
});
Ok(())
}
pub unsafe fn draw_rgba_nocopy<T: WidgetBase>(wid: &mut T, fb: &[u8]) {
let ptr = fb.as_ptr();
let len = fb.len();
let width = wid.width();
let height = wid.height();
wid.draw(move |s| {
let x = s.x();
let y = s.y();
let w = s.width();
let h = s.height();
if let Ok(mut img) = crate::image::RgbImage::from_data(
std::slice::from_raw_parts(ptr, len),
width,
height,
ColorDepth::Rgba8,
) {
img.scale(w, h, false, true);
img.draw(x, y, w, h);
}
});
}
pub fn draw_rgb<'a, T: WidgetBase>(wid: &'a mut T, fb: &'a [u8]) -> Result<(), FltkError> {
let width = wid.width();
let height = wid.height();
let mut img = crate::image::RgbImage::new(fb, width, height, ColorDepth::Rgb8)?;
wid.draw(move |s| {
let x = s.x();
let y = s.y();
let w = s.width();
let h = s.height();
img.scale(w, h, false, true);
img.draw(x, y, w, h);
});
Ok(())
}
pub unsafe fn draw_rgb_nocopy<T: WidgetBase>(wid: &mut T, fb: &[u8]) {
let ptr = fb.as_ptr();
let len = fb.len();
let width = wid.width();
let height = wid.height();
wid.draw(move |s| {
let x = s.x();
let y = s.y();
let w = s.width();
let h = s.height();
if let Ok(mut img) = crate::image::RgbImage::from_data(
std::slice::from_raw_parts(ptr, len),
width,
height,
ColorDepth::Rgb8,
) {
img.scale(w, h, false, true);
img.draw(x, y, w, h);
}
});
}
pub fn draw_image(
data: &[u8],
x: i32,
y: i32,
w: i32,
h: i32,
depth: ColorDepth,
) -> Result<(), FltkError> {
let sz = (w * h * depth as i32) as usize;
if sz > data.len() {
return Err(FltkError::Internal(FltkErrorKind::ImageFormatError));
}
unsafe {
Fl_draw_image(data.as_ptr(), x, y, w, h, depth as i32, 0);
}
Ok(())
}
pub unsafe fn draw_image2(data: &[u8], x: i32, y: i32, w: i32, h: i32, depth: i32, line_data: i32) {
Fl_draw_image(data.as_ptr(), x, y, w, h, depth, line_data);
}