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