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