1 | |
use gtk;
|
| 1 |
use gdk;
|
2 | 2 |
use gtk::{
|
| 3 |
self,
|
3 | 4 |
ContainerExt,
|
4 | 5 |
HeaderBarExt,
|
5 | 6 |
WindowExt,
|
|
8 | 9 |
FileChooserExt,
|
9 | 10 |
ButtonExt,
|
10 | 11 |
};
|
| 12 |
use std::cell::RefCell;
|
11 | 13 |
use std::process;
|
| 14 |
use std::rc::Rc;
|
12 | 15 |
|
13 | 16 |
pub struct App {
|
14 | 17 |
pub window: gtk::Window,
|
15 | |
pub canvas: gtk::DrawingArea,
|
| 18 |
pub canvas: GridCanvas,
|
16 | 19 |
pub header: Header,
|
17 | 20 |
}
|
18 | 21 |
|
|
23 | 26 |
pub save_as_btn: gtk::Button,
|
24 | 27 |
}
|
25 | 28 |
|
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 {
|
30 | 36 |
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();
|
31 | 40 |
|
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| {
|
38 | 42 |
let w = cv.get_allocated_width();
|
39 | 43 |
let h = cv.get_allocated_height();
|
40 | 44 |
ctx.set_source_rgb(1.0, 1.0, 1.0);
|
41 | 45 |
ctx.rectangle(0.0, 0.0, w as f64, h as f64);
|
42 | 46 |
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 |
|
43 | 54 |
ctx.set_source_rgb(0.8, 0.8, 0.8);
|
44 | 55 |
for x in 0..((w / 32) + 1) {
|
45 | 56 |
ctx.move_to(x as f64 * 32.0, 0.0);
|
|
53 | 64 |
}
|
54 | 65 |
gtk::Inhibit(false)
|
55 | 66 |
});
|
| 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);
|
56 | 95 |
|
57 | 96 |
gtk::Window::set_default_icon_name("iconname");
|
58 | 97 |
|