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
//!
//! Safe wrapper for wxSimpleBook.
use crate::event::{Event, EventType, WxEvtHandler};
use crate::geometry::{Point, Size};
use crate::id::Id;
use crate::window::{Window, WindowHandle, WxWidget};
use std::ffi::CString;
use std::os::raw::c_int;
use wxdragon_sys as ffi;
// --- Style enum using macro ---
widget_style_enum!(
name: SimpleBookStyle,
doc: "Window style flags for SimpleBook",
variants: {
Default: ffi::WXD_BK_DEFAULT, "Default style.",
Top: ffi::WXD_BK_TOP, "Place pages at the top (no visual effect for SimpleBook).",
Bottom: ffi::WXD_BK_BOTTOM, "Place pages at the bottom (no visual effect for SimpleBook).",
Left: ffi::WXD_BK_LEFT, "Place pages on the left (no visual effect for SimpleBook).",
Right: ffi::WXD_BK_RIGHT, "Place pages on the right (no visual effect for SimpleBook)."
},
default_variant: Default
);
/// Represents a wxSimpleBook widget.
///
/// wxSimpleBook is a book control without visual tabs. Pages are switched programmatically,
/// not through visible tabs. This makes it ideal for wizard-like interfaces or when you
/// want to control page navigation through other UI elements.
///
/// SimpleBook uses `WindowHandle` internally for safe memory management.
/// When the underlying window is destroyed (by calling `destroy()` or when
/// its parent is destroyed), the handle becomes invalid and all operations
/// become safe no-ops.
#[derive(Clone, Copy)]
pub struct SimpleBook {
/// Safe handle to the underlying wxSimpleBook - automatically invalidated on destroy
handle: WindowHandle,
}
impl SimpleBook {
/// Creates a new SimpleBook builder.
pub fn builder(parent: &dyn WxWidget) -> SimpleBookBuilder<'_> {
SimpleBookBuilder::new(parent)
}
// Internal constructor
pub(crate) unsafe fn from_ptr(ptr: *mut ffi::wxd_SimpleBook_t) -> Self {
SimpleBook {
handle: WindowHandle::new(ptr as *mut ffi::wxd_Window_t),
}
}
/// Helper to get raw simplebook pointer, returns null if widget has been destroyed
#[inline]
fn simplebook_ptr(&self) -> *mut ffi::wxd_SimpleBook_t {
self.handle
.get_ptr()
.map(|p| p as *mut ffi::wxd_SimpleBook_t)
.unwrap_or(std::ptr::null_mut())
}
/// Adds a new page to the simplebook.
///
/// # Arguments
/// * `page` - The window to be added as a page.
/// * `text` - The text for the page (stored internally but not visually displayed).
/// * `select` - If `true`, selects the page after adding it.
/// * `image_id` - Optional image index (ignored for SimpleBook as it has no tabs).
///
/// Returns `true` if the page was added successfully.
/// Returns `false` if the simplebook has been destroyed.
pub fn add_page<W: WxWidget>(&self, page: &W, text: &str, select: bool, image_id: Option<i32>) -> bool {
let ptr = self.simplebook_ptr();
if ptr.is_null() {
return false;
}
let c_text = CString::new(text).expect("CString::new failed");
unsafe {
if let Some(id) = image_id {
ffi::wxd_SimpleBook_AddPageWithImageId(ptr, page.handle_ptr(), c_text.as_ptr(), select, id as c_int)
} else {
ffi::wxd_SimpleBook_AddPage(ptr, page.handle_ptr(), c_text.as_ptr(), select)
}
}
}
/// Inserts a new page at the specified position.
///
/// # Arguments
/// * `index` - The position for the new page.
/// * `page` - The window to be added as a page.
/// * `text` - The text for the page (stored internally but not visually displayed).
/// * `select` - If `true`, selects the page after adding it.
/// * `image_id` - Optional image index (ignored for SimpleBook as it has no tabs).
///
/// Returns `true` if the page was inserted successfully.
/// Returns `false` if the simplebook has been destroyed.
pub fn insert_page<W: WxWidget>(&self, index: usize, page: &W, text: &str, select: bool, image_id: Option<i32>) -> bool {
let ptr = self.simplebook_ptr();
if ptr.is_null() {
return false;
}
let c_text = CString::new(text).expect("CString::new failed");
unsafe {
if let Some(id) = image_id {
ffi::wxd_SimpleBook_InsertPageWithImageId(ptr, index, page.handle_ptr(), c_text.as_ptr(), select, id as c_int)
} else {
ffi::wxd_SimpleBook_InsertPage(ptr, index, page.handle_ptr(), c_text.as_ptr(), select)
}
}
}
/// Gets the index of the currently selected page.
/// Returns `wxNOT_FOUND` (-1) if no page is selected or if the simplebook has been destroyed.
pub fn selection(&self) -> i32 {
let ptr = self.simplebook_ptr();
if ptr.is_null() {
return -1;
}
unsafe { ffi::wxd_SimpleBook_GetSelection(ptr) }
}
/// Sets the selection to the given page index.
/// Returns the index of the previously selected page.
/// Returns -1 if the simplebook has been destroyed.
pub fn set_selection(&self, page: usize) -> i32 {
let ptr = self.simplebook_ptr();
if ptr.is_null() {
return -1;
}
unsafe { ffi::wxd_SimpleBook_SetSelection(ptr, page as c_int) }
}
/// Changes the selection to the given page, returning the old selection.
/// This function does not generate a `EVT_BOOKCTRL_PAGE_CHANGING` event.
/// Returns -1 if the simplebook has been destroyed.
pub fn change_selection(&self, page: usize) -> i32 {
let ptr = self.simplebook_ptr();
if ptr.is_null() {
return -1;
}
unsafe { ffi::wxd_SimpleBook_ChangeSelection(ptr, page) }
}
/// Gets the number of pages in the simplebook.
/// Returns 0 if the simplebook has been destroyed.
pub fn get_page_count(&self) -> usize {
let ptr = self.simplebook_ptr();
if ptr.is_null() {
return 0;
}
unsafe { ffi::wxd_SimpleBook_GetPageCount(ptr) }
}
/// Returns the window at the given page position.
/// Returns `None` if the page index is out of bounds or if the simplebook has been destroyed.
pub fn get_page(&self, index: usize) -> Option<Window> {
let ptr = self.simplebook_ptr();
if ptr.is_null() {
return None;
}
unsafe {
let page_ptr = ffi::wxd_SimpleBook_GetPage(ptr, index);
if page_ptr.is_null() {
None
} else {
Some(Window::from_ptr(page_ptr))
}
}
}
/// Removes the page at the given index.
/// Returns `true` if the page was removed successfully.
/// Returns `false` if the simplebook has been destroyed.
pub fn remove_page(&self, index: usize) -> bool {
let ptr = self.simplebook_ptr();
if ptr.is_null() {
return false;
}
unsafe { ffi::wxd_SimpleBook_RemovePage(ptr, index) }
}
/// Returns the underlying WindowHandle for this simplebook.
pub fn window_handle(&self) -> WindowHandle {
self.handle
}
}
// Manual WxWidget implementation for SimpleBook (using WindowHandle)
impl WxWidget for SimpleBook {
fn handle_ptr(&self) -> *mut ffi::wxd_Window_t {
self.handle.get_ptr().unwrap_or(std::ptr::null_mut())
}
fn is_valid(&self) -> bool {
self.handle.is_valid()
}
}
// Implement WxEvtHandler for event binding
impl WxEvtHandler for SimpleBook {
unsafe fn get_event_handler_ptr(&self) -> *mut ffi::wxd_EvtHandler_t {
self.handle.get_ptr().unwrap_or(std::ptr::null_mut()) as *mut ffi::wxd_EvtHandler_t
}
}
// Implement common event traits that all Window-based widgets support
impl crate::event::WindowEvents for SimpleBook {}
// Use the widget_builder macro to generate the SimpleBookBuilder implementation
widget_builder!(
name: SimpleBook,
parent_type: &'a dyn WxWidget,
style_type: SimpleBookStyle,
fields: {},
build_impl: |slf| {
let simplebook_ptr = unsafe {
ffi::wxd_SimpleBook_Create(
slf.parent.handle_ptr(),
slf.id as c_int,
slf.pos.into(),
slf.size.into(),
slf.style.bits() as ffi::wxd_Style_t,
)
};
if simplebook_ptr.is_null() {
panic!("Failed to create SimpleBook");
}
unsafe { SimpleBook::from_ptr(simplebook_ptr) }
}
);
/// Events that can be emitted by a `SimpleBook`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SimpleBookEvent {
/// A simplebook page has been changed.
PageChanged,
}
/// Event data for a `SimpleBook::PageChanged` event.
#[derive(Debug)]
pub struct SimpleBookPageChangedEvent {
/// The base event data.
pub base: Event,
}
impl SimpleBookPageChangedEvent {
/// Creates new `SimpleBookPageChangedEvent` from a base `Event`.
pub fn new(base_event: Event) -> Self {
Self { base: base_event }
}
/// Gets the page that has been selected.
/// For a `PageChanged` event, this is the new page.
pub fn get_selection(&self) -> Option<i32> {
if self.base.is_null() {
return None;
}
// SimpleBook uses the same event infrastructure as Notebook
let val = unsafe { ffi::wxd_NotebookEvent_GetSelection(self.base.0) };
if val == ffi::WXD_NOT_FOUND as i32 { None } else { Some(val) }
}
/// Gets the page that was selected before the change.
/// For a `PageChanged` event, this is the old page.
pub fn get_old_selection(&self) -> Option<i32> {
if self.base.is_null() {
return None;
}
// SimpleBook uses the same event infrastructure as Notebook
let val = unsafe { ffi::wxd_NotebookEvent_GetOldSelection(self.base.0) };
if val == ffi::WXD_NOT_FOUND as i32 { None } else { Some(val) }
}
}
// Use the implement_widget_local_event_handlers macro for simplebook events
crate::implement_widget_local_event_handlers!(
SimpleBook, SimpleBookEvent, SimpleBookPageChangedEvent,
PageChanged => page_changed, EventType::NOTEBOOK_PAGE_CHANGED
);
// XRC Support - enables SimpleBook to be created from XRC-managed pointers
#[cfg(feature = "xrc")]
impl crate::xrc::XrcSupport for SimpleBook {
unsafe fn from_xrc_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
SimpleBook {
handle: WindowHandle::new(ptr),
}
}
}
// Enable widget casting for SimpleBook
impl crate::window::FromWindowWithClassName for SimpleBook {
fn class_name() -> &'static str {
"wxSimplebook"
}
unsafe fn from_ptr(ptr: *mut ffi::wxd_Window_t) -> Self {
SimpleBook {
handle: WindowHandle::new(ptr),
}
}
}