Advertisement
Guest User

Widget circular

a guest
May 30th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 3.23 KB | None | 0 0
  1. // Compile with:
  2. // valac --pkg gtk+-3.0 -X -lm chronometer.vala
  3.  
  4. public class Chronometer: Gtk.DrawingArea {
  5.  
  6.     public int time = 0;
  7.     public int max_time = 60;
  8.     public int line_width = 10;
  9.  
  10.     public double b_color = 0.7;
  11.     public double l_color = 0.4;
  12.     public double t_color = 0.2;
  13.  
  14.     public Chronometer () {
  15.         set_size_request(100, 100);
  16.         //Timeout.add(1000, count_time);
  17.     }
  18.  
  19.     public override bool draw(Cairo.Context ctx) {
  20.         int width = get_allocated_width();
  21.         int height = get_allocated_height();
  22.         int x = width / 2;
  23.         int y = height / 2;
  24.         var radius = int.min(width, height) / 2 - 10;
  25.  
  26.         // Draw background circle
  27.         ctx.set_source_rgb(this.b_color, this.b_color, this.b_color);
  28.         ctx.set_line_width(this.line_width);
  29.         ctx.arc(x, y, radius, 0, 2 * Math.PI);
  30.         ctx.stroke();
  31.  
  32.         // Draw current time
  33.         var t = this.time * Math.PI * 2 / this.max_time - (Math.PI * 0.5);
  34.  
  35.         ctx.set_source_rgb(this.l_color, this.l_color, this.l_color);
  36.         ctx.arc(x, y, radius, Math.PI * 2 - (Math.PI * 0.5), t);
  37.         ctx.stroke();
  38.  
  39.         // Draw text
  40.         Cairo.TextExtents extents;
  41.         var label = this.time.to_string();
  42.         var size = radius - 20 / 2.0;
  43.  
  44.         ctx.select_font_face("Adventure", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
  45.         ctx.set_font_size(size);
  46.         ctx.text_extents(label, out extents);
  47.         ctx.move_to(x - extents.width / 2, y + extents.height / 2);
  48.         ctx.show_text(label);
  49.  
  50.         return true;
  51.     }
  52.  
  53.     public void set_time(int time) {
  54.         this.time = time;
  55.         update();
  56.     }
  57.  
  58.     public void set_max_time(int time) {
  59.         this.max_time = time;
  60.  
  61.         if (this.time < this.max_time) {
  62.             this.time = this.max_time;
  63.         }
  64.  
  65.         update();
  66.     }
  67.  
  68.     private bool count_time() {
  69.         set_time(this.time - 1);
  70.         return true;
  71.     }
  72.  
  73.     private void update() {
  74.         var window = get_window ();
  75.         if (null == window) {
  76.             return;
  77.         }
  78.  
  79.         var region = window.get_clip_region();
  80.         window.invalidate_region(region, true);
  81.         window.process_updates(true);
  82.     }
  83. }
  84.  
  85. public class Window: Gtk.Window {
  86.  
  87.     private Chronometer chronometer;
  88.  
  89.     public Window () {
  90.         this.destroy.connect(Gtk.main_quit);
  91.         set_default_size(600, 350);
  92.         set_title("Chronometer");
  93.         set_border_width(10);
  94.  
  95.         Gtk.Box vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 10);
  96.  
  97.         chronometer = new Chronometer();
  98.         vbox.pack_start(chronometer, true, true, 0);
  99.  
  100.         Gtk.Adjustment adj = new Gtk.Adjustment(0, 0, 60, 1, 5, 0);
  101.         adj.value_changed.connect(set_time);
  102.  
  103.         Gtk.HScale scale = new Gtk.HScale(adj);
  104.         scale.set_digits(0);
  105.         vbox.pack_end(scale, false, false, 0);
  106.  
  107.         add(vbox);
  108.     }
  109.  
  110.     private void set_time(Gtk.Adjustment adj) {
  111.         int val = int.parse(adj.value.to_string());
  112.         chronometer.set_time(val);
  113.     }
  114. }
  115.  
  116. public static int main (string[] args) {
  117.     Gtk.init (ref args);
  118.  
  119.     var window = new Window ();
  120.     window.show_all ();
  121.  
  122.     Gtk.main ();
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement