Wt examples 4.8.2
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 <Wt/WAny.h>
9
10#include "Popup.h"
11
12using namespace Wt;
13
14Popup::Popup(Type t, const WString& message, std::string defaultValue)
15 : WObject(),
16 okPressed_(this, "ok"),
17 cancelPressed_(this, "cancel"),
18 t_(t),
19 message_(message),
20 defaultValue_(defaultValue)
21{
23}
24
26{
27 /*
28 * Sets the JavaScript code.
29 *
30 * Notice how Wt.emit() is used to emit the okPressed or cancelPressed
31 * signal, and how arguments may be passed to it, matching the number and
32 * type of arguments in the JSignal definition.
33 */
34 switch (t_) {
35 case Confirm:
36 show.setJavaScript
37 ("function(){ if (confirm('" + message_.narrow() + "')) {"
38 + okPressed_.createCall({"''"}) +
39 "} else {"
40 + cancelPressed_.createCall({}) +
41 "}}");
42 break;
43 case Alert:
44 show.setJavaScript
45 ("function(){ alert('" + message_.narrow() + "');"
46 + okPressed_.createCall({"''"}) +
47 "}");
48 break;
49 case Prompt:
50 show.setJavaScript
51 ("function(){var n = prompt('" + message_.narrow() + "', '"
52 + defaultValue_ + "');"
53 "if (n != null) {"
54 + okPressed_.createCall({"n"}) +
55 "} else {"
56 + cancelPressed_.createCall({}) +
57 "}}");
58 }
59}
60
61void Popup::setMessage(const WString& message)
62{
65}
66
67void Popup::setDefaultValue(const std::string defaultValue)
68{
71}
72
73std::unique_ptr<Popup> Popup::createConfirm(const WString& message)
74{
75 return std::make_unique<Popup>(Type::Confirm, message, std::string());
76}
77
78std::unique_ptr<Popup> Popup::createAlert(const WString& message)
79{
80 return std::make_unique<Popup>(Type::Alert, message, std::string());
81}
82
83std::unique_ptr<Popup> Popup::createPrompt(const WString& message,
84 const std::string defaultValue)
85{
86 return std::make_unique<Popup>(Type::Prompt, message, defaultValue);
87}
void setMessage(const WString &message)
Change the message.
Definition: Popup.C:61
JSignal cancelPressed_
Definition: Popup.h:89
static std::unique_ptr< Popup > createConfirm(const WString &message)
Create a confirm dialog.
Definition: Popup.C:73
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:70
JSlot show
Show the dialog.
Definition: Popup.h:77
Popup(Type t, const WString &message, const std::string defaultValue)
Popup constructor.
Definition: Popup.C:14
Type t_
Definition: Popup.h:91
static std::unique_ptr< Popup > createAlert(const WString &message)
Create an alert dialog.
Definition: Popup.C:78
WString message_
Definition: Popup.h:92
void setDefaultValue(const std::string defaultValue)
Change the default value for a prompt dialog.
Definition: Popup.C:67
const WString & message() const
Get the current message.
Definition: Popup.h:66
static std::unique_ptr< Popup > createPrompt(const WString &message, const std::string defaultValue)
Create a prompt dialog with the given default value.
Definition: Popup.C:83
std::string defaultValue_
Definition: Popup.h:93
Type
Popup type.
Definition: Popup.h:37
@ Alert
Definition: Popup.h:37
@ Prompt
Definition: Popup.h:37
@ Confirm
Definition: Popup.h:37
JSignal< std::string > okPressed_
Definition: Popup.h:88
void setJavaScript()
Update the javascript code.
Definition: Popup.C:25

Generated on Sat Nov 5 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.5