Wt examples  3.7.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. More...
 
virtual ~SourceView ()
 Destructor. More...
 
bool setIndex (const Wt::WModelIndex &index)
 Sets the model index. More...
 
virtual Wt::WWidgetrenderView ()
 Returns the widget that renders the view. More...
 
- Public Member Functions inherited from Wt::WViewWidget
 WViewWidget ()
 
void update ()
 
virtual void load () override
 
virtual void render (WFlags< RenderFlag > flags) override
 
virtual void refresh () override
 
- Public Member Functions inherited from Wt::WWebWidget
 WWebWidget ()
 
virtual std::vector< WWidget *> children () const override
 
SignalchildrenChanged ()
 
virtual void setPositionScheme (PositionScheme scheme) override
 
virtual PositionScheme positionScheme () const override
 
virtual void setOffsets (const WLength &offset, WFlags< Side > sides=AllSides) override
 
virtual WLength offset (Side s) const override
 
virtual void resize (const WLength &width, const WLength &height) override
 
virtual WLength width () const override
 
virtual WLength height () const override
 
virtual void setMinimumSize (const WLength &width, const WLength &height) override
 
virtual WLength minimumWidth () const override
 
virtual WLength minimumHeight () const override
 
virtual void setMaximumSize (const WLength &width, const WLength &height) override
 
virtual WLength maximumWidth () const override
 
virtual WLength maximumHeight () const override
 
virtual void setLineHeight (const WLength &height) override
 
virtual WLength lineHeight () const override
 
virtual void setFloatSide (Side s) override
 
virtual Side floatSide () const override
 
virtual void setClearSides (WFlags< Side > sides) override
 
virtual WFlags< SideclearSides () const override
 
virtual void setMargin (const WLength &margin, WFlags< Side > sides=AllSides) override
 
virtual WLength margin (Side side) const override
 
virtual void setHiddenKeepsGeometry (bool enabled) override
 
virtual bool hiddenKeepsGeometry () const override
 
virtual void setHidden (bool hidden, const WAnimation &animation=WAnimation()) override
 
virtual bool isHidden () const override
 
virtual bool isVisible () const override
 
virtual void setDisabled (bool disabled) override
 
virtual bool isDisabled () const override
 
virtual bool isEnabled () const override
 
virtual void setPopup (bool popup) override
 
virtual bool isPopup () const override
 
virtual void setInline (bool isInline) override
 
virtual bool isInline () const override
 
virtual void setDecorationStyle (const WCssDecorationStyle &style) override
 
virtual WCssDecorationStyledecorationStyle () override
 
virtual void setStyleClass (const WString &styleClass) override
 
virtual WString styleClass () const override
 
virtual void addStyleClass (const WString &styleClass, bool force=false) override
 
virtual void removeStyleClass (const WString &styleClass, bool force=false) override
 
virtual bool hasStyleClass (const WString &styleClass) const override
 
virtual void setVerticalAlignment (AlignmentFlag alignment, const WLength &length=WLength()) override
 
virtual AlignmentFlag verticalAlignment () const override
 
virtual WLength verticalAlignmentLength () const override
 
virtual void setToolTip (const WString &text, TextFormat textFormat=TextFormat::Plain) override
 
virtual void setDeferredToolTip (bool enable, TextFormat textFormat=TextFormat::Plain) override
 
virtual WString toolTip () const override
 
virtual void setAttributeValue (const std::string &name, const WString &value) override
 
virtual WString attributeValue (const std::string &name) const override
 
virtual void setJavaScriptMember (const std::string &name, const std::string &value) override
 
virtual std::string javaScriptMember (const std::string &name) const override
 
virtual void callJavaScriptMember (const std::string &name, const std::string &args) override
 
virtual bool loaded () const override
 
virtual void setId (const std::string &id) override
 
virtual WWidgetfind (const std::string &name) override
 
virtual WWidgetfindById (const std::string &id) override
 
virtual void setSelectable (bool selectable) override
 
virtual void doJavaScript (const std::string &javascript) override
 
virtual const std::string id () const override
 
void setLoadLaterWhenInvisible (bool)
 
std::string htmlTagName () const
 
void setHtmlTagName (const std::string &tag)
 
virtual void setCanReceiveFocus (bool enabled) override
 
virtual bool canReceiveFocus () const override
 
virtual bool setFirstFocus () override
 
virtual void setFocus (bool focus) override
 
virtual bool hasFocus () const override
 
virtual void setTabIndex (int index) override
 
virtual int tabIndex () const override
 
EventSignalblurred ()
 
EventSignalfocussed ()
 
virtual bool scrollVisibilityEnabled () const final override
 
virtual void setScrollVisibilityEnabled (bool enabled) final override
 
virtual int scrollVisibilityMargin () const final override
 
virtual void setScrollVisibilityMargin (int margin) final override
 
virtual Signal< bool > & scrollVisibilityChanged () final override
 
virtual bool isScrollVisible () const final override
 
virtual void setThemeStyleEnabled (bool enabled) final override
 
virtual bool isThemeStyleEnabled () const final override
 
virtual void setObjectName (const std::string &name) override
 
- Public Member Functions inherited from Wt::WWidget
virtual ~WWidget ()
 
WWidgetparent () const
 
virtual std::unique_ptr< WWidgetremoveWidget (WWidget *widget)
 
std::unique_ptr< Widget > removeWidget (Widget *widget)
 
std::unique_ptr< WWidgetremoveFromParent ()
 
void setWidth (const WLength &width)
 
void setHeight (const WLength &height)
 
virtual void positionAt (const WWidget *widget, Orientation orientation=Orientation::Vertical)
 
virtual void toggleStyleClass (const WString &styleClass, bool add, bool force=false)
 
std::string jsRef () const
 
void setFocus ()
 
virtual void acceptDrops (const std::string &mimeType, const WString &hoverStyleClass=WString())
 
virtual void stopAcceptDrops (const std::string &mimeType)
 
virtual void htmlText (std::ostream &out)
 
bool isRendered () const
 
void hide ()
 
void animateHide (const WAnimation &animation)
 
void show ()
 
void animateShow (const WAnimation &animation)
 
void enable ()
 
void disable ()
 
bool layoutSizeAware () const
 
- Public Member Functions inherited from Wt::WObject
void addChild (std::unique_ptr< WObject > child)
 
Child * addChild (std::unique_ptr< Child > child)
 
std::unique_ptr< WObjectremoveChild (WObject *child)
 
std::unique_ptr< Child > removeChild (Child *child)
 
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)())
 
void isNotStateless ()
 
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
 
- Public Member Functions inherited from Wt::Core::observable
 observable () noexcept
 
virtual ~observable ()
 
auto bindSafe (void(C::*method)(Args...)) noexcept
 
auto bindSafe (void(C::*method)(Args...) const) const noexcept
 
auto bindSafe (const Function &function) noexcept
 

Private Member Functions

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

Private Attributes

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

Additional Inherited Members

- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 
- Static Public Member Functions inherited from Wt::WWebWidget
static std::string jsStringLiteral (const std::string &v, char delimiter='\'')
 
- Static Public Member Functions inherited from Wt::WWidget
static WString tr (const char *key)
 
- Protected Member Functions inherited from Wt::WWebWidget
virtual void propagateSetEnabled (bool enabled) override
 
virtual void enableAjax () override
 
virtual WStatelessSlot * getStateless (Method method) override
 
- Protected Member Functions inherited from Wt::WWidget
void setLayoutSizeAware (bool sizeAware)
 
virtual void layoutSizeChanged (int width, int height)
 
 WWidget ()
 
virtual void dropEvent (WDropEvent dropEvent)
 
virtual int boxPadding (Orientation orientation) const
 
virtual int boxBorder (Orientation orientation) const
 
void scheduleRender (WFlags< RepaintFlag > flags=None)
 

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::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.

21  : fileNameRole_(fileNameRole),
22  contentRole_(contentRole),
23  filePathRole_(filePathRole),
25 {}
int contentRole_
Definition: SourceView.h:61
int fileNameRole_
The role that is currently displayed.
Definition: SourceView.h:60
int filePathRole_
Definition: SourceView.h:62
Wt::WMemoryResource * imageResource_
Definition: SourceView.h:64

◆ ~SourceView()

SourceView::~SourceView ( )
virtual

Destructor.

Definition at line 27 of file SourceView.C.

28 { }

Member Function Documentation

◆ imageExtension()

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

Definition at line 204 of file SourceView.C.

205 {
206  static const char *imageExtensions[] = {
207  ".png", ".gif", ".jpg", "jpeg", ".ico", 0
208  };
209 
210  fs::path p(fileName);
211  std::string extension = fs::extension(p);
212 
213  for (const char **s = imageExtensions; *s != 0; ++s)
214  if (*s == extension)
215  return extension.substr(1);
216 
217  return std::string();
218 }

◆ renderView()

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.

96 {
97  if (!index_.isValid()) {
98  // no content
99  WText *result = new WText();
100  result->setInline(false);
101  return result;
102  }
103 
104  /*
105  * read the contents, from string or file name
106  */
107  boost::any contentsData = index_.data(contentRole_);
108  std::string content;
109  if (!contentsData.empty())
110  content = boost::any_cast<const std::string&>(contentsData);
111  boost::any fileNameData = index_.data(fileNameRole_);
112  std::string fileName =
113  boost::any_cast<const std::string&>(fileNameData);
114  boost::any filePathData = index_.data(filePathRole_);
115  std::string filePath;
116  if (!filePathData.empty())
117  filePath = boost::any_cast<const std::string&>(filePathData);
118 
119  /*
120  * determine source language, for source highlight
121  */
122  std::string lang = getLanguageFromFileExtension(fileName);
123  if (content != "" && content.substr(0, 100).find("-*- C++ -*-")
124  != std::string::npos)
125  lang = "cpp";
126 
127  std::string outputFileName;
128 
129  if (lang != "") {
130  std::string inputFileName;
131 
132  if (!filePathData.empty())
133  inputFileName = filePath;
134  else {
135  inputFileName = tempFileName();
136  std::ofstream out(inputFileName.c_str(),
137  std::ios::out | std::ios::binary);
138  out.write(content.c_str(), (std::streamsize)content.length());
139  out.close();
140  }
141 
142  outputFileName = tempFileName();
143 
144  std::string sourceHighlightCommand = "source-highlight ";
145  sourceHighlightCommand += "--src-lang=" + lang + " ";
146  sourceHighlightCommand += "--out-format=xhtml ";
147  sourceHighlightCommand += "--input=" + inputFileName + " ";
148  sourceHighlightCommand += "--output=" + outputFileName + " ";
149 
150  std::cerr << sourceHighlightCommand << std::endl;
151  bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
152 
153  if (sourceHighlightOk)
154  content = readFileToString(outputFileName);
155  else {
156  content = readFileToString(inputFileName);
157  lang = "";
158  }
159  unlink(outputFileName.c_str());
160 
161  if (filePathData.empty())
162  unlink(inputFileName.c_str());
163  }
164 
165  if (content == "")
166  // do not load binary files, we would need to perform proper UTF-8
167  // transcoding to display them
168  if (!boost::iends_with(fileName, ".jar")
169  && !boost::iends_with(fileName, ".war")
170  && !boost::iends_with(fileName, ".class"))
171  content = readFileToString(fileName);
172 
173  delete imageResource_;
174  imageResource_ = 0;
175 
176  WWidget *result = 0;
177 
178  if (!imageExtension(fileName).empty()) {
179  WImage *image = new WImage();
180  imageResource_ = new WMemoryResource(this);
181  imageResource_->setMimeType("mime/" + imageExtension(fileName));
182  imageResource_->setData((const unsigned char*)content.data(),
183  (int)content.length());
185  result = image;
186  } else if (lang != "") {
187  WText *text = new WText();
188  text->setTextFormat(XHTMLUnsafeText);
189  text->setText(WString::fromUTF8(content));
190  result = text;
191  } else {
192  WText *text = new WText();
193  text->setTextFormat(PlainText);
194  text->setText(WString::fromUTF8(content));
195  result = text;
196  }
197 
198  result->setInline(false);
199  WApplication::instance()
200  ->doJavaScript(result->jsRef() + ".parentNode.scrollTop = 0;");
201  return result;
202 }
bool isValid() const
std::string jsRef() const
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
int contentRole_
Definition: SourceView.h:61
int fileNameRole_
The role that is currently displayed.
Definition: SourceView.h:60
bool setText(const WString &text)
bool setTextFormat(TextFormat format)
int filePathRole_
Definition: SourceView.h:62
void setMimeType(const std::string &mimeType)
std::string readFileToString(const std::string &fileName)
Definition: SourceView.C:83
void setData(const std::vector< unsigned char > &data)
std::string imageExtension(const std::string &fileName)
Definition: SourceView.C:204
virtual void setInline(bool inlined)=0
void setImageLink(const WLink &link)
Wt::WMemoryResource * imageResource_
Definition: SourceView.h:64
std::string getLanguageFromFileExtension(const std::string &fileName)
Definition: SourceView.C:63
Wt::WModelIndex index_
The index that is currently displayed.
Definition: SourceView.h:57
virtual void setInline(bool isInline) override
std::string tempFileName()
Definition: SourceView.C:48

◆ setIndex()

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.

31 {
32  if (index != index_ && index.isValid()) {
33  std::string fp = index.data(filePathRole_).empty() ? std::string()
34  : boost::any_cast<std::string>(index.data(filePathRole_));
35 
36  if (!index.data(contentRole_).empty()
37  || (!fp.empty() && !fs::is_directory(fp))) {
38  index_ = index;
39  update();
40 
41  return true;
42  }
43  }
44 
45  return false;
46 }
bool isValid() const
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
int contentRole_
Definition: SourceView.h:61
int filePathRole_
Definition: SourceView.h:62
Wt::WModelIndex index_
The index that is currently displayed.
Definition: SourceView.h:57

Member Data Documentation

◆ contentRole_

int SourceView::contentRole_
private

Definition at line 61 of file SourceView.h.

◆ fileNameRole_

int SourceView::fileNameRole_
private

The role that is currently displayed.

Definition at line 60 of file SourceView.h.

◆ filePathRole_

int SourceView::filePathRole_
private

Definition at line 62 of file SourceView.h.

◆ imageResource_

Wt::WMemoryResource* SourceView::imageResource_
private

Definition at line 64 of file SourceView.h.

◆ index_

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 Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13