Home | History | Annotate | Download | only in javascript
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #include "fpdfsdk/javascript/event.h"
      8 
      9 #include "fpdfsdk/javascript/Field.h"
     10 #include "fpdfsdk/javascript/JS_Define.h"
     11 #include "fpdfsdk/javascript/JS_EventHandler.h"
     12 #include "fpdfsdk/javascript/JS_Object.h"
     13 #include "fpdfsdk/javascript/JS_Value.h"
     14 #include "fpdfsdk/javascript/cjs_event_context.h"
     15 
     16 JSConstSpec CJS_Event::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}};
     17 
     18 JSPropertySpec CJS_Event::PropertySpecs[] = {
     19     {"change", get_change_static, set_change_static},
     20     {"changeEx", get_changeEx_static, set_changeEx_static},
     21     {"commitKey", get_commitKey_static, set_commitKey_static},
     22     {"fieldFull", get_fieldFull_static, set_fieldFull_static},
     23     {"keyDown", get_keyDown_static, set_keyDown_static},
     24     {"modifier", get_modifier_static, set_modifier_static},
     25     {"name", get_name_static, set_name_static},
     26     {"rc", get_rc_static, set_rc_static},
     27     {"richChange", get_richChange_static, set_richChange_static},
     28     {"richChangeEx", get_richChangeEx_static, set_richChangeEx_static},
     29     {"richValue", get_richValue_static, set_richValue_static},
     30     {"selEnd", get_selEnd_static, set_selEnd_static},
     31     {"selStart", get_selStart_static, set_selStart_static},
     32     {"shift", get_shift_static, set_shift_static},
     33     {"source", get_source_static, set_source_static},
     34     {"target", get_target_static, set_target_static},
     35     {"targetName", get_targetName_static, set_targetName_static},
     36     {"type", get_type_static, set_type_static},
     37     {"value", get_value_static, set_value_static},
     38     {"willCommit", get_willCommit_static, set_willCommit_static},
     39     {0, 0, 0}};
     40 
     41 JSMethodSpec CJS_Event::MethodSpecs[] = {{0, 0}};
     42 
     43 IMPLEMENT_JS_CLASS(CJS_Event, event)
     44 
     45 event::event(CJS_Object* pJsObject) : CJS_EmbedObj(pJsObject) {}
     46 
     47 event::~event() {}
     48 
     49 bool event::change(CJS_Runtime* pRuntime,
     50                    CJS_PropValue& vp,
     51                    CFX_WideString& sError) {
     52   CJS_EventHandler* pEvent =
     53       pRuntime->GetCurrentEventContext()->GetEventHandler();
     54   CFX_WideString& wChange = pEvent->Change();
     55   if (vp.IsSetting()) {
     56     if (vp.GetJSValue()->GetType() == CJS_Value::VT_string)
     57       vp >> wChange;
     58     return true;
     59   }
     60   vp << wChange;
     61   return true;
     62 }
     63 
     64 bool event::changeEx(CJS_Runtime* pRuntime,
     65                      CJS_PropValue& vp,
     66                      CFX_WideString& sError) {
     67   if (!vp.IsGetting())
     68     return false;
     69 
     70   CJS_EventHandler* pEvent =
     71       pRuntime->GetCurrentEventContext()->GetEventHandler();
     72 
     73   vp << pEvent->ChangeEx();
     74   return true;
     75 }
     76 
     77 bool event::commitKey(CJS_Runtime* pRuntime,
     78                       CJS_PropValue& vp,
     79                       CFX_WideString& sError) {
     80   if (!vp.IsGetting())
     81     return false;
     82 
     83   CJS_EventHandler* pEvent =
     84       pRuntime->GetCurrentEventContext()->GetEventHandler();
     85 
     86   vp << pEvent->CommitKey();
     87   return true;
     88 }
     89 
     90 bool event::fieldFull(CJS_Runtime* pRuntime,
     91                       CJS_PropValue& vp,
     92                       CFX_WideString& sError) {
     93   CJS_EventHandler* pEvent =
     94       pRuntime->GetCurrentEventContext()->GetEventHandler();
     95 
     96   if (!vp.IsGetting() &&
     97       wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
     98     return false;
     99 
    100   vp << pEvent->FieldFull();
    101   return true;
    102 }
    103 
    104 bool event::keyDown(CJS_Runtime* pRuntime,
    105                     CJS_PropValue& vp,
    106                     CFX_WideString& sError) {
    107   if (!vp.IsGetting())
    108     return false;
    109 
    110   CJS_EventHandler* pEvent =
    111       pRuntime->GetCurrentEventContext()->GetEventHandler();
    112 
    113   vp << pEvent->KeyDown();
    114   return true;
    115 }
    116 
    117 bool event::modifier(CJS_Runtime* pRuntime,
    118                      CJS_PropValue& vp,
    119                      CFX_WideString& sError) {
    120   if (!vp.IsGetting())
    121     return false;
    122 
    123   CJS_EventHandler* pEvent =
    124       pRuntime->GetCurrentEventContext()->GetEventHandler();
    125 
    126   vp << pEvent->Modifier();
    127   return true;
    128 }
    129 
    130 bool event::name(CJS_Runtime* pRuntime,
    131                  CJS_PropValue& vp,
    132                  CFX_WideString& sError) {
    133   if (!vp.IsGetting())
    134     return false;
    135 
    136   CJS_EventHandler* pEvent =
    137       pRuntime->GetCurrentEventContext()->GetEventHandler();
    138 
    139   vp << pEvent->Name();
    140   return true;
    141 }
    142 
    143 bool event::rc(CJS_Runtime* pRuntime,
    144                CJS_PropValue& vp,
    145                CFX_WideString& sError) {
    146   CJS_EventHandler* pEvent =
    147       pRuntime->GetCurrentEventContext()->GetEventHandler();
    148 
    149   bool& bRc = pEvent->Rc();
    150   if (vp.IsSetting())
    151     vp >> bRc;
    152   else
    153     vp << bRc;
    154 
    155   return true;
    156 }
    157 
    158 bool event::richChange(CJS_Runtime* pRuntime,
    159                        CJS_PropValue& vp,
    160                        CFX_WideString& sError) {
    161   return true;
    162 }
    163 
    164 bool event::richChangeEx(CJS_Runtime* pRuntime,
    165                          CJS_PropValue& vp,
    166                          CFX_WideString& sError) {
    167   return true;
    168 }
    169 
    170 bool event::richValue(CJS_Runtime* pRuntime,
    171                       CJS_PropValue& vp,
    172                       CFX_WideString& sError) {
    173   return true;
    174 }
    175 
    176 bool event::selEnd(CJS_Runtime* pRuntime,
    177                    CJS_PropValue& vp,
    178                    CFX_WideString& sError) {
    179   CJS_EventHandler* pEvent =
    180       pRuntime->GetCurrentEventContext()->GetEventHandler();
    181 
    182   if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
    183     return true;
    184 
    185   int& iSelEnd = pEvent->SelEnd();
    186   if (vp.IsSetting())
    187     vp >> iSelEnd;
    188   else
    189     vp << iSelEnd;
    190 
    191   return true;
    192 }
    193 
    194 bool event::selStart(CJS_Runtime* pRuntime,
    195                      CJS_PropValue& vp,
    196                      CFX_WideString& sError) {
    197   CJS_EventHandler* pEvent =
    198       pRuntime->GetCurrentEventContext()->GetEventHandler();
    199 
    200   if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
    201     return true;
    202 
    203   int& iSelStart = pEvent->SelStart();
    204   if (vp.IsSetting())
    205     vp >> iSelStart;
    206   else
    207     vp << iSelStart;
    208 
    209   return true;
    210 }
    211 
    212 bool event::shift(CJS_Runtime* pRuntime,
    213                   CJS_PropValue& vp,
    214                   CFX_WideString& sError) {
    215   if (!vp.IsGetting())
    216     return false;
    217 
    218   CJS_EventHandler* pEvent =
    219       pRuntime->GetCurrentEventContext()->GetEventHandler();
    220 
    221   vp << pEvent->Shift();
    222   return true;
    223 }
    224 
    225 bool event::source(CJS_Runtime* pRuntime,
    226                    CJS_PropValue& vp,
    227                    CFX_WideString& sError) {
    228   if (!vp.IsGetting())
    229     return false;
    230 
    231   CJS_EventHandler* pEvent =
    232       pRuntime->GetCurrentEventContext()->GetEventHandler();
    233 
    234   vp << pEvent->Source()->GetJSObject();
    235   return true;
    236 }
    237 
    238 bool event::target(CJS_Runtime* pRuntime,
    239                    CJS_PropValue& vp,
    240                    CFX_WideString& sError) {
    241   if (!vp.IsGetting())
    242     return false;
    243 
    244   CJS_EventHandler* pEvent =
    245       pRuntime->GetCurrentEventContext()->GetEventHandler();
    246 
    247   vp << pEvent->Target_Field()->GetJSObject();
    248   return true;
    249 }
    250 
    251 bool event::targetName(CJS_Runtime* pRuntime,
    252                        CJS_PropValue& vp,
    253                        CFX_WideString& sError) {
    254   if (!vp.IsGetting())
    255     return false;
    256 
    257   CJS_EventHandler* pEvent =
    258       pRuntime->GetCurrentEventContext()->GetEventHandler();
    259 
    260   vp << pEvent->TargetName();
    261   return true;
    262 }
    263 
    264 bool event::type(CJS_Runtime* pRuntime,
    265                  CJS_PropValue& vp,
    266                  CFX_WideString& sError) {
    267   if (!vp.IsGetting())
    268     return false;
    269 
    270   CJS_EventHandler* pEvent =
    271       pRuntime->GetCurrentEventContext()->GetEventHandler();
    272 
    273   vp << pEvent->Type();
    274   return true;
    275 }
    276 
    277 bool event::value(CJS_Runtime* pRuntime,
    278                   CJS_PropValue& vp,
    279                   CFX_WideString& sError) {
    280   CJS_EventHandler* pEvent =
    281       pRuntime->GetCurrentEventContext()->GetEventHandler();
    282 
    283   if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0)
    284     return false;
    285 
    286   if (!pEvent->m_pValue)
    287     return false;
    288 
    289   CFX_WideString& val = pEvent->Value();
    290   if (vp.IsSetting())
    291     vp >> val;
    292   else
    293     vp << val;
    294 
    295   return true;
    296 }
    297 
    298 bool event::willCommit(CJS_Runtime* pRuntime,
    299                        CJS_PropValue& vp,
    300                        CFX_WideString& sError) {
    301   if (!vp.IsGetting())
    302     return false;
    303 
    304   CJS_EventHandler* pEvent =
    305       pRuntime->GetCurrentEventContext()->GetEventHandler();
    306 
    307   vp << pEvent->WillCommit();
    308   return true;
    309 }
    310