/src/3rdparty/webkit/Source/WebCore/page/win/FrameCGWin.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 115 lines · 66 code · 25 blank · 24 comment · 1 complexity · 302f7da1351bcca94bbc761749348918 MD5 · raw file

  1. /*
  2. * Copyright (C) 2006, 2007, 2008 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 COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "FrameWin.h"
  27. #include <windows.h>
  28. #include "BitmapInfo.h"
  29. #include "FrameView.h"
  30. #include "GraphicsContextCG.h"
  31. #include "RenderObject.h"
  32. #include "Settings.h"
  33. #include <CoreGraphics/CoreGraphics.h>
  34. using std::min;
  35. namespace WebCore {
  36. static void drawRectIntoContext(IntRect rect, FrameView* view, GraphicsContext* gc)
  37. {
  38. IntSize offset = view->scrollOffset();
  39. rect.move(-offset.width(), -offset.height());
  40. rect = view->convertToContainingWindow(rect);
  41. gc->concatCTM(AffineTransform().translate(-rect.x(), -rect.y()));
  42. view->paint(gc, rect);
  43. }
  44. static HBITMAP imageFromRect(const Frame* frame, IntRect& ir)
  45. {
  46. PaintBehavior oldPaintBehavior = frame->view()->paintBehavior();
  47. frame->view()->setPaintBehavior(oldPaintBehavior | PaintBehaviorFlattenCompositingLayers);
  48. void* bits;
  49. HDC hdc = CreateCompatibleDC(0);
  50. int w = ir.width();
  51. int h = ir.height();
  52. BitmapInfo bmp = BitmapInfo::create(IntSize(w, h));
  53. HBITMAP hbmp = CreateDIBSection(0, &bmp, DIB_RGB_COLORS, static_cast<void**>(&bits), 0, 0);
  54. HBITMAP hbmpOld = static_cast<HBITMAP>(SelectObject(hdc, hbmp));
  55. CGContextRef context = CGBitmapContextCreate(static_cast<void*>(bits), w, h,
  56. 8, w * sizeof(RGBQUAD), deviceRGBColorSpaceRef(), kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
  57. CGContextSaveGState(context);
  58. GraphicsContext gc(context);
  59. drawRectIntoContext(ir, frame->view(), &gc);
  60. CGContextRelease(context);
  61. SelectObject(hdc, hbmpOld);
  62. DeleteDC(hdc);
  63. frame->view()->setPaintBehavior(oldPaintBehavior);
  64. return hbmp;
  65. }
  66. HBITMAP imageFromSelection(Frame* frame, bool forceBlackText)
  67. {
  68. frame->document()->updateLayout();
  69. frame->view()->setPaintBehavior(PaintBehaviorSelectionOnly | (forceBlackText ? PaintBehaviorForceBlackText : 0));
  70. FloatRect fr = frame->selection()->bounds();
  71. IntRect ir(static_cast<int>(fr.x()), static_cast<int>(fr.y()),
  72. static_cast<int>(fr.width()), static_cast<int>(fr.height()));
  73. HBITMAP image = imageFromRect(frame, ir);
  74. frame->view()->setPaintBehavior(PaintBehaviorNormal);
  75. return image;
  76. }
  77. DragImageRef Frame::nodeImage(Node* node)
  78. {
  79. RenderObject* renderer = node->renderer();
  80. if (!renderer)
  81. return 0;
  82. IntRect topLevelRect;
  83. IntRect paintingRect = renderer->paintingRootRect(topLevelRect);
  84. document()->updateLayout();
  85. m_view->setNodeToDraw(node); // invoke special sub-tree drawing mode
  86. HBITMAP result = imageFromRect(this, paintingRect);
  87. m_view->setNodeToDraw(0);
  88. return result;
  89. }
  90. } // namespace WebCore