StyleExample.C 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <boost/lexical_cast.hpp>
  7. #include <Wt/WLineEdit>
  8. #include <Wt/WIntValidator>
  9. #include <Wt/WText>
  10. #include <Wt/WPushButton>
  11. #include <Wt/WApplication>
  12. #include <Wt/WBreak>
  13. #include "RoundedWidget.h"
  14. #include "StyleExample.h"
  15. char loremipsum[] = "Lorem ipsum dolor sit amet, consectetur adipisicing "
  16. "elit, sed do eiusmod tempor incididunt ut labore et "
  17. "dolore magna aliqua. Ut enim ad minim veniam, quis "
  18. "nostrud exercitation ullamco laboris nisi ut aliquip "
  19. "ex ea commodo consequat. Duis aute irure dolor in "
  20. "reprehenderit in voluptate velit esse cillum dolore eu "
  21. "fugiat nulla pariatur. Excepteur sint occaecat cupidatat "
  22. "non proident, sunt in culpa qui officia deserunt mollit "
  23. "anim id est laborum.";
  24. StyleExample::StyleExample(WContainerWidget *parent)
  25. : WContainerWidget(parent)
  26. {
  27. w_ = new RoundedWidget(RoundedWidget::All, this);
  28. new WText(loremipsum, w_->contents());
  29. new WBreak(this);
  30. new WText("Color (rgb): ", this);
  31. r_ = createValidateLineEdit(w_->backgroundColor().red(), 0, 255);
  32. g_ = createValidateLineEdit(w_->backgroundColor().green(), 0, 255);
  33. b_ = createValidateLineEdit(w_->backgroundColor().blue(), 0, 255);
  34. new WBreak(this);
  35. new WText("Radius (px): ", this);
  36. radius_ = createValidateLineEdit(w_->cornerRadius(), 1, 500);
  37. new WBreak(this);
  38. WPushButton *p = new WPushButton("Update!", this);
  39. p->clicked().connect(this, &StyleExample::updateStyle);
  40. new WBreak(this);
  41. error_ = new WText("", this);
  42. }
  43. WLineEdit *StyleExample::createValidateLineEdit(int value, int min, int max)
  44. {
  45. WLineEdit *le = new WLineEdit(boost::lexical_cast<std::wstring>(value), this);
  46. le->setTextSize(3);
  47. le->setValidator(new WIntValidator(min, max));
  48. return le;
  49. }
  50. void StyleExample::updateStyle()
  51. {
  52. if ((r_->validate() != WValidator::Valid)
  53. || (g_->validate() != WValidator::Valid)
  54. || (b_->validate() != WValidator::Valid))
  55. error_->setText("Color components must be numbers between 0 and 255.");
  56. else if (radius_->validate() != WValidator::Valid)
  57. error_->setText("Radius must be between 1 and 500.");
  58. else {
  59. w_->setBackgroundColor(WColor(boost::lexical_cast<int>(r_->text()),
  60. boost::lexical_cast<int>(g_->text()),
  61. boost::lexical_cast<int>(b_->text())));
  62. w_->setCornerRadius(boost::lexical_cast<int>(radius_->text()));
  63. error_->setText("");
  64. }
  65. }
  66. WApplication *createApplication(const WEnvironment& env)
  67. {
  68. WApplication *app = new WApplication(env);
  69. app->setTitle("Style example");
  70. app->root()->addWidget(new StyleExample());
  71. return app;
  72. }
  73. int main(int argc, char **argv)
  74. {
  75. return WRun(argc, argv, &createApplication);
  76. }