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_dbaccess.hxx"
30 
31 #ifndef _SBA_BWRCTRLR_HXX
32 #include "brwctrlr.hxx"
33 #endif
34 #ifndef _SBX_BRWVIEW_HXX
35 #include "brwview.hxx"
36 #endif
37 #ifndef _SBA_GRID_HXX
38 #include "sbagrid.hxx"
39 #endif
40 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
41 #include <toolkit/helper/vclunohelper.hxx>
42 #endif
43 #ifndef _COMPHELPER_TYPES_HXX_
44 #include <comphelper/types.hxx>
45 #endif
46 #ifndef _SV_SPLIT_HXX
47 #include <vcl/split.hxx>
48 #endif
49 #ifndef DBACCESS_UI_DBTREEVIEW_HXX
50 #include "dbtreeview.hxx"
51 #endif
52 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
53 #include "dbustrings.hrc"
54 #endif
55 #ifndef _DBU_BRW_HRC_
56 #include "dbu_brw.hrc"
57 #endif
58 #ifndef _COM_SUN_STAR_FORM_XLOADABLE_HPP_
59 #include <com/sun/star/form/XLoadable.hpp>
60 #endif
61 #ifndef _COM_SUN_STAR_AWT_XCONTROLCONTAINER_HPP_
62 #include <com/sun/star/awt/XControlContainer.hpp>
63 #endif
64 #ifndef DBAUI_TOOLS_HXX
65 #include "UITools.hxx"
66 #endif
67 
68 
69 using namespace dbaui;
70 using namespace ::com::sun::star::uno;
71 using namespace ::com::sun::star::sdb;
72 using namespace ::com::sun::star::form;
73 //	using namespace ::com::sun::star::sdbcx;
74 using namespace ::com::sun::star::beans;
75 using namespace ::com::sun::star::container;
76 using namespace ::com::sun::star::lang;
77 
78 
79 namespace
80 {
81 	sal_Bool isGrabVclControlFocusAllowed(const UnoDataBrowserView* _pView)
82 	{
83 		sal_Bool bGrabFocus = sal_False;
84 		SbaGridControl* pVclControl = _pView->getVclControl();
85 		Reference< ::com::sun::star::awt::XControl > xGrid = _pView->getGridControl();
86 		if (pVclControl && xGrid.is())
87 		{
88 			bGrabFocus = sal_True;
89 			if(!pVclControl->HasChildPathFocus())
90 			{
91 				Reference<XChild> xChild(xGrid->getModel(),UNO_QUERY);
92 				Reference<XLoadable> xLoad;
93 				if(xChild.is())
94 					xLoad.set(xChild->getParent(),UNO_QUERY);
95 				bGrabFocus = xLoad.is() && xLoad->isLoaded();
96 			}
97 		}
98 		return bGrabFocus;
99 	}
100 }
101 //==================================================================
102 //= UnoDataBrowserView
103 //==================================================================
104 
105 DBG_NAME(UnoDataBrowserView)
106 // -------------------------------------------------------------------------
107 UnoDataBrowserView::UnoDataBrowserView(	Window* pParent,
108 										IController& _rController,
109 										const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rFactory)
110 	:ODataView(pParent,_rController,_rFactory)
111 	,m_pTreeView(NULL)
112 	,m_pSplitter(NULL)
113 	,m_pVclControl(NULL)
114 	,m_pStatus(NULL)
115 {
116     DBG_CTOR(UnoDataBrowserView,NULL);
117 
118 }
119 // -------------------------------------------------------------------------
120 void UnoDataBrowserView::Construct(const Reference< ::com::sun::star::awt::XControlModel >& xModel)
121 {
122 	try
123 	{
124 		ODataView::Construct();
125 
126 		// our UNO representation
127 		m_xMe = VCLUnoHelper::CreateControlContainer(this);
128 
129 		// create the (UNO-) control
130 		m_xGrid = new SbaXGridControl(getORB());
131 		DBG_ASSERT(m_xGrid.is(), "UnoDataBrowserView::Construct : could not create a grid control !");
132 		// in design mode (for the moment)
133 		m_xGrid->setDesignMode(sal_True);
134 
135 		Reference< ::com::sun::star::awt::XWindow >  xGridWindow(m_xGrid, UNO_QUERY);
136 		xGridWindow->setVisible(sal_True);
137 		xGridWindow->setEnable(sal_True);
138 
139 		// introduce the model to the grid
140 		m_xGrid->setModel(xModel);
141 		// introduce the container (me) to the grid
142 		Reference< ::com::sun::star::beans::XPropertySet >  xModelSet(xModel, UNO_QUERY);
143 		getContainer()->addControl(::comphelper::getString(xModelSet->getPropertyValue(PROPERTY_NAME)), m_xGrid);
144 
145 		// get the VCL-control
146 		m_pVclControl = NULL;
147 		getVclControl();
148 
149 		DBG_ASSERT(m_pVclControl != NULL, "UnoDataBrowserView::Construct : no real grid control !");
150 	}
151 	catch(Exception&)
152 	{
153 		::comphelper::disposeComponent(m_xGrid);
154 		throw;
155 	}
156 }
157 // -------------------------------------------------------------------------
158 UnoDataBrowserView::~UnoDataBrowserView()
159 {
160 	{
161 		::std::auto_ptr<Splitter> aTemp(m_pSplitter);
162 		m_pSplitter = NULL;
163 	}
164 	setTreeView(NULL);
165 
166 	if ( m_pStatus )
167 	{
168 		delete m_pStatus;
169 		m_pStatus = NULL;
170 	}
171 
172 	try
173 	{
174 		::comphelper::disposeComponent(m_xGrid);
175 		::comphelper::disposeComponent(m_xMe);
176 	}
177 	catch(Exception)
178 	{}
179 
180     DBG_DTOR(UnoDataBrowserView,NULL);
181 }
182 // -----------------------------------------------------------------------------
183 IMPL_LINK( UnoDataBrowserView, SplitHdl, void*, /*NOINTERESTEDIN*/ )
184 {
185 	long nYPos = m_pSplitter->GetPosPixel().Y();
186 	m_pSplitter->SetPosPixel( Point( m_pSplitter->GetSplitPosPixel(), nYPos ) );
187 	Resize();
188 
189 	return 0L;
190 }
191 // -------------------------------------------------------------------------
192 void UnoDataBrowserView::setSplitter(Splitter* _pSplitter)
193 {
194 	m_pSplitter = _pSplitter;
195 	m_pSplitter->SetSplitHdl( LINK( this, UnoDataBrowserView, SplitHdl ) );
196 	LINK( this, UnoDataBrowserView, SplitHdl ).Call(m_pSplitter);
197 }
198 // -------------------------------------------------------------------------
199 void UnoDataBrowserView::setTreeView(DBTreeView* _pTreeView)
200 {
201 	if (m_pTreeView != _pTreeView)
202 	{
203 		if (m_pTreeView)
204 		{
205 			::std::auto_ptr<Window> aTemp(m_pTreeView);
206 			m_pTreeView = NULL;
207 		}
208 		m_pTreeView = _pTreeView;
209 	}
210 }
211 // -------------------------------------------------------------------------
212 void UnoDataBrowserView::showStatus( const String& _rStatus )
213 {
214 	if (0 == _rStatus.Len())
215 		hideStatus();
216 	else
217 	{
218 		if (!m_pStatus)
219 			m_pStatus = new FixedText(this);
220 		m_pStatus->SetText(_rStatus);
221 		m_pStatus->Show();
222 		Resize();
223 		Update();
224 	}
225 }
226 
227 // -------------------------------------------------------------------------
228 void UnoDataBrowserView::hideStatus()
229 {
230 	if (!m_pStatus || !m_pStatus->IsVisible())
231 		// nothing to do
232 		return;
233 	m_pStatus->Hide();
234 	Resize();
235 	Update();
236 }
237 
238 // -------------------------------------------------------------------------
239 void UnoDataBrowserView::resizeDocumentView(Rectangle& _rPlayground)
240 {
241 	Point	aSplitPos;
242 	Size	aSplitSize;
243 	Point	aPlaygroundPos( _rPlayground.TopLeft() );
244 	Size	aPlaygroundSize( _rPlayground.GetSize() );
245 
246 	if (m_pTreeView && m_pTreeView->IsVisible() && m_pSplitter)
247 	{
248 		// calculate the splitter pos and size
249 		aSplitPos	= m_pSplitter->GetPosPixel();
250 		aSplitPos.Y() = aPlaygroundPos.Y();
251 		aSplitSize	= m_pSplitter->GetOutputSizePixel();
252 		aSplitSize.Height() = aPlaygroundSize.Height();
253 
254 		if( ( aSplitPos.X() + aSplitSize.Width() ) > ( aPlaygroundSize.Width() ))
255 			aSplitPos.X() = aPlaygroundSize.Width() - aSplitSize.Width();
256 
257 		if( aSplitPos.X() <= aPlaygroundPos.X() )
258 			aSplitPos.X() = aPlaygroundPos.X() + sal_Int32(aPlaygroundSize.Width() * 0.2);
259 
260 		// the tree pos and size
261 		Point	aTreeViewPos( aPlaygroundPos );
262 		Size	aTreeViewSize( aSplitPos.X(), aPlaygroundSize.Height() );
263 
264 		// the status pos and size
265 		if (m_pStatus && m_pStatus->IsVisible())
266 		{
267 			Size aStatusSize(aPlaygroundPos.X(), GetTextHeight() + 2);
268 			aStatusSize = LogicToPixel(aStatusSize, MAP_APPFONT);
269 			aStatusSize.Width() = aTreeViewSize.Width() - 2 - 2;
270 
271 			Point aStatusPos( aPlaygroundPos.X() + 2, aTreeViewPos.Y() + aTreeViewSize.Height() - aStatusSize.Height() );
272 			m_pStatus->SetPosSizePixel( aStatusPos, aStatusSize );
273 			aTreeViewSize.Height() -= aStatusSize.Height();
274 		}
275 
276 		// set the size of treelistbox
277 		m_pTreeView->SetPosSizePixel( aTreeViewPos, aTreeViewSize );
278 
279 		//set the size of the splitter
280 		m_pSplitter->SetPosSizePixel( aSplitPos, Size( aSplitSize.Width(), aPlaygroundSize.Height() ) );
281 		m_pSplitter->SetDragRectPixel( _rPlayground );
282 	}
283 
284 	// set the size of grid control
285 	Reference< ::com::sun::star::awt::XWindow >  xGridAsWindow(m_xGrid, UNO_QUERY);
286 	if (xGridAsWindow.is())
287 		xGridAsWindow->setPosSize( aSplitPos.X() + aSplitSize.Width(), aPlaygroundPos.Y(),
288 								   aPlaygroundSize.Width() - aSplitSize.Width() - aSplitPos.X(), aPlaygroundSize.Height(), ::com::sun::star::awt::PosSize::POSSIZE);
289 
290 	// just for completeness: there is no space left, we occupied it all ...
291 	_rPlayground.SetPos( _rPlayground.BottomRight() );
292 	_rPlayground.SetSize( Size( 0, 0 ) );
293 }
294 
295 //------------------------------------------------------------------
296 sal_uInt16 UnoDataBrowserView::View2ModelPos(sal_uInt16 nPos) const
297 {
298 	return m_pVclControl ? m_pVclControl->GetModelColumnPos(m_pVclControl->GetColumnIdFromViewPos(nPos)) : -1;
299 }
300 
301 // -----------------------------------------------------------------------------
302 SbaGridControl*	UnoDataBrowserView::getVclControl() const
303 {
304 	if ( !m_pVclControl )
305 	{
306 		OSL_ENSURE(m_xGrid.is(),"Grid not set!");
307 		if ( m_xGrid.is() )
308 		{
309 			Reference< ::com::sun::star::awt::XWindowPeer >  xPeer = m_xGrid->getPeer();
310 			if ( xPeer.is() )
311 			{
312 				SbaXGridPeer* pPeer = SbaXGridPeer::getImplementation(xPeer);
313 				UnoDataBrowserView* pTHIS = const_cast<UnoDataBrowserView*>(this);
314 				if ( pPeer )
315 				{
316 					m_pVclControl = static_cast<SbaGridControl*>(pPeer->GetWindow());
317 					pTHIS->startComponentListening(Reference<XComponent>(VCLUnoHelper::GetInterface(m_pVclControl),UNO_QUERY));
318 				}
319 			}
320 		}
321 	}
322 	return m_pVclControl;
323 }
324 // -----------------------------------------------------------------------------
325 void UnoDataBrowserView::GetFocus()
326 {
327 	ODataView::GetFocus();
328 	if( m_pTreeView && m_pTreeView->IsVisible() && !m_pTreeView->HasChildPathFocus())
329 		m_pTreeView->GrabFocus();
330 	else if (m_pVclControl && m_xGrid.is())
331 	{
332 		sal_Bool bGrabFocus = sal_False;
333 		if(!m_pVclControl->HasChildPathFocus())
334 		{
335 			bGrabFocus = isGrabVclControlFocusAllowed(this);
336 			if( bGrabFocus )
337 				m_pVclControl->GrabFocus();
338 		}
339 		if(!bGrabFocus && m_pTreeView && m_pTreeView->IsVisible() )
340 			m_pTreeView->GrabFocus();
341 	}
342 }
343 // -----------------------------------------------------------------------------
344 void UnoDataBrowserView::_disposing( const ::com::sun::star::lang::EventObject& /*_rSource*/ )
345 {
346 	stopComponentListening(Reference<XComponent>(VCLUnoHelper::GetInterface(m_pVclControl),UNO_QUERY));
347 	m_pVclControl = NULL;
348 }
349 // -------------------------------------------------------------------------
350 long UnoDataBrowserView::PreNotify( NotifyEvent& rNEvt )
351 {
352 	long nDone = 0L;
353 	if(rNEvt.GetType() == EVENT_KEYINPUT)
354 	{
355 		sal_Bool bGrabAllowed = isGrabVclControlFocusAllowed(this);
356 		if ( bGrabAllowed )
357 		{
358 			const KeyEvent* pKeyEvt = rNEvt.GetKeyEvent();
359 			const KeyCode& rKeyCode = pKeyEvt->GetKeyCode();
360 			if (  ( rKeyCode == KeyCode( KEY_E, sal_True, sal_True, sal_False, sal_False ) )
361                || ( rKeyCode == KeyCode( KEY_TAB, sal_True, sal_False, sal_False, sal_False ) )
362                )
363 			{
364 				if ( m_pTreeView && m_pVclControl && m_pTreeView->HasChildPathFocus() )
365 					m_pVclControl->GrabFocus();
366 				else if ( m_pTreeView && m_pVclControl && m_pVclControl->HasChildPathFocus() )
367 					m_pTreeView->GrabFocus();
368 
369 				nDone = 1L;
370 			}
371 		}
372 	}
373 	return nDone ? nDone : ODataView::PreNotify(rNEvt);
374 }
375 
376 DBG_NAME(BrowserViewStatusDisplay)
377 // -----------------------------------------------------------------------------
378 BrowserViewStatusDisplay::BrowserViewStatusDisplay( UnoDataBrowserView* _pView, const String& _rStatus )
379 	:m_pView(_pView)
380 {
381     DBG_CTOR(BrowserViewStatusDisplay,NULL);
382 
383 	if (m_pView)
384 		m_pView->showStatus(_rStatus);
385 }
386 
387 // -----------------------------------------------------------------------------
388 BrowserViewStatusDisplay::~BrowserViewStatusDisplay( )
389 {
390 	if (m_pView)
391 		m_pView->showStatus(String());
392 
393     DBG_DTOR(BrowserViewStatusDisplay,NULL);
394 }
395 // -----------------------------------------------------------------------------
396