winsafe 0.0.27

Windows API and GUI in safe, idiomatic Rust.
Documentation
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use std::any::TypeId;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::rc::Rc;

use crate::co;
use crate::decl::*;
use crate::gui::*;
use crate::msg::*;
use crate::prelude::*;

/// Exposes item methods of a [`ListView`](crate::gui::ListView) control.
///
/// You cannot directly instantiate this object, it is created internally by the
/// control.
pub struct ListViewItems<'a, T: 'static> {
	owner: &'a ListView<T>,
}

impl<'a, T> ListViewItems<'a, T> {
	#[must_use]
	pub(in crate::gui) const fn new(owner: &'a ListView<T>) -> Self {
		Self { owner }
	}

	/// Appends a new item by sending an
	/// [`lvm::InsertItem`](crate::msg::lvm::InsertItem) message, returning it.
	///
	/// The texts are relative to each column.
	///
	/// # Panics
	///
	/// Panics if `texts` is empty, or if the number of texts is greater than
	/// the number of columns.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, gui};
	///
	/// let my_list: gui::ListView; // initialized somewhere
	/// # let wnd = gui::WindowMain::new(gui::WindowMainOpts::default());
	/// # let my_list = gui::ListView::new(&wnd, gui::ListViewOpts::default());
	///
	/// let new_item = my_list.items().add(
	///     &[
	///         "First column text",
	///         "Second column text",
	///     ],
	///     None, // no icon; requires icons to be added to the image list
	///     (),   // no object data; requires specifying the generic `ListView` type
	/// );
	/// ```
	pub fn add(
		&self,
		texts: &[impl AsRef<str>],
		icon_index: Option<u32>,
		data: T,
	) -> SysResult<ListViewItem<'a, T>> {
		if texts.is_empty() {
			panic!("No texts passed when adding a ListView item.");
		} else if texts.len() > self.owner.cols().count()? as _ {
			panic!(
				"Cannot set {} text(s) to {} column(s).",
				texts.len(),
				self.owner.cols().count()?
			);
		}

		let mut lvi = LVITEM::default();
		lvi.iItem = 0x0fff_ffff; // insert as the last item
		lvi.mask = co::LVIF::TEXT | co::LVIF::IMAGE;

		let mut wtext = WString::from_str(texts[0].as_ref()); // text of 1st column
		lvi.set_pszText(Some(&mut wtext));

		lvi.iImage = match icon_index {
			Some(i) => i as _,
			None => -1,
		};

		if TypeId::of::<T>() != TypeId::of::<()>() {
			// User has defined a generic type, so we store the object right away.
			lvi.mask |= co::LVIF::PARAM;
			let rc_data = Rc::new(RefCell::new(data));
			lvi.lParam = Rc::into_raw(rc_data) as _; // store the Rc pointer
		}

		let new_idx = unsafe {
			self.owner
				.hwnd()
				.SendMessage(lvm::InsertItem { item: &lvi })
		}?;
		let new_item = self.get(new_idx);

		texts
			.iter()
			.enumerate()
			.skip(1) // iterate over subsequent columns
			.try_for_each(|(idx, text)| {
				new_item.set_text(idx as _, text.as_ref())?; // set the text ordinarily
				SysResult::Ok(())
			})?;

		Ok(new_item)
	}

	/// Retrieves the total number of items by sending an
	/// [`lvm::GetItemCount`](crate::msg::lvm::GetItemCount) message.
	#[must_use]
	pub fn count(&self) -> u32 {
		unsafe { self.owner.hwnd().SendMessage(lvm::GetItemCount {}) }
	}

	/// Deletes all items by sending an
	/// [`lvm::DeleteAllItems`](crate::msg::lvm::DeleteAllItems) message.
	pub fn delete_all(&self) -> SysResult<()> {
		unsafe { self.owner.hwnd().SendMessage(lvm::DeleteAllItems {}) }
	}

	/// Deletes all selected items by sending
	/// [`lvm::DeleteItem`](crate::msg::lvm::DeleteItem) messages.
	pub fn delete_selected(&self) -> SysResult<()> {
		loop {
			let next_idx = unsafe {
				self.owner.hwnd().SendMessage(lvm::GetNextItem {
					initial_index: None,
					relationship: co::LVNI::SELECTED,
				})
			};
			match next_idx {
				Some(next_idx) => self.get(next_idx).delete()?,
				None => break,
			}
		}
		Ok(())
	}

	/// Searches for an item with the given text, case-insensitive, by sending
	/// an [`lvm::FindItem`](crate::msg::lvm::FindItem) message.
	#[must_use]
	pub fn find(&self, text: &str) -> Option<ListViewItem<'a, T>> {
		let mut buf = WString::from_str(text);

		let mut lvfi = LVFINDINFO::default();
		lvfi.flags = co::LVFI::STRING;
		lvfi.set_psz(Some(&mut buf));

		unsafe {
			self.owner
				.hwnd()
				.SendMessage(lvm::FindItem { start_index: None, lvfindinfo: &mut lvfi })
		}
		.map(|idx| self.get(idx))
	}

	/// Retrieves the focused item by sending an
	/// [`lvm::GetNextItem`](crate::msg::lvm::GetNextItem) message.
	#[must_use]
	pub fn focused(&self) -> Option<ListViewItem<'a, T>> {
		unsafe {
			self.owner.hwnd().SendMessage(lvm::GetNextItem {
				initial_index: None,
				relationship: co::LVNI::FOCUSED,
			})
		}
		.map(|idx| self.get(idx))
	}

	/// Retrieves the item at the given zero-based position.
	///
	/// **Note:** This method is cheap – even if `index` is beyond the range of
	/// existing items, an object will still be returned. However, operations
	/// upon this object will produce no effect.
	#[must_use]
	pub const fn get(&self, index: u32) -> ListViewItem<'a, T> {
		ListViewItem::new(self.owner, index)
	}

	/// Retrieves the item at the specified position by sending an
	/// [`lvm::HitTest`](crate::msg::lvm::HitTest) message.
	///
	/// `coords` must be relative to the list view.
	#[must_use]
	pub fn hit_test(&self, coords: POINT) -> Option<ListViewItem<'a, T>> {
		let mut lvhti = LVHITTESTINFO::default();
		lvhti.pt = coords;

		unsafe {
			self.owner
				.hwnd()
				.SendMessage(lvm::HitTest { info: &mut lvhti })
		}
		.map(|index| self.get(index))
	}

	/// Returns an iterator over all items.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, gui};
	///
	/// let my_list: gui::ListView; // initialized somewhere
	/// # let wnd = gui::WindowMain::new(gui::WindowMainOpts::default());
	/// # let my_list = gui::ListView::<()>::new(&wnd, gui::ListViewOpts::default());
	///
	/// for item in my_list.items().iter() {
	///     println!("Item {}, text of the first column: {}",
	///         item.index(), item.text(0));
	/// }
	///
	/// let vec_items = my_list.items()
	///     .iter()
	///     .collect::<Vec<_>>();
	/// ```
	#[must_use]
	pub fn iter(&self) -> impl DoubleEndedIterator<Item = ListViewItem<'a, T>> + 'a {
		ListViewItemIter::new(self.owner, false)
	}

	/// Returns an iterator over the selected items.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, gui};
	///
	/// let my_list: gui::ListView; // initialized somewhere
	/// # let wnd = gui::WindowMain::new(gui::WindowMainOpts::default());
	/// # let my_list = gui::ListView::<()>::new(&wnd, gui::ListViewOpts::default());
	///
	/// for item in my_list.items().iter_selected() {
	///     println!("Selected item {}, text of the first column: {}",
	///         item.index(), item.text(0));
	/// }
	///
	/// let vec_items = my_list.items()
	///     .iter_selected()
	///     .collect::<Vec<_>>();
	/// ```
	#[must_use]
	pub fn iter_selected(&self) -> impl DoubleEndedIterator<Item = ListViewItem<'a, T>> + 'a {
		ListViewItemIter::new(self.owner, true)
	}

	/// Retrieves the item of the unique ID by sending an
	/// [`lvm::MapIdToIndex`](crate::msg::lvm::MapIdToIndex) message.
	///
	/// If the item of the given unique ID doesn't exist anymore, returns
	/// `None`.
	#[must_use]
	pub fn get_by_uid(&self, uid: u32) -> Option<ListViewItem<'a, T>> {
		unsafe { self.owner.hwnd().SendMessage(lvm::MapIdToIndex { id: uid }) }
			.map(|idx| self.get(idx))
	}

	/// Returns the last item, if any.
	pub fn last(&self) -> Option<ListViewItem<'a, T>> {
		let count = self.count();
		if count > 0 { Some(self.get(count - 1)) } else { None }
	}

	/// Returns the last selected item, if any.
	pub fn last_selected(&self) -> Option<ListViewItem<'a, T>> {
		let count = self.selected_count();
		if count > 0 { Some(self.get(count - 1)) } else { None }
	}

	/// Sets or remove the selection for all items by sending an
	/// [`lvm::SetItemState`](crate::msg::lvm::SetItemState) message.
	pub fn select_all(&self, set: bool) -> SysResult<()> {
		let styles: co::LVS = self.owner.hwnd().style().into();
		if styles.has(co::LVS::SINGLESEL) {
			return Ok(()); // LVM_SETITEMSTATE fails for all items in single-sel list views
		}

		let mut lvi = LVITEM::default();
		lvi.stateMask = co::LVIS::SELECTED;
		if set {
			lvi.state = co::LVIS::SELECTED;
		}

		unsafe {
			self.owner
				.hwnd()
				.SendMessage(lvm::SetItemState { index: None, lvitem: &lvi })
		}
	}

	/// Retrieves the number of selected items by sending an
	/// [`lvm::GetSelectedCount`](crate::msg::lvm::GetSelectedCount) message.
	#[must_use]
	pub fn selected_count(&self) -> u32 {
		unsafe { self.owner.hwnd().SendMessage(lvm::GetSelectedCount {}) }
	}

	/// Sets the number of items in a virtual list view – that is, a list view
	/// created with [`LVS::OWNERDATA`](crate::co::LVS::OWNERDATA) style – by
	/// sending an [`lvm::SetItemCount`](crate::msg::lvm::SetItemCount) message.
	pub fn set_count(&self, count: u32, behavior: Option<co::LVSICF>) -> SysResult<()> {
		unsafe {
			self.owner
				.hwnd()
				.SendMessage(lvm::SetItemCount { count, behavior })
		}
	}

	/// Sorts the items according to a callback by sending an
	/// [`lvm::SortItemsEx`](crate::msg::lvm::SortItemsEx) message.
	///
	/// The callback receives the two items to be compared.
	///
	/// # Examples
	///
	/// Sorting by the text of the first column:
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, gui};
	///
	/// let my_list: gui::ListView; // initialized somewhere
	/// # let wnd = gui::WindowMain::new(gui::WindowMainOpts::default());
	/// # let my_list = gui::ListView::<()>::new(&wnd, gui::ListViewOpts::default());
	///
	/// my_list.items().sort(|itemA, itemB| -> std::cmp::Ordering {
	///     itemA.text(0).cmp( &itemB.text(0) )
	/// });
	/// ```
	pub fn sort<F>(&self, func: F) -> SysResult<()>
	where
		F: FnMut(ListViewItem, ListViewItem) -> Ordering,
	{
		let mut func = func;
		let data = (self.owner, &mut func);

		unsafe {
			self.owner.hwnd().SendMessage(lvm::SortItemsEx {
				param: &data as *const _ as _,
				callback: Self::list_view_item_sort::<F>,
			})
		}
	}

	pub(in crate::gui) extern "system" fn list_view_item_sort<F>(
		lparam1: isize,
		lparam2: isize,
		lparam_sort: isize,
	) -> i32
	where
		F: FnMut(ListViewItem, ListViewItem) -> Ordering,
	{
		let data = unsafe { &mut *(lparam_sort as *mut (&ListView, &mut F)) };
		let item1 = data.0.items().get(lparam1 as _);
		let item2 = data.0.items().get(lparam2 as _);
		data.1(item1, item2) as _
	}
}

struct ListViewItemIter<'a, T: 'static> {
	owner: &'a ListView<T>,
	front_idx: u32,
	past_back_idx: u32,
	is_sel: bool,
}

impl<'a, T> Iterator for ListViewItemIter<'a, T> {
	type Item = ListViewItem<'a, T>;

	fn next(&mut self) -> Option<Self::Item> {
		self.grab(true)
	}
}
impl<'a, T> DoubleEndedIterator for ListViewItemIter<'a, T> {
	fn next_back(&mut self) -> Option<Self::Item> {
		self.grab(false)
	}
}

impl<'a, T> ListViewItemIter<'a, T> {
	#[must_use]
	fn new(owner: &'a ListView<T>, is_sel: bool) -> Self {
		Self {
			owner,
			front_idx: 0,
			past_back_idx: owner.items().count(),
			is_sel,
		}
	}

	fn grab(&mut self, is_front: bool) -> Option<ListViewItem<'a, T>> {
		if self.front_idx == self.past_back_idx {
			return None;
		}

		let mut our_idx = if is_front { self.front_idx } else { self.past_back_idx - 1 };
		let mut item = self.owner.items().get(our_idx);

		// LVNI_SELECTED|LVNI_PREVIOUS flags don't seem to work together, so we check each item manually.
		while self.is_sel && !item.is_selected() {
			if is_front {
				self.front_idx += 1;
			} else {
				self.past_back_idx -= 1;
			}
			if self.front_idx == self.past_back_idx {
				return None;
			}
			our_idx = if is_front { self.front_idx } else { self.past_back_idx - 1 };
			item = self.owner.items().get(our_idx);
		}

		if is_front {
			self.front_idx += 1;
		} else {
			self.past_back_idx -= 1;
		}
		Some(item)
	}
}