WindowContext.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "WindowContext.h"
  23. #include "Window.h"
  24. #include "Bind.h"
  25. namespace Crown
  26. {
  27. WindowContext::WindowContext(WindowsManager* windowsManager):
  28. mWindowsManager(windowsManager), mAssociatedWindow(NULL)
  29. {
  30. assert(mWindowsManager);
  31. }
  32. WindowContext::~WindowContext()
  33. {
  34. //TODO: Verify, should Actions be deleted too? Memory Leak!
  35. ConverterDictionary::Enumerator e = mRegisteredConverters.getBegin();
  36. while (e.next())
  37. {
  38. delete e.current().value;
  39. }
  40. }
  41. Widget* WindowContext::FindWidgetByName(const Str& name) const
  42. {
  43. assert(mAssociatedWindow);
  44. return mAssociatedWindow->FindChildByName(name);
  45. }
  46. void WindowContext::SetAssociatedWindow(Window* associatedWindow)
  47. {
  48. if (mAssociatedWindow != NULL)
  49. {
  50. Log::E("WindowContext can only be associated to a Window once and for all");
  51. }
  52. else
  53. {
  54. mAssociatedWindow = associatedWindow;
  55. mAssociatedWindow->mWindowContext = this;
  56. //Made the Binding Context default to the WindowContext of the window
  57. mAssociatedWindow->SetBindingContext(this);
  58. }
  59. }
  60. void WindowContext::RegisterAction(const Str& name, ActionDelegate* slot)
  61. {
  62. if (mRegisteredActions.Contains(name))
  63. {
  64. Log::E("WindowContext already has an Action registered with name '" + name + "'");
  65. }
  66. else
  67. {
  68. mRegisteredActions[name] = slot;
  69. }
  70. }
  71. ActionDelegate* WindowContext::GetRegisteredAction(const Str& name)
  72. {
  73. if (mRegisteredActions.Contains(name))
  74. {
  75. //Return a duplicate because the caller will usually pass the delegate to an event, which transfers ownership
  76. //TODO: Verify memory leak. Maybe Duplicate could be avoided
  77. return (ActionDelegate*)mRegisteredActions[name]->Duplicate();
  78. }
  79. return NULL;
  80. }
  81. void WindowContext::RegisterConverter(const Str& name, Converter* converter)
  82. {
  83. if (mRegisteredConverters.Contains(name))
  84. {
  85. Log::E("WindowContext already has a Converter registered with name '" + name + "'");
  86. }
  87. else
  88. {
  89. mRegisteredConverters[name] = converter;
  90. }
  91. }
  92. Converter* WindowContext::GetRegisteredConverter(const Str& name)
  93. {
  94. if (mRegisteredConverters.Contains(name))
  95. {
  96. return mRegisteredConverters[name];
  97. }
  98. return NULL;
  99. }
  100. }