/usr/lib/Wt/examples/codeview/ObserverWidget.C is in witty-examples 3.1.10-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | /*
* Copyright (C) 2010 Emweb bvba, Heverlee, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "ObserverWidget.h"
#include <Wt/WText>
#include <boost/tuple/tuple.hpp>
using namespace Wt;
class BufferViewWidget : public WContainerWidget
{
public:
BufferViewWidget()
{
setStyleClass("viewer");
WApplication::instance()->require("prettify/prettify.min.js");
WApplication::instance()->useStyleSheet("prettify/prettify.css");
new WText("File: ", this);
bufferName_ = new WText(this);
bufferText_ = new WText(this);
bufferText_->setInline(false);
bufferText_->setStyleClass("prettyprint");
}
void setName(const WString& name) {
bufferName_->setText(name);
}
void setText(const WString& text) {
WApplication::instance()->doJavaScript
(bufferText_->jsRef() + ".innerHTML="
"'<pre class=\"prettyprint\">' + prettyPrintOne("
+ text.jsStringLiteral() + ", " + bufferText_->jsRef()
+ ") + '</pre>';");
}
private:
WText *bufferName_;
WText *bufferText_;
};
ObserverWidget::ObserverWidget(const std::string& id)
{
WApplication::instance()->enableUpdates(true);
session_ = CodeSession::addObserver
(id, boost::bind(&ObserverWidget::updateBuffer, this, _1, _2));
if (session_) {
std::vector<CodeSession::Buffer> buffers = session_->buffers();
for (unsigned i = 0; i < buffers.size(); ++i)
insertBuffer(buffers[i], i);
}
}
ObserverWidget::~ObserverWidget()
{
if (session_)
session_->removeObserver();
WApplication::instance()->enableUpdates(false);
}
void ObserverWidget::insertBuffer(const CodeSession::Buffer& buffer, int i)
{
BufferViewWidget *w = new BufferViewWidget();
w->setName(buffer.name);
w->setText(buffer.text);
insertWidget(i, w);
}
void ObserverWidget::updateBuffer(int buffer, CodeSession::BufferUpdate update)
{
switch (update) {
case CodeSession::Inserted:
insertBuffer(session_->buffer(buffer), buffer);
break;
case CodeSession::Deleted:
delete widget(buffer);
break;
case CodeSession::Changed:
{
BufferViewWidget *w = dynamic_cast<BufferViewWidget *>(widget(buffer));
w->setName(session_->buffer(buffer).name);
w->setText(session_->buffer(buffer).text);
}
}
WApplication::instance()->triggerUpdate();
}
|