Bump version to 4.3-4
[LibreOffice.git] / cui / source / options / connpooloptions.cxx
blobfcb0ca90288bfbda61ee9973920071a6a1b5d980
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 "connpooloptions.hxx"
21 #include <svtools/editbrowsebox.hxx>
22 #include <vcl/field.hxx>
23 #include "connpoolsettings.hxx"
24 #include <svl/eitem.hxx>
25 #include <cuires.hrc>
26 #include "helpid.hrc"
27 #include <dialmgr.hxx>
29 using ::svt::EditBrowseBox;
31 namespace offapp
33 /// Widget for the Connection Pool options page
34 class DriverListControl : public EditBrowseBox
36 using Window::Update;
37 protected:
38 DriverPoolingSettings m_aSavedSettings;
39 DriverPoolingSettings m_aSettings;
40 DriverPoolingSettings::const_iterator m_aSeekRow;
42 OUString m_sYes;
43 OUString m_sNo;
45 Link m_aRowChangeHandler;
47 public:
48 DriverListControl(Window* _pParent);
50 virtual void Init() SAL_OVERRIDE;
51 void Update(const DriverPoolingSettings& _rSettings);
52 virtual OUString GetCellText( long nRow, sal_uInt16 nColId ) const SAL_OVERRIDE;
54 // the handler will be called with a DriverPoolingSettings::const_iterator as parameter,
55 // or NULL if no valid current row exists
56 void SetRowChangeHandler(const Link& _rHdl) { m_aRowChangeHandler = _rHdl; }
58 DriverPooling* getCurrentRow();
59 void updateCurrentRow();
61 const DriverPoolingSettings& getSettings() const { return m_aSettings; }
63 void saveValue() { m_aSavedSettings = m_aSettings; }
64 bool isModified() const;
66 protected:
67 virtual void InitController( ::svt::CellControllerRef& rController, long nRow, sal_uInt16 nCol ) SAL_OVERRIDE;
68 virtual ::svt::CellController* GetController( long nRow, sal_uInt16 nCol ) SAL_OVERRIDE;
70 virtual void PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColId ) const SAL_OVERRIDE;
72 virtual bool SeekRow( long nRow ) SAL_OVERRIDE;
73 virtual bool SaveModified() SAL_OVERRIDE;
75 virtual bool IsTabAllowed(bool _bForward) const SAL_OVERRIDE;
77 virtual void StateChanged( StateChangedType nStateChange ) SAL_OVERRIDE;
79 virtual void CursorMoved() SAL_OVERRIDE;
81 protected:
82 virtual sal_uInt32 GetTotalCellWidth(long nRow, sal_uInt16 nColId) SAL_OVERRIDE;
85 private:
86 OUString implGetCellText(DriverPoolingSettings::const_iterator _rPos, sal_uInt16 _nColId) const;
90 DriverListControl::DriverListControl(Window* _pParent)
91 :EditBrowseBox(_pParent, EBBF_NOROWPICTURE, BROWSER_AUTO_VSCROLL | BROWSER_AUTO_HSCROLL | BROWSER_HIDECURSOR | BROWSER_AUTOSIZE_LASTCOL | WB_BORDER)
92 ,m_aSeekRow(m_aSettings.end())
93 ,m_sYes(CUI_RES(RID_SVXSTR_YES))
94 ,m_sNo(CUI_RES(RID_SVXSTR_NO))
96 SetStyle((GetStyle() & ~WB_HSCROLL) | WB_AUTOHSCROLL);
98 SetUniqueId(UID_OFA_CONNPOOL_DRIVERLIST_BACK);
101 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeDriverListControl(Window *pParent, VclBuilder::stringmap &)
103 return new DriverListControl(pParent);
107 bool DriverListControl::IsTabAllowed(bool /*_bForward*/) const
109 // no travelling within the fields via RETURN and TAB
110 return false;
114 bool DriverListControl::isModified() const
116 if (m_aSettings.size() != m_aSavedSettings.size())
117 return true;
119 DriverPoolingSettings::const_iterator aCurrent = m_aSettings.begin();
120 DriverPoolingSettings::const_iterator aCurrentEnd = m_aSettings.end();
121 DriverPoolingSettings::const_iterator aSaved = m_aSavedSettings.begin();
122 for (;aCurrent != aCurrentEnd; ++aCurrent, ++aSaved)
124 if (*aCurrent != *aSaved)
125 return true;
128 return false;
132 void DriverListControl::Init()
134 EditBrowseBox::Init();
136 Size aColWidth = LogicToPixel(Size(160, 0), MAP_APPFONT);
137 InsertDataColumn(1, OUString(CUI_RES(RID_SVXSTR_DRIVER_NAME)), aColWidth.Width());
138 aColWidth = LogicToPixel(Size(30, 0), MAP_APPFONT);
139 InsertDataColumn(2, OUString(CUI_RES(RID_SVXSTR_POOLED_FLAG)), aColWidth.Width());
140 aColWidth = LogicToPixel(Size(60, 0), MAP_APPFONT);
141 InsertDataColumn(3, OUString(CUI_RES(RID_SVXSTR_POOL_TIMEOUT)), aColWidth.Width());
142 // Attention: the resource of the string is local to the resource of the enclosing dialog!
146 void DriverListControl::CursorMoved()
148 EditBrowseBox::CursorMoved();
150 // call the row change handler
151 if ( m_aRowChangeHandler.IsSet() )
153 if ( GetCurRow() >= 0 )
154 { // == -1 may happen in case the browse box has just been cleared
155 m_aRowChangeHandler.Call( getCurrentRow() );
160 DriverPooling* DriverListControl::getCurrentRow()
162 OSL_ENSURE( ( GetCurRow() < m_aSettings.size() ) && ( GetCurRow() >= 0 ),
163 "DriverListControl::getCurrentRow: invalid current row!");
165 if ( ( GetCurRow() >= 0 ) && ( GetCurRow() < m_aSettings.size() ) )
166 return &(*(m_aSettings.begin() + GetCurRow()));
168 return NULL;
172 void DriverListControl::updateCurrentRow()
174 Window::Invalidate( GetRowRectPixel( GetCurRow() ), INVALIDATE_UPDATE );
178 void DriverListControl::Update(const DriverPoolingSettings& _rSettings)
180 m_aSettings = _rSettings;
182 SetUpdateMode(false);
183 RowRemoved(0, GetRowCount());
184 RowInserted(0, m_aSettings.size());
185 SetUpdateMode(true);
187 ActivateCell(1, 0);
191 sal_uInt32 DriverListControl::GetTotalCellWidth(long nRow, sal_uInt16 nColId)
193 return GetDataWindow().GetTextWidth(GetCellText(nRow, nColId));
197 OUString DriverListControl::implGetCellText(DriverPoolingSettings::const_iterator _rPos, sal_uInt16 _nColId) const
199 OSL_ENSURE(_rPos < m_aSettings.end(), "DriverListControl::implGetCellText: invalid position!");
201 OUString sReturn;
202 switch (_nColId)
204 case 1:
205 sReturn = _rPos->sName;
206 break;
207 case 2:
208 sReturn = _rPos->bEnabled ? m_sYes : m_sNo;
209 break;
210 case 3:
211 if (_rPos->bEnabled)
212 sReturn = OUString::number(_rPos->nTimeoutSeconds);
213 break;
214 default:
215 OSL_FAIL("DriverListControl::implGetCellText: invalid column id!");
217 return sReturn;
221 void DriverListControl::StateChanged( StateChangedType nStateChange )
223 if (STATE_CHANGE_ENABLE == nStateChange)
224 Window::Invalidate(INVALIDATE_UPDATE);
225 EditBrowseBox::StateChanged( nStateChange );
229 OUString DriverListControl::GetCellText( long nRow, sal_uInt16 nColId ) const
231 OUString sReturn;
232 if (nRow > m_aSettings.size())
234 OSL_FAIL("DriverListControl::GetCellText: don't ask me for such rows!");
236 else
238 sReturn = implGetCellText(m_aSettings.begin() + nRow, nColId);
240 return sReturn;
244 void DriverListControl::InitController( ::svt::CellControllerRef& rController, long nRow, sal_uInt16 nCol )
246 rController->GetWindow().SetText(GetCellText(nRow, nCol));
250 ::svt::CellController* DriverListControl::GetController( long /*nRow*/, sal_uInt16 /*nCol*/ )
252 return NULL;
256 bool DriverListControl::SaveModified()
258 return true;
262 bool DriverListControl::SeekRow( long _nRow )
264 EditBrowseBox::SeekRow(_nRow);
266 if (_nRow < m_aSettings.size())
267 m_aSeekRow = m_aSettings.begin() + _nRow;
268 else
269 m_aSeekRow = m_aSettings.end();
271 return m_aSeekRow != m_aSettings.end();
275 void DriverListControl::PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColId ) const
277 OSL_ENSURE(m_aSeekRow != m_aSettings.end(), "DriverListControl::PaintCell: invalid row!");
279 if (m_aSeekRow != m_aSettings.end())
281 rDev.SetClipRegion(Region(rRect));
283 sal_uInt16 nStyle = TEXT_DRAW_CLIP;
284 if (!IsEnabled())
285 nStyle |= TEXT_DRAW_DISABLE;
286 switch (nColId)
288 case 1: nStyle |= TEXT_DRAW_LEFT; break;
289 case 2:
290 case 3: nStyle |= TEXT_DRAW_CENTER; break;
293 rDev.DrawText(rRect, implGetCellText(m_aSeekRow, nColId), nStyle);
295 rDev.SetClipRegion();
300 //= ConnectionPoolOptionsPage
303 ConnectionPoolOptionsPage::ConnectionPoolOptionsPage(Window* _pParent, const SfxItemSet& _rAttrSet)
304 : SfxTabPage(_pParent, "ConnPoolPage", "cui/ui/connpooloptions.ui", _rAttrSet)
306 get(m_pEnablePooling, "connectionpooling");
307 get(m_pDriversLabel, "driverslabel");
308 get(m_pDriverList, "driverlist");
309 get(m_pDriverLabel, "driverlabel");
310 get(m_pDriver, "driver");
311 get(m_pDriverPoolingEnabled, "enablepooling");
312 get(m_pTimeoutLabel, "timeoutlabel");
313 get(m_pTimeout, "timeout");
315 Size aControlSize(248, 100);
316 aControlSize = LogicToPixel(aControlSize, MAP_APPFONT);
317 m_pDriverList->set_width_request(aControlSize.Width());
318 m_pDriverList->set_height_request(aControlSize.Height());
319 m_pDriverList->Init();
320 m_pDriverList->Show();
322 m_pEnablePooling->SetClickHdl( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
323 m_pDriverPoolingEnabled->SetClickHdl( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
325 m_pDriverList->SetRowChangeHandler( LINK(this, ConnectionPoolOptionsPage, OnDriverRowChanged) );
329 SfxTabPage* ConnectionPoolOptionsPage::Create(Window* _pParent, const SfxItemSet& _rAttrSet)
331 return new ConnectionPoolOptionsPage(_pParent, _rAttrSet);
335 void ConnectionPoolOptionsPage::implInitControls(const SfxItemSet& _rSet, bool /*_bFromReset*/)
337 // the enabled flag
338 SFX_ITEMSET_GET( _rSet, pEnabled, SfxBoolItem, SID_SB_POOLING_ENABLED, true );
339 OSL_ENSURE(pEnabled, "ConnectionPoolOptionsPage::implInitControls: missing the Enabled item!");
340 m_pEnablePooling->Check(pEnabled ? pEnabled->GetValue() : sal_True);
342 m_pEnablePooling->SaveValue();
344 // the settings for the single drivers
345 SFX_ITEMSET_GET( _rSet, pDriverSettings, DriverPoolingSettingsItem, SID_SB_DRIVER_TIMEOUTS, true );
346 if (pDriverSettings)
347 m_pDriverList->Update(pDriverSettings->getSettings());
348 else
350 OSL_FAIL("ConnectionPoolOptionsPage::implInitControls: missing the DriverTimeouts item!");
351 m_pDriverList->Update(DriverPoolingSettings());
353 m_pDriverList->saveValue();
355 // reflect the new settings
356 OnEnabledDisabled(m_pEnablePooling);
360 bool ConnectionPoolOptionsPage::Notify( NotifyEvent& _rNEvt )
362 if (EVENT_LOSEFOCUS == _rNEvt.GetType())
363 if (m_pTimeout->IsWindowOrChild(_rNEvt.GetWindow()))
364 commitTimeoutField();
366 return SfxTabPage::Notify(_rNEvt);
370 bool ConnectionPoolOptionsPage::FillItemSet(SfxItemSet& _rSet)
372 commitTimeoutField();
374 bool bModified = false;
375 // the enabled flag
376 if (m_pEnablePooling->IsValueChangedFromSaved())
378 _rSet.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, m_pEnablePooling->IsChecked()), SID_SB_POOLING_ENABLED);
379 bModified = true;
382 // the settings for the single drivers
383 if (m_pDriverList->isModified())
385 _rSet.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, m_pDriverList->getSettings()), SID_SB_DRIVER_TIMEOUTS);
386 bModified = true;
389 return bModified;
393 void ConnectionPoolOptionsPage::ActivatePage( const SfxItemSet& _rSet)
395 SfxTabPage::ActivatePage(_rSet);
396 implInitControls(_rSet, false);
400 void ConnectionPoolOptionsPage::Reset(const SfxItemSet& _rSet)
402 implInitControls(_rSet, true);
406 IMPL_LINK( ConnectionPoolOptionsPage, OnDriverRowChanged, const void*, _pRowIterator )
408 bool bValidRow = (NULL != _pRowIterator);
409 m_pDriverPoolingEnabled->Enable(bValidRow && m_pEnablePooling->IsChecked());
410 m_pTimeoutLabel->Enable(bValidRow);
411 m_pTimeout->Enable(bValidRow);
413 if (!bValidRow)
414 { // positioned on an invalid row
415 m_pDriver->SetText(OUString());
417 else
419 const DriverPooling *pDriverPos = static_cast<const DriverPooling*>(_pRowIterator);
421 m_pDriver->SetText(pDriverPos->sName);
422 m_pDriverPoolingEnabled->Check(pDriverPos->bEnabled);
423 m_pTimeout->SetText(OUString::number(pDriverPos->nTimeoutSeconds));
425 OnEnabledDisabled(m_pDriverPoolingEnabled);
428 return 0L;
432 void ConnectionPoolOptionsPage::commitTimeoutField()
434 if (DriverPooling* pCurrentDriver = m_pDriverList->getCurrentRow())
436 pCurrentDriver->nTimeoutSeconds = static_cast<long>(m_pTimeout->GetValue());
437 m_pDriverList->updateCurrentRow();
442 IMPL_LINK( ConnectionPoolOptionsPage, OnEnabledDisabled, const CheckBox*, _pCheckBox )
444 bool bGloballyEnabled = m_pEnablePooling->IsChecked();
445 bool bLocalDriverChanged = m_pDriverPoolingEnabled == _pCheckBox;
447 if (m_pEnablePooling == _pCheckBox)
449 m_pDriversLabel->Enable(bGloballyEnabled);
450 m_pDriverList->Enable(bGloballyEnabled);
451 m_pDriverLabel->Enable(bGloballyEnabled);
452 m_pDriver->Enable(bGloballyEnabled);
453 m_pDriverPoolingEnabled->Enable(bGloballyEnabled);
455 else
456 OSL_ENSURE(bLocalDriverChanged, "ConnectionPoolOptionsPage::OnEnabledDisabled: where did this come from?");
458 m_pTimeoutLabel->Enable(bGloballyEnabled && m_pDriverPoolingEnabled->IsChecked());
459 m_pTimeout->Enable(bGloballyEnabled && m_pDriverPoolingEnabled->IsChecked());
461 if (bLocalDriverChanged)
463 // update the list
464 m_pDriverList->getCurrentRow()->bEnabled = m_pDriverPoolingEnabled->IsChecked();
465 m_pDriverList->updateCurrentRow();
468 return 0L;
471 } // namespace offapp
473 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */