Revert "Revert "Revert "stronger typing for SwClient::GetRegisteredIn"" and fix SwIte...
[LibreOffice.git] / svx / source / stbctrls / modctrl.cxx
blob98f18bcc747f6d0a9ad63a25a01334dec15b39ca
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 <vcl/event.hxx>
25 #include <svl/eitem.hxx>
26 #include <tools/debug.hxx>
28 #include <svx/strings.hrc>
29 #include <svx/modctrl.hxx>
30 #include <svx/dialmgr.hxx>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include "modctrl_internal.hxx"
34 #include <bitmaps.hlst>
36 using ::com::sun::star::uno::Sequence;
37 using ::com::sun::star::beans::PropertyValue;
39 SFX_IMPL_STATUSBAR_CONTROL(SvxModifyControl, SfxBoolItem);
41 struct SvxModifyControl::ImplData
43 enum ModificationState
45 MODIFICATION_STATE_NO = 0,
46 MODIFICATION_STATE_YES,
47 MODIFICATION_STATE_FEEDBACK,
48 MODIFICATION_STATE_SIZE
51 Idle maIdle { "svx::SvxModifyControl maIdle" };
52 Image maImages[MODIFICATION_STATE_SIZE];
54 ModificationState mnModState;
56 ImplData():
57 mnModState(MODIFICATION_STATE_NO)
59 maImages[MODIFICATION_STATE_NO] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_NO);
60 maImages[MODIFICATION_STATE_YES] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_YES);
61 maImages[MODIFICATION_STATE_FEEDBACK] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_FEEDBACK);
63 maIdle.SetPriority(TaskPriority::LOWEST);
67 SvxModifyControl::SvxModifyControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStb ) :
68 SfxStatusBarControl( _nSlotId, _nId, rStb ),
69 mxImpl(std::make_shared<ImplData>())
71 mxImpl->maIdle.SetInvokeHandler( LINK(this, SvxModifyControl, OnTimer) );
75 void SvxModifyControl::StateChangedAtStatusBarControl( sal_uInt16, SfxItemState eState,
76 const SfxPoolItem* pState )
78 if ( SfxItemState::DEFAULT != eState )
79 return;
81 DBG_ASSERT( dynamic_cast<const SfxBoolItem*>( pState) != nullptr, "invalid item type" );
82 const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pState);
83 mxImpl->maIdle.Stop();
85 bool modified = pItem->GetValue();
86 bool start = ( !modified && mxImpl->mnModState == ImplData::MODIFICATION_STATE_YES); // should timer be started and feedback image displayed ?
88 mxImpl->mnModState = (start ? ImplData::MODIFICATION_STATE_FEEDBACK : (modified ? ImplData::MODIFICATION_STATE_YES : ImplData::MODIFICATION_STATE_NO));
90 _repaint();
92 TranslateId pResId = modified ? RID_SVXSTR_DOC_MODIFIED_YES : RID_SVXSTR_DOC_MODIFIED_NO;
93 GetStatusBar().SetQuickHelpText(GetId(), SvxResId(pResId));
95 if ( start )
96 mxImpl->maIdle.Start();
100 IMPL_LINK( SvxModifyControl, OnTimer, Timer *, pTimer, void )
102 if (pTimer == nullptr)
103 return;
105 pTimer->Stop();
106 mxImpl->mnModState = ImplData::MODIFICATION_STATE_NO;
108 _repaint();
112 void SvxModifyControl::_repaint()
114 GetStatusBar().SetItemData( GetId(), nullptr ); // force repaint
118 * Given a bounding rectangle and an image, determine the top-left position
119 * of the image so that the image would look centered both horizontally and
120 * vertically.
122 * @param rBoundingRect bounding rectangle
123 * @param rImg image
125 * @return Point top-left corner of the centered image position
127 Point centerImage(const tools::Rectangle& rBoundingRect, const Image& rImg)
129 Size aImgSize = rImg.GetSizePixel();
130 Size aRectSize = rBoundingRect.GetSize();
131 tools::Long nXOffset = (aRectSize.getWidth() - aImgSize.getWidth())/2;
132 tools::Long nYOffset = (aRectSize.getHeight() - aImgSize.getHeight())/2;
133 Point aPt = rBoundingRect.TopLeft();
134 aPt += Point(nXOffset, nYOffset);
135 return aPt;
138 void SvxModifyControl::Paint( const UserDrawEvent& rUsrEvt )
140 vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
141 tools::Rectangle aRect(rUsrEvt.GetRect());
143 ImplData::ModificationState state = mxImpl->mnModState;
144 Point aPt = centerImage(aRect, mxImpl->maImages[state]);
145 pDev->DrawImage(aPt, mxImpl->maImages[state]);
148 void SvxModifyControl::Click()
150 if (mxImpl->mnModState != ImplData::MODIFICATION_STATE_YES)
151 // document not modified. nothing to do here.
152 return;
154 Sequence<PropertyValue> aArgs;
155 execute(u".uno:Save"_ustr, aArgs);
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */