Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / sidebar / ControllerItem.cxx
blob22dd244e52e08c692b38f942ab500a86f2522eaa
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 .
19 #include <sfx2/sidebar/ControllerItem.hxx>
21 #include <sfx2/msgpool.hxx>
22 #include <sfx2/viewsh.hxx>
23 #include <sfx2/imagemgr.hxx>
24 #include <sfx2/bindings.hxx>
25 #include <unotools/cmdoptions.hxx>
26 #include <sfx2/sidebar/CommandInfoProvider.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/toolbox.hxx>
29 #include <vcl/help.hxx>
31 #include <com/sun/star/frame/XFrame.hpp>
32 #include <com/sun/star/frame/XFrameActionListener.hpp>
34 using namespace css;
35 using namespace css::uno;
37 namespace
39 typedef ::cppu::WeakComponentImplHelper1 <
40 css::frame::XFrameActionListener
41 > FrameActionListenerInterfaceBase;
43 class FrameActionListener
44 : public ::cppu::BaseMutex,
45 public FrameActionListenerInterfaceBase
47 public:
48 FrameActionListener (
49 sfx2::sidebar::ControllerItem& rControllerItem,
50 const Reference<frame::XFrame>& rxFrame)
51 : FrameActionListenerInterfaceBase(m_aMutex),
52 mrControllerItem(rControllerItem),
53 mxFrame(rxFrame)
55 if (mxFrame.is())
56 mxFrame->addFrameActionListener(this);
58 virtual ~FrameActionListener()
61 virtual void SAL_CALL disposing() SAL_OVERRIDE
63 SolarMutexGuard g;
64 if (mxFrame.is())
65 mxFrame->removeFrameActionListener(this);
67 virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
68 throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
70 (void)rEvent;
72 SolarMutexGuard g;
73 mrControllerItem.ResetFrame();
74 mxFrame = NULL;
76 virtual void SAL_CALL frameAction (const css::frame::FrameActionEvent& rEvent)
77 throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
79 SolarMutexGuard g;
80 if (rEvent.Action == frame::FrameAction_CONTEXT_CHANGED)
81 mrControllerItem.NotifyFrameContextChange();
84 private:
85 sfx2::sidebar::ControllerItem& mrControllerItem;
86 Reference<frame::XFrame> mxFrame;
90 namespace sfx2 { namespace sidebar {
92 ControllerItem::ControllerItem (
93 const sal_uInt16 nSlotId,
94 SfxBindings &rBindings,
95 ItemUpdateReceiverInterface& rItemUpdateReceiver)
96 : SfxControllerItem(nSlotId, rBindings),
97 mrItemUpdateReceiver(rItemUpdateReceiver),
98 mxFrame(),
99 mxFrameActionListener(),
100 msCommandName()
104 ControllerItem::ControllerItem (
105 const sal_uInt16 nSlotId,
106 SfxBindings &rBindings,
107 ItemUpdateReceiverInterface& rItemUpdateReceiver,
108 const ::rtl::OUString& rsCommandName,
109 const Reference<frame::XFrame>& rxFrame)
110 : SfxControllerItem(nSlotId, rBindings),
111 mrItemUpdateReceiver(rItemUpdateReceiver),
112 mxFrame(rxFrame),
113 mxFrameActionListener(new FrameActionListener(*this, mxFrame)),
114 msCommandName(rsCommandName)
118 ControllerItem::~ControllerItem()
120 dispose();
123 void ControllerItem::dispose()
125 if (mxFrameActionListener.is())
126 mxFrameActionListener->dispose();
127 mxFrameActionListener.clear();
129 SfxControllerItem::dispose();
132 void ControllerItem::StateChanged (
133 sal_uInt16 nSID,
134 SfxItemState eState,
135 const SfxPoolItem* pState)
137 mrItemUpdateReceiver.NotifyItemUpdate(nSID, eState, pState, IsEnabled(eState));
140 bool ControllerItem::IsEnabled (SfxItemState eState) const
142 if (eState == SfxItemState::DISABLED)
143 return false;
144 else if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED))
146 // There are no disabled commands.
147 return true;
149 else if (msCommandName.getLength() == 0)
151 // We were not given a command name at construction and can
152 // not check the state now. Assume the best and return true.
153 return true;
155 else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED, msCommandName))
157 // The command is part of a list of disabled commands.
158 return false;
160 else
161 return true;
164 void ControllerItem::RequestUpdate()
166 SfxPoolItem* pState = NULL;
167 const SfxItemState eState (GetBindings().QueryState(GetId(), pState));
168 mrItemUpdateReceiver.NotifyItemUpdate(GetId(), eState, pState, IsEnabled(eState));
171 void ControllerItem::NotifyFrameContextChange()
173 RequestUpdate();
176 void ControllerItem::ResetFrame()
178 mxFrame = NULL;
181 ::rtl::OUString ControllerItem::GetLabel() const
183 return CommandInfoProvider::Instance().GetLabelForCommand(
184 ".uno:" + msCommandName,
185 mxFrame);
188 ::rtl::OUString ControllerItem::GetHelpText() const
190 Help* pHelp = Application::GetHelp();
191 if (pHelp != NULL)
193 if (msCommandName.getLength() > 0)
195 const ::rtl::OUString sHelp (pHelp->GetHelpText(".uno:" + msCommandName, NULL));
196 return sHelp;
199 return ::rtl::OUString();
202 Image ControllerItem::GetIcon() const
204 return GetImage(mxFrame, ".uno:" + msCommandName, false);
207 ControllerItem::ItemUpdateReceiverInterface::~ItemUpdateReceiverInterface()
211 void ControllerItem::SetupToolBoxItem (ToolBox& rToolBox, const sal_uInt16 nIndex)
213 rToolBox.SetQuickHelpText(nIndex, GetLabel());
214 rToolBox.SetHelpText(nIndex, GetHelpText());
215 rToolBox.SetItemImage(nIndex, GetIcon());
218 } } // end of namespace sfx2::sidebar
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */