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 <JoinController.hxx>
23 #include <JoinExchange.hxx>
24 #include <JoinTableView.hxx>
25 #include <JoinDesignView.hxx>
26 #include <osl/diagnose.h>
27 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
28 #include <com/sun/star/sdbc/SQLException.hpp>
29 #include <vcl/svapp.hxx>
30 #include <vcl/commandevent.hxx>
31 #include <o3tl/string_view.hxx>
33 using namespace dbaui
;
34 using namespace ::com::sun::star::sdbc
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::datatransfer
;
38 OJoinExchangeData::OJoinExchangeData(OTableWindowListBox
* pBox
)
40 , nEntry(pBox
->get_widget().get_selected_index())
44 OTableWindowListBox::OTableWindowListBox(OTableWindow
* pParent
)
45 : InterimItemWindow(pParent
, u
"dbaccess/ui/tablelistbox.ui"_ustr
, u
"TableListBox"_ustr
)
46 , m_xTreeView(m_xBuilder
->weld_tree_view(u
"treeview"_ustr
))
47 , m_xDragDropTargetHelper(new TableWindowListBoxHelper(*this, m_xTreeView
->get_drop_target()))
49 , m_nDropEvent(nullptr)
52 m_xTreeView
->connect_row_activated(LINK(this, OTableWindowListBox
, OnDoubleClick
));
53 m_xTreeView
->connect_visible_range_changed(LINK(this, OTableWindowListBox
, ScrollHdl
));
54 m_xTreeView
->connect_popup_menu(LINK(this, OTableWindowListBox
, CommandHdl
));
56 m_xHelper
.set(new OJoinExchObj
);
57 rtl::Reference
<TransferDataContainer
> xHelper(m_xHelper
);
58 m_xTreeView
->enable_drag_source(xHelper
, DND_ACTION_LINK
);
59 m_xTreeView
->connect_drag_begin(LINK(this, OTableWindowListBox
, DragBeginHdl
));
62 IMPL_LINK(OTableWindowListBox
, CommandHdl
, const CommandEvent
&, rCEvt
, bool)
64 if (rCEvt
.GetCommand() != CommandEventId::ContextMenu
)
66 m_pTabWin
->Command(rCEvt
);
70 void OTableWindowListBox::dragFinished()
72 // first show the error msg when existing
73 m_pTabWin
->getDesignView()->getController().showError(
74 m_pTabWin
->getDesignView()->getController().clearOccurredError());
75 // second look for ui activities which should happen after d&d
77 Application::RemoveUserEvent(m_nUiEvent
);
79 = Application::PostUserEvent(LINK(this, OTableWindowListBox
, LookForUiHdl
), nullptr, true);
82 OTableWindowListBox::~OTableWindowListBox() { disposeOnce(); }
84 void OTableWindowListBox::dispose()
87 Application::RemoveUserEvent(m_nDropEvent
);
89 Application::RemoveUserEvent(m_nUiEvent
);
91 m_xDragDropTargetHelper
.reset();
93 InterimItemWindow::dispose();
96 int OTableWindowListBox::GetEntryFromText(std::u16string_view rEntryText
)
98 // iterate through the list
99 OJoinDesignView
* pView
= m_pTabWin
->getDesignView();
100 OJoinController
& rController
= pView
->getController();
105 const Reference
<XConnection
>& xConnection
= rController
.getConnection();
106 if (xConnection
.is())
108 Reference
<XDatabaseMetaData
> xMeta
= xConnection
->getMetaData();
110 bCase
= xMeta
->supportsMixedCaseQuotedIdentifiers();
112 for (int nEntry
= 0, nCount
= m_xTreeView
->n_children(); nEntry
< nCount
; ++nEntry
)
114 if (bCase
? rEntryText
== m_xTreeView
->get_text(nEntry
)
115 : o3tl::equalsIgnoreAsciiCase(rEntryText
, m_xTreeView
->get_text(nEntry
)))
119 catch (SQLException
&)
126 IMPL_LINK_NOARG(OTableWindowListBox
, ScrollHdl
, weld::TreeView
&, void)
128 // connections of this table, if any, should be redrawn
129 m_pTabWin
->getTableView()->Invalidate(InvalidateFlags::NoChildren
);
132 IMPL_LINK(OTableWindowListBox
, DragBeginHdl
, bool&, rUnsetDragIcon
, bool)
134 rUnsetDragIcon
= false;
135 if (m_xTreeView
->get_selected_index() == -1)
137 // no drag without a field
141 OJoinTableView
* pCont
= m_pTabWin
->getTableView();
142 if (!pCont
->getDesignView()->getController().isReadOnly()
143 && pCont
->getDesignView()->getController().isConnected())
145 // asterisk was not allowed to be copied to selection browsebox
146 bool bFirstNotAllowed
= m_xTreeView
->is_selected(0) && m_pTabWin
->GetData()->IsShowAll();
147 // create a description of the source
148 OJoinExchangeData
jxdSource(this);
149 // update the exchange object
150 m_xHelper
->setDescriptors(jxdSource
, bFirstNotAllowed
);
158 sal_Int8
OTableWindowListBox::AcceptDrop(const AcceptDropEvent
& _rEvt
)
160 // to enable the autoscroll when we're close to the edges
161 std::unique_ptr
<weld::TreeIter
> xEntry(m_xTreeView
->make_iterator());
162 bool bHasDestRow
= m_xTreeView
->get_dest_row_at_pos(_rEvt
.maPosPixel
, xEntry
.get(), true);
164 sal_Int8 nDND_Action
= DND_ACTION_NONE
;
166 if (!OJoinExchObj::isFormatAvailable(
167 m_xDragDropTargetHelper
->GetDataFlavorExVector(),
168 SotClipboardFormatId::SBA_TABID
) // this means that the first entry is to be dragged
169 && OJoinExchObj::isFormatAvailable(m_xDragDropTargetHelper
->GetDataFlavorExVector()))
170 { // don't drop into the window if it's the drag source itself
172 // remove the selection if the dragging operation is leaving the window
174 m_xTreeView
->unselect_all();
178 return DND_ACTION_NONE
;
180 // automatically select right entry when dragging
181 m_xTreeView
->unselect_all();
182 m_xTreeView
->select(*xEntry
);
184 // one cannot drop on the first (*) entry
185 if (!(m_pTabWin
->GetData()->IsShowAll()
186 && (m_xTreeView
->get_iter_index_in_parent(*xEntry
) == 0)))
187 nDND_Action
= DND_ACTION_LINK
;
193 IMPL_LINK_NOARG(OTableWindowListBox
, LookForUiHdl
, void*, void)
195 m_nUiEvent
= nullptr;
196 m_pTabWin
->getTableView()->lookForUiActivities();
199 IMPL_LINK_NOARG(OTableWindowListBox
, DropHdl
, void*, void)
201 // create the connection
202 m_nDropEvent
= nullptr;
203 OSL_ENSURE(m_pTabWin
, "No TableWindow!");
206 OJoinTableView
* pCont
= m_pTabWin
->getTableView();
207 OSL_ENSURE(pCont
, "No QueryTableView!");
208 pCont
->AddConnection(m_aDropInfo
.aSource
, m_aDropInfo
.aDest
);
210 catch (const SQLException
& e
)
212 // remember the exception so that we can show them later when d&d is finished
213 m_pTabWin
->getDesignView()->getController().setErrorOccurred(
214 ::dbtools::SQLExceptionInfo(e
));
218 sal_Int8
OTableWindowListBox::ExecuteDrop(const ExecuteDropEvent
& _rEvt
)
220 TransferableDataHelper
aDropped(_rEvt
.maDropEvent
.Transferable
);
221 if (OJoinExchObj::isFormatAvailable(aDropped
.GetDataFlavorExVector()))
222 { // don't drop into the window if it's the drag source itself
223 m_aDropInfo
.aSource
= OJoinExchangeData(this);
224 m_aDropInfo
.aDest
= OJoinExchObj::GetSourceDescription(_rEvt
.maDropEvent
.Transferable
);
227 Application::RemoveUserEvent(m_nDropEvent
);
229 = Application::PostUserEvent(LINK(this, OTableWindowListBox
, DropHdl
), nullptr, true);
233 return DND_ACTION_NONE
;
235 return DND_ACTION_NONE
;
238 void OTableWindowListBox::LoseFocus()
241 m_pTabWin
->setActive(false);
242 InterimItemWindow::LoseFocus();
245 void OTableWindowListBox::GetFocus()
248 m_pTabWin
->setActive();
252 std::unique_ptr
<weld::TreeIter
> xCurrent
= m_xTreeView
->make_iterator();
253 if (m_xTreeView
->get_cursor(xCurrent
.get()))
255 m_xTreeView
->unselect_all();
256 m_xTreeView
->select(*xCurrent
);
260 InterimItemWindow::GetFocus();
263 IMPL_LINK_NOARG(OTableWindowListBox
, OnDoubleClick
, weld::TreeView
&, bool)
266 vcl::Window
* pParent
= Window::GetParent();
267 OSL_ENSURE(pParent
!= nullptr, "OTableWindowListBox::OnDoubleClick : have no Parent !");
269 std::unique_ptr
<weld::TreeIter
> xCurrent
= m_xTreeView
->make_iterator();
270 if (!m_xTreeView
->get_cursor(xCurrent
.get()))
273 static_cast<OTableWindow
*>(pParent
)->OnEntryDoubleClicked(*xCurrent
);
278 void OTableWindowListBox::Command(const CommandEvent
& rEvt
)
280 switch (rEvt
.GetCommand())
282 case CommandEventId::ContextMenu
:
284 static_cast<OTableWindow
*>(Window::GetParent())->Command(rEvt
);
288 InterimItemWindow::Command(rEvt
);
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */