Skip to content

Commit

Permalink
Initial commit of datagrid example
Browse files Browse the repository at this point in the history
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
cmdcolin authored and bilelmoussaoui committed Sep 10, 2022
1 parent d6a30cc commit 7cb33b0
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ libloading = { version = "0.7.0", optional = true }
[dependencies.gtk]
path = "../gtk4"
package = "gtk4"
features = ["v4_8"]

[features]
default = []
Expand All @@ -36,6 +37,10 @@ path = "clipboard/main.rs"
name = "clock"
path = "clock/main.rs"

[[bin]]
name = "column_view_datagrid"
path = "column_view_datagrid/main.rs"

[[bin]]
name = "composite_template"
path = "composite_template/main.rs"
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ For example, if you want to run the example in subfolder "basics", execute:
cargo run --bin basics
```


- [Basic example](./basics/)
- [Using the Builder pattern](./builder_pattern/)
- [Clipboard](./clipboard/)
- [Clock example](./clock/)
- [Column View Datagrid Example](./column_view_datagrid/)
- [Composite Template](./composite_template/)
- [Content Provider](./content_provider/)
- [CSS](./css/)
Expand Down
6 changes: 6 additions & 0 deletions examples/column_view_datagrid/README.md
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.

![Screenshot](screenshot.png)
10 changes: 10 additions & 0 deletions examples/column_view_datagrid/grid_cell/grid_cell.ui
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>
41 changes: 41 additions & 0 deletions examples/column_view_datagrid/grid_cell/imp.rs
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 {}
28 changes: 28 additions & 0 deletions examples/column_view_datagrid/grid_cell/mod.rs
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));
}
}
91 changes: 91 additions & 0 deletions examples/column_view_datagrid/main.rs
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: &gtk::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();
}
Binary file added examples/column_view_datagrid/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7cb33b0

Please sign in to comment.