1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sfx2.hxx"
23 
24 #include "sidebar/ControllerItem.hxx"
25 
26 #include <sfx2/msgpool.hxx>
27 #include <sfx2/viewsh.hxx>
28 #include "sfx2/imagemgr.hxx"
29 #include "sfx2/bindings.hxx"
30 #include <unotools/cmdoptions.hxx>
31 #include "sfx2/sidebar/CommandInfoProvider.hxx"
32 #include <vcl/svapp.hxx>
33 #include <vcl/toolbox.hxx>
34 #include <vcl/help.hxx>
35 
36 #include <com/sun/star/frame/XFrame.hpp>
37 #include <com/sun/star/frame/XFrameActionListener.hpp>
38 
39 
40 using namespace css;
41 using namespace cssu;
42 
43 
44 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
45 
46 namespace
47 {
48     typedef ::cppu::WeakComponentImplHelper1 <
49         css::frame::XFrameActionListener
50         > FrameActionListenerInterfaceBase;
51 
52     class FrameActionListener
53         : public ::cppu::BaseMutex,
54           public FrameActionListenerInterfaceBase
55     {
56     public:
FrameActionListener(sfx2::sidebar::ControllerItem & rControllerItem,const Reference<frame::XFrame> & rxFrame)57         FrameActionListener (
58             sfx2::sidebar::ControllerItem& rControllerItem,
59             const Reference<frame::XFrame>& rxFrame)
60             : FrameActionListenerInterfaceBase(m_aMutex),
61               mrControllerItem(rControllerItem),
62               mxFrame(rxFrame)
63         {
64             if (mxFrame.is())
65                 mxFrame->addFrameActionListener(this);
66         }
~FrameActionListener(void)67         virtual ~FrameActionListener (void)
68         {
69         }
disposing(void)70         virtual void SAL_CALL disposing (void)
71         {
72             if (mxFrame.is())
73                 mxFrame->removeFrameActionListener(this);
74         }
disposing(const css::lang::EventObject & rEvent)75         virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
76             throw (cssu::RuntimeException)
77         {
78             (void)rEvent;
79             mrControllerItem.ResetFrame();
80             mxFrame = NULL;
81         }
frameAction(const css::frame::FrameActionEvent & rEvent)82         virtual void SAL_CALL frameAction (const css::frame::FrameActionEvent& rEvent)
83             throw (cssu::RuntimeException)
84         {
85             if (rEvent.Action == frame::FrameAction_CONTEXT_CHANGED)
86                 mrControllerItem.NotifyFrameContextChange();
87         }
88 
89     private:
90         sfx2::sidebar::ControllerItem& mrControllerItem;
91         Reference<frame::XFrame> mxFrame;
92     };
93 }
94 
95 namespace sfx2 { namespace sidebar {
96 
ControllerItem(const sal_uInt16 nSlotId,SfxBindings & rBindings,ItemUpdateReceiverInterface & rItemUpdateReceiver)97 ControllerItem::ControllerItem (
98     const sal_uInt16 nSlotId,
99     SfxBindings &rBindings,
100     ItemUpdateReceiverInterface& rItemUpdateReceiver)
101     : SfxControllerItem(nSlotId, rBindings),
102       mrItemUpdateReceiver(rItemUpdateReceiver),
103       mxFrame(),
104       mxFrameActionListener(),
105       msCommandName()
106 {
107 }
108 
109 
110 
111 
ControllerItem(const sal_uInt16 nSlotId,SfxBindings & rBindings,ItemUpdateReceiverInterface & rItemUpdateReceiver,const::rtl::OUString & rsCommandName,const Reference<frame::XFrame> & rxFrame)112 ControllerItem::ControllerItem (
113     const sal_uInt16 nSlotId,
114     SfxBindings &rBindings,
115     ItemUpdateReceiverInterface& rItemUpdateReceiver,
116     const ::rtl::OUString& rsCommandName,
117     const Reference<frame::XFrame>& rxFrame)
118     : SfxControllerItem(nSlotId, rBindings),
119       mrItemUpdateReceiver(rItemUpdateReceiver),
120       mxFrame(rxFrame),
121       mxFrameActionListener(new FrameActionListener(*this, mxFrame)),
122       msCommandName(rsCommandName)
123 {
124 }
125 
126 
127 
128 
~ControllerItem(void)129 ControllerItem::~ControllerItem (void)
130 {
131     if (mxFrameActionListener.is())
132         mxFrameActionListener->dispose();
133 }
134 
135 
136 
137 
StateChanged(sal_uInt16 nSID,SfxItemState eState,const SfxPoolItem * pState)138 void ControllerItem::StateChanged (
139     sal_uInt16 nSID,
140     SfxItemState eState,
141     const SfxPoolItem* pState)
142 {
143     mrItemUpdateReceiver.NotifyItemUpdate(nSID, eState, pState, IsEnabled(eState));
144 }
145 
146 
147 
148 
IsEnabled(SfxItemState eState) const149 bool ControllerItem::IsEnabled (SfxItemState eState) const
150 {
151     if (eState == SFX_ITEM_DISABLED)
152         return false;
153     else if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED))
154     {
155         // There are no disabled commands.
156         return true;
157     }
158     else if (msCommandName.getLength() == 0)
159     {
160         // We were not given a command name at construction and can
161         // not check the state now.  Assume the best and return true.
162         return true;
163     }
164     else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED, msCommandName))
165     {
166         // The command is part of a list of disabled commands.
167         return false;
168     }
169     else
170         return true;
171 }
172 
173 
174 
175 
RequestUpdate(void)176 void ControllerItem::RequestUpdate (void)
177 {
178     SfxPoolItem* pState = NULL;
179     const SfxItemState eState (GetBindings().QueryState(GetId(), pState));
180     mrItemUpdateReceiver.NotifyItemUpdate(GetId(), eState, pState, IsEnabled(eState));
181 }
182 
183 
184 
185 
NotifyFrameContextChange(void)186 void ControllerItem::NotifyFrameContextChange (void)
187 {
188     RequestUpdate();
189 }
190 
191 
192 
193 
ResetFrame(void)194 void ControllerItem::ResetFrame (void)
195 {
196     mxFrame = NULL;
197 }
198 
199 
200 
201 
GetLabel(void) const202 ::rtl::OUString ControllerItem::GetLabel (void) const
203 {
204     return CommandInfoProvider::Instance().GetLabelForCommand(
205         A2S(".uno:")+msCommandName,
206         mxFrame);
207 }
208 
209 
210 
211 
GetHelpText(void) const212 ::rtl::OUString ControllerItem::GetHelpText (void) const
213 {
214     Help* pHelp = Application::GetHelp();
215     if (pHelp != NULL)
216     {
217         if (msCommandName.getLength() > 0)
218         {
219             const ::rtl::OUString sHelp (pHelp->GetHelpText(A2S(".uno:")+msCommandName, NULL));
220             return sHelp;
221         }
222     }
223     return ::rtl::OUString();
224 }
225 
226 
227 
228 
GetIcon(void) const229 Image ControllerItem::GetIcon (void) const
230 {
231     return GetIcon(Application::GetSettings().GetStyleSettings().GetHighContrastMode());
232 }
233 
234 
235 
236 
GetIcon(const bool bIsHighContrastMode) const237 Image ControllerItem::GetIcon (const bool bIsHighContrastMode) const
238 {
239     return GetImage(mxFrame, A2S(".uno:")+msCommandName, sal_False, bIsHighContrastMode);
240 
241 }
242 
243 
244 
245 
SetupToolBoxItem(ToolBox & rToolBox,const sal_uInt16 nIndex)246 void ControllerItem::SetupToolBoxItem (ToolBox& rToolBox, const sal_uInt16 nIndex)
247 {
248     rToolBox.SetQuickHelpText(nIndex, GetLabel());
249     rToolBox.SetHelpText(nIndex, GetHelpText());
250     rToolBox.SetItemImage(nIndex, GetIcon());
251 }
252 
253 
254 } } // end of namespace sfx2::sidebar
255