1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Implementation of the app.nit UI module for GNU/Linux
24 redef fun setup
do gtk_init
26 # Single GTK window of this application
27 var native_window
: GtkWindow is lazy
do
28 var win
= new GtkWindow(new GtkWindowType.toplevel
)
29 win
.connect_destroy_signal_to_quit
30 win
.titlebar
= native_header_bar
36 var native_header_bar
: GtkHeaderBar is lazy
do
37 var bar
= new GtkHeaderBar
38 bar
.title
= "app.nit" # TODO offer a portable API to name windows
39 bar
.show_close_button
= true
41 # TODO add back button
46 # Root `GtkStack` used to simulate the many app.nit windows
47 var native_stack
: GtkStack is lazy
do
48 var stack
= new GtkStack
49 stack
.homogeneous
= false
53 # On GNU/Linux, we go through all the callbacks once,
54 # there is no complex life-cycle.
62 native_window
.show_all
71 # Spacing between GTK controls, default at 2
72 var control_spacing
= 2 is writable
74 redef fun window
=(window
)
76 var root_view
= window
.view
77 assert root_view
!= null
78 native_stack
.add root_view
.native
79 native_stack
.visible_child
= root_view
.native
88 # The GTK element used to implement `self`
89 fun native
: NATIVE is abstract
92 type NATIVE: GtkWidget
97 if not native
.address_is_null
then native
.destroy
101 redef class CompositeControl
104 # On GNU/Linux, a window is implemented by placing the `view` in a `GtkStack` in the single GTK window
107 # Root view of this window
108 var view
: nullable View = null
112 if view
isa View then
123 redef fun enabled
do return native
.sensitive
124 redef fun enabled
=(enabled
) do native
.sensitive
= enabled
or else true
128 redef type NATIVE: GtkBox
133 if item
isa View then native
.add item
.native
136 redef fun remove
(item
)
139 if item
isa View then native
.remove item
.native
143 redef class HorizontalLayout
144 redef var native
= new GtkBox(new GtkOrientation.horizontal
, app
.control_spacing
)
149 native
.homogeneous
= true
150 native
.set_child_packing
(item
.native
, true, true, 0, new GtkPackType.start
)
154 redef class VerticalLayout
155 redef var native
= new GtkBox(new GtkOrientation.vertical
, app
.control_spacing
)
161 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
162 native
.homogeneous
= true
163 native
.set_child_packing
(item
.native
, true, true, 0, new GtkPackType.start
)
167 redef class ListLayout
168 redef type NATIVE: GtkListBox
169 redef var native
= new GtkListBox
171 init do native
.selection_mode
= new GtkSelectionMode.none
175 redef type NATIVE: GtkButton
176 redef var native
= new GtkButton
178 redef fun text
do return native
.text
179 redef fun text
=(value
) do native
.text
= (value
or else "").to_s
181 redef fun signal
(sender
, data
) do notify_observers
new ButtonPressEvent(self)
183 init do native
.signal_connect
("clicked", self, null)
187 redef type NATIVE: GtkLabel
188 redef var native
= new GtkLabel("")
190 redef fun text
do return native
.text
191 redef fun text
=(value
) do native
.text
= (value
or else "").to_s
194 redef class TextInput
195 redef type NATIVE: GtkEntry
196 redef var native
= new GtkEntry
198 redef fun text
do return native
.text
199 redef fun text
=(value
) do
200 if value
== null then value
= ""
201 native
.text
= value
.to_s
204 redef fun is_password
=(value
)
206 native
.visibility
= value
!= true