bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / stbctrls / modctrl.cxx
blobd22b3a550e1a3cf5972c78fee5b2c2dfc0280715
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 <vcl/status.hxx>
21 #include <vcl/image.hxx>
22 #include <vcl/timer.hxx>
23 #include <vcl/idle.hxx>
24 #include <svl/eitem.hxx>
25 #include <sfx2/app.hxx>
27 #include <svx/dialogs.hrc>
28 #include <svx/modctrl.hxx>
29 #include <svx/dialmgr.hxx>
31 #include <com/sun/star/beans/PropertyValue.hpp>
32 #include "modctrl_internal.hxx"
34 using ::com::sun::star::uno::Sequence;
35 using ::com::sun::star::beans::PropertyValue;
37 SFX_IMPL_STATUSBAR_CONTROL(SvxModifyControl, SfxBoolItem);
39 struct SvxModifyControl::ImplData
41 enum ModificationState
43 MODIFICATION_STATE_NO = 0,
44 MODIFICATION_STATE_YES,
45 MODIFICATION_STATE_FEEDBACK,
46 MODIFICATION_STATE_SIZE
49 Idle maIdle;
50 Image maImages[MODIFICATION_STATE_SIZE];
52 ModificationState mnModState;
54 ImplData():
55 mnModState(MODIFICATION_STATE_NO)
57 maImages[MODIFICATION_STATE_NO] = Image(SVX_RES(RID_SVXBMP_DOC_MODIFIED_NO));
58 maImages[MODIFICATION_STATE_YES] = Image(SVX_RES(RID_SVXBMP_DOC_MODIFIED_YES));
59 maImages[MODIFICATION_STATE_FEEDBACK] = Image(SVX_RES(RID_SVXBMP_DOC_MODIFIED_FEEDBACK));
61 maIdle.SetPriority(SchedulerPriority::LOWEST);
65 SvxModifyControl::SvxModifyControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStb ) :
66 SfxStatusBarControl( _nSlotId, _nId, rStb ),
67 mxImpl(new ImplData)
69 //#ifndef MACOSX
70 if ( rStb.GetDPIScaleFactor() > 1 )
72 for (int i = 0; i < mxImpl->MODIFICATION_STATE_SIZE; i++)
74 BitmapEx b = mxImpl->maImages[i].GetBitmapEx();
75 b.Scale(rStb.GetDPIScaleFactor(), rStb.GetDPIScaleFactor(), BmpScaleFlag::Fast);
76 mxImpl->maImages[i] = Image(b);
79 //#endif
80 mxImpl->maIdle.SetIdleHdl( LINK(this, SvxModifyControl, OnTimer) );
85 void SvxModifyControl::StateChanged( sal_uInt16, SfxItemState eState,
86 const SfxPoolItem* pState )
88 if ( SfxItemState::DEFAULT != eState )
89 return;
91 DBG_ASSERT( pState->ISA( SfxBoolItem ), "invalid item type" );
92 const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pState);
93 mxImpl->maIdle.Stop();
95 bool modified = pItem->GetValue();
96 bool start = ( !modified && mxImpl->mnModState == ImplData::MODIFICATION_STATE_YES); // should timer be started and feedback image displayed ?
98 mxImpl->mnModState = (start ? ImplData::MODIFICATION_STATE_FEEDBACK : (modified ? ImplData::MODIFICATION_STATE_YES : ImplData::MODIFICATION_STATE_NO));
100 _repaint();
102 int nResId = modified ? RID_SVXSTR_DOC_MODIFIED_YES : RID_SVXSTR_DOC_MODIFIED_NO;
103 GetStatusBar().SetQuickHelpText(GetId(), SVX_RESSTR(nResId));
105 if ( start )
106 mxImpl->maIdle.Start();
111 IMPL_LINK_TYPED( SvxModifyControl, OnTimer, Idle *, pTimer, void )
113 if (pTimer == 0)
114 return;
116 pTimer->Stop();
117 mxImpl->mnModState = ImplData::MODIFICATION_STATE_NO;
119 _repaint();
124 void SvxModifyControl::_repaint()
126 if ( GetStatusBar().AreItemsVisible() )
127 GetStatusBar().SetItemData( GetId(), 0 ); // force repaint
131 * Given a bounding rectangle and an image, determine the top-left position
132 * of the image so that the image would look centered both horizontally and
133 * vertically.
135 * @param rBoundingRect bounding rectangle
136 * @param rImg image
138 * @return Point top-left corner of the centered image position
140 Point centerImage(const Rectangle& rBoundingRect, const Image& rImg)
142 Size aImgSize = rImg.GetSizePixel();
143 Size aRectSize = rBoundingRect.GetSize();
144 long nXOffset = (aRectSize.getWidth() - aImgSize.getWidth())/2;
145 long nYOffset = (aRectSize.getHeight() - aImgSize.getHeight())/2;
146 Point aPt = rBoundingRect.TopLeft();
147 aPt += Point(nXOffset, nYOffset);
148 return aPt;
151 void SvxModifyControl::Paint( const UserDrawEvent& rUsrEvt )
153 vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
154 Rectangle aRect(rUsrEvt.GetRect());
156 ImplData::ModificationState state = mxImpl->mnModState;
157 Point aPt = centerImage(aRect, mxImpl->maImages[state]);
158 pDev->DrawImage(aPt, mxImpl->maImages[state]);
161 void SvxModifyControl::Click()
163 if (mxImpl->mnModState != ImplData::MODIFICATION_STATE_YES)
164 // document not modified. nothing to do here.
165 return;
167 Sequence<PropertyValue> aArgs;
168 execute(OUString(".uno:Save"), aArgs);
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */