Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / utlui / viewlayoutctrl.cxx
blob453dcd91a2ebb6bce4c7f255eee220c1b6579a44
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <viewlayoutctrl.hxx>
22 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <comphelper/propertyvalue.hxx>
25 #include <vcl/event.hxx>
26 #include <vcl/status.hxx>
27 #include <vcl/image.hxx>
28 #include <svx/viewlayoutitem.hxx>
29 #include <strings.hrc>
30 #include <bitmaps.hlst>
31 #include <swtypes.hxx>
33 SFX_IMPL_STATUSBAR_CONTROL( SwViewLayoutControl, SvxViewLayoutItem );
35 struct SwViewLayoutControl::SwViewLayoutControl_Impl
37 sal_uInt16 mnState; // 0 = auto, 1= single, 2 = book, 3 = none, 4 = off
38 Image maImageSingleColumn;
39 Image maImageSingleColumn_Active;
40 Image maImageAutomatic;
41 Image maImageAutomatic_Active;
42 Image maImageBookMode;
43 Image maImageBookMode_Active;
46 SwViewLayoutControl::SwViewLayoutControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStatusBar ) :
47 SfxStatusBarControl( _nSlotId, _nId, rStatusBar ),
48 mpImpl( new SwViewLayoutControl_Impl )
50 mpImpl->mnState = 1;
52 mpImpl->maImageSingleColumn = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_SINGLECOLUMN);
53 mpImpl->maImageSingleColumn_Active = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_SINGLECOLUMN_ACTIVE);
54 mpImpl->maImageAutomatic = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_AUTOMATIC);
55 mpImpl->maImageAutomatic_Active = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_AUTOMATIC_ACTIVE);
56 mpImpl->maImageBookMode = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_BOOKMODE);
57 mpImpl->maImageBookMode_Active = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_BOOKMODE_ACTIVE);
60 SwViewLayoutControl::~SwViewLayoutControl()
64 void SwViewLayoutControl::StateChangedAtStatusBarControl( sal_uInt16 /*nSID*/, SfxItemState eState, const SfxPoolItem* pState )
66 if ( SfxItemState::DEFAULT != eState || pState->IsVoidItem() )
68 GetStatusBar().SetItemText( GetId(), OUString() );
69 mpImpl->mnState = 4; //tdf#148441 switch off, if disabled
71 else
73 assert( dynamic_cast< const SvxViewLayoutItem *>( pState ) && "invalid item type" );
74 const sal_uInt16 nColumns = static_cast<const SvxViewLayoutItem*>( pState )->GetValue();
75 const bool bBookMode = static_cast<const SvxViewLayoutItem*>( pState )->IsBookMode();
77 // SingleColumn Mode
78 if ( 1 == nColumns )
79 mpImpl->mnState = 0;
80 // Automatic Mode
81 else if ( 0 == nColumns )
82 mpImpl->mnState = 1;
83 // Book Mode
84 else if ( bBookMode && 2 == nColumns )
85 mpImpl->mnState = 2;
86 else
87 mpImpl->mnState = 3;
90 GetStatusBar().SetItemData( GetId(), nullptr ); // force repaint
93 void SwViewLayoutControl::Paint( const UserDrawEvent& rUsrEvt )
95 vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
96 tools::Rectangle aRect(rUsrEvt.GetRect());
98 const tools::Rectangle aControlRect = getControlRect();
100 if (mpImpl->mnState < 4)
102 const bool bSingleColumn = 0 == mpImpl->mnState;
103 const bool bAutomatic = 1 == mpImpl->mnState;
104 const bool bBookMode = 2 == mpImpl->mnState;
105 const int nDistance = 6;
107 const tools::Long nImageWidthSum = mpImpl->maImageSingleColumn.GetSizePixel().Width() +
108 mpImpl->maImageAutomatic.GetSizePixel().Width() +
109 mpImpl->maImageBookMode.GetSizePixel().Width();
111 const tools::Long nXOffset = (aRect.GetWidth() - nImageWidthSum - (2* nDistance)) / 2;
112 const tools::Long nYOffset = (aControlRect.GetHeight() - mpImpl->maImageSingleColumn.GetSizePixel().Height()) / 2;
114 aRect.AdjustLeft( nXOffset );
115 aRect.AdjustTop( nYOffset );
117 // draw single column image:
118 pDev->DrawImage( aRect.TopLeft(), bSingleColumn ? mpImpl->maImageSingleColumn_Active : mpImpl->maImageSingleColumn );
120 // draw automatic image:
121 aRect.AdjustLeft(mpImpl->maImageSingleColumn.GetSizePixel().Width() + nDistance);
122 pDev->DrawImage( aRect.TopLeft(), bAutomatic ? mpImpl->maImageAutomatic_Active : mpImpl->maImageAutomatic );
124 // draw bookmode image:
125 aRect.AdjustLeft(mpImpl->maImageAutomatic.GetSizePixel().Width() + nDistance);
126 pDev->DrawImage( aRect.TopLeft(), bBookMode ? mpImpl->maImageBookMode_Active : mpImpl->maImageBookMode );
128 else
130 pDev->DrawRect( aControlRect );
134 bool SwViewLayoutControl::MouseButtonDown( const MouseEvent & rEvt )
136 if (mpImpl->mnState < 4)
138 const tools::Rectangle aRect = getControlRect();
139 const Point aPoint = rEvt.GetPosPixel();
140 const tools::Long nXDiff = aPoint.X() - aRect.Left();
142 sal_uInt16 nColumns = 1;
143 bool bBookMode = false;
145 const tools::Long nImageWidthSingle = mpImpl->maImageSingleColumn.GetSizePixel().Width();
146 const tools::Long nImageWidthAuto = mpImpl->maImageAutomatic.GetSizePixel().Width();
147 const tools::Long nImageWidthBook = mpImpl->maImageBookMode.GetSizePixel().Width();
148 const tools::Long nImageWidthSum = nImageWidthSingle + nImageWidthAuto + nImageWidthBook;
150 const tools::Long nXOffset = (aRect.GetWidth() - nImageWidthSum)/2;
152 if ( nXDiff < nXOffset + nImageWidthSingle )
154 mpImpl->mnState = 0; // single
155 nColumns = 1;
157 else if ( nXDiff < nXOffset + nImageWidthSingle + nImageWidthAuto )
159 mpImpl->mnState = 1; // auto
160 nColumns = 0;
162 else
164 mpImpl->mnState = 2; // book
165 nColumns = 2;
166 bBookMode = true;
169 // commit state change
170 SvxViewLayoutItem aViewLayout( nColumns, bBookMode );
172 css::uno::Any a;
173 aViewLayout.QueryValue( a );
175 css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue("ViewLayout",
176 a) };
177 execute( aArgs );
179 return true;
182 bool SwViewLayoutControl::MouseMove( const MouseEvent & rEvt )
184 if (mpImpl->mnState < 4)
186 const tools::Rectangle aRect = getControlRect();
187 const Point aPoint = rEvt.GetPosPixel();
188 const tools::Long nXDiff = aPoint.X() - aRect.Left();
190 const tools::Long nImageWidthSingle = mpImpl->maImageSingleColumn.GetSizePixel().Width();
191 const tools::Long nImageWidthAuto = mpImpl->maImageAutomatic.GetSizePixel().Width();
192 const tools::Long nImageWidthBook = mpImpl->maImageBookMode.GetSizePixel().Width();
193 const tools::Long nImageWidthSum = nImageWidthSingle + nImageWidthAuto + nImageWidthBook;
195 const tools::Long nXOffset = (aRect.GetWidth() - nImageWidthSum)/2;
197 if ( nXDiff < nXOffset + nImageWidthSingle )
199 GetStatusBar().SetQuickHelpText(GetId(), SwResId(STR_VIEWLAYOUT_ONE));
201 else if ( nXDiff < nXOffset + nImageWidthSingle + nImageWidthAuto )
203 GetStatusBar().SetQuickHelpText(GetId(), SwResId(STR_VIEWLAYOUT_MULTI));
205 else
207 GetStatusBar().SetQuickHelpText(GetId(), SwResId(STR_VIEWLAYOUT_BOOK));
210 return true;
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */