BsMacOSWindow.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2017 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Math/BsVector2I.h"
  6. #include <Math/BsRect2I.h>
  7. #ifdef BS_COCOA_INTERNALS
  8. #import <Cocoa/Cocoa.h>
  9. #endif
  10. #ifdef BS_COCOA_INTERNALS
  11. @class BSWindowDelegate;
  12. @class BSWindowListener;
  13. @class BSView;
  14. @class BSWindow;
  15. #endif
  16. namespace bs
  17. {
  18. class CocoaDropTarget;
  19. /** @addtogroup Internal-Utility
  20. * @{
  21. */
  22. /** @addtogroup Platform-Utility-Internal
  23. * @{
  24. */
  25. /** Descriptor used for creating a platform specific native window. */
  26. struct WINDOW_DESC
  27. {
  28. String title;
  29. INT32 x = -1;
  30. INT32 y = -1;
  31. UINT32 width = 20;
  32. UINT32 height = 20;
  33. bool showDecorations = true;
  34. bool allowResize = true;
  35. bool modal = false;
  36. bool floating = false;
  37. SPtr<PixelData> background;
  38. };
  39. /**
  40. * Represents a Cocoa window. Note this class should only be used from the sim thread as Cocoa does not support
  41. * event handling, windows or views outside of the main thread.
  42. */
  43. class BS_UTILITY_EXPORT CocoaWindow
  44. {
  45. public:
  46. #ifdef BS_COCOA_INTERNALS
  47. struct Pimpl
  48. {
  49. NSWindow* window = nil;
  50. BSView* view = nil;
  51. BSWindowListener* responder = nil;
  52. BSWindowDelegate* delegate = nil;
  53. UINT32 numDropTargets = 0;
  54. bool isModal = false;
  55. NSUInteger style = 0;
  56. bool isFullscreen;
  57. NSRect windowedRect;
  58. NSModalSession modalSession = nil;
  59. void* userData = nullptr;
  60. };
  61. #else
  62. struct Pimpl;
  63. #endif
  64. CocoaWindow(const WINDOW_DESC& desc);
  65. ~CocoaWindow();
  66. /** Returns the current area of the window, relative to the top-left origin of the screen. */
  67. Rect2I getArea() const;
  68. /** Hides the window. */
  69. void hide();
  70. /** Shows (unhides) the window. */
  71. void show();
  72. /** Minimizes the window. */
  73. void minimize();
  74. /** Maximizes the window over the entire current screen. */
  75. void maximize();
  76. /** Restores the window to original position and size if it is minimized or maximized. */
  77. void restore();
  78. /** Change the size of the window. */
  79. void resize(UINT32 width, UINT32 height);
  80. /** Reposition the window. */
  81. void move(INT32 left, INT32 top);
  82. /** Switches from fullscreen to windowed mode. */
  83. void setWindowed();
  84. /** Switches from windowed to fullscreen mode. */
  85. void setFullscreen();
  86. /** Converts screen position into window local position. */
  87. Vector2I screenToWindowPos(const Vector2I& screenPos) const;
  88. /** Converts window local position to screen position. */
  89. Vector2I windowToScreenPos(const Vector2I& windowPos) const;
  90. /**
  91. * @name Internal
  92. * @{
  93. */
  94. /**
  95. * Destroys the window, cleaning up any resources and removing it from the display. No further methods should be
  96. * called on this object after it has been destroyed. This called automatically in the destructor.
  97. */
  98. void _destroy();
  99. /** Returns an identifier that unique identifies this window. */
  100. UINT32 _getWindowId() const { return mWindowId; }
  101. /**
  102. * Sets a portion of the window in which the user can click and drag in order to move the window. This is needed
  103. * when window has no title bar, yet you still want to allow the user to drag it by clicking on some specific area
  104. * (e.g. a title bar you manually render).
  105. *
  106. * @param[in] rects Areas of the window (relative to the window origin in top-left corner) in which the drag
  107. * operation in allowed.
  108. */
  109. void _setDragZones(const Vector<Rect2I>& rects);
  110. /** Attaches non-specific user data that can later be retrieved through _getUserData(). */
  111. void _setUserData(void* data);
  112. /** Returns user data attached to the object when _setUserData was called. */
  113. void* _getUserData() const;
  114. /**
  115. * Registers the window with the drag and drop manager and allows it to accept file drop operations. Each call
  116. * to this method must eventually be followed with _unregisterForDragAndDrop.
  117. */
  118. void _registerForDragAndDrop();
  119. /**
  120. * Unregisters the window from the drag and drop manager. This will need to be called multiple times if
  121. * _registerForDragAndDrop was called multiple times.
  122. */
  123. void _unregisterForDragAndDrop();
  124. /** Returns internal private data for use by friends. */
  125. Pimpl* _getPrivateData() const { return m; }
  126. /** @} */
  127. private:
  128. Pimpl* m;
  129. UINT32 mWindowId;
  130. };
  131. /** @} */
  132. /** @} */
  133. }