Wt examples  3.2.1
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SourceView Class Reference

View class for source code. More...

#include <SourceView.h>

Inheritance diagram for SourceView:
Inheritance graph
[legend]

Public Member Functions

 SourceView (int fileNameRole, int contentRole, int filePathRole)
 Constructor.
virtual ~SourceView ()
 Destructor.
bool setIndex (const Wt::WModelIndex &index)
 Sets the model index.
virtual Wt::WWidgetrenderView ()
 Returns the widget that renders the view.
- Public Member Functions inherited from Wt::WViewWidget
 WViewWidget (WContainerWidget *parent=0)
void update ()
virtual void load ()
virtual void refresh ()
 WWebWidget (WContainerWidget *parent=0)
virtual void setPositionScheme (PositionScheme scheme)
virtual PositionScheme positionScheme () const
virtual void setOffsets (const WLength &offset, WFlags< Side > sides=All)
virtual WLength offset (Side s) const
virtual void resize (const WLength &width, const WLength &height)
virtual WLength width () const
virtual WLength height () const
virtual void setMinimumSize (const WLength &width, const WLength &height)
virtual WLength minimumWidth () const
virtual WLength minimumHeight () const
virtual void setMaximumSize (const WLength &width, const WLength &height)
virtual WLength maximumWidth () const
virtual WLength maximumHeight () const
virtual void setLineHeight (const WLength &height)
virtual WLength lineHeight () const
virtual void setFloatSide (Side s)
virtual Side floatSide () const
virtual void setClearSides (WFlags< Side > sides)
virtual WFlags< SideclearSides () const
virtual void setMargin (const WLength &margin, WFlags< Side > sides=All)
virtual WLength margin (Side side) const
virtual void setHiddenKeepsGeometry (bool enabled)
virtual bool hiddenKeepsGeometry () const
virtual void setHidden (bool hidden, const WAnimation &animation=WAnimation())
virtual bool isHidden () const
virtual bool isVisible () const
virtual void setDisabled (bool disabled)
virtual bool isDisabled () const
virtual bool isEnabled () const
virtual void setPopup (bool popup)
virtual bool isPopup () const
virtual void setInline (bool isInline)
virtual bool isInline () const
virtual void setDecorationStyle (const WCssDecorationStyle &style)
virtual WCssDecorationStyledecorationStyle ()
virtual void setStyleClass (const WString &styleClass)
virtual WString styleClass () const
virtual void addStyleClass (const WString &styleClass, bool force=false)
virtual void removeStyleClass (const WString &styleClass, bool force=false)
virtual void setVerticalAlignment (AlignmentFlag alignment, const WLength &length=WLength())
virtual AlignmentFlag verticalAlignment () const
virtual WLength verticalAlignmentLength () const
virtual void setToolTip (const WString &text, TextFormat textFormat=PlainText)
virtual WString toolTip () const
virtual void setAttributeValue (const std::string &name, const WString &value)
virtual WString attributeValue (const std::string &name) const
virtual void setJavaScriptMember (const std::string &name, const std::string &value)
virtual std::string javaScriptMember (const std::string &name) const
virtual void callJavaScriptMember (const std::string &name, const std::string &args)
virtual bool loaded () const
virtual void setTabIndex (int index)
virtual int tabIndex () const
virtual void setId (const std::string &id)
virtual WWidgetfind (const std::string &name)
virtual void setSelectable (bool selectable)
virtual void doJavaScript (const std::string &javascript)
virtual const std::string id () const
void setLoadLaterWhenInvisible (bool)
const std::vector< WWidget * > & children () const
SignalchildrenChanged ()
bool isRendered () const
virtual ~WWidget ()
WWidgetparent () const
void setWidth (const WLength &width)
void setHeight (const WLength &height)
virtual void positionAt (const WWidget *widget, Orientation orientation=Vertical)
std::string jsRef () const
virtual void acceptDrops (const std::string &mimeType, const WString &hoverStyleClass=WString())
virtual void stopAcceptDrops (const std::string &mimeType)
virtual void htmlText (std::ostream &out)
void hide ()
void animateHide (const WAnimation &animation)
void show ()
void animateShow (const WAnimation &animation)
void enable ()
void disable ()
bool layoutSizeAware () const
 WObject (WObject *parent=0)
virtual ~WObject ()
void setObjectName (const std::string &name)
virtual std::string objectName () const
void resetLearnedSlots ()
void resetLearnedSlot (void(T::*method)())
WStatelessSlot * implementStateless (void(T::*method)())
WStatelessSlot * implementStateless (void(T::*method)(), void(T::*undoMethod)())
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
void addChild (WObject *child)

Private Member Functions

std::string imageExtension (const std::string &fileName)

Private Attributes

Wt::WModelIndex index_
 The index that is currently displayed.
int fileNameRole_
 The role that is currently displayed.
int contentRole_
int filePathRole_
Wt::WMemoryResourceimageResource_

Additional Inherited Members

- Public Types inherited from Wt::WViewWidget
typedef void(WObject::* Method )()
- Static Public Member Functions inherited from Wt::WViewWidget
static WString escapeText (const WString &text, bool newlinesToo=false)
static std::string & escapeText (std::string &text, bool newlinestoo=false)
static bool removeScript (WString &text)
static std::string jsStringLiteral (const std::string &v, char delimiter= '\'')
- Protected Member Functions inherited from Wt::WViewWidget
virtual void propagateSetEnabled (bool enabled)
virtual void enableAjax ()
virtual void removeChild (WObject *child)
void setLayoutSizeAware (bool sizeAware)
virtual void layoutSizeChanged (int width, int height)
 WWidget (WContainerWidget *parent=0)
virtual void dropEvent (WDropEvent dropEvent)
virtual int boxPadding (Orientation orientation) const
virtual int boxBorder (Orientation orientation) const
virtual WStatelessSlot * getStateless (Method method)
- Static Protected Member Functions inherited from Wt::WViewWidget
static WObjectsender ()

Detailed Description

View class for source code.

A view class is used so that no server-side memory is used while displaying a potentially large file.

Definition at line 26 of file SourceView.h.

Constructor & Destructor Documentation

SourceView::SourceView ( int  fileNameRole,
int  contentRole,
int  filePathRole 
)

Constructor.

The fileNameRole will be used to retrieve data from a file to be displayed. If no data is set for this role, then contentRole should hold the data as a string.

Definition at line 20 of file SourceView.C.

: fileNameRole_(fileNameRole),
contentRole_(contentRole),
filePathRole_(filePathRole),
{}
SourceView::~SourceView ( )
virtual

Destructor.

Definition at line 27 of file SourceView.C.

{ }

Member Function Documentation

std::string SourceView::imageExtension ( const std::string &  fileName)
private

Definition at line 204 of file SourceView.C.

{
static const char *imageExtensions[] = {
".png", ".gif", ".jpg", "jpeg", ".ico", 0
};
fs::path p(fileName);
std::string extension = fs::extension(p);
for (const char **s = imageExtensions; *s != 0; ++s)
if (*s == extension)
return extension.substr(1);
return std::string();
}
WWidget * SourceView::renderView ( )
virtual

Returns the widget that renders the view.

Returns he view contents: renders the file to a WText widget. WViewWidget deletes this widget after every rendering step.

Implements Wt::WViewWidget.

Definition at line 95 of file SourceView.C.

{
if (!index_.isValid()) {
// no content
WText *result = new WText();
result->setInline(false);
return result;
}
/*
* read the contents, from string or file name
*/
boost::any contentsData = index_.data(contentRole_);
std::string content;
if (!contentsData.empty())
content = boost::any_cast<const std::string&>(contentsData);
boost::any fileNameData = index_.data(fileNameRole_);
std::string fileName =
boost::any_cast<const std::string&>(fileNameData);
boost::any filePathData = index_.data(filePathRole_);
std::string filePath;
if (!filePathData.empty())
filePath = boost::any_cast<const std::string&>(filePathData);
/*
* determine source language, for source highlight
*/
std::string lang = getLanguageFromFileExtension(fileName);
if (content != "" && content.substr(0, 100).find("-*- C++ -*-")
!= std::string::npos)
lang = "cpp";
std::string outputFileName;
if (lang != "") {
std::string inputFileName;
if (!filePathData.empty())
inputFileName = filePath;
else {
inputFileName = tempFileName();
std::ofstream out(inputFileName.c_str(),
std::ios::out | std::ios::binary);
out.write(content.c_str(), (std::streamsize)content.length());
out.close();
}
outputFileName = tempFileName();
std::string sourceHighlightCommand = "source-highlight ";
sourceHighlightCommand += "--src-lang=" + lang + " ";
sourceHighlightCommand += "--out-format=xhtml ";
sourceHighlightCommand += "--input=" + inputFileName + " ";
sourceHighlightCommand += "--output=" + outputFileName + " ";
std::cerr << sourceHighlightCommand << std::endl;
bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
if (sourceHighlightOk)
content = readFileToString(outputFileName);
else {
content = readFileToString(inputFileName);
lang = "";
}
unlink(outputFileName.c_str());
if (filePathData.empty())
unlink(inputFileName.c_str());
}
if (content == "")
// do not load binary files, we would need to perform proper UTF-8
// transcoding to display them
if (!boost::iends_with(fileName, ".jar")
&& !boost::iends_with(fileName, ".war")
&& !boost::iends_with(fileName, ".class"))
content = readFileToString(fileName);
WWidget *result = 0;
if (!imageExtension(fileName).empty()) {
WImage *image = new WImage();
imageResource_->setMimeType("mime/" + imageExtension(fileName));
imageResource_->setData((const unsigned char*)content.data(),
(int)content.length());
result = image;
} else if (lang != "") {
WText *text = new WText();
text->setTextFormat(XHTMLUnsafeText);
text->setText(WString::fromUTF8(content));
result = text;
} else {
WText *text = new WText();
text->setTextFormat(PlainText);
text->setText(WString::fromUTF8(content));
result = text;
}
result->setInline(false);
WApplication::instance()
->doJavaScript(result->jsRef() + ".parentNode.scrollTop = 0;");
return result;
}
bool SourceView::setIndex ( const Wt::WModelIndex index)

Sets the model index.

Returns true whether the view will be rerendered. The view will only be rerendered if the index contains new data.

Definition at line 30 of file SourceView.C.

{
if (index != index_ && index.isValid()) {
std::string fp = index.data(filePathRole_).empty() ? std::string()
: boost::any_cast<std::string>(index.data(filePathRole_));
if (!index.data(contentRole_).empty()
|| (!fp.empty() && !fs::is_directory(fp))) {
index_ = index;
update();
return true;
}
}
return false;
}

Member Data Documentation

int SourceView::contentRole_
private

Definition at line 61 of file SourceView.h.

int SourceView::fileNameRole_
private

The role that is currently displayed.

Definition at line 60 of file SourceView.h.

int SourceView::filePathRole_
private

Definition at line 62 of file SourceView.h.

Wt::WMemoryResource* SourceView::imageResource_
private

Definition at line 64 of file SourceView.h.

Wt::WModelIndex SourceView::index_
private

The index that is currently displayed.

Definition at line 57 of file SourceView.h.


The documentation for this class was generated from the following files:

Generated on Wed Oct 22 2014 for the C++ Web Toolkit (Wt) by doxygen 1.8.1.2