Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Compile with:
- // valac --pkg gtk+-3.0 -X -lm chronometer.vala
- public class Chronometer: Gtk.DrawingArea {
- public int time = 0;
- public int max_time = 60;
- public int line_width = 10;
- public double b_color = 0.7;
- public double l_color = 0.4;
- public double t_color = 0.2;
- public Chronometer () {
- set_size_request(100, 100);
- //Timeout.add(1000, count_time);
- }
- public override bool draw(Cairo.Context ctx) {
- int width = get_allocated_width();
- int height = get_allocated_height();
- int x = width / 2;
- int y = height / 2;
- var radius = int.min(width, height) / 2 - 10;
- // Draw background circle
- ctx.set_source_rgb(this.b_color, this.b_color, this.b_color);
- ctx.set_line_width(this.line_width);
- ctx.arc(x, y, radius, 0, 2 * Math.PI);
- ctx.stroke();
- // Draw current time
- var t = this.time * Math.PI * 2 / this.max_time - (Math.PI * 0.5);
- ctx.set_source_rgb(this.l_color, this.l_color, this.l_color);
- ctx.arc(x, y, radius, Math.PI * 2 - (Math.PI * 0.5), t);
- ctx.stroke();
- // Draw text
- Cairo.TextExtents extents;
- var label = this.time.to_string();
- var size = radius - 20 / 2.0;
- ctx.select_font_face("Adventure", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
- ctx.set_font_size(size);
- ctx.text_extents(label, out extents);
- ctx.move_to(x - extents.width / 2, y + extents.height / 2);
- ctx.show_text(label);
- return true;
- }
- public void set_time(int time) {
- this.time = time;
- update();
- }
- public void set_max_time(int time) {
- this.max_time = time;
- if (this.time < this.max_time) {
- this.time = this.max_time;
- }
- update();
- }
- private bool count_time() {
- set_time(this.time - 1);
- return true;
- }
- private void update() {
- var window = get_window ();
- if (null == window) {
- return;
- }
- var region = window.get_clip_region();
- window.invalidate_region(region, true);
- window.process_updates(true);
- }
- }
- public class Window: Gtk.Window {
- private Chronometer chronometer;
- public Window () {
- this.destroy.connect(Gtk.main_quit);
- set_default_size(600, 350);
- set_title("Chronometer");
- set_border_width(10);
- Gtk.Box vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 10);
- chronometer = new Chronometer();
- vbox.pack_start(chronometer, true, true, 0);
- Gtk.Adjustment adj = new Gtk.Adjustment(0, 0, 60, 1, 5, 0);
- adj.value_changed.connect(set_time);
- Gtk.HScale scale = new Gtk.HScale(adj);
- scale.set_digits(0);
- vbox.pack_end(scale, false, false, 0);
- add(vbox);
- }
- private void set_time(Gtk.Adjustment adj) {
- int val = int.parse(adj.value.to_string());
- chronometer.set_time(val);
- }
- }
- public static int main (string[] args) {
- Gtk.init (ref args);
- var window = new Window ();
- window.show_all ();
- Gtk.main ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement