MessageBox.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // Copyright (c) 2008-2013 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 "Button.h"
  24. #include "Context.h"
  25. #include "Graphics.h"
  26. #include "MessageBox.h"
  27. #include "ResourceCache.h"
  28. #include "Text.h"
  29. #include "UI.h"
  30. #include "UIEvents.h"
  31. #include "Window.h"
  32. #include "XMLFile.h"
  33. namespace Urho3D
  34. {
  35. MessageBox::MessageBox(Context* context, const String& messageString, const String& titleString, XMLFile* layoutFile, XMLFile* styleFile) :
  36. Object(context)
  37. {
  38. // If layout file is not given, use the default message box layout
  39. if (!layoutFile)
  40. {
  41. ResourceCache* cache = GetSubsystem<ResourceCache>();
  42. layoutFile = cache->GetResource<XMLFile>("UI/MessageBox.xml");
  43. }
  44. UI* ui = GetSubsystem<UI>();
  45. window_ = ui->LoadLayout(layoutFile, styleFile);
  46. ui->GetRoot()->AddChild(window_);
  47. // Set the title and message strings if they are given
  48. titleText_ = dynamic_cast<Text*>(window_->GetChild("TitleText", true));
  49. if (titleText_ && !titleString.Empty())
  50. titleText_->SetText(titleString);
  51. messageText_ = dynamic_cast<Text*>(window_->GetChild("MessageText", true));
  52. if (messageText_ && !messageString.Empty())
  53. messageText_->SetText(messageString);
  54. // Center window after the message is set
  55. Window* window = dynamic_cast<Window*>(window_.Get());
  56. if (window)
  57. {
  58. Graphics* graphics = GetSubsystem<Graphics>(); // May be null if headless
  59. if (graphics)
  60. {
  61. const IntVector2& size = window->GetSize();
  62. window->SetPosition((graphics->GetWidth() - size.x_) / 2, (graphics->GetHeight() - size.y_) / 2);
  63. }
  64. window->SetModal(true);
  65. }
  66. // Bind the buttons (if any in the loaded UI layout) to event handlers
  67. okButton_ = dynamic_cast<Button*>(window_->GetChild("OkButton", true));
  68. if (okButton_)
  69. {
  70. ui->SetFocusElement(okButton_);
  71. SubscribeToEvent(okButton_, E_RELEASED, HANDLER(MessageBox, HandleMessageAcknowledged));
  72. }
  73. Button* cancelButton = dynamic_cast<Button*>(window_->GetChild("CancelButton", true));
  74. if (cancelButton)
  75. SubscribeToEvent(cancelButton, E_RELEASED, HANDLER(MessageBox, HandleMessageAcknowledged));
  76. Button* closeButton = dynamic_cast<Button*>(window_->GetChild("CloseButton", true));
  77. if (closeButton)
  78. SubscribeToEvent(closeButton, E_RELEASED, HANDLER(MessageBox, HandleMessageAcknowledged));
  79. SubscribeToEvent(window_, E_MODALCHANGED, HANDLER(MessageBox, HandleMessageAcknowledged));
  80. }
  81. MessageBox::~MessageBox()
  82. {
  83. window_->Remove();
  84. }
  85. void MessageBox::RegisterObject(Context* context)
  86. {
  87. context->RegisterFactory<MessageBox>();
  88. }
  89. void MessageBox::SetTitle(const String& text)
  90. {
  91. if (titleText_)
  92. titleText_->SetText(text);
  93. }
  94. void MessageBox::SetMessage(const String& text)
  95. {
  96. if (messageText_)
  97. messageText_->SetText(text);
  98. }
  99. const String& MessageBox::GetTitle() const
  100. {
  101. return titleText_ ? titleText_->GetText() : String::EMPTY;
  102. }
  103. const String& MessageBox::GetMessage() const
  104. {
  105. return messageText_ ? messageText_->GetText() : String::EMPTY;
  106. }
  107. void MessageBox::HandleMessageAcknowledged(StringHash eventType, VariantMap& eventData)
  108. {
  109. using namespace MessageACK;
  110. VariantMap newEventData;
  111. newEventData[P_OK] = eventData[Released::P_ELEMENT] == okButton_;
  112. SendEvent(E_MESSAGEACK, newEventData);
  113. this->ReleaseRef();
  114. }
  115. }