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
420
421
use winapi::um::winuser::{WS_VISIBLE, WS_DISABLED, BS_AUTOCHECKBOX, BS_AUTO3STATE, BS_PUSHLIKE, WS_TABSTOP};
use crate::win32::{base_helper::check_hwnd, window_helper as wh};
use crate::{Font, NwgError, RawEventHandler};
use super::{ControlBase, ControlHandle};
use std::cell::RefCell;

const NOT_BOUND: &'static str = "CheckBox is not yet bound to a winapi object";
const BAD_HANDLE: &'static str = "INTERNAL ERROR: CheckBox handle is not HWND!";


bitflags! {
    /**
        The CheckBox flags

        * NONE:     No flags. Equivalent to a invisible default checkbox.
        * VISIBLE:  The checkbox is immediatly visible after creation
        * DISABLED: The checkbox cannot be interacted with by the user. It also has a grayed out look.
        * TRISTATE: The checkbox will have a 3rd state
        * PUSHLIKE: The checkbox will look like a regular button
        * TAB_STOP: The control can be selected using tab navigation
    */
    pub struct CheckBoxFlags: u32 {
        const NONE = 0;
        const VISIBLE = WS_VISIBLE;
        const DISABLED = WS_DISABLED;
        const TRISTATE = BS_AUTO3STATE;
        const PUSHLIKE = BS_PUSHLIKE;
        const TAB_STOP = WS_TABSTOP;
    }
}

/// Represents the check status of a checkbox
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum CheckBoxState {
    Checked,
    Unchecked,

    /// New state for the tristate checkbox
    Indeterminate
}

/**
A check box consists of a square box and an application-defined labe that indicates a choice the user can make by selecting the button.
Applications typically display check boxes to enable the user to choose one or more options that are not mutually exclusive.

CheckBox is not behind any features.

**Builder parameters:**
  * `parent`:           **Required.** The checkbox parent container.
  * `text`:             The checkbox text.
  * `size`:             The checkbox size.
  * `position`:         The checkbox position.
  * `enabled`:          If the checkbox can be used by the user. It also has a grayed out look if disabled.
  * `flags`:            A combination of the CheckBoxFlags values.
  * `font`:             The font used for the checkbox text
  * `background_color`: The background color of the checkbox. Defaults to the default window background (light gray)
  * `check_state`:      The default check state
  * `focus`:            The control receive focus after being created

**Control events:**
  * `OnButtonClick`: When the checkbox is clicked once by the user
  * `OnButtonDoubleClick`: When the checkbox is clicked twice rapidly by the user
  * `MousePress(_)`: Generic mouse press events on the checkbox
  * `OnMouseMove`: Generic mouse mouse event
  * `OnMouseWheel`: Generic mouse wheel event


```rust
use native_windows_gui as nwg;
fn build_checkbox(button: &mut nwg::CheckBox, window: &nwg::Window, font: &nwg::Font) {
    nwg::CheckBox::builder()
        .text("Hello")
        .flags(nwg::CheckBoxFlags::VISIBLE)
        .font(Some(font))
        .parent(window)
        .build(button);
}
```
*/
#[derive(Default)]
pub struct CheckBox {
    pub handle: ControlHandle,
    handler0: RefCell<Option<RawEventHandler>>,
}

impl CheckBox {

    pub fn builder<'a>() -> CheckBoxBuilder<'a> {
        CheckBoxBuilder {
            text: "A checkbox",
            size: (100, 25),
            position: (0, 0),
            enabled: true,
            focus: false,
            background_color: None,
            check_state: CheckBoxState::Unchecked,
            flags: None,
            font: None,
            parent: None,
        }
    }

    /// Return `true` if the checkbox can have a third state or `false` otherwise
    pub fn tristate(&self) -> bool {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        let style = wh::get_style(handle);
        style & BS_AUTO3STATE == BS_AUTO3STATE
    }

    /// Sets or unsets the checkbox as tristate
    pub fn set_tristate(&self, tri: bool) {
        use winapi::um::winuser::{BM_SETSTYLE};
        use winapi::shared::minwindef::WPARAM;

        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        
        let style = match tri {
            true => BS_AUTO3STATE,
            false => BS_AUTOCHECKBOX
        };

        wh::send_message(handle, BM_SETSTYLE, style as WPARAM, 1);
    }

    /// Return the check state of the check box
    pub fn check_state(&self) -> CheckBoxState {
        use winapi::um::winuser::{BM_GETCHECK, BST_CHECKED, BST_INDETERMINATE, BST_UNCHECKED};

        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);

        match wh::send_message(handle, BM_GETCHECK, 0, 0) as usize {
            BST_UNCHECKED => CheckBoxState::Unchecked,
            BST_CHECKED => CheckBoxState::Checked,
            BST_INDETERMINATE => CheckBoxState::Indeterminate,
            _ => unreachable!()
        }
    }

    /// Sets the check state of the check box
    pub fn set_check_state(&self, state: CheckBoxState) {
        use winapi::um::winuser::{BM_SETCHECK, BST_CHECKED, BST_INDETERMINATE, BST_UNCHECKED};
        use winapi::shared::minwindef::WPARAM;

        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);

        let x = match state {
            CheckBoxState::Unchecked => BST_UNCHECKED,
            CheckBoxState::Checked => BST_CHECKED,
            CheckBoxState::Indeterminate => BST_INDETERMINATE,
        };

        wh::send_message(handle, BM_SETCHECK, x as WPARAM, 0);
    }

    /// Return the font of the control
    pub fn font(&self) -> Option<Font> {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);

        let font_handle = wh::get_window_font(handle);
        if font_handle.is_null() {
            None
        } else {
            Some(Font { handle: font_handle })
        }
    }

    /// Set the font of the control
    pub fn set_font(&self, font: Option<&Font>) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::set_window_font(handle, font.map(|f| f.handle), true); }
    }

    /// Return true if the control currently has the keyboard focus
    pub fn focus(&self) -> bool {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::get_focus(handle) }
    }

    /// Set the keyboard focus on the button.
    pub fn set_focus(&self) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::set_focus(handle); }
    }

    /// Return true if the control user can interact with the control, return false otherwise
    pub fn enabled(&self) -> bool {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::get_window_enabled(handle) }
    }

    /// Enable or disable the control
    pub fn set_enabled(&self, v: bool) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::set_window_enabled(handle, v) }
    }

    /// Return true if the control is visible to the user. Will return true even if the 
    /// control is outside of the parent client view (ex: at the position (10000, 10000))
    pub fn visible(&self) -> bool {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::get_window_visibility(handle) }
    }

    /// Show or hide the control to the user
    pub fn set_visible(&self, v: bool) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::set_window_visibility(handle, v) }
    }

    /// Return the size of the check box in the parent window
    pub fn size(&self) -> (u32, u32) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::get_window_size(handle) }
    }

    /// Set the size of the check box in the parent window
    pub fn set_size(&self, x: u32, y: u32) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::set_window_size(handle, x, y, false) }
    }

    /// Return the position of the check box in the parent window
    pub fn position(&self) -> (i32, i32) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::get_window_position(handle) }
    }

    /// Set the position of the check box in the parent window
    pub fn set_position(&self, x: i32, y: i32) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::set_window_position(handle, x, y) }
    }

    /// Return the check box label
    pub fn text(&self) -> String { 
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::get_window_text(handle) }
    }

    /// Set the check box label
    pub fn set_text<'a>(&self, v: &'a str) {
        let handle = check_hwnd(&self.handle, NOT_BOUND, BAD_HANDLE);
        unsafe { wh::set_window_text(handle, v) }
    }

    /// Winapi class name used during control creation
    pub fn class_name(&self) -> &'static str {
        "BUTTON"
    }

    /// Winapi base flags used during window creation
    pub fn flags(&self) -> u32 {
        WS_VISIBLE | WS_TABSTOP
    }

    /// Winapi flags required by the control
    pub fn forced_flags(&self) -> u32 {
        use winapi::um::winuser::{BS_NOTIFY, WS_CHILD};

        BS_NOTIFY | WS_CHILD
    }

    /// Change the checkbox background color.
    fn hook_background_color(&self, c: [u8; 3]) {
        use crate::bind_raw_event_handler_inner;
        use winapi::um::winuser::{WM_CTLCOLORSTATIC};
        use winapi::shared::{basetsd::UINT_PTR, windef::{HWND}, minwindef::LRESULT};
        use winapi::um::wingdi::{CreateSolidBrush, RGB};

        if self.handle.blank() { panic!(NOT_BOUND); }
        let handle = self.handle.hwnd().expect(BAD_HANDLE);

        let parent_handle = ControlHandle::Hwnd(wh::get_window_parent(handle));
        let brush = unsafe { CreateSolidBrush(RGB(c[0], c[1], c[2])) };
        
        let handler = bind_raw_event_handler_inner(&parent_handle, handle as UINT_PTR, move |_hwnd, msg, _w, l| {
            match msg {
                WM_CTLCOLORSTATIC => {
                    let child = l as HWND;
                    if child == handle {
                        return Some(brush as LRESULT);
                    }
                },
                _ => {}
            }

            None
        });

        *self.handler0.borrow_mut() = Some(handler.unwrap());
    }

}

impl Drop for CheckBox {
    fn drop(&mut self) {
        use crate::unbind_raw_event_handler;
        
        let handler = self.handler0.borrow();
        if let Some(h) = handler.as_ref() {
            drop(unbind_raw_event_handler(h));
        }

        self.handle.destroy();
    }
}

pub struct CheckBoxBuilder<'a> {
    text: &'a str,
    size: (i32, i32),
    position: (i32, i32),
    enabled: bool,
    focus: bool,
    background_color: Option<[u8; 3]>,
    check_state: CheckBoxState,
    flags: Option<CheckBoxFlags>,
    font: Option<&'a Font>,
    parent: Option<ControlHandle>
}

impl<'a> CheckBoxBuilder<'a> {

    pub fn flags(mut self, flags: CheckBoxFlags) -> CheckBoxBuilder<'a> {
        self.flags = Some(flags);
        self
    }

    pub fn text(mut self, text: &'a str) -> CheckBoxBuilder<'a> {
        self.text = text;
        self
    }

    pub fn size(mut self, size: (i32, i32)) -> CheckBoxBuilder<'a> {
        self.size = size;
        self
    }

    pub fn position(mut self, pos: (i32, i32)) -> CheckBoxBuilder<'a> {
        self.position = pos;
        self
    }

    pub fn enabled(mut self, e: bool) -> CheckBoxBuilder<'a> {
        self.enabled = e;
        self
    }

    pub fn focus(mut self, focus: bool) -> CheckBoxBuilder<'a> {
        self.focus = focus;
        self
    }

    pub fn check_state(mut self, check: CheckBoxState) -> CheckBoxBuilder<'a> {
        self.check_state = check;
        self
    }

    pub fn background_color(mut self, color: Option<[u8;3]>) -> CheckBoxBuilder<'a> {
        self.background_color = color;
        self
    }

    pub fn font(mut self, font: Option<&'a Font>) -> CheckBoxBuilder<'a> {
        self.font = font;
        self
    }

    pub fn parent<C: Into<ControlHandle>>(mut self, p: C) -> CheckBoxBuilder<'a> {
        self.parent = Some(p.into());
        self
    }

    pub fn build(self, out: &mut CheckBox) -> Result<(), NwgError> {
        let mut flags = self.flags.map(|f| f.bits()).unwrap_or(out.flags());
        if flags & BS_AUTO3STATE == 0 {
            flags |= BS_AUTOCHECKBOX;
        }

        let parent = match self.parent {
            Some(p) => Ok(p),
            None => Err(NwgError::no_parent("CheckBox"))
        }?;

        out.handle = ControlBase::build_hwnd()
            .class_name(out.class_name())
            .forced_flags(out.forced_flags())
            .flags(flags)
            .size(self.size)
            .position(self.position)
            .text(self.text)
            .parent(Some(parent))
            .build()?;

        if self.font.is_some() {
            out.set_font(self.font);
        } else {
            out.set_font(Font::global_default().as_ref());
        }

        out.set_enabled(self.enabled);

        if self.background_color.is_some() {
            out.hook_background_color(self.background_color.unwrap());
        }

        if self.focus {
            out.set_focus();
        }

        out.set_check_state(self.check_state);

        Ok(())
    }

}

impl PartialEq for CheckBox {
    fn eq(&self, other: &Self) -> bool {
        self.handle == other.handle
    }
}