________________________________
>From: Wim Dumon <w...@emweb.be>
>To: witty-interest@lists.sourceforge.net
>Sent: Tuesday, September 4, 2012 8:34 PM
>Subject: Re: [Wt-interest] [Wt Interest] Using CodeMirror with Wt
>This is for JWt. The translation to C++ is left as an exercise to the
>reader. And maybe if someone contributes a nice C++ widget we may add
>it to Wt (and JWt)?
Hi Rob, Wim,
Pasted below is the beginnings of the code sent by Wim that I translated
into C++.
I was having trouble with it, just as I was having trouble with my own
initial attempt, until I realised I had not loaded the CodeMirror CSS.
Once that was done, it is working fine. No additional functionality has
been added, but I intend to do that, now that the core functionality seems
fine. I have pasted the code below along with the test class.
Regards,
Neil
/*
*
=====================================================================================
*
* Filename: WCodeMirror.h
*
* Description:
*
* Version: 1.0
* Created: Thursday 06 September 2012 01:05:33 IST
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
*
=====================================================================================
*/
/*
import eu.webtoolkit.jwt.WApplication;
import eu.webtoolkit.jwt.WContainerWidget;
import eu.webtoolkit.jwt.WTextArea;
public class CodeMirrorTextArea extends WContainerWidget {
private WTextArea textArea;
public CodeMirrorTextArea(WContainerWidget parent) {
super(parent);
textArea = new WTextArea(this);
WApplication app = WApplication.getInstance();
app.require(app.resolveRelativeUrl("codemirror-2.32/lib/codemirror.js"));
app.require(app.resolveRelativeUrl("codemirror-2.32/mode/groovy/groovy.js"));
//TODO:
//We save the editor state to the text area on each key stroke,
//it appears to be not a performance issue,
//however it might very well become one when editing larger fragments
of code.
//A better solution would be to save this state to the text area only
when
//the form is submitted, currently this is not yet possible in Wt???.
String js =
"var e = " + textArea.getJsRef() + ";" +
"var cm = CodeMirror.fromTextArea(e, {" +
" onKeyEvent : function (editor, event) {" +
" editor.save();" +
" }," +
" lineNumbers: true" +
" });" +
"var self = " + getJsRef() + ";" +
"self.cm = cm;";
this.doJavaScript(js);
}
public CodeMirrorTextArea() {
this(null);
}
public void setText(String text) {
textArea.setText(text);
}
public String getText() {
return textArea.getText();
}
public void setMarker(int line, String htmlMarker) {
String js =
"var self = " + getJsRef() + ";" +
"self.cm.setMarker(" + line + ", " + jsStringLiteral(htmlMarker +
"%N%") + ");";
this.doJavaScript(js);
}
public void clearMarker(int line) {
String js =
"var self = " + getJsRef() + ";" +
"self.cm.clearMarker(" + line + ");";
this.doJavaScript(js);
}
}
*/
#ifndef WT_WCodeMirrorTextArea
#define WT_WCodeMirrorTextArea
#include <Wt/WApplication>
#include <Wt/WTextArea>
#include <Wt/WContainerWidget>
#include <string>
#include <sstream>
namespace Wt {
class WCodeMirrorTextArea : public WContainerWidget
{
public:
WTextArea * textArea_;
WCodeMirrorTextArea (WContainerWidget * parent)
: WContainerWidget (parent)
{
textArea_ = new WTextArea(this);
textArea_->setText("var v1;\nfunction f (p1, p2, p3) {\n // does nothing
\n};\n");
WApplication * app = WApplication:: instance();
//app.require(app.resolveRelativeUrl("codemirror-2.32/lib/codemirror.js"));
//app.require(app.resolveRelativeUrl("codemirror-2.32/mode/groovy/groovy.js"));
//true
//
app->useStyleSheet(app->resolveRelativeUrl("CodeMirror-2.33/lib/codemirror.css"));
app->require(app->resolveRelativeUrl("CodeMirror-2.33/lib/codemirror.js"));
app->require(app->resolveRelativeUrl("CodeMirror-2.33/mode/javascript/javascript.js"));
app->useStyleSheet(app->resolveRelativeUrl("CodeMirror-2.33/doc/docs.css"));
//TODO:
//We save the editor state to the text area on each key stroke,
//it appears to be not a performance issue,
//however it might very well become one when editing larger fragments of
code.
//A better solution would be to save this state to the text area only when
//the form is submitted, currently this is not yet possible in Wt???.
using std::string;
string js =
"var e = " + textArea_->jsRef() + ";" +
"var cm = CodeMirror.fromTextArea(e, {" +
" onKeyEvent : function (editor, event) {" +
" editor.save();" +
" }," +
" lineNumbers: true, " +
" value: \"var v1; function f2 (p1, p2, p3) { var v2; }\" " +
" });" +
"var self = " + this->jsRef() + ";" +
"self.cm = cm;";
this->doJavaScript(js);
}
/*
public CodeMirrorTextArea() {
this(null);
}
*/
void setText(std::string text)
{
textArea_->setText(text);
}
std::string getText()
{
return textArea_-> text().toUTF8();
}
void setMarker(int line, std::string htmlMarker)
{
std::stringstream js ;
js << "var self = " << jsRef() << ";"
<< "self.cm.setMarker("
<< line << ", " << jsStringLiteral(htmlMarker + "%N%")
<< ");";
this->doJavaScript(js.str());
}
void clearMarker(int line)
{
std::stringstream js ;
js << "var self = " << jsRef() << ";"
<< "self.cm.clearMarker(" << line << ");";
this->doJavaScript(js.str());
}
};
}
#endif /* WT_WCodeMirrorTextArea */
// ============== test main function: testCM.C =============
#include "WCodeMirror.h"
#include <iostream>
#include <string>
#include <Wt/WApplication>
class MyWtCodeMirror : public Wt::WApplication
{
public:
MyWtCodeMirror (const Wt::WEnvironment& env);
Wt::WCodeMirrorTextArea * cmta_;
};
MyWtCodeMirror::MyWtCodeMirror (const Wt::WEnvironment& env)
: Wt::WApplication (env), cmta_(new Wt::WCodeMirrorTextArea(this->root()))
{
std::cout << cmta_->getText() << std::endl;
}
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
/*
* You could read information from the environment to decide whether
* the user has permission to start a new application
*/
return new MyWtCodeMirror(env);
}
int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, &createApplication);
}
=======================================
And for completeness sake - compile instructions
simple_compiler/stubs/widgetset>g++ testCM.C -lwt -lwthttp
simple_compiler/stubs/widgetset>./a.out --http-address=127.0.0.1
--http-port=8081 --docroot .
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
witty-interest mailing list
witty-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/witty-interest