LibreOffice Module sw (master)  1
PageOrientationControl.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 
21 #include "PageMarginControl.hxx"
22 #include <com/sun/star/document/XUndoManager.hpp>
23 #include <com/sun/star/document/XUndoManagerSupplier.hpp>
24 #include <com/sun/star/frame/XFrame.hpp>
25 
26 #include <swtypes.hxx>
27 #include <sfx2/viewsh.hxx>
28 #include <sfx2/dispatch.hxx>
29 #include <sfx2/viewfrm.hxx>
30 #include <svx/svxids.hrc>
31 #include <cmdid.h>
32 
33 namespace {
34  css::uno::Reference< css::document::XUndoManager > getUndoManager( const css::uno::Reference< css::frame::XFrame >& rxFrame )
35  {
36  const css::uno::Reference< css::frame::XController >& xController = rxFrame->getController();
37  if ( xController.is() )
38  {
39  const css::uno::Reference< css::frame::XModel >& xModel = xController->getModel();
40  if ( xModel.is() )
41  {
42  const css::uno::Reference< css::document::XUndoManagerSupplier > xSuppUndo( xModel, css::uno::UNO_QUERY_THROW );
43  return css::uno::Reference< css::document::XUndoManager >( xSuppUndo->getUndoManager(), css::uno::UNO_SET_THROW );
44  }
45  }
46 
47  return css::uno::Reference< css::document::XUndoManager > ();
48  }
49 }
50 
51 namespace sw { namespace sidebar {
52 
54  : SfxPopupWindow( nId, pParent, "PageOrientationControl", "modules/swriter/ui/pageorientationcontrol.ui" )
55  , mpPageItem( new SvxPageItem(SID_ATTR_PAGE) )
56  , mpPageSizeItem( new SvxSizeItem(SID_ATTR_PAGE_SIZE) )
57  , mpPageLRMarginItem( new SvxLongLRSpaceItem( 0, 0, SID_ATTR_PAGE_LRSPACE ) )
58  , mpPageULMarginItem( new SvxLongULSpaceItem( 0, 0, SID_ATTR_PAGE_ULSPACE ) )
59 {
60  get(m_pPortrait, "portrait");
61  get(m_pLandscape, "landscape");
62 
63  m_pPortrait->SetClickHdl( LINK( this, PageOrientationControl,ImplOrientationHdl ) );
64  m_pLandscape->SetClickHdl( LINK( this, PageOrientationControl,ImplOrientationHdl ) );
65 }
66 
68 {
69  disposeOnce();
70 }
71 
73 {
76 
77  mpPageItem.reset();
78  mpPageLRMarginItem.reset();
79  mpPageULMarginItem.reset();
80  mpPageSizeItem.reset();
81 
83 }
84 
86  const long nPageLeftMargin,
87  const long nPageRightMargin )
88 {
89  mpPageLRMarginItem->SetLeft( nPageLeftMargin );
90  mpPageLRMarginItem->SetRight( nPageRightMargin );
91  SfxViewShell::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_LRSPACE,
92  SfxCallMode::RECORD, { mpPageLRMarginItem.get() });
93 }
94 
96  const long nPageTopMargin,
97  const long nPageBottomMargin )
98 {
99  mpPageULMarginItem->SetUpper( nPageTopMargin );
100  mpPageULMarginItem->SetLower( nPageBottomMargin );
101  SfxViewShell::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_ULSPACE,
102  SfxCallMode::RECORD, { mpPageULMarginItem.get() });
103 }
104 
106 {
107  css::uno::Reference< css::document::XUndoManager > mxUndoManager(
108  getUndoManager( SfxViewFrame::Current()->GetFrame().GetFrameInterface() ) );
109 
110  if ( mxUndoManager.is() )
111  mxUndoManager->enterUndoContext( "" );
112 
113  const SfxPoolItem* pItem;
114  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_SIZE, pItem);
115  mpPageSizeItem.reset( static_cast<SvxSizeItem*>(pItem->Clone()) );
116 
117  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_LRSPACE, pItem);
118  mpPageLRMarginItem.reset( static_cast<SvxLongLRSpaceItem*>(pItem->Clone()) );
119 
120  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_ULSPACE, pItem);
121  mpPageULMarginItem.reset( static_cast<SvxLongULSpaceItem*>(pItem->Clone()) );
122 
123  {
124  // set new page orientation
125  mpPageItem->SetLandscape( bLandscape );
126 
127  // swap the width and height of the page size
128  const long nRotatedWidth = mpPageSizeItem->GetSize().Height();
129  const long nRotatedHeight = mpPageSizeItem->GetSize().Width();
130  mpPageSizeItem->SetSize(Size(nRotatedWidth, nRotatedHeight));
131 
132  // apply changed attributes
133  if (SfxViewShell::Current())
134  {
135  SfxViewShell::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
136  SfxCallMode::RECORD, { mpPageSizeItem.get(), mpPageItem.get() });
137  }
138  }
139 
140  // check, if margin values still fit to the changed page size.
141  // if not, adjust margin values
142  {
143  const long nML = mpPageLRMarginItem->GetLeft();
144  const long nMR = mpPageLRMarginItem->GetRight();
145  const long nTmpPW = nML + nMR + MINBODY;
146 
147  const long nPW = mpPageSizeItem->GetSize().Width();
148 
149  if ( nTmpPW > nPW )
150  {
151  if ( nML <= nMR )
152  {
153  ExecuteMarginLRChange( mpPageLRMarginItem->GetLeft(), nMR - (nTmpPW - nPW ) );
154  }
155  else
156  {
157  ExecuteMarginLRChange( nML - (nTmpPW - nPW ), mpPageLRMarginItem->GetRight() );
158  }
159  }
160 
161  const long nMT = mpPageULMarginItem->GetUpper();
162  const long nMB = mpPageULMarginItem->GetLower();
163  const long nTmpPH = nMT + nMB + MINBODY;
164 
165  const long nPH = mpPageSizeItem->GetSize().Height();
166 
167  if ( nTmpPH > nPH )
168  {
169  if ( nMT <= nMB )
170  {
171  ExecuteMarginULChange( mpPageULMarginItem->GetUpper(), nMB - ( nTmpPH - nPH ) );
172  }
173  else
174  {
175  ExecuteMarginULChange( nMT - ( nTmpPH - nPH ), mpPageULMarginItem->GetLower() );
176  }
177  }
178  }
179 
180  if ( mxUndoManager.is() )
181  mxUndoManager->leaveUndoContext();
182 }
183 
184 IMPL_LINK(PageOrientationControl, ImplOrientationHdl, Button*, pControl, void)
185 {
186  if ( pControl == m_pPortrait.get() )
187  ExecuteOrientationChange( false );
188  else
189  ExecuteOrientationChange( true );
190 
191  EndPopupMode();
192 }
193 
194 } } // end of namespace sw::sidebar
195 
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< SvxSizeItem > mpPageSizeItem
const SfxPoolItem * ExecuteList(sal_uInt16 nSlot, SfxCallMode nCall, std::initializer_list< SfxPoolItem const * > args, std::initializer_list< SfxPoolItem const * > internalargs=std::initializer_list< SfxPoolItem const * >())
void SetClickHdl(const Link< Button *, void > &rLink)
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
const css::uno::Reference< css::frame::XFrame > & GetFrame() const
Dialog to specify the properties of date form field.
Reference< XController > xController
static SfxViewShell * Current()
void ExecuteMarginLRChange(const long nPageLeftMargin, const long nPageRightMargin)
std::unique_ptr< SvxLongLRSpaceItem > mpPageLRMarginItem
void ExecuteMarginULChange(const long nPageTopMargin, const long nPageBottomMargin)
std::unique_ptr< SvxLongULSpaceItem > mpPageULMarginItem
SfxDispatcher * GetDispatcher() const
std::unique_ptr< SvxPageItem > mpPageItem
SfxBindings & GetBindings()
SfxItemState QueryState(sal_uInt16 nSID, const SfxPoolItem *&rpState)
static const long MINBODY
static SfxViewFrame * Current()
void ExecuteOrientationChange(const bool bLandscape)
SfxDispatcher * GetDispatcher() const
Reference< XModel > xModel
IMPL_LINK(PageColumnControl, ColumnButtonClickHdl_Impl, Button *, pButton, void)
virtual void dispose() override
PageOrientationControl(sal_uInt16 nId, vcl::Window *pParent)