WebClient.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 WebClient : public Object
  33. {
  34. friend class WebBrowserHost;
  35. friend class WebClientPrivate;
  36. ATOMIC_OBJECT(WebClient, Object)
  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. /// Set browser zoom level, specify 0.0 to reset the zoom level
  47. void SetZoomLevel(float zoomLevel);
  48. /// Send a mouse click event to the browser
  49. void SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier, int clickCount = 1) const;
  50. /// Send a mouse press event to the browser
  51. void SendMousePressEvent(int x, int y, unsigned button = 0, unsigned modifier = 0, int clickCount = 1) const;
  52. /// Send a mouse move event to the browser
  53. void SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave = false) const;
  54. /// Send a mouse wheel event to the browser
  55. void SendMouseWheelEvent(int x, int y, unsigned modifier, int deltaX, int deltaY) const;
  56. /// Send a focus event to the browser
  57. void SendFocusEvent(bool focus = true);
  58. /// Send a TextInput event to the browser
  59. void SendTextInputEvent(const StringHash eventType, VariantMap& eventData);
  60. /// Send a key event to the browser
  61. void SendKeyEvent(const StringHash eventType, VariantMap& eventData);
  62. // Shortcuts, note that some web pages (notably some text editors)
  63. // only work with key events and not all shorcuts
  64. /// Invoke the Cut shortcut on the browser's main frame
  65. void ShortcutCut();
  66. /// Invoke the Copy shortcut on the browser's main frame
  67. void ShortcutCopy();
  68. /// Invoke the Paste shortcut on the browser's main frame
  69. void ShortcutPaste();
  70. /// Invoke the SelectAll shortcut on the browser's main frame
  71. void ShortcutSelectAll();
  72. /// Invoke the Undo shortcut on the browser's main frame
  73. void ShortcutUndo();
  74. /// Invoke the Redo shortcut on the browser's main frame
  75. void ShortcutRedo();
  76. /// Invoke the Delete shortcut on the browser's main frame
  77. void ShortcutDelete();
  78. /// Add a message handler to the WebClient
  79. void AddMessageHandler(WebMessageHandler* handler, bool first = false);
  80. /// Remove a message handler to the WebClient
  81. void RemoveMessageHandler(WebMessageHandler* handler);
  82. /// Execute some JavaScript in the browser
  83. void ExecuteJavaScript(const String& script);
  84. /// Eval some JavaScript in the browser (async return value referenced by evalID)
  85. void EvalJavaScript(unsigned evalID, const String& script);
  86. // Navigation
  87. /// Returns true if the page is currently loading
  88. bool IsLoading();
  89. /// Load the specified url into the main frame of the browser
  90. void LoadURL(const String& url);
  91. /// Load html source into main frame of browser
  92. void LoadString(const String& source, const String &url = "http://localcontent/");
  93. /// Go back in page history
  94. void GoBack();
  95. /// Go forward in page history
  96. void GoForward();
  97. /// Reload the current page
  98. void Reload();
  99. /// Set the render handler for this client
  100. void SetWebRenderHandler(WebRenderHandler* handler);
  101. /// Get the (internal) CefClient for this WebClient
  102. CefClient* GetCefClient();
  103. private:
  104. void HandleWebViewGlobalPropertiesChanged(StringHash eventType, VariantMap& eventData);
  105. void UpdateGlobalProperties();
  106. void WasResized();
  107. void EvalJavaScriptResult(unsigned evalID, bool result, const String& value);
  108. SharedPtr<WebRenderHandler> renderHandler_;
  109. List<SharedPtr<WebMessageHandler>> messageHandlers_;
  110. WebClientPrivate* d_;
  111. };
  112. }