gtk: new widgets and base classe Misc
[nit.git] / lib / gtk / gtk.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Classes and services to use libGTK widgets
18 module gtk
19
20 in "C Header" `{
21 #include <gtk/gtk.h>
22 `}
23
24 `{
25 /* callback user data */
26 typedef struct {
27 GtkCallable to_call;
28 nullable_Object user_data;
29 } NitGtkSignal;
30
31 void nit_gtk_callback_func( GtkWidget *widget,
32 gpointer callback_data ) {
33 NitGtkSignal *data;
34 data = (NitGtkSignal*)callback_data;
35 GtkCallable_signal( data->to_call, widget, data->user_data );
36 }
37 `}
38
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(); `}
43 end
44
45 interface GtkCallable
46 # return true to stop event processing, false to let it propagate
47 fun signal( sender : GtkWidget, user_data : nullable Object ) is abstract
48 end
49
50 extern GdkEvent `{GdkEvent *`}
51 end
52
53
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 ); `}
58
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) );
61
62 GtkCallable_incr_ref( to_call );
63 Object_incr_ref( user_data );
64
65 data->to_call = to_call;
66 data->user_data = user_data;
67
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),
72 data );
73 `}
74
75 redef fun == ( o ) do return o isa GtkWidget and equal_to_gtk_widget( o )
76
77 private fun equal_to_gtk_widget( o : GtkWidget ) : Bool `{
78 return recv == o;
79 `}
80
81 fun request_size( width, height : Int ) `{
82 gtk_widget_set_size_request( recv, width, height );
83 `}
84 end
85
86 #Base class for widgets which contain other widgets
87 #@https://developer.gnome.org/gtk3/stable/GtkContainer.html
88 extern GtkContainer `{GtkContainer *`}
89 super GtkWidget
90
91 fun add( widget : GtkWidget ) is extern `{
92 gtk_container_add( recv, widget );
93 `}
94 end
95
96 #A container with just one child
97 #@https://developer.gnome.org/gtk3/stable/GtkBin.html
98 extern GtkBin `{GtkBin *`}
99 super GtkContainer
100
101 fun child : GtkWidget is extern `{
102 return gtk_bin_get_child( recv );
103 `}
104 end
105
106 #Pack widgets in a rows and columns
107 #@https://developer.gnome.org/gtk3/3.3/GtkGrid.html
108 extern GtkGrid `{GtkGrid *`}
109 super GtkContainer
110
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 );
114 `}
115
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 );
119 `}
120 end
121
122 #Toplevel which can contain other widgets
123 #@https://developer.gnome.org/gtk3/stable/GtkWindow.html
124 extern GtkWindow `{GtkWindow *`}
125 super GtkBin
126
127 new ( flag : Int ) is extern `{
128 GtkWindow *win;
129
130 win = GTK_WINDOW(gtk_window_new( flag ));
131 g_signal_connect(win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
132 return win;
133 `}
134
135 fun title=( title : String ) is extern import String::to_cstring `{
136 gtk_window_set_title( recv, String_to_cstring( title ) );
137 `}
138
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 )
141 do
142 signal_connect( "destroy", to_call, user_data )
143 end
144
145 fun resizable : Bool is extern `{
146 return gtk_window_get_resizable( recv );
147 `}
148
149 fun resizable=( is_resizable : Bool) is extern `{
150 return gtk_window_set_resizable( recv, is_resizable );
151 `}
152
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 );
157 `}
158
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 );
162 `}
163
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 );
170 `}
171
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 );
176 `}
177
178 fun active : Bool is extern `{
179 return gtk_window_is_active( recv );
180 `}
181
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 );
185 `}
186
187 fun get_focus : GtkWidget is extern `{
188 return gtk_window_get_focus( recv );
189 `}
190
191 fun set_focus( widget : GtkWidget ) is extern `{
192 return gtk_window_set_focus( recv, widget );
193 `}
194
195 fun get_default_widget : GtkWidget is extern `{
196 return gtk_window_get_default_widget( recv );
197 `}
198
199 fun set_default_widget( widget : GtkWidget ) is extern `{
200 return gtk_window_set_default( recv, widget );
201 `}
202
203 fun maximize is extern `{
204 return gtk_window_maximize( recv );
205 `}
206
207 fun unmaximize is extern `{
208 return gtk_window_unmaximize( recv );
209 `}
210
211 fun fullscreen is extern `{
212 return gtk_window_fullscreen( recv );
213 `}
214
215 fun unfullscreen is extern `{
216 return gtk_window_unfullscreen( recv );
217 `}
218
219 fun keep_above=( setting : Bool ) is extern `{
220 gtk_window_set_keep_above( recv, setting );
221 `}
222
223 fun keep_below=( setting : Bool ) is extern `{
224 gtk_window_set_keep_below( recv, setting );
225 `}
226 end
227
228 #Base class for widgets with alignments and padding
229 #@https://developer.gnome.org/gtk3/3.2/GtkMisc.html
230 extern GtkMisc `{GtkMisc *`}
231 super GtkWidget
232
233 fun alignment : GtkAlignment is abstract
234
235 fun alignment=( x : Float, y : Float ) is extern `{
236 gtk_misc_set_alignment( recv, x, y );
237 `}
238
239 fun padding : GtkAlignment is abstract
240
241 fun padding=( x : Float, y : Float ) is extern `{
242 gtk_misc_set_padding( recv, x, y );
243 `}
244
245 end
246
247 extern GtkColorSelectionDialog
248 super GtkWidget
249 new ( title : String, parent : GtkWindow ) is extern import String::to_cstring `{
250 return gtk_color_chooser_dialog_new( String_to_cstring( title ), parent );
251 `}
252 end
253
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 *`}
257 super GtkMisc
258
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 ) );
262 `}
263
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 ) );
267 `}
268
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 ) );
272 `}
273
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 );
278 `}
279
280 # Returns the angle of rotation for the label.
281 fun angle : Float `{
282 return gtk_label_get_angle( recv );
283 `}
284
285 end
286
287 #A widget displaying an image
288 #@https://developer.gnome.org/gtk3/3.2/GtkImage.html
289 extern GtkImage `{GtkImage *`}
290 super GtkMisc
291
292 # Create a GtkImage
293 new is extern `{
294 return (GtkImage*)gtk_image_new( );
295 `}
296
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 ) );
300 `}
301
302 fun pixel_size : Int is extern `{
303 return gtk_image_get_pixel_size( recv );
304 `}
305
306 fun pixel_size=( size : Int) is extern `{
307 gtk_image_set_pixel_size( recv, size );
308 `}
309
310 fun clear is extern `{
311 gtk_image_clear( recv );
312 `}
313 end
314
315 #enum GtkImageType
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; `}
321
322 # The widget contains a GdkPixbuf.
323 new pixbuf `{ return GTK_IMAGE_PIXBUF; `}
324
325 # The widget contains a stock icon name.
326 new stock `{ return GTK_IMAGE_STOCK; `}
327
328 # The widget contains a GtkIconSet.
329 new icon_set `{ return GTK_IMAGE_ICON_SET; `}
330
331 # The widget contains a GdkPixbufAnimation.
332 new animation `{ return GTK_IMAGE_ANIMATION; `}
333
334 # The widget contains a named icon.
335 new icon_name `{ return GTK_IMAGE_ICON_NAME; `}
336
337 # The widget contains a GIcon.
338 new gicon `{ return GTK_IMAGE_GICON; `}
339 end
340
341 #Displays an arrow
342 #@https://developer.gnome.org/gtk3/3.2/GtkArrow.html
343 #extern GtkArrow `{GtkArrow *`}
344 # super GtkMisc
345 #
346 # new ( arrow_type : GtkArrowType, shadow_type : GtkShadowType ) is extern `{
347 # return (GtkArrow *)gtk_arrow_new( arrow_type, shadow_type );
348 # `}
349 #
350 # fun set( arrow_type : GtkArrowType, shadow_type : GtkShadowType ) is extern `{
351 # gtk_arrow_set( recv, arrow_type, shadow_type );
352 # `}
353 #end
354
355 #A widget that emits a signal when clicked on
356 #@https://developer.gnome.org/gtk3/stable/GtkButton.html
357 extern GtkButton `{GtkButton *`}
358 super GtkBin
359
360 new is extern `{
361 return (GtkButton *)gtk_button_new( );
362 `}
363
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 ) );
367 `}
368
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 ) );
371 `}
372
373 fun text : String is extern `{
374 return new_String_from_cstring( (char *)gtk_button_get_label( recv ) );
375 `}
376
377 fun text=( value : String ) is extern import String::to_cstring`{
378 gtk_button_set_label( recv, String_to_cstring( value ) );
379 `}
380
381 fun on_click( to_call : GtkCallable, user_data : nullable Object ) do
382 signal_connect( "clicked", to_call, user_data )
383 end
384
385 end
386
387 #Show a spinner animation
388 #@https://developer.gnome.org/gtk3/3.2/GtkSpinner.html
389 extern GtkSpinner `{GtkSpinner *`}
390 super GtkWidget
391
392 new is extern `{
393 return (GtkSpinner *)gtk_spinner_new();
394 `}
395
396 fun start is extern `{
397 return gtk_spinner_start( recv );
398 `}
399
400 fun stop is extern `{
401 return gtk_spinner_stop( recv );
402 `}
403 end
404
405 #A "light switch" style toggle
406 #@https://developer.gnome.org/gtk3/3.2/GtkSwitch.html
407 extern GtkSwitch `{GtkSwitch *`}
408 super GtkWidget
409
410 new is extern `{
411 return (GtkSwitch *)gtk_switch_new();
412 `}
413
414 fun active : Bool is extern `{
415 return gtk_switch_get_active( recv );
416 `}
417
418 fun active=( is_active : Bool ) is extern `{
419 return gtk_switch_set_active( recv, is_active );
420 `}
421 end
422
423
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 *`}
427 super GtkBin
428
429 new ( xalign : Float, yalign : Float, xscale : Float, yscale : Float ) is extern `{
430 return (GtkAlignment *)gtk_alignment_new( xalign, yalign, xscale, yscale );
431 `}
432
433 fun set ( xalign : Float, yalign : Float, xscale : Float, yscale : Float ) is extern `{
434 gtk_alignment_set( recv, xalign, yalign, xscale, yscale );
435 `}
436
437 #get_padding
438 #set_padding
439 end
440