LibreOffice Module sw (master) 1
chartins.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sfx2/viewfrm.hxx>
21#include <sfx2/dispatch.hxx>
23#include <osl/diagnose.h>
24
25#include <swmodule.hxx>
26#include <wrtsh.hxx>
27#include <docsh.hxx>
28#include <view.hxx>
29#include <chartins.hxx>
30#include <tablemgr.hxx>
31#include <frmfmt.hxx>
32#include <unochart.hxx>
33
34#include <edtwin.hxx>
35
36#include <cmdid.h>
37#include <anchoredobject.hxx>
38
41#include <com/sun/star/awt/Point.hpp>
42#include <com/sun/star/awt/Size.hpp>
43#include <com/sun/star/awt/XWindow.hpp>
45#include <com/sun/star/chart2/data/XDataProvider.hpp>
46#include <com/sun/star/frame/XModel.hpp>
47#include <com/sun/star/lang/XInitialization.hpp>
48#include <com/sun/star/lang/XMultiComponentFactory.hpp>
49#include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
50
51using namespace ::com::sun::star;
52using namespace ::com::sun::star::uno;
53
54Point SwGetChartDialogPos( const vcl::Window *pParentWin, const Size& rDialogSize, const tools::Rectangle& rLogicChart )
55{
56 // positioning code according to spec; similar to Calc fuins2.cxx
57 Point aRet;
58
59 OSL_ENSURE( pParentWin, "Window not found" );
60 if (pParentWin)
61 {
62 tools::Rectangle aObjPixel = pParentWin->LogicToPixel( rLogicChart, pParentWin->GetMapMode() );
63 tools::Rectangle aObjAbs( pParentWin->OutputToAbsoluteScreenPixel( aObjPixel.TopLeft() ),
64 pParentWin->OutputToAbsoluteScreenPixel( aObjPixel.BottomRight() ) );
65
66 tools::Rectangle aDesktop = pParentWin->GetDesktopRectPixel();
67 Size aSpace = pParentWin->LogicToPixel(Size(8, 12), MapMode(MapUnit::MapAppFont));
68
69 bool bCenterHor = false;
70
71 if ( aDesktop.Bottom() - aObjAbs.Bottom() >= rDialogSize.Height() + aSpace.Height() )
72 {
73 // first preference: below the chart
74 aRet.setY( aObjAbs.Bottom() + aSpace.Height() );
75 bCenterHor = true;
76 }
77 else if ( aObjAbs.Top() - aDesktop.Top() >= rDialogSize.Height() + aSpace.Height() )
78 {
79 // second preference: above the chart
80 aRet.setY( aObjAbs.Top() - rDialogSize.Height() - aSpace.Height() );
81 bCenterHor = true;
82 }
83 else
84 {
85 bool bFitLeft = ( aObjAbs.Left() - aDesktop.Left() >= rDialogSize.Width() + aSpace.Width() );
86 bool bFitRight = ( aDesktop.Right() - aObjAbs.Right() >= rDialogSize.Width() + aSpace.Width() );
87
88 if ( bFitLeft || bFitRight )
89 {
90 if (SwView* pView = GetActiveView())
91 {
92 // if both fit, prefer right in RTL mode, left otherwise
93 bool bLayoutRTL = pView->GetWrtShell().IsTableRightToLeft();
94 bool bPutRight = bFitRight && ( bLayoutRTL || !bFitLeft );
95 if ( bPutRight )
96 aRet.setX( aObjAbs.Right() + aSpace.Width() );
97 else
98 aRet.setX( aObjAbs.Left() - rDialogSize.Width() - aSpace.Width() );
99 }
100
101 // center vertically
102 aRet.setY( aObjAbs.Top() + ( aObjAbs.GetHeight() - rDialogSize.Height() ) / 2 );
103 }
104 else
105 {
106 // doesn't fit on any edge - put at the bottom of the screen
107 aRet.setY( aDesktop.Bottom() - rDialogSize.Height() );
108 bCenterHor = true;
109 }
110 }
111 if ( bCenterHor )
112 aRet.setX( aObjAbs.Left() + ( aObjAbs.GetWidth() - rDialogSize.Width() ) / 2 );
113
114 // limit to screen (centering might lead to invalid positions)
115 if ( aRet.X() + rDialogSize.Width() - 1 > aDesktop.Right() )
116 aRet.setX( aDesktop.Right() - rDialogSize.Width() + 1 );
117 if ( aRet.X() < aDesktop.Left() )
118 aRet.setX( aDesktop.Left() );
119 if ( aRet.Y() + rDialogSize.Height() - 1 > aDesktop.Bottom() )
120 aRet.setY( aDesktop.Bottom() - rDialogSize.Height() + 1 );
121 if ( aRet.Y() < aDesktop.Top() )
122 aRet.setY( aDesktop.Top() );
123 }
124
125 return aRet;
126}
127
129{
130 SwView *pView = ::GetActiveView();
131 if (!pView)
132 return;
133
134 // get range string of marked data
135 SwWrtShell &rWrtShell = pView->GetWrtShell();
136 uno::Reference< chart2::data::XDataProvider > xDataProvider;
137 uno::Reference< frame::XModel > xChartModel;
138 OUString aRangeString;
139
140 if( rWrtShell.IsCursorInTable())
141 {
142 if (!rWrtShell.IsTableMode())
143 {
144 // select whole table
145 rWrtShell.GetView().GetViewFrame().GetDispatcher()->
146 Execute(FN_TABLE_SELECT_ALL, SfxCallMode::SYNCHRON);
147 }
148 if( ! rWrtShell.IsTableComplexForChart())
149 {
150 SwFrameFormat* pTableFormat = rWrtShell.GetTableFormat();
151 aRangeString = pTableFormat->GetName() + "." + rWrtShell.GetBoxNms();
152
153 // get table data provider
154 xDataProvider.set( pView->GetDocShell()->getIDocumentChartDataProviderAccess().GetChartDataProvider( true ) );
155 }
156 }
157
158 SwFlyFrameFormat *pFlyFrameFormat = nullptr;
159 xChartModel.set( SwTableFUNC( &rWrtShell ).InsertChart( xDataProvider, xDataProvider.is(), aRangeString, &pFlyFrameFormat ));
160
161 //open wizard
162 //@todo get context from writer if that has one
163 uno::Reference< uno::XComponentContext > xContext(
164 ::cppu::defaultBootstrap_InitialComponentContext() );
165 if( !(xContext.is() && xChartModel.is() && xDataProvider.is()))
166 return;
167
168 uno::Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
169 if(!xMCF.is())
170 return;
171
172 uno::Reference< ui::dialogs::XAsynchronousExecutableDialog > xDialog(
173 xMCF->createInstanceWithContext(
174 "com.sun.star.comp.chart2.WizardDialog", xContext),
175 uno::UNO_QUERY);
176 uno::Reference< lang::XInitialization > xInit( xDialog, uno::UNO_QUERY );
177 if( xInit.is() )
178 {
179 // initialize dialog
180 uno::Sequence<uno::Any> aSeq(comphelper::InitAnyPropertySequence(
181 {
182 {"ParentWindow", uno::Any(uno::Reference< awt::XWindow >())},
183 {"ChartModel", uno::Any(xChartModel)}
184 }));
185 xInit->initialize( aSeq );
186
187 // try to set the dialog's position so it doesn't hide the chart
188 uno::Reference < beans::XPropertySet > xDialogProps( xDialog, uno::UNO_QUERY );
189 if ( xDialogProps.is() )
190 {
191 try
192 {
193 //get dialog size:
194 awt::Size aDialogAWTSize;
195 if( xDialogProps->getPropertyValue("Size")
196 >>= aDialogAWTSize )
197 {
198 Size aDialogSize( aDialogAWTSize.Width, aDialogAWTSize.Height );
199 if ( !aDialogSize.IsEmpty() )
200 {
201 //calculate and set new position
202 SwRect aSwRect;
203 if (pFlyFrameFormat)
204 aSwRect = pFlyFrameFormat->GetAnchoredObj()->GetObjRectWithSpaces();
205 tools::Rectangle aRect( aSwRect.SVRect() );
206 Point aDialogPos = SwGetChartDialogPos( &rWrtShell.GetView().GetEditWin(), aDialogSize, aRect );
207 xDialogProps->setPropertyValue("Position",
208 uno::Any( awt::Point(aDialogPos.getX(),aDialogPos.getY()) ) );
209 }
210 }
211 }
212 catch (const uno::Exception&)
213 {
214 OSL_FAIL("Chart wizard couldn't be positioned automatically" );
215 }
216 }
217
218 rtl::Reference<::svt::DialogClosedListener> pListener = new ::svt::DialogClosedListener();
219 pListener->SetDialogClosedLink( rLink );
220
221 xDialog->startExecuteModal( pListener );
222 }
223 else
224 {
225 uno::Reference< lang::XComponent > xComponent( xDialog, uno::UNO_QUERY );
226 if( xComponent.is())
227 xComponent->dispose();
228 }
229}
230
231/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Point SwGetChartDialogPos(const vcl::Window *pParentWin, const Size &rDialogSize, const tools::Rectangle &rLogicChart)
Definition: chartins.cxx:54
virtual SwChartDataProvider * GetChartDataProvider(bool bCreate=false) const =0
returns or creates the data-provider for chart
constexpr tools::Long Y() const
void setX(tools::Long nX)
void setY(tools::Long nY)
constexpr tools::Long X() const
constexpr tools::Long getX() const
constexpr tools::Long getY() const
SfxDispatcher * GetDispatcher()
SfxViewFrame & GetViewFrame() const
bool IsEmpty() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
const SwRect & GetObjRectWithSpaces() const
method to determine object area inclusive its spacing
bool IsTableComplexForChart()
Definition: trvltbl.cxx:723
OUString GetBoxNms() const
Definition: trvltbl.cxx:743
const SwTableNode * IsCursorInTable() const
Check if Point of current cursor is placed within a table.
Definition: crsrsh.cxx:600
bool IsTableMode() const
Definition: crsrsh.hxx:668
IDocumentChartDataProviderAccess & getIDocumentChartDataProviderAccess()
Definition: docshini.cxx:706
SwFrameFormat * GetTableFormat()
Definition: edws.cxx:183
SwAnchoredObject * GetAnchoredObj() const
Definition: atrfrm.cxx:3152
const OUString & GetName() const
Definition: format.hxx:131
Style of a layout element.
Definition: frmfmt.hxx:72
SwInsertChart(const Link< css::ui::dialogs::DialogClosedEvent *, void > &rLink)
Definition: chartins.cxx:128
Of course Writer needs its own rectangles.
Definition: swrect.hxx:35
tools::Rectangle SVRect() const
Definition: swrect.hxx:292
Definition: view.hxx:146
SwWrtShell & GetWrtShell() const
Definition: view.hxx:423
SwEditWin & GetEditWin()
Definition: view.hxx:426
SwDocShell * GetDocShell()
Definition: view.cxx:1193
Used by the UI to modify the document model.
Definition: wrtsh.hxx:97
const SwView & GetView() const
Definition: wrtsh.hxx:443
constexpr tools::Long GetWidth() const
constexpr tools::Long Top() const
constexpr Point TopLeft() const
constexpr tools::Long Right() const
constexpr Point BottomRight() const
constexpr tools::Long GetHeight() const
constexpr tools::Long Left() const
constexpr tools::Long Bottom() const
Point LogicToPixel(const Point &rLogicPt) const
tools::Rectangle GetDesktopRectPixel() const
const MapMode & GetMapMode() const
Point OutputToAbsoluteScreenPixel(const Point &rPos) const
#define FN_TABLE_SELECT_ALL
Definition: cmdid.h:394
Sequence< sal_Int8 > aSeq
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
const ::avmedia::MediaItem * Execute(const SdrMarkView *pSdrView, SfxRequest const &rReq)
SwView * GetActiveView()
Definition: swmodul1.cxx:115