1use glib::translate::*;
4use libc::{c_int, c_uint};
5
6glib::wrapper! {
7 #[doc(alias = "GtkEntryBuffer")]
57 pub struct EntryBuffer(Object<ffi::GtkEntryBuffer, ffi::GtkEntryBufferClass>);
58
59 match fn {
60 type_ => || ffi::gtk_entry_buffer_get_type(),
61 }
62}
63
64macro_rules! to_u16 {
65 ($e:expr) => (
66 {
67 let x = $e;
68 assert!(x as usize <= u16::max_value() as usize,
69 "Unexpected value exceeding `u16` range");
70 x as u16
71 }
72 )
73}
74
75#[allow(clippy::cast_lossless)]
76impl EntryBuffer {
77 #[doc(alias = "gtk_entry_buffer_new")]
78 pub fn new(initial_chars: Option<&str>) -> EntryBuffer {
79 assert_initialized_main_thread!();
80 unsafe {
81 from_glib_full(ffi::gtk_entry_buffer_new(
82 initial_chars.to_glib_none().0,
83 -1,
84 ))
85 }
86 }
87
88 #[doc(alias = "gtk_entry_buffer_delete_text")]
89 pub fn delete_text(&self, position: u16, n_chars: Option<u16>) -> u16 {
90 unsafe {
91 to_u16!(ffi::gtk_entry_buffer_delete_text(
92 self.to_glib_none().0,
93 position as c_uint,
94 n_chars.map(|n| n as c_int).unwrap_or(-1)
95 ))
96 }
97 }
98
99 #[doc(alias = "gtk_entry_buffer_get_bytes")]
100 #[doc(alias = "get_bytes")]
101 pub fn bytes(&self) -> u32 {
102 unsafe { ffi::gtk_entry_buffer_get_bytes(self.to_glib_none().0) as u32 }
103 }
104
105 #[doc(alias = "gtk_entry_buffer_get_length")]
106 #[doc(alias = "get_length")]
107 pub fn length(&self) -> u16 {
108 unsafe { to_u16!(ffi::gtk_entry_buffer_get_length(self.to_glib_none().0)) }
109 }
110
111 #[doc(alias = "gtk_entry_buffer_get_max_length")]
112 #[doc(alias = "get_max_length")]
113 pub fn max_length(&self) -> Option<u16> {
114 unsafe {
115 match ffi::gtk_entry_buffer_get_max_length(self.to_glib_none().0) {
116 0 => None,
117 x => Some(to_u16!(x)),
118 }
119 }
120 }
121
122 #[doc(alias = "gtk_entry_buffer_get_text")]
123 #[doc(alias = "get_text")]
124 pub fn text(&self) -> String {
125 unsafe { from_glib_none(ffi::gtk_entry_buffer_get_text(self.to_glib_none().0)) }
126 }
127
128 #[doc(alias = "gtk_entry_buffer_insert_text")]
129 pub fn insert_text(&self, position: u16, chars: &str) -> u16 {
130 unsafe {
131 to_u16!(ffi::gtk_entry_buffer_insert_text(
132 self.to_glib_none().0,
133 position as c_uint,
134 chars.to_glib_none().0,
135 -1
136 ))
137 }
138 }
139
140 #[doc(alias = "gtk_entry_buffer_set_max_length")]
141 pub fn set_max_length(&self, max_length: Option<u16>) {
142 unsafe {
143 assert_ne!(max_length, Some(0), "Zero maximum length not supported");
144 ffi::gtk_entry_buffer_set_max_length(
145 self.to_glib_none().0,
146 max_length.unwrap_or(0) as c_int,
147 );
148 }
149 }
150
151 #[doc(alias = "gtk_entry_buffer_set_text")]
152 pub fn set_text(&self, chars: &str) {
153 unsafe {
154 ffi::gtk_entry_buffer_set_text(self.to_glib_none().0, chars.to_glib_none().0, -1);
155 }
156 }
157}