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