1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2013-2016, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use glib::translate::*;
use gtk_sys;
use libc::{c_int, c_uint};

glib_wrapper! {
    /// The `EntryBuffer` class contains the actual text displayed in a
    /// `Entry` widget.
    ///
    /// A single `EntryBuffer` object can be shared by multiple `Entry`
    /// widgets which will then share the same text content, but not the cursor
    /// position, visibility attributes, icon etc.
    ///
    /// `EntryBuffer` may be derived from. Such a derived class might allow
    /// text to be stored in an alternate location, such as non-pageable memory,
    /// useful in the case of important passwords. Or a derived class could
    /// integrate with an application’s concept of undo/redo.
    ///
    /// # Implements
    ///
    /// [`EntryBufferExt`](trait.EntryBufferExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html)
    pub struct EntryBuffer(Object<gtk_sys::GtkEntryBuffer, gtk_sys::GtkEntryBufferClass, EntryBufferClass>);

    match fn {
        get_type => || gtk_sys::gtk_entry_buffer_get_type(),
    }
}

macro_rules! to_u16 {
    ($e:expr) => (
        {
            let x = $e;
            assert!(x as usize <= u16::max_value() as usize,
                "Unexpected value exceeding `u16` range");
            x as u16
        }
    )
}

#[allow(clippy::cast_lossless)]
impl EntryBuffer {
    pub fn new(initial_chars: Option<&str>) -> EntryBuffer {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(gtk_sys::gtk_entry_buffer_new(
                initial_chars.to_glib_none().0,
                -1,
            ))
        }
    }

    pub fn delete_text(&self, position: u16, n_chars: Option<u16>) -> u16 {
        unsafe {
            to_u16!(gtk_sys::gtk_entry_buffer_delete_text(
                self.to_glib_none().0,
                position as c_uint,
                n_chars.map(|n| n as c_int).unwrap_or(-1)
            ))
        }
    }

    pub fn get_bytes(&self) -> u32 {
        unsafe { gtk_sys::gtk_entry_buffer_get_bytes(self.to_glib_none().0) as u32 }
    }

    pub fn get_length(&self) -> u16 {
        unsafe { to_u16!(gtk_sys::gtk_entry_buffer_get_length(self.to_glib_none().0)) }
    }

    pub fn get_max_length(&self) -> Option<u16> {
        unsafe {
            match gtk_sys::gtk_entry_buffer_get_max_length(self.to_glib_none().0) {
                0 => None,
                x => Some(to_u16!(x)),
            }
        }
    }

    pub fn get_text(&self) -> String {
        unsafe { from_glib_none(gtk_sys::gtk_entry_buffer_get_text(self.to_glib_none().0)) }
    }

    pub fn insert_text(&self, position: u16, chars: &str) -> u16 {
        unsafe {
            to_u16!(gtk_sys::gtk_entry_buffer_insert_text(
                self.to_glib_none().0,
                position as c_uint,
                chars.to_glib_none().0,
                -1
            ))
        }
    }

    pub fn set_max_length(&self, max_length: Option<u16>) {
        unsafe {
            assert_ne!(max_length, Some(0), "Zero maximum length not supported");
            gtk_sys::gtk_entry_buffer_set_max_length(
                self.to_glib_none().0,
                max_length.unwrap_or(0) as c_int,
            );
        }
    }

    pub fn set_text(&self, chars: &str) {
        unsafe {
            gtk_sys::gtk_entry_buffer_set_text(self.to_glib_none().0, chars.to_glib_none().0, -1);
        }
    }
}