// -*- c++ -*-

/*
 *  Copyright (C) 2001-2003, 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: kjsembedpart.h,v 1.7 2003/02/01 00:16:17 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 *interpreter() 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();

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

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

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

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

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

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

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

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

    /** Loads a script from the specified file, removing the #! line (if present). */
    QString loadScript( const QString &file );

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

private:
    /** @internal */
    friend class KJSEmbedPartImp;

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:
