Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "indexfieldscontrol.hxx"
21 : #include "dbu_dlg.hrc"
22 : #include <osl/diagnose.h>
23 : #include "dbaccess_helpid.hrc"
24 : #include <vcl/settings.hxx>
25 :
26 : namespace dbaui
27 : {
28 :
29 : #define BROWSER_STANDARD_FLAGS BROWSER_COLUMNSELECTION | BROWSER_HLINESFULL | BROWSER_VLINESFULL | \
30 : BROWSER_HIDECURSOR | BROWSER_HIDESELECT | BROWSER_AUTO_HSCROLL | BROWSER_AUTO_VSCROLL
31 :
32 : #define COLUMN_ID_FIELDNAME 1
33 : #define COLUMN_ID_ORDER 2
34 :
35 : using namespace ::com::sun::star::uno;
36 : using namespace ::svt;
37 :
38 : // DbaMouseDownListBoxController
39 0 : class DbaMouseDownListBoxController : public ListBoxCellController
40 : {
41 : protected:
42 : Link m_aOriginalModifyHdl;
43 : Link m_aAdditionalModifyHdl;
44 :
45 : public:
46 0 : DbaMouseDownListBoxController(ListBoxControl* _pParent)
47 0 : :ListBoxCellController(_pParent)
48 : {
49 0 : }
50 :
51 : void SetAdditionalModifyHdl(const Link& _rHdl);
52 :
53 : protected:
54 0 : virtual bool WantMouseEvent() const SAL_OVERRIDE { return true; }
55 : virtual void SetModifyHdl(const Link& _rHdl) SAL_OVERRIDE;
56 :
57 : private:
58 : void implCheckLinks();
59 : DECL_LINK( OnMultiplexModify, void* );
60 : };
61 :
62 0 : void DbaMouseDownListBoxController::SetAdditionalModifyHdl(const Link& _rHdl)
63 : {
64 0 : m_aAdditionalModifyHdl = _rHdl;
65 0 : implCheckLinks();
66 0 : }
67 :
68 0 : void DbaMouseDownListBoxController::SetModifyHdl(const Link& _rHdl)
69 : {
70 0 : m_aOriginalModifyHdl = _rHdl;
71 0 : implCheckLinks();
72 0 : }
73 :
74 0 : IMPL_LINK( DbaMouseDownListBoxController, OnMultiplexModify, void*, _pArg )
75 : {
76 0 : if (m_aAdditionalModifyHdl.IsSet())
77 0 : m_aAdditionalModifyHdl.Call(_pArg);
78 0 : if (m_aOriginalModifyHdl.IsSet())
79 0 : m_aOriginalModifyHdl.Call(_pArg);
80 0 : return 0L;
81 : }
82 :
83 0 : void DbaMouseDownListBoxController::implCheckLinks()
84 : {
85 0 : if (m_aAdditionalModifyHdl.IsSet() || m_aOriginalModifyHdl.IsSet())
86 0 : ListBoxCellController::SetModifyHdl(LINK(this, DbaMouseDownListBoxController, OnMultiplexModify));
87 : else
88 0 : ListBoxCellController::SetModifyHdl(Link());
89 0 : }
90 :
91 : // IndexFieldsControl
92 0 : IndexFieldsControl::IndexFieldsControl( Window* _pParent, const ResId& _rId ,sal_Int32 _nMaxColumnsInIndex,sal_Bool _bAddIndexAppendix)
93 : :EditBrowseBox(_pParent, _rId, EBBF_SMART_TAB_TRAVEL | EBBF_ACTIVATE_ON_BUTTONDOWN, BROWSER_STANDARD_FLAGS)
94 0 : ,m_aSeekRow(m_aFields.end())
95 : ,m_pSortingCell(NULL)
96 : ,m_pFieldNameCell(NULL)
97 : ,m_nMaxColumnsInIndex(_nMaxColumnsInIndex)
98 0 : ,m_bAddIndexAppendix(_bAddIndexAppendix)
99 : {
100 :
101 0 : SetUniqueId( UID_DLGINDEX_INDEXDETAILS_BACK );
102 0 : GetDataWindow().SetUniqueId( UID_DLGINDEX_INDEXDETAILS_MAIN );
103 0 : }
104 :
105 0 : IndexFieldsControl::~IndexFieldsControl()
106 : {
107 0 : delete m_pSortingCell;
108 0 : delete m_pFieldNameCell;
109 :
110 0 : }
111 :
112 0 : bool IndexFieldsControl::SeekRow(long nRow)
113 : {
114 0 : if (!EditBrowseBox::SeekRow(nRow))
115 0 : return false;
116 :
117 0 : if (nRow < 0)
118 : {
119 0 : m_aSeekRow = m_aFields.end();
120 : }
121 : else
122 : {
123 0 : m_aSeekRow = m_aFields.begin() + nRow;
124 : OSL_ENSURE(m_aSeekRow <= m_aFields.end(), "IndexFieldsControl::SeekRow: invalid row!");
125 : }
126 :
127 0 : return true;
128 : }
129 :
130 0 : void IndexFieldsControl::PaintCell( OutputDevice& _rDev, const Rectangle& _rRect, sal_uInt16 _nColumnId ) const
131 : {
132 0 : Point aPos(_rRect.TopLeft());
133 0 : aPos.X() += 1;
134 :
135 0 : OUString aText = GetRowCellText(m_aSeekRow,_nColumnId);
136 0 : Size TxtSize(GetDataWindow().GetTextWidth(aText), GetDataWindow().GetTextHeight());
137 :
138 : // clipping
139 0 : if (aPos.X() < _rRect.Right() || aPos.X() + TxtSize.Width() > _rRect.Right() ||
140 0 : aPos.Y() < _rRect.Top() || aPos.Y() + TxtSize.Height() > _rRect.Bottom())
141 0 : _rDev.SetClipRegion(Region(_rRect));
142 :
143 : // allow for a disabled control ...
144 0 : sal_Bool bEnabled = IsEnabled();
145 0 : Color aOriginalColor = _rDev.GetTextColor();
146 0 : if (!bEnabled)
147 0 : _rDev.SetTextColor(GetSettings().GetStyleSettings().GetDisableColor());
148 :
149 : // draw the text
150 0 : _rDev.DrawText(aPos, aText);
151 :
152 : // reset the color (if necessary)
153 0 : if (!bEnabled)
154 0 : _rDev.SetTextColor(aOriginalColor);
155 :
156 0 : if (_rDev.IsClipRegion())
157 0 : _rDev.SetClipRegion();
158 0 : }
159 :
160 0 : void IndexFieldsControl::initializeFrom(const IndexFields& _rFields)
161 : {
162 : // copy the field descriptions
163 0 : m_aFields = _rFields;
164 0 : m_aSeekRow = m_aFields.end();
165 :
166 0 : SetUpdateMode(false);
167 : // remove all rows
168 0 : RowRemoved(1, GetRowCount());
169 : // insert rows for the fields
170 0 : RowInserted(GetRowCount(), m_aFields.size(), false);
171 : // insert an additional row for a new field for that index
172 0 : RowInserted(GetRowCount(), 1, false);
173 0 : SetUpdateMode(true);
174 :
175 0 : GoToRowColumnId(0, COLUMN_ID_FIELDNAME);
176 0 : }
177 :
178 0 : void IndexFieldsControl::commitTo(IndexFields& _rFields)
179 : {
180 : // do not just copy the array, we may have empty field names (which should not be copied)
181 0 : _rFields.resize(m_aFields.size());
182 0 : IndexFields::const_iterator aSource = m_aFields.begin();
183 0 : IndexFields::const_iterator aSourceEnd = m_aFields.end();
184 0 : IndexFields::iterator aDest = _rFields.begin();
185 0 : for (; aSource < aSourceEnd; ++aSource)
186 0 : if (!aSource->sFieldName.isEmpty())
187 : {
188 0 : *aDest = *aSource;
189 0 : ++aDest;
190 : }
191 :
192 0 : _rFields.resize(aDest - _rFields.begin());
193 0 : }
194 :
195 0 : sal_uInt32 IndexFieldsControl::GetTotalCellWidth(long _nRow, sal_uInt16 _nColId)
196 : {
197 0 : if (COLUMN_ID_ORDER == _nColId)
198 : {
199 0 : sal_Int32 nWidthAsc = GetTextWidth(m_sAscendingText) + GetSettings().GetStyleSettings().GetScrollBarSize();
200 0 : sal_Int32 nWidthDesc = GetTextWidth(m_sDescendingText) + GetSettings().GetStyleSettings().GetScrollBarSize();
201 : // maximum plus some additional space
202 0 : return (nWidthAsc > nWidthDesc ? nWidthAsc : nWidthDesc) + GetTextWidth(OUString('0')) * 2;
203 : }
204 0 : return EditBrowseBox::GetTotalCellWidth(_nRow, _nColId);
205 : }
206 :
207 0 : void IndexFieldsControl::Init(const Sequence< OUString >& _rAvailableFields)
208 : {
209 0 : RemoveColumns();
210 :
211 : // for the width: both columns together should be somewhat smaller than the whole window (without the scrollbar)
212 0 : sal_Int32 nFieldNameWidth = GetSizePixel().Width();
213 :
214 0 : if ( m_bAddIndexAppendix )
215 : {
216 0 : m_sAscendingText = ModuleRes(STR_ORDER_ASCENDING);
217 0 : m_sDescendingText = ModuleRes(STR_ORDER_DESCENDING);
218 :
219 : // the "sort order" column
220 0 : OUString sColumnName = ModuleRes(STR_TAB_INDEX_SORTORDER);
221 : // the width of the order column is the maximum widths of the texts used
222 : // (the title of the column)
223 0 : sal_Int32 nSortOrderColumnWidth = GetTextWidth(sColumnName);
224 : // ("ascending" + scrollbar width)
225 0 : sal_Int32 nOther = GetTextWidth(m_sAscendingText) + GetSettings().GetStyleSettings().GetScrollBarSize();
226 0 : nSortOrderColumnWidth = nSortOrderColumnWidth > nOther ? nSortOrderColumnWidth : nOther;
227 : // ("descending" + scrollbar width)
228 0 : nOther = GetTextWidth(m_sDescendingText) + GetSettings().GetStyleSettings().GetScrollBarSize();
229 0 : nSortOrderColumnWidth = nSortOrderColumnWidth > nOther ? nSortOrderColumnWidth : nOther;
230 : // (plus some additional space)
231 0 : nSortOrderColumnWidth += GetTextWidth(OUString('0')) * 2;
232 0 : InsertDataColumn(COLUMN_ID_ORDER, sColumnName, nSortOrderColumnWidth, HIB_STDSTYLE, 1);
233 :
234 0 : m_pSortingCell = new ListBoxControl(&GetDataWindow());
235 0 : m_pSortingCell->InsertEntry(m_sAscendingText);
236 0 : m_pSortingCell->InsertEntry(m_sDescendingText);
237 0 : m_pSortingCell->SetHelpId( HID_DLGINDEX_INDEXDETAILS_SORTORDER );
238 :
239 0 : nFieldNameWidth -= nSortOrderColumnWidth;
240 : }
241 0 : StyleSettings aSystemStyle = Application::GetSettings().GetStyleSettings();
242 0 : nFieldNameWidth -= aSystemStyle.GetScrollBarSize();
243 0 : nFieldNameWidth -= 8;
244 : // the "field name" column
245 0 : OUString sColumnName = ModuleRes(STR_TAB_INDEX_FIELD);
246 0 : InsertDataColumn(COLUMN_ID_FIELDNAME, sColumnName, nFieldNameWidth, HIB_STDSTYLE, 0);
247 :
248 : // create the cell controllers
249 : // for the field name cell
250 0 : m_pFieldNameCell = new ListBoxControl(&GetDataWindow());
251 0 : m_pFieldNameCell->InsertEntry(OUString());
252 0 : m_pFieldNameCell->SetHelpId( HID_DLGINDEX_INDEXDETAILS_FIELD );
253 0 : const OUString* pFields = _rAvailableFields.getConstArray();
254 0 : const OUString* pFieldsEnd = pFields + _rAvailableFields.getLength();
255 0 : for (;pFields < pFieldsEnd; ++pFields)
256 0 : m_pFieldNameCell->InsertEntry(*pFields);
257 0 : }
258 :
259 0 : CellController* IndexFieldsControl::GetController(long _nRow, sal_uInt16 _nColumnId)
260 : {
261 0 : if (!IsEnabled())
262 0 : return NULL;
263 :
264 0 : IndexFields::const_iterator aRow;
265 0 : sal_Bool bNewField = !implGetFieldDesc(_nRow, aRow);
266 :
267 0 : DbaMouseDownListBoxController* pReturn = NULL;
268 0 : switch (_nColumnId)
269 : {
270 : case COLUMN_ID_ORDER:
271 0 : if (!bNewField && m_pSortingCell && !aRow->sFieldName.isEmpty())
272 0 : pReturn = new DbaMouseDownListBoxController(m_pSortingCell);
273 0 : break;
274 :
275 : case COLUMN_ID_FIELDNAME:
276 0 : pReturn = new DbaMouseDownListBoxController(m_pFieldNameCell);
277 0 : break;
278 :
279 : default:
280 : OSL_FAIL("IndexFieldsControl::GetController: invalid column id!");
281 : }
282 :
283 0 : if (pReturn)
284 0 : pReturn->SetAdditionalModifyHdl(LINK(this, IndexFieldsControl, OnListEntrySelected));
285 :
286 0 : return pReturn;
287 : }
288 :
289 0 : sal_Bool IndexFieldsControl::implGetFieldDesc(long _nRow, IndexFields::const_iterator& _rPos)
290 : {
291 0 : _rPos = m_aFields.end();
292 0 : if ((_nRow < 0) || (_nRow >= (sal_Int32)m_aFields.size()))
293 0 : return sal_False;
294 0 : _rPos = m_aFields.begin() + _nRow;
295 0 : return sal_True;
296 : }
297 :
298 0 : bool IndexFieldsControl::IsModified() const
299 : {
300 0 : return EditBrowseBox::IsModified();
301 : }
302 :
303 0 : bool IndexFieldsControl::SaveModified()
304 : {
305 0 : if (!IsModified())
306 0 : return true;
307 :
308 0 : switch (GetCurColumnId())
309 : {
310 : case COLUMN_ID_FIELDNAME:
311 : {
312 0 : OUString sFieldSelected = m_pFieldNameCell->GetSelectEntry();
313 0 : sal_Bool bEmptySelected = sFieldSelected.isEmpty();
314 0 : if (isNewField())
315 : {
316 0 : if (!bEmptySelected)
317 : {
318 : // add a new field to the collection
319 0 : OIndexField aNewField;
320 0 : aNewField.sFieldName = sFieldSelected;
321 0 : m_aFields.push_back(aNewField);
322 0 : RowInserted(GetRowCount(), 1, true);
323 : }
324 : }
325 : else
326 : {
327 0 : sal_Int32 nRow = GetCurRow();
328 : OSL_ENSURE(nRow < (sal_Int32)m_aFields.size(), "IndexFieldsControl::SaveModified: invalid current row!");
329 0 : if (nRow >= 0) // may be -1 in case the control was empty
330 : {
331 : // remove the field from the selection
332 0 : IndexFields::iterator aPos = m_aFields.begin() + nRow;
333 :
334 0 : if (bEmptySelected)
335 : {
336 0 : aPos->sFieldName = "";
337 :
338 : // invalidate the row to force repaint
339 0 : Invalidate(GetRowRectPixel(nRow));
340 0 : return true;
341 : }
342 :
343 0 : if (sFieldSelected == aPos->sFieldName)
344 : // nothing changed
345 0 : return true;
346 :
347 0 : aPos->sFieldName = sFieldSelected;
348 : }
349 : }
350 :
351 0 : Invalidate(GetRowRectPixel(GetCurRow()));
352 : }
353 0 : break;
354 : case COLUMN_ID_ORDER:
355 : {
356 : OSL_ENSURE(!isNewField(), "IndexFieldsControl::SaveModified: why the hell ...!!!");
357 : // selected entry
358 0 : sal_Int32 nPos = m_pSortingCell->GetSelectEntryPos();
359 : OSL_ENSURE(LISTBOX_ENTRY_NOTFOUND != nPos, "IndexFieldsControl::SaveModified: how did you get this selection??");
360 : // adjust the sort flag in the index field description
361 0 : OIndexField& rCurrentField = m_aFields[GetCurRow()];
362 0 : rCurrentField.bSortAscending = (0 == nPos);
363 :
364 : }
365 0 : break;
366 : default:
367 : OSL_FAIL("IndexFieldsControl::SaveModified: invalid column id!");
368 : }
369 0 : return true;
370 : }
371 :
372 0 : void IndexFieldsControl::InitController(CellControllerRef& /*_rController*/, long _nRow, sal_uInt16 _nColumnId)
373 : {
374 0 : IndexFields::const_iterator aFieldDescription;
375 0 : sal_Bool bNewField = !implGetFieldDesc(_nRow, aFieldDescription);
376 :
377 0 : switch (_nColumnId)
378 : {
379 : case COLUMN_ID_FIELDNAME:
380 0 : m_pFieldNameCell->SelectEntry(bNewField ? OUString() : aFieldDescription->sFieldName);
381 0 : m_pFieldNameCell->SaveValue();
382 0 : break;
383 :
384 : case COLUMN_ID_ORDER:
385 0 : m_pSortingCell->SelectEntry(aFieldDescription->bSortAscending ? m_sAscendingText : m_sDescendingText);
386 0 : m_pSortingCell->SaveValue();
387 0 : break;
388 :
389 : default:
390 : OSL_FAIL("IndexFieldsControl::InitController: invalid column id!");
391 : }
392 0 : }
393 :
394 0 : IMPL_LINK( IndexFieldsControl, OnListEntrySelected, ListBox*, _pBox )
395 : {
396 0 : if (!_pBox->IsTravelSelect() && m_aModifyHdl.IsSet())
397 0 : m_aModifyHdl.Call(this);
398 :
399 0 : if (_pBox == m_pFieldNameCell)
400 : { // a field has been selected
401 0 : if (GetCurRow() >= GetRowCount() - 2)
402 : { // and we're in one of the last two rows
403 0 : OUString sSelectedEntry = m_pFieldNameCell->GetSelectEntry();
404 0 : sal_Int32 nCurrentRow = GetCurRow();
405 0 : sal_Int32 rowCount = GetRowCount();
406 :
407 : OSL_ENSURE(((sal_Int32)(m_aFields.size() + 1)) == rowCount, "IndexFieldsControl::OnListEntrySelected: inconsistence!");
408 :
409 0 : if (!sSelectedEntry.isEmpty() && (nCurrentRow == rowCount - 1) /*&& (!m_nMaxColumnsInIndex || rowCount < m_nMaxColumnsInIndex )*/ )
410 : { // in the last row, an non-empty string has been selected
411 : // -> insert a new row
412 0 : m_aFields.push_back(OIndexField());
413 0 : RowInserted(GetRowCount(), 1);
414 0 : Invalidate(GetRowRectPixel(nCurrentRow));
415 : }
416 0 : else if (sSelectedEntry.isEmpty() && (nCurrentRow == rowCount - 2))
417 : { // in the (last-1)th row, an empty entry has been selected
418 : // -> remove the last row
419 0 : m_aFields.erase(m_aFields.end() - 1);
420 0 : RowRemoved(GetRowCount() - 1, 1);
421 0 : Invalidate(GetRowRectPixel(nCurrentRow));
422 0 : }
423 : }
424 :
425 0 : SaveModified();
426 : }
427 0 : return 0L;
428 : }
429 0 : OUString IndexFieldsControl::GetCellText(long _nRow,sal_uInt16 nColId) const
430 : {
431 0 : IndexFields::const_iterator aRow = m_aFields.end();
432 0 : if ( _nRow >= 0 )
433 : {
434 0 : aRow = m_aFields.begin() + _nRow;
435 : OSL_ENSURE(aRow <= m_aFields.end(), "IndexFieldsControl::SeekRow: invalid row!");
436 : }
437 0 : return GetRowCellText(aRow,nColId);
438 : }
439 0 : OUString IndexFieldsControl::GetRowCellText(const IndexFields::const_iterator& _rRow,sal_uInt16 nColId) const
440 : {
441 0 : if (_rRow < m_aFields.end())
442 : {
443 0 : switch (nColId)
444 : {
445 : case COLUMN_ID_FIELDNAME:
446 0 : return _rRow->sFieldName;
447 : case COLUMN_ID_ORDER:
448 0 : if (_rRow->sFieldName.isEmpty())
449 0 : return OUString();
450 : else
451 0 : return _rRow->bSortAscending ? m_sAscendingText : m_sDescendingText;
452 : default:
453 : OSL_FAIL("IndexFieldsControl::GetCurrentRowCellText: invalid column id!");
454 : }
455 : }
456 0 : return OUString();
457 : }
458 0 : bool IndexFieldsControl::IsTabAllowed(bool /*bForward*/) const
459 : {
460 0 : return false;
461 : }
462 :
463 : } // namespace dbaui
464 :
465 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|