Popup.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // This may look like C code, but it's really -*- C++ -*-
  2. /*
  3. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  4. *
  5. * See the LICENSE file for terms of use.
  6. */
  7. #ifndef POPUP_H_
  8. #define POPUP_H_
  9. #include <Wt/WObject>
  10. #include <Wt/WString>
  11. #include <Wt/WJavaScript>
  12. using namespace Wt;
  13. /**
  14. * @addtogroup javascript
  15. */
  16. /*@{*/
  17. /*! \brief A JavaScript based popup window, encapsulating the Javascript
  18. * functions alert(), confirm(), and prompt().
  19. *
  20. * Use one of the create static methods to create a popup. This will not
  21. * display the popup, until either the show slot is triggered from an
  22. * event handler, or is executed using it's exec() method.
  23. *
  24. * When the user closes the popup, either the okPressed or cancelPressed
  25. * signal is emitted. For a prompt dialog, the value is passed as a parameter
  26. * to the okPressed signal.
  27. */
  28. class Popup : public WObject
  29. {
  30. public:
  31. /*! \brief Create a confirm dialog.
  32. */
  33. static Popup *createConfirm(const WString& message, WObject *parent = 0);
  34. /*! \brief Create a prompt dialog with the given default value
  35. */
  36. static Popup *createPrompt(const WString& message,
  37. const std::string defaultValue,
  38. WObject *parent = 0);
  39. /*! \brief Create an alert dialog.
  40. */
  41. static Popup *createAlert(const WString& message, WObject *parent = 0);
  42. /*! \brief Change the message
  43. */
  44. void setMessage(const WString& message);
  45. /*! \brief Change the default value for a prompt dialog.
  46. */
  47. void setDefaultValue(const std::string defaultValue);
  48. /*! \brief Get the current message.
  49. */
  50. const WString& message() const { return message_; }
  51. /*! \brief Get the default value for a prompt dialog.
  52. */
  53. const std::string& defaultValue() const { return defaultValue_; }
  54. /*! \brief Show the dialog.
  55. *
  56. * Use show.exec() to show the dialog, or connect the slot to an EventSignal
  57. * to directly show the dialog without a server round trip.
  58. */
  59. JSlot show;
  60. /*! \brief Signal emitted when ok pressed.
  61. */
  62. JSignal<std::string>& okPressed() { return okPressed_; }
  63. /*! \brief Signal emitted when cancel is pressed.
  64. */
  65. JSignal<void>& cancelPressed() { return cancelPressed_; }
  66. private:
  67. /*! \brief Popup type.
  68. */
  69. enum Type { Confirm, Alert, Prompt };
  70. /*! \brief Popup constructor.
  71. */
  72. Popup(Type t, const WString& message, const std::string defaultValue,
  73. WObject *parent);
  74. JSignal<std::string> okPressed_;
  75. JSignal<void> cancelPressed_;
  76. Type t_;
  77. WString message_;
  78. std::string defaultValue_;
  79. /*! \brief Update the javascript code.
  80. */
  81. void setJavaScript();
  82. };
  83. /*@}*/
  84. #endif // POPUP_H_