BsMacOSWindow.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. SPtr<PixelData> background;
  37. };
  38. /**
  39. * Represents a Cocoa window. Note this class should only be used from the sim thread as Cocoa does not support
  40. * event handling, windows or views outside of the main thread.
  41. */
  42. class BS_UTILITY_EXPORT CocoaWindow
  43. {
  44. public:
  45. #ifdef BS_COCOA_INTERNALS
  46. struct Pimpl
  47. {
  48. NSWindow* window = nil;
  49. BSView* view = nil;
  50. BSWindowListener* responder = nil;
  51. BSWindowDelegate* delegate = nil;
  52. UINT32 numDropTargets = 0;
  53. bool isModal = false;
  54. NSUInteger style = 0;
  55. bool isFullscreen;
  56. NSRect windowedRect;
  57. NSModalSession modalSession = nil;
  58. void* userData = nullptr;
  59. };
  60. #else
  61. struct Pimpl;
  62. #endif
  63. CocoaWindow(const WINDOW_DESC& desc);
  64. ~CocoaWindow();
  65. /** Returns the current area of the window, relative to the top-left origin of the screen. */
  66. Rect2I getArea() const;
  67. /** Hides the window. */
  68. void hide();
  69. /** Shows (unhides) the window. */
  70. void show();
  71. /** Minimizes the window. */
  72. void minimize();
  73. /** Maximizes the window over the entire current screen. */
  74. void maximize();
  75. /** Restores the window to original position and size if it is minimized or maximized. */
  76. void restore();
  77. /** Change the size of the window. */
  78. void resize(UINT32 width, UINT32 height);
  79. /** Reposition the window. */
  80. void move(INT32 left, INT32 top);
  81. /** Switches from fullscreen to windowed mode. */
  82. void setWindowed();
  83. /** Switches from windowed to fullscreen mode. */
  84. void setFullscreen();
  85. /** Converts screen position into window local position. */
  86. Vector2I screenToWindowPos(const Vector2I& screenPos) const;
  87. /** Converts window local position to screen position. */
  88. Vector2I windowToScreenPos(const Vector2I& windowPos) const;
  89. /**
  90. * @name Internal
  91. * @{
  92. */
  93. /**
  94. * Destroys the window, cleaning up any resources and removing it from the display. No further methods should be
  95. * called on this object after it has been destroyed. This called automatically in the destructor.
  96. */
  97. void _destroy();
  98. /** Returns an identifier that unique identifies this window. */
  99. UINT32 _getWindowId() const { return mWindowId; }
  100. /**
  101. * Sets a portion of the window in which the user can click and drag in order to move the window. This is needed
  102. * when window has no title bar, yet you still want to allow the user to drag it by clicking on some specific area
  103. * (e.g. a title bar you manually render).
  104. *
  105. * @param[in] rects Areas of the window (relative to the window origin in top-left corner) in which the drag
  106. * operation in allowed.
  107. */
  108. void _setDragZones(const Vector<Rect2I>& rects);
  109. /** Attaches non-specific user data that can later be retrieved through _getUserData(). */
  110. void _setUserData(void* data);
  111. /** Returns user data attached to the object when _setUserData was called. */
  112. void* _getUserData() const;
  113. /**
  114. * Registers the window with the drag and drop manager and allows it to accept file drop operations. Each call
  115. * to this method must eventually be followed with _unregisterForDragAndDrop.
  116. */
  117. void _registerForDragAndDrop();
  118. /**
  119. * Unregisters the window from the drag and drop manager. This will need to be called multiple times if
  120. * _registerForDragAndDrop was called multiple times.
  121. */
  122. void _unregisterForDragAndDrop();
  123. /** Returns internal private data for use by friends. */
  124. Pimpl* _getPrivateData() const { return m; }
  125. /** @} */
  126. private:
  127. Pimpl* m;
  128. UINT32 mWindowId;
  129. };
  130. /** @} */
  131. /** @} */
  132. }