Wt examples
3.7.1
public-git
wt
examples
javascript
JavascriptExample.C
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
3
*
4
* See the LICENSE file for terms of use.
5
*/
6
#include <iostream>
7
8
#include <Wt/WApplication>
9
#include <Wt/WBreak>
10
#include <Wt/WContainerWidget>
11
#include <Wt/WText>
12
#include <Wt/WPushButton>
13
14
#include "
JavascriptExample.h
"
15
#include "
Popup.h
"
16
17
using namespace
Wt
;
18
19
JavascriptExample::JavascriptExample
(
const
WEnvironment
& env)
20
:
WApplication
(env)
21
{
22
setTitle
(
"Javascript example"
);
23
24
// Create a popup for prompting the amount of money, and connect the
25
// okPressed button to the slot for setting the amount of money.
26
//
27
// Note that the input provided by the user in the prompt box is passed as
28
// an argument to the slot.
29
promptAmount_
=
Popup::createPrompt
(
"How much do you want to pay?"
,
""
,
30
this
);
31
promptAmount_
->
okPressed
().
connect
(
this
, &
JavascriptExample::setAmount
);
32
33
// Create a popup for confirming the payment.
34
//
35
// Since a confirm popup does not allow input, we ignore the
36
// argument carrying the input (which will be empty anyway).
37
confirmPay_
=
Popup::createConfirm
(
""
,
this
);
38
confirmPay_
->
okPressed
().
connect
(
this
, &
JavascriptExample::confirmed
);
39
40
new
WText
(
"<h2>Wt Javascript example</h2>"
41
"<p>Wt makes abstraction of Javascript, and therefore allows you"
42
" to develop web applications without any knowledge of Javascript,"
43
" and which are not dependent on Javascript."
44
" However, Wt does allow you to add custom Javascript code:</p>"
45
" <ul>"
46
" <li>To call custom JavaScript code from an event handler, "
47
"connect the Wt::EventSignal to a Wt::JSlot.</li>"
48
" <li>To call C++ code from custom JavaScript, use "
49
"Wt.emit() to emit a Wt::JSignal.</li>"
50
" <li>To call custom JavaScript code from C++, use "
51
"WApplication::doJavascript() or Wt::JSlot::exec().</li>"
52
" </ul>"
53
"<p>This simple application shows how to interact between C++ and"
54
" JavaScript using the JSlot and JSignal classes.</p>"
,
root
());
55
56
currentAmount_
57
=
new
WText
(
"Current amount: $"
+
promptAmount_
->
defaultValue
(),
root
());
58
59
WPushButton
*amountButton =
new
WPushButton
(
"Change ..."
,
root
());
60
amountButton->setMargin(10,
Left
|
Right
);
61
62
new
WBreak
(
root
());
63
64
WPushButton
*confirmButton =
new
WPushButton
(
"Pay now."
,
root
());
65
confirmButton->
setMargin
(10,
Top
|
Bottom
);
66
67
// Connect the event handlers to a JSlot: this will execute the JavaScript
68
// immediately, without a server round trip.
69
amountButton->clicked().connect(
promptAmount_
->
show
);
70
confirmButton->
clicked
().connect(
confirmPay_
->
show
);
71
72
// Set the initial amount
73
setAmount
(
"1000"
);
74
}
75
76
void
JavascriptExample::setAmount
(
const
std::string amount)
77
{
78
// Change the confirmation message to include the amount.
79
confirmPay_
->
setMessage
(
"Are you sure you want to pay $"
+ amount +
" ?"
);
80
81
// Change the default value for the prompt.
82
promptAmount_
->
setDefaultValue
(amount);
83
84
// Change the text that shows the current amount.
85
currentAmount_
->
setText
(
"Current amount: $"
+
promptAmount_
->
defaultValue
());
86
}
87
88
void
JavascriptExample::confirmed
()
89
{
90
new
WText
(
"<br/>Just payed $"
+
promptAmount_
->
defaultValue
() +
"."
,
root
());
91
}
92
93
WApplication
*
createApplication
(
const
WEnvironment
& env)
94
{
95
return
new
JavascriptExample
(env);
96
}
97
98
int
main
(
int
argc,
char
**argv)
99
{
100
return
WRun(argc, argv, &
createApplication
);
101
}
102
Popup.h
JavascriptExample::promptAmount_
Popup * promptAmount_
Popup for changing the amount.
Definition:
JavascriptExample.h:42
Popup::createConfirm
static Popup * createConfirm(const WString &message, WObject *parent=0)
Create a confirm dialog.
Definition:
Popup.C:72
JavascriptExample::confirmed
void confirmed()
The user has confirmed the payment.
Definition:
JavascriptExample.C:88
Key::Left
Popup::show
JSlot show
Show the dialog.
Definition:
Popup.h:70
JavascriptExample::currentAmount_
WText * currentAmount_
WText for showing the current amount.
Definition:
JavascriptExample.h:50
Wt::WInteractWidget::clicked
EventSignal< WMouseEvent > & clicked()
Wt::WEnvironment
main
int main(int argc, char **argv)
Definition:
JavascriptExample.C:98
Key::Right
Wt::JSignal::connect
Wt::Signals::connection connect(F function)
Wt::Side::Top
Wt::WText
Wt::WApplication::setTitle
void setTitle(const WString &title)
Wt::WApplication
JavascriptExample.h
Wt::WText::setText
bool setText(const WString &text)
Wt::WWebWidget::setMargin
virtual void setMargin(const WLength &margin, WFlags< Side > sides=AllSides) override
JavascriptExample::setAmount
void setAmount(std::string amount)
Set the amount to be payed.
Definition:
JavascriptExample.C:76
Wt::WPushButton
Popup::setDefaultValue
void setDefaultValue(const std::string defaultValue)
Change the default value for a prompt dialog.
Definition:
Popup.C:66
Wt::WApplication::root
WContainerWidget * root() const
Popup::createPrompt
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
Wt::Side::Bottom
Popup::setMessage
void setMessage(const WString &message)
Change the message.
Definition:
Popup.C:60
Wt
Popup::okPressed
JSignal< std::string > & okPressed()
Signal emitted when ok pressed.
Definition:
Popup.h:74
Popup::defaultValue
const std::string & defaultValue() const
Get the default value for a prompt dialog.
Definition:
Popup.h:63
createApplication
WApplication * createApplication(const WEnvironment &env)
Definition:
JavascriptExample.C:93
JavascriptExample::confirmPay_
Popup * confirmPay_
Popup for paying.
Definition:
JavascriptExample.h:46
JavascriptExample::JavascriptExample
JavascriptExample(const WEnvironment &env)
Create the example application.
Definition:
JavascriptExample.C:19
Wt::WBreak
Generated on Tue Dec 15 2020 for
the C++ Web Toolkit (Wt)
by
1.8.13