00001
00002
00003
00004
00005
00006 #include <iostream>
00007
00008 #include <Wt/WApplication>
00009 #include <Wt/WBreak>
00010 #include <Wt/WContainerWidget>
00011 #include <Wt/WText>
00012 #include <Wt/WPushButton>
00013
00014 #include "JavascriptExample.h"
00015 #include "Popup.h"
00016
00017 using namespace Wt;
00018
00019 JavascriptExample::JavascriptExample(const WEnvironment& env)
00020 : WApplication(env)
00021 {
00022 setTitle("Javascript example");
00023
00024
00025
00026
00027
00028
00029 promptAmount_ = Popup::createPrompt("How much do you want to pay?", "",
00030 this);
00031 promptAmount_->okPressed().connect(SLOT(this, JavascriptExample::setAmount));
00032
00033
00034
00035
00036
00037 confirmPay_ = Popup::createConfirm("", this);
00038 confirmPay_->okPressed().connect(SLOT(this, JavascriptExample::confirmed));
00039
00040 new WText("<h2>Wt Javascript example</h2>"
00041 "<p>Wt makes abstraction of Javascript, and therefore allows you"
00042 " to develop web applications without any knowledge of Javascript,"
00043 " and which are not dependent on Javascript."
00044 " However, Wt does allow you to add custom Javascript code:</p>"
00045 " <ul>"
00046 " <li>To call custom JavaScript code from an event handler, "
00047 "connect the Wt::EventSignal to a Wt::JSlot.</li>"
00048 " <li>To call C++ code from custom JavaScript, use "
00049 "Wt.emit() to emit a Wt::JSignal.</li>"
00050 " <li>To call custom JavaScript code from C++, use "
00051 "WApplication::doJavascript() or Wt::JSlot::exec().</li>"
00052 " </ul>"
00053 "<p>This simple application shows how to interact between C++ and"
00054 " JavaScript using the JSlot and JSignal classes.</p>", root());
00055
00056 currentAmount_
00057 = new WText("Current amount: $" + promptAmount_->defaultValue(), root());
00058
00059 WPushButton *amountButton = new WPushButton("Change ...", root());
00060 amountButton->setMargin(10, Left | Right);
00061
00062 new WBreak(root());
00063
00064 WPushButton *confirmButton = new WPushButton("Pay now.", root());
00065 confirmButton->setMargin(10, Top | Bottom);
00066
00067
00068
00069 amountButton->clicked().connect(promptAmount_->show);
00070 confirmButton->clicked().connect(confirmPay_->show);
00071
00072
00073 setAmount("1000");
00074 }
00075
00076 void JavascriptExample::setAmount(const std::string amount)
00077 {
00078
00079 confirmPay_->setMessage("Are you sure you want to pay $" + amount + " ?");
00080
00081
00082 promptAmount_->setDefaultValue(amount);
00083
00084
00085 currentAmount_->setText("Current amount: $" + promptAmount_->defaultValue());
00086 }
00087
00088 void JavascriptExample::confirmed()
00089 {
00090 new WText("<br/>Just payed $" + promptAmount_->defaultValue() + ".", root());
00091 }
00092
00093 WApplication *createApplication(const WEnvironment& env)
00094 {
00095 return new JavascriptExample(env);
00096 }
00097
00098 int main(int argc, char **argv)
00099 {
00100 return WRun(argc, argv, &createApplication);
00101 }
00102