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
23 # Request width of the GTK window for an app.nit application
25 # This is the minimum width of the window, it may grow bigger to fit content.
26 fun gtk_window_width_request
: Int do return 480
29 redef fun setup
do gtk_init
31 # Single GTK window of this application
32 var native_window
: GtkWindow is lazy
do
33 var win
= new GtkWindow(new GtkWindowType.toplevel
)
34 win
.connect_destroy_signal_to_quit
35 win
.titlebar
= native_header_bar
41 var native_header_bar
: GtkHeaderBar is lazy
do
42 var bar
= new GtkHeaderBar
43 bar
.title
= "app.nit" # TODO offer a portable API to name windows
44 bar
.show_close_button
= true
46 # TODO add back button
51 # Root `GtkStack` used to simulate the many app.nit windows
52 var native_stack
: GtkStack is lazy
do
53 var stack
= new GtkStack
54 stack
.homogeneous
= false
58 # On GNU/Linux, we go through all the callbacks once,
59 # there is no complex life-cycle.
67 native_window
.show_all
76 # Spacing between GTK controls, default at 2
77 var control_spacing
= 2 is writable
79 redef fun window
=(window
)
81 var root_view
= window
.view
82 assert root_view
!= null
83 native_stack
.add root_view
.native
84 native_stack
.visible_child
= root_view
.native
86 # FIXME These settings forces the GTK window to resize to its minimum
87 # size when changing app.nit windows. It is not pretty, but it could be
88 # improved with GTK 3.18 and interpolate_size.
89 native_window
.resizable
= false
99 # The GTK element used to implement `self`
100 fun native
: NATIVE is abstract
103 type NATIVE: GtkWidget
108 if not native
.address_is_null
then native
.destroy
112 redef class CompositeControl
115 # On GNU/Linux, a window is implemented by placing the `view` in a `GtkStack` in the single GTK window
118 # Root view of this window
119 var view
: nullable View = null
123 if view
isa View then
125 view
.native
.valign
= new GtkAlign.start
126 view
.native
.set_size_request
(gtk_window_width_request
, 0)
136 redef fun enabled
do return native
.sensitive
137 redef fun enabled
=(enabled
) do native
.sensitive
= enabled
or else true
141 redef type NATIVE: GtkBox
146 if item
isa View then native
.add item
.native
149 redef fun remove
(item
)
152 if item
isa View then native
.remove item
.native
156 redef class HorizontalLayout
157 redef var native
= new GtkBox(new GtkOrientation.horizontal
, app
.control_spacing
)
162 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
163 native
.homogeneous
= true
164 native
.set_child_packing
(item
.native
, true, true, 0, new GtkPackType.start
)
168 redef class VerticalLayout
169 redef var native
= new GtkBox(new GtkOrientation.vertical
, app
.control_spacing
)
175 native
.set_child_packing
(item
.native
, true, true, 0, new GtkPackType.start
)
179 # On GNU/Linux, this is implemented by a `GtkListBox` inside a `GtkScrolledWindow`
180 redef class ListLayout
182 redef type NATIVE: GtkScrolledWindow
184 redef var native
= new GtkScrolledWindow
186 # Container inside `native`
187 var native_list_box
= new GtkListBox
190 native_list_box
.selection_mode
= new GtkSelectionMode.none
191 native
.add native_list_box
193 # Set the size of the GtkScrolledWindow:
194 # use content width and set static height
195 native
.set_policy
(new GtkPolicyType.never
, new GtkPolicyType.automatic
)
196 native
.set_size_request
(gtk_window_width_request
, 640)
202 if item
isa View then native_list_box
.add item
.native
205 redef fun remove
(item
)
208 if item
isa View then native_list_box
.remove item
.native
213 redef type NATIVE: GtkButton
214 redef var native
= new GtkButton
216 redef fun text
do return native
.text
217 redef fun text
=(value
) do native
.text
= (value
or else "").to_s
219 redef fun signal
(sender
, data
) do notify_observers
new ButtonPressEvent(self)
221 init do native
.signal_connect
("clicked", self, null)
225 redef type NATIVE: GtkLabel
226 redef var native
= new GtkLabel("")
228 redef fun text
do return native
.text
229 redef fun text
=(value
) do native
.text
= (value
or else "").to_s
233 redef type NATIVE: GtkCheckButton
234 redef var native
= new GtkCheckButton
236 redef fun text
do return native
.text
237 redef fun text
=(value
) do native
.text
= (value
or else "").to_s
239 redef fun is_checked
do return native
.active
240 redef fun is_checked
=(value
) do native
.active
= value
243 redef class TextInput
244 redef type NATIVE: GtkEntry
245 redef var native
= new GtkEntry
247 redef fun text
do return native
.text
248 redef fun text
=(value
) do
249 if value
== null then value
= ""
250 native
.text
= value
.to_s
253 redef fun is_password
=(value
)
255 native
.visibility
= value
!= true