Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############ DialogExample.h ################################
- // This may look like C code, but it's really -*- C++ -*-
- /*
- * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
- *
- * See the LICENSE file for terms of use.
- */
- #ifndef DIALOG_EXAMPLE_H_
- #define DIALOG_EXAMPLE_H_
- #include <Wt/WApplication>
- #include <Wt/WMessageBox>
- using namespace Wt;
- /**
- * \defgroup dialog Dialog example
- */
- /*@{*/
- /*! \brief An example illustrating usage of Dialogs
- */
- class DialogExample : public WApplication
- {
- public:
- /*! \brief Create the example application.
- */
- DialogExample(const WEnvironment& env);
- private:
- void messageBox1();
- void messageBox2();
- void messageBox3();
- void messageBox4();
- void custom();
- void grid1();
- void messageBoxDone(StandardButton result);
- void setStatus(const WString& text);
- WMessageBox *messageBox_;
- WText *status_;
- };
- /*@}*/
- #endif // DIALOGEXAMPLE_H_
- ######################################### DialogExample.C #######################################
- /*
- * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
- *
- * See the LICENSE file for terms of use.
- */
- #include <iostream>
- #include <Wt/WApplication>
- #include <Wt/WBreak>
- #include <Wt/WContainerWidget>
- #include <Wt/WEnvironment>
- #include <Wt/WLineEdit>
- #include <Wt/WMessageBox>
- #include <Wt/WPushButton>
- #include <Wt/WText>
- #include <Wt/WGridLayout>
- #include "DialogExample.h"
- using namespace Wt;
- DialogExample::DialogExample(const WEnvironment& env)
- : WApplication(env),
- messageBox_(0)
- {
- setTitle("Dialog example");
- WContainerWidget *textdiv = new WContainerWidget(root());
- textdiv->setStyleClass("text");
- new WText("<h2>Wt dialogs example</h2>", textdiv);
- new WText("You can use WMessageBox for simple modal dialog boxes. <br />",
- textdiv);
- WContainerWidget *buttons = new WContainerWidget(root());
- buttons->setStyleClass("buttons");
- WPushButton *button;
- button = new WPushButton("One liner", buttons);
- button->clicked().connect(this, &DialogExample::messageBox1);
- button = new WPushButton("Comfortable ?", buttons);
- button->clicked().connect(this, &DialogExample::messageBox2);
- button = new WPushButton("Havoc!", buttons);
- button->clicked().connect(this, &DialogExample::messageBox3);
- button = new WPushButton("Discard", buttons);
- button->clicked().connect(this, &DialogExample::messageBox4);
- button = new WPushButton("Familiar", buttons);
- button->clicked().connect(this, &DialogExample::custom);
- button = new WPushButton("Grid", buttons);
- button->clicked().connect(this, &DialogExample::grid1);
- textdiv = new WContainerWidget(root());
- textdiv->setStyleClass("text");
- status_ = new WText("Go ahead...", textdiv);
- styleSheet().addRule(".buttons",
- "padding: 5px;");
- styleSheet().addRule(".buttons BUTTON",
- "padding-left: 4px; padding-right: 4px;"
- "margin-top: 4px; display: block");
- // avoid scrollbar problems
- styleSheet().addRule(".text", "padding: 4px 8px");
- styleSheet().addRule("body", "margin: 0px;");
- }
- void DialogExample::messageBox1()
- {
- WMessageBox::show("Information",
- "Enjoy displaying messages with a one-liner.", Ok);
- setStatus("Ok'ed");
- }
- void DialogExample::messageBox2()
- {
- messageBox_
- = new WMessageBox("Question",
- "Are you getting comfortable ?",
- NoIcon, Yes | No | Cancel);
- messageBox_
- ->buttonClicked().connect(this, &DialogExample::messageBoxDone);
- messageBox_->animateShow
- (WAnimation(WAnimation::Pop | WAnimation::Fade, WAnimation::Linear, 250));
- }
- void DialogExample::messageBox3()
- {
- StandardButton
- result = WMessageBox::show("Confirm", "About to wreak havoc... Continue ?",
- Ok | Cancel,
- WAnimation(WAnimation::SlideInFromTop));
- if (result == Ok)
- setStatus("Wreaking havoc.");
- else
- setStatus("Cancelled!");
- }
- void DialogExample::messageBox4()
- {
- messageBox_
- = new WMessageBox("Your work",
- "Your work is not saved",
- NoIcon, NoButton);
- messageBox_->addButton("Cancel modifications", Ok);
- messageBox_->addButton("Continue modifying work", Cancel);
- messageBox_
- ->buttonClicked().connect(this, &DialogExample::messageBoxDone);
- messageBox_->setOffsets(0, Bottom);
- messageBox_->animateShow
- (WAnimation(WAnimation::SlideInFromBottom
- | WAnimation::Fade, WAnimation::Linear, 250));
- }
- void DialogExample::messageBoxDone(StandardButton result)
- {
- switch (result) {
- case Ok:
- setStatus("Ok'ed"); break;
- case Cancel:
- setStatus("Cancelled!"); break;
- case Yes:
- setStatus("Me too!"); break;
- case No:
- setStatus("Me neither!"); break;
- default:
- setStatus("Unkonwn result?");
- }
- delete messageBox_;
- messageBox_ = 0;
- }
- void DialogExample::custom()
- {
- WDialog dialog("Personalia");
- dialog.setClosable(true);
- dialog.setResizable(true);
- new WText("Enter your name: ", dialog.contents());
- WLineEdit edit(dialog.contents());
- new WBreak(dialog.contents());
- WPushButton ok("Ok", dialog.contents());
- edit.setFocus();
- edit.enterPressed().connect(&dialog, &WDialog::accept);
- ok.clicked().connect(&dialog, &WDialog::accept);
- if (dialog.exec() == WDialog::Accepted) {
- setStatus("Welcome, " + edit.text());
- }
- }
- WLineEdit *labeledLineEntry(WGridLayout *grid, const std::string& label,
- int &row, int col, const std::string & placeholder ,int rowSpan, int colSpan)
- {
- WText *lbl = new WText(label);
- grid->addWidget(lbl, row, col, rowSpan, colSpan);
- WLineEdit *le = new WLineEdit(placeholder);
- grid->addWidget(le, row, col+colSpan);
- row += rowSpan;
- return le;
- }
- void DialogExample::grid1() {
- WDialog dialog("grid1");
- WContainerWidget *container = new WContainerWidget(dialog.contents());
- dialog.setWindowTitle("Please Login ");
- dialog.setClosable(true);
- dialog.setResizable(true);
- dialog.setMinimumSize(550, 350);
- WGridLayout *grid = new WGridLayout();
- container->setLayout(grid );
- const int col = 0;
- int row = 0;
- const int colSpan = 2;
- WLineEdit *idLE = labeledLineEntry(grid, "Id (e.g. admin)", row, col, "----", 1, colSpan);
- WLineEdit *passwdLE = labeledLineEntry(grid, "Password", row, col, "----", 1, colSpan);
- passwdLE->setEchoMode(WLineEdit::Password);
- WLineEdit *nameLE_ = labeledLineEntry(grid, "User Name (e.g. Joe Smith)", row, col, "----", 1, colSpan);
- WLineEdit *locationLE = labeledLineEntry(grid, "Location (e.g. Firestone)", row, col, "----", 1, colSpan);
- WLineEdit *commentLE_ = labeledLineEntry(grid, "Comment", row, col, "----", 1, colSpan);
- row += 3;
- WPushButton *ok = new WPushButton("OK");
- WPushButton *cancel = new WPushButton("Cancel");
- grid->addWidget(cancel, row, col+1);
- grid->addWidget(ok, row, col+2);
- ok->clicked().connect(SLOT(&dialog, WDialog::accept));
- cancel->clicked().connect(SLOT(&dialog, WDialog::reject));
- if (dialog.exec() == WDialog::Accepted) {
- setStatus("grid login: " + idLE->text());
- }
- }
- void DialogExample::setStatus(const WString& result)
- {
- status_->setText(result);
- }
- WApplication *createApplication(const WEnvironment& env)
- {
- return new DialogExample(env);
- }
- int main(int argc, char **argv)
- {
- return WRun(argc, argv, &createApplication);
- }
Advertisement
Add Comment
Please, Sign In to add comment