1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- use gdk;
- use gtk::{
- self,
- WidgetExt
- };
- use std::cell::RefCell;
- use std::rc::Rc;
- pub struct Picker {
- pub canvas: gtk::DrawingArea,
- pub mouse_loc: Rc<RefCell<Option<(i32, i32)>>>,
- }
- impl Picker {
- pub fn new() -> Picker {
- let canvas = gtk::DrawingArea::new();
- let mouse_loc = Rc::new(RefCell::new(Some((2, 2))));
- let reader_mouse = mouse_loc.clone();
- let writer_mouse = mouse_loc.clone();
- canvas.connect_draw(move |cv, ctx| {
- let w = cv.get_allocated_width();
- let h = cv.get_allocated_height();
- ctx.set_source_rgb(1.0, 1.0, 1.0);
- ctx.rectangle(0.0, 0.0, w as f64, h as f64);
- ctx.fill();
- reader_mouse.borrow().map(|(x, y)| {
- ctx.set_source_rgb(0.9, 0.9, 0.9);
- ctx.rectangle(
- x as f64 * 32.0,
- y as f64 * 32.0,
- 32.0,
- 32.0,
- );
- ctx.fill();
- });
- gtk::Inhibit(false)
- });
- canvas.connect_motion_notify_event(move |cv, motion| {
- let (x, y) = motion.get_position();
- *writer_mouse.borrow_mut() =
- Some((x as i32 / 32, y as i32 / 32));
- cv.queue_draw();
- gtk::Inhibit(false)
- });
- canvas.add_events(gdk::POINTER_MOTION_MASK.bits() as i32);
- Picker {
- canvas,
- mouse_loc,
- }
- }
- }
|