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"
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
32namespace {
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
50namespace 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 tools::Long nPageLeftMargin,
77 const tools::Long nPageRightMargin )
78{
79 mpPageLRMarginItem->SetLeft( nPageLeftMargin );
80 mpPageLRMarginItem->SetRight( nPageRightMargin );
81 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
82 pViewFrm->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_LRSPACE,
83 SfxCallMode::RECORD, { mpPageLRMarginItem.get() });
84}
85
87 const tools::Long nPageTopMargin,
88 const tools::Long nPageBottomMargin )
89{
90 mpPageULMarginItem->SetUpper( nPageTopMargin );
91 mpPageULMarginItem->SetLower( nPageBottomMargin );
92 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
93 pViewFrm->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_ULSPACE,
94 SfxCallMode::RECORD, { mpPageULMarginItem.get() });
95}
96
98{
100 if (!pViewFrm)
101 return;
102
103 css::uno::Reference< css::document::XUndoManager > mxUndoManager(
104 getUndoManager( pViewFrm->GetFrame().GetFrameInterface() ) );
105
106 if ( mxUndoManager.is() )
107 mxUndoManager->enterUndoContext( "" );
108
109 const SvxSizeItem* pSizeItem;
110 pViewFrm->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_SIZE, pSizeItem);
111 mpPageSizeItem.reset( pSizeItem->Clone() );
112
113 // Prevent accidental toggling of page orientation
114 if ((mpPageSizeItem->GetWidth() > mpPageSizeItem->GetHeight()) == bLandscape)
115 {
116 if ( mxUndoManager.is() )
117 mxUndoManager->leaveUndoContext();
118 return;
119 }
120
121 const SvxLongLRSpaceItem* pLRSpaceItem;
122 pViewFrm->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_LRSPACE, pLRSpaceItem);
123 mpPageLRMarginItem.reset( pLRSpaceItem->Clone() );
124
125 const SvxLongULSpaceItem* pULSpaceItem;
126 pViewFrm->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_ULSPACE, pULSpaceItem);
127 mpPageULMarginItem.reset( pULSpaceItem->Clone() );
128
129 {
130 // set new page orientation
131 mpPageItem->SetLandscape( bLandscape );
132
133 // swap the width and height of the page size
134 const tools::Long nRotatedWidth = mpPageSizeItem->GetSize().Height();
135 const tools::Long nRotatedHeight = mpPageSizeItem->GetSize().Width();
136 mpPageSizeItem->SetSize(Size(nRotatedWidth, nRotatedHeight));
137
138 // apply changed attributes
139 pViewFrm->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
140 SfxCallMode::RECORD, { mpPageSizeItem.get(), mpPageItem.get() });
141 }
142
143 // check, if margin values still fit to the changed page size.
144 // if not, adjust margin values
145 {
146 const tools::Long nML = mpPageLRMarginItem->GetLeft();
147 const tools::Long nMR = mpPageLRMarginItem->GetRight();
148 const tools::Long nTmpPW = nML + nMR + MINBODY;
149
150 const tools::Long nPW = mpPageSizeItem->GetSize().Width();
151
152 if ( nTmpPW > nPW )
153 {
154 if ( nML <= nMR )
155 {
156 ExecuteMarginLRChange( mpPageLRMarginItem->GetLeft(), nMR - (nTmpPW - nPW ) );
157 }
158 else
159 {
160 ExecuteMarginLRChange( nML - (nTmpPW - nPW ), mpPageLRMarginItem->GetRight() );
161 }
162 }
163
164 const tools::Long nMT = mpPageULMarginItem->GetUpper();
165 const tools::Long nMB = mpPageULMarginItem->GetLower();
166 const tools::Long nTmpPH = nMT + nMB + MINBODY;
167
168 const tools::Long nPH = mpPageSizeItem->GetSize().Height();
169
170 if ( nTmpPH > nPH )
171 {
172 if ( nMT <= nMB )
173 {
174 ExecuteMarginULChange( mpPageULMarginItem->GetUpper(), nMB - ( nTmpPH - nPH ) );
175 }
176 else
177 {
178 ExecuteMarginULChange( nMT - ( nTmpPH - nPH ), mpPageULMarginItem->GetLower() );
179 }
180 }
181 }
182
183 if ( mxUndoManager.is() )
184 mxUndoManager->leaveUndoContext();
185}
186
187IMPL_LINK(PageOrientationControl, ImplOrientationHdl, weld::Button&, rControl, void)
188{
189 if (&rControl == m_xPortrait.get())
190 ExecuteOrientationChange( false );
191 else
192 ExecuteOrientationChange( true );
193
194 m_xControl->EndPopupMode();
195}
196
197} // end of namespace sw::sidebar
198
199/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr tools::Long MINBODY
SfxDispatcher * GetDispatcher() const
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 * >())
SfxItemState QueryState(sal_uInt16 nSID, const SfxPoolItem *&rpState)
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
SfxBindings & GetBindings()
SfxDispatcher * GetDispatcher()
SfxFrame & GetFrame() const
virtual SvxLongLRSpaceItem * Clone(SfxItemPool *pPool=nullptr) const override
virtual SvxLongULSpaceItem * Clone(SfxItemPool *pPool=nullptr) const override
virtual SvxSizeItem * Clone(SfxItemPool *pPool=nullptr) const override
PageOrientationControl(PageOrientationPopup *pControl, weld::Widget *pParent)
std::unique_ptr< SvxLongULSpaceItem > mpPageULMarginItem
void ExecuteOrientationChange(const bool bLandscape)
void ExecuteMarginLRChange(const tools::Long nPageLeftMargin, const tools::Long nPageRightMargin)
std::unique_ptr< weld::Button > m_xLandscape
std::unique_ptr< SvxPageItem > mpPageItem
std::unique_ptr< weld::Button > m_xPortrait
void ExecuteMarginULChange(const tools::Long nPageTopMargin, const tools::Long nPageBottomMargin)
std::unique_ptr< SvxLongLRSpaceItem > mpPageLRMarginItem
std::unique_ptr< SvxSizeItem > mpPageSizeItem
IMPL_LINK(PageColumnControl, ColumnButtonClickHdl_Impl, weld::Button &, rButton, void)
long Long
Reference< XController > xController
Reference< XModel > xModel
Reference< XControl > m_xControl