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
)
145 fun resizable
: Bool is extern `{
146 return gtk_window_get_resizable( recv );
149 fun resizable
=( is_resizable
: Bool) is extern `{
150 return gtk_window_set_resizable( recv, is_resizable );
153 #Activates the current focused widget within the window.
154 #returns TRUE if a widget got activated.
155 fun activate_focus
: Bool is extern `{
156 return gtk_window_activate_focus( recv );
159 #Sets a window modal or non-modal. Modal windows prevent interaction with other windows in the same application.
160 fun modal
=( is_modal
: Bool ) is extern `{
161 gtk_window_set_modal( recv, is_modal );
164 #Windows can't actually be 0x0 in size, they must be at least 1x1
165 #but passing 0 for width and height is OK, resulting in a 1x1 default size.
166 #params width in pixels, or -1 to unset the default width
167 #params height in pixels, or -1 to unset the default height
168 fun default_size
( width
: Int, height
: Int ) is extern `{
169 gtk_window_set_default_size( recv, width, height );
172 #Activates the default widget for the window
173 #unless the current focused widget has been configured to receive the default action (see gtk_widget_set_receives_default()), in which case the focused widget is activated.
174 fun activate_default
: Bool is extern `{
175 return gtk_window_activate_default( recv );
178 fun active
: Bool is extern `{
179 return gtk_window_is_active( recv );
182 #Returns whether the input focus is within this GtkWindow. For real toplevel windows, this is identical to gtk_window_is_active(), but for embedded windows, like GtkPlug, the results will differ.
183 fun has_toplevel_focus
: Bool is extern `{
184 return gtk_window_has_toplevel_focus( recv );
187 fun get_focus
: GtkWidget is extern `{
188 return gtk_window_get_focus( recv );
191 fun set_focus
( widget
: GtkWidget ) is extern `{
192 return gtk_window_set_focus( recv, widget );
195 fun get_default_widget
: GtkWidget is extern `{
196 return gtk_window_get_default_widget( recv );
199 fun set_default_widget
( widget
: GtkWidget ) is extern `{
200 return gtk_window_set_default( recv, widget );
203 fun maximize
is extern `{
204 return gtk_window_maximize( recv );
207 fun unmaximize
is extern `{
208 return gtk_window_unmaximize( recv );
211 fun fullscreen
is extern `{
212 return gtk_window_fullscreen( recv );
215 fun unfullscreen
is extern `{
216 return gtk_window_unfullscreen( recv );
219 fun keep_above
=( setting
: Bool ) is extern `{
220 gtk_window_set_keep_above( recv, setting );
223 fun keep_below
=( setting
: Bool ) is extern `{
224 gtk_window_set_keep_below( recv, setting );
228 #Base class for widgets with alignments and padding
229 #@https://developer.gnome.org/gtk3/3.2/GtkMisc.html
230 extern GtkMisc `{GtkMisc *`}
233 fun alignment : GtkAlignment is abstract
235 fun alignment=( x : Float, y : Float ) is extern `{
236 gtk_misc_set_alignment
( recv
, x
, y
);
239 fun padding : GtkAlignment is abstract
241 fun padding=( x : Float, y : Float ) is extern `{
242 gtk_misc_set_padding
( recv
, x
, y
);
247 extern GtkColorSelectionDialog
249 new ( title : String, parent : GtkWindow ) is extern import String::to_cstring `{
250 return gtk_color_chooser_dialog_new
( String_to_cstring( title
), parent
);
254 #A widget that displays a small to medium amount of text
255 #@https://developer.gnome.org/gtk3/3.2/GtkLabel.html
256 extern GtkLabel `{GtkLabel *`}
259 # Create a GtkLabel with text
260 new ( text
: String ) is extern import String::to_cstring
`{
261 return (GtkLabel*)gtk_label_new( String_to_cstring( text ) );
264 # Set the text of the label
265 fun text
=( text
: String ) import String::to_cstring
`{
266 gtk_label_set_text( recv, String_to_cstring( text ) );
269 # Returns the text of the label
270 fun text
: String import String::from_cstring
`{
271 return new_String_from_cstring( (char*)gtk_label_get_text( recv ) );
274 # Sets the angle of rotation for the label.
275 # An angle of 90 reads from from bottom to top, an angle of 270, from top to bottom.
276 fun angle
=( degre
: Float ) `{
277 gtk_label_set_angle( recv, degre );
280 # Returns the angle of rotation for the label.
282 return gtk_label_get_angle( recv );
287 #A widget displaying an image
288 #@https://developer.gnome.org/gtk3/3.2/GtkImage.html
289 extern GtkImage `{GtkImage *`}
294 return (GtkImage*)gtk_image_new
( );
297 # Create a GtkImage with text
298 new file( filename : String ) is extern import String::to_cstring `{
299 return (GtkImage*)gtk_image_new_from_file
( String_to_cstring( filename
) );
302 fun pixel_size : Int is extern `{
303 return gtk_image_get_pixel_size
( recv
);
306 fun pixel_size=( size : Int) is extern `{
307 gtk_image_set_pixel_size
( recv
, size
);
310 fun clear is extern `{
311 gtk_image_clear
( recv
);
316 #Describes the image data representation used by a GtkImage.
317 #@https://developer.gnome.org/gtk3/3.2/GtkImage.html#GtkImageType
318 extern GtkImageType `{GtkImageType`}
319 # There is no image displayed by the widget.
320 new empty
`{ return GTK_IMAGE_EMPTY; `}
322 # The widget contains a GdkPixbuf.
323 new pixbuf `{ return GTK_IMAGE_PIXBUF; `}
325 # The widget contains a stock icon name.
326 new stock
`{ return GTK_IMAGE_STOCK; `}
328 # The widget contains a GtkIconSet.
329 new icon_set `{ return GTK_IMAGE_ICON_SET; `}
331 # The widget contains a GdkPixbufAnimation.
332 new animation
`{ return GTK_IMAGE_ANIMATION; `}
334 # The widget contains a named icon.
335 new icon_name `{ return GTK_IMAGE_ICON_NAME; `}
337 # The widget contains a GIcon.
338 new gicon
`{ return GTK_IMAGE_GICON; `}
342 #@https://developer.gnome.org/gtk3/3.2/GtkArrow.html
343 #extern GtkArrow `{GtkArrow *`}
346 # new ( arrow_type : GtkArrowType, shadow_type : GtkShadowType ) is extern `{
347 # return (GtkArrow *)gtk_arrow_new( arrow_type, shadow_type );
350 # fun set( arrow_type : GtkArrowType, shadow_type : GtkShadowType ) is extern `{
351 # gtk_arrow_set( recv, arrow_type, shadow_type );
355 #A widget that emits a signal when clicked on
356 #@https://developer.gnome.org/gtk3/stable/GtkButton.html
357 extern GtkButton `{GtkButton *`}
361 return (GtkButton *)gtk_button_new
( );
364 #Create a GtkButton with text
365 new with_label( text : String ) is extern import String::to_cstring `{
366 return (GtkButton *)gtk_button_new_with_label
( String_to_cstring( text
) );
369 new from_stock( stock_id : String ) is extern import String::to_cstring `{
370 return (GtkButton *)gtk_button_new_from_stock
( String_to_cstring( stock_id
) );
373 fun text : String is extern `{
374 return new_String_from_cstring
( (char
*)gtk_button_get_label
( recv
) );
377 fun text=( value : String ) is extern import String::to_cstring`{
378 gtk_button_set_label
( recv
, String_to_cstring( value
) );
381 fun on_click( to_call : GtkCallable, user_data : nullable Object ) do
382 signal_connect( "clicked", to_call, user_data )
387 #Show a spinner animation
388 #@https://developer.gnome.org/gtk3/3.2/GtkSpinner.html
389 extern GtkSpinner `{GtkSpinner *`}
393 return (GtkSpinner *)gtk_spinner_new();
396 fun start
is extern `{
397 return gtk_spinner_start( recv );
400 fun stop
is extern `{
401 return gtk_spinner_stop( recv );
405 #A "light switch" style toggle
406 #@https://developer.gnome.org/gtk3/3.2/GtkSwitch.html
407 extern GtkSwitch `{GtkSwitch *`}
411 return (GtkSwitch *)gtk_switch_new
();
414 fun active : Bool is extern `{
415 return gtk_switch_get_active
( recv
);
418 fun active=( is_active : Bool ) is extern `{
419 return gtk_switch_set_active
( recv
, is_active
);
424 #A widget which controls the alignment and size of its child
425 #https://developer.gnome.org/gtk3/stable/GtkAlignment.html
426 extern GtkAlignment `{GtkAlignment *`}
429 new ( xalign
: Float, yalign
: Float, xscale
: Float, yscale
: Float ) is extern `{
430 return (GtkAlignment *)gtk_alignment_new( xalign, yalign, xscale, yscale );
433 fun set
( xalign
: Float, yalign
: Float, xscale
: Float, yscale
: Float ) is extern `{
434 gtk_alignment_set( recv, xalign, yalign, xscale, yscale );