1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "brwctrlr.hxx"
21 #include "brwview.hxx"
22 #include "sbagrid.hxx"
23 #include <toolkit/helper/vclunohelper.hxx>
24 #include <comphelper/types.hxx>
25 #include <vcl/split.hxx>
26 #include "dbtreeview.hxx"
27 #include "dbustrings.hrc"
28 #include "dbu_brw.hrc"
29 #include <com/sun/star/form/XLoadable.hpp>
30 #include <com/sun/star/awt/XControlContainer.hpp>
31 #include "UITools.hxx"
32 #include <osl/diagnose.h>
33 #include <boost/scoped_ptr.hpp>
35 using namespace dbaui
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::sdb
;
38 using namespace ::com::sun::star::form
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::container
;
41 using namespace ::com::sun::star::lang
;
45 bool isGrabVclControlFocusAllowed(const UnoDataBrowserView
* _pView
)
47 bool bGrabFocus
= false;
48 SbaGridControl
* pVclControl
= _pView
->getVclControl();
49 Reference
< ::com::sun::star::awt::XControl
> xGrid
= _pView
->getGridControl();
50 if (pVclControl
&& xGrid
.is())
53 if(!pVclControl
->HasChildPathFocus())
55 Reference
<XChild
> xChild(xGrid
->getModel(),UNO_QUERY
);
56 Reference
<XLoadable
> xLoad
;
58 xLoad
.set(xChild
->getParent(),UNO_QUERY
);
59 bGrabFocus
= xLoad
.is() && xLoad
->isLoaded();
68 UnoDataBrowserView::UnoDataBrowserView( vcl::Window
* pParent
,
69 IController
& _rController
,
70 const Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
)
71 :ODataView(pParent
,_rController
,_rxContext
)
80 void UnoDataBrowserView::Construct(const Reference
< ::com::sun::star::awt::XControlModel
>& xModel
)
84 ODataView::Construct();
86 // our UNO representation
87 m_xMe
= VCLUnoHelper::CreateControlContainer(this);
89 // create the (UNO-) control
90 m_xGrid
= new SbaXGridControl( getORB() );
91 OSL_ENSURE(m_xGrid
.is(), "UnoDataBrowserView::Construct : could not create a grid control !");
92 // in design mode (for the moment)
93 m_xGrid
->setDesignMode(sal_True
);
95 Reference
< ::com::sun::star::awt::XWindow
> xGridWindow(m_xGrid
, UNO_QUERY
);
96 xGridWindow
->setVisible(sal_True
);
97 xGridWindow
->setEnable(sal_True
);
99 // introduce the model to the grid
100 m_xGrid
->setModel(xModel
);
101 // introduce the container (me) to the grid
102 Reference
< ::com::sun::star::beans::XPropertySet
> xModelSet(xModel
, UNO_QUERY
);
103 getContainer()->addControl(::comphelper::getString(xModelSet
->getPropertyValue(PROPERTY_NAME
)), m_xGrid
);
105 // get the VCL-control
106 m_pVclControl
= NULL
;
109 OSL_ENSURE(m_pVclControl
!= nullptr, "UnoDataBrowserView::Construct : no real grid control !");
111 catch(const Exception
&)
113 ::comphelper::disposeComponent(m_xGrid
);
118 UnoDataBrowserView::~UnoDataBrowserView()
123 void UnoDataBrowserView::dispose()
125 m_pSplitter
.disposeAndClear();
128 m_pStatus
.disposeAndClear();
132 ::comphelper::disposeComponent(m_xGrid
);
133 ::comphelper::disposeComponent(m_xMe
);
135 catch(const Exception
&)
138 m_pVclControl
.clear();
139 ODataView::dispose();
142 IMPL_LINK_NOARG( UnoDataBrowserView
, SplitHdl
)
144 long nYPos
= m_pSplitter
->GetPosPixel().Y();
145 m_pSplitter
->SetPosPixel( Point( m_pSplitter
->GetSplitPosPixel(), nYPos
) );
151 void UnoDataBrowserView::setSplitter(Splitter
* _pSplitter
)
153 m_pSplitter
= _pSplitter
;
154 m_pSplitter
->SetSplitHdl( LINK( this, UnoDataBrowserView
, SplitHdl
) );
155 LINK( this, UnoDataBrowserView
, SplitHdl
).Call(m_pSplitter
);
158 void UnoDataBrowserView::setTreeView(DBTreeView
* _pTreeView
)
160 if (m_pTreeView
.get() != _pTreeView
)
162 m_pTreeView
.disposeAndClear();
163 m_pTreeView
= _pTreeView
;
167 void UnoDataBrowserView::showStatus( const OUString
& _rStatus
)
169 if (_rStatus
.isEmpty())
174 m_pStatus
= VclPtr
<FixedText
>::Create(this);
175 m_pStatus
->SetText(_rStatus
);
182 void UnoDataBrowserView::hideStatus()
184 if (!m_pStatus
|| !m_pStatus
->IsVisible())
192 void UnoDataBrowserView::resizeDocumentView(Rectangle
& _rPlayground
)
196 Point
aPlaygroundPos( _rPlayground
.TopLeft() );
197 Size
aPlaygroundSize( _rPlayground
.GetSize() );
199 if (m_pTreeView
&& m_pTreeView
->IsVisible() && m_pSplitter
)
201 // calculate the splitter pos and size
202 aSplitPos
= m_pSplitter
->GetPosPixel();
203 aSplitPos
.Y() = aPlaygroundPos
.Y();
204 aSplitSize
= m_pSplitter
->GetOutputSizePixel();
205 aSplitSize
.Height() = aPlaygroundSize
.Height();
207 if( ( aSplitPos
.X() + aSplitSize
.Width() ) > ( aPlaygroundSize
.Width() ))
208 aSplitPos
.X() = aPlaygroundSize
.Width() - aSplitSize
.Width();
210 if( aSplitPos
.X() <= aPlaygroundPos
.X() )
211 aSplitPos
.X() = aPlaygroundPos
.X() + sal_Int32(aPlaygroundSize
.Width() * 0.2);
213 // the tree pos and size
214 Point
aTreeViewPos( aPlaygroundPos
);
215 Size
aTreeViewSize( aSplitPos
.X(), aPlaygroundSize
.Height() );
217 // the status pos and size
218 if (m_pStatus
&& m_pStatus
->IsVisible())
220 Size
aStatusSize(aPlaygroundPos
.X(), GetTextHeight() + 2);
221 aStatusSize
= LogicToPixel(aStatusSize
, MAP_APPFONT
);
222 aStatusSize
.Width() = aTreeViewSize
.Width() - 2 - 2;
224 Point
aStatusPos( aPlaygroundPos
.X() + 2, aTreeViewPos
.Y() + aTreeViewSize
.Height() - aStatusSize
.Height() );
225 m_pStatus
->SetPosSizePixel( aStatusPos
, aStatusSize
);
226 aTreeViewSize
.Height() -= aStatusSize
.Height();
229 // set the size of treelistbox
230 m_pTreeView
->SetPosSizePixel( aTreeViewPos
, aTreeViewSize
);
232 //set the size of the splitter
233 m_pSplitter
->SetPosSizePixel( aSplitPos
, Size( aSplitSize
.Width(), aPlaygroundSize
.Height() ) );
234 m_pSplitter
->SetDragRectPixel( _rPlayground
);
237 // set the size of grid control
238 Reference
< ::com::sun::star::awt::XWindow
> xGridAsWindow(m_xGrid
, UNO_QUERY
);
239 if (xGridAsWindow
.is())
240 xGridAsWindow
->setPosSize( aSplitPos
.X() + aSplitSize
.Width(), aPlaygroundPos
.Y(),
241 aPlaygroundSize
.Width() - aSplitSize
.Width() - aSplitPos
.X(), aPlaygroundSize
.Height(), ::com::sun::star::awt::PosSize::POSSIZE
);
243 // just for completeness: there is no space left, we occupied it all ...
244 _rPlayground
.SetPos( _rPlayground
.BottomRight() );
245 _rPlayground
.SetSize( Size( 0, 0 ) );
248 sal_uInt16
UnoDataBrowserView::View2ModelPos(sal_uInt16 nPos
) const
250 return m_pVclControl
? m_pVclControl
->GetModelColumnPos(m_pVclControl
->GetColumnIdFromViewPos(nPos
)) : -1;
253 SbaGridControl
* UnoDataBrowserView::getVclControl() const
255 if ( !m_pVclControl
)
257 OSL_ENSURE(m_xGrid
.is(),"Grid not set!");
260 Reference
< ::com::sun::star::awt::XWindowPeer
> xPeer
= m_xGrid
->getPeer();
263 SbaXGridPeer
* pPeer
= SbaXGridPeer::getImplementation(xPeer
);
264 UnoDataBrowserView
* pTHIS
= const_cast<UnoDataBrowserView
*>(this);
267 m_pVclControl
= static_cast<SbaGridControl
*>(pPeer
->GetWindow().get());
268 pTHIS
->startComponentListening(Reference
<XComponent
>(VCLUnoHelper::GetInterface(m_pVclControl
),UNO_QUERY
));
273 return m_pVclControl
;
276 void UnoDataBrowserView::GetFocus()
278 ODataView::GetFocus();
279 if( m_pTreeView
&& m_pTreeView
->IsVisible() && !m_pTreeView
->HasChildPathFocus())
280 m_pTreeView
->GrabFocus();
281 else if (m_pVclControl
&& m_xGrid
.is())
283 bool bGrabFocus
= false;
284 if(!m_pVclControl
->HasChildPathFocus())
286 bGrabFocus
= isGrabVclControlFocusAllowed(this);
288 m_pVclControl
->GrabFocus();
290 if(!bGrabFocus
&& m_pTreeView
&& m_pTreeView
->IsVisible() )
291 m_pTreeView
->GrabFocus();
295 void UnoDataBrowserView::_disposing( const ::com::sun::star::lang::EventObject
& /*_rSource*/ )
297 stopComponentListening(Reference
<XComponent
>(VCLUnoHelper::GetInterface(m_pVclControl
),UNO_QUERY
));
298 m_pVclControl
= NULL
;
301 bool UnoDataBrowserView::PreNotify( NotifyEvent
& rNEvt
)
304 if(rNEvt
.GetType() == MouseNotifyEvent::KEYINPUT
)
306 bool bGrabAllowed
= isGrabVclControlFocusAllowed(this);
309 const KeyEvent
* pKeyEvt
= rNEvt
.GetKeyEvent();
310 const vcl::KeyCode
& rKeyCode
= pKeyEvt
->GetKeyCode();
311 if ( ( rKeyCode
== vcl::KeyCode( KEY_E
, true, true, false, false ) )
312 || ( rKeyCode
== vcl::KeyCode( KEY_TAB
, true, false, false, false ) )
315 if ( m_pTreeView
&& m_pVclControl
&& m_pTreeView
->HasChildPathFocus() )
316 m_pVclControl
->GrabFocus();
317 else if ( m_pTreeView
&& m_pVclControl
&& m_pVclControl
->HasChildPathFocus() )
318 m_pTreeView
->GrabFocus();
324 return nDone
|| ODataView::PreNotify(rNEvt
);
327 BrowserViewStatusDisplay::BrowserViewStatusDisplay( UnoDataBrowserView
* _pView
, const OUString
& _rStatus
)
332 m_pView
->showStatus(_rStatus
);
335 BrowserViewStatusDisplay::~BrowserViewStatusDisplay( )
338 m_pView
->showStatus(OUString());
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */