bump product version to 5.0.4.1
[LibreOffice.git] / sfx2 / source / control / ctrlitem.cxx
blob0be5e36658b75b9933be69644e6ba5d3e03cb16a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <svl/itempool.hxx>
22 #include <sfx2/ctrlitem.hxx>
23 #include <sfx2/bindings.hxx>
24 #include <sfx2/dispatch.hxx>
25 #include <sfx2/msgpool.hxx>
26 #include "statcach.hxx"
27 #include <sfx2/viewfrm.hxx>
29 #ifdef DBG_UTIL
31 void SfxControllerItem::CheckConfigure_Impl( SfxSlotMode nType )
33 // Real Slot? (i.e. no Separator etc.)
34 if ( !nId )
35 return;
37 // is the ID configurable at all in 'nType'?
38 const SfxSlot *pSlot = SFX_SLOTPOOL().GetSlot(nId);
39 DBG_ASSERTWARNING( pSlot, "SfxControllerItem: binding not existing slot" );
40 SAL_WARN_IF(
41 pSlot && !pSlot->IsMode(nType), "sfx.control",
42 "SfxControllerItem: slot without ...Config-flag at SID "
43 << pSlot->GetSlotId());
46 #endif
49 // returns the next registered SfxControllerItem with the same id
51 SfxControllerItem* SfxControllerItem::GetItemLink()
53 return pNext == this ? 0 : pNext;
57 // returns sal_True if this binding is really bound to a function
59 bool SfxControllerItem::IsBound() const
61 return pNext != this;
65 // registeres with the id at the bindings
67 void SfxControllerItem::Bind( sal_uInt16 nNewId, SfxBindings *pBindinx )
69 DBG_ASSERT(pBindings || pBindinx, "No Bindings");
71 if ( IsBound() ) {
72 DBG_ASSERT(pBindings, "No Bindings");
73 pBindings->Release(*this);
76 nId = nNewId;
77 pNext = 0;
79 if (pBindinx)
80 pBindings = pBindinx;
81 pBindings->Register(*this);
84 void SfxControllerItem::BindInternal_Impl( sal_uInt16 nNewId, SfxBindings *pBindinx )
86 DBG_ASSERT(pBindings || pBindinx, "No Bindings");
88 if ( IsBound() ) {
89 DBG_ASSERT(pBindings, "No Bindings");
90 pBindings->Release(*this);
93 nId = nNewId;
94 pNext = 0;
96 if (pBindinx)
97 pBindings = pBindinx;
98 pBindings->RegisterInternal_Impl(*this);
104 void SfxControllerItem::UnBind()
106 /* [Description]
108 Unbinds the connection of this SfxControllerItems with the SfxBindings
109 instance with which it to time is bound. From this time on it does not
110 receive any status notifications (<SfxControllerItem::StateChented()>)
111 anymore.
113 [Cross-reference]
115 <SfxControllerItem::ReBind()>
116 <SfxControllerItem::ClearCache()>
119 DBG_ASSERT(pBindings, "No Bindings");
120 DBG_ASSERT( IsBound(), "unbindings unbound SfxControllerItem" );
122 pBindings->Release(*this);
123 pNext = this;
128 void SfxControllerItem::ReBind()
130 /* [Description]
132 Binds this SfxControllerItem with the SfxBindings instance again,
133 with which it was last bound. From this time on it does receive status
134 notifications (<SfxControllerItem::StateChented()>) again.
136 [Cross-reference]
138 <SfxControllerItem::UnBind()>
139 <SfxControllerItem::ClearCache()>
143 DBG_ASSERT(pBindings, "No Bindings");
144 DBG_ASSERT( !IsBound(), "bindings rebound SfxControllerItem" );
146 pBindings->Register(*this);
151 void SfxControllerItem::ClearCache()
153 /* [Description]
155 Clears the cache status for this SfxControllerItem. That is by the next
156 status update is the <SfxPoolItem> sent in any case, even if the same was
157 sent before. This is needed if a controller can be switched on and note
158 that status themselves.
160 [Example]
162 The combined controller for adjusting the surface type and the concrete
163 expression (blue color, or hatching X) can be changed in type, but is then
164 notified of the next selection again, even if it the same data.
166 [Cross-reference]
168 <SfxControllerItem::UnBind()>
169 <SfxControllerItem::ReBind()>
174 DBG_ASSERT(pBindings, "No Bindings");
176 pBindings->ClearCache_Impl( GetId() );
180 // replaces the successor in the list of bindings of the same id
182 SfxControllerItem* SfxControllerItem::ChangeItemLink( SfxControllerItem* pNewLink )
184 SfxControllerItem* pOldLink = pNext;
185 pNext = pNewLink;
186 return pOldLink == this ? 0 : pOldLink;
190 // changes the id of unbound functions (e.g. for sub-menu-ids)
192 void SfxControllerItem::SetId( sal_uInt16 nItemId )
194 DBG_ASSERT( !IsBound(), "changing id of bound binding" );
195 nId = nItemId;
200 // creates a atomic item for a controller without registration.
202 SfxControllerItem::SfxControllerItem():
203 nId(0),
204 pNext(this),
205 pBindings(0)
210 // creates a representation of the function nId and registeres it
212 SfxControllerItem::SfxControllerItem( sal_uInt16 nID, SfxBindings &rBindings ):
213 nId(nID),
214 pNext(this),
215 pBindings(&rBindings)
217 Bind(nId, &rBindings);
221 // unregisters the item in the bindings
223 SfxControllerItem::~SfxControllerItem()
225 dispose();
228 void SfxControllerItem::dispose()
230 if ( IsBound() )
231 pBindings->Release(*this);
234 void SfxControllerItem::StateChanged
236 sal_uInt16, // <SID> of the triggering slot
237 SfxItemState, // <SfxItemState> of 'pState'
238 const SfxPoolItem* // Slot-Status, NULL or IsInvalidItem()
241 /* [Description]
243 This virtual method is called by the SFx to inform the <SfxControllerItem>s
244 is about that state of the slots 'NSID' has changed. The new value and the
245 value determined by this status is given as 'pState' or 'eState'.
247 The status of a slot may change, for example when the MDI window is
248 switched or when the slot was invalidated explicitly with
249 <SfxBindings::Invalidate()>.
251 Achtung! Die Methode wird nicht gerufen, wenn der Slot ung"ultig wurde,
252 danach jedoch wieder denselben Wert angenommen hat.
254 Beware! The method is not called when the slot is invalid, however
255 has again assumed the same value.
257 This base class need not be called, further interim steps however
258 (eg <SfxToolboxControl> ) should be called.
266 void SfxStatusForwarder::StateChanged
268 sal_uInt16 nSID, // <SID> of the triggering slot
269 SfxItemState eState, // <SfxItemState> of 'pState'
270 const SfxPoolItem* pState // Slot-Status, NULL or IsInvalidItem()
274 pMaster->StateChanged( nSID, eState, pState );
279 SfxStatusForwarder::SfxStatusForwarder(
280 sal_uInt16 nSlotId,
281 SfxControllerItem& rMaster ):
282 SfxControllerItem( nSlotId, rMaster.GetBindings() ),
283 pMaster( &rMaster )
289 SfxItemState SfxControllerItem::GetItemState
291 const SfxPoolItem* pState /* Pointer to <SfxPoolItem>, which
292 Status should be queried. */
295 /* [Description]
297 Static method to determine the status of the SfxPoolItem-Pointers, to be
298 used in the method <SfxControllerItem::StateChanged(const SfxPoolItem*)>
300 [Return value]
302 SfxItemState SfxItemState::UNKNOWN
303 Enabled, but no further status information available.
304 Typical for <Slot>s, which anyway are sometimes
305 disabled, but otherwise do not change their appearance.
307 SfxItemState::DISABLED
308 Disabled and no further status information available.
309 All other values that may appear should be reset to
310 default.
312 SfxItemState::DONTCARE
313 Enabled but there were only ambiguous values available
314 (i.e. non that can be queried).
316 SfxItemState::DEFAULT
317 Enabled and with available values, which are queried
318 by 'pState'. The Type is thus clearly defined in the
319 entire Program and specified through the Slot.
323 return !pState
324 ? SfxItemState::DISABLED
325 : IsInvalidItem(pState)
326 ? SfxItemState::DONTCARE
327 : pState->ISA(SfxVoidItem) && !pState->Which()
328 ? SfxItemState::UNKNOWN
329 : SfxItemState::DEFAULT;
334 SfxMapUnit SfxControllerItem::GetCoreMetric() const
336 /* [Description]
338 Gets the measurement unit from the competent pool, in which the Status
339 item exist.
343 SfxStateCache *pCache = pBindings->GetStateCache( nId );
344 SfxDispatcher *pDispat = pBindings->GetDispatcher_Impl();
346 if ( !pDispat )
348 SfxViewFrame* pViewFrame = SfxViewFrame::Current();
349 if ( !pViewFrame )
350 SfxViewFrame::GetFirst();
351 if ( pViewFrame )
352 pDispat = pViewFrame->GetDispatcher();
355 if ( pDispat && pCache )
357 const SfxSlotServer *pServer = pCache->GetSlotServer( *pDispat );
358 if ( pServer )
360 SfxShell *pSh = pDispat->GetShell( pServer->GetShellLevel() );
361 SfxItemPool &rPool = pSh->GetPool();
362 sal_uInt16 nWhich = rPool.GetWhich( nId );
364 // invalidate slot and its message|slot server as 'global' information
365 // about the validated message|slot server is not made available
366 pCache->Invalidate( true );
368 return rPool.GetMetric( nWhich );
372 DBG_WARNING( "W1: Can not find ItemPool!" );
373 return SFX_MAPUNIT_100TH_MM;
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */