00001
00002
00003
00004
00005
00006 #include <iostream>
00007
00008 #include <WApplication>
00009 #include <WBreak>
00010 #include <WContainerWidget>
00011 #include <WText>
00012 #include <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:"
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 "WtSignalEmit() 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>"
00054 "<p>This simple application shows how to interact between C++ and"
00055 " JavaScript using the JSlot and JSignal classes.</p>", root());
00056
00057 currentAmount_
00058 = new WText("Current amount: $" + promptAmount_->defaultValue(), root());
00059
00060 WPushButton *amountButton = new WPushButton("Change ...", root());
00061 amountButton->setMargin(10, WWidget::Left | WWidget::Right);
00062
00063 new WBreak(root());
00064
00065 WPushButton *confirmButton = new WPushButton("Pay now.", root());
00066 confirmButton->setMargin(10, WWidget::Top | WWidget::Bottom);
00067
00068
00069
00070 amountButton->clicked.connect(promptAmount_->show);
00071 confirmButton->clicked.connect(confirmPay_->show);
00072
00073
00074 setAmount("1000");
00075 }
00076
00077 void JavascriptExample::setAmount(const std::string amount)
00078 {
00079
00080 confirmPay_->setMessage("Are you sure you want to pay $" + amount + " ?");
00081
00082
00083 promptAmount_->setDefaultValue(amount);
00084
00085
00086 currentAmount_->setText("Current amount: $" + promptAmount_->defaultValue());
00087 }
00088
00089 void JavascriptExample::confirmed()
00090 {
00091 new WText("<br/>Just payed $" + promptAmount_->defaultValue() + ".", root());
00092 }
00093
00094 WApplication *createApplication(const WEnvironment& env)
00095 {
00096 return new JavascriptExample(env);
00097 }
00098
00099 int main(int argc, char **argv)
00100 {
00101 return WRun(argc, argv, &createApplication);
00102 }
00103