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