dev/vala/drawing_area/drawing_area.vala

69 lines
1.9 KiB
Vala

public class Application : Gtk.Window {
public Application () {
// Prepare Gtk.Window:
this.title = "My Gtk.DrawingArea";
this.window_position = Gtk.WindowPosition.CENTER;
this.destroy.connect (Gtk.main_quit);
this.set_default_size (400, 400);
// The drawing area:
Gtk.DrawingArea drawing_area = new Gtk.DrawingArea ();
drawing_area.draw.connect ((context) => {
// Get necessary data:
weak Gtk.StyleContext style_context = drawing_area.get_style_context ();
int height = drawing_area.get_allocated_height ();
int width = drawing_area.get_allocated_width ();
Gdk.RGBA color = style_context.get_color (0);
// Draw an arc:
/*double xc = width / 2.0;
double yc = height / 2.0;
double radius = int.min (width, height) / 2.0;
double angle1 = 0;
double angle2 = 2*Math.PI;
context.arc (xc, yc, radius, angle1, angle2);
Gdk.cairo_set_source_rgba (context, color);
context.fill ();*/
// Chart
context.set_line_width (1);
context.move_to (30, 30);
context.line_to (30, height - 30);
context.line_to (width - 30, height - 30);
context.stroke ();
context.move_to (25, 40);
context.line_to (30, 30);
context.line_to (35, 40);
context.stroke ();
context.move_to (width - 40, height - 35);
context.line_to (width - 30, height - 30);
context.line_to (width - 40, height - 25);
context.stroke ();
// Text:
context.set_source_rgb (0.1, 0.1, 0.1);
context.select_font_face ("Adventure", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
context.set_font_size (20);
context.move_to (10, 40);
context.show_text ("Y");
context.move_to (width - 45, height - 7);
context.show_text ("X");
return true;
});
this.add (drawing_area);
}
public static int main (string[] args) {
Gtk.init (ref args);
Application app = new Application ();
app.show_all ();
Gtk.main ();
return 0;
}
}