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
use crate::co;
use crate::decl::*;
use crate::gui::*;
use crate::msg::*;
use crate::prelude::*;
/// A single part of a [`StatusBar`](crate::gui::StatusBar) control.
///
/// **Note:** Each object keeps the zero-based index of a part. If new parts are
/// added/removed from the status bar control, the object may then point to a
/// different item.
///
/// You cannot directly instantiate this object, it is created internally by the
/// control.
#[derive(Clone, Copy)]
pub struct StatusBarPart<'a> {
owner: &'a StatusBar,
index: u32,
}
impl<'a> StatusBarPart<'a> {
#[must_use]
pub(in crate::gui) const fn new(owner: &'a StatusBar, index: u32) -> Self {
Self { owner, index }
}
/// Returns the zero-based index of the part.
#[must_use]
pub const fn index(&self) -> u32 {
self.index
}
/// Sets the icon of a part by sending an
/// [`sb::SetIcon`](crate::msg::sb::SetIcon) message.
///
/// Returns the same part, so further operations can be chained.
pub fn set_icon(&self, hicon: Option<&HICON>) -> SysResult<Self> {
unsafe {
self.owner
.hwnd()
.SendMessage(sb::SetIcon { part_index: self.index as _, hicon })?;
}
Ok(*self)
}
/// Sets the text of a part by sending an
/// [`sb::SetText`](crate::msg::sb::SetText) message.
///
/// Returns the same part, so further operations can be chained.
pub fn set_text(&self, text: &str) -> SysResult<Self> {
unsafe {
self.owner.hwnd().SendMessage(sb::SetText {
part_index: self.index as _,
draw_operation: co::SBT::BORDER,
text: WString::from_str(text),
})?;
}
Ok(*self)
}
/// Retrieves the text of the item by sending a
/// [`sb::GetText`](crate::msg::sb::GetText) message.
///
/// # Examples
///
/// ```no_run
/// use winsafe::{self as w, prelude::*, gui};
///
/// let my_sb: gui::StatusBar; // initialized somewhere
/// # let wnd = gui::WindowMain::new(gui::WindowMainOpts::default());
/// # let my_sb = gui::StatusBar::new(&wnd, &[]);
///
/// println!("Text: {}", my_sb.parts().get(0).text());
/// ```
#[must_use]
pub fn text(&self) -> String {
let (num_chars, _) = unsafe {
self.owner
.hwnd()
.SendMessage(sb::GetTextLength { part_index: self.index as _ })
};
let mut buf = WString::new_alloc_buf(num_chars as usize + 1);
unsafe {
self.owner.hwnd().SendMessage(sb::GetText {
part_index: self.index as _,
text: &mut buf,
});
}
buf.to_string()
}
}