core::hash_collection: native array storage is no more nullable
[nit.git] / lib / linux / ui.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Implementation of the app.nit UI module for GNU/Linux
16 module ui
17
18 import app::ui
19 import gtk
20
21 import data_store
22
23 redef class App
24 redef fun setup do init_gtk
25
26 # On GNU/Linux, we go through all the callbacks once,
27 # there is no complex life-cycle.
28 redef fun run
29 do
30 app.on_create
31 app.on_restore_state
32 app.on_start
33 app.on_resume
34
35 var window = window
36 window.native.show_all
37 run_gtk
38
39 app.on_pause
40 app.on_stop
41 app.on_save_state
42 app.on_destroy
43 end
44
45 # Spacing between GTK controls, default at 2
46 var control_spacing = 2 is writable
47 end
48
49 redef class Control
50 super GtkCallable
51 super Finalizable
52
53 # The GTK element used to implement `self`
54 fun native: NATIVE is abstract
55
56 # Type of `native`
57 type NATIVE: GtkWidget
58
59 redef fun finalize
60 do
61 var native = native
62 if not native.address_is_null then native.destroy
63 end
64 end
65
66 redef class CompositeControl
67 redef type NATIVE: GtkContainer
68
69 redef fun add(item)
70 do
71 super
72 native.add item.native
73 end
74 end
75
76 redef class Window
77 redef type NATIVE: GtkWindow
78 redef var native do
79 var win = new GtkWindow(new GtkWindowType.toplevel)
80 win.connect_destroy_signal_to_quit
81 return win
82 end
83 end
84
85 redef class View
86 redef fun enabled do return native.sensitive
87 redef fun enabled=(enabled) do native.sensitive = enabled or else true
88 end
89
90 redef class Layout
91 redef type NATIVE: GtkBox
92 end
93
94 redef class HorizontalLayout
95 redef var native = new GtkBox(new GtkOrientation.horizontal, app.control_spacing)
96
97 redef fun add(item)
98 do
99 super
100 native.homogeneous = true
101 native.set_child_packing(item.native, true, true, 0, new GtkPackType.start)
102 end
103 end
104
105 redef class VerticalLayout
106 redef var native = new GtkBox(new GtkOrientation.vertical, app.control_spacing)
107
108 redef fun add(item)
109 do
110 super
111
112 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
113 native.homogeneous = true
114 native.set_child_packing(item.native, true, true, 0, new GtkPackType.start)
115 end
116 end
117
118 redef class Button
119 redef type NATIVE: GtkButton
120 redef var native = new GtkButton
121
122 redef fun text do return native.text
123 redef fun text=(value) do native.text = (value or else "").to_s
124
125 redef fun signal(sender, data) do notify_observers new ButtonPressEvent(self)
126
127 init do native.signal_connect("clicked", self, null)
128 end
129
130 redef class TextInput
131 redef type NATIVE: GtkEntry
132 redef var native = new GtkEntry
133
134 redef fun text do return native.text
135 redef fun text=(value) do
136 if value == null then value = ""
137 native.text = value.to_s
138 end
139 end