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