-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cde126
commit adafca2
Showing
1 changed file
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//! # Custom GdkPaintable | ||
//! | ||
//! This example shows how to create a custom gdk::Paintable | ||
//! that renders a square with a linear color gradient | ||
|
||
use gtk::prelude::*; | ||
use gtk::subclass::prelude::*; | ||
use gtk::{gdk, glib, graphene, gsk}; | ||
use std::env::args; | ||
|
||
mod imp { | ||
use super::*; | ||
|
||
pub struct CustomEditable { | ||
pub text: gtk::Text, | ||
} | ||
|
||
impl Default for CustomEditable { | ||
fn default() -> Self { | ||
Self { | ||
text: gtk::Text::new(), | ||
} | ||
} | ||
} | ||
|
||
#[glib::object_subclass] | ||
impl ObjectSubclass for CustomEditable { | ||
const NAME: &'static str = "CustomEditable"; | ||
type Type = super::CustomEditable; | ||
type ParentType = gtk::Widget; | ||
type Interfaces = (gtk::Editable,); | ||
|
||
fn class_init(klass: &mut Self::Class) { | ||
let total_props = klass.install_properties(); | ||
println!("total props: {}", total_props); | ||
klass.set_layout_manager_type::<gtk::BoxLayout>(); | ||
klass.set_css_name("entry"); | ||
klass.set_accessible_role(gtk::AccessibleRole::TextBox); | ||
} | ||
} | ||
|
||
impl ObjectImpl for CustomEditable { | ||
fn set_property( | ||
&self, | ||
editable: &Self::Type, | ||
id: usize, | ||
value: &glib::Value, | ||
pspec: &glib::ParamSpec, | ||
) { | ||
println!("Setter: {} - ID: {:#?}", pspec.get_name(), id); | ||
if EditableImpl::properties(self).contains(&pspec.get_name().to_string()) { | ||
EditableImpl::set_property(self, editable, id, value, pspec); | ||
} else { | ||
unimplemented!(); | ||
} | ||
} | ||
|
||
fn get_property( | ||
&self, | ||
editable: &Self::Type, | ||
id: usize, | ||
pspec: &glib::ParamSpec, | ||
) -> glib::Value { | ||
println!("Getter: {} - ID: {:#?}",pspec.get_name(), id); | ||
if EditableImpl::properties(self).contains(&pspec.get_name().to_string()) { | ||
EditableImpl::get_property(self, editable, id, pspec).unwrap() | ||
} else { | ||
unimplemented!(); | ||
} | ||
} | ||
|
||
fn constructed(&self, editable: &Self::Type) { | ||
editable.init_delegate(); | ||
self.text.set_hexpand(true); | ||
self.text.set_vexpand(true); | ||
|
||
self.text.set_parent(editable); | ||
self.text.set_max_width_chars(6); | ||
self.text.set_width_chars(6); | ||
editable.add_css_class("tagged"); | ||
editable.set_enable_undo(false); | ||
} | ||
|
||
fn dispose(&self, editable: &Self::Type) { | ||
editable.finish_delegate(); | ||
self.text.unparent(); | ||
} | ||
} | ||
impl WidgetImpl for CustomEditable {} | ||
impl EditableImpl for CustomEditable { | ||
fn get_delegate(&self, editable: &Self::Type) -> Option<gtk::Editable> { | ||
Some(self.text.clone().upcast()) | ||
} | ||
} | ||
} | ||
|
||
glib::wrapper! { | ||
pub struct CustomEditable(ObjectSubclass<imp::CustomEditable>) @extends gtk::Widget, @implements gtk::Editable; | ||
} | ||
|
||
impl CustomEditable { | ||
pub fn new() -> Self { | ||
glib::Object::new(&[]).expect("Failed to create a CustomEditable") | ||
} | ||
} | ||
|
||
fn build_ui(application: >k::Application) { | ||
let window = gtk::ApplicationWindow::new(application); | ||
window.set_title(Some("Custom Editable")); | ||
window.set_default_size(500, 500); | ||
|
||
let container = gtk::Box::new(gtk::Orientation::Horizontal, 12); | ||
container.set_valign(gtk::Align::Center); | ||
container.set_halign(gtk::Align::Fill); | ||
|
||
let label = gtk::Label::new(Some("Some stuff")); | ||
container.append(&label); | ||
|
||
let editable = CustomEditable::new(); | ||
container.append(&editable); | ||
|
||
println!("{}", editable.get_enable_undo()); | ||
|
||
window.set_child(Some(&container)); | ||
window.show(); | ||
} | ||
|
||
fn main() { | ||
let application = gtk::Application::new( | ||
Some("com.github.gtk-rs.examples.editable"), | ||
Default::default(), | ||
) | ||
.expect("Initialization failed..."); | ||
|
||
application.connect_activate(|app| { | ||
build_ui(app); | ||
}); | ||
|
||
application.run(&args().collect::<Vec<_>>()); | ||
} |