/src/wt/src/Wt/WInPlaceEdit.h
Line | Count | Source |
1 | | // This may look like C code, but it's really -*- C++ -*- |
2 | | /* |
3 | | * Copyright (C) 2008 Emweb bv, Herent, Belgium. |
4 | | * |
5 | | * See the LICENSE file for terms of use. |
6 | | */ |
7 | | #ifndef WINPLACE_EDIT_H_ |
8 | | #define WINPLACE_EDIT_H_ |
9 | | |
10 | | #include <Wt/WCompositeWidget.h> |
11 | | |
12 | | namespace Wt { |
13 | | |
14 | | class WText; |
15 | | class WLineEdit; |
16 | | class WPushButton; |
17 | | |
18 | | /*! \class WInPlaceEdit Wt/WInPlaceEdit.h Wt/WInPlaceEdit.h |
19 | | * \brief A widget that provides in-place-editable text. |
20 | | * |
21 | | * The %WInPlaceEdit provides a text that may be edited in place by |
22 | | * the user by clicking on it. When clicked, the text turns into a |
23 | | * line edit, with optionally a save and cancel button (see |
24 | | * setButtonsEnabled()). |
25 | | * |
26 | | * When the user saves the edit, the valueChanged() signal is emitted. |
27 | | * |
28 | | * Usage example: |
29 | | * \if cpp |
30 | | * \code |
31 | | * auto w = std::make_unique<Wt::WContainerWidget>(); |
32 | | * w->addWidget(std::make_unique<Wt::WText>("Name: ")); |
33 | | * w->addWidget(std::make_unique<Wt::WInPlaceEdit>("Bob Smith")); |
34 | | * edit->setStyleClass("inplace"); |
35 | | * \endcode |
36 | | * \elseif java |
37 | | * \code |
38 | | * WContainerWidget w = new WContainerWidget(); |
39 | | * new WText("Name: ", w); |
40 | | * WInPlaceEdit edit = new WInPlaceEdit("Bob Smith", w); |
41 | | * edit.setStyleClass("inplace"); |
42 | | * \endcode |
43 | | * \endif |
44 | | * |
45 | | * This code will produce an edit that looks like: |
46 | | * \image html WInPlaceEdit-1.png "WInPlaceEdit text mode" |
47 | | * When the text is clicked, the edit will expand to become: |
48 | | * \image html WInPlaceEdit-2.png "WInPlaceEdit edit mode" |
49 | | * |
50 | | * <h3>CSS</h3> |
51 | | * |
52 | | * A WInPlaceEdit widget renders as a <tt><span></tt> containing |
53 | | * a WText, a WLineEdit and optional buttons (WPushButton). All these |
54 | | * widgets may be styled as such. It does not provide style information. |
55 | | * |
56 | | * In particular, you may want to provide a visual indication that the text |
57 | | * is editable e.g. using a hover effect: |
58 | | * |
59 | | * CSS stylesheet: |
60 | | * \code |
61 | | * .inplace span:hover { |
62 | | * background-color: gray; |
63 | | * } |
64 | | * \endcode |
65 | | */ |
66 | | class WT_API WInPlaceEdit : public WCompositeWidget |
67 | | { |
68 | | public: |
69 | | /*! \brief Creates an in-place edit. |
70 | | */ |
71 | | WInPlaceEdit(); |
72 | | |
73 | | /*! \brief Creates an in-place edit with the given text. |
74 | | */ |
75 | | WInPlaceEdit(const WString& text); |
76 | | |
77 | | /*! \brief Creates an in-place edit with the given text. |
78 | | * |
79 | | * The first parameter configures whether buttons are available in edit |
80 | | * mode. |
81 | | * |
82 | | * \sa setButtonsEnabled() |
83 | | */ |
84 | | WInPlaceEdit(bool buttons, const WString& text); |
85 | | |
86 | | /*! \brief Returns the current value. |
87 | | * |
88 | | * \sa setText() |
89 | | */ |
90 | | const WString& text() const; |
91 | | |
92 | | /*! \brief Sets the current value. |
93 | | * |
94 | | * \sa text() |
95 | | */ |
96 | | void setText(const WString& text); |
97 | | |
98 | | /*! \brief Sets the placeholder text. |
99 | | * |
100 | | * This sets the text that is shown when the field is empty. |
101 | | */ |
102 | | void setPlaceholderText(const WString& placeholder); |
103 | | |
104 | | /*! \brief Returns the placeholder text. |
105 | | * |
106 | | * \sa setPlaceholderText() |
107 | | */ |
108 | | const WString& placeholderText() const; |
109 | | |
110 | | /*! \brief Returns the line edit. |
111 | | * |
112 | | * You may use this for example to set a validator on the line edit. |
113 | | */ |
114 | 0 | WLineEdit *lineEdit() const { return edit_; } |
115 | | |
116 | | /*! \brief Returns the WText widget that renders the current string. |
117 | | * |
118 | | * You may use this for example to set the text format of the displayed |
119 | | * string. |
120 | | */ |
121 | 0 | WText *textWidget() const { return text_; } |
122 | | |
123 | | /*! \brief Returns the save button. |
124 | | * |
125 | | * This method returns \c 0 if the buttons were disabled. |
126 | | * |
127 | | * \sa cancelButton(), setButtonsEnabled() |
128 | | */ |
129 | 0 | WPushButton *saveButton() const { return save_; } |
130 | | |
131 | | /*! \brief Returns the cancel button. |
132 | | * |
133 | | * This method returns \c 0 if the buttons were disabled. |
134 | | * |
135 | | * \sa saveButton(), setButtonsEnabled() |
136 | | */ |
137 | 0 | WPushButton *cancelButton() const { return cancel_; } |
138 | | |
139 | | /*! \brief %Signal emitted when the value has been changed. |
140 | | * |
141 | | * The signal argument provides the new value. |
142 | | */ |
143 | 0 | Signal<WString>& valueChanged() { return valueChanged_; } |
144 | | |
145 | | /*! \brief Displays the Save and 'Cancel' button during editing |
146 | | * |
147 | | * By default, the Save and Cancel buttons are shown. Call this |
148 | | * function with \p enabled = \c false to only show a line edit. |
149 | | * |
150 | | * In this mode, the enter key or any event that causes focus to be |
151 | | * lost saves the value while the escape key cancels the editing. |
152 | | */ |
153 | | void setButtonsEnabled(bool enabled = true); |
154 | | |
155 | | protected: |
156 | | virtual void render(WFlags<RenderFlag> flags) override; |
157 | | |
158 | | private: |
159 | | void create(); |
160 | | void save(); |
161 | | void cancel(); |
162 | | |
163 | | private: |
164 | | Signal<WString> valueChanged_; |
165 | | WContainerWidget *impl_, *editing_, *buttons_; |
166 | | WText *text_; |
167 | | WLineEdit *edit_; |
168 | | WPushButton *save_, *cancel_; |
169 | | WString placeholderText_; |
170 | | Wt::Signals::connection c2_; |
171 | | bool empty_; |
172 | | }; |
173 | | |
174 | | } |
175 | | |
176 | | #endif // WINPLACE_EDIT_H_ |