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 
31 SFX_IMPL_STATUSBAR_CONTROL( SwViewLayoutControl, SvxViewLayoutItem );
32 
33 struct SwViewLayoutControl::SwViewLayoutControl_Impl
34 {
35     sal_uInt16      mnState; // 0 = auto, 1= single, 2 = book, 3 = none
36     Image       maImageSingleColumn;
37     Image       maImageSingleColumn_Active;
38     Image       maImageAutomatic;
39     Image       maImageAutomatic_Active;
40     Image       maImageBookMode;
41     Image       maImageBookMode_Active;
42 };
43 
SwViewLayoutControl(sal_uInt16 _nSlotId,sal_uInt16 _nId,StatusBar & rStatusBar)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 
~SwViewLayoutControl()58 SwViewLayoutControl::~SwViewLayoutControl()
59 {
60 }
61 
StateChanged(sal_uInt16,SfxItemState eState,const SfxPoolItem * pState)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 
Paint(const UserDrawEvent & rUsrEvt)88 void SwViewLayoutControl::Paint( const UserDrawEvent& rUsrEvt )
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 tools::Long nImageWidthSum = mpImpl->maImageSingleColumn.GetSizePixel().Width() +
100                                 mpImpl->maImageAutomatic.GetSizePixel().Width() +
101                                 mpImpl->maImageBookMode.GetSizePixel().Width();
102 
103     const tools::Long nXOffset = (aRect.GetWidth() - nImageWidthSum) / 2;
104     const tools::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 
MouseButtonDown(const MouseEvent & rEvt)121 bool SwViewLayoutControl::MouseButtonDown( const MouseEvent & rEvt )
122 {
123     const tools::Rectangle aRect = getControlRect();
124     const Point aPoint = rEvt.GetPosPixel();
125     const tools::Long nXDiff = aPoint.X() - aRect.Left();
126 
127     sal_uInt16 nColumns = 1;
128     bool bBookMode = false;
129 
130     const tools::Long nImageWidthSingle = mpImpl->maImageSingleColumn.GetSizePixel().Width();
131     const tools::Long nImageWidthAuto = mpImpl->maImageAutomatic.GetSizePixel().Width();
132     const tools::Long nImageWidthBook = mpImpl->maImageBookMode.GetSizePixel().Width();
133     const tools::Long nImageWidthSum = nImageWidthSingle + nImageWidthAuto + nImageWidthBook;
134 
135     const tools::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 
MouseMove(const MouseEvent & rEvt)169 bool SwViewLayoutControl::MouseMove( const MouseEvent & rEvt )
170 {
171     const tools::Rectangle aRect = getControlRect();
172     const Point aPoint = rEvt.GetPosPixel();
173     const tools::Long nXDiff = aPoint.X() - aRect.Left();
174 
175     const tools::Long nImageWidthSingle = mpImpl->maImageSingleColumn.GetSizePixel().Width();
176     const tools::Long nImageWidthAuto = mpImpl->maImageAutomatic.GetSizePixel().Width();
177     const tools::Long nImageWidthBook = mpImpl->maImageBookMode.GetSizePixel().Width();
178     const tools::Long nImageWidthSum = nImageWidthSingle + nImageWidthAuto + nImageWidthBook;
179 
180     const tools::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: */
198