Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Requesting notification of language change

Applications may need to be notified when the device language is changed. Applications can register a callback function using the Brew MP ISetting_OnChange() function. The registered callback function will be called when the language setting changes.

The examples below are taken from c_localizedapp, which is in samplecode directory provided by the Brew MP SDK.

The following example shows how to register for language change notifications:

         
    int nRes;
    ISignalCBFactory * piSignalCBFactory = NULL;

    // Create temporary signal factory object:
    nRes = ISHELL_CreateInstance(pMe->piShell,
                                 AEECLSID_SignalCBFactory,
                                 (void **)&piSignalCBFactory);

    if (AEE_SUCCESS != nRes) { // If error occurred, then cleanup:
        DBGPRINTF("Failed to create ISignalCBFactory object");
        goto cleanup;
    }

    // Create signal and signal-control objects associated with callback function:
    nRes = ISignalCBFactory_CreateSignal( piSignalCBFactory,
				    MyApp_OnLanguageChange,
				    (void *)pMe,
				    &pMe->piSignal,       // ISignal object.
				    &pMe->piSignalCtl );  // Controlling interface for ISignal object.

    if (AEE_SUCCESS != nRes) { // If error occurred, then cleanup:
        DBGPRINTF("Failed to create language change signal");
        goto cleanup;
    }


    // Register signal object so we're notified when the active device 
    // language setting is changed:
    return ISettings_OnChange( pMe->piSettings,
				     ISETTINGSKEY_LANGUAGE,
				     pMe->piSignal,          // ISignal object.
				     &pMe->pLangChangeReg);  // Registration object.
    


    ///////////////
    // Here is the error/cleanup code exit point that executes only if an error occurs.  
    // Since our C language Brew MP API calls use return codes instead of exception 
    // handling, this is one of the rare cases where the strongly discouraged goto 
    // statement allows uncluttered code, enhances code readability, and is thus 
    // less error prone than an equivalent construct with control structures.  
cleanup:
    // Release temporary signal CB factory object:
    if (NULL != piSignalCBFactory) {
        ISignalCBFactory_Release(piSignalCBFactory);
        piSignalCBFactory = NULL;
    }

    return nRes;

The following example shows how an example callback function, which redraws the screen and re-enables the signal to receive additional language change notifications:

static void MyApp_OnLanguageChange(void *pCx)
{
    MyApp * pMe = (MyApp *)pCx;
	
    // Redraw screen using resources from newly changed active device language:
    MyApp_DrawScreen(pMe);

    // So future callbacks can occur, re-enable the Signal given to ISettings
    // so it can listen if the setting changes again:
    ISignalCtl_Enable(pMe->piSignalCtl);
}

MyApp_DrawScreen() draws the text as follows:

    // Get font line heights for positioning calculations:
    nFontHeightNormal = IDisplay_GetFontMetrics(pMe->piDisplay, AEE_FONT_NORMAL, NULL, NULL);
    nFontHeightLarge  = IDisplay_GetFontMetrics(pMe->piDisplay, AEE_FONT_LARGE, NULL, NULL);

    /////////////
    // Draw world image caption text "<active language> Speaking Countries":

    // Attempt to load string from localized resource file into buffer:
    nStrLen = ISHELL_LoadResString(pMe->piShell, C_LOCALIZEDAPP_RES_FILE, 
                                  IDS_WORLD_TITLE, wszBuf, sizeof(wszBuf));
    
    // Set user-text color to black:
    oldTextColor = IDisplay_SetColor(pMe->piDisplay, CLR_USER_TEXT, RGB_BLACK);

    if (0 < nStrLen) {  // If string successfully loaded:
        // Center text within image width and make sure x-coordinate is at
        // least 4 pixels (doesn't go negative):
        int w = IDISPLAY_MeasureText(pMe->piDisplay, AEE_FONT_NORMAL, wszBuf);
        int x = MAX(4, (nImageWidth - w) / 2);

        IDisplay_DrawText(pMe->piDisplay, // Display instance.
				      AEE_FONT_NORMAL,    // Specified font.
				      wszBuf,             // String buffer to contain text.
				      -1,   // Automatically compute string length.
				      x,    // x-coordinate of top-left corner.
				      y,    // y-coordinate of top-left corner.
				      NULL, // No clipping.
                      IDF_TEXT_TRANSPARENT); // Transparent background.

        // Calculate vertical position of next line of text:
        y += nFontHeightNormal + 24;  // Font height plus spacing between lines.
    }


    //////////////
    // Draw center message:

    // Draw text "":
    if (MyApp_DrawText(pMe, IDS_ACTIVE_LANG, RGB_LIGHT_GRAY, AEE_FONT_LARGE, 22, y)) {
        // Since text was drawn, calculate vertical position of next line of text:
        y += nFontHeightLarge + spacing;  // Font height plus spacing between lines.
    }

    // Draw "is the active device language"
    if (MyApp_DrawText(pMe, IDS_IS_ACTIVE_LANG, RGB_GRAY, AEE_FONT_NORMAL, 22, y)) {
         y += nFontHeightNormal + spacing;  // Calc font height plus line spacing.
    }