/home/koen/project/wt/cvs/wt/examples/javascript/JavascriptExample.C

Go to the documentation of this file.
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 <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   // 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(SLOT(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(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   // Connect the event handlers to a JSlot: this will execute the JavaScript
00069   // immediately, without a server round trip.
00070   amountButton->clicked.connect(promptAmount_->show);
00071   confirmButton->clicked.connect(confirmPay_->show);
00072 
00073   // Set the initial amount
00074   setAmount("1000");
00075 }
00076 
00077 void JavascriptExample::setAmount(const std::string amount)
00078 {
00079   // Change the confirmation message to include the amount.
00080   confirmPay_->setMessage("Are you sure you want to pay $" + amount + " ?");
00081 
00082   // Change the default value for the prompt.
00083   promptAmount_->setDefaultValue(amount);
00084 
00085   // Change the text that shows the current amount.
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 

Generated on Mon Apr 14 15:15:04 2008 for Wt by doxygen 1.5.3