bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / fmcomp / gridctrl.cxx
blobab8f128a7b290b89961d9e03511cc5d086cb37ba
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 <sal/log.hxx>
21 #include <helpids.h>
22 #include <svx/gridctrl.hxx>
23 #include <gridcell.hxx>
24 #include <svx/fmtools.hxx>
25 #include <svtools/stringtransfer.hxx>
26 #include <connectivity/dbtools.hxx>
27 #include <connectivity/dbconversion.hxx>
29 #include <fmprop.hxx>
30 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
31 #include <com/sun/star/accessibility/XAccessible.hpp>
32 #include <com/sun/star/sdb/XResultSetAccess.hpp>
33 #include <com/sun/star/sdb/RowChangeAction.hpp>
34 #include <com/sun/star/sdb/XRowsChangeBroadcaster.hpp>
35 #include <com/sun/star/sdbc/SQLException.hpp>
36 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
37 #include <com/sun/star/sdbc/XRowSet.hpp>
38 #include <com/sun/star/sdbcx/Privilege.hpp>
39 #include <com/sun/star/util/NumberFormatter.hpp>
40 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
43 #include <com/sun/star/container/XIndexAccess.hpp>
44 #include <tools/diagnose_ex.h>
45 #include <tools/debug.hxx>
46 #include <vcl/settings.hxx>
47 #include <vcl/commandevent.hxx>
48 #include <vcl/svapp.hxx>
49 #include <vcl/weld.hxx>
50 #include <vcl/weldutils.hxx>
52 #include <svx/strings.hrc>
54 #include <svx/dialmgr.hxx>
55 #include <sdbdatacolumn.hxx>
57 #include <comphelper/property.hxx>
58 #include <comphelper/types.hxx>
59 #include <cppuhelper/implbase.hxx>
61 #include <algorithm>
62 #include <cstdlib>
63 #include <map>
64 #include <memory>
66 using namespace ::dbtools;
67 using namespace ::dbtools::DBTypeConversion;
68 using namespace ::svxform;
69 using namespace ::svt;
70 using namespace ::com::sun::star::beans;
71 using namespace ::com::sun::star::lang;
72 using namespace ::com::sun::star::uno;
73 using namespace ::com::sun::star::sdbc;
74 using namespace ::com::sun::star::sdbcx;
75 using namespace ::com::sun::star::sdb;
76 using namespace ::com::sun::star::datatransfer;
77 using namespace ::com::sun::star::container;
78 using namespace com::sun::star::accessibility;
80 #define ROWSTATUS(row) (!row.is() ? "NULL" : row->GetStatus() == GridRowStatus::Clean ? "CLEAN" : row->GetStatus() == GridRowStatus::Modified ? "MODIFIED" : row->GetStatus() == GridRowStatus::Deleted ? "DELETED" : "INVALID")
82 constexpr auto DEFAULT_BROWSE_MODE =
83 BrowserMode::COLUMNSELECTION
84 | BrowserMode::MULTISELECTION
85 | BrowserMode::KEEPHIGHLIGHT
86 | BrowserMode::TRACKING_TIPS
87 | BrowserMode::HLINES
88 | BrowserMode::VLINES
89 | BrowserMode::HEADERBAR_NEW;
91 class RowSetEventListener : public ::cppu::WeakImplHelper<XRowsChangeListener>
93 VclPtr<DbGridControl> m_pControl;
94 public:
95 explicit RowSetEventListener(DbGridControl* i_pControl) : m_pControl(i_pControl)
99 private:
100 // XEventListener
101 virtual void SAL_CALL disposing(const css::lang::EventObject& /*i_aEvt*/) override
104 virtual void SAL_CALL rowsChanged(const css::sdb::RowsChangeEvent& i_aEvt) override
106 if ( i_aEvt.Action != RowChangeAction::UPDATE )
107 return;
109 ::DbGridControl::GrantControlAccess aAccess;
110 CursorWrapper* pSeek = m_pControl->GetSeekCursor(aAccess);
111 const DbGridRowRef& rSeekRow = m_pControl->GetSeekRow(aAccess);
112 for(const Any& rBookmark : i_aEvt.Bookmarks)
114 pSeek->moveToBookmark(rBookmark);
115 // get the data
116 rSeekRow->SetState(pSeek, true);
117 sal_Int32 nSeekPos = pSeek->getRow() - 1;
118 m_pControl->SetSeekPos(nSeekPos,aAccess);
119 m_pControl->RowModified(nSeekPos);
124 class GridFieldValueListener;
125 typedef std::map<sal_uInt16, GridFieldValueListener*> ColumnFieldValueListeners;
127 class GridFieldValueListener : protected ::comphelper::OPropertyChangeListener
129 osl::Mutex m_aMutex;
130 DbGridControl& m_rParent;
131 rtl::Reference<::comphelper::OPropertyChangeMultiplexer> m_pRealListener;
132 sal_uInt16 m_nId;
133 sal_Int16 m_nSuspended;
134 bool m_bDisposed : 1;
136 public:
137 GridFieldValueListener(DbGridControl& _rParent, const Reference< XPropertySet >& xField, sal_uInt16 _nId);
138 virtual ~GridFieldValueListener() override;
140 virtual void _propertyChanged(const PropertyChangeEvent& evt) override;
142 void suspend() { ++m_nSuspended; }
143 void resume() { --m_nSuspended; }
145 void dispose();
148 GridFieldValueListener::GridFieldValueListener(DbGridControl& _rParent, const Reference< XPropertySet >& _rField, sal_uInt16 _nId)
149 :OPropertyChangeListener(m_aMutex)
150 ,m_rParent(_rParent)
151 ,m_nId(_nId)
152 ,m_nSuspended(0)
153 ,m_bDisposed(false)
155 if (_rField.is())
157 m_pRealListener = new ::comphelper::OPropertyChangeMultiplexer(this, _rField);
158 m_pRealListener->addProperty(FM_PROP_VALUE);
162 GridFieldValueListener::~GridFieldValueListener()
164 dispose();
167 void GridFieldValueListener::_propertyChanged(const PropertyChangeEvent& /*_evt*/)
169 DBG_ASSERT(m_nSuspended>=0, "GridFieldValueListener::_propertyChanged : resume > suspend !");
170 if (m_nSuspended <= 0)
171 m_rParent.FieldValueChanged(m_nId);
174 void GridFieldValueListener::dispose()
176 if (m_bDisposed)
178 DBG_ASSERT(!m_pRealListener, "GridFieldValueListener::dispose : inconsistent !");
179 return;
182 if (m_pRealListener.is())
184 m_pRealListener->dispose();
185 m_pRealListener.clear();
188 m_bDisposed = true;
189 m_rParent.FieldListenerDisposing(m_nId);
192 class DisposeListenerGridBridge : public FmXDisposeListener
194 DbGridControl& m_rParent;
195 rtl::Reference<FmXDisposeMultiplexer> m_xRealListener;
197 public:
198 DisposeListenerGridBridge( DbGridControl& _rParent, const Reference< XComponent >& _rxObject);
199 virtual ~DisposeListenerGridBridge() override;
201 virtual void disposing(sal_Int16 _nId) override { m_rParent.disposing(_nId); }
204 DisposeListenerGridBridge::DisposeListenerGridBridge(DbGridControl& _rParent, const Reference< XComponent >& _rxObject)
205 :FmXDisposeListener()
206 ,m_rParent(_rParent)
209 if (_rxObject.is())
211 m_xRealListener = new FmXDisposeMultiplexer(this, _rxObject);
215 DisposeListenerGridBridge::~DisposeListenerGridBridge()
217 if (m_xRealListener.is())
219 m_xRealListener->dispose();
223 const DbGridControlNavigationBarState ControlMap[] =
225 DbGridControlNavigationBarState::Text,
226 DbGridControlNavigationBarState::Absolute,
227 DbGridControlNavigationBarState::Of,
228 DbGridControlNavigationBarState::Count,
229 DbGridControlNavigationBarState::First,
230 DbGridControlNavigationBarState::Next,
231 DbGridControlNavigationBarState::Prev,
232 DbGridControlNavigationBarState::Last,
233 DbGridControlNavigationBarState::New,
234 DbGridControlNavigationBarState::NONE
237 bool CompareBookmark(const Any& aLeft, const Any& aRight)
239 return aLeft == aRight;
242 class FmXGridSourcePropListener : public ::comphelper::OPropertyChangeListener
244 VclPtr<DbGridControl> m_pParent;
246 // a DbGridControl has no mutex, so we use our own as the base class expects one
247 osl::Mutex m_aMutex;
248 sal_Int16 m_nSuspended;
250 public:
251 explicit FmXGridSourcePropListener(DbGridControl* _pParent);
253 void suspend() { ++m_nSuspended; }
254 void resume() { --m_nSuspended; }
256 virtual void _propertyChanged(const PropertyChangeEvent& evt) override;
259 FmXGridSourcePropListener::FmXGridSourcePropListener(DbGridControl* _pParent)
260 :OPropertyChangeListener(m_aMutex)
261 ,m_pParent(_pParent)
262 ,m_nSuspended(0)
264 DBG_ASSERT(m_pParent, "FmXGridSourcePropListener::FmXGridSourcePropListener : invalid parent !");
267 void FmXGridSourcePropListener::_propertyChanged(const PropertyChangeEvent& evt)
269 DBG_ASSERT(m_nSuspended>=0, "FmXGridSourcePropListener::_propertyChanged : resume > suspend !");
270 if (m_nSuspended <= 0)
271 m_pParent->DataSourcePropertyChanged(evt);
274 const int nReserveNumDigits = 7;
276 NavigationBar::AbsolutePos::AbsolutePos(std::unique_ptr<weld::Entry> xEntry, NavigationBar* pBar)
277 : RecordItemWindowBase(std::move(xEntry))
278 , m_xParent(pBar)
282 bool NavigationBar::AbsolutePos::DoKeyInput(const KeyEvent& rEvt)
284 if (rEvt.GetKeyCode() == KEY_TAB)
286 m_xParent->GetParent()->GrabFocus();
287 return true;
289 return RecordItemWindowBase::DoKeyInput(rEvt);
292 void NavigationBar::AbsolutePos::PositionFired(sal_Int64 nRecord)
294 m_xParent->PositionDataSource(nRecord);
295 m_xParent->InvalidateState(DbGridControlNavigationBarState::Absolute);
298 void NavigationBar::PositionDataSource(sal_Int32 nRecord)
300 if (m_bPositioning)
301 return;
302 // the MoveToPosition may cause a LoseFocus which would lead to a second MoveToPosition,
303 // so protect against this recursion
304 m_bPositioning = true;
305 static_cast<DbGridControl*>(GetParent())->MoveToPosition(nRecord - 1);
306 m_bPositioning = false;
309 NavigationBar::NavigationBar(vcl::Window* pParent)
310 : InterimItemWindow(pParent, "svx/ui/navigationbar.ui", "NavigationBar")
311 , m_xRecordText(m_xBuilder->weld_label("recordtext"))
312 , m_xAbsolute(new NavigationBar::AbsolutePos(m_xBuilder->weld_entry("entry-noframe"), this))
313 , m_xRecordOf(m_xBuilder->weld_label("recordof"))
314 , m_xRecordCount(m_xBuilder->weld_label("recordcount"))
315 , m_xFirstBtn(m_xBuilder->weld_button("first"))
316 , m_xPrevBtn(m_xBuilder->weld_button("prev"))
317 , m_xNextBtn(m_xBuilder->weld_button("next"))
318 , m_xLastBtn(m_xBuilder->weld_button("last"))
319 , m_xNewBtn(m_xBuilder->weld_button("new"))
320 , m_xPrevRepeater(std::make_shared<weld::ButtonPressRepeater>(*m_xPrevBtn, LINK(this,NavigationBar,OnClick)))
321 , m_xNextRepeater(std::make_shared<weld::ButtonPressRepeater>(*m_xNextBtn, LINK(this,NavigationBar,OnClick)))
322 , m_nCurrentPos(-1)
323 , m_bPositioning(false)
325 vcl::Font aApplFont(Application::GetSettings().GetStyleSettings().GetToolFont());
326 m_xAbsolute->set_font(aApplFont);
327 aApplFont.SetTransparent(true);
328 m_xRecordText->set_font(aApplFont);
329 m_xRecordOf->set_font(aApplFont);
330 m_xRecordCount->set_font(aApplFont);
332 m_xFirstBtn->set_help_id(HID_GRID_TRAVEL_FIRST);
333 m_xPrevBtn->set_help_id(HID_GRID_TRAVEL_PREV);
334 m_xNextBtn->set_help_id(HID_GRID_TRAVEL_NEXT);
335 m_xLastBtn->set_help_id(HID_GRID_TRAVEL_LAST);
336 m_xNewBtn->set_help_id(HID_GRID_TRAVEL_NEW);
337 m_xAbsolute->set_help_id(HID_GRID_TRAVEL_ABSOLUTE);
338 m_xRecordCount->set_help_id(HID_GRID_NUMBEROFRECORDS);
340 // set handlers for buttons
341 m_xFirstBtn->connect_clicked(LINK(this,NavigationBar,OnClick));
342 m_xLastBtn->connect_clicked(LINK(this,NavigationBar,OnClick));
343 m_xNewBtn->connect_clicked(LINK(this,NavigationBar,OnClick));
345 m_xRecordText->set_label(SvxResId(RID_STR_REC_TEXT));
346 m_xRecordOf->set_label(SvxResId(RID_STR_REC_FROM_TEXT));
347 m_xRecordCount->set_label(OUString('?'));
349 auto nReserveWidth = m_xRecordCount->get_approximate_digit_width() * nReserveNumDigits;
350 m_xAbsolute->GetWidget()->set_size_request(nReserveWidth, -1);
351 m_xRecordCount->set_size_request(nReserveWidth, -1);
354 NavigationBar::~NavigationBar()
356 disposeOnce();
359 void NavigationBar::dispose()
361 m_xRecordText.reset();
362 m_xAbsolute.reset();
363 m_xRecordOf.reset();
364 m_xRecordCount.reset();
365 m_xFirstBtn.reset();
366 m_xPrevBtn.reset();
367 m_xNextBtn.reset();
368 m_xLastBtn.reset();
369 m_xNewBtn.reset();
370 InterimItemWindow::dispose();
373 sal_uInt16 NavigationBar::ArrangeControls()
375 return m_xContainer->get_preferred_size().Width();
378 IMPL_LINK(NavigationBar, OnClick, weld::Button&, rButton, void)
380 DbGridControl* pParent = static_cast<DbGridControl*>(GetParent());
382 if (pParent->m_aMasterSlotExecutor.IsSet())
384 bool lResult = false;
385 if (&rButton == m_xFirstBtn.get())
386 lResult = pParent->m_aMasterSlotExecutor.Call(DbGridControlNavigationBarState::First);
387 else if( &rButton == m_xPrevBtn.get() )
388 lResult = pParent->m_aMasterSlotExecutor.Call(DbGridControlNavigationBarState::Prev);
389 else if( &rButton == m_xNextBtn.get() )
390 lResult = pParent->m_aMasterSlotExecutor.Call(DbGridControlNavigationBarState::Next);
391 else if( &rButton == m_xLastBtn.get() )
392 lResult = pParent->m_aMasterSlotExecutor.Call(DbGridControlNavigationBarState::Last);
393 else if( &rButton == m_xNewBtn.get() )
394 lResult = pParent->m_aMasterSlotExecutor.Call(DbGridControlNavigationBarState::New);
396 if (lResult)
397 // the link already handled it
398 return;
401 if (&rButton == m_xFirstBtn.get())
402 pParent->MoveToFirst();
403 else if( &rButton == m_xPrevBtn.get() )
404 pParent->MoveToPrev();
405 else if( &rButton == m_xNextBtn.get() )
406 pParent->MoveToNext();
407 else if( &rButton == m_xLastBtn.get() )
408 pParent->MoveToLast();
409 else if( &rButton == m_xNewBtn.get() )
410 pParent->AppendNew();
413 void NavigationBar::InvalidateAll(sal_Int32 nCurrentPos, bool bAll)
415 if (!(m_nCurrentPos != nCurrentPos || nCurrentPos < 0 || bAll))
416 return;
418 DbGridControl* pParent = static_cast<DbGridControl*>(GetParent());
420 sal_Int32 nAdjustedRowCount = pParent->GetRowCount() - ((pParent->GetOptions() & DbGridControlOptions::Insert) ? 2 : 1);
422 // check if everything needs to be invalidated
423 bAll = bAll || m_nCurrentPos <= 0;
424 bAll = bAll || nCurrentPos <= 0;
425 bAll = bAll || m_nCurrentPos >= nAdjustedRowCount;
426 bAll = bAll || nCurrentPos >= nAdjustedRowCount;
428 if ( bAll )
430 m_nCurrentPos = nCurrentPos;
431 int i = 0;
432 while (ControlMap[i] != DbGridControlNavigationBarState::NONE)
433 SetState(ControlMap[i++]);
435 else // is in the center
437 m_nCurrentPos = nCurrentPos;
438 SetState(DbGridControlNavigationBarState::Count);
439 SetState(DbGridControlNavigationBarState::Absolute);
443 bool NavigationBar::GetState(DbGridControlNavigationBarState nWhich) const
445 DbGridControl* pParent = static_cast<DbGridControl*>(GetParent());
447 if (!pParent->IsOpen() || pParent->IsDesignMode() || !pParent->IsEnabled()
448 || pParent->IsFilterMode() )
449 return false;
450 else
452 // check if we have a master state provider
453 if (pParent->m_aMasterStateProvider.IsSet())
455 tools::Long nState = pParent->m_aMasterStateProvider.Call( nWhich );
456 if (nState>=0)
457 return (nState>0);
460 bool bAvailable = true;
462 switch (nWhich)
464 case DbGridControlNavigationBarState::First:
465 case DbGridControlNavigationBarState::Prev:
466 bAvailable = m_nCurrentPos > 0;
467 break;
468 case DbGridControlNavigationBarState::Next:
469 if(pParent->m_bRecordCountFinal)
471 bAvailable = m_nCurrentPos < pParent->GetRowCount() - 1;
472 if (!bAvailable && pParent->GetOptions() & DbGridControlOptions::Insert)
473 bAvailable = (m_nCurrentPos == pParent->GetRowCount() - 2) && pParent->IsModified();
475 break;
476 case DbGridControlNavigationBarState::Last:
477 if(pParent->m_bRecordCountFinal)
479 if (pParent->GetOptions() & DbGridControlOptions::Insert)
480 bAvailable = pParent->IsCurrentAppending() ? pParent->GetRowCount() > 1 :
481 m_nCurrentPos != pParent->GetRowCount() - 2;
482 else
483 bAvailable = m_nCurrentPos != pParent->GetRowCount() - 1;
485 break;
486 case DbGridControlNavigationBarState::New:
487 bAvailable = (pParent->GetOptions() & DbGridControlOptions::Insert) && pParent->GetRowCount() && m_nCurrentPos < pParent->GetRowCount() - 1;
488 break;
489 case DbGridControlNavigationBarState::Absolute:
490 bAvailable = pParent->GetRowCount() > 0;
491 break;
492 default: break;
494 return bAvailable;
498 void NavigationBar::SetState(DbGridControlNavigationBarState nWhich)
500 bool bAvailable = GetState(nWhich);
501 DbGridControl* pParent = static_cast<DbGridControl*>(GetParent());
502 weld::Widget* pWnd = nullptr;
503 switch (nWhich)
505 case DbGridControlNavigationBarState::First:
506 pWnd = m_xFirstBtn.get();
507 break;
508 case DbGridControlNavigationBarState::Prev:
509 pWnd = m_xPrevBtn.get();
510 break;
511 case DbGridControlNavigationBarState::Next:
512 pWnd = m_xNextBtn.get();
513 break;
514 case DbGridControlNavigationBarState::Last:
515 pWnd = m_xLastBtn.get();
516 break;
517 case DbGridControlNavigationBarState::New:
518 pWnd = m_xNewBtn.get();
519 break;
520 case DbGridControlNavigationBarState::Absolute:
521 pWnd = m_xAbsolute->GetWidget();
522 if (bAvailable)
523 m_xAbsolute->set_text(OUString::number(m_nCurrentPos + 1));
524 else
525 m_xAbsolute->set_text(OUString());
526 break;
527 case DbGridControlNavigationBarState::Text:
528 pWnd = m_xRecordText.get();
529 break;
530 case DbGridControlNavigationBarState::Of:
531 pWnd = m_xRecordOf.get();
532 break;
533 case DbGridControlNavigationBarState::Count:
535 pWnd = m_xRecordCount.get();
536 OUString aText;
537 if (bAvailable)
539 if (pParent->GetOptions() & DbGridControlOptions::Insert)
541 if (pParent->IsCurrentAppending() && !pParent->IsModified())
542 aText = OUString::number(pParent->GetRowCount());
543 else
544 aText = OUString::number(pParent->GetRowCount() - 1);
546 else
547 aText = OUString::number(pParent->GetRowCount());
548 if(!pParent->m_bRecordCountFinal)
549 aText += " *";
551 else
552 aText.clear();
554 // add the number of selected rows, if applicable
555 if (pParent->GetSelectRowCount())
557 OUString aExtendedInfo = aText + " (" +
558 OUString::number(pParent->GetSelectRowCount()) + ")";
559 m_xRecordCount->set_label(aExtendedInfo);
561 else
562 m_xRecordCount->set_label(aText);
564 pParent->SetRealRowCount(aText);
565 } break;
566 default: break;
568 DBG_ASSERT(pWnd, "no window");
569 if (!(pWnd && (pWnd->get_sensitive() != bAvailable)))
570 return;
572 // this "pWnd->IsEnabled() != bAvailable" is a little hack : Window::Enable always generates a user
573 // event (ImplGenerateMouseMove) even if nothing happened. This may lead to some unwanted effects, so we
574 // do this check.
575 // For further explanation see Bug 69900.
576 pWnd->set_sensitive(bAvailable);
577 if (!bAvailable)
579 if (pWnd == m_xNextBtn.get())
580 m_xNextRepeater->Stop();
581 else if (pWnd == m_xPrevBtn.get())
582 m_xPrevRepeater->Stop();
586 DbGridRow::DbGridRow():m_eStatus(GridRowStatus::Clean), m_bIsNew(true)
589 DbGridRow::DbGridRow(CursorWrapper* pCur, bool bPaintCursor)
590 :m_bIsNew(false)
593 if (pCur && pCur->Is())
595 Reference< XIndexAccess > xColumns(pCur->getColumns(), UNO_QUERY);
596 for (sal_Int32 i = 0; i < xColumns->getCount(); ++i)
598 Reference< XPropertySet > xColSet(
599 xColumns->getByIndex(i), css::uno::UNO_QUERY);
600 m_aVariants.emplace_back( new DataColumn(xColSet) );
603 if (pCur->rowDeleted())
604 m_eStatus = GridRowStatus::Deleted;
605 else
607 if (bPaintCursor)
608 m_eStatus = (pCur->isAfterLast() || pCur->isBeforeFirst()) ? GridRowStatus::Invalid : GridRowStatus::Clean;
609 else
611 const Reference< XPropertySet >& xSet = pCur->getPropertySet();
612 if (xSet.is())
614 m_bIsNew = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISNEW));
615 if (!m_bIsNew && (pCur->isAfterLast() || pCur->isBeforeFirst()))
616 m_eStatus = GridRowStatus::Invalid;
617 else if (::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISMODIFIED)))
618 m_eStatus = GridRowStatus::Modified;
619 else
620 m_eStatus = GridRowStatus::Clean;
622 else
623 m_eStatus = GridRowStatus::Invalid;
626 if (!m_bIsNew && IsValid())
627 m_aBookmark = pCur->getBookmark();
628 else
629 m_aBookmark = Any();
631 else
632 m_eStatus = GridRowStatus::Invalid;
635 DbGridRow::~DbGridRow()
639 void DbGridRow::SetState(CursorWrapper* pCur, bool bPaintCursor)
641 if (pCur && pCur->Is())
643 if (pCur->rowDeleted())
645 m_eStatus = GridRowStatus::Deleted;
646 m_bIsNew = false;
648 else
650 m_eStatus = GridRowStatus::Clean;
651 if (!bPaintCursor)
653 const Reference< XPropertySet >& xSet = pCur->getPropertySet();
654 DBG_ASSERT(xSet.is(), "DbGridRow::SetState : invalid cursor !");
656 if (::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISMODIFIED)))
657 m_eStatus = GridRowStatus::Modified;
658 m_bIsNew = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISNEW));
660 else
661 m_bIsNew = false;
666 if (!m_bIsNew && IsValid())
667 m_aBookmark = pCur->getBookmark();
668 else
669 m_aBookmark = Any();
671 catch(SQLException&)
673 DBG_UNHANDLED_EXCEPTION("svx");
674 m_aBookmark = Any();
675 m_eStatus = GridRowStatus::Invalid;
676 m_bIsNew = false;
679 else
681 m_aBookmark = Any();
682 m_eStatus = GridRowStatus::Invalid;
683 m_bIsNew = false;
687 DbGridControl::DbGridControl(
688 Reference< XComponentContext > const & _rxContext,
689 vcl::Window* pParent,
690 WinBits nBits)
691 :EditBrowseBox(pParent, EditBrowseBoxFlags::NONE, nBits, DEFAULT_BROWSE_MODE )
692 ,m_xContext(_rxContext)
693 ,m_aBar(VclPtr<NavigationBar>::Create(this))
694 ,m_nAsynAdjustEvent(nullptr)
695 ,m_pDataSourcePropListener(nullptr)
696 ,m_pFieldListeners(nullptr)
697 ,m_pGridListener(nullptr)
698 ,m_nSeekPos(-1)
699 ,m_nTotalCount(-1)
700 ,m_aNullDate(::dbtools::DBTypeConversion::getStandardDate())
701 ,m_nMode(DEFAULT_BROWSE_MODE)
702 ,m_nCurrentPos(-1)
703 ,m_nDeleteEvent(nullptr)
704 ,m_nOptions(DbGridControlOptions::Readonly)
705 ,m_nOptionMask(DbGridControlOptions::Insert | DbGridControlOptions::Update | DbGridControlOptions::Delete)
706 ,m_nLastColId(sal_uInt16(-1))
707 ,m_nLastRowId(-1)
708 ,m_bDesignMode(false)
709 ,m_bRecordCountFinal(false)
710 ,m_bNavigationBar(true)
711 ,m_bSynchDisplay(true)
712 ,m_bHandle(true)
713 ,m_bFilterMode(false)
714 ,m_bWantDestruction(false)
715 ,m_bPendingAdjustRows(false)
716 ,m_bHideScrollbars( false )
717 ,m_bUpdating(false)
720 OUString sName(SvxResId(RID_STR_NAVIGATIONBAR));
721 m_aBar->SetAccessibleName(sName);
722 m_aBar->Show();
723 ImplInitWindow( InitWindowFacet::All );
726 void DbGridControl::InsertHandleColumn()
728 // BrowseBox has problems when painting without a handleColumn (hide it here)
729 if (HasHandle())
730 BrowseBox::InsertHandleColumn(GetDefaultColumnWidth(OUString()));
731 else
732 BrowseBox::InsertHandleColumn(0);
735 void DbGridControl::Init()
737 VclPtr<BrowserHeader> pNewHeader = CreateHeaderBar(this);
738 pHeader->SetMouseTransparent(false);
740 SetHeaderBar(pNewHeader);
741 SetMode(m_nMode);
742 SetCursorColor(Color(0xFF, 0, 0));
744 InsertHandleColumn();
747 DbGridControl::~DbGridControl()
749 disposeOnce();
752 void DbGridControl::dispose()
754 RemoveColumns();
756 m_bWantDestruction = true;
757 osl::MutexGuard aGuard(m_aDestructionSafety);
758 if (m_pFieldListeners)
759 DisconnectFromFields();
760 m_pCursorDisposeListener.reset();
762 if (m_nDeleteEvent)
763 Application::RemoveUserEvent(m_nDeleteEvent);
765 if (m_pDataSourcePropMultiplexer.is())
767 m_pDataSourcePropMultiplexer->dispose();
768 m_pDataSourcePropMultiplexer.clear(); // this should delete the multiplexer
769 delete m_pDataSourcePropListener;
770 m_pDataSourcePropListener = nullptr;
772 m_xRowSetListener.clear();
774 m_pDataCursor.reset();
775 m_pSeekCursor.reset();
777 m_aBar.disposeAndClear();
779 EditBrowseBox::dispose();
782 void DbGridControl::StateChanged( StateChangedType nType )
784 EditBrowseBox::StateChanged( nType );
786 switch (nType)
788 case StateChangedType::Mirroring:
789 ImplInitWindow( InitWindowFacet::WritingMode );
790 Invalidate();
791 break;
793 case StateChangedType::Zoom:
795 ImplInitWindow( InitWindowFacet::Font );
797 // and give it a chance to rearrange
798 Point aPoint = GetControlArea().TopLeft();
799 sal_uInt16 nX = static_cast<sal_uInt16>(aPoint.X());
800 ArrangeControls(nX, static_cast<sal_uInt16>(aPoint.Y()));
801 ReserveControlArea(nX);
803 break;
804 case StateChangedType::ControlFont:
805 ImplInitWindow( InitWindowFacet::Font );
806 Invalidate();
807 break;
808 case StateChangedType::ControlForeground:
809 ImplInitWindow( InitWindowFacet::Foreground );
810 Invalidate();
811 break;
812 case StateChangedType::ControlBackground:
813 ImplInitWindow( InitWindowFacet::Background );
814 Invalidate();
815 break;
816 default:;
820 void DbGridControl::DataChanged( const DataChangedEvent& rDCEvt )
822 EditBrowseBox::DataChanged( rDCEvt );
823 if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS ) &&
824 (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
826 ImplInitWindow( InitWindowFacet::All );
827 Invalidate();
831 void DbGridControl::Select()
833 EditBrowseBox::Select();
835 // as the selected rows may have changed, update the according display in our navigation bar
836 m_aBar->InvalidateState(DbGridControlNavigationBarState::Count);
838 if (m_pGridListener)
839 m_pGridListener->selectionChanged();
842 void DbGridControl::ImplInitWindow( const InitWindowFacet _eInitWhat )
844 for (auto const & pCol : m_aColumns)
846 pCol->ImplInitWindow( GetDataWindow(), _eInitWhat );
849 if ( _eInitWhat & InitWindowFacet::WritingMode )
851 if ( m_bNavigationBar )
853 m_aBar->EnableRTL( IsRTLEnabled() );
857 if ( _eInitWhat & InitWindowFacet::Font )
859 if ( m_bNavigationBar )
861 if ( IsControlFont() )
862 m_aBar->SetControlFont( GetControlFont() );
863 else
864 m_aBar->SetControlFont();
866 m_aBar->SetZoom( GetZoom() );
870 if ( !(_eInitWhat & InitWindowFacet::Background) )
871 return;
873 if (IsControlBackground())
875 GetDataWindow().SetBackground(GetControlBackground());
876 GetDataWindow().SetControlBackground(GetControlBackground());
877 GetDataWindow().GetOutDev()->SetFillColor(GetControlBackground());
879 else
881 GetDataWindow().SetControlBackground();
882 GetDataWindow().GetOutDev()->SetFillColor(GetOutDev()->GetFillColor());
886 void DbGridControl::RemoveRows(bool bNewCursor)
888 // Did the data cursor change?
889 if (!bNewCursor)
891 m_pSeekCursor.reset();
892 m_xPaintRow = m_xDataRow = m_xEmptyRow = m_xCurrentRow = m_xSeekRow = nullptr;
893 m_nCurrentPos = m_nSeekPos = -1;
894 m_nOptions = DbGridControlOptions::Readonly;
896 RowRemoved(0, GetRowCount(), false);
897 m_nTotalCount = -1;
899 else
901 RemoveRows();
905 void DbGridControl::RemoveRows()
907 // we're going to remove all columns and all row, so deactivate the current cell
908 if (IsEditing())
909 DeactivateCell();
911 // de-initialize all columns
912 // if there are columns, free all controllers
913 for (auto const & pColumn : m_aColumns)
914 pColumn->Clear();
916 m_pSeekCursor.reset();
917 m_pDataCursor.reset();
919 m_xPaintRow = m_xDataRow = m_xEmptyRow = m_xCurrentRow = m_xSeekRow = nullptr;
920 m_nCurrentPos = m_nSeekPos = m_nTotalCount = -1;
921 m_nOptions = DbGridControlOptions::Readonly;
923 // reset number of sentences to zero in the browser
924 EditBrowseBox::RemoveRows();
925 m_aBar->InvalidateAll(m_nCurrentPos, true);
928 void DbGridControl::ArrangeControls(sal_uInt16& nX, sal_uInt16 nY)
930 // positioning of the controls
931 if (m_bNavigationBar)
933 tools::Rectangle aRect(GetControlArea());
934 m_aBar->SetPosSizePixel(Point(0, nY + 1), Size(aRect.GetSize().Width(), aRect.GetSize().Height() - 1));
935 nX = m_aBar->ArrangeControls();
939 void DbGridControl::EnableHandle(bool bEnable)
941 if (m_bHandle == bEnable)
942 return;
944 // HandleColumn is only hidden because there are a lot of problems while painting otherwise
945 RemoveColumn( HandleColumnId );
946 m_bHandle = bEnable;
947 InsertHandleColumn();
950 namespace
952 bool adjustModeForScrollbars( BrowserMode& _rMode, bool _bNavigationBar, bool _bHideScrollbars )
954 BrowserMode nOldMode = _rMode;
956 if ( !_bNavigationBar )
958 _rMode &= ~BrowserMode::AUTO_HSCROLL;
961 if ( _bHideScrollbars )
963 _rMode |= BrowserMode::NO_HSCROLL | BrowserMode::NO_VSCROLL;
964 _rMode &= ~BrowserMode( BrowserMode::AUTO_HSCROLL | BrowserMode::AUTO_VSCROLL );
966 else
968 _rMode |= BrowserMode::AUTO_HSCROLL | BrowserMode::AUTO_VSCROLL;
969 _rMode &= ~BrowserMode( BrowserMode::NO_HSCROLL | BrowserMode::NO_VSCROLL );
972 // note: if we have a navigation bar, we always have an AUTO_HSCROLL. In particular,
973 // _bHideScrollbars is ignored then
974 if ( _bNavigationBar )
976 _rMode |= BrowserMode::AUTO_HSCROLL;
977 _rMode &= ~BrowserMode::NO_HSCROLL;
980 return nOldMode != _rMode;
984 void DbGridControl::EnableNavigationBar(bool bEnable)
986 if (m_bNavigationBar == bEnable)
987 return;
989 m_bNavigationBar = bEnable;
991 if (bEnable)
993 m_aBar->Show();
994 m_aBar->Enable();
995 m_aBar->InvalidateAll(m_nCurrentPos, true);
997 if ( adjustModeForScrollbars( m_nMode, m_bNavigationBar, m_bHideScrollbars ) )
998 SetMode( m_nMode );
1000 // get size of the reserved ControlArea
1001 Point aPoint = GetControlArea().TopLeft();
1002 sal_uInt16 nX = static_cast<sal_uInt16>(aPoint.X());
1004 ArrangeControls(nX, static_cast<sal_uInt16>(aPoint.Y()));
1005 ReserveControlArea(nX);
1007 else
1009 m_aBar->Hide();
1010 m_aBar->Disable();
1012 if ( adjustModeForScrollbars( m_nMode, m_bNavigationBar, m_bHideScrollbars ) )
1013 SetMode( m_nMode );
1015 ReserveControlArea();
1019 DbGridControlOptions DbGridControl::SetOptions(DbGridControlOptions nOpt)
1021 DBG_ASSERT(!m_xCurrentRow.is() || !m_xCurrentRow->IsModified(),
1022 "DbGridControl::SetOptions : please do not call when editing a record (things are much easier this way ;) !");
1024 // for the next setDataSource (which is triggered by a refresh, for instance)
1025 m_nOptionMask = nOpt;
1027 // normalize the new options
1028 Reference< XPropertySet > xDataSourceSet = m_pDataCursor->getPropertySet();
1029 if (xDataSourceSet.is())
1031 // check what kind of options are available
1032 sal_Int32 nPrivileges = 0;
1033 xDataSourceSet->getPropertyValue(FM_PROP_PRIVILEGES) >>= nPrivileges;
1034 if ((nPrivileges & Privilege::INSERT) == 0)
1035 nOpt &= ~DbGridControlOptions::Insert;
1036 if ((nPrivileges & Privilege::UPDATE) == 0)
1037 nOpt &= ~DbGridControlOptions::Update;
1038 if ((nPrivileges & Privilege::DELETE) == 0)
1039 nOpt &= ~DbGridControlOptions::Delete;
1041 else
1042 nOpt = DbGridControlOptions::Readonly;
1044 // need to do something after that ?
1045 if (nOpt == m_nOptions)
1046 return m_nOptions;
1048 // the 'update' option only affects our BrowserMode (with or w/o focus rect)
1049 BrowserMode nNewMode = m_nMode;
1050 if (!(m_nMode & BrowserMode::CURSOR_WO_FOCUS))
1052 if (nOpt & DbGridControlOptions::Update)
1053 nNewMode |= BrowserMode::HIDECURSOR;
1054 else
1055 nNewMode &= ~BrowserMode::HIDECURSOR;
1057 else
1058 nNewMode &= ~BrowserMode::HIDECURSOR;
1059 // should not be necessary if EnablePermanentCursor is used to change the cursor behaviour, but to be sure ...
1061 if (nNewMode != m_nMode)
1063 SetMode(nNewMode);
1064 m_nMode = nNewMode;
1067 // _after_ setting the mode because this results in an ActivateCell
1068 DeactivateCell();
1070 bool bInsertChanged = (nOpt & DbGridControlOptions::Insert) != (m_nOptions & DbGridControlOptions::Insert);
1071 m_nOptions = nOpt;
1072 // we need to set this before the code below because it indirectly uses m_nOptions
1074 // the 'insert' option affects our empty row
1075 if (bInsertChanged)
1077 if (m_nOptions & DbGridControlOptions::Insert)
1078 { // the insert option is to be set
1079 m_xEmptyRow = new DbGridRow();
1080 RowInserted(GetRowCount());
1082 else
1083 { // the insert option is to be reset
1084 m_xEmptyRow = nullptr;
1085 if ((GetCurRow() == GetRowCount() - 1) && (GetCurRow() > 0))
1086 GoToRowColumnId(GetCurRow() - 1, GetCurColumnId());
1087 RowRemoved(GetRowCount());
1091 // the 'delete' options has no immediate consequences
1093 ActivateCell();
1094 Invalidate();
1095 return m_nOptions;
1098 void DbGridControl::ForceHideScrollbars()
1100 if ( m_bHideScrollbars )
1101 return;
1103 m_bHideScrollbars = true;
1105 if ( adjustModeForScrollbars( m_nMode, m_bNavigationBar, m_bHideScrollbars ) )
1106 SetMode( m_nMode );
1109 void DbGridControl::EnablePermanentCursor(bool bEnable)
1111 if (IsPermanentCursorEnabled() == bEnable)
1112 return;
1114 if (bEnable)
1116 m_nMode &= ~BrowserMode::HIDECURSOR; // without this BrowserMode::CURSOR_WO_FOCUS won't have any affect
1117 m_nMode |= BrowserMode::CURSOR_WO_FOCUS;
1119 else
1121 if (m_nOptions & DbGridControlOptions::Update)
1122 m_nMode |= BrowserMode::HIDECURSOR; // no cursor at all
1123 else
1124 m_nMode &= ~BrowserMode::HIDECURSOR; // at least the "non-permanent" cursor
1126 m_nMode &= ~BrowserMode::CURSOR_WO_FOCUS;
1128 SetMode(m_nMode);
1130 bool bWasEditing = IsEditing();
1131 DeactivateCell();
1132 if (bWasEditing)
1133 ActivateCell();
1136 bool DbGridControl::IsPermanentCursorEnabled() const
1138 return (m_nMode & BrowserMode::CURSOR_WO_FOCUS) && !(m_nMode & BrowserMode::HIDECURSOR);
1141 void DbGridControl::refreshController(sal_uInt16 _nColId, GrantControlAccess /*_aAccess*/)
1143 if ((GetCurColumnId() == _nColId) && IsEditing())
1144 { // the controller which is currently active needs to be refreshed
1145 DeactivateCell();
1146 ActivateCell();
1150 void DbGridControl::setDataSource(const Reference< XRowSet >& _xCursor, DbGridControlOptions nOpts)
1152 if (!_xCursor.is() && !m_pDataCursor)
1153 return;
1155 if (m_pDataSourcePropMultiplexer.is())
1157 m_pDataSourcePropMultiplexer->dispose();
1158 m_pDataSourcePropMultiplexer.clear(); // this should delete the multiplexer
1159 delete m_pDataSourcePropListener;
1160 m_pDataSourcePropListener = nullptr;
1162 m_xRowSetListener.clear();
1164 // is the new cursor valid ?
1165 // the cursor is only valid if it contains some columns
1166 // if there is no cursor or the cursor is not valid we have to clean up and leave
1167 if (!_xCursor.is() || !Reference< XColumnsSupplier > (_xCursor, UNO_QUERY_THROW)->getColumns()->hasElements())
1169 RemoveRows();
1170 return;
1173 // did the data cursor change?
1174 sal_uInt16 nCurPos = GetColumnPos(GetCurColumnId());
1176 SetUpdateMode(false);
1177 RemoveRows();
1178 DisconnectFromFields();
1180 m_pCursorDisposeListener.reset();
1183 ::osl::MutexGuard aGuard(m_aAdjustSafety);
1184 if (m_nAsynAdjustEvent)
1186 // the adjust was thought to work with the old cursor which we don't have anymore
1187 RemoveUserEvent(m_nAsynAdjustEvent);
1188 m_nAsynAdjustEvent = nullptr;
1192 // get a new formatter and data cursor
1193 m_xFormatter = nullptr;
1194 Reference< css::util::XNumberFormatsSupplier > xSupplier = getNumberFormats(getConnection(_xCursor), true);
1195 if (xSupplier.is())
1197 m_xFormatter = css::util::NumberFormatter::create(m_xContext);
1198 m_xFormatter->attachNumberFormatsSupplier(xSupplier);
1200 // retrieve the datebase of the Numberformatter
1203 xSupplier->getNumberFormatSettings()->getPropertyValue("NullDate") >>= m_aNullDate;
1205 catch(Exception&)
1210 m_pDataCursor.reset(new CursorWrapper(_xCursor));
1212 // now create a cursor for painting rows
1213 // we need that cursor only if we are not in insert only mode
1214 Reference< XResultSet > xClone;
1215 Reference< XResultSetAccess > xAccess( _xCursor, UNO_QUERY );
1218 xClone = xAccess.is() ? xAccess->createResultSet() : Reference< XResultSet > ();
1220 catch(Exception&)
1223 if (xClone.is())
1224 m_pSeekCursor.reset(new CursorWrapper(xClone));
1226 // property listening on the data source
1227 // (Normally one class would be sufficient : the multiplexer which could forward the property change to us.
1228 // But for that we would have been derived from ::comphelper::OPropertyChangeListener, which isn't exported.
1229 // So we introduce a second class, which is a ::comphelper::OPropertyChangeListener (in the implementation file we know this class)
1230 // and forwards the property changes to our special method "DataSourcePropertyChanged".)
1231 if (m_pDataCursor)
1233 m_pDataSourcePropListener = new FmXGridSourcePropListener(this);
1234 m_pDataSourcePropMultiplexer = new ::comphelper::OPropertyChangeMultiplexer(m_pDataSourcePropListener, m_pDataCursor->getPropertySet() );
1235 m_pDataSourcePropMultiplexer->addProperty(FM_PROP_ISMODIFIED);
1236 m_pDataSourcePropMultiplexer->addProperty(FM_PROP_ISNEW);
1239 BrowserMode nOldMode = m_nMode;
1240 if (m_pSeekCursor)
1244 Reference< XPropertySet > xSet(_xCursor, UNO_QUERY);
1245 if (xSet.is())
1247 // check what kind of options are available
1248 sal_Int32 nConcurrency = ResultSetConcurrency::READ_ONLY;
1249 xSet->getPropertyValue(FM_PROP_RESULTSET_CONCURRENCY) >>= nConcurrency;
1251 if ( ResultSetConcurrency::UPDATABLE == nConcurrency )
1253 sal_Int32 nPrivileges = 0;
1254 xSet->getPropertyValue(FM_PROP_PRIVILEGES) >>= nPrivileges;
1256 // Insert Option should be set if insert only otherwise you won't see any rows
1257 // and no insertion is possible
1258 if ((m_nOptionMask & DbGridControlOptions::Insert)
1259 && ((nPrivileges & Privilege::INSERT) == Privilege::INSERT) && (nOpts & DbGridControlOptions::Insert))
1260 m_nOptions |= DbGridControlOptions::Insert;
1261 if ((m_nOptionMask & DbGridControlOptions::Update)
1262 && ((nPrivileges & Privilege::UPDATE) == Privilege::UPDATE) && (nOpts & DbGridControlOptions::Update))
1263 m_nOptions |= DbGridControlOptions::Update;
1264 if ((m_nOptionMask & DbGridControlOptions::Delete)
1265 && ((nPrivileges & Privilege::DELETE) == Privilege::DELETE) && (nOpts & DbGridControlOptions::Delete))
1266 m_nOptions |= DbGridControlOptions::Delete;
1270 catch( const Exception& )
1272 DBG_UNHANDLED_EXCEPTION("svx");
1275 bool bPermanentCursor = IsPermanentCursorEnabled();
1276 m_nMode = DEFAULT_BROWSE_MODE;
1278 if ( bPermanentCursor )
1280 m_nMode |= BrowserMode::CURSOR_WO_FOCUS;
1281 m_nMode &= ~BrowserMode::HIDECURSOR;
1283 else
1285 // updates are allowed -> no focus rectangle
1286 if ( m_nOptions & DbGridControlOptions::Update )
1287 m_nMode |= BrowserMode::HIDECURSOR;
1290 m_nMode |= BrowserMode::MULTISELECTION;
1292 adjustModeForScrollbars( m_nMode, m_bNavigationBar, m_bHideScrollbars );
1294 Reference< XColumnsSupplier > xSupplyColumns(_xCursor, UNO_QUERY);
1295 if (xSupplyColumns.is())
1296 InitColumnsByFields(Reference< XIndexAccess > (xSupplyColumns->getColumns(), UNO_QUERY));
1298 ConnectToFields();
1301 sal_uInt32 nRecordCount(0);
1303 if (m_pSeekCursor)
1305 Reference< XPropertySet > xSet = m_pDataCursor->getPropertySet();
1306 xSet->getPropertyValue(FM_PROP_ROWCOUNT) >>= nRecordCount;
1307 m_bRecordCountFinal = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ROWCOUNTFINAL));
1309 m_xRowSetListener = new RowSetEventListener(this);
1310 Reference< XRowsChangeBroadcaster> xChangeBroad(xSet,UNO_QUERY);
1311 if ( xChangeBroad.is( ) )
1312 xChangeBroad->addRowsChangeListener(m_xRowSetListener);
1315 // insert the currently known rows
1316 // and one row if we are able to insert rows
1317 if (m_nOptions & DbGridControlOptions::Insert)
1319 // insert the empty row for insertion
1320 m_xEmptyRow = new DbGridRow();
1321 ++nRecordCount;
1323 if (nRecordCount)
1325 m_xPaintRow = m_xSeekRow = new DbGridRow(m_pSeekCursor.get(), true);
1326 m_xDataRow = new DbGridRow(m_pDataCursor.get(), false);
1327 RowInserted(0, nRecordCount, false);
1329 if (m_xSeekRow->IsValid())
1332 m_nSeekPos = m_pSeekCursor->getRow() - 1;
1334 catch( const Exception& )
1336 DBG_UNHANDLED_EXCEPTION("svx");
1337 m_nSeekPos = -1;
1340 else
1342 // no rows so we don't need a seekcursor
1343 m_pSeekCursor.reset();
1347 // go to the old column
1348 if (nCurPos == BROWSER_INVALIDID || nCurPos >= ColCount())
1349 nCurPos = 0;
1351 // Column zero is a valid choice and guaranteed to exist,
1352 // but invisible to the user; if we have at least one
1353 // user-visible column, go to that one.
1354 if (nCurPos == 0 && ColCount() > 1)
1355 nCurPos = 1;
1357 // there are rows so go to the selected current column
1358 if (nRecordCount)
1359 GoToRowColumnId(0, GetColumnId(nCurPos));
1360 // else stop the editing if necessary
1361 else if (IsEditing())
1362 DeactivateCell();
1364 // now reset the mode
1365 if (m_nMode != nOldMode)
1366 SetMode(m_nMode);
1368 // RecalcRows was already called while resizing
1369 if (!IsResizing() && GetRowCount())
1370 RecalcRows(GetTopRow(), GetVisibleRows(), true);
1372 m_aBar->InvalidateAll(m_nCurrentPos, true);
1373 SetUpdateMode(true);
1375 // start listening on the seek cursor
1376 if (m_pSeekCursor)
1377 m_pCursorDisposeListener.reset(new DisposeListenerGridBridge(*this, Reference< XComponent > (Reference< XInterface >(*m_pSeekCursor), UNO_QUERY)));
1380 void DbGridControl::RemoveColumns()
1382 if ( !isDisposed() && IsEditing() )
1383 DeactivateCell();
1385 m_aColumns.clear();
1387 EditBrowseBox::RemoveColumns();
1390 std::unique_ptr<DbGridColumn> DbGridControl::CreateColumn(sal_uInt16 nId)
1392 return std::unique_ptr<DbGridColumn>(new DbGridColumn(nId, *this));
1395 sal_uInt16 DbGridControl::AppendColumn(const OUString& rName, sal_uInt16 nWidth, sal_uInt16 nModelPos, sal_uInt16 nId)
1397 DBG_ASSERT(nId == BROWSER_INVALIDID, "DbGridControl::AppendColumn : I want to set the ID myself ...");
1398 sal_uInt16 nRealPos = nModelPos;
1399 if (nModelPos != HEADERBAR_APPEND)
1401 // calc the view pos. we can't use our converting functions because the new column
1402 // has no VCL-representation, yet.
1403 sal_Int16 nViewPos = nModelPos;
1404 while (nModelPos--)
1406 if ( m_aColumns[ nModelPos ]->IsHidden() )
1407 --nViewPos;
1409 // restore nModelPos, we need it later
1410 nModelPos = nRealPos;
1411 // the position the base class gets is the view pos + 1 (because of the handle column)
1412 nRealPos = nViewPos + 1;
1415 // calculate the new id
1416 for (nId=1; (GetModelColumnPos(nId) != GRID_COLUMN_NOT_FOUND) && size_t(nId) <= m_aColumns.size(); ++nId)
1418 DBG_ASSERT(GetViewColumnPos(nId) == GRID_COLUMN_NOT_FOUND, "DbGridControl::AppendColumn : inconsistent internal state !");
1419 // my column's models say "there is no column with id nId", but the view (the base class) says "there is a column ..."
1421 EditBrowseBox::AppendColumn(rName, nWidth, nRealPos, nId);
1422 if (nModelPos == HEADERBAR_APPEND)
1423 m_aColumns.push_back( CreateColumn(nId) );
1424 else
1425 m_aColumns.insert( m_aColumns.begin() + nModelPos, CreateColumn(nId) );
1427 return nId;
1430 void DbGridControl::RemoveColumn(sal_uInt16 nId)
1432 EditBrowseBox::RemoveColumn(nId);
1434 const sal_uInt16 nIndex = GetModelColumnPos(nId);
1435 if(nIndex != GRID_COLUMN_NOT_FOUND)
1437 m_aColumns.erase( m_aColumns.begin()+nIndex );
1441 void DbGridControl::ColumnMoved(sal_uInt16 nId)
1443 EditBrowseBox::ColumnMoved(nId);
1445 // remove the col from the model
1446 sal_uInt16 nOldModelPos = GetModelColumnPos(nId);
1447 #ifdef DBG_UTIL
1448 DbGridColumn* pCol = m_aColumns[ nOldModelPos ].get();
1449 DBG_ASSERT(!pCol->IsHidden(), "DbGridControl::ColumnMoved : moved a hidden col ? how this ?");
1450 #endif
1452 // for the new model pos we can't use GetModelColumnPos because we are altering the model at the moment
1453 // so the method won't work (in fact it would return the old model pos)
1455 // the new view pos is calculated easily
1456 sal_uInt16 nNewViewPos = GetViewColumnPos(nId);
1458 // from that we can compute the new model pos
1459 size_t nNewModelPos;
1460 for (nNewModelPos = 0; nNewModelPos < m_aColumns.size(); ++nNewModelPos)
1462 if (!m_aColumns[ nNewModelPos ]->IsHidden())
1464 if (!nNewViewPos)
1465 break;
1466 else
1467 --nNewViewPos;
1470 DBG_ASSERT( nNewModelPos < m_aColumns.size(), "DbGridControl::ColumnMoved : could not find the new model position !");
1472 // this will work. of course the model isn't fully consistent with our view right now, but let's
1473 // look at the situation : a column has been moved with in the VIEW from pos m to n, say m<n (in the
1474 // other case we can use analogue arguments).
1475 // All cols k with m<k<=n have been shifted left on pos, the former col m now has pos n.
1476 // In the model this affects a range of cols x to y, where x<=m and y<=n. And the number of hidden cols
1477 // within this range is constant, so we may calculate the view pos from the model pos in the above way.
1479 // for instance, let's look at a grid with six columns where the third one is hidden. this will
1480 // initially look like this :
1482 // +---+---+---+---+---+---+
1483 // model pos | 0 | 1 |*2*| 3 | 4 | 5 |
1484 // +---+---+---+---+---+---+
1485 // ID | 1 | 2 | 3 | 4 | 5 | 6 |
1486 // +---+---+---+---+---+---+
1487 // view pos | 0 | 1 | - | 2 | 3 | 4 |
1488 // +---+---+---+---+---+---+
1490 // if we move the column at (view) pos 1 to (view) pos 3 we have :
1492 // +---+---+---+---+---+---+
1493 // model pos | 0 | 3 |*2*| 4 | 1 | 5 | // not reflecting the changes, yet
1494 // +---+---+---+---+---+---+
1495 // ID | 1 | 4 | 3 | 5 | 2 | 6 | // already reflecting the changes
1496 // +---+---+---+---+---+---+
1497 // view pos | 0 | 1 | - | 2 | 3 | 4 |
1498 // +---+---+---+---+---+---+
1500 // or, sorted by the out-of-date model positions :
1502 // +---+---+---+---+---+---+
1503 // model pos | 0 | 1 |*2*| 3 | 4 | 5 |
1504 // +---+---+---+---+---+---+
1505 // ID | 1 | 2 | 3 | 4 | 5 | 6 |
1506 // +---+---+---+---+---+---+
1507 // view pos | 0 | 3 | - | 1 | 2 | 4 |
1508 // +---+---+---+---+---+---+
1510 // We know the new view pos (3) of the moved column because our base class tells us. So we look at our
1511 // model for the 4th (the pos is zero-based) visible column, it is at (model) position 4. And this is
1512 // exactly the pos where we have to re-insert our column's model, so it looks ike this :
1514 // +---+---+---+---+---+---+
1515 // model pos | 0 |*1*| 2 | 3 | 4 | 5 |
1516 // +---+---+---+---+---+---+
1517 // ID | 1 | 3 | 4 | 5 | 2 | 6 |
1518 // +---+---+---+---+---+---+
1519 // view pos | 0 | - | 1 | 2 | 3 | 4 |
1520 // +---+---+---+---+---+---+
1522 // Now, all is consistent again.
1523 // (except of the hidden column : The cycling of the cols occurred on the model, not on the view. maybe
1524 // the user expected the latter but there really is no good argument against our method ;) ...)
1526 // And no, this large explanation isn't just because I wanted to play a board game or something like
1527 // that. It's because it took me a while to see it myself, and the whole theme (hidden cols, model col
1528 // positions, view col positions) is really painful (at least for me) so the above pictures helped me a lot ;)
1530 auto temp = std::move(m_aColumns[ nOldModelPos ]);
1531 m_aColumns.erase( m_aColumns.begin() + nOldModelPos );
1532 m_aColumns.insert( m_aColumns.begin() + nNewModelPos, std::move(temp) );
1535 bool DbGridControl::SeekRow(sal_Int32 nRow)
1537 // in filter mode or in insert only mode we don't have any cursor!
1538 if ( !SeekCursor( nRow ) )
1539 return false;
1541 if ( IsFilterMode() )
1543 DBG_ASSERT( IsFilterRow( nRow ), "DbGridControl::SeekRow(): No filter row, wrong mode" );
1544 m_xPaintRow = m_xEmptyRow;
1546 else
1548 // on the current position we have to take the current row for display as we want
1549 // to have the most recent values for display
1550 if ( ( nRow == m_nCurrentPos ) && getDisplaySynchron() )
1551 m_xPaintRow = m_xCurrentRow;
1552 // seek to the empty insert row
1553 else if ( IsInsertionRow( nRow ) )
1554 m_xPaintRow = m_xEmptyRow;
1555 else
1557 m_xSeekRow->SetState( m_pSeekCursor.get(), true );
1558 m_xPaintRow = m_xSeekRow;
1562 EditBrowseBox::SeekRow(nRow);
1564 return m_nSeekPos >= 0;
1567 // Is called whenever the visible amount of data changes
1568 void DbGridControl::VisibleRowsChanged( sal_Int32 nNewTopRow, sal_uInt16 nLinesOnScreen )
1570 RecalcRows(nNewTopRow, nLinesOnScreen, false);
1573 void DbGridControl::RecalcRows(sal_Int32 nNewTopRow, sal_uInt16 nLinesOnScreen, bool bUpdateCursor)
1575 // If no cursor -> no rows in the browser.
1576 if (!m_pSeekCursor)
1578 DBG_ASSERT(GetRowCount() == 0,"DbGridControl: without cursor no rows are allowed to be there");
1579 return;
1582 // ignore any implicitly made updates
1583 bool bDisablePaint = !bUpdateCursor && IsPaintEnabled();
1584 if (bDisablePaint)
1585 EnablePaint(false);
1587 // adjust cache to the visible area
1588 Reference< XPropertySet > xSet = m_pSeekCursor->getPropertySet();
1589 sal_Int32 nCacheSize = 0;
1590 xSet->getPropertyValue(FM_PROP_FETCHSIZE) >>= nCacheSize;
1591 bool bCacheAligned = false;
1592 // no further cursor movements after initializing (m_nSeekPos < 0) because it is already
1593 // positioned on the first sentence
1594 tools::Long nDelta = nNewTopRow - GetTopRow();
1595 // limit for relative positioning
1596 tools::Long nLimit = nCacheSize ? nCacheSize / 2 : 0;
1598 // more lines on screen than in cache
1599 if (nLimit < nLinesOnScreen)
1601 Any aCacheSize;
1602 aCacheSize <<= sal_Int32(nLinesOnScreen*2);
1603 xSet->setPropertyValue(FM_PROP_FETCHSIZE, aCacheSize);
1604 // here we need to update the cursor for sure
1605 bUpdateCursor = true;
1606 bCacheAligned = true;
1607 nLimit = nLinesOnScreen;
1610 // In the following, all positionings are done as it is
1611 // ensured that there are enough lines in the data cache
1613 // window goes downwards with less than two windows difference or
1614 // the cache was updated and no rowcount yet
1615 if (nDelta < nLimit && (nDelta > 0
1616 || (bCacheAligned && m_nTotalCount < 0)) )
1617 SeekCursor(nNewTopRow + nLinesOnScreen - 1);
1618 else if (nDelta < 0 && std::abs(nDelta) < nLimit)
1619 SeekCursor(nNewTopRow);
1620 else if (nDelta != 0 || bUpdateCursor)
1621 SeekCursor(nNewTopRow, true);
1623 AdjustRows();
1625 // ignore any updates implicit made
1626 EnablePaint(true);
1629 void DbGridControl::RowInserted(sal_Int32 nRow, sal_Int32 nNumRows, bool bDoPaint)
1631 if (!nNumRows)
1632 return;
1634 if (m_bRecordCountFinal && m_nTotalCount < 0)
1636 // if we have an insert row we have to reduce to count by 1
1637 // as the total count reflects only the existing rows in database
1638 m_nTotalCount = GetRowCount() + nNumRows;
1639 if (m_xEmptyRow.is())
1640 --m_nTotalCount;
1642 else if (m_nTotalCount >= 0)
1643 m_nTotalCount += nNumRows;
1645 EditBrowseBox::RowInserted(nRow, nNumRows, bDoPaint);
1646 m_aBar->InvalidateState(DbGridControlNavigationBarState::Count);
1649 void DbGridControl::RowRemoved(sal_Int32 nRow, sal_Int32 nNumRows, bool bDoPaint)
1651 if (!nNumRows)
1652 return;
1654 if (m_bRecordCountFinal && m_nTotalCount < 0)
1656 m_nTotalCount = GetRowCount() - nNumRows;
1657 // if we have an insert row reduce by 1
1658 if (m_xEmptyRow.is())
1659 --m_nTotalCount;
1661 else if (m_nTotalCount >= 0)
1662 m_nTotalCount -= nNumRows;
1664 EditBrowseBox::RowRemoved(nRow, nNumRows, bDoPaint);
1665 m_aBar->InvalidateState(DbGridControlNavigationBarState::Count);
1668 void DbGridControl::AdjustRows()
1670 if (!m_pSeekCursor)
1671 return;
1673 Reference< XPropertySet > xSet = m_pDataCursor->getPropertySet();
1675 // refresh RecordCount
1676 sal_Int32 nRecordCount = 0;
1677 xSet->getPropertyValue(FM_PROP_ROWCOUNT) >>= nRecordCount;
1678 if (!m_bRecordCountFinal)
1679 m_bRecordCountFinal = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ROWCOUNTFINAL));
1681 // Did the number of rows change?
1682 // Here we need to consider that there might be an additional row for adding new data sets
1684 // add additional AppendRow for insertion
1685 if (m_nOptions & DbGridControlOptions::Insert)
1686 ++nRecordCount;
1688 // If there is currently an insertion, so do not consider this added row in RecordCount or Appendrow
1689 if (!IsUpdating() && m_bRecordCountFinal && IsModified() && m_xCurrentRow != m_xEmptyRow &&
1690 m_xCurrentRow->IsNew())
1691 ++nRecordCount;
1692 // ensured with !m_bUpdating: otherwise the edited data set (that SaveRow added and why this
1693 // method was called) would be called twice (if m_bUpdating == sal_True): once in RecordCount
1694 // and a second time here (60787 - FS)
1696 if (nRecordCount != GetRowCount())
1698 tools::Long nDelta = GetRowCount() - static_cast<tools::Long>(nRecordCount);
1699 if (nDelta > 0) // too many
1701 RowRemoved(GetRowCount() - nDelta, nDelta, false);
1702 // some rows are gone, thus, repaint starting at the current position
1703 Invalidate();
1705 sal_Int32 nNewPos = AlignSeekCursor();
1706 if (m_bSynchDisplay)
1707 EditBrowseBox::GoToRow(nNewPos);
1709 SetCurrent(nNewPos);
1710 // there are rows so go to the selected current column
1711 if (nRecordCount)
1712 GoToRowColumnId(nNewPos, GetColumnId(GetCurColumnId()));
1713 if (!IsResizing() && GetRowCount())
1714 RecalcRows(GetTopRow(), GetVisibleRows(), true);
1715 m_aBar->InvalidateAll(m_nCurrentPos, true);
1717 else // too few
1718 RowInserted(GetRowCount(), -nDelta);
1721 if (m_bRecordCountFinal && m_nTotalCount < 0)
1723 if (m_nOptions & DbGridControlOptions::Insert)
1724 m_nTotalCount = GetRowCount() - 1;
1725 else
1726 m_nTotalCount = GetRowCount();
1728 m_aBar->InvalidateState(DbGridControlNavigationBarState::Count);
1731 svt::EditBrowseBox::RowStatus DbGridControl::GetRowStatus(sal_Int32 nRow) const
1733 if (IsFilterRow(nRow))
1734 return EditBrowseBox::FILTER;
1735 else if (m_nCurrentPos >= 0 && nRow == m_nCurrentPos)
1737 // new row
1738 if (!IsValid(m_xCurrentRow))
1739 return EditBrowseBox::DELETED;
1740 else if (IsModified())
1741 return EditBrowseBox::MODIFIED;
1742 else if (m_xCurrentRow->IsNew())
1743 return EditBrowseBox::CURRENTNEW;
1744 else
1745 return EditBrowseBox::CURRENT;
1747 else if (IsInsertionRow(nRow))
1748 return EditBrowseBox::NEW;
1749 else if (!IsValid(m_xSeekRow))
1750 return EditBrowseBox::DELETED;
1751 else
1752 return EditBrowseBox::CLEAN;
1755 void DbGridControl::PaintCell(OutputDevice& rDev, const tools::Rectangle& rRect, sal_uInt16 nColumnId) const
1757 if (!IsValid(m_xPaintRow))
1758 return;
1760 size_t Location = GetModelColumnPos(nColumnId);
1761 DbGridColumn* pColumn = (Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
1762 if (pColumn)
1764 tools::Rectangle aArea(rRect);
1765 if ((GetMode() & BrowserMode::CURSOR_WO_FOCUS) == BrowserMode::CURSOR_WO_FOCUS)
1767 aArea.AdjustTop(1 );
1768 aArea.AdjustBottom( -1 );
1770 pColumn->Paint(rDev, aArea, m_xPaintRow.get(), getNumberFormatter());
1774 bool DbGridControl::CursorMoving(sal_Int32 nNewRow, sal_uInt16 nNewCol)
1777 DeactivateCell( false );
1779 if ( m_pDataCursor
1780 && ( m_nCurrentPos != nNewRow )
1781 && !SetCurrent( nNewRow )
1784 ActivateCell();
1785 return false;
1788 return EditBrowseBox::CursorMoving( nNewRow, nNewCol );
1791 bool DbGridControl::SetCurrent(sal_Int32 nNewRow)
1793 // Each movement of the datacursor must start with BeginCursorAction and end with
1794 // EndCursorAction to block all notifications during the movement
1795 BeginCursorAction();
1799 // compare positions
1800 if (SeekCursor(nNewRow))
1802 if (IsFilterRow(nNewRow)) // special mode for filtering
1804 m_xCurrentRow = m_xDataRow = m_xPaintRow = m_xEmptyRow;
1805 m_nCurrentPos = nNewRow;
1807 else
1809 bool bNewRowInserted = false;
1810 // Should we go to the insertrow ?
1811 if (IsInsertionRow(nNewRow))
1813 // to we need to move the cursor to the insert row?
1814 // we need to insert the if the current row isn't the insert row or if the
1815 // cursor triggered the move by itself and we need a reinitialization of the row
1816 Reference< XPropertySet > xCursorProps = m_pDataCursor->getPropertySet();
1817 if ( !::comphelper::getBOOL(xCursorProps->getPropertyValue(FM_PROP_ISNEW)) )
1819 Reference< XResultSetUpdate > xUpdateCursor(Reference< XInterface >(*m_pDataCursor), UNO_QUERY);
1820 xUpdateCursor->moveToInsertRow();
1822 bNewRowInserted = true;
1824 else
1827 if ( !m_pSeekCursor->isBeforeFirst() && !m_pSeekCursor->isAfterLast() )
1829 Any aBookmark = m_pSeekCursor->getBookmark();
1830 if (!m_xCurrentRow.is() || m_xCurrentRow->IsNew() || !CompareBookmark(aBookmark, m_pDataCursor->getBookmark()))
1832 // adjust the cursor to the new desired row
1833 if (!m_pDataCursor->moveToBookmark(aBookmark))
1835 EndCursorAction();
1836 return false;
1841 m_xDataRow->SetState(m_pDataCursor.get(), false);
1842 m_xCurrentRow = m_xDataRow;
1844 tools::Long nPaintPos = -1;
1845 // do we have to repaint the last regular row in case of setting defaults or autovalues
1846 if (m_nCurrentPos >= 0 && m_nCurrentPos >= (GetRowCount() - 2))
1847 nPaintPos = m_nCurrentPos;
1849 m_nCurrentPos = nNewRow;
1851 // repaint the new row to display all defaults
1852 if (bNewRowInserted)
1853 RowModified(m_nCurrentPos);
1854 if (nPaintPos >= 0)
1855 RowModified(nPaintPos);
1858 else
1860 OSL_FAIL("DbGridControl::SetCurrent : SeekRow failed !");
1861 EndCursorAction();
1862 return false;
1865 catch ( const Exception& )
1867 DBG_UNHANDLED_EXCEPTION("svx");
1868 EndCursorAction();
1869 return false;
1872 EndCursorAction();
1873 return true;
1876 void DbGridControl::CursorMoved()
1879 // cursor movement due to deletion or insertion of rows
1880 if (m_pDataCursor && m_nCurrentPos != GetCurRow())
1882 DeactivateCell();
1883 SetCurrent(GetCurRow());
1886 EditBrowseBox::CursorMoved();
1887 m_aBar->InvalidateAll(m_nCurrentPos);
1889 // select the new column when they moved
1890 if ( IsDesignMode() && GetSelectedColumnCount() > 0 && GetCurColumnId() )
1892 SelectColumnId( GetCurColumnId() );
1895 if ( m_nLastColId != GetCurColumnId() )
1896 onColumnChange();
1897 m_nLastColId = GetCurColumnId();
1899 if ( m_nLastRowId != GetCurRow() )
1900 onRowChange();
1901 m_nLastRowId = GetCurRow();
1904 void DbGridControl::onRowChange()
1906 // not interested in
1909 void DbGridControl::onColumnChange()
1911 if ( m_pGridListener )
1912 m_pGridListener->columnChanged();
1915 void DbGridControl::setDisplaySynchron(bool bSync)
1917 if (bSync != m_bSynchDisplay)
1919 m_bSynchDisplay = bSync;
1920 if (m_bSynchDisplay)
1921 AdjustDataSource();
1925 void DbGridControl::AdjustDataSource(bool bFull)
1927 SAL_INFO("svx.fmcomp", "DbGridControl::AdjustDataSource");
1928 SolarMutexGuard aGuard;
1929 // If the current row is recalculated at the moment, do not adjust
1931 if (bFull)
1932 m_xCurrentRow = nullptr;
1933 // if we are on the same row only repaint
1934 // but this is only possible for rows which are not inserted, in that case the comparison result
1935 // may not be correct
1936 else
1937 if ( m_xCurrentRow.is()
1938 && !m_xCurrentRow->IsNew()
1939 && !m_pDataCursor->isBeforeFirst()
1940 && !m_pDataCursor->isAfterLast()
1941 && !m_pDataCursor->rowDeleted()
1944 bool bEqualBookmarks = CompareBookmark( m_xCurrentRow->GetBookmark(), m_pDataCursor->getBookmark() );
1946 bool bDataCursorIsOnNew = false;
1947 m_pDataCursor->getPropertySet()->getPropertyValue( FM_PROP_ISNEW ) >>= bDataCursorIsOnNew;
1949 if ( bEqualBookmarks && !bDataCursorIsOnNew )
1951 // position of my data cursor is the same as the position our current row points tpo
1952 // sync the status, repaint, done
1953 DBG_ASSERT(m_xDataRow == m_xCurrentRow, "Errors in the data row");
1954 SAL_INFO("svx.fmcomp", "same position, new state: " << ROWSTATUS(m_xCurrentRow));
1955 RowModified(m_nCurrentPos);
1956 return;
1960 // away from the data cursor's row
1961 if (m_xPaintRow == m_xCurrentRow)
1962 m_xPaintRow = m_xSeekRow;
1964 // not up-to-date row, thus, adjust completely
1965 if (!m_xCurrentRow.is())
1966 AdjustRows();
1968 sal_Int32 nNewPos = AlignSeekCursor();
1969 if (nNewPos < 0)// could not find any position
1970 return;
1972 if (nNewPos != m_nCurrentPos)
1974 if (m_bSynchDisplay)
1975 EditBrowseBox::GoToRow(nNewPos);
1977 if (!m_xCurrentRow.is())
1978 // Happens e.g. when deleting the n last datasets (n>1) while the cursor was positioned
1979 // on the last one. In this case, AdjustRows deletes two rows from BrowseBox, by what
1980 // CurrentRow is corrected to point two rows down, so that GoToRow will point into
1981 // emptiness (since we are - purportedly - at the correct position)
1982 SetCurrent(nNewPos);
1984 else
1986 SetCurrent(nNewPos);
1987 RowModified(nNewPos);
1990 // if the data cursor was moved from outside, this section is voided
1991 SetNoSelection();
1992 m_aBar->InvalidateAll(m_nCurrentPos, m_xCurrentRow.is());
1995 sal_Int32 DbGridControl::AlignSeekCursor()
1997 // position SeekCursor onto the data cursor, no data transmission
1999 if (!m_pSeekCursor)
2000 return -1;
2002 Reference< XPropertySet > xSet = m_pDataCursor->getPropertySet();
2004 // now align the seek cursor and the data cursor
2005 if (::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISNEW)))
2006 m_nSeekPos = GetRowCount() - 1;
2007 else
2011 if ( m_pDataCursor->isBeforeFirst() )
2013 // this is somewhat strange, but can nevertheless happen
2014 SAL_INFO( "svx.fmcomp", "DbGridControl::AlignSeekCursor: nobody should tamper with my cursor this way (before first)!" );
2015 m_pSeekCursor->first();
2016 m_pSeekCursor->previous();
2017 m_nSeekPos = -1;
2019 else if ( m_pDataCursor->isAfterLast() )
2021 SAL_INFO( "svx.fmcomp", "DbGridControl::AlignSeekCursor: nobody should tamper with my cursor this way (after last)!" );
2022 m_pSeekCursor->last();
2023 m_pSeekCursor->next();
2024 m_nSeekPos = -1;
2026 else
2028 m_pSeekCursor->moveToBookmark(m_pDataCursor->getBookmark());
2029 if (!CompareBookmark(m_pDataCursor->getBookmark(), m_pSeekCursor->getBookmark()))
2030 // unfortunately, moveToBookmark might lead to a re-positioning of the seek
2031 // cursor (if the complex moveToBookmark with all its events fires an update
2032 // somewhere) -> retry
2033 m_pSeekCursor->moveToBookmark(m_pDataCursor->getBookmark());
2034 // Now there is still the chance of a failure but it is less likely.
2035 // The alternative would be a loop until everything is fine - no good solution...
2036 m_nSeekPos = m_pSeekCursor->getRow() - 1;
2039 catch(Exception&)
2043 return m_nSeekPos;
2046 bool DbGridControl::SeekCursor(sal_Int32 nRow, bool bAbsolute)
2048 // position SeekCursor onto the data cursor, no data transmission
2050 // additions for the filtermode
2051 if (IsFilterRow(nRow))
2053 m_nSeekPos = 0;
2054 return true;
2057 if (!m_pSeekCursor)
2058 return false;
2060 // is this an insertion?
2061 if (IsValid(m_xCurrentRow) && m_xCurrentRow->IsNew() &&
2062 nRow >= m_nCurrentPos)
2064 // if so, scrolling down must be prevented as this is already the last data set!
2065 if (nRow == m_nCurrentPos)
2067 // no adjustment necessary
2068 m_nSeekPos = nRow;
2070 else if (IsInsertionRow(nRow)) // blank row for data insertion
2071 m_nSeekPos = nRow;
2073 else if (IsInsertionRow(nRow)) // blank row for data insertion
2074 m_nSeekPos = nRow;
2075 else if ((-1 == nRow) && (GetRowCount() == ((m_nOptions & DbGridControlOptions::Insert) ? 1 : 0)) && m_pSeekCursor->isAfterLast())
2076 m_nSeekPos = nRow;
2077 else
2079 bool bSuccess = false;
2080 tools::Long nSteps = 0;
2083 if ( m_pSeekCursor->rowDeleted() )
2085 // somebody deleted the current row of the seek cursor. Move it away from this row.
2086 m_pSeekCursor->next();
2087 if ( m_pSeekCursor->isAfterLast() || m_pSeekCursor->isBeforeFirst() )
2088 bAbsolute = true;
2091 if ( !bAbsolute )
2093 DBG_ASSERT( !m_pSeekCursor->isAfterLast() && !m_pSeekCursor->isBeforeFirst(),
2094 "DbGridControl::SeekCursor: how did the seek cursor get to this position?!" );
2095 nSteps = nRow - (m_pSeekCursor->getRow() - 1);
2096 bAbsolute = std::abs(nSteps) > 100;
2099 if ( bAbsolute )
2101 bSuccess = m_pSeekCursor->absolute(nRow + 1);
2102 if (bSuccess)
2103 m_nSeekPos = nRow;
2105 else
2107 if (nSteps > 0) // position onto the last needed data set
2109 if (m_pSeekCursor->isAfterLast())
2110 bSuccess = false;
2111 else if (m_pSeekCursor->isBeforeFirst())
2112 bSuccess = m_pSeekCursor->absolute(nSteps);
2113 else
2114 bSuccess = m_pSeekCursor->relative(nSteps);
2116 else if (nSteps < 0)
2118 if (m_pSeekCursor->isBeforeFirst())
2119 bSuccess = false;
2120 else if (m_pSeekCursor->isAfterLast())
2121 bSuccess = m_pSeekCursor->absolute(nSteps);
2122 else
2123 bSuccess = m_pSeekCursor->relative(nSteps);
2125 else
2127 m_nSeekPos = nRow;
2128 return true;
2132 catch(Exception&)
2134 OSL_FAIL("DbGridControl::SeekCursor : failed ...");
2139 if (!bSuccess)
2141 if (bAbsolute || nSteps > 0)
2143 if (m_pSeekCursor->isLast())
2144 bSuccess = true;
2145 else
2146 bSuccess = m_pSeekCursor->last();
2148 else
2150 if (m_pSeekCursor->isFirst())
2151 bSuccess = true;
2152 else
2153 bSuccess = m_pSeekCursor->first();
2157 if (bSuccess)
2158 m_nSeekPos = m_pSeekCursor->getRow() - 1;
2159 else
2160 m_nSeekPos = -1;
2162 catch(Exception&)
2164 DBG_UNHANDLED_EXCEPTION("svx");
2165 OSL_FAIL("DbGridControl::SeekCursor : failed ...");
2166 m_nSeekPos = -1; // no further data set available
2169 return m_nSeekPos == nRow;
2172 void DbGridControl::MoveToFirst()
2174 if (m_pSeekCursor && (GetCurRow() != 0))
2175 MoveToPosition(0);
2178 void DbGridControl::MoveToLast()
2180 if (!m_pSeekCursor)
2181 return;
2183 if (m_nTotalCount < 0) // no RecordCount, yet
2187 bool bRes = m_pSeekCursor->last();
2189 if (bRes)
2191 m_nSeekPos = m_pSeekCursor->getRow() - 1;
2192 AdjustRows();
2195 catch(Exception&)
2200 // position onto the last data set not on a blank row
2201 if (m_nOptions & DbGridControlOptions::Insert)
2203 if ((GetRowCount() - 1) > 0)
2204 MoveToPosition(GetRowCount() - 2);
2206 else if (GetRowCount())
2207 MoveToPosition(GetRowCount() - 1);
2210 void DbGridControl::MoveToPrev()
2212 sal_Int32 nNewRow = std::max(GetCurRow() - 1, sal_Int32(0));
2213 if (GetCurRow() != nNewRow)
2214 MoveToPosition(nNewRow);
2217 void DbGridControl::MoveToNext()
2219 if (!m_pSeekCursor)
2220 return;
2222 if (m_nTotalCount > 0)
2224 // move the data cursor to the right position
2225 tools::Long nNewRow = std::min(GetRowCount() - 1, GetCurRow() + 1);
2226 if (GetCurRow() != nNewRow)
2227 MoveToPosition(nNewRow);
2229 else
2231 bool bOk = false;
2234 // try to move to next row
2235 // when not possible our paint cursor is already on the last row
2236 // then we must be sure that the data cursor is on the position
2237 // we call ourself again
2238 bOk = m_pSeekCursor->next();
2239 if (bOk)
2241 m_nSeekPos = m_pSeekCursor->getRow() - 1;
2242 MoveToPosition(GetCurRow() + 1);
2245 catch(SQLException &)
2247 DBG_UNHANDLED_EXCEPTION("svx");
2250 if(!bOk)
2252 AdjustRows();
2253 if (m_nTotalCount > 0) // only to avoid infinite recursion
2254 MoveToNext();
2259 void DbGridControl::MoveToPosition(sal_uInt32 nPos)
2261 if (!m_pSeekCursor)
2262 return;
2264 if (m_nTotalCount < 0 && static_cast<tools::Long>(nPos) >= GetRowCount())
2268 if (!m_pSeekCursor->absolute(nPos + 1))
2270 AdjustRows();
2271 return;
2273 else
2275 m_nSeekPos = m_pSeekCursor->getRow() - 1;
2276 AdjustRows();
2279 catch(Exception&)
2281 return;
2284 EditBrowseBox::GoToRow(nPos);
2285 m_aBar->InvalidateAll(m_nCurrentPos);
2288 void DbGridControl::AppendNew()
2290 if (!m_pSeekCursor || !(m_nOptions & DbGridControlOptions::Insert))
2291 return;
2293 if (m_nTotalCount < 0) // no RecordCount, yet
2297 bool bRes = m_pSeekCursor->last();
2299 if (bRes)
2301 m_nSeekPos = m_pSeekCursor->getRow() - 1;
2302 AdjustRows();
2305 catch(Exception&)
2307 return;
2311 tools::Long nNewRow = m_nTotalCount + 1;
2312 if (nNewRow > 0 && GetCurRow() != nNewRow)
2313 MoveToPosition(nNewRow - 1);
2316 void DbGridControl::SetDesignMode(bool bMode)
2318 if (IsDesignMode() == bMode)
2319 return;
2321 // adjust Enable/Disable for design mode so that the headerbar remains configurable
2322 if (bMode)
2324 if (!IsEnabled())
2326 Enable();
2327 GetDataWindow().Disable();
2330 else
2332 // disable completely
2333 if (!GetDataWindow().IsEnabled())
2334 Disable();
2337 m_bDesignMode = bMode;
2338 GetDataWindow().SetMouseTransparent(bMode);
2339 SetMouseTransparent(bMode);
2341 m_aBar->InvalidateAll(m_nCurrentPos, true);
2344 void DbGridControl::SetFilterMode(bool bMode)
2346 if (IsFilterMode() == bMode)
2347 return;
2349 m_bFilterMode = bMode;
2351 if (bMode)
2353 SetUpdateMode(false);
2355 // there is no cursor anymore
2356 if (IsEditing())
2357 DeactivateCell();
2358 RemoveRows(false);
2360 m_xEmptyRow = new DbGridRow();
2362 // setting the new filter controls
2363 for (auto const & pCurCol : m_aColumns)
2365 if (!pCurCol->IsHidden())
2366 pCurCol->UpdateControl();
2369 // one row for filtering
2370 RowInserted(0);
2371 SetUpdateMode(true);
2373 else
2374 setDataSource(Reference< XRowSet > ());
2377 OUString DbGridControl::GetCellText(sal_Int32 _nRow, sal_uInt16 _nColId) const
2379 size_t Location = GetModelColumnPos( _nColId );
2380 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
2381 OUString sRet;
2382 if ( const_cast<DbGridControl*>(this)->SeekRow(_nRow) )
2383 sRet = GetCurrentRowCellText(pColumn, m_xPaintRow);
2384 return sRet;
2387 OUString DbGridControl::GetCurrentRowCellText(DbGridColumn const * pColumn,const DbGridRowRef& _rRow) const
2389 // text output for a single row
2390 OUString aText;
2391 if ( pColumn && IsValid(_rRow) )
2392 aText = pColumn->GetCellText(_rRow.get(), m_xFormatter);
2393 return aText;
2396 sal_uInt32 DbGridControl::GetTotalCellWidth(sal_Int32 nRow, sal_uInt16 nColId)
2398 if (SeekRow(nRow))
2400 size_t Location = GetModelColumnPos( nColId );
2401 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
2402 return GetDataWindow().GetTextWidth(GetCurrentRowCellText(pColumn,m_xPaintRow));
2404 else
2405 return 30; // FIXME magic number for default cell width
2408 void DbGridControl::PreExecuteRowContextMenu(weld::Menu& rMenu)
2410 bool bDelete = (m_nOptions & DbGridControlOptions::Delete) && GetSelectRowCount() && !IsCurrentAppending();
2411 // if only a blank row is selected then do not delete
2412 bDelete = bDelete && !((m_nOptions & DbGridControlOptions::Insert) && GetSelectRowCount() == 1 && IsRowSelected(GetRowCount() - 1));
2414 rMenu.set_visible("delete", bDelete);
2415 rMenu.set_visible("save", IsModified());
2417 // the undo is more difficult
2418 bool bCanUndo = IsModified();
2419 int nState = -1;
2420 if (m_aMasterStateProvider.IsSet())
2421 nState = m_aMasterStateProvider.Call(DbGridControlNavigationBarState::Undo);
2422 bCanUndo &= ( 0 != nState );
2424 rMenu.set_visible("undo", bCanUndo);
2427 void DbGridControl::PostExecuteRowContextMenu(const OString& rExecutionResult)
2429 if (rExecutionResult == "delete")
2431 // delete asynchronously
2432 if (m_nDeleteEvent)
2433 Application::RemoveUserEvent(m_nDeleteEvent);
2434 m_nDeleteEvent = Application::PostUserEvent(LINK(this,DbGridControl,OnDelete), nullptr, true);
2436 else if (rExecutionResult == "undo")
2437 Undo();
2438 else if (rExecutionResult == "save")
2439 SaveRow();
2442 void DbGridControl::DataSourcePropertyChanged(const PropertyChangeEvent& evt)
2444 SAL_INFO("svx.fmcomp", "DbGridControl::DataSourcePropertyChanged");
2445 SolarMutexGuard aGuard;
2446 // prop "IsModified" changed ?
2447 // during update don't care about the modified state
2448 if (IsUpdating() || evt.PropertyName != FM_PROP_ISMODIFIED)
2449 return;
2451 Reference< XPropertySet > xSource(evt.Source, UNO_QUERY);
2452 DBG_ASSERT( xSource.is(), "DbGridControl::DataSourcePropertyChanged: invalid event source!" );
2453 bool bIsNew = false;
2454 if (xSource.is())
2455 bIsNew = ::comphelper::getBOOL(xSource->getPropertyValue(FM_PROP_ISNEW));
2457 if (bIsNew && m_xCurrentRow.is())
2459 DBG_ASSERT(::comphelper::getBOOL(xSource->getPropertyValue(FM_PROP_ROWCOUNTFINAL)), "DbGridControl::DataSourcePropertyChanged : somebody moved the form to a new record before the row count was final !");
2460 sal_Int32 nRecordCount = 0;
2461 xSource->getPropertyValue(FM_PROP_ROWCOUNT) >>= nRecordCount;
2462 if (::comphelper::getBOOL(evt.NewValue))
2463 { // modified state changed from sal_False to sal_True and we're on an insert row
2464 // -> we've to add a new grid row
2465 if ((nRecordCount == GetRowCount() - 1) && m_xCurrentRow->IsNew())
2467 RowInserted(GetRowCount());
2468 InvalidateStatusCell(m_nCurrentPos);
2469 m_aBar->InvalidateAll(m_nCurrentPos);
2472 else
2473 { // modified state changed from sal_True to sal_False and we're on an insert row
2474 // we have two "new row"s at the moment : the one we're editing currently (where the current
2475 // column is the only dirty element) and a "new new" row which is completely clean. As the first
2476 // one is about to be cleaned, too, the second one is obsolete now.
2477 if (m_xCurrentRow->IsNew() && nRecordCount == (GetRowCount() - 2))
2479 RowRemoved(GetRowCount() - 1);
2480 InvalidateStatusCell(m_nCurrentPos);
2481 m_aBar->InvalidateAll(m_nCurrentPos);
2485 if (m_xCurrentRow.is())
2487 m_xCurrentRow->SetStatus(::comphelper::getBOOL(evt.NewValue) ? GridRowStatus::Modified : GridRowStatus::Clean);
2488 m_xCurrentRow->SetNew( bIsNew );
2489 InvalidateStatusCell(m_nCurrentPos);
2490 SAL_INFO("svx.fmcomp", "modified flag changed, new state: " << ROWSTATUS(m_xCurrentRow));
2494 void DbGridControl::StartDrag( sal_Int8 /*nAction*/, const Point& rPosPixel )
2496 if (!m_pSeekCursor || IsResizing())
2497 return;
2499 sal_uInt16 nColId = GetColumnId(GetColumnAtXPosPixel(rPosPixel.X()));
2500 tools::Long nRow = GetRowAtYPosPixel(rPosPixel.Y());
2501 if (nColId != HandleColumnId && nRow >= 0)
2503 if (GetDataWindow().IsMouseCaptured())
2504 GetDataWindow().ReleaseMouse();
2506 size_t Location = GetModelColumnPos( nColId );
2507 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
2508 rtl::Reference<OStringTransferable> pTransferable = new OStringTransferable(GetCurrentRowCellText(pColumn,m_xPaintRow));
2509 pTransferable->StartDrag(this, DND_ACTION_COPY);
2513 bool DbGridControl::canCopyCellText(sal_Int32 _nRow, sal_uInt16 _nColId)
2515 return (_nRow >= 0)
2516 && (_nRow < GetRowCount())
2517 && (_nColId != HandleColumnId)
2518 && (GetModelColumnPos(_nColId) != GRID_COLUMN_NOT_FOUND);
2521 void DbGridControl::copyCellText(sal_Int32 _nRow, sal_uInt16 _nColId)
2523 DBG_ASSERT(canCopyCellText(_nRow, _nColId), "DbGridControl::copyCellText: invalid call!");
2524 DbGridColumn* pColumn = m_aColumns[ GetModelColumnPos(_nColId) ].get();
2525 SeekRow(_nRow);
2526 OStringTransfer::CopyString( GetCurrentRowCellText( pColumn,m_xPaintRow ), this );
2529 void DbGridControl::executeRowContextMenu(const Point& _rPreferredPos)
2531 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(nullptr, "svx/ui/rowsmenu.ui"));
2532 std::unique_ptr<weld::Menu> xContextMenu(xBuilder->weld_menu("menu"));
2534 tools::Rectangle aRect(_rPreferredPos, Size(1,1));
2535 weld::Window* pParent = weld::GetPopupParent(*this, aRect);
2537 PreExecuteRowContextMenu(*xContextMenu);
2538 PostExecuteRowContextMenu(xContextMenu->popup_at_rect(pParent, aRect));
2541 void DbGridControl::Command(const CommandEvent& rEvt)
2543 switch (rEvt.GetCommand())
2545 case CommandEventId::ContextMenu:
2547 if ( !m_pSeekCursor )
2549 EditBrowseBox::Command(rEvt);
2550 return;
2553 if ( !rEvt.IsMouseEvent() )
2554 { // context menu requested by keyboard
2555 if ( GetSelectRowCount() )
2557 tools::Long nRow = FirstSelectedRow( );
2559 ::tools::Rectangle aRowRect( GetRowRectPixel( nRow ) );
2560 executeRowContextMenu(aRowRect.LeftCenter());
2562 // handled
2563 return;
2567 sal_uInt16 nColId = GetColumnId(GetColumnAtXPosPixel(rEvt.GetMousePosPixel().X()));
2568 tools::Long nRow = GetRowAtYPosPixel(rEvt.GetMousePosPixel().Y());
2570 if (nColId == HandleColumnId)
2572 executeRowContextMenu(rEvt.GetMousePosPixel());
2574 else if (canCopyCellText(nRow, nColId))
2576 ::tools::Rectangle aRect(rEvt.GetMousePosPixel(), Size(1, 1));
2577 weld::Window* pPopupParent = weld::GetPopupParent(*this, aRect);
2578 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(pPopupParent, "svx/ui/cellmenu.ui"));
2579 std::unique_ptr<weld::Menu> xContextMenu(xBuilder->weld_menu("menu"));
2580 if (!xContextMenu->popup_at_rect(pPopupParent, aRect).isEmpty())
2581 copyCellText(nRow, nColId);
2583 else
2585 EditBrowseBox::Command(rEvt);
2586 return;
2589 [[fallthrough]];
2591 default:
2592 EditBrowseBox::Command(rEvt);
2596 IMPL_LINK_NOARG(DbGridControl, OnDelete, void*, void)
2598 m_nDeleteEvent = nullptr;
2599 DeleteSelectedRows();
2602 void DbGridControl::DeleteSelectedRows()
2604 DBG_ASSERT(GetSelection(), "no selection!!!");
2606 if (!m_pSeekCursor)
2607 return;
2610 CellController* DbGridControl::GetController(sal_Int32 /*nRow*/, sal_uInt16 nColumnId)
2612 if (!IsValid(m_xCurrentRow) || !IsEnabled())
2613 return nullptr;
2615 size_t Location = GetModelColumnPos(nColumnId);
2616 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
2617 if (!pColumn)
2618 return nullptr;
2620 CellController* pReturn = nullptr;
2621 if (IsFilterMode())
2622 pReturn = pColumn->GetController().get();
2623 else
2625 if (::comphelper::hasProperty(FM_PROP_ENABLED, pColumn->getModel()))
2627 if (!::comphelper::getBOOL(pColumn->getModel()->getPropertyValue(FM_PROP_ENABLED)))
2628 return nullptr;
2631 bool bInsert = (m_xCurrentRow->IsNew() && (m_nOptions & DbGridControlOptions::Insert));
2632 bool bUpdate = (!m_xCurrentRow->IsNew() && (m_nOptions & DbGridControlOptions::Update));
2634 if ((bInsert && !pColumn->IsAutoValue()) || bUpdate)
2636 pReturn = pColumn->GetController().get();
2639 return pReturn;
2642 void DbGridControl::CellModified()
2644 SAL_INFO("svx.fmcomp", "DbGridControl::CellModified");
2647 ::osl::MutexGuard aGuard(m_aAdjustSafety);
2648 if (m_nAsynAdjustEvent)
2650 SAL_INFO("svx.fmcomp", "forcing a synchron call to " << (m_bPendingAdjustRows ? "AdjustRows" : "AdustDataSource"));
2651 RemoveUserEvent(m_nAsynAdjustEvent);
2652 m_nAsynAdjustEvent = nullptr;
2654 // force the call : this should be no problem as we're probably running in the solar thread here
2655 // (cell modified is triggered by user actions)
2656 if (m_bPendingAdjustRows)
2657 AdjustRows();
2658 else
2659 AdjustDataSource();
2663 if (IsFilterMode() || !IsValid(m_xCurrentRow) || m_xCurrentRow->IsModified())
2664 return;
2666 // enable edit mode
2667 // a data set should be inserted
2668 if (m_xCurrentRow->IsNew())
2670 m_xCurrentRow->SetStatus(GridRowStatus::Modified);
2671 SAL_INFO("svx.fmcomp", "current row is new, new state: MODIFIED");
2672 // if no row was added yet, do it now
2673 if (m_nCurrentPos == GetRowCount() - 1)
2675 // increment RowCount
2676 RowInserted(GetRowCount());
2677 InvalidateStatusCell(m_nCurrentPos);
2678 m_aBar->InvalidateAll(m_nCurrentPos);
2681 else if (m_xCurrentRow->GetStatus() != GridRowStatus::Modified)
2683 m_xCurrentRow->SetState(m_pDataCursor.get(), false);
2684 SAL_INFO("svx.fmcomp", "current row is not new, after SetState, new state: " << ROWSTATUS(m_xCurrentRow));
2685 m_xCurrentRow->SetStatus(GridRowStatus::Modified);
2686 SAL_INFO("svx.fmcomp", "current row is not new, new state: MODIFIED");
2687 InvalidateStatusCell(m_nCurrentPos);
2691 void DbGridControl::Dispatch(sal_uInt16 nId)
2693 if (nId == BROWSER_CURSORENDOFFILE)
2695 if (m_nOptions & DbGridControlOptions::Insert)
2696 AppendNew();
2697 else
2698 MoveToLast();
2700 else
2701 EditBrowseBox::Dispatch(nId);
2704 void DbGridControl::Undo()
2706 if (IsFilterMode() || !IsValid(m_xCurrentRow) || !IsModified())
2707 return;
2709 // check if we have somebody doin' the UNDO for us
2710 int nState = -1;
2711 if (m_aMasterStateProvider.IsSet())
2712 nState = m_aMasterStateProvider.Call(DbGridControlNavigationBarState::Undo);
2713 if (nState>0)
2714 { // yes, we have, and the slot is enabled
2715 DBG_ASSERT(m_aMasterSlotExecutor.IsSet(), "DbGridControl::Undo : a state, but no execute link ?");
2716 bool lResult = m_aMasterSlotExecutor.Call(DbGridControlNavigationBarState::Undo);
2717 if (lResult)
2718 // handled
2719 return;
2721 else if (nState == 0)
2722 // yes, we have, and the slot is disabled
2723 return;
2725 BeginCursorAction();
2727 bool bAppending = m_xCurrentRow->IsNew();
2728 bool bDirty = m_xCurrentRow->IsModified();
2732 // cancel editing
2733 Reference< XResultSetUpdate > xUpdateCursor(Reference< XInterface >(*m_pDataCursor), UNO_QUERY);
2734 // no effects if we're not updating currently
2735 if (bAppending)
2736 // just refresh the row
2737 xUpdateCursor->moveToInsertRow();
2738 else
2739 xUpdateCursor->cancelRowUpdates();
2742 catch(Exception&)
2744 DBG_UNHANDLED_EXCEPTION("svx");
2747 EndCursorAction();
2749 m_xDataRow->SetState(m_pDataCursor.get(), false);
2750 if (m_xPaintRow == m_xCurrentRow)
2751 m_xPaintRow = m_xCurrentRow = m_xDataRow;
2752 else
2753 m_xCurrentRow = m_xDataRow;
2755 if (bAppending && (EditBrowseBox::IsModified() || bDirty))
2756 // remove the row
2757 if (m_nCurrentPos == GetRowCount() - 2)
2758 { // maybe we already removed it (in resetCurrentRow, called if the above moveToInsertRow
2759 // caused our data source form to be reset - which should be the usual case...)
2760 RowRemoved(GetRowCount() - 1);
2761 m_aBar->InvalidateAll(m_nCurrentPos);
2764 RowModified(m_nCurrentPos);
2767 void DbGridControl::resetCurrentRow()
2769 if (IsModified())
2771 // scenario : we're on the insert row, the row is dirty, and thus there exists a "second" insert row (which
2772 // is clean). Normally in DataSourcePropertyChanged we would remove this second row if the modified state of
2773 // the insert row changes from sal_True to sal_False. But if our current cell is the only modified element (means the
2774 // data source isn't modified) and we're reset this DataSourcePropertyChanged would never be called, so we
2775 // would never delete the obsolete "second insert row". Thus in this special case this method here
2776 // is the only possibility to determine the redundance of the row (resetCurrentRow is called when the
2777 // "first insert row" is about to be cleaned, so of course the "second insert row" is redundant now)
2778 Reference< XPropertySet > xDataSource = getDataSource()->getPropertySet();
2779 if (xDataSource.is() && !::comphelper::getBOOL(xDataSource->getPropertyValue(FM_PROP_ISMODIFIED)))
2781 // are we on a new row currently ?
2782 if (m_xCurrentRow->IsNew())
2784 if (m_nCurrentPos == GetRowCount() - 2)
2786 RowRemoved(GetRowCount() - 1);
2787 m_aBar->InvalidateAll(m_nCurrentPos);
2792 // update the rows
2793 m_xDataRow->SetState(m_pDataCursor.get(), false);
2794 if (m_xPaintRow == m_xCurrentRow)
2795 m_xPaintRow = m_xCurrentRow = m_xDataRow;
2796 else
2797 m_xCurrentRow = m_xDataRow;
2800 RowModified(GetCurRow()); // will update the current controller if affected
2803 void DbGridControl::RowModified( sal_Int32 nRow )
2805 if (nRow == m_nCurrentPos && IsEditing())
2807 CellControllerRef aTmpRef = Controller();
2808 aTmpRef->SaveValue();
2809 InitController(aTmpRef, m_nCurrentPos, GetCurColumnId());
2811 EditBrowseBox::RowModified(nRow);
2814 bool DbGridControl::IsModified() const
2816 return !IsFilterMode() && IsValid(m_xCurrentRow) && (m_xCurrentRow->IsModified() || EditBrowseBox::IsModified());
2819 bool DbGridControl::IsCurrentAppending() const
2821 return m_xCurrentRow.is() && m_xCurrentRow->IsNew();
2824 bool DbGridControl::IsInsertionRow(sal_Int32 nRow) const
2826 return (m_nOptions & DbGridControlOptions::Insert) && m_nTotalCount >= 0 && (nRow == GetRowCount() - 1);
2829 bool DbGridControl::SaveModified()
2831 SAL_INFO("svx.fmcomp", "DbGridControl::SaveModified");
2832 DBG_ASSERT(IsValid(m_xCurrentRow), "GridControl:: Invalid row");
2833 if (!IsValid(m_xCurrentRow))
2834 return true;
2836 // accept input for this field
2837 // Where there changes at the current input field?
2838 if (!EditBrowseBox::IsModified())
2839 return true;
2841 size_t Location = GetModelColumnPos( GetCurColumnId() );
2842 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
2843 bool bOK = pColumn && pColumn->Commit();
2844 DBG_ASSERT( Controller().is(), "DbGridControl::SaveModified: was modified, by have no controller?!" );
2845 if ( !Controller().is() )
2846 // this might happen if the callbacks implicitly triggered by Commit
2847 // fiddled with the form or the control ...
2848 // (Note that this here is a workaround, at most. We need a general concept how
2849 // to treat this, you can imagine an arbitrary number of scenarios where a callback
2850 // triggers something which leaves us in an expected state.)
2851 // #i67147# / 2006-07-17 / frank.schoenheit@sun.com
2852 return bOK;
2854 if (bOK)
2856 Controller()->SaveValue();
2858 if ( IsValid(m_xCurrentRow) )
2860 m_xCurrentRow->SetState(m_pDataCursor.get(), false);
2861 SAL_INFO("svx.fmcomp", "explicit SetState, new state: " << ROWSTATUS(m_xCurrentRow));
2862 InvalidateStatusCell( m_nCurrentPos );
2864 else
2866 SAL_INFO("svx.fmcomp", "no SetState, new state: " << ROWSTATUS(m_xCurrentRow));
2870 return bOK;
2873 bool DbGridControl::SaveRow()
2875 SAL_INFO("svx.fmcomp", "DbGridControl::SaveRow");
2876 // valid row
2877 if (!IsValid(m_xCurrentRow) || !IsModified())
2878 return true;
2879 // value of the controller was not saved, yet
2880 else if (Controller().is() && Controller()->IsValueChangedFromSaved())
2882 if (!SaveModified())
2883 return false;
2885 m_bUpdating = true;
2887 BeginCursorAction();
2888 bool bAppending = m_xCurrentRow->IsNew();
2889 bool bSuccess = false;
2892 Reference< XResultSetUpdate > xUpdateCursor(Reference< XInterface >(*m_pDataCursor), UNO_QUERY);
2893 if (bAppending)
2894 xUpdateCursor->insertRow();
2895 else
2896 xUpdateCursor->updateRow();
2897 bSuccess = true;
2899 catch(SQLException&)
2901 EndCursorAction();
2902 m_bUpdating = false;
2903 return false;
2908 if (bSuccess)
2910 // if we are appending we still sit on the insert row
2911 // we don't move just clear the flags not to move on the current row
2912 m_xCurrentRow->SetState(m_pDataCursor.get(), false);
2913 SAL_INFO("svx.fmcomp", "explicit SetState after a successful update, new state: " << ROWSTATUS(m_xCurrentRow));
2914 m_xCurrentRow->SetNew(false);
2916 // adjust the seekcursor if it is on the same position as the datacursor
2917 if (m_nSeekPos == m_nCurrentPos || bAppending)
2919 // get the bookmark to refetch the data
2920 // in insert mode we take the new bookmark of the data cursor
2921 Any aBookmark = bAppending ? m_pDataCursor->getBookmark() : m_pSeekCursor->getBookmark();
2922 m_pSeekCursor->moveToBookmark(aBookmark);
2923 // get the data
2924 m_xSeekRow->SetState(m_pSeekCursor.get(), true);
2925 m_nSeekPos = m_pSeekCursor->getRow() - 1;
2928 // and repaint the row
2929 RowModified(m_nCurrentPos);
2931 catch(Exception&)
2935 m_bUpdating = false;
2936 EndCursorAction();
2938 // The old code returned (nRecords != 0) here.
2939 // Me thinks this is wrong : If something goes wrong while update the record, an exception will be thrown,
2940 // which results in a "return sal_False" (see above). If no exception is thrown, everything is fine. If nRecords
2941 // is zero, this simply means all fields had their original values.
2942 // FS - 06.12.99 - 70502
2943 return true;
2946 bool DbGridControl::PreNotify(NotifyEvent& rEvt)
2948 // do not handle events of the Navbar
2949 if (m_aBar->IsWindowOrChild(rEvt.GetWindow()))
2950 return BrowseBox::PreNotify(rEvt);
2952 switch (rEvt.GetType())
2954 case MouseNotifyEvent::KEYINPUT:
2956 const KeyEvent* pKeyEvent = rEvt.GetKeyEvent();
2958 sal_uInt16 nCode = pKeyEvent->GetKeyCode().GetCode();
2959 bool bShift = pKeyEvent->GetKeyCode().IsShift();
2960 bool bCtrl = pKeyEvent->GetKeyCode().IsMod1();
2961 bool bAlt = pKeyEvent->GetKeyCode().IsMod2();
2962 if ( ( KEY_TAB == nCode ) && bCtrl && !bAlt )
2964 // Ctrl-Tab is used to step out of the control, without traveling to the
2965 // remaining cells first
2966 // -> build a new key event without the Ctrl-key, and let the very base class handle it
2967 vcl::KeyCode aNewCode( KEY_TAB, bShift, false, false, false );
2968 KeyEvent aNewEvent( pKeyEvent->GetCharCode(), aNewCode );
2970 // call the Control - our direct base class will interpret this in a way we do not want (and do
2971 // a cell traveling)
2972 Control::KeyInput( aNewEvent );
2973 return true;
2976 if ( !bShift && !bCtrl && ( KEY_ESCAPE == nCode ) )
2978 if (IsModified())
2980 Undo();
2981 return true;
2984 else if ( ( KEY_DELETE == nCode ) && !bShift && !bCtrl ) // delete rows
2986 if ((m_nOptions & DbGridControlOptions::Delete) && GetSelectRowCount())
2988 // delete asynchronously
2989 if (m_nDeleteEvent)
2990 Application::RemoveUserEvent(m_nDeleteEvent);
2991 m_nDeleteEvent = Application::PostUserEvent(LINK(this,DbGridControl,OnDelete), nullptr, true);
2992 return true;
2996 [[fallthrough]];
2998 default:
2999 return EditBrowseBox::PreNotify(rEvt);
3003 bool DbGridControl::IsTabAllowed(bool bRight) const
3005 if (bRight)
3006 // Tab only if not on the _last_ row
3007 return GetCurRow() < (GetRowCount() - 1) || !m_bRecordCountFinal ||
3008 GetViewColumnPos(GetCurColumnId()) < (GetViewColCount() - 1);
3009 else
3011 // Tab only if not on the _first_ row
3012 return GetCurRow() > 0 || (GetCurColumnId() && GetViewColumnPos(GetCurColumnId()) > 0);
3016 void DbGridControl::KeyInput( const KeyEvent& rEvt )
3018 if (rEvt.GetKeyCode().GetFunction() == KeyFuncType::COPY)
3020 tools::Long nRow = GetCurRow();
3021 sal_uInt16 nColId = GetCurColumnId();
3022 if (nRow >= 0 && nRow < GetRowCount() && nColId < ColCount())
3024 size_t Location = GetModelColumnPos( nColId );
3025 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
3026 OStringTransfer::CopyString( GetCurrentRowCellText( pColumn, m_xCurrentRow ), this );
3027 return;
3030 EditBrowseBox::KeyInput(rEvt);
3033 void DbGridControl::HideColumn(sal_uInt16 nId)
3035 DeactivateCell();
3037 // determine the col for the focus to set to after removal
3038 sal_uInt16 nPos = GetViewColumnPos(nId);
3039 sal_uInt16 nNewColId = nPos == (ColCount()-1)
3040 ? GetColumnIdFromViewPos(nPos-1) // last col is to be removed -> take the previous
3041 : GetColumnIdFromViewPos(nPos+1); // take the next
3043 tools::Long lCurrentWidth = GetColumnWidth(nId);
3044 EditBrowseBox::RemoveColumn(nId);
3045 // don't use my own RemoveColumn, this would remove it from m_aColumns, too
3047 // update my model
3048 size_t Location = GetModelColumnPos( nId );
3049 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
3050 DBG_ASSERT(pColumn, "DbGridControl::HideColumn : somebody did hide a nonexistent column !");
3051 if (pColumn)
3053 pColumn->m_bHidden = true;
3054 pColumn->m_nLastVisibleWidth = CalcReverseZoom(lCurrentWidth);
3057 // and reset the focus
3058 if ( nId == GetCurColumnId() )
3059 GoToColumnId( nNewColId );
3062 void DbGridControl::ShowColumn(sal_uInt16 nId)
3064 sal_uInt16 nPos = GetModelColumnPos(nId);
3065 DBG_ASSERT(nPos != GRID_COLUMN_NOT_FOUND, "DbGridControl::ShowColumn : invalid argument !");
3066 if (nPos == GRID_COLUMN_NOT_FOUND)
3067 return;
3069 DbGridColumn* pColumn = m_aColumns[ nPos ].get();
3070 if (!pColumn->IsHidden())
3072 DBG_ASSERT(GetViewColumnPos(nId) != GRID_COLUMN_NOT_FOUND, "DbGridControl::ShowColumn : inconsistent internal state !");
3073 // if the column isn't marked as hidden, it should be visible, shouldn't it ?
3074 return;
3076 DBG_ASSERT(GetViewColumnPos(nId) == GRID_COLUMN_NOT_FOUND, "DbGridControl::ShowColumn : inconsistent internal state !");
3077 // the opposite situation ...
3079 // to determine the new view position we need an adjacent non-hidden column
3080 sal_uInt16 nNextNonHidden = BROWSER_INVALIDID;
3081 // first search the cols to the right
3082 for ( size_t i = nPos + 1; i < m_aColumns.size(); ++i )
3084 DbGridColumn* pCurCol = m_aColumns[ i ].get();
3085 if (!pCurCol->IsHidden())
3087 nNextNonHidden = i;
3088 break;
3091 if ((nNextNonHidden == BROWSER_INVALIDID) && (nPos > 0))
3093 // then to the left
3094 for ( size_t i = nPos; i > 0; --i )
3096 DbGridColumn* pCurCol = m_aColumns[ i-1 ].get();
3097 if (!pCurCol->IsHidden())
3099 nNextNonHidden = i-1;
3100 break;
3104 sal_uInt16 nNewViewPos = (nNextNonHidden == BROWSER_INVALIDID)
3105 ? 1 // there is no visible column -> insert behind the handle col
3106 : GetViewColumnPos( m_aColumns[ nNextNonHidden ]->GetId() ) + 1;
3107 // the first non-handle col has "view pos" 0, but the pos arg for InsertDataColumn expects
3108 // a position 1 for the first non-handle col -> +1
3109 DBG_ASSERT(nNewViewPos != GRID_COLUMN_NOT_FOUND, "DbGridControl::ShowColumn : inconsistent internal state !");
3110 // we found a col marked as visible but got no view pos for it ...
3112 if ((nNextNonHidden<nPos) && (nNextNonHidden != BROWSER_INVALIDID))
3113 // nNextNonHidden is a column to the left, so we want to insert the new col _right_ beside it's pos
3114 ++nNewViewPos;
3116 DeactivateCell();
3118 OUString aName;
3119 pColumn->getModel()->getPropertyValue(FM_PROP_LABEL) >>= aName;
3120 InsertDataColumn(nId, aName, CalcZoom(pColumn->m_nLastVisibleWidth), HeaderBarItemBits::CENTER | HeaderBarItemBits::CLICKABLE, nNewViewPos);
3121 pColumn->m_bHidden = false;
3123 ActivateCell();
3124 Invalidate();
3127 sal_uInt16 DbGridControl::GetColumnIdFromModelPos( sal_uInt16 nPos ) const
3129 if (nPos >= m_aColumns.size())
3131 OSL_FAIL("DbGridControl::GetColumnIdFromModelPos : invalid argument !");
3132 return GRID_COLUMN_NOT_FOUND;
3135 DbGridColumn* pCol = m_aColumns[ nPos ].get();
3136 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
3137 // in the debug version, we convert the ModelPos into a ViewPos and compare this with the
3138 // value we will return (nId at the corresponding Col in m_aColumns)
3140 if (!pCol->IsHidden())
3141 { // makes sense only if the column is visible
3142 sal_uInt16 nViewPos = nPos;
3143 for ( size_t i = 0; i < m_aColumns.size() && i < nPos; ++i)
3144 if ( m_aColumns[ i ]->IsHidden())
3145 --nViewPos;
3147 DBG_ASSERT(pCol && GetColumnIdFromViewPos(nViewPos) == pCol->GetId(),
3148 "DbGridControl::GetColumnIdFromModelPos : this isn't consistent... did I misunderstand something ?");
3150 #endif
3151 return pCol->GetId();
3154 sal_uInt16 DbGridControl::GetModelColumnPos( sal_uInt16 nId ) const
3156 for ( size_t i = 0; i < m_aColumns.size(); ++i )
3157 if ( m_aColumns[ i ]->GetId() == nId )
3158 return i;
3160 return GRID_COLUMN_NOT_FOUND;
3163 void DbGridControl::implAdjustInSolarThread(bool _bRows)
3165 SAL_INFO("svx.fmcomp", "DbGridControl::implAdjustInSolarThread");
3166 ::osl::MutexGuard aGuard(m_aAdjustSafety);
3167 if (!Application::IsMainThread())
3169 m_nAsynAdjustEvent = PostUserEvent(LINK(this, DbGridControl, OnAsyncAdjust), reinterpret_cast< void* >( _bRows ), true);
3170 m_bPendingAdjustRows = _bRows;
3171 if (_bRows)
3172 SAL_INFO("svx.fmcomp", "posting an AdjustRows");
3173 else
3174 SAL_INFO("svx.fmcomp", "posting an AdjustDataSource");
3176 else
3178 if (_bRows)
3179 SAL_INFO("svx.fmcomp", "doing an AdjustRows");
3180 else
3181 SAL_INFO("svx.fmcomp", "doing an AdjustDataSource");
3182 // always adjust the rows before adjusting the data source
3183 // If this is not necessary (because the row count did not change), nothing is done
3184 // The problem is that we can't rely on the order of which the calls come in: If the cursor was moved
3185 // to a position behind row count know 'til now, the cursorMoved notification may come before the
3186 // RowCountChanged notification
3187 // 94093 - 02.11.2001 - frank.schoenheit@sun.com
3188 AdjustRows();
3190 if ( !_bRows )
3191 AdjustDataSource();
3195 IMPL_LINK(DbGridControl, OnAsyncAdjust, void*, pAdjustWhat, void)
3197 m_nAsynAdjustEvent = nullptr;
3199 AdjustRows();
3200 // see implAdjustInSolarThread for a comment why we do this every time
3202 if ( !pAdjustWhat )
3203 AdjustDataSource();
3206 void DbGridControl::BeginCursorAction()
3208 if (m_pFieldListeners)
3210 ColumnFieldValueListeners* pListeners = static_cast<ColumnFieldValueListeners*>(m_pFieldListeners);
3211 for (const auto& rListener : *pListeners)
3213 GridFieldValueListener* pCurrent = rListener.second;
3214 if (pCurrent)
3215 pCurrent->suspend();
3219 if (m_pDataSourcePropListener)
3220 m_pDataSourcePropListener->suspend();
3223 void DbGridControl::EndCursorAction()
3225 if (m_pFieldListeners)
3227 ColumnFieldValueListeners* pListeners = static_cast<ColumnFieldValueListeners*>(m_pFieldListeners);
3228 for (const auto& rListener : *pListeners)
3230 GridFieldValueListener* pCurrent = rListener.second;
3231 if (pCurrent)
3232 pCurrent->resume();
3236 if (m_pDataSourcePropListener)
3237 m_pDataSourcePropListener->resume();
3240 void DbGridControl::ConnectToFields()
3242 ColumnFieldValueListeners* pListeners = static_cast<ColumnFieldValueListeners*>(m_pFieldListeners);
3243 DBG_ASSERT(!pListeners || pListeners->empty(), "DbGridControl::ConnectToFields : please call DisconnectFromFields first !");
3245 if (!pListeners)
3247 pListeners = new ColumnFieldValueListeners;
3248 m_pFieldListeners = pListeners;
3251 for (auto const & pCurrent : m_aColumns)
3253 sal_uInt16 nViewPos = pCurrent ? GetViewColumnPos(pCurrent->GetId()) : GRID_COLUMN_NOT_FOUND;
3254 if (GRID_COLUMN_NOT_FOUND == nViewPos)
3255 continue;
3257 Reference< XPropertySet > xField = pCurrent->GetField();
3258 if (!xField.is())
3259 continue;
3261 // column is visible and bound here
3262 GridFieldValueListener*& rpListener = (*pListeners)[pCurrent->GetId()];
3263 DBG_ASSERT(!rpListener, "DbGridControl::ConnectToFields : already a listener for this column ?!");
3264 rpListener = new GridFieldValueListener(*this, xField, pCurrent->GetId());
3268 void DbGridControl::DisconnectFromFields()
3270 if (!m_pFieldListeners)
3271 return;
3273 ColumnFieldValueListeners* pListeners = static_cast<ColumnFieldValueListeners*>(m_pFieldListeners);
3274 while (!pListeners->empty())
3276 sal_Int32 nOldSize = pListeners->size();
3277 pListeners->begin()->second->dispose();
3278 DBG_ASSERT(nOldSize > static_cast<sal_Int32>(pListeners->size()), "DbGridControl::DisconnectFromFields : dispose on a listener should result in a removal from my list !");
3281 delete pListeners;
3282 m_pFieldListeners = nullptr;
3285 void DbGridControl::FieldValueChanged(sal_uInt16 _nId)
3287 osl::MutexGuard aPreventDestruction(m_aDestructionSafety);
3288 // needed as this may run in a thread other than the main one
3289 if (GetRowStatus(GetCurRow()) != EditBrowseBox::MODIFIED)
3290 // all other cases are handled elsewhere
3291 return;
3293 size_t Location = GetModelColumnPos( _nId );
3294 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
3295 if (!pColumn)
3296 return;
3298 std::unique_ptr<vcl::SolarMutexTryAndBuyGuard> pGuard;
3299 while (!m_bWantDestruction && (!pGuard || !pGuard->isAcquired()))
3300 pGuard.reset(new vcl::SolarMutexTryAndBuyGuard);
3302 if (m_bWantDestruction)
3303 { // at this moment, within another thread, our destructor tries to destroy the listener which called this method
3304 // => don't do anything
3305 // 73365 - 23.02.00 - FS
3306 return;
3309 // and finally do the update ...
3310 pColumn->UpdateFromField(m_xCurrentRow.get(), m_xFormatter);
3311 RowModified(GetCurRow());
3314 void DbGridControl::FieldListenerDisposing(sal_uInt16 _nId)
3316 ColumnFieldValueListeners* pListeners = static_cast<ColumnFieldValueListeners*>(m_pFieldListeners);
3317 if (!pListeners)
3319 OSL_FAIL("DbGridControl::FieldListenerDisposing : invalid call (have no listener array) !");
3320 return;
3323 ColumnFieldValueListeners::const_iterator aPos = pListeners->find(_nId);
3324 if (aPos == pListeners->end())
3326 OSL_FAIL("DbGridControl::FieldListenerDisposing : invalid call (did not find the listener) !");
3327 return;
3330 delete aPos->second;
3332 pListeners->erase(aPos);
3335 void DbGridControl::disposing(sal_uInt16 _nId)
3337 if (_nId == 0)
3338 { // the seek cursor is being disposed
3339 ::osl::MutexGuard aGuard(m_aAdjustSafety);
3340 setDataSource(nullptr, DbGridControlOptions::Readonly); // our clone was disposed so we set our datasource to null to avoid later access to it
3341 if (m_nAsynAdjustEvent)
3343 RemoveUserEvent(m_nAsynAdjustEvent);
3344 m_nAsynAdjustEvent = nullptr;
3349 sal_Int32 DbGridControl::GetAccessibleControlCount() const
3351 return EditBrowseBox::GetAccessibleControlCount() + 1; // the navigation control
3354 Reference<XAccessible > DbGridControl::CreateAccessibleControl( sal_Int32 _nIndex )
3356 Reference<XAccessible > xRet;
3357 if ( _nIndex == EditBrowseBox::GetAccessibleControlCount() )
3359 xRet = m_aBar->GetAccessible();
3361 else
3362 xRet = EditBrowseBox::CreateAccessibleControl( _nIndex );
3363 return xRet;
3366 Reference< XAccessible > DbGridControl::CreateAccessibleCell( sal_Int32 _nRow, sal_uInt16 _nColumnPos )
3368 sal_uInt16 nColumnId = GetColumnId( _nColumnPos );
3369 size_t Location = GetModelColumnPos(nColumnId);
3370 DbGridColumn* pColumn = ( Location < m_aColumns.size() ) ? m_aColumns[ Location ].get() : nullptr;
3371 if ( pColumn )
3373 Reference< css::awt::XControl> xInt(pColumn->GetCell());
3374 Reference< css::awt::XCheckBox> xBox(xInt,UNO_QUERY);
3375 if ( xBox.is() )
3377 TriState eValue = TRISTATE_FALSE;
3378 switch( xBox->getState() )
3380 case 0:
3381 eValue = TRISTATE_FALSE;
3382 break;
3383 case 1:
3384 eValue = TRISTATE_TRUE;
3385 break;
3386 case 2:
3387 eValue = TRISTATE_INDET;
3388 break;
3390 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow, _nColumnPos,eValue );
3393 return EditBrowseBox::CreateAccessibleCell( _nRow, _nColumnPos );
3396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */