/src/wt/src/Wt/WIconPair.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 | | #include "Wt/WIcon.h" |
7 | | #include "Wt/WImage.h" |
8 | | #include "Wt/WContainerWidget.h" |
9 | | #include "Wt/WCssDecorationStyle.h" |
10 | | #include "Wt/WIconPair.h" |
11 | | |
12 | | namespace Wt { |
13 | | |
14 | | WIconPair::WIconPair(const std::string& icon1Str, const std::string& icon2Str, |
15 | | bool clickIsSwitch) |
16 | 0 | : clickIsSwitch_(clickIsSwitch), |
17 | 0 | impl_(new WContainerWidget()) |
18 | 0 | { |
19 | 0 | iconStr_[0] = icon1Str; |
20 | 0 | iconStr_[1] = icon2Str; |
21 | 0 | wicon_[0] = nullptr; |
22 | 0 | wicon_[1] = nullptr; |
23 | 0 | setImplementation(std::unique_ptr<WWidget>(impl_)); |
24 | 0 | image_[0] = impl_->addWidget(std::make_unique<WImage>(WLink(icon1Str))); |
25 | 0 | image_[1] = impl_->addWidget(std::make_unique<WImage>(WLink(icon2Str))); |
26 | 0 | impl_->setLoadLaterWhenInvisible(false); |
27 | |
|
28 | 0 | setInline(true); |
29 | |
|
30 | 0 | image_[1]->hide(); |
31 | |
|
32 | 0 | if (clickIsSwitch) { |
33 | 0 | #ifndef WT_TARGET_JAVA |
34 | 0 | std::string fic1 = image_[0]->id(); |
35 | 0 | std::string fic2 = image_[1]->id(); |
36 | 0 | std::string hide_1 = WT_CLASS ".hide('" + fic1 +"');"; |
37 | 0 | std::string show_1 = WT_CLASS ".inline('" + fic1 +"');"; |
38 | 0 | std::string hide_2 = WT_CLASS ".hide('" + fic2 +"');"; |
39 | 0 | std::string show_2 = WT_CLASS ".inline('" + fic2 +"');"; |
40 | 0 | implementJavaScript(&WIconPair::showIcon1, hide_2 + show_1 |
41 | 0 | + WT_CLASS ".cancelEvent(e);"); |
42 | 0 | implementJavaScript(&WIconPair::showIcon2, hide_1 + show_2 |
43 | 0 | + WT_CLASS ".cancelEvent(e);"); |
44 | | #else |
45 | | image_[0]->clicked().preventPropagation(); |
46 | | image_[1]->clicked().preventPropagation(); |
47 | | #endif // WT_TARGET_JAVA |
48 | |
|
49 | 0 | image_[0]->clicked().connect(this, &WIconPair::showIcon2); |
50 | 0 | image_[1]->clicked().connect(this, &WIconPair::showIcon1); |
51 | |
|
52 | 0 | decorationStyle().setCursor(Cursor::PointingHand); |
53 | 0 | } |
54 | 0 | } |
55 | | void WIconPair::setIconsType(IconType type) |
56 | 0 | { |
57 | 0 | setIcon1Type(type); |
58 | 0 | setIcon2Type(type); |
59 | 0 | } |
60 | | |
61 | | void WIconPair::setIcon1Type(IconType type) |
62 | 0 | { |
63 | 0 | resetIcon(0, type); |
64 | 0 | if (clickIsSwitch_) { |
65 | 0 | usedIcon1()->clicked().connect(this, &WIconPair::showIcon2); |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | void WIconPair::setIcon2Type(IconType type) |
70 | 0 | { |
71 | 0 | resetIcon(1, type); |
72 | 0 | if (clickIsSwitch_) { |
73 | 0 | usedIcon2()->clicked().connect(this, &WIconPair::showIcon1); |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | | void WIconPair::setState(int num) |
78 | 0 | { |
79 | 0 | if (num == 0) { |
80 | 0 | usedIcon1()->show(); |
81 | 0 | usedIcon2()->hide(); |
82 | 0 | } else { |
83 | 0 | usedIcon1()->hide(); |
84 | 0 | usedIcon2()->show(); |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | int WIconPair::state() const |
89 | 0 | { |
90 | 0 | return (usedIcon1()->isHidden() ? 1 : 0); |
91 | 0 | } |
92 | | |
93 | | void WIconPair::showIcon1() |
94 | 0 | { |
95 | 0 | setState(0); |
96 | 0 | } |
97 | | |
98 | | void WIconPair::showIcon2() |
99 | 0 | { |
100 | 0 | setState(1); |
101 | 0 | } |
102 | | |
103 | | EventSignal<WMouseEvent>& WIconPair::icon1Clicked() |
104 | 0 | { |
105 | 0 | return usedIcon1()->clicked(); |
106 | 0 | } |
107 | | |
108 | | EventSignal<WMouseEvent>& WIconPair::icon2Clicked() |
109 | 0 | { |
110 | 0 | return usedIcon2()->clicked(); |
111 | 0 | } |
112 | | |
113 | | WInteractWidget *WIconPair::usedIcon(int i) const |
114 | 0 | { |
115 | 0 | if (wicon_[i]) { |
116 | 0 | return wicon_[i]; |
117 | 0 | } |
118 | 0 | return image_[i]; |
119 | 0 | } |
120 | | |
121 | | void WIconPair::resetIcon(int i, IconType type) |
122 | 0 | { |
123 | 0 | int currentState = state(); |
124 | 0 | if (wicon_[i]) { |
125 | 0 | impl_->removeWidget(wicon_[i]); |
126 | 0 | } |
127 | 0 | if (image_[i]) { |
128 | 0 | impl_->removeWidget(image_[i]); |
129 | 0 | } |
130 | 0 | if (type == IconType::IconName) { |
131 | 0 | wicon_[i] = impl_->addWidget(std::make_unique<WIcon>(iconStr_[i])); |
132 | 0 | image_[i] = nullptr; |
133 | 0 | } else { |
134 | 0 | image_[i] = impl_->addWidget(std::make_unique<WImage>(WLink(iconStr_[i]))); |
135 | 0 | wicon_[i] = nullptr; |
136 | 0 | } |
137 | 0 | resetLearnedSlots(); |
138 | 0 | setState(currentState); |
139 | 0 | } |
140 | | |
141 | | } |