1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include <drawinglayer/primitive2d/controlprimitive2d.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <comphelper/processfactory.hxx>
#include <com/sun/star/awt/XWindow2.hpp>
#include <drawinglayer/geometry/viewinformation2d.hxx>
#include <vcl/virdev.hxx>
#include <vcl/svapp.hxx>
#include <com/sun/star/awt/PosSize.hpp>
#include <vcl/bitmapex.hxx>
#include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
#include <tools/diagnose_ex.h>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>
#include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
#include <svtools/optionsdrawinglayer.hxx>
#include <toolkit/awt/vclxwindow.hxx>
#include <vcl/window.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>
//////////////////////////////////////////////////////////////////////////////
using namespace com::sun::star;
//////////////////////////////////////////////////////////////////////////////
namespace drawinglayer
{
namespace primitive2d
{
void ControlPrimitive2D::createXControl()
{
if(!mxXControl.is() && getControlModel().is())
{
uno::Reference< beans::XPropertySet > xSet(getControlModel(), uno::UNO_QUERY);
if(xSet.is())
{
uno::Any aValue(xSet->getPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultControl"))));
rtl::OUString aUnoControlTypeName;
if(aValue >>= aUnoControlTypeName)
{
if(aUnoControlTypeName.getLength())
{
uno::Reference< lang::XMultiServiceFactory > xFactory( comphelper::getProcessServiceFactory() );
if(xFactory.is())
{
uno::Reference< awt::XControl > xXControl(xFactory->createInstance(aUnoControlTypeName), uno::UNO_QUERY);
if(xXControl.is())
{
xXControl->setModel(getControlModel());
// remember XControl
mxXControl = xXControl;
}
}
}
}
}
}
}
Primitive2DReference ControlPrimitive2D::createBitmapDecomposition(const geometry::ViewInformation2D& rViewInformation) const
{
Primitive2DReference xRetval;
const uno::Reference< awt::XControl >& rXControl(getXControl());
if(rXControl.is())
{
uno::Reference< awt::XWindow > xControlWindow(rXControl, uno::UNO_QUERY);
if(xControlWindow.is())
{
// get decomposition to get size
basegfx::B2DVector aScale, aTranslate;
double fRotate, fShearX;
getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
// get absolute discrete size (no mirror or rotate here)
aScale = basegfx::absolute(aScale);
basegfx::B2DVector aDiscreteSize(rViewInformation.getObjectToViewTransformation() * aScale);
// limit to a maximum square size, e.g. 300x150 pixels (45000)
const SvtOptionsDrawinglayer aDrawinglayerOpt;
const double fDiscreteMax(aDrawinglayerOpt.GetQuadraticFormControlRenderLimit());
const double fDiscreteQuadratic(aDiscreteSize.getX() * aDiscreteSize.getY());
const bool bScaleUsed(fDiscreteQuadratic > fDiscreteMax);
double fFactor(1.0);
if(bScaleUsed)
{
// get factor and adapt to scaled size
fFactor = sqrt(fDiscreteMax / fDiscreteQuadratic);
aDiscreteSize *= fFactor;
}
// go to integer
const sal_Int32 nSizeX(basegfx::fround(aDiscreteSize.getX()));
const sal_Int32 nSizeY(basegfx::fround(aDiscreteSize.getY()));
if(nSizeX > 0 && nSizeY > 0)
{
// prepare VirtualDevice
VirtualDevice aVirtualDevice(*Application::GetDefaultDevice());
const Size aSizePixel(nSizeX, nSizeY);
aVirtualDevice.SetOutputSizePixel(aSizePixel);
// set size at control
xControlWindow->setPosSize(0, 0, nSizeX, nSizeY, awt::PosSize::POSSIZE);
// get graphics and view
uno::Reference< awt::XGraphics > xGraphics(aVirtualDevice.CreateUnoGraphics());
uno::Reference< awt::XView > xControlView(rXControl, uno::UNO_QUERY);
if(xGraphics.is() && xControlView.is())
{
// link graphics and view
xControlView->setGraphics(xGraphics);
{ // #i93162# For painting the control setting a Zoom (using setZoom() at the xControlView)
// is needed to define the font size. Normally this is done in
// ViewObjectContactOfUnoControl::createPrimitive2DSequence by using positionControlForPaint().
// For some reason the difference between MAP_TWIPS and MAP_100TH_MM still plays
// a role there so that for Draw/Impress/Calc (the MAP_100TH_MM users) i need to set a zoom
// here, too. The factor includes the needed scale, but is calculated by pure comparisons. It
// is somehow related to the twips/100thmm relationship.
bool bUserIs100thmm(false);
const uno::Reference< awt::XControl > xControl(xControlView, uno::UNO_QUERY);
if(xControl.is())
{
uno::Reference< awt::XWindowPeer > xWindowPeer(xControl->getPeer());
if(xWindowPeer.is())
{
VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation(xWindowPeer);
if(pVCLXWindow)
{
Window* pWindow = pVCLXWindow->GetWindow();
if(pWindow)
{
pWindow = pWindow->GetParent();
if(pWindow)
{
if(MAP_100TH_MM == pWindow->GetMapMode().GetMapUnit())
{
bUserIs100thmm = true;
}
}
}
}
}
}
if(bUserIs100thmm)
{
// calc screen zoom for text display. fFactor is already added indirectly in aDiscreteSize
basegfx::B2DVector aScreenZoom(
basegfx::fTools::equalZero(aScale.getX()) ? 1.0 : aDiscreteSize.getX() / aScale.getX(),
basegfx::fTools::equalZero(aScale.getY()) ? 1.0 : aDiscreteSize.getY() / aScale.getY());
static double fZoomScale(28.0); // do not ask for this constant factor, but it gets the zoom right
aScreenZoom *= fZoomScale;
// set zoom at control view for text scaling
xControlView->setZoom((float)aScreenZoom.getX(), (float)aScreenZoom.getY());
}
}
try
{
// try to paint it to VirtualDevice
xControlView->draw(0, 0);
// get bitmap
const Bitmap aContent(aVirtualDevice.GetBitmap(Point(), aSizePixel));
// to avoid scaling, use the Bitmap pixel size as primitive size
const Size aBitmapSize(aContent.GetSizePixel());
basegfx::B2DVector aBitmapSizeLogic(
rViewInformation.getInverseObjectToViewTransformation() *
basegfx::B2DVector(aBitmapSize.getWidth() - 1, aBitmapSize.getHeight() - 1));
if(bScaleUsed)
{
// if scaled adapt to scaled size
aBitmapSizeLogic /= fFactor;
}
// short form for scale and translate transformation
const basegfx::B2DHomMatrix aBitmapTransform(basegfx::tools::createScaleTranslateB2DHomMatrix(
aBitmapSizeLogic.getX(), aBitmapSizeLogic.getY(), aTranslate.getX(), aTranslate.getY()));
// create primitive
xRetval = new BitmapPrimitive2D(BitmapEx(aContent), aBitmapTransform);
}
catch( const uno::Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
}
}
}
}
return xRetval;
}
Primitive2DReference ControlPrimitive2D::createPlaceholderDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
{
// create a gray placeholder hairline polygon in object size
basegfx::B2DRange aObjectRange(0.0, 0.0, 1.0, 1.0);
aObjectRange.transform(getTransform());
const basegfx::B2DPolygon aOutline(basegfx::tools::createPolygonFromRect(aObjectRange));
const basegfx::BColor aGrayTone(0xc0 / 255.0, 0xc0 / 255.0, 0xc0 / 255.0);
// The replacement object may also get a text like 'empty group' here later
Primitive2DReference xRetval(new PolygonHairlinePrimitive2D(aOutline, aGrayTone));
return xRetval;
}
Primitive2DSequence ControlPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
{
// try to create a bitmap decomposition. If that fails for some reason,
// at least create a replacement decomposition.
Primitive2DReference xReference(createBitmapDecomposition(rViewInformation));
if(!xReference.is())
{
xReference = createPlaceholderDecomposition(rViewInformation);
}
return Primitive2DSequence(&xReference, 1L);
}
ControlPrimitive2D::ControlPrimitive2D(
const basegfx::B2DHomMatrix& rTransform,
const uno::Reference< awt::XControlModel >& rxControlModel)
: BufferedDecompositionPrimitive2D(),
maTransform(rTransform),
mxControlModel(rxControlModel),
mxXControl(),
maLastViewScaling()
{
}
ControlPrimitive2D::ControlPrimitive2D(
const basegfx::B2DHomMatrix& rTransform,
const uno::Reference< awt::XControlModel >& rxControlModel,
const uno::Reference< awt::XControl >& rxXControl)
: BufferedDecompositionPrimitive2D(),
maTransform(rTransform),
mxControlModel(rxControlModel),
mxXControl(rxXControl),
maLastViewScaling()
{
}
const uno::Reference< awt::XControl >& ControlPrimitive2D::getXControl() const
{
if(!mxXControl.is())
{
const_cast< ControlPrimitive2D* >(this)->createXControl();
}
return mxXControl;
}
bool ControlPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
{
// use base class compare operator
if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
{
const ControlPrimitive2D& rCompare = (ControlPrimitive2D&)rPrimitive;
if(getTransform() == rCompare.getTransform())
{
// check if ControlModel references both are/are not
bool bRetval(getControlModel().is() == rCompare.getControlModel().is());
if(bRetval && getControlModel().is())
{
// both exist, check for equality
bRetval = (getControlModel() == rCompare.getControlModel());
}
if(bRetval)
{
// check if XControl references both are/are not
bRetval = (getXControl().is() == rCompare.getXControl().is());
}
if(bRetval && getXControl().is())
{
// both exist, check for equality
bRetval = (getXControl() == rCompare.getXControl());
}
return bRetval;
}
}
return false;
}
basegfx::B2DRange ControlPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
{
// simply derivate from unit range
basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
aRetval.transform(getTransform());
return aRetval;
}
Primitive2DSequence ControlPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
{
// this primitive is view-dependent related to the scaling. If scaling has changed,
// destroy existing decomposition. To detect change, use size of unit size in view coordinates
::osl::MutexGuard aGuard( m_aMutex );
const basegfx::B2DVector aNewScaling(rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(1.0, 1.0));
if(getBuffered2DDecomposition().hasElements())
{
if(!maLastViewScaling.equal(aNewScaling))
{
// conditions of last local decomposition have changed, delete
const_cast< ControlPrimitive2D* >(this)->setBuffered2DDecomposition(Primitive2DSequence());
}
}
if(!getBuffered2DDecomposition().hasElements())
{
// remember ViewTransformation
const_cast< ControlPrimitive2D* >(this)->maLastViewScaling = aNewScaling;
}
// use parent implementation
return BufferedDecompositionPrimitive2D::get2DDecomposition(rViewInformation);
}
// provide unique ID
ImplPrimitrive2DIDBlock(ControlPrimitive2D, PRIMITIVE2D_ID_CONTROLPRIMITIVE2D)
} // end of namespace primitive2d
} // end of namespace drawinglayer
//////////////////////////////////////////////////////////////////////////////
// eof
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|