MessageBox.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/Log.h"
  25. #include "../Resource/ResourceCache.h"
  26. #include "../Resource/XMLFile.h"
  27. #include "../UI/Button.h"
  28. #include "../UI/MessageBox.h"
  29. #include "../UI/Text.h"
  30. #include "../UI/UI.h"
  31. #include "../UI/UIEvents.h"
  32. #include "../UI/Window.h"
  33. namespace Urho3D
  34. {
  35. MessageBox::MessageBox(Context* context, const String& messageString, const String& titleString, XMLFile* layoutFile,
  36. XMLFile* styleFile) :
  37. Object(context),
  38. window_(0),
  39. titleText_(0),
  40. messageText_(0),
  41. okButton_(0)
  42. {
  43. // If layout file is not given, use the default message box layout
  44. if (!layoutFile)
  45. {
  46. ResourceCache* cache = GetSubsystem<ResourceCache>();
  47. layoutFile = cache->GetResource<XMLFile>("UI/MessageBox.xml");
  48. if (!layoutFile) // Error is already logged
  49. return; // Note: windowless MessageBox should not be used!
  50. }
  51. UI* ui = GetSubsystem<UI>();
  52. UIElement* root = ui->GetRoot();
  53. {
  54. SharedPtr<UIElement> holder = ui->LoadLayout(layoutFile, styleFile);
  55. if (!holder) // Error is already logged
  56. return;
  57. window_ = holder;
  58. root->AddChild(window_); // Take ownership of the object before SharedPtr goes out of scope
  59. }
  60. // Set the title and message strings if they are given
  61. titleText_ = window_->GetChildDynamicCast<Text>("TitleText", true);
  62. if (titleText_ && !titleString.Empty())
  63. titleText_->SetText(titleString);
  64. messageText_ = window_->GetChildDynamicCast<Text>("MessageText", true);
  65. if (messageText_ && !messageString.Empty())
  66. messageText_->SetText(messageString);
  67. // Center window after the message is set
  68. Window* window = dynamic_cast<Window*>(window_);
  69. if (window)
  70. {
  71. const IntVector2& size = window->GetSize();
  72. window->SetPosition((root->GetWidth() - size.x_) / 2, (root->GetHeight() - size.y_) / 2);
  73. window->SetModal(true);
  74. SubscribeToEvent(window, E_MODALCHANGED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
  75. }
  76. // Bind the buttons (if any in the loaded UI layout) to event handlers
  77. okButton_ = window_->GetChildDynamicCast<Button>("OkButton", true);
  78. if (okButton_)
  79. {
  80. ui->SetFocusElement(okButton_);
  81. SubscribeToEvent(okButton_, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
  82. }
  83. Button* cancelButton = window_->GetChildDynamicCast<Button>("CancelButton", true);
  84. if (cancelButton)
  85. SubscribeToEvent(cancelButton, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
  86. Button* closeButton = window_->GetChildDynamicCast<Button>("CloseButton", true);
  87. if (closeButton)
  88. SubscribeToEvent(closeButton, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
  89. // Increase reference count to keep Self alive
  90. AddRef();
  91. }
  92. MessageBox::~MessageBox()
  93. {
  94. // This would remove the UI-element regardless of whether it is parented to UI's root or UI's modal-root
  95. if (window_)
  96. window_->Remove();
  97. }
  98. void MessageBox::RegisterObject(Context* context)
  99. {
  100. context->RegisterFactory<MessageBox>();
  101. }
  102. void MessageBox::SetTitle(const String& text)
  103. {
  104. if (titleText_)
  105. titleText_->SetText(text);
  106. }
  107. void MessageBox::SetMessage(const String& text)
  108. {
  109. if (messageText_)
  110. messageText_->SetText(text);
  111. }
  112. const String& MessageBox::GetTitle() const
  113. {
  114. return titleText_ ? titleText_->GetText() : String::EMPTY;
  115. }
  116. const String& MessageBox::GetMessage() const
  117. {
  118. return messageText_ ? messageText_->GetText() : String::EMPTY;
  119. }
  120. void MessageBox::HandleMessageAcknowledged(StringHash eventType, VariantMap& eventData)
  121. {
  122. using namespace MessageACK;
  123. VariantMap& newEventData = GetEventDataMap();
  124. newEventData[P_OK] = eventData[Released::P_ELEMENT] == okButton_;
  125. SendEvent(E_MESSAGEACK, newEventData);
  126. // Self destruct
  127. ReleaseRef();
  128. }
  129. }