/src/libreoffice/svx/source/stbctrls/modctrl.cxx
Line | Count | Source |
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 | | |
39 | | SFX_IMPL_STATUSBAR_CONTROL(SvxModifyControl, SfxBoolItem); |
40 | | |
41 | | struct SvxModifyControl::ImplData |
42 | | { |
43 | | enum ModificationState |
44 | | { |
45 | | MODIFICATION_STATE_NO = 0, |
46 | | MODIFICATION_STATE_YES, |
47 | | MODIFICATION_STATE_FEEDBACK, |
48 | | MODIFICATION_STATE_SIZE |
49 | | }; |
50 | | |
51 | | Idle maIdle { "svx::SvxModifyControl maIdle" }; |
52 | | Image maImages[MODIFICATION_STATE_SIZE]; |
53 | | |
54 | | ModificationState mnModState; |
55 | | |
56 | | ImplData(): |
57 | 0 | mnModState(MODIFICATION_STATE_NO) |
58 | 0 | { |
59 | 0 | maImages[MODIFICATION_STATE_NO] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_NO); |
60 | 0 | maImages[MODIFICATION_STATE_YES] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_YES); |
61 | 0 | maImages[MODIFICATION_STATE_FEEDBACK] = Image(StockImage::Yes, RID_SVXBMP_DOC_MODIFIED_FEEDBACK); |
62 | |
|
63 | 0 | maIdle.SetPriority(TaskPriority::LOWEST); |
64 | 0 | } |
65 | | }; |
66 | | |
67 | | SvxModifyControl::SvxModifyControl( sal_uInt16 _nSlotId, sal_uInt16 _nId, StatusBar& rStb ) : |
68 | 0 | SfxStatusBarControl( _nSlotId, _nId, rStb ), |
69 | 0 | mxImpl(std::make_shared<ImplData>()) |
70 | 0 | { |
71 | 0 | mxImpl->maIdle.SetInvokeHandler( LINK(this, SvxModifyControl, OnTimer) ); |
72 | 0 | } |
73 | | |
74 | | |
75 | | void SvxModifyControl::StateChangedAtStatusBarControl( sal_uInt16, SfxItemState eState, |
76 | | const SfxPoolItem* pState ) |
77 | 0 | { |
78 | 0 | if ( SfxItemState::DEFAULT != eState ) |
79 | 0 | return; |
80 | | |
81 | 0 | DBG_ASSERT( dynamic_cast<const SfxBoolItem*>( pState) != nullptr, "invalid item type" ); |
82 | 0 | const SfxBoolItem* pItem = static_cast<const SfxBoolItem*>(pState); |
83 | 0 | mxImpl->maIdle.Stop(); |
84 | |
|
85 | 0 | bool modified = pItem->GetValue(); |
86 | 0 | bool start = ( !modified && mxImpl->mnModState == ImplData::MODIFICATION_STATE_YES); // should timer be started and feedback image displayed ? |
87 | |
|
88 | 0 | mxImpl->mnModState = (start ? ImplData::MODIFICATION_STATE_FEEDBACK : (modified ? ImplData::MODIFICATION_STATE_YES : ImplData::MODIFICATION_STATE_NO)); |
89 | |
|
90 | 0 | _repaint(); |
91 | |
|
92 | 0 | TranslateId pResId = modified ? RID_SVXSTR_DOC_MODIFIED_YES : RID_SVXSTR_DOC_MODIFIED_NO; |
93 | 0 | GetStatusBar().SetQuickHelpText(GetId(), SvxResId(pResId)); |
94 | |
|
95 | 0 | if ( start ) |
96 | 0 | mxImpl->maIdle.Start(); |
97 | 0 | } |
98 | | |
99 | | |
100 | | IMPL_LINK( SvxModifyControl, OnTimer, Timer *, pTimer, void ) |
101 | 0 | { |
102 | 0 | if (pTimer == nullptr) |
103 | 0 | return; |
104 | | |
105 | 0 | pTimer->Stop(); |
106 | 0 | mxImpl->mnModState = ImplData::MODIFICATION_STATE_NO; |
107 | |
|
108 | 0 | _repaint(); |
109 | 0 | } |
110 | | |
111 | | |
112 | | void SvxModifyControl::_repaint() |
113 | 0 | { |
114 | 0 | GetStatusBar().SetItemData( GetId(), nullptr ); // force repaint |
115 | 0 | } |
116 | | |
117 | | /** |
118 | | * Given a bounding rectangle and an image, determine the top-left position |
119 | | * of the image so that the image would look centered both horizontally and |
120 | | * vertically. |
121 | | * |
122 | | * @param rBoundingRect bounding rectangle |
123 | | * @param rImg image |
124 | | * |
125 | | * @return Point top-left corner of the centered image position |
126 | | */ |
127 | | Point centerImage(const tools::Rectangle& rBoundingRect, const Image& rImg) |
128 | 0 | { |
129 | 0 | Size aImgSize = rImg.GetSizePixel(); |
130 | 0 | Size aRectSize = rBoundingRect.GetSize(); |
131 | 0 | tools::Long nXOffset = (aRectSize.getWidth() - aImgSize.getWidth())/2; |
132 | 0 | tools::Long nYOffset = (aRectSize.getHeight() - aImgSize.getHeight())/2; |
133 | 0 | Point aPt = rBoundingRect.TopLeft(); |
134 | 0 | aPt += Point(nXOffset, nYOffset); |
135 | 0 | return aPt; |
136 | 0 | } |
137 | | |
138 | | void SvxModifyControl::Paint( const UserDrawEvent& rUsrEvt ) |
139 | 0 | { |
140 | 0 | vcl::RenderContext* pDev = rUsrEvt.GetRenderContext(); |
141 | 0 | tools::Rectangle aRect(rUsrEvt.GetRect()); |
142 | |
|
143 | 0 | ImplData::ModificationState state = mxImpl->mnModState; |
144 | 0 | Point aPt = centerImage(aRect, mxImpl->maImages[state]); |
145 | 0 | pDev->DrawImage(aPt, mxImpl->maImages[state]); |
146 | 0 | } |
147 | | |
148 | | void SvxModifyControl::Click() |
149 | 0 | { |
150 | 0 | if (mxImpl->mnModState != ImplData::MODIFICATION_STATE_YES) |
151 | | // document not modified. nothing to do here. |
152 | 0 | return; |
153 | | |
154 | 0 | Sequence<PropertyValue> aArgs; |
155 | 0 | execute(u".uno:Save"_ustr, aArgs); |
156 | 0 | } |
157 | | |
158 | | |
159 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |