hello.C 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2014 Emweb bvba, Heverlee, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <iostream>
  7. #include <Wt/WBreak>
  8. #include <Wt/WContainerWidget>
  9. #include <Wt/WLineEdit>
  10. #include <Wt/WMessageBox>
  11. #include <Wt/WPushButton>
  12. #include <Wt/WText>
  13. #include "SingleThreadedApplication.h"
  14. using namespace Wt;
  15. class HelloApplication : public SingleThreadedApplication
  16. {
  17. public:
  18. HelloApplication(const WEnvironment& env);
  19. protected:
  20. virtual void create();
  21. virtual void destroy();
  22. private:
  23. WLineEdit *nameEdit_;
  24. WText *greeting_;
  25. void greet();
  26. };
  27. HelloApplication::HelloApplication(const WEnvironment& env)
  28. : SingleThreadedApplication(env)
  29. {
  30. /* Intentionally left blank! Actual construction happens in create() */
  31. }
  32. void HelloApplication::create()
  33. {
  34. setTitle("Hello world");
  35. root()->addWidget(new WText("Your name, please ? "));
  36. nameEdit_ = new WLineEdit(root());
  37. nameEdit_->setFocus();
  38. WPushButton *button = new WPushButton("Greet me.", root());
  39. button->setMargin(5, Left);
  40. root()->addWidget(new WBreak());
  41. greeting_ = new WText(root());
  42. button->clicked().connect(this, &HelloApplication::greet);
  43. nameEdit_->enterPressed().connect
  44. (boost::bind(&HelloApplication::greet, this));
  45. }
  46. void HelloApplication::destroy()
  47. {
  48. root()->clear();
  49. }
  50. void HelloApplication::greet()
  51. {
  52. /*
  53. * You can even functions that block the event loop (and start a recursive
  54. * event loop).
  55. */
  56. WMessageBox::show("Welcome", "Hello there, " + nameEdit_->text(), Ok);
  57. }
  58. WApplication *createApplication(const WEnvironment& env)
  59. {
  60. return new HelloApplication(env);
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. return WRun(argc, argv, &createApplication);
  65. }