Advertisement
Guest User

fibonacci.vala

a guest
Mar 7th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 2.41 KB | None | 0 0
  1. using Gtk;
  2.  
  3.  
  4.  public class MyThread : Object{
  5.     public long n { get; private set; }
  6.    
  7.     public MyThread(long n){
  8.         this.n = n;
  9.     }
  10.    
  11.     public long run(){
  12.         long number;
  13.         number = this.fibonacci(this.n);
  14.        
  15.         Thread.exit (42);
  16.         return number;
  17.     }
  18.     public long fibonacci (long i){
  19.    
  20.         if (i < 2) {
  21.             return i;
  22.       } else {
  23.             stdout.printf("Running! \n");
  24.             return (this.fibonacci(i - 1) + fibonacci(i - 2));
  25.         }
  26.     }
  27.    
  28. }
  29.  
  30.  
  31. public class Application : Gtk.Application {
  32.  
  33.     public Application(){
  34.         Object(application_id:"testing.my.application", flags: ApplicationFlags.FLAGS_NONE);
  35.  
  36.     }
  37.  
  38.     protected override void activate(){
  39.    
  40.     Gtk.ApplicationWindow window = new Gtk.ApplicationWindow (this);
  41.    
  42.     window.title = "Fibonacci GTK+";
  43.     window.set_border_width (12);
  44.     window.set_default_size (70, 70);
  45.     window.set_position (Gtk.WindowPosition.CENTER);
  46.     window.destroy.connect (Gtk.main_quit);
  47.  
  48.     //var loop = new MainLoop();
  49.  
  50.     var headerbar = new Gtk.HeaderBar ();
  51.     var entry = new Gtk.Entry ();
  52.     var button = new Gtk.Button.with_label ("Calculate");
  53.     var label = new Gtk.Label ("Result");
  54.     var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
  55.  
  56.     headerbar.title = "Fibonacci GTK+";
  57.     headerbar.show_close_button = true;
  58.  
  59.     box.pack_start (entry, false, true, 0);
  60.     box.pack_start (label, false, true, 12);
  61.     box.pack_start (button, false, true, 0);
  62.  
  63.     window.set_titlebar (headerbar);
  64.  
  65.     window.add (box);
  66.     window.show_all ();
  67.  
  68.     /*** LOGIC ***/
  69.     button.clicked.connect ( ()=> {
  70.         try {
  71.         // Start a thread:
  72.         MyThread my_thread = new MyThread (long.parse (entry.get_text ()));
  73.         Thread<long> thread = new Thread<long>.try ("My fst. thread", my_thread.run);
  74.  
  75.         // Wait until thread finishes:
  76.         long result = thread.join ();
  77.        
  78.         label.set_label (result.to_string ());
  79.  
  80.     } catch (Error e) {
  81.         stdout.printf ("Error: %s\n", e.message);
  82.     }
  83.    
  84.     });    
  85.    
  86.     }
  87. }
  88.  
  89. public static int main (string[] args){
  90.  
  91.     if (Thread.supported () == false) {
  92.         stderr.printf ("Threads are not supported!\n");
  93.         return -1;
  94.     }
  95.    
  96.     var app = new Application();
  97.    
  98.     app.run(args);
  99.     Gtk.main ();
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement