fltk/prelude.rs
1use crate::enums::{
2 Align, CallbackTrigger, Color, ColorDepth, Cursor, Damage, Event, Font, FrameType, LabelType,
3 Shortcut,
4};
5use std::convert::From;
6use std::string::FromUtf8Error;
7use std::{fmt, io};
8
9/// Error types returned by fltk-rs + wrappers of std errors
10#[derive(Debug)]
11#[non_exhaustive]
12pub enum FltkError {
13 /// i/o error
14 IoError(io::Error),
15 /// Utf-8 conversion error
16 Utf8Error(FromUtf8Error),
17 /// Null string conversion error
18 NullError(std::ffi::NulError),
19 /// Internal fltk error
20 Internal(FltkErrorKind),
21 /// Error using an erroneous env variable
22 EnvVarError(std::env::VarError),
23 /// Parsing error
24 ParseIntError(std::num::ParseIntError),
25 /// Unknown error
26 Unknown(String),
27}
28
29unsafe impl Send for FltkError {}
30unsafe impl Sync for FltkError {}
31
32/// Error kinds enum for `FltkError`
33#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
34#[non_exhaustive]
35pub enum FltkErrorKind {
36 /// Failed to run the application
37 FailedToRun,
38 /// Failed to initialize the multithreading
39 FailedToLock,
40 /// Failed to set the general scheme of the application
41 FailedToSetScheme,
42 /// Failed operation, mostly unknown reason!
43 FailedOperation,
44 /// System resource (file, image) not found
45 ResourceNotFound,
46 /// Image format error when opening an image of an unsupported format
47 ImageFormatError,
48 /// Error filling table
49 TableError,
50 /// Error due to printing
51 PrintError,
52 /// Invalid color
53 InvalidColor,
54 /// Failed to set grid widget
55 FailedGridSetWidget,
56}
57
58impl std::error::Error for FltkError {
59 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60 match self {
61 FltkError::IoError(err) => Some(err),
62 FltkError::NullError(err) => Some(err),
63 _ => None,
64 }
65 }
66}
67
68impl fmt::Display for FltkError {
69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70 match *self {
71 FltkError::IoError(ref err) => err.fmt(f),
72 FltkError::NullError(ref err) => err.fmt(f),
73 FltkError::Internal(ref err) => write!(f, "An internal error occurred {:?}", err),
74 FltkError::EnvVarError(ref err) => write!(f, "An env var error occurred {:?}", err),
75 FltkError::Utf8Error(ref err) => {
76 write!(f, "A UTF8 conversion error occurred {:?}", err)
77 }
78 FltkError::ParseIntError(ref err) => {
79 write!(f, "An int parsing error occurred {:?}", err)
80 }
81 FltkError::Unknown(ref err) => write!(f, "An unknown error occurred {:?}", err),
82 }
83 }
84}
85
86impl From<io::Error> for FltkError {
87 fn from(err: io::Error) -> FltkError {
88 FltkError::IoError(err)
89 }
90}
91
92impl From<std::ffi::NulError> for FltkError {
93 fn from(err: std::ffi::NulError) -> FltkError {
94 FltkError::NullError(err)
95 }
96}
97
98impl From<std::env::VarError> for FltkError {
99 fn from(err: std::env::VarError) -> FltkError {
100 FltkError::EnvVarError(err)
101 }
102}
103
104impl From<std::string::FromUtf8Error> for FltkError {
105 fn from(err: std::string::FromUtf8Error) -> FltkError {
106 FltkError::Utf8Error(err)
107 }
108}
109
110impl From<std::num::ParseIntError> for FltkError {
111 fn from(err: std::num::ParseIntError) -> FltkError {
112 FltkError::ParseIntError(err)
113 }
114}
115
116/// A trait defined for all enums passable to the [`WidgetExt::set_type()`](`crate::prelude::WidgetExt::set_type`) method
117pub trait WidgetType {
118 /// Get the integral representation of the widget type
119 fn to_i32(self) -> i32;
120 /// Get the widget type from its integral representation
121 fn from_i32(val: i32) -> Self;
122}
123
124/// Defines the methods implemented by all widgets
125///
126/// For multithreaded usage, see the [`widget` module documentation's note](crate::widget)
127/// # Safety
128/// fltk-rs traits depend on some FLTK internal code
129/// # Warning
130/// fltk-rs traits are non-exhaustive,
131/// to avoid future breakage if you try to implement them manually,
132/// use the Deref and DerefMut pattern or the `widget_extends!` macro
133pub unsafe trait WidgetExt {
134 /// Initialize to a position x, y
135 fn with_pos(self, x: i32, y: i32) -> Self
136 where
137 Self: Sized;
138 /// Initialize to size width, height
139 fn with_size(self, width: i32, height: i32) -> Self
140 where
141 Self: Sized;
142 /// Initialize with a label
143 fn with_label(self, title: &str) -> Self
144 where
145 Self: Sized;
146 /// Initialize with alignment
147 fn with_align(self, align: crate::enums::Align) -> Self
148 where
149 Self: Sized;
150 /// Initialize with type
151 fn with_type<T: WidgetType>(self, typ: T) -> Self
152 where
153 Self: Sized;
154 /// Initialize at bottom of another widget
155 fn below_of<W: WidgetExt>(self, wid: &W, padding: i32) -> Self
156 where
157 Self: Sized;
158 /// Initialize above of another widget
159 fn above_of<W: WidgetExt>(self, wid: &W, padding: i32) -> Self
160 where
161 Self: Sized;
162 /// Initialize right of another widget
163 fn right_of<W: WidgetExt>(self, wid: &W, padding: i32) -> Self
164 where
165 Self: Sized;
166 /// Initialize left of another widget
167 fn left_of<W: WidgetExt>(self, wid: &W, padding: i32) -> Self
168 where
169 Self: Sized;
170 /// Initialize center of another widget
171 fn center_of<W: WidgetExt>(self, w: &W) -> Self
172 where
173 Self: Sized;
174 /// Initialize center of another widget on the x axis
175 fn center_x<W: WidgetExt>(self, w: &W) -> Self
176 where
177 Self: Sized;
178 /// Initialize center of another widget on the y axis
179 fn center_y<W: WidgetExt>(self, w: &W) -> Self
180 where
181 Self: Sized;
182 /// Initialize center of parent
183 fn center_of_parent(self) -> Self
184 where
185 Self: Sized;
186 /// Initialize to the size of another widget
187 fn size_of<W: WidgetExt>(self, w: &W) -> Self
188 where
189 Self: Sized;
190 /// Initialize to the size of the parent
191 fn size_of_parent(self) -> Self
192 where
193 Self: Sized;
194 /// Set to position x, y
195 fn set_pos(&mut self, x: i32, y: i32);
196 /// Set to dimensions width and height
197 fn set_size(&mut self, width: i32, height: i32);
198 /// Sets the widget's label.
199 /// labels support special symbols preceded by an `@` [sign](https://www.fltk.org/doc-1.3/symbols.png).
200 /// and for the [associated formatting](https://www.fltk.org/doc-1.3/common.html).
201 fn set_label(&mut self, title: &str);
202 /// Redraws a widget, necessary for resizing and changing positions
203 fn redraw(&mut self);
204 /// Shows the widget
205 fn show(&mut self);
206 /// Hides the widget
207 fn hide(&mut self);
208 /// Returns the x coordinate of the widget
209 fn x(&self) -> i32;
210 /// Returns the y coordinate of the widget
211 fn y(&self) -> i32;
212 /// Returns the width of the widget
213 fn width(&self) -> i32;
214 /// Returns the height of the widget
215 fn height(&self) -> i32;
216 /// Returns the width of the widget
217 fn w(&self) -> i32;
218 /// Returns the height of the widget
219 fn h(&self) -> i32;
220 /// Returns the label of the widget
221 fn label(&self) -> String;
222 /// Measures the label's width and height
223 fn measure_label(&self) -> (i32, i32);
224 /// transforms a widget to a base `Fl_Widget`, for internal use
225 fn as_widget_ptr(&self) -> *mut fltk_sys::widget::Fl_Widget;
226 /// Checks whether the self widget is inside another widget
227 fn inside<W: WidgetExt>(&self, wid: &W) -> bool
228 where
229 Self: Sized;
230 /// Returns the widget type when applicable
231 fn get_type<T: WidgetType>(&self) -> T
232 where
233 Self: Sized;
234 /// Sets the widget type
235 fn set_type<T: WidgetType>(&mut self, typ: T)
236 where
237 Self: Sized;
238 /// Sets the image of the widget
239 fn set_image<I: ImageExt>(&mut self, image: Option<I>)
240 where
241 Self: Sized;
242 /// Sets the image of the widget scaled to the widget's size
243 fn set_image_scaled<I: ImageExt>(&mut self, image: Option<I>)
244 where
245 Self: Sized;
246 /// Gets the image associated with the widget
247 fn image(&self) -> Option<Box<dyn ImageExt>>
248 where
249 Self: Sized;
250 /// Sets the deactivated image of the widget
251 fn set_deimage<I: ImageExt>(&mut self, image: Option<I>)
252 where
253 Self: Sized;
254 /// Sets the deactivated image of the widget scaled to the widget's size
255 fn set_deimage_scaled<I: ImageExt>(&mut self, image: Option<I>)
256 where
257 Self: Sized;
258 /// Gets the deactivated image associated with the widget
259 fn deimage(&self) -> Option<Box<dyn ImageExt>>
260 where
261 Self: Sized;
262 /// Sets the callback when the widget is triggered (clicks for example)
263 /// takes the widget as a closure argument
264 fn set_callback<F: FnMut(&mut Self) + 'static>(&mut self, cb: F)
265 where
266 Self: Sized;
267 /// Emits a message on callback using a sender
268 fn emit<T: 'static + Clone + Send + Sync>(&mut self, sender: crate::app::Sender<T>, msg: T)
269 where
270 Self: Sized;
271 /// Activates the widget
272 fn activate(&mut self);
273 /// Deactivates the widget
274 fn deactivate(&mut self);
275 /// Redraws the label of the widget
276 fn redraw_label(&mut self);
277 /// Resizes and/or moves the widget, takes x, y, width and height
278 fn resize(&mut self, x: i32, y: i32, width: i32, height: i32);
279 /// Returns the tooltip text
280 fn tooltip(&self) -> Option<String>;
281 /// Sets the tooltip text
282 fn set_tooltip(&mut self, txt: &str);
283 /// Returns the widget color
284 fn color(&self) -> Color;
285 /// Sets the widget's color
286 fn set_color(&mut self, color: Color);
287 /// Returns the widget label's color
288 fn label_color(&self) -> Color;
289 /// Sets the widget label's color
290 fn set_label_color(&mut self, color: Color);
291 /// Returns the widget label's font
292 fn label_font(&self) -> Font;
293 /// Sets the widget label's font
294 fn set_label_font(&mut self, font: Font);
295 /// Returns the widget label's size
296 fn label_size(&self) -> i32;
297 /// Sets the widget label's size
298 fn set_label_size(&mut self, sz: i32);
299 /// Returns the widget label's type
300 fn label_type(&self) -> LabelType;
301 /// Sets the widget label's type
302 fn set_label_type(&mut self, typ: LabelType);
303 /// Returns the widget's frame type
304 fn frame(&self) -> FrameType;
305 /// Sets the widget's frame type
306 fn set_frame(&mut self, typ: FrameType);
307 /// Returns whether the widget was changed
308 fn changed(&self) -> bool;
309 /// Mark the widget as changed
310 fn set_changed(&mut self);
311 /// Clears the changed status of the widget
312 fn clear_changed(&mut self);
313 /// Returns the alignment of the widget
314 fn align(&self) -> Align;
315 /// Sets the alignment of the widget
316 fn set_align(&mut self, align: Align);
317 /// Returns the parent of the widget
318 fn parent(&self) -> Option<crate::group::Group>;
319 /// Gets the selection color of the widget
320 fn selection_color(&self) -> Color;
321 /// Sets the selection color of the widget
322 fn set_selection_color(&mut self, color: Color);
323 /// Runs the already registered callback
324 fn do_callback(&mut self);
325 /// Returns the direct window holding the widget
326 fn window(&self) -> Option<Box<dyn WindowExt>>;
327 /// Returns the topmost window holding the widget
328 fn top_window(&self) -> Option<Box<dyn WindowExt>>;
329 /// Checks whether a widget is capable of taking events
330 fn takes_events(&self) -> bool;
331 /// Make the widget take focus
332 /// # Errors
333 /// Errors on failure to take focus
334 fn take_focus(&mut self) -> Result<(), FltkError>;
335 /// Set the widget to have visible focus
336 fn set_visible_focus(&mut self);
337 /// Clear visible focus
338 fn clear_visible_focus(&mut self);
339 /// Set the visible focus using a flag
340 fn visible_focus(&mut self, v: bool);
341 /// Return whether the widget has visible focus
342 fn has_visible_focus(&self) -> bool;
343 /// Return whether the widget has focus
344 fn has_focus(&self) -> bool;
345 /// Check if a widget was deleted
346 fn was_deleted(&self) -> bool;
347 /// Return whether the widget was damaged
348 fn damage(&self) -> bool;
349 /// Signal the widget as damaged and it should be redrawn in the next event loop cycle
350 fn set_damage(&mut self, flag: bool);
351 /// Return the damage mask
352 fn damage_type(&self) -> Damage;
353 /// Signal the type of damage a widget received
354 fn set_damage_type(&mut self, mask: Damage);
355 /// Signal damage for an area inside the widget
356 fn set_damage_area(&mut self, mask: Damage, x: i32, y: i32, w: i32, h: i32);
357 /// Clear the damaged flag
358 fn clear_damage(&mut self);
359 /// Sets the default callback trigger for a widget, equivalent to `when()`
360 fn set_trigger(&mut self, trigger: CallbackTrigger);
361 /// Return the callback trigger, equivalent to `when()`
362 fn trigger(&self) -> CallbackTrigger;
363 /// Return the widget as a window if it's a window
364 fn as_window(&self) -> Option<Box<dyn WindowExt>>;
365 /// Return the widget as a group widget if it's a group widget
366 fn as_group(&self) -> Option<crate::group::Group>;
367 #[doc(hidden)]
368 /// INTERNAL: Retakes ownership of the user callback data
369 /// # Safety
370 /// Can return multiple mutable references to the `user_data`
371 unsafe fn user_data(&self) -> Option<Box<dyn FnMut()>>;
372 #[doc(hidden)]
373 /// INTERNAL: Get the raw user data of the widget
374 /// # Safety
375 /// Can return multiple mutable references to the `user_data`
376 unsafe fn raw_user_data(&self) -> *mut std::os::raw::c_void;
377 #[doc(hidden)]
378 /// INTERNAL: Set the raw user data of the widget
379 /// # Safety
380 /// Can return multiple mutable references to the `user_data`
381 unsafe fn set_raw_user_data(&mut self, data: *mut std::os::raw::c_void);
382 /// Upcast a `WidgetExt` to some widget type
383 /// # Safety
384 /// Allows for potentially unsafe casts between incompatible widget types
385 #[allow(clippy::wrong_self_convention)]
386 unsafe fn into_widget<W: WidgetBase>(&self) -> W
387 where
388 Self: Sized;
389 /// Upcast a `WidgetExt` to a Widget
390 fn as_base_widget(&self) -> crate::widget::Widget
391 where
392 Self: Sized,
393 {
394 unsafe { self.into_widget() }
395 }
396 /// Returns whether a widget is visible
397 fn visible(&self) -> bool;
398 /// Returns whether a widget or any of its parents are visible (recursively)
399 fn visible_r(&self) -> bool;
400 /// Return whether two widgets object point to the same widget
401 fn is_same<W: WidgetExt>(&self, other: &W) -> bool
402 where
403 Self: Sized;
404 /// Returns whether a widget is active
405 fn active(&self) -> bool;
406 /// Returns whether a widget or any of its parents are active (recursively)
407 fn active_r(&self) -> bool;
408 #[doc(hidden)]
409 /**
410 Return the default callback function, this allows storing then running within the overridden callback.
411 Works only for FLTK types (with no callback defined in the Rust side)
412 ```rust,no_run
413 use fltk::{prelude::*, *};
414 fn main() {
415 let a = app::App::default();
416 let mut win = window::Window::default().with_size(400, 300);
417 let scroll = group::Scroll::default().size_of_parent();
418 let _btn = button::Button::new(160, 500, 80, 40, "click");
419 let mut scrollbar = scroll.scrollbar();
420 scrollbar.set_callback({
421 let mut cb = scrollbar.callback();
422 move |_| {
423 println!("print something, and also run the default callback");
424 if let Some(cb) = cb.as_mut() {
425 (*cb)();
426 }
427 }
428 });
429 win.end();
430 win.show();
431 a.run().unwrap();
432 }
433 ```
434 */
435 fn callback(&self) -> Option<Box<dyn FnMut()>>;
436 /// Does a simple resize ignoring class-specific resize functionality
437 fn widget_resize(&mut self, x: i32, y: i32, w: i32, h: i32);
438 /// Handle a specific event
439 fn handle_event(&mut self, event: Event) -> bool;
440 /// Check whether a widget is derived
441 fn is_derived(&self) -> bool {
442 unimplemented!();
443 }
444}
445
446/// Defines the extended methods implemented by all widgets
447/// # Safety
448/// fltk-rs traits depend on some FLTK internal code
449/// # Warning
450/// fltk-rs traits are non-exhaustive,
451/// to avoid future breakage if you try to implement them manually,
452/// use the Deref and DerefMut pattern or the `widget_extends!` macro
453pub unsafe trait WidgetBase: WidgetExt {
454 /// Creates a new widget, takes an x, y coordinates, as well as a width and height, plus a title
455 /// # Arguments
456 /// * `x` - The x coordinate in the screen
457 /// * `y` - The y coordinate in the screen
458 /// * `width` - The width of the widget
459 /// * `heigth` - The height of the widget
460 /// * `title` - The title or label of the widget
461 ///
462 /// To use dynamic strings use `with_label(self, &str)` or `set_label(&mut self, &str)`.
463 /// labels support special symbols preceded by an `@` [sign](https://www.fltk.org/doc-1.3/symbols.png)
464 /// and for the [associated formatting](https://www.fltk.org/doc-1.3/common.html).
465 fn new<'a, T: Into<Option<&'a str>>>(x: i32, y: i32, width: i32, height: i32, title: T)
466 -> Self;
467 /// Constructs a widget with the size of its parent
468 fn default_fill() -> Self;
469 /// Deletes widgets and their children.
470 fn delete(wid: Self)
471 where
472 Self: Sized;
473 /// transforms a widget pointer to a Widget, for internal use
474 /// # Safety
475 /// The pointer must be valid
476 unsafe fn from_widget_ptr(ptr: *mut fltk_sys::widget::Fl_Widget) -> Self;
477 /// Get a widget from base widget
478 /// # Safety
479 /// The underlying object must be valid
480 unsafe fn from_widget<W: WidgetExt>(w: W) -> Self;
481 /// Set a custom handler, where events are managed manually, akin to `Fl_Widget::handle(int)`.
482 /// Handled or ignored events should return true, unhandled events should return false.
483 /// takes the widget as a closure argument.
484 /// The ability to handle an event might depend on handling other events, as explained [here](https://www.fltk.org/doc-1.4/events.html)
485 fn handle<F: FnMut(&mut Self, Event) -> bool + 'static>(&mut self, cb: F);
486 /// Set a custom draw method.
487 /// takes the widget as a closure argument.
488 /// macOS requires that `WidgetBase::draw` actually calls drawing functions
489 fn draw<F: FnMut(&mut Self) + 'static>(&mut self, cb: F);
490 #[doc(hidden)]
491 /// INTERNAL: Retrieve the draw data
492 /// # Safety
493 /// Can return multiple mutable references to the `draw_data`
494 unsafe fn draw_data(&self) -> Option<Box<dyn FnMut()>>;
495 #[doc(hidden)]
496 /// INTERNAL: Retrieve the handle data
497 /// # Safety
498 /// Can return multiple mutable references to the `handle_data`
499 unsafe fn handle_data(&self) -> Option<Box<dyn FnMut(Event) -> bool>>;
500 /// Perform a callback on resize.
501 /// Avoid resizing the parent or the same widget to avoid infinite recursion
502 fn resize_callback<F: FnMut(&mut Self, i32, i32, i32, i32) + 'static>(&mut self, cb: F);
503 /// Makes the widget derived
504 /// # Safety
505 /// Calling this on a non-derived widget can cause undefined behavior
506 unsafe fn assume_derived(&mut self) {
507 unimplemented!();
508 }
509 /// Cast a type-erased widget back to its original widget
510 fn from_dyn_widget<W: WidgetExt>(_w: &W) -> Option<Self>
511 where
512 Self: Sized,
513 {
514 None
515 }
516 /// Cast a type-erased widget pointer back to its original widget
517 fn from_dyn_widget_ptr(_w: *mut fltk_sys::widget::Fl_Widget) -> Option<Self>
518 where
519 Self: Sized,
520 {
521 None
522 }
523 #[doc(hidden)]
524 /// Determine whether the base class's draw method is called, default is true
525 fn super_draw(&mut self, flag: bool) {
526 let _ = flag;
527 }
528 #[doc(hidden)]
529 /// Determine whether the base class's draw method is called last, default is true
530 fn super_draw_first(&mut self, flag: bool) {
531 let _ = flag;
532 }
533 #[doc(hidden)]
534 /// Determine whether the base class's handle method should have an event propagated even
535 /// if handled by the instantiated widget
536 fn super_handle_first(&mut self, flag: bool) {
537 let _ = flag;
538 }
539}
540
541/// Defines the methods implemented by all button widgets.
542/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/buttons).
543/// # Safety
544/// fltk-rs traits depend on some FLTK internal code
545/// # Warning
546/// fltk-rs traits are non-exhaustive,
547/// to avoid future breakage if you try to implement them manually,
548/// use the Deref and DerefMut pattern or the `widget_extends!` macro
549pub unsafe trait ButtonExt: WidgetExt {
550 /// Gets the shortcut associated with a button
551 fn shortcut(&self) -> Shortcut;
552 /// Sets the shortcut associated with a button
553 fn set_shortcut(&mut self, shortcut: Shortcut);
554 /// Clears the value of the button.
555 /// Useful for round, radio, light, toggle and check buttons
556 fn clear(&mut self);
557 /// Returns whether a button is set or not.
558 /// Useful for round, radio, light, toggle and check buttons
559 fn is_set(&self) -> bool;
560 /// Sets whether a button is set or not.
561 /// Useful for round, radio, light, toggle and check buttons
562 fn set(&mut self, flag: bool);
563 /// Returns whether a button is set or not.
564 /// Useful for round, radio, light, toggle and check buttons
565 fn value(&self) -> bool;
566 /// Sets whether a button is set or not.
567 /// Useful for round, radio, light, toggle and check buttons
568 fn set_value(&mut self, flag: bool);
569 /// Set the `down_box` of the widget
570 fn set_down_frame(&mut self, f: FrameType);
571 /// Get the down frame type of the widget
572 fn down_frame(&self) -> FrameType;
573}
574
575/// Defines the methods implemented by all group widgets.
576/// These widgets include Window types and others found in the group module: Group, Scroll, Pack, Tile, Flex ...etc.
577/// Widgets implementing the GroupExt trait, are characterized by having to call `::end()` method to basically close them.
578/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/group_widgets).
579/// ```rust
580/// use fltk::{app, button::Button, window::Window, prelude::GroupExt};
581/// let a = app::App::default();
582/// let win = Window::default();
583/// let btn = Button::default();
584/// // Instantiate other widgets
585/// win.end();
586/// ```
587/// In the above example, the button `btn` will be parented by the window.
588/// After `end`ing such GroupExt widgets, any other widgets instantiated after the `end` call, will be instantiated outside.
589/// These can still be added using the `::add(&other_widget)` method (or using `::insert`):
590/// ```rust
591/// use fltk::{app, button::Button, window::Window, prelude::GroupExt};
592/// let a = app::App::default();
593/// let mut win = Window::default();
594/// win.end();
595/// let btn = Button::default();
596/// win.add(&btn);
597/// ```
598/// Another option is to reopen the widget:
599/// ```rust
600/// use fltk::{app, button::Button, window::Window, prelude::GroupExt};
601/// let a = app::App::default();
602/// let win = Window::default();
603/// win.end();
604/// win.begin();
605/// let btn = Button::default();
606/// // other widgets
607/// win.end();
608/// ```
609/// # Safety
610/// fltk-rs traits depend on some FLTK internal code
611/// # Warning
612/// fltk-rs traits are non-exhaustive,
613/// to avoid future breakage if you try to implement them manually,
614/// use the Deref and DerefMut pattern or the `widget_extends!` macro
615pub unsafe trait GroupExt: WidgetExt {
616 /// Begins a group, used for widgets implementing the group trait
617 fn begin(&self);
618 /// Ends a group, used for widgets implementing the group trait
619 fn end(&self);
620 /// Clear a group from all widgets
621 fn clear(&mut self);
622 #[doc(hidden)]
623 /// Clear a group from all widgets using FLTK's clear call.
624 /// # Safety
625 /// Ignores widget tracking
626 unsafe fn unsafe_clear(&mut self);
627 /// Return the number of children in a group
628 fn children(&self) -> i32;
629 /// Return child widget by index
630 fn child(&self, idx: i32) -> Option<crate::widget::Widget>;
631 /// Find a widget within a group and return its index
632 fn find<W: WidgetExt>(&self, widget: &W) -> i32
633 where
634 Self: Sized;
635 /// Add a widget to a group
636 fn add<W: WidgetExt>(&mut self, widget: &W)
637 where
638 Self: Sized;
639 /// Insert a widget to a group at a certain index
640 fn insert<W: WidgetExt>(&mut self, widget: &W, index: i32)
641 where
642 Self: Sized;
643 /// Remove a widget from a group, but does not delete it
644 fn remove<W: WidgetExt>(&mut self, widget: &W)
645 where
646 Self: Sized;
647 /// Remove a child widget by its index
648 fn remove_by_index(&mut self, idx: i32);
649 /// The resizable widget defines both the resizing frame and the resizing behavior of the group and its children.
650 fn resizable<W: WidgetExt>(&self, widget: &W)
651 where
652 Self: Sized;
653 /// Make the group itself resizable, should be called before the widget is shown
654 fn make_resizable(&mut self, val: bool);
655 /// Adds a widget to the group and makes it the resizable widget
656 fn add_resizable<W: WidgetExt>(&mut self, widget: &W)
657 where
658 Self: Sized;
659 /// Clips children outside the group boundaries
660 fn set_clip_children(&mut self, flag: bool);
661 /// Get whether `clip_children` is set
662 fn clip_children(&self) -> bool;
663 /// Draw a child widget, the call should be in a [`WidgetBase::draw`](`crate::prelude::WidgetBase::draw`) method
664 fn draw_child<W: WidgetExt>(&self, w: &mut W)
665 where
666 Self: Sized;
667 /// Update a child widget, the call should be in a [`WidgetBase::draw`](`crate::prelude::WidgetBase::draw`) method
668 fn update_child<W: WidgetExt>(&self, w: &mut W)
669 where
670 Self: Sized;
671 /// Draw the outside label, the call should be in a [`WidgetBase::draw`](`crate::prelude::WidgetBase::draw`) method
672 fn draw_outside_label<W: WidgetExt>(&self, w: &mut W)
673 where
674 Self: Sized;
675 /// Draw children, the call should be in a [`WidgetBase::draw`](`crate::prelude::WidgetBase::draw`) method
676 fn draw_children(&mut self);
677 /// Resets the internal array of widget sizes and positions
678 fn init_sizes(&mut self);
679 /// Get the bounds of all children widgets (left, upper, right, bottom)
680 fn bounds(&self) -> Vec<(i32, i32, i32, i32)>;
681 /// Converts a widget implementing GroupExt into a Group widget
682 /// # Safety
683 /// If the widget wasn't created by fltk-rs,
684 /// vtable differences mean certain methods can't be overridden (e.g. handle & draw)
685 #[allow(clippy::wrong_self_convention)]
686 unsafe fn into_group(&self) -> crate::group::Group;
687}
688
689/// Defines the methods implemented by all window widgets.
690/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/windows).
691/// Windows (which can be found in the window module) implement GroupExt as well.
692/// # Safety
693/// fltk-rs traits depend on some FLTK internal code
694/// # Warning
695/// fltk-rs traits are non-exhaustive,
696/// to avoid future breakage if you try to implement them manually,
697/// use the Deref and DerefMut pattern or the `widget_extends!` macro
698pub unsafe trait WindowExt: GroupExt {
699 /// Positions the window to the center of the screen
700 fn center_screen(self) -> Self
701 where
702 Self: Sized;
703 /// Makes a window modal, should be called before `show`
704 fn make_modal(&mut self, val: bool);
705 /// Makes a window fullscreen.
706 /// Requires that the window is resizable.
707 fn fullscreen(&mut self, val: bool);
708 /// Makes the window current
709 fn make_current(&mut self);
710 /// Returns the icon of the window
711 fn icon(&self) -> Option<Box<dyn ImageExt>>;
712 /// Sets the windows icon.
713 /// Supported formats are bmp, jpeg, png and rgb.
714 fn set_icon<T: ImageExt>(&mut self, image: Option<T>)
715 where
716 Self: Sized;
717 /// Sets the cursor style within the window.
718 /// Needs to be called after the window is shown
719 fn set_cursor(&mut self, cursor: Cursor);
720 /// Returns whether a window is shown
721 fn shown(&self) -> bool;
722 /// Sets whether the window has a border
723 fn set_border(&mut self, flag: bool);
724 /// Returns whether a window has a border
725 fn border(&self) -> bool;
726 /// Frees the position of the window
727 fn free_position(&mut self);
728 /// Get the raw system handle of the window
729 fn raw_handle(&self) -> crate::window::RawHandle;
730 #[doc(hidden)]
731 /// Set the window associated with a raw handle.
732 /// `RawHandle` is a void pointer to: (Windows: `HWND`, X11: `Xid` (`u64`), macOS: `NSWindow`)
733 /// # Safety
734 /// The data must be valid and is OS-dependent. The window must be shown.
735 unsafe fn set_raw_handle(&mut self, handle: crate::window::RawHandle);
736 /// Get the graphical draw region of the window
737 fn region(&self) -> crate::draw::Region;
738 /// Set the graphical draw region of the window
739 /// # Safety
740 /// The data must be valid.
741 unsafe fn set_region(&mut self, region: crate::draw::Region);
742 /// Iconifies the window.
743 /// You can tell that the window is iconized by checking that it's shown and not visible
744 fn iconize(&mut self);
745 /// Returns whether the window is fullscreen or not
746 fn fullscreen_active(&self) -> bool;
747 /// Returns the decorated width
748 fn decorated_w(&self) -> i32;
749 /// Returns the decorated height
750 fn decorated_h(&self) -> i32;
751 /// Set the window's minimum width, minimum height, max width and max height.
752 /// You can pass 0 as max_w and max_h to allow unlimited upward resize of the window.
753 fn size_range(&mut self, min_w: i32, min_h: i32, max_w: i32, max_h: i32);
754 /// Set the hotspot widget of the window
755 fn hotspot<W: WidgetExt>(&mut self, w: &W)
756 where
757 Self: Sized;
758 /// Set the shape of the window.
759 /// Supported image formats are BMP, RGB and Pixmap.
760 /// The window covers non-transparent/non-black shape of the image.
761 /// The image must not be scaled(resized) beforehand.
762 /// The size will be adapted to the window's size
763 fn set_shape<I: ImageExt>(&mut self, image: Option<I>)
764 where
765 Self: Sized;
766 /// Get the shape of the window
767 fn shape(&self) -> Option<Box<dyn ImageExt>>;
768 /// Get the window's x coord from the screen
769 fn x_root(&self) -> i32;
770 /// Get the window's y coord from the screen
771 fn y_root(&self) -> i32;
772 /// Set the cursor image
773 fn set_cursor_image(&mut self, image: crate::image::RgbImage, hot_x: i32, hot_y: i32);
774 /// Set the window's default cursor
775 fn default_cursor(&mut self, cursor: Cursor);
776 /// Get the screen number
777 fn screen_num(&self) -> i32;
778 /// Set the screen number
779 fn set_screen_num(&mut self, n: i32);
780 /// wait for the window to be displayed after calling `show()`.
781 /// More info [here](https://www.fltk.org/doc-1.4/classFl__Window.html#aafbec14ca8ff8abdaff77a35ebb23dd8)
782 fn wait_for_expose(&self);
783 /// Get the window's opacity
784 fn opacity(&self) -> f64;
785 /// Set the window's opacity,
786 /// Ranges from 0.0 to 1.0, where 1.0 is fully opaque and 0.0 is fully transparent.
787 /// This should be called on a shown window.
788 /// On X11, opacity support depends on the window manager and can be queried:
789 /// ```ignore
790 /// $ xprop -root _NET_SUPPORTED | grep -o _NET_WM_WINDOW_OPACITY
791 /// ```
792 fn set_opacity(&mut self, val: f64);
793 /// Get the window's XA_WM_CLASS property
794 fn xclass(&self) -> Option<String>;
795 /// Set the window's XA_WM_CLASS property.
796 /// This should be called before showing the window
797 fn set_xclass(&mut self, s: &str);
798 /// Clear the modal state of the window
799 fn clear_modal_states(&mut self);
800 /// removes the window border and sets the window on top, by settings the NOBORDER and OVERRIDE flags
801 fn set_override(&mut self);
802 /// Checks whether the OVERRIDE flag was set
803 fn is_override(&self) -> bool;
804 /// Forces the position of the window
805 fn force_position(&mut self, flag: bool);
806 /// Set the icon label
807 fn set_icon_label(&mut self, label: &str);
808 /// Get the icon label
809 fn icon_label(&self) -> Option<String>;
810 /// Allow the window to expand outside its parent (if supported by the platform/driver)
811 fn allow_expand_outside_parent(&mut self);
812 /// Return the OS-specific window id/handle as an integer (useful for interop)
813 fn os_id(&self) -> usize;
814}
815
816/// Defines the methods implemented by all input and output widgets.
817/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/inout_widgets).
818/// # Safety
819/// fltk-rs traits depend on some FLTK internal code
820/// # Warning
821/// fltk-rs traits are non-exhaustive,
822/// to avoid future breakage if you try to implement them manually,
823/// use the Deref and DerefMut pattern or the `widget_extends!` macro
824pub unsafe trait InputExt: WidgetExt {
825 /// Returns the value inside the input/output widget
826 fn value(&self) -> String;
827 /// Sets the value inside an input/output widget
828 fn set_value(&mut self, val: &str);
829 /// Returns the maximum size (in bytes) accepted by an input/output widget
830 fn maximum_size(&self) -> i32;
831 /// Sets the maximum size (in bytes) accepted by an input/output widget
832 fn set_maximum_size(&mut self, val: i32);
833 /// Returns the index position inside an input/output widget
834 fn position(&self) -> i32;
835 /// Sets the index position inside an input/output widget
836 /// # Errors
837 /// Errors on failure to set the cursor position in the text
838 fn set_position(&mut self, val: i32) -> Result<(), FltkError>;
839 /// Returns the index mark inside an input/output widget
840 fn mark(&self) -> i32;
841 /// Sets the index mark inside an input/output widget
842 /// # Errors
843 /// Errors on failure to set the mark
844 fn set_mark(&mut self, val: i32) -> Result<(), FltkError>;
845 /// Replace content with a &str
846 /// # Errors
847 /// Errors on failure to replace text
848 fn replace(&mut self, beg: i32, end: i32, val: &str) -> Result<(), FltkError>;
849 /// Insert a &str
850 /// # Errors
851 /// Errors on failure to insert text
852 fn insert(&mut self, txt: &str) -> Result<(), FltkError>;
853 /// Append a &str
854 /// # Errors
855 /// Errors on failure to append text
856 fn append(&mut self, txt: &str) -> Result<(), FltkError>;
857 /// Copy the value within the widget
858 /// # Errors
859 /// Errors on failure to copy selection
860 fn copy(&mut self) -> Result<(), FltkError>;
861 /// Undo changes
862 /// # Errors
863 /// Errors on failure to undo
864 fn undo(&mut self) -> Result<(), FltkError>;
865 /// Cut the value within the widget
866 /// # Errors
867 /// Errors on failure to cut selection
868 fn cut(&mut self) -> Result<(), FltkError>;
869 /// Return the cursor color
870 fn cursor_color(&self) -> Color;
871 /// Sets the cursor color
872 fn set_cursor_color(&mut self, color: Color);
873 /// Return the text font
874 fn text_font(&self) -> Font;
875 /// Sets the text font
876 fn set_text_font(&mut self, font: Font);
877 /// Return the text color
878 fn text_color(&self) -> Color;
879 /// Sets the text color
880 fn set_text_color(&mut self, color: Color);
881 /// Return the text size
882 fn text_size(&self) -> i32;
883 /// Sets the text size
884 fn set_text_size(&mut self, sz: i32);
885 /// Returns whether the input/output widget is readonly
886 fn readonly(&self) -> bool;
887 /// Set readonly status of the input/output widget
888 fn set_readonly(&mut self, val: bool);
889 /// Return whether text is wrapped inside an input/output widget
890 fn wrap(&self) -> bool;
891 /// Set whether text is wrapped inside an input/output widget
892 fn set_wrap(&mut self, val: bool);
893 /// Sets whether tab navigation is enabled, true by default
894 fn set_tab_nav(&mut self, val: bool);
895 /// Returns whether tab navigation is enabled
896 fn tab_nav(&self) -> bool;
897}
898
899/// Defines the methods implemented by all menu widgets
900/// These are found in the menu module: MenuBar, SysMenuBar, Choice, MenuButton ...etc.
901/// Menus function in 2 main ways which are discussed in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/menus)
902/// # Safety
903/// fltk-rs traits depend on some FLTK internal code
904/// # Warning
905/// fltk-rs traits are non-exhaustive,
906/// to avoid future breakage if you try to implement them manually,
907/// use the Deref and DerefMut pattern or the `widget_extends!` macro
908pub unsafe trait MenuExt: WidgetExt {
909 /// Get a menu item by name
910 fn find_item(&self, name: &str) -> Option<crate::menu::MenuItem>;
911 /// Set selected item
912 fn set_item(&mut self, item: &crate::menu::MenuItem) -> bool;
913 /// Find an item's index by its label
914 fn find_index(&self, label: &str) -> i32;
915 /// Return the text font
916 fn text_font(&self) -> Font;
917 /// Sets the text font
918 fn set_text_font(&mut self, c: Font);
919 /// Return the text size
920 fn text_size(&self) -> i32;
921 /// Sets the text size
922 fn set_text_size(&mut self, c: i32);
923 /// Return the text color
924 fn text_color(&self) -> Color;
925 /// Sets the text color
926 fn set_text_color(&mut self, c: Color);
927 /// Add a menu item along with its callback.
928 /// The characters "&", "/", "\\", and "\_" (underscore) are treated as special characters in the label string. The "&" character specifies that the following character is an accelerator and will be underlined.
929 /// The "\\" character is used to escape the next character in the string. Labels starting with the "\_" (underscore) character cause a divider to be placed after that menu item.
930 /// Takes the menu item as a closure argument
931 fn add<F: FnMut(&mut Self) + 'static>(
932 &mut self,
933 name: &str,
934 shortcut: Shortcut,
935 flag: crate::menu::MenuFlag,
936 cb: F,
937 ) -> i32
938 where
939 Self: Sized;
940 /// Inserts a menu item at an index along with its callback.
941 /// The characters "&", "/", "\\", and "\_" (underscore) are treated as special characters in the label string. The "&" character specifies that the following character is an accelerator and will be underlined.
942 /// The "\\" character is used to escape the next character in the string. Labels starting with the "\_" (underscore) character cause a divider to be placed after that menu item.
943 /// Takes the menu item as a closure argument
944 fn insert<F: FnMut(&mut Self) + 'static>(
945 &mut self,
946 idx: i32,
947 name: &str,
948 shortcut: Shortcut,
949 flag: crate::menu::MenuFlag,
950 cb: F,
951 ) -> i32
952 where
953 Self: Sized;
954 /// Add a menu item along with an emit (sender and message).
955 /// The characters "&", "/", "\\", and "\_" (underscore) are treated as special characters in the label string. The "&" character specifies that the following character is an accelerator and will be underlined.
956 /// The "\\" character is used to escape the next character in the string. Labels starting with the "\_" (underscore) character cause a divider to be placed after that menu item.
957 fn add_emit<T: 'static + Clone + Send + Sync>(
958 &mut self,
959 label: &str,
960 shortcut: Shortcut,
961 flag: crate::menu::MenuFlag,
962 sender: crate::app::Sender<T>,
963 msg: T,
964 ) -> i32
965 where
966 Self: Sized;
967 /// Inserts a menu item along with an emit (sender and message).
968 /// The characters "&", "/", "\\", and "\_" (underscore) are treated as special characters in the label string. The "&" character specifies that the following character is an accelerator and will be underlined.
969 /// The "\\" character is used to escape the next character in the string. Labels starting with the "\_" (underscore) character cause a divider to be placed after that menu item.
970 fn insert_emit<T: 'static + Clone + Send + Sync>(
971 &mut self,
972 idx: i32,
973 label: &str,
974 shortcut: Shortcut,
975 flag: crate::menu::MenuFlag,
976 sender: crate::app::Sender<T>,
977 msg: T,
978 ) -> i32
979 where
980 Self: Sized;
981 /// Remove a menu item by index
982 fn remove(&mut self, idx: i32);
983 /// Adds a simple text option to the Choice and `MenuButton` widgets.
984 /// The characters "&", "/", "\\", "|", and "\_" (underscore) are treated as special characters in the label string. The "&" character specifies that the following character is an accelerator and will be underlined.
985 /// The "\\" character is used to escape the next character in the string. Labels starting with the "\_" (underscore) character cause a divider to be placed after that menu item.
986 fn add_choice(&mut self, text: &str) -> i32;
987 /// Gets the user choice from the Choice and `MenuButton` widgets
988 fn choice(&self) -> Option<String>;
989 /// Get index into menu of the last item chosen, returns -1 if no item was chosen
990 fn value(&self) -> i32;
991 /// Set index into menu of the last item chosen,return true if the new value is different than the old one
992 fn set_value(&mut self, v: i32) -> bool;
993 /// Clears the items in a menu, effectively deleting them.
994 fn clear(&mut self);
995 /// Clears a submenu by index
996 /// # Errors
997 /// Errors on failure to clear the submenu, failure returns an [`FltkErrorKind::FailedOperation`](`crate::prelude::FltkErrorKind::FailedOperation`)
998 fn clear_submenu(&mut self, idx: i32) -> Result<(), FltkError>;
999 /// Clears the items in a menu, effectively deleting them, and recursively force-cleans capturing callbacks
1000 /// # Safety
1001 /// Deletes `user_data` and any captured objects in the callback
1002 unsafe fn unsafe_clear(&mut self);
1003 #[doc(hidden)]
1004 /// Clears a submenu by index. Also recursively force-cleans capturing callbacks
1005 /// # Safety
1006 /// Deletes `user_data` and any captured objects in the callback, , failure returns an [`FltkErrorKind::FailedOperation`](`crate::prelude::FltkErrorKind::FailedOperation`)
1007 /// # Errors
1008 /// Errors on failure to clear the submenu
1009 unsafe fn unsafe_clear_submenu(&mut self, idx: i32) -> Result<(), FltkError>;
1010 /// Get the size of the menu widget
1011 fn size(&self) -> i32;
1012 /// Get the text label of the menu item at index idx
1013 fn text(&self, idx: i32) -> Option<String>;
1014 /// Get the menu item at an index
1015 fn at(&self, idx: i32) -> Option<crate::menu::MenuItem>;
1016 /// Get the mode of a menu item by index and flag
1017 fn mode(&self, idx: i32) -> crate::menu::MenuFlag;
1018 /// Set the mode of a menu item
1019 fn set_mode(&mut self, idx: i32, flag: crate::menu::MenuFlag);
1020 /// End the menu
1021 fn end(&mut self);
1022 /// Set the `down_box` of the widget
1023 fn set_down_frame(&mut self, f: FrameType);
1024 /// Get the down frame type of the widget
1025 fn down_frame(&self) -> FrameType;
1026 /// Make a menu globally accessible from any window
1027 fn global(&mut self);
1028 /// Get the menu element
1029 fn menu(&self) -> Option<crate::menu::MenuItem>;
1030 /// Set the menu element
1031 /// # Safety
1032 /// The MenuItem must be in a format recognized by FLTK (Empty CMenuItem after submenus and at the end of the menu)
1033 unsafe fn set_menu(&mut self, item: crate::menu::MenuItem);
1034 /// Get an item's pathname
1035 fn item_pathname(&self, item: Option<&crate::menu::MenuItem>) -> Result<String, FltkError>;
1036 /// Set the menu's popup frame type
1037 fn set_menu_frame(&mut self, f: FrameType);
1038 /// Get the menu's popup frame type
1039 fn menu_frame(&self) -> FrameType;
1040 /// Get the selected menu item
1041 fn mvalue(&self) -> Option<crate::menu::MenuItem>;
1042 /// Get the previously selected menu item
1043 fn prev_mvalue(&self) -> Option<crate::menu::MenuItem>;
1044}
1045
1046/// Defines the methods implemented by all valuator widgets
1047/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/valuators).
1048/// # Safety
1049/// fltk-rs traits depend on some FLTK internal code
1050/// # Warning
1051/// fltk-rs traits are non-exhaustive,
1052/// to avoid future breakage if you try to implement them manually,
1053/// use the Deref and DerefMut pattern or the `widget_extends!` macro
1054pub unsafe trait ValuatorExt: WidgetExt {
1055 /// Set bounds of a valuator
1056 fn set_bounds(&mut self, a: f64, b: f64);
1057 /// Get the minimum bound of a valuator
1058 fn minimum(&self) -> f64;
1059 /// Set the minimum bound of a valuator
1060 fn set_minimum(&mut self, a: f64);
1061 /// Get the maximum bound of a valuator
1062 fn maximum(&self) -> f64;
1063 /// Set the maximum bound of a valuator
1064 fn set_maximum(&mut self, a: f64);
1065 /// Set the range of a valuator
1066 fn set_range(&mut self, a: f64, b: f64);
1067 /// Set change step of a valuator.
1068 /// Rounds to multiples of a/b, or no rounding if a is zero
1069 fn set_step(&mut self, a: f64, b: i32);
1070 /// Get change step of a valuator
1071 fn step(&self) -> f64;
1072 /// Set the precision of a valuator
1073 fn set_precision(&mut self, digits: i32);
1074 /// Get the value of a valuator
1075 fn value(&self) -> f64;
1076 /// Set the value of a valuator
1077 fn set_value(&mut self, arg2: f64);
1078 /// Set the format of a valuator
1079 /// # Errors
1080 /// Errors on failure to set the format of the widget
1081 fn format(&mut self, arg2: &str) -> Result<(), FltkError>;
1082 /// Round the valuator
1083 fn round(&self, arg2: f64) -> f64;
1084 /// Clamp the valuator
1085 fn clamp(&self, arg2: f64) -> f64;
1086 /// Increment the valuator
1087 fn increment(&mut self, arg2: f64, arg3: i32) -> f64;
1088}
1089
1090/// Defines the methods implemented by `TextDisplay` and `TextEditor`
1091/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/text).
1092/// # Safety
1093/// fltk-rs traits depend on some FLTK internal code
1094/// # Warning
1095/// fltk-rs traits are non-exhaustive,
1096/// to avoid future breakage if you try to implement them manually,
1097/// use the Deref and DerefMut pattern or the `widget_extends!` macro
1098pub unsafe trait DisplayExt: WidgetExt {
1099 /// Check if the Display widget has an associated buffer
1100 #[doc(hidden)]
1101 fn has_buffer(&self) -> bool;
1102 /// Get the associated `TextBuffer`
1103 fn buffer(&self) -> Option<crate::text::TextBuffer>;
1104 /// Sets the associated `TextBuffer`.
1105 /// Since the widget is long-lived, the lifetime of the buffer is prolonged to the lifetime of the program
1106 fn set_buffer<B: Into<Option<crate::text::TextBuffer>>>(&mut self, buffer: B);
1107 /// Get the associated style `TextBuffer`
1108 fn style_buffer(&self) -> Option<crate::text::TextBuffer>;
1109 /// Return the text font
1110 fn text_font(&self) -> Font;
1111 /// Sets the text font
1112 fn set_text_font(&mut self, font: Font);
1113 /// Return the text color
1114 fn text_color(&self) -> Color;
1115 /// Sets the text color
1116 fn set_text_color(&mut self, color: Color);
1117 /// Return the text size
1118 fn text_size(&self) -> i32;
1119 /// Sets the text size
1120 fn set_text_size(&mut self, sz: i32);
1121 /// Scroll down the Display widget
1122 fn scroll(&mut self, top_line_num: i32, horiz_offset: i32);
1123 /// Insert into Display widget
1124 fn insert(&self, text: &str);
1125 /// Set the insert position
1126 fn set_insert_position(&mut self, new_pos: i32);
1127 /// Return the insert position
1128 fn insert_position(&self) -> i32;
1129 /// Gets the x and y positions of the cursor
1130 fn position_to_xy(&self, pos: i32) -> (i32, i32);
1131 /// Counts the lines from start to end
1132 fn count_lines(&self, start: i32, end: i32, is_line_start: bool) -> i32;
1133 /// Moves the cursor right
1134 /// # Errors
1135 /// Errors on failure to move the cursor
1136 fn move_right(&mut self) -> Result<(), FltkError>;
1137 /// Moves the cursor left
1138 /// # Errors
1139 /// Errors on failure to move the cursor
1140 fn move_left(&mut self) -> Result<(), FltkError>;
1141 /// Moves the cursor up
1142 /// # Errors
1143 /// Errors on failure to move the cursor
1144 fn move_up(&mut self) -> Result<(), FltkError>;
1145 /// Moves the cursor down
1146 /// # Errors
1147 /// Errors on failure to move the cursor
1148 fn move_down(&mut self) -> Result<(), FltkError>;
1149 /// Shows/hides the cursor
1150 fn show_cursor(&mut self, val: bool);
1151 /// Sets the style of the text widget
1152 fn set_highlight_data<
1153 B: Into<Option<crate::text::TextBuffer>>,
1154 E: Into<Vec<crate::text::StyleTableEntry>>,
1155 >(
1156 &mut self,
1157 style_buffer: B,
1158 entries: E,
1159 );
1160 /// Sets the style of the text widget
1161 fn set_highlight_data_ext<
1162 B: Into<Option<crate::text::TextBuffer>>,
1163 E: Into<Vec<crate::text::StyleTableEntryExt>>,
1164 >(
1165 &mut self,
1166 style_buffer: B,
1167 entries: E,
1168 );
1169 /// Unset the style of the text widget
1170 fn unset_highlight_data<B: Into<Option<crate::text::TextBuffer>>>(&mut self, style_buffer: B);
1171 /// Sets the cursor style
1172 fn set_cursor_style(&mut self, style: crate::text::Cursor);
1173 /// Sets the cursor color
1174 fn set_cursor_color(&mut self, color: Color);
1175 /// Sets the scrollbar size in pixels
1176 fn set_scrollbar_size(&mut self, size: i32);
1177 /// Sets the scrollbar alignment
1178 fn set_scrollbar_align(&mut self, align: Align);
1179 /// Returns the cursor style
1180 fn cursor_style(&self) -> crate::text::Cursor;
1181 /// Returns the cursor color
1182 fn cursor_color(&self) -> Color;
1183 /// Returns the scrollbar size in pixels
1184 fn scrollbar_size(&self) -> i32;
1185 /// Returns the scrollbar alignment
1186 fn scrollbar_align(&self) -> Align;
1187 /// Returns the beginning of the line from the current position.
1188 /// Returns new position as index
1189 fn line_start(&self, pos: i32) -> i32;
1190 /// Returns the ending of the line from the current position.
1191 /// Returns new position as index
1192 fn line_end(&self, start_pos: i32, is_line_start: bool) -> i32;
1193 /// Skips lines from `start_pos`
1194 fn skip_lines(&mut self, start_pos: i32, lines: i32, is_line_start: bool) -> i32;
1195 /// Rewinds the lines
1196 fn rewind_lines(&mut self, start_pos: i32, lines: i32) -> i32;
1197 /// Goes to the next word
1198 fn next_word(&mut self);
1199 /// Goes to the previous word
1200 fn previous_word(&mut self);
1201 /// Returns the position of the start of the word, relative to the current position
1202 fn word_start(&self, pos: i32) -> i32;
1203 /// Returns the position of the end of the word, relative to the current position
1204 fn word_end(&self, pos: i32) -> i32;
1205 /// Convert an x pixel position into a column number.
1206 fn x_to_col(&self, x: f64) -> f64;
1207 /// Convert a column number into an x pixel position
1208 fn col_to_x(&self, col: f64) -> f64;
1209 /// Sets the linenumber width
1210 fn set_linenumber_width(&mut self, w: i32);
1211 /// Gets the linenumber width
1212 fn linenumber_width(&self) -> i32;
1213 /// Sets the linenumber font
1214 fn set_linenumber_font(&mut self, font: Font);
1215 /// Gets the linenumber font
1216 fn linenumber_font(&self) -> Font;
1217 /// Sets the linenumber size
1218 fn set_linenumber_size(&mut self, size: i32);
1219 /// Gets the linenumber size
1220 fn linenumber_size(&self) -> i32;
1221 /// Sets the linenumber foreground color
1222 fn set_linenumber_fgcolor(&mut self, color: Color);
1223 /// Gets the linenumber foreground color
1224 fn linenumber_fgcolor(&self) -> Color;
1225 /// Sets the linenumber background color
1226 fn set_linenumber_bgcolor(&mut self, color: Color);
1227 /// Gets the linenumber background color
1228 fn linenumber_bgcolor(&self) -> Color;
1229 /// Sets the linenumber alignment
1230 fn set_linenumber_align(&mut self, align: Align);
1231 /// Gets the linenumber alignment
1232 fn linenumber_align(&self) -> Align;
1233 /// Sets the line number format string
1234 fn set_linenumber_format(&mut self, fmt: &str);
1235 /// Gets the current line number format string
1236 fn linenumber_format(&self) -> Option<String>;
1237 /// Query style position in line
1238 fn position_style(&self, line_start_pos: i32, line_len: i32, line_index: i32) -> i32;
1239 /// Maintain absolute top line number state
1240 fn maintain_absolute_top_line_number(&mut self, state: bool);
1241 /// Get absolute top line number
1242 fn get_absolute_top_line_number(&self) -> i32;
1243 /// Update absolute top line number with old first char
1244 fn absolute_top_line_number(&mut self, old_first_char: i32);
1245 /// Return whether maintaining absolute top line number is enabled
1246 fn maintaining_absolute_top_line_number(&self) -> bool;
1247 /// Reset absolute top line number tracking
1248 fn reset_absolute_top_line_number(&mut self);
1249 /// Checks whether a pixel is within a text selection
1250 fn in_selection(&self, x: i32, y: i32) -> bool;
1251 /// Sets the wrap mode of the Display widget.
1252 /// If the wrap mode is `AtColumn`, wrap margin is the column.
1253 /// If the wrap mode is `AtPixel`, wrap margin is the pixel.
1254 /// For more [info](https://www.fltk.org/doc-1.4/classFl__Text__Display.html#ab9378d48b949f8fc7da04c6be4142c54)
1255 fn wrap_mode(&mut self, wrap: crate::text::WrapMode, wrap_margin: i32);
1256 /// Correct a column number based on an unconstrained position
1257 fn wrapped_column(&self, row: i32, column: i32) -> i32;
1258 /// Correct a row number from an unconstrained position
1259 fn wrapped_row(&self, row: i32) -> i32;
1260 /// Set the grammar underline color
1261 fn set_grammar_underline_color(&mut self, color: Color);
1262 /// Get the grammar underline color
1263 fn grammar_underline_color(&self) -> Color;
1264 /// Set the spelling underline color
1265 fn set_spelling_underline_color(&mut self, color: Color);
1266 /// Get the spelling underline color
1267 fn spelling_underline_color(&self) -> Color;
1268 /// Set the secondary selection color
1269 fn set_secondary_selection_color(&mut self, color: Color);
1270 /// Get the secondary selection color
1271 fn secondary_selection_color(&self) -> Color;
1272 /// Scrolls the text buffer to show the current insert position
1273 fn show_insert_position(&mut self);
1274 /// Overstrikes the current selection or inserts text at cursor
1275 fn overstrike(&mut self, text: &str);
1276 /// Redisplay a range of text
1277 fn redisplay_range(&mut self, start: i32, end: i32);
1278 /// Converts x and y pixel positions into a position in the text buffer
1279 fn xy_to_position(&self, x: i32, y: i32, pos_type: crate::text::PositionType) -> i32;
1280 /// Converts x and y pixel positions into a column and row number
1281 fn xy_to_rowcol(&self, x: i32, y: i32, pos_type: crate::text::PositionType) -> (i32, i32);
1282 /// Returns the number of rows
1283 fn scroll_row(&self) -> i32;
1284 /// Returns the number of columns
1285 fn scroll_col(&self) -> i32;
1286}
1287
1288/// Defines the methods implemented by all browser types
1289/// More info can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/browsers)
1290/// # Safety
1291/// fltk-rs traits depend on some FLTK internal code
1292/// # Warning
1293/// fltk-rs traits are non-exhaustive,
1294/// to avoid future breakage if you try to implement them manually,
1295/// use the Deref and DerefMut pattern or the `widget_extends!` macro
1296pub unsafe trait BrowserExt: WidgetExt {
1297 /// Removes the specified line.
1298 /// Lines start at 1
1299 fn remove(&mut self, line: i32);
1300 /// Adds an item
1301 fn add(&mut self, item: &str);
1302 /// Adds an item with associated data
1303 fn add_with_data<T: Clone + 'static>(&mut self, item: &str, data: T);
1304 /// Inserts an item at an index.
1305 /// Lines start at 1
1306 fn insert(&mut self, line: i32, item: &str);
1307 /// Inserts an item at an index with associated data.
1308 /// Lines start at 1
1309 fn insert_with_data<T: Clone + 'static>(&mut self, line: i32, item: &str, data: T);
1310 /// Moves an item.
1311 /// Lines start at 1
1312 fn move_item(&mut self, to: i32, from: i32);
1313 /// Swaps 2 items.
1314 /// Lines start at 1
1315 fn swap(&mut self, a: i32, b: i32);
1316 /// Clears the browser widget
1317 fn clear(&mut self);
1318 /// Returns the number of items
1319 fn size(&self) -> i32;
1320 /// Select an item at the specified line.
1321 /// Lines start at 1
1322 fn select(&mut self, line: i32);
1323 /// Select an item at the specified line.
1324 /// Lines start at 1
1325 fn deselect(&mut self, line: i32);
1326 /// Returns whether the item is selected
1327 /// Lines start at 1
1328 fn selected(&self, line: i32) -> bool;
1329 /// Returns the text of the item at `line`.
1330 /// Lines start at 1
1331 fn text(&self, line: i32) -> Option<String>;
1332 /// Returns the text of the selected item.
1333 /// Lines start at 1
1334 fn selected_text(&self) -> Option<String>;
1335 /// Sets the text of the selected item.
1336 /// Lines start at 1
1337 fn set_text(&mut self, line: i32, txt: &str);
1338 /// Load a file
1339 /// # Errors
1340 /// Errors on non-existent paths
1341 fn load<P: AsRef<std::path::Path>>(&mut self, path: P) -> Result<(), FltkError>;
1342 /// Return the text size
1343 fn text_size(&self) -> i32;
1344 /// Sets the text size.
1345 /// Lines start at 1
1346 fn set_text_size(&mut self, sz: i32);
1347 /// Sets the icon for browser elements.
1348 /// Lines start at 1
1349 fn set_icon<Img: ImageExt>(&mut self, line: i32, image: Option<Img>);
1350 /// Returns the icon of a browser element.
1351 /// Lines start at 1
1352 fn icon(&self, line: i32) -> Option<Box<dyn ImageExt>>;
1353 /// Removes the icon of a browser element.
1354 /// Lines start at 1
1355 fn remove_icon(&mut self, line: i32);
1356 /// Scrolls the browser so the top item in the browser is showing the specified line.
1357 /// Lines start at 1
1358 fn top_line(&mut self, line: i32);
1359 /// Scrolls the browser so the bottom item in the browser is showing the specified line.
1360 /// Lines start at 1
1361 fn bottom_line(&mut self, line: i32);
1362 /// Scrolls the browser so the middle item in the browser is showing the specified line.
1363 /// Lines start at 1
1364 fn middle_line(&mut self, line: i32);
1365 /// Gets the current format code prefix character, which by default is '\@'.
1366 /// More info [here](https://www.fltk.org/doc-1.3/classFl__Browser.html#a129dca59d64baf166503ba59341add69)
1367 fn format_char(&self) -> char;
1368 /// Sets the current format code prefix character to \p c. The default prefix is '\@'.
1369 /// c should be ascii
1370 fn set_format_char(&mut self, c: char);
1371 /// Gets the current column separator character. The default is '\t'
1372 fn column_char(&self) -> char;
1373 /// Sets the column separator to c. This will only have an effect if you also use `set_column_widths()`.
1374 /// c should be ascii
1375 fn set_column_char(&mut self, c: char);
1376 /// Gets the current column width array
1377 fn column_widths(&self) -> Vec<i32>;
1378 /// Sets the current column width array
1379 fn set_column_widths(&mut self, arr: &[i32]);
1380 /// Returns whether a certain line is displayed
1381 fn displayed(&self, line: i32) -> bool;
1382 /// Makes a specified line visible
1383 fn make_visible(&mut self, line: i32);
1384 /// Gets the vertical scroll position of the list as a pixel position
1385 fn position(&self) -> i32;
1386 /// Sets the vertical scroll position of the list as a pixel position
1387 fn set_position(&mut self, pos: i32);
1388 /// Gets the horizontal scroll position of the list as a pixel position
1389 fn hposition(&self) -> i32;
1390 /// Sets the horizontal scroll position of the list as a pixel position
1391 fn set_hposition(&mut self, pos: i32);
1392 /// Returns the type of scrollbar associated with the browser
1393 fn has_scrollbar(&self) -> crate::browser::BrowserScrollbar;
1394 /// Sets the type of scrollbar associated with the browser
1395 fn set_has_scrollbar(&mut self, mode: crate::browser::BrowserScrollbar);
1396 /// Gets the scrollbar size
1397 fn scrollbar_size(&self) -> i32;
1398 /// Sets the scrollbar size
1399 fn set_scrollbar_size(&mut self, new_size: i32);
1400 /// Sorts the items of the browser
1401 fn sort(&mut self);
1402 /// Returns the vertical scrollbar
1403 fn scrollbar(&self) -> crate::valuator::Scrollbar;
1404 /// Returns the horizontal scrollbar
1405 fn hscrollbar(&self) -> crate::valuator::Scrollbar;
1406 /// Returns the selected line, returns 0 if no line is selected
1407 fn value(&self) -> i32;
1408 /// Set the data associated with the line
1409 fn set_data<T: Clone + 'static>(&mut self, line: i32, data: T);
1410 /// Get the data associated with the line
1411 /// # Safety
1412 /// Type correctness is insured by the developer
1413 unsafe fn data<T: Clone + 'static>(&self, line: i32) -> Option<T>;
1414 /// Hides a the specified line
1415 fn hide_line(&mut self, line: i32);
1416 /// Gets the selected items
1417 fn selected_items(&self) -> Vec<i32>;
1418}
1419
1420/// Defines the methods implemented by table types.
1421/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/tables).
1422/// # Safety
1423/// fltk-rs traits depend on some FLTK internal code
1424/// # Warning
1425/// fltk-rs traits are non-exhaustive,
1426/// to avoid future breakage if you try to implement them manually,
1427/// use the Deref and DerefMut pattern or the `widget_extends!` macro
1428pub unsafe trait TableExt: GroupExt {
1429 /// Clears the table
1430 fn clear(&mut self);
1431 /// Sets the table frame
1432 fn set_table_frame(&mut self, frame: FrameType);
1433 /// Gets the table frame
1434 fn table_frame(&self) -> FrameType;
1435 /// Sets the number of rows
1436 fn set_rows(&mut self, val: i32);
1437 /// Gets the number of rows
1438 fn rows(&self) -> i32;
1439 /// Sets the number of columns
1440 fn set_cols(&mut self, val: i32);
1441 /// Gets the number of columns
1442 fn cols(&self) -> i32;
1443 /// The range of row and column numbers for all visible and partially visible cells in the table.
1444 /// Returns (`row_top`, `col_left`, `row_bot`, `col_right`)
1445 fn visible_cells(&self) -> (i32, i32, i32, i32);
1446 /// The range of row and column numbers for all visible and partially visible cells in the table.
1447 /// Returns (`row_top`, `col_left`, `row_bot`, `col_right`)
1448 fn try_visible_cells(&self) -> Option<(i32, i32, i32, i32)>;
1449 /// Returns whether the resize is interactive
1450 fn is_interactive_resize(&self) -> bool;
1451 /// Returns whether a row is resizable
1452 fn row_resize(&self) -> bool;
1453 /// Sets a row to be resizable
1454 fn set_row_resize(&mut self, flag: bool);
1455 /// Returns whether a column is resizable
1456 fn col_resize(&self) -> bool;
1457 /// Sets a column to be resizable
1458 fn set_col_resize(&mut self, flag: bool);
1459 /// Returns the current column minimum resize value.
1460 fn col_resize_min(&self) -> i32;
1461 /// Sets the current column minimum resize value.
1462 fn set_col_resize_min(&mut self, val: i32);
1463 /// Returns the current row minimum resize value.
1464 fn row_resize_min(&self) -> i32;
1465 /// Sets the current row minimum resize value.
1466 fn set_row_resize_min(&mut self, val: i32);
1467 /// Returns if row headers are enabled or not
1468 fn row_header(&self) -> bool;
1469 /// Sets whether a row headers are enabled or not
1470 fn set_row_header(&mut self, flag: bool);
1471 /// Returns if column headers are enabled or not
1472 fn col_header(&self) -> bool;
1473 /// Sets whether a column headers are enabled or not
1474 fn set_col_header(&mut self, flag: bool);
1475 /// Sets the column header height
1476 fn set_col_header_height(&mut self, height: i32);
1477 /// Gets the column header height
1478 fn col_header_height(&self) -> i32;
1479 /// Sets the row header width
1480 fn set_row_header_width(&mut self, width: i32);
1481 /// Gets the row header width
1482 fn row_header_width(&self) -> i32;
1483 /// Sets the row header color
1484 fn set_row_header_color(&mut self, val: Color);
1485 /// Gets the row header color
1486 fn row_header_color(&self) -> Color;
1487 /// Sets the column header color
1488 fn set_col_header_color(&mut self, val: Color);
1489 /// Gets the row header color
1490 fn col_header_color(&self) -> Color;
1491 /// Sets the row's height
1492 fn set_row_height(&mut self, row: i32, height: i32);
1493 /// Gets the row's height
1494 fn row_height(&self, row: i32) -> i32;
1495 /// Sets the column's width
1496 fn set_col_width(&mut self, col: i32, width: i32);
1497 /// Gets the column's width
1498 fn col_width(&self, col: i32) -> i32;
1499 /// Sets all rows height
1500 fn set_row_height_all(&mut self, height: i32);
1501 /// Sets all column's width
1502 fn set_col_width_all(&mut self, width: i32);
1503 /// Sets the row's position
1504 fn set_row_position(&mut self, row: i32);
1505 /// Sets the column's position
1506 fn set_col_position(&mut self, col: i32);
1507 /// Gets the row's position
1508 fn row_position(&self) -> i32;
1509 /// Gets the column's position
1510 fn col_position(&self) -> i32;
1511 /// Sets the top row
1512 fn set_top_row(&mut self, row: i32);
1513 /// Gets the top row
1514 fn top_row(&self) -> i32;
1515 /// Returns whether a cell is selected
1516 fn is_selected(&self, r: i32, c: i32) -> bool;
1517 /// Gets the selection.
1518 /// Returns (`row_top`, `col_left`, `row_bot`, `col_right`).
1519 /// Returns -1 if no selection.
1520 fn get_selection(&self) -> (i32, i32, i32, i32);
1521 /// Tries to get the selection.
1522 /// Returns an Option((`row_top`, `col_left`, `row_bot`, `col_right`))
1523 fn try_get_selection(&self) -> Option<(i32, i32, i32, i32)>;
1524 /// Sets the selection
1525 fn set_selection(&mut self, row_top: i32, col_left: i32, row_bot: i32, col_right: i32);
1526 /// Unset selection
1527 fn unset_selection(&mut self);
1528 /// Moves the cursor with shift select
1529 /// # Errors
1530 /// Errors on failure to move the cursor
1531 fn move_cursor_with_shift_select(
1532 &mut self,
1533 r: i32,
1534 c: i32,
1535 shiftselect: bool,
1536 ) -> Result<(), FltkError>;
1537 /// Moves the cursor
1538 /// # Errors
1539 /// Errors on failure to move the cursor
1540 fn move_cursor(&mut self, r: i32, c: i32) -> Result<(), FltkError>;
1541 /// Returns the scrollbar size
1542 fn scrollbar_size(&self) -> i32;
1543 /// Sets the scrollbar size
1544 fn set_scrollbar_size(&mut self, new_size: i32);
1545 /// Sets whether tab key cell navigation is enabled
1546 fn set_tab_cell_nav(&mut self, val: bool);
1547 /// Returns whether tab key cell navigation is enabled
1548 fn tab_cell_nav(&self) -> bool;
1549 /// Override `draw_cell`.
1550 /// callback args: &mut self, `TableContext`, Row: i32, Column: i32, X: i32, Y: i32, Width: i32 and Height: i32.
1551 /// takes the widget as a closure argument
1552 fn draw_cell<
1553 F: FnMut(&mut Self, crate::table::TableContext, i32, i32, i32, i32, i32, i32) + 'static,
1554 >(
1555 &mut self,
1556 cb: F,
1557 );
1558 #[doc(hidden)]
1559 /// INTERNAL: Retrieve the draw cell data
1560 /// # Safety
1561 /// Can return multiple mutable references to the `draw_cell_data`
1562 unsafe fn draw_cell_data(&self) -> Option<Box<dyn FnMut()>>;
1563 /// Get the callback column, should be called from within a callback
1564 fn callback_col(&self) -> i32;
1565 /// Get the callback row, should be called from within a callback
1566 fn callback_row(&self) -> i32;
1567 /// Get the callback context, should be called from within a callback
1568 fn callback_context(&self) -> crate::table::TableContext;
1569 /// Returns the table's vertical scrollbar
1570 fn scrollbar(&self) -> crate::valuator::Scrollbar;
1571 /// Returns the table's horizontal scrollbar
1572 fn hscrollbar(&self) -> crate::valuator::Scrollbar;
1573 /// Returns the Scroll child of the Table
1574 fn scroll(&self) -> Option<crate::group::Scroll>;
1575 /// Find a cell's coords and size by row and column
1576 fn find_cell(
1577 &self,
1578 ctx: crate::table::TableContext,
1579 row: i32,
1580 col: i32,
1581 ) -> Option<(i32, i32, i32, i32)>;
1582 /// Get the cursor to row/col
1583 fn cursor2rowcol(
1584 &self,
1585 ) -> Option<(
1586 crate::table::TableContext,
1587 i32,
1588 i32,
1589 crate::table::TableResizeFlag,
1590 )>;
1591}
1592
1593/// Defines the methods implemented by all image types
1594/// # Safety
1595/// fltk-rs traits depend on some FLTK internal code
1596/// # Warning
1597/// fltk-rs traits are non-exhaustive,
1598/// to avoid future breakage if you try to implement them manually,
1599/// use the Deref and DerefMut pattern
1600pub unsafe trait ImageExt {
1601 /// Performs a deep copy of the image
1602 fn copy(&self) -> Self
1603 where
1604 Self: Sized;
1605 /// Performs a deep copy of the image but to a new size. This will make use of the scaling algorithm when resizing.
1606 fn copy_sized(&self, w: i32, h: i32) -> Self
1607 where
1608 Self: Sized;
1609 /// Draws the image at the presupplied coordinates and size
1610 fn draw(&mut self, x: i32, y: i32, width: i32, height: i32);
1611 /// Draws the image at the presupplied coordinates and size and offset cx, cy
1612 fn draw_ext(&mut self, x: i32, y: i32, width: i32, height: i32, cx: i32, cy: i32);
1613 /// Return the width of the image
1614 fn width(&self) -> i32;
1615 /// Return the height of the image
1616 fn height(&self) -> i32;
1617 /// Return the width of the image
1618 fn w(&self) -> i32;
1619 /// Return the height of the image
1620 fn h(&self) -> i32;
1621 /// Returns a pointer of the image
1622 fn as_image_ptr(&self) -> *mut fltk_sys::image::Fl_Image;
1623 /// Transforms a raw image pointer to an image
1624 /// # Safety
1625 /// The pointer must be valid
1626 unsafe fn from_image_ptr(ptr: *mut fltk_sys::image::Fl_Image) -> Self
1627 where
1628 Self: Sized;
1629 /// Returns the underlying raw rgb image data
1630 fn to_rgb_data(&self) -> Vec<u8>;
1631 /// Returns the underlying raw image data
1632 fn to_raw_data(&self) -> *const *const u8;
1633 /// Transforms the image into an `RgbImage`
1634 /// # Errors
1635 /// Errors on failure to transform to `RgbImage`
1636 fn to_rgb(&self) -> Result<crate::image::RgbImage, FltkError>;
1637 /// Transforms the image into an `RgbImage`
1638 /// # Errors
1639 /// Errors on failure to transform to `RgbImage`
1640 fn to_rgb_image(&self) -> Result<crate::image::RgbImage, FltkError>;
1641 /// Scales the image
1642 fn scale(&mut self, width: i32, height: i32, proportional: bool, can_expand: bool);
1643 /// Return the count of pointers in an image (Pixmaps have more than 1, bitmaps have 0, Rgb based images have 1)
1644 fn count(&self) -> i32;
1645 /// Gets the image's data width
1646 fn data_w(&self) -> i32;
1647 /// Gets the image's data height
1648 fn data_h(&self) -> i32;
1649 /// Gets the image's depth
1650 fn depth(&self) -> ColorDepth;
1651 /// Gets the image's line data size
1652 fn ld(&self) -> i32;
1653 /// Greys the image
1654 fn inactive(&mut self);
1655 /// Deletes the image
1656 /// # Safety
1657 /// An image shouldn't be deleted while it's being used by a widget
1658 unsafe fn delete(img: Self)
1659 where
1660 Self: Sized;
1661 /// Checks if the image was deleted
1662 fn was_deleted(&self) -> bool;
1663 /// Transforms an Image base into another Image
1664 /// # Safety
1665 /// Can be unsafe if used to downcast to an image of different format
1666 unsafe fn into_image<I: ImageExt>(self) -> I
1667 where
1668 Self: Sized;
1669 /// Blend the image with color c with weight i in range 0 to 1
1670 fn color_average(&mut self, c: crate::enums::Color, i: f32);
1671 /// Desaturate (grayscale) the image
1672 fn desaturate(&mut self);
1673 /// Clear internal caches
1674 fn uncache(&mut self);
1675 #[doc(hidden)]
1676 /// Set this image as the label image for a widget
1677 fn label_for_widget<W: WidgetExt>(&self, item: &mut W)
1678 where
1679 Self: Sized;
1680 #[doc(hidden)]
1681 /// Set this image as the label image for a menu item
1682 fn label_for_menu_item(&self, item: &mut crate::menu::MenuItem);
1683 #[doc(hidden)]
1684 /// Cast an image back to its original type
1685 fn from_dyn_image_ptr(_p: *mut fltk_sys::image::Fl_Image) -> Option<Self>
1686 where
1687 Self: Sized,
1688 {
1689 None
1690 }
1691 #[doc(hidden)]
1692 /// Cast an image back to its original type
1693 fn from_dyn_image<I: ImageExt>(_i: &I) -> Option<Self>
1694 where
1695 Self: Sized,
1696 {
1697 None
1698 }
1699}
1700
1701/// Defines the methods implemented by all surface types, currently `ImageSurface`
1702pub trait SurfaceDevice {
1703 /// Checks whether this surface is the current surface
1704 fn is_current(&self) -> bool;
1705 /// Get the current surface
1706 fn surface() -> Self;
1707 /// Push a surface as a current surface
1708 fn push_current(new_current: &Self);
1709 /// Pop the current surface
1710 fn pop_current();
1711}
1712
1713/// Defines a set of convenience functions for constructing and anchoring custom widgets.
1714/// Usage: fltk::widget_extends!(CustomWidget, BaseWidget, member);
1715/// It basically implements Deref and DerefMut on the custom widget, and adds the aforementioned methods.
1716/// This means you can call widget methods directly, for e.x. `custom_widget.set_color(enums::Color::Red)`.
1717/// For your custom widget to be treated as a widget it would need dereferencing: `group.add(&*custom_widget);`
1718#[macro_export]
1719macro_rules! widget_extends {
1720 ($widget:ty, $base:ty, $member:tt) => {
1721 impl $widget {
1722 /// Initialize to position x, y
1723 pub fn with_pos(mut self, x: i32, y: i32) -> Self {
1724 let w = self.w();
1725 let h = self.h();
1726 self.resize(x, y, w, h);
1727 self
1728 }
1729
1730 /// Initialize to size width, height
1731 pub fn with_size(mut self, width: i32, height: i32) -> Self {
1732 let x = self.x();
1733 let y = self.y();
1734 let w = self.width();
1735 let h = self.height();
1736 if w == 0 || h == 0 {
1737 self.widget_resize(x, y, width, height);
1738 } else {
1739 self.resize(x, y, width, height);
1740 }
1741 self
1742 }
1743
1744 /// Initialize with a label
1745 pub fn with_label(mut self, title: &str) -> Self {
1746 self.set_label(title);
1747 self
1748 }
1749
1750 /// Initialize with alignment
1751 pub fn with_align(mut self, align: $crate::enums::Align) -> Self {
1752 self.set_align(align);
1753 self
1754 }
1755
1756 /// Initialize with type
1757 pub fn with_type<T: $crate::prelude::WidgetType>(mut self, typ: T) -> Self {
1758 self.set_type(typ);
1759 self
1760 }
1761
1762 /// Initialize at bottom of another widget
1763 pub fn below_of<W: $crate::prelude::WidgetExt>(
1764 mut self,
1765 wid: &W,
1766 padding: i32,
1767 ) -> Self {
1768 let w = self.w();
1769 let h = self.h();
1770 debug_assert!(
1771 w != 0 && h != 0,
1772 "below_of requires the size of the widget to be known!"
1773 );
1774 self.resize(wid.x(), wid.y() + wid.h() + padding, w, h);
1775 self
1776 }
1777
1778 /// Initialize above of another widget
1779 pub fn above_of<W: $crate::prelude::WidgetExt>(
1780 mut self,
1781 wid: &W,
1782 padding: i32,
1783 ) -> Self {
1784 let w = self.w();
1785 let h = self.h();
1786 debug_assert!(
1787 w != 0 && h != 0,
1788 "above_of requires the size of the widget to be known!"
1789 );
1790 self.resize(wid.x(), wid.y() - padding - h, w, h);
1791 self
1792 }
1793
1794 /// Initialize right of another widget
1795 pub fn right_of<W: $crate::prelude::WidgetExt>(
1796 mut self,
1797 wid: &W,
1798 padding: i32,
1799 ) -> Self {
1800 let w = self.w();
1801 let h = self.h();
1802 debug_assert!(
1803 w != 0 && h != 0,
1804 "right_of requires the size of the widget to be known!"
1805 );
1806 self.resize(wid.x() + wid.width() + padding, wid.y(), w, h);
1807 self
1808 }
1809
1810 /// Initialize left of another widget
1811 pub fn left_of<W: $crate::prelude::WidgetExt>(mut self, wid: &W, padding: i32) -> Self {
1812 let w = self.w();
1813 let h = self.h();
1814 debug_assert!(
1815 w != 0 && h != 0,
1816 "left_of requires the size of the widget to be known!"
1817 );
1818 self.resize(wid.x() - w - padding, wid.y(), w, h);
1819 self
1820 }
1821
1822 /// Initialize center of another widget
1823 pub fn center_of<W: $crate::prelude::WidgetExt>(mut self, w: &W) -> Self {
1824 debug_assert!(
1825 w.width() != 0 && w.height() != 0,
1826 "center_of requires the size of the widget to be known!"
1827 );
1828 let sw = self.width() as f64;
1829 let sh = self.height() as f64;
1830 let ww = w.width() as f64;
1831 let wh = w.height() as f64;
1832 let sx = (ww - sw) / 2.0;
1833 let sy = (wh - sh) / 2.0;
1834 let wx = if w.as_window().is_some() { 0 } else { w.x() };
1835 let wy = if w.as_window().is_some() { 0 } else { w.y() };
1836 self.resize(sx as i32 + wx, sy as i32 + wy, sw as i32, sh as i32);
1837 self.redraw();
1838 self
1839 }
1840
1841 /// Initialize center of another widget on the x axis
1842 pub fn center_x<W: $crate::prelude::WidgetExt>(mut self, w: &W) -> Self {
1843 debug_assert!(
1844 w.width() != 0 && w.height() != 0,
1845 "center_of requires the size of the widget to be known!"
1846 );
1847 let sw = self.width() as f64;
1848 let sh = self.height() as f64;
1849 let ww = w.width() as f64;
1850 let sx = (ww - sw) / 2.0;
1851 let sy = self.y();
1852 let wx = if w.as_window().is_some() { 0 } else { w.x() };
1853 self.resize(sx as i32 + wx, sy, sw as i32, sh as i32);
1854 self.redraw();
1855 self
1856 }
1857
1858 /// Initialize center of another widget on the y axis
1859 pub fn center_y<W: $crate::prelude::WidgetExt>(mut self, w: &W) -> Self {
1860 debug_assert!(
1861 w.width() != 0 && w.height() != 0,
1862 "center_of requires the size of the widget to be known!"
1863 );
1864 let sw = self.width() as f64;
1865 let sh = self.height() as f64;
1866 let wh = w.height() as f64;
1867 let sx = self.x();
1868 let sy = (wh - sh) / 2.0;
1869 let wy = if w.as_window().is_some() { 0 } else { w.y() };
1870 self.resize(sx, sy as i32 + wy, sw as i32, sh as i32);
1871 self.redraw();
1872 self
1873 }
1874
1875 /// Initialize center of parent
1876 pub fn center_of_parent(mut self) -> Self {
1877 if let Some(w) = self.parent() {
1878 debug_assert!(
1879 w.width() != 0 && w.height() != 0,
1880 "center_of requires the size of the widget to be known!"
1881 );
1882 let sw = self.width() as f64;
1883 let sh = self.height() as f64;
1884 let ww = w.width() as f64;
1885 let wh = w.height() as f64;
1886 let sx = (ww - sw) / 2.0;
1887 let sy = (wh - sh) / 2.0;
1888 let wx = if w.as_window().is_some() { 0 } else { w.x() };
1889 let wy = if w.as_window().is_some() { 0 } else { w.y() };
1890 self.resize(sx as i32 + wx, sy as i32 + wy, sw as i32, sh as i32);
1891 self.redraw();
1892 }
1893 self
1894 }
1895
1896 /// Initialize to the size of another widget
1897 pub fn size_of<W: $crate::prelude::WidgetExt>(mut self, w: &W) -> Self {
1898 debug_assert!(
1899 w.width() != 0 && w.height() != 0,
1900 "size_of requires the size of the widget to be known!"
1901 );
1902 let x = self.x();
1903 let y = self.y();
1904 self.resize(x, y, w.width(), w.height());
1905 self
1906 }
1907
1908 /// Initialize to the size of the parent
1909 pub fn size_of_parent(mut self) -> Self {
1910 if let Some(parent) = self.parent() {
1911 let w = parent.width();
1912 let h = parent.height();
1913 let x = self.x();
1914 let y = self.y();
1915 self.resize(x, y, w, h);
1916 }
1917 self
1918 }
1919 }
1920
1921 impl std::ops::Deref for $widget {
1922 type Target = $base;
1923
1924 fn deref(&self) -> &Self::Target {
1925 &self.$member
1926 }
1927 }
1928
1929 impl std::ops::DerefMut for $widget {
1930 fn deref_mut(&mut self) -> &mut Self::Target {
1931 &mut self.$member
1932 }
1933 }
1934 };
1935}
1936
1937pub use crate::app::WidgetId;