Merge: More keep going
[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 final ViewGroup final_layout = layout;
45 final Activity final_self = self;
46
47 self.runOnUiThread(new Runnable() {
48 @Override
49 public void run() {
50 final_self.setContentView(final_layout);
51
52 final_layout.requestFocus();
53 }
54 });
55 `}
56 end
57
58 # A `View` for Android
59 extern class NativeView in "Java" `{ android.view.View `}
60 super JavaObject
61
62 fun minimum_width=(val: Int) in "Java" `{ self.setMinimumWidth((int)val); `}
63 fun minimum_height=(val: Int) in "Java" `{ self.setMinimumHeight((int)val); `}
64
65 fun enabled: Bool in "Java" `{ return self.isEnabled(); `}
66 fun enabled=(value: Bool) in "Java" `{
67 final View final_self = self;
68 final boolean final_value = value;
69
70 ((Activity)self.getContext()).runOnUiThread(new Runnable() {
71 @Override
72 public void run() {
73 final_self.setEnabled(final_value);
74 }
75 });
76 `}
77 end
78
79 # A collection of `NativeView`
80 extern class NativeViewGroup in "Java" `{ android.view.ViewGroup `}
81 super NativeView
82
83 fun add_view(view: NativeView) in "Java" `{ self.addView(view); `}
84
85 fun add_view_with_weight(view: NativeView, weight: Float)
86 in "Java" `{
87 self.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, (float)weight));
88 `}
89 end
90
91 # A `NativeViewGroup` organized in a line
92 extern class NativeLinearLayout in "Java" `{ android.widget.LinearLayout `}
93 super NativeViewGroup
94
95 new(context: NativeActivity) in "Java" `{ return new LinearLayout(context); `}
96
97 fun set_vertical in "Java" `{ self.setOrientation(LinearLayout.VERTICAL); `}
98 fun set_horizontal in "Java" `{ self.setOrientation(LinearLayout.HORIZONTAL); `}
99
100 redef fun add_view(view) in "Java"
101 `{
102 MarginLayoutParams params = new MarginLayoutParams(
103 LinearLayout.LayoutParams.MATCH_PARENT,
104 LinearLayout.LayoutParams.WRAP_CONTENT);
105 self.addView(view, params);
106 `}
107 end
108
109 # A `NativeViewGroup` organized as a grid
110 extern class NativeGridLayout in "Java" `{ android.widget.GridLayout `}
111 super NativeViewGroup
112
113 new(context: NativeActivity) in "Java" `{ return new android.widget.GridLayout(context); `}
114
115 fun row_count=(val: Int) in "Java" `{ self.setRowCount((int)val); `}
116
117 fun column_count=(val: Int) in "Java" `{ self.setColumnCount((int)val); `}
118
119 redef fun add_view(view) in "Java" `{ self.addView(view); `}
120 end
121
122 extern class NativePopupWindow in "Java" `{ android.widget.PopupWindow `}
123 super NativeView
124
125 new (context: NativeActivity) in "Java" `{
126 PopupWindow self = new PopupWindow(context);
127 self.setWindowLayoutMode(LinearLayout.LayoutParams.MATCH_PARENT,
128 LinearLayout.LayoutParams.MATCH_PARENT);
129 self.setClippingEnabled(false);
130 return self;
131 `}
132
133 fun content_view=(layout: NativeViewGroup) in "Java" `{ self.setContentView(layout); `}
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" `{
144
145 final TextView final_self = self;
146 final String final_value = value;
147
148 ((Activity)self.getContext()).runOnUiThread(new Runnable() {
149 @Override
150 public void run() {
151 final_self.setText(final_value);
152 }
153 });
154 `}
155
156 fun gravity_center in "Java" `{
157 self.setGravity(Gravity.CENTER);
158 `}
159
160 fun text_size: Float in "Java" `{
161 return self.getTextSize();
162 `}
163 fun text_size=(dpi: Float) in "Java" `{
164 self.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, (float)dpi);
165 `}
166 end
167
168 extern class NativeEditText in "Java" `{ android.widget.EditText `}
169 super NativeTextView
170
171 redef type SELF: NativeEditText
172
173 new (context: NativeActivity) in "Java" `{ return new android.widget.EditText(context); `}
174
175 fun width=(val: Int) in "Java" `{ self.setWidth((int)val); `}
176
177 fun input_type_text in "Java" `{ self.setInputType(android.text.InputType.TYPE_CLASS_TEXT); `}
178
179 redef fun new_global_ref: SELF import sys, Sys.jni_env `{
180 Sys sys = NativeEditText_sys(self);
181 JNIEnv *env = Sys_jni_env(sys);
182 return (*env)->NewGlobalRef(env, self);
183 `}
184 end
185
186 extern class NativeButton in "Java" `{ android.widget.Button `}
187 super NativeTextView
188
189 redef type SELF: NativeButton
190
191 redef fun new_global_ref: SELF import sys, Sys.jni_env `{
192 Sys sys = NativeButton_sys(self);
193 JNIEnv *env = Sys_jni_env(sys);
194 return (*env)->NewGlobalRef(env, self);
195 `}
196 end