-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWAnchor.h
More file actions
235 lines (206 loc) · 6.54 KB
/
WAnchor.h
File metadata and controls
235 lines (206 loc) · 6.54 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef WANCHOR_H_
#define WANCHOR_H_
#include <Wt/WContainerWidget.h>
#include <Wt/WJavaScript.h>
#include <Wt/WImage.h>
#include <Wt/WLink.h>
namespace Wt {
namespace Impl {
class AreaWidget;
}
/*! \class WAnchor Wt/WAnchor.h Wt/WAnchor.h
* \brief A widget that represents an HTML anchor (to link to other documents).
*
* Use an anchor to link to another web page, document, internal
* application path or a resource (which specifies
* application-dependent content that may be generated by your
* application on demand). The anchor may contain a label text, an
* image, or any other widget (as it inherits from
* WContainerWidget).
*
* If you link to a document or external url, and do not want the
* application to terminate when the user follows the anchor, you must
* use \link WLink.setTarget(LinkTarget)
* link.setTarget(LinkTarget::NewWindow)\endlink. Even for non-HTML
* documents, this may be important since pending Ajax requests are
* cancelled if documents are not served within the browser window in
* certain browsers.
*
* \if cpp
* Usage example:
* \code
* WAnchor *a;
*
* // Create an anchor that links to a URL
* a = addWidget(std::make_unique<Wt::WAnchor>("https://www.webtoolkit.eu/", "Wt web toolkit"));
*
* // Create an anchor that links to an internal path
* a = addWidget(std::make_unique<Wt::WAnchor>(
* WLink(LinkType::InternalPath, "/docs/" + myDocName()), "Doc"));
* // and listen to the corresponding change in internal path
* WApplication::instance()->internalPathChanged().connect(this, &DocsListWidget::onInternalPathChange);
*
* // Create an anchor that links to a resource
* WResource *r = addChild(std::make_unique<PdfResource>()); // serializes to a PDF file.
* a = addWidget(std::make_unique<Wt::WAnchor>(r, "PDF version"));
* \endcode
* \endif
*
* %WAnchor is an \link WWidget::setInline(bool) inline \endlink widget.
*
* <h3>CSS</h3>
*
* The widget corresponds to the HTML <tt><a></tt> tag and does
* not provide styling. It can be styled using inline or external CSS
* as appropriate.
*/
class WT_API WAnchor : public WContainerWidget
{
public:
/*! \brief Creates an anchor.
*/
WAnchor();
/*! \brief Creates an anchor for the given link.
*
* The \p link may point to a URL, a dynamic resource, or an
* internal path.
*
* \sa setLink()
*/
WAnchor(const WLink& link);
/*! \brief Creates an anchor for the given link with a text.
*
* The \p link may point to a URL, a dynamic resource, or an
* internal path.
*
* \sa setLink(), setText()
*/
WAnchor(const WLink& link, const WString& text);
/*! \brief Creates an anchor for the given link with an image.
*
* \if cpp
* Ownership of the image is transferred to the anchor.
* \endif
*
* \sa setLink(), setImage()
*/
WAnchor(const WLink& link, std::unique_ptr<WImage> image);
/*! \brief Sets the link.
*
* The link may hold a URL, a resource, or an internal path.
*
* When the link points to a \link LinkType::Resource resource\endlink,
* the contents of the link may be generated by your application on
* demand.
*
* When the link points to an \link LinkType::InternalPath internal
* path\endlink, activating the anchor will change the \link
* WApplication::internalPath() application's internal path\endlink
* or open a new session with the given path as \link
* WEnvironment::internalPath() initial path\endlink). This is the
* easiest way to let the application participate in browser
* history, and generate URLs that are bookmarkable and search
* engine friendly.
*/
void setLink(const WLink& link);
/*! \brief Returns the link.
*
* \sa setLink()
*/
const WLink& link() const { return linkState_.link; }
/*! \brief Sets the label text
*
* If no text was previously set, a new WText widget is added using
* addWidget().
*/
void setText(const WString& text);
/*! \brief Returns the label text.
*
* Returns an empty string if no label was set.
*
* \sa setText()
*/
const WString& text() const;
/*! \brief Configures text word wrapping.
*
* When \p wordWrap is \c true, the text set with setText() may be
* broken up over multiple lines. When \p wordWrap is \c false, the
* text will displayed on a single line, unless the text contains
* <tt><br /></tt> tags or other block-level tags.
*
* The default value is \c true.
*
* \sa wordWrap()
*/
void setWordWrap(bool wordWrap);
/*! \brief Configures the text format.
*
* The default text format is TextFormat::XHTML.
*
* \sa WText::setTextFormat()
*/
void setTextFormat(TextFormat format);
/*! \brief Returns the text format.
*
* \sa setTextFormat()
*/
TextFormat textFormat() const;
/*! \brief Returns whether the widget may break lines.
*
* \sa setWordWrap(bool)
*/
bool wordWrap() const;
/*! \brief Sets an image.
*
* If an image was previously set, it is deleted. The \p image
* is added using addWidget().
*/
void setImage(std::unique_ptr<WImage> image);
/*! \brief Returns the image.
*
* Returns \c 0 if no image is set.
*
* \sa setImage()
*/
WImage *image() const { return image_.get(); }
virtual bool canReceiveFocus() const override;
virtual int tabIndex() const override;
virtual bool setFirstFocus() override;
private:
static const int BIT_LINK_CHANGED = 0;
static const int BIT_TARGET_CHANGED = 1;
struct WT_API LinkState {
LinkState();
~LinkState();
WLink link;
JSlot *clickJS;
};
LinkState linkState_;
observing_ptr<WText> text_;
observing_ptr<WImage> image_;
std::bitset<2> flags_;
void resourceChanged();
static bool renderHRef(WInteractWidget *widget,
LinkState& linkState, DomElement& element);
static void renderHTarget(LinkState& linkState, DomElement& element,
bool all);
static void renderUrlResolution(WWidget *widget, DomElement& element,
bool all);
protected:
virtual void updateDom(DomElement& element, bool all) override;
virtual DomElementType domElementType() const override;
virtual void propagateRenderOk(bool deep) override;
virtual void propagateSetEnabled(bool enabled) override;
virtual void enableAjax() override;
friend class WAbstractArea;
friend class WPushButton;
friend class Impl::AreaWidget;
};
}
#endif // WANCHOR_H_