7324609341bf5312e40aa2957bb5544b5e61cc49
[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 end
145
146 extern GtkColorSelectionDialog
147 super GtkWidget
148 new ( title : String, parent : GtkWindow ) is extern import String::to_cstring `{
149 return gtk_color_chooser_dialog_new( String_to_cstring( title ), parent );
150 `}
151 end
152
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*`}
156 super GtkWidget
157
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 ) );
161 `}
162
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 ) );
166 `}
167
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 ) );
171 `}
172 end
173
174 #A widget that emits a signal when clicked on
175 #@https://developer.gnome.org/gtk3/stable/GtkButton.html
176 extern GtkButton
177 super GtkWidget
178
179 new is extern `{
180 return gtk_button_new( );
181 `}
182
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 ) );
186 `}
187
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 ) );
190 `}
191 end
192