kjs_event.cpp 4.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/***************************************************************************
 *   Copyright (C) 2018 by Intevation GmbH <intevation@intevation.de>      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "kjs_event_p.h"

#include <kjs/kjsinterpreter.h>
#include <kjs/kjsobject.h>
#include <kjs/kjsprototype.h>
#include <kjs/kjsarguments.h>

#include "kjs_field_p.h"
#include "event_p.h"

using namespace Okular;

static KJSPrototype *g_eventProto;

// Event.name
static KJSObject eventGetName( KJSContext *, void *object )
{
    const Event *event = reinterpret_cast< Event * >( object );
    return KJSString( event->name() );
}

// Event.type
static KJSObject eventGetType( KJSContext *, void *object )
{
    const Event *event = reinterpret_cast< Event * >( object );
    return KJSString( event->type() );
}

// Event.targetName (getter)
static KJSObject eventGetTargetName( KJSContext *, void *object )
{
    const Event *event = reinterpret_cast< Event * >( object );
    return KJSString( event->targetName() );
}

// Event.targetName (setter)
static void eventSetTargetName( KJSContext *ctx, void *object, KJSObject value )
{
    Event *event = reinterpret_cast< Event * >( object );
    event->setTargetName ( value.toString ( ctx ) );
}

// Event.source
static KJSObject eventGetSource( KJSContext *ctx, void *object )
{
    const Event *event = reinterpret_cast< Event * >( object );
    if ( event->eventType() == Event::FieldCalculate )
    {
        FormField *src = event->source();
        if ( src )
            return JSField::wrapField( ctx, src, event->sourcePage() );
    }
    return KJSUndefined();
}

// Event.target
static KJSObject eventGetTarget( KJSContext *ctx, void *object )
{
    const Event *event = reinterpret_cast< Event * >( object );
69
    switch( event->eventType() )
70
    {
71 72 73 74 75 76 77 78 79
        case Event::FieldCalculate:
        case Event::FieldFormat:
        case Event::FieldKeystroke:
        {
            FormField *target = static_cast< FormField * >( event->target() );
            if ( target )
                return JSField::wrapField( ctx, target, event->targetPage() );
            break;
        }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    }
    return KJSUndefined();
}

// Event.value (getter)
static KJSObject eventGetValue( KJSContext *, void *object )
{
    const Event *event = reinterpret_cast< Event * >( object );
    return KJSString( event->value().toString() );
}

// Event.value (setter)
static void eventSetValue( KJSContext *ctx, void *object, KJSObject value )
{
    Event *event = reinterpret_cast< Event * >( object );
    event->setValue ( QVariant( value.toString ( ctx ) ) );
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111
// Event.rc (getter)
static KJSObject eventGetReturnCode( KJSContext *, void *object )
{
    const Event *event = reinterpret_cast< Event * >( object );
    return KJSBoolean( event->returnCode() );
}

// Event.rc (setter)
static void eventSetReturnCode( KJSContext *ctx, void *object, KJSObject value )
{
    Event *event = reinterpret_cast< Event * >( object );
    event->setReturnCode ( value.toBoolean ( ctx ) );
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
void JSEvent::initType( KJSContext *ctx )
{
    static bool initialized = false;
    if ( initialized )
        return;
    initialized = true;

    if ( !g_eventProto )
        g_eventProto = new KJSPrototype();

    g_eventProto->defineProperty( ctx, QStringLiteral( "name" ), eventGetName );
    g_eventProto->defineProperty( ctx, QStringLiteral( "type" ), eventGetType );
    g_eventProto->defineProperty( ctx, QStringLiteral( "targetName" ), eventGetTargetName,
                                  eventSetTargetName );
    g_eventProto->defineProperty( ctx, QStringLiteral( "source" ), eventGetSource );
    g_eventProto->defineProperty( ctx, QStringLiteral( "target" ), eventGetTarget );
    g_eventProto->defineProperty( ctx, QStringLiteral( "value" ), eventGetValue, eventSetValue );
129
    g_eventProto->defineProperty( ctx, QStringLiteral( "rc" ), eventGetReturnCode, eventSetReturnCode );
130 131 132 133 134 135 136 137
}

KJSObject JSEvent::wrapEvent( KJSContext *ctx, Event *event )
{
    return g_eventProto->constructObject( ctx, event );
}