Wt examples  3.2.1
/home/koen/project/wt/public-git/wt/examples/javascript/Popup.C
Go to the documentation of this file.
00001 // This may look like C code, but it's really -*- C++ -*-
00002 /*
00003  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
00004  *
00005  * See the LICENSE file for terms of use.
00006  */
00007 
00008 #include "Popup.h"
00009 
00010 using namespace Wt;
00011 
00012 Popup::Popup(Type t, const WString& message, std::string defaultValue,
00013              WObject *parent)
00014   : WObject(parent),
00015     okPressed_(this, "ok"),
00016     cancelPressed_(this, "cancel"),
00017     t_(t),
00018     message_(message),
00019     defaultValue_(defaultValue)
00020 {
00021   setJavaScript();
00022 }
00023 
00024 void Popup::setJavaScript()
00025 {
00026   /*
00027    * Sets the JavaScript code.
00028    *
00029    * Notice how Wt.emit() is used to emit the okPressed or cancelPressed
00030    * signal, and how arguments may be passed to it, matching the number and
00031    * type of arguments in the JSignal definition.
00032    */
00033   switch (t_) {
00034   case Confirm:
00035     show.setJavaScript
00036       ("function(){ if (confirm('" + message_.narrow() + "')) {"
00037        + okPressed_.createCall("''") +
00038        "} else {"
00039        + cancelPressed_.createCall() +
00040        "}}");
00041     break;
00042   case Alert:
00043     show.setJavaScript
00044       ("function(){ alert('" + message_.narrow() + "');"
00045        + okPressed_.createCall("''") +
00046        "}");
00047     break;
00048   case Prompt:
00049     show.setJavaScript
00050       ("function(){var n = prompt('" + message_.narrow() + "', '"
00051        + defaultValue_ + "');"
00052        "if (n != null) {"
00053        + okPressed_.createCall("n") +
00054        "} else {"
00055        + cancelPressed_.createCall() +
00056        "}}");
00057   }
00058 }
00059 
00060 void Popup::setMessage(const WString& message)
00061 {
00062   message_ = message;
00063   setJavaScript();
00064 }
00065 
00066 void Popup::setDefaultValue(const std::string defaultValue)
00067 {
00068   defaultValue_ = defaultValue;
00069   setJavaScript();
00070 }
00071 
00072 Popup *Popup::createConfirm(const WString& message, WObject *parent)
00073 {
00074   return new Popup(Confirm, message, std::string(), parent);
00075 }
00076 
00077 Popup *Popup::createAlert(const WString& message, WObject *parent)
00078 {
00079   return new Popup(Alert, message, std::string(), parent);
00080 }
00081 
00082 Popup *Popup::createPrompt(const WString& message,
00083                            const std::string defaultValue, WObject *parent)
00084 {
00085   return new Popup(Prompt, message, defaultValue, parent);
00086 }

Generated on Fri Mar 30 2012 for the C++ Web Toolkit (Wt) by doxygen 1.7.5.1