bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / sidebar / ControllerItem.cxx
blobeb22a184054fcc83efb7a489339bbd0f54f8943e
1 /*
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>
34 using namespace css;
35 using namespace cssu;
38 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
40 namespace
42 typedef ::cppu::WeakComponentImplHelper1 <
43 css::frame::XFrameActionListener
44 > FrameActionListenerInterfaceBase;
46 class FrameActionListener
47 : public ::cppu::BaseMutex,
48 public FrameActionListenerInterfaceBase
50 public:
51 FrameActionListener (
52 sfx2::sidebar::ControllerItem& rControllerItem,
53 const Reference<frame::XFrame>& rxFrame)
54 : FrameActionListenerInterfaceBase(m_aMutex),
55 mrControllerItem(rControllerItem),
56 mxFrame(rxFrame)
58 if (mxFrame.is())
59 mxFrame->addFrameActionListener(this);
61 virtual ~FrameActionListener (void)
64 virtual void SAL_CALL disposing (void)
66 if (mxFrame.is())
67 mxFrame->removeFrameActionListener(this);
69 virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
70 throw (cssu::RuntimeException)
72 (void)rEvent;
73 mrControllerItem.ResetFrame();
74 mxFrame = NULL;
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();
83 private:
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),
97 mxFrame(),
98 mxFrameActionListener(),
99 msCommandName()
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),
114 mxFrame(rxFrame),
115 mxFrameActionListener(new FrameActionListener(*this, mxFrame)),
116 msCommandName(rsCommandName)
123 ControllerItem::~ControllerItem (void)
125 if (mxFrameActionListener.is())
126 mxFrameActionListener->dispose();
132 void ControllerItem::StateChanged (
133 sal_uInt16 nSID,
134 SfxItemState eState,
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)
146 return false;
147 else if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED))
149 // There are no disabled commands.
150 return true;
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.
156 return true;
158 else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED, msCommandName))
160 // The command is part of a list of disabled commands.
161 return false;
163 else
164 return true;
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)
182 RequestUpdate();
188 void ControllerItem::ResetFrame (void)
190 mxFrame = NULL;
196 ::rtl::OUString ControllerItem::GetLabel (void) const
198 return CommandInfoProvider::Instance().GetLabelForCommand(
199 A2S(".uno:")+msCommandName,
200 mxFrame);
206 ::rtl::OUString ControllerItem::GetHelpText (void) const
208 Help* pHelp = Application::GetHelp();
209 if (pHelp != NULL)
211 if (msCommandName.getLength() > 0)
213 const ::rtl::OUString sHelp (pHelp->GetHelpText(A2S(".uno:")+msCommandName, NULL));
214 return sHelp;
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