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 /// Get a reference type of the widget's image
445 /// # Safety
446 /// The widget needs to be still around when the image is accessed
447 unsafe fn image_mut(&self) -> Option<&mut crate::image::Image> {
448 None
449 }
450 /// Get a reference type of the widget's deactivated image
451 /// # Safety
452 /// The widget needs to be still around when the image is accessed
453 unsafe fn deimage_mut(&self) -> Option<&mut crate::image::Image> {
454 None
455 }
456}
457
458/// Defines the extended methods implemented by all widgets
459/// # Safety
460/// fltk-rs traits depend on some FLTK internal code
461/// # Warning
462/// fltk-rs traits are non-exhaustive,
463/// to avoid future breakage if you try to implement them manually,
464/// use the Deref and DerefMut pattern or the `widget_extends!` macro
465pub unsafe trait WidgetBase: WidgetExt {
466 /// Creates a new widget, takes an x, y coordinates, as well as a width and height, plus a title
467 /// # Arguments
468 /// * `x` - The x coordinate in the screen
469 /// * `y` - The y coordinate in the screen
470 /// * `width` - The width of the widget
471 /// * `heigth` - The height of the widget
472 /// * `title` - The title or label of the widget
473 ///
474 /// To use dynamic strings use `with_label(self, &str)` or `set_label(&mut self, &str)`.
475 /// labels support special symbols preceded by an `@` [sign](https://www.fltk.org/doc-1.3/symbols.png)
476 /// and for the [associated formatting](https://www.fltk.org/doc-1.3/common.html).
477 fn new<'a, T: Into<Option<&'a str>>>(x: i32, y: i32, width: i32, height: i32, title: T)
478 -> Self;
479 /// Constructs a widget with the size of its parent
480 fn default_fill() -> Self;
481 /// Deletes widgets and their children.
482 fn delete(wid: Self)
483 where
484 Self: Sized;
485 /// transforms a widget pointer to a Widget, for internal use
486 /// # Safety
487 /// The pointer must be valid
488 unsafe fn from_widget_ptr(ptr: *mut fltk_sys::widget::Fl_Widget) -> Self;
489 /// Get a widget from base widget
490 /// # Safety
491 /// The underlying object must be valid
492 unsafe fn from_widget<W: WidgetExt>(w: W) -> Self;
493 /// Set a custom handler, where events are managed manually, akin to `Fl_Widget::handle(int)`.
494 /// Handled or ignored events should return true, unhandled events should return false.
495 /// takes the widget as a closure argument.
496 /// The ability to handle an event might depend on handling other events, as explained [here](https://www.fltk.org/doc-1.4/events.html)
497 fn handle<F: FnMut(&mut Self, Event) -> bool + 'static>(&mut self, cb: F);
498 /// Set a custom draw method.
499 /// takes the widget as a closure argument.
500 /// macOS requires that `WidgetBase::draw` actually calls drawing functions
501 fn draw<F: FnMut(&mut Self) + 'static>(&mut self, cb: F);
502 #[doc(hidden)]
503 /// INTERNAL: Retrieve the draw data
504 /// # Safety
505 /// Can return multiple mutable references to the `draw_data`
506 unsafe fn draw_data(&self) -> Option<Box<dyn FnMut()>>;
507 #[doc(hidden)]
508 /// INTERNAL: Retrieve the handle data
509 /// # Safety
510 /// Can return multiple mutable references to the `handle_data`
511 unsafe fn handle_data(&self) -> Option<Box<dyn FnMut(Event) -> bool>>;
512 /// Perform a callback on resize.
513 /// Avoid resizing the parent or the same widget to avoid infinite recursion
514 fn resize_callback<F: FnMut(&mut Self, i32, i32, i32, i32) + 'static>(&mut self, cb: F);
515 /// Makes the widget derived
516 /// # Safety
517 /// Calling this on a non-derived widget can cause undefined behavior
518 unsafe fn assume_derived(&mut self) {
519 unimplemented!();
520 }
521 /// Cast a type-erased widget back to its original widget
522 fn from_dyn_widget<W: WidgetExt>(_w: &W) -> Option<Self>
523 where
524 Self: Sized,
525 {
526 None
527 }
528 /// Cast a type-erased widget pointer back to its original widget
529 fn from_dyn_widget_ptr(_w: *mut fltk_sys::widget::Fl_Widget) -> Option<Self>
530 where
531 Self: Sized,
532 {
533 None
534 }
535 #[doc(hidden)]
536 /// Determine whether the base class's draw method is called, default is true
537 fn super_draw(&mut self, flag: bool) {
538 let _ = flag;
539 }
540 #[doc(hidden)]
541 /// Determine whether the base class's draw method is called last, default is true
542 fn super_draw_first(&mut self, flag: bool) {
543 let _ = flag;
544 }
545 #[doc(hidden)]
546 /// Determine whether the base class's handle method should have an event propagated even
547 /// if handled by the instantiated widget
548 fn super_handle_first(&mut self, flag: bool) {
549 let _ = flag;
550 }
551}
552
553/// Defines the methods implemented by all button widgets.
554/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/buttons).
555/// # Safety
556/// fltk-rs traits depend on some FLTK internal code
557/// # Warning
558/// fltk-rs traits are non-exhaustive,
559/// to avoid future breakage if you try to implement them manually,
560/// use the Deref and DerefMut pattern or the `widget_extends!` macro
561pub unsafe trait ButtonExt: WidgetExt {
562 /// Gets the shortcut associated with a button
563 fn shortcut(&self) -> Shortcut;
564 /// Sets the shortcut associated with a button
565 fn set_shortcut(&mut self, shortcut: Shortcut);
566 /// Clears the value of the button.
567 /// Useful for round, radio, light, toggle and check buttons
568 fn clear(&mut self);
569 /// Returns whether a button is set or not.
570 /// Useful for round, radio, light, toggle and check buttons
571 fn is_set(&self) -> bool;
572 /// Sets whether a button is set or not.
573 /// Useful for round, radio, light, toggle and check buttons
574 fn set(&mut self, flag: bool);
575 /// Returns whether a button is set or not.
576 /// Useful for round, radio, light, toggle and check buttons
577 fn value(&self) -> bool;
578 /// Sets whether a button is set or not.
579 /// Useful for round, radio, light, toggle and check buttons
580 fn set_value(&mut self, flag: bool);
581 /// Set the `down_box` of the widget
582 fn set_down_frame(&mut self, f: FrameType);
583 /// Get the down frame type of the widget
584 fn down_frame(&self) -> FrameType;
585}
586
587/// Defines the methods implemented by all group widgets.
588/// These widgets include Window types and others found in the group module: Group, Scroll, Pack, Tile, Flex ...etc.
589/// Widgets implementing the GroupExt trait, are characterized by having to call `::end()` method to basically close them.
590/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/group_widgets).
591/// ```rust
592/// use fltk::{app, button::Button, window::Window, prelude::GroupExt};
593/// let a = app::App::default();
594/// let win = Window::default();
595/// let btn = Button::default();
596/// // Instantiate other widgets
597/// win.end();
598/// ```
599/// In the above example, the button `btn` will be parented by the window.
600/// After `end`ing such GroupExt widgets, any other widgets instantiated after the `end` call, will be instantiated outside.
601/// These can still be added using the `::add(&other_widget)` method (or using `::insert`):
602/// ```rust
603/// use fltk::{app, button::Button, window::Window, prelude::GroupExt};
604/// let a = app::App::default();
605/// let mut win = Window::default();
606/// win.end();
607/// let btn = Button::default();
608/// win.add(&btn);
609/// ```
610/// Another option is to reopen the widget:
611/// ```rust
612/// use fltk::{app, button::Button, window::Window, prelude::GroupExt};
613/// let a = app::App::default();
614/// let win = Window::default();
615/// win.end();
616/// win.begin();
617/// let btn = Button::default();
618/// // other widgets
619/// win.end();
620/// ```
621/// # Safety
622/// fltk-rs traits depend on some FLTK internal code
623/// # Warning
624/// fltk-rs traits are non-exhaustive,
625/// to avoid future breakage if you try to implement them manually,
626/// use the Deref and DerefMut pattern or the `widget_extends!` macro
627pub unsafe trait GroupExt: WidgetExt {
628 /// Begins a group, used for widgets implementing the group trait
629 fn begin(&self);
630 /// Ends a group, used for widgets implementing the group trait
631 fn end(&self);
632 /// Clear a group from all widgets
633 fn clear(&mut self);
634 #[doc(hidden)]
635 /// Clear a group from all widgets using FLTK's clear call.
636 /// # Safety
637 /// Ignores widget tracking
638 unsafe fn unsafe_clear(&mut self);
639 /// Return the number of children in a group
640 fn children(&self) -> i32;
641 /// Return child widget by index
642 fn child(&self, idx: i32) -> Option<crate::widget::Widget>;
643 /// Find a widget within a group and return its index
644 fn find<W: WidgetExt>(&self, widget: &W) -> i32
645 where
646 Self: Sized;
647 /// Add a widget to a group
648 fn add<W: WidgetExt>(&mut self, widget: &W)
649 where
650 Self: Sized;
651 /// Insert a widget to a group at a certain index
652 fn insert<W: WidgetExt>(&mut self, widget: &W, index: i32)
653 where
654 Self: Sized;
655 /// Remove a widget from a group, but does not delete it
656 fn remove<W: WidgetExt>(&mut self, widget: &W)
657 where
658 Self: Sized;
659 /// Remove a child widget by its index
660 fn remove_by_index(&mut self, idx: i32);
661 /// The resizable widget defines both the resizing frame and the resizing behavior of the group and its children.
662 fn resizable<W: WidgetExt>(&self, widget: &W)
663 where
664 Self: Sized;
665 /// Make the group itself resizable, should be called before the widget is shown
666 fn make_resizable(&mut self, val: bool);
667 /// Adds a widget to the group and makes it the resizable widget
668 fn add_resizable<W: WidgetExt>(&mut self, widget: &W)
669 where
670 Self: Sized;
671 /// Clips children outside the group boundaries
672 fn set_clip_children(&mut self, flag: bool);
673 /// Get whether `clip_children` is set
674 fn clip_children(&self) -> bool;
675 /// Draw a child widget, the call should be in a [`WidgetBase::draw`](`crate::prelude::WidgetBase::draw`) method
676 fn draw_child<W: WidgetExt>(&self, w: &mut W)
677 where
678 Self: Sized;
679 /// Update a child widget, the call should be in a [`WidgetBase::draw`](`crate::prelude::WidgetBase::draw`) method
680 fn update_child<W: WidgetExt>(&self, w: &mut W)
681 where
682 Self: Sized;
683 /// Draw the outside label, the call should be in a [`WidgetBase::draw`](`crate::prelude::WidgetBase::draw`) method
684 fn draw_outside_label<W: WidgetExt>(&self, w: &mut W)
685 where
686 Self: Sized;
687 /// Draw children, the call should be in a [`WidgetBase::draw`](`crate::prelude::WidgetBase::draw`) method
688 fn draw_children(&mut self);
689 /// Resets the internal array of widget sizes and positions
690 fn init_sizes(&mut self);
691 /// Get the bounds of all children widgets (left, upper, right, bottom)
692 fn bounds(&self) -> Vec<(i32, i32, i32, i32)>;
693 /// Converts a widget implementing GroupExt into a Group widget
694 /// # Safety
695 /// If the widget wasn't created by fltk-rs,
696 /// vtable differences mean certain methods can't be overridden (e.g. handle & draw)
697 #[allow(clippy::wrong_self_convention)]
698 unsafe fn into_group(&self) -> crate::group::Group;
699}
700
701/// Defines the methods implemented by all window widgets.
702/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/windows).
703/// Windows (which can be found in the window module) implement GroupExt as well.
704/// # Safety
705/// fltk-rs traits depend on some FLTK internal code
706/// # Warning
707/// fltk-rs traits are non-exhaustive,
708/// to avoid future breakage if you try to implement them manually,
709/// use the Deref and DerefMut pattern or the `widget_extends!` macro
710pub unsafe trait WindowExt: GroupExt {
711 /// Positions the window to the center of the screen
712 fn center_screen(self) -> Self
713 where
714 Self: Sized;
715 /// Makes a window modal, should be called before `show`
716 fn make_modal(&mut self, val: bool);
717 /// Makes a window fullscreen.
718 /// Requires that the window is resizable.
719 fn fullscreen(&mut self, val: bool);
720 /// Makes the window current
721 fn make_current(&mut self);
722 /// Returns the icon of the window
723 fn icon(&self) -> Option<Box<dyn ImageExt>>;
724 /// Sets the windows icon.
725 /// Supported formats are bmp, jpeg, png and rgb.
726 fn set_icon<T: ImageExt>(&mut self, image: Option<T>)
727 where
728 Self: Sized;
729 /// Sets the cursor style within the window.
730 /// Needs to be called after the window is shown
731 fn set_cursor(&mut self, cursor: Cursor);
732 /// Returns whether a window is shown
733 fn shown(&self) -> bool;
734 /// Sets whether the window has a border
735 fn set_border(&mut self, flag: bool);
736 /// Returns whether a window has a border
737 fn border(&self) -> bool;
738 /// Frees the position of the window
739 fn free_position(&mut self);
740 /// Get the raw system handle of the window
741 fn raw_handle(&self) -> crate::window::RawHandle;
742 #[doc(hidden)]
743 /// Set the window associated with a raw handle.
744 /// `RawHandle` is a void pointer to: (Windows: `HWND`, X11: `Xid` (`u64`), macOS: `NSWindow`)
745 /// # Safety
746 /// The data must be valid and is OS-dependent. The window must be shown.
747 unsafe fn set_raw_handle(&mut self, handle: crate::window::RawHandle);
748 /// Get the graphical draw region of the window
749 fn region(&self) -> crate::draw::Region;
750 /// Set the graphical draw region of the window
751 /// # Safety
752 /// The data must be valid.
753 unsafe fn set_region(&mut self, region: crate::draw::Region);
754 /// Iconifies the window.
755 /// You can tell that the window is iconized by checking that it's shown and not visible
756 fn iconize(&mut self);
757 /// Returns whether the window is fullscreen or not
758 fn fullscreen_active(&self) -> bool;
759 /// Returns the decorated width
760 fn decorated_w(&self) -> i32;
761 /// Returns the decorated height
762 fn decorated_h(&self) -> i32;
763 /// Set the window's minimum width, minimum height, max width and max height.
764 /// You can pass 0 as max_w and max_h to allow unlimited upward resize of the window.
765 fn size_range(&mut self, min_w: i32, min_h: i32, max_w: i32, max_h: i32);
766 /// Set the hotspot widget of the window
767 fn hotspot<W: WidgetExt>(&mut self, w: &W)
768 where
769 Self: Sized;
770 /// Set the shape of the window.
771 /// Supported image formats are BMP, RGB and Pixmap.
772 /// The window covers non-transparent/non-black shape of the image.
773 /// The image must not be scaled(resized) beforehand.
774 /// The size will be adapted to the window's size
775 fn set_shape<I: ImageExt>(&mut self, image: Option<I>)
776 where
777 Self: Sized;
778 /// Get the shape of the window
779 fn shape(&self) -> Option<Box<dyn ImageExt>>;
780 /// Get the window's x coord from the screen
781 fn x_root(&self) -> i32;
782 /// Get the window's y coord from the screen
783 fn y_root(&self) -> i32;
784 /// Set the cursor image
785 fn set_cursor_image(&mut self, image: crate::image::RgbImage, hot_x: i32, hot_y: i32);
786 /// Set the window's default cursor
787 fn default_cursor(&mut self, cursor: Cursor);
788 /// Get the screen number
789 fn screen_num(&self) -> i32;
790 /// Set the screen number
791 fn set_screen_num(&mut self, n: i32);
792 /// wait for the window to be displayed after calling `show()`.
793 /// More info [here](https://www.fltk.org/doc-1.4/classFl__Window.html#aafbec14ca8ff8abdaff77a35ebb23dd8)
794 fn wait_for_expose(&self);
795 /// Get the window's opacity
796 fn opacity(&self) -> f64;
797 /// Set the window's opacity,
798 /// Ranges from 0.0 to 1.0, where 1.0 is fully opaque and 0.0 is fully transparent.
799 /// This should be called on a shown window.
800 /// On X11, opacity support depends on the window manager and can be queried:
801 /// ```ignore
802 /// $ xprop -root _NET_SUPPORTED | grep -o _NET_WM_WINDOW_OPACITY
803 /// ```
804 fn set_opacity(&mut self, val: f64);
805 /// Get the window's XA_WM_CLASS property
806 fn xclass(&self) -> Option<String>;
807 /// Set the window's XA_WM_CLASS property.
808 /// This should be called before showing the window
809 fn set_xclass(&mut self, s: &str);
810 /// Clear the modal state of the window
811 fn clear_modal_states(&mut self);
812 /// removes the window border and sets the window on top, by settings the NOBORDER and OVERRIDE flags
813 fn set_override(&mut self);
814 /// Checks whether the OVERRIDE flag was set
815 fn is_override(&self) -> bool;
816 /// Forces the position of the window
817 fn force_position(&mut self, flag: bool);
818 /// Set the icon label
819 fn set_icon_label(&mut self, label: &str);
820 /// Get the icon label
821 fn icon_label(&self) -> Option<String>;
822 /// Allow the window to expand outside its parent (if supported by the platform/driver)
823 fn allow_expand_outside_parent(&mut self);
824 /// Return the OS-specific window id/handle as an integer (useful for interop)
825 fn os_id(&self) -> usize;
826}
827
828/// Defines the methods implemented by all input and output widgets.
829/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/inout_widgets).
830/// # Safety
831/// fltk-rs traits depend on some FLTK internal code
832/// # Warning
833/// fltk-rs traits are non-exhaustive,
834/// to avoid future breakage if you try to implement them manually,
835/// use the Deref and DerefMut pattern or the `widget_extends!` macro
836pub unsafe trait InputExt: WidgetExt {
837 /// Returns the value inside the input/output widget
838 fn value(&self) -> String;
839 /// Sets the value inside an input/output widget
840 fn set_value(&mut self, val: &str);
841 /// Returns the maximum size (in bytes) accepted by an input/output widget
842 fn maximum_size(&self) -> i32;
843 /// Sets the maximum size (in bytes) accepted by an input/output widget
844 fn set_maximum_size(&mut self, val: i32);
845 /// Returns the index position inside an input/output widget
846 fn position(&self) -> i32;
847 /// Sets the index position inside an input/output widget
848 /// # Errors
849 /// Errors on failure to set the cursor position in the text
850 fn set_position(&mut self, val: i32) -> Result<(), FltkError>;
851 /// Returns the index mark inside an input/output widget
852 fn mark(&self) -> i32;
853 /// Sets the index mark inside an input/output widget
854 /// # Errors
855 /// Errors on failure to set the mark
856 fn set_mark(&mut self, val: i32) -> Result<(), FltkError>;
857 /// Replace content with a &str
858 /// # Errors
859 /// Errors on failure to replace text
860 fn replace(&mut self, beg: i32, end: i32, val: &str) -> Result<(), FltkError>;
861 /// Insert a &str
862 /// # Errors
863 /// Errors on failure to insert text
864 fn insert(&mut self, txt: &str) -> Result<(), FltkError>;
865 /// Append a &str
866 /// # Errors
867 /// Errors on failure to append text
868 fn append(&mut self, txt: &str) -> Result<(), FltkError>;
869 /// Copy the value within the widget
870 /// # Errors
871 /// Errors on failure to copy selection
872 fn copy(&mut self) -> Result<(), FltkError>;
873 /// Undo changes
874 /// # Errors
875 /// Errors on failure to undo
876 fn undo(&mut self) -> Result<(), FltkError>;
877 /// Cut the value within the widget
878 /// # Errors
879 /// Errors on failure to cut selection
880 fn cut(&mut self) -> Result<(), FltkError>;
881 /// Return the cursor color
882 fn cursor_color(&self) -> Color;
883 /// Sets the cursor color
884 fn set_cursor_color(&mut self, color: Color);
885 /// Return the text font
886 fn text_font(&self) -> Font;
887 /// Sets the text font
888 fn set_text_font(&mut self, font: Font);
889 /// Return the text color
890 fn text_color(&self) -> Color;
891 /// Sets the text color
892 fn set_text_color(&mut self, color: Color);
893 /// Return the text size
894 fn text_size(&self) -> i32;
895 /// Sets the text size
896 fn set_text_size(&mut self, sz: i32);
897 /// Returns whether the input/output widget is readonly
898 fn readonly(&self) -> bool;
899 /// Set readonly status of the input/output widget
900 fn set_readonly(&mut self, val: bool);
901 /// Return whether text is wrapped inside an input/output widget
902 fn wrap(&self) -> bool;
903 /// Set whether text is wrapped inside an input/output widget
904 fn set_wrap(&mut self, val: bool);
905 /// Sets whether tab navigation is enabled, true by default
906 fn set_tab_nav(&mut self, val: bool);
907 /// Returns whether tab navigation is enabled
908 fn tab_nav(&self) -> bool;
909}
910
911/// Defines the methods implemented by all menu widgets
912/// These are found in the menu module: MenuBar, SysMenuBar, Choice, MenuButton ...etc.
913/// Menus function in 2 main ways which are discussed in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/menus)
914/// # Safety
915/// fltk-rs traits depend on some FLTK internal code
916/// # Warning
917/// fltk-rs traits are non-exhaustive,
918/// to avoid future breakage if you try to implement them manually,
919/// use the Deref and DerefMut pattern or the `widget_extends!` macro
920pub unsafe trait MenuExt: WidgetExt {
921 /// Get a menu item by name
922 fn find_item(&self, name: &str) -> Option<crate::menu::MenuItem>;
923 /// Set selected item
924 fn set_item(&mut self, item: &crate::menu::MenuItem) -> bool;
925 /// Find an item's index by its label
926 fn find_index(&self, label: &str) -> i32;
927 /// Return the text font
928 fn text_font(&self) -> Font;
929 /// Sets the text font
930 fn set_text_font(&mut self, c: Font);
931 /// Return the text size
932 fn text_size(&self) -> i32;
933 /// Sets the text size
934 fn set_text_size(&mut self, c: i32);
935 /// Return the text color
936 fn text_color(&self) -> Color;
937 /// Sets the text color
938 fn set_text_color(&mut self, c: Color);
939 /// Add a menu item along with its callback.
940 /// 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.
941 /// 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.
942 /// Takes the menu item as a closure argument
943 fn add<F: FnMut(&mut Self) + 'static>(
944 &mut self,
945 name: &str,
946 shortcut: Shortcut,
947 flag: crate::menu::MenuFlag,
948 cb: F,
949 ) -> i32
950 where
951 Self: Sized;
952 /// Inserts a menu item at an index along with its callback.
953 /// 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.
954 /// 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.
955 /// Takes the menu item as a closure argument
956 fn insert<F: FnMut(&mut Self) + 'static>(
957 &mut self,
958 idx: i32,
959 name: &str,
960 shortcut: Shortcut,
961 flag: crate::menu::MenuFlag,
962 cb: F,
963 ) -> i32
964 where
965 Self: Sized;
966 /// Add a menu item along with an emit (sender and message).
967 /// 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.
968 /// 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.
969 fn add_emit<T: 'static + Clone + Send + Sync>(
970 &mut self,
971 label: &str,
972 shortcut: Shortcut,
973 flag: crate::menu::MenuFlag,
974 sender: crate::app::Sender<T>,
975 msg: T,
976 ) -> i32
977 where
978 Self: Sized;
979 /// Inserts a menu item along with an emit (sender and message).
980 /// 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.
981 /// 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.
982 fn insert_emit<T: 'static + Clone + Send + Sync>(
983 &mut self,
984 idx: i32,
985 label: &str,
986 shortcut: Shortcut,
987 flag: crate::menu::MenuFlag,
988 sender: crate::app::Sender<T>,
989 msg: T,
990 ) -> i32
991 where
992 Self: Sized;
993 /// Remove a menu item by index
994 fn remove(&mut self, idx: i32);
995 /// Adds a simple text option to the Choice and `MenuButton` widgets.
996 /// 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.
997 /// 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.
998 fn add_choice(&mut self, text: &str) -> i32;
999 /// Gets the user choice from the Choice and `MenuButton` widgets
1000 fn choice(&self) -> Option<String>;
1001 /// Get index into menu of the last item chosen, returns -1 if no item was chosen
1002 fn value(&self) -> i32;
1003 /// Set index into menu of the last item chosen,return true if the new value is different than the old one
1004 fn set_value(&mut self, v: i32) -> bool;
1005 /// Clears the items in a menu, effectively deleting them.
1006 fn clear(&mut self);
1007 /// Clears a submenu by index
1008 /// # Errors
1009 /// Errors on failure to clear the submenu, failure returns an [`FltkErrorKind::FailedOperation`](`crate::prelude::FltkErrorKind::FailedOperation`)
1010 fn clear_submenu(&mut self, idx: i32) -> Result<(), FltkError>;
1011 /// Clears the items in a menu, effectively deleting them, and recursively force-cleans capturing callbacks
1012 /// # Safety
1013 /// Deletes `user_data` and any captured objects in the callback
1014 unsafe fn unsafe_clear(&mut self);
1015 #[doc(hidden)]
1016 /// Clears a submenu by index. Also recursively force-cleans capturing callbacks
1017 /// # Safety
1018 /// Deletes `user_data` and any captured objects in the callback, , failure returns an [`FltkErrorKind::FailedOperation`](`crate::prelude::FltkErrorKind::FailedOperation`)
1019 /// # Errors
1020 /// Errors on failure to clear the submenu
1021 unsafe fn unsafe_clear_submenu(&mut self, idx: i32) -> Result<(), FltkError>;
1022 /// Get the size of the menu widget
1023 fn size(&self) -> i32;
1024 /// Get the text label of the menu item at index idx
1025 fn text(&self, idx: i32) -> Option<String>;
1026 /// Get the menu item at an index
1027 fn at(&self, idx: i32) -> Option<crate::menu::MenuItem>;
1028 /// Get the mode of a menu item by index and flag
1029 fn mode(&self, idx: i32) -> crate::menu::MenuFlag;
1030 /// Set the mode of a menu item
1031 fn set_mode(&mut self, idx: i32, flag: crate::menu::MenuFlag);
1032 /// End the menu
1033 fn end(&mut self);
1034 /// Set the `down_box` of the widget
1035 fn set_down_frame(&mut self, f: FrameType);
1036 /// Get the down frame type of the widget
1037 fn down_frame(&self) -> FrameType;
1038 /// Make a menu globally accessible from any window
1039 fn global(&mut self);
1040 /// Get the menu element
1041 fn menu(&self) -> Option<crate::menu::MenuItem>;
1042 /// Set the menu element
1043 /// # Safety
1044 /// The MenuItem must be in a format recognized by FLTK (Empty CMenuItem after submenus and at the end of the menu)
1045 unsafe fn set_menu(&mut self, item: crate::menu::MenuItem);
1046 /// Get an item's pathname
1047 fn item_pathname(&self, item: Option<&crate::menu::MenuItem>) -> Result<String, FltkError>;
1048 /// Set the menu's popup frame type
1049 fn set_menu_frame(&mut self, f: FrameType);
1050 /// Get the menu's popup frame type
1051 fn menu_frame(&self) -> FrameType;
1052 /// Get the selected menu item
1053 fn mvalue(&self) -> Option<crate::menu::MenuItem>;
1054 /// Get the previously selected menu item
1055 fn prev_mvalue(&self) -> Option<crate::menu::MenuItem>;
1056}
1057
1058/// Defines the methods implemented by all valuator widgets
1059/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/valuators).
1060/// # Safety
1061/// fltk-rs traits depend on some FLTK internal code
1062/// # Warning
1063/// fltk-rs traits are non-exhaustive,
1064/// to avoid future breakage if you try to implement them manually,
1065/// use the Deref and DerefMut pattern or the `widget_extends!` macro
1066pub unsafe trait ValuatorExt: WidgetExt {
1067 /// Set bounds of a valuator
1068 fn set_bounds(&mut self, a: f64, b: f64);
1069 /// Get the minimum bound of a valuator
1070 fn minimum(&self) -> f64;
1071 /// Set the minimum bound of a valuator
1072 fn set_minimum(&mut self, a: f64);
1073 /// Get the maximum bound of a valuator
1074 fn maximum(&self) -> f64;
1075 /// Set the maximum bound of a valuator
1076 fn set_maximum(&mut self, a: f64);
1077 /// Set the range of a valuator
1078 fn set_range(&mut self, a: f64, b: f64);
1079 /// Set change step of a valuator.
1080 /// Rounds to multiples of a/b, or no rounding if a is zero
1081 fn set_step(&mut self, a: f64, b: i32);
1082 /// Get change step of a valuator
1083 fn step(&self) -> f64;
1084 /// Set the precision of a valuator
1085 fn set_precision(&mut self, digits: i32);
1086 /// Get the value of a valuator
1087 fn value(&self) -> f64;
1088 /// Set the value of a valuator
1089 fn set_value(&mut self, arg2: f64);
1090 /// Set the format of a valuator
1091 /// # Errors
1092 /// Errors on failure to set the format of the widget
1093 fn format(&mut self, arg2: &str) -> Result<(), FltkError>;
1094 /// Round the valuator
1095 fn round(&self, arg2: f64) -> f64;
1096 /// Clamp the valuator
1097 fn clamp(&self, arg2: f64) -> f64;
1098 /// Increment the valuator
1099 fn increment(&mut self, arg2: f64, arg3: i32) -> f64;
1100}
1101
1102/// Defines the methods implemented by `TextDisplay` and `TextEditor`
1103/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/text).
1104/// # Safety
1105/// fltk-rs traits depend on some FLTK internal code
1106/// # Warning
1107/// fltk-rs traits are non-exhaustive,
1108/// to avoid future breakage if you try to implement them manually,
1109/// use the Deref and DerefMut pattern or the `widget_extends!` macro
1110pub unsafe trait DisplayExt: WidgetExt {
1111 /// Check if the Display widget has an associated buffer
1112 #[doc(hidden)]
1113 fn has_buffer(&self) -> bool;
1114 /// Get the associated `TextBuffer`
1115 fn buffer(&self) -> Option<crate::text::TextBuffer>;
1116 /// Sets the associated `TextBuffer`.
1117 /// Since the widget is long-lived, the lifetime of the buffer is prolonged to the lifetime of the program
1118 fn set_buffer<B: Into<Option<crate::text::TextBuffer>>>(&mut self, buffer: B);
1119 /// Get the associated style `TextBuffer`
1120 fn style_buffer(&self) -> Option<crate::text::TextBuffer>;
1121 /// Return the text font
1122 fn text_font(&self) -> Font;
1123 /// Sets the text font
1124 fn set_text_font(&mut self, font: Font);
1125 /// Return the text color
1126 fn text_color(&self) -> Color;
1127 /// Sets the text color
1128 fn set_text_color(&mut self, color: Color);
1129 /// Return the text size
1130 fn text_size(&self) -> i32;
1131 /// Sets the text size
1132 fn set_text_size(&mut self, sz: i32);
1133 /// Scroll down the Display widget
1134 fn scroll(&mut self, top_line_num: i32, horiz_offset: i32);
1135 /// Insert into Display widget
1136 fn insert(&self, text: &str);
1137 /// Set the insert position
1138 fn set_insert_position(&mut self, new_pos: i32);
1139 /// Return the insert position
1140 fn insert_position(&self) -> i32;
1141 /// Gets the x and y positions of the cursor
1142 fn position_to_xy(&self, pos: i32) -> (i32, i32);
1143 /// Counts the lines from start to end
1144 fn count_lines(&self, start: i32, end: i32, is_line_start: bool) -> i32;
1145 /// Moves the cursor right
1146 /// # Errors
1147 /// Errors on failure to move the cursor
1148 fn move_right(&mut self) -> Result<(), FltkError>;
1149 /// Moves the cursor left
1150 /// # Errors
1151 /// Errors on failure to move the cursor
1152 fn move_left(&mut self) -> Result<(), FltkError>;
1153 /// Moves the cursor up
1154 /// # Errors
1155 /// Errors on failure to move the cursor
1156 fn move_up(&mut self) -> Result<(), FltkError>;
1157 /// Moves the cursor down
1158 /// # Errors
1159 /// Errors on failure to move the cursor
1160 fn move_down(&mut self) -> Result<(), FltkError>;
1161 /// Shows/hides the cursor
1162 fn show_cursor(&mut self, val: bool);
1163 /// Sets the style of the text widget
1164 fn set_highlight_data<
1165 B: Into<Option<crate::text::TextBuffer>>,
1166 E: Into<Vec<crate::text::StyleTableEntry>>,
1167 >(
1168 &mut self,
1169 style_buffer: B,
1170 entries: E,
1171 );
1172 /// Sets the style of the text widget
1173 fn set_highlight_data_ext<
1174 B: Into<Option<crate::text::TextBuffer>>,
1175 E: Into<Vec<crate::text::StyleTableEntryExt>>,
1176 >(
1177 &mut self,
1178 style_buffer: B,
1179 entries: E,
1180 );
1181 /// Unset the style of the text widget
1182 fn unset_highlight_data<B: Into<Option<crate::text::TextBuffer>>>(&mut self, style_buffer: B);
1183 /// Sets the cursor style
1184 fn set_cursor_style(&mut self, style: crate::text::Cursor);
1185 /// Sets the cursor color
1186 fn set_cursor_color(&mut self, color: Color);
1187 /// Sets the scrollbar size in pixels
1188 fn set_scrollbar_size(&mut self, size: i32);
1189 /// Sets the scrollbar alignment
1190 fn set_scrollbar_align(&mut self, align: Align);
1191 /// Returns the cursor style
1192 fn cursor_style(&self) -> crate::text::Cursor;
1193 /// Returns the cursor color
1194 fn cursor_color(&self) -> Color;
1195 /// Returns the scrollbar size in pixels
1196 fn scrollbar_size(&self) -> i32;
1197 /// Returns the scrollbar alignment
1198 fn scrollbar_align(&self) -> Align;
1199 /// Returns the beginning of the line from the current position.
1200 /// Returns new position as index
1201 fn line_start(&self, pos: i32) -> i32;
1202 /// Returns the ending of the line from the current position.
1203 /// Returns new position as index
1204 fn line_end(&self, start_pos: i32, is_line_start: bool) -> i32;
1205 /// Skips lines from `start_pos`
1206 fn skip_lines(&mut self, start_pos: i32, lines: i32, is_line_start: bool) -> i32;
1207 /// Rewinds the lines
1208 fn rewind_lines(&mut self, start_pos: i32, lines: i32) -> i32;
1209 /// Goes to the next word
1210 fn next_word(&mut self);
1211 /// Goes to the previous word
1212 fn previous_word(&mut self);
1213 /// Returns the position of the start of the word, relative to the current position
1214 fn word_start(&self, pos: i32) -> i32;
1215 /// Returns the position of the end of the word, relative to the current position
1216 fn word_end(&self, pos: i32) -> i32;
1217 /// Convert an x pixel position into a column number.
1218 fn x_to_col(&self, x: f64) -> f64;
1219 /// Convert a column number into an x pixel position
1220 fn col_to_x(&self, col: f64) -> f64;
1221 /// Sets the linenumber width
1222 fn set_linenumber_width(&mut self, w: i32);
1223 /// Gets the linenumber width
1224 fn linenumber_width(&self) -> i32;
1225 /// Sets the linenumber font
1226 fn set_linenumber_font(&mut self, font: Font);
1227 /// Gets the linenumber font
1228 fn linenumber_font(&self) -> Font;
1229 /// Sets the linenumber size
1230 fn set_linenumber_size(&mut self, size: i32);
1231 /// Gets the linenumber size
1232 fn linenumber_size(&self) -> i32;
1233 /// Sets the linenumber foreground color
1234 fn set_linenumber_fgcolor(&mut self, color: Color);
1235 /// Gets the linenumber foreground color
1236 fn linenumber_fgcolor(&self) -> Color;
1237 /// Sets the linenumber background color
1238 fn set_linenumber_bgcolor(&mut self, color: Color);
1239 /// Gets the linenumber background color
1240 fn linenumber_bgcolor(&self) -> Color;
1241 /// Sets the linenumber alignment
1242 fn set_linenumber_align(&mut self, align: Align);
1243 /// Gets the linenumber alignment
1244 fn linenumber_align(&self) -> Align;
1245 /// Sets the line number format string
1246 fn set_linenumber_format(&mut self, fmt: &str);
1247 /// Gets the current line number format string
1248 fn linenumber_format(&self) -> Option<String>;
1249 /// Query style position in line
1250 fn position_style(&self, line_start_pos: i32, line_len: i32, line_index: i32) -> i32;
1251 /// Maintain absolute top line number state
1252 fn maintain_absolute_top_line_number(&mut self, state: bool);
1253 /// Get absolute top line number
1254 fn get_absolute_top_line_number(&self) -> i32;
1255 /// Update absolute top line number with old first char
1256 fn absolute_top_line_number(&mut self, old_first_char: i32);
1257 /// Return whether maintaining absolute top line number is enabled
1258 fn maintaining_absolute_top_line_number(&self) -> bool;
1259 /// Reset absolute top line number tracking
1260 fn reset_absolute_top_line_number(&mut self);
1261 /// Checks whether a pixel is within a text selection
1262 fn in_selection(&self, x: i32, y: i32) -> bool;
1263 /// Sets the wrap mode of the Display widget.
1264 /// If the wrap mode is `AtColumn`, wrap margin is the column.
1265 /// If the wrap mode is `AtPixel`, wrap margin is the pixel.
1266 /// For more [info](https://www.fltk.org/doc-1.4/classFl__Text__Display.html#ab9378d48b949f8fc7da04c6be4142c54)
1267 fn wrap_mode(&mut self, wrap: crate::text::WrapMode, wrap_margin: i32);
1268 /// Correct a column number based on an unconstrained position
1269 fn wrapped_column(&self, row: i32, column: i32) -> i32;
1270 /// Correct a row number from an unconstrained position
1271 fn wrapped_row(&self, row: i32) -> i32;
1272 /// Set the grammar underline color
1273 fn set_grammar_underline_color(&mut self, color: Color);
1274 /// Get the grammar underline color
1275 fn grammar_underline_color(&self) -> Color;
1276 /// Set the spelling underline color
1277 fn set_spelling_underline_color(&mut self, color: Color);
1278 /// Get the spelling underline color
1279 fn spelling_underline_color(&self) -> Color;
1280 /// Set the secondary selection color
1281 fn set_secondary_selection_color(&mut self, color: Color);
1282 /// Get the secondary selection color
1283 fn secondary_selection_color(&self) -> Color;
1284 /// Scrolls the text buffer to show the current insert position
1285 fn show_insert_position(&mut self);
1286 /// Overstrikes the current selection or inserts text at cursor
1287 fn overstrike(&mut self, text: &str);
1288 /// Redisplay a range of text
1289 fn redisplay_range(&mut self, start: i32, end: i32);
1290 /// Converts x and y pixel positions into a position in the text buffer
1291 fn xy_to_position(&self, x: i32, y: i32, pos_type: crate::text::PositionType) -> i32;
1292 /// Converts x and y pixel positions into a column and row number
1293 fn xy_to_rowcol(&self, x: i32, y: i32, pos_type: crate::text::PositionType) -> (i32, i32);
1294 /// Returns the number of rows
1295 fn scroll_row(&self) -> i32;
1296 /// Returns the number of columns
1297 fn scroll_col(&self) -> i32;
1298}
1299
1300/// Defines the methods implemented by all browser types
1301/// More info can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/browsers)
1302/// # Safety
1303/// fltk-rs traits depend on some FLTK internal code
1304/// # Warning
1305/// fltk-rs traits are non-exhaustive,
1306/// to avoid future breakage if you try to implement them manually,
1307/// use the Deref and DerefMut pattern or the `widget_extends!` macro
1308pub unsafe trait BrowserExt: WidgetExt {
1309 /// Removes the specified line.
1310 /// Lines start at 1
1311 fn remove(&mut self, line: i32);
1312 /// Adds an item
1313 fn add(&mut self, item: &str);
1314 /// Adds an item with associated data
1315 fn add_with_data<T: Clone + 'static>(&mut self, item: &str, data: T);
1316 /// Inserts an item at an index.
1317 /// Lines start at 1
1318 fn insert(&mut self, line: i32, item: &str);
1319 /// Inserts an item at an index with associated data.
1320 /// Lines start at 1
1321 fn insert_with_data<T: Clone + 'static>(&mut self, line: i32, item: &str, data: T);
1322 /// Moves an item.
1323 /// Lines start at 1
1324 fn move_item(&mut self, to: i32, from: i32);
1325 /// Swaps 2 items.
1326 /// Lines start at 1
1327 fn swap(&mut self, a: i32, b: i32);
1328 /// Clears the browser widget
1329 fn clear(&mut self);
1330 /// Returns the number of items
1331 fn size(&self) -> i32;
1332 /// Select an item at the specified line.
1333 /// Lines start at 1
1334 fn select(&mut self, line: i32);
1335 /// Select an item at the specified line.
1336 /// Lines start at 1
1337 fn deselect(&mut self, line: i32);
1338 /// Returns whether the item is selected
1339 /// Lines start at 1
1340 fn selected(&self, line: i32) -> bool;
1341 /// Returns the text of the item at `line`.
1342 /// Lines start at 1
1343 fn text(&self, line: i32) -> Option<String>;
1344 /// Returns the text of the selected item.
1345 /// Lines start at 1
1346 fn selected_text(&self) -> Option<String>;
1347 /// Sets the text of the selected item.
1348 /// Lines start at 1
1349 fn set_text(&mut self, line: i32, txt: &str);
1350 /// Load a file
1351 /// # Errors
1352 /// Errors on non-existent paths
1353 fn load<P: AsRef<std::path::Path>>(&mut self, path: P) -> Result<(), FltkError>;
1354 /// Return the text size
1355 fn text_size(&self) -> i32;
1356 /// Sets the text size.
1357 /// Lines start at 1
1358 fn set_text_size(&mut self, sz: i32);
1359 /// Sets the icon for browser elements.
1360 /// Lines start at 1
1361 fn set_icon<Img: ImageExt>(&mut self, line: i32, image: Option<Img>);
1362 /// Returns the icon of a browser element.
1363 /// Lines start at 1
1364 fn icon(&self, line: i32) -> Option<Box<dyn ImageExt>>;
1365 /// Removes the icon of a browser element.
1366 /// Lines start at 1
1367 fn remove_icon(&mut self, line: i32);
1368 /// Scrolls the browser so the top item in the browser is showing the specified line.
1369 /// Lines start at 1
1370 fn top_line(&mut self, line: i32);
1371 /// Scrolls the browser so the bottom item in the browser is showing the specified line.
1372 /// Lines start at 1
1373 fn bottom_line(&mut self, line: i32);
1374 /// Scrolls the browser so the middle item in the browser is showing the specified line.
1375 /// Lines start at 1
1376 fn middle_line(&mut self, line: i32);
1377 /// Gets the current format code prefix character, which by default is '\@'.
1378 /// More info [here](https://www.fltk.org/doc-1.3/classFl__Browser.html#a129dca59d64baf166503ba59341add69)
1379 fn format_char(&self) -> char;
1380 /// Sets the current format code prefix character to \p c. The default prefix is '\@'.
1381 /// c should be ascii
1382 fn set_format_char(&mut self, c: char);
1383 /// Gets the current column separator character. The default is '\t'
1384 fn column_char(&self) -> char;
1385 /// Sets the column separator to c. This will only have an effect if you also use `set_column_widths()`.
1386 /// c should be ascii
1387 fn set_column_char(&mut self, c: char);
1388 /// Gets the current column width array
1389 fn column_widths(&self) -> Vec<i32>;
1390 /// Sets the current column width array
1391 fn set_column_widths(&mut self, arr: &[i32]);
1392 /// Returns whether a certain line is displayed
1393 fn displayed(&self, line: i32) -> bool;
1394 /// Makes a specified line visible
1395 fn make_visible(&mut self, line: i32);
1396 /// Gets the vertical scroll position of the list as a pixel position
1397 fn position(&self) -> i32;
1398 /// Sets the vertical scroll position of the list as a pixel position
1399 fn set_position(&mut self, pos: i32);
1400 /// Gets the horizontal scroll position of the list as a pixel position
1401 fn hposition(&self) -> i32;
1402 /// Sets the horizontal scroll position of the list as a pixel position
1403 fn set_hposition(&mut self, pos: i32);
1404 /// Returns the type of scrollbar associated with the browser
1405 fn has_scrollbar(&self) -> crate::browser::BrowserScrollbar;
1406 /// Sets the type of scrollbar associated with the browser
1407 fn set_has_scrollbar(&mut self, mode: crate::browser::BrowserScrollbar);
1408 /// Gets the scrollbar size
1409 fn scrollbar_size(&self) -> i32;
1410 /// Sets the scrollbar size
1411 fn set_scrollbar_size(&mut self, new_size: i32);
1412 /// Sorts the items of the browser
1413 fn sort(&mut self);
1414 /// Returns the vertical scrollbar
1415 fn scrollbar(&self) -> crate::valuator::Scrollbar;
1416 /// Returns the horizontal scrollbar
1417 fn hscrollbar(&self) -> crate::valuator::Scrollbar;
1418 /// Returns the selected line, returns 0 if no line is selected
1419 fn value(&self) -> i32;
1420 /// Set the data associated with the line
1421 fn set_data<T: Clone + 'static>(&mut self, line: i32, data: T);
1422 /// Get the data associated with the line
1423 /// # Safety
1424 /// Type correctness is insured by the developer
1425 unsafe fn data<T: Clone + 'static>(&self, line: i32) -> Option<T>;
1426 /// Hides a the specified line
1427 fn hide_line(&mut self, line: i32);
1428 /// Gets the selected items
1429 fn selected_items(&self) -> Vec<i32>;
1430}
1431
1432/// Defines the methods implemented by table types.
1433/// More details can be found in the [wiki](https://github.com/fltk-rs/fltk-rs/wiki/tables).
1434/// # Safety
1435/// fltk-rs traits depend on some FLTK internal code
1436/// # Warning
1437/// fltk-rs traits are non-exhaustive,
1438/// to avoid future breakage if you try to implement them manually,
1439/// use the Deref and DerefMut pattern or the `widget_extends!` macro
1440pub unsafe trait TableExt: GroupExt {
1441 /// Clears the table
1442 fn clear(&mut self);
1443 /// Sets the table frame
1444 fn set_table_frame(&mut self, frame: FrameType);
1445 /// Gets the table frame
1446 fn table_frame(&self) -> FrameType;
1447 /// Sets the number of rows
1448 fn set_rows(&mut self, val: i32);
1449 /// Gets the number of rows
1450 fn rows(&self) -> i32;
1451 /// Sets the number of columns
1452 fn set_cols(&mut self, val: i32);
1453 /// Gets the number of columns
1454 fn cols(&self) -> i32;
1455 /// The range of row and column numbers for all visible and partially visible cells in the table.
1456 /// Returns (`row_top`, `col_left`, `row_bot`, `col_right`)
1457 fn visible_cells(&self) -> (i32, i32, i32, i32);
1458 /// The range of row and column numbers for all visible and partially visible cells in the table.
1459 /// Returns (`row_top`, `col_left`, `row_bot`, `col_right`)
1460 fn try_visible_cells(&self) -> Option<(i32, i32, i32, i32)>;
1461 /// Returns whether the resize is interactive
1462 fn is_interactive_resize(&self) -> bool;
1463 /// Returns whether a row is resizable
1464 fn row_resize(&self) -> bool;
1465 /// Sets a row to be resizable
1466 fn set_row_resize(&mut self, flag: bool);
1467 /// Returns whether a column is resizable
1468 fn col_resize(&self) -> bool;
1469 /// Sets a column to be resizable
1470 fn set_col_resize(&mut self, flag: bool);
1471 /// Returns the current column minimum resize value.
1472 fn col_resize_min(&self) -> i32;
1473 /// Sets the current column minimum resize value.
1474 fn set_col_resize_min(&mut self, val: i32);
1475 /// Returns the current row minimum resize value.
1476 fn row_resize_min(&self) -> i32;
1477 /// Sets the current row minimum resize value.
1478 fn set_row_resize_min(&mut self, val: i32);
1479 /// Returns if row headers are enabled or not
1480 fn row_header(&self) -> bool;
1481 /// Sets whether a row headers are enabled or not
1482 fn set_row_header(&mut self, flag: bool);
1483 /// Returns if column headers are enabled or not
1484 fn col_header(&self) -> bool;
1485 /// Sets whether a column headers are enabled or not
1486 fn set_col_header(&mut self, flag: bool);
1487 /// Sets the column header height
1488 fn set_col_header_height(&mut self, height: i32);
1489 /// Gets the column header height
1490 fn col_header_height(&self) -> i32;
1491 /// Sets the row header width
1492 fn set_row_header_width(&mut self, width: i32);
1493 /// Gets the row header width
1494 fn row_header_width(&self) -> i32;
1495 /// Sets the row header color
1496 fn set_row_header_color(&mut self, val: Color);
1497 /// Gets the row header color
1498 fn row_header_color(&self) -> Color;
1499 /// Sets the column header color
1500 fn set_col_header_color(&mut self, val: Color);
1501 /// Gets the row header color
1502 fn col_header_color(&self) -> Color;
1503 /// Sets the row's height
1504 fn set_row_height(&mut self, row: i32, height: i32);
1505 /// Gets the row's height
1506 fn row_height(&self, row: i32) -> i32;
1507 /// Sets the column's width
1508 fn set_col_width(&mut self, col: i32, width: i32);
1509 /// Gets the column's width
1510 fn col_width(&self, col: i32) -> i32;
1511 /// Sets all rows height
1512 fn set_row_height_all(&mut self, height: i32);
1513 /// Sets all column's width
1514 fn set_col_width_all(&mut self, width: i32);
1515 /// Sets the row's position
1516 fn set_row_position(&mut self, row: i32);
1517 /// Sets the column's position
1518 fn set_col_position(&mut self, col: i32);
1519 /// Gets the row's position
1520 fn row_position(&self) -> i32;
1521 /// Gets the column's position
1522 fn col_position(&self) -> i32;
1523 /// Sets the top row
1524 fn set_top_row(&mut self, row: i32);
1525 /// Gets the top row
1526 fn top_row(&self) -> i32;
1527 /// Returns whether a cell is selected
1528 fn is_selected(&self, r: i32, c: i32) -> bool;
1529 /// Gets the selection.
1530 /// Returns (`row_top`, `col_left`, `row_bot`, `col_right`).
1531 /// Returns -1 if no selection.
1532 fn get_selection(&self) -> (i32, i32, i32, i32);
1533 /// Tries to get the selection.
1534 /// Returns an Option((`row_top`, `col_left`, `row_bot`, `col_right`))
1535 fn try_get_selection(&self) -> Option<(i32, i32, i32, i32)>;
1536 /// Sets the selection
1537 fn set_selection(&mut self, row_top: i32, col_left: i32, row_bot: i32, col_right: i32);
1538 /// Unset selection
1539 fn unset_selection(&mut self);
1540 /// Moves the cursor with shift select
1541 /// # Errors
1542 /// Errors on failure to move the cursor
1543 fn move_cursor_with_shift_select(
1544 &mut self,
1545 r: i32,
1546 c: i32,
1547 shiftselect: bool,
1548 ) -> Result<(), FltkError>;
1549 /// Moves the cursor
1550 /// # Errors
1551 /// Errors on failure to move the cursor
1552 fn move_cursor(&mut self, r: i32, c: i32) -> Result<(), FltkError>;
1553 /// Returns the scrollbar size
1554 fn scrollbar_size(&self) -> i32;
1555 /// Sets the scrollbar size
1556 fn set_scrollbar_size(&mut self, new_size: i32);
1557 /// Sets whether tab key cell navigation is enabled
1558 fn set_tab_cell_nav(&mut self, val: bool);
1559 /// Returns whether tab key cell navigation is enabled
1560 fn tab_cell_nav(&self) -> bool;
1561 /// Override `draw_cell`.
1562 /// callback args: &mut self, `TableContext`, Row: i32, Column: i32, X: i32, Y: i32, Width: i32 and Height: i32.
1563 /// takes the widget as a closure argument
1564 fn draw_cell<
1565 F: FnMut(&mut Self, crate::table::TableContext, i32, i32, i32, i32, i32, i32) + 'static,
1566 >(
1567 &mut self,
1568 cb: F,
1569 );
1570 #[doc(hidden)]
1571 /// INTERNAL: Retrieve the draw cell data
1572 /// # Safety
1573 /// Can return multiple mutable references to the `draw_cell_data`
1574 unsafe fn draw_cell_data(&self) -> Option<Box<dyn FnMut()>>;
1575 /// Get the callback column, should be called from within a callback
1576 fn callback_col(&self) -> i32;
1577 /// Get the callback row, should be called from within a callback
1578 fn callback_row(&self) -> i32;
1579 /// Get the callback context, should be called from within a callback
1580 fn callback_context(&self) -> crate::table::TableContext;
1581 /// Returns the table's vertical scrollbar
1582 fn scrollbar(&self) -> crate::valuator::Scrollbar;
1583 /// Returns the table's horizontal scrollbar
1584 fn hscrollbar(&self) -> crate::valuator::Scrollbar;
1585 /// Returns the Scroll child of the Table
1586 fn scroll(&self) -> Option<crate::group::Scroll>;
1587 /// Find a cell's coords and size by row and column
1588 fn find_cell(
1589 &self,
1590 ctx: crate::table::TableContext,
1591 row: i32,
1592 col: i32,
1593 ) -> Option<(i32, i32, i32, i32)>;
1594 /// Get the cursor to row/col
1595 fn cursor2rowcol(
1596 &self,
1597 ) -> Option<(
1598 crate::table::TableContext,
1599 i32,
1600 i32,
1601 crate::table::TableResizeFlag,
1602 )>;
1603}
1604
1605/// Defines the methods implemented by all image types
1606/// # Safety
1607/// fltk-rs traits depend on some FLTK internal code
1608/// # Warning
1609/// fltk-rs traits are non-exhaustive,
1610/// to avoid future breakage if you try to implement them manually,
1611/// use the Deref and DerefMut pattern
1612pub unsafe trait ImageExt {
1613 /// Performs a deep copy of the image
1614 fn copy(&self) -> Self
1615 where
1616 Self: Sized;
1617 /// Performs a deep copy of the image but to a new size. This will make use of the scaling algorithm when resizing.
1618 fn copy_sized(&self, w: i32, h: i32) -> Self
1619 where
1620 Self: Sized;
1621 /// Draws the image at the presupplied coordinates and size
1622 fn draw(&mut self, x: i32, y: i32, width: i32, height: i32);
1623 /// Draws the image at the presupplied coordinates and size and offset cx, cy
1624 fn draw_ext(&mut self, x: i32, y: i32, width: i32, height: i32, cx: i32, cy: i32);
1625 /// Return the width of the image
1626 fn width(&self) -> i32;
1627 /// Return the height of the image
1628 fn height(&self) -> i32;
1629 /// Return the width of the image
1630 fn w(&self) -> i32;
1631 /// Return the height of the image
1632 fn h(&self) -> i32;
1633 /// Returns a pointer of the image
1634 fn as_image_ptr(&self) -> *mut fltk_sys::image::Fl_Image;
1635 /// Transforms a raw image pointer to an image
1636 /// # Safety
1637 /// The pointer must be valid
1638 unsafe fn from_image_ptr(ptr: *mut fltk_sys::image::Fl_Image) -> Self
1639 where
1640 Self: Sized;
1641 /// Returns the underlying raw rgb image data
1642 fn to_rgb_data(&self) -> Vec<u8>;
1643 /// Returns the underlying raw image data
1644 fn to_raw_data(&self) -> *const *const u8;
1645 /// Transforms the image into an `RgbImage`
1646 /// # Errors
1647 /// Errors on failure to transform to `RgbImage`
1648 fn to_rgb(&self) -> Result<crate::image::RgbImage, FltkError>;
1649 /// Transforms the image into an `RgbImage`
1650 /// # Errors
1651 /// Errors on failure to transform to `RgbImage`
1652 fn to_rgb_image(&self) -> Result<crate::image::RgbImage, FltkError>;
1653 /// Scales the image
1654 fn scale(&mut self, width: i32, height: i32, proportional: bool, can_expand: bool);
1655 /// Return the count of pointers in an image (Pixmaps have more than 1, bitmaps have 0, Rgb based images have 1)
1656 fn count(&self) -> i32;
1657 /// Gets the image's data width
1658 fn data_w(&self) -> i32;
1659 /// Gets the image's data height
1660 fn data_h(&self) -> i32;
1661 /// Gets the image's depth
1662 fn depth(&self) -> ColorDepth;
1663 /// Gets the image's line data size
1664 fn ld(&self) -> i32;
1665 /// Greys the image
1666 fn inactive(&mut self);
1667 /// Deletes the image
1668 /// # Safety
1669 /// An image shouldn't be deleted while it's being used by a widget
1670 unsafe fn delete(img: Self)
1671 where
1672 Self: Sized;
1673 /// Checks if the image was deleted
1674 fn was_deleted(&self) -> bool;
1675 /// Transforms an Image base into another Image
1676 /// # Safety
1677 /// Can be unsafe if used to downcast to an image of different format
1678 unsafe fn into_image<I: ImageExt>(self) -> I
1679 where
1680 Self: Sized;
1681 /// Blend the image with color c with weight i in range 0 to 1
1682 fn color_average(&mut self, c: crate::enums::Color, i: f32);
1683 /// Desaturate (grayscale) the image
1684 fn desaturate(&mut self);
1685 /// Clear internal caches
1686 fn uncache(&mut self);
1687 #[doc(hidden)]
1688 /// Set this image as the label image for a widget
1689 fn label_for_widget<W: WidgetExt>(&self, item: &mut W)
1690 where
1691 Self: Sized;
1692 #[doc(hidden)]
1693 /// Set this image as the label image for a menu item
1694 fn label_for_menu_item(&self, item: &mut crate::menu::MenuItem);
1695 #[doc(hidden)]
1696 /// Cast an image back to its original type
1697 fn from_dyn_image_ptr(_p: *mut fltk_sys::image::Fl_Image) -> Option<Self>
1698 where
1699 Self: Sized,
1700 {
1701 None
1702 }
1703 #[doc(hidden)]
1704 /// Cast an image back to its original type
1705 fn from_dyn_image<I: ImageExt>(_i: &I) -> Option<Self>
1706 where
1707 Self: Sized,
1708 {
1709 None
1710 }
1711}
1712
1713/// Defines the methods implemented by all surface types, currently `ImageSurface`
1714pub trait SurfaceDevice {
1715 /// Checks whether this surface is the current surface
1716 fn is_current(&self) -> bool;
1717 /// Get the current surface
1718 fn surface() -> Self;
1719 /// Push a surface as a current surface
1720 fn push_current(new_current: &Self);
1721 /// Pop the current surface
1722 fn pop_current();
1723}
1724
1725/// Defines a set of convenience functions for constructing and anchoring custom widgets.
1726/// Usage: fltk::widget_extends!(CustomWidget, BaseWidget, member);
1727/// It basically implements Deref and DerefMut on the custom widget, and adds the aforementioned methods.
1728/// This means you can call widget methods directly, for e.x. `custom_widget.set_color(enums::Color::Red)`.
1729/// For your custom widget to be treated as a widget it would need dereferencing: `group.add(&*custom_widget);`
1730#[macro_export]
1731macro_rules! widget_extends {
1732 ($widget:ty, $base:ty, $member:tt) => {
1733 impl $widget {
1734 /// Initialize to position x, y
1735 pub fn with_pos(mut self, x: i32, y: i32) -> Self {
1736 let w = self.w();
1737 let h = self.h();
1738 self.resize(x, y, w, h);
1739 self
1740 }
1741
1742 /// Initialize to size width, height
1743 pub fn with_size(mut self, width: i32, height: i32) -> Self {
1744 let x = self.x();
1745 let y = self.y();
1746 let w = self.width();
1747 let h = self.height();
1748 if w == 0 || h == 0 {
1749 self.widget_resize(x, y, width, height);
1750 } else {
1751 self.resize(x, y, width, height);
1752 }
1753 self
1754 }
1755
1756 /// Initialize with a label
1757 pub fn with_label(mut self, title: &str) -> Self {
1758 self.set_label(title);
1759 self
1760 }
1761
1762 /// Initialize with alignment
1763 pub fn with_align(mut self, align: $crate::enums::Align) -> Self {
1764 self.set_align(align);
1765 self
1766 }
1767
1768 /// Initialize with type
1769 pub fn with_type<T: $crate::prelude::WidgetType>(mut self, typ: T) -> Self {
1770 self.set_type(typ);
1771 self
1772 }
1773
1774 /// Initialize at bottom of another widget
1775 pub fn below_of<W: $crate::prelude::WidgetExt>(
1776 mut self,
1777 wid: &W,
1778 padding: i32,
1779 ) -> Self {
1780 let w = self.w();
1781 let h = self.h();
1782 debug_assert!(
1783 w != 0 && h != 0,
1784 "below_of requires the size of the widget to be known!"
1785 );
1786 self.resize(wid.x(), wid.y() + wid.h() + padding, w, h);
1787 self
1788 }
1789
1790 /// Initialize above of another widget
1791 pub fn above_of<W: $crate::prelude::WidgetExt>(
1792 mut self,
1793 wid: &W,
1794 padding: i32,
1795 ) -> Self {
1796 let w = self.w();
1797 let h = self.h();
1798 debug_assert!(
1799 w != 0 && h != 0,
1800 "above_of requires the size of the widget to be known!"
1801 );
1802 self.resize(wid.x(), wid.y() - padding - h, w, h);
1803 self
1804 }
1805
1806 /// Initialize right of another widget
1807 pub fn right_of<W: $crate::prelude::WidgetExt>(
1808 mut self,
1809 wid: &W,
1810 padding: i32,
1811 ) -> Self {
1812 let w = self.w();
1813 let h = self.h();
1814 debug_assert!(
1815 w != 0 && h != 0,
1816 "right_of requires the size of the widget to be known!"
1817 );
1818 self.resize(wid.x() + wid.width() + padding, wid.y(), w, h);
1819 self
1820 }
1821
1822 /// Initialize left of another widget
1823 pub fn left_of<W: $crate::prelude::WidgetExt>(mut self, wid: &W, padding: i32) -> Self {
1824 let w = self.w();
1825 let h = self.h();
1826 debug_assert!(
1827 w != 0 && h != 0,
1828 "left_of requires the size of the widget to be known!"
1829 );
1830 self.resize(wid.x() - w - padding, wid.y(), w, h);
1831 self
1832 }
1833
1834 /// Initialize center of another widget
1835 pub fn center_of<W: $crate::prelude::WidgetExt>(mut self, w: &W) -> Self {
1836 debug_assert!(
1837 w.width() != 0 && w.height() != 0,
1838 "center_of requires the size of the widget to be known!"
1839 );
1840 let sw = self.width() as f64;
1841 let sh = self.height() as f64;
1842 let ww = w.width() as f64;
1843 let wh = w.height() as f64;
1844 let sx = (ww - sw) / 2.0;
1845 let sy = (wh - sh) / 2.0;
1846 let wx = if w.as_window().is_some() { 0 } else { w.x() };
1847 let wy = if w.as_window().is_some() { 0 } else { w.y() };
1848 self.resize(sx as i32 + wx, sy as i32 + wy, sw as i32, sh as i32);
1849 self.redraw();
1850 self
1851 }
1852
1853 /// Initialize center of another widget on the x axis
1854 pub fn center_x<W: $crate::prelude::WidgetExt>(mut self, w: &W) -> Self {
1855 debug_assert!(
1856 w.width() != 0 && w.height() != 0,
1857 "center_of requires the size of the widget to be known!"
1858 );
1859 let sw = self.width() as f64;
1860 let sh = self.height() as f64;
1861 let ww = w.width() as f64;
1862 let sx = (ww - sw) / 2.0;
1863 let sy = self.y();
1864 let wx = if w.as_window().is_some() { 0 } else { w.x() };
1865 self.resize(sx as i32 + wx, sy, sw as i32, sh as i32);
1866 self.redraw();
1867 self
1868 }
1869
1870 /// Initialize center of another widget on the y axis
1871 pub fn center_y<W: $crate::prelude::WidgetExt>(mut self, w: &W) -> Self {
1872 debug_assert!(
1873 w.width() != 0 && w.height() != 0,
1874 "center_of requires the size of the widget to be known!"
1875 );
1876 let sw = self.width() as f64;
1877 let sh = self.height() as f64;
1878 let wh = w.height() as f64;
1879 let sx = self.x();
1880 let sy = (wh - sh) / 2.0;
1881 let wy = if w.as_window().is_some() { 0 } else { w.y() };
1882 self.resize(sx, sy as i32 + wy, sw as i32, sh as i32);
1883 self.redraw();
1884 self
1885 }
1886
1887 /// Initialize center of parent
1888 pub fn center_of_parent(mut self) -> Self {
1889 if let Some(w) = self.parent() {
1890 debug_assert!(
1891 w.width() != 0 && w.height() != 0,
1892 "center_of requires the size of the widget to be known!"
1893 );
1894 let sw = self.width() as f64;
1895 let sh = self.height() as f64;
1896 let ww = w.width() as f64;
1897 let wh = w.height() as f64;
1898 let sx = (ww - sw) / 2.0;
1899 let sy = (wh - sh) / 2.0;
1900 let wx = if w.as_window().is_some() { 0 } else { w.x() };
1901 let wy = if w.as_window().is_some() { 0 } else { w.y() };
1902 self.resize(sx as i32 + wx, sy as i32 + wy, sw as i32, sh as i32);
1903 self.redraw();
1904 }
1905 self
1906 }
1907
1908 /// Initialize to the size of another widget
1909 pub fn size_of<W: $crate::prelude::WidgetExt>(mut self, w: &W) -> Self {
1910 debug_assert!(
1911 w.width() != 0 && w.height() != 0,
1912 "size_of requires the size of the widget to be known!"
1913 );
1914 let x = self.x();
1915 let y = self.y();
1916 self.resize(x, y, w.width(), w.height());
1917 self
1918 }
1919
1920 /// Initialize to the size of the parent
1921 pub fn size_of_parent(mut self) -> Self {
1922 if let Some(parent) = self.parent() {
1923 let w = parent.width();
1924 let h = parent.height();
1925 let x = self.x();
1926 let y = self.y();
1927 self.resize(x, y, w, h);
1928 }
1929 self
1930 }
1931 }
1932
1933 impl std::ops::Deref for $widget {
1934 type Target = $base;
1935
1936 fn deref(&self) -> &Self::Target {
1937 &self.$member
1938 }
1939 }
1940
1941 impl std::ops::DerefMut for $widget {
1942 fn deref_mut(&mut self) -> &mut Self::Target {
1943 &mut self.$member
1944 }
1945 }
1946 };
1947}
1948
1949pub use crate::app::WidgetId;