joe_vanandel

layout error demo -Wt Dialog with WGrid

Nov 5th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.29 KB | None | 0 0
  1. ############ DialogExample.h ################################
  2. // This may look like C code, but it's really -*- C++ -*-
  3. /*
  4.  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  5.  *
  6.  * See the LICENSE file for terms of use.
  7.  */
  8. #ifndef DIALOG_EXAMPLE_H_
  9. #define DIALOG_EXAMPLE_H_
  10.  
  11. #include <Wt/WApplication>
  12. #include <Wt/WMessageBox>
  13.  
  14. using namespace Wt;
  15.  
  16. /**
  17.  * \defgroup dialog Dialog example
  18.  */
  19. /*@{*/
  20.  
  21. /*! \brief An example illustrating usage of Dialogs
  22.  */
  23. class DialogExample : public WApplication
  24. {
  25. public:
  26.   /*! \brief Create the example application.
  27.    */
  28.   DialogExample(const WEnvironment& env);
  29.  
  30. private:
  31.   void messageBox1();
  32.   void messageBox2();
  33.   void messageBox3();
  34.   void messageBox4();
  35.   void custom();
  36.   void grid1();
  37.  
  38.   void messageBoxDone(StandardButton result);
  39.  
  40.   void setStatus(const WString& text);
  41.  
  42.   WMessageBox *messageBox_;
  43.   WText *status_;
  44. };
  45.  
  46. /*@}*/
  47.  
  48. #endif // DIALOGEXAMPLE_H_
  49.  
  50. ######################################### DialogExample.C #######################################
  51. /*
  52.  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  53.  *
  54.  * See the LICENSE file for terms of use.
  55.  */
  56. #include <iostream>
  57.  
  58. #include <Wt/WApplication>
  59. #include <Wt/WBreak>
  60. #include <Wt/WContainerWidget>
  61. #include <Wt/WEnvironment>
  62. #include <Wt/WLineEdit>
  63. #include <Wt/WMessageBox>
  64. #include <Wt/WPushButton>
  65. #include <Wt/WText>
  66. #include <Wt/WGridLayout>
  67.  
  68. #include "DialogExample.h"
  69.  
  70. using namespace Wt;
  71.  
  72. DialogExample::DialogExample(const WEnvironment& env)
  73.   : WApplication(env),
  74.     messageBox_(0)
  75. {
  76.   setTitle("Dialog example");
  77.  
  78.   WContainerWidget *textdiv = new WContainerWidget(root());
  79.   textdiv->setStyleClass("text");
  80.  
  81.   new WText("<h2>Wt dialogs example</h2>", textdiv);
  82.   new WText("You can use WMessageBox for simple modal dialog boxes. <br />",
  83.         textdiv);
  84.  
  85.   WContainerWidget *buttons = new WContainerWidget(root());
  86.   buttons->setStyleClass("buttons");
  87.  
  88.   WPushButton *button;
  89.  
  90.   button = new WPushButton("One liner", buttons);
  91.   button->clicked().connect(this, &DialogExample::messageBox1);
  92.  
  93.   button = new WPushButton("Comfortable ?", buttons);
  94.   button->clicked().connect(this, &DialogExample::messageBox2);
  95.  
  96.   button = new WPushButton("Havoc!", buttons);
  97.   button->clicked().connect(this, &DialogExample::messageBox3);
  98.  
  99.   button = new WPushButton("Discard", buttons);
  100.   button->clicked().connect(this, &DialogExample::messageBox4);
  101.  
  102.   button = new WPushButton("Familiar", buttons);
  103.   button->clicked().connect(this, &DialogExample::custom);
  104.  
  105.   button = new WPushButton("Grid", buttons);
  106.   button->clicked().connect(this, &DialogExample::grid1);
  107.  
  108.   textdiv = new WContainerWidget(root());
  109.   textdiv->setStyleClass("text");
  110.  
  111.   status_ = new WText("Go ahead...", textdiv);
  112.  
  113.   styleSheet().addRule(".buttons",
  114.                "padding: 5px;");
  115.   styleSheet().addRule(".buttons BUTTON",
  116.                "padding-left: 4px; padding-right: 4px;"
  117.                "margin-top: 4px; display: block");
  118.  
  119.   // avoid scrollbar problems
  120.   styleSheet().addRule(".text", "padding: 4px 8px");
  121.   styleSheet().addRule("body", "margin: 0px;");
  122. }
  123.  
  124. void DialogExample::messageBox1()
  125. {
  126.   WMessageBox::show("Information",
  127.             "Enjoy displaying messages with a one-liner.", Ok);
  128.   setStatus("Ok'ed");
  129. }
  130.  
  131. void DialogExample::messageBox2()
  132. {
  133.   messageBox_
  134.     = new WMessageBox("Question",
  135.               "Are you getting comfortable ?",
  136.               NoIcon, Yes | No | Cancel);
  137.  
  138.   messageBox_
  139.     ->buttonClicked().connect(this, &DialogExample::messageBoxDone);
  140.  
  141.   messageBox_->animateShow
  142.     (WAnimation(WAnimation::Pop | WAnimation::Fade, WAnimation::Linear, 250));
  143. }
  144.  
  145. void DialogExample::messageBox3()
  146. {
  147.   StandardButton
  148.     result = WMessageBox::show("Confirm", "About to wreak havoc... Continue ?",
  149.                    Ok | Cancel,
  150.                    WAnimation(WAnimation::SlideInFromTop));
  151.  
  152.   if (result == Ok)
  153.     setStatus("Wreaking havoc.");
  154.   else
  155.     setStatus("Cancelled!");
  156. }
  157.  
  158. void DialogExample::messageBox4()
  159. {
  160.   messageBox_
  161.     = new WMessageBox("Your work",
  162.               "Your work is not saved",
  163.               NoIcon, NoButton);
  164.  
  165.   messageBox_->addButton("Cancel modifications", Ok);
  166.   messageBox_->addButton("Continue modifying work", Cancel);
  167.  
  168.   messageBox_
  169.     ->buttonClicked().connect(this, &DialogExample::messageBoxDone);
  170.  
  171.   messageBox_->setOffsets(0, Bottom);
  172.  
  173.   messageBox_->animateShow
  174.     (WAnimation(WAnimation::SlideInFromBottom
  175.         | WAnimation::Fade, WAnimation::Linear, 250));
  176. }
  177.  
  178. void DialogExample::messageBoxDone(StandardButton result)
  179. {
  180.   switch (result) {
  181.   case Ok:
  182.     setStatus("Ok'ed"); break;
  183.   case Cancel:
  184.     setStatus("Cancelled!"); break;
  185.   case Yes:
  186.     setStatus("Me too!"); break;
  187.   case No:
  188.     setStatus("Me neither!"); break;
  189.   default:
  190.     setStatus("Unkonwn result?");
  191.   }
  192.  
  193.   delete messageBox_;
  194.   messageBox_ = 0;
  195. }
  196.  
  197. void DialogExample::custom()
  198. {
  199.   WDialog dialog("Personalia");
  200.   dialog.setClosable(true);
  201.   dialog.setResizable(true);
  202.  
  203.   new WText("Enter your name: ", dialog.contents());
  204.   WLineEdit edit(dialog.contents());
  205.   new WBreak(dialog.contents());
  206.   WPushButton ok("Ok", dialog.contents());
  207.  
  208.   edit.setFocus();
  209.  
  210.   edit.enterPressed().connect(&dialog, &WDialog::accept);
  211.   ok.clicked().connect(&dialog, &WDialog::accept);
  212.  
  213.   if (dialog.exec() == WDialog::Accepted) {
  214.     setStatus("Welcome, " + edit.text());
  215.   }
  216. }
  217. WLineEdit *labeledLineEntry(WGridLayout *grid, const std::string& label,
  218. int &row, int col, const std::string & placeholder ,int rowSpan, int colSpan)
  219. {
  220.   WText *lbl = new WText(label);
  221.   grid->addWidget(lbl, row, col, rowSpan, colSpan);
  222.   WLineEdit *le = new WLineEdit(placeholder);
  223.   grid->addWidget(le, row, col+colSpan);
  224.   row += rowSpan;
  225.   return le;
  226. }
  227. void DialogExample::grid1() {
  228.   WDialog dialog("grid1");
  229.   WContainerWidget *container = new WContainerWidget(dialog.contents());
  230.  
  231.   dialog.setWindowTitle("Please Login ");
  232.   dialog.setClosable(true);
  233.   dialog.setResizable(true);
  234.   dialog.setMinimumSize(550, 350);
  235.  
  236.   WGridLayout *grid = new WGridLayout();
  237.   container->setLayout(grid );
  238.  
  239.   const int col = 0;
  240.  
  241.   int row = 0;
  242.   const int colSpan = 2;
  243.   WLineEdit *idLE = labeledLineEntry(grid, "Id (e.g. admin)", row, col, "----", 1, colSpan);
  244.   WLineEdit *passwdLE = labeledLineEntry(grid, "Password", row, col, "----", 1, colSpan);
  245.   passwdLE->setEchoMode(WLineEdit::Password);
  246.   WLineEdit *nameLE_ = labeledLineEntry(grid, "User Name (e.g. Joe Smith)", row, col, "----", 1, colSpan);
  247.   WLineEdit *locationLE = labeledLineEntry(grid, "Location (e.g. Firestone)", row, col, "----", 1, colSpan);
  248.   WLineEdit *commentLE_ = labeledLineEntry(grid, "Comment", row, col, "----", 1, colSpan);
  249.  
  250.   row += 3;
  251.   WPushButton *ok = new WPushButton("OK");
  252.   WPushButton *cancel = new WPushButton("Cancel");
  253.  
  254.   grid->addWidget(cancel, row, col+1);
  255.   grid->addWidget(ok, row, col+2);
  256.  
  257.   ok->clicked().connect(SLOT(&dialog, WDialog::accept));
  258.   cancel->clicked().connect(SLOT(&dialog, WDialog::reject));
  259.  
  260.   if (dialog.exec() == WDialog::Accepted) {
  261.     setStatus("grid login: " + idLE->text());
  262.   }
  263. }
  264.  
  265. void DialogExample::setStatus(const WString& result)
  266. {
  267.   status_->setText(result);
  268. }
  269.  
  270. WApplication *createApplication(const WEnvironment& env)
  271. {
  272.   return new DialogExample(env);
  273. }
  274.  
  275. int main(int argc, char **argv)
  276. {
  277.    return WRun(argc, argv, &createApplication);
  278. }
Advertisement
Add Comment
Please, Sign In to add comment