lib: intro of the GTK module
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 19 Feb 2013 20:31:07 +0000 (15:31 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 4 Apr 2013 13:11:00 +0000 (09:11 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/gtk/gtk.nit [new file with mode: 0644]
lib/gtk/gtk.nit.args [new file with mode: 0644]
tests/nitg-e.skip
tests/nitg-s.skip
tests/nitg.skip
tests/niti.skip
tests/sav/test_gtk.sav [new file with mode: 0644]
tests/test_gtk.nit [new file with mode: 0644]

diff --git a/lib/gtk/gtk.nit b/lib/gtk/gtk.nit
new file mode 100644 (file)
index 0000000..75ecce1
--- /dev/null
@@ -0,0 +1,159 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2013 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
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Classes and services to use libGTK widgets
+module gtk
+
+in "C Header" `{
+       #include <gtk/gtk.h>
+`}
+
+`{
+               /* callback user data */
+               typedef struct {
+                       GtkCallable to_call;
+                       nullable_Object user_data;
+               } NitGtkSignal;
+
+               void nit_gtk_callback_func( GtkWidget *widget,
+                                                       gpointer   callback_data ) {
+                       NitGtkSignal *data;
+
+                       data = (NitGtkSignal*)callback_data;
+
+                       GtkCallable_signal( data->to_call, widget, data->user_data );
+               }
+`}
+
+interface GtkCallable
+       # return true to stop event processing, false to let it propagate
+       fun signal( sender : GtkWidget, user_data : nullable Object ) is abstract
+end
+
+extern GdkEvent `{GdkEvent *`}
+end
+
+extern GtkWidget `{GtkWidget *`}
+       fun show_all is extern `{ gtk_widget_show_all( recv ); `}
+       fun signal_connect( signal_name : String, to_call : GtkCallable, user_data : nullable Object ) is extern import String::to_cstring, GtkCallable::signal, Object as not nullable `{
+               NitGtkSignal *data = malloc( sizeof(NitGtkSignal) );
+
+               GtkCallable_incr_ref( to_call );
+               Object_incr_ref( user_data );
+
+               data->to_call = to_call;
+               data->user_data = user_data;
+
+               g_signal_connect( recv,
+                                                       String_to_cstring( signal_name ),
+                                                       G_CALLBACK(nit_gtk_callback_func),
+                                                       data );
+       `}
+
+       redef fun == ( o ) do return o isa GtkWidget and equal_to_gtk_widget( o )
+
+       private fun equal_to_gtk_widget( o : GtkWidget ) : Bool `{
+               return recv == o;
+       `}
+
+       fun set_size_request( width, height : Int ) `{
+               gtk_widget_set_size_request( recv, width, height );
+       `}
+end
+
+extern GtkContainer `{GtkContainer*`}
+       super GtkWidget
+
+       fun add( widget : GtkWidget ) is extern `{
+               gtk_container_add( recv, widget );
+       `}
+end
+
+extern GtkGrid `{GtkGrid*`}
+       super GtkContainer
+
+       new ( rows : Int, columns : Int, homogeneous : Bool ) `{
+               return (GtkGrid*)gtk_grid_new();
+       `}
+       fun attach( child : GtkWidget, left, top, width, height : Int ) `{
+               gtk_grid_attach( recv, child, left, top, width, height );
+       `}
+end
+
+extern GtkWindow `{GtkWindow*`}
+       super GtkContainer
+
+       new ( flag : Int ) is extern `{
+               GtkWindow *win;
+
+               win = GTK_WINDOW(gtk_window_new( flag ));
+               g_signal_connect(win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
+               return win;
+       `}
+
+       fun title=( title : String ) is extern import String::to_cstring `{
+               gtk_window_set_title( recv, String_to_cstring( title ) );
+       `}
+
+       fun signal_close_connect( to_call : GtkCallable,
+                                                         user_data : nullable Object )
+       do
+               signal_connect( "close", to_call, user_data )
+       end
+end
+
+extern GtkColorSelectionDialog
+       super GtkWidget
+       new ( title : String, parent : GtkWindow ) is extern  import String::to_cstring `{
+                return gtk_color_chooser_dialog_new( String_to_cstring( title ), parent );
+       `}
+end
+
+extern GtkLabel `{GtkLabel*`}
+       super GtkWidget
+
+       new ( text : String ) is extern import String::to_cstring `{
+               return (GtkLabel*)gtk_label_new( String_to_cstring( text ) );
+       `}
+
+       fun text=( text : String ) import String::to_cstring `{
+               gtk_label_set_text( recv, String_to_cstring( text ) );
+       `}
+
+       fun text : String import String::from_cstring `{
+               return new_String_from_cstring( (char*)gtk_label_get_text( recv ) );
+       `}
+end
+
+extern GtkButton
+       super GtkWidget
+
+       new is extern `{
+               return gtk_button_new(  );
+       `}
+       new with_label( text : String ) is extern import String::to_cstring `{
+               return gtk_button_new_with_label( String_to_cstring( text ) );
+       `}
+       new from_stock( stock_id : String ) is extern import String::to_cstring `{
+               return gtk_button_new_from_stock( String_to_cstring( stock_id ) );
+       `}
+end
+
+redef interface Object
+       protected fun init_gtk `{ gtk_init( 0, NULL ); `}
+       protected fun run_gtk `{ gtk_main(); `}
+       protected fun quit_gtk `{ gtk_main_quit(); `}
+end
diff --git a/lib/gtk/gtk.nit.args b/lib/gtk/gtk.nit.args
new file mode 100644 (file)
index 0000000..9df1550
--- /dev/null
@@ -0,0 +1 @@
+--cc-lib-name gtk-3 --cc-lib-name gdk-3 --cc-lib-name atk-1.0 --cc-lib-name gio-2.0 --cc-lib-name pangocairo-1.0 --cc-lib-name gdk_pixbuf-2.0 --cc-lib-name cairo-gobject --cc-lib-name pango-1.0 --cc-lib-name cairo --cc-lib-name gobject-2.0 --cc-lib-name glib-2.0 --cc-header-path /usr/include/gtk-3.0 --cc-header-path /usr/include/atk-1.0 --cc-header-path /usr/include/at-spi2-atk/2.0 --cc-header-path /usr/include/pango-1.0 --cc-header-path /usr/include/gio-unix-2.0/ --cc-header-path /usr/include/cairo --cc-header-path /usr/include/gdk-pixbuf-2.0 --cc-header-path /usr/include/glib-2.0 --cc-header-path /usr/lib/x86_64-linux-gnu/glib-2.0/include --cc-header-path /usr/include/freetype2 --cc-header-path /usr/include/pixman-1 --cc-header-path /usr/include/libpng12  
index 41116ef..fbbfcd4 100644 (file)
@@ -5,6 +5,7 @@ inline
 test_extern
 test_ni_
 test_ffi_
+test_gtk
 nitc
 nitdoc
 nits
index 41116ef..fbbfcd4 100644 (file)
@@ -5,6 +5,7 @@ inline
 test_extern
 test_ni_
 test_ffi_
+test_gtk
 nitc
 nitdoc
 nits
index 41116ef..fbbfcd4 100644 (file)
@@ -5,6 +5,7 @@ inline
 test_extern
 test_ni_
 test_ffi_
+test_gtk
 nitc
 nitdoc
 nits
index 3a2705b..7396ba9 100644 (file)
@@ -6,6 +6,7 @@ test_ni_
 test_ffi_
 test_math
 test_mem
+test_gtk
 shoot_logic
 bench_
 nit_args1
diff --git a/tests/sav/test_gtk.sav b/tests/sav/test_gtk.sav
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/test_gtk.nit b/tests/test_gtk.nit
new file mode 100644 (file)
index 0000000..290d4ef
--- /dev/null
@@ -0,0 +1,73 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2011-2013 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
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import gtk
+
+class MyApp
+       super GtkCallable
+
+       var win : GtkWindow
+
+       var container : GtkContainer
+
+       var lbl : GtkLabel
+       var but_ok : GtkButton
+       var but_cancel : GtkButton
+
+       redef fun signal( sender, user_data )
+       do
+               if user_data != null then print "Userdata: {user_data}"
+
+               if sender == but_ok then
+                       print "ok!"
+                       quit_gtk
+               else if sender == but_cancel then
+                       print "cancel!"
+                       quit_gtk
+               else
+                       print sender
+               end
+               
+       end
+
+       init
+       do
+               init_gtk
+
+               win = new GtkWindow( 0 )
+
+               container = new GtkGrid(2,1,true)
+               win.add( container )
+
+               lbl = new GtkLabel( "Hello world" )
+               container.add( lbl )
+
+               but_ok = new GtkButton.from_stock( "OK" )
+               but_ok.signal_connect( "clicked", self, "ok" )
+               container.add( but_ok )
+
+               but_cancel = new GtkButton.from_stock( "Cancel" )
+               but_cancel.signal_connect( "clicked", self, null )
+               container.add( but_cancel )
+
+               win.show_all
+       end
+end
+
+if "NIT_TESTING".environ != "true" then
+       var app = new MyApp
+       run_gtk
+end