gdritter repos palladio / bed69e0
Cursor-like selection thingy Getty Ritter 6 years ago
3 changed file(s) with 55 addition(s) and 12 deletion(s). Collapse all Expand all
55
66 [dependencies]
77 gtk = { version = "0.2", features = ["v3_22"] }
8 gdk = { version = "0.6" }
89 rand = "0.4.2"
910 failure = "0.1.1"
1011 serde = "1.0.43"
1213 serde_derive = "*"
1314 zip = "0.3"
1415 image = "*"
16 cairo-rs = "0.2.0"
1 extern crate cairo;
2 extern crate gdk;
13 extern crate gtk;
24 extern crate rand;
35 extern crate failure;
1 use gtk;
1 use gdk;
22 use gtk::{
3 self,
34 ContainerExt,
45 HeaderBarExt,
56 WindowExt,
89 FileChooserExt,
910 ButtonExt,
1011 };
12 use std::cell::RefCell;
1113 use std::process;
14 use std::rc::Rc;
1215
1316 pub struct App {
1417 pub window: gtk::Window,
15 pub canvas: gtk::DrawingArea,
18 pub canvas: GridCanvas,
1619 pub header: Header,
1720 }
1821
2326 pub save_as_btn: gtk::Button,
2427 }
2528
26 impl App {
27 fn new() -> App {
28 let window = gtk::Window::new(gtk::WindowType::Toplevel);
29 let header = Header::new();
29 pub struct GridCanvas {
30 pub canvas: gtk::DrawingArea,
31 pub mouse_loc: Rc<RefCell<Option<(i32, i32)>>>,
32 }
33
34 impl GridCanvas {
35 fn new() -> GridCanvas {
3036 let canvas = gtk::DrawingArea::new();
37 let mouse_loc = Rc::new(RefCell::new(Some((2, 2))));
38 let reader_mouse = mouse_loc.clone();
39 let writer_mouse = mouse_loc.clone();
3140
32 window.set_titlebar(&header.container);
33 window.set_title("Palladio");
34 window.set_wmclass("palladio", "Palladio");
35 window.add(&canvas);
36
37 canvas.connect_draw(|cv, ctx| {
41 canvas.connect_draw(move |cv, ctx| {
3842 let w = cv.get_allocated_width();
3943 let h = cv.get_allocated_height();
4044 ctx.set_source_rgb(1.0, 1.0, 1.0);
4145 ctx.rectangle(0.0, 0.0, w as f64, h as f64);
4246 ctx.fill();
47
48 ctx.set_source_rgb(0.9, 0.9, 0.9);
49 reader_mouse.borrow().map(|(x, y)| {
50 ctx.rectangle(x as f64 * 32.0, y as f64 * 32.0, 32.0, 32.0);
51 ctx.fill();
52 });
53
4354 ctx.set_source_rgb(0.8, 0.8, 0.8);
4455 for x in 0..((w / 32) + 1) {
4556 ctx.move_to(x as f64 * 32.0, 0.0);
5364 }
5465 gtk::Inhibit(false)
5566 });
67
68 canvas.connect_motion_notify_event(move |cv, motion| {
69 let (x, y) = motion.get_position();
70 *writer_mouse.borrow_mut() = Some((x as i32 / 32, y as i32 / 32));
71 cv.queue_draw();
72 gtk::Inhibit(false)
73 });
74
75 canvas.add_events(gdk::POINTER_MOTION_MASK.bits() as i32);
76
77 GridCanvas {
78 canvas,
79 mouse_loc,
80 }
81 }
82 }
83
84 impl App {
85 fn new() -> App {
86 let window = gtk::Window::new(gtk::WindowType::Toplevel);
87 let header = Header::new();
88 let canvas = GridCanvas::new();
89
90 window.add_events(gdk::POINTER_MOTION_MASK.bits() as i32);
91 window.set_titlebar(&header.container);
92 window.set_title("Palladio");
93 window.set_wmclass("palladio", "Palladio");
94 window.add(&canvas.canvas);
5695
5796 gtk::Window::set_default_icon_name("iconname");
5897