File: hello.C

package info (click to toggle)
witty 3.1.2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 45,512 kB
  • ctags: 35,832
  • sloc: cpp: 69,469; ansic: 66,945; xml: 4,383; sh: 594; perl: 108; makefile: 106
file content (146 lines) | stat: -rw-r--r-- 3,821 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WEnvironment>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WServer>
#include <Wt/WText>

using namespace Wt;

class HelloApplication : public WApplication
{
public:
  HelloApplication(const WEnvironment& env, bool embedded);

private:
  WLineEdit *nameEdit_;
  WText *greeting_;

  void greet();
};

HelloApplication::HelloApplication(const WEnvironment& env, bool embedded)
  : WApplication(env)
{
  /*
   * By default, "dynamic script tags" are used to relay event information
   * in WidgetSet mode. This has the benefit of allowing an application to
   * be embedded from within a web page on another domain.
   *
   * You can revert to plain AJAX requests using the following call. This will
   * only work if your application is hosted on the same domain as the
   * web page in which it is embedded.
   */
  //setAjaxMethod(XMLHttpRequest);

  WContainerWidget *top;

  setTitle("Hello world");

  if (!embedded) {
    /*
     * In Application mode, we have the root() is a container
     * corresponding to the entire browser window
     */
    top = root();

  } else {
    /*
     * In WidgetSet mode, we create and bind containers to existing
     * divs in the web page. In this example, we create a single div
     * whose DOM id was passed as a request argument.
     */
    top = new WContainerWidget();
    const std::string *div = env.getParameter("div");
    if (div)
      bindWidget(top, *div);
    else {
      std::cerr << "Missing: parameter: 'div'" << std::endl;
      return;
    }
  }

  if (!embedded)
    new WText("<p><emph>Note: you can also run this application "
	      "from within <a href=\"hello.html\">a web page</a>.</emph></p>",
	      root());

  /*
   * Everything else is business as usual.
   */

  top->addWidget(new WText("Your name, please ? "));
  nameEdit_ = new WLineEdit(top);
  nameEdit_->setFocus();

  WPushButton *b = new WPushButton("Greet me.", top);
  b->setMargin(5, Left);

  top->addWidget(new WBreak());

  greeting_ = new WText(top);

  /*
   * Connect signals with slots
   */
  b->clicked().connect(SLOT(this, HelloApplication::greet));
  nameEdit_->enterPressed().connect(SLOT(this, HelloApplication::greet));
}

void HelloApplication::greet()
{
  /*
   * Update the text, using text input into the nameEdit_ field.
   */
  greeting_->setText("Hello there, " + nameEdit_->text());
}

WApplication *createApplication(const WEnvironment& env)
{
  return new HelloApplication(env, false);
}

WApplication *createWidgetSet(const WEnvironment& env)
{
  return new HelloApplication(env, true);
}

int main(int argc, char **argv)
{
  WServer server(argv[0]);

  // Use default server configuration: command line arguments and the
  // wthttpd configuration file.
  server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);

  // Application entry points. Each entry point binds an URL with an
  // application (with a callback function used to bootstrap a new
  // application).

  // The following is the default entry point. It configures a
  // standalone Wt application at the deploy path configured in the
  // server configuration.
  server.addEntryPoint(Application, createApplication);

  // The following adds an entry point for a widget set. A widget set
  // must be loaded as a JavaScript from an HTML page.
  server.addEntryPoint(WidgetSet, createWidgetSet, "/hello.wtjs");

  // Start the server (in the background if there is threading support)
  // and wait for a shutdown signal (e.g. Ctrl C, SIGKILL)
  if (server.start()) {
    WServer::waitForShutdown();

    // Cleanly terminate all sessions
    server.stop();
  }
}