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
//! Assorted types that can contain multiple widgets.
//!
//! All container types can be nested.
//!
//! Use the `children!{}` macro in this crate to convert a heterogeneous list of widgets into a
//! `Vec<BaseWidget>` for the container constructors.

use base::BaseWidget;
use widget_prelude::*;

/// Vertical alignment setting, used by `Horizontal` and `Grid`.
#[derive(Copy, Clone)]
pub enum VAlign {
    Top,
    Center,
    Bottom,
}

impl VAlign {
    fn as_cstr(self) -> &'static str {
        use self::VAlign::*;

        match self {
            Top => cstr!("ATOP"),
            Center => cstr!("ACENTER"),
            Bottom => cstr!("ABOTTOM"),
        }
    }
}

/// Horizontal alignment setting, used by `Vertical` and `Grid`.
#[derive(Copy, Clone)]
pub enum HAlign {
    Left,
    Center,
    Right,
}

impl HAlign {
    fn as_cstr(self) -> &'static str {
        use self::HAlign::*;

        match self {
            Left => cstr!("ALEFT"),
            Center => cstr!("ACENTER"),
            Right => cstr!("ARIGHT"),
        }
    }
}

/// The behavior of this enum depends on its point of use.
#[derive(PartialEq, Eq, Copy, Clone)]
pub enum Orientation {
    Vertical,
    Horizontal,
}

impl Orientation {
    #[doc(hidden)]
    pub fn as_cstr(self) -> &'static str {
        use self::Orientation::*;

        match self {
            Vertical => cstr!("VERTICAL"),
            Horizontal => cstr!("HORIZONTAL"),
        }
    }
}


fn raw_handle_vec<B>(widgets: B) -> Vec<IUPPtr> where B: AsRef<[BaseWidget]> {
    let mut raw_handles: Vec<_> = widgets.as_ref().iter().cloned().map(BaseWidget::ptr).collect();
    raw_handles.push(::std::ptr::null_mut());
    raw_handles
}

/// A builder for `Absolute`, used to create and add children.
pub struct AbsoluteBuilder {
    handles: Vec<IUPPtr>,
}

impl AbsoluteBuilder {
    fn new() -> AbsoluteBuilder {
        AbsoluteBuilder { handles: Vec::new() }
    }

    /// Add a child to the `Absolute` at the given coordinates, relative to the top-left corner of
    /// the container.
    pub fn add_child_at<W: Widget>(&mut self, x: u32, y: u32, child: W) -> &mut Self {
        Absolute::set_child_pos(x, y, child);
        self.handles.push(child.ptr());
        self
    }
}


/// A container type that makes no effort to arrange its children. Instead, they must be positioned
/// manually.
pub struct Absolute(IUPPtr);

impl Absolute {
    /// Create a new absolute container using the given closure, which will be passed a mutable builder
    /// instance.
    ///
    /// The builder is necessary to ensure that the children have their positions set correctly, as
    /// `Widget::set_position` will not set the attributes that this particular container is
    /// expecting.
    pub fn new<F>(build_fn: F) -> Absolute where F: FnOnce(&mut AbsoluteBuilder) {
        let mut builder = AbsoluteBuilder::new();
        build_fn(&mut builder);

        unsafe {
            let ptr = ::iup_sys::IupCboxv(builder.handles.as_mut_ptr());
            Self::from_ptr(ptr)
        }
    }

    /// Set the position of a child of an `Absolute` relative to its top-left corner.
    pub fn set_child_pos<W: Widget>(x: u32, y: u32, child: W) {
        child.set_int_attribute(::attrs::CX, x as i32);
        child.set_int_attribute(::attrs::CY, y as i32);
    }
}

impl_widget! { Absolute, "cbox" }

/// A container widget that lines up its children from left to right.
pub struct Horizontal(IUPPtr);

impl Horizontal {
    /// Create a new horizontal container with the given vector or array of children, which may
    /// also be empty.
    ///
    /// See the `children![]` macro in this crate for more info.
    pub fn new<C>(children: C) -> Horizontal where C: AsRef<[BaseWidget]> {
        let mut raw_handles = raw_handle_vec(children);

        unsafe { 
            let ptr = ::iup_sys::IupHboxv(raw_handles.as_mut_ptr());
            Self::from_ptr(ptr)
        }
    }

    pub fn set_valign(self, valign: VAlign) -> Self {
        self.set_const_str_attribute(::attrs::ALIGNMENT_VERT, valign.as_cstr());
        self
    }

    pub fn set_elem_spacing_pixels(self, spacing: u32) -> Self {
        self.set_str_attribute(::attrs::GAP, spacing.to_string());
        self
    } 
}

impl_widget! { Horizontal, "hbox" }

/// A container widget that lines up its children from top to bottom.
pub struct Vertical(IUPPtr);

impl Vertical {
    pub fn new<C>(children: C) -> Vertical where C: AsRef<[BaseWidget]> {
       let mut raw_handles = raw_handle_vec(children); 

        unsafe {
            let ptr = ::iup_sys::IupVboxv(raw_handles.as_mut_ptr());
            Self::from_ptr(ptr)
        }
    }

    pub fn set_halign(self, halign: HAlign) -> Self {
        self.set_const_str_attribute(::attrs::ALIGNMENT_HORI, halign.as_cstr());
        self
    }

    pub fn set_elem_spacing_pixels(self, spacing: u32) -> Self {
        self.set_str_attribute(::attrs::GAP, spacing.to_string());
        self
    }
}


impl_widget! { Vertical, "vbox" }

/// A container widget that lines up its children from left to right, and from top to bottom.
pub struct Grid(IUPPtr);

impl Grid {
    pub fn new<C>(children: C) -> Grid where C: AsRef<[BaseWidget]> {
       let mut raw_handles = raw_handle_vec(children); 

        unsafe {
            let ptr = ::iup_sys::IupGridBoxv(raw_handles.as_mut_ptr());
            Self::from_ptr(ptr)
        }
    }
    pub fn set_valign(self, valign: VAlign) -> Self {
        self.set_const_str_attribute(::attrs::ALIGNMENT_VERT, valign.as_cstr());
        self
    }

    pub fn set_halign(self, halign: HAlign) -> Self {
        self.set_const_str_attribute(::attrs::ALIGNMENT_HORI, halign.as_cstr());
        self
    }

    /// Based on the orientation, set the number of children to place in a:
    ///
    /// * `Vertial`: **column**
    /// * `Horizontal`: **row**
    ///
    /// before beginning the next one.
    pub fn set_ndiv(self, ndiv: u32) -> Self {
        self.set_int_attribute(::attrs::NUMDIV, ndiv as i32);
        self
    }

    /// Sets how children are distributed in the container.
    ///
    /// * `Vertical`: The container will fill columns first.
    /// 
    /// Visual example (`ndiv=3` grid with 7 children):
    /// <table>
    ///     <tr>
    ///         <td>Child</td>
    ///         <td>Child</td>
    ///         <td>Child</td>
    ///     </tr>
    ///     <tr>
    ///         <td>Child</td>
    ///         <td>Child</td>
    ///     </tr>
    ///     <tr>
    ///         <td>Child</td>
    ///         <td>Child</td>
    ///     </tr>
    /// </table>
    ///
    /// * `Horizontal`: The container will fill rows first. **Default.**
    ///
    /// Visual example (`ndiv=3` grid with 7 children):
    /// <table>
    ///     <tr>
    ///         <td>Child</td>
    ///         <td>Child</td>
    ///         <td>Child</td>
    ///     </tr>
    ///     <tr>
    ///         <td>Child</td>
    ///         <td>Child</td>
    ///         <td>Child</td>
    ///     </tr>
    ///     <tr>
    ///         <td>Child</td>
    ///     </tr>
    /// </table>
    ///
    pub fn set_orientation(&mut self, orientation: Orientation) -> &mut Self {
        self.set_const_str_attribute(::attrs::ORIENTATION, orientation.as_cstr());
        self
    }
}

impl_widget! { Grid, "matrix" }

/// Convert a heterogeneous list of widgets into a `Vec<BaseWidget>`,
/// suitable for passing to any function that takes `AsRef<[BaseWidget]>`, such as a constructor
/// for one of the container types.
#[macro_export]
macro_rules! children [
    // Accepts invocation with or without a final comma.
    ($($child:expr),+,) => (children![$($child),+]);
    ($($child:expr),+) => ({
        use ::kiss_ui::widget::Widget;
        vec![$($child.to_base()),+]
    });
    () => (vec![]);
];