1 /*
2  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3  *
4  * See the LICENSE file for terms of use.
5  */
6 #include "Wt/WImage.h"
7 #include "Wt/WContainerWidget.h"
8 #include "Wt/WCssDecorationStyle.h"
9 #include "Wt/WIconPair.h"
10 
11 namespace Wt {
12 
WIconPair(const std::string & icon1URI,const std::string & icon2URI,bool clickIsSwitch)13 WIconPair::WIconPair(const std::string& icon1URI, const std::string& icon2URI,
14 		     bool clickIsSwitch)
15   : impl_(new WContainerWidget()),
16     icon1_(new WImage(icon1URI)),
17     icon2_(new WImage(icon2URI))
18 {
19   setImplementation(std::unique_ptr<WWidget>(impl_));
20   impl_->addWidget(std::unique_ptr<WWidget>(icon1_));
21   impl_->addWidget(std::unique_ptr<WWidget>(icon2_));
22   impl_->setLoadLaterWhenInvisible(false);
23 
24   setInline(true);
25 
26   icon2_->hide();
27 
28   if (clickIsSwitch) {
29 #ifndef WT_TARGET_JAVA
30     std::string fic1 = icon1_->id();
31     std::string fic2 = icon2_->id();
32     std::string hide_1 = WT_CLASS ".hide('" + fic1 +"');";
33     std::string show_1 = WT_CLASS ".inline('" + fic1 +"');";
34     std::string hide_2 = WT_CLASS ".hide('" + fic2 +"');";
35     std::string show_2 = WT_CLASS ".inline('" + fic2 +"');";
36     implementJavaScript(&WIconPair::showIcon1, hide_2 + show_1
37 			+ WT_CLASS ".cancelEvent(e);");
38     implementJavaScript(&WIconPair::showIcon2, hide_1 + show_2
39 			+ WT_CLASS ".cancelEvent(e);");
40 #else
41     icon1_->clicked().preventPropagation();
42     icon2_->clicked().preventPropagation();
43 #endif // WT_TARGET_JAVA
44 
45     icon1_->clicked().connect(this, &WIconPair::showIcon2);
46     icon2_->clicked().connect(this, &WIconPair::showIcon1);
47 
48     decorationStyle().setCursor(Cursor::PointingHand);
49   }
50 }
51 
setState(int num)52 void WIconPair::setState(int num)
53 {
54   if (num == 0) {
55     icon1_->show();
56     icon2_->hide();
57   } else {
58     icon1_->hide();
59     icon2_->show();
60   }
61 }
62 
state()63 int WIconPair::state() const
64 {
65   return (icon1_->isHidden() ? 1 : 0);
66 }
67 
showIcon1()68 void WIconPair::showIcon1()
69 {
70   setState(0);
71 }
72 
showIcon2()73 void WIconPair::showIcon2()
74 {
75   setState(1);
76 }
77 
icon1Clicked()78 EventSignal<WMouseEvent>& WIconPair::icon1Clicked()
79 {
80   return icon1_->clicked();
81 }
82 
icon2Clicked()83 EventSignal<WMouseEvent>& WIconPair::icon2Clicked()
84 {
85   return icon2_->clicked();
86 }
87 
88 }
89