forked from gtk-rs/gtk4-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add screenshot Update examples/column_view_datagrid/grid_cell/mod.rs Co-authored-by: Hofer-Julian <[email protected]> Update examples/column_view_datagrid/grid_cell/grid_cell.ui Co-authored-by: Marco Melorio <[email protected]> Just use a Gtk::Label in the gridcell Subclass plain Gtk::Widget instead of Gtk::Box xref 'gtk-rs#1111 (comment)' Add dispose function Append a bunch of items Add note Update examples/column_view_datagrid/grid_cell/imp.rs Co-authored-by: Marco Melorio <[email protected]> Update examples/column_view_datagrid/grid_cell/mod.rs Co-authored-by: Marco Melorio <[email protected]> Update examples/column_view_datagrid/grid_cell/imp.rs Co-authored-by: Marco Melorio <[email protected]> Rename to set_entry Rename some variables and remove outer Gtk.Box Fix lint Update examples/column_view_datagrid/grid_cell/imp.rs Co-authored-by: Bilal Elmoussaoui <[email protected]> Update to use gtk::Inscription instead of gtk::Label
- Loading branch information
1 parent
d6a30cc
commit 7cb33b0
Showing
8 changed files
with
182 additions
and
1 deletion.
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
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
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,6 @@ | ||
# ColumnView: Data Grid Example | ||
|
||
This example shows how to create a `gtk::ColumnView` and fill it with some | ||
miscellaneous hard-coded data. | ||
|
||
 |
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,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<interface> | ||
<template class="GridCell" parent="GtkWidget"> | ||
<child> | ||
<object class="GtkInscription" id="name"> | ||
<property name="xalign">0</property> | ||
</object> | ||
</child> | ||
</template> | ||
</interface> |
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,41 @@ | ||
use gtk::glib; | ||
use gtk::prelude::*; | ||
use gtk::subclass::prelude::*; | ||
|
||
use gtk::BinLayout; | ||
use gtk::CompositeTemplate; | ||
|
||
#[derive(Debug, Default, CompositeTemplate)] | ||
#[template(file = "grid_cell.ui")] | ||
pub struct GridCell { | ||
#[template_child] | ||
// gtk::Inscription requires gtk>=4.8. If you target an older version of gtk, you should switch | ||
// to gtk::Label. The benefits for using gtk::Inscription are explained here | ||
// https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.Inscription.html | ||
pub name: TemplateChild<gtk::Inscription>, | ||
} | ||
|
||
#[glib::object_subclass] | ||
impl ObjectSubclass for GridCell { | ||
const NAME: &'static str = "GridCell"; | ||
type Type = super::GridCell; | ||
type ParentType = gtk::Widget; | ||
|
||
fn class_init(klass: &mut Self::Class) { | ||
// When inheriting from GtkWidget directly, you have to either override the size_allocate/measure | ||
// functions of WidgetImpl trait or use a layout manager which provides those functions for your widgets like below. | ||
klass.set_layout_manager_type::<BinLayout>(); | ||
klass.bind_template(); | ||
} | ||
|
||
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) { | ||
obj.init_template(); | ||
} | ||
} | ||
|
||
impl ObjectImpl for GridCell { | ||
fn dispose(&self, _widget: &Self::Type) { | ||
self.name.unparent(); | ||
} | ||
} | ||
impl WidgetImpl for GridCell {} |
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,28 @@ | ||
mod imp; | ||
use gtk::glib; | ||
use gtk::subclass::prelude::*; | ||
|
||
glib::wrapper! { | ||
pub struct GridCell(ObjectSubclass<imp::GridCell>) | ||
@extends gtk::Widget; | ||
} | ||
|
||
impl Default for GridCell { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
pub struct Entry { | ||
pub name: String, | ||
} | ||
|
||
impl GridCell { | ||
pub fn new() -> Self { | ||
glib::Object::new(&[]).expect("Failed to create GridCell") | ||
} | ||
|
||
pub fn set_entry(&self, entry: &Entry) { | ||
self.imp().name.set_text(Some(&entry.name)); | ||
} | ||
} |
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,91 @@ | ||
mod grid_cell; | ||
|
||
use crate::grid_cell::Entry; | ||
use crate::grid_cell::GridCell; | ||
use gtk::gio; | ||
use gtk::glib::BoxedAnyObject; | ||
use gtk::prelude::*; | ||
|
||
struct Row { | ||
col1: String, | ||
col2: String, | ||
} | ||
|
||
use std::cell::Ref; | ||
|
||
fn main() { | ||
let app = gtk::Application::new( | ||
Some("com.github.gtk-rs.examples.columnview-example"), | ||
Default::default(), | ||
); | ||
app.connect_activate(build_ui); | ||
app.run(); | ||
} | ||
|
||
fn build_ui(application: >k::Application) { | ||
let window = gtk::ApplicationWindow::builder() | ||
.default_width(320) | ||
.default_height(480) | ||
.application(application) | ||
.title("ColumnView Example") | ||
.build(); | ||
|
||
let store = gio::ListStore::new(BoxedAnyObject::static_type()); | ||
|
||
(0..10000).for_each(|i| { | ||
store.append(&BoxedAnyObject::new(Row { | ||
col1: format!("col1 {}", i), | ||
col2: format!("col2 {}", i), | ||
})) | ||
}); | ||
let sel = gtk::SingleSelection::new(Some(&store)); | ||
let columnview = gtk::ColumnView::new(Some(&sel)); | ||
|
||
let col1factory = gtk::SignalListItemFactory::new(); | ||
let col2factory = gtk::SignalListItemFactory::new(); | ||
let col1 = gtk::ColumnViewColumn::new(Some("Column 1"), Some(&col1factory)); | ||
let col2 = gtk::ColumnViewColumn::new(Some("Column 2"), Some(&col2factory)); | ||
col1factory.connect_setup(move |_factory, item| { | ||
let item = item.downcast_ref::<gtk::ListItem>().unwrap(); | ||
let row = GridCell::new(); | ||
item.set_child(Some(&row)); | ||
}); | ||
|
||
col1factory.connect_bind(move |_factory, item| { | ||
let item = item.downcast_ref::<gtk::ListItem>().unwrap(); | ||
let child = item.child().unwrap().downcast::<GridCell>().unwrap(); | ||
let entry = item.item().unwrap().downcast::<BoxedAnyObject>().unwrap(); | ||
let r: Ref<Row> = entry.borrow(); | ||
let ent = Entry { | ||
name: r.col1.to_string(), | ||
}; | ||
child.set_entry(&ent); | ||
}); | ||
col2factory.connect_setup(move |_factory, item| { | ||
let item = item.downcast_ref::<gtk::ListItem>().unwrap(); | ||
let row = GridCell::new(); | ||
item.set_child(Some(&row)); | ||
}); | ||
|
||
col2factory.connect_bind(move |_factory, item| { | ||
let item = item.downcast_ref::<gtk::ListItem>().unwrap(); | ||
let child = item.child().unwrap().downcast::<GridCell>().unwrap(); | ||
let entry = item.item().unwrap().downcast::<BoxedAnyObject>().unwrap(); | ||
let r: Ref<Row> = entry.borrow(); | ||
let ent = Entry { | ||
name: r.col2.to_string(), | ||
}; | ||
child.set_entry(&ent); | ||
}); | ||
columnview.append_column(&col1); | ||
columnview.append_column(&col2); | ||
|
||
let scrolled_window = gtk::ScrolledWindow::builder() | ||
.hscrollbar_policy(gtk::PolicyType::Never) // Disable horizontal scrolling | ||
.build(); | ||
|
||
scrolled_window.set_child(Some(&columnview)); | ||
|
||
window.set_child(Some(&scrolled_window)); | ||
window.show(); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.