1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
use crate::{ error, error_ref_mut, nctree_create, widgets::{NcTree, NcTreeItem, NcTreeOptions}, NcError, NcInput, NcPlane, NcResult, NCRESULT_ERR, }; /// # `NcTree` constructors & destructors impl NcTree { /// Creates an [NcTree] with the specified options. /// /// *C style function: [nctree_create()][crate::nctree_create].* pub fn new<'a>(plane: &mut NcPlane, options: NcTreeOptions) -> NcResult<&'a mut Self> { error_ref_mut![unsafe { nctree_create(plane, &options) }, "Creating NcTree"] } /// Destroys an NcTree created with [new()][NcTree#method.new]. /// /// *C style function: [nctree_destroy()][crate::nctree_destroy].* pub fn destroy(&mut self) { unsafe { crate::nctree_destroy(self) }; } } /// # `NcTree` methods impl NcTree { // NOTE: not implemented yet in C API // // /// Goes to the item specified by the array |spec|, terminated by UINT_MAX. // /// // /// If the spec is invalid, returns an error and the depth of the first // /// invalid spec is written to *|failspec|. // /// // /// Otherwise, the true depth is written to *|failspec|, // /// and the curry is returned (|failspec| is necessary because the // /// curry could itself be NULL). // /// // /// *C style function: [nctree_goto()][crate::nctree_goto].* // pub fn goto(&mut self, spec: ... , failspec: ...) -> NcResult<&mut NcTreeItem> { // let res = unsafe { crate::nctree_goto(self) }; // if !res.is_null() { // Ok(unsafe { &mut *(res as *mut NcTreeItem) }) // } else { // Err(NcError::with_msg(NCRESULT_ERR, "NcTree.goto()")) // } // } /// Returns the focused item, if any items are present. /// /// This is not a copy; be careful to use it only for the duration of a /// critical section. /// /// *C style function: [nctree_focused()][crate::nctree_focused].* pub fn focused(&mut self) -> NcResult<&mut NcTreeItem> { let res = unsafe { crate::nctree_focused(self) }; if !res.is_null() { Ok(unsafe { &mut *(res as *mut NcTreeItem) }) } else { Err(NcError::with_msg(NCRESULT_ERR, "NcTree.focused()")) } } /// Changes the focus to the next item, and returns it. /// /// *C style function: [nctree_next()][crate::nctree_next].* #[allow(clippy::should_implement_trait)] pub fn next(&mut self) -> NcResult<&mut NcTreeItem> { let res = unsafe { crate::nctree_next(self) }; if !res.is_null() { Ok(unsafe { &mut *(res as *mut NcTreeItem) }) } else { Err(NcError::with_msg(NCRESULT_ERR, "NcTree.next()")) } } /// Changes the focus to the previous item, and returns it. /// /// *C style function: [nctree_prev()][crate::nctree_prev].* pub fn prev(&mut self) -> NcResult<&mut NcTreeItem> { let res = unsafe { crate::nctree_prev(self) }; if !res.is_null() { Ok(unsafe { &mut *(res as *mut NcTreeItem) }) } else { Err(NcError::with_msg(NCRESULT_ERR, "NcTree.prev()")) } } /// Offers the `input` to this NcTree. /// /// If it's relevant, this function returns true, /// and the input ought not be processed further. /// If it's irrelevant to the tree, false is returned. /// /// Relevant inputs include: /// /// - a mouse click on an item (focuses item) /// - a mouse scrollwheel event (srolls tree) /// - up, down, pgup, or pgdown (navigates among items) /// /// *C style function: [nctree_offer_input()][crate::nctree_offer_input].* pub fn offer_input(&mut self, input: NcInput) -> bool { unsafe { crate::nctree_offer_input(self, &input) } } /// Returns the [NcPlane] backing this NcTree. /// /// *C style function: [nctree_plane()][crate::nctree_plane].* pub fn plane(&mut self) -> NcResult<&NcPlane> { error_ref_mut![unsafe { crate::nctree_plane(self) }, "NcTree.plane()"] } /// Redraws the NcTree in its entirety. /// /// The tree will be cleared, and items will be laid out, using the focused /// item as a fulcrum. /// /// Item-drawing callbacks will be invoked for each visible item. /// /// *C style function: [nctree_redraw()][crate::nctree_redraw].* pub fn redraw(&mut self) -> NcResult<()> { error![unsafe { crate::nctree_redraw(self) }, "NcTree.redraw()"] } }