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 <sfx2/app.hxx>
27 #include <tools/debug.hxx>
28 
29 #include <svx/strings.hrc>
30 #include <svx/modctrl.hxx>
31 #include <svx/dialmgr.hxx>
32 
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include "modctrl_internal.hxx"
35 #include <bitmaps.hlst>
36 
37 using ::com::sun::star::uno::Sequence;
38 using ::com::sun::star::beans::PropertyValue;
39 
41 
43 {
45  {
50  };
51 
54 
56 
58  mnModState(MODIFICATION_STATE_NO)
59  {
60  maImages[MODIFICATION_STATE_NO] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_NO);
61  maImages[MODIFICATION_STATE_YES] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_YES);
62  maImages[MODIFICATION_STATE_FEEDBACK] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_FEEDBACK);
63 
64  maIdle.SetPriority(TaskPriority::LOWEST);
65  maIdle.SetDebugName("svx::SvxModifyControl maIdle");
66  }
67 };
68 
69 SvxModifyControl::SvxModifyControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStb ) :
70  SfxStatusBarControl( _nSlotId, _nId, rStb ),
71  mxImpl(std::make_shared<ImplData>())
72 {
73  mxImpl->maIdle.SetInvokeHandler( LINK(this, SvxModifyControl, OnTimer) );
74 }
75 
76 
78  const SfxPoolItem* pState )
79 {
80  if ( SfxItemState::DEFAULT != eState )
81  return;
82 
83  DBG_ASSERT( dynamic_cast<const SfxBoolItem*>( pState) != nullptr, "invalid item type" );
84  const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pState);
85  mxImpl->maIdle.Stop();
86 
87  bool modified = pItem->GetValue();
88  bool start = ( !modified && mxImpl->mnModState == ImplData::MODIFICATION_STATE_YES); // should timer be started and feedback image displayed ?
89 
91 
92  _repaint();
93 
94  const char* pResId = modified ? RID_SVXSTR_DOC_MODIFIED_YES : RID_SVXSTR_DOC_MODIFIED_NO;
96 
97  if ( start )
98  mxImpl->maIdle.Start();
99 }
100 
101 
102 IMPL_LINK( SvxModifyControl, OnTimer, Timer *, pTimer, void )
103 {
104  if (pTimer == nullptr)
105  return;
106 
107  pTimer->Stop();
108  mxImpl->mnModState = ImplData::MODIFICATION_STATE_NO;
109 
110  _repaint();
111 }
112 
113 
115 {
116  GetStatusBar().SetItemData( GetId(), nullptr ); // force repaint
117 }
118 
129 Point centerImage(const tools::Rectangle& rBoundingRect, const Image& rImg)
130 {
131  Size aImgSize = rImg.GetSizePixel();
132  Size aRectSize = rBoundingRect.GetSize();
133  long nXOffset = (aRectSize.getWidth() - aImgSize.getWidth())/2;
134  long nYOffset = (aRectSize.getHeight() - aImgSize.getHeight())/2;
135  Point aPt = rBoundingRect.TopLeft();
136  aPt += Point(nXOffset, nYOffset);
137  return aPt;
138 }
139 
141 {
142  vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
143  tools::Rectangle aRect(rUsrEvt.GetRect());
144 
145  ImplData::ModificationState state = mxImpl->mnModState;
146  Point aPt = centerImage(aRect, mxImpl->maImages[state]);
147  pDev->DrawImage(aPt, mxImpl->maImages[state]);
148 }
149 
151 {
152  if (mxImpl->mnModState != ImplData::MODIFICATION_STATE_YES)
153  // document not modified. nothing to do here.
154  return;
155 
156  Sequence<PropertyValue> aArgs;
157  execute(".uno:Save", aArgs);
158 }
159 
160 
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Point TopLeft() const
bool GetValue() const
Size GetSizePixel() const
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
const tools::Rectangle & GetRect() const
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
void SetItemData(sal_uInt16 nItemId, void *pNewData)
IMPL_LINK(SvxModifyControl, OnTimer, Timer *, pTimer, void)
Definition: modctrl.cxx:102
std::shared_ptr< ImplData > mxImpl
Definition: modctrl.hxx:49
void SetDebugName(const char *pDebugName)
sal_uInt16 GetId() const
vcl::RenderContext * GetRenderContext() const
#define DBG_ASSERT(sCon, aError)
Image maImages[MODIFICATION_STATE_SIZE]
Definition: modctrl.cxx:53
SFX_IMPL_STATUSBAR_CONTROL(SvxModifyControl, SfxBoolItem)
ModificationState mnModState
Definition: modctrl.cxx:55
virtual void StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: modctrl.cxx:77
Size GetSize() const
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
SfxItemState
StatusBar & GetStatusBar() const
void SetPriority(TaskPriority ePriority)
long getHeight() 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:129
SvxModifyControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: modctrl.cxx:69
long getWidth() const
virtual void Paint(const UserDrawEvent &rUsrEvt) override
Definition: modctrl.cxx:140
virtual void Click() override
Definition: modctrl.cxx:150