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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svtools.hxx"
26 
27 #include <toolkit/helper/vclunohelper.hxx>
28 
29 #include <vcl/toolbox.hxx>
30 #include <vcl/svapp.hxx>
31 
32 #include "svtools/popupwindowcontroller.hxx"
33 #include "svtools/toolbarmenu.hxx"
34 
35 using rtl::OUString;
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 
40 
41 namespace svt
42 {
43 
44 class PopupWindowControllerImpl
45 {
46 public:
47 	PopupWindowControllerImpl();
48 	~PopupWindowControllerImpl();
49 
50 	void SetPopupWindow( ::Window* pPopupWindow, ToolBox* pToolBox );
51 
52     DECL_LINK( WindowEventListener, VclSimpleEvent* );
53 	DECL_STATIC_LINK( PopupWindowControllerImpl, AsyncDeleteWindowHdl, Window* );
54 
55 private:
56 	::Window* mpPopupWindow;
57 	ToolBox* mpToolBox;
58 };
59 
PopupWindowControllerImpl()60 PopupWindowControllerImpl::PopupWindowControllerImpl()
61 : mpPopupWindow( 0 )
62 , mpToolBox( 0 )
63 {
64 }
65 
~PopupWindowControllerImpl()66 PopupWindowControllerImpl::~PopupWindowControllerImpl()
67 {
68 	if( mpPopupWindow )
69 		SetPopupWindow(0,0);
70 }
71 
SetPopupWindow(::Window * pPopupWindow,ToolBox * pToolBox)72 void PopupWindowControllerImpl::SetPopupWindow( ::Window* pPopupWindow, ToolBox* pToolBox )
73 {
74 	if( mpPopupWindow )
75 	{
76 		mpPopupWindow->RemoveEventListener( LINK( this, PopupWindowControllerImpl, WindowEventListener ) );
77 		Application::PostUserEvent( STATIC_LINK( this, PopupWindowControllerImpl, AsyncDeleteWindowHdl ), mpPopupWindow );
78 	}
79 	mpPopupWindow = pPopupWindow;
80 	mpToolBox = pToolBox;
81 
82 	if( mpPopupWindow )
83 	{
84 		mpPopupWindow->AddEventListener( LINK( this, PopupWindowControllerImpl, WindowEventListener ));
85 	}
86 }
87 
IMPL_LINK(PopupWindowControllerImpl,WindowEventListener,VclSimpleEvent *,pEvent)88 IMPL_LINK( PopupWindowControllerImpl, WindowEventListener, VclSimpleEvent*, pEvent )
89 {
90 	VclWindowEvent* pWindowEvent = dynamic_cast< VclWindowEvent* >( pEvent );
91     if( pWindowEvent )
92 	{
93 		switch( pWindowEvent->GetId() )
94 		{
95 		case VCLEVENT_WINDOW_CLOSE:
96 		case VCLEVENT_WINDOW_ENDPOPUPMODE:
97 			SetPopupWindow(0,0);
98 			break;
99 
100 		case VCLEVENT_WINDOW_SHOW:
101 		{
102 			if( mpPopupWindow )
103 			{
104 				if( mpToolBox )
105 					mpToolBox->CallEventListeners( VCLEVENT_DROPDOWN_OPEN, (void*)mpPopupWindow );
106 				mpPopupWindow->CallEventListeners( VCLEVENT_WINDOW_GETFOCUS, 0 );
107 
108 				svtools::ToolbarMenu* pToolbarMenu = dynamic_cast< svtools::ToolbarMenu* >( mpPopupWindow );
109 				if( pToolbarMenu )
110 					pToolbarMenu->highlightFirstEntry();
111 				break;
112 			}
113 			break;
114 		}
115 		case VCLEVENT_WINDOW_HIDE:
116 		{
117 			if( mpPopupWindow )
118 			{
119 				mpPopupWindow->CallEventListeners( VCLEVENT_WINDOW_LOSEFOCUS, 0 );
120 				if( mpToolBox )
121 					mpToolBox->CallEventListeners( VCLEVENT_DROPDOWN_CLOSE, (void*)mpPopupWindow );
122 			}
123 			break;
124 		}
125 		}
126 	}
127     return 1;
128 }
129 
130 //--------------------------------------------------------------------
131 
IMPL_STATIC_LINK(PopupWindowControllerImpl,AsyncDeleteWindowHdl,Window *,pWindow)132 IMPL_STATIC_LINK( PopupWindowControllerImpl, AsyncDeleteWindowHdl, Window*, pWindow )
133 {
134 	(void)*pThis;
135 	delete pWindow;
136 	return 0;
137 }
138 
139 //========================================================================
140 // class PopupWindowController
141 //========================================================================
142 
PopupWindowController(const Reference<lang::XMultiServiceFactory> & rServiceManager,const Reference<frame::XFrame> & xFrame,const OUString & aCommandURL)143 PopupWindowController::PopupWindowController( const Reference< lang::XMultiServiceFactory >& rServiceManager,
144 											  const Reference< frame::XFrame >& xFrame,
145 											  const OUString& aCommandURL )
146 : svt::ToolboxController( rServiceManager, xFrame, aCommandURL )
147 , mpImpl( new PopupWindowControllerImpl() )
148 {
149 }
150 
~PopupWindowController()151 PopupWindowController::~PopupWindowController()
152 {
153 }
154 
155 // XInterface
queryInterface(const Type & aType)156 Any SAL_CALL PopupWindowController::queryInterface( const Type& aType )
157 throw (RuntimeException)
158 {
159     Any a( ToolboxController::queryInterface( aType ) );
160     if ( a.hasValue() )
161         return a;
162 
163     return ::cppu::queryInterface( aType, static_cast< lang::XServiceInfo* >( this ));
164 }
165 
acquire()166 void SAL_CALL PopupWindowController::acquire() throw ()
167 {
168     ToolboxController::acquire();
169 }
170 
release()171 void SAL_CALL PopupWindowController::release() throw ()
172 {
173     ToolboxController::release();
174 }
175 
176 // XServiceInfo
supportsService(const OUString & ServiceName)177 sal_Bool SAL_CALL PopupWindowController::supportsService( const OUString& ServiceName ) throw(RuntimeException)
178 {
179     const Sequence< OUString > aSNL( getSupportedServiceNames() );
180     const OUString * pArray = aSNL.getConstArray();
181 
182     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
183         if( pArray[i] == ServiceName )
184             return true;
185 
186     return false;
187 }
188 
189 // XInitialization
initialize(const::com::sun::star::uno::Sequence<::com::sun::star::uno::Any> & aArguments)190 void SAL_CALL PopupWindowController::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
191 {
192 	svt::ToolboxController::initialize( aArguments );
193 	if( m_aCommandURL.getLength() )
194 		addStatusListener( m_aCommandURL );
195 }
196 
197 // XComponent
dispose()198 void SAL_CALL PopupWindowController::dispose() throw (RuntimeException)
199 {
200 	if( m_aCommandURL.getLength() )
201 		removeStatusListener( m_aCommandURL );
202 
203     svt::ToolboxController::dispose();
204 }
205 
206 
207 // XStatusListener
statusChanged(const frame::FeatureStateEvent & rEvent)208 void SAL_CALL PopupWindowController::statusChanged( const frame::FeatureStateEvent& rEvent ) throw ( RuntimeException )
209 {
210 	svt::ToolboxController::statusChanged(rEvent);
211 	enable( rEvent.IsEnabled );
212 }
213 
214 // XToolbarController
execute(sal_Int16 KeyModifier)215 void SAL_CALL PopupWindowController::execute( sal_Int16 KeyModifier ) throw (RuntimeException)
216 {
217 	svt::ToolboxController::execute( KeyModifier );
218 }
219 
click()220 void SAL_CALL PopupWindowController::click() throw (RuntimeException)
221 {
222 	svt::ToolboxController::click();
223 }
224 
doubleClick()225 void SAL_CALL PopupWindowController::doubleClick() throw (RuntimeException)
226 {
227 	svt::ToolboxController::doubleClick();
228 }
229 
createPopupWindow()230 Reference< awt::XWindow > SAL_CALL PopupWindowController::createPopupWindow() throw (RuntimeException)
231 {
232 	ToolBox* pToolBox = dynamic_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
233 	if( pToolBox )
234 	{
235 		::Window* pItemWindow = pToolBox->GetItemWindow( pToolBox->GetDownItemId() );
236 		::Window* pWin = createPopupWindow( pItemWindow ? pItemWindow : pToolBox );
237 	    if( pWin )
238 		{
239    			pWin->EnableDocking(true);
240 			mpImpl->SetPopupWindow(pWin,pToolBox);
241             ::Window::GetDockingManager()->StartPopupMode( pToolBox, pWin,
242                                                            FLOATWIN_POPUPMODE_GRABFOCUS |
243                                                            FLOATWIN_POPUPMODE_NOFOCUSCLOSE |
244                                                            FLOATWIN_POPUPMODE_ALLMOUSEBUTTONCLOSE |
245                                                            FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE );
246 		}
247 	}
248     return Reference< awt::XWindow >();
249 }
250 
createItemWindow(const Reference<awt::XWindow> &)251 Reference< awt::XWindow > SAL_CALL PopupWindowController::createItemWindow( const Reference< awt::XWindow >& /*Parent*/ )
252     throw (RuntimeException)
253 {
254     return Reference< awt::XWindow >();
255 }
256 
257 }
258 
259