SystemInterface.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/SystemInterface.h"
  29. #include "../../Include/RmlUi/Core/Log.h"
  30. #include "../../Include/RmlUi/Core/StringUtilities.h"
  31. #include "../../Include/RmlUi/Core/URL.h"
  32. #ifdef RMLUI_PLATFORM_WIN32
  33. #include <windows.h>
  34. #else
  35. #include <stdio.h>
  36. #endif
  37. namespace Rml {
  38. static String clipboard_text;
  39. SystemInterface::SystemInterface()
  40. {
  41. }
  42. SystemInterface::~SystemInterface()
  43. {
  44. }
  45. #ifdef RMLUI_PLATFORM_WIN32
  46. bool SystemInterface::LogMessage(Log::Type logtype, const String& message)
  47. {
  48. // By default we just send a platform message
  49. #if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
  50. if (logtype == Log::LT_ASSERT)
  51. {
  52. String message_user = CreateString(1024, "%s\nWould you like to interrupt execution?", message.c_str());
  53. // Return TRUE if the user presses NO (continue execution)
  54. return (IDNO == MessageBoxA(nullptr, message_user.c_str(), "Assertion Failure", MB_YESNO | MB_ICONSTOP | MB_DEFBUTTON2 | MB_TASKMODAL));
  55. }
  56. else
  57. #endif
  58. {
  59. OutputDebugStringA(message.c_str());
  60. OutputDebugStringA("\r\n");
  61. }
  62. return true;
  63. }
  64. #else
  65. bool SystemInterface::LogMessage(Log::Type /*logtype*/, const String& message)
  66. {
  67. #ifdef RMLUI_PLATFORM_EMSCRIPTEN
  68. puts(message.c_str());
  69. #else
  70. fprintf(stderr, "%s\n", message.c_str());
  71. #endif
  72. return true;
  73. }
  74. #endif
  75. void SystemInterface::SetMouseCursor(const String& /*cursor_name*/)
  76. {
  77. }
  78. void SystemInterface::SetClipboardText(const String& text)
  79. {
  80. // The default implementation will only copy and paste within the application
  81. clipboard_text = text;
  82. }
  83. void SystemInterface::GetClipboardText(String& text)
  84. {
  85. text = clipboard_text;
  86. }
  87. int SystemInterface::TranslateString(String& translated, const String& input)
  88. {
  89. translated = input;
  90. return 0;
  91. }
  92. void SystemInterface::JoinPath(String& translated_path, const String& document_path, const String& path)
  93. {
  94. // If the path is absolute, strip the leading / and return it.
  95. if (path.size() > 0 && path[0] == '/')
  96. {
  97. translated_path = path.substr(1);
  98. return;
  99. }
  100. // If the path is a Windows-style absolute path, return it directly.
  101. size_t drive_pos = path.find(':');
  102. size_t slash_pos = Math::Min(path.find('/'), path.find('\\'));
  103. if (drive_pos != String::npos &&
  104. drive_pos < slash_pos)
  105. {
  106. translated_path = path;
  107. return;
  108. }
  109. using StringUtilities::Replace;
  110. // Strip off the referencing document name.
  111. translated_path = document_path;
  112. translated_path = Replace(translated_path, '\\', '/');
  113. size_t file_start = translated_path.rfind('/');
  114. if (file_start != String::npos)
  115. translated_path.resize(file_start + 1);
  116. else
  117. translated_path.clear();
  118. // Append the paths and send through URL to removing any '..'.
  119. URL url(Replace(translated_path, ':', '|') + Replace(path, '\\', '/'));
  120. translated_path = Replace(url.GetPathedFileName(), '|', ':');
  121. }
  122. void SystemInterface::ActivateKeyboard(Rml::Vector2f /*caret_position*/, float /*line_height*/) {}
  123. void SystemInterface::DeactivateKeyboard() {}
  124. } // namespace Rml