webengine/widgetengine/src/MenuItem.cpp
changeset 0 dd21522fd290
child 5 10e98eab6f85
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2003 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 the License "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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 
       
    22 #include <eikamnt.h>
       
    23 
       
    24 #include "MenuItem.h"
       
    25 #include "MenuItemFuncs.h"
       
    26 #include "MenuItemCallbacks.h"
       
    27 
       
    28 using namespace KJS;
       
    29 
       
    30 // ----------------------------------------------------------------------------
       
    31 // JSMenuItemConstructor::JSMenuItemConstructor
       
    32 // Default constructor
       
    33 //
       
    34 //
       
    35 // ----------------------------------------------------------------------------
       
    36 JSMenuItemConstructor::JSMenuItemConstructor(MJSMenuItemCallbacks* callbacks)
       
    37     : JSObject(), 
       
    38     m_callbacks(callbacks),
       
    39     m_internalId(0)
       
    40 {
       
    41 }
       
    42 
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // JSMenuItemConstructor::implementsConstruct
       
    46 // returns true if this object implements the construct() method, otherwise
       
    47 // returns false
       
    48 //
       
    49 // ----------------------------------------------------------------------------
       
    50 bool JSMenuItemConstructor::implementsConstruct() const
       
    51 {
       
    52     return true;
       
    53 }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // JSMenuItemConstructor::construct
       
    57 // Creates a new JSMenuItem object
       
    58 //
       
    59 //
       
    60 // ----------------------------------------------------------------------------
       
    61 JSObject* JSMenuItemConstructor::construct( ExecState* exec, const List& aList)
       
    62 {
       
    63 
       
    64     UString text;
       
    65     int cmdId = 0;
       
    66 
       
    67     if ( aList.size() > 1 ) {
       
    68     
       
    69         JSValue* t = aList.at( 0 );
       
    70         JSValue* id = aList.at( 1 );
       
    71         cmdId = id->toInt32( exec );
       
    72         text = t->toString( exec );
       
    73 
       
    74         if ( ( t->type() == StringType ) && 
       
    75              ( id->type() == NumberType ) && 
       
    76              ( cmdId >= 0 ) && 
       
    77              ( text.size() > 0 ) ) {
       
    78             
       
    79             text = t->toString( exec );
       
    80            
       
    81             int textlen = (text.size() > CEikAutoMenuTitle::ENominalTextLength) ?
       
    82                         CEikAutoMenuTitle::ENominalTextLength : text.size(); 
       
    83 
       
    84             TPtrC tptrc((const unsigned short*)(text.data()), textlen);
       
    85 
       
    86             JSMenuItem* mi = new JSMenuItem(exec, m_callbacks, tptrc, cmdId, m_internalId );
       
    87             KJS::Collector::protect(mi); 
       
    88             m_callbacks->createOptionsMenuItem( tptrc, cmdId, m_internalId++, (void*) mi );
       
    89            
       
    90             return static_cast<KJS::JSObject*>(mi);
       
    91         }
       
    92     }
       
    93 
       
    94     return throwError(exec, GeneralError);
       
    95 }
       
    96 
       
    97 
       
    98 // ------------------------------ JSMenuItem -----------------------------
       
    99 
       
   100 const ClassInfo JSMenuItem::info = { "MenuItem", 0, &TMenuItemTable, 0 };
       
   101 
       
   102 /*
       
   103 @begin TMenuItemTable 5
       
   104   append    JSMenuItem::Append      DontDelete|Function 1
       
   105   remove    JSMenuItem::Remove      DontDelete|Function 1
       
   106   setDimmed JSMenuItem::SetDim      DontDelete|Function 1
       
   107   onSelect  JSMenuItem::OnSelect    DontDelete|ReadOnly
       
   108   toString  JSMenuItem::ToString    DontDelete|Function 0
       
   109 @end
       
   110 */
       
   111 
       
   112 
       
   113 // ----------------------------------------------------------------------------
       
   114 // JSMenuItem::JSMenuItem
       
   115 // Default constructor
       
   116 //
       
   117 //
       
   118 // ----------------------------------------------------------------------------
       
   119 JSMenuItem::JSMenuItem(  
       
   120     ExecState* exec,   
       
   121     MJSMenuItemCallbacks* callbacks,
       
   122     TDesC& text,
       
   123     int cmdId, 
       
   124     int internalId,
       
   125     WidgetEventHandler* onSelectCallback )
       
   126     : JSObject(exec->lexicalInterpreter()->builtinObjectPrototype()),      
       
   127       d(new MenuItemPrivate(callbacks,cmdId,internalId,onSelectCallback))  
       
   128     {
       
   129     }
       
   130     
       
   131 // ----------------------------------------------------------------------------
       
   132 // JSMenuItem::~JSMenuItem
       
   133 // Destructor
       
   134 //
       
   135 //
       
   136 // ----------------------------------------------------------------------------
       
   137 JSMenuItem::~JSMenuItem()
       
   138 {
       
   139     delete d;
       
   140 }
       
   141 
       
   142 int JSMenuItem::Id() const
       
   143 { 
       
   144     return d->m_cmdId; 
       
   145 } 
       
   146        
       
   147 int JSMenuItem::InternalId()  const
       
   148 { 
       
   149     return d->m_internalId; 
       
   150 }        
       
   151 
       
   152 bool JSMenuItem::Dimmed()  const
       
   153 { 
       
   154     return d->m_dimmed; 
       
   155 }
       
   156 
       
   157 bool JSMenuItem::Show()  const
       
   158 { 
       
   159     return d->m_show; 
       
   160 }
       
   161 
       
   162 void JSMenuItem::SetDimmed(bool val)
       
   163 { 
       
   164     d->m_dimmed=val; 
       
   165     d->m_callbacks->setDimmed(d->m_internalId, val);
       
   166 }                
       
   167 
       
   168 void JSMenuItem::SetShow(bool val) 
       
   169 { 
       
   170     d->m_show=val; 
       
   171 }       
       
   172 
       
   173 WidgetEventHandler* JSMenuItem::callback()  const
       
   174 { 
       
   175     return d->m_onSelectCallback; 
       
   176 }
       
   177 
       
   178 // ----------------------------------------------------------------------------
       
   179 // JSMenuItem::AddOptionsMenuItem
       
   180 //
       
   181 //
       
   182 // ----------------------------------------------------------------------------
       
   183 void JSMenuItem::AddOptionsMenuItem(bool show)
       
   184 {    
       
   185  
       
   186     // if no error (KErrNone) in adding the menu item
       
   187     if ( ( d->m_callbacks->addOptionsMenuItem(d->m_internalId, -1, ETrue ) == KErrNone ) ) {        
       
   188         SetShow( show );
       
   189         if ( d->m_callbacks ) {
       
   190             d->m_callbacks->setMenuItemObserver(d->m_internalId, d->m_onSelectCallback);
       
   191         }
       
   192     }    
       
   193 }
       
   194 
       
   195 // ----------------------------------------------------------------------------
       
   196 // JSMenuItem::DeleteMenuItem
       
   197 //
       
   198 //
       
   199 // ----------------------------------------------------------------------------
       
   200 void JSMenuItem::DeleteMenuItem()
       
   201 {
       
   202     d->m_callbacks->deleteMenuItem( d->m_internalId );    
       
   203 }
       
   204 
       
   205 // ----------------------------------------------------------------------------
       
   206 // JSMenuItem::toString
       
   207 // Returns string representation of the object
       
   208 //
       
   209 //
       
   210 // ----------------------------------------------------------------------------
       
   211 UString JSMenuItem::toString( ExecState *exec ) const
       
   212 {
       
   213     return "[object MenuItem]";
       
   214 }
       
   215 
       
   216 // ----------------------------------------------------------------------------
       
   217 // JSMenuItem::canPut
       
   218 //
       
   219 //
       
   220 //
       
   221 // ----------------------------------------------------------------------------
       
   222 bool JSMenuItem::canPut(ExecState *exec, const Identifier &propertyName) const 
       
   223 { 
       
   224     return true; 
       
   225 }        
       
   226 
       
   227 // ----------------------------------------------------------------------------
       
   228 // JSMenuItem::put
       
   229 // Sets the specified property
       
   230 //
       
   231 //
       
   232 // ----------------------------------------------------------------------------
       
   233 void JSMenuItem::put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr)
       
   234 {
       
   235     const HashEntry * entry = Lookup::findEntry( &TMenuItemTable, propertyName );
       
   236 
       
   237     // if the property exists in the table
       
   238     if ( entry ) {
       
   239         
       
   240         // changes the properties of the Menu element
       
   241         switch ( entry->value ) {
       
   242             case OnSelect: {
       
   243                 delete d->m_onSelectCallback;
       
   244                 d->m_onSelectCallback = NULL;
       
   245                 d->m_onSelectCallback = new WidgetEventHandler ( value, exec->lexicalInterpreter()->globalExec() );
       
   246                 d->m_callbacks->setMenuItemObserver( d->m_internalId, d->m_onSelectCallback );
       
   247                 break;
       
   248             }
       
   249             default:
       
   250                 break;
       
   251         }
       
   252     }
       
   253         
       
   254 }
       
   255 
       
   256 
       
   257 // ----------------------------------------------------------------------------
       
   258 // MenuPrototypeImp::getOwnPropertySlot
       
   259 //
       
   260 //
       
   261 //
       
   262 // ----------------------------------------------------------------------------
       
   263 bool JSMenuItem::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
       
   264 {
       
   265     const HashEntry* entry = Lookup::findEntry(&TMenuItemTable, propertyName);
       
   266     if (entry) {
       
   267         slot.setStaticEntry(this, entry, staticValueGetter<JSMenuItem>);
       
   268         return true;
       
   269     }
       
   270    
       
   271     return JSObject::getOwnPropertySlot(exec, propertyName, slot);    
       
   272 }
       
   273       
       
   274 // ----------------------------------------------------------------------------
       
   275 // JSMenuItem::getValueProperty
       
   276 // Retrieves the specified property from the object. If neither the object
       
   277 // or any other object in it's prototype chain have the property, this function
       
   278 // will return Undefined
       
   279 //
       
   280 //
       
   281 // ----------------------------------------------------------------------------
       
   282 JSValue* JSMenuItem::getValueProperty(KJS::ExecState* exec, int token) const
       
   283 {
       
   284     
       
   285     switch ( token ) {
       
   286         case Append:
       
   287         case Remove:
       
   288         case SetDim:
       
   289         case ToString: {
       
   290             KJS::JSMenuItemFunc *mif = new KJS::JSMenuItemFunc(exec, d->m_callbacks, token);
       
   291             return mif;
       
   292         }
       
   293         default:
       
   294             break;
       
   295     }
       
   296 
       
   297     return throwError(exec, GeneralError);
       
   298 }
       
   299