RoundedWidget.C 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <Wt/WCssDecorationStyle>
  7. #include <Wt/WContainerWidget>
  8. #include <Wt/WImage>
  9. #include "RoundedWidget.h"
  10. #include "CornerImage.h"
  11. RoundedWidget::RoundedWidget(int corners, WContainerWidget *parent)
  12. : WCompositeWidget(parent),
  13. backgroundColor_(WColor(0xD4,0xDD,0xFF)),
  14. surroundingColor_(WColor(0xFF,0xFF,0xFF)),
  15. radius_(10),
  16. corners_(corners)
  17. {
  18. setImplementation(impl_ = new WContainerWidget());
  19. contents_ = new WContainerWidget(impl_);
  20. create();
  21. }
  22. RoundedWidget::~RoundedWidget()
  23. {
  24. if (images_[1])
  25. delete images_[1];
  26. if (images_[3])
  27. delete images_[3];
  28. }
  29. void RoundedWidget::create()
  30. {
  31. if (corners_ & TopLeft) {
  32. images_[0] = new CornerImage(CornerImage::TopLeft, backgroundColor_,
  33. surroundingColor_, radius_);
  34. images_[0]->setPositionScheme(Absolute);
  35. images_[0]->setMargin(0);
  36. } else
  37. images_[0] = 0;
  38. if (corners_ & TopRight)
  39. images_[1] = new CornerImage(CornerImage::TopRight, backgroundColor_,
  40. surroundingColor_, radius_);
  41. else
  42. images_[1] = 0;
  43. if (corners_ & BottomLeft) {
  44. images_[2] = new CornerImage(CornerImage::BottomLeft, backgroundColor_,
  45. surroundingColor_, radius_);
  46. images_[2]->setPositionScheme(Absolute);
  47. images_[2]->setMargin(0);
  48. } else
  49. images_[2] = 0;
  50. if (corners_ & BottomRight)
  51. images_[3] = new CornerImage(CornerImage::BottomRight, backgroundColor_,
  52. surroundingColor_, radius_);
  53. else
  54. images_[3] = 0;
  55. /*
  56. * At the top: an image (top left corner) inside
  57. * a container widget with background image top right.
  58. */
  59. top_ = new WContainerWidget();
  60. top_->resize(WLength::Auto, radius_);
  61. top_->setPositionScheme(Relative);
  62. if (images_[1])
  63. top_->decorationStyle().setBackgroundImage(images_[1]->imageRef(),
  64. WCssDecorationStyle::NoRepeat,
  65. Top | Right);
  66. if (images_[0])
  67. top_->addWidget(images_[0]);
  68. impl_->insertBefore(top_, contents_); // insert top before the contents
  69. /*
  70. * At the bottom: an image (bottom left corner) inside
  71. * a container widget with background image bottom right.
  72. */
  73. bottom_ = new WContainerWidget();
  74. bottom_->setPositionScheme(Relative);
  75. bottom_->resize(WLength::Auto, radius_);
  76. if (images_[3])
  77. bottom_->decorationStyle().setBackgroundImage(images_[3]->imageRef(),
  78. WCssDecorationStyle::NoRepeat,
  79. Bottom | Right);
  80. if (images_[2])
  81. bottom_->addWidget(images_[2]);
  82. impl_->addWidget(bottom_);
  83. decorationStyle().setBackgroundColor(backgroundColor_);
  84. contents_->setMargin(WLength(radius_), Left | Right);
  85. }
  86. void RoundedWidget::setBackgroundColor(WColor color)
  87. {
  88. backgroundColor_ = color;
  89. adjust();
  90. }
  91. void RoundedWidget::setSurroundingColor(WColor color)
  92. {
  93. surroundingColor_ = color;
  94. adjust();
  95. }
  96. void RoundedWidget::setCornerRadius(int radius)
  97. {
  98. radius_ = radius;
  99. adjust();
  100. }
  101. void RoundedWidget::enableRoundedCorners(bool how)
  102. {
  103. if (images_[0]) images_[0]->setHidden(!how);
  104. if (images_[2]) images_[2]->setHidden(!how);
  105. if (images_[1]) {
  106. images_[1]->setHidden(!how);
  107. if (!how)
  108. top_->decorationStyle().setBackgroundImage("");
  109. else
  110. top_->decorationStyle()
  111. .setBackgroundImage(images_[1]->imageRef(),
  112. WCssDecorationStyle::NoRepeat,
  113. Top | Right);
  114. }
  115. if (images_[3]) {
  116. images_[3]->setHidden(!how);
  117. if (!how)
  118. bottom_->decorationStyle().setBackgroundImage("");
  119. else
  120. bottom_->decorationStyle()
  121. .setBackgroundImage(images_[3]->imageRef(),
  122. WCssDecorationStyle::NoRepeat,
  123. Top | Right);
  124. }
  125. }
  126. void RoundedWidget::adjust()
  127. {
  128. if (images_[0] && !images_[0]->isHidden()) images_[0]->setRadius(radius_);
  129. if (images_[1] && !images_[1]->isHidden()) images_[1]->setRadius(radius_);
  130. if (images_[2] && !images_[2]->isHidden()) images_[2]->setRadius(radius_);
  131. if (images_[3] && !images_[3]->isHidden()) images_[3]->setRadius(radius_);
  132. if (images_[0] && !images_[0]->isHidden())
  133. images_[0]->setForeground(backgroundColor_);
  134. if (images_[1] && !images_[1]->isHidden())
  135. images_[1]->setForeground(backgroundColor_);
  136. if (images_[2] && !images_[2]->isHidden())
  137. images_[2]->setForeground(backgroundColor_);
  138. if (images_[3] && !images_[3]->isHidden())
  139. images_[3]->setForeground(backgroundColor_);
  140. if (images_[1])
  141. top_->decorationStyle().setBackgroundImage(images_[1]->imageRef(),
  142. WCssDecorationStyle::NoRepeat,
  143. Top | Right);
  144. if (images_[3])
  145. bottom_->decorationStyle().setBackgroundImage(images_[3]->imageRef(),
  146. WCssDecorationStyle::NoRepeat,
  147. Bottom | Right);
  148. top_->resize(WLength::Auto, radius_);
  149. bottom_->resize(WLength::Auto, radius_);
  150. contents_->setMargin(radius_, Left | Right);
  151. decorationStyle().setBackgroundColor(backgroundColor_);
  152. }