readme: add information section
[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::v3_10
20
21 import data_store
22
23 redef class App
24 redef fun setup do gtk_init
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 gtk_main
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 init do native.show
87
88 redef fun enabled do return native.sensitive
89 redef fun enabled=(enabled) do native.sensitive = enabled or else true
90 end
91
92 redef class Layout
93 redef type NATIVE: GtkBox
94 redef fun remove(view)
95 do
96 super
97 native.remove view.native
98 end
99 end
100
101 redef class HorizontalLayout
102 redef var native = new GtkBox(new GtkOrientation.horizontal, app.control_spacing)
103
104 redef fun add(item)
105 do
106 super
107 native.homogeneous = true
108 native.set_child_packing(item.native, true, true, 0, new GtkPackType.start)
109 end
110 end
111
112 redef class VerticalLayout
113 redef var native = new GtkBox(new GtkOrientation.vertical, app.control_spacing)
114
115 redef fun add(item)
116 do
117 super
118
119 # FIXME abstract the use either homogeneous or weight to balance views size in a layout
120 native.homogeneous = true
121 native.set_child_packing(item.native, true, true, 0, new GtkPackType.start)
122 end
123 end
124
125 redef class ListLayout
126 redef type NATIVE: GtkListBox
127 redef var native = new GtkListBox
128
129 init do native.selection_mode = new GtkSelectionMode.none
130 end
131
132 redef class Button
133 redef type NATIVE: GtkButton
134 redef var native = new GtkButton
135
136 redef fun text do return native.text
137 redef fun text=(value) do native.text = (value or else "").to_s
138
139 redef fun signal(sender, data) do notify_observers new ButtonPressEvent(self)
140
141 init do native.signal_connect("clicked", self, null)
142 end
143
144 redef class Label
145 redef type NATIVE: GtkLabel
146 redef var native = new GtkLabel("")
147
148 redef fun text do return native.text
149 redef fun text=(value) do native.text = (value or else "").to_s
150 end
151
152 redef class TextInput
153 redef type NATIVE: GtkEntry
154 redef var native = new GtkEntry
155
156 redef fun text do return native.text
157 redef fun text=(value) do
158 if value == null then value = ""
159 native.text = value.to_s
160 end
161 end