/src/wt/src/Wt/WCheckBox.C
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2008 Emweb bv, Herent, Belgium. |
3 | | * |
4 | | * See the LICENSE file for terms of use. |
5 | | */ |
6 | | |
7 | | #include "Wt/WCheckBox.h" |
8 | | |
9 | | #include "Wt/WApplication.h" |
10 | | #include "Wt/WEnvironment.h" |
11 | | #include "Wt/WJavaScript.h" |
12 | | #include "DomElement.h" |
13 | | |
14 | | namespace Wt { |
15 | | |
16 | | WCheckBox::WCheckBox() |
17 | 0 | : WAbstractToggleButton(), |
18 | 0 | triState_(false), |
19 | 0 | partialStateSelectable_(false) |
20 | 0 | { |
21 | 0 | setFormObject(true); |
22 | 0 | } |
23 | | |
24 | | WCheckBox::WCheckBox(const WString& text) |
25 | 0 | : WAbstractToggleButton(text), |
26 | 0 | triState_(false), |
27 | 0 | partialStateSelectable_(false) |
28 | 0 | { |
29 | 0 | setFormObject(true); |
30 | 0 | } |
31 | | |
32 | | void WCheckBox::setTristate(bool tristate) |
33 | 0 | { |
34 | 0 | triState_ = tristate; |
35 | |
|
36 | 0 | if (triState_) { |
37 | 0 | if (!supportsIndeterminate(WApplication::instance()->environment())) |
38 | 0 | updateJSlot(); |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | | void WCheckBox::setPartialStateSelectable(bool t) |
43 | 0 | { |
44 | 0 | if (t && !isTristate()) |
45 | 0 | setTristate(true); |
46 | |
|
47 | 0 | partialStateSelectable_ = t; |
48 | 0 | updateJSlot(); |
49 | 0 | updateNextState(); |
50 | 0 | } |
51 | | |
52 | | void WCheckBox::updateJSlot() |
53 | 0 | { |
54 | 0 | jslot_ = nullptr; |
55 | 0 | std::unique_ptr<JSlot> slot; |
56 | 0 | std::string partialOn, partialOff; |
57 | 0 | if (!supportsIndeterminate(WApplication::instance()->environment())) { |
58 | 0 | partialOff = "obj.style.opacity='';"; |
59 | 0 | partialOn = "obj.style.opacity='0.5';"; |
60 | 0 | if (triState_ && !partialStateSelectable_) |
61 | 0 | slot.reset(new JSlot("function(obj, e) { " + partialOff + "}", this)); |
62 | 0 | } else { |
63 | 0 | partialOn = "obj.indeterminate=true;"; |
64 | 0 | partialOff = "obj.indeterminate=false;"; |
65 | 0 | } |
66 | |
|
67 | 0 | if (partialStateSelectable_) { |
68 | 0 | std::stringstream ss; |
69 | 0 | ss << "function(obj, e) {\n" |
70 | 0 | << "if(obj.nextState == 'c'){\n" |
71 | 0 | << "obj.checked=true;" |
72 | 0 | << partialOff |
73 | 0 | << " obj.nextState='u';" |
74 | 0 | << "} else if( obj.nextState=='i') {\n" |
75 | 0 | << "obj.nextState='c';" |
76 | 0 | << partialOn |
77 | 0 | << " } else if( obj.nextState=='u') {\n" |
78 | 0 | << "obj.nextState='i';" |
79 | 0 | << "obj.checked=false;" |
80 | 0 | << partialOff |
81 | 0 | << " } else obj.nextState='i';" |
82 | 0 | << "}"; |
83 | 0 | slot.reset(new JSlot(ss.str(), this)); |
84 | 0 | } |
85 | |
|
86 | 0 | if (slot) { |
87 | 0 | changed().connect(*slot); |
88 | 0 | jslot_ = std::move(slot); |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | 0 | void WCheckBox::updateNextState() { |
93 | 0 | std::string nextState; |
94 | |
|
95 | 0 | switch(state_) { |
96 | 0 | case CheckState::Checked: |
97 | 0 | nextState="u"; |
98 | 0 | break; |
99 | 0 | case CheckState::Unchecked: |
100 | 0 | nextState="i"; |
101 | 0 | break; |
102 | 0 | case CheckState::PartiallyChecked: |
103 | 0 | nextState="c"; |
104 | 0 | break; |
105 | 0 | } |
106 | | |
107 | 0 | if (partialStateSelectable_) |
108 | 0 | doJavaScript(this->jsRef() + ".nextState='"+nextState+"';"); |
109 | 0 | else |
110 | 0 | doJavaScript(this->jsRef() + ".nextState=null;"); |
111 | 0 | } |
112 | | |
113 | | void WCheckBox::setCheckState(CheckState state) |
114 | 0 | { |
115 | 0 | WAbstractToggleButton::setCheckState(state); |
116 | 0 | updateNextState(); |
117 | 0 | } |
118 | | |
119 | | void WCheckBox::updateInput(DomElement& input, bool all) |
120 | 0 | { |
121 | 0 | if (all) |
122 | 0 | input.setAttribute("type", "checkbox"); |
123 | 0 | } |
124 | | |
125 | | } |