llwindow.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * @file llwindow.h
  3. * @brief Basic graphical window class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLWINDOW_H
  33. #define LL_LLWINDOW_H
  34. #include "llcoord.h"
  35. #include "llcursortypes.h"
  36. #include "llrect.h"
  37. #include "llsd.h"
  38. #include "llstring.h"
  39. class LLPreeditor;
  40. class LLSplashScreen;
  41. class LLWindow;
  42. class LLWindowCallbacks
  43. {
  44. public:
  45. virtual ~LLWindowCallbacks() {}
  46. virtual bool handleTranslatedKeyDown(KEY key, MASK mask, bool repeated);
  47. virtual bool handleTranslatedKeyUp(KEY key, MASK mask);
  48. virtual void handleScanKey(KEY key, bool key_down, bool key_up, bool key_level);
  49. virtual bool handleUnicodeChar(llwchar uni_char, MASK mask);
  50. virtual bool handleMouseDown(LLWindow* window, LLCoordGL pos, MASK mask);
  51. virtual bool handleMouseUp(LLWindow* window, LLCoordGL pos, MASK mask);
  52. virtual void handleMouseLeave(LLWindow* window);
  53. // Returns true to allow window to close, which will then cause handleQuit
  54. // to be called
  55. virtual bool handleCloseRequest(LLWindow* window);
  56. // window is about to be destroyed, clean up your business
  57. virtual void handleQuit(LLWindow* window);
  58. virtual bool handleRightMouseDown(LLWindow* window, LLCoordGL pos, MASK mask);
  59. virtual bool handleRightMouseUp(LLWindow* window, LLCoordGL pos, MASK mask);
  60. virtual bool handleMiddleMouseDown(LLWindow* window, LLCoordGL pos, MASK mask);
  61. virtual bool handleMiddleMouseUp(LLWindow* window, LLCoordGL pos, MASK mask);
  62. virtual bool handleActivate(LLWindow* window, bool activated);
  63. virtual bool handleActivateApp(LLWindow* window, bool activating);
  64. virtual void handleMouseMove(LLWindow* window, LLCoordGL pos, MASK mask);
  65. #if LL_DARWIN
  66. virtual void handleMouseDragged(LLWindow* window, LLCoordGL pos, MASK mask);
  67. #endif
  68. virtual void handleScrollWheel(LLWindow* window, S32 clicks);
  69. virtual void handleResize(LLWindow* window, S32 width, S32 height);
  70. virtual void handleFocus(LLWindow* window);
  71. virtual void handleFocusLost(LLWindow* window);
  72. virtual void handleMenuSelect(LLWindow* window, S32 menu_item);
  73. virtual bool handlePaint(LLWindow* window, S32 x, S32 y, S32 width, S32 height);
  74. // Double-click of left mouse button:
  75. virtual bool handleDoubleClick(LLWindow* window, LLCoordGL pos, MASK mask);
  76. // Window is taking over CPU for a while:
  77. virtual void handleWindowBlock(LLWindow* window);
  78. // Window coming back after taking over CPU for a while:
  79. virtual void handleWindowUnblock(LLWindow* window);
  80. virtual void handleDataCopy(LLWindow* window, S32 data_type, void* data);
  81. virtual bool handleTimerEvent(LLWindow* window);
  82. virtual bool handleDeviceChange(LLWindow* window);
  83. virtual bool handleDPIChanged(LLWindow* window, F32 ui_scale_factor,
  84. S32 window_width, S32 window_height);
  85. virtual bool handleWindowDidChangeScreen(LLWindow* window);
  86. };
  87. // Refer to llwindow_test in test/common/llwindow for usage example
  88. class LLWindow
  89. {
  90. public:
  91. struct LLWindowResolution
  92. {
  93. S32 mWidth;
  94. S32 mHeight;
  95. };
  96. enum ESwapMethod
  97. {
  98. SWAP_METHOD_UNDEFINED,
  99. SWAP_METHOD_EXCHANGE,
  100. SWAP_METHOD_COPY
  101. };
  102. enum EFlags
  103. {
  104. // Currently unused
  105. };
  106. public:
  107. static void createWindow(const std::string& title, S32 x, S32 y,
  108. U32 width, U32 height, U32 flags = 0,
  109. bool fullscreen = false,
  110. bool disable_vsync = true,
  111. U32 fsaa_samples = 0);
  112. static void destroyWindow();
  113. virtual void setWindowTitle(const std::string& title) = 0;
  114. virtual void show() = 0;
  115. virtual void hide() = 0;
  116. virtual void close() = 0;
  117. virtual void minimize() = 0;
  118. virtual void restore() = 0;
  119. virtual bool getVisible() = 0;
  120. virtual bool getMinimized() = 0;
  121. virtual bool getMaximized() = 0;
  122. virtual bool maximize() = 0;
  123. virtual bool getFullscreen() = 0;
  124. virtual bool getSize(LLCoordScreen* size) = 0;
  125. virtual bool getSize(LLCoordWindow* size) = 0;
  126. virtual bool setSize(LLCoordScreen size) = 0;
  127. // *HACK: to compute window borders offsets (needed for SDL2). HB
  128. LL_INLINE virtual void calculateBordersOffsets() {}
  129. // *HACK: to force-redraw the screen (needed for SDL). HB
  130. LL_INLINE virtual void refresh() {}
  131. virtual bool getPosition(LLCoordScreen* position) = 0;
  132. virtual bool setPosition(LLCoordScreen position) = 0;
  133. virtual bool switchContext(bool fullscreen, const LLCoordScreen& size,
  134. bool disable_vsync,
  135. const LLCoordScreen* const posp = NULL) = 0;
  136. // Creates a new GL context that shares a namespace with this Window's main
  137. // GL context and makes it current on the current thread. Returns a pointer
  138. // to be handed back to destroySharedConext()/makeContextCurrent().
  139. virtual void* createSharedContext() = 0;
  140. // Makes the given context current on the current thread
  141. virtual void makeContextCurrent(void* context) = 0;
  142. // Destroys the given context that was retrieved by createSharedContext().
  143. // Must be called on the same thread that called createSharedContext().
  144. virtual void destroySharedContext(void* context) = 0;
  145. virtual bool setCursorPosition(const LLCoordWindow& position) = 0;
  146. virtual bool getCursorPosition(LLCoordWindow* position) = 0;
  147. virtual void showCursor() = 0;
  148. virtual void hideCursor() = 0;
  149. virtual bool isCursorHidden() = 0;
  150. virtual void showCursorFromMouseMove() = 0;
  151. virtual void hideCursorUntilMouseMove() = 0;
  152. // These two methods create a way to make a busy cursor instead of an arrow
  153. // when someone is busy doing something.
  154. LL_INLINE void incBusyCount() { ++mBusyCount; }
  155. void decBusyCount();
  156. LL_INLINE void resetBusyCount() { mBusyCount = 0; }
  157. LL_INLINE S32 getBusyCount() const { return mBusyCount; }
  158. // Sets cursor, may set to arrow+hourglass
  159. virtual void setCursor(ECursorType cursor) = 0;
  160. LL_INLINE ECursorType getCursor() const { return mCurrentCursor; }
  161. // Used to prevent any cursor change during a call where various methods
  162. // are called that can each chenge the cursor, causing a flickering. HB
  163. LL_INLINE void freezeCursor(bool freeze) { mCursorFrozen = freeze; }
  164. virtual void captureMouse() = 0;
  165. virtual void releaseMouse() = 0;
  166. virtual void setMouseClipping(bool b) = 0;
  167. virtual bool isClipboardTextAvailable() = 0;
  168. virtual bool pasteTextFromClipboard(LLWString& text) = 0;
  169. virtual bool copyTextToClipboard(const LLWString& text) = 0;
  170. virtual bool isPrimaryTextAvailable() = 0;
  171. virtual bool pasteTextFromPrimary(LLWString& text) = 0;
  172. virtual bool copyTextToPrimary(const LLWString& text) = 0;
  173. virtual void flashIcon(F32 seconds) = 0;
  174. LL_INLINE virtual F32 getGamma() { return mCurrentGamma; }
  175. virtual bool setGamma(F32 gamma) = 0; // Set the gamma
  176. // Restore original gamma table (before updating gamma)
  177. virtual bool restoreGamma() = 0;
  178. // set number of FSAA samples
  179. virtual void setFSAASamples(U32 fsaa_samples) = 0;
  180. virtual U32 getFSAASamples() = 0;
  181. LL_INLINE virtual ESwapMethod getSwapMethod() { return mSwapMethod; }
  182. virtual void gatherInput() = 0;
  183. virtual void delayInputProcessing() = 0;
  184. virtual void swapBuffers() = 0;
  185. virtual void bringToFront() = 0;
  186. // This may not have meaning or be required on other platforms, therefore,
  187. // it is not abstract
  188. LL_INLINE virtual void focusClient() {}
  189. // Handy coordinate space conversion routines
  190. // NB: screen to window and vice verse won't work on width/height
  191. // coordinate pairs, as the conversion must take into account left AND
  192. // right border widths, etc.
  193. virtual bool convertCoords(LLCoordScreen from, LLCoordWindow* to) = 0;
  194. virtual bool convertCoords(LLCoordWindow from, LLCoordScreen* to) = 0;
  195. virtual bool convertCoords(LLCoordWindow from, LLCoordGL* to) = 0;
  196. virtual bool convertCoords(LLCoordGL from, LLCoordWindow* to) = 0;
  197. virtual bool convertCoords(LLCoordScreen from, LLCoordGL* to) = 0;
  198. virtual bool convertCoords(LLCoordGL from, LLCoordScreen* to) = 0;
  199. // Query supported resolutions
  200. virtual LLWindowResolution* getSupportedResolutions(S32& num_resolutions) = 0;
  201. virtual F32 getNativeAspectRatio() = 0;
  202. virtual F32 getPixelAspectRatio() = 0;
  203. LL_INLINE virtual void setNativeAspectRatio(F32 ratio)
  204. {
  205. mOverrideAspectRatio = ratio;
  206. }
  207. void setCallbacks(LLWindowCallbacks* callbacks);
  208. // Prepares to put up an OS dialog (if special measures are required, such
  209. // as in fullscreen mode)
  210. LL_INLINE virtual void beforeDialog() {}
  211. // Undoes whatever was done in beforeDialog()
  212. LL_INLINE virtual void afterDialog() {}
  213. // Returns a platform-specific window reference (HWND on Windows,
  214. // WindowRef on the Mac, Gtk window on Linux)
  215. virtual void* getPlatformWindow() = 0;
  216. // Control the platform language text input mechanisms.
  217. LL_INLINE virtual void allowLanguageTextInput(LLPreeditor* p, bool b)
  218. {
  219. }
  220. LL_INLINE virtual void setLanguageTextInput(const LLCoordGL& pos)
  221. {
  222. }
  223. LL_INLINE virtual void updateLanguageTextInputArea()
  224. {
  225. }
  226. LL_INLINE virtual void interruptLanguageTextInput()
  227. {
  228. }
  229. LL_INLINE virtual void spawnWebBrowser(const std::string& escaped_url,
  230. bool async)
  231. {
  232. }
  233. static std::vector<std::string> getDynamicFallbackFontList();
  234. // Provides native key event data
  235. LL_INLINE virtual LLSD getNativeKeyData() { return LLSD::emptyMap(); }
  236. // Get system UI size based on DPI (for 96 DPI UI size should be 1.f)
  237. LL_INLINE virtual F32 getSystemUISize() { return 1.f; }
  238. protected:
  239. LLWindow(bool fullscreen, U32 flags);
  240. virtual ~LLWindow() {}
  241. LL_INLINE virtual bool isValid() { return true; }
  242. LL_INLINE bool shouldPostQuit() { return mPostQuit; }
  243. // Handles a UTF-16 encoding unit received from keyboard. Converting the
  244. // series of UTF-16 encoding units to UTF-32 data, this method passes the
  245. // resulting UTF-32 data to mCallback's handleUnicodeChar. The mask should
  246. // be that to be passed to the callback. This method uses mHighSurrogate as
  247. // a dedicated work variable.
  248. void handleUnicodeUTF16(U16 utf16, MASK mask);
  249. protected:
  250. static LLWindowCallbacks sDefaultCallbacks;
  251. protected:
  252. LLWindowCallbacks* mCallbacks;
  253. LLWindowResolution* mSupportedResolutions;
  254. S32 mFullscreenWidth;
  255. S32 mFullscreenHeight;
  256. S32 mFullscreenBits;
  257. S32 mFullscreenRefresh;
  258. F32 mOverrideAspectRatio;
  259. F32 mCurrentGamma;
  260. S32 mNumSupportedResolutions;
  261. S32 mBusyCount; // how deep is the "cursor busy" stack ?
  262. U32 mFlags;
  263. U16 mHighSurrogate;
  264. ECursorType mCurrentCursor;
  265. #if LL_DARWIN
  266. ECursorType mNextCursor;
  267. #endif
  268. ESwapMethod mSwapMethod;
  269. // should this window post a quit message when destroyed ?
  270. bool mPostQuit;
  271. bool mFullscreen;
  272. bool mCursorFrozen;
  273. bool mCursorHidden;
  274. bool mHideCursorPermanent;
  275. // Is this window currently clipping the mouse ?
  276. bool mIsMouseClipping;
  277. // "Primary" (mouse selection) clipboard buffer for systems without one
  278. // such clipboard.
  279. LLWString mPrimaryClipboard;
  280. };
  281. // LLSplashScreen
  282. // A simple, OS-specific splash screen that we can display while initializing
  283. // the application and before creating a GL window
  284. class LLSplashScreen
  285. {
  286. public:
  287. LLSplashScreen() {}
  288. virtual ~LLSplashScreen() {}
  289. // Call to display the window.
  290. static void show();
  291. static void hide();
  292. static void update(const std::string& string);
  293. static bool isVisible();
  294. protected:
  295. // These are overridden by the platform implementation
  296. virtual void showImpl() = 0;
  297. virtual void updateImpl(const std::string& string) = 0;
  298. virtual void hideImpl() = 0;
  299. };
  300. // Platform-neutral for accessing the platform specific message box
  301. // Buttons in the message box
  302. constexpr U32 OSMB_OK = 0;
  303. constexpr U32 OSMB_OKCANCEL = 1;
  304. constexpr U32 OSMB_YESNO = 2;
  305. // Returned button number
  306. constexpr S32 OSBTN_YES = 0;
  307. constexpr S32 OSBTN_NO = 1;
  308. constexpr S32 OSBTN_OK = 2;
  309. constexpr S32 OSBTN_CANCEL = 3;
  310. S32 OSMessageBox(const std::string& text, const std::string& caption = "Error",
  311. U32 type = OSMB_OK);
  312. // Globals
  313. extern LLWindow* gWindowp;
  314. extern bool gDebugClicks;
  315. extern bool gDebugWindowProc;
  316. extern bool gHiDPISupport;
  317. // Protocols, like "http" and "https" we support in URLs
  318. extern const S32 gURLProtocolWhitelistCount;
  319. extern const std::string gURLProtocolWhitelist[];
  320. void simpleEscapeString(std::string& stringIn);
  321. #endif // LL_LLWINDOW_H