mainwindow: can draw a rectangle at mouse position

This commit is contained in:
Stéphane Cerveau 2021-11-04 14:25:25 +01:00
parent 465ac78c08
commit 52718e0388
2 changed files with 31 additions and 6 deletions

View file

@ -364,14 +364,11 @@
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkViewport">
<object class="GtkViewport" id="drawing_area">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkDrawingArea">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<placeholder/>
</child>
</object>
</child>

View file

@ -1,8 +1,13 @@
use gtk::ffi::GtkDrawingArea;
use gtk::gdk::Display;
use gtk::glib;
use gtk::prelude::*;
use gtk::{
AboutDialog, AccelFlags, AccelGroup, ApplicationWindow, Builder, MenuItem, WindowPosition,
AboutDialog, AccelFlags, AccelGroup, ApplicationWindow, Builder, DrawingArea, EventBox,
MenuItem, Viewport, WindowPosition,
};
use std::cell::RefCell;
use std::rc::Rc;
pub fn build_ui(application: &gtk::Application) {
let glade_src = include_str!("gps.ui");
@ -14,6 +19,29 @@ pub fn build_ui(application: &gtk::Application) {
window.set_title("GstPipelineStudio");
window.set_position(WindowPosition::Center);
window.set_size_request(800, 600);
let drawing_area = DrawingArea::new();
let view_port: Viewport = builder.object("drawing_area").expect("Couldn't get window");
let event_box = EventBox::new();
event_box.add(&drawing_area);
view_port.add(&event_box);
let mut position = (0.0, 0.0);
let position = Rc::new(RefCell::new((0.0, 0.0)));
let p_clone = position.clone();
drawing_area.connect_draw(move |w, c| {
println!("w: {} c:{} p: {:?}", w, c, p_clone);
c.rectangle(p_clone.borrow().0, p_clone.borrow().1, 10.0, 20.0);
c.fill();
gtk::Inhibit(false)
});
event_box.connect_button_release_event(move |w, evt| {
let mut p = position.borrow_mut();
p.0 = evt.position().0; // = evt.position().clone();
p.1 = evt.position().1;
drawing_area.queue_draw();
gtk::Inhibit(false)
});
window.show_all();
let quit: MenuItem = builder.object("menu-quit").expect("Couldn't get window");