Skip to main content

gdk/
functions.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::ffi;
4use glib::translate::*;
5
6#[repr(C, packed)]
7pub struct GRange(pub i32, pub i32);
8
9/// Obtains a clip region which contains the areas where the given
10/// ranges of text would be drawn. `x_origin` and `y_origin` are the top left
11/// position of the layout. `index_ranges`
12/// should contain ranges of bytes in the layout’s text. The clip
13/// region will include space to the left or right of the line (to the
14/// layout bounding box) if you have indexes above or below the indexes
15/// contained inside the line. This is to draw the selection all the way
16/// to the side of the layout. However, the clip region is in line coordinates,
17/// not layout coordinates.
18///
19/// Note that the regions returned correspond to logical extents of the text
20/// ranges, not ink extents. So the drawn line may in fact touch areas out of
21/// the clip region. The clip region is mainly useful for highlightling parts
22/// of text, such as when text is selected.
23/// ## `line`
24/// a [`pango::LayoutLine`][crate::pango::LayoutLine]
25/// ## `x_origin`
26/// X pixel where you intend to draw the layout line with this clip
27/// ## `y_origin`
28/// baseline pixel where you intend to draw the layout line with this clip
29/// ## `index_ranges`
30/// array of byte indexes into the layout,
31///  where even members of array are start indexes and odd elements
32///  are end indexes
33/// ## `n_ranges`
34/// number of ranges in `index_ranges`, i.e. half the size of `index_ranges`
35///
36/// # Returns
37///
38/// a clip region containing the given ranges
39#[doc(alias = "gdk_pango_layout_line_get_clip_region")]
40pub fn pango_layout_line_get_clip_region(
41    line: &pango::LayoutLine,
42    x_origin: i32,
43    y_origin: i32,
44    index_ranges: &[GRange],
45) -> Option<cairo::Region> {
46    assert_initialized_main_thread!();
47
48    let ptr: *const i32 = index_ranges.as_ptr() as _;
49    unsafe {
50        from_glib_full(ffi::gdk_pango_layout_line_get_clip_region(
51            line.to_glib_none().0,
52            x_origin,
53            y_origin,
54            mut_override(ptr),
55            index_ranges.len() as i32,
56        ))
57    }
58}
59
60#[doc(alias = "gdk_pango_layout_get_clip_region")]
61pub fn pango_layout_get_clip_region(
62    layout: &pango::Layout,
63    x_origin: i32,
64    y_origin: i32,
65    index_ranges: &[GRange],
66) -> Option<cairo::Region> {
67    assert_initialized_main_thread!();
68
69    let ptr: *const i32 = index_ranges.as_ptr() as _;
70    unsafe {
71        from_glib_full(ffi::gdk_pango_layout_get_clip_region(
72            layout.to_glib_none().0,
73            x_origin,
74            y_origin,
75            ptr,
76            index_ranges.len() as i32,
77        ))
78    }
79}
80
81#[doc(alias = "gdk_setting_get")]
82pub fn setting_get(name: &str) -> Option<glib::Value> {
83    assert_initialized_main_thread!();
84    unsafe {
85        let mut value = glib::Value::uninitialized();
86        let done: bool = from_glib(ffi::gdk_setting_get(
87            name.to_glib_none().0,
88            value.to_glib_none_mut().0,
89        ));
90        if done { Some(value) } else { None }
91    }
92}
93
94#[doc(alias = "gdk_property_change")]
95pub fn property_change(
96    window: &super::Window,
97    property: &super::Atom,
98    type_: &super::Atom,
99    format: i32,
100    mode: super::PropMode,
101    data: super::ChangeData,
102) {
103    skip_assert_initialized!();
104    let nelements = data.len();
105    unsafe {
106        ffi::gdk_property_change(
107            window.to_glib_none().0,
108            property.to_glib_none().0,
109            type_.to_glib_none().0,
110            format,
111            mode.into_glib(),
112            data.to_glib(),
113            nelements as i32,
114        );
115    }
116}