2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 #include "sidebar/ControllerItem.hxx"
20 #include <sfx2/msgpool.hxx>
21 #include <sfx2/viewsh.hxx>
22 #include "sfx2/imagemgr.hxx"
23 #include "sfx2/bindings.hxx"
24 #include <unotools/cmdoptions.hxx>
25 #include "sfx2/sidebar/CommandInfoProvider.hxx"
26 #include <vcl/svapp.hxx>
27 #include <vcl/toolbox.hxx>
28 #include <vcl/help.hxx>
30 #include <com/sun/star/frame/XFrame.hpp>
31 #include <com/sun/star/frame/XFrameActionListener.hpp>
38 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
42 typedef ::cppu::WeakComponentImplHelper1
<
43 css::frame::XFrameActionListener
44 > FrameActionListenerInterfaceBase
;
46 class FrameActionListener
47 : public ::cppu::BaseMutex
,
48 public FrameActionListenerInterfaceBase
52 sfx2::sidebar::ControllerItem
& rControllerItem
,
53 const Reference
<frame::XFrame
>& rxFrame
)
54 : FrameActionListenerInterfaceBase(m_aMutex
),
55 mrControllerItem(rControllerItem
),
59 mxFrame
->addFrameActionListener(this);
61 virtual ~FrameActionListener (void)
64 virtual void SAL_CALL
disposing (void)
67 mxFrame
->removeFrameActionListener(this);
69 virtual void SAL_CALL
disposing (const css::lang::EventObject
& rEvent
)
70 throw (cssu::RuntimeException
)
73 mrControllerItem
.ResetFrame();
76 virtual void SAL_CALL
frameAction (const css::frame::FrameActionEvent
& rEvent
)
77 throw (cssu::RuntimeException
)
79 if (rEvent
.Action
== frame::FrameAction_CONTEXT_CHANGED
)
80 mrControllerItem
.NotifyFrameContextChange();
84 sfx2::sidebar::ControllerItem
& mrControllerItem
;
85 Reference
<frame::XFrame
> mxFrame
;
89 namespace sfx2
{ namespace sidebar
{
91 ControllerItem::ControllerItem (
92 const sal_uInt16 nSlotId
,
93 SfxBindings
&rBindings
,
94 ItemUpdateReceiverInterface
& rItemUpdateReceiver
)
95 : SfxControllerItem(nSlotId
, rBindings
),
96 mrItemUpdateReceiver(rItemUpdateReceiver
),
98 mxFrameActionListener(),
106 ControllerItem::ControllerItem (
107 const sal_uInt16 nSlotId
,
108 SfxBindings
&rBindings
,
109 ItemUpdateReceiverInterface
& rItemUpdateReceiver
,
110 const ::rtl::OUString
& rsCommandName
,
111 const Reference
<frame::XFrame
>& rxFrame
)
112 : SfxControllerItem(nSlotId
, rBindings
),
113 mrItemUpdateReceiver(rItemUpdateReceiver
),
115 mxFrameActionListener(new FrameActionListener(*this, mxFrame
)),
116 msCommandName(rsCommandName
)
123 ControllerItem::~ControllerItem (void)
125 if (mxFrameActionListener
.is())
126 mxFrameActionListener
->dispose();
132 void ControllerItem::StateChanged (
135 const SfxPoolItem
* pState
)
137 mrItemUpdateReceiver
.NotifyItemUpdate(nSID
, eState
, pState
, IsEnabled(eState
));
143 bool ControllerItem::IsEnabled (SfxItemState eState
) const
145 if (eState
== SFX_ITEM_DISABLED
)
147 else if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED
))
149 // There are no disabled commands.
152 else if (msCommandName
.getLength() == 0)
154 // We were not given a command name at construction and can
155 // not check the state now. Assume the best and return true.
158 else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED
, msCommandName
))
160 // The command is part of a list of disabled commands.
170 void ControllerItem::RequestUpdate (void)
172 SfxPoolItem
* pState
= NULL
;
173 const SfxItemState
eState (GetBindings().QueryState(GetId(), pState
));
174 mrItemUpdateReceiver
.NotifyItemUpdate(GetId(), eState
, pState
, IsEnabled(eState
));
180 void ControllerItem::NotifyFrameContextChange (void)
188 void ControllerItem::ResetFrame (void)
196 ::rtl::OUString
ControllerItem::GetLabel (void) const
198 return CommandInfoProvider::Instance().GetLabelForCommand(
199 A2S(".uno:")+msCommandName
,
206 ::rtl::OUString
ControllerItem::GetHelpText (void) const
208 Help
* pHelp
= Application::GetHelp();
211 if (msCommandName
.getLength() > 0)
213 const ::rtl::OUString
sHelp (pHelp
->GetHelpText(A2S(".uno:")+msCommandName
, NULL
));
217 return ::rtl::OUString();
223 Image
ControllerItem::GetIcon (void) const
225 return GetImage(mxFrame
, A2S(".uno:")+msCommandName
, sal_False
);
231 ControllerItem::ItemUpdateReceiverInterface::~ItemUpdateReceiverInterface()
236 void ControllerItem::SetupToolBoxItem (ToolBox
& rToolBox
, const sal_uInt16 nIndex
)
238 rToolBox
.SetQuickHelpText(nIndex
, GetLabel());
239 rToolBox
.SetHelpText(nIndex
, GetHelpText());
240 rToolBox
.SetItemImage(nIndex
, GetIcon());
244 } } // end of namespace sfx2::sidebar