/** * 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; } } } }
/** * Widget override. * Get a widget property as a string, setting also the result code. */ MAUtil::String FacebookWebView::getPropertyString( const MAUtil::String& property ) const { char *buffer = new char[BUFFER_SIZE]; int result = maWidgetGetProperty(this->getWidgetHandle(), property.c_str(), buffer, BUFFER_SIZE); MAUtil::String temp; if( result == MAW_RES_INVALID_STRING_BUFFER_SIZE) { delete []buffer; int newBufferSize = 2*BUFFER_SIZE; buffer = new char[newBufferSize]; result = maWidgetGetProperty(this->getWidgetHandle(), property.c_str(), buffer, newBufferSize); } if( result == RES_OK) { temp.append(buffer, BUFFER_SIZE); } delete []buffer; return temp; }
/** * Update wiki list of titles, with the ones selected by the user. */ void TitleScreen::updateWikiTitles() { // For each check box that is unchecked, mark the corresponding entry // in the engine. So that when displaying summary screen, those entries // will not be displayed. mWiki->clearHiddenIndexList(); char buf[6]; for (int i =0; i < mCheckBoxes.size(); i++) { maWidgetGetProperty(mCheckBoxes[i], MAW_CHECK_BOX_CHECKED, buf, 6); if ( strcmp(buf, "false") == 0 ){ mWiki->markAsHidden(i); } } }
//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 widget events. * @param event A MoSync event data structure. */ void NativeScreen::customEvent(const MAEvent& event) { // Get the information sent by the widget. MAWidgetEventData* widgetEventData = (MAWidgetEventData*) event.data; if ( event.type == EVENT_TYPE_IMAGE_PICKER) { if ( event.imagePickerState == 1 ) { char checkboxBuffer[BUF_SIZE]; maWidgetGetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, checkboxBuffer, BUF_SIZE); MAUtil::String value = MAUtil::lowerString(checkboxBuffer); int resCode = -1; if ( strcmp(value.c_str(),"true") == 0 ) { resCode = handleImageData(event.imagePickerItem); } else { resCode = handleImage(event.imagePickerItem); } if (resCode == RES_OK) setLabelText(mLabel, "Preview is available"); else setLabelText(mLabel, "Preview is not available"); } else { setLabelText(mLabel, "The user canceled the image selection"); } } // Check that the event was a click (touch) event. if (widgetEventData->eventType == MAW_EVENT_CLICKED) { // Handle the event emitted by the widget widgetClicked(widgetEventData->widgetHandle); } }
/** * Provide the checked categories in a string, separated by comma. */ MAUtil::String HomeScreen::getCheckedCategories() { char buf[6]; MAUtil::String categories=""; for (int i=0; i < mCategoryBoxes.size(); i++) { maWidgetGetProperty(mCategoryBoxes[i], MAW_CHECK_BOX_CHECKED, buf, 6); if ( strcmp(buf, "true") == 0) { if ( i == 0 ) { // All categories are selected, so do not add any search term. break; } // Add category name and commas. categories += "," + mCategoriesStrings[i]; } } return categories; }
/** * Handle widget click events. * @param widgetHandle The handle (reference) to the widget * that was clicked. */ void NativeScreen::widgetClicked(MAHandle widgetHandle) { // Exit the application when the exit button is clicked. if (widgetHandle == mExitButton) { TestMoblet::getInstance()->closeEvent(); } else if ( mButton == widgetHandle) { char checkboxBuffer[BUF_SIZE]; maWidgetGetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, checkboxBuffer, BUF_SIZE); MAUtil::String value = MAUtil::lowerString(checkboxBuffer); if ( strcmp(value.c_str(),"true") == 0 ) { maImagePickerOpenWithEventReturnType(MA_IMAGE_PICKER_EVENT_RETURN_TYPE_IMAGE_DATA); } else { maImagePickerOpen(); } } }
/* * Handle events on screen's widgets * NOTE: take care to dismiss the Keyboard until you press other buttons, * because it is not modal. */ void HomeScreen::widgetClicked(MAHandle widgetHandle) { if (widgetHandle == mSearchButton) { // Disable the search button until new search is made. maWidgetSetProperty(mSearchButton, MAW_WIDGET_ENABLED, "false"); // Close the edit box forced. maWidgetSetProperty(mEditBox, MAW_EDIT_BOX_SHOW_KEYBOARD, "false"); // Declare a buffer for the text contained in the edit box. char textBuffer[256]; // Get the text from the edit box and put it into the buffer. int textLength = maWidgetGetProperty(mEditBox, MAW_EDIT_BOX_TEXT, textBuffer, 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 || strcmp(textBuffer,"") == 0) { return; } MAUtil::String text = textBuffer; // Now we can perform a wiki search. // After the result is received and processed, go to next screen. if (text.size() > 2) { // Show the progress bar and it's label. maWidgetSetProperty(mProgressLabel,MAW_WIDGET_VISIBLE, "true"); maWidgetSetProperty(mProgressBar, MAW_WIDGET_VISIBLE, "true"); // This label is indicating that a search action is in progress. maWidgetSetProperty( mProgressLabel, MAW_LABEL_TEXT, MESSAGE_WAITNOTE.c_str()); // Initiate the search with a result limit. mWiki->search(text, mSliderValue); } } else if ( mCategoryBoxes[0] == widgetHandle) { // If All category is selected, all the other ones are selected also. char buf[6]; maWidgetGetProperty(mCategoryBoxes[0], MAW_CHECK_BOX_CHECKED, buf, 6); // make sure the first 4 chars are lower case so we can have our strcmp with "true" for(int i = 0; i < 4; i++) { buf[i] = tolower(buf[i]); } if (strcmp(buf,"true") == 0) { // Check the other ones. for (int i=1; i < mCategoryBoxes.size(); i++) { maWidgetSetProperty(mCategoryBoxes[i], MAW_CHECK_BOX_CHECKED, "true"); } } else { // Uncheck all. for (int i=1; i < mCategoryBoxes.size(); i++) { maWidgetSetProperty(mCategoryBoxes[i], MAW_CHECK_BOX_CHECKED, "false"); } } } else { // If any other category then All is unchecked, than uncheck All also. for (int i=1; i< mCategoryBoxes.size(); i++) { if ( mCategoryBoxes[i] == widgetHandle ) { char buf[6]; maWidgetGetProperty (mCategoryBoxes[i], MAW_CHECK_BOX_CHECKED, buf, 6); if (strcmp(buf,"false") == 0) { maWidgetSetProperty( mCategoryBoxes[0], MAW_CHECK_BOX_CHECKED, "false"); } break; } } } }
/** * Handle widget events. * @param event A MoSync event data structure. */ void NativeScreen::customEvent(const MAEvent& event) { // Get the information sent by the widget. MAWidgetEventData* widgetEventData = (MAWidgetEventData*) event.data; if ( event.type == EVENT_TYPE_IMAGE_PICKER) { if ( event.imagePickerState == 1 ) { // ready, get handle MAHandle myImage = event.imagePickerItem; char checkboxBuffer[BUF_SIZE]; maWidgetGetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, checkboxBuffer, BUF_SIZE); MAUtil::String value = MAUtil::lowerString(checkboxBuffer); if ( strcmp(value.c_str(),"true") == 0 ) { MAHandle hImage = maCreatePlaceholder(); int dataSize = maGetDataSize(event.imagePickerItem); int createImageRes= maCreateImageFromData(hImage, event.imagePickerItem, 0, dataSize); // Used for testing only. MAUtil::String info = "Ready.Size = " + MAUtil::integerToString(dataSize) + "res = " + MAUtil::integerToString(createImageRes) + ", mime type = " + MAUtil::integerToString(event.imagePickerEncodingType); if ( createImageRes != RES_OK ) { maAlert("Memory Warning", " The image cannot be created. Try again", NULL, NULL, NULL); maWidgetSetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, "false"); // If the Android VM gets an out of memory exception, get the image handle instead. maImagePickerOpen(); } else { char imgHandle[256]; sprintf(imgHandle, "%d", hImage); int resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, imgHandle); } maDestroyPlaceholder(hImage); } else { char buffer[256]; sprintf(buffer, "%d", myImage); int resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, buffer); } setLabelText(mLabel, "Preview is available"); } else { setLabelText(mLabel, "The user canceled the image selection"); } } // Check that the event was a click (touch) event. if (widgetEventData->eventType == MAW_EVENT_CLICKED) { // Handle the event emitted by the widget widgetClicked(widgetEventData->widgetHandle); } }
/** * Implementation of standard API exposed to JavaScript * This function is used to detect different messages from JavaScript * and call the respective function in MoSync. * * @return true if stream was handled, false if not. */ bool NativeUIMessageHandler::handleMessage(Wormhole::MessageStream& stream) { char buffer[1024]; printf("Getting the next action \n"); char * action = (char*)stream.getNext(); printf("action: %s\n", action); // Widget Handling Calls if(0 == strcmp("maWidgetCreate", action)) { char* widgetType = (char*)stream.getNext(); char* widgetID = (char*)stream.getNext(); char* callbackID = (char*)stream.getNext(); printf("maWidgetCreate: %s, %s, %s\n", widgetType, widgetID, callbackID); int numParams = stringToInteger(stream.getNext()); MAWidgetHandle widget = maWidgetCreate(widgetType); if(widget <= 0) { sprintf(buffer,"'%s', %d", callbackID, widget); sendNativeUIError(buffer); } else { if(numParams > 0) { for(int i = 0; i < numParams/2; i++) { char* property = (char*)stream.getNext(); char* value = (char*)stream.getNext(); printf("maWidgetSetProperty %s, %s\n", property, value); int res = maWidgetSetProperty(widget, property, value); if(res < 0) { printf("could not set property\n"); } else { printf("set property done\n"); } } } //We use a special callback for widget creation printf("calling CallBack \n"); sprintf( buffer, "mosync.nativeui.createCallback('%s', '%s', %d)", callbackID, widgetID, widget); printf("Done creatign the script %s\n", buffer); mWebView->callJS(buffer); printf("done Calling Callback"); } } else if(0 == strcmp("maWidgetDestroy", action)) { MAWidgetHandle widget = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetDestroy(widget); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetAddChild", action) ) { MAWidgetHandle parent = stringToInteger(stream.getNext()); MAWidgetHandle child = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetAddChild(parent, child); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetInsertChild", action)) { MAWidgetHandle parent = stringToInteger((char*)stream.getNext()); MAWidgetHandle child = stringToInteger((char*)stream.getNext()); int index = stringToInteger((char*)stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetInsertChild(parent, child, index); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetRemoveChild", action)) { MAWidgetHandle child = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetRemoveChild(child); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetModalDialogShow", action)) { MAWidgetHandle dialogHandle = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetModalDialogShow(dialogHandle); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetModalDialogHide", action)) { MAWidgetHandle dialogHandle = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetModalDialogHide(dialogHandle); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetScreenShow", action)) { MAWidgetHandle screenHandle = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetScreenShow(screenHandle); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetStackScreenPush", action)) { MAWidgetHandle stackScreen = stringToInteger(stream.getNext()); MAWidgetHandle newScreen = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetStackScreenPush(stackScreen, newScreen); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetStackScreenPop", action)) { MAWidgetHandle stackScreen = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetStackScreenPop(stackScreen); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetSetProperty", action)) { MAWidgetHandle widget = stringToInteger(stream.getNext()); char *property = (char*)stream.getNext(); char *value = (char*)stream.getNext(); char* callbackID = (char*)stream.getNext(); int res = maWidgetSetProperty(widget, property, value); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetGetProperty", action)) { char value[1024]; MAWidgetHandle widget = stringToInteger(stream.getNext()); char* property = (char*)stream.getNext(); char* callbackID = (char*)stream.getNext(); int res = maWidgetGetProperty(widget, property, value, 1024); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', '%s', '%s'", callbackID, property, value); sendNativeUISuccess(buffer); } } // Tell the WebView that we have processed the stream, so that // it can send the next one. char replyScript[1024]; char * mosyncCallBackId = (char*)stream.getNext(); if(mosyncCallBackId != NULL) { sprintf( replyScript, "mosync.bridge.reply(%s)", mosyncCallBackId); printf("calling general callback %s\n", replyScript); mWebView->callJS(replyScript); } return true; }
/** * Retrieve an integer property of a widget. * * @param handle Handle to the widget * @param property The property of the widget to get. * * @return The value of the property. */ int widgetGetPropertyInt(MAHandle handle, const char *property) { char buffer[256]; maWidgetGetProperty(handle, property, buffer, 256); return atoi(buffer); }