LibreOffice Module sw (master)  1
viewlayoutctrl.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 <viewlayoutctrl.hxx>
21 
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <vcl/event.hxx>
24 #include <vcl/status.hxx>
25 #include <vcl/image.hxx>
26 #include <svx/viewlayoutitem.hxx>
27 #include <strings.hrc>
28 #include <bitmaps.hlst>
29 #include <swtypes.hxx>
30 
32 
34 {
35  sal_uInt16 mnState; // 0 = auto, 1= single, 2 = book, 3 = none
42 };
43 
44 SwViewLayoutControl::SwViewLayoutControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStatusBar ) :
45  SfxStatusBarControl( _nSlotId, _nId, rStatusBar ),
46  mpImpl( new SwViewLayoutControl_Impl )
47 {
48  mpImpl->mnState = 1;
49 
50  mpImpl->maImageSingleColumn = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_SINGLECOLUMN);
51  mpImpl->maImageSingleColumn_Active = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_SINGLECOLUMN_ACTIVE);
52  mpImpl->maImageAutomatic = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_AUTOMATIC);
53  mpImpl->maImageAutomatic_Active = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_AUTOMATIC_ACTIVE);
54  mpImpl->maImageBookMode = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_BOOKMODE);
55  mpImpl->maImageBookMode_Active = Image(StockImage::Yes, RID_BMP_VIEWLAYOUT_BOOKMODE_ACTIVE);
56 }
57 
59 {
60 }
61 
62 void SwViewLayoutControl::StateChanged( sal_uInt16 /*nSID*/, SfxItemState eState, const SfxPoolItem* pState )
63 {
64  if ( SfxItemState::DEFAULT != eState || pState->IsVoidItem() )
65  GetStatusBar().SetItemText( GetId(), OUString() );
66  else
67  {
68  OSL_ENSURE( dynamic_cast< const SvxViewLayoutItem *>( pState ) != nullptr, "invalid item type" );
69  const sal_uInt16 nColumns = static_cast<const SvxViewLayoutItem*>( pState )->GetValue();
70  const bool bBookMode = static_cast<const SvxViewLayoutItem*>( pState )->IsBookMode();
71 
72  // SingleColumn Mode
73  if ( 1 == nColumns )
74  mpImpl->mnState = 0;
75  // Automatic Mode
76  else if ( 0 == nColumns )
77  mpImpl->mnState = 1;
78  // Book Mode
79  else if ( bBookMode && 2 == nColumns )
80  mpImpl->mnState = 2;
81  else
82  mpImpl->mnState = 3;
83  }
84 
85  GetStatusBar().SetItemData( GetId(), nullptr ); // force repaint
86 }
87 
89 {
90  vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
91  tools::Rectangle aRect(rUsrEvt.GetRect());
92 
93  const tools::Rectangle aControlRect = getControlRect();
94 
95  const bool bSingleColumn = 0 == mpImpl->mnState;
96  const bool bAutomatic = 1 == mpImpl->mnState;
97  const bool bBookMode = 2 == mpImpl->mnState;
98 
99  const long nImageWidthSum = mpImpl->maImageSingleColumn.GetSizePixel().Width() +
100  mpImpl->maImageAutomatic.GetSizePixel().Width() +
101  mpImpl->maImageBookMode.GetSizePixel().Width();
102 
103  const long nXOffset = (aRect.GetWidth() - nImageWidthSum) / 2;
104  const long nYOffset = (aControlRect.GetHeight() - mpImpl->maImageSingleColumn.GetSizePixel().Height()) / 2;
105 
106  aRect.AdjustLeft( nXOffset );
107  aRect.AdjustTop( nYOffset );
108 
109  // draw single column image:
110  pDev->DrawImage( aRect.TopLeft(), bSingleColumn ? mpImpl->maImageSingleColumn_Active : mpImpl->maImageSingleColumn );
111 
112  // draw automatic image:
113  aRect.AdjustLeft(mpImpl->maImageSingleColumn.GetSizePixel().Width() );
114  pDev->DrawImage( aRect.TopLeft(), bAutomatic ? mpImpl->maImageAutomatic_Active : mpImpl->maImageAutomatic );
115 
116  // draw bookmode image:
117  aRect.AdjustLeft(mpImpl->maImageAutomatic.GetSizePixel().Width() );
118  pDev->DrawImage( aRect.TopLeft(), bBookMode ? mpImpl->maImageBookMode_Active : mpImpl->maImageBookMode );
119 }
120 
122 {
123  const tools::Rectangle aRect = getControlRect();
124  const Point aPoint = rEvt.GetPosPixel();
125  const long nXDiff = aPoint.X() - aRect.Left();
126 
127  sal_uInt16 nColumns = 1;
128  bool bBookMode = false;
129 
130  const long nImageWidthSingle = mpImpl->maImageSingleColumn.GetSizePixel().Width();
131  const long nImageWidthAuto = mpImpl->maImageAutomatic.GetSizePixel().Width();
132  const long nImageWidthBook = mpImpl->maImageBookMode.GetSizePixel().Width();
133  const long nImageWidthSum = nImageWidthSingle + nImageWidthAuto + nImageWidthBook;
134 
135  const long nXOffset = (aRect.GetWidth() - nImageWidthSum)/2;
136 
137  if ( nXDiff < nXOffset + nImageWidthSingle )
138  {
139  mpImpl->mnState = 0; // single
140  nColumns = 1;
141  }
142  else if ( nXDiff < nXOffset + nImageWidthSingle + nImageWidthAuto )
143  {
144  mpImpl->mnState = 1; // auto
145  nColumns = 0;
146  }
147  else
148  {
149  mpImpl->mnState = 2; // book
150  nColumns = 2;
151  bBookMode = true;
152  }
153 
154  // commit state change
155  SvxViewLayoutItem aViewLayout( nColumns, bBookMode );
156 
157  css::uno::Any a;
158  aViewLayout.QueryValue( a );
159 
160  css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
161  aArgs[0].Name = "ViewLayout";
162  aArgs[0].Value = a;
163 
164  execute( aArgs );
165 
166  return true;
167 }
168 
170 {
171  const tools::Rectangle aRect = getControlRect();
172  const Point aPoint = rEvt.GetPosPixel();
173  const long nXDiff = aPoint.X() - aRect.Left();
174 
175  const long nImageWidthSingle = mpImpl->maImageSingleColumn.GetSizePixel().Width();
176  const long nImageWidthAuto = mpImpl->maImageAutomatic.GetSizePixel().Width();
177  const long nImageWidthBook = mpImpl->maImageBookMode.GetSizePixel().Width();
178  const long nImageWidthSum = nImageWidthSingle + nImageWidthAuto + nImageWidthBook;
179 
180  const long nXOffset = (aRect.GetWidth() - nImageWidthSum)/2;
181 
182  if ( nXDiff < nXOffset + nImageWidthSingle )
183  {
184  GetStatusBar().SetQuickHelpText(GetId(), SwResId(STR_VIEWLAYOUT_ONE));
185  }
186  else if ( nXDiff < nXOffset + nImageWidthSingle + nImageWidthAuto )
187  {
188  GetStatusBar().SetQuickHelpText(GetId(), SwResId(STR_VIEWLAYOUT_MULTI));
189  }
190  else
191  {
192  GetStatusBar().SetQuickHelpText(GetId(), SwResId(STR_VIEWLAYOUT_BOOK));
193  }
194  return true;
195 }
196 
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long GetWidth() const
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
std::string GetValue
const tools::Rectangle & GetRect() const
void SetItemText(sal_uInt16 nItemId, const OUString &rText, int nCharsWidth=-1)
virtual bool MouseButtonDown(const MouseEvent &) override
virtual ~SwViewLayoutControl() override
SFX_IMPL_STATUSBAR_CONTROL(SwViewLayoutControl, SvxViewLayoutItem)
void SetItemData(sal_uInt16 nItemId, void *pNewData)
virtual bool IsVoidItem() const
SwViewLayoutControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
virtual void StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
sal_uInt16 GetId() const
std::unique_ptr< SwViewLayoutControl_Impl > mpImpl
vcl::RenderContext * GetRenderContext() const
uno_Any a
OUString SwResId(const char *pId)
Definition: swmodule.cxx:178
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
SfxItemState
StatusBar & GetStatusBar() const
virtual void Paint(const UserDrawEvent &rEvt) override
const Point & GetPosPixel() const
long Left() const
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
virtual bool MouseMove(const MouseEvent &) override