aaae0bd3fcac040e23a2644f76332e0ecf5b0336
[nit.git] / lib / android / ui / native_ui.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 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 # Native services from the `android.view` and `android.widget` namespaces
18 module native_ui is android_api_min 14
19
20 import nit_activity
21
22 in "Java" `{
23 import android.app.Activity;
24
25 import android.view.Gravity;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.ViewGroup.MarginLayoutParams;
29
30 import android.widget.Button;
31 import android.widget.LinearLayout;
32 import android.widget.GridLayout;
33 import android.widget.PopupWindow;
34 import android.widget.TextView;
35
36 import java.lang.*;
37 import java.util.*;
38 `}
39
40 redef extern class NativeActivity
41
42 # Set the main layout of this activity
43 fun content_view=(layout: NativeViewGroup) in "Java" `{
44 self.setContentView(layout);
45 `}
46 end
47
48 # A `View` for Android
49 extern class NativeView in "Java" `{ android.view.View `}
50 super JavaObject
51
52 fun minimum_width=(val: Int) in "Java" `{ self.setMinimumWidth((int)val); `}
53 fun minimum_height=(val: Int) in "Java" `{ self.setMinimumHeight((int)val); `}
54
55 fun enabled: Bool in "Java" `{ return self.isEnabled(); `}
56 fun enabled=(value: Bool) in "Java" `{ self.setEnabled(value); `}
57 end
58
59 # A collection of `NativeView`
60 extern class NativeViewGroup in "Java" `{ android.view.ViewGroup `}
61 super NativeView
62
63 fun add_view(view: NativeView) in "Java" `{ self.addView(view); `}
64
65 fun remove_view(view: NativeView) in "Java" `{ self.removeView(view); `}
66
67 fun add_view_with_weight(view: NativeView, weight: Float)
68 in "Java" `{
69 self.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, (float)weight));
70 `}
71 end
72
73 # A `NativeViewGroup` organized in a line
74 extern class NativeLinearLayout in "Java" `{ android.widget.LinearLayout `}
75 super NativeViewGroup
76
77 new(context: NativeActivity) in "Java" `{ return new LinearLayout(context); `}
78
79 fun set_vertical in "Java" `{ self.setOrientation(LinearLayout.VERTICAL); `}
80 fun set_horizontal in "Java" `{ self.setOrientation(LinearLayout.HORIZONTAL); `}
81
82 redef fun add_view(view) in "Java"
83 `{
84 MarginLayoutParams params = new MarginLayoutParams(
85 LinearLayout.LayoutParams.MATCH_PARENT,
86 LinearLayout.LayoutParams.WRAP_CONTENT);
87 self.addView(view, params);
88 `}
89
90 redef fun new_global_ref import sys, Sys.jni_env `{
91 Sys sys = NativeLinearLayout_sys(self);
92 JNIEnv *env = Sys_jni_env(sys);
93 return (*env)->NewGlobalRef(env, self);
94 `}
95 end
96
97 # A `NativeViewGroup` organized as a grid
98 extern class NativeGridLayout in "Java" `{ android.widget.GridLayout `}
99 super NativeViewGroup
100
101 new(context: NativeActivity) in "Java" `{ return new android.widget.GridLayout(context); `}
102
103 fun row_count=(val: Int) in "Java" `{ self.setRowCount((int)val); `}
104
105 fun column_count=(val: Int) in "Java" `{ self.setColumnCount((int)val); `}
106
107 redef fun add_view(view) in "Java" `{ self.addView(view); `}
108
109 redef fun new_global_ref import sys, Sys.jni_env `{
110 Sys sys = NativeGridLayout_sys(self);
111 JNIEnv *env = Sys_jni_env(sys);
112 return (*env)->NewGlobalRef(env, self);
113 `}
114 end
115
116 extern class NativePopupWindow in "Java" `{ android.widget.PopupWindow `}
117 super NativeView
118
119 new (context: NativeActivity) in "Java" `{
120 PopupWindow self = new PopupWindow(context);
121 self.setWindowLayoutMode(LinearLayout.LayoutParams.MATCH_PARENT,
122 LinearLayout.LayoutParams.MATCH_PARENT);
123 self.setClippingEnabled(false);
124 return self;
125 `}
126
127 fun content_view=(layout: NativeViewGroup) in "Java" `{ self.setContentView(layout); `}
128
129 redef fun new_global_ref import sys, Sys.jni_env `{
130 Sys sys = NativePopupWindow_sys(self);
131 JNIEnv *env = Sys_jni_env(sys);
132 return (*env)->NewGlobalRef(env, self);
133 `}
134 end
135
136 extern class NativeTextView in "Java" `{ android.widget.TextView `}
137 super NativeView
138
139 new (context: NativeActivity) in "Java" `{ return new TextView(context); `}
140
141 fun text: JavaString in "Java" `{ return self.getText().toString(); `}
142
143 fun text=(value: JavaString) in "Java" `{ self.setText(value); `}
144
145 fun gravity_center in "Java" `{
146 self.setGravity(Gravity.CENTER);
147 `}
148
149 fun text_size: Float in "Java" `{
150 return self.getTextSize();
151 `}
152 fun text_size=(dpi: Float) in "Java" `{
153 self.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, (float)dpi);
154 `}
155
156 redef fun new_global_ref import sys, Sys.jni_env `{
157 Sys sys = NativeTextView_sys(self);
158 JNIEnv *env = Sys_jni_env(sys);
159 return (*env)->NewGlobalRef(env, self);
160 `}
161 end
162
163 extern class NativeEditText in "Java" `{ android.widget.EditText `}
164 super NativeTextView
165
166 redef type SELF: NativeEditText
167
168 new (context: NativeActivity) in "Java" `{ return new android.widget.EditText(context); `}
169
170 fun width=(val: Int) in "Java" `{ self.setWidth((int)val); `}
171
172 fun input_type_text in "Java" `{ self.setInputType(android.text.InputType.TYPE_CLASS_TEXT); `}
173
174 redef fun new_global_ref import sys, Sys.jni_env `{
175 Sys sys = NativeEditText_sys(self);
176 JNIEnv *env = Sys_jni_env(sys);
177 return (*env)->NewGlobalRef(env, self);
178 `}
179 end
180
181 extern class NativeButton in "Java" `{ android.widget.Button `}
182 super NativeTextView
183
184 redef type SELF: NativeButton
185
186 redef fun new_global_ref import sys, Sys.jni_env `{
187 Sys sys = NativeButton_sys(self);
188 JNIEnv *env = Sys_jni_env(sys);
189 return (*env)->NewGlobalRef(env, self);
190 `}
191 end