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
use winapi::um::winuser::{WS_VISIBLE, WS_DISABLED, WS_BORDER, WS_CHILD, WS_CLIPCHILDREN, WS_EX_CONTROLPARENT};
use crate::win32::window_helper as wh;
use crate::win32::base_helper::check_hwnd;
use crate::NwgError;
use super::{ControlBase, ControlHandle};

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


bitflags! {
    /**
        The frame flags

        * NONE:     No flags. Equivalent to a invisible frame without borders.
        * VISIBLE:  The frame is immediatly visible after creation
        * DISABLED: The frame chidlren cannot be interacted with by the user.
        * BORDER:   The frame has a thin black border
    */
    pub struct FrameFlags: u32 {
        const NONE = 0;
        const VISIBLE = WS_VISIBLE;
        const DISABLED = WS_DISABLED;
        const BORDER = WS_BORDER;
    }
}

/**
A frame is a rectangle containing children controls. Frame is implemented as a custom control.

Requires the `frame` feature. 

**Builder parameters:**
  * `parent`:   **Required.** The frame parent container.
  * `size`:     The frame size.
  * `position`: The frame position.
  * `enabled`:  If the frame children can be used by the user.
  * `flags`:    A combination of the FrameFlags values.
  * `OnMouseWheel`: Generic mouse wheel event

**Control events:**
  * `MousePress(_)`: Generic mouse press events on the button
  * `OnMouseMove`: Generic mouse mouse event
*/
#[derive(Default, PartialEq, Eq)]
pub struct Frame {
    pub handle: ControlHandle
}

impl Frame {

    pub fn builder() -> FrameBuilder {
        FrameBuilder {
            size: (100, 25),
            position: (0, 0),
            enabled: true,
            flags: None,
            parent: None,
        }
    }

    /// Returns 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) }
    }

    /// Sets 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); }
    }

    /// Returns 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) }
    }

    /// Returns 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) }
    }

    /// Returns the size of the button 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) }
    }

    /// Sets the size of the button 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) }
    }

    /// Returns the position of the button 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) }
    }

    /// Sets the position of the button 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) }
    }

    /// Winapi class name used during control creation

    pub fn class_name(&self) -> &'static str {
        "NWG_FRAME"
    }

    /// Winapi base flags used during window creation

    pub fn flags(&self) -> u32 {
        WS_VISIBLE | WS_BORDER
    }

    /// Winapi flags required by the control

    pub fn forced_flags(&self) -> u32 {
        WS_CHILD | WS_CLIPCHILDREN
    }

}

impl Drop for Frame {
    fn drop(&mut self) {
        self.handle.destroy();
    }
}
pub struct FrameBuilder {
    size: (i32, i32),
    position: (i32, i32),
    enabled: bool,
    flags: Option<FrameFlags>,
    parent: Option<ControlHandle>
}

impl FrameBuilder {

    pub fn flags(mut self, flags: FrameFlags) -> FrameBuilder {
        self.flags = Some(flags);
        self
    }

    pub fn size(mut self, size: (i32, i32)) -> FrameBuilder {
        self.size = size;
        self
    }

    pub fn position(mut self, pos: (i32, i32)) -> FrameBuilder {
        self.position = pos;
        self
    }

    pub fn enabled(mut self, e: bool) -> FrameBuilder {
        self.enabled = e;
        self
    }

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

    pub fn build(self, out: &mut Frame) -> Result<(), NwgError> {
        let flags = self.flags.map(|f| f.bits()).unwrap_or(out.flags());

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

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

        out.set_enabled(self.enabled);

        Ok(())
    }

}