Wt examples  3.7.1
Popup.C
Go to the documentation of this file.
1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 
8 #include "Popup.h"
9 
10 using namespace Wt;
11 
12 Popup::Popup(Type t, const WString& message, std::string defaultValue,
13  WObject *parent)
14  : WObject(parent),
15  okPressed_(this, "ok"),
16  cancelPressed_(this, "cancel"),
17  t_(t),
18  message_(message),
19  defaultValue_(defaultValue)
20 {
21  setJavaScript();
22 }
23 
25 {
26  /*
27  * Sets the JavaScript code.
28  *
29  * Notice how Wt.emit() is used to emit the okPressed or cancelPressed
30  * signal, and how arguments may be passed to it, matching the number and
31  * type of arguments in the JSignal definition.
32  */
33  switch (t_) {
34  case Confirm:
36  ("function(){ if (confirm('" + message_.narrow() + "')) {"
37  + okPressed_.createCall("''") +
38  "} else {"
40  "}}");
41  break;
42  case Alert:
44  ("function(){ alert('" + message_.narrow() + "');"
45  + okPressed_.createCall("''") +
46  "}");
47  break;
48  case Prompt:
50  ("function(){var n = prompt('" + message_.narrow() + "', '"
51  + defaultValue_ + "');"
52  "if (n != null) {"
53  + okPressed_.createCall("n") +
54  "} else {"
56  "}}");
57  }
58 }
59 
61 {
62  message_ = message;
63  setJavaScript();
64 }
65 
66 void Popup::setDefaultValue(const std::string defaultValue)
67 {
69  setJavaScript();
70 }
71 
73 {
74  return new Popup(Confirm, message, std::string(), parent);
75 }
76 
78 {
79  return new Popup(Alert, message, std::string(), parent);
80 }
81 
83  const std::string defaultValue, WObject *parent)
84 {
85  return new Popup(Prompt, message, defaultValue, parent);
86 }
static Popup * createConfirm(const WString &message, WObject *parent=0)
Create a confirm dialog.
Definition: Popup.C:72
JSlot show
Show the dialog.
Definition: Popup.h:70
static Popup * createAlert(const WString &message, WObject *parent=0)
Create an alert dialog.
Definition: Popup.C:77
const std::string createCall(std::initializer_list< std::string > args) const
std::string narrow(const std::locale &loc=std::locale()) const
WString message_
Definition: Popup.h:94
void setDefaultValue(const std::string defaultValue)
Change the default value for a prompt dialog.
Definition: Popup.C:66
void setJavaScript()
Update the javascript code.
Definition: Popup.C:24
static Popup * createPrompt(const WString &message, const std::string defaultValue, WObject *parent=0)
Create a prompt dialog with the given default value.
Definition: Popup.C:82
std::string defaultValue_
Definition: Popup.h:95
JSignal< void > cancelPressed_
Definition: Popup.h:91
void setMessage(const WString &message)
Change the message.
Definition: Popup.C:60
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:63
const WString & message() const
Get the current message.
Definition: Popup.h:59
Type t_
Definition: Popup.h:93
JSignal< std::string > okPressed_
Definition: Popup.h:90
A JavaScript based popup window, encapsulating the Javascript functions alert(), confirm(), and prompt().
Definition: Popup.h:32
Popup(Type t, const WString &message, const std::string defaultValue, WObject *parent)
Popup constructor.
Definition: Popup.C:12
void setJavaScript(const std::string &javaScript, int nbArgs=0)
Type
Popup type.
Definition: Popup.h:83

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13