Project

General

Profile

Bug #3641 ยป hello.C

Koen Deforche, 02/16/2015 05:47 PM

 
1
/*
2
 * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
3
 *
4
 * See the LICENSE file for terms of use.
5
 */
6

    
7
#include <Wt/WApplication>
8
#include <Wt/WBreak>
9
#include <Wt/WContainerWidget>
10
#include <Wt/WLineEdit>
11
#include <Wt/WPushButton>
12
#include <Wt/WText>
13

    
14
// c++0x only, for std::bind
15
// #include <functional>
16

    
17
using namespace Wt;
18

    
19
/*
20
 * A simple hello world application class which demonstrates how to react
21
 * to events, read input, and give feed-back.
22
 */
23
class HelloApplication : public WApplication
24
{
25
public:
26
  HelloApplication(const WEnvironment& env);
27

    
28
private:
29
  WLineEdit *nameEdit_;
30
  WText *greeting_;
31

    
32
  void greet();
33
};
34

    
35
/*
36
 * The env argument contains information about the new session, and
37
 * the initial request. It must be passed to the WApplication
38
 * constructor so it is typically also an argument for your custom
39
 * application constructor.
40
*/
41
HelloApplication::HelloApplication(const WEnvironment& env)
42
  : WApplication(env)
43
{
44
  setTitle("Hello world");                               // application title
45

    
46
  root()->addWidget(new WText("Your name, please ? "));  // show some text
47
  nameEdit_ = new WLineEdit(root());                     // allow text input
48
  nameEdit_->setFocus();                                 // give focus
49
  nameEdit_->setPlaceholderText("Test");
50

    
51
  WPushButton *button
52
    = new WPushButton("Greet me.", root());              // create a button
53
  button->setMargin(5, Left);                            // add 5 pixels margin
54

    
55
  root()->addWidget(new WBreak());                       // insert a line break
56

    
57
  greeting_ = new WText(root());                         // empty text
58

    
59
  /*
60
   * Connect signals with slots
61
   *
62
   * - simple Wt-way
63
   */
64
  button->clicked().connect(this, &HelloApplication::greet);
65

    
66
  /*
67
   * - using an arbitrary function object (binding values with boost::bind())
68
   */
69
  nameEdit_->enterPressed().connect
70
    (boost::bind(&HelloApplication::greet, this));
71

    
72
  /*
73
   * - using a c++0x lambda:
74
   */
75
  // button->clicked().connect(std::bind([=]() { 
76
  //       greeting_->setText("Hello there, " + nameEdit_->text());
77
  // }));
78
}
79

    
80
void HelloApplication::greet()
81
{
82
  /*
83
   * Update the text, using text input into the nameEdit_ field.
84
   */
85
  greeting_->setText("Hello there, " + nameEdit_->text());
86
  nameEdit_->setText("No way!");
87
}
88

    
89
WApplication *createApplication(const WEnvironment& env)
90
{
91
  /*
92
   * You could read information from the environment to decide whether
93
   * the user has permission to start a new application
94
   */
95
  return new HelloApplication(env);
96
}
97

    
98
int main(int argc, char **argv)
99
{
100
  /*
101
   * Your main method may set up some shared resources, but should then
102
   * start the server application (FastCGI or httpd) that starts listening
103
   * for requests, and handles all of the application life cycles.
104
   *
105
   * The last argument to WRun specifies the function that will instantiate
106
   * new application objects. That function is executed when a new user surfs
107
   * to the Wt application, and after the library has negotiated browser
108
   * support. The function should return a newly instantiated application
109
   * object.
110
   */
111
  return WRun(argc, argv, &createApplication);
112
}
113

    
    (1-1/1)