gtk/
entry_buffer.rs

1// Copyright 2013-2016, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use glib::translate::*;
6use gtk_sys;
7use libc::{c_int, c_uint};
8
9glib_wrapper! {
10    pub struct EntryBuffer(Object<gtk_sys::GtkEntryBuffer, gtk_sys::GtkEntryBufferClass, EntryBufferClass>);
11
12    match fn {
13        get_type => || gtk_sys::gtk_entry_buffer_get_type(),
14    }
15}
16
17macro_rules! to_u16 {
18    ($e:expr) => (
19        {
20            let x = $e;
21            assert!(x as usize <= u16::max_value() as usize,
22                "Unexpected value exceeding `u16` range");
23            x as u16
24        }
25    )
26}
27
28#[cfg_attr(feature = "cargo-clippy", allow(cast_lossless))]
29impl EntryBuffer {
30    pub fn new(initial_chars: Option<&str>) -> EntryBuffer {
31        assert_initialized_main_thread!();
32        unsafe {
33            from_glib_full(gtk_sys::gtk_entry_buffer_new(
34                initial_chars.to_glib_none().0,
35                -1,
36            ))
37        }
38    }
39
40    pub fn delete_text(&self, position: u16, n_chars: Option<u16>) -> u16 {
41        unsafe {
42            to_u16!(gtk_sys::gtk_entry_buffer_delete_text(
43                self.to_glib_none().0,
44                position as c_uint,
45                n_chars.map(|n| n as c_int).unwrap_or(-1)
46            ))
47        }
48    }
49
50    pub fn get_bytes(&self) -> u32 {
51        unsafe { gtk_sys::gtk_entry_buffer_get_bytes(self.to_glib_none().0) as u32 }
52    }
53
54    pub fn get_length(&self) -> u16 {
55        unsafe { to_u16!(gtk_sys::gtk_entry_buffer_get_length(self.to_glib_none().0)) }
56    }
57
58    pub fn get_max_length(&self) -> Option<u16> {
59        unsafe {
60            match gtk_sys::gtk_entry_buffer_get_max_length(self.to_glib_none().0) {
61                0 => None,
62                x => Some(to_u16!(x)),
63            }
64        }
65    }
66
67    pub fn get_text(&self) -> String {
68        unsafe { from_glib_none(gtk_sys::gtk_entry_buffer_get_text(self.to_glib_none().0)) }
69    }
70
71    pub fn insert_text(&self, position: u16, chars: &str) -> u16 {
72        unsafe {
73            to_u16!(gtk_sys::gtk_entry_buffer_insert_text(
74                self.to_glib_none().0,
75                position as c_uint,
76                chars.to_glib_none().0,
77                -1
78            ))
79        }
80    }
81
82    pub fn set_max_length(&self, max_length: Option<u16>) {
83        unsafe {
84            assert_ne!(max_length, Some(0), "Zero maximum length not supported");
85            gtk_sys::gtk_entry_buffer_set_max_length(
86                self.to_glib_none().0,
87                max_length.unwrap_or(0) as c_int,
88            );
89        }
90    }
91
92    pub fn set_text(&self, chars: &str) {
93        unsafe {
94            gtk_sys::gtk_entry_buffer_set_text(self.to_glib_none().0, chars.to_glib_none().0, -1);
95        }
96    }
97}