Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

viewportlisteners.cpp 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. *
  3. * Copyright (C) 2008 Opera Software AS. All rights reserved.
  4. *
  5. * This file is part of the Opera web browser.
  6. * It may not be distributed under any circumstances.
  7. */
  8. #include "core/pch.h"
  9. #include "modules/dochand/viewportlisteners.h"
  10. #include "modules/debug/debug.h"
  11. #include "modules/dochand/win.h"
  12. // #define DEBUG_VIEWPORT_LISTENER
  13. /**
  14. * This macro can be used to add an OpRect to the OP_DBG debug
  15. * statement. You need to add the debug string formatter
  16. * "(%d,%d)+%dx%d" at the correct place to use this macro.
  17. *
  18. * Example:
  19. * \code
  20. * OpRect my_rect = ...;
  21. * OP_DBG((UNI_L("rectangle: (%d,%d)+%dx%d"), _DBG_OP_RECT(my_rect)));
  22. * \endcode
  23. * @param rect is a GogiRect instance.
  24. */
  25. #define _DBG_OP_RECT(rect) rect.x, rect.y, rect.width, rect.height
  26. void DefaultViewportRequestListener::AdjustRenderingViewport(OpViewportController* controller, const OpRect& new_visual_viewport)
  27. {
  28. OP_NEW_DBG("DefaultViewportRequestListener::AdjustRenderingViewport", "viewport.listener");
  29. OP_DBG(("new visual viewport: (%d,%d)+%dx%d", _DBG_OP_RECT(new_visual_viewport)));
  30. // Move rendering viewport if it no longer contains the visual viewport
  31. OpRect rendering_viewport = controller->GetRenderingViewport();
  32. OpPoint new_rendering_pos(rendering_viewport.x, rendering_viewport.y);
  33. BOOL visual_viewport_wider = rendering_viewport.width < new_visual_viewport.width;
  34. BOOL visual_viewport_higher = rendering_viewport.height < new_visual_viewport.height;
  35. /* If visual viewport is wider we need to make an opposite comparison
  36. (adjust only if the rendering viewport is outside the visual one.
  37. The same concerns every edge. */
  38. if (visual_viewport_wider ? new_visual_viewport.x > rendering_viewport.x : new_visual_viewport.x < rendering_viewport.x)
  39. new_rendering_pos.x = new_visual_viewport.x;
  40. else
  41. {
  42. INT32 right_edge_diff = new_visual_viewport.Right() - rendering_viewport.Right();
  43. if (visual_viewport_wider ? right_edge_diff < 0 : right_edge_diff > 0)
  44. new_rendering_pos.x = new_visual_viewport.Right() - rendering_viewport.width;
  45. }
  46. if (visual_viewport_higher ? new_visual_viewport.y > rendering_viewport.y : new_visual_viewport.y < rendering_viewport.y)
  47. new_rendering_pos.y = new_visual_viewport.y;
  48. else
  49. {
  50. INT32 bottom_edge_diff = new_visual_viewport.Bottom() - rendering_viewport.Bottom();
  51. if (visual_viewport_higher ? bottom_edge_diff < 0 : bottom_edge_diff > 0)
  52. new_rendering_pos.y = new_visual_viewport.Bottom() - rendering_viewport.height;
  53. }
  54. if (new_rendering_pos.x != rendering_viewport.x || new_rendering_pos.y != rendering_viewport.y)
  55. {
  56. unsigned int doc_width, doc_height;
  57. controller->GetDocumentSize(&doc_width, &doc_height);
  58. // Keep the new rendering viewport within the document's bounds.
  59. if (new_rendering_pos.x + rendering_viewport.width > (int) doc_width)
  60. new_rendering_pos.x = doc_width - rendering_viewport.width;
  61. if (new_rendering_pos.y + rendering_viewport.height > (int) doc_height)
  62. new_rendering_pos.y = doc_height - rendering_viewport.height;
  63. if (new_rendering_pos.x < 0)
  64. new_rendering_pos.x = 0;
  65. if (new_rendering_pos.y < 0)
  66. new_rendering_pos.y = 0;
  67. OP_DBG(("new rendering viewport position: (%d,%d)", new_rendering_pos.x, new_rendering_pos.y));
  68. controller->SetRenderingViewportPos(new_rendering_pos);
  69. }
  70. }
  71. void DefaultViewportRequestListener::OnVisualViewportChangeRequest(OpViewportController* controller, const OpRect& viewport, const OpRect& priority_rect, OpViewportChangeReason reason, DirectionHint* dir_hint)
  72. {
  73. OP_NEW_DBG("DefaultViewportRequestListener::OnVisualViewportChangeRequest", "viewport.listener");
  74. OP_DBG(("viewport=(%d,%d)+%dx%d priority_rect=(%d,%d)+%dx%d reason=%d", _DBG_OP_RECT(viewport), _DBG_OP_RECT(priority_rect), reason));
  75. OpRect new_viewport_adjusted = viewport;
  76. if (controller->IsVisualViewportSizeLocked())
  77. {
  78. /* If visual viewport size is locked, simply ignore the (new) viewport
  79. dimension. Perhaps this can be improved to honor the DirectionHint. */
  80. OpRect cur_visual_viewport = controller->GetVisualViewport();
  81. new_viewport_adjusted.width = cur_visual_viewport.width;
  82. new_viewport_adjusted.height = cur_visual_viewport.height;
  83. }
  84. controller->SetBufferedMode(TRUE);
  85. AdjustRenderingViewport(controller, new_viewport_adjusted);
  86. controller->SetVisualViewport(new_viewport_adjusted);
  87. controller->SetBufferedMode(FALSE);
  88. }
  89. void DefaultViewportRequestListener::OnVisualViewportEdgeChangeRequest(OpViewportController* controller, const ViewportPosition& pos, OpViewportChangeReason reason, DirectionHint* dir_hint)
  90. {
  91. OP_NEW_DBG("DefaultViewportRequestListener::OnVisualViewportEdgeChangeRequest", "viewport.listener");
  92. OP_DBG(("viewport position=(%d,%d) %s-%s reason=%d", pos.point.x, pos.point.y, (pos.IsTopEdge() ? "top":"bottom"), (pos.IsLeftEdge() ? "left":"right"), reason));
  93. OpRect new_visual_viewport = controller->GetVisualViewport();
  94. if (pos.hor_edge == EDGE_LEFT)
  95. new_visual_viewport.x = pos.point.x;
  96. else
  97. new_visual_viewport.x = pos.point.x - (new_visual_viewport.width - 1);
  98. if (pos.vert_edge == EDGE_TOP)
  99. new_visual_viewport.y = pos.point.y;
  100. else
  101. new_visual_viewport.y = pos.point.y - (new_visual_viewport.height - 1);
  102. controller->SetBufferedMode(TRUE);
  103. AdjustRenderingViewport(controller, new_visual_viewport);
  104. controller->SetVisualViewportPos(OpPoint(new_visual_viewport.x, new_visual_viewport.y));
  105. controller->SetBufferedMode(FALSE);
  106. }
  107. #ifdef DOC_SEND_POIMOVED
  108. void DefaultViewportRequestListener::OnPOIMoved(OpViewportController* controller, const POIData& element_data)
  109. {
  110. OpRect new_visual_viewport = controller->GetVisualViewport();
  111. new_visual_viewport.x += element_data.movement.x;
  112. new_visual_viewport.y += element_data.movement.y;
  113. controller->SetBufferedMode(TRUE);
  114. AdjustRenderingViewport(controller, new_visual_viewport);
  115. controller->SetVisualViewportPos(OpPoint(new_visual_viewport.x, new_visual_viewport.y));
  116. controller->SetBufferedMode(FALSE);
  117. }
  118. #endif // DOC_SEND_POIMOVED
  119. void DefaultViewportRequestListener::OnZoomLevelChangeRequest(OpViewportController* controller, double zoom_level, const OpRect* priority_rect, OpViewportChangeReason reason)
  120. {
  121. OP_NEW_DBG("DefaultViewportRequestListener::OnZoomLevelChangeRequest", "viewport.listener");
  122. OP_DBG(("requested zoom-level: %f", zoom_level));
  123. switch (reason) {
  124. // Ignore the initial zoom-level specified in a viewport
  125. // meta-tag:
  126. case VIEWPORT_CHANGE_REASON_NEW_PAGE: break;
  127. case VIEWPORT_CHANGE_REASON_INPUT_ACTION:
  128. // execute the request for any input action like
  129. // ctrl+mouse-wheel:
  130. m_commander->SetScale(static_cast<int>(100 * zoom_level + 0.5));
  131. // note: if this listener is used, then the visual viewport is
  132. // locked to the window-size, so there is no need to adjust
  133. // the size of the visual viewport - and the priority_rect is
  134. // currently ignored...
  135. break;
  136. // ignore any other reason...
  137. }
  138. }
  139. void NullViewportInfoListener::OnNewPage(OpViewportController* controller, OpViewportChangeReason reason, int id)
  140. {
  141. OP_NEW_DBG("NullViewportInfoListener::OnNewPage", "viewport.listener");
  142. }
  143. void NullViewportInfoListener::OnNewPageReady(OpViewportController* controller)
  144. {
  145. OP_NEW_DBG("NullViewportInfoListener::OnNewPageReady", "viewport.listener");
  146. }
  147. void NullViewportInfoListener::OnZoomLevelLimitsChanged(OpViewportController* controller, double min_zoom_level, double max_zoom_level, BOOL user_zoomable)
  148. {
  149. OP_NEW_DBG("NullViewportInfoListener::OnZoomLevelLimitsChanged", "viewport.listener");
  150. OP_DBG(("min: %f; max: %f; user_zoomable: %d", min_zoom_level, max_zoom_level, user_zoomable));
  151. }
  152. void NullViewportInfoListener::OnLayoutViewportSizeChanged(OpViewportController* controller, unsigned int width, unsigned int height)
  153. {
  154. OP_NEW_DBG("NullViewportInfoListener::OnLayoutViewportSizeChanged", "viewport.listener");
  155. OP_DBG(("layout viewport size: %dx%d", width, height));
  156. }
  157. void NullViewportInfoListener::OnDocumentSizeChanged(OpViewportController* controller, unsigned int width, unsigned int height)
  158. {
  159. OP_NEW_DBG("NullViewportInfoListener::OnDocumentSizeChanged", "viewport.listener");
  160. OP_DBG(("document size: %dx%d", width, height));
  161. }
  162. void NullViewportInfoListener::OnDocumentContentChanged(OpViewportController* controller, CONTENT_CHANGED_REASON reason)
  163. {
  164. OP_NEW_DBG("NullViewportInfoListener::OnDocumentContentChanged", "viewport.listener");
  165. }
  166. #ifdef CONTENT_MAGIC
  167. void NullViewportInfoListener::OnContentMagicFound(OpViewportController* controller, const OpRect& content_magic_rect)
  168. {
  169. OP_NEW_DBG("NullViewportInfoListener::OnContentMagicFound", "viewport.listener");
  170. OP_DBG(("content-magic: (%d,%d)+%dx%d", _DBG_OP_RECT(content_magic_rect)));
  171. }
  172. #endif // CONTENT_MAGIC
  173. void NullViewportInfoListener::OnReservedRegionChanged(OpViewportController* controller, const OpRect* rect_array, unsigned int num_rects)
  174. {
  175. OP_NEW_DBG("NullViewportInfoListener::OnReservedRegionChanged", "viewport.listener");
  176. OP_DBG(("%d rects", num_rects));
  177. }
  178. #ifdef ADAPTIVE_ZOOM_SUPPORT
  179. void NullViewportInfoListener::OnAreaOfInterestChanged(OpViewportController* controller, const OpRect& primary_rect, const OpRect& secondary_rect)
  180. {
  181. OP_NEW_DBG("NullViewportInfoListener::OnAreaOfInterestChanged", "viewport.listener.aoi");
  182. OP_DBG(("primary: (%d,%d)+%dx%d; secondary: (%d,%d)+%dx%d", _DBG_OP_RECT(primary_rect), _DBG_OP_RECT(secondary_rect)));
  183. }
  184. #endif // ADAPTIVE_ZOOM_SUPPORT
  185. void NullViewportInfoListener::OnTrueZoomBaseScaleChanged(OpViewportController* controller, unsigned int scale_percentage)
  186. {
  187. OP_NEW_DBG("NullViewportInfoListener::OnTrueZoomBaseScaleChanged", "viewport.listener");
  188. OP_DBG(("base scale: %d", scale_percentage));
  189. }
  190. #ifdef PAGED_MEDIA_SUPPORT
  191. void NullViewportInfoListener::OnPageChanged(int new_page_number, int new_page_count)
  192. {
  193. OP_NEW_DBG("NullViewportInfoListener::OnPageChanged", "viewport.listener");
  194. OP_DBG(("new_page_number: %d; new_page_count: %d", new_page_number, new_page_count));
  195. }
  196. #endif // PAGED_MEDIA_SUPPORT
  197. #undef _DBG_OP_RECT