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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{Constraint, ConstraintLayout, Widget};
use glib::translate::*;
use glib::IsA;
use std::collections::HashMap;

impl ConstraintLayout {
    /// Creates a list of constraints from a VFL description.
    ///
    /// This function is a convenience wrapper around
    /// [``add_constraints_from_description()``][`Self::add_constraints_from_description()`], using
    /// variadic arguments to populate the view/target map.
    /// ## `lines`
    /// an array of Visual Format Language lines
    ///  defining a set of constraints
    /// ## `hspacing`
    /// default horizontal spacing value, or -1 for the fallback value
    /// ## `vspacing`
    /// default vertical spacing value, or -1 for the fallback value
    /// ## `first_view`
    /// the name of a view in the VFL description, followed by the
    ///  [`ConstraintTarget`][crate::ConstraintTarget] to which it maps
    ///
    /// # Returns
    ///
    /// the list of
    ///  [`Constraint`][crate::Constraint]s that were added to the layout
    #[doc(alias = "gtk_constraint_layout_add_constraints_from_descriptionv")]
    #[doc(alias = "gtk_constraint_layout_add_constraints_from_description")]
    #[doc(alias = "add_constraints_from_descriptionv")]
    pub fn add_constraints_from_description<W: IsA<Widget>>(
        &self,
        lines: &[&str],
        hspacing: i32,
        vspacing: i32,
        views: &HashMap<&str, &W>,
    ) -> Result<Vec<Constraint>, glib::Error> {
        unsafe {
            let mut err = std::ptr::null_mut();
            let hash_table = glib::ffi::g_hash_table_new_full(
                Some(glib::ffi::g_str_hash),
                Some(glib::ffi::g_str_equal),
                Some(glib::ffi::g_free),
                Some(glib::ffi::g_free),
            );

            for (key, widget) in views {
                let key_ptr: *mut libc::c_char = key.to_glib_full();
                glib::ffi::g_hash_table_insert(
                    hash_table,
                    key_ptr as *mut _,
                    widget.to_glib_full() as *mut _,
                );
            }

            let out = ffi::gtk_constraint_layout_add_constraints_from_descriptionv(
                self.to_glib_none().0,
                lines.to_glib_none().0,
                lines.len() as usize,
                hspacing,
                vspacing,
                hash_table,
                &mut err,
            );
            if !err.is_null() {
                Err(from_glib_full(err))
            } else {
                Ok(FromGlibPtrContainer::from_glib_container(out))
            }
        }
    }
}