javauis/lcdui_akn/javalcdui/src.nokialcdui/CanvasGraphicsItem.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: JNI implementation of CanvasGraphicsItem class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INTERNAL INCLUDES
       
    20 #include "com_nokia_mid_ui_CanvasGraphicsItem.h"
       
    21 #include "CMIDCanvas.h" // userinclude in nokialcdui.component
       
    22 
       
    23 // EXTERNAL INCLUDES
       
    24 #include <MMIDCanvasGraphicsItem.h>
       
    25 #include <MMIDCanvasGraphicsItemPainter.h>
       
    26 #include <CMIDToolkit.h>
       
    27 #include <jutils.h>
       
    28 #include <jdebug.h>
       
    29 
       
    30 /**
       
    31  * Local helper function for creating the native side peer object for
       
    32  * CanvasGraphicsItem.
       
    33  *
       
    34  * @param aNativePeerHandle On return, contains the native peer's handle
       
    35  */
       
    36 LOCAL_C void CreateNativePeerL(
       
    37     CMIDToolkit* aToolkit,
       
    38     jobject aPeer,
       
    39     TInt* aNativePeerHandle,
       
    40     MMIDCanvasGraphicsItemPainter* aItemPainter)
       
    41 {
       
    42     // Get LCDUI component factory.
       
    43     MMIDComponentFactory* factory = aToolkit->ComponentFactory();
       
    44 
       
    45     // Create new graphics item component.
       
    46     MMIDCanvasGraphicsItem* graphicsItem = factory->CreateCanvasGraphicsItemL(
       
    47                                                aItemPainter);
       
    48 
       
    49     // Put the component to cleanup stack during the register operation.
       
    50     CleanupDisposePushL(graphicsItem);
       
    51 
       
    52     // Register component to the LCDUI.
       
    53     *aNativePeerHandle = aToolkit->RegisterComponentL(graphicsItem, aPeer);
       
    54 
       
    55     // Component can be popped from the cleanup stack.
       
    56     CleanupPopComponent(graphicsItem);
       
    57 }
       
    58 
       
    59 /*
       
    60  * Class:     com_nokia_mid_ui_CanvasGraphicsItem
       
    61  * Method:    _createNativePeer
       
    62  * Signature: (I)I
       
    63  */
       
    64 JNIEXPORT jint JNICALL
       
    65 Java_com_nokia_mid_ui_CanvasGraphicsItem__1createNativePeer(
       
    66     JNIEnv* aJniEnv,
       
    67     jobject aPeer,
       
    68     jint aToolkitHandle,
       
    69     jint aPainterHandle,
       
    70     jint /* aWidth */,
       
    71     jint /* aHeight */)
       
    72 {
       
    73     DEBUG("CanvasGraphicsItem.cpp - createNativePeer +");
       
    74 
       
    75     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
       
    76 
       
    77     MMIDCanvasGraphicsItemPainter* itemPainter =
       
    78         MIDUnhandObject<MMIDCanvasGraphicsItemPainter>(aPainterHandle);
       
    79 
       
    80     jobject peer = aJniEnv->NewWeakGlobalRef(aPeer);
       
    81     TInt handle = -1;
       
    82     TSize size;
       
    83 
       
    84     TInt error = toolkit->ExecuteTrap(
       
    85                      &CreateNativePeerL,
       
    86                      toolkit,
       
    87                      peer,
       
    88                      &handle,
       
    89                      itemPainter);
       
    90 
       
    91     if (error == KErrNone)
       
    92     {
       
    93         DEBUG(
       
    94             "CanvasGraphicsItem.cpp - createNativePeer");
       
    95     }
       
    96     else
       
    97     {
       
    98         // Global reference must be removed at this point if construction
       
    99         // failed for some reason.
       
   100         aJniEnv->DeleteWeakGlobalRef(static_cast< jweak >(peer));
       
   101     }
       
   102 
       
   103     DEBUG_INT("CanvasGraphicsItem.cpp - createNativePeer, error = %d", error);
       
   104 
       
   105     return (error != KErrNone ? error : handle);
       
   106 }
       
   107 
       
   108 
       
   109 /**
       
   110  * Local helper function for setting the size of a graphics item
       
   111  *
       
   112  * @param aGraphicsItem The graphics item object to be modified.
       
   113  * @param aWidth The width of the graphics item.
       
   114  * @param aHeight The height of the graphics item.
       
   115  * @param aHeightInRows Indicates if the height is presented in rows.
       
   116  */
       
   117 LOCAL_C void SetSizeL(
       
   118     MMIDCanvasGraphicsItem* aGraphicsItem,
       
   119     TInt aWidth,
       
   120     TInt aHeight)
       
   121 {
       
   122     aGraphicsItem->SetSizeL(aWidth, aHeight);
       
   123 }
       
   124 
       
   125 /*
       
   126  * Class:     com_nokia_mid_ui_CanvasGraphicsItem
       
   127  * Method:    _setSize
       
   128  * Signature: (IIII)I
       
   129  */
       
   130 JNIEXPORT jint JNICALL Java_com_nokia_mid_ui_CanvasGraphicsItem__1setSize(
       
   131     JNIEnv* /* aJniEnv */,
       
   132     jobject /* aPeer */,
       
   133     jint aToolkitHandle,
       
   134     jint aNativePeerHandle,
       
   135     jint aWidth,
       
   136     jint aHeight)
       
   137 {
       
   138     DEBUG("CanvasGraphicsItem.cpp - setSize +");
       
   139 
       
   140     CMIDToolkit* toolkit =
       
   141         JavaUnhand< CMIDToolkit >(aToolkitHandle);
       
   142 
       
   143     MMIDCanvasGraphicsItem* item =
       
   144         MIDUnhandObject< MMIDCanvasGraphicsItem >(aNativePeerHandle);
       
   145 
       
   146     TInt error = toolkit->ExecuteTrap(
       
   147                      &SetSizeL,
       
   148                      item,
       
   149                      aWidth,
       
   150                      aHeight);
       
   151 
       
   152     DEBUG("CanvasGraphicsItem.cpp - setSize -");
       
   153 
       
   154     return error;
       
   155 }
       
   156 
       
   157 /**
       
   158  * Local helper function for setting the parent of a graphics item
       
   159  *
       
   160  * @param aGraphicsItem The graphics item object to be modified.
       
   161  * @param aParent The parent to be set.
       
   162  */
       
   163 LOCAL_C void SetParentL(
       
   164     MMIDCanvasGraphicsItem* aGraphicsItem, MMIDComponent* aParent)
       
   165 {
       
   166     MMIDCustomComponentContainer* container = NULL;
       
   167     MDirectContainer* directContainer = NULL;
       
   168 
       
   169     if (aParent)
       
   170     {
       
   171         MMIDComponent::TType type(aParent->Type());
       
   172 
       
   173         __ASSERT_DEBUG(type == MMIDComponent::ECanvas ||
       
   174                        type == MMIDComponent::ECustomItem,
       
   175                        User::Invariant());
       
   176 
       
   177         // Use static cast instead of reinterpret_cast because
       
   178         // reinterpret_cast does not preform the conversion correctly.
       
   179         // static_cast is OK eventhough CMIDCanvas is non-sharable class.
       
   180         // We don't use its methods.
       
   181         container = static_cast< CMIDCanvas* >(aParent);
       
   182         directContainer = static_cast< CMIDCanvas* >(aParent);
       
   183     }
       
   184 
       
   185     aGraphicsItem->SetParentL(container);
       
   186     aGraphicsItem->SetDirectContainerL(directContainer);
       
   187 }
       
   188 
       
   189 /*
       
   190  * Class:     com_nokia_mid_ui_CanvasGraphicsItem
       
   191  * Method:    _setParent
       
   192  * Signature: (III)I
       
   193  */
       
   194 JNIEXPORT jint JNICALL Java_com_nokia_mid_ui_CanvasGraphicsItem__1setParent(
       
   195     JNIEnv* /* aJniEnv */,
       
   196     jobject /* aPeer */,
       
   197     jint aToolkitHandle,
       
   198     jint aNativePeerHandle,
       
   199     jint aParentHandle)
       
   200 {
       
   201     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
       
   202     MMIDComponent* parent = NULL;
       
   203 
       
   204     MMIDCanvasGraphicsItem* item =
       
   205         MIDUnhandObject<MMIDCanvasGraphicsItem>(aNativePeerHandle);
       
   206 
       
   207     if (aParentHandle)
       
   208     {
       
   209         parent = MIDUnhandObject< MMIDComponent >(aParentHandle);
       
   210     }
       
   211 
       
   212     TInt error = toolkit->ExecuteTrap(
       
   213                      &SetParentL,
       
   214                      item,
       
   215                      parent);
       
   216 
       
   217     DEBUG_INT("CanvasGraphicsItem.cpp - setParent, error = %d", error);
       
   218 
       
   219     return error;
       
   220 }
       
   221 
       
   222 /**
       
   223  * Local helper function for setting a graphics item visible.
       
   224  *
       
   225  * @param aItem The graphics item to set visible.
       
   226  */
       
   227 LOCAL_C void SetVisibleL(MMIDCanvasGraphicsItem* aItem, TBool aVisible)
       
   228 {
       
   229     aItem->SetVisibleL(aVisible);
       
   230 }
       
   231 
       
   232 /*
       
   233  * Class:     com_nokia_mid_ui_CanvasGraphicsItem
       
   234  * Method:    _show
       
   235  * Signature: (II)I
       
   236  */
       
   237 JNIEXPORT jint JNICALL Java_com_nokia_mid_ui_CanvasGraphicsItem__1setVisible(
       
   238     JNIEnv* /* aJniEnv */,
       
   239     jobject /* aPeer */,
       
   240     jint aToolkitHandle,
       
   241     jint aNativePeerHandle,
       
   242     jboolean aVisible)
       
   243 {
       
   244     DEBUG_INT("CanvasGraphicsItem.cpp - setVisible +, aVisible=%d", aVisible);
       
   245 
       
   246     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
       
   247 
       
   248     MMIDCanvasGraphicsItem* item =
       
   249         MIDUnhandObject<MMIDCanvasGraphicsItem>(aNativePeerHandle);
       
   250 
       
   251     TInt error = toolkit->ExecuteTrap(
       
   252                      &SetVisibleL,
       
   253                      item,
       
   254                      (TBool)aVisible);
       
   255 
       
   256     DEBUG_INT("CanvasGraphicsItem.cpp - setVisible -, error=%d", error);
       
   257 
       
   258     return error;
       
   259 }
       
   260 
       
   261 /**
       
   262  * Local helper function for setting a graphics item's position.
       
   263  *
       
   264  * @param aItem The graphics item.
       
   265  * @param aX The x coordinate of the anchor point.
       
   266  * @param aY The y coordinate of the anchor point.
       
   267  */
       
   268 LOCAL_C void SetPosition(MMIDCanvasGraphicsItem* aItem, TInt aX, TInt aY)
       
   269 {
       
   270     aItem->SetPosition(aX, aY);
       
   271 }
       
   272 
       
   273 /*
       
   274  * Class:     com_nokia_mid_ui_CanvasGraphicsItem
       
   275  * Method:    _setPosition
       
   276  * Signature: (IIII)I
       
   277  */
       
   278 JNIEXPORT jint JNICALL Java_com_nokia_mid_ui_CanvasGraphicsItem__1setPosition(
       
   279     JNIEnv* /* aJniEnv */,
       
   280     jobject /* aPeer */,
       
   281     jint aToolkitHandle,
       
   282     jint aNativePeerHandle,
       
   283     jint aX,
       
   284     jint aY)
       
   285 {
       
   286     DEBUG("CanvasGraphicsItem.cpp - setPosition +");
       
   287 
       
   288     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
       
   289 
       
   290     MMIDCanvasGraphicsItem* item =
       
   291         MIDUnhandObject<MMIDCanvasGraphicsItem>(aNativePeerHandle);
       
   292 
       
   293     toolkit->ExecuteV(&SetPosition, item, aX, aY);
       
   294 
       
   295     DEBUG("CanvasGraphicsItem.cpp - setPosition -");
       
   296 
       
   297     return KErrNone;
       
   298 }
       
   299 
       
   300 
       
   301 /**
       
   302  * Local helper function for setting the elevation of a graphics item.
       
   303  *
       
   304  * @param aGraphicsItem The graphics item object to be modified.
       
   305  * @param aZ The z-position defining the elevation.
       
   306  */
       
   307 LOCAL_C void SetElevationL(MMIDCanvasGraphicsItem* aGraphicsItem, TInt aZ)
       
   308 {
       
   309     aGraphicsItem->SetElevationL(aZ);
       
   310 }
       
   311 
       
   312 /*
       
   313  * Class:     com_nokia_mid_ui_CanvasGraphicsItem
       
   314  * Method:    _setZPosition
       
   315  * Signature: (III)I
       
   316  */
       
   317 JNIEXPORT jint JNICALL Java_com_nokia_mid_ui_CanvasGraphicsItem__1setZPosition(
       
   318     JNIEnv* /* aJniEnv */,
       
   319     jobject /* aPeer */,
       
   320     jint aToolkitHandle,
       
   321     jint aNativePeerHandle,
       
   322     jint aZ)
       
   323 {
       
   324     DEBUG("CanvasGraphicsItem.cpp - setZPosition +");
       
   325 
       
   326     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
       
   327 
       
   328     MMIDCanvasGraphicsItem* item =
       
   329         MIDUnhandObject<MMIDCanvasGraphicsItem>(aNativePeerHandle);
       
   330 
       
   331     TInt error = toolkit->ExecuteTrap(
       
   332                      &SetElevationL,
       
   333                      item,
       
   334                      aZ);
       
   335 
       
   336     DEBUG_INT("CanvasGraphicsItem.cpp - setZPosition -, error=%d", error);
       
   337 
       
   338     return error;
       
   339 }
       
   340 
       
   341 /**
       
   342  * Local helper function for getting the elevation of a graphics item.
       
   343  *
       
   344  * @param aItem The graphics item.
       
   345  * @return The elevation of the graphics item's content.
       
   346  */
       
   347 LOCAL_C TInt Elevation(MMIDCanvasGraphicsItem* aItem)
       
   348 {
       
   349     return aItem->Elevation();
       
   350 }
       
   351 
       
   352 /*
       
   353  * Class:     com_nokia_mid_ui_CanvasGraphicsItem
       
   354  * Method:    _getZPosition
       
   355  * Signature: (II)I
       
   356  */
       
   357 JNIEXPORT jint JNICALL Java_com_nokia_mid_ui_CanvasGraphicsItem__1getZPosition(
       
   358     JNIEnv* /* aJniEnv */,
       
   359     jobject /* aPeer */,
       
   360     jint aToolkitHandle,
       
   361     jint aNativePeerHandle)
       
   362 {
       
   363     DEBUG("CanvasGraphicsItem.cpp - getZPosition +");
       
   364 
       
   365     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
       
   366 
       
   367     MMIDCanvasGraphicsItem* item =
       
   368         MIDUnhandObject<MMIDCanvasGraphicsItem>(aNativePeerHandle);
       
   369 
       
   370     TInt position = toolkit->Execute(&Elevation, item);
       
   371 
       
   372     DEBUG_INT("CanvasGraphicsItem.cpp - getZPosition -, position=%d",
       
   373               position);
       
   374 
       
   375     return position;
       
   376 }
       
   377 
       
   378 
       
   379 /**
       
   380  * Local helper function for dispoisng graphics item native side component.
       
   381  *
       
   382  * @param aItem The graphics item to be destroyed.
       
   383  */
       
   384 LOCAL_C void Dispose(CMIDToolkit* aToolkit, MMIDCanvasGraphicsItem* aItem)
       
   385 {
       
   386     aToolkit->DisposeObject(aItem);
       
   387 }
       
   388 
       
   389 /*
       
   390  * Class:     com_nokia_mid_ui_CanvasGraphicsItem
       
   391  * Method:    _dispose
       
   392  * Signature: (II)V
       
   393  */
       
   394 JNIEXPORT void JNICALL Java_com_nokia_mid_ui_CanvasGraphicsItem__1dispose(
       
   395     JNIEnv* /* aJniEnv */,
       
   396     jobject /* aPeer */,
       
   397     jint aToolkitHandle,
       
   398     jint aNativePeerHandle)
       
   399 {
       
   400     DEBUG("CanvasGraphicsItem.cpp - dispose +");
       
   401 
       
   402     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
       
   403 
       
   404     MMIDCanvasGraphicsItem* item =
       
   405         MIDUnhandObject<MMIDCanvasGraphicsItem>(aNativePeerHandle);
       
   406 
       
   407     toolkit->ExecuteV(&Dispose, toolkit, item);
       
   408 
       
   409     DEBUG("CanvasGraphicsItem.cpp - dispose -");
       
   410 }
       
   411