gtk4/
entry_buffer.rs
1use glib::{translate::*, GString};
4use libc::{c_int, c_uint};
5
6use crate::{ffi, prelude::*, EntryBuffer};
7
8impl EntryBuffer {
9 #[doc(alias = "gtk_entry_buffer_new")]
21 pub fn new(initial_chars: impl IntoOptionalGStr) -> Self {
22 assert_initialized_main_thread!();
23 unsafe {
24 initial_chars.run_with_gstr(|initial_chars| {
25 from_glib_full(ffi::gtk_entry_buffer_new(
26 initial_chars.to_glib_none().0,
27 -1,
28 ))
29 })
30 }
31 }
32}
33
34macro_rules! to_u16 {
35 ($e:expr) => (
36 {
37 let x = $e;
38 assert!(x as usize <= u16::MAX as usize,
39 "Unexpected value exceeding `u16` range");
40 x as u16
41 }
42 )
43}
44mod sealed {
45 pub trait Sealed {}
46 impl<T: super::IsA<super::EntryBuffer>> Sealed for T {}
47}
48
49pub trait EntryBufferExtManual: sealed::Sealed + IsA<EntryBuffer> + 'static {
53 #[doc(alias = "gtk_entry_buffer_delete_text")]
54 fn delete_text(&self, position: u16, n_chars: Option<u16>) -> u16 {
55 unsafe {
56 to_u16!(ffi::gtk_entry_buffer_delete_text(
57 self.as_ref().to_glib_none().0,
58 position as c_uint,
59 n_chars.map(|n| n as c_int).unwrap_or(-1)
60 ))
61 }
62 }
63
64 #[doc(alias = "gtk_entry_buffer_get_bytes")]
65 #[doc(alias = "get_bytes")]
66 fn bytes(&self) -> usize {
67 unsafe { ffi::gtk_entry_buffer_get_bytes(self.as_ref().to_glib_none().0) as _ }
68 }
69
70 #[doc(alias = "gtk_entry_buffer_get_length")]
71 #[doc(alias = "get_length")]
72 fn length(&self) -> u16 {
73 unsafe {
74 to_u16!(ffi::gtk_entry_buffer_get_length(
75 self.as_ref().to_glib_none().0
76 ))
77 }
78 }
79
80 #[doc(alias = "gtk_entry_buffer_get_max_length")]
81 #[doc(alias = "get_max_length")]
82 fn max_length(&self) -> Option<u16> {
83 unsafe {
84 match ffi::gtk_entry_buffer_get_max_length(self.as_ref().to_glib_none().0) {
85 0 => None,
86 x => Some(to_u16!(x)),
87 }
88 }
89 }
90
91 #[doc(alias = "gtk_entry_buffer_get_text")]
92 #[doc(alias = "get_text")]
93 fn text(&self) -> GString {
94 unsafe {
95 from_glib_none(ffi::gtk_entry_buffer_get_text(
96 self.as_ref().to_glib_none().0,
97 ))
98 }
99 }
100
101 #[doc(alias = "gtk_entry_buffer_insert_text")]
102 fn insert_text(&self, position: u16, chars: impl IntoGStr) -> u16 {
103 unsafe {
104 chars.run_with_gstr(|chars| {
105 to_u16!(ffi::gtk_entry_buffer_insert_text(
106 self.as_ref().to_glib_none().0,
107 position as c_uint,
108 chars.as_ptr(),
109 -1
110 ))
111 })
112 }
113 }
114
115 #[doc(alias = "gtk_entry_buffer_set_max_length")]
116 fn set_max_length(&self, max_length: Option<u16>) {
117 unsafe {
118 assert_ne!(max_length, Some(0), "Zero maximum length not supported");
119 ffi::gtk_entry_buffer_set_max_length(
120 self.as_ref().to_glib_none().0,
121 max_length.unwrap_or(0) as c_int,
122 );
123 }
124 }
125
126 #[doc(alias = "gtk_entry_buffer_set_text")]
127 fn set_text(&self, chars: impl IntoGStr) {
128 unsafe {
129 chars.run_with_gstr(|chars| {
130 ffi::gtk_entry_buffer_set_text(self.as_ref().to_glib_none().0, chars.as_ptr(), -1);
131 })
132 }
133 }
134}
135
136impl<O: IsA<EntryBuffer>> EntryBufferExtManual for O {}
137
138impl Default for EntryBuffer {
139 fn default() -> Self {
140 glib::Object::new()
141 }
142}
143
144impl std::fmt::Write for EntryBuffer {
145 fn write_str(&mut self, s: &str) -> std::fmt::Result {
146 let pos = self.length();
147 self.insert_text(pos, s);
148 Ok(())
149 }
150}