forked from MoSyncLabs/JSNativeUI
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
347 lines (311 loc) · 9.63 KB
/
main.cpp
File metadata and controls
347 lines (311 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* @file main.cpp
* @author Ali Sarraf, Iraklis Rosis
*
* Template application for developing NativeUI Apps in JavaScript
* Do not Modify this file unless you know what you are doing
*/
// Include Moblet for web applications.
#include <Wormhole/WebAppMoblet.h>
// Namespaces we want to access.
using namespace MAUtil; // Class Moblet
using namespace NativeUI; // WebView widget.
using namespace Wormhole; // Class WebAppMoblet
/**
* The application class.
*/
class MyMoblet : public WebAppMoblet
{
public:
MyMoblet()
{
// Enable message sending from JavaScript to C++.
enableWebViewMessages();
//The webview that processes our Javascript code it hidden
// users can create other webviews from html
getWebView()->disableZoom();
getWebView()->setVisible(false);
// The page in the "LocalFiles" folder to
// show when the application starts.
//The page contains calls into the Native UI System
showPage("index.html");
//We have added this class as a custom event listener so it
//can forward all of the custom events to JavaScript
Environment::getEnvironment().addCustomEventListener(this);
}
/**
* This method handles messages sent from the WebView.
* @param webView The WebView that sent the message.
* @param urlData Data object that holds message content.
* Note that the data object will be valid only during
* the life-time of the call of this method, then it
* will be deallocated.
*/
void handleWebViewMessage(WebView* webView, MAHandle urlData)
{
// Create message object. This parses the message.
WebViewMessage message(webView, urlData);
char buffer[128];
if (message.is("close"))
{
//Close the App by request from JavaScript
close();
}
// Widget Handling Calls
else if(message.is("maWidgetCreate"))
{
//This also expects an "id" that will identify
//the returned handle to javascript
MAWidgetHandle widget =
maWidgetCreate(message.getParam("widgetType").c_str());
const char* widgetID = message.getParam("widgetID").c_str();
sprintf(buffer,"NativeUI.widgetCreated('%s', %d)", widgetID, widget);
callJS(buffer);
}
else if(message.is("maWidgetDestroy"))
{
MAWidgetHandle widget = stringToInteger(message.getParam("widget"));
maWidgetDestroy(widget);
}
else if(message.is("maWidgetAddChild"))
{
MAWidgetHandle parent = stringToInteger(message.getParam("parent"));
MAWidgetHandle child = stringToInteger(message.getParam("child"));
maWidgetAddChild(parent, child);
}
else if(message.is("maWidgetInsertChild"))
{
MAWidgetHandle parent = stringToInteger(message.getParam("parent"));
MAWidgetHandle child = stringToInteger(message.getParam("child"));
int index = stringToInteger(message.getParam("index"));
maWidgetInsertChild(parent, child, index);
}
else if(message.is("maWidgetRemoveChild"))
{
MAWidgetHandle child = stringToInteger(message.getParam("child"));
maWidgetRemoveChild(child);
}
else if(message.is("maWidgetModalDialogShow"))
{
MAWidgetHandle dialogHandle =
stringToInteger(message.getParam("dialogHandle"));
maWidgetModalDialogShow(dialogHandle);
}
else if(message.is("maWidgetModalDialogHide"))
{
MAWidgetHandle dialogHandle =
stringToInteger(message.getParam("dialogHandle"));
maWidgetModalDialogHide(dialogHandle);
}
else if(message.is("maWidgetScreenShow"))
{
MAWidgetHandle screenHandle =
stringToInteger(message.getParam("screenHandle"));
maWidgetScreenShow(screenHandle);
}
else if(message.is("maWidgetStackScreenPush"))
{
MAWidgetHandle stackScreen =
stringToInteger(message.getParam("stackScreen"));
MAWidgetHandle newScreen =
stringToInteger(message.getParam("newScreen"));
maWidgetStackScreenPush(stackScreen, newScreen);
}
else if(message.is("maWidgetStackScreenPop"))
{
MAWidgetHandle stackScreen =
stringToInteger(message.getParam("stackScreen"));
maWidgetStackScreenPop(stackScreen);
}
else if(message.is("maWidgetSetProperty"))
{
MAWidgetHandle widget =
stringToInteger(message.getParam("widget"));
const char *property = message.getParam("property").c_str();
const char *value = message.getParam("value").c_str();
int res = maWidgetSetProperty(widget, property, value);
}
else if(message.is("maWidgetGetProperty"))
{
char value[64];
MAWidgetHandle widget =
stringToInteger(message.getParam("widget"));
const char *property = message.getParam("property").c_str();
maWidgetGetProperty(widget, property, value, 64);
sprintf(buffer,
"NativeUI.widgetProperty(%d, \"%s\", \"%s\")",
widget,
property,
value);
callJS(buffer);
}
//Call for loading an Image resource
else if(message.is("loadImage"))
{
MAHandle imageHandle =
loadImageResource(message.getParam("imagePath").c_str());
const char* imageID = message.getParam("imageID").c_str();
sprintf(buffer,
"bridge.ResourceHandler.imageLoaded(\"%s\", %d)",
imageID,
imageHandle);
callJS(buffer);
}
// Tell the WebView that we have processed the message, so that
// it can send the next one.
callJS("bridge.messagehandler.processedMessage()");
}
MAHandle loadImageResource(const char *imagePath)
{
int bufferSize = 1024;
char buffer[bufferSize];
//Get the local path which is the same path as the root of HTML apps
int size = maGetSystemProperty(
"mosync.path.local",
buffer,
bufferSize);
//Construct a full path by concatenating the relative path and local path
char completePath[2048];
sprintf(completePath,
"%s%s",
buffer,
imagePath);
//Load the image and create a data handle from it
MAHandle imageFile = maFileOpen(completePath, MA_ACCESS_READ);
int fileSize = maFileSize(imageFile);
MAHandle fileData = maCreatePlaceholder();
int res = maCreateData(fileData, fileSize);
res = maFileReadToData(imageFile, fileData, 0, fileSize);
maFileClose(imageFile);
MAHandle imageHandle = maCreatePlaceholder();
res = maCreateImageFromData(
imageHandle,
fileData,
0,
maGetDataSize(fileData));
maDestroyObject(fileData);
//return the handle to the loaded image
return imageHandle;
}
/**
* This method is called when a key is pressed. It closes
* the application when the back key (on Android) is pressed.
* Forwards the event to JavaScript by Calling the same function
* Implement the function to handle events in JavaScript
*/
void keyPressEvent(int keyCode, int nativeCode)
{
char buffer[256];
// forward the event to application
sprintf(buffer,
"keyPressEvent(%d, %d)",
keyCode,
nativeCode);
callJS(buffer);
}
/**
* This method is called whenever a custom
* event is fired.
* It is used to forward events to the JavaScript side.
*/
void customEvent (const MAEvent &event)
{
char buffer[128];
if(event.type == EVENT_TYPE_WIDGET)
{
MAWidgetEventData *data = (MAWidgetEventData*)event.data;
MAWidgetHandle widget = data->widgetHandle;
if((widget == getWebView()->getWidgetHandle()))
{
return;
}
int firstParameter = data->dayOfMonth;
int secondParameter = data->month;
int thirdParameter = data->year;
char *eventType;
switch(data->eventType)
{
case MAW_EVENT_POINTER_PRESSED:
eventType = "PointerPressed";
break;
case MAW_EVENT_POINTER_RELEASED:
eventType = "PointerReleased";
break;
case MAW_EVENT_CONTENT_LOADED:
eventType = "ContentLoaded";
break;
case MAW_EVENT_CLICKED:
eventType = "Clicked";
break;
case MAW_EVENT_ITEM_CLICKED:
eventType = "ItemClicked";
break;
case MAW_EVENT_TAB_CHANGED:
eventType = "TabChanged";
break;
case MAW_EVENT_GL_VIEW_READY:
eventType = "GLViewReady";
break;
case MAW_EVENT_WEB_VIEW_URL_CHANGED:
eventType = "WebViewURLChanged";
break;
case MAW_EVENT_STACK_SCREEN_POPPED:
eventType = "StackScreenPopped";
break;
case MAW_EVENT_SLIDER_VALUE_CHANGED:
eventType = "SliderValueChanged";
break;
case MAW_EVENT_DATE_PICKER_VALUE_CHANGED:
eventType = "DatePickerValueChanged";
break;
case MAW_EVENT_NUMBER_PICKER_VALUE_CHANGED:
eventType = "NumberPickerValueChanged";
break;
case MAW_EVENT_VIDEO_STATE_CHANGED:
eventType = "VideoStateChanged";
break;
case MAW_EVENT_EDIT_BOX_EDITING_DID_BEGIN:
eventType = "EditBoxEditingDidBegin";
break;
case MAW_EVENT_EDIT_BOX_EDITING_DID_END:
eventType = "EditBoxEditingDidEnd";
break;
case MAW_EVENT_EDIT_BOX_TEXT_CHANGED:
eventType = "EditBoxTextChanged";
break;
case MAW_EVENT_EDIT_BOX_RETURN:
eventType = "EditBoxReturn";
break;
case MAW_EVENT_WEB_VIEW_CONTENT_LOADING:
eventType = "WebViewContentLoading";
break;
case MAW_EVENT_WEB_VIEW_HOOK_INVOKED:
eventType = "WebViewHookInvoked";
break;
case MAW_EVENT_DIALOG_DISMISSED:
eventType = "DialogDismissed";
break;
}
sprintf(buffer,
"NativeUI.event(%d, \"%s\", %d, %d, %d)",
widget,
eventType,
firstParameter,
secondParameter,
thirdParameter);
callJS(buffer);
}
}
private:
MAHandle mFileData;
};
/**
* Main function that is called when the program starts.
* Here an instance of the MyMoblet class is created and
* the program enters the main event loop.
*/
extern "C" int MAMain()
{
Moblet::run(new MyMoblet());
return 0;
}