Merge: A collection of miscellaneous commits
authorJean Privat <jean@pryen.org>
Tue, 27 Jan 2015 03:59:43 +0000 (10:59 +0700)
committerJean Privat <jean@pryen.org>
Tue, 27 Jan 2015 03:59:43 +0000 (10:59 +0700)
Pull-Request: #1122
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Romain Chanoir <chanoir.romain@courrier.uqam.ca>
Reviewed-by: Jean Privat <jean@pryen.org>

examples/calculator/Makefile
examples/calculator/src/calculator_gtk.nit
examples/calculator/src/calculator_logic.nit
lib/android/input_events.nit
lib/egl.nit
lib/gtk/v3_4/gtk_core.nit
lib/mnit_input.nit
lib/more_collections.nit
lib/sdl.nit
src/ffi/c.nit

index f16f885..43cdcc4 100644 (file)
@@ -1,6 +1,8 @@
 all:
        mkdir -p bin/
-       ../../bin/nitc --dir bin/ src/calculator_gtk.nit src/calculator_test.nit
+
+       # Compile in global mode to silence warnings on unused GTK deprecated
+       ../../bin/nitc --global --dir bin/ src/calculator_gtk.nit src/calculator_test.nit
 
 android:
        mkdir -p bin/ res/
index bd2bd11..bd01986 100644 (file)
@@ -21,17 +21,18 @@ 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, op)
        do
@@ -54,57 +55,57 @@ class CalculatorGui
        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
index c5fbaf7..57d3b5a 100644 (file)
@@ -1,7 +1,5 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
-# Copyright 2013-2014 Alexis Laferrière <alexis.laf@xymus.net>
-#
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 # Business logic of a calculator
 module calculator_logic
 
+# Hold the state of the calculator and its services
 class CalculatorContext
+       # Result of the last operation
        var result: nullable Numeric = null
 
+       # Last operation pushed with `push_op`, to be executed on the next push
        var last_op: nullable Char = null
 
+       # Value currently being entered
        var current: nullable FlatBuffer = null
+
+       # Text to display on screen
        fun display_text: String
        do
                var result = result
@@ -51,7 +55,8 @@ class CalculatorContext
                return buf.to_s
        end
 
-       fun push_op( op : Char )
+       # Push operation `op`, will usually execute the last operation
+       fun push_op(op: Char)
        do
                apply_last_op_if_any
                if op == 'C' then
@@ -65,7 +70,8 @@ class CalculatorContext
                self.current = null
        end
 
-       fun push_digit( digit : Int )
+       # Push a digit
+       fun push_digit(digit: Int)
        do
                var current = current
                if current == null then current = new FlatBuffer
@@ -78,6 +84,7 @@ class CalculatorContext
                end
        end
 
+       # Switch entry mode from integer to decimal
        fun switch_to_decimals
        do
                var current = current
@@ -86,7 +93,8 @@ class CalculatorContext
                self.current = current
        end
 
-       fun apply_last_op_if_any
+       # Execute the last operation it not null
+       protected fun apply_last_op_if_any
        do
                var op = last_op
 
@@ -106,6 +114,7 @@ class CalculatorContext
                else if op == '*' then
                        result = result.mul(current.to_n)
                end
+
                self.result = result
                self.current = null
        end
index c766a16..55f21a5 100644 (file)
@@ -216,6 +216,8 @@ extern class AndroidKeyEvent `{AInputEvent *`}
                return 0;
        `}
 
+       redef fun name do return key_code.to_s
+
        # Was this event raised by the back key?
        fun is_back_key: Bool do return key_code == 4
 
index 5c50387..4c24b57 100644 (file)
@@ -412,6 +412,12 @@ class EGLConfigChooser
        fun surface_type=(flag: Int) do insert_attrib_with_val(0x3033, flag)
        fun surface_type_egl do surface_type = 4
 
+       # Set which client rendering APIs are supported
+       fun renderable_type=(flag: Int) do insert_attrib_with_val(0x3040, flag)
+
+       # Set EGL as the only supported rendering API
+       fun renderable_type_egl do renderable_type = 4
+
        fun blue_size=(size: Int) do insert_attrib_with_val(0x3022, size)
        fun green_size=(size: Int) do insert_attrib_with_val(0x3023, size)
        fun red_size=(size: Int) do insert_attrib_with_val(0x3024, size)
index f6f10aa..64b7b49 100644 (file)
@@ -404,8 +404,8 @@ extern class GtkEntry `{GtkEntry *`}
                 return (GtkEntry *)gtk_entry_new();
        `}
 
-       fun text : String is extern import NativeString.to_s `{
-               return NativeString_to_s( (char *)gtk_entry_get_text( recv ) );
+       fun text : String is extern import NativeString.to_s_with_copy `{
+               return NativeString_to_s_with_copy( (char *)gtk_entry_get_text( recv ) );
        `}
 
        fun text=( value : String) is extern import String.to_cstring`{
index cd6c59d..58864e4 100644 (file)
@@ -85,6 +85,11 @@ interface KeyEvent
 
        # Get Char value of key, if any
        fun to_c: nullable Char is abstract
+
+       # Name of the key that raised `self`
+       #
+       # Use mainly for debug since it is implementation dependent.
+       fun name: String is abstract
 end
 
 # Mobile hardware (or pseudo hardware) event
index 6396ec2..ecbbf9f 100644 (file)
@@ -86,6 +86,17 @@ class HashMap2[K1, K2, V]
                end
                level2[k2] = v
        end
+
+       # Remove the item at `k1` and `k2`
+       fun remove_at(k1: K1, k2: K2)
+       do
+               var level1 = self.level1
+
+               if not level1.has_key(k1) then return
+
+               var level2 = level1[k1]
+               level2.keys.remove(k2)
+       end
 end
 
 # Simple way to store an `HashMap[K1, HashMap[K2, HashMap[K3, V]]]`
@@ -123,6 +134,17 @@ class HashMap3[K1, K2, K3, V]
                end
                level2[k2, k3] = v
        end
+
+       # Remove the item at `k1`, `k2` and `k3`
+       fun remove_at(k1: K1, k2: K2, k3: K3)
+       do
+               var level1 = self.level1
+
+               if not level1.has_key(k1) then return
+
+               var level2 = level1[k1]
+               level2.remove_at(k2, k3)
+       end
 end
 
 # A map with a default value.
index 41ddc5b..6de6d2d 100644 (file)
@@ -181,6 +181,12 @@ extern class SDLDisplay `{SDL_Surface *`}
        fun ignore_mouse_motion_events=(val: Bool) `{
                SDL_EventState(SDL_MOUSEMOTION, val? SDL_IGNORE: SDL_ENABLE);
        `}
+
+       # Does `self` has the mouse focus?
+       fun mouse_focus: Bool `{ return SDL_GetAppState() & SDL_APPMOUSEFOCUS; `}
+
+       # Does `self` has the input focus?
+       fun input_focus: Bool `{ return SDL_GetAppState() & SDL_APPINPUTFOCUS; `}
 end
 
 # Basic Drawing figures
@@ -369,27 +375,27 @@ class SDLKeyEvent
        super KeyEvent
        super SDLInputEvent
 
-       var key_name: String
+       redef var name
        var down: Bool
 
        init (key_name: String, down: Bool)
        do
-               self.key_name = key_name
+               self.name = key_name
                self.down = down
        end
 
        redef fun to_c: nullable Char
        do
-               if key_name.length == 1 then return key_name.chars.first
+               if name.length == 1 then return name.chars.first
                return null
        end
 
        redef fun to_s
        do
                if down then
-                       return "KeyboardEvent key {key_name} down"
+                       return "KeyboardEvent key {name} down"
                else
-                       return "KeyboardEvent key {key_name} up"
+                       return "KeyboardEvent key {name} up"
                end
        end
 
@@ -397,13 +403,13 @@ class SDLKeyEvent
        redef fun is_down do return down
 
        # Return true if the key is the up arrow
-       redef fun is_arrow_up do return key_name == "up"
+       redef fun is_arrow_up do return name == "up"
        # Return true if the key is the left arrow
-       redef fun is_arrow_left do return key_name == "left"
+       redef fun is_arrow_left do return name == "left"
        # Return true if the key is the down arrow
-       redef fun is_arrow_down do return key_name == "down"
+       redef fun is_arrow_down do return name == "down"
        # Return true if the key is the right arrow
-       redef fun is_arrow_right do return key_name == "right"
+       redef fun is_arrow_right do return name == "right"
 end
 
 class SDLQuitEvent
index ebd2277..1790f48 100644 (file)
@@ -31,11 +31,13 @@ class CLanguage
        redef fun compile_module_block(block, ecc, mmodule)
        do
                if block.is_c_header then
-                       ecc.header_custom.add( block.location.as_line_pragma )
-                       ecc.header_custom.add( block.code )
+                       ecc.header_custom.add block.location.as_line_pragma
+                       ecc.header_custom.add "\n"
+                       ecc.header_custom.add block.code
                else if block.is_c_body then
-                       ecc.body_custom.add( block.location.as_line_pragma )
-                       ecc.body_impl.add( block.code )
+                       ecc.body_impl.add block.location.as_line_pragma
+                       ecc.body_impl.add "\n"
+                       ecc.body_impl.add block.code
                end
        end