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 "TableWindowListBox.hxx"
21 #include "TableWindow.hxx"
22 #include "QueryDesignView.hxx"
23 #include "QueryTableView.hxx"
24 #include "querycontroller.hxx"
25 #include "JoinExchange.hxx"
26 #include <osl/diagnose.h>
27 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
28 #include <vcl/svapp.hxx>
30 using namespace dbaui
;
31 using namespace ::com::sun::star::sdbc
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::datatransfer
;
35 OJoinExchangeData::OJoinExchangeData(OTableWindowListBox
* pBox
)
37 , pEntry(pBox
->FirstSelected())
40 const sal_uLong SCROLLING_TIMESPAN
= 500;
41 const long LISTBOX_SCROLLING_AREA
= 6;
42 // class OTableWindowListBox
43 OTableWindowListBox::OTableWindowListBox( OTableWindow
* pParent
)
44 :SvTreeListBox( pParent
, WB_HASBUTTONS
| WB_BORDER
)
45 ,m_aMousePos( Point(0,0) )
47 ,m_nDropEvent(nullptr)
49 ,m_bReallyScrolled( false )
51 m_aScrollTimer
.SetTimeout( SCROLLING_TIMESPAN
);
52 SetDoubleClickHdl( LINK(this, OTableWindowListBox
, OnDoubleClick
) );
54 SetSelectionMode(SINGLE_SELECTION
);
59 void OTableWindowListBox::dragFinished( )
61 // first show the error msg when existing
62 m_pTabWin
->getDesignView()->getController().showError(m_pTabWin
->getDesignView()->getController().clearOccurredError());
63 // second look for ui activities which should happen after d&d
65 Application::RemoveUserEvent(m_nUiEvent
);
66 m_nUiEvent
= Application::PostUserEvent(LINK(this, OTableWindowListBox
, LookForUiHdl
), nullptr, true);
69 OTableWindowListBox::~OTableWindowListBox()
74 void OTableWindowListBox::dispose()
77 Application::RemoveUserEvent(m_nDropEvent
);
79 Application::RemoveUserEvent(m_nUiEvent
);
80 if( m_aScrollTimer
.IsActive() )
81 m_aScrollTimer
.Stop();
83 SvTreeListBox::dispose();
86 SvTreeListEntry
* OTableWindowListBox::GetEntryFromText( const OUString
& rEntryText
)
88 // iterate through the list
89 SvTreeList
* pTreeList
= GetModel();
90 SvTreeListEntry
* pEntry
= pTreeList
->First();
91 OJoinDesignView
* pView
= m_pTabWin
->getDesignView();
92 OJoinController
& rController
= pView
->getController();
97 Reference
<XConnection
> xConnection
= rController
.getConnection();
100 Reference
<XDatabaseMetaData
> xMeta
= xConnection
->getMetaData();
102 bCase
= xMeta
->supportsMixedCaseQuotedIdentifiers();
106 if((bCase
? rEntryText
== GetEntryText(pEntry
) : rEntryText
.equalsIgnoreAsciiCase(GetEntryText(pEntry
))))
110 pEntry
= pTreeList
->Next(pEntry
);
120 void OTableWindowListBox::NotifyScrolled()
122 m_bReallyScrolled
= true;
125 void OTableWindowListBox::NotifyEndScroll()
127 if (m_bReallyScrolled
)
128 // connections of this table, if any, should be redrawn
129 m_pTabWin
->getTableView()->Invalidate(InvalidateFlags::NoChildren
);
131 // without InvalidateFlags::NoChildren all tables would be redrawn,
133 m_bReallyScrolled
= false;
136 bool OTableWindowListBox::PreNotify(NotifyEvent
& rNEvt
)
138 bool bHandled
= false;
139 switch (rNEvt
.GetType())
141 case MouseNotifyEvent::KEYINPUT
:
143 const KeyEvent
* pKeyEvent
= rNEvt
.GetKeyEvent();
144 const vcl::KeyCode
& rCode
= pKeyEvent
->GetKeyCode();
146 if (rCode
.GetCode() != KEY_RETURN
)
150 bHandled
= m_pTabWin
->HandleKeyInput(*pKeyEvent
);
155 if (rCode
.IsMod1() || rCode
.IsMod2() || rCode
.IsShift())
158 static_cast<OTableWindow
*>(Window::GetParent())->OnEntryDoubleClicked(FirstSelected());
166 return SvTreeListBox::PreNotify(rNEvt
);
170 IMPL_LINK_NOARG_TYPED( OTableWindowListBox
, ScrollUpHdl
, Timer
*, void )
172 SvTreeListEntry
* pEntry
= GetEntry( m_aMousePos
);
176 if( pEntry
!= Last() )
178 ScrollOutputArea( -1 );
179 pEntry
= GetEntry( m_aMousePos
);
184 IMPL_LINK_NOARG_TYPED( OTableWindowListBox
, ScrollDownHdl
, Timer
*, void )
186 SvTreeListEntry
* pEntry
= GetEntry( m_aMousePos
);
190 if( pEntry
!= Last() )
192 ScrollOutputArea( 1 );
193 pEntry
= GetEntry( m_aMousePos
);
198 void OTableWindowListBox::StartDrag( sal_Int8
/*nAction*/, const Point
& /*rPosPixel*/ )
200 OJoinTableView
* pCont
= m_pTabWin
->getTableView();
201 if (!pCont
->getDesignView()->getController().isReadOnly() && pCont
->getDesignView()->getController().isConnected())
203 // asterisk was not allowed to be copied to selection browsebox
204 bool bFirstNotAllowed
= FirstSelected() == First() && m_pTabWin
->GetData()->IsShowAll();
206 // create a description of the source
207 OJoinExchangeData
jxdSource(this);
208 // put it into a exchange object
209 OJoinExchObj
* pJoin
= new OJoinExchObj(jxdSource
,bFirstNotAllowed
);
210 Reference
< XTransferable
> xEnsureDelete(pJoin
);
211 pJoin
->StartDrag(this, DND_ACTION_LINK
, this);
215 sal_Int8
OTableWindowListBox::AcceptDrop( const AcceptDropEvent
& _rEvt
)
217 sal_Int8 nDND_Action
= DND_ACTION_NONE
;
219 if ( !OJoinExchObj::isFormatAvailable(GetDataFlavorExVector(),SotClipboardFormatId::SBA_TABID
) // this means that the first entry is to be draged
220 && OJoinExchObj::isFormatAvailable(GetDataFlavorExVector()) )
221 { // don't drop into the window if it's the drag source itself
223 // remove the selection if the dragging operation is leaving the window
229 m_aMousePos
= _rEvt
.maPosPixel
;
230 Size aOutputSize
= GetOutputSizePixel();
231 SvTreeListEntry
* pEntry
= GetEntry( m_aMousePos
);
233 return DND_ACTION_NONE
;
236 Rectangle
aBottomScrollArea( Point(0, aOutputSize
.Height()-LISTBOX_SCROLLING_AREA
),
237 Size(aOutputSize
.Width(), LISTBOX_SCROLLING_AREA
) );
238 Rectangle
aTopScrollArea( Point(0,0), Size(aOutputSize
.Width(), LISTBOX_SCROLLING_AREA
) );
240 // scroll up if the pointer is on the upper scroll area
241 if( aBottomScrollArea
.IsInside(m_aMousePos
) )
243 if( !m_aScrollTimer
.IsActive() )
245 m_aScrollTimer
.SetTimeoutHdl( LINK(this, OTableWindowListBox
, ScrollUpHdl
) );
246 ScrollUpHdl( nullptr );
250 // scroll down if the pointer is on the lower scroll area
251 else if( aTopScrollArea
.IsInside(m_aMousePos
) )
253 if( !m_aScrollTimer
.IsActive() )
255 m_aScrollTimer
.SetTimeoutHdl( LINK(this, OTableWindowListBox
, ScrollDownHdl
) );
256 ScrollDownHdl( nullptr );
261 if( m_aScrollTimer
.IsActive() )
262 m_aScrollTimer
.Stop();
265 // automatically select right entry when dragging
266 if ((FirstSelected() != pEntry
) || (FirstSelected() && NextSelected(FirstSelected())))
270 // one cannot drop on the first (*) entry
271 if(!( m_pTabWin
->GetData()->IsShowAll() && (pEntry
==First()) ))
272 nDND_Action
= DND_ACTION_LINK
;
278 IMPL_LINK_NOARG_TYPED( OTableWindowListBox
, LookForUiHdl
, void*, void )
280 m_nUiEvent
= nullptr;
281 m_pTabWin
->getTableView()->lookForUiActivities();
284 IMPL_LINK_NOARG_TYPED( OTableWindowListBox
, DropHdl
, void*, void )
286 // create the connection
287 m_nDropEvent
= nullptr;
288 OSL_ENSURE(m_pTabWin
,"No TableWindow!");
291 OJoinTableView
* pCont
= m_pTabWin
->getTableView();
292 OSL_ENSURE(pCont
,"No QueryTableView!");
293 pCont
->AddConnection(m_aDropInfo
.aSource
, m_aDropInfo
.aDest
);
295 catch(const SQLException
& e
)
297 // remember the exception so that we can show them later when d&d is finished
298 m_pTabWin
->getDesignView()->getController().setErrorOccurred(::dbtools::SQLExceptionInfo(e
));
302 sal_Int8
OTableWindowListBox::ExecuteDrop( const ExecuteDropEvent
& _rEvt
)
304 TransferableDataHelper
aDropped(_rEvt
.maDropEvent
.Transferable
);
305 if ( OJoinExchObj::isFormatAvailable(aDropped
.GetDataFlavorExVector()))
306 { // don't drop into the window if it's the drag source itself
307 m_aDropInfo
.aSource
= OJoinExchangeData(this);
308 m_aDropInfo
.aDest
= OJoinExchObj::GetSourceDescription(_rEvt
.maDropEvent
.Transferable
);
311 Application::RemoveUserEvent(m_nDropEvent
);
312 m_nDropEvent
= Application::PostUserEvent(LINK(this, OTableWindowListBox
, DropHdl
), nullptr, true);
314 return DND_ACTION_LINK
;
316 return DND_ACTION_NONE
;
319 void OTableWindowListBox::LoseFocus()
322 m_pTabWin
->setActive(false);
323 SvTreeListBox::LoseFocus();
326 void OTableWindowListBox::GetFocus()
329 m_pTabWin
->setActive();
331 if (GetCurEntry() != nullptr)
333 if ( GetSelectionCount() == 0 || GetCurEntry() != FirstSelected() )
335 if ( FirstSelected() )
336 Select(FirstSelected(), false);
337 Select(GetCurEntry());
340 ShowFocusRect(FirstSelected());
342 SvTreeListBox::GetFocus();
345 IMPL_LINK_NOARG_TYPED( OTableWindowListBox
, OnDoubleClick
, SvTreeListBox
*, bool )
348 vcl::Window
* pParent
= Window::GetParent();
349 OSL_ENSURE(pParent
!= nullptr, "OTableWindowListBox::OnDoubleClick : habe kein Parent !");
351 static_cast<OTableWindow
*>(pParent
)->OnEntryDoubleClicked(GetHdlEntry());
356 void OTableWindowListBox::Command(const CommandEvent
& rEvt
)
358 switch (rEvt
.GetCommand())
360 case CommandEventId::ContextMenu
:
362 static_cast<OTableWindow
*>(Window::GetParent())->Command(rEvt
);
366 SvTreeListBox::Command(rEvt
);
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */