PlatformWebViewMac.mm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #import "config.h"
  26. #import "PlatformWebView.h"
  27. #import <WebKit2/WKViewPrivate.h>
  28. #import <Carbon/Carbon.h>
  29. @interface ActiveOffscreenWindow : NSWindow
  30. @end
  31. @implementation ActiveOffscreenWindow
  32. - (BOOL)isKeyWindow
  33. {
  34. return YES;
  35. }
  36. @end
  37. namespace TestWebKitAPI {
  38. PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
  39. {
  40. NSRect rect = NSMakeRect(0, 0, 800, 600);
  41. m_view = [[WKView alloc] initWithFrame:rect contextRef:contextRef pageGroupRef:pageGroupRef];
  42. NSRect windowRect = NSOffsetRect(rect, -10000, [(NSScreen *)[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
  43. m_window = [[ActiveOffscreenWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
  44. [m_window setColorSpace:[[NSScreen mainScreen] colorSpace]];
  45. [[m_window contentView] addSubview:m_view];
  46. [m_window orderBack:nil];
  47. [m_window setAutodisplay:NO];
  48. [m_window setReleasedWhenClosed:NO];
  49. }
  50. PlatformWebView::~PlatformWebView()
  51. {
  52. [m_window close];
  53. [m_window release];
  54. [m_view release];
  55. }
  56. void PlatformWebView::resizeTo(unsigned width, unsigned height)
  57. {
  58. [m_view setFrame:NSMakeRect(0, 0, width, height)];
  59. }
  60. WKPageRef PlatformWebView::page() const
  61. {
  62. return [m_view pageRef];
  63. }
  64. void PlatformWebView::focus()
  65. {
  66. // Implement.
  67. }
  68. void PlatformWebView::simulateSpacebarKeyPress()
  69. {
  70. NSEvent *event = [NSEvent keyEventWithType:NSKeyDown
  71. location:NSMakePoint(5, 5)
  72. modifierFlags:0
  73. timestamp:GetCurrentEventTime()
  74. windowNumber:[m_window windowNumber]
  75. context:[NSGraphicsContext currentContext]
  76. characters:@" "
  77. charactersIgnoringModifiers:@" "
  78. isARepeat:NO
  79. keyCode:0x31];
  80. [m_view keyDown:event];
  81. event = [NSEvent keyEventWithType:NSKeyUp
  82. location:NSMakePoint(5, 5)
  83. modifierFlags:0
  84. timestamp:GetCurrentEventTime()
  85. windowNumber:[m_window windowNumber]
  86. context:[NSGraphicsContext currentContext]
  87. characters:@" "
  88. charactersIgnoringModifiers:@" "
  89. isARepeat:NO
  90. keyCode:0x31];
  91. [m_view keyUp:event];
  92. }
  93. void PlatformWebView::simulateRightClick(unsigned x, unsigned y)
  94. {
  95. NSEvent *event = [NSEvent mouseEventWithType:NSRightMouseDown
  96. location:NSMakePoint(x, y)
  97. modifierFlags:0
  98. timestamp:GetCurrentEventTime()
  99. windowNumber:[m_window windowNumber]
  100. context:[NSGraphicsContext currentContext]
  101. eventNumber:0
  102. clickCount:0
  103. pressure:0];
  104. [m_view rightMouseDown:event];
  105. event = [NSEvent mouseEventWithType:NSRightMouseUp
  106. location:NSMakePoint(x, y)
  107. modifierFlags:0
  108. timestamp:GetCurrentEventTime()
  109. windowNumber:[m_window windowNumber]
  110. context:[NSGraphicsContext currentContext]
  111. eventNumber:0
  112. clickCount:0
  113. pressure:0];
  114. [m_view rightMouseUp:event];
  115. }
  116. void PlatformWebView::simulateMouseMove(unsigned x, unsigned y)
  117. {
  118. NSEvent *event = [NSEvent mouseEventWithType:NSMouseMoved
  119. location:NSMakePoint(x, y)
  120. modifierFlags:0
  121. timestamp:GetCurrentEventTime()
  122. windowNumber:[m_window windowNumber]
  123. context:[NSGraphicsContext currentContext]
  124. eventNumber:0
  125. clickCount:0
  126. pressure:0];
  127. [m_view mouseMoved:event];
  128. }
  129. } // namespace TestWebKitAPI