S60 3rd Edition SDK for Symbian OS
Example Applications Guide

Bitmapecominst.cpp

00001 /**
00002 * =============================================================================
00003 *  Name        : Bitmapecominst.cpp
00004 *  Part of     : Plugin test
00005 *  Interface   : Browser Plug-in API
00006 *  Description : Example for developing a plug-in
00007 *  Version     : 1.0
00008 *
00009 *  Copyright (c) 2005-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * =============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include <e32std.h>
00018 #include <coecntrl.h>
00019 #include <coeinput.h>
00020 #include "bitmapinst.h"
00021 #include "bitmapecommain.h"
00022 #include <allbitmaps.mbg>
00023 
00024 // CONSTANTS
00025 
00026 _LIT(KSourceUrl, "sourceurl");
00027 
00028 
00029 // ============================================================================
00030 // CBitmapInst Class
00031 // ============================================================================
00032 
00033 
00034 // ----------------------------------------------------------------------------
00035 // Two phase construction.
00036 //
00037 // @param aNpp - plug-in's opaque instance handle.
00038 // @return     - plug-in object.
00039 // ----------------------------------------------------------------------------
00040 CBitmapInst* CBitmapInst :: NewL( NPP aNpp )
00041     {
00042     CBitmapInst* self = new ( ELeave ) CBitmapInst( aNpp );
00043     CleanupStack :: PushL( self );
00044     self->Construct();
00045     CleanupStack :: Pop();
00046     return self;
00047     }
00048 
00049 
00050 // ----------------------------------------------------------------------------
00051 // Constructor
00052 // ----------------------------------------------------------------------------
00053 CBitmapInst :: CBitmapInst( NPP aNpp ) : CCoeControl(), iNpp( aNpp )
00054     {
00055     iWriteReady     = 100;
00056     iSourceUrl      = NULL;
00057     iFirstTime      = ETrue;
00058     iFlipBitmap     = EFalse;
00059     iNumDownload    = 0;
00060     }
00061 
00062 
00063 // ----------------------------------------------------------------------------
00064 // second phase constructor
00065 // ----------------------------------------------------------------------------
00066 void CBitmapInst :: Construct( void )
00067     {
00068     return;
00069     }
00070 
00071 
00072 // ----------------------------------------------------------------------------
00073 // Destructor
00074 // ----------------------------------------------------------------------------
00075 CBitmapInst :: ~CBitmapInst( void )
00076     {
00077     if ( iFileName )
00078         {
00079             delete iFileName;
00080             iFileName = NULL;
00081         }
00082     CBitmapEcomMain* npm = ( CBitmapEcomMain* ) Dll :: Tls();
00083     delete iSourceUrl;
00084     }
00085 
00086 
00087 // ----------------------------------------------------------------------------
00088 // This function creates a new instance of the plug-in.
00089 //
00090 // @param aPluginType - the MIME type.
00091 // @param aInstance   - the plug-in instance.
00092 // @param aMode       - the mode
00093 // @param aArgn       - attribute of the <object> tag; the names
00094 // @param aArgv       - attribute of the <object> tag; the values
00095 // @param aSaved      - this parameter is not supported.
00096 // @return            - NPError status code.
00097 // ----------------------------------------------------------------------------
00098 NPError CBitmapInst :: PluginNew( 
00099                             NPMIMEType /*aPluginType*/, 
00100                             NPP aInstance, 
00101                             uint16 /*aMode*/, 
00102                             CDesC16Array* aArgn, 
00103                             CDesC16Array* aArgv, 
00104                             NPSavedData* /*aSaved*/ )
00105     {
00106     if ( aInstance->pdata != this )
00107         {
00108         return NPERR_INVALID_INSTANCE_ERROR;
00109         }
00110 
00111     // Traverse the attribute list to find the Url from which to retrieve data
00112     for ( TInt i = 0; i < aArgn->Count(); i++ )
00113         {
00114         TPtrC16 namePtr( (*aArgn)[i] );
00115         TPtrC16 valPtr( (*aArgv)[i] );
00116 
00117         if ( ( KSourceUrl().FindF( namePtr ) == 0 ) )
00118             {
00119             iSourceUrl = new TUint16[valPtr.Length() + 1];
00120             if ( iSourceUrl == NULL )
00121                 {
00122                 return NPERR_OUT_OF_MEMORY_ERROR;
00123                 }
00124             TPtr16 ptr( iSourceUrl, 0, valPtr.Length() + 1 );
00125             ptr.Copy( valPtr );
00126             ptr.ZeroTerminate();
00127             break;
00128             }
00129         }
00130     return HandleGet();
00131     }
00132 
00133 
00134 // ----------------------------------------------------------------------------
00135 // This function deletes a plug-in instance.
00136 // 
00137 // @param aSave - this parameter is not supported.
00138 // @return      - NPError status code.
00139 // ----------------------------------------------------------------------------
00140 NPError CBitmapInst :: PluginDestroy( NPSavedData** /*aSave*/ )
00141     {
00142     delete this;
00143     return NPERR_NO_ERROR;
00144     }
00145 
00146 
00147 // ----------------------------------------------------------------------------
00148 // This function sets the parent and the coordinates for the plug-in. 
00149 // The first time PluginSetWindowL() is called, it is 
00150 // important to construct the corresponding plug-in control. The other times 
00151 // this call is made, it is expected that the function simply updates the 
00152 // bounds of the control.
00153 //
00154 // @param aWindow - a plug-in window structure that contains window coordinates
00155 //                  and platform specific window information.
00156 // @return        - an error code, on success returns NPERR_NO_ERROR.
00157 // ----------------------------------------------------------------------------
00158 NPError CBitmapInst :: PluginSetWindowL( NPWindow* aWindow )
00159     {
00160     MPluginAdapterPTR pPluginAdapter( NULL );
00161     pPluginAdapter = static_cast<MPluginAdapter*>( aWindow->window );
00162     CCoeControl* pParentControl = pPluginAdapter->GetParentControl();
00163 
00164         if ( !pParentControl )
00165                 return NPERR_NO_ERROR;
00166 
00167     if ( iFirstTime )
00168         {
00169             CreateWindowL( pParentControl );
00170         pPluginAdapter->SetPluginNotifier( this );
00171         pPluginAdapter->PluginConstructedL( this );
00172         }
00173   
00174     TPoint point( aWindow->x, aWindow->y );
00175     TSize size( aWindow->width, aWindow->height );
00176     SetExtent( point, size );
00177   
00178     if ( iFirstTime )
00179         {
00180         ActivateL();
00181         iFirstTime = EFalse;
00182         }
00183 
00184     return NPERR_NO_ERROR;
00185     }
00186 
00187 
00188 // ----------------------------------------------------------------------------
00189 // This function has an empty implementation here, but the actual purpose of 
00190 // the function is to set values for the plugin variables. 
00191 //
00192 // @param aVariable - the variable whose value is to be set.
00193 // @param aValue    - pointer to the 32-bit parameter that contains the value.
00194 // @return          - NPError status code.
00195 // ----------------------------------------------------------------------------
00196 NPError CBitmapInst :: PluginSetValue( 
00197                             NPNVariable /*aVariable*/, 
00198                             void* /*aValue*/ )
00199     {
00200     return NPERR_NO_ERROR;
00201     }
00202 
00203 
00204 // ----------------------------------------------------------------------------
00205 // This function notifies a plug-in instance of a new data stream.
00206 // 
00207 // @param aMimeType - the MIME type of the stream.
00208 // @param aStream   - the new stream object.
00209 // @param aSeekable - flag that indicates whether or not stream is searchable.
00210 // @param aStype    - the type of the stream. The plug-in sets the stream type.
00211 //                    Currently supported stream types are:     
00212 //                        NP_NORMAL
00213 //                        NP_ASFILE
00214  //                       NP_ASFILEONLY
00215 // @return          - NPError status code.
00216 // ----------------------------------------------------------------------------
00217 NPError CBitmapInst :: PluginNewStream( 
00218                             NPMIMEType /*aMimeType*/, 
00219                             NPStream* aStream, 
00220                             NPBool /*aSeekable*/, 
00221                             uint16* aStype )
00222     {
00223     *aStype         = NP_ASFILE;
00224     iStream         = aStream;
00225     iStream->pdata  = this;
00226     return NPERR_NO_ERROR;
00227     }
00228 
00229 
00230 // ----------------------------------------------------------------------------
00231 // This function destroys the stream that was previously created to stream data
00232 // to the plug-in.
00233 //
00234 // @param aStream - the stream to be destroyed.
00235 // @param aReason - the reason for destroying the stream. Possible values are:
00236 //                      NPRES_DONE - normal completion and all data was sent to
00237 //                      the instance. 
00238 //                      NPRES_USER_BREAK - the user canceled the stream
00239 //                      NPRES_NETWORK_ERR - stream failed because of problems 
00240 //                      with the network, disk I/O error, lack of memory, or 
00241 //                      some other problem.        
00242 // @return        - NPError status code.
00243 // ----------------------------------------------------------------------------
00244 NPError CBitmapInst :: PluginDestroyStream( 
00245                                 NPStream* /*aStream*/, 
00246                                 NPReason /*aReason*/ )
00247     {
00248     return NPERR_NO_ERROR;
00249     }
00250 
00251 
00252 // ----------------------------------------------------------------------------
00253 // This function passes a file name to the plug-in in which the stream data 
00254 // is stored. 
00255 //
00256 // @param aStream   - the stream
00257 // @param aFileName - the file name
00258 // ----------------------------------------------------------------------------
00259 void CBitmapInst :: PluginStreamAsFile( 
00260                             NPStream* aStream, 
00261                             const TDesC16& aFileName )
00262     {
00263     if ( ( aStream->pdata != this ) || ( aStream != iStream ) 
00264                                     || ( iNumDownload > 0 ) )
00265         {
00266         return;
00267         }    
00268     iFileName = aFileName.Alloc();
00269     iNumDownload ++;
00270     }
00271 
00272 
00273 // ----------------------------------------------------------------------------
00274 // This function writes a chunk of data to the plug-in.
00275 //
00276 // @param aStream - the stream
00277 // @param aOffset - the offset in the stream.
00278 // @param aLength - the size of the new data.
00279 // @param aBuffer - the data.
00280 // @return        - the number of bytes consumed by the plug-in instance.
00281 // ----------------------------------------------------------------------------
00282 int32 CBitmapInst :: PluginWrite( 
00283                         NPStream* aStream, 
00284                         int32 /*aOffset*/, 
00285                         int32 aLength, 
00286                         void* /*aBuffer*/ )
00287     {
00288     if ( aStream->pdata != this || aStream != iStream )
00289         {
00290         return 0;
00291         }
00292 
00293     return aLength;
00294     }
00295 
00296 
00297 // ----------------------------------------------------------------------------
00298 // The browser calls the NPP_Write function with the amount of data returned 
00299 // from the NPP_WriteReady function.
00300 //
00301 // @param aStream - the stream
00302 // @return        - maximum data size that the plug-in can handle.
00303 // ----------------------------------------------------------------------------
00304 int32 CBitmapInst :: PluginWriteReady( NPStream* /*aStream*/ )
00305     {
00306     return iWriteReady;
00307     }
00308 
00309 
00310 // ----------------------------------------------------------------------------
00311 // The browser calls the NPP_URLNotify function to notify the plug-in of the 
00312 // completion of a URL request made by the NPN_GetURLNotify function or the 
00313 // NPN_PostURLNotify function.
00314 //
00315 // @param aUrl        - url of the NPN_GetURLNotify function or of the 
00316 //                      NPN_PostURLNotify function request.
00317 // @param aReason     - reason code for completion of the request.
00318 // @param aNotifyData - contains the private plug-in data passed to the 
00319 //                      corresponding call to the NPN_GetURLNotify function.
00320 // ----------------------------------------------------------------------------
00321 void CBitmapInst :: PluginURLNotify( 
00322                         const TDesC16& /*aUrl*/, 
00323                         NPReason       /*aReason*/, 
00324                         void*          /*aNotifyData*/ )
00325     {
00326     return;
00327     }
00328 
00329 
00330 // ----------------------------------------------------------------------------
00331 // This function draws the control. When iFlipBitmap is true it renders one 
00332 // bitmap, and when iFlipBitmap is false it renders another one.
00333 //
00334 // @param aRect - The region of the control to be redrawn. Co-ordinates are 
00335 //                relative to the control's origin (top left corner).
00336 // ----------------------------------------------------------------------------
00337 void CBitmapInst :: Draw( const TRect& aRect ) const
00338     {   
00339     TInt ret( KErrNone );
00340     CFbsBitmap* bitmap = NULL;
00341   
00342     bitmap = new CFbsBitmap();
00343     if ( !bitmap )
00344         {
00345         return;
00346         }
00347 
00348     if ( iFileName )
00349                 {
00350                 if( iFlipBitmap )
00351                         {
00352                         ret = bitmap->Load( iFileName->Des(), EMbmAllbitmapsRedflower, ETrue );
00353                         }
00354                 else 
00355                         {
00356                         ret = bitmap->Load( iFileName->Des(), EMbmAllbitmapsBlueflower, ETrue );
00357                         }
00358                 }
00359     if ( ret != KErrNone )
00360         {
00361         delete ( bitmap );
00362         bitmap = NULL;
00363         return;
00364         }
00365 
00366     SystemGc().Clear();
00367     SystemGc().DrawBitmap( aRect, bitmap );
00368     delete ( bitmap );
00369     bitmap = NULL;
00370     }
00371 // ----------------------------------------------------------------------------
00372 // This function implements the NotifyL() function of the MPluginNotifier 
00373 // interface. It notifies the plug-in of an event. 
00374 //
00375 // @param aCallType - the event type that is passed to the plug-in. 
00376 //                    Possible values are :
00377 //                    EEditCut, 
00378 //                    EEditCopy, 
00379 //                    EEditPaste, 
00380 //                    EEditDelete, 
00381 //                    EApplicationFocusChanged, 
00382 //                    ESystemNotification
00383 // @param aParam    - the parameter associated with the event.
00384 // @return          - not used.
00385 // ----------------------------------------------------------------------------
00386 TInt CBitmapInst :: NotifyL( TNotificationType aCallType, TAny* /*aParam*/ )
00387     {
00388     switch ( aCallType )
00389         {
00390         case EApplicationFocusChanged:
00391             {
00392                         if ( iFlipBitmap )
00393                 {
00394                         iFlipBitmap = EFalse;
00395                 }
00396                 else 
00397                 {
00398                         iFlipBitmap = ETrue;
00399                 }
00400             DrawNow();
00401             break;
00402             }
00403         default:
00404             break;
00405         }
00406     return 0;
00407     }
00408 
00409 
00410 // ----------------------------------------------------------------------------
00411 // Returns the supported input capabilities which correspond to the behaviour 
00412 // of the OfferKeyEventL() function of the control; 
00413 //
00414 // @return - TCoeInputCapabilities::EAllText which supports all types of text.
00415 // ----------------------------------------------------------------------------
00416 
00417 TCoeInputCapabilities CBitmapInst :: InputCapabilities ( void ) const
00418     {
00419         return TCoeInputCapabilities( TCoeInputCapabilities::EAllText );
00420     }
00421 
00422 
00423 // ----------------------------------------------------------------------------
00424 // This function handles key events. when 'Enter' key or 'OK' key is pressed, 
00425 // the bitmap rendered is flipped from redflower to blueflower and viceversa.
00426 //
00427 // @param aKeyEvent - the key event.
00428 // @param aType     - type of key event:EEventKey, EEventKeyUp or EEventKeyDown
00429 // @return          - the function returns EKeyWasNotConsumed if it does not do 
00430 //                    anything in response to a key event. If it is able to 
00431 //                    process the event it returns EKeyWasConsumed.
00432 // ----------------------------------------------------------------------------
00433 TKeyResponse CBitmapInst :: OfferKeyEventL( 
00434                                 const TKeyEvent& aKeyEvent, 
00435                                 TEventCode aType )
00436     {
00437         if ( aType == EEventKeyUp )
00438             {
00439                 switch ( aKeyEvent.iScanCode )
00440                     {
00441                     case EStdKeyEnter:
00442                     case EStdKeyDevice3:
00443                 {
00444                                 if ( iFlipBitmap )
00445                     {
00446                             iFlipBitmap = EFalse;
00447                     }
00448                     else 
00449                     {
00450                             iFlipBitmap = ETrue;
00451                     }
00452                             DrawNow();
00453                             return EKeyWasConsumed;
00454                 }
00455                     default:
00456                             return EKeyWasNotConsumed;
00457                     }
00458             }
00459         return EKeyWasNotConsumed;
00460     }
00461 
00462 
00463 // ----------------------------------------------------------------------------
00464 // This function inherited from MCoeControlObserver handles an event from an 
00465 // observed control.
00466 //
00467 // @param aControl   - pointer to the control from which the event originated.
00468 // @param aEventType - the event type.
00469 // ----------------------------------------------------------------------------
00470 void CBitmapInst :: HandleControlEventL( 
00471                             CCoeControl* aControl,
00472                             TCoeEvent aEventType )
00473     {
00474     if ( aControl != (( MPluginAdapter* ) ( iNpp->ndata ))->GetParentControl() )
00475         {
00476         return;
00477         }
00478 
00479     switch ( aEventType )
00480         {
00481         case EEventRequestFocus:
00482             default:
00483                 break;
00484         }
00485     }
00486 
00487 
00488 // ----------------------------------------------------------------------------
00489 // This function handles pointer events. On clicking the left mouse button, 
00490 // the bitmap rendered is flipped from redflower to blueflower and viceversa.
00491 //
00492 // @param aPointerEvent - The pointer event.
00493 // ----------------------------------------------------------------------------
00494 void CBitmapInst :: HandlePointerEventL( const TPointerEvent& aPointerEvent )
00495     {
00496     switch ( aPointerEvent.iType )
00497         {
00498         case TPointerEvent::EButton1Down:
00499             {
00500                         if ( iFlipBitmap )
00501                 {
00502                         iFlipBitmap = EFalse;
00503                 }
00504                 else 
00505                 {
00506                         iFlipBitmap = ETrue;
00507                 }
00508                 DrawNow();
00509                 break;
00510             }       
00511         default:
00512                 break;
00513         }
00514     }
00515 
00516 
00517 // ----------------------------------------------------------------------------
00518 // This function is called whenever a control gains or loses focus - as a 
00519 // result of a call to SetFocus(). 
00520 //
00521 // @param aDrawNow  - Contains the value that was passed to it by SetFocus().
00522 // ----------------------------------------------------------------------------
00523 void CBitmapInst :: FocusChanged( TDrawNow /*aDrawNow*/ )
00524     {
00525     DrawNow();
00526     }
00527 
00528 
00529 // ----------------------------------------------------------------------------
00530 // This function which responds to size changes to set the size and position of 
00531 // the contents of the control does nothing and has an empty implementation.
00532 // ----------------------------------------------------------------------------
00533 void CBitmapInst :: SizeChanged( void )
00534     {
00535     return;
00536     }
00537 
00538 
00539 // ----------------------------------------------------------------------------
00540 // This function calls the NPN_GetURL function to ask the browser to deliver 
00541 // the data to the plug-in instance in a new stream.
00542 //
00543 // @return - if the url is NULL returns NPERR_GENERIC_ERROR, otherwise returns
00544 //           NPError status code from calling the NPN_GetURL function.
00545 // ----------------------------------------------------------------------------
00546 NPError CBitmapInst :: HandleGet( void )
00547     {
00548     CBitmapEcomMain* npm = ( CBitmapEcomMain* ) Dll :: Tls();
00549     TPtrC url;
00550 
00551     if ( iSourceUrl ) // iSourceUrl is non-null
00552         {
00553         url.Set( iSourceUrl );
00554         return ( npm->Funcs()->geturl( iNpp, url, NULL ) );
00555         }
00556     else
00557         {
00558         return NPERR_GENERIC_ERROR;
00559         }
00560     }
00561 

© Nokia 2006

Back to top