xref: /trunk/main/sfx2/source/sidebar/ControllerItem.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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         {
77             (void)rEvent;
78             mrControllerItem.ResetFrame();
79             mxFrame = NULL;
80         }
frameAction(const css::frame::FrameActionEvent & rEvent)81         virtual void SAL_CALL frameAction (const css::frame::FrameActionEvent& rEvent)
82         {
83             if (rEvent.Action == frame::FrameAction_CONTEXT_CHANGED)
84                 mrControllerItem.NotifyFrameContextChange();
85         }
86 
87     private:
88         sfx2::sidebar::ControllerItem& mrControllerItem;
89         Reference<frame::XFrame> mxFrame;
90     };
91 }
92 
93 namespace sfx2 { namespace sidebar {
94 
ControllerItem(const sal_uInt16 nSlotId,SfxBindings & rBindings,ItemUpdateReceiverInterface & rItemUpdateReceiver)95 ControllerItem::ControllerItem (
96     const sal_uInt16 nSlotId,
97     SfxBindings &rBindings,
98     ItemUpdateReceiverInterface& rItemUpdateReceiver)
99     : SfxControllerItem(nSlotId, rBindings),
100       mrItemUpdateReceiver(rItemUpdateReceiver),
101       mxFrame(),
102       mxFrameActionListener(),
103       msCommandName()
104 {
105 }
106 
107 
108 
109 
ControllerItem(const sal_uInt16 nSlotId,SfxBindings & rBindings,ItemUpdateReceiverInterface & rItemUpdateReceiver,const::rtl::OUString & rsCommandName,const Reference<frame::XFrame> & rxFrame)110 ControllerItem::ControllerItem (
111     const sal_uInt16 nSlotId,
112     SfxBindings &rBindings,
113     ItemUpdateReceiverInterface& rItemUpdateReceiver,
114     const ::rtl::OUString& rsCommandName,
115     const Reference<frame::XFrame>& rxFrame)
116     : SfxControllerItem(nSlotId, rBindings),
117       mrItemUpdateReceiver(rItemUpdateReceiver),
118       mxFrame(rxFrame),
119       mxFrameActionListener(new FrameActionListener(*this, mxFrame)),
120       msCommandName(rsCommandName)
121 {
122 }
123 
124 
125 
126 
~ControllerItem(void)127 ControllerItem::~ControllerItem (void)
128 {
129     if (mxFrameActionListener.is())
130         mxFrameActionListener->dispose();
131 }
132 
133 
134 
135 
StateChanged(sal_uInt16 nSID,SfxItemState eState,const SfxPoolItem * pState)136 void ControllerItem::StateChanged (
137     sal_uInt16 nSID,
138     SfxItemState eState,
139     const SfxPoolItem* pState)
140 {
141     mrItemUpdateReceiver.NotifyItemUpdate(nSID, eState, pState, IsEnabled(eState));
142 }
143 
144 
145 
146 
IsEnabled(SfxItemState eState) const147 bool ControllerItem::IsEnabled (SfxItemState eState) const
148 {
149     if (eState == SFX_ITEM_DISABLED)
150         return false;
151     else if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED))
152     {
153         // There are no disabled commands.
154         return true;
155     }
156     else if (msCommandName.getLength() == 0)
157     {
158         // We were not given a command name at construction and can
159         // not check the state now.  Assume the best and return true.
160         return true;
161     }
162     else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED, msCommandName))
163     {
164         // The command is part of a list of disabled commands.
165         return false;
166     }
167     else
168         return true;
169 }
170 
171 
172 
173 
RequestUpdate(void)174 void ControllerItem::RequestUpdate (void)
175 {
176     SfxPoolItem* pState = NULL;
177     const SfxItemState eState (GetBindings().QueryState(GetId(), pState));
178     mrItemUpdateReceiver.NotifyItemUpdate(GetId(), eState, pState, IsEnabled(eState));
179 }
180 
181 
182 
183 
NotifyFrameContextChange(void)184 void ControllerItem::NotifyFrameContextChange (void)
185 {
186     RequestUpdate();
187 }
188 
189 
190 
191 
ResetFrame(void)192 void ControllerItem::ResetFrame (void)
193 {
194     mxFrame = NULL;
195 }
196 
197 
198 
199 
GetLabel(void) const200 ::rtl::OUString ControllerItem::GetLabel (void) const
201 {
202     return CommandInfoProvider::Instance().GetLabelForCommand(
203         A2S(".uno:")+msCommandName,
204         mxFrame);
205 }
206 
207 
208 
209 
GetHelpText(void) const210 ::rtl::OUString ControllerItem::GetHelpText (void) const
211 {
212     Help* pHelp = Application::GetHelp();
213     if (pHelp != NULL)
214     {
215         if (msCommandName.getLength() > 0)
216         {
217             const ::rtl::OUString sHelp (pHelp->GetHelpText(A2S(".uno:")+msCommandName, NULL));
218             return sHelp;
219         }
220     }
221     return ::rtl::OUString();
222 }
223 
224 
225 
226 
GetIcon(void) const227 Image ControllerItem::GetIcon (void) const
228 {
229     return GetIcon(Application::GetSettings().GetStyleSettings().GetHighContrastMode());
230 }
231 
232 
233 
234 
GetIcon(const bool bIsHighContrastMode) const235 Image ControllerItem::GetIcon (const bool bIsHighContrastMode) const
236 {
237     return GetImage(mxFrame, A2S(".uno:")+msCommandName, sal_False, bIsHighContrastMode);
238 
239 }
240 
241 
242 
243 
SetupToolBoxItem(ToolBox & rToolBox,const sal_uInt16 nIndex)244 void ControllerItem::SetupToolBoxItem (ToolBox& rToolBox, const sal_uInt16 nIndex)
245 {
246     rToolBox.SetQuickHelpText(nIndex, GetLabel());
247     rToolBox.SetHelpText(nIndex, GetHelpText());
248     rToolBox.SetItemImage(nIndex, GetIcon());
249 }
250 
251 
252 } } // end of namespace sfx2::sidebar
253