Text provides a primary means for displaying information in the user interface. The WText class provides a simple way to add plain or markup text to the user interface.
The WText
widget displays text using an HTML <span>
or <div>
element (depending on whether it is inline
or not). It can display either XHTML formatted text or plain text.
The text contents is contained in a WString. This string class provides at the same time support for localization and internationalization:
In its most simple form, the WText
widget displays plain text
(escaping special characters as needed).
Of course, a WText
widget may also display XHTML formatted text.
#include <Wt/WText.h>
auto text
= std::make_unique<Wt::WText>("This is <b>XHTML</b> markup text. "
"It supports a safe subset of XHTML tags and "
"attributes, which have only decorative "
"functions.");
XHTML text that is not read from a localized strings interface (which is
considered inherently safe), is protected against unwanted side effects
from Cross-Site Scripting (XSS) attacks. The text of an XHTML-formatted
WText
is filtered using an XML parser and all malicious tags are
removed (unless this feature is explicitly by-passed by using the
XHTMLUnsafeText
text format).
This XHTML text contains JavaScript, wich is filtered by the XSS filter.
A warning is printed in the logs.
#include <Wt/WText.h>
auto text
= std::make_unique<Wt::WText>("<p>This XHTML text contains JavaScript, "
"wich is filtered by the XSS filter.</p>"
"<script>alert(\"XSS Attack!\");</script>"
"<p>A warning is printed in the logs.</p>");
If you want to display a text label associated with a form field then a WLabel is more suitable as it can be linked to the input field, relaying focus to it when clicked.
If you want to display an HTML fragment which contains widgets or other bound contents, then a WTemplate widget is probably what you are looking for.
The functionality of WText
is very basic. As this widget derives
- like many widgets - from WInteractWidget it may respond
to mouse events and also keyboard events if it can be given keyboard
focus. A few mouse events are demonstrated below.
#include <Wt/WContainerWidget.h>
#include <Wt/WText.h>
auto container = std::make_unique<Wt::WContainerWidget>();
// Create four text widgets.
Wt::WText *text1 =
container->addNew<Wt::WText>("This text reacts to <tt>clicked()</tt>");
text1->setStyleClass("reactive");
Wt::WText *text2 =
container->addNew<Wt::WText>("This text reacts to <tt>doubleClicked()</tt>");
text2->setStyleClass("reactive");
Wt::WText *text3 =
container->addNew<Wt::WText>("This text reacts to <tt>mouseWentOver()</tt>");
text3->setStyleClass("reactive");
Wt::WText *text4 =
container->addNew<Wt::WText>("This text reacts to <tt>mouseWentOut()</tt>");
text4->setStyleClass("reactive");
// Create an additional text control to show status messages.
Wt::WText *out = container->addNew<Wt::WText>();
// Assign a signal/slot mechanism to the text controls.
text1->clicked().connect([=] {
out->setText("<p>Text was clicked.</p>");
});
text2->doubleClicked().connect([=] {
out->setText("<p>Text was double clicked.</p>");
});
text3->mouseWentOver().connect([=] {
out->setText("<p>Mouse went over text.</p>");
});
text4->mouseWentOut().connect([=] {
out->setText("<p>Mouse went out text.</p>");
});
You may add a tooltip or deferred tooltip to WText
#include <Wt/WText.h>
auto text
= std::make_unique<Wt::WText>("Some text", Wt::TextFormat::Plain);
text->setToolTip("ToolTip", Wt::TextFormat::XHTML);
#include <Wt/WText.h>
#include <Wt/WString.h>
#include <Wt/WContainerWidget.h>
class Text : public Wt::WText
{
public:
Text() : WText(){}
Wt::WString calculateToolTip() const
{
return "Deferred tooltip";
}
virtual Wt::WString toolTip() const
{
return calculateToolTip();
}
};
auto text = std::make_unique<Text>();
text->setText("Text");
text->setDeferredToolTip(true);