1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 #include <toolkit/helper/vclunohelper.hxx>
32 
33 #include <vcl/toolbox.hxx>
34 #include <vcl/svapp.hxx>
35 
36 #include "svtools/popupwindowcontroller.hxx"
37 #include "svtools/toolbarmenu.hxx"
38 
39 using rtl::OUString;
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 
44 
45 namespace svt
46 {
47 
48 class PopupWindowControllerImpl
49 {
50 public:
51 	PopupWindowControllerImpl();
52 	~PopupWindowControllerImpl();
53 
54 	void SetPopupWindow( ::Window* pPopupWindow, ToolBox* pToolBox );
55 
56     DECL_LINK( WindowEventListener, VclSimpleEvent* );
57 	DECL_STATIC_LINK( PopupWindowControllerImpl, AsyncDeleteWindowHdl, Window* );
58 
59 private:
60 	::Window* mpPopupWindow;
61 	ToolBox* mpToolBox;
62 };
63 
64 PopupWindowControllerImpl::PopupWindowControllerImpl()
65 : mpPopupWindow( 0 )
66 , mpToolBox( 0 )
67 {
68 }
69 
70 PopupWindowControllerImpl::~PopupWindowControllerImpl()
71 {
72 	if( mpPopupWindow )
73 		SetPopupWindow(0,0);
74 }
75 
76 void PopupWindowControllerImpl::SetPopupWindow( ::Window* pPopupWindow, ToolBox* pToolBox )
77 {
78 	if( mpPopupWindow )
79 	{
80 		mpPopupWindow->RemoveEventListener( LINK( this, PopupWindowControllerImpl, WindowEventListener ) );
81 		Application::PostUserEvent( STATIC_LINK( this, PopupWindowControllerImpl, AsyncDeleteWindowHdl ), mpPopupWindow );
82 	}
83 	mpPopupWindow = pPopupWindow;
84 	mpToolBox = pToolBox;
85 
86 	if( mpPopupWindow )
87 	{
88 		mpPopupWindow->AddEventListener( LINK( this, PopupWindowControllerImpl, WindowEventListener ));
89 	}
90 }
91 
92 IMPL_LINK( PopupWindowControllerImpl, WindowEventListener, VclSimpleEvent*, pEvent )
93 {
94 	VclWindowEvent* pWindowEvent = dynamic_cast< VclWindowEvent* >( pEvent );
95     if( pWindowEvent )
96 	{
97 		switch( pWindowEvent->GetId() )
98 		{
99 		case VCLEVENT_WINDOW_CLOSE:
100 		case VCLEVENT_WINDOW_ENDPOPUPMODE:
101 			SetPopupWindow(0,0);
102 			break;
103 
104 		case VCLEVENT_WINDOW_SHOW:
105 		{
106 			if( mpPopupWindow )
107 			{
108 				if( mpToolBox )
109 					mpToolBox->CallEventListeners( VCLEVENT_DROPDOWN_OPEN, (void*)mpPopupWindow );
110 				mpPopupWindow->CallEventListeners( VCLEVENT_WINDOW_GETFOCUS, 0 );
111 
112 				svtools::ToolbarMenu* pToolbarMenu = dynamic_cast< svtools::ToolbarMenu* >( mpPopupWindow );
113 				if( pToolbarMenu )
114 					pToolbarMenu->highlightFirstEntry();
115 				break;
116 			}
117 			break;
118 		}
119 		case VCLEVENT_WINDOW_HIDE:
120 		{
121 			if( mpPopupWindow )
122 			{
123 				mpPopupWindow->CallEventListeners( VCLEVENT_WINDOW_LOSEFOCUS, 0 );
124 				if( mpToolBox )
125 					mpToolBox->CallEventListeners( VCLEVENT_DROPDOWN_CLOSE, (void*)mpPopupWindow );
126 			}
127 			break;
128 		}
129 		}
130 	}
131     return 1;
132 }
133 
134 //--------------------------------------------------------------------
135 
136 IMPL_STATIC_LINK( PopupWindowControllerImpl, AsyncDeleteWindowHdl, Window*, pWindow )
137 {
138 	(void)*pThis;
139 	delete pWindow;
140 	return 0;
141 }
142 
143 //========================================================================
144 // class PopupWindowController
145 //========================================================================
146 
147 PopupWindowController::PopupWindowController( const Reference< lang::XMultiServiceFactory >& rServiceManager,
148 											  const Reference< frame::XFrame >& xFrame,
149 											  const OUString& aCommandURL )
150 : svt::ToolboxController( rServiceManager, xFrame, aCommandURL )
151 , mpImpl( new PopupWindowControllerImpl() )
152 {
153 }
154 
155 PopupWindowController::~PopupWindowController()
156 {
157 }
158 
159 // XInterface
160 Any SAL_CALL PopupWindowController::queryInterface( const Type& aType )
161 throw (RuntimeException)
162 {
163     Any a( ToolboxController::queryInterface( aType ) );
164     if ( a.hasValue() )
165         return a;
166 
167     return ::cppu::queryInterface( aType, static_cast< lang::XServiceInfo* >( this ));
168 }
169 
170 void SAL_CALL PopupWindowController::acquire() throw ()
171 {
172     ToolboxController::acquire();
173 }
174 
175 void SAL_CALL PopupWindowController::release() throw ()
176 {
177     ToolboxController::release();
178 }
179 
180 // XServiceInfo
181 sal_Bool SAL_CALL PopupWindowController::supportsService( const OUString& ServiceName ) throw(RuntimeException)
182 {
183     const Sequence< OUString > aSNL( getSupportedServiceNames() );
184     const OUString * pArray = aSNL.getConstArray();
185 
186     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
187         if( pArray[i] == ServiceName )
188             return true;
189 
190     return false;
191 }
192 
193 // XInitialization
194 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)
195 {
196 	svt::ToolboxController::initialize( aArguments );
197 	if( m_aCommandURL.getLength() )
198 		addStatusListener( m_aCommandURL );
199 }
200 
201 // XComponent
202 void SAL_CALL PopupWindowController::dispose() throw (RuntimeException)
203 {
204 	if( m_aCommandURL.getLength() )
205 		removeStatusListener( m_aCommandURL );
206 
207     svt::ToolboxController::dispose();
208 }
209 
210 
211 // XStatusListener
212 void SAL_CALL PopupWindowController::statusChanged( const frame::FeatureStateEvent& rEvent ) throw ( RuntimeException )
213 {
214 	svt::ToolboxController::statusChanged(rEvent);
215 	enable( rEvent.IsEnabled );
216 }
217 
218 // XToolbarController
219 void SAL_CALL PopupWindowController::execute( sal_Int16 KeyModifier ) throw (RuntimeException)
220 {
221 	svt::ToolboxController::execute( KeyModifier );
222 }
223 
224 void SAL_CALL PopupWindowController::click() throw (RuntimeException)
225 {
226 	svt::ToolboxController::click();
227 }
228 
229 void SAL_CALL PopupWindowController::doubleClick() throw (RuntimeException)
230 {
231 	svt::ToolboxController::doubleClick();
232 }
233 
234 Reference< awt::XWindow > SAL_CALL PopupWindowController::createPopupWindow() throw (RuntimeException)
235 {
236 	ToolBox* pToolBox = dynamic_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
237 	if( pToolBox )
238 	{
239 		::Window* pItemWindow = pToolBox->GetItemWindow( pToolBox->GetDownItemId() );
240 		::Window* pWin = createPopupWindow( pItemWindow ? pItemWindow : pToolBox );
241 	    if( pWin )
242 		{
243    			pWin->EnableDocking(true);
244 			mpImpl->SetPopupWindow(pWin,pToolBox);
245 			::Window::GetDockingManager()->StartPopupMode( pToolBox, pWin, FLOATWIN_POPUPMODE_NOFOCUSCLOSE|FLOATWIN_POPUPMODE_ALLMOUSEBUTTONCLOSE |FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE );
246 		}
247 	}
248     return Reference< awt::XWindow >();
249 }
250 
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