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_svx.hxx"
23 
24 #include "InsertPropertyPanel.hxx"
25 #include "InsertPropertyPanel.hrc"
26 #include "sfx2/sidebar/CommandInfoProvider.hxx"
27 
28 #include <sfx2/sidebar/Theme.hxx>
29 #include <sfx2/sidebar/Tools.hxx>
30 #include <sfx2/sidebar/ControlFactory.hxx>
31 #include <sfx2/sidebar/ControllerFactory.hxx>
32 
33 #include <svx/dialmgr.hxx>
34 #include <svtools/miscopt.hxx>
35 #include <svtools/generictoolboxcontroller.hxx>
36 #include <vcl/toolbox.hxx>
37 #include <sfx2/tbxctrl.hxx>
38 #include <framework/sfxhelperfunctions.hxx>
39 #include <framework/imageproducer.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <cppuhelper/compbase1.hxx>
42 #include <cppuhelper/basemutex.hxx>
43 
44 #include <com/sun/star/frame/XStatusListener.hpp>
45 
46 using namespace css;
47 using namespace cssu;
48 using ::rtl::OUString;
49 using ::sfx2::sidebar::SidebarToolBox;
50 
51 namespace svx { namespace sidebar {
52 
53 
InsertPropertyPanel(Window * pParent,const cssu::Reference<css::frame::XFrame> & rxFrame)54 InsertPropertyPanel::InsertPropertyPanel (
55     Window* pParent,
56     const cssu::Reference<css::frame::XFrame>& rxFrame)
57     :	Control(pParent, SVX_RES(RID_SIDEBAR_INSERT_PANEL)),
58         mpStandardShapesBackground(sfx2::sidebar::ControlFactory::CreateToolBoxBackground(this)),
59         mpStandardShapesToolBox(sfx2::sidebar::ControlFactory::CreateToolBox(
60                 mpStandardShapesBackground.get(),
61                 SVX_RES(TB_INSERT_STANDARD),
62                 rxFrame)),
63         mpCustomShapesBackground(sfx2::sidebar::ControlFactory::CreateToolBoxBackground(this)),
64         mpCustomShapesToolBox(sfx2::sidebar::ControlFactory::CreateToolBox(
65                 mpCustomShapesBackground.get(),
66                 SVX_RES(TB_INSERT_CUSTOM),
67                 rxFrame)),
68         mxFrame(rxFrame)
69 {
70     FreeResource();
71 
72     mpStandardShapesToolBox->Show();
73     mpCustomShapesToolBox->Show();
74 
75     // Listen to all tool box selection events.
76     Window* pTopWindow = pParent;
77     while (pTopWindow->GetParent() != NULL)
78         pTopWindow = pTopWindow->GetParent();
79     pTopWindow->AddChildEventListener(LINK(this, InsertPropertyPanel, WindowEventListener));
80 }
81 
82 
83 
84 
~InsertPropertyPanel(void)85 InsertPropertyPanel::~InsertPropertyPanel (void)
86 {
87     // Remove window child listener.
88     Window* pTopWindow = this;
89     while (pTopWindow->GetParent() != NULL)
90         pTopWindow = pTopWindow->GetParent();
91     pTopWindow->RemoveChildEventListener(LINK(this, InsertPropertyPanel, WindowEventListener));
92 
93     mpStandardShapesToolBox.reset();
94     mpCustomShapesToolBox.reset();
95     mpStandardShapesBackground.reset();
96     mpCustomShapesBackground.reset();
97 }
98 
99 
100 
101 
IMPL_LINK(InsertPropertyPanel,WindowEventListener,VclSimpleEvent *,pEvent)102 IMPL_LINK(InsertPropertyPanel, WindowEventListener, VclSimpleEvent*, pEvent)
103 {
104     // We will be getting a lot of window events (well, basically all
105     // of them), so reject early everything that is not connected to
106     // toolbox selection.
107     if (pEvent == NULL)
108         return 1;
109     if ( ! pEvent->ISA(VclWindowEvent))
110         return 1;
111     if (pEvent->GetId() != VCLEVENT_TOOLBOX_SELECT)
112         return 1;
113 
114     Window* pWindow = dynamic_cast<VclWindowEvent*>(pEvent)->GetWindow();
115     ToolBox* pToolBox = dynamic_cast<ToolBox*>(pWindow);
116     if (pToolBox == NULL)
117         return 1;
118 
119     // Extract name of (sub)toolbar from help id.
120     OUString sToolbarName (rtl::OStringToOUString(pToolBox->GetHelpId(), RTL_TEXTENCODING_UTF8));
121     if (sToolbarName.getLength() == 0)
122         return 1;
123     const util::URL aURL (sfx2::sidebar::Tools::GetURL(sToolbarName));
124     if (aURL.Path.getLength() == 0)
125         return 1;
126 
127     // Get item id.
128     sal_uInt16 nId = pToolBox->GetCurItemId();
129     if (nId == 0)
130         return 1;
131 
132     SidebarToolBox* pSidebarToolBox = dynamic_cast<SidebarToolBox*>(mpStandardShapesToolBox.get());
133     if (pSidebarToolBox == NULL)
134         return 1;
135     sal_uInt16 nItemId (pSidebarToolBox->GetItemIdForSubToolbarName(aURL.Path));
136     if (nItemId == 0)
137     {
138         pSidebarToolBox = dynamic_cast<SidebarToolBox*>(mpCustomShapesToolBox.get());
139         if (pSidebarToolBox == NULL)
140             return 1;
141         nItemId = pSidebarToolBox->GetItemIdForSubToolbarName(aURL.Path);
142         if (nItemId == 0)
143             return 1;
144     }
145 
146     Reference<frame::XSubToolbarController> xController (pSidebarToolBox->GetControllerForItemId(nItemId), UNO_QUERY);
147     if ( ! xController.is())
148         return 1;
149 
150     const OUString sCommand (pToolBox->GetItemCommand(nId));
151     xController->functionSelected(sCommand);
152 
153     return 1;
154 }
155 
156 
157 } } // end of namespace svx::sidebar
158