/home/koen/project/wt/cvs/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 WtSignalEmit 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       ("if (confirm('" + message_.narrow() + "')) {"
00037        "  WtSignalEmit('" + id() + "','" + okPressed.name() + "', '');"
00038        "} else {"
00039        "  WtSignalEmit('" + id() + "','" + cancelPressed.name() + "');"
00040        "}");
00041     break;
00042   case Alert:
00043     show.setJavaScript
00044       ("alert('" + message_.narrow() + "');"
00045        "WtSignalEmit('" + id() + "','" + okPressed.name() + "', '');");
00046     break;
00047   case Prompt:
00048     show.setJavaScript
00049       ("var n = prompt('" + message_.narrow() + "', '" + defaultValue_ + "');"
00050        "if (n != null) {"
00051        "  WtSignalEmit('" + id() + "','" + okPressed.name() + "', n);"
00052        "} else {"
00053        "  WtSignalEmit('" + id() + "','" + cancelPressed.name() + "');"
00054        "}");
00055   }
00056 }
00057 
00058 void Popup::setMessage(const WString& message)
00059 {
00060   message_ = message;
00061   setJavaScript();
00062 }
00063 
00064 void Popup::setDefaultValue(const std::string defaultValue)
00065 {
00066   defaultValue_ = defaultValue;
00067   setJavaScript();
00068 }
00069 
00070 Popup *Popup::createConfirm(const WString& message, WObject *parent)
00071 {
00072   return new Popup(Confirm, message, std::string(), parent);
00073 }
00074 
00075 Popup *Popup::createAlert(const WString& message, WObject *parent)
00076 {
00077   return new Popup(Alert, message, std::string(), parent);
00078 }
00079 
00080 Popup *Popup::createPrompt(const WString& message,
00081                            const std::string defaultValue, WObject *parent)
00082 {
00083   return new Popup(Prompt, message, defaultValue, parent);
00084 }

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