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 "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());
143 }
144 
145 
146 
147 
148 bool ControllerItem::IsEnabled (void) const
149 {
150     if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED))
151     {
152         // There are no disabled commands.
153         return true;
154     }
155     else if (msCommandName.getLength() == 0)
156     {
157         // We were not given a command name at construction and can
158         // not check the state now.  Assume the best and return true.
159         return true;
160     }
161     else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED, msCommandName))
162     {
163         // The command is part of a list of disabled commands.
164         return false;
165     }
166     else
167         return true;
168 }
169 
170 
171 
172 
173 void ControllerItem::RequestUpdate (void)
174 {
175     SfxPoolItem* pState = NULL;
176     const SfxItemState eState (GetBindings().QueryState(GetId(), pState));
177     mrItemUpdateReceiver.NotifyItemUpdate(GetId(), eState, pState, IsEnabled());
178 }
179 
180 
181 
182 
183 void ControllerItem::NotifyFrameContextChange (void)
184 {
185     RequestUpdate();
186 }
187 
188 
189 
190 
191 void ControllerItem::ResetFrame (void)
192 {
193     mxFrame = NULL;
194 }
195 
196 
197 
198 
199 ::rtl::OUString ControllerItem::GetLabel (void) const
200 {
201     return CommandInfoProvider::Instance().GetLabelForCommand(
202         A2S(".uno:")+msCommandName,
203         mxFrame);
204 }
205 
206 
207 
208 
209 Image ControllerItem::GetIcon (void) const
210 {
211     return GetIcon(Application::GetSettings().GetStyleSettings().GetHighContrastMode());
212 }
213 
214 
215 
216 
217 Image ControllerItem::GetIcon (const bool bIsHighContrastMode) const
218 {
219     return GetImage(mxFrame, A2S(".uno:")+msCommandName, sal_False, bIsHighContrastMode);
220 
221 }
222 
223 
224 
225 
226 void ControllerItem::SetupToolBoxItem (ToolBox& rToolBox, const sal_uInt16 nIndex)
227 {
228     rToolBox.SetQuickHelpText(nIndex, GetLabel());
229     rToolBox.SetItemImage(nIndex, GetIcon());
230 }
231 
232 
233 } } // end of namespace sfx2::sidebar
234