Line data Source code
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 : #include <sfx2/sidebar/ControllerItem.hxx>
20 :
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>
30 :
31 : #include <com/sun/star/frame/XFrame.hpp>
32 : #include <com/sun/star/frame/XFrameActionListener.hpp>
33 :
34 : using namespace css;
35 : using namespace css::uno;
36 :
37 : namespace
38 : {
39 : typedef ::cppu::WeakComponentImplHelper1 <
40 : css::frame::XFrameActionListener
41 : > FrameActionListenerInterfaceBase;
42 :
43 : class FrameActionListener
44 : : public ::cppu::BaseMutex,
45 : public FrameActionListenerInterfaceBase
46 : {
47 : public:
48 2143 : FrameActionListener (
49 : sfx2::sidebar::ControllerItem& rControllerItem,
50 : const Reference<frame::XFrame>& rxFrame)
51 : : FrameActionListenerInterfaceBase(m_aMutex),
52 : mrControllerItem(rControllerItem),
53 2143 : mxFrame(rxFrame)
54 : {
55 2143 : if (mxFrame.is())
56 2143 : mxFrame->addFrameActionListener(this);
57 2143 : }
58 4286 : virtual ~FrameActionListener()
59 2143 : {
60 4286 : }
61 2143 : virtual void SAL_CALL disposing() SAL_OVERRIDE
62 : {
63 2143 : SolarMutexGuard g;
64 2143 : if (mxFrame.is())
65 2143 : mxFrame->removeFrameActionListener(this);
66 2143 : }
67 0 : virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
68 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
69 : {
70 : (void)rEvent;
71 :
72 0 : SolarMutexGuard g;
73 0 : mrControllerItem.ResetFrame();
74 0 : mxFrame = NULL;
75 0 : }
76 6179 : virtual void SAL_CALL frameAction (const css::frame::FrameActionEvent& rEvent)
77 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
78 : {
79 6179 : SolarMutexGuard g;
80 6179 : if (rEvent.Action == frame::FrameAction_CONTEXT_CHANGED)
81 0 : mrControllerItem.NotifyFrameContextChange();
82 6179 : }
83 :
84 : private:
85 : sfx2::sidebar::ControllerItem& mrControllerItem;
86 : Reference<frame::XFrame> mxFrame;
87 : };
88 : }
89 :
90 : namespace sfx2 { namespace sidebar {
91 :
92 5031 : 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 5031 : msCommandName()
101 : {
102 5031 : }
103 :
104 2143 : 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 2143 : mxFrameActionListener(new FrameActionListener(*this, mxFrame)),
114 4286 : msCommandName(rsCommandName)
115 : {
116 2143 : }
117 :
118 0 : ControllerItem::~ControllerItem()
119 : {
120 0 : dispose();
121 0 : }
122 :
123 7174 : void ControllerItem::dispose()
124 : {
125 7174 : if (mxFrameActionListener.is())
126 2143 : mxFrameActionListener->dispose();
127 7174 : mxFrameActionListener.clear();
128 :
129 7174 : SfxControllerItem::dispose();
130 7174 : }
131 :
132 5040 : void ControllerItem::StateChanged (
133 : sal_uInt16 nSID,
134 : SfxItemState eState,
135 : const SfxPoolItem* pState)
136 : {
137 5040 : mrItemUpdateReceiver.NotifyItemUpdate(nSID, eState, pState, IsEnabled(eState));
138 5040 : }
139 :
140 5378 : bool ControllerItem::IsEnabled (SfxItemState eState) const
141 : {
142 5378 : if (eState == SfxItemState::DISABLED)
143 196 : return false;
144 5182 : else if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED))
145 : {
146 : // There are no disabled commands.
147 5182 : return true;
148 : }
149 0 : else if (msCommandName.getLength() == 0)
150 : {
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 0 : return true;
154 : }
155 0 : else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED, msCommandName))
156 : {
157 : // The command is part of a list of disabled commands.
158 0 : return false;
159 : }
160 : else
161 0 : return true;
162 : }
163 :
164 338 : void ControllerItem::RequestUpdate()
165 : {
166 338 : SfxPoolItem* pState = NULL;
167 338 : const SfxItemState eState (GetBindings().QueryState(GetId(), pState));
168 338 : mrItemUpdateReceiver.NotifyItemUpdate(GetId(), eState, pState, IsEnabled(eState));
169 338 : }
170 :
171 0 : void ControllerItem::NotifyFrameContextChange()
172 : {
173 0 : RequestUpdate();
174 0 : }
175 :
176 0 : void ControllerItem::ResetFrame()
177 : {
178 0 : mxFrame = NULL;
179 0 : }
180 :
181 978 : ::rtl::OUString ControllerItem::GetLabel() const
182 : {
183 978 : return CommandInfoProvider::Instance().GetLabelForCommand(
184 1956 : ".uno:" + msCommandName,
185 3912 : mxFrame);
186 : }
187 :
188 978 : ::rtl::OUString ControllerItem::GetHelpText() const
189 : {
190 978 : Help* pHelp = Application::GetHelp();
191 978 : if (pHelp != NULL)
192 : {
193 978 : if (msCommandName.getLength() > 0)
194 : {
195 978 : const ::rtl::OUString sHelp (pHelp->GetHelpText(".uno:" + msCommandName, NULL));
196 978 : return sHelp;
197 : }
198 : }
199 0 : return ::rtl::OUString();
200 : }
201 :
202 1654 : Image ControllerItem::GetIcon() const
203 : {
204 1654 : return GetImage(mxFrame, ".uno:" + msCommandName, false);
205 : }
206 :
207 0 : ControllerItem::ItemUpdateReceiverInterface::~ItemUpdateReceiverInterface()
208 : {
209 0 : }
210 :
211 978 : void ControllerItem::SetupToolBoxItem (ToolBox& rToolBox, const sal_uInt16 nIndex)
212 : {
213 978 : rToolBox.SetQuickHelpText(nIndex, GetLabel());
214 978 : rToolBox.SetHelpText(nIndex, GetHelpText());
215 978 : rToolBox.SetItemImage(nIndex, GetIcon());
216 978 : }
217 :
218 648 : } } // end of namespace sfx2::sidebar
219 :
220 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|