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 <brwview.hxx>
21 #include <sbagrid.hxx>
22 #include <toolkit/helper/vclunohelper.hxx>
23 #include <comphelper/types.hxx>
24 #include <vcl/split.hxx>
25 #include <strings.hxx>
26 #include <com/sun/star/form/XLoadable.hpp>
27 #include <com/sun/star/awt/XControlContainer.hpp>
28 #include <com/sun/star/awt/PosSize.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/container/XChild.hpp>
31 #include <osl/diagnose.h>
33 using namespace dbaui
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::form
;
36 using namespace ::com::sun::star::beans
;
37 using namespace ::com::sun::star::container
;
38 using namespace ::com::sun::star::lang
;
42 bool isGrabVclControlFocusAllowed(const UnoDataBrowserView
* _pView
)
44 bool bGrabFocus
= false;
45 SbaGridControl
* pVclControl
= _pView
->getVclControl();
46 const Reference
< css::awt::XControl
> xGrid
= _pView
->getGridControl();
47 if (pVclControl
&& xGrid
.is())
50 if(!pVclControl
->HasChildPathFocus())
52 Reference
<XChild
> xChild(xGrid
->getModel(),UNO_QUERY
);
53 Reference
<XLoadable
> xLoad
;
55 xLoad
.set(xChild
->getParent(),UNO_QUERY
);
56 bGrabFocus
= xLoad
.is() && xLoad
->isLoaded();
65 UnoDataBrowserView::UnoDataBrowserView( vcl::Window
* pParent
,
66 IController
& _rController
,
67 const Reference
< css::uno::XComponentContext
>& _rxContext
)
68 :ODataView(pParent
,_rController
,_rxContext
)
71 ,m_pVclControl(nullptr)
76 void UnoDataBrowserView::Construct(const Reference
< css::awt::XControlModel
>& xModel
)
80 ODataView::Construct();
82 // our UNO representation
83 m_xMe
= VCLUnoHelper::CreateControlContainer(this);
85 // create the (UNO-) control
86 m_xGrid
= new SbaXGridControl( getORB() );
87 OSL_ENSURE(m_xGrid
.is(), "UnoDataBrowserView::Construct : could not create a grid control !");
88 // in design mode (for the moment)
89 m_xGrid
->setDesignMode(true);
90 m_xGrid
->setVisible(true);
91 m_xGrid
->setEnable(true);
93 // introduce the model to the grid
94 m_xGrid
->setModel(xModel
);
95 // introduce the container (me) to the grid
96 Reference
< css::beans::XPropertySet
> xModelSet(xModel
, UNO_QUERY
);
97 getContainer()->addControl(::comphelper::getString(xModelSet
->getPropertyValue(PROPERTY_NAME
)), m_xGrid
);
99 // get the VCL-control
100 m_pVclControl
= nullptr;
103 OSL_ENSURE(m_pVclControl
!= nullptr, "UnoDataBrowserView::Construct : no real grid control !");
105 catch(const Exception
&)
107 ::comphelper::disposeComponent(m_xGrid
);
112 UnoDataBrowserView::~UnoDataBrowserView()
117 void UnoDataBrowserView::dispose()
119 m_pSplitter
.disposeAndClear();
120 setTreeView(nullptr);
124 ::comphelper::disposeComponent(m_xGrid
);
125 ::comphelper::disposeComponent(m_xMe
);
127 catch(const Exception
&)
130 m_pVclControl
.clear();
131 ODataView::dispose();
134 IMPL_LINK_NOARG( UnoDataBrowserView
, SplitHdl
, Splitter
*, void )
136 tools::Long nYPos
= m_pSplitter
->GetPosPixel().Y();
137 m_pSplitter
->SetPosPixel( Point( m_pSplitter
->GetSplitPosPixel(), nYPos
) );
141 void UnoDataBrowserView::setSplitter(Splitter
* _pSplitter
)
143 m_pSplitter
= _pSplitter
;
144 m_pSplitter
->SetSplitHdl( LINK( this, UnoDataBrowserView
, SplitHdl
) );
145 LINK( this, UnoDataBrowserView
, SplitHdl
).Call(m_pSplitter
);
148 void UnoDataBrowserView::setTreeView(InterimDBTreeListBox
* pTreeView
)
150 if (m_pTreeView
.get() != pTreeView
)
152 m_pTreeView
.disposeAndClear();
153 m_pTreeView
= pTreeView
;
157 void UnoDataBrowserView::showStatus( const OUString
& _rStatus
)
159 if (_rStatus
.isEmpty())
165 weld::Label
& rLabel
= m_pTreeView
->GetStatusBar();
166 rLabel
.set_label(_rStatus
);
173 void UnoDataBrowserView::hideStatus()
177 weld::Label
& rLabel
= m_pTreeView
->GetStatusBar();
178 if (!rLabel
.get_visible())
188 void UnoDataBrowserView::resizeDocumentView(tools::Rectangle
& _rPlayground
)
192 Point
aPlaygroundPos( _rPlayground
.TopLeft() );
193 Size
aPlaygroundSize( _rPlayground
.GetSize() );
195 if (m_pTreeView
&& m_pTreeView
->IsVisible() && m_pSplitter
)
197 // calculate the splitter pos and size
198 aSplitPos
= m_pSplitter
->GetPosPixel();
199 aSplitPos
.setY( aPlaygroundPos
.Y() );
200 aSplitSize
= m_pSplitter
->GetOutputSizePixel();
201 aSplitSize
.setHeight( aPlaygroundSize
.Height() );
203 if( ( aSplitPos
.X() + aSplitSize
.Width() ) > ( aPlaygroundSize
.Width() ))
204 aSplitPos
.setX( aPlaygroundSize
.Width() - aSplitSize
.Width() );
206 if( aSplitPos
.X() <= aPlaygroundPos
.X() )
207 aSplitPos
.setX( aPlaygroundPos
.X() + sal_Int32(aPlaygroundSize
.Width() * 0.2) );
209 // the tree pos and size
210 Point
aTreeViewPos( aPlaygroundPos
);
211 Size
aTreeViewSize( aSplitPos
.X(), aPlaygroundSize
.Height() );
213 // set the size of treelistbox
214 m_pTreeView
->SetPosSizePixel( aTreeViewPos
, aTreeViewSize
);
215 // Call this to trigger InterimItemWindow::Layout immediately, and
216 // not later on idle so the statusbar will be shown to explain
217 // a long delay on opening databases
218 m_pTreeView
->Resize();
220 //set the size of the splitter
221 m_pSplitter
->SetPosSizePixel( aSplitPos
, Size( aSplitSize
.Width(), aPlaygroundSize
.Height() ) );
222 m_pSplitter
->SetDragRectPixel( _rPlayground
);
225 // set the size of grid control
227 m_xGrid
->setPosSize( aSplitPos
.X() + aSplitSize
.Width(), aPlaygroundPos
.Y(),
228 aPlaygroundSize
.Width() - aSplitSize
.Width() - aSplitPos
.X(), aPlaygroundSize
.Height(), css::awt::PosSize::POSSIZE
);
230 // just for completeness: there is no space left, we occupied it all ...
231 _rPlayground
.SetPos( _rPlayground
.BottomRight() );
232 _rPlayground
.SetSize( Size( 0, 0 ) );
235 sal_uInt16
UnoDataBrowserView::View2ModelPos(sal_uInt16 nPos
) const
237 return m_pVclControl
? m_pVclControl
->GetModelColumnPos(m_pVclControl
->GetColumnIdFromViewPos(nPos
)) : -1;
240 SbaGridControl
* UnoDataBrowserView::getVclControl() const
242 if ( !m_pVclControl
)
244 OSL_ENSURE(m_xGrid
.is(),"Grid not set!");
247 Reference
< css::awt::XWindowPeer
> xPeer
= m_xGrid
->getPeer();
250 SbaXGridPeer
* pPeer
= dynamic_cast<SbaXGridPeer
*>(xPeer
.get());
251 UnoDataBrowserView
* pTHIS
= const_cast<UnoDataBrowserView
*>(this);
254 m_pVclControl
= static_cast<SbaGridControl
*>(pPeer
->GetWindow());
255 pTHIS
->startComponentListening(VCLUnoHelper::GetInterface(m_pVclControl
));
260 return m_pVclControl
;
263 void UnoDataBrowserView::GetFocus()
265 ODataView::GetFocus();
266 if( m_pTreeView
&& m_pTreeView
->IsVisible() && !m_pTreeView
->HasChildPathFocus())
267 m_pTreeView
->GrabFocus();
268 else if (m_pVclControl
&& m_xGrid
.is())
270 bool bGrabFocus
= false;
271 if(!m_pVclControl
->HasChildPathFocus())
273 bGrabFocus
= isGrabVclControlFocusAllowed(this);
275 m_pVclControl
->GrabFocus();
277 if(!bGrabFocus
&& m_pTreeView
&& m_pTreeView
->IsVisible() )
278 m_pTreeView
->GrabFocus();
282 void UnoDataBrowserView::_disposing( const css::lang::EventObject
& /*_rSource*/ )
284 stopComponentListening(VCLUnoHelper::GetInterface(m_pVclControl
));
285 m_pVclControl
= nullptr;
288 bool UnoDataBrowserView::PreNotify( NotifyEvent
& rNEvt
)
291 if(rNEvt
.GetType() == NotifyEventType::KEYINPUT
)
293 bool bGrabAllowed
= isGrabVclControlFocusAllowed(this);
296 const KeyEvent
* pKeyEvt
= rNEvt
.GetKeyEvent();
297 const vcl::KeyCode
& rKeyCode
= pKeyEvt
->GetKeyCode();
298 if ( ( rKeyCode
== vcl::KeyCode( KEY_E
, true, true, false, false ) )
299 || ( rKeyCode
== vcl::KeyCode( KEY_TAB
, true, false, false, false ) )
302 if ( m_pTreeView
&& m_pVclControl
&& m_pTreeView
->HasChildPathFocus() )
303 m_pVclControl
->GrabFocus();
304 else if ( m_pTreeView
&& m_pVclControl
&& m_pVclControl
->HasChildPathFocus() )
305 m_pTreeView
->GrabFocus();
311 return bDone
|| ODataView::PreNotify(rNEvt
);
314 BrowserViewStatusDisplay::BrowserViewStatusDisplay( UnoDataBrowserView
* _pView
, const OUString
& _rStatus
)
319 m_pView
->showStatus(_rStatus
);
322 BrowserViewStatusDisplay::~BrowserViewStatusDisplay( )
325 m_pView
->showStatus(OUString());
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */