Coverage Report

Created: 2026-09-23 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wt/src/Wt/WRadioButton.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 WRADIOBUTTON_H_
8
#define WRADIOBUTTON_H_
9
10
#include <Wt/WAbstractToggleButton.h>
11
#include <Wt/WButtonGroup.h>
12
13
namespace Wt {
14
15
class WButtonGroup;
16
17
/*! \class WRadioButton Wt/WRadioButton.h Wt/WRadioButton.h
18
 *  \brief A user control that represents a radio button.
19
 *
20
 * Use a WButtonGroup to group together radio buttons that reflect
21
 * options that are mutually exclusive.
22
 *
23
 * Usage example:
24
 * \if cpp
25
 * \code
26
 * enum class Vote { Republican = 1, Democrat = 2, NoVote = 10 };
27
 *
28
 * // use a group box as widget container for 3 radio buttons, with a title
29
 * auto container = std::make_unique<Wt::WGroupBox>("USA elections vote");
30
 *
31
 * // use a button group to logically group the 3 options
32
 * auto group = std::make_shared<Wt::WButtonGroup>();
33
 *
34
 * Wt::WRadioButton *button;
35
 * button = container->addWidget(
36
 *            std::make_unique<Wt::WRadioButton>(
37
 *              "I voted Republican"));
38
 * container->addWidget(std::make_unique<Wt::WBreak>());
39
 * group->addButton(button, Vote::Republican);
40
 *
41
 * button = container->addWidget(
42
 *            std::make_unique<Wt::WRadioButton>(
43
 *              "I voted Democrat"));
44
 * container->addWidget(std::make_unique<Wt::WBreak>());
45
 * group->addButton(button, Vote::Democrat);
46
 *
47
 * button = container->addWidget(
48
 *            std::make_unique<Wt::WRadioButton>(
49
 *              "I didn't vote"));
50
 * container->addWidget(std::make_unique<Wt::WBreak>());
51
 * group->addButton(button, Vote::NoVote);
52
 *
53
 * group->setCheckedButton(group->button(Vote::NoVote));
54
 * \endcode
55
 * \elseif java
56
 * \code
57
 * enum Vote { Republican, Democrate, NoVote };
58
 *
59
 * // use a group box as widget container for 3 radio buttons, with a title
60
 * WGroupBox container = new WGroupBox("USA elections vote");
61
62
 * // use a button group to logically group the 3 options
63
 * WButtonGroup group = new WButtonGroup(this);
64
65
 * WRadioButton button;
66
 * button = new WRadioButton("I voted Republican", container);
67
 * new WBreak(container);
68
 * group.addButton(button, Vote.Republican.ordinal());
69
 * button = new WRadioButton("I voted Democrat", container);
70
 * new WBreak(container);
71
 * group.addButton(button, Vote.Democrate.ordinal());
72
73
 * button = new WRadioButton("I didn't vote", container);
74
 * new WBreak(container);
75
 * group.addButton(button, Vote.NoVote.ordinal());
76
77
 * group.setCheckedButton(group.button(Vote.NoVote.ordinal()));
78
 * \endcode
79
 * \endif
80
 *
81
 * %WRadioButton is an \link WWidget::setInline(bool) inline \endlink widget.
82
 *
83
 * <h3>CSS</h3>
84
 *
85
 * This widget corresponds to the HTML <tt>&lt;input
86
 * type="radio"&gt;</tt> tag.  When a label is specified, the input
87
 * element is nested in a <tt>&lt;label&gt;</tt>.
88
 *
89
 * This widget does not provide styling, and can be styled using
90
 * inline or external CSS as appropriate.
91
 *
92
 * \sa WAbstractToggleButton, WButtonGroup
93
 */
94
class WT_API WRadioButton : public WAbstractToggleButton
95
{
96
public:
97
  /*! \brief Creates an unchecked radio button with empty label and optional
98
   *         parent.
99
   */
100
  WRadioButton();
101
102
  /*! \brief Creates an unchecked radio button with given text and optional
103
   *         parent.
104
   */
105
  WRadioButton(const WString& text);
106
107
  /*! \brief Destructor.
108
   */
109
  ~WRadioButton();
110
111
  /*! \brief Returns the button group.
112
   *
113
   * Returns the button group to which this button belongs.
114
   *
115
   * \sa WButtonGroup::addButton(WRadioButton *, int)
116
   */
117
0
  WButtonGroup *group() const { return buttonGroup_.get(); }
118
119
private:
120
  std::shared_ptr<WButtonGroup> buttonGroup_;
121
122
  void setGroup(std::shared_ptr<WButtonGroup> buttonGroup);
123
  friend class WButtonGroup;
124
125
protected:
126
  virtual void updateInput(DomElement& input, bool all) override;
127
  virtual void getFormObjects(FormObjectsMap& formObjects) override;
128
129
  virtual void setFormData(const FormData& formData) override;
130
};
131
132
}
133
134
#endif // WRADIOBUTTON_H_