update credits
[LibreOffice.git] / cui / source / options / connpooloptions.cxx
blob0745f8f9ef96dc94cb141fefddce72fe3bd156cf
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 "connpooloptions.hrc"
22 #include <svtools/editbrowsebox.hxx>
23 #include <vcl/field.hxx>
24 #include "connpoolsettings.hxx"
25 #include <svl/eitem.hxx>
26 #include <cuires.hrc>
27 #include "helpid.hrc"
28 #include <dialmgr.hxx>
30 //........................................................................
31 namespace offapp
33 //........................................................................
35 //====================================================================
36 //= DriverListControl
37 //====================================================================
38 typedef ::svt::EditBrowseBox DriverListControl_Base;
39 class DriverListControl : public DriverListControl_Base
41 using Window::Update;
42 protected:
43 DriverPoolingSettings m_aSavedSettings;
44 DriverPoolingSettings m_aSettings;
45 DriverPoolingSettings::const_iterator m_aSeekRow;
47 String m_sYes;
48 String m_sNo;
50 Link m_aRowChangeHandler;
52 public:
53 DriverListControl( Window* _pParent, const ResId& _rId);
55 virtual void Init();
56 void Update(const DriverPoolingSettings& _rSettings);
57 virtual String GetCellText( long nRow, sal_uInt16 nColId ) const;
59 // the handler will be called with a DriverPoolingSettings::const_iterator as parameter,
60 // or NULL if no valid current row exists
61 void SetRowChangeHandler(const Link& _rHdl) { m_aRowChangeHandler = _rHdl; }
62 Link GetRowChangeHandler() const { return m_aRowChangeHandler; }
64 const DriverPooling* getCurrentRow() const;
65 DriverPooling* getCurrentRow();
66 void updateCurrentRow();
68 const DriverPoolingSettings& getSettings() const { return m_aSettings; }
70 void saveValue() { m_aSavedSettings = m_aSettings; }
71 sal_Bool isModified() const;
73 protected:
74 virtual void InitController( ::svt::CellControllerRef& rController, long nRow, sal_uInt16 nCol );
75 virtual ::svt::CellController* GetController( long nRow, sal_uInt16 nCol );
77 virtual void PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColId ) const;
79 virtual sal_Bool SeekRow( long nRow );
80 virtual sal_Bool SaveModified();
82 virtual sal_Bool IsTabAllowed(sal_Bool _bForward) const;
84 virtual void StateChanged( StateChangedType nStateChange );
86 virtual void CursorMoved();
88 protected:
89 virtual sal_uInt32 GetTotalCellWidth(long nRow, sal_uInt16 nColId);
92 private:
93 String implGetCellText(DriverPoolingSettings::const_iterator _rPos, sal_uInt16 _nColId) const;
96 //--------------------------------------------------------------------
97 DriverListControl::DriverListControl( Window* _pParent, const ResId& _rId)
98 :DriverListControl_Base(_pParent, _rId, EBBF_NOROWPICTURE, BROWSER_AUTO_VSCROLL | BROWSER_AUTO_HSCROLL | BROWSER_HIDECURSOR | BROWSER_AUTOSIZE_LASTCOL)
99 ,m_aSeekRow(m_aSettings.end())
100 ,m_sYes(ResId(STR_YES,*_rId.GetResMgr()))
101 ,m_sNo(ResId(STR_NO,*_rId.GetResMgr()))
103 SetStyle((GetStyle() & ~WB_HSCROLL) | WB_AUTOHSCROLL);
105 SetUniqueId(UID_OFA_CONNPOOL_DRIVERLIST_BACK);
106 GetDataWindow().SetHelpId(HID_OFA_CONNPOOL_DRIVERLIST);
109 //--------------------------------------------------------------------
110 sal_Bool DriverListControl::IsTabAllowed(sal_Bool /*_bForward*/) const
112 // no travinling within the fields via RETURN and TAB
113 return sal_False;
116 //--------------------------------------------------------------------
117 sal_Bool DriverListControl::isModified() const
119 if (m_aSettings.size() != m_aSavedSettings.size())
120 return sal_True;
122 DriverPoolingSettings::const_iterator aCurrent = m_aSettings.begin();
123 DriverPoolingSettings::const_iterator aCurrentEnd = m_aSettings.end();
124 DriverPoolingSettings::const_iterator aSaved = m_aSavedSettings.begin();
125 for (;aCurrent != aCurrentEnd; ++aCurrent, ++aSaved)
127 if (*aCurrent != *aSaved)
128 return sal_True;
131 return sal_False;
134 //--------------------------------------------------------------------
135 void DriverListControl::Init()
137 DriverListControl_Base::Init();
139 Size aColWidth = LogicToPixel(Size(160, 0), MAP_APPFONT);
140 InsertDataColumn(1, String(CUI_RES(STR_DRIVER_NAME)), aColWidth.Width());
141 aColWidth = LogicToPixel(Size(30, 0), MAP_APPFONT);
142 InsertDataColumn(2, String(CUI_RES(STR_POOLED_FLAG)), aColWidth.Width());
143 aColWidth = LogicToPixel(Size(60, 0), MAP_APPFONT);
144 InsertDataColumn(3, String(CUI_RES(STR_POOL_TIMEOUT)), aColWidth.Width());
145 // Attention: the resource of the string is local to the resource of the enclosing dialog!
148 //--------------------------------------------------------------------
149 void DriverListControl::CursorMoved()
151 DriverListControl_Base::CursorMoved();
153 // call the row change handler
154 if ( m_aRowChangeHandler.IsSet() )
156 if ( GetCurRow() >= 0 )
157 { // == -1 may happen in case the browse box has just been cleared
158 m_aRowChangeHandler.Call( getCurrentRow() );
163 //--------------------------------------------------------------------
164 const DriverPooling* DriverListControl::getCurrentRow() const
166 OSL_ENSURE( ( GetCurRow() < m_aSettings.size() ) && ( GetCurRow() >= 0 ),
167 "DriverListControl::getCurrentRow: invalid current row!");
169 if ( ( GetCurRow() >= 0 ) && ( GetCurRow() < m_aSettings.size() ) )
170 return &(*(m_aSettings.begin() + GetCurRow()));
172 return NULL;
175 //--------------------------------------------------------------------
176 DriverPooling* DriverListControl::getCurrentRow()
178 OSL_ENSURE( ( GetCurRow() < m_aSettings.size() ) && ( GetCurRow() >= 0 ),
179 "DriverListControl::getCurrentRow: invalid current row!");
181 if ( ( GetCurRow() >= 0 ) && ( GetCurRow() < m_aSettings.size() ) )
182 return &(*(m_aSettings.begin() + GetCurRow()));
184 return NULL;
187 //--------------------------------------------------------------------
188 void DriverListControl::updateCurrentRow()
190 Window::Invalidate( GetRowRectPixel( GetCurRow() ), INVALIDATE_UPDATE );
193 //--------------------------------------------------------------------
194 void DriverListControl::Update(const DriverPoolingSettings& _rSettings)
196 m_aSettings = _rSettings;
198 SetUpdateMode(sal_False);
199 RowRemoved(0, GetRowCount());
200 RowInserted(0, m_aSettings.size());
201 SetUpdateMode(sal_True);
203 ActivateCell(1, 0);
206 //--------------------------------------------------------------------
207 sal_uInt32 DriverListControl::GetTotalCellWidth(long nRow, sal_uInt16 nColId)
209 return GetDataWindow().GetTextWidth(GetCellText(nRow, nColId));
212 //--------------------------------------------------------------------
213 String DriverListControl::implGetCellText(DriverPoolingSettings::const_iterator _rPos, sal_uInt16 _nColId) const
215 OSL_ENSURE(_rPos < m_aSettings.end(), "DriverListControl::implGetCellText: invalid position!");
217 String sReturn;
218 switch (_nColId)
220 case 1:
221 sReturn = _rPos->sName;
222 break;
223 case 2:
224 sReturn = _rPos->bEnabled ? m_sYes : m_sNo;
225 break;
226 case 3:
227 if (_rPos->bEnabled)
228 sReturn = OUString::number(_rPos->nTimeoutSeconds);
229 break;
230 default:
231 OSL_FAIL("DriverListControl::implGetCellText: invalid column id!");
233 return sReturn;
236 //--------------------------------------------------------------------
237 void DriverListControl::StateChanged( StateChangedType nStateChange )
239 if (STATE_CHANGE_ENABLE == nStateChange)
240 Window::Invalidate(INVALIDATE_UPDATE);
241 DriverListControl_Base::StateChanged( nStateChange );
244 //--------------------------------------------------------------------
245 String DriverListControl::GetCellText( long nRow, sal_uInt16 nColId ) const
247 String sReturn;
248 if (nRow > m_aSettings.size())
250 OSL_FAIL("DriverListControl::GetCellText: don't ask me for such rows!");
252 else
254 sReturn = implGetCellText(m_aSettings.begin() + nRow, nColId);
256 return sReturn;
259 //--------------------------------------------------------------------
260 void DriverListControl::InitController( ::svt::CellControllerRef& rController, long nRow, sal_uInt16 nCol )
262 rController->GetWindow().SetText(GetCellText(nRow, nCol));
265 //--------------------------------------------------------------------
266 ::svt::CellController* DriverListControl::GetController( long /*nRow*/, sal_uInt16 /*nCol*/ )
268 return NULL;
271 //--------------------------------------------------------------------
272 sal_Bool DriverListControl::SaveModified()
274 return sal_True;
277 //--------------------------------------------------------------------
278 sal_Bool DriverListControl::SeekRow( long _nRow )
280 DriverListControl_Base::SeekRow(_nRow);
282 if (_nRow < m_aSettings.size())
283 m_aSeekRow = m_aSettings.begin() + _nRow;
284 else
285 m_aSeekRow = m_aSettings.end();
287 return m_aSeekRow != m_aSettings.end();
290 //--------------------------------------------------------------------
291 void DriverListControl::PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColId ) const
293 OSL_ENSURE(m_aSeekRow != m_aSettings.end(), "DriverListControl::PaintCell: invalid row!");
295 if (m_aSeekRow != m_aSettings.end())
297 rDev.SetClipRegion(Region(rRect));
299 sal_uInt16 nStyle = TEXT_DRAW_CLIP;
300 if (!IsEnabled())
301 nStyle |= TEXT_DRAW_DISABLE;
302 switch (nColId)
304 case 1: nStyle |= TEXT_DRAW_LEFT; break;
305 case 2:
306 case 3: nStyle |= TEXT_DRAW_CENTER; break;
309 rDev.DrawText(rRect, implGetCellText(m_aSeekRow, nColId), nStyle);
311 rDev.SetClipRegion();
315 //====================================================================
316 //= ConnectionPoolOptionsPage
317 //====================================================================
318 //--------------------------------------------------------------------
319 ConnectionPoolOptionsPage::ConnectionPoolOptionsPage(Window* _pParent, const SfxItemSet& _rAttrSet)
320 :SfxTabPage(_pParent, CUI_RES(RID_OFAPAGE_CONNPOOLOPTIONS ), _rAttrSet)
321 ,m_aFrame (this, CUI_RES(FL_POOLING))
322 ,m_aEnablePooling (this, CUI_RES(CB_POOL_CONNS))
323 ,m_aDriversLabel (this, CUI_RES(FT_DRIVERS))
324 ,m_pDriverList(new DriverListControl(this, CUI_RES(CTRL_DRIVER_LIST)))
325 ,m_aDriverLabel (this, CUI_RES(FT_DRIVERLABEL))
326 ,m_aDriver (this, CUI_RES(FT_DRIVER))
327 ,m_aDriverPoolingEnabled(this, CUI_RES(CB_DRIVERPOOLING))
328 ,m_aTimeoutLabel (this, CUI_RES(FT_TIMEOUT))
329 ,m_aTimeout (this, CUI_RES(NF_TIMEOUT))
331 m_pDriverList->Init();
332 m_pDriverList->Show();
334 FreeResource();
336 m_aEnablePooling.SetClickHdl( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
337 m_aDriverPoolingEnabled.SetClickHdl( LINK(this, ConnectionPoolOptionsPage, OnEnabledDisabled) );
339 m_pDriverList->SetRowChangeHandler( LINK(this, ConnectionPoolOptionsPage, OnDriverRowChanged) );
342 //--------------------------------------------------------------------
343 SfxTabPage* ConnectionPoolOptionsPage::Create(Window* _pParent, const SfxItemSet& _rAttrSet)
345 return new ConnectionPoolOptionsPage(_pParent, _rAttrSet);
348 //--------------------------------------------------------------------
349 ConnectionPoolOptionsPage::~ConnectionPoolOptionsPage()
351 delete m_pDriverList;
354 //--------------------------------------------------------------------
355 void ConnectionPoolOptionsPage::implInitControls(const SfxItemSet& _rSet, sal_Bool /*_bFromReset*/)
357 // the enabled flag
358 SFX_ITEMSET_GET( _rSet, pEnabled, SfxBoolItem, SID_SB_POOLING_ENABLED, sal_True );
359 OSL_ENSURE(pEnabled, "ConnectionPoolOptionsPage::implInitControls: missing the Enabled item!");
360 m_aEnablePooling.Check(pEnabled ? pEnabled->GetValue() : sal_True);
362 m_aEnablePooling.SaveValue();
364 // the settings for the single drivers
365 SFX_ITEMSET_GET( _rSet, pDriverSettings, DriverPoolingSettingsItem, SID_SB_DRIVER_TIMEOUTS, sal_True );
366 if (pDriverSettings)
367 m_pDriverList->Update(pDriverSettings->getSettings());
368 else
370 OSL_FAIL("ConnectionPoolOptionsPage::implInitControls: missing the DriverTimeouts item!");
371 m_pDriverList->Update(DriverPoolingSettings());
373 m_pDriverList->saveValue();
375 // reflect the new settings
376 OnEnabledDisabled(&m_aEnablePooling);
379 //--------------------------------------------------------------------
380 long ConnectionPoolOptionsPage::Notify( NotifyEvent& _rNEvt )
382 if (EVENT_LOSEFOCUS == _rNEvt.GetType())
383 if (m_aTimeout.IsWindowOrChild(_rNEvt.GetWindow()))
384 commitTimeoutField();
386 return SfxTabPage::Notify(_rNEvt);
389 //--------------------------------------------------------------------
390 sal_Bool ConnectionPoolOptionsPage::FillItemSet(SfxItemSet& _rSet)
392 commitTimeoutField();
394 sal_Bool bModified = sal_False;
395 // the enabled flag
396 if (m_aEnablePooling.GetSavedValue() != m_aEnablePooling.IsChecked())
398 _rSet.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, m_aEnablePooling.IsChecked()), SID_SB_POOLING_ENABLED);
399 bModified = sal_True;
402 // the settings for the single drivers
403 if (m_pDriverList->isModified())
405 _rSet.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, m_pDriverList->getSettings()), SID_SB_DRIVER_TIMEOUTS);
406 bModified = sal_True;
409 return bModified;
412 //--------------------------------------------------------------------
413 void ConnectionPoolOptionsPage::ActivatePage( const SfxItemSet& _rSet)
415 SfxTabPage::ActivatePage(_rSet);
416 implInitControls(_rSet, sal_False);
419 //--------------------------------------------------------------------
420 void ConnectionPoolOptionsPage::Reset(const SfxItemSet& _rSet)
422 implInitControls(_rSet, sal_True);
425 //--------------------------------------------------------------------
426 IMPL_LINK( ConnectionPoolOptionsPage, OnDriverRowChanged, const void*, _pRowIterator )
428 sal_Bool bValidRow = (NULL != _pRowIterator);
429 m_aDriverPoolingEnabled.Enable(bValidRow && m_aEnablePooling.IsChecked());
430 m_aTimeoutLabel.Enable(bValidRow);
431 m_aTimeout.Enable(bValidRow);
433 if (!bValidRow)
434 { // positioned on an invalid row
435 m_aDriver.SetText(String());
437 else
439 const DriverPooling *pDriverPos = static_cast<const DriverPooling*>(_pRowIterator);
441 m_aDriver.SetText(pDriverPos->sName);
442 m_aDriverPoolingEnabled.Check(pDriverPos->bEnabled);
443 m_aTimeout.SetText(OUString::number(pDriverPos->nTimeoutSeconds));
445 OnEnabledDisabled(&m_aDriverPoolingEnabled);
448 return 0L;
451 //--------------------------------------------------------------------
452 void ConnectionPoolOptionsPage::commitTimeoutField()
454 if (DriverPooling* pCurrentDriver = m_pDriverList->getCurrentRow())
456 pCurrentDriver->nTimeoutSeconds = static_cast<long>(m_aTimeout.GetValue());
457 m_pDriverList->updateCurrentRow();
461 //--------------------------------------------------------------------
462 IMPL_LINK( ConnectionPoolOptionsPage, OnEnabledDisabled, const CheckBox*, _pCheckBox )
464 sal_Bool bGloballyEnabled = m_aEnablePooling.IsChecked();
465 sal_Bool bLocalDriverChanged = &m_aDriverPoolingEnabled == _pCheckBox;
467 if (&m_aEnablePooling == _pCheckBox)
469 m_aDriversLabel.Enable(bGloballyEnabled);
470 m_pDriverList->Enable(bGloballyEnabled);
471 m_aDriverLabel.Enable(bGloballyEnabled);
472 m_aDriver.Enable(bGloballyEnabled);
473 m_aDriverPoolingEnabled.Enable(bGloballyEnabled);
475 else
476 OSL_ENSURE(bLocalDriverChanged, "ConnectionPoolOptionsPage::OnEnabledDisabled: where did this come from?");
478 m_aTimeoutLabel.Enable(bGloballyEnabled && m_aDriverPoolingEnabled.IsChecked());
479 m_aTimeout.Enable(bGloballyEnabled && m_aDriverPoolingEnabled.IsChecked());
481 if (bLocalDriverChanged)
483 // update the list
484 m_pDriverList->getCurrentRow()->bEnabled = m_aDriverPoolingEnabled.IsChecked();
485 m_pDriverList->updateCurrentRow();
488 return 0L;
491 //........................................................................
492 } // namespace offapp
493 //........................................................................
496 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */