/** * Utility function to create a label. * The height of the label is computed automatically. * @param width Label width. * @param fontColor Text color. * @param fontSize Text size. * @param text Label text. * @return Handle to the new label. */ MAWidgetHandle NativeScreen::createLabel( int width, int fontColor, int fontSize, const char* text) { // Create the label. MAWidgetHandle label = maWidgetCreate(MAW_LABEL); // Set the label font color. setWidgetProperty(label, MAW_LABEL_FONT_COLOR, fontColor, 16); // Set the label font size. setWidgetProperty(label, MAW_LABEL_FONT_SIZE, fontSize); // Set the widget size. setWidgetProperty(label, MAW_WIDGET_WIDTH, width); setWidgetProperty(label, MAW_WIDGET_HEIGHT, MAW_CONSTANT_WRAP_CONTENT); // Set the label text. maWidgetSetProperty(label, MAW_LABEL_TEXT, text); // Set text alignment. maWidgetSetProperty(label, MAW_LABEL_TEXT_VERTICAL_ALIGNMENT, "center"); maWidgetSetProperty(label, MAW_LABEL_TEXT_HORIZONTAL_ALIGNMENT, "center"); return label; }
/** * Show the screen. */ void HomeScreen::showScreen() { // Make the home screen listen for key events. MAUtil::Environment::getEnvironment().addKeyListener(this); // Reset the progress bar value to 0, and dismiss it. maWidgetSetProperty(mProgressLabel,MAW_WIDGET_VISIBLE, "false"); setWidgetProperty(mProgressBar,MAW_PROGRESS_BAR_PROGRESS, 0); maWidgetSetProperty(mProgressBar,MAW_WIDGET_VISIBLE, "false"); maWidgetSetProperty(mSearchButton, MAW_WIDGET_ENABLED, "true"); // Reset the slider value. setWidgetProperty(mSlider, MAW_SLIDER_VALUE, 10); // Clear the edit box. maWidgetSetProperty(mEditBox, MAW_EDIT_BOX_TEXT, ""); // Check all categories. for (int i=0; i<mCategoryBoxes.size(); i++) { maWidgetSetProperty(mCategoryBoxes[i], MAW_CHECK_BOX_CHECKED, "true"); } // Initialize the top text label. setLabelText(mLabel, " "); // Display this screen. BasicScreen::showScreen(); }
/** * Creates a button with centered text by default, * font size is 20, and color is DARK GREY. * @param text The button text. * @return Handle to the new button. */ MAWidgetHandle NativeScreen::createButton(const char* text, int width, int height) { // Create button. MAWidgetHandle button = maWidgetCreate(MAW_BUTTON); // Set text and text alignment. maWidgetSetProperty(button, MAW_BUTTON_TEXT, text); maWidgetSetProperty(button, MAW_BUTTON_TEXT_VERTICAL_ALIGNMENT, "center"); maWidgetSetProperty(button, MAW_BUTTON_TEXT_HORIZONTAL_ALIGNMENT, "center"); setWidgetProperty(button, MAW_WIDGET_WIDTH, width); setWidgetProperty(button, MAW_WIDGET_HEIGHT, height); // Set font color. setWidgetProperty(button, MAW_BUTTON_FONT_COLOR, BLUE, 16); // setWidgetProperty(button, MAW_WIDGET_BACKGROUND_COLOR, DARK_SEA_GREEN, 16); // maWidgetSetProperty(button,"Gradient", " "); // Set font size to small. // setWidgetProperty(button, MAW_BUTTON_FONT_SIZE, mFontRegularSize); return button; }
/** * Utility function to create a label. * The height of the label is computed automatically. * @param width Label width. * @param text Label text. * @param fontColor Text color. * @param fontSize Text size. * * @return Handle to the new label. */ MAWidgetHandle createLabel(int width, const char* text, int fontColor, int fontSize) { MAWidgetHandle label = maWidgetCreate(MAW_LABEL); // Set the label font color. setWidgetProperty(label, MAW_LABEL_FONT_COLOR, fontColor, 16); // Set the label font size. setWidgetProperty(label, MAW_LABEL_FONT_SIZE, fontSize); // Set the widget size. setWidgetProperty(label, MAW_WIDGET_WIDTH, width); setWidgetProperty(label, MAW_WIDGET_HEIGHT, MAW_CONSTANT_WRAP_CONTENT); // Set the label text. maWidgetSetProperty(label, MAW_LABEL_TEXT, text); maWidgetSetProperty( label, MAW_LABEL_TEXT_VERTICAL_ALIGNMENT, MAW_ALIGNMENT_CENTER); maWidgetSetProperty( label, MAW_LABEL_TEXT_HORIZONTAL_ALIGNMENT, MAW_ALIGNMENT_CENTER); return label; }
/** * This method implements a custom event listener. * Widget events are sent as custom events. */ void SettingsScreen::customEvent(const MAEvent& event) { // Check if this is a widget event. if (EVENT_TYPE_WIDGET == event.type) { // Get the widget event data structure. MAWidgetEventData* eventData = (MAWidgetEventData*) event.data; // Here we handle clicked events. if (MAW_EVENT_CLICKED == eventData->eventType) { if(mFlashModeButton == eventData->widgetHandle) { if(flashModeIndex < 3) { flashModeIndex++; } else { flashModeIndex = 0; } char buffer[128]; sprintf(buffer, "Flash Mode: %s", getModeForIndex(flashModeIndex)); maWidgetSetProperty(mFlashModeButton,MAW_BUTTON_TEXT, buffer); } else if(mSwapCameraButton == eventData->widgetHandle) { if(currentCamera < numCameras-1) { currentCamera++; } else { currentCamera = 0; } if(currentCamera == 0) { maWidgetSetProperty( mSwapCameraButton, MAW_LABEL_TEXT, "Camera Selected: Back"); } else { maWidgetSetProperty( mSwapCameraButton, MAW_LABEL_TEXT, "Camera Selected: Front"); } } else if(mOKButton == eventData->widgetHandle) { isViewed= false; maWidgetStackScreenPop(mStackScreen); } } } }
/** * The UI is notified when an engine error occurs. */ void HomeScreen::engineError(MAUtil::String errorMessage) { // Some error occurred. setLabelText(mLabel, errorMessage.c_str()); // Hide the progress bar and it's label. maWidgetSetProperty(mProgressBar, MAW_WIDGET_VISIBLE, "false"); maWidgetSetProperty(mProgressLabel,MAW_WIDGET_VISIBLE, "false"); }
/** * Utility function to set sizes of a widget. * @param widget The widget handle. * @param width The widget width. * @param height The widget height. */ void setWidgetSize(MAWidgetHandle widget, int width, int height) { char buf[10]; itoa(width, buf, 10); maWidgetSetProperty(widget, MAW_WIDGET_WIDTH, buf); itoa(height, buf, 10); maWidgetSetProperty(widget, MAW_WIDGET_HEIGHT, buf); }
/** * from CustomEventListener * The custom event listener interface. */ void HomeScreen::customEvent(const MAEvent& event) { //If the event does not come from a widget, we just ignore it. if (event.type != EVENT_TYPE_WIDGET) { return; } // Get the information sent by the widget. MAWidgetEventData *widgetEventData = (MAWidgetEventData *) event.data; // Check that the event was sent from a widget... if (widgetEventData->eventType == MAW_EVENT_CLICKED) { // handle the event emitted by the widget widgetClicked(widgetEventData->widgetHandle); } // Check if the event came from a slider value change... else if(widgetEventData->eventType == MAW_EVENT_SLIDER_VALUE_CHANGED) { // Set the current value of the slider. mSliderValue = widgetEventData->sliderValue; } else if(widgetEventData->eventType == MAW_EVENT_EDIT_BOX_RETURN) { // Hide the keyboard. maWidgetSetProperty( widgetEventData->widgetHandle, MAW_EDIT_BOX_SHOW_KEYBOARD, "false"); } return; }
/** * Register this web view to receive messages from JavaScript. * This will set a hook for urls with the "mosync://" * scheme. Messages will arrive in in the * WebViewListener::webViewHookInvoked() method. Use class * WebViewMessage to inspect and parse messages. */ void WebView::enableWebViewMessages() { maWidgetSetProperty( getWidgetHandle(), MAW_WEB_VIEW_HARD_HOOK, "mosync://.*"); }
/** * Unregister this web view from receiving messages sent * from JavaScript. This will clear the web view url hook. */ void WebView::disableWebViewMessages() { maWidgetSetProperty( getWidgetHandle(), MAW_WEB_VIEW_HARD_HOOK, ""); }
/** * Evaluate JavaScript the WebView associated with this message. * @param script JavaScript string. */ void MessageStream::callJS(const MAUtil::String& script) { MAUtil::String url = "javascript:" + script; maWidgetSetProperty( mWebViewHandle, MAW_WEB_VIEW_URL, url.c_str()); }
int createOpenGLScreen(int& openglView) { int screen = maWidgetCreate( "Screen" ); maWidgetSetProperty( screen, "title", "OpenGL" ); openglView = maWidgetCreate( "GLView" ); maWidgetAddChild( screen, openglView ); return screen; }
/** * Run JavaScript code in the web view. * @param script The JavaScript code to run. * @return #MAW_RES_OK on success, <0 on error. */ int WebView::callJS(const MAUtil::String& script) { MAUtil::String url = "javascript:" + script; return maWidgetSetProperty( getWidgetHandle(), MAW_WEB_VIEW_URL, url.c_str()); }
/** * Set a widget float property. * @param property The property name. * @param value The float value. */ void Widget::setProperty( const MAUtil::String& property, float value) { char buffer[256]; sprintf(buffer, "%f", value); maWidgetSetProperty(mWidgetHandle, property.c_str(), buffer); }
/* * Apply an int property for a widget. * @param aWidget The handle for the widget. * @param aProperty A string representing which property to set. * @param aValue The value which will be assigned to the property. * @param base The output radix. * * * \returns Any of the following result codes: * - #MAW_RES_OK if the property could be set. * - #MAW_RES_INVALID_HANDLE if the handle was invalid. * - #MAW_RES_INVALID_PROPERTY_NAME if the property name was invalid. * - #MAW_RES_INVALID_PROPERTY_VALUE if the property value was invalid. * - #MAW_RES_ERROR otherwise. */ int NativeScreen::setWidgetProperty(MAWidgetHandle aWidget, const char* aProperty, int aValue, int base) { // Buffer for property values. char buf[10]; itoa( aValue, buf, base); // Apply the property to the widget. return maWidgetSetProperty(aWidget, aProperty, buf); }
//Next a method for handling events from widgets that have been clicked. void widgetClicked(MAHandle widgetHandle) { //If the clear button was clicked... if(widgetHandle == mClearButton) { //...clear the edit box. maWidgetSetProperty(mPasswordBox, MAW_EDIT_BOX_TEXT, ""); } //If the submit button was clicked... else if(widgetHandle == mSubmitButton) { //Declare a buffer for the text contained in the edit box, since //there is no high level API available. char passwordTextBuffer[256]; //Get the text from the password box and put it into the buffer. int textLength = maWidgetGetProperty(mPasswordBox, MAW_EDIT_BOX_TEXT, passwordTextBuffer, 256); //If the text does not fit in the buffer, textLength will be set //to -1, therefore we need to check that the length is greater than //or equal to 0, otherwise we just ignore the event. if(textLength == MAW_RES_INVALID_STRING_BUFFER_SIZE) { return; } //Check that the password is at least 6 characters long. if(textLength < 6) { //If the password is too short we use the instructions label //to inform the user. Note how C automatically //concatenates strings split over multiple lines. maWidgetSetProperty(mInstructions, MAW_LABEL_TEXT, "Password too short. " "Please enter a password of at least 6 characters:"); } else { //If the password is at least 6 characters long, we congratulate //user. maWidgetSetProperty(mInstructions, MAW_LABEL_TEXT, "Password OK"); } } }
/** * Handle events on screen's widgets. */ void TitleScreen::widgetClicked(MAHandle widgetHandle) { char buf[6]; if ( widgetHandle == mNextButton ) { // For selected titles, a short snippet will be displayed ( in a separate screen). updateWikiTitles(); // Show the next screen. mSummaryScreen->showScreen(true); } else if ( widgetHandle == mBackButton ) { // Go back to the home screen. showHomeScreen(); } else if ( mSelectAll == widgetHandle ) { // Select all titles, or deselect them. maWidgetGetProperty(mSelectAll, MAW_CHECK_BOX_CHECKED, buf, 6); for (int i=0; i < mCheckBoxes.size(); i++) { maWidgetSetProperty(mCheckBoxes[i], MAW_CHECK_BOX_CHECKED, buf); } } else { // If a check box is unselected, than Select all is unselected too. for (int i=0; i < mCheckBoxes.size(); i++) { if (mCheckBoxes[i] == widgetHandle){ maWidgetGetProperty( mCheckBoxes[i], MAW_CHECK_BOX_CHECKED, buf, 6); if (strcmp(buf, "false") == 0) { maWidgetSetProperty( mSelectAll, MAW_CHECK_BOX_CHECKED, "false"); } break; } } } }
/** * We have got a location event. * Call a JavaScript function with the updated location data. * Setting the "url" property using the "javascript:" scheme * evaluates the JavaScript code in the WebView. */ void handleGeoLocationEvent(MALocation* location) { char script[512]; sprintf( script, "javascript:GeoLocationUpdated('%f','%f');", location->lat, location->lon); maWidgetSetProperty(mWebView, "url", script); }
void testCheckedProperty() { TESTIFY_ASSERT_EQUAL( maWidgetSetProperty( getTestWidgetHandle( ), MAW_TOGGLE_BUTTON_CHECKED, "false" ), MAW_RES_OK ); char checkedBuffer[256]; TESTIFY_ASSERT_EQUAL( maWidgetGetProperty( getTestWidgetHandle( ), MAW_TOGGLE_BUTTON_CHECKED, checkedBuffer, 256 ), strlen( "false" ) ); TESTIFY_ASSERT_EQUAL( strcmp( checkedBuffer, "false" ), 0 ); TESTIFY_ASSERT_EQUAL( maWidgetGetProperty( getTestWidgetHandle( ), MAW_TOGGLE_BUTTON_CHECKED, checkedBuffer, 1 ), MAW_RES_INVALID_STRING_BUFFER_SIZE ); }
/** * Open a web view for a certain title * compose the url and display * @param The article title for which we want to open a wikipedia definition. * @return false if some error occur. */ bool WebScreen::openWebView(MAUtil::String title) { MAUtil::String url = "http://en.wikipedia.org/wiki/" + title; // Replace spaces. replaceString(url," ","%20"); // Display the article. maWidgetSetProperty(mWebView,MAW_WEB_VIEW_URL,url.c_str() ); }
/** * Fill the list box with data provided by the engine. */ void SummaryScreen::fillListBox() { // Clear previous data. mSnippetLabels.clear(); mSnippets.clear(); // For each snippet we need to know the article title. mSnippets = mWiki->getAllSnippetsWithTitle(); // Destroy the list view, and recreate it. if ( mListView != -1){ maWidgetDestroy(mListView); } mListView = createListView(mScreenWidth, 7*mScreenHeight/8); setWidgetProperty(mListView, MAW_WIDGET_BACKGROUND_COLOR, DARK_WHITE, 16); maWidgetAddChild(mMainLayout, mListView); // If no titles are selected, display a short message. if (mSnippets.size() == 0) { MAWidgetHandle listItem = maWidgetCreate(MAW_LIST_VIEW_ITEM); maWidgetSetProperty( listItem, MAW_LIST_VIEW_ITEM_TEXT, WARNING_NOTHING_SELECTED.c_str() ); maWidgetAddChild(mListView, listItem); } // Update the UI. for (int i=0; i < mSnippets.size(); i++) { MAWidgetHandle listItem = maWidgetCreate(MAW_LIST_VIEW_ITEM); maWidgetSetProperty( listItem, MAW_LIST_VIEW_ITEM_TEXT, mSnippets[i].c_str() ); mSnippetLabels.add(listItem); maWidgetAddChild(mListView,mSnippetLabels[i]); } }
/** * Evaluate JavaScript code in a WebView. * @param webViewHandle The MoSync handle to the WebView in which * to evaluate the script (this handle is an integer id). * @param script JavaScript string. */ void HybridMoblet::callJS( MAWidgetHandle webViewHandle, const MAUtil::String& script) { // Call the JavaScript code on the WebView. MAUtil::String url = "javascript:" + script; maWidgetSetProperty( webViewHandle, MAW_WEB_VIEW_URL, url.c_str()); }
/** * Utility function to create a button. * Create a button with centered text. * By default, font size is 15, and color DARK GREY. * @param text The text for the button. * @param fontColor The font color. * @param fontSize The font size. * @param width The widget width. * @param height The widget height. */ MAWidgetHandle createButton(const char* text, int fontColor, int fontSize, int width, int height) { MAWidgetHandle button = maWidgetCreate(MAW_BUTTON); maWidgetSetProperty(button, MAW_BUTTON_TEXT, text); // Set the widget size. setWidgetProperty(button, MAW_WIDGET_WIDTH, width); setWidgetProperty(button, MAW_WIDGET_HEIGHT, height); maWidgetSetProperty( button, MAW_BUTTON_TEXT_VERTICAL_ALIGNMENT, MAW_ALIGNMENT_CENTER); maWidgetSetProperty( button, MAW_BUTTON_TEXT_HORIZONTAL_ALIGNMENT, MAW_ALIGNMENT_CENTER); setWidgetProperty(button,MAW_BUTTON_FONT_SIZE, fontSize); setWidgetProperty(button,MAW_BUTTON_FONT_COLOR, fontColor, 16); return button; }
MAWidgetHandle createWebView() { // Create the WebView MAWidgetHandle webView = maWidgetCreate("WebView"); // Set size of the WebView to fill the parent. maWidgetSetProperty(webView, "width", "-1"); maWidgetSetProperty(webView, "height", "-1"); // Enable zooming. maWidgetSetProperty(webView, "enableZoom", "true"); // Get the HTML for the page from a resource. MAUtil::String html = Util::createTextFromHandle(INDEX_HTML); // Set the HTML the WebView displays. maWidgetSetProperty(webView, "html", html.c_str()); return webView; }
int NativeScreen::handleImage(MAHandle myImage) { printf("handleImage(%d)", myImage); char buffer[256]; sprintf(buffer, "%d", myImage); printf("imgHandle=%s---hImage=%d", buffer, myImage); // display the preview int resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, buffer); return resCode; }
int NativeScreen::handleImageData(MAHandle myImageData) { printf("handleImageData(%d)", myImageData); int resCode = -1; MAHandle hImage = maCreatePlaceholder(); int dataSize = maGetDataSize(myImageData); int createImageRes = maCreateImageFromData(hImage, myImageData, 0, dataSize); // Used for testing only. MAUtil::String info = "Ready.Size = " + MAUtil::integerToString(dataSize) + " res = " + MAUtil::integerToString(createImageRes); printf("\n%s\n", info.c_str()); if (createImageRes != RES_OK) { // If the Android VM gets an out of memory exception, get the image handle instead. maAlert("Memory Warning", " The image cannot be created. Try again", NULL, NULL, NULL); maWidgetSetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, "false"); maImagePickerOpen(); } else { char imgHandle[256]; sprintf(imgHandle, "%d", hImage); printf("imgHandle=%s---hImage=%d", imgHandle, hImage); // display the preview resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, imgHandle); } // at this point the new selected image is either displayed or non-existent (out of memory) // the former displayed image (if exists) can be now safely deleted and reused if (mLastDisplayedImageHandle != -1) { maDestroyPlaceholder(mLastDisplayedImageHandle); } mLastDisplayedImageHandle = hImage; return resCode; }
void customEvent(const MAEvent& event) { if(event.type == EVENT_TYPE_WIDGET) { MAWidgetEventData* data = (MAWidgetEventData*)event.data; // maybe we need to send an event whenever a gl // surface is recreated (and textures have to be uploaded etc.) if( data->eventType == MAW_EVENT_GL_VIEW_READY ) { maWidgetSetProperty(data->widgetHandle, "bind", ""); mMoblet->init(); mMoblet->startDrawing(); mWasDrawing = true; } } }
/** * Method that implements the custom event listener interface. * Widget events are sent as custom events. */ void customEvent(const MAEvent& event) { // Check if this is a widget event. if (EVENT_TYPE_WIDGET == event.type) { // Get the widget event data structure. MAWidgetEventData* eventData = (MAWidgetEventData*) event.data; // MAW_EVENT_GL_VIEW_READY is sent when the GL view is // ready for drawing. if (MAW_EVENT_GL_VIEW_READY == eventData->eventType) { // Associate the OpenGL context with the GLView. maWidgetSetProperty(mGLView, MAW_GL_VIEW_BIND, ""); // Create the texture we will use for rendering. createTexture(); // Set the GL viewport. int viewWidth = widgetGetPropertyInt(mGLView, MAW_WIDGET_WIDTH); int viewHeight = widgetGetPropertyInt(mGLView, MAW_WIDGET_HEIGHT); setViewport(viewWidth, viewHeight); // Initialize OpenGL. initGL(); // Flag that the GLView has been initialized. mGLViewInitialized = true; // Draw the initial scene. draw(); // Start timer that will redraw the scene. // This calls runTimerEvent each 20 ms. MAUtil::Environment::getEnvironment().addTimer(this, 20, -1); } } }
static void ogl_updateScreen(void) { if(sNativeUIOpenGLView == -1) maUpdateScreen(); else maWidgetSetProperty(sNativeUIOpenGLView, "invalidate", ""); }
/** * The purpose of this test is to test all properties that * you can set on a screen. */ void testIconProperty() { TESTIFY_ASSERT_EQUAL( maWidgetSetProperty( getTestWidgetHandle( ), MAW_LIST_VIEW_ITEM_ICON, getIconHandleAsString( ) ), MAW_RES_OK ); }