Wt examples
3.2.0
|
00001 /* 00002 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium. 00003 * 00004 * See the LICENSE file for terms of use. 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 // Create a popup for prompting the amount of money, and connect the 00025 // okPressed button to the slot for setting the amount of money. 00026 // 00027 // Note that the input provided by the user in the prompt box is passed as 00028 // an argument to the slot. 00029 promptAmount_ = Popup::createPrompt("How much do you want to pay?", "", 00030 this); 00031 promptAmount_->okPressed().connect(this, &JavascriptExample::setAmount); 00032 00033 // Create a popup for confirming the payment. 00034 // 00035 // Since a confirm popup does not allow input, we ignore the 00036 // argument carrying the input (which will be empty anyway). 00037 confirmPay_ = Popup::createConfirm("", this); 00038 confirmPay_->okPressed().connect(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 // Connect the event handlers to a JSlot: this will execute the JavaScript 00068 // immediately, without a server round trip. 00069 amountButton->clicked().connect(promptAmount_->show); 00070 confirmButton->clicked().connect(confirmPay_->show); 00071 00072 // Set the initial amount 00073 setAmount("1000"); 00074 } 00075 00076 void JavascriptExample::setAmount(const std::string amount) 00077 { 00078 // Change the confirmation message to include the amount. 00079 confirmPay_->setMessage("Are you sure you want to pay $" + amount + " ?"); 00080 00081 // Change the default value for the prompt. 00082 promptAmount_->setDefaultValue(amount); 00083 00084 // Change the text that shows the current amount. 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