Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
34 :
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;
42 :
43 : namespace
44 : {
45 0 : sal_Bool isGrabVclControlFocusAllowed(const UnoDataBrowserView* _pView)
46 : {
47 0 : sal_Bool bGrabFocus = sal_False;
48 0 : SbaGridControl* pVclControl = _pView->getVclControl();
49 0 : Reference< ::com::sun::star::awt::XControl > xGrid = _pView->getGridControl();
50 0 : if (pVclControl && xGrid.is())
51 : {
52 0 : bGrabFocus = sal_True;
53 0 : if(!pVclControl->HasChildPathFocus())
54 : {
55 0 : Reference<XChild> xChild(xGrid->getModel(),UNO_QUERY);
56 0 : Reference<XLoadable> xLoad;
57 0 : if(xChild.is())
58 0 : xLoad.set(xChild->getParent(),UNO_QUERY);
59 0 : bGrabFocus = xLoad.is() && xLoad->isLoaded();
60 : }
61 : }
62 0 : return bGrabFocus;
63 : }
64 : }
65 :
66 : // UnoDataBrowserView
67 :
68 0 : UnoDataBrowserView::UnoDataBrowserView( Window* pParent,
69 : IController& _rController,
70 : const Reference< ::com::sun::star::uno::XComponentContext >& _rxContext)
71 : :ODataView(pParent,_rController,_rxContext)
72 : ,m_pTreeView(NULL)
73 : ,m_pSplitter(NULL)
74 : ,m_pVclControl(NULL)
75 0 : ,m_pStatus(NULL)
76 : {
77 :
78 0 : }
79 :
80 0 : void UnoDataBrowserView::Construct(const Reference< ::com::sun::star::awt::XControlModel >& xModel)
81 : {
82 : try
83 : {
84 0 : ODataView::Construct();
85 :
86 : // our UNO representation
87 0 : m_xMe = VCLUnoHelper::CreateControlContainer(this);
88 :
89 : // create the (UNO-) control
90 0 : 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 0 : m_xGrid->setDesignMode(sal_True);
94 :
95 0 : Reference< ::com::sun::star::awt::XWindow > xGridWindow(m_xGrid, UNO_QUERY);
96 0 : xGridWindow->setVisible(sal_True);
97 0 : xGridWindow->setEnable(sal_True);
98 :
99 : // introduce the model to the grid
100 0 : m_xGrid->setModel(xModel);
101 : // introduce the container (me) to the grid
102 0 : Reference< ::com::sun::star::beans::XPropertySet > xModelSet(xModel, UNO_QUERY);
103 0 : getContainer()->addControl(::comphelper::getString(xModelSet->getPropertyValue(PROPERTY_NAME)), m_xGrid);
104 :
105 : // get the VCL-control
106 0 : m_pVclControl = NULL;
107 0 : getVclControl();
108 :
109 0 : OSL_ENSURE(m_pVclControl != NULL, "UnoDataBrowserView::Construct : no real grid control !");
110 : }
111 0 : catch(const Exception&)
112 : {
113 0 : ::comphelper::disposeComponent(m_xGrid);
114 0 : throw;
115 : }
116 0 : }
117 :
118 0 : UnoDataBrowserView::~UnoDataBrowserView()
119 : {
120 : {
121 0 : boost::scoped_ptr<Splitter> aTemp(m_pSplitter);
122 0 : m_pSplitter = NULL;
123 : }
124 0 : setTreeView(NULL);
125 :
126 0 : if ( m_pStatus )
127 : {
128 0 : delete m_pStatus;
129 0 : m_pStatus = NULL;
130 : }
131 :
132 : try
133 : {
134 0 : ::comphelper::disposeComponent(m_xGrid);
135 0 : ::comphelper::disposeComponent(m_xMe);
136 : }
137 0 : catch(const Exception&)
138 : {}
139 :
140 0 : }
141 :
142 0 : IMPL_LINK( UnoDataBrowserView, SplitHdl, void*, /*NOINTERESTEDIN*/ )
143 : {
144 0 : long nYPos = m_pSplitter->GetPosPixel().Y();
145 0 : m_pSplitter->SetPosPixel( Point( m_pSplitter->GetSplitPosPixel(), nYPos ) );
146 0 : Resize();
147 :
148 0 : return 0L;
149 : }
150 :
151 0 : void UnoDataBrowserView::setSplitter(Splitter* _pSplitter)
152 : {
153 0 : m_pSplitter = _pSplitter;
154 0 : m_pSplitter->SetSplitHdl( LINK( this, UnoDataBrowserView, SplitHdl ) );
155 0 : LINK( this, UnoDataBrowserView, SplitHdl ).Call(m_pSplitter);
156 0 : }
157 :
158 0 : void UnoDataBrowserView::setTreeView(DBTreeView* _pTreeView)
159 : {
160 0 : if (m_pTreeView != _pTreeView)
161 : {
162 0 : if (m_pTreeView)
163 : {
164 0 : boost::scoped_ptr<Window> aTemp(m_pTreeView);
165 0 : m_pTreeView = NULL;
166 : }
167 0 : m_pTreeView = _pTreeView;
168 : }
169 0 : }
170 :
171 0 : void UnoDataBrowserView::showStatus( const OUString& _rStatus )
172 : {
173 0 : if (_rStatus.isEmpty())
174 0 : hideStatus();
175 : else
176 : {
177 0 : if (!m_pStatus)
178 0 : m_pStatus = new FixedText(this);
179 0 : m_pStatus->SetText(_rStatus);
180 0 : m_pStatus->Show();
181 0 : Resize();
182 0 : Update();
183 : }
184 0 : }
185 :
186 0 : void UnoDataBrowserView::hideStatus()
187 : {
188 0 : if (!m_pStatus || !m_pStatus->IsVisible())
189 : // nothing to do
190 0 : return;
191 0 : m_pStatus->Hide();
192 0 : Resize();
193 0 : Update();
194 : }
195 :
196 0 : void UnoDataBrowserView::resizeDocumentView(Rectangle& _rPlayground)
197 : {
198 0 : Point aSplitPos;
199 0 : Size aSplitSize;
200 0 : Point aPlaygroundPos( _rPlayground.TopLeft() );
201 0 : Size aPlaygroundSize( _rPlayground.GetSize() );
202 :
203 0 : if (m_pTreeView && m_pTreeView->IsVisible() && m_pSplitter)
204 : {
205 : // calculate the splitter pos and size
206 0 : aSplitPos = m_pSplitter->GetPosPixel();
207 0 : aSplitPos.Y() = aPlaygroundPos.Y();
208 0 : aSplitSize = m_pSplitter->GetOutputSizePixel();
209 0 : aSplitSize.Height() = aPlaygroundSize.Height();
210 :
211 0 : if( ( aSplitPos.X() + aSplitSize.Width() ) > ( aPlaygroundSize.Width() ))
212 0 : aSplitPos.X() = aPlaygroundSize.Width() - aSplitSize.Width();
213 :
214 0 : if( aSplitPos.X() <= aPlaygroundPos.X() )
215 0 : aSplitPos.X() = aPlaygroundPos.X() + sal_Int32(aPlaygroundSize.Width() * 0.2);
216 :
217 : // the tree pos and size
218 0 : Point aTreeViewPos( aPlaygroundPos );
219 0 : Size aTreeViewSize( aSplitPos.X(), aPlaygroundSize.Height() );
220 :
221 : // the status pos and size
222 0 : if (m_pStatus && m_pStatus->IsVisible())
223 : {
224 0 : Size aStatusSize(aPlaygroundPos.X(), GetTextHeight() + 2);
225 0 : aStatusSize = LogicToPixel(aStatusSize, MAP_APPFONT);
226 0 : aStatusSize.Width() = aTreeViewSize.Width() - 2 - 2;
227 :
228 0 : Point aStatusPos( aPlaygroundPos.X() + 2, aTreeViewPos.Y() + aTreeViewSize.Height() - aStatusSize.Height() );
229 0 : m_pStatus->SetPosSizePixel( aStatusPos, aStatusSize );
230 0 : aTreeViewSize.Height() -= aStatusSize.Height();
231 : }
232 :
233 : // set the size of treelistbox
234 0 : m_pTreeView->SetPosSizePixel( aTreeViewPos, aTreeViewSize );
235 :
236 : //set the size of the splitter
237 0 : m_pSplitter->SetPosSizePixel( aSplitPos, Size( aSplitSize.Width(), aPlaygroundSize.Height() ) );
238 0 : m_pSplitter->SetDragRectPixel( _rPlayground );
239 : }
240 :
241 : // set the size of grid control
242 0 : Reference< ::com::sun::star::awt::XWindow > xGridAsWindow(m_xGrid, UNO_QUERY);
243 0 : if (xGridAsWindow.is())
244 0 : xGridAsWindow->setPosSize( aSplitPos.X() + aSplitSize.Width(), aPlaygroundPos.Y(),
245 0 : aPlaygroundSize.Width() - aSplitSize.Width() - aSplitPos.X(), aPlaygroundSize.Height(), ::com::sun::star::awt::PosSize::POSSIZE);
246 :
247 : // just for completeness: there is no space left, we occupied it all ...
248 0 : _rPlayground.SetPos( _rPlayground.BottomRight() );
249 0 : _rPlayground.SetSize( Size( 0, 0 ) );
250 0 : }
251 :
252 0 : sal_uInt16 UnoDataBrowserView::View2ModelPos(sal_uInt16 nPos) const
253 : {
254 0 : return m_pVclControl ? m_pVclControl->GetModelColumnPos(m_pVclControl->GetColumnIdFromViewPos(nPos)) : -1;
255 : }
256 :
257 0 : SbaGridControl* UnoDataBrowserView::getVclControl() const
258 : {
259 0 : if ( !m_pVclControl )
260 : {
261 : OSL_ENSURE(m_xGrid.is(),"Grid not set!");
262 0 : if ( m_xGrid.is() )
263 : {
264 0 : Reference< ::com::sun::star::awt::XWindowPeer > xPeer = m_xGrid->getPeer();
265 0 : if ( xPeer.is() )
266 : {
267 0 : SbaXGridPeer* pPeer = SbaXGridPeer::getImplementation(xPeer);
268 0 : UnoDataBrowserView* pTHIS = const_cast<UnoDataBrowserView*>(this);
269 0 : if ( pPeer )
270 : {
271 0 : m_pVclControl = static_cast<SbaGridControl*>(pPeer->GetWindow());
272 0 : pTHIS->startComponentListening(Reference<XComponent>(VCLUnoHelper::GetInterface(m_pVclControl),UNO_QUERY));
273 : }
274 0 : }
275 : }
276 : }
277 0 : return m_pVclControl;
278 : }
279 :
280 0 : void UnoDataBrowserView::GetFocus()
281 : {
282 0 : ODataView::GetFocus();
283 0 : if( m_pTreeView && m_pTreeView->IsVisible() && !m_pTreeView->HasChildPathFocus())
284 0 : m_pTreeView->GrabFocus();
285 0 : else if (m_pVclControl && m_xGrid.is())
286 : {
287 0 : sal_Bool bGrabFocus = sal_False;
288 0 : if(!m_pVclControl->HasChildPathFocus())
289 : {
290 0 : bGrabFocus = isGrabVclControlFocusAllowed(this);
291 0 : if( bGrabFocus )
292 0 : m_pVclControl->GrabFocus();
293 : }
294 0 : if(!bGrabFocus && m_pTreeView && m_pTreeView->IsVisible() )
295 0 : m_pTreeView->GrabFocus();
296 : }
297 0 : }
298 :
299 0 : void UnoDataBrowserView::_disposing( const ::com::sun::star::lang::EventObject& /*_rSource*/ )
300 : {
301 0 : stopComponentListening(Reference<XComponent>(VCLUnoHelper::GetInterface(m_pVclControl),UNO_QUERY));
302 0 : m_pVclControl = NULL;
303 0 : }
304 :
305 0 : bool UnoDataBrowserView::PreNotify( NotifyEvent& rNEvt )
306 : {
307 0 : bool nDone = false;
308 0 : if(rNEvt.GetType() == EVENT_KEYINPUT)
309 : {
310 0 : sal_Bool bGrabAllowed = isGrabVclControlFocusAllowed(this);
311 0 : if ( bGrabAllowed )
312 : {
313 0 : const KeyEvent* pKeyEvt = rNEvt.GetKeyEvent();
314 0 : const KeyCode& rKeyCode = pKeyEvt->GetKeyCode();
315 0 : if ( ( rKeyCode == KeyCode( KEY_E, true, true, false, false ) )
316 0 : || ( rKeyCode == KeyCode( KEY_TAB, true, false, false, false ) )
317 : )
318 : {
319 0 : if ( m_pTreeView && m_pVclControl && m_pTreeView->HasChildPathFocus() )
320 0 : m_pVclControl->GrabFocus();
321 0 : else if ( m_pTreeView && m_pVclControl && m_pVclControl->HasChildPathFocus() )
322 0 : m_pTreeView->GrabFocus();
323 :
324 0 : nDone = true;
325 : }
326 : }
327 : }
328 0 : return nDone || ODataView::PreNotify(rNEvt);
329 : }
330 :
331 0 : BrowserViewStatusDisplay::BrowserViewStatusDisplay( UnoDataBrowserView* _pView, const OUString& _rStatus )
332 0 : :m_pView(_pView)
333 : {
334 :
335 0 : if (m_pView)
336 0 : m_pView->showStatus(_rStatus);
337 0 : }
338 :
339 0 : BrowserViewStatusDisplay::~BrowserViewStatusDisplay( )
340 : {
341 0 : if (m_pView)
342 0 : m_pView->showStatus(OUString());
343 :
344 0 : }
345 :
346 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|