FormExample.C 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include "FormExample.h"
  7. #include "Form.h"
  8. #include <Wt/WApplication>
  9. #include <Wt/WText>
  10. #include <Wt/WStringUtil>
  11. FormExample::FormExample(WContainerWidget *parent)
  12. : WContainerWidget(parent)
  13. {
  14. WContainerWidget *langLayout = new WContainerWidget(this);
  15. langLayout->setContentAlignment(AlignRight);
  16. new WText(tr("language"), langLayout);
  17. const char *lang[] = { "en", "nl" };
  18. for (int i = 0; i < 2; ++i) {
  19. WText *t = new WText(widen(lang[i]), langLayout);
  20. t->setMargin(5);
  21. t->clicked().connect(this, &FormExample::changeLanguage);
  22. languageSelects_.push_back(t);
  23. }
  24. /*
  25. * Start with the reported locale, if available
  26. */
  27. setLanguage(wApp->locale().name());
  28. Form *form = new Form(this);
  29. form->setMargin(20);
  30. }
  31. void FormExample::setLanguage(const std::string lang)
  32. {
  33. bool haveLang = false;
  34. for (unsigned i = 0; i < languageSelects_.size(); ++i) {
  35. WText *t = languageSelects_[i];
  36. // prefix match, e.g. en matches en-us.
  37. bool isLang = lang.find(narrow(t->text().value())) == 0;
  38. t->setStyleClass(isLang ? L"langcurrent" : L"lang");
  39. haveLang = haveLang || isLang;
  40. }
  41. if (!haveLang) {
  42. languageSelects_[0]->setStyleClass(L"langcurrent");
  43. WApplication::instance()
  44. ->setLocale(narrow(languageSelects_[0]->text().value()));
  45. } else
  46. WApplication::instance()->setLocale(lang);
  47. }
  48. void FormExample::changeLanguage()
  49. {
  50. WText *t = (WText *)sender();
  51. setLanguage(narrow(t->text().value()));
  52. }
  53. WApplication *createApplication(const WEnvironment& env)
  54. {
  55. WApplication *app = new WApplication(env);
  56. app->messageResourceBundle().use(WApplication::appRoot() + "form-example");
  57. app->setTitle("Form example");
  58. app->root()->addWidget(new FormExample());
  59. WCssDecorationStyle langStyle;
  60. langStyle.font().setSize(WFont::Smaller);
  61. langStyle.setCursor(PointingHandCursor);
  62. langStyle.setForegroundColor(blue);
  63. langStyle.setTextDecoration(WCssDecorationStyle::Underline);
  64. app->styleSheet().addRule(".lang", langStyle);
  65. langStyle.setCursor(ArrowCursor);
  66. langStyle.font().setWeight(WFont::Bold);
  67. app->styleSheet().addRule(".langcurrent", langStyle);
  68. return app;
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. return WRun(argc, argv, &createApplication);
  73. }