Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / dbaccess / source / ui / querydesign / TableWindowListBox.cxx
blob731aa9f0f0cb4948d878f3808010006cea9a0d5d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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)
39 : pListBox(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()))
48 , m_pTabWin(pParent)
49 , m_nDropEvent(nullptr)
50 , m_nUiEvent(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)
65 return false;
66 m_pTabWin->Command(rCEvt);
67 return true;
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
76 if (m_nUiEvent)
77 Application::RemoveUserEvent(m_nUiEvent);
78 m_nUiEvent
79 = Application::PostUserEvent(LINK(this, OTableWindowListBox, LookForUiHdl), nullptr, true);
82 OTableWindowListBox::~OTableWindowListBox() { disposeOnce(); }
84 void OTableWindowListBox::dispose()
86 if (m_nDropEvent)
87 Application::RemoveUserEvent(m_nDropEvent);
88 if (m_nUiEvent)
89 Application::RemoveUserEvent(m_nUiEvent);
90 m_pTabWin.clear();
91 m_xDragDropTargetHelper.reset();
92 m_xTreeView.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();
104 bool bCase = false;
105 const Reference<XConnection>& xConnection = rController.getConnection();
106 if (xConnection.is())
108 Reference<XDatabaseMetaData> xMeta = xConnection->getMetaData();
109 if (xMeta.is())
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)))
116 return nEntry;
119 catch (SQLException&)
123 return -1;
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
138 return true;
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);
152 return false;
155 return true;
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;
165 // check the format
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
173 if (_rEvt.mbLeaving)
174 m_xTreeView->unselect_all();
175 else
177 if (!bHasDestRow)
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;
190 return nDND_Action;
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);
226 if (m_nDropEvent)
227 Application::RemoveUserEvent(m_nDropEvent);
228 m_nDropEvent
229 = Application::PostUserEvent(LINK(this, OTableWindowListBox, DropHdl), nullptr, true);
231 dragFinished();
233 return DND_ACTION_NONE;
235 return DND_ACTION_NONE;
238 void OTableWindowListBox::LoseFocus()
240 if (m_pTabWin)
241 m_pTabWin->setActive(false);
242 InterimItemWindow::LoseFocus();
245 void OTableWindowListBox::GetFocus()
247 if (m_pTabWin)
248 m_pTabWin->setActive();
250 if (m_xTreeView)
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)
265 // tell my parent
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()))
271 return false;
273 static_cast<OTableWindow*>(pParent)->OnEntryDoubleClicked(*xCurrent);
275 return false;
278 void OTableWindowListBox::Command(const CommandEvent& rEvt)
280 switch (rEvt.GetCommand())
282 case CommandEventId::ContextMenu:
284 static_cast<OTableWindow*>(Window::GetParent())->Command(rEvt);
285 break;
287 default:
288 InterimItemWindow::Command(rEvt);
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */