// -*- c++ -*-

/*
 *  Copyright (C) 2001-2002, Richard J. Moore <rich@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#ifndef KJSEMBEDJSCONSOLEPART_H
#define KJSEMBEDJSCONSOLEPART_H

#include <qstringlist.h>

#include <kparts/part.h>
#include <kjs/interpreter.h>
#include <kjs/object.h>
#include <kjsembed/jsbinding.h>
#include <kjsembed/xmlactionclient.h>

class QWidget;

#define KJSEMBED_VERSION_STRING "KJSEmbed 0.2"
#define KJSEMBED_VERSION_MAJOR 0
#define KJSEMBED_VERSION_MINOR 2

/**
 * Namespace containing the KJSEmbed library.
 */
namespace KJSEmbed {

class JSObjectProxy;
class JSConsoleWidget;

/**
 * A KPart for embedding KJS in an application.
 *
 * @version $Id: jsconsolepart.h,v 1.1.1.1 2002/10/04 23:32:50 rich Exp $
 * @author Richard Moore, rich@kde.org
 */
class KJSEmbedPart : public KParts::ReadOnlyPart, public virtual KJSEmbed::XMLActionRunner
{
    Q_OBJECT

public:
    /** Create a KJSEmbedPart. */
    KJSEmbedPart( QObject *parent=0, const char *name=0 );

    /** Create a KJSEmbedPart for which the parents of the view and part are different. */
    KJSEmbedPart( QWidget *wparent, const char *wname=0, QObject *parent=0, const char *name=0 );

    /** Create a KJSEmbedPart with the specified interpreter. */
    KJSEmbedPart( KJS::Interpreter *js, QWidget *wparent=0, const char *wname=0,
		  QObject *parent=0, const char *name=0 );

    /** Cleans up. */
    virtual ~KJSEmbedPart();

    /** Returns the current interpreter. */
    KJS::Interpreter *jscript() const { return js; }

    /** Returns the view widget, creating one if required.*/
    virtual JSConsoleWidget *view();

    /** Overloaded to use the interpreter's global ExecState and object. */
    void publishStdBindings();

    /** Makes the 'Factory' object visible as a child of the specfied object. */
    void publishStdBindings( KJS::ExecState *exec, KJS::Object &object );

    /** Publishes 'obj' as property 'name' of the global object. */
    void publish( QObject *obj, const char *name=0 );

    /** Binds a QObject to a Javascript object using @ref JSObjectProxy. */
    KJS::Object bind( QObject *obj );

    /**
     * Publishes 'obj' as property 'name' of object 'parent'. The binding is defined
     * using @ref JSObjectProxy, and is subject to the current default SecurityPolicy.
     */
    void publish( QObject *obj, KJS::Object &parent, const char *name=0 );

    /** Returns a new interpreter each time it is called. */
    static KJS::Interpreter *createInterpreter();

    /** Reimplemented to run KJS actions in the interpreter. */
    virtual bool run( KJSEmbed::XMLActionClient *client, const KJSEmbed::XMLActionScript &script );

    /**
     * Creates an instance of a QObject subclass. If the instance cannot be
     * created then 0 is returned.
     */
    static QObject *create( QObject *parent=0, const char *name=0, const char *classname="QObject" );

    /**
     * Loads the widget defined in the specified .ui file. If the widget
     * cannot be created then 0 is returned.
     */
    static QWidget *loadUI( const QString &uiFile, QObject *connector=0, QWidget *parent=0, const char *name=0 );

public slots:
    /** Opens a URL. If the URL has the protocol 'javascript' then it is executed. */
    virtual bool openURL( const KURL &url );

    /** Loads and runs the specified file. */
    virtual bool loadScript( const QString &filename );

    /** Executes the specified string. */
    virtual bool executeScript( const QString &script );

    /** Loads the XML actions defined in the specified file to the default XMLActionClient. */
    bool loadActionSet( const QString &file );

protected:
    virtual bool openFile() { return false; }

private:
    /**
     * @internal
     * Provides the implementation of a JS method.
     */
    class MethodImp : public KJS::ObjectImp
    {
    public:
	enum MethodId {
	    MethodCreate, MethodLoadUI, MethodLoadScript,
	    MethodPrint, MethodWarn, MethodReadLine
	};

	MethodImp( KJSEmbedPart *part, MethodId id );
	~MethodImp();

	/** 
	 * Reimplemented to specify that we implement the call operation.
	 */
	virtual bool implementsCall() const { return true; }

	virtual KJS::Value call( KJS::ExecState *exec, KJS::Object &self,
				 const KJS::List &args );

    private:
	KJSEmbedPart *part;
	MethodId id;
    };

    /** @internal */
    friend class MethodImp;

private:
    XMLActionClient *xmlclient;
    QWidget *widgetparent;
    QCString widgetname;
    KJS::Interpreter *js;
    JSConsoleWidget *jsConsole;
    bool deletejs;
    KJS::Object global;
    class Private *d;
};

}; // namespace KJSEmbed

#endif // KJSEMBEDJSCONSOLEPART_H

// Local Variables:
// c-basic-offset: 4
// End:
