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