use crate::*; pub struct Grid { inner: gtk::Grid, } pub struct CellPosition(i32, i32); impl Grid { // Initialize a grid, populating its cells // Only square grid for now pub fn init(inner: gtk::Grid, size: i32) -> Self { for i in 0..(size * size) { let x = i % size; let y = i / size; let cell = grid::Cell::new(CellPosition(x,y)); inner.attach(cell.as_ref(), x, y, 1, 1); // Signals } Grid { inner } } } struct CellWidget { eventbox: gtk::EventBox, position: gtk::Label, header : gtk::Label, } impl CellWidget { fn new(pos: CellPosition, header_text: &str) -> Self { let cell_src = include_str!("../res/cell.glade"); let builder = gtk::Builder::new_from_string(&cell_src); let eventbox: gtk::EventBox = builder .get_object("cell") .unwrap(); eventbox.set_events(gdk::EventMask::BUTTON_PRESS_MASK); // Drag-and-drop capacity let targets = vec![]; eventbox.drag_dest_set( gtk::DestDefaults::ALL, &targets, gdk::DragAction::COPY, ); eventbox.drag_source_set( gdk::ModifierType::MODIFIER_MASK, &targets, gdk::DragAction::COPY, ); eventbox.connect_drag_begin(|_,_| { println!("Lift..."); }); eventbox.connect_drag_end(|_,_| { println!("Drop !"); }); let header: gtk::Label = builder .get_object("header") .unwrap(); header.set_text(header_text); let position: gtk::Label = builder .get_object("position") .unwrap(); position.set_text(&format!("{}x{}", pos.0, pos.1)); let header_clone = header.clone(); eventbox.connect_button_press_event(Self::on_click(header_clone)); CellWidget { eventbox, position, header, } } // Handler for button press events fn on_click( data: gtk::Label ) -> impl Fn(>k::EventBox, &gdk::EventButton) -> gtk::Inhibit { move |_,_| { data.set_text("Clicked."); gtk::Inhibit(true) } } } impl AsRef for CellWidget { #[inline] fn as_ref(&self) -> >k::EventBox { &self.eventbox } } pub struct Cell { inner: CellWidget, is_active: bool, is_occupied: bool, } impl Cell { pub fn new(pos: CellPosition) -> Self { let inner = CellWidget::new(pos, "Empty cell"); Cell { inner, is_active: false, is_occupied: false, } } } impl AsRef for Cell { #[inline] fn as_ref(&self) -> >k::EventBox { self.inner.as_ref() } }