#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum MousePressEvent {
MousePressLeftUp,
MousePressLeftDown,
MousePressRightUp,
MousePressRightDown
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(usize)]
pub enum Event {
Unknown,
OnMousePress(MousePressEvent),
OnMouseMove,
OnMouseWheel,
OnContextMenu,
OnInit,
OnPaint,
OnChar,
OnKeyPress,
OnKeyRelease,
OnResize,
OnResizeBegin,
OnResizeEnd,
OnWindowMaximize,
OnWindowMinimize,
OnMove,
OnVerticalScroll,
OnHorizontalScroll,
OnFileDrop,
OnButtonClick,
OnButtonDoubleClick,
OnLabelClick,
OnLabelDoubleClick,
OnImageFrameClick,
OnImageFrameDoubleClick,
OnTextInput,
OnComboBoxClosed,
OnComboBoxDropdown,
OnComboxBoxSelection,
OnDatePickerDropdown,
OnDatePickerClosed,
OnDatePickerChanged,
OnListBoxDoubleClick,
OnListBoxSelect,
TabsContainerChanged,
TabsContainerChanging,
TrackBarUpdated,
OnMenuOpen,
OnMenuHover,
OnMenuItemSelected,
OnTooltipText,
OnTreeViewClick,
OnTreeViewDoubleClick,
OnTreeViewRightClick,
OnTreeFocusLost,
OnTreeFocus,
OnTreeItemDelete,
OnTreeItemExpanded,
OnTreeItemChanged,
OnTreeItemSelectionChanged,
OnListViewClear,
OnListViewItemRemoved,
OnListViewItemInsert,
OnListViewItemActivated,
OnListViewItemChanged,
OnListViewFocus,
OnListViewFocusLost,
OnTrayNotificationShow,
OnTrayNotificationHide,
OnTrayNotificationTimeout,
OnTrayNotificationUserClose,
OnTimerTick,
OnNotice,
OnWindowClose,
}
#[derive(Debug)]
pub enum EventData {
NoData,
OnWindowClose(WindowCloseData),
OnTooltipText(ToolTipTextData),
OnChar(char),
OnKey(u32),
OnPaint(PaintData),
OnMouseWheel(i32),
OnFileDrop(DropFiles),
#[cfg(feature="tree-view")]
OnTreeItemDelete(crate::TreeItem),
#[cfg(feature="tree-view")]
OnTreeItemUpdate{ item: crate::TreeItem, action: crate::TreeItemAction },
#[cfg(feature="tree-view")]
OnTreeItemSelectionChanged{ old: crate::TreeItem, new: crate::TreeItem },
#[cfg(feature="list-view")]
OnListViewItemIndex { row_index: usize, column_index: usize },
#[cfg(feature="list-view")]
OnListViewItemChanged { row_index: usize, column_index: usize, selected: bool },
}
impl EventData {
pub fn on_paint(&self) -> &PaintData {
match self {
EventData::OnPaint(p) => p,
d => panic!("Wrong data type: {:?}", d)
}
}
pub fn on_char(&self) -> char {
match self {
EventData::OnChar(c) => *c,
d => panic!("Wrong data type: {:?}", d)
}
}
pub fn on_tooltip_text(&self) -> &ToolTipTextData {
match self {
EventData::OnTooltipText(d) => d,
d => panic!("Wrong data type: {:?}", d)
}
}
pub fn on_file_drop(&self) -> &DropFiles {
match self {
EventData::OnFileDrop(d) => d,
d => panic!("Wrong data type: {:?}", d)
}
}
pub fn on_key(&self) -> u32 {
match self {
EventData::OnKey(key) => *key,
d => panic!("Wrong data type: {:?}", d)
}
}
#[cfg(feature="tree-view")]
pub fn on_tree_item_delete(&self) -> &crate::TreeItem {
match self {
EventData::OnTreeItemDelete(item) => item,
d => panic!("Wrong data type: {:?}", d)
}
}
#[cfg(feature="tree-view")]
pub fn on_tree_item_update(&self) -> (&crate::TreeItem, crate::TreeItemAction) {
match self {
EventData::OnTreeItemUpdate { item, action } => (item, *action),
d => panic!("Wrong data type: {:?}", d)
}
}
#[cfg(feature="tree-view")]
pub fn on_tree_item_selection_changed(&self) -> (&crate::TreeItem, &crate::TreeItem) {
match self {
EventData::OnTreeItemSelectionChanged { old, new } => (old, new),
d => panic!("Wrong data type: {:?}", d)
}
}
#[cfg(feature="list-view")]
pub fn on_list_view_item_index(&self) -> (usize, usize) {
match self {
&EventData::OnListViewItemIndex { row_index, column_index } => (row_index, column_index),
d => panic!("Wrong data type: {:?}", d)
}
}
#[cfg(feature="list-view")]
pub fn on_list_view_item_changed(&self) -> (usize, usize, bool) {
match self {
&EventData::OnListViewItemChanged { row_index, column_index, selected} => (row_index, column_index, selected),
d => panic!("Wrong data type: {:?}", d)
}
}
}
use winapi::um::commctrl::NMTTDISPINFOW;
use winapi::um::winuser::{PAINTSTRUCT, BeginPaint, EndPaint};
use winapi::um::shellapi::{HDROP, DragFinish};
use winapi::shared::windef::HWND;
use std::fmt;
pub struct ToolTipTextData {
pub(crate) data: *mut NMTTDISPINFOW
}
impl ToolTipTextData {
pub fn keep(&self, keep: bool) {
use ::winapi::um::commctrl::TTF_DI_SETITEM;
let data = unsafe { &mut *self.data };
match keep {
true => { data.uFlags |= TTF_DI_SETITEM; },
false => { data.uFlags &= !TTF_DI_SETITEM; }
}
}
pub fn set_text<'b>(&self, text: &'b str) {
use crate::win32::base_helper::to_utf16;
use std::ptr;
let text_len = text.len();
if text_len > 79 {
return;
}
self.clear();
unsafe {
let data = &mut *self.data;
let local_text = to_utf16(text);
ptr::copy_nonoverlapping(local_text.as_ptr(), data.szText.as_mut_ptr(), text_len);
}
}
fn clear(&self) {
use winapi::um::winnt::WCHAR;
use std::{ptr, mem};
unsafe {
let data = &mut *self.data;
ptr::write(&mut data.szText as *mut [WCHAR; 80], mem::zeroed());
}
}
}
impl fmt::Debug for ToolTipTextData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ToolTipTextData")
}
}
pub struct WindowCloseData {
pub(crate) data: *mut bool
}
impl WindowCloseData {
pub fn close(&self, value: bool) {
unsafe{ *self.data = value; }
}
pub fn closing(&self) -> bool {
unsafe{ *self.data }
}
}
impl fmt::Debug for WindowCloseData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "WindowCloseData({})", self.closing())
}
}
#[derive(Debug)]
pub struct PaintData {
pub(crate) hwnd: HWND
}
impl PaintData {
pub fn begin_paint(&self) -> PAINTSTRUCT {
unsafe {
let mut paint: PAINTSTRUCT = ::std::mem::zeroed();
BeginPaint(self.hwnd, &mut paint);
paint
}
}
pub fn end_paint(&self, p: &PAINTSTRUCT) {
unsafe {
EndPaint(self.hwnd, p);
}
}
}
pub struct DropFiles {
pub(crate) drop: HDROP,
}
impl DropFiles {
pub fn point(&self) -> [i32; 2] {
use winapi::um::shellapi::DragQueryPoint;
use winapi::shared::windef::POINT;
unsafe {
let mut pt = POINT { x: 0, y: 0 };
DragQueryPoint(self.drop, &mut pt);
[pt.x, pt.y]
}
}
pub fn len(&self) -> usize {
use winapi::um::shellapi::DragQueryFileW;
use std::ptr;
unsafe {
DragQueryFileW(self.drop, 0xFFFFFFFF, ptr::null_mut(), 0) as usize
}
}
pub fn files(&self) -> Vec<String> {
use winapi::um::shellapi::DragQueryFileW;
use crate::win32::base_helper::from_utf16;
use std::ptr;
let len = self.len();
let mut files = Vec::with_capacity(len);
unsafe {
for i in 0..len {
let buffer_size = (DragQueryFileW(self.drop, i as _, ptr::null_mut(), 0) + 1) as usize;
let mut buffer: Vec<u16> = Vec::with_capacity(buffer_size);
buffer.set_len(buffer_size);
DragQueryFileW(self.drop, i as _, buffer.as_mut_ptr(), buffer_size as _);
files.push(from_utf16(&buffer));
}
}
files
}
}
impl fmt::Debug for DropFiles {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DragData {{ point: {:?}, files: {:?} }}", self.point(), self.files())
}
}
impl Drop for DropFiles {
fn drop(&mut self) {
if !self.drop.is_null() {
unsafe { DragFinish(self.drop) }
}
}
}