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 ) {
34 data = (NitGtkSignal*)callback_data;
35 GtkCallable_signal( data->to_call, widget, data->user_data );
39 redef interface Object
40 protected fun init_gtk
`{ gtk_init( 0, NULL ); `}
41 protected fun run_gtk `{ gtk_main(); `}
42 protected fun quit_gtk
`{ gtk_main_quit(); `}
46 # return true to stop event processing, false to let it propagate
47 fun signal( sender : GtkWidget, user_data : nullable Object ) is abstract
50 extern GdkEvent `{GdkEvent *`}
54 #Base class for all widgets
55 #@https://developer.gnome.org/gtk3/stable/GtkWidget.html
56 extern GtkWidget `{GtkWidget *`}
57 fun show_all is extern `{ gtk_widget_show_all( recv ); `}
59 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 `{
60 NitGtkSignal *data = malloc( sizeof(NitGtkSignal) );
62 GtkCallable_incr_ref( to_call );
63 Object_incr_ref( user_data );
65 data->to_call = to_call;
66 data->user_data = user_data;
68 /*Use G_CALLBACK() to cast the callback function to a GCallback*/
69 g_signal_connect( recv,
70 String_to_cstring( signal_name ),
71 G_CALLBACK(nit_gtk_callback_func),
75 redef fun == ( o
) do return o
isa GtkWidget and equal_to_gtk_widget
( o
)
77 private fun equal_to_gtk_widget
( o
: GtkWidget ) : Bool `{
81 fun request_size
( width
, height
: Int ) `{
82 gtk_widget_set_size_request( recv, width, height );
86 #Base class for widgets which contain other widgets
87 #@https://developer.gnome.org/gtk3/stable/GtkContainer.html
88 extern GtkContainer `{GtkContainer *`}
91 fun add( widget : GtkWidget ) is extern `{
92 gtk_container_add
( recv
, widget
);
96 #A container with just one child
97 #@https://developer.gnome.org/gtk3/stable/GtkBin.html
98 extern GtkBin `{GtkBin *`}
101 fun child
: GtkWidget is extern `{
102 return gtk_bin_get_child( recv );
106 #Pack widgets in a rows and columns
107 #@https://developer.gnome.org/gtk3/3.3/GtkGrid.html
108 extern GtkGrid `{GtkGrid *`}
111 # Create a grid with a fixed number of rows and columns
112 new ( rows : Int, columns : Int, homogeneous : Bool ) `{
113 return (GtkGrid*)gtk_grid_new
(); // rows
, columns
, homogeneous
);
116 # Set a widget child inside the grid at a given position
117 fun attach( child : GtkWidget, left, top, width, height : Int ) `{
118 gtk_grid_attach
( recv
, child
, left
, top
, width
, height
);
122 #Toplevel which can contain other widgets
123 #@https://developer.gnome.org/gtk3/stable/GtkWindow.html
124 extern GtkWindow `{GtkWindow *`}
127 new ( flag
: Int ) is extern `{
130 win = GTK_WINDOW(gtk_window_new( flag ));
131 g_signal_connect(win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
135 fun title
=( title
: String ) is extern import String::to_cstring
`{
136 gtk_window_set_title( recv, String_to_cstring( title ) );
139 #The "destroy" signal is emitted when a widget is destroyed, either by explicitly calling gtk_widget_destroy() or when the widget is unparented. Top-level GtkWindows are also destroyed when the Close window control button is clicked.
140 fun on_close
( to_call
: GtkCallable, user_data
: nullable Object )
142 signal_connect
( "destroy", to_call
, user_data
)
146 extern GtkColorSelectionDialog
148 new ( title
: String, parent
: GtkWindow ) is extern import String::to_cstring
`{
149 return gtk_color_chooser_dialog_new( String_to_cstring( title ), parent );
153 #A widget that displays a small to medium amount of text
154 #@https://developer.gnome.org/gtk3/3.2/GtkLabel.html
155 extern GtkLabel `{GtkLabel*`}
158 # Create a GtkLabel with text
159 new ( text : String ) is extern import String::to_cstring `{
160 return (GtkLabel*)gtk_label_new
( String_to_cstring( text
) );
163 # Set the text of the label
164 fun text=( text : String ) import String::to_cstring `{
165 gtk_label_set_text
( recv
, String_to_cstring( text
) );
168 # Returns the text of the label
169 fun text : String import String::from_cstring `{
170 return new_String_from_cstring
( (char
*)gtk_label_get_text
( recv
) );
174 #A widget that emits a signal when clicked on
175 #@https://developer.gnome.org/gtk3/stable/GtkButton.html
180 return gtk_button_new
( );
183 #Create a GtkButton with text
184 new with_label( text : String ) is extern import String::to_cstring `{
185 return gtk_button_new_with_label
( String_to_cstring( text
) );
188 new from_stock( stock_id : String ) is extern import String::to_cstring `{
189 return gtk_button_new_from_stock
( String_to_cstring( stock_id
) );