I am trying to build a GUI in Rust using GTK, Cairo and Glade. I want to draw a playing field using gtk::DrawingArea
but I do not know how to use it. I have this:
extern crate cairo;
extern crate gtk;
use gtk::*;
fn main() {
gtk::init().unwrap(); //init gtk before using it
let glade_src = include_str!("Glade_gui.glade"); //build the glade gui
let builder = gtk::Builder::new_from_string(glade_src);
//get widgets from the gui
let draw_area: gtk::DrawingArea = builder.get_object("zeichenbrett").unwrap();
let window: gtk::Window = builder.get_object("fenster").unwrap();
let size = (600, 600); //define the size for the image
let style_context: gtk::StyleContext = draw_area.get_style_context().unwrap(); //get the style context from the drawing area
let surface: cairo::ImageSurface =
cairo::ImageSurface::create(cairo::Format::ARgb32, size.0, size.1).unwrap(); //build a new ImageSurface to draw on
let context: cairo::Context = cairo::Context::new(&surface); //build a new cairo context from that ImageSurface to draw on
//just a blue area
context.set_source_rgb(0.0, 0.0, 1.0);
context.paint();
context.stroke();
gtk::functions::render_background(
&style_context,
&context,
0.0,
0.0,
size.0 as f64,
size.1 as f64,
); //here I thought that I drew the context cairo::Context to the drawingArea but it seems to do nothing.
window.show_all();
gtk::main();
}
It compiles and runs, but no playing field is shown on the window.
I think I am not using the render_background
function correctly, but I do not know how to do it right.
Here is the Glade_gui.glade file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="fenster">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Reise nach Jerusalem</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">5</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkButton" id="links">
<property name="label" translatable="yes">Left</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="rechts">
<property name="label" translatable="yes">Right</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="quit">
<property name="label" translatable="yes">Quit</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkDrawingArea" id="zeichenbrett">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>