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 
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  maIdle.SetDebugName("svx::SvxModifyControl maIdle");
65  }
66 };
67 
68 SvxModifyControl::SvxModifyControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStb ) :
69  SfxStatusBarControl( _nSlotId, _nId, rStb ),
70  mxImpl(std::make_shared<ImplData>())
71 {
72  mxImpl->maIdle.SetInvokeHandler( LINK(this, SvxModifyControl, OnTimer) );
73 }
74 
75 
77  const SfxPoolItem* pState )
78 {
79  if ( SfxItemState::DEFAULT != eState )
80  return;
81 
82  DBG_ASSERT( dynamic_cast<const SfxBoolItem*>( pState) != nullptr, "invalid item type" );
83  const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pState);
84  mxImpl->maIdle.Stop();
85 
86  bool modified = pItem->GetValue();
87  bool start = ( !modified && mxImpl->mnModState == ImplData::MODIFICATION_STATE_YES); // should timer be started and feedback image displayed ?
88 
90 
91  _repaint();
92 
93  const char* pResId = modified ? RID_SVXSTR_DOC_MODIFIED_YES : RID_SVXSTR_DOC_MODIFIED_NO;
95 
96  if ( start )
97  mxImpl->maIdle.Start();
98 }
99 
100 
101 IMPL_LINK( SvxModifyControl, OnTimer, Timer *, pTimer, void )
102 {
103  if (pTimer == nullptr)
104  return;
105 
106  pTimer->Stop();
107  mxImpl->mnModState = ImplData::MODIFICATION_STATE_NO;
108 
109  _repaint();
110 }
111 
112 
114 {
115  GetStatusBar().SetItemData( GetId(), nullptr ); // force repaint
116 }
117 
128 Point centerImage(const tools::Rectangle& rBoundingRect, const Image& rImg)
129 {
130  Size aImgSize = rImg.GetSizePixel();
131  Size aRectSize = rBoundingRect.GetSize();
132  long nXOffset = (aRectSize.getWidth() - aImgSize.getWidth())/2;
133  long nYOffset = (aRectSize.getHeight() - aImgSize.getHeight())/2;
134  Point aPt = rBoundingRect.TopLeft();
135  aPt += Point(nXOffset, nYOffset);
136  return aPt;
137 }
138 
140 {
141  vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
142  tools::Rectangle aRect(rUsrEvt.GetRect());
143 
144  ImplData::ModificationState state = mxImpl->mnModState;
145  Point aPt = centerImage(aRect, mxImpl->maImages[state]);
146  pDev->DrawImage(aPt, mxImpl->maImages[state]);
147 }
148 
150 {
151  if (mxImpl->mnModState != ImplData::MODIFICATION_STATE_YES)
152  // document not modified. nothing to do here.
153  return;
154 
155  Sequence<PropertyValue> aArgs;
156  execute(".uno:Save", aArgs);
157 }
158 
159 
160 /* 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
std::shared_ptr< T > make_shared(Args &&...args)
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:101
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:52
SFX_IMPL_STATUSBAR_CONTROL(SvxModifyControl, SfxBoolItem)
ModificationState mnModState
Definition: modctrl.cxx:54
virtual void StateChanged(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: modctrl.cxx:76
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:128
SvxModifyControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: modctrl.cxx:68
long getWidth() const
virtual void Paint(const UserDrawEvent &rUsrEvt) override
Definition: modctrl.cxx:139
virtual void Click() override
Definition: modctrl.cxx:149