1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Classes and services to use libGTK widgets
25 /* callback user data */
28 nullable_Object user_data;
31 void nit_gtk_callback_func( GtkWidget *widget,
32 gpointer callback_data ) {
35 data = (NitGtkSignal*)callback_data;
37 GtkCallable_signal( data->to_call, widget, data->user_data );
42 # return true to stop event processing, false to let it propagate
43 fun signal
( sender
: GtkWidget, user_data
: nullable Object ) is abstract
46 extern GdkEvent `{GdkEvent *`}
49 extern GtkWidget `{GtkWidget *`}
50 fun show_all
is extern `{ gtk_widget_show_all( recv ); `}
51 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 `{
52 NitGtkSignal *data
= malloc
( sizeof
(NitGtkSignal) );
54 GtkCallable_incr_ref( to_call
);
55 Object_incr_ref( user_data
);
57 data-
>to_call
= to_call
;
58 data-
>user_data
= user_data
;
60 g_signal_connect
( recv
,
61 String_to_cstring( signal_name
),
62 G_CALLBACK(nit_gtk_callback_func
),
66 redef fun == ( o ) do return o isa GtkWidget and equal_to_gtk_widget( o )
68 private fun equal_to_gtk_widget( o : GtkWidget ) : Bool `{
72 fun set_size_request( width, height : Int ) `{
73 gtk_widget_set_size_request
( recv
, width
, height
);
77 extern GtkContainer `{GtkContainer*`}
80 fun add
( widget
: GtkWidget ) is extern `{
81 gtk_container_add( recv, widget );
85 extern GtkGrid `{GtkGrid*`}
88 new ( rows : Int, columns : Int, homogeneous : Bool ) `{
89 return (GtkGrid*)gtk_grid_new
();
91 fun attach( child : GtkWidget, left, top, width, height : Int ) `{
92 gtk_grid_attach
( recv
, child
, left
, top
, width
, height
);
96 extern GtkWindow `{GtkWindow*`}
99 new ( flag
: Int ) is extern `{
102 win = GTK_WINDOW(gtk_window_new( flag ));
103 g_signal_connect(win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
107 fun title
=( title
: String ) is extern import String::to_cstring
`{
108 gtk_window_set_title( recv, String_to_cstring( title ) );
111 fun signal_close_connect
( to_call
: GtkCallable,
112 user_data
: nullable Object )
114 signal_connect
( "close", to_call
, user_data
)
118 extern GtkColorSelectionDialog
120 new ( title
: String, parent
: GtkWindow ) is extern import String::to_cstring
`{
121 return gtk_color_chooser_dialog_new( String_to_cstring( title ), parent );
125 extern GtkLabel `{GtkLabel*`}
128 new ( text : String ) is extern import String::to_cstring `{
129 return (GtkLabel*)gtk_label_new
( String_to_cstring( text
) );
132 fun text=( text : String ) import String::to_cstring `{
133 gtk_label_set_text
( recv
, String_to_cstring( text
) );
136 fun text : String import String::from_cstring `{
137 return new_String_from_cstring
( (char
*)gtk_label_get_text
( recv
) );
145 return gtk_button_new
( );
147 new with_label( text : String ) is extern import String::to_cstring `{
148 return gtk_button_new_with_label
( String_to_cstring( text
) );
150 new from_stock( stock_id : String ) is extern import String::to_cstring `{
151 return gtk_button_new_from_stock
( String_to_cstring( stock_id
) );
155 redef interface Object
156 protected fun init_gtk `{ gtk_init( 0, NULL ); `}
157 protected fun run_gtk
`{ gtk_main(); `}
158 protected fun quit_gtk `{ gtk_main_quit(); `}