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