LibreOffice Module svx (master)  1
modctrl.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 <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>
27 
28 #include <svx/strings.hrc>
29 #include <svx/modctrl.hxx>
30 #include <svx/dialmgr.hxx>
31 
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include "modctrl_internal.hxx"
34 #include <bitmaps.hlst>
35 
36 using ::com::sun::star::uno::Sequence;
37 using ::com::sun::star::beans::PropertyValue;
38 
40 
42 {
44  {
49  };
50 
51  Idle maIdle { "svx::SvxModifyControl maIdle" };
53 
55 
57  mnModState(MODIFICATION_STATE_NO)
58  {
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);
62 
63  maIdle.SetPriority(TaskPriority::LOWEST);
64  }
65 };
66 
67 SvxModifyControl::SvxModifyControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStb ) :
68  SfxStatusBarControl( _nSlotId, _nId, rStb ),
69  mxImpl(std::make_shared<ImplData>())
70 {
71  mxImpl->maIdle.SetInvokeHandler( LINK(this, SvxModifyControl, OnTimer) );
72 }
73 
74 
76  const SfxPoolItem* pState )
77 {
78  if ( SfxItemState::DEFAULT != eState )
79  return;
80 
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();
84 
85  bool modified = pItem->GetValue();
86  bool start = ( !modified && mxImpl->mnModState == ImplData::MODIFICATION_STATE_YES); // should timer be started and feedback image displayed ?
87 
89 
90  _repaint();
91 
92  TranslateId pResId = modified ? RID_SVXSTR_DOC_MODIFIED_YES : RID_SVXSTR_DOC_MODIFIED_NO;
94 
95  if ( start )
96  mxImpl->maIdle.Start();
97 }
98 
99 
100 IMPL_LINK( SvxModifyControl, OnTimer, Timer *, pTimer, void )
101 {
102  if (pTimer == nullptr)
103  return;
104 
105  pTimer->Stop();
106  mxImpl->mnModState = ImplData::MODIFICATION_STATE_NO;
107 
108  _repaint();
109 }
110 
111 
113 {
114  GetStatusBar().SetItemData( GetId(), nullptr ); // force repaint
115 }
116 
127 Point centerImage(const tools::Rectangle& rBoundingRect, const Image& rImg)
128 {
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;
136 }
137 
139 {
140  vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
141  tools::Rectangle aRect(rUsrEvt.GetRect());
142 
143  ImplData::ModificationState state = mxImpl->mnModState;
144  Point aPt = centerImage(aRect, mxImpl->maImages[state]);
145  pDev->DrawImage(aPt, mxImpl->maImages[state]);
146 }
147 
149 {
150  if (mxImpl->mnModState != ImplData::MODIFICATION_STATE_YES)
151  // document not modified. nothing to do here.
152  return;
153 
154  Sequence<PropertyValue> aArgs;
155  execute(".uno:Save", aArgs);
156 }
157 
158 
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool GetValue() const
Size GetSizePixel() const
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
const tools::Rectangle & GetRect() const
long Long
std::shared_ptr< T > make_shared(Args &&...args)
void SetItemData(sal_uInt16 nItemId, void *pNewData)
IMPL_LINK(SvxModifyControl, OnTimer, Timer *, pTimer, void)
Definition: modctrl.cxx:100
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
constexpr tools::Long getHeight() const
std::shared_ptr< ImplData > mxImpl
Definition: modctrl.hxx:49
sal_uInt16 GetId() const
virtual void StateChangedAtStatusBarControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: modctrl.cxx:75
vcl::RenderContext * GetRenderContext() const
#define DBG_ASSERT(sCon, aError)
Image maImages[MODIFICATION_STATE_SIZE]
Definition: modctrl.cxx:52
Idle maIdle svx::SvxModifyControl maIdle
Definition: modctrl.cxx:51
SFX_IMPL_STATUSBAR_CONTROL(SvxModifyControl, SfxBoolItem)
ModificationState mnModState
Definition: modctrl.cxx:54
constexpr Point TopLeft() const
constexpr Size GetSize() const
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
SfxItemState
StatusBar & GetStatusBar() const
void SetPriority(TaskPriority ePriority)
constexpr tools::Long getWidth() const
Point centerImage(const tools::Rectangle &rBoundingRect, const Image &rImg)
Given a bounding rectangle and an image, determine the top-left position of the image so that the ima...
Definition: modctrl.cxx:127
SvxModifyControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: modctrl.cxx:67
virtual void Paint(const UserDrawEvent &rUsrEvt) override
Definition: modctrl.cxx:138
virtual void Click() override
Definition: modctrl.cxx:148