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 <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
32 using namespace dbaui;
33 using namespace ::com::sun::star::sdbc;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::datatransfer;
36
OJoinExchangeData(OTableWindowListBox * pBox)37 OJoinExchangeData::OJoinExchangeData(OTableWindowListBox* pBox)
38 : pListBox(pBox)
39 , nEntry(pBox->get_widget().get_selected_index())
40 {
41 }
42
OTableWindowListBox(OTableWindow * pParent)43 OTableWindowListBox::OTableWindowListBox(OTableWindow* pParent)
44 : InterimItemWindow(pParent, "dbaccess/ui/tablelistbox.ui", "TableListBox")
45 , m_xTreeView(m_xBuilder->weld_tree_view("treeview"))
46 , m_xDragDropTargetHelper(new TableWindowListBoxHelper(*this, m_xTreeView->get_drop_target()))
47 , m_pTabWin(pParent)
48 , m_nDropEvent(nullptr)
49 , m_nUiEvent(nullptr)
50 {
51 m_xTreeView->connect_row_activated(LINK(this, OTableWindowListBox, OnDoubleClick));
52 m_xTreeView->connect_visible_range_changed(LINK(this, OTableWindowListBox, ScrollHdl));
53 m_xTreeView->connect_popup_menu(LINK(this, OTableWindowListBox, CommandHdl));
54
55 m_xHelper.set(new OJoinExchObj);
56 rtl::Reference<TransferDataContainer> xHelper(m_xHelper);
57 m_xTreeView->enable_drag_source(xHelper, DND_ACTION_LINK);
58 m_xTreeView->connect_drag_begin(LINK(this, OTableWindowListBox, DragBeginHdl));
59 }
60
IMPL_LINK(OTableWindowListBox,CommandHdl,const CommandEvent &,rCEvt,bool)61 IMPL_LINK(OTableWindowListBox, CommandHdl, const CommandEvent&, rCEvt, bool)
62 {
63 if (rCEvt.GetCommand() != CommandEventId::ContextMenu)
64 return false;
65 m_pTabWin->Command(rCEvt);
66 return true;
67 }
68
dragFinished()69 void OTableWindowListBox::dragFinished()
70 {
71 // first show the error msg when existing
72 m_pTabWin->getDesignView()->getController().showError(
73 m_pTabWin->getDesignView()->getController().clearOccurredError());
74 // second look for ui activities which should happen after d&d
75 if (m_nUiEvent)
76 Application::RemoveUserEvent(m_nUiEvent);
77 m_nUiEvent
78 = Application::PostUserEvent(LINK(this, OTableWindowListBox, LookForUiHdl), nullptr, true);
79 }
80
~OTableWindowListBox()81 OTableWindowListBox::~OTableWindowListBox() { disposeOnce(); }
82
dispose()83 void OTableWindowListBox::dispose()
84 {
85 if (m_nDropEvent)
86 Application::RemoveUserEvent(m_nDropEvent);
87 if (m_nUiEvent)
88 Application::RemoveUserEvent(m_nUiEvent);
89 m_pTabWin.clear();
90 m_xDragDropTargetHelper.reset();
91 m_xTreeView.reset();
92 InterimItemWindow::dispose();
93 }
94
GetEntryFromText(const OUString & rEntryText)95 int OTableWindowListBox::GetEntryFromText(const OUString& rEntryText)
96 {
97 // iterate through the list
98 OJoinDesignView* pView = m_pTabWin->getDesignView();
99 OJoinController& rController = pView->getController();
100
101 try
102 {
103 bool bCase = false;
104 const Reference<XConnection>& xConnection = rController.getConnection();
105 if (xConnection.is())
106 {
107 Reference<XDatabaseMetaData> xMeta = xConnection->getMetaData();
108 if (xMeta.is())
109 bCase = xMeta->supportsMixedCaseQuotedIdentifiers();
110 }
111 for (int nEntry = 0, nCount = m_xTreeView->n_children(); nEntry < nCount; ++nEntry)
112 {
113 if (bCase ? rEntryText == m_xTreeView->get_text(nEntry)
114 : rEntryText.equalsIgnoreAsciiCase(m_xTreeView->get_text(nEntry)))
115 return nEntry;
116 }
117 }
118 catch (SQLException&)
119 {
120 }
121
122 return -1;
123 }
124
IMPL_LINK_NOARG(OTableWindowListBox,ScrollHdl,weld::TreeView &,void)125 IMPL_LINK_NOARG(OTableWindowListBox, ScrollHdl, weld::TreeView&, void)
126 {
127 // connections of this table, if any, should be redrawn
128 m_pTabWin->getTableView()->Invalidate(InvalidateFlags::NoChildren);
129 }
130
IMPL_LINK(OTableWindowListBox,DragBeginHdl,bool &,rUnsetDragIcon,bool)131 IMPL_LINK(OTableWindowListBox, DragBeginHdl, bool&, rUnsetDragIcon, bool)
132 {
133 rUnsetDragIcon = false;
134 if (m_xTreeView->get_selected_index() == -1)
135 {
136 // no drag without a field
137 return true;
138 }
139
140 OJoinTableView* pCont = m_pTabWin->getTableView();
141 if (!pCont->getDesignView()->getController().isReadOnly()
142 && pCont->getDesignView()->getController().isConnected())
143 {
144 // asterisk was not allowed to be copied to selection browsebox
145 bool bFirstNotAllowed = m_xTreeView->is_selected(0) && m_pTabWin->GetData()->IsShowAll();
146 // create a description of the source
147 OJoinExchangeData jxdSource(this);
148 // update the exchange object
149 m_xHelper->setDescriptors(jxdSource, bFirstNotAllowed);
150
151 return false;
152 }
153
154 return true;
155 }
156
AcceptDrop(const AcceptDropEvent & _rEvt)157 sal_Int8 OTableWindowListBox::AcceptDrop(const AcceptDropEvent& _rEvt)
158 {
159 // to enable the autoscroll when we're close to the edges
160 std::unique_ptr<weld::TreeIter> xEntry(m_xTreeView->make_iterator());
161 bool bHasDestRow = m_xTreeView->get_dest_row_at_pos(_rEvt.maPosPixel, xEntry.get(), true);
162
163 sal_Int8 nDND_Action = DND_ACTION_NONE;
164 // check the format
165 if (!OJoinExchObj::isFormatAvailable(
166 m_xDragDropTargetHelper->GetDataFlavorExVector(),
167 SotClipboardFormatId::SBA_TABID) // this means that the first entry is to be dragged
168 && OJoinExchObj::isFormatAvailable(m_xDragDropTargetHelper->GetDataFlavorExVector()))
169 { // don't drop into the window if it's the drag source itself
170
171 // remove the selection if the dragging operation is leaving the window
172 if (_rEvt.mbLeaving)
173 m_xTreeView->unselect_all();
174 else
175 {
176 if (!bHasDestRow)
177 return DND_ACTION_NONE;
178
179 // automatically select right entry when dragging
180 m_xTreeView->unselect_all();
181 m_xTreeView->select(*xEntry);
182
183 // one cannot drop on the first (*) entry
184 if (!(m_pTabWin->GetData()->IsShowAll()
185 && (m_xTreeView->get_iter_index_in_parent(*xEntry) == 0)))
186 nDND_Action = DND_ACTION_LINK;
187 }
188 }
189 return nDND_Action;
190 }
191
IMPL_LINK_NOARG(OTableWindowListBox,LookForUiHdl,void *,void)192 IMPL_LINK_NOARG(OTableWindowListBox, LookForUiHdl, void*, void)
193 {
194 m_nUiEvent = nullptr;
195 m_pTabWin->getTableView()->lookForUiActivities();
196 }
197
IMPL_LINK_NOARG(OTableWindowListBox,DropHdl,void *,void)198 IMPL_LINK_NOARG(OTableWindowListBox, DropHdl, void*, void)
199 {
200 // create the connection
201 m_nDropEvent = nullptr;
202 OSL_ENSURE(m_pTabWin, "No TableWindow!");
203 try
204 {
205 OJoinTableView* pCont = m_pTabWin->getTableView();
206 OSL_ENSURE(pCont, "No QueryTableView!");
207 pCont->AddConnection(m_aDropInfo.aSource, m_aDropInfo.aDest);
208 }
209 catch (const SQLException& e)
210 {
211 // remember the exception so that we can show them later when d&d is finished
212 m_pTabWin->getDesignView()->getController().setErrorOccurred(
213 ::dbtools::SQLExceptionInfo(e));
214 }
215 }
216
ExecuteDrop(const ExecuteDropEvent & _rEvt)217 sal_Int8 OTableWindowListBox::ExecuteDrop(const ExecuteDropEvent& _rEvt)
218 {
219 TransferableDataHelper aDropped(_rEvt.maDropEvent.Transferable);
220 if (OJoinExchObj::isFormatAvailable(aDropped.GetDataFlavorExVector()))
221 { // don't drop into the window if it's the drag source itself
222 m_aDropInfo.aSource = OJoinExchangeData(this);
223 m_aDropInfo.aDest = OJoinExchObj::GetSourceDescription(_rEvt.maDropEvent.Transferable);
224
225 if (m_nDropEvent)
226 Application::RemoveUserEvent(m_nDropEvent);
227 m_nDropEvent
228 = Application::PostUserEvent(LINK(this, OTableWindowListBox, DropHdl), nullptr, true);
229
230 dragFinished();
231
232 return DND_ACTION_NONE;
233 }
234 return DND_ACTION_NONE;
235 }
236
LoseFocus()237 void OTableWindowListBox::LoseFocus()
238 {
239 if (m_pTabWin)
240 m_pTabWin->setActive(false);
241 InterimItemWindow::LoseFocus();
242 }
243
GetFocus()244 void OTableWindowListBox::GetFocus()
245 {
246 if (m_pTabWin)
247 m_pTabWin->setActive();
248
249 if (m_xTreeView)
250 {
251 std::unique_ptr<weld::TreeIter> xCurrent = m_xTreeView->make_iterator();
252 if (m_xTreeView->get_cursor(xCurrent.get()))
253 {
254 m_xTreeView->unselect_all();
255 m_xTreeView->select(*xCurrent);
256 }
257 }
258
259 InterimItemWindow::GetFocus();
260 }
261
IMPL_LINK_NOARG(OTableWindowListBox,OnDoubleClick,weld::TreeView &,bool)262 IMPL_LINK_NOARG(OTableWindowListBox, OnDoubleClick, weld::TreeView&, bool)
263 {
264 // tell my parent
265 vcl::Window* pParent = Window::GetParent();
266 OSL_ENSURE(pParent != nullptr, "OTableWindowListBox::OnDoubleClick : have no Parent !");
267
268 std::unique_ptr<weld::TreeIter> xCurrent = m_xTreeView->make_iterator();
269 if (!m_xTreeView->get_cursor(xCurrent.get()))
270 return false;
271
272 static_cast<OTableWindow*>(pParent)->OnEntryDoubleClicked(*xCurrent);
273
274 return false;
275 }
276
Command(const CommandEvent & rEvt)277 void OTableWindowListBox::Command(const CommandEvent& rEvt)
278 {
279 switch (rEvt.GetCommand())
280 {
281 case CommandEventId::ContextMenu:
282 {
283 static_cast<OTableWindow*>(Window::GetParent())->Command(rEvt);
284 break;
285 }
286 default:
287 InterimItemWindow::Command(rEvt);
288 }
289 }
290
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
292