Line data Source code
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 <svl/eitem.hxx>
24 : #include <sfx2/app.hxx>
25 :
26 : #include <svx/dialogs.hrc>
27 : #include <svx/modctrl.hxx>
28 : #include <svx/dialmgr.hxx>
29 :
30 : #include <com/sun/star/beans/PropertyValue.hpp>
31 : #include "modctrl_internal.hxx"
32 :
33 : using ::com::sun::star::uno::Sequence;
34 : using ::com::sun::star::beans::PropertyValue;
35 :
36 1712 : SFX_IMPL_STATUSBAR_CONTROL(SvxModifyControl, SfxBoolItem);
37 :
38 :
39 : namespace
40 : {
41 : const unsigned _FEEDBACK_TIMEOUT = 3000;
42 : }
43 :
44 1464 : struct SvxModifyControl::ImplData
45 : {
46 : enum ModificationState
47 : {
48 : MODIFICATION_STATE_NO = 0,
49 : MODIFICATION_STATE_YES,
50 : MODIFICATION_STATE_FEEDBACK,
51 : MODIFICATION_STATE_SIZE
52 : };
53 :
54 : Timer maTimer;
55 : Image maImages[MODIFICATION_STATE_SIZE];
56 :
57 : ModificationState mnModState;
58 :
59 1464 : ImplData():
60 1464 : mnModState(MODIFICATION_STATE_NO)
61 : {
62 1464 : maImages[MODIFICATION_STATE_NO] = Image(SVX_RES(RID_SVXBMP_DOC_MODIFIED_NO));
63 1464 : maImages[MODIFICATION_STATE_YES] = Image(SVX_RES(RID_SVXBMP_DOC_MODIFIED_YES));
64 1464 : maImages[MODIFICATION_STATE_FEEDBACK] = Image(SVX_RES(RID_SVXBMP_DOC_MODIFIED_FEEDBACK));
65 :
66 1464 : maTimer.SetTimeout(_FEEDBACK_TIMEOUT);
67 1464 : }
68 : };
69 :
70 1464 : SvxModifyControl::SvxModifyControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStb ) :
71 : SfxStatusBarControl( _nSlotId, _nId, rStb ),
72 1464 : mpImpl(new ImplData)
73 : {
74 : //#ifndef MACOSX
75 1464 : if ( rStb.GetDPIScaleFactor() > 1 )
76 : {
77 0 : for (int i = 0; i < mpImpl->MODIFICATION_STATE_SIZE; i++)
78 : {
79 0 : BitmapEx b = mpImpl->maImages[i].GetBitmapEx();
80 0 : b.Scale(rStb.GetDPIScaleFactor(), rStb.GetDPIScaleFactor(), BMP_SCALE_FAST);
81 0 : mpImpl->maImages[i] = Image(b);
82 0 : }
83 : }
84 : //#endif
85 1464 : mpImpl->maTimer.SetTimeoutHdl( LINK(this, SvxModifyControl, OnTimer) );
86 1464 : }
87 :
88 :
89 :
90 1721 : void SvxModifyControl::StateChanged( sal_uInt16, SfxItemState eState,
91 : const SfxPoolItem* pState )
92 : {
93 1721 : if ( SfxItemState::DEFAULT != eState )
94 1721 : return;
95 :
96 : DBG_ASSERT( pState->ISA( SfxBoolItem ), "invalid item type" );
97 1721 : const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pState);
98 1721 : mpImpl->maTimer.Stop();
99 :
100 1721 : bool modified = pItem->GetValue();
101 1721 : bool start = ( !modified && mpImpl->mnModState == ImplData::MODIFICATION_STATE_YES); // should timer be started and feedback image displayed ?
102 :
103 1721 : mpImpl->mnModState = (start ? ImplData::MODIFICATION_STATE_FEEDBACK : (modified ? ImplData::MODIFICATION_STATE_YES : ImplData::MODIFICATION_STATE_NO));
104 :
105 1721 : _repaint();
106 :
107 1721 : int nResId = modified ? RID_SVXSTR_DOC_MODIFIED_YES : RID_SVXSTR_DOC_MODIFIED_NO;
108 1721 : GetStatusBar().SetQuickHelpText(GetId(), SVX_RESSTR(nResId));
109 :
110 1721 : if ( start )
111 0 : mpImpl->maTimer.Start();
112 : }
113 :
114 :
115 :
116 0 : IMPL_LINK( SvxModifyControl, OnTimer, Timer *, pTimer )
117 : {
118 0 : if (pTimer == 0)
119 0 : return 0;
120 :
121 0 : pTimer->Stop();
122 0 : mpImpl->mnModState = ImplData::MODIFICATION_STATE_NO;
123 :
124 0 : _repaint();
125 :
126 0 : return 0;
127 : }
128 :
129 :
130 :
131 1721 : void SvxModifyControl::_repaint()
132 : {
133 1721 : if ( GetStatusBar().AreItemsVisible() )
134 1721 : GetStatusBar().SetItemData( GetId(), 0 ); // force repaint
135 1721 : }
136 :
137 : /**
138 : * Given a bounding rectangle and an image, determine the top-left position
139 : * of the image so that the image would look centered both horizontally and
140 : * vertically.
141 : *
142 : * @param rBoundingRect bounding rectangle
143 : * @param rImg image
144 : *
145 : * @return Point top-left corner of the centered image position
146 : */
147 2102 : Point centerImage(const Rectangle& rBoundingRect, const Image& rImg)
148 : {
149 2102 : Size aImgSize = rImg.GetSizePixel();
150 2102 : Size aRectSize = rBoundingRect.GetSize();
151 2102 : long nXOffset = (aRectSize.getWidth() - aImgSize.getWidth())/2;
152 2102 : long nYOffset = (aRectSize.getHeight() - aImgSize.getHeight())/2;
153 2102 : Point aPt = rBoundingRect.TopLeft();
154 2102 : aPt += Point(nXOffset, nYOffset);
155 2102 : return aPt;
156 : }
157 :
158 2086 : void SvxModifyControl::Paint( const UserDrawEvent& rUsrEvt )
159 : {
160 2086 : OutputDevice* pDev = rUsrEvt.GetDevice();
161 2086 : Rectangle aRect = rUsrEvt.GetRect();
162 :
163 2086 : ImplData::ModificationState state = mpImpl->mnModState;
164 2086 : Point aPt = centerImage(aRect, mpImpl->maImages[state]);
165 2086 : pDev->DrawImage(aPt, mpImpl->maImages[state]);
166 2086 : }
167 :
168 0 : void SvxModifyControl::DoubleClick()
169 : {
170 0 : if (mpImpl->mnModState != ImplData::MODIFICATION_STATE_YES)
171 : // document not modified. nothing to do here.
172 0 : return;
173 :
174 0 : Sequence<PropertyValue> aArgs;
175 0 : execute(OUString(".uno:Save"), aArgs);
176 594 : }
177 :
178 :
179 :
180 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|