1ff12d537SAndre Fischer /**************************************************************
2ff12d537SAndre Fischer  *
3ff12d537SAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
4ff12d537SAndre Fischer  * or more contributor license agreements.  See the NOTICE file
5ff12d537SAndre Fischer  * distributed with this work for additional information
6ff12d537SAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
7ff12d537SAndre Fischer  * to you under the Apache License, Version 2.0 (the
8ff12d537SAndre Fischer  * "License"); you may not use this file except in compliance
9ff12d537SAndre Fischer  * with the License.  You may obtain a copy of the License at
10ff12d537SAndre Fischer  *
11ff12d537SAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
12ff12d537SAndre Fischer  *
13ff12d537SAndre Fischer  * Unless required by applicable law or agreed to in writing,
14ff12d537SAndre Fischer  * software distributed under the License is distributed on an
15ff12d537SAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ff12d537SAndre Fischer  * KIND, either express or implied.  See the License for the
17ff12d537SAndre Fischer  * specific language governing permissions and limitations
18ff12d537SAndre Fischer  * under the License.
19ff12d537SAndre Fischer  *
20ff12d537SAndre Fischer  *************************************************************/
21ff12d537SAndre Fischer 
22b9e67834SAndre Fischer #include "precompiled_sfx2.hxx"
23ff12d537SAndre Fischer 
24b9e67834SAndre Fischer #include "sidebar/ControllerItem.hxx"
25ff12d537SAndre Fischer 
2645da7d5eSAndre Fischer #include <sfx2/msgpool.hxx>
2745da7d5eSAndre Fischer #include <sfx2/viewsh.hxx>
2845da7d5eSAndre Fischer #include "sfx2/imagemgr.hxx"
2945da7d5eSAndre Fischer #include "sfx2/bindings.hxx"
3045da7d5eSAndre Fischer #include <unotools/cmdoptions.hxx>
31f35c6d02SAndre Fischer #include "sfx2/sidebar/CommandInfoProvider.hxx"
3245da7d5eSAndre Fischer #include <vcl/svapp.hxx>
3345da7d5eSAndre Fischer #include <vcl/toolbox.hxx>
34*9397b367SHerbert Dürr #include <vcl/help.hxx>
3545da7d5eSAndre Fischer 
3645da7d5eSAndre Fischer #include <com/sun/star/frame/XFrame.hpp>
3745da7d5eSAndre Fischer #include <com/sun/star/frame/XFrameActionListener.hpp>
3845da7d5eSAndre Fischer 
3945da7d5eSAndre Fischer 
4045da7d5eSAndre Fischer using namespace css;
4145da7d5eSAndre Fischer using namespace cssu;
4245da7d5eSAndre Fischer 
4345da7d5eSAndre Fischer 
4445da7d5eSAndre Fischer #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
4545da7d5eSAndre Fischer 
4645da7d5eSAndre Fischer namespace
4745da7d5eSAndre Fischer {
4845da7d5eSAndre Fischer     typedef ::cppu::WeakComponentImplHelper1 <
4945da7d5eSAndre Fischer         css::frame::XFrameActionListener
5045da7d5eSAndre Fischer         > FrameActionListenerInterfaceBase;
5145da7d5eSAndre Fischer 
5245da7d5eSAndre Fischer     class FrameActionListener
5345da7d5eSAndre Fischer         : public ::cppu::BaseMutex,
5445da7d5eSAndre Fischer           public FrameActionListenerInterfaceBase
5545da7d5eSAndre Fischer     {
5645da7d5eSAndre Fischer     public:
FrameActionListener(sfx2::sidebar::ControllerItem & rControllerItem,const Reference<frame::XFrame> & rxFrame)5745da7d5eSAndre Fischer         FrameActionListener (
5845da7d5eSAndre Fischer             sfx2::sidebar::ControllerItem& rControllerItem,
5945da7d5eSAndre Fischer             const Reference<frame::XFrame>& rxFrame)
6045da7d5eSAndre Fischer             : FrameActionListenerInterfaceBase(m_aMutex),
6145da7d5eSAndre Fischer               mrControllerItem(rControllerItem),
6245da7d5eSAndre Fischer               mxFrame(rxFrame)
6345da7d5eSAndre Fischer         {
6445da7d5eSAndre Fischer             if (mxFrame.is())
6545da7d5eSAndre Fischer                 mxFrame->addFrameActionListener(this);
6645da7d5eSAndre Fischer         }
~FrameActionListener(void)6745da7d5eSAndre Fischer         virtual ~FrameActionListener (void)
6845da7d5eSAndre Fischer         {
6945da7d5eSAndre Fischer         }
disposing(void)7045da7d5eSAndre Fischer         virtual void SAL_CALL disposing (void)
7145da7d5eSAndre Fischer         {
7245da7d5eSAndre Fischer             if (mxFrame.is())
7345da7d5eSAndre Fischer                 mxFrame->removeFrameActionListener(this);
7445da7d5eSAndre Fischer         }
disposing(const css::lang::EventObject & rEvent)7545da7d5eSAndre Fischer         virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
7645da7d5eSAndre Fischer             throw (cssu::RuntimeException)
7745da7d5eSAndre Fischer         {
7845da7d5eSAndre Fischer             (void)rEvent;
7945da7d5eSAndre Fischer             mrControllerItem.ResetFrame();
8045da7d5eSAndre Fischer             mxFrame = NULL;
8145da7d5eSAndre Fischer         }
frameAction(const css::frame::FrameActionEvent & rEvent)8245da7d5eSAndre Fischer         virtual void SAL_CALL frameAction (const css::frame::FrameActionEvent& rEvent)
8345da7d5eSAndre Fischer             throw (cssu::RuntimeException)
8445da7d5eSAndre Fischer         {
8545da7d5eSAndre Fischer             if (rEvent.Action == frame::FrameAction_CONTEXT_CHANGED)
8645da7d5eSAndre Fischer                 mrControllerItem.NotifyFrameContextChange();
8745da7d5eSAndre Fischer         }
8845da7d5eSAndre Fischer 
8945da7d5eSAndre Fischer     private:
9045da7d5eSAndre Fischer         sfx2::sidebar::ControllerItem& mrControllerItem;
9145da7d5eSAndre Fischer         Reference<frame::XFrame> mxFrame;
9245da7d5eSAndre Fischer     };
9345da7d5eSAndre Fischer }
9445da7d5eSAndre Fischer 
95ff12d537SAndre Fischer namespace sfx2 { namespace sidebar {
96ff12d537SAndre Fischer 
ControllerItem(const sal_uInt16 nSlotId,SfxBindings & rBindings,ItemUpdateReceiverInterface & rItemUpdateReceiver)97b9e67834SAndre Fischer ControllerItem::ControllerItem (
9845da7d5eSAndre Fischer     const sal_uInt16 nSlotId,
99b9e67834SAndre Fischer     SfxBindings &rBindings,
100b9e67834SAndre Fischer     ItemUpdateReceiverInterface& rItemUpdateReceiver)
10145da7d5eSAndre Fischer     : SfxControllerItem(nSlotId, rBindings),
10245da7d5eSAndre Fischer       mrItemUpdateReceiver(rItemUpdateReceiver),
10345da7d5eSAndre Fischer       mxFrame(),
10445da7d5eSAndre Fischer       mxFrameActionListener(),
10545da7d5eSAndre Fischer       msCommandName()
10645da7d5eSAndre Fischer {
10745da7d5eSAndre Fischer }
10845da7d5eSAndre Fischer 
10945da7d5eSAndre Fischer 
11045da7d5eSAndre Fischer 
11145da7d5eSAndre Fischer 
ControllerItem(const sal_uInt16 nSlotId,SfxBindings & rBindings,ItemUpdateReceiverInterface & rItemUpdateReceiver,const::rtl::OUString & rsCommandName,const Reference<frame::XFrame> & rxFrame)11245da7d5eSAndre Fischer ControllerItem::ControllerItem (
11345da7d5eSAndre Fischer     const sal_uInt16 nSlotId,
11445da7d5eSAndre Fischer     SfxBindings &rBindings,
11545da7d5eSAndre Fischer     ItemUpdateReceiverInterface& rItemUpdateReceiver,
11645da7d5eSAndre Fischer     const ::rtl::OUString& rsCommandName,
11745da7d5eSAndre Fischer     const Reference<frame::XFrame>& rxFrame)
11845da7d5eSAndre Fischer     : SfxControllerItem(nSlotId, rBindings),
11945da7d5eSAndre Fischer       mrItemUpdateReceiver(rItemUpdateReceiver),
12045da7d5eSAndre Fischer       mxFrame(rxFrame),
12145da7d5eSAndre Fischer       mxFrameActionListener(new FrameActionListener(*this, mxFrame)),
12245da7d5eSAndre Fischer       msCommandName(rsCommandName)
123ff12d537SAndre Fischer {
124b9e67834SAndre Fischer }
125ff12d537SAndre Fischer 
126ff12d537SAndre Fischer 
127ff12d537SAndre Fischer 
128b9e67834SAndre Fischer 
~ControllerItem(void)129b9e67834SAndre Fischer ControllerItem::~ControllerItem (void)
130b9e67834SAndre Fischer {
13145da7d5eSAndre Fischer     if (mxFrameActionListener.is())
13245da7d5eSAndre Fischer         mxFrameActionListener->dispose();
133b9e67834SAndre Fischer }
134b9e67834SAndre Fischer 
135b9e67834SAndre Fischer 
136b9e67834SAndre Fischer 
137b9e67834SAndre Fischer 
StateChanged(sal_uInt16 nSID,SfxItemState eState,const SfxPoolItem * pState)138b9e67834SAndre Fischer void ControllerItem::StateChanged (
139b9e67834SAndre Fischer     sal_uInt16 nSID,
140b9e67834SAndre Fischer     SfxItemState eState,
141b9e67834SAndre Fischer     const SfxPoolItem* pState)
142b9e67834SAndre Fischer {
143bc7f1831SAndre Fischer     mrItemUpdateReceiver.NotifyItemUpdate(nSID, eState, pState, IsEnabled(eState));
14445da7d5eSAndre Fischer }
14545da7d5eSAndre Fischer 
14645da7d5eSAndre Fischer 
14745da7d5eSAndre Fischer 
14845da7d5eSAndre Fischer 
IsEnabled(SfxItemState eState) const149bc7f1831SAndre Fischer bool ControllerItem::IsEnabled (SfxItemState eState) const
15045da7d5eSAndre Fischer {
151bc7f1831SAndre Fischer     if (eState == SFX_ITEM_DISABLED)
152bc7f1831SAndre Fischer         return false;
153bc7f1831SAndre Fischer     else if ( ! SvtCommandOptions().HasEntries(SvtCommandOptions::CMDOPTION_DISABLED))
15445da7d5eSAndre Fischer     {
15545da7d5eSAndre Fischer         // There are no disabled commands.
15645da7d5eSAndre Fischer         return true;
15745da7d5eSAndre Fischer     }
15845da7d5eSAndre Fischer     else if (msCommandName.getLength() == 0)
15945da7d5eSAndre Fischer     {
16045da7d5eSAndre Fischer         // We were not given a command name at construction and can
16145da7d5eSAndre Fischer         // not check the state now.  Assume the best and return true.
16245da7d5eSAndre Fischer         return true;
16345da7d5eSAndre Fischer     }
16445da7d5eSAndre Fischer     else if (SvtCommandOptions().Lookup(SvtCommandOptions::CMDOPTION_DISABLED, msCommandName))
16545da7d5eSAndre Fischer     {
16645da7d5eSAndre Fischer         // The command is part of a list of disabled commands.
16745da7d5eSAndre Fischer         return false;
16845da7d5eSAndre Fischer     }
16945da7d5eSAndre Fischer     else
17045da7d5eSAndre Fischer         return true;
17145da7d5eSAndre Fischer }
17245da7d5eSAndre Fischer 
17345da7d5eSAndre Fischer 
17445da7d5eSAndre Fischer 
17545da7d5eSAndre Fischer 
RequestUpdate(void)17645da7d5eSAndre Fischer void ControllerItem::RequestUpdate (void)
17745da7d5eSAndre Fischer {
17845da7d5eSAndre Fischer     SfxPoolItem* pState = NULL;
17945da7d5eSAndre Fischer     const SfxItemState eState (GetBindings().QueryState(GetId(), pState));
180bc7f1831SAndre Fischer     mrItemUpdateReceiver.NotifyItemUpdate(GetId(), eState, pState, IsEnabled(eState));
18145da7d5eSAndre Fischer }
18245da7d5eSAndre Fischer 
18345da7d5eSAndre Fischer 
18445da7d5eSAndre Fischer 
18545da7d5eSAndre Fischer 
NotifyFrameContextChange(void)18645da7d5eSAndre Fischer void ControllerItem::NotifyFrameContextChange (void)
18745da7d5eSAndre Fischer {
18845da7d5eSAndre Fischer     RequestUpdate();
18945da7d5eSAndre Fischer }
19045da7d5eSAndre Fischer 
19145da7d5eSAndre Fischer 
19245da7d5eSAndre Fischer 
19345da7d5eSAndre Fischer 
ResetFrame(void)19445da7d5eSAndre Fischer void ControllerItem::ResetFrame (void)
19545da7d5eSAndre Fischer {
19645da7d5eSAndre Fischer     mxFrame = NULL;
19745da7d5eSAndre Fischer }
19845da7d5eSAndre Fischer 
19945da7d5eSAndre Fischer 
20045da7d5eSAndre Fischer 
20145da7d5eSAndre Fischer 
GetLabel(void) const20245da7d5eSAndre Fischer ::rtl::OUString ControllerItem::GetLabel (void) const
20345da7d5eSAndre Fischer {
20445da7d5eSAndre Fischer     return CommandInfoProvider::Instance().GetLabelForCommand(
20545da7d5eSAndre Fischer         A2S(".uno:")+msCommandName,
20645da7d5eSAndre Fischer         mxFrame);
20745da7d5eSAndre Fischer }
20845da7d5eSAndre Fischer 
20945da7d5eSAndre Fischer 
21045da7d5eSAndre Fischer 
21145da7d5eSAndre Fischer 
GetHelpText(void) const212ed3119eeSAndre Fischer ::rtl::OUString ControllerItem::GetHelpText (void) const
213ed3119eeSAndre Fischer {
214ed3119eeSAndre Fischer     Help* pHelp = Application::GetHelp();
215ed3119eeSAndre Fischer     if (pHelp != NULL)
216ed3119eeSAndre Fischer     {
217ed3119eeSAndre Fischer         if (msCommandName.getLength() > 0)
218ed3119eeSAndre Fischer         {
219ed3119eeSAndre Fischer             const ::rtl::OUString sHelp (pHelp->GetHelpText(A2S(".uno:")+msCommandName, NULL));
220ed3119eeSAndre Fischer             return sHelp;
221ed3119eeSAndre Fischer         }
222ed3119eeSAndre Fischer     }
223ed3119eeSAndre Fischer     return ::rtl::OUString();
224ed3119eeSAndre Fischer }
225ed3119eeSAndre Fischer 
226ed3119eeSAndre Fischer 
227ed3119eeSAndre Fischer 
228ed3119eeSAndre Fischer 
GetIcon(void) const22945da7d5eSAndre Fischer Image ControllerItem::GetIcon (void) const
23045da7d5eSAndre Fischer {
23145da7d5eSAndre Fischer     return GetIcon(Application::GetSettings().GetStyleSettings().GetHighContrastMode());
23245da7d5eSAndre Fischer }
23345da7d5eSAndre Fischer 
23445da7d5eSAndre Fischer 
23545da7d5eSAndre Fischer 
23645da7d5eSAndre Fischer 
GetIcon(const bool bIsHighContrastMode) const23745da7d5eSAndre Fischer Image ControllerItem::GetIcon (const bool bIsHighContrastMode) const
23845da7d5eSAndre Fischer {
23945da7d5eSAndre Fischer     return GetImage(mxFrame, A2S(".uno:")+msCommandName, sal_False, bIsHighContrastMode);
24045da7d5eSAndre Fischer 
241b9e67834SAndre Fischer }
242b9e67834SAndre Fischer 
2437a32b0c8SAndre Fischer 
2447a32b0c8SAndre Fischer 
2457a32b0c8SAndre Fischer 
SetupToolBoxItem(ToolBox & rToolBox,const sal_uInt16 nIndex)24645da7d5eSAndre Fischer void ControllerItem::SetupToolBoxItem (ToolBox& rToolBox, const sal_uInt16 nIndex)
24745da7d5eSAndre Fischer {
24845da7d5eSAndre Fischer     rToolBox.SetQuickHelpText(nIndex, GetLabel());
249ed3119eeSAndre Fischer     rToolBox.SetHelpText(nIndex, GetHelpText());
25045da7d5eSAndre Fischer     rToolBox.SetItemImage(nIndex, GetIcon());
25145da7d5eSAndre Fischer }
25245da7d5eSAndre Fischer 
25345da7d5eSAndre Fischer 
254b9e67834SAndre Fischer } } // end of namespace sfx2::sidebar
255