diff options
Diffstat (limited to 'kate/kjswrapper')
-rw-r--r-- | kate/kjswrapper/Makefile.am | 16 | ||||
-rw-r--r-- | kate/kjswrapper/bindings.cpp | 466 | ||||
-rw-r--r-- | kate/kjswrapper/bindings.h | 150 | ||||
-rw-r--r-- | kate/kjswrapper/plugin_katekjswrapper.cpp | 437 | ||||
-rw-r--r-- | kate/kjswrapper/plugin_katekjswrapper.h | 180 | ||||
-rw-r--r-- | kate/kjswrapper/samples/Makefile.am | 23 | ||||
-rw-r--r-- | kate/kjswrapper/samples/katekjsconsolewindow.desktop | 55 | ||||
-rw-r--r-- | kate/kjswrapper/samples/katekjsconsolewindow.js | 18 | ||||
-rw-r--r-- | kate/kjswrapper/samples/katekjsconsolewindow.rc | 8 | ||||
-rw-r--r-- | kate/kjswrapper/samples/katekjsselect.desktop | 53 | ||||
-rw-r--r-- | kate/kjswrapper/samples/katekjsselect.js | 92 | ||||
-rw-r--r-- | kate/kjswrapper/samples/katekjsselect.rc | 8 | ||||
-rw-r--r-- | kate/kjswrapper/samples/katekjstest1.desktop | 90 | ||||
-rw-r--r-- | kate/kjswrapper/samples/katekjstest1.js | 167 |
14 files changed, 1763 insertions, 0 deletions
diff --git a/kate/kjswrapper/Makefile.am b/kate/kjswrapper/Makefile.am new file mode 100644 index 0000000..51258f7 --- /dev/null +++ b/kate/kjswrapper/Makefile.am @@ -0,0 +1,16 @@ +SUBDIRS= . samples +INCLUDES = $(all_includes) +METASOURCES = AUTO + +# Install this plugin in the KDE modules directory +kde_module_LTLIBRARIES = katekjswrapperplugin.la + +# This is all standard. Remove the LIB_KHTML reference if you are not +# using the KHTML Part +katekjswrapperplugin_la_SOURCES = plugin_katekjswrapper.cpp bindings.cpp +katekjswrapperplugin_la_LIBADD = -lkateinterfaces -lkjsembed -lkjs +katekjswrapperplugin_la_LDFLAGS = -module $(KDE_PLUGIN) $(all_libraries) + +messages: rc.cpp + $(XGETTEXT) *.cpp -o $(podir)/katekjswrapper.pot + diff --git a/kate/kjswrapper/bindings.cpp b/kate/kjswrapper/bindings.cpp new file mode 100644 index 0000000..fcb8d38 --- /dev/null +++ b/kate/kjswrapper/bindings.cpp @@ -0,0 +1,466 @@ +#include "bindings.h" +#include "bindings.moc" +#include "plugin_katekjswrapper.h" +#include <qstringlist.h> +#include <kdebug.h> +#include <kate/documentmanager.h> +#include <kate/document.h> +#include <kjsembed/kjsembedpart.h> +#include <kjsembed/jsfactory.h> +#include <kjsembed/jsobjectproxy_imp.h> +#include <klocale.h> +#include <kjs/value.h> +#include <kdockwidget.h> +#include <kate/application.h> +#include <kate/documentmanager.h> +#include <kjsembed/jsconsolewidget.h> + + +using namespace Kate::JS; +Bindings::Bindings(QObject *parent): KJSEmbed::Bindings::JSBindingPlugin(parent,"KateAppBindings",QStringList()) { +} + +Bindings::~Bindings() { +} + +KJS::Object Bindings::createBinding(KJSEmbed::KJSEmbedPart *jspart, KJS::ExecState *exec, const KJS::List &args) const { +/* + kdDebug() << "Loading a custom object" << endl; + DocumentManager *obj = new DocumentManager(); + JSOpaqueProxy *prx = new JSOpaqueProxy( (void *) obj, "Kate::JS::DocumentManager" ); + + KJS::Object proxyObj(prx); + DocumentManagerImp::addBindings( exec, proxyObj ); + return proxyObj; +*/ + return KJS::Object(); +} + + +void Bindings::addBindings(KJS::ExecState *exec, KJS::Object &target) const { + kdDebug()<<"Kate::JS::Bindings:: ADDING CUSTOM BINDINGS"<<endl; + + KJSEmbed::JSObjectProxy *proxy = KJSEmbed::JSProxy::toObjectProxy( target.imp() ); + if ( !proxy ) + return; + + Kate::DocumentManager *dm=dynamic_cast<Kate::DocumentManager*>(proxy->object()); + if (dm) { + DocumentManager::addBindings(exec,proxy,target); + } else { + Kate::Application *app=dynamic_cast<Kate::Application*>(proxy->object()); + if (app) { + Application::addBindings(exec,proxy,target); + } else { + Kate::MainWindow *win=dynamic_cast<Kate::MainWindow*>(proxy->object()); + if (win) { + MainWindow::addBindings(exec,proxy,target); + } + } + } + General::addBindings(exec,proxy,target); +} + +void DocumentManager::addBindings(KJS::ExecState *exec, KJSEmbed::JSObjectProxy *proxy,KJS::Object &object){ + RefCountedObjectDict *dict=new RefCountedObjectDict(100); + object.put(exec, "document", KJS::Object(new DocumentManager( exec, Document, proxy,dict ))); + object.put(exec, "activeDocument", KJS::Object(new DocumentManager( exec, ActiveDocument, proxy,dict ))); + object.put(exec, "documentForID", KJS::Object(new DocumentManager( exec, DocumentWithID, proxy,dict ))); + object.put(exec, "documentForURL", KJS::Object(new DocumentManager( exec, FindDocument, proxy,dict ))); + object.put(exec, "openURL", KJS::Object(new DocumentManager( exec, OpenURL, proxy,dict ))); + object.put(exec, "isOpen", KJS::Object(new DocumentManager( exec, IsOpen, proxy,dict ))); + object.put(exec, "documentCount", KJS::Object(new DocumentManager( exec, Documents, proxy,dict ))); + object.put(exec, "closeDocument", KJS::Object(new DocumentManager( exec, CloseDocument, proxy,dict ))); + object.put(exec, "closeAllDocument", KJS::Object(new DocumentManager( exec, CloseAllDocuments, proxy,dict ))); +} + +DocumentManager::DocumentManager( KJS::ExecState *exec, int id, KJSEmbed::JSObjectProxy *parent, RefCountedObjectDict *dict ):KJSEmbed::JSProxyImp(exec) { + m_dict=dict; + m_dict->incRef(); + m_id=id; + m_proxy=parent; +} + +DocumentManager::~DocumentManager() { + m_dict->decRef(); +} + +KJS::Value DocumentManager::call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ) { + QObject *o=m_proxy->object(); + Kate::DocumentManager *dm=dynamic_cast<Kate::DocumentManager*>(o); + if (!dm) { + kdWarning()<<"Object died"<<endl; + QString msg = i18n("Call of DocumentManager member on destroyed object"); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); + } + QString mdesc; + switch (m_id) { + case Document: { + mdesc="document(int)"; + if (args.size()!=1) break; + uint index=args[0].toUInt32(exec); + if (exec->hadException()) break; + QObject *doc=dynamic_cast<QObject*>(dm->document(index)); + if (!doc) return KJS::Null(); + return m_dict->jsObject(exec,doc,m_proxy); } + break; + case ActiveDocument: { + mdesc="activeDocument()"; + if (args.size()!=0) break; + QObject *doc=dynamic_cast<QObject*>(dm->activeDocument()); + if (!doc) return KJS::Null(); + return m_dict->jsObject(exec,doc,m_proxy); } + break; + case DocumentWithID: { + mdesc="documentForID(int)"; + if (args.size()!=1) break; + uint id=args[0].toUInt32(exec); + if (exec->hadException()) break; + QObject *doc=dynamic_cast<QObject*>(dm->documentWithID(id)); + if (!doc) return KJS::Null(); + return m_dict->jsObject(exec,doc,m_proxy); } + break; + case FindDocument: { + mdesc="documentForURL(KURL)"; + if (args.size()!=1) break; + KURL url = QString( args[0].toString(exec).qstring() ); + if (exec->hadException()) break; + return KJS::Number(dm->findDocument(url)); } + break; + case IsOpen: { + mdesc="isOpen(KURL)"; + if (args.size()!=0) break; + KURL url = QString( args[0].toString(exec).qstring() ); + if (exec->hadException()) break; + return KJS::Boolean(dm->isOpen(url));} + + break; + case OpenURL: { + mdesc="openURL(KURL[,String encoding])"; + uint docID; + if (args.size()==1) { + KURL url = QString( args[0].toString(exec).qstring() ); + if (exec->hadException()) break; + (void)dm->openURL(url,QString::null,&docID); + return KJS::Number(docID); + } else if (args.size()==2) { + KURL url = QString( args[0].toString(exec).qstring() ); + if (exec->hadException()) break; + QString encoding=QString( args[1].toString(exec).qstring() ); + (void)dm->openURL(url,encoding,&docID); + return KJS::Number(docID); + } + } + break; + case Documents: { + mdesc="documentCount()"; + if (args.size()!=0) break; + return KJS::Number(dm->documents()); } + break; + case CloseDocument: { + mdesc="closeDocument(Kate::Document)"; + if (args.size()!=1) break; + KJSEmbed::JSObjectProxy *proxy = KJSEmbed::JSProxy::toObjectProxy( args[0].imp() ); + if (!proxy) break; + QObject *tmp=proxy->object(); + Kate::Document *tmpdoc=dynamic_cast<Kate::Document*>(tmp); + if (!tmpdoc) break; + return KJS::Boolean(dm->closeDocument(tmpdoc)); } + break; + case CloseAllDocuments: { + mdesc="closeAllDocuments()"; + if (args.size()!=0) break; + return KJS::Boolean(dm->closeAllDocuments()); } + break; + default: + kdDebug()<<"calling illegal method of DocumentManager"<<endl; + return KJS::Null(); + } + QString msg = i18n("Method %1 called with wrong signature").arg(mdesc); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); + +} + +Kate::JS::Management::Management(KJS::ExecState *exec, int id, PluginKateKJSWrapper *kateplug):KJSEmbed::JSProxyImp(exec) { + m_id=id; + m_wrapper=kateplug; +} + + +KJS::Value Kate::JS::Management::call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ) { + if (m_id==AddConfigPage) { + if (args.size()!=1) { + QString msg = i18n("One parameter expected"); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); + } + KJS::Value v=args[0]; +// m_wrapper->m_configPageFactories.append(v); +#warning implement me + } else if (m_id==SetConfigPages) { + if (args.size()>1) { + QString msg=i18n("One or no parameter expected"); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); + } + m_wrapper->m_configPageFactories=(args.size()>0)?args[0]:KJS::Value(); + } else if (m_id==SetWindowConfiguration) { + if (args.size()>3) { + QString msg = i18n("A maximum of three parameters expected"); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); + } + kdDebug()<<"***********************************************************************************"<<endl<< + "Kate::JS::Management::call: Object type for m_toolViewConstructors (1):"<<args[0].type()<<endl; + m_wrapper->m_toolViewConstructors=(args.size()>0)?args[0]:KJS::Value(); + kdDebug()<<"Kate::JS::Management::call: Object type for m_toolViewConstructors (2):"<<m_wrapper->m_toolViewConstructors.type()<<endl; + m_wrapper->m_newWindowHandler=(args.size()>1)?args[1]:KJS::Value(); + m_wrapper->m_removeWindowHandler=(args.size()>2)?args[2]:KJS::Value(); + } else if (m_id==KJSConsole) { + m_wrapper->m_part->view()->show(); + } else + kdDebug()<<"Remove not implemented yet"<<endl; + return KJS::Boolean(true); +} + +void Kate::JS::Application::addBindings(KJS::ExecState *exec, KJSEmbed::JSObjectProxy *proxy,KJS::Object &object){ + PluginKateKJSWrapper *wrap=static_cast<PluginKateKJSWrapper*>(proxy->part()->parent()); + KJS::Object ToolView(new Application( exec, ToolView, proxy ,wrap)); + ToolView.put(exec,KJS::Identifier("Left"),KJS::Number(KDockWidget::DockLeft) ,KJS::ReadOnly | KJS::DontDelete); + ToolView.put(exec,KJS::Identifier("Top"),KJS::Number(KDockWidget::DockTop) ,KJS::ReadOnly | KJS::DontDelete); + ToolView.put(exec,KJS::Identifier("Right"),KJS::Number(KDockWidget::DockRight) ,KJS::ReadOnly | KJS::DontDelete); + ToolView.put(exec,KJS::Identifier("Bottom"),KJS::Number(KDockWidget::DockBottom), KJS::ReadOnly | KJS::DontDelete); + General::addBindings(exec,proxy,ToolView); + object.put(exec, "ToolView",ToolView); + + object.put(exec, KJS::Identifier("DocumentManager"),proxy->part()->bind(::Kate::documentManager()),KJS::ReadOnly | KJS::DontDelete); + + object.put(exec, "windowCount", KJS::Object(new Application( exec, WindowCount, proxy,wrap))); + object.put(exec, "activeWindow", KJS::Object(new Application( exec, ActiveWindow, proxy,wrap))); + object.put(exec, "window", KJS::Object(new Application( exec, Window, proxy,wrap ))); + +// object.put(exec, "ProjectManager",proxy->part()->bind(::Kate::projectManager()); + +/* obbject.put(exec, KJS::Identifier("WindowManager"),proxy->part + KJS::Object*/ +/* + Kate::PluginManager *pluginManager (); + + Kate::InitPluginManager *initPluginManager (); + + Kate::MainWindow *activeMainWindow ();*/ + + // uint mainWindows (); + // Kate::MainWindow *mainWindow (uint n = 0); + +} + +Kate::JS::Application::Application( KJS::ExecState *exec, int id, KJSEmbed::JSObjectProxy *parent,PluginKateKJSWrapper *plugin):KJSEmbed::JSProxyImp(exec) { + kdDebug()<<"Kate::JS::Application::Application"<<endl; + m_id=id; + m_proxy=parent; + m_plugin=plugin; +} + +Kate::JS::Application::~Application() { +} + +KJS::Value Kate::JS::Application::call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ) { + QObject *o=m_proxy->object(); + Kate::Application *ka=dynamic_cast<Kate::Application*>(o); + if (!ka) { + kdWarning()<<"Object died"<<endl; + QString msg = i18n("Call of KATE member on destroyed object"); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); + } + QString mdesc; + switch (m_id) { + case WindowCount: { + mdesc="windowCount()"; + if (args.size()!=0) break; + return KJS::Number(ka->mainWindows()); } + break; + case Window: { + mdesc="window(int)"; + if (args.size()!=1) break; + uint index=args[0].toUInt32(exec); + if (exec->hadException()) break; + Kate::MainWindow *mw=ka->mainWindow(index); + if (!mw) return KJS::Null(); + return m_plugin->getViewObject(mw)->winObj; + } + break; + case ActiveWindow: { + mdesc="activeWindow()"; + if (args.size()!=0) break; + Kate::MainWindow *mw=ka->activeMainWindow(); + if (!mw) return KJS::Null(); + return m_plugin->getViewObject(mw)->winObj; + } + break; + + + } + QString msg = i18n("Method %1 called with wrong signature").arg(mdesc); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); + +} + + + + + + +void Kate::JS::General::addBindings(KJS::ExecState *exec, KJSEmbed::JSObjectProxy *proxy,KJS::Object &object){ +#warning "try to find a way to make the function implementations static, right now this doesn't work because of the need to retrieve the interpreter" + KJS::Object methods= KJS::Object(new General( exec,proxy->interpreter(),MethodMethods)); + KJS::Object fields= KJS::Object(new General( exec,proxy->interpreter(),MethodFields)); + object.put(exec, "methods", methods); + object.put(exec, "fields", fields); +} + +Kate::JS::General::General( KJS::ExecState *exec, KJS::Interpreter *interpreter, int id):KJSEmbed::JSProxyImp(exec) { + m_id=id; + m_interpreter=interpreter; +} + + +KJS::Value Kate::JS::General::call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ) { + switch (m_id) { + case MethodMethods: + return methodz(exec,self,args); + case MethodFields: + return fieldz(exec,self,args); + default: + return KJS::Null(); + } +} + + + + +KJS::Value Kate::JS::General::methodz(KJS::ExecState *exec, KJS::Object &obj, const KJS::List &) +{ + KJS::List items; + + KJS::ReferenceList list=obj.propList(exec, /*bool recursive*/ false); + for (KJS::ReferenceListIterator it=list.begin();it!=list.end();it++) { + if (it->getValue(exec).toObject(exec).implementsCall()) items.append(KJS::String(it->getPropertyName(exec).qstring())); + } + return KJS::Object(m_interpreter->builtinArray().construct(exec,items) ); +} + +KJS::Value Kate::JS::General::fieldz(KJS::ExecState *exec, KJS::Object &obj, const KJS::List &) +{ + KJS::List items; + + KJS::ReferenceList list=obj.propList(exec, /*bool recursive*/ false); + for (KJS::ReferenceListIterator it=list.begin();it!=list.end();it++) { + if (!(it->getValue(exec).toObject(exec).implementsCall())) items.append(KJS::String(it->getPropertyName(exec).qstring())); + } + return KJS::Object(m_interpreter->builtinArray().construct(exec,items) ); +} + + +Kate::JS::RefCountedObjectDict::RefCountedObjectDict(int size): QObject(), QPtrDict<ObjectEntry>(size) { + m_usageCount=0; + setAutoDelete(true); +} + +void Kate::JS::RefCountedObjectDict::incRef() { + m_usageCount++; +} + +void Kate::JS::RefCountedObjectDict::decRef() { + kdDebug()<<"Kate::JS:RefCountedObjectDict::decCount()"<<endl; + m_usageCount--; + if (m_usageCount<1) deleteLater(); + +} + +KJS::Object Kate::JS::RefCountedObjectDict::jsObject(KJS::ExecState *exec, QObject *obj, KJSEmbed::JSObjectProxy *proxy) { + ObjectEntry *oe=find(obj); + if (oe==0) { + oe=new ObjectEntry; + oe->obj=proxy->part()->factory()->createProxy(exec,obj,proxy); + connect(obj,SIGNAL(destroyed()),this,SLOT(removeSender())); + insert(obj,oe); + return oe->obj; + } else return oe->obj; + +} + +void Kate::JS::RefCountedObjectDict::removeSender() { + kdDebug()<<"Trying to remove object from dict"<<sender()<<endl; + remove((void*)sender()); +} + + +void Kate::JS::MainWindow::addBindings(KJS::ExecState *exec, KJSEmbed::JSObjectProxy *proxy,KJS::Object &object){ + kdDebug()<<"Kate::JS::MainWindow::addBindings - 1"<<endl; + if (!proxy) + return; + kdDebug()<<"Kate::JS::MainWindow::addBindings - 2"<<endl; + + Kate::MainWindow *mw=dynamic_cast<Kate::MainWindow*>(proxy->object()); + if (!mw) return; + kdDebug()<<"Kate::JS::MainWindow::addBindings - 3"<<endl; + + PluginKateKJSWrapper *wrap=static_cast<PluginKateKJSWrapper*>(proxy->part()->parent()); + if (!wrap) return; + kdDebug()<<"Kate::JS::MainWindow::addBindings - 4"<<endl; + + object.put(exec, "actionCollection", KJS::Object(new MainWindow( exec, ActionCollection, proxy,wrap))); + +} + + + +Kate::JS::MainWindow::MainWindow( KJS::ExecState *exec, int id, KJSEmbed::JSObjectProxy *parent,PluginKateKJSWrapper *plugin):KJSEmbed::JSProxyImp(exec) { + kdDebug()<<"Kate::JS::MainWindow::MainWindow"<<endl; + m_id=id; + m_proxy=parent; + m_plugin=plugin; +} + +Kate::JS::MainWindow::~MainWindow() { +} + +KJS::Value Kate::JS::MainWindow::call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ) { + QObject *o=m_proxy->object(); + Kate::MainWindow *mw=dynamic_cast<Kate::MainWindow*>(o); + if (!mw) { + kdWarning()<<"Object died"<<endl; + QString msg = i18n("Call of MainWindow member on destroyed object"); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); + } + QString mdesc; + switch (m_id) { + case ActionCollection: { + mdesc="actionCollection()"; + if (args.size()!=0) break; + return m_plugin->getViewObject(mw)->actionCollectionObj; + } + break; + default: + return KJS::Undefined(); + } + QString msg = i18n("Method %1 called with wrong signature").arg(mdesc); + KJS::Object err = KJS::Error::create( exec, KJS::GeneralError, msg.utf8() ); + exec->setException( err ); + return KJS::Undefined(); +} + diff --git a/kate/kjswrapper/bindings.h b/kate/kjswrapper/bindings.h new file mode 100644 index 0000000..2e98ee8 --- /dev/null +++ b/kate/kjswrapper/bindings.h @@ -0,0 +1,150 @@ +#ifndef _KATE_JS_BINDINGS_H_ +#define _KATE_JS_BINDINGS_H_ + +#include <kjsembed/jsbindingplugin.h> +#include <kjsembed/jsproxy_imp.h> +#include <kjsembed/jsobjectproxy.h> +#include <kjsembed/jsfactory.h> +#include <qptrdict.h> + +class PluginKateKJSWrapper; + +namespace Kate { + namespace JS { + + + struct ObjectEntry { + KJS::Object obj; + }; + + class RefCountedObjectDict: public QObject, public QPtrDict<ObjectEntry> { + Q_OBJECT + public: + RefCountedObjectDict(int size); + void incRef(); + void decRef(); + KJS::Object jsObject(KJS::ExecState *exec, QObject *obj, KJSEmbed::JSObjectProxy *proxy); + public slots: + void removeSender(); + private: + int m_usageCount; + }; + + + class Bindings: public KJSEmbed::Bindings::JSBindingPlugin { + public: + Bindings(QObject *parent); + virtual ~Bindings(); + KJS::Object createBinding(KJSEmbed::KJSEmbedPart *jspart, KJS::ExecState *exec, const KJS::List &args) const; + void addBindings(KJS::ExecState *exec, KJS::Object &target) const; + }; + + class DocumentManager: public KJSEmbed::JSProxyImp { + public: + enum MethodID { + Document, + ActiveDocument, + DocumentWithID, + FindDocument, + IsOpen, + OpenURL, + Documents, + CloseDocument, + CloseAllDocuments + }; + virtual bool implementsCall() const { return true; } + virtual KJS::Value call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ); + static void addBindings(KJS::ExecState *exec, KJSEmbed::JSObjectProxy *proxy,KJS::Object &target); + private: + DocumentManager( KJS::ExecState *exec, int id, KJSEmbed::JSObjectProxy *parent, RefCountedObjectDict *dict ); + virtual ~DocumentManager(); + private: + RefCountedObjectDict *m_dict; + int m_id; + KJSEmbed::JSObjectProxy *m_proxy; + + }; + + class Management: public KJSEmbed::JSProxyImp { + public: + enum MethodID { + AddConfigPage, + SetConfigPages, + RemoveConfigPage, + SetWindowConfiguration, + KJSConsole + }; + Management( KJS::ExecState *exec, int id, class PluginKateKJSWrapper *kateplug); + virtual bool implementsCall() const { return true; } + virtual KJS::Value call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ); + + private: + int m_id; + class PluginKateKJSWrapper *m_wrapper; + + }; + + class Application: public KJSEmbed::JSProxyImp { + public: + enum MethodID { + ToolView, + WindowCount, + Window, + ActiveWindow, + + }; + virtual bool implementsCall() const { return true; } + virtual KJS::Value call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ); + static void addBindings(KJS::ExecState *exec, KJSEmbed::JSObjectProxy *proxy,KJS::Object &target); + private: + Application( KJS::ExecState *exec, int id, KJSEmbed::JSObjectProxy *parent, PluginKateKJSWrapper *plugin ); + ~Application(); + private: + int m_id; + KJSEmbed::JSObjectProxy *m_proxy; + PluginKateKJSWrapper *m_plugin; + }; + + class General: public KJSEmbed::JSProxyImp { + public: + enum MethodID { + MethodMethods, + MethodFields + }; + virtual bool implementsCall() const { return true; } + virtual KJS::Value call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ); + virtual KJS::Value fieldz( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ); + virtual KJS::Value methodz( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ); + static void addBindings(KJS::ExecState *exec,KJSEmbed::JSObjectProxy *proxy,KJS::Object &target); + private: + General( KJS::ExecState *exec,KJS::Interpreter *interpreter,int id); + + private: + int m_id; + KJS::Interpreter *m_interpreter; + }; + + + class MainWindow: public KJSEmbed::JSProxyImp { + public: + enum MethodID { + ActionCollection + + }; + virtual bool implementsCall() const { return true; } + virtual KJS::Value call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args ); + static void addBindings(KJS::ExecState *exec, KJSEmbed::JSObjectProxy *proxy,KJS::Object &target); + private: + MainWindow( KJS::ExecState *exec, int id, KJSEmbed::JSObjectProxy *parent, PluginKateKJSWrapper *plugin ); + ~MainWindow(); + private: + int m_id; + KJSEmbed::JSObjectProxy *m_proxy; + PluginKateKJSWrapper *m_plugin; + }; + + + } +} + +#endif diff --git a/kate/kjswrapper/plugin_katekjswrapper.cpp b/kate/kjswrapper/plugin_katekjswrapper.cpp new file mode 100644 index 0000000..c3e54b7 --- /dev/null +++ b/kate/kjswrapper/plugin_katekjswrapper.cpp @@ -0,0 +1,437 @@ +/* This file is part of the KDE project + Copyright (C) 2004 Joseph Wenninger <jowenn@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 version 2 as published by the Free Software Foundation. + + 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., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +//BEGIN includes +#include "plugin_katekjswrapper.h" +#include "plugin_katekjswrapper.moc" +#include "bindings.h" + +#include <kjsembed/kjsembedpart.h> +#include <kjsembed/jssecuritypolicy.h> +#include <kjsembed/jsfactory.h> +#include <kjsembed/jsconsolewidget.h> +#include <kjs/interpreter.h> +#include <kjs/value.h> +#include <kjs/object.h> +#include <kgenericfactory.h> +#include <kdebug.h> +#include <qlayout.h> +#include <kstandarddirs.h> +#include <kate/mainwindow.h> +#include <kate/toolviewmanager.h> +#include <kdockwidget.h> +#include <qvbox.h> +//END includes + +K_EXPORT_COMPONENT_FACTORY( katekjswrapperplugin, KGenericFactory<PluginKateKJSWrapper>( "katekjswrapper" ) ) + +PluginKateKJSWrapperView::~PluginKateKJSWrapperView() { +} + +void PluginKateKJSWrapperView::removeFromWindow() { + kdDebug()<<"PluginKateKJSWrapperView::removeFromWindow"<<endl; + for (QValueList<QGuardedPtr<KMDI::ToolViewAccessor> >::iterator it=toolviews.begin();it!=toolviews.end();it=toolviews.begin()) { + kdDebug()<<"removeFromWindow: removing a toolview"<<endl; + KMDI::ToolViewAccessor* tva=(*it); + toolviews.remove(it); + win->toolViewManager()->removeToolView (tva); + } + win->guiFactory()->removeClient (this); + } + +PluginKateKJSWrapper::PluginKateKJSWrapper( QObject* parent, const char* name, const QStringList& list) + : Kate::Plugin ( (Kate::Application *)parent, name ) { + m_views.setAutoDelete(true); + m_scriptname=list[0]; + m_kateAppBindings=new Kate::JS::Bindings(this); + KJSEmbed::JSSecurityPolicy::setDefaultPolicy( KJSEmbed::JSSecurityPolicy::CapabilityAll ); + m_part = new KJSEmbed::KJSEmbedPart(this); + KJS::Interpreter *js = m_part->interpreter(); + + KJSEmbed::JSFactory *factory=m_part->factory(); + +/* factories for kate app classes */ + factory->addQObjectPlugin("Kate::Application",m_kateAppBindings); + factory->addQObjectPlugin("Kate::DocumentManager",m_kateAppBindings); + factory->addQObjectPlugin("Kate::MainWindow",m_kateAppBindings); + factory->addQObjectPlugin("Kate::PluginManager",m_kateAppBindings); + factory->addQObjectPlugin("Kate::InitPluginManager",m_kateAppBindings); + factory->addQObjectPlugin("Kate::ProjectManager",m_kateAppBindings); + factory->addQObjectPlugin("Kate::Project",m_kateAppBindings); + factory->addQObjectPlugin("Kate::ViewManager",m_kateAppBindings); + factory->addQObjectPlugin("Kate::View",m_kateAppBindings); +/* toplevel objects*/ + KJS::Object appobj=m_part->addObject(Kate::application(),"KATE"); + js->globalObject().put( js->globalExec(), "addConfigPage", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::AddConfigPage,this ))); + js->globalObject().put( js->globalExec(), "setConfigPages", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::SetConfigPages,this ))); + js->globalObject().put( js->globalExec(), "removeConfigPage", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::RemoveConfigPage,this ))); + js->globalObject().put( js->globalExec(), "setWindowConfiguration", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::SetWindowConfiguration,this ))); + js->globalObject().put( js->globalExec(), "KJSConsole", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::KJSConsole,this ))); + +/* KJSEmbed::JSConsoleWidget *w=m_part->view(); + w->show(); + //w->show();*/ + kdDebug()<<"m_scriptname="<<m_scriptname<<endl; + m_part->runFile(locate("appdata",QString("plugins/%1/%2.js").arg(m_scriptname).arg(m_scriptname))); +//"/home/jowenn/development/kde/cvs/kdeaddons/kate/kjswrapper/samples/test1.js"); +} + +PluginKateKJSWrapper::~PluginKateKJSWrapper() +{ + delete m_part; + m_part=0; +} + + +uint PluginKateKJSWrapper::configPages () const { + KJS::Interpreter *js = m_part->interpreter(); + KJS::ExecState *exec=js->globalExec(); + + if (! (m_configPageFactories.isNull() || (m_configPageFactories.type()==KJS::NullType))) { + KJS::Object constrs=m_configPageFactories.toObject(exec); + if (!exec->hadException()) { + if (QString(constrs.classInfo()->className)=="Array") { + kdDebug()<<"config page constructor array detected"<<endl; + uint size=constrs.get(exec,KJS::Identifier("length")).toInteger(exec); + if (exec->hadException()) { + exec->clearException(); + kdDebug()<<"Error while retrieving array length"<<endl; + } + else return size; + } else return 1; + } + } + exec->clearException(); + return 0; +} + + +static KJS::Object getObj(KJS::Interpreter *js, KJS::Value mightBeArray, int id) { + KJS::ExecState *exec=js->globalExec(); + KJS::Object constrs=mightBeArray.toObject(exec); + KJS::Value constr; + if (!exec->hadException()) { + if (QString(constrs.classInfo()->className)=="Array") { + kdDebug()<<"config page constructor array detected"<<endl; + constr=constrs.get(exec,id); + } else constr=mightBeArray; + + } + return constr.toObject(js->globalExec()); +} + +QString PluginKateKJSWrapper::configPageName(uint id) const { + if (id>=configPages()) return ""; + KJS::Interpreter *js = m_part->interpreter(); + + KJS::Object constr=getObj(js,m_configPageFactories,id); + + KJS::Value o=constr.get(js->globalExec(),KJS::Identifier("name")); + QString retVal( o.toString(js->globalExec()).qstring() ); + + kdDebug()<<"=============================================================================================="<<endl; + kdDebug()<<"PluginKateKJSWrapper::configPageName: "<<retVal<<endl; + kdDebug()<<"=============================================================================================="<<endl; + js->globalExec()->clearException(); + return retVal; +} + +QString PluginKateKJSWrapper::configPageFullName(uint id) const { + if (id>=configPages()) return ""; + KJS::Interpreter *js = m_part->interpreter(); + + KJS::Object constr=getObj(js,m_configPageFactories,id); + + KJS::Value o=constr.get(js->globalExec(),KJS::Identifier("fullName")); + QString retVal( o.toString(js->globalExec()).qstring() ); + + kdDebug()<<"=============================================================================================="<<endl; + kdDebug()<<"PluginKateKJSWrapper::configPageFullName: "<<retVal<<endl; + kdDebug()<<"=============================================================================================="<<endl; + js->globalExec()->clearException(); + return retVal; +} + +QPixmap PluginKateKJSWrapper::configPagePixmap (uint /*number = 0*/, + int /*size = KIcon::SizeSmall*/) const { + return 0; +} + + +Kate::PluginConfigPage* PluginKateKJSWrapper::configPage (uint id, + QWidget *w, const char */*name*/) { + kdDebug()<<"PluginKateKJSWrapper::configPage"<<endl; + + if (id>=configPages()) return 0; + KJS::Interpreter *js = m_part->interpreter(); + + KJS::Object constr=getObj(js,m_configPageFactories,id); + + if (js->globalExec()->hadException()) { + kdDebug()<<"PluginKateKJSWrapper::configPage: exit 1"<<endl; + js->globalExec()->clearException(); + return 0; + } + + if (!constr.implementsConstruct()) { + kdWarning()<<"config page factory has to be an object constructor"<<endl; + return 0; + } + + KateKJSWrapperConfigPage *p=new KateKJSWrapperConfigPage(constr,this,w); + return (Kate::PluginConfigPage*)p; +/* + KateKJSWrapperConfigPage* p = new KateKJSWrapperConfigPage(this, w); + //init + connect( p, SIGNAL(configPageApplyRequest(KateKJSWrapperConfigPage*)), + this, SLOT(applyConfig(KateKJSWrapperConfigPage*)) ); + return (Kate::PluginConfigPage*);*/ +} + + + + +static KMDI::ToolViewAccessor *createToolView(KJSEmbed::JSFactory *factory,KJS::Interpreter *js, Kate::MainWindow *winN,KJS::Object win,KJS::Object viewConstructor) { + KJS::List params; + KJS::ExecState *exec = js->globalExec(); + params.append(win); + exec->clearException(); + int dockPos; + if (!viewConstructor.implementsConstruct()) return 0; + KJS::Value dockPosV=viewConstructor.get(exec,KJS::Identifier("startPosition")); + if (exec->hadException()) { + dockPos=KDockWidget::DockLeft; + exec->clearException(); + } else { + dockPos=dockPosV.toInteger(exec); + if (exec->hadException()) { + dockPos=KDockWidget::DockLeft; + exec->clearException(); + } + } + QString viewName; + KJS::Value viewNameV=viewConstructor.get(exec,KJS::Identifier("name")); + if (exec->hadException()) { + viewName="kjs_unknown"; + exec->clearException(); + } else { + viewName=QString( viewNameV.toString(exec).qstring() ); + if (exec->hadException()) { + viewName="kjs_unknown"; + exec->clearException(); + } + } + + Kate::JS::ToolView *tv=new Kate::JS::ToolView(viewConstructor,exec,factory,params,viewName.utf8()); + //params.append(factory->createProxy(exec,tv)); + //KJS::Object otv=viewConstructor.construct(exec,params); + if (exec->hadException()) { + kdDebug()<<"Error while calling constructor"<<endl; + delete tv; + kdDebug()<<exec->exception().toString(exec).qstring()<<endl; + exec->clearException(); + return 0; + } + KMDI::ToolViewAccessor *tva=winN->toolViewManager()->addToolView((KDockWidget::DockPosition)dockPos,tv, + tv->icon()?(*(tv->icon())):QPixmap(),tv->caption()); + kdDebug()<<"****************************************************************************************"<<endl; + kdDebug()<<"PluginKateKJSWrapper: Toolview has been added"<<endl; + kdDebug()<<"****************************************************************************************"<<endl; + return tva; + +} + +PluginKateKJSWrapperView *PluginKateKJSWrapper::getViewObject(Kate::MainWindow *win) { + PluginKateKJSWrapperView * view=m_views[win]; + if (!view) { + view=new PluginKateKJSWrapperView(); + view->win=win; + connect(win,SIGNAL(destroyed()),this,SLOT(slotWindowDestroyed())); + m_views.insert(win,view); + KJS::Interpreter *js = m_part->interpreter(); + KJS::ExecState *exec = js->globalExec(); + view->actionCollectionObj=m_part->factory()->createProxy(exec,view->actionCollection()); + view->winObj=m_part->factory()->createProxy(exec,win); + } else kdDebug()<<"returning cached View/Window Object"<<endl; + return view; +} + +void PluginKateKJSWrapper::addView(Kate::MainWindow *win) +{ + PluginKateKJSWrapperView * view=getViewObject(win); // this is needed to ensure correct caching the javascript object + KJS::Interpreter *js = m_part->interpreter(); + KJS::ExecState *exec = js->globalExec(); + exec->clearException(); + kdDebug()<<"****************************************************************************************"<<endl; + kdDebug()<<"PluginKateKJSWrapper::addView"<<endl; + kdDebug()<<"****************************************************************************************"<<endl; + kdDebug()<<"checking for newWindowHandler"<<endl; + if (!m_newWindowHandler.isNull()) { + KJS::List param; + param.append(view->winObj); + KJS::Object newWinFunc=m_newWindowHandler.toObject(exec); + if (exec->hadException()) { + exec->clearException(); + } else { + if (newWinFunc.implementsCall()) { + newWinFunc.call(exec,js->globalObject(),param); + if (exec->hadException()) { + kdDebug()<<"Error while calling newWindowHandler"<<endl; + exec->clearException(); + } + } + } + } + if (exec->hadException()) kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): exec had an exception - 1"<<endl; + + kdDebug()<<"checking for toolview constructors"<<endl; + if (! (m_toolViewConstructors.isNull() || (m_toolViewConstructors.type()==KJS::NullType))) { + KJS::Object constrs=m_toolViewConstructors.toObject(exec); + if (!exec->hadException()) { + if (QString(constrs.classInfo()->className)=="Array") { + kdDebug()<<"Toolview constructor array detected"<<endl; + int size=constrs.get(exec,KJS::Identifier("length")).toInteger(exec); + if (exec->hadException()) { + exec->clearException(); + kdDebug()<<"Error while retrieving array length"<<endl; + } + else { + for (int i=0;i<size;i++) { + KJS::Object constrO=constrs.get(exec,i).toObject(exec); + if (exec->hadException()) { + exec->clearException(); + } else { + KMDI::ToolViewAccessor *w=createToolView(m_part->factory(),js,win,view->winObj,constrO); + if (w) { + view->toolviews.append(QGuardedPtr<KMDI::ToolViewAccessor>(w)); + } + exec->clearException(); + } + } + } + } else { + kdDebug()<<"Single toolview constructor detected"<<endl; + if (!constrs.implementsConstruct()) { + kdWarning()<<"wrong object type"<<endl; + } else { + KMDI::ToolViewAccessor *w=createToolView(m_part->factory(),js,win,view->winObj,constrs); + if (w) { + view->toolviews.append(QGuardedPtr<KMDI::ToolViewAccessor>(w)); + } + exec->clearException(); + } + } + + } + } else kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): no toolview constructors"<<endl; + + + if (exec->hadException()) kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): exec had an exception - 2"<<endl; + + view->setInstance (new KInstance("kate")); + view->setXMLFile(QString("plugins/%1/%2.rc").arg(m_scriptname).arg(m_scriptname)); + win->guiFactory()->addClient (view); +} + + +void PluginKateKJSWrapper::slotWindowDestroyed() { + m_views.remove((void*)sender()); +} + +void PluginKateKJSWrapper::removeView(Kate::MainWindow *win) +{ +//here toolviews must not be destroyed. Only cleanup functions called the view should be removed in the slot connected to the windows destroy signal only + m_views[win]->removeFromWindow(); +} + + + +void PluginKateKJSWrapper::applyConfig( KateKJSWrapperConfigPage *p ) +{ +#if 0 + config->writeEntry( "Command History Length", p->sb_cmdhistlen->value() ); + // truncate the cmd hist if nessecary? + config->writeEntry( "Start In", p->rg_startin->id(p->rg_startin->selected()) ); + config->sync(); +#endif +} + +KateKJSWrapperConfigPage::KateKJSWrapperConfigPage(KJS::Object pageConstructor,PluginKateKJSWrapper* parent, + QWidget *parentWidget) + : Kate::PluginConfigPage( parentWidget ),m_plugin(parent) +{ + QVBoxLayout *l=new QVBoxLayout(this); + l->setAutoAdd(true); + l->activate(); + KJS::Interpreter *js = parent->m_part->interpreter(); + KJS::ExecState *exec = js->globalExec(); + exec->clearException(); + KJS::List param; + param.append(parent->m_part->factory()->createProxy(exec,this,0)); + m_pageObject=pageConstructor.construct(exec,param); +} + + +static void callJS(KJSEmbed::KJSEmbedPart *p,KJS::Object o,const QString& funcName){ + KJS::Interpreter *js = p->interpreter(); + KJS::ExecState *exec = js->globalExec(); + KJS::List param; + exec->clearException(); + KJS::Value funcV=o.get(exec,KJS::Identifier(funcName)); + if (exec->hadException()) { +#warning clear exception ? + return; + } + KJS::Object func=funcV.toObject(exec); + if (exec->hadException()) { +#warning clear exception ? + return; + } + if (func.implementsCall()) { + func.call(exec,o,param); + if (js->globalExec()->hadException()) { +#warning clear exception ? + return; + } + } +} + +void KateKJSWrapperConfigPage::apply() +{ + callJS(m_plugin->m_part,m_pageObject,"apply"); +} + +void KateKJSWrapperConfigPage::reset() +{ + callJS(m_plugin->m_part,m_pageObject,"reset"); +} + +void KateKJSWrapperConfigPage::defaults() +{ + callJS(m_plugin->m_part,m_pageObject,"defaults"); +} + + +Kate::JS::ToolView::ToolView(KJS::Object constr, KJS::ExecState *exec, KJSEmbed::JSFactory *factory, KJS::List parameters, const char *name):QVBox(0,name) { + parameters.append(factory->createProxy(exec,this)); + handler=constr.construct(exec,parameters); + +} + +Kate::JS::ToolView::~ToolView() { +} + diff --git a/kate/kjswrapper/plugin_katekjswrapper.h b/kate/kjswrapper/plugin_katekjswrapper.h new file mode 100644 index 0000000..f04043e --- /dev/null +++ b/kate/kjswrapper/plugin_katekjswrapper.h @@ -0,0 +1,180 @@ +/* This file is part of the KDE project + Copyright (C) 2004 Joseph Wenninger <jowenn@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 version 2 as published by the Free Software Foundation. + + 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., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef _PLUGIN_KATE_KJS_WRAPPER_H_ +#define _PLUGIN_KATE_KJS_WRAPPER_H_ + +#include <kate/application.h> +#include <kate/documentmanager.h> +#include <kate/document.h> +#include <kate/pluginconfiginterface.h> +#include <kate/pluginconfiginterfaceextension.h> +#include <kate/mainwindow.h> +#include <kate/plugin.h> +#include <kate/view.h> +#include <kate/viewmanager.h> + +#include <kcombobox.h> +#include <kdialogbase.h> +#include <klibloader.h> +#include <klocale.h> +#include <kurlrequester.h> +#include <qcheckbox.h> +#include <qvaluelist.h> +#include <kjs/value.h> +#include <kjs/object.h> +#include <qvbox.h> +#include <qptrdict.h> +#include <kxmlguiclient.h> +#include <qvaluelist.h> +#include <qguardedptr.h> + +namespace Kate { + class PluginConfigPage; + namespace JS { + class Bindings; + class Management; + class RefCountedObjectDict; + class Application; + class MainWindow; + } +} + + +namespace KJSEmbed { + class KJSEmbedPart; + class JSFactory; +} + +class KateKJSWrapperConfigPage; +class PluginKateKJSWrapperView; + +namespace KMDI { + class ToolViewAccessor; +} + +class PluginKateKJSWrapper : public Kate::Plugin, + Kate::PluginViewInterface, + Kate::PluginConfigInterfaceExtension +{ + Q_OBJECT + + public: + PluginKateKJSWrapper( QObject* parent = 0, const char* name = 0, const QStringList& = QStringList() ); + virtual ~PluginKateKJSWrapper(); + + void addView (Kate::MainWindow *win); + void removeView (Kate::MainWindow *win); + + Kate::View *kv; + + + QPtrDict<class PluginKateKJSWrapperView> m_views; + uint configPages () const; + Kate::PluginConfigPage *configPage (uint , QWidget *w, const char *name=0); + QString configPageName(uint) const; + QString configPageFullName(uint) const; + QPixmap configPagePixmap (uint /*number = 0*/, + int /*size = KIcon::SizeSmall*/) const; + PluginKateKJSWrapperView *getViewObject(Kate::MainWindow *win); + + public slots: + //void slotInsertCommand(); + //void slotAbort(); + void applyConfig( KateKJSWrapperConfigPage* ); + void slotWindowDestroyed(); + + private: + friend class Kate::JS::Management; + friend class KateKJSWrapperConfigPage; + KJSEmbed::KJSEmbedPart *m_part; + Kate::JS::Bindings *m_kateAppBindings; + //QValueList<KJS::Value> m_configPageFactories; + KJS::Value m_configPageFactories; + KJS::Value m_toolViewConstructors; + KJS::Value m_newWindowHandler; + KJS::Value m_removeWindowHandler; + QString m_scriptname; + }; + + +/** Config page for the plugin. */ +class KateKJSWrapperConfigPage : public Kate::PluginConfigPage +{ + Q_OBJECT + friend class PluginKateKJSWrapper; + + public: + KateKJSWrapperConfigPage(KJS::Object pageConstructor,PluginKateKJSWrapper* parent = 0L, QWidget *parentWidget = 0L); + ~KateKJSWrapperConfigPage() {}; + + /** Reimplemented from Kate::PluginConfigPage + * just emits configPageApplyRequest( this ). + */ + void apply(); + + void reset (); + void defaults (); + + signals: + /** Ask the plugin to set initial values */ + void configPageApplyRequest( KateKJSWrapperConfigPage* ); + /** Ask the plugin to apply changes */ + void configPageInitRequest( KateKJSWrapperConfigPage* ); + + private: + KJS::Object m_pageObject; + PluginKateKJSWrapper *m_plugin; + }; + +class PluginKateKJSWrapperView : public KXMLGUIClient +{ + public: + + virtual ~PluginKateKJSWrapperView(); + + private: + friend class PluginKateKJSWrapper; + friend class Kate::JS::Application; + friend class Kate::JS::MainWindow; + void removeFromWindow(); + + Kate::MainWindow *win; + KJS::Object winObj; + KJS::Object actionCollectionObj; + QValueList<QGuardedPtr<KMDI::ToolViewAccessor> > toolviews; +}; + + + +namespace Kate { + namespace JS { + class ToolView: public QVBox { + Q_OBJECT + public: + ToolView(KJS::Object constr, KJS::ExecState *exec, KJSEmbed::JSFactory *factory, KJS::List parameters, const char * name); + virtual ~ToolView(); + private: + KJS::Object handler; + }; + } + +} + +#endif // _PLUGIN_KATE_KJS_WRAPPER_H_ + diff --git a/kate/kjswrapper/samples/Makefile.am b/kate/kjswrapper/samples/Makefile.am new file mode 100644 index 0000000..ee9a457 --- /dev/null +++ b/kate/kjswrapper/samples/Makefile.am @@ -0,0 +1,23 @@ + +# Install the .rc file in the Part's directory (in this case, the part +# is KHTMLPart) +#pluginsdir=$(kde_datadir)/kate/plugins/katekjswrapper +#plugins_DATA=ui.rc + +plugins1dir=$(kde_datadir)/kate/plugins/katekjstest1 +plugins1_DATA=katekjstest1.js + +pluginsconsolewindowdir=$(kde_datadir)/kate/plugins/katekjsconsolewindow +pluginsconsolewindow_DATA=katekjsconsolewindow.js katekjsconsolewindow.rc + +pluginsselectdir=$(kde_datadir)/kate/plugins/katekjsselect +pluginsselect_DATA=katekjsselect.js katekjsselect.rc + +#plugins2dir = $(kde_datadir)/kate/plugins/katekjstest2 +#plugins2_DATA = katekjstest2.js + +kde_services_DATA = katekjstest1.desktop katekjsconsolewindow.desktop katekjsselect.desktop +#kde_services_DATA = katekjstest2.desktop + +messages: rc.cpp + $(XGETTEXT) *.cpp -o $(podir)/kateinsertcommand.pot diff --git a/kate/kjswrapper/samples/katekjsconsolewindow.desktop b/kate/kjswrapper/samples/katekjsconsolewindow.desktop new file mode 100644 index 0000000..a882953 --- /dev/null +++ b/kate/kjswrapper/samples/katekjsconsolewindow.desktop @@ -0,0 +1,55 @@ +[Desktop Entry] +Type=Service +ServiceTypes=Kate/Plugin +X-KDE-Library=katekjswrapperplugin +X-Kate-Version=2.2 +X-Kate-PluginName=katekjsconsolewindow +Name=Kate Javascript Console Window +Name[bg]=Прозорец за изхода на Javascript +Name[ca]=Finestra de consola de Javascript per a Kate +Name[cs]=Okno javscriptové konzole Kate +Name[da]=Kate Javascript konsolvindue +Name[de]=Konsolenfenster für JavaScript in Kate +Name[el]=Παράθυρο κονσόλας Javascript του Kate +Name[eo]=Javaskripta konzolfenestro por Kodredaktilo +Name[es]=Ventana de la consola de Javascript de Kate +Name[et]=Kate Javascripti konsooliaken +Name[eu]=Kate Javascript konsola leihoa +Name[fa]=پنجرۀ پیشانۀ جاوااسکریپت Kate +Name[fi]=Kate Javascript konsoli-ikkuna +Name[fr]=Console Javascript pour Kate +Name[fy]=Kate Javascript console-finster +Name[ga]=Fuinneog Chonsóil Javascript Kate +Name[gl]=Fiestra de Consola de JavaScript para Kate +Name[he]= חלון תסריטי הג'אווה של Kate +Name[hi]=केएटीई जावास्क्रिप्ट कंसोल विंडो +Name[hr]=Kate terminalski prozor za Javascript +Name[hu]=Kate Javascript-es parancsértelmező ablak +Name[is]=Kate Javascript skjáhermir +Name[it]=Finestra console JavaScript di Kate +Name[ja]=Kate Javascript コンソールウィンドウ +Name[ka]=Kate Javascript კონსოლის ფანჯარა +Name[kk]=Kate Javascript консоль терезесі +Name[km]=បង្អួចកុងសូលរបស់ Kate Javascript +Name[mk]=прозорец со Javascript-конзола за Кате +Name[nb]=Javascript konsollvindu for Kate +Name[nds]=Javascript-Konsoolfinster för Kate +Name[ne]=केट जाभास्क्रिप्ट कन्सोल सञ्झ्याल +Name[nl]=Kate Javascript console-venster +Name[nn]=Javascript-konsollvindauge til Kate +Name[pa]=ਕੇਟ ਜਾਵਾਸਕਰਿਪ ਕੰਨਸੋਲ ਝਰੋਖਾ +Name[pl]=Okno konsoli Javascript dla Kate +Name[pt]=Janela de Consola de JavaScript do Kate +Name[pt_BR]=Janela de Console Javascript do Kate +Name[ru]=Окно консоли Javascript Kate +Name[sk]=Konzolové okno Kate Javascript +Name[sl]=Konzolno okno javascripta v Kate +Name[sr]=Конзолни прозор JavaSript-а за Kate +Name[sr@Latn]=Konzolni prozor JavaSript-a za Kate +Name[sv]=Kate Javaskript terminalfönster +Name[tg]=Тирезаи нозиргоҳи Kate Javascript +Name[tr]=Kate Javascript Konsol Penceresi +Name[uk]=Вікно консолі Javascript для Kate +Name[vi]=Cửa sổ bàn giao tiếp Javascript Kate +Name[zh_CN]=Kate Javascript 控制台窗口 +Name[zh_TW]=Kate Javascript 主控台視窗 diff --git a/kate/kjswrapper/samples/katekjsconsolewindow.js b/kate/kjswrapper/samples/katekjsconsolewindow.js new file mode 100644 index 0000000..e253ce0 --- /dev/null +++ b/kate/kjswrapper/samples/katekjsconsolewindow.js @@ -0,0 +1,18 @@ +function newWindowCallBack(mainwindow) { + var ac=mainwindow.actionCollection(); + action = new KAction( ac, 'kjsconsole_show_action' ); + action.text = 'Javascript Console Window'; + action.icon = 'konsole'; + + mainwindow.showConsole = function() + { + + KJSConsole(); + } + + action.connect( action, 'activated()', mainwindow, 'showConsole' ); + +} + +setWindowConfiguration(null,newWindowCallBack,null); + diff --git a/kate/kjswrapper/samples/katekjsconsolewindow.rc b/kate/kjswrapper/samples/katekjsconsolewindow.rc new file mode 100644 index 0000000..7ac34fc --- /dev/null +++ b/kate/kjswrapper/samples/katekjsconsolewindow.rc @@ -0,0 +1,8 @@ +<!DOCTYPE kpartgui> +<kpartplugin name="katekjsconsolewindow" library="libkatekjsconsolewindowplugin" version="1"> +<MenuBar> + <Menu name="tools"><Text>&Tools</Text> + <Action name="kjsconsole_show_action"/> + </Menu> +</MenuBar> +</kpartplugin> diff --git a/kate/kjswrapper/samples/katekjsselect.desktop b/kate/kjswrapper/samples/katekjsselect.desktop new file mode 100644 index 0000000..c4d1273 --- /dev/null +++ b/kate/kjswrapper/samples/katekjsselect.desktop @@ -0,0 +1,53 @@ +[Desktop Entry] +Type=Service +ServiceTypes=Kate/Plugin +X-KDE-Library=katekjswrapperplugin +X-Kate-Version=2.2 +X-Kate-PluginName=katekjsselect +Name=Select Smallest Enclosing Block +Name[ca]=Selecció del bloc tancat més petit +Name[cs]=Zvolte nejmenší blok +Name[da]=Vælg den mindste omsluttende blok +Name[de]=Kleinsten einschließenden Block auswählen +Name[el]=Επιλογή του μικρότερου περικλυόμενου κομματιού +Name[eo]=Elektu pli mallongan entenantan blokon +Name[es]=Selecciona un bloque cerrado más pequeño +Name[et]=Vali väikseim sulgev blokk +Name[eu]=Hautatu blokerik txikiena +Name[fa]=گزینش کوچکترین بلوک محصور +Name[fi]=Valitse pienin ympäröity lohko +Name[fr]=Sélection du plus petit bloc enfermé +Name[fy]=Lytste ynslutende blok selektearje +Name[gl]=Seleccionar o Menor Bloco Envolvente +Name[he]=בחר את בלוק הסגירה הכי קטן +Name[hr]=Odabir najmanjeg zatvorenog bloka +Name[hu]=A legkisebb bezáró blokk kijelölése +Name[is]=Velja minnstu umluktu blokkina +Name[it]=Seleziona il più piccolo blocco +Name[ja]=囲っている最も内側のブロックを選択します +Name[ka]=უმცირესი დახურვის ბლოკის ამორჩევა +Name[kk]=Ең шағын блокты таңдау +Name[km]=ជ្រើសប្លុកដែលបានព័ទ្ធជុំវិញតូចបំផុត +Name[lt]=Žymėti mažiausią bloką +Name[mk]=Најмал опкружувачки блок +Name[ms]=Pilih Blok Penutupan Terkecil +Name[nb]=Velg minste omsluttende blokk +Name[nds]=Den lüttsten insluten Block utsöken +Name[ne]=सामेल गरिने सबैभन्दा सानो खण्ड चयन गर्नुहोस् +Name[nl]=Kleinste insluitende blok selecteren +Name[nn]=Vel minste omsluttande blokk +Name[pl]=Wybierz najmniejszy blok zawierający +Name[pt]=Seleccionar o Menor Bloco Envolvente +Name[pt_BR]=Sleecione o Menor Bloco Aninhado +Name[ru]=Выбор наименьшего блока +Name[sk]=Zvolí najmenší ohraničujúci blok +Name[sl]=Izberite najmanjši obdajajoč blok +Name[sr]=Изаберите најмањи обухватајући блок +Name[sr@Latn]=Izaberite najmanji obuhvatajući blok +Name[sv]=Markera minsta omgivande block +Name[ta]=மிகச்சிறிய இணைப்பை தேர்ந்தெடு +Name[tr]=Çevreleyen en küçük bloğu seç +Name[uk]=Виберіть найменший блок включення +Name[vi]=Chọn khối chứa nhỏ nhất +Name[zh_CN]=选择最小的包围块 +Name[zh_TW]=選擇最小封閉區域 diff --git a/kate/kjswrapper/samples/katekjsselect.js b/kate/kjswrapper/samples/katekjsselect.js new file mode 100644 index 0000000..d6a33fb --- /dev/null +++ b/kate/kjswrapper/samples/katekjsselect.js @@ -0,0 +1,92 @@ +function newWindowCallBack(mainwindow) { + var ac=mainwindow.actionCollection(); + action = new KAction( ac, 'kjsselect_select_action' ); + action.text = 'Select enclosing block'; + //action.icon = 'konsole'; + + + mainwindow.selectIt = function() + { + endChars=Array(); + endChars['\"']="\""; + endChars['(']=")"; + endChars['[']="]"; + endChars['\'']="'"; + endChars['{']="}"; + endChar=""; + av=this.viewManager().activeView(); + d=KATE.DocumentManager.activeDocument(); + + lineCnt=d.numLines(); + x=av.cursorColumn(); + y=av.cursorLine(); + line=d.textLine(y); + sy=y; + sx=x-1; + while (true) { + if (sx<0) { + sy=sy-1; + if (sy<0) { + d.selectAll(); + return; + } + line=d.textLine(sy); + while (line.length==0) { + sy=sy-1; + if (sy<0) { + d.selectAll(); + return; + } + line=d.textLine(sy); + } + sx=line.length-1; + + } + if ( + (line[sx]=="\"") || + (line[sx]=="'") || + (line[sx]=="(") || + (line[sx]=="[") || + (line[sx]=="{") + ) { + endChar=endChars[line[sx]]; + break; + }else sx--; + } + + + alert("Searching end"); + ex=x; + ey=y; + line=d.textLine(y); + while (true) { + if (ex>=(line.length-1)) { + ey=ey+1; + if (ey>=lineCnt) { + d.selectAll(); + return; + } + line=d.textLine(ey); + while (line.length==0) { + ey=ey+1; + if (ey>=lineCnt) { + d.selectAll(); + return; + } + line=d.textLine(ey); + } + ex=0; + } + if (line[ex]==endChar) + break; else ex++; + } + d.setSelection(sy,sx,ey,ex); + + } + + action.connect( action, 'activated()', mainwindow, 'selectIt' ); + +} + +setWindowConfiguration(null,newWindowCallBack,null); + diff --git a/kate/kjswrapper/samples/katekjsselect.rc b/kate/kjswrapper/samples/katekjsselect.rc new file mode 100644 index 0000000..2b210a1 --- /dev/null +++ b/kate/kjswrapper/samples/katekjsselect.rc @@ -0,0 +1,8 @@ +<!DOCTYPE kpartgui> +<kpartplugin name="katekjsselect" library="libkatekjsselect" version="1"> +<MenuBar> + <Menu name="tools"><Text>&Tools</Text> + <Action name="kjsselect_select_action"/> + </Menu> +</MenuBar> +</kpartplugin> diff --git a/kate/kjswrapper/samples/katekjstest1.desktop b/kate/kjswrapper/samples/katekjstest1.desktop new file mode 100644 index 0000000..d5731b5 --- /dev/null +++ b/kate/kjswrapper/samples/katekjstest1.desktop @@ -0,0 +1,90 @@ +[Desktop Entry] +Type=Service +ServiceTypes=Kate/Plugin +X-KDE-Library=katekjswrapperplugin +X-Kate-Version=2.2 +X-Kate-PluginName=katekjstest1 +Name=Kate KJS Test 1 +Name[bs]=Kate KJS test 1 +Name[ca]=Test 1 per KJS a Kate +Name[cs]=Kate KJS test 1 +Name[el]=Kate KJS δοκιμή 1 +Name[eo]=KJS testo 1 de Kodredaktilo +Name[et]=Kate KJS test 1 +Name[eu]=Kate KJS-ren 1 froga +Name[fa]=آزمون ۱ Kate KJS +Name[fi]=Kate KJS testi 1 +Name[fr]=Test 1 KJS pour Kate +Name[ga]=Tástáil 1 Kate KJS +Name[gl]=Proba 1 para Kate KJS +Name[he]=ניסוי KJS של Kate מספר 1 +Name[hi]=के-एटीई केजेएस जाँच 1 +Name[is]=Kate KJS prufa 1 +Name[ja]=Kate KJS テスト 1 +Name[km]=សាកល្បង Kate KJS +Name[ms]=Kate KJS Ujian 1 +Name[nds]=Kate-KJSTest 1 +Name[ne]=केट केजेएस परीक्षण १ +Name[nn]=Kate KJS-test 1 +Name[pa]=ਕੇਟ KJS Test 1 +Name[pl]=KJS Test 1 dla Kate +Name[pt]=Teste 1 Kate do KJS +Name[pt_BR]=Teste Kate KJS 1 +Name[sl]=Kate KJS preizkus 1 +Name[sr]=Kate KJS, тест 1 +Name[sr@Latn]=Kate KJS, test 1 +Name[ta]=கேட் KJS சோதனை 1 +Name[tr]=Kate KJS Deneme1 +Name[uk]=Kate KJS Тест 1 +Name[vi]=Thử thách KJS Kate 1 +Name[zh_CN]=Kate KJS 测试 1 +Comment=Test for the KJS Wrapper +Comment[bg]=Проба на обвивката на KJS +Comment[ca]=Test pel Wrapper de KJS +Comment[cs]=Test KJS wrapperu +Comment[da]=Test for KJS Wrapper +Comment[de]=Test für den KJS-Wrapper +Comment[el]=Δοκιμή για το KJS Wrapper +Comment[eo]=Testo por la KJS envolvaĵo +Comment[es]=Test para el ajuste KJS +Comment[et]=KJS Wrapperi test +Comment[eu]=KJS Wrapper-erako testua +Comment[fa]=آزمون برای سطربندی KJS +Comment[fi]=KJS Wrapperin testi +Comment[fr]=Test pour l'interface avec KJS +Comment[fy]=Test foar de KJS Wrapper +Comment[ga]=Tástáil an Rapar KJS +Comment[gl]=Proba para o Wrapper de KJS +Comment[he]=ניסיון עבור המעטפת KJS +Comment[hi]=केजेएस व्रेपर के लिए जाँच +Comment[hr]=Provjera za KJS omotač +Comment[hu]=Tesztprogram a KJS felülethez +Comment[is]=Prufa fyrir KJS eininguna +Comment[it]=Test per il wrapper KJS +Comment[ja]=KJS ラッパーのテスト +Comment[ka]=KJS Wrapper-ის შემოწმება +Comment[kk]=KJS Wrapper сынағы +Comment[km]=សាកល្បងសម្រាប់កម្មវិធីរុំ KJS +Comment[lt]=Bandymas KJS įdėklui +Comment[mk]=Тест за обвивката KJS +Comment[ms]=Ujian untuk Pembungkus KJS +Comment[nb]=Test for KJS-omslaget +Comment[nds]=Test för den KJS-Wrapper +Comment[ne]=केजेएस आवरणका लागि परीक्षण +Comment[nl]=Test voor de KJS Wrapper +Comment[nn]=Test for KJS-omslaget +Comment[pl]=Test obsługi KJS +Comment[pt]=Teste para o 'Wrapper' de KJS +Comment[pt_BR]=Teste para o Wrapper do KJS +Comment[ru]=Тест KJS Wrapper +Comment[sk]=Test pre obálku KJS +Comment[sl]=Preizkus za KJS Wrapper +Comment[sr]=Тест за KJS омотач +Comment[sr@Latn]=Test za KJS omotač +Comment[sv]=Test för KJS-omslutande program +Comment[ta]=KJS சுற்றுத்தாளுக்கான சோதனை +Comment[tr]=KJS Wrapper Testi +Comment[uk]=Тест для KJS Wrapper +Comment[vi]=Thử thách cho bộ bao bọc KJS +Comment[zh_CN]=KJS Wrapper 的测试 +Comment[zh_TW]=KJS 包裝測試 diff --git a/kate/kjswrapper/samples/katekjstest1.js b/kate/kjswrapper/samples/katekjstest1.js new file mode 100644 index 0000000..7e4e041 --- /dev/null +++ b/kate/kjswrapper/samples/katekjstest1.js @@ -0,0 +1,167 @@ +/**************************************************************** + First configuration page +****************************************************************/ +function Page1 (parentWidget) { + this.defaults=function() { + alert("Defaults has been called"); + } + this.apply=function() { + alert("Apply has been called"); + } + this.reset=function() { + alert("Reset defaults has been called"); + } + box=new QVBox(parentWidget); + this.button1=new QPushButton(box); + this.button1.text="P1 Button 1"; + this.button1.show(); + this.button2=new QPushButton(box); + this.button2.text="P1 Button 2"; + this.button2.show(); + box.show(); +} +Page1.name="Page1" +Page1.fullName="Test1/Page1"; + +/**************************************************************** + Second configuration page +****************************************************************/ + +function Page2 (parentWidget) { + box=new QVBox(parentWidget); + this.button1=new QPushButton(box); + this.button1.text="P2 Button 1"; + this.button1.show(); + this.button2=new QPushButton(box); + this.button2.text="P2Button 2"; + this.button2.show(); + box.show(); +} +Page2.name="Page2"; +Page2.fullName="Test1/Page2"; + +/**************************************************************** + Third configuration page +****************************************************************/ + +function Page3 (parentWidget) { + box=new QVBox(parentWidget); + this.button1=new QPushButton(box); + this.button1.text="P3 Button 1"; + this.button1.show(); + this.button2=new QPushButton(box); + this.button2.text="P3 Button 2"; + this.button2.show(); + box.show(); +} +Page3.name="Page3"; +Page3.fullName="Test1/Page3"; + + +/**************************************************************** + First toolview +****************************************************************/ + +function MyToolView1 (mainwindow,parentwidget) { + parentwidget.caption="This is my first JS Toolview"; + parentwidget.icon=StdIcons.BarIcon("yes"); + + this.lv = new KListView( parentwidget ); + + this.lv.addColumn('Pix'); + this.lv.addColumn('One'); + this.lv.addColumn('Two'); + this.lv.addColumn('Three'); + + this.lv.insertItem( StdIcons.BarIcon("no"), 'Something', "Nothing", "Thing" ); + this.lv.insertItem( StdIcons.BarIcon("no"), 'Something', "Nothing", "Thing" ); + this.lv.insertItem( StdIcons.BarIcon("no"), 'Something', "Nothing", "Thing" ); + this.lv.insertItem( StdIcons.BarIcon("no"), 'Something', "Nothing", "Thing" ); + + this.changed=function() { + alert("Item changed"); + KATE.DocumentManager.activeDocument().insertText(0,0,"TEST"); + } + this.lv.connect(this.lv,'selectionChanged()',this,'changed'); + + + this.mw=mainwindow; + this.cleanup=function() { + alert("Cleanup MyToolView1"); + } + +} +MyToolView1.startPosition=KATE.ToolView.Right; +MyToolView1.name="myfirstjstoolview" + + +/**************************************************************** + Second toolview +****************************************************************/ + +function MyToolView2 (mainwindow,parentwidget) { + parentwidget.caption="This is my second JS Toolview"; + parentwidget.icon=StdIcons.BarIcon("no"); + + this.lb=new QListBox(parentwidget); + this.mainwindow=mainwindow; + this.cleanup=function() { + alert("Cleanup MyToolView2"); + } +} +MyToolView2.startPosition=KATE.ToolView.Left; +MyToolView2.name="mysecondjstoolview" + + + +/**************************************************************** + NewWindow callback +****************************************************************/ + +function newWindowCallBack(mainwindow) { + alert("New Window has been created"); +/* + anotherToolView = function (mainwindow,parentwidget) { + parentwidget.caption="This is my third JS Toolview"; + parentwidget.icon=StdIcons.BarIcon("kate"); + + this.lb=new QListBox(parentwidget); + this.mainwindow=mainwindow; + this.cleanup=function() { + alert("Cleanup MyToolView3"); + } + } + anotherToolView.startPosition=KATE.ToolView.Left; + anotherToolView.name="mythirdjsoolview" + mainwindow.createToolView(anotherToolView);*/ +} + +/**************************************************************** + WindowRemoved callback +****************************************************************/ +function windowRemovedCallBack(mainwindow) { + alert("Window has been removed"); +} + + + + + + +/**************************************************************** + Initialization +****************************************************************/ + +cpc=new Array(); +cpc.push(Page1); +cpc.push(Page2); +setConfigPages(cpc); +//setConfigPages(Page3); +//addConfigPage(Page3); + +tvc=new Array(); +tvc.push(MyToolView1); +tvc.push(MyToolView2); +setWindowConfiguration(tvc,newWindowCallBack,windowRemovedCallBack); +//setWindowConfiguration(MyToolView1,newWindowCallBack,windowRemovedCallBack); + |