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 
35 #include <com/sun/star/frame/XFrame.hpp>
36 #include <com/sun/star/frame/XFrameActionListener.hpp>
37 
38 
39 using namespace css;
40 using namespace cssu;
41 
42 
43 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
44 
45 namespace
46 {
47     typedef ::cppu::WeakComponentImplHelper1 <
48         css::frame::XFrameActionListener
49         > FrameActionListenerInterfaceBase;
50 
51     class FrameActionListener
52         : public ::cppu::BaseMutex,
53           public FrameActionListenerInterfaceBase
54     {
55     public:
56         FrameActionListener (
57             sfx2::sidebar::ControllerItem& rControllerItem,
58             const Reference<frame::XFrame>& rxFrame)
59             : FrameActionListenerInterfaceBase(m_aMutex),
60               mrControllerItem(rControllerItem),
61               mxFrame(rxFrame)
62         {
63             if (mxFrame.is())
64                 mxFrame->addFrameActionListener(this);
65         }
66         virtual ~FrameActionListener (void)
67         {
68         }
69         virtual void SAL_CALL disposing (void)
70         {
71             if (mxFrame.is())
72                 mxFrame->removeFrameActionListener(this);
73         }
74         virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
75             throw (cssu::RuntimeException)
76         {
77             (void)rEvent;
78             mrControllerItem.ResetFrame();
79             mxFrame = NULL;
80         }
81         virtual void SAL_CALL frameAction (const css::frame::FrameActionEvent& rEvent)
82             throw (cssu::RuntimeException)
83         {
84             if (rEvent.Action == frame::FrameAction_CONTEXT_CHANGED)
85                 mrControllerItem.NotifyFrameContextChange();
86         }
87 
88     private:
89         sfx2::sidebar::ControllerItem& mrControllerItem;
90         Reference<frame::XFrame> mxFrame;
91     };
92 }
93 
94 namespace sfx2 { namespace sidebar {
95 
96 ControllerItem::ControllerItem (
97     const sal_uInt16 nSlotId,
98     SfxBindings &rBindings,
99     ItemUpdateReceiverInterface& rItemUpdateReceiver)
100     : SfxControllerItem(nSlotId, rBindings),
101       mrItemUpdateReceiver(rItemUpdateReceiver),
102       mxFrame(),
103       mxFrameActionListener(),
104       msCommandName()
105 {
106 }
107 
108 
109 
110 
111 ControllerItem::ControllerItem (
112     const sal_uInt16 nSlotId,
113     SfxBindings &rBindings,
114     ItemUpdateReceiverInterface& rItemUpdateReceiver,
115     const ::rtl::OUString& rsCommandName,
116     const Reference<frame::XFrame>& rxFrame)
117     : SfxControllerItem(nSlotId, rBindings),
118       mrItemUpdateReceiver(rItemUpdateReceiver),
119       mxFrame(rxFrame),
120       mxFrameActionListener(new FrameActionListener(*this, mxFrame)),
121       msCommandName(rsCommandName)
122 {
123 }
124 
125 
126 
127 
128 ControllerItem::~ControllerItem (void)
129 {
130     if (mxFrameActionListener.is())
131         mxFrameActionListener->dispose();
132 }
133 
134 
135 
136 
137 void ControllerItem::StateChanged (
138     sal_uInt16 nSID,
139     SfxItemState eState,
140     const SfxPoolItem* pState)
141 {
142     mrItemUpdateReceiver.NotifyItemUpdate(nSID, eState, pState, IsEnabled(eState));
143 }
144 
145 
146 
147 
148 bool ControllerItem::IsEnabled (SfxItemState eState) const
149 {
150     if (eState == SFX_ITEM_DISABLED)
151         return false;
152     else if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED))
153     {
154         // There are no disabled commands.
155         return true;
156     }
157     else if (msCommandName.getLength() == 0)
158     {
159         // We were not given a command name at construction and can
160         // not check the state now.  Assume the best and return true.
161         return true;
162     }
163     else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED, msCommandName))
164     {
165         // The command is part of a list of disabled commands.
166         return false;
167     }
168     else
169         return true;
170 }
171 
172 
173 
174 
175 void ControllerItem::RequestUpdate (void)
176 {
177     SfxPoolItem* pState = NULL;
178     const SfxItemState eState (GetBindings().QueryState(GetId(), pState));
179     mrItemUpdateReceiver.NotifyItemUpdate(GetId(), eState, pState, IsEnabled(eState));
180 }
181 
182 
183 
184 
185 void ControllerItem::NotifyFrameContextChange (void)
186 {
187     RequestUpdate();
188 }
189 
190 
191 
192 
193 void ControllerItem::ResetFrame (void)
194 {
195     mxFrame = NULL;
196 }
197 
198 
199 
200 
201 ::rtl::OUString ControllerItem::GetLabel (void) const
202 {
203     return CommandInfoProvider::Instance().GetLabelForCommand(
204         A2S(".uno:")+msCommandName,
205         mxFrame);
206 }
207 
208 
209 
210 
211 Image ControllerItem::GetIcon (void) const
212 {
213     return GetIcon(Application::GetSettings().GetStyleSettings().GetHighContrastMode());
214 }
215 
216 
217 
218 
219 Image ControllerItem::GetIcon (const bool bIsHighContrastMode) const
220 {
221     return GetImage(mxFrame, A2S(".uno:")+msCommandName, sal_False, bIsHighContrastMode);
222 
223 }
224 
225 
226 
227 
228 void ControllerItem::SetupToolBoxItem (ToolBox& rToolBox, const sal_uInt16 nIndex)
229 {
230     rToolBox.SetQuickHelpText(nIndex, GetLabel());
231     rToolBox.SetItemImage(nIndex, GetIcon());
232 }
233 
234 
235 } } // end of namespace sfx2::sidebar
236