PlatformWebViewGtk.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2012 Igalia S.L.
  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 "PlatformWebView.h"
  27. #include <WebCore/GOwnPtrGtk.h>
  28. #include <gtk/gtk.h>
  29. #include <wtf/gobject/GOwnPtr.h>
  30. namespace TestWebKitAPI {
  31. PlatformWebView::PlatformWebView(WKContextRef contextRef, WKPageGroupRef pageGroupRef)
  32. {
  33. m_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  34. m_view = WKViewCreate(contextRef, pageGroupRef);
  35. gtk_container_add(GTK_CONTAINER(m_window), GTK_WIDGET(m_view));
  36. gtk_widget_show(GTK_WIDGET(m_view));
  37. gtk_widget_show(m_window);
  38. }
  39. PlatformWebView::~PlatformWebView()
  40. {
  41. gtk_widget_destroy(m_window);
  42. }
  43. WKPageRef PlatformWebView::page() const
  44. {
  45. return WKViewGetPage(m_view);
  46. }
  47. void PlatformWebView::resizeTo(unsigned width, unsigned height)
  48. {
  49. gtk_window_resize(GTK_WINDOW(m_window), width, height);
  50. }
  51. static void doKeyStroke(GtkWidget* viewWidget, unsigned int keyVal)
  52. {
  53. GOwnPtr<GdkEvent> event(gdk_event_new(GDK_KEY_PRESS));
  54. event->key.keyval = keyVal;
  55. event->key.time = GDK_CURRENT_TIME;
  56. event->key.state = 0;
  57. event->key.window = gtk_widget_get_window(viewWidget);
  58. g_object_ref(event->key.window);
  59. gdk_event_set_device(event.get(), gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(gtk_widget_get_display(viewWidget))));
  60. // When synthesizing an event, an invalid hardware_keycode value can cause it to be badly processed by GTK+.
  61. GOwnPtr<GdkKeymapKey> keys;
  62. int keysCount;
  63. if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), keyVal, &keys.outPtr(), &keysCount))
  64. event->key.hardware_keycode = keys.get()[0].keycode;
  65. gtk_main_do_event(event.get());
  66. event->key.type = GDK_KEY_RELEASE;
  67. gtk_main_do_event(event.get());
  68. }
  69. void PlatformWebView::simulateSpacebarKeyPress()
  70. {
  71. GtkWidget* viewWidget = GTK_WIDGET(m_view);
  72. if (!gtk_widget_get_realized(viewWidget))
  73. gtk_widget_show(m_window);
  74. doKeyStroke(viewWidget, GDK_KEY_KP_Space);
  75. }
  76. void PlatformWebView::simulateAltKeyPress()
  77. {
  78. GtkWidget* viewWidget = GTK_WIDGET(m_view);
  79. if (!gtk_widget_get_realized(viewWidget))
  80. gtk_widget_show(m_window);
  81. doKeyStroke(viewWidget, GDK_KEY_Alt_L);
  82. }
  83. static void doMouseButtonEvent(GtkWidget* viewWidget, GdkEventType eventType, int x, int y, unsigned int button)
  84. {
  85. GOwnPtr<GdkEvent> event(gdk_event_new(eventType));
  86. event->button.x = x;
  87. event->button.y = y;
  88. event->button.button = button;
  89. event->button.time = GDK_CURRENT_TIME;
  90. event->button.axes = 0;
  91. event->button.state = 0;
  92. event->button.window = gtk_widget_get_window(viewWidget);
  93. g_object_ref(event->button.window);
  94. event->button.device = gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(gtk_widget_get_display(viewWidget)));
  95. int xRoot, yRoot;
  96. gdk_window_get_root_coords(gtk_widget_get_window(viewWidget), x, y, &xRoot, &yRoot);
  97. event->button.x_root = xRoot;
  98. event->button.y_root = yRoot;
  99. gtk_main_do_event(event.get());
  100. }
  101. void PlatformWebView::simulateRightClick(unsigned x, unsigned y)
  102. {
  103. GtkWidget* viewWidget = GTK_WIDGET(m_view);
  104. if (!gtk_widget_get_realized(viewWidget))
  105. gtk_widget_show(m_window);
  106. doMouseButtonEvent(viewWidget, GDK_BUTTON_PRESS, x, y, 3);
  107. doMouseButtonEvent(viewWidget, GDK_BUTTON_RELEASE, x, y, 3);
  108. }
  109. void PlatformWebView::simulateMouseMove(unsigned x, unsigned y)
  110. {
  111. GOwnPtr<GdkEvent> event(gdk_event_new(GDK_MOTION_NOTIFY));
  112. event->motion.x = x;
  113. event->motion.y = y;
  114. event->motion.time = GDK_CURRENT_TIME;
  115. event->motion.state = 0;
  116. event->motion.axes = 0;
  117. GtkWidget* viewWidget = GTK_WIDGET(m_view);
  118. if (!gtk_widget_get_realized(viewWidget))
  119. gtk_widget_show(m_window);
  120. event->motion.window = gtk_widget_get_window(viewWidget);
  121. g_object_ref(event->motion.window);
  122. event->motion.device = gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(gtk_widget_get_display(viewWidget)));
  123. int xRoot, yRoot;
  124. gdk_window_get_root_coords(gtk_widget_get_window(viewWidget), x, y, &xRoot, &yRoot);
  125. event->motion.x_root = xRoot;
  126. event->motion.y_root = yRoot;
  127. gtk_main_do_event(event.get());
  128. }
  129. } // namespace TestWebKitAPI