WebClient.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  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. #pragma once
  23. #include <Atomic/Container/List.h>
  24. #include <Atomic/Core/Object.h>
  25. #include "WebRenderHandler.h"
  26. class CefClient;
  27. namespace Atomic
  28. {
  29. class WebClientPrivate;
  30. class WebMessageHandler;
  31. /// WebClient is the main interface for communicating with a browser instance
  32. class ATOMIC_API WebClient : public Object
  33. {
  34. friend class WebBrowserHost;
  35. friend class WebClientPrivate;
  36. OBJECT(WebClient)
  37. public:
  38. /// Construct.
  39. WebClient(Context* context);
  40. /// Destruct.
  41. virtual ~WebClient();
  42. /// Create the browser, call only once initialized with handlers
  43. bool CreateBrowser(const String& initialURL, int width, int height);
  44. /// Set the browser's width and height
  45. void SetSize(int width, int height);
  46. /// Send a mouse click event to the browser
  47. void SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier, int clickCount = 1) const;
  48. /// Send a mouse press event to the browser
  49. void SendMousePressEvent(int x, int y, unsigned button = 0, unsigned modifier = 0, int clickCount = 1) const;
  50. /// Send a mouse move event to the browser
  51. void SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave = false) const;
  52. /// Send a mouse wheel event to the browser
  53. void SendMouseWheelEvent(int x, int y, unsigned modifier, int deltaX, int deltaY) const;
  54. /// Send a focus event to the browser
  55. void SendFocusEvent(bool focus = true);
  56. /// Send a TextInput event to the browser
  57. void SendTextInputEvent(const StringHash eventType, VariantMap& eventData);
  58. /// Send a key event to the browser
  59. void SendKeyEvent(const StringHash eventType, VariantMap& eventData);
  60. // Shortcuts, note that some web pages (notably some text editors)
  61. // only work with key events and not all shorcuts
  62. /// Invoke the Cut shortcut on the browser's main frame
  63. void ShortcutCut();
  64. /// Invoke the Copy shortcut on the browser's main frame
  65. void ShortcutCopy();
  66. /// Invoke the Paste shortcut on the browser's main frame
  67. void ShortcutPaste();
  68. /// Invoke the SelectAll shortcut on the browser's main frame
  69. void ShortcutSelectAll();
  70. /// Invoke the Undo shortcut on the browser's main frame
  71. void ShortcutUndo();
  72. /// Invoke the Redo shortcut on the browser's main frame
  73. void ShortcutRedo();
  74. /// Invoke the Delete shortcut on the browser's main frame
  75. void ShortcutDelete();
  76. /// Add a message handler to the WebClient
  77. void AddMessageHandler(WebMessageHandler* handler, bool first = false);
  78. /// Remove a message handler to the WebClient
  79. void RemoveMessageHandler(WebMessageHandler* handler);
  80. /// Execute some JavaScript in the browser
  81. void ExecuteJavaScript(const String& script);
  82. /// Eval some JavaScript in the browser (async return value referenced by evalID)
  83. void EvalJavaScript(unsigned evalID, const String& script);
  84. // Navigation
  85. /// Returns true if the page is currently loading
  86. bool IsLoading();
  87. /// Load the specified url into the main frame of the browser
  88. void LoadURL(const String& url);
  89. /// Go back in page history
  90. void GoBack();
  91. /// Go forward in page history
  92. void GoForward();
  93. /// Reload the current page
  94. void Reload();
  95. /// Set the render handler for this client
  96. void SetWebRenderHandler(WebRenderHandler* handler);
  97. /// Get the (internal) CefClient for this WebClient
  98. CefClient* GetCefClient();
  99. private:
  100. void WasResized();
  101. void EvalJavaScriptResult(unsigned evalID, bool result, const String& value);
  102. SharedPtr<WebRenderHandler> renderHandler_;
  103. List<SharedPtr<WebMessageHandler>> messageHandlers_;
  104. WebClientPrivate* d_;
  105. };
  106. }