WKContextMenuItem.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "WKContextMenuItem.h"
  27. #include "MutableArray.h"
  28. #include "WebContextMenuItem.h"
  29. #include "WebContextMenuItemData.h"
  30. #include "WKAPICast.h"
  31. #include "WKContextMenuItemTypes.h"
  32. #if PLATFORM(MAC)
  33. #import <mach-o/dyld.h>
  34. #endif
  35. using namespace WebCore;
  36. using namespace WebKit;
  37. WKTypeID WKContextMenuItemGetTypeID()
  38. {
  39. #if ENABLE(CONTEXT_MENUS)
  40. return toAPI(WebContextMenuItem::APIType);
  41. #else
  42. return toAPI(APIObject::TypeNull);
  43. #endif
  44. }
  45. WKContextMenuItemRef WKContextMenuItemCreateAsAction(WKContextMenuItemTag tag, WKStringRef title, bool enabled)
  46. {
  47. #if ENABLE(CONTEXT_MENUS)
  48. return toAPI(WebContextMenuItem::create(WebContextMenuItemData(ActionType, toImpl(tag), toImpl(title)->string(), enabled, false)).leakRef());
  49. #else
  50. return 0;
  51. #endif
  52. }
  53. WKContextMenuItemRef WKContextMenuItemCreateAsCheckableAction(WKContextMenuItemTag tag, WKStringRef title, bool enabled, bool checked)
  54. {
  55. #if ENABLE(CONTEXT_MENUS)
  56. return toAPI(WebContextMenuItem::create(WebContextMenuItemData(CheckableActionType, toImpl(tag), toImpl(title)->string(), enabled, checked)).leakRef());
  57. #else
  58. return 0;
  59. #endif
  60. }
  61. WKContextMenuItemRef WKContextMenuItemCreateAsSubmenu(WKStringRef title, bool enabled, WKArrayRef submenuItems)
  62. {
  63. #if ENABLE(CONTEXT_MENUS)
  64. return toAPI(WebContextMenuItem::create(toImpl(title)->string(), enabled, toImpl(submenuItems)).leakRef());
  65. #else
  66. return 0;
  67. #endif
  68. }
  69. WKContextMenuItemRef WKContextMenuItemSeparatorItem()
  70. {
  71. #if ENABLE(CONTEXT_MENUS)
  72. return toAPI(WebContextMenuItem::separatorItem());
  73. #else
  74. return 0;
  75. #endif
  76. }
  77. #if PLATFORM(MAC)
  78. static WKContextMenuItemTag compatibleContextMenuItemTag(WKContextMenuItemTag tag)
  79. {
  80. static bool needsWorkaround = ^bool {
  81. const int32_t safariFrameworkVersionWithIncompatibleContextMenuItemTags = 0x02181900; // 536.25.0 (Safari 6.0)
  82. return NSVersionOfRunTimeLibrary("Safari") == safariFrameworkVersionWithIncompatibleContextMenuItemTags;
  83. }();
  84. if (!needsWorkaround)
  85. return tag;
  86. // kWKContextMenuItemTagDictationAlternative was inserted before kWKContextMenuItemTagInspectElement.
  87. // DictationAlternative is now at the end like it should have been. To be compatible we need to return
  88. // InspectElement for DictationAlternative and shift InspectElement and after by one.
  89. if (tag == kWKContextMenuItemTagDictationAlternative)
  90. return kWKContextMenuItemTagInspectElement;
  91. if (tag >= kWKContextMenuItemTagInspectElement && tag < kWKContextMenuItemBaseApplicationTag)
  92. return tag + 1;
  93. return tag;
  94. }
  95. #endif
  96. WKContextMenuItemTag WKContextMenuItemGetTag(WKContextMenuItemRef itemRef)
  97. {
  98. #if ENABLE(CONTEXT_MENUS)
  99. #if PLATFORM(MAC)
  100. return compatibleContextMenuItemTag(toAPI(toImpl(itemRef)->data()->action()));
  101. #else
  102. return toAPI(toImpl(itemRef)->data()->action());
  103. #endif
  104. #else
  105. return toAPI(ContextMenuItemTagNoAction);
  106. #endif
  107. }
  108. WKContextMenuItemType WKContextMenuItemGetType(WKContextMenuItemRef itemRef)
  109. {
  110. #if ENABLE(CONTEXT_MENUS)
  111. return toAPI(toImpl(itemRef)->data()->type());
  112. #else
  113. return toAPI(ActionType);
  114. #endif
  115. }
  116. WKStringRef WKContextMenuItemCopyTitle(WKContextMenuItemRef itemRef)
  117. {
  118. #if ENABLE(CONTEXT_MENUS)
  119. return toCopiedAPI(toImpl(itemRef)->data()->title().impl());
  120. #else
  121. return 0;
  122. #endif
  123. }
  124. bool WKContextMenuItemGetEnabled(WKContextMenuItemRef itemRef)
  125. {
  126. #if ENABLE(CONTEXT_MENUS)
  127. return toImpl(itemRef)->data()->enabled();
  128. #else
  129. return false;
  130. #endif
  131. }
  132. bool WKContextMenuItemGetChecked(WKContextMenuItemRef itemRef)
  133. {
  134. #if ENABLE(CONTEXT_MENUS)
  135. return toImpl(itemRef)->data()->checked();
  136. #else
  137. return false;
  138. #endif
  139. }
  140. WKArrayRef WKContextMenuCopySubmenuItems(WKContextMenuItemRef itemRef)
  141. {
  142. #if ENABLE(CONTEXT_MENUS)
  143. return toAPI(toImpl(itemRef)->submenuItemsAsImmutableArray().leakRef());
  144. #else
  145. return 0;
  146. #endif
  147. }
  148. WKTypeRef WKContextMenuItemGetUserData(WKContextMenuItemRef itemRef)
  149. {
  150. #if ENABLE(CONTEXT_MENUS)
  151. return toAPI(toImpl(itemRef)->userData());
  152. #else
  153. return 0;
  154. #endif
  155. }
  156. void WKContextMenuItemSetUserData(WKContextMenuItemRef itemRef, WKTypeRef userDataRef)
  157. {
  158. #if ENABLE(CONTEXT_MENUS)
  159. toImpl(itemRef)->setUserData(toImpl(userDataRef));
  160. #endif
  161. }