Wt examples  3.3.6
Public Member Functions | Static Public Member Functions | Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members

A JavaScript based popup window, encapsulating the Javascript functions alert(), confirm(), and prompt(). More...

#include <Popup.h>

Inheritance diagram for Popup:
Inheritance graph
[legend]

Public Member Functions

void setMessage (const WString &message)
 Change the message. More...
 
void setDefaultValue (const std::string defaultValue)
 Change the default value for a prompt dialog. More...
 
const WString & message () const
 Get the current message. More...
 
const std::string & defaultValue () const
 Get the default value for a prompt dialog. More...
 
JSignal< std::string > & okPressed ()
 Signal emitted when ok pressed. More...
 
JSignal< void > & cancelPressed ()
 Signal emitted when cancel is pressed. More...
 

Static Public Member Functions

static PopupcreateConfirm (const WString &message, WObject *parent=0)
 Create a confirm dialog. More...
 
static PopupcreatePrompt (const WString &message, const std::string defaultValue, WObject *parent=0)
 Create a prompt dialog with the given default value. More...
 
static PopupcreateAlert (const WString &message, WObject *parent=0)
 Create an alert dialog. More...
 

Public Attributes

JSlot show
 Show the dialog. More...
 

Private Types

enum  Type { Confirm, Alert, Prompt }
 Popup type. More...
 

Private Member Functions

 Popup (Type t, const WString &message, const std::string defaultValue, WObject *parent)
 Popup constructor. More...
 
void setJavaScript ()
 Update the javascript code. More...
 

Private Attributes

JSignal< std::string > okPressed_
 
JSignal< void > cancelPressed_
 
Type t_
 
WString message_
 
std::string defaultValue_
 

Detailed Description

A JavaScript based popup window, encapsulating the Javascript functions alert(), confirm(), and prompt().

Use one of the create static methods to create a popup. This will not display the popup, until either the show slot is triggered from an event handler, or is executed using it's exec() method.

When the user closes the popup, either the okPressed or cancelPressed signal is emitted. For a prompt dialog, the value is passed as a parameter to the okPressed signal.

Definition at line 32 of file Popup.h.

Member Enumeration Documentation

◆ Type

enum Popup::Type
private

Popup type.

Enumerator
Confirm 
Alert 
Prompt 

Definition at line 83 of file Popup.h.

Constructor & Destructor Documentation

◆ Popup()

Popup::Popup ( Type  t,
const WString &  message,
const std::string  defaultValue,
WObject *  parent 
)
private

Popup constructor.

Definition at line 12 of file Popup.C.

14  : WObject(parent),
15  okPressed_(this, "ok"),
16  cancelPressed_(this, "cancel"),
17  t_(t),
20 {
21  setJavaScript();
22 }
WString message_
Definition: Popup.h:94
void setJavaScript()
Update the javascript code.
Definition: Popup.C:24
std::string defaultValue_
Definition: Popup.h:95
JSignal< void > cancelPressed_
Definition: Popup.h:91
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

Member Function Documentation

◆ cancelPressed()

JSignal<void>& Popup::cancelPressed ( )
inline

Signal emitted when cancel is pressed.

Definition at line 78 of file Popup.h.

78 { return cancelPressed_; }
JSignal< void > cancelPressed_
Definition: Popup.h:91

◆ createAlert()

Popup * Popup::createAlert ( const WString &  message,
WObject *  parent = 0 
)
static

Create an alert dialog.

Definition at line 77 of file Popup.C.

78 {
79  return new Popup(Alert, message, std::string(), parent);
80 }
const WString & message() const
Get the current message.
Definition: Popup.h:59
Popup(Type t, const WString &message, const std::string defaultValue, WObject *parent)
Popup constructor.
Definition: Popup.C:12

◆ createConfirm()

Popup * Popup::createConfirm ( const WString &  message,
WObject *  parent = 0 
)
static

Create a confirm dialog.

Definition at line 72 of file Popup.C.

73 {
74  return new Popup(Confirm, message, std::string(), parent);
75 }
const WString & message() const
Get the current message.
Definition: Popup.h:59
Popup(Type t, const WString &message, const std::string defaultValue, WObject *parent)
Popup constructor.
Definition: Popup.C:12

◆ createPrompt()

Popup * Popup::createPrompt ( const WString &  message,
const std::string  defaultValue,
WObject *  parent = 0 
)
static

Create a prompt dialog with the given default value.

Definition at line 82 of file Popup.C.

84 {
85  return new Popup(Prompt, message, defaultValue, parent);
86 }
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
Popup(Type t, const WString &message, const std::string defaultValue, WObject *parent)
Popup constructor.
Definition: Popup.C:12

◆ defaultValue()

const std::string& Popup::defaultValue ( ) const
inline

Get the default value for a prompt dialog.

Definition at line 63 of file Popup.h.

63 { return defaultValue_; }
std::string defaultValue_
Definition: Popup.h:95

◆ message()

const WString& Popup::message ( ) const
inline

Get the current message.

Definition at line 59 of file Popup.h.

59 { return message_; }
WString message_
Definition: Popup.h:94

◆ okPressed()

JSignal<std::string>& Popup::okPressed ( )
inline

Signal emitted when ok pressed.

Definition at line 74 of file Popup.h.

74 { return okPressed_; }
JSignal< std::string > okPressed_
Definition: Popup.h:90

◆ setDefaultValue()

void Popup::setDefaultValue ( const std::string  defaultValue)

Change the default value for a prompt dialog.

Definition at line 66 of file Popup.C.

67 {
69  setJavaScript();
70 }
void setJavaScript()
Update the javascript code.
Definition: Popup.C:24
std::string defaultValue_
Definition: Popup.h:95
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition: Popup.h:63

◆ setJavaScript()

void Popup::setJavaScript ( )
private

Update the javascript code.

Definition at line 24 of file Popup.C.

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:
35  show.setJavaScript
36  ("function(){ if (confirm('" + message_.narrow() + "')) {"
37  + okPressed_.createCall("''") +
38  "} else {"
39  + cancelPressed_.createCall() +
40  "}}");
41  break;
42  case Alert:
43  show.setJavaScript
44  ("function(){ alert('" + message_.narrow() + "');"
45  + okPressed_.createCall("''") +
46  "}");
47  break;
48  case Prompt:
49  show.setJavaScript
50  ("function(){var n = prompt('" + message_.narrow() + "', '"
51  + defaultValue_ + "');"
52  "if (n != null) {"
53  + okPressed_.createCall("n") +
54  "} else {"
55  + cancelPressed_.createCall() +
56  "}}");
57  }
58 }
JSlot show
Show the dialog.
Definition: Popup.h:70
WString message_
Definition: Popup.h:94
std::string defaultValue_
Definition: Popup.h:95
JSignal< void > cancelPressed_
Definition: Popup.h:91
Type t_
Definition: Popup.h:93
JSignal< std::string > okPressed_
Definition: Popup.h:90

◆ setMessage()

void Popup::setMessage ( const WString &  message)

Change the message.

Definition at line 60 of file Popup.C.

61 {
62  message_ = message;
63  setJavaScript();
64 }
WString message_
Definition: Popup.h:94
void setJavaScript()
Update the javascript code.
Definition: Popup.C:24
const WString & message() const
Get the current message.
Definition: Popup.h:59

Member Data Documentation

◆ cancelPressed_

JSignal<void> Popup::cancelPressed_
private

Definition at line 91 of file Popup.h.

◆ defaultValue_

std::string Popup::defaultValue_
private

Definition at line 95 of file Popup.h.

◆ message_

WString Popup::message_
private

Definition at line 94 of file Popup.h.

◆ okPressed_

JSignal<std::string> Popup::okPressed_
private

Definition at line 90 of file Popup.h.

◆ show

JSlot Popup::show

Show the dialog.

Use show.exec() to show the dialog, or connect the slot to an EventSignal to directly show the dialog without a server round trip.

Definition at line 70 of file Popup.h.

◆ t_

Type Popup::t_
private

Definition at line 93 of file Popup.h.


The documentation for this class was generated from the following files:

Generated on Thu Jan 12 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.13