Merge: ldflags for Android
[nit.git] / examples / calculator / src / calculator_gtk.nit
index d4edbdb..bd01986 100644 (file)
@@ -21,115 +21,91 @@ import calculator_logic
 
 import gtk
 
+# GTK calculator UI
 class CalculatorGui
        super GtkCallable
 
-       var win : GtkWindow is noinit
-       var container : GtkGrid is noinit
+       private var win: GtkWindow is noinit
+       private var container: GtkGrid is noinit
 
-       var lbl_disp : GtkLabel is noinit
-       var but_eq : GtkButton is noinit
-       var but_dot : GtkButton is noinit
+       private var lbl_disp: GtkLabel is noinit
+       private var but_eq: GtkButton is noinit
+       private var but_dot: GtkButton is noinit
 
-       var context = new CalculatorContext
+       private var context = new CalculatorContext
 
-       redef fun signal( sender, user_data )
+       redef fun signal(sender, op)
        do
-               var after_point = context.after_point
-               if after_point == null then
-                   after_point = 0
-               else
-                   after_point = (after_point.abs)
-               end
-
-               if user_data isa Char then # is an operation
-                       var c = user_data
-                       if c == '.' then
-                               but_dot.sensitive= false
+               if op isa Char then # is an operation
+                       if op == '.' then
+                               but_dot.sensitive = false
                                context.switch_to_decimals
-                               lbl_disp.text = "{context.current.to_i}."
                        else
-                               but_dot.sensitive= true
-                               context.push_op( c )
-
-                               var s = context.result.to_precision_native(6)
-                               var index : nullable Int = null
-                               for i in s.length.times do
-                                   var chiffre = s.chars[i]
-                                   if chiffre == '0' and index == null then
-                                       index = i
-                                   else if chiffre != '0' then
-                                       index = null
-                                   end
-                               end
-                               if index != null then
-                                       s = s.substring(0, index)
-                                       if s.chars[s.length-1] == ',' then s = s.substring(0, s.length-1)
-                               end
-                               lbl_disp.text = s
+                               but_dot.sensitive = true
+                               context.push_op op
                        end
-               else if user_data isa Int then # is a number
-                       var n = user_data
-                       context.push_digit( n )
-                       lbl_disp.text = context.current.to_precision_native(after_point)
+               else if op isa Int then # is a number
+                       context.push_digit op
                end
+
+               lbl_disp.text = context.display_text
        end
 
        init
        do
                init_gtk
 
-               win = new GtkWindow( 0 )
+               win = new GtkWindow(0)
 
-               container = new GtkGrid(5,5,true)
-               win.add( container )
+               container = new GtkGrid(5, 5, true)
+               win.add(container)
 
-               lbl_disp = new GtkLabel( "_" )
-               container.attach( lbl_disp, 0, 0, 5, 1 )
+               lbl_disp = new GtkLabel("_")
+               container.attach(lbl_disp, 0, 0, 5, 1)
 
-               # digits
+               # Digits
                for n in [0..9] do
-                       var but = new GtkButton.with_label( n.to_s )
-                       but.request_size( 64, 64 )
-                       but.signal_connect( "clicked", self, n )
+                       var but = new GtkButton.with_label(n.to_s)
+                       but.request_size(64, 64)
+                       but.signal_connect("clicked", self, n)
                        if n == 0 then
-                               container.attach( but, 0, 4, 1, 1 )
-                       else container.attach( but, (n-1)%3, 3-(n-1)/3, 1, 1 )
+                               container.attach(but, 0, 4, 1, 1)
+                       else container.attach(but, (n-1)%3, 3-(n-1)/3, 1, 1)
                end
 
-               # operators
+               # Operators
                var r = 1
-               for op in ['+', '-', '*', '/' ] do
-                       var but = new GtkButton.with_label( op.to_s )
-                       but.request_size( 64, 64 )
-                       but.signal_connect( "clicked", self, op )
-                       container.attach( but, 3, r, 1, 1 )
+               for op in ['+', '-', '*', '/'] do
+                       var but = new GtkButton.with_label(op.to_s)
+                       but.request_size(64, 64)
+                       but.signal_connect("clicked", self, op)
+                       container.attach(but, 3, r, 1, 1)
                        r+=1
                end
 
                # =
-               but_eq = new GtkButton.with_label( "=" )
-               but_eq.request_size( 64, 64 )
-               but_eq.signal_connect( "clicked", self, '=' )
-               container.attach( but_eq, 4, 3, 1, 2 )
+               but_eq = new GtkButton.with_label("=")
+               but_eq.request_size(64, 64)
+               but_eq.signal_connect("clicked", self, '=')
+               container.attach(but_eq, 4, 3, 1, 2)
 
                # .
-               but_dot = new GtkButton.with_label( "." )
-               but_dot.request_size( 64, 64 )
-               but_dot.signal_connect( "clicked", self, '.' )
-               container.attach( but_dot, 1, 4, 1, 1 )
-
-               #C
-               var but_c =  new GtkButton.with_label( "C" )
-               but_c.request_size( 64, 64 )
+               but_dot = new GtkButton.with_label(".")
+               but_dot.request_size(64, 64)
+               but_dot.signal_connect("clicked", self, '.')
+               container.attach(but_dot, 1, 4, 1, 1)
+
+               # C
+               var but_c =  new GtkButton.with_label("C")
+               but_c.request_size(64, 64)
                but_c.signal_connect("clicked", self, 'C')
-               container.attach( but_c, 2, 4, 1, 1 )
+               container.attach(but_c, 2, 4, 1, 1)
 
                win.show_all
        end
 end
 
-# graphical application
+# Do not show GUI in when testing
 if "NIT_TESTING".environ == "true" then exit 0
 
 var app = new CalculatorGui