Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / control / FieldDescControl.cxx
blob9752c627252d55b1e34fdd58ba95a90892711c7b
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 "FieldDescControl.hxx"
21 #include "FieldControls.hxx"
22 #include <tools/diagnose_ex.h>
23 #include "TableDesignHelpBar.hxx"
24 #include <vcl/scrbar.hxx>
25 #include <vcl/button.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/fixed.hxx>
28 #include <vcl/msgbox.hxx>
29 #include <vector>
30 #include "FieldDescriptions.hxx"
31 #include "dlgattr.hxx"
32 #include <svx/numfmtsh.hxx>
33 #include <svx/svxids.hrc>
34 #include <svx/algitem.hxx>
35 #include <svl/itempool.hxx>
36 #include <svl/zforlist.hxx>
37 #include <svl/rngitem.hxx>
38 #include <svl/intitem.hxx>
39 #include <svl/numuno.hxx>
40 #include <svtools/transfer.hxx>
41 #include <com/sun/star/lang/XUnoTunnel.hpp>
42 #include <com/sun/star/util/NumberFormat.hpp>
43 #include <com/sun/star/util/XNumberFormatPreviewer.hpp>
44 #include <com/sun/star/util/XNumberFormatTypes.hpp>
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include "QEnumTypes.hxx"
47 #include "dbaccess_helpid.hrc"
48 #include <connectivity/dbtools.hxx>
49 #include <connectivity/dbconversion.hxx>
50 #include <comphelper/numbers.hxx>
51 #include <comphelper/string.hxx>
52 #include "UITools.hxx"
53 #include "dbu_control.hrc"
54 #include "dbu_tbl.hrc"
55 #include <osl/diagnose.h>
57 using namespace dbaui;
58 using namespace dbtools;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::beans;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star::sdbc;
63 using namespace ::com::sun::star::util;
65 // For the Controls on the OFieldDescGenPage
66 #define CONTROL_SPACING_X 18 // 6
67 #define CONTROL_SPACING_Y 4
68 #define CONTROL_WIDTH_1 160 // 100
69 #define CONTROL_WIDTH_2 100 // 60
70 #define CONTROL_WIDTH_3 250
71 #define CONTROL_WIDTH_4 (CONTROL_WIDTH_3 - 20 - 5)
73 #define HSCROLL_STEP 20
75 namespace
78 template< typename T1, typename T2> void lcl_HideAndDeleteControl(short& _nPos,VclPtr<T1>& _pControl, VclPtr<T2>& _pControlText)
80 if ( _pControl )
82 --_nPos;
83 _pControl->Hide();
84 _pControlText->Hide();
85 _pControl.disposeAndClear();
86 _pControlText.disposeAndClear();
92 // class OFieldDescControl
93 OFieldDescControl::OFieldDescControl( vcl::Window* pParent, OTableDesignHelpBar* pHelpBar )
94 :TabPage( pParent, WB_3DLOOK | WB_DIALOGCONTROL )
95 ,pHelp( pHelpBar )
96 ,pLastFocusWindow(nullptr)
97 ,m_pActFocusWindow(nullptr)
98 ,pDefaultText(nullptr)
99 ,pRequiredText(nullptr)
100 ,pAutoIncrementText(nullptr)
101 ,pTextLenText(nullptr)
102 ,pNumTypeText(nullptr)
103 ,pLengthText(nullptr)
104 ,pScaleText(nullptr)
105 ,pFormatText(nullptr)
106 ,pBoolDefaultText(nullptr)
107 ,m_pColumnNameText(nullptr)
108 ,m_pTypeText(nullptr)
109 ,m_pAutoIncrementValueText(nullptr)
110 ,pRequired(nullptr)
111 ,pNumType(nullptr)
112 ,pAutoIncrement(nullptr)
113 ,pDefault(nullptr)
114 ,pTextLen(nullptr)
115 ,pLength(nullptr)
116 ,pScale(nullptr)
117 ,pFormatSample(nullptr)
118 ,pBoolDefault(nullptr)
119 ,m_pColumnName(nullptr)
120 ,m_pType(nullptr)
121 ,m_pAutoIncrementValue(nullptr)
122 ,pFormat(nullptr)
123 ,m_pVertScroll( nullptr )
124 ,m_pHorzScroll( nullptr )
125 ,m_pPreviousType()
126 ,m_nPos(-1)
127 ,aYes(ModuleRes(STR_VALUE_YES))
128 ,aNo(ModuleRes(STR_VALUE_NO))
129 ,m_nOldVThumb( 0 )
130 ,m_nOldHThumb( 0 )
131 ,m_nWidth(50)
132 ,m_bAdded(false)
133 ,m_bRightAligned(false)
134 ,pActFieldDescr(nullptr)
136 Contruct();
139 void OFieldDescControl::Contruct()
141 m_pVertScroll = VclPtr<ScrollBar>::Create(this, WB_VSCROLL | WB_REPEAT | WB_DRAG);
142 m_pHorzScroll = VclPtr<ScrollBar>::Create(this, WB_HSCROLL | WB_REPEAT | WB_DRAG);
143 m_pVertScroll->SetScrollHdl(LINK(this, OFieldDescControl, OnScroll));
144 m_pHorzScroll->SetScrollHdl(LINK(this, OFieldDescControl, OnScroll));
145 m_pVertScroll->Show();
146 m_pHorzScroll->Show();
148 m_pVertScroll->EnableClipSiblings();
149 m_pHorzScroll->EnableClipSiblings();
151 m_pVertScroll->SetLineSize(1);
152 m_pVertScroll->SetPageSize(1);
153 m_pHorzScroll->SetLineSize(1);
154 m_pHorzScroll->SetPageSize(1);
156 m_nOldVThumb = m_nOldHThumb = 0;
159 OFieldDescControl::~OFieldDescControl()
161 disposeOnce();
164 void OFieldDescControl::dispose()
166 if ( m_bAdded )
167 ::dbaui::notifySystemWindow(this,this,::comphelper::mem_fun(&TaskPaneList::RemoveWindow));
169 // Destroy children
170 DeactivateAggregate( tpDefault );
171 DeactivateAggregate( tpRequired );
172 DeactivateAggregate( tpTextLen );
173 DeactivateAggregate( tpNumType );
174 DeactivateAggregate( tpScale );
175 DeactivateAggregate( tpLength );
176 DeactivateAggregate( tpFormat );
177 DeactivateAggregate( tpAutoIncrement );
178 DeactivateAggregate( tpBoolDefault );
179 DeactivateAggregate( tpColumnName );
180 DeactivateAggregate( tpType );
181 DeactivateAggregate( tpAutoIncrementValue );
182 pHelp.clear();
183 pLastFocusWindow.clear();
184 m_pActFocusWindow.clear();
185 pDefaultText.clear();
186 pRequiredText.clear();
187 pAutoIncrementText.clear();
188 pTextLenText.clear();
189 pNumTypeText.clear();
190 pLengthText.clear();
191 pScaleText.clear();
192 pFormatText.clear();
193 pBoolDefaultText.clear();
194 m_pColumnNameText.clear();
195 m_pTypeText.clear();
196 m_pAutoIncrementValueText.clear();
197 pRequired.clear();
198 pNumType.clear();
199 pAutoIncrement.clear();
200 pDefault.clear();
201 pTextLen.clear();
202 pLength.clear();
203 pScale.clear();
204 pFormatSample.clear();
205 pBoolDefault.clear();
206 m_pColumnName.clear();
207 m_pType.clear();
208 m_pAutoIncrementValue.clear();
209 pFormat.clear();
210 m_pVertScroll.disposeAndClear();
211 m_pHorzScroll.disposeAndClear();
212 TabPage::dispose();
215 OUString OFieldDescControl::BoolStringPersistent(const OUString& rUIString) const
217 if (rUIString == aNo)
218 return OUString('0');
219 if (rUIString == aYes)
220 return OUString('1');
221 return OUString();
224 OUString OFieldDescControl::BoolStringUI(const OUString& rPersistentString) const
226 // Older versions may store a language dependent string as a default
227 if (rPersistentString == aYes || rPersistentString == aNo)
228 return rPersistentString;
230 if (rPersistentString == "0")
231 return aNo;
232 if (rPersistentString == "1")
233 return aYes;
235 return ModuleRes(STR_VALUE_NONE).toString();
238 void OFieldDescControl::Init()
240 Reference< css::util::XNumberFormatter > xFormatter = GetFormatter();
241 ::dbaui::setEvalDateFormatForFormatter(xFormatter);
244 IMPL_LINK_TYPED(OFieldDescControl, OnScroll, ScrollBar*, /*pBar*/, void)
246 ScrollAllAggregates();
249 namespace
251 void getMaxXPosition(vcl::Window* _pWindow,long& _rnMaxXPosition)
253 if (_pWindow)
255 long nTemp = _pWindow->GetSizePixel().Width() + _pWindow->GetPosPixel().X();
256 _rnMaxXPosition = ::std::max(_rnMaxXPosition, nTemp);
261 void OFieldDescControl::CheckScrollBars()
263 // Calculate the ScrollBars' new position
264 Size szOverallSize = GetSizePixel();
265 long nHScrollHeight = m_pHorzScroll->GetSizePixel().Height();
266 long nVScrollWidth = m_pVertScroll->GetSizePixel().Width();
268 long nNewHWidth = szOverallSize.Width() - nVScrollWidth;
269 long nNewVHeight = szOverallSize.Height() - nHScrollHeight;
271 bool bNeedHScrollBar(false), bNeedVScrollBar(false);
273 // Adjust the areas
274 // Do I actually need ScrollBars?
275 // horizontal :
276 long lMaxXPosition = 0;
277 Control* ppAggregates[] = { pRequired, pNumType, pAutoIncrement, pDefault, pTextLen, pLength, pScale, pFormat, m_pColumnName, m_pType,m_pAutoIncrementValue};
278 for (Control* ppAggregate : ppAggregates)
279 getMaxXPosition(ppAggregate,lMaxXPosition);
281 lMaxXPosition += m_pHorzScroll->GetThumbPos() * HSCROLL_STEP;
283 long lMaxXAvailable = szOverallSize.Width();
284 bNeedHScrollBar = lMaxXPosition > lMaxXAvailable;
285 // Might change
287 // Vertical
288 // How many Controls do I have?
289 sal_uInt16 nActive = CountActiveAggregates();
290 // Which one is the last one that fits?
291 sal_uInt16 nLastVisible;
292 const sal_Int32 nControlHeight = GetMaxControlHeight();
293 const sal_Int32 nControl_Spacing_y = LogicToPixel(Size(0, CONTROL_SPACING_Y),MAP_APPFONT).Height();
294 if (bNeedHScrollBar)
295 nLastVisible = static_cast<sal_uInt16>((szOverallSize.Height() - nControl_Spacing_y - nHScrollHeight) / (nControl_Spacing_y + nControlHeight));
296 else
297 nLastVisible = static_cast<sal_uInt16>((szOverallSize.Height() - nControl_Spacing_y) / (nControl_Spacing_y + nControlHeight));
298 bNeedVScrollBar = nActive>nLastVisible;
300 if (bNeedVScrollBar)
302 // When originally calculating lMaxXAvailable we did not take into account that we have a VScrollBar, so we need to do that now
303 lMaxXAvailable -= nVScrollWidth;
304 if (!bNeedHScrollBar && (lMaxXPosition > lMaxXAvailable))
306 // The vertical one now necessitates a horizontal one
307 bNeedHScrollBar = true;
308 // Adjust nLastVisible
309 nLastVisible = static_cast<sal_uInt16>((szOverallSize.Height() - nControl_Spacing_y - nHScrollHeight) / (nControl_Spacing_y + nControlHeight));
310 // bNeedVScrollBar does NOT change: it's already set to sal_True and nLastVisible will only decrease
314 // Now we can really position them and set their parameters
315 if (bNeedVScrollBar)
317 m_pVertScroll->Show();
318 m_pVertScroll->SetRangeMax(nActive - nLastVisible);
320 m_pVertScroll->SetPosSizePixel( Point(nNewHWidth, 0), Size(nVScrollWidth, szOverallSize.Height()) );
322 else
324 m_pVertScroll->Hide();
325 m_pVertScroll->SetRangeMax(0);
326 m_pVertScroll->SetThumbPos(0);
329 if (bNeedHScrollBar)
331 m_pHorzScroll->Show();
332 m_pHorzScroll->SetRangeMax((lMaxXPosition - lMaxXAvailable + HSCROLL_STEP - 1 )/HSCROLL_STEP);
334 m_pHorzScroll->SetPosSizePixel( Point(0, nNewVHeight), Size(bNeedVScrollBar ? nNewHWidth : szOverallSize.Width(), nHScrollHeight) );
336 else
338 m_pHorzScroll->Hide();
339 m_pHorzScroll->SetRangeMax(0);
340 m_pHorzScroll->SetThumbPos(0);
344 void OFieldDescControl::Resize()
346 CheckScrollBars();
347 ScrollAllAggregates();
350 inline void OFieldDescControl::ScrollAggregate(Control* pText, Control* pInput, Control* pButton, long nDeltaX, long nDeltaY)
352 if (!pText)
353 return;
354 pText->SetPosPixel(pText->GetPosPixel() + Point(nDeltaX, nDeltaY));
355 pInput->SetPosPixel(pInput->GetPosPixel() + Point(nDeltaX, nDeltaY));
356 if (pButton)
357 pButton->SetPosPixel(pButton->GetPosPixel() + Point(nDeltaX, nDeltaY));
360 void OFieldDescControl::ScrollAllAggregates()
362 long nDeltaX = 0, nDeltaY = 0;
363 if (m_nOldHThumb != m_pHorzScroll->GetThumbPos())
365 nDeltaX = (m_nOldHThumb - m_pHorzScroll->GetThumbPos()) * HSCROLL_STEP;
366 m_nOldHThumb = m_pHorzScroll->GetThumbPos();
369 if (m_nOldVThumb != m_pVertScroll->GetThumbPos())
371 const sal_Int32 nControlHeight = GetMaxControlHeight();
372 const sal_Int32 nControl_Spacing_y = LogicToPixel(Size(0, CONTROL_SPACING_Y),MAP_APPFONT).Height();
373 nDeltaY = (m_nOldVThumb - m_pVertScroll->GetThumbPos()) * (nControl_Spacing_y + nControlHeight);
374 m_nOldVThumb = m_pVertScroll->GetThumbPos();
377 if (nDeltaX || nDeltaY)
379 Control* ppAggregates[] = { pRequired, pNumType
380 , pAutoIncrement, pDefault
381 , pTextLen, pLength
382 , pScale, m_pColumnName
383 , m_pType, m_pAutoIncrementValue};
384 Control* ppAggregatesText[] = { pRequiredText, pNumTypeText
385 , pAutoIncrementText, pDefaultText
386 , pTextLenText, pLengthText
387 , pScaleText, m_pColumnNameText
388 , m_pTypeText, m_pAutoIncrementValueText};
389 OSL_ENSURE(SAL_N_ELEMENTS(ppAggregates) == SAL_N_ELEMENTS(ppAggregatesText),"Lists are not identical!");
391 for (sal_uInt16 i=0; i<SAL_N_ELEMENTS(ppAggregates); ++i)
392 ScrollAggregate(ppAggregatesText[i],ppAggregates[i],nullptr,nDeltaX, nDeltaY);
394 ScrollAggregate(pFormatText,pFormatSample,pFormat,nDeltaX, nDeltaY);
398 sal_uInt16 OFieldDescControl::CountActiveAggregates() const
400 Control* ppAggregates[] = { pRequired, pNumType, pAutoIncrement, pDefault, pTextLen, pLength, pScale, pFormat, m_pColumnName, m_pType,m_pAutoIncrementValue};
401 sal_uInt16 nVisibleAggregates = 0;
402 for (Control* pAggregate : ppAggregates)
403 if (pAggregate)
404 ++nVisibleAggregates;
405 return nVisibleAggregates;
408 sal_Int32 OFieldDescControl::GetMaxControlHeight() const
410 Size aHeight;
411 Control* ppAggregates[] = { pRequired, pNumType, pAutoIncrement, pDefault, pTextLen, pLength, pScale, pFormat, m_pColumnName, m_pType,m_pAutoIncrementValue};
412 for (Control* pAggregate : ppAggregates)
414 if ( pAggregate )
416 const Size aTemp(pAggregate->GetOptimalSize());
417 if ( aTemp.Height() > aHeight.Height() )
418 aHeight.Height() = aTemp.Height();
422 return aHeight.Height();
425 void OFieldDescControl::SetReadOnly( bool bReadOnly )
427 // Enable/disable Controls
428 Control* ppAggregates[] = { pRequired, pNumType
429 , pAutoIncrement, pDefault
430 , pTextLen, pLength
431 , pScale, m_pColumnName
432 , m_pType, m_pAutoIncrementValue
433 , pFormat};
434 Control* ppAggregatesText[] = { pRequiredText, pNumTypeText
435 , pAutoIncrementText, pDefaultText
436 , pTextLenText, pLengthText
437 , pScaleText, m_pColumnNameText
438 , m_pTypeText, m_pAutoIncrementValueText
439 , pFormatText};
441 OSL_ENSURE(SAL_N_ELEMENTS(ppAggregates) == SAL_N_ELEMENTS(ppAggregatesText),"Lists are not identical!");
443 for (sal_uInt16 i=0; i<SAL_N_ELEMENTS(ppAggregates); ++i)
445 if ( ppAggregatesText[i] )
446 ppAggregatesText[i]->Enable( !bReadOnly );
447 if ( ppAggregates[i] )
448 ppAggregates[i]->Enable( !bReadOnly );
452 void OFieldDescControl::SetControlText( sal_uInt16 nControlId, const OUString& rText )
454 // Set the Controls' texts
455 switch( nControlId )
457 case FIELD_PROPERTY_BOOL_DEFAULT:
458 if (pBoolDefault)
460 OUString sOld = pBoolDefault->GetSelectEntry();
461 pBoolDefault->SelectEntry(rText);
462 if (sOld != rText)
463 LINK(this, OFieldDescControl, ChangeHdl).Call(*pBoolDefault);
465 break;
466 case FIELD_PROPERTY_DEFAULT:
467 if (pDefault)
469 pDefault->SetText(rText);
470 UpdateFormatSample(pActFieldDescr);
472 break;
474 case FIELD_PROPERTY_REQUIRED:
475 if (pRequired)
476 pRequired->SelectEntry(rText);
477 break;
479 case FIELD_PROPERTY_TEXTLEN:
480 if (pTextLen)
481 pTextLen->SetText(rText);
482 break;
484 case FIELD_PROPERTY_NUMTYPE:
485 if (pNumType)
486 pNumType->SelectEntry(rText);
487 break;
489 case FIELD_PROPERTY_AUTOINC:
490 if (pAutoIncrement)
492 OUString sOld = pAutoIncrement->GetSelectEntry();
493 pAutoIncrement->SelectEntry(rText);
494 if (sOld != rText)
495 LINK(this, OFieldDescControl, ChangeHdl).Call(*pAutoIncrement);
497 break;
499 case FIELD_PROPERTY_LENGTH:
500 if (pLength)
501 pLength->SetText(rText);
502 break;
504 case FIELD_PROPERTY_SCALE:
505 if (pScale)
506 pScale->SetText(rText);
507 break;
509 case FIELD_PROPERTY_FORMAT:
510 if (pActFieldDescr)
511 UpdateFormatSample(pActFieldDescr);
512 break;
513 case FIELD_PROPERTY_COLUMNNAME:
514 if(m_pColumnName)
515 m_pColumnName->SetText(rText);
516 break;
517 case FIELD_PROPERTY_TYPE:
518 if(m_pType)
519 m_pType->SelectEntry(rText);
520 break;
521 case FIELD_PROPERTY_AUTOINCREMENT:
522 if(m_pAutoIncrementValue)
523 m_pAutoIncrementValue->SetText(rText);
524 break;
528 IMPL_LINK_NOARG_TYPED( OFieldDescControl, FormatClickHdl, Button *, void )
530 // Create temporary Column, which is used for data exchange with Dialog
531 if( !pActFieldDescr )
532 return;
534 sal_Int32 nOldFormatKey(pActFieldDescr->GetFormatKey());
535 SvxCellHorJustify rOldJustify = pActFieldDescr->GetHorJustify();
536 Reference< XNumberFormatsSupplier > xSupplier = GetFormatter()->getNumberFormatsSupplier();
537 SvNumberFormatsSupplierObj* pSupplierImpl = SvNumberFormatsSupplierObj::getImplementation( xSupplier );
538 if (!pSupplierImpl)
539 return;
541 SvNumberFormatter* pFormatter = pSupplierImpl->GetNumberFormatter();
542 if(::dbaui::callColumnFormatDialog(this,pFormatter,pActFieldDescr->GetType(),nOldFormatKey,rOldJustify,true))
544 bool bModified = false;
545 if(nOldFormatKey != pActFieldDescr->GetFormatKey())
547 pActFieldDescr->SetFormatKey( nOldFormatKey );
548 bModified = true;
550 if(rOldJustify != pActFieldDescr->GetHorJustify())
552 pActFieldDescr->SetHorJustify( rOldJustify );
553 bModified = true;
556 if(bModified)
558 SetModified(true);
559 UpdateFormatSample(pActFieldDescr);
564 void OFieldDescControl::SetModified(bool /*bModified*/)
568 IMPL_LINK_TYPED( OFieldDescControl, ChangeHdl, ListBox&, rListBox, void )
570 if ( !pActFieldDescr )
571 return;
573 if ( rListBox.IsValueChangedFromSaved() )
574 SetModified(true);
576 // Special treatment for Bool fields
577 if(&rListBox == pRequired && pBoolDefault )
579 // If pRequired = sal_True then the sal_Bool field must NOT contain <<none>>
580 OUString sDef = BoolStringUI(::comphelper::getString(pActFieldDescr->GetControlDefault()));
582 if(pRequired->GetSelectEntryPos() == 0) // Yes
584 pBoolDefault->RemoveEntry(OUString(ModuleRes(STR_VALUE_NONE)));
585 if (sDef != aYes && sDef != aNo)
586 pBoolDefault->SelectEntryPos(1); // No as a default
587 else
588 pBoolDefault->SelectEntry(sDef);
590 else if(pBoolDefault->GetEntryCount() < 3)
592 pBoolDefault->InsertEntry(OUString(ModuleRes(STR_VALUE_NONE)));
593 pBoolDefault->SelectEntry(sDef);
597 // A special treatment only for AutoIncrement
598 if (&rListBox == pAutoIncrement)
600 if(rListBox.GetSelectEntryPos() == 1)
601 { // no
602 DeactivateAggregate( tpAutoIncrementValue );
603 if(pActFieldDescr->IsPrimaryKey())
604 DeactivateAggregate( tpRequired );
605 else if( pActFieldDescr->getTypeInfo()->bNullable )
607 ActivateAggregate( tpRequired );
608 if(pRequired)
610 if( pActFieldDescr->IsNullable() )
611 pRequired->SelectEntryPos( 1 ); // no
612 else
613 pRequired->SelectEntryPos( 0 ); // yes
616 ActivateAggregate( tpDefault );
618 else
620 DeactivateAggregate( tpRequired );
621 DeactivateAggregate( tpDefault );
622 ActivateAggregate( tpAutoIncrementValue );
624 // Move all up
625 ArrangeAggregates();
628 if(&rListBox == m_pType)
630 TOTypeInfoSP pTypeInfo = getTypeInfo(m_pType->GetSelectEntryPos());
631 pActFieldDescr->FillFromTypeInfo(pTypeInfo,true,false); // SetType(pTypeInfo);
633 DisplayData(pActFieldDescr);
634 CellModified(-1, m_pType->GetPos());
638 // Rearrange all Controls, such that they are in fixed order and really on top
639 // of the DescriptionPage
640 void OFieldDescControl::ArrangeAggregates()
642 // A Control's description
643 struct AGGREGATE_DESCRIPTION
645 VclPtr<Control> pctrlInputControl; // The actual Control for input
646 VclPtr<Control> pctrlTextControl; // The corresponding Label
647 sal_uInt16 nPosSizeArgument; // The second argument for SetPosSize
649 AGGREGATE_DESCRIPTION adAggregates[] = {
650 { m_pColumnName, m_pColumnNameText, 1},
651 { m_pType, m_pTypeText, 1},
652 { pAutoIncrement, pAutoIncrementText, 1 },
653 { m_pAutoIncrementValue, m_pAutoIncrementValueText, 3 },
654 { pNumType, pNumTypeText, 1 },
655 { pRequired, pRequiredText, 1 },
656 { pTextLen, pTextLenText, 1 },
657 { pLength, pLengthText, 1 },
658 { pScale, pScaleText, 1 },
659 { pDefault, pDefaultText, 3 },
660 { pFormatSample, pFormatText, 4 },
661 { pBoolDefault, pBoolDefaultText, 1 },
664 long nMaxWidth = 0;
665 for (const AGGREGATE_DESCRIPTION & adAggregate : adAggregates)
667 if (adAggregate.pctrlTextControl)
669 nMaxWidth = ::std::max<long>(OutputDevice::GetTextWidth(adAggregate.pctrlTextControl->GetText()),nMaxWidth);
673 OSL_ENSURE(nMaxWidth != 0,"Invalid width!");
675 // And go ...
676 int nCurrentControlPos = 0;
677 Control* pZOrderPredecessor = nullptr;
678 for (AGGREGATE_DESCRIPTION & adAggregate : adAggregates)
680 if (adAggregate.pctrlInputControl)
682 SetPosSize(adAggregate.pctrlTextControl, nCurrentControlPos, 0);
683 SetPosSize(adAggregate.pctrlInputControl, nCurrentControlPos, adAggregate.nPosSizeArgument);
685 // Set the z-order in a way such that the Controls can be traversed in the same sequence in which they have been arranged here
686 adAggregate.pctrlTextControl->SetZOrder(pZOrderPredecessor, pZOrderPredecessor ? ZOrderFlags::Behind : ZOrderFlags::First);
687 adAggregate.pctrlInputControl->SetZOrder(adAggregate.pctrlTextControl, ZOrderFlags::Behind );
688 pZOrderPredecessor = adAggregate.pctrlInputControl;
690 if (adAggregate.pctrlInputControl == pFormatSample)
692 pFormat->SetZOrder(pZOrderPredecessor, ZOrderFlags::Behind);
693 pZOrderPredecessor = pFormat;
696 ++nCurrentControlPos;
700 // Special treatment for the Format Controls
701 if (pFormat)
703 Point ptSamplePos(pFormatSample->GetPosPixel());
704 Size szSampleSize(pFormatSample->GetSizePixel());
705 pFormat->SetPosPixel(Point(ptSamplePos.X() + szSampleSize.Width() + 5, ptSamplePos.Y()));
708 // Finally, put the ScrollBars at the top of the z-order
709 m_pVertScroll->SetZOrder(nullptr, ZOrderFlags::First);
710 m_pHorzScroll->SetZOrder(nullptr, ZOrderFlags::First);
713 void OFieldDescControl::ActivateAggregate( EControlType eType )
715 // Create Controls
716 switch( eType )
718 case tpDefault:
719 if( pDefault )
720 return;
721 m_nPos++;
722 pDefaultText = CreateText(STR_DEFAULT_VALUE);
723 pDefault = VclPtr<OPropEditCtrl>::Create( this, STR_HELP_DEFAULT_VALUE, FIELD_PROPERTY_DEFAULT, WB_BORDER );
724 InitializeControl(pDefault,HID_TAB_ENT_DEFAULT,false);
725 break;
726 case tpAutoIncrementValue:
727 if( m_pAutoIncrementValue || !isAutoIncrementValueEnabled() )
728 return;
729 m_nPos++;
730 m_pAutoIncrementValueText = CreateText(STR_AUTOINCREMENT_VALUE);
731 m_pAutoIncrementValue = VclPtr<OPropEditCtrl>::Create( this, STR_HELP_AUTOINCREMENT_VALUE, FIELD_PROPERTY_AUTOINCREMENT, WB_BORDER );
732 m_pAutoIncrementValue->SetText( getAutoIncrementValue() );
733 InitializeControl(m_pAutoIncrementValue,HID_TAB_AUTOINCREMENTVALUE,false);
734 break;
736 case tpRequired:
738 if( pRequired )
739 return;
740 Reference< XDatabaseMetaData> xMetaData = getMetaData();
742 if(xMetaData.is() && xMetaData->supportsNonNullableColumns())
744 m_nPos++;
745 pRequiredText = CreateText(STR_FIELD_REQUIRED);
746 pRequired = VclPtr<OPropListBoxCtrl>::Create( this, STR_HELP_FIELD_REQUIRED, FIELD_PROPERTY_REQUIRED, WB_DROPDOWN);
748 pRequired->InsertEntry( aYes );
749 pRequired->InsertEntry( aNo );
750 pRequired->SelectEntryPos(1);
752 InitializeControl(pRequired,HID_TAB_ENT_REQUIRED,true);
755 break;
756 case tpAutoIncrement:
758 if( pAutoIncrement )
759 return;
760 m_nPos++;
761 pAutoIncrementText = CreateText(STR_FIELD_AUTOINCREMENT);
762 pAutoIncrement = VclPtr<OPropListBoxCtrl>::Create( this, STR_HELP_AUTOINCREMENT, FIELD_PROPERTY_AUTOINC, WB_DROPDOWN );
763 pAutoIncrement->InsertEntry( aYes );
764 pAutoIncrement->InsertEntry( aNo );
765 pAutoIncrement->SelectEntryPos(0);
766 InitializeControl(pAutoIncrement,HID_TAB_ENT_AUTOINCREMENT,true);
768 break;
769 case tpTextLen:
770 if( pTextLen )
771 return;
772 m_nPos++;
773 pTextLenText = CreateText(STR_TEXT_LENGTH);
774 pTextLen = CreateNumericControl(STR_HELP_TEXT_LENGTH, FIELD_PROPERTY_TEXTLEN,HID_TAB_ENT_TEXT_LEN);
775 break;
777 case tpType:
778 if( m_pType)
779 return;
780 m_nPos++;
781 m_pTypeText = CreateText(STR_TAB_FIELD_DATATYPE);
782 m_pType = VclPtr<OPropListBoxCtrl>::Create( this, STR_HELP_AUTOINCREMENT, FIELD_PROPERTY_TYPE, WB_DROPDOWN );
783 m_pType->SetDropDownLineCount(20);
785 const OTypeInfoMap* pTypeInfo = getTypeInfo();
786 OTypeInfoMap::const_iterator aIter = pTypeInfo->begin();
787 OTypeInfoMap::const_iterator aEnd = pTypeInfo->end();
788 for(;aIter != aEnd;++aIter)
789 m_pType->InsertEntry( aIter->second->aUIName );
791 m_pType->SelectEntryPos(0);
792 InitializeControl(m_pType,HID_TAB_ENT_TYPE,true);
793 break;
794 case tpColumnName:
795 if( m_pColumnName )
796 return;
797 m_nPos++;
799 sal_Int32 nMax = EDIT_NOLIMIT;
800 OUString aTmpString;
803 Reference< XDatabaseMetaData> xMetaData = getMetaData();
804 if ( xMetaData.is() )
806 nMax = xMetaData->getMaxColumnNameLength();
807 aTmpString = xMetaData->getExtraNameCharacters();
810 catch (const Exception&)
812 DBG_UNHANDLED_EXCEPTION();
814 m_pColumnNameText = CreateText(STR_TAB_FIELD_NAME);
815 m_pColumnName = VclPtr<OPropColumnEditCtrl>::Create( this,
816 aTmpString,
817 STR_HELP_DEFAULT_VALUE,
818 FIELD_PROPERTY_COLUMNNAME,
819 WB_BORDER );
820 m_pColumnName->SetMaxTextLen(nMax ? nMax : EDIT_NOLIMIT);
821 m_pColumnName->setCheck( isSQL92CheckEnabled(getConnection()) );
824 InitializeControl(m_pColumnName,HID_TAB_ENT_COLUMNNAME,false);
825 break;
826 case tpNumType:
827 if( pNumType )
828 return;
829 m_nPos++;
830 pNumTypeText = CreateText(STR_NUMERIC_TYPE);
832 pNumType = VclPtr<OPropListBoxCtrl>::Create( this, STR_HELP_NUMERIC_TYPE, FIELD_PROPERTY_NUMTYPE, WB_DROPDOWN );
833 pNumType->SetDropDownLineCount(5);
835 pNumType->InsertEntry( OUString("Byte") );
836 pNumType->InsertEntry( OUString("SmallInt") );
837 pNumType->InsertEntry( OUString("Integer") );
838 pNumType->InsertEntry( OUString("Single") );
839 pNumType->InsertEntry( OUString("Double") );
840 pNumType->SelectEntryPos(2);
841 InitializeControl(pNumType,HID_TAB_ENT_NUMTYP,true);
842 break;
844 case tpLength:
845 if( pLength )
846 return;
847 m_nPos++;
848 pLengthText = CreateText(STR_LENGTH);
849 pLength = CreateNumericControl(STR_HELP_LENGTH, FIELD_PROPERTY_LENGTH,HID_TAB_ENT_LEN);
850 break;
852 case tpScale:
853 if( pScale )
854 return;
855 m_nPos++;
856 pScaleText = CreateText(STR_SCALE);
857 pScale = CreateNumericControl(STR_HELP_SCALE, FIELD_PROPERTY_SCALE,HID_TAB_ENT_SCALE);
858 break;
860 case tpFormat:
861 if (!pFormat)
863 m_nPos++;
864 pFormatText = CreateText(STR_FORMAT);
866 pFormatSample = VclPtr<OPropEditCtrl>::Create( this, STR_HELP_FORMAT_CODE, -1, WB_BORDER );
867 pFormatSample->SetReadOnly();
868 pFormatSample->Enable(false);
869 InitializeControl(pFormatSample,HID_TAB_ENT_FORMAT_SAMPLE,false);
871 pFormat = VclPtr<PushButton>::Create( this, ModuleRes(PB_FORMAT) );
872 const sal_Int32 nControlHeight = GetMaxControlHeight();
873 pFormat->SetSizePixel(Size(nControlHeight, nControlHeight));
874 pFormat->SetClickHdl( LINK( this, OFieldDescControl, FormatClickHdl ) );
875 InitializeControl(pFormat,HID_TAB_ENT_FORMAT,false);
878 UpdateFormatSample(pActFieldDescr);
879 break;
880 case tpBoolDefault:
881 if (pBoolDefault)
882 return;
884 m_nPos++;
885 pBoolDefaultText = CreateText(STR_DEFAULT_VALUE);
886 pBoolDefault = VclPtr<OPropListBoxCtrl>::Create( this, STR_HELP_BOOL_DEFAULT, FIELD_PROPERTY_BOOL_DEFAULT, WB_DROPDOWN );
887 pBoolDefault->SetDropDownLineCount(3);
888 pBoolDefault->InsertEntry(OUString(ModuleRes(STR_VALUE_NONE)));
889 pBoolDefault->InsertEntry(aYes);
890 pBoolDefault->InsertEntry(aNo);
892 InitializeControl(pBoolDefault,HID_TAB_ENT_BOOL_DEFAULT,false);
893 break;
897 void OFieldDescControl::InitializeControl(Control* _pControl,const OString& _sHelpId,bool _bAddChangeHandler)
899 _pControl->SetHelpId(_sHelpId);
900 if ( _bAddChangeHandler )
901 static_cast<OPropListBoxCtrl*>(_pControl)->SetSelectHdl(LINK(this,OFieldDescControl,ChangeHdl));
903 _pControl->SetGetFocusHdl(LINK(this, OFieldDescControl, OnControlFocusGot));
904 _pControl->SetLoseFocusHdl(LINK(this, OFieldDescControl, OnControlFocusLost));
905 _pControl->EnableClipSiblings();
908 VclPtr<FixedText> OFieldDescControl::CreateText(sal_uInt16 _nTextRes)
910 VclPtrInstance<FixedText> pFixedText( this );
911 pFixedText->SetText( ModuleRes(_nTextRes) );
912 pFixedText->EnableClipSiblings();
913 return pFixedText;
916 VclPtr<OPropNumericEditCtrl> OFieldDescControl::CreateNumericControl(sal_uInt16 _nHelpStr,short _nProperty,const OString& _sHelpId)
918 VclPtrInstance<OPropNumericEditCtrl> pControl( this, _nHelpStr, _nProperty, WB_BORDER );
919 pControl->SetDecimalDigits(0);
920 pControl->SetMin(0);
921 pControl->SetMax(0x7FFFFFFF); // Should be changed outside, if needed
922 pControl->SetStrictFormat(true);
924 InitializeControl(pControl,_sHelpId,false);
926 return pControl;
929 void OFieldDescControl::DeactivateAggregate( EControlType eType )
931 pLastFocusWindow = nullptr;
932 // Destroy Controls
933 switch( eType )
935 case tpDefault:
936 lcl_HideAndDeleteControl(m_nPos,pDefault,pDefaultText);
937 break;
939 case tpAutoIncrementValue:
940 lcl_HideAndDeleteControl(m_nPos,m_pAutoIncrementValue,m_pAutoIncrementValueText);
941 break;
943 case tpColumnName:
944 lcl_HideAndDeleteControl(m_nPos,m_pColumnName,m_pColumnNameText);
945 break;
947 case tpType:
948 lcl_HideAndDeleteControl(m_nPos,m_pType,m_pTypeText);
949 break;
951 case tpAutoIncrement:
952 lcl_HideAndDeleteControl(m_nPos,pAutoIncrement,pAutoIncrementText);
953 break;
955 case tpRequired:
956 lcl_HideAndDeleteControl(m_nPos,pRequired,pRequiredText);
957 break;
959 case tpTextLen:
960 lcl_HideAndDeleteControl(m_nPos,pTextLen,pTextLenText);
961 break;
963 case tpNumType:
964 lcl_HideAndDeleteControl(m_nPos,pNumType,pNumTypeText);
965 break;
967 case tpLength:
968 lcl_HideAndDeleteControl(m_nPos,pLength,pLengthText);
969 break;
971 case tpScale:
972 lcl_HideAndDeleteControl(m_nPos,pScale,pScaleText);
973 break;
975 case tpFormat:
976 // TODO: we have to check if we have to increment m_nPos again
977 lcl_HideAndDeleteControl(m_nPos,pFormat,pFormatText);
978 if ( pFormatSample )
980 pFormatSample->Hide();
981 pFormatSample.disposeAndClear();
983 break;
984 case tpBoolDefault:
985 lcl_HideAndDeleteControl(m_nPos,pBoolDefault,pBoolDefaultText);
986 break;
990 void OFieldDescControl::SetPosSize( VclPtr<Control>& rControl, long nRow, sal_uInt16 nCol )
993 // Calculate size
994 const sal_Int32 nControlHeight = GetMaxControlHeight();
995 Size aSize(0,nControlHeight);
996 if ( isRightAligned() && nCol )
997 aSize.Width() = LogicToPixel(Size(m_nWidth, 0),MAP_APPFONT).Width();
998 else
1000 switch( nCol )
1002 case 0:
1003 default:
1004 aSize.Width() = CONTROL_WIDTH_1;
1005 break;
1006 case 1:
1007 aSize.Width() = CONTROL_WIDTH_2;
1008 break;
1009 case 3:
1010 aSize.Width() = CONTROL_WIDTH_3;
1011 break;
1012 case 4:
1013 aSize.Width() = CONTROL_WIDTH_4;
1014 break;
1018 // Calculate Position
1019 Point aPosition;
1020 switch( nCol )
1022 case 0:
1023 aPosition.X() = 0;
1024 aPosition.Y() = 1;
1025 break;
1026 case 1:
1027 case 3:
1028 case 4:
1029 if ( isRightAligned() )
1031 Size aOwnSize = GetSizePixel();
1032 aPosition.X() = aOwnSize.Width() - aSize.Width();
1034 else
1035 aPosition.X() = CONTROL_WIDTH_1 + CONTROL_SPACING_X;
1036 break;
1037 default:
1038 aPosition.X() = 0;
1041 rControl->SetSizePixel( aSize );
1042 aSize = rControl->GetSizePixel( );
1044 const sal_Int32 nControl_Spacing_y = LogicToPixel(Size(0, CONTROL_SPACING_Y),MAP_APPFONT).Height();
1045 aPosition.Y() += ((nRow+1)*nControl_Spacing_y) +
1046 (nRow*nControlHeight);
1048 // Display Control
1049 rControl->SetPosSizePixel( aPosition, aSize );
1050 aSize = rControl->GetSizePixel();
1052 rControl->Show();
1055 void OFieldDescControl::DisplayData(OFieldDescription* pFieldDescr )
1057 pActFieldDescr = pFieldDescr;
1058 if(!pFieldDescr)
1060 DeactivateAggregate( tpDefault );
1061 DeactivateAggregate( tpRequired );
1062 DeactivateAggregate( tpTextLen );
1063 DeactivateAggregate( tpNumType );
1064 DeactivateAggregate( tpScale );
1065 DeactivateAggregate( tpLength );
1066 DeactivateAggregate( tpFormat );
1067 DeactivateAggregate( tpAutoIncrement );
1068 DeactivateAggregate( tpBoolDefault );
1069 DeactivateAggregate( tpColumnName );
1070 DeactivateAggregate( tpType );
1071 DeactivateAggregate( tpAutoIncrementValue );
1072 m_pPreviousType = TOTypeInfoSP();
1073 // Reset the saved focus' pointer
1074 pLastFocusWindow = nullptr;
1075 if ( m_bAdded )
1077 ::dbaui::notifySystemWindow(this,this,::comphelper::mem_fun(&TaskPaneList::RemoveWindow));
1078 m_bAdded = false;
1080 return;
1083 if ( !m_bAdded )
1085 ::dbaui::notifySystemWindow(this,this,::comphelper::mem_fun(&TaskPaneList::AddWindow));
1086 m_bAdded = true;
1089 TOTypeInfoSP pFieldType;
1090 if( pFieldDescr )
1091 pFieldType = pFieldDescr->getTypeInfo();
1093 ActivateAggregate( tpColumnName );
1094 ActivateAggregate( tpType );
1096 OSL_ENSURE(pFieldType.get(),"We need a type information here!");
1097 // If the type has changed, substitute Controls
1098 if( m_pPreviousType != pFieldType )
1100 // Reset the saved focus' pointer
1101 pLastFocusWindow = nullptr;
1103 // Controls, which must NOT be displayed again
1104 DeactivateAggregate( tpNumType );
1106 // determine which controls we should show and which not
1108 // 1. the required control
1109 if ( pFieldType->bNullable )
1110 ActivateAggregate( tpRequired );
1111 else
1112 DeactivateAggregate( tpRequired );
1114 // 2. the autoincrement
1115 if ( pFieldType->bAutoIncrement )
1117 DeactivateAggregate( tpRequired );
1118 DeactivateAggregate( tpDefault );
1119 ActivateAggregate( tpAutoIncrement );
1120 ActivateAggregate( tpAutoIncrementValue );
1122 else
1124 DeactivateAggregate( tpAutoIncrement );
1125 DeactivateAggregate( tpAutoIncrementValue );
1126 if(pFieldType->bNullable)
1127 ActivateAggregate( tpRequired );
1128 else
1129 DeactivateAggregate( tpRequired );
1130 ActivateAggregate( tpDefault );
1132 // 3. the scale and precision
1133 if (pFieldType->nPrecision)
1135 ActivateAggregate( tpLength );
1136 pLength->SetMax(::std::max<sal_Int32>(pFieldType->nPrecision,pFieldDescr->GetPrecision()));
1137 pLength->SetSpecialReadOnly(pFieldType->aCreateParams.isEmpty());
1139 else
1140 DeactivateAggregate( tpLength );
1142 if (pFieldType->nMaximumScale)
1144 ActivateAggregate( tpScale );
1145 pScale->SetMax(::std::max<sal_Int32>(pFieldType->nMaximumScale,pFieldDescr->GetScale()));
1146 pScale->SetMin(pFieldType->nMinimumScale);
1147 static const char s_sPRECISION[] = "PRECISION";
1148 pScale->SetSpecialReadOnly(pFieldType->aCreateParams.isEmpty() || pFieldType->aCreateParams == s_sPRECISION);
1150 else
1151 DeactivateAggregate( tpScale );
1153 // and now look for type specific things
1154 switch( pFieldType->nType )
1156 case DataType::CHAR:
1157 case DataType::VARCHAR:
1158 case DataType::LONGVARCHAR:
1159 DeactivateAggregate( tpLength );
1160 DeactivateAggregate( tpBoolDefault );
1162 ActivateAggregate( tpDefault );
1163 ActivateAggregate( tpFormat );
1164 if (pFieldType->nPrecision)
1166 ActivateAggregate( tpTextLen );
1167 pTextLen->SetMax(::std::max<sal_Int32>(pFieldType->nPrecision,pFieldDescr->GetPrecision()));
1168 pTextLen->SetSpecialReadOnly(pFieldType->aCreateParams.isEmpty());
1170 else
1171 DeactivateAggregate( tpTextLen );
1172 break;
1173 case DataType::DATE:
1174 case DataType::TIME:
1175 case DataType::TIMESTAMP:
1176 DeactivateAggregate( tpLength ); // we don't need a length for date types
1177 DeactivateAggregate( tpTextLen );
1178 DeactivateAggregate( tpBoolDefault );
1180 ActivateAggregate( tpDefault );
1181 ActivateAggregate( tpFormat );
1182 break;
1183 case DataType::BIT:
1184 if ( !pFieldType->aCreateParams.isEmpty() )
1186 DeactivateAggregate( tpFormat );
1187 DeactivateAggregate( tpTextLen );
1188 DeactivateAggregate( tpBoolDefault );
1189 break;
1191 SAL_FALLTHROUGH;
1192 case DataType::BOOLEAN:
1193 DeactivateAggregate( tpTextLen );
1194 DeactivateAggregate( tpFormat );
1195 DeactivateAggregate( tpDefault );
1197 ActivateAggregate( tpBoolDefault );
1198 break;
1199 case DataType::DECIMAL:
1200 case DataType::NUMERIC:
1201 case DataType::BIGINT:
1202 case DataType::FLOAT:
1203 case DataType::DOUBLE:
1204 case DataType::TINYINT:
1205 case DataType::SMALLINT:
1206 case DataType::INTEGER:
1207 case DataType::REAL:
1208 DeactivateAggregate( tpTextLen );
1209 DeactivateAggregate( tpBoolDefault );
1211 ActivateAggregate( tpFormat );
1212 break;
1213 case DataType::BINARY:
1214 case DataType::VARBINARY:
1215 DeactivateAggregate( tpDefault );
1216 DeactivateAggregate( tpRequired );
1217 DeactivateAggregate( tpTextLen );
1218 DeactivateAggregate( tpBoolDefault );
1220 ActivateAggregate( tpFormat );
1221 break;
1222 case DataType::LONGVARBINARY:
1223 case DataType::SQLNULL:
1224 case DataType::OBJECT:
1225 case DataType::DISTINCT:
1226 case DataType::STRUCT:
1227 case DataType::ARRAY:
1228 case DataType::BLOB:
1229 case DataType::CLOB:
1230 case DataType::REF:
1231 case DataType::OTHER:
1232 DeactivateAggregate( tpFormat );
1233 DeactivateAggregate( tpTextLen );
1234 DeactivateAggregate( tpBoolDefault );
1236 break;
1237 default:
1238 OSL_FAIL("Unknown type");
1240 m_pPreviousType = pFieldType;
1242 if(pFieldDescr)
1244 if(pFieldDescr->IsPrimaryKey())
1246 DeactivateAggregate( tpRequired );
1248 else if ( !pAutoIncrement && pFieldType.get() )
1250 if ( pFieldType->bNullable )
1251 ActivateAggregate( tpRequired );
1252 else
1253 DeactivateAggregate( tpRequired );
1256 // Initialize Controls
1257 if( pAutoIncrement )
1259 if ( pFieldDescr->IsAutoIncrement() )
1261 pAutoIncrement->SelectEntryPos( 0 ); // yes
1262 ActivateAggregate( tpAutoIncrementValue );
1263 if ( m_pAutoIncrementValue )
1264 m_pAutoIncrementValue->SetText(pFieldDescr->GetAutoIncrementValue());
1265 DeactivateAggregate( tpRequired );
1266 DeactivateAggregate( tpDefault );
1268 else
1270 // disable autoincrement value because it should only be visible when autoincrement is to true
1271 DeactivateAggregate( tpAutoIncrementValue );
1272 pAutoIncrement->SelectEntryPos( 1 ); // no
1273 ActivateAggregate( tpDefault );
1274 // Affects pRequired
1275 if(!pFieldDescr->IsPrimaryKey())
1276 ActivateAggregate( tpRequired );
1280 if( pDefault )
1282 pDefault->SetText( getControlDefault(pFieldDescr) );
1283 pDefault->ClearModifyFlag();
1286 if( pBoolDefault )
1288 // If pRequired = sal_True then the sal_Bool field must NOT contain <<none>>
1289 OUString sValue;
1290 pFieldDescr->GetControlDefault() >>= sValue;
1291 OUString sDef = BoolStringUI(sValue);
1293 // Make sure that <<none>> is only present if the field can be NULL
1294 if ( ( pFieldType.get() && !pFieldType->bNullable ) || !pFieldDescr->IsNullable() )
1296 pFieldDescr->SetIsNullable(ColumnValue::NO_NULLS); // The type says so
1298 pBoolDefault->RemoveEntry(OUString(ModuleRes(STR_VALUE_NONE)));
1299 if ( sDef != aYes && sDef != aNo )
1300 pBoolDefault->SelectEntryPos(1); // No as a default
1301 else
1302 pBoolDefault->SelectEntry(sDef);
1304 pFieldDescr->SetControlDefault(makeAny(OUString(BoolStringPersistent(pBoolDefault->GetSelectEntry()))));
1306 else if(pBoolDefault->GetEntryCount() < 3)
1308 pBoolDefault->InsertEntry(OUString(ModuleRes(STR_VALUE_NONE)));
1309 pBoolDefault->SelectEntry(sDef);
1311 else
1312 pBoolDefault->SelectEntry(sDef);
1315 if( pRequired )
1317 if( pFieldDescr->IsNullable() )
1318 pRequired->SelectEntryPos( 1 ); // no
1319 else
1320 pRequired->SelectEntryPos( 0 ); // yes
1323 if( pTextLen )
1325 pTextLen->SetText( OUString::number(pFieldDescr->GetPrecision()) );
1326 pTextLen->ClearModifyFlag();
1329 if( pNumType )
1331 OSL_FAIL("OFieldDescControl::DisplayData: invalid num type!");
1334 if( pLength )
1335 pLength->SetText( OUString::number(pFieldDescr->GetPrecision()) );
1337 if( pScale )
1338 pScale->SetText( OUString::number(pFieldDescr->GetScale()) );
1340 if( pFormat )
1341 UpdateFormatSample(pFieldDescr);
1343 if(m_pColumnName)
1344 m_pColumnName->SetText(pFieldDescr->GetName());
1346 if(m_pType)
1348 sal_Int32 nPos = pFieldType.get() ? m_pType->GetEntryPos(OUString(pFieldDescr->getTypeInfo()->aUIName)) : LISTBOX_ENTRY_NOTFOUND;
1349 if(nPos == LISTBOX_ENTRY_NOTFOUND)
1351 const OTypeInfoMap* pMap = getTypeInfo();
1352 OTypeInfoMap::const_iterator aIter = pMap->find(pFieldType.get() ? pFieldDescr->getTypeInfo()->nType : pFieldDescr->GetType());
1353 if(aIter == pMap->end() && !pMap->empty())
1355 aIter = pMap->begin();
1356 if(pFieldDescr->GetPrecision() > aIter->second->nPrecision)
1357 pFieldDescr->SetPrecision(aIter->second->nPrecision);
1358 if(pFieldDescr->GetScale() > aIter->second->nMaximumScale)
1359 pFieldDescr->SetScale(0);
1360 if(!aIter->second->bNullable && pFieldDescr->IsNullable())
1361 pFieldDescr->SetIsNullable(ColumnValue::NO_NULLS);
1362 if(!aIter->second->bAutoIncrement && pFieldDescr->IsAutoIncrement())
1363 pFieldDescr->SetAutoIncrement(false);
1365 if ( aIter != pMap->end() )
1367 pFieldDescr->SetType(aIter->second);
1370 m_pType->SelectEntry(pFieldDescr->getTypeInfo()->aUIName);
1373 // Enable/disable Controls
1374 bool bRead(IsReadOnly());
1376 ArrangeAggregates();
1377 CheckScrollBars();
1378 ScrollAllAggregates();
1380 SetReadOnly( bRead );
1383 IMPL_LINK_TYPED(OFieldDescControl, OnControlFocusGot, Control&, rControl, void )
1385 OUString strHelpText;
1386 OPropNumericEditCtrl* pNumeric = dynamic_cast< OPropNumericEditCtrl* >( &rControl );
1387 if ( pNumeric )
1389 pNumeric->SaveValue();
1390 strHelpText = pNumeric->GetHelp();
1393 OPropColumnEditCtrl* pColumn = dynamic_cast< OPropColumnEditCtrl* >( &rControl );
1394 if ( pColumn )
1396 pColumn->SaveValue();
1397 strHelpText = pColumn->GetHelp();
1400 OPropEditCtrl* pEdit = dynamic_cast< OPropEditCtrl* >( &rControl );
1401 if ( pEdit )
1403 pEdit->SaveValue();
1404 strHelpText = pEdit->GetHelp();
1407 OPropListBoxCtrl* pListBox = dynamic_cast< OPropListBoxCtrl* >( &rControl );
1408 if ( pListBox )
1410 pListBox->SaveValue();
1411 strHelpText = pListBox->GetHelp();
1414 if (&rControl == pFormat)
1415 strHelpText = ModuleRes(STR_HELP_FORMAT_BUTTON);
1417 if (!strHelpText.isEmpty() && (pHelp != nullptr))
1418 pHelp->SetHelpText(strHelpText);
1420 m_pActFocusWindow = &rControl;
1423 IMPL_LINK_TYPED(OFieldDescControl, OnControlFocusLost, Control&, rControl, void )
1425 if ((&rControl == pLength) || (&rControl == pTextLen) || (&rControl == pScale))
1427 OPropNumericEditCtrl* pConverted = static_cast<OPropNumericEditCtrl*>(&rControl);
1428 if (pConverted->IsModified())
1429 CellModified(-1, pConverted->GetPos());
1431 if(&rControl == m_pColumnName)
1433 OPropColumnEditCtrl* pConverted = static_cast<OPropColumnEditCtrl*>(&rControl);
1434 if (pConverted->IsModified())
1435 CellModified(-1, pConverted->GetPos());
1437 else if ((&rControl == pDefault) || (&rControl == pFormatSample) || (&rControl == m_pAutoIncrementValue) )
1439 OPropEditCtrl* pConverted = static_cast<OPropEditCtrl*>(&rControl);
1440 if (pConverted->IsModified())
1441 CellModified(-1, pConverted->GetPos());
1443 else if ((&rControl == pRequired) || (&rControl == pNumType) || (&rControl == pAutoIncrement) || (&rControl == pBoolDefault) || (&rControl == m_pType))
1445 OPropListBoxCtrl* pConverted = static_cast<OPropListBoxCtrl*>(&rControl);
1446 if (pConverted->IsModified())
1447 CellModified(-1, pConverted->GetPos());
1450 if (&rControl == pDefault)
1451 UpdateFormatSample(pActFieldDescr);
1453 implFocusLost(&rControl);
1456 void OFieldDescControl::SaveData( OFieldDescription* pFieldDescr )
1458 if( !pFieldDescr )
1459 return;
1461 // Read out Controls
1462 OUString sDefault;
1463 if (pDefault)
1465 sDefault = pDefault->GetText();
1467 else if (pBoolDefault)
1469 sDefault = BoolStringPersistent(pBoolDefault->GetSelectEntry());
1472 if ( !sDefault.isEmpty() )
1473 pFieldDescr->SetControlDefault(makeAny(sDefault));
1474 else
1475 pFieldDescr->SetControlDefault(Any());
1477 if((pRequired && pRequired->GetSelectEntryPos() == 0) || pFieldDescr->IsPrimaryKey() || (pBoolDefault && pBoolDefault->GetEntryCount() == 2)) // yes
1478 pFieldDescr->SetIsNullable( ColumnValue::NO_NULLS );
1479 else
1480 pFieldDescr->SetIsNullable( ColumnValue::NULLABLE );
1482 if ( pAutoIncrement )
1483 pFieldDescr->SetAutoIncrement( pAutoIncrement->GetSelectEntryPos() == 0 );
1485 if( pTextLen )
1486 pFieldDescr->SetPrecision( static_cast<sal_Int32>(pTextLen->GetValue()) );
1487 else if( pLength )
1488 pFieldDescr->SetPrecision( static_cast<sal_Int32>(pLength->GetValue()) );
1489 if( pScale )
1490 pFieldDescr->SetScale( static_cast<sal_Int32>(pScale->GetValue()) );
1492 if(m_pColumnName)
1493 pFieldDescr->SetName(m_pColumnName->GetText());
1495 if ( m_pAutoIncrementValue && isAutoIncrementValueEnabled() )
1496 pFieldDescr->SetAutoIncrementValue(m_pAutoIncrementValue->GetText());
1499 void OFieldDescControl::UpdateFormatSample(OFieldDescription* pFieldDescr)
1501 if ( pFieldDescr && pFormatSample )
1502 pFormatSample->SetText(getControlDefault(pFieldDescr,false));
1505 void OFieldDescControl::GetFocus()
1507 // Set the Focus to the Control that has been active last
1508 TabPage::GetFocus();
1509 if( pLastFocusWindow )
1511 pLastFocusWindow->GrabFocus();
1512 pLastFocusWindow = nullptr;
1516 void OFieldDescControl::implFocusLost(vcl::Window* _pWhich)
1518 OSL_ENSURE(!_pWhich || IsChild(_pWhich), "OFieldDescControl::implFocusLost : invalid window !");
1520 // Remember the active Control
1521 if (!pLastFocusWindow)
1522 pLastFocusWindow = _pWhich;
1524 // Reset HelpText
1525 if (pHelp && !pHelp->HasChildPathFocus())
1526 pHelp->SetHelpText( OUString() );
1529 void OFieldDescControl::LoseFocus()
1531 implFocusLost(nullptr);
1533 TabPage::LoseFocus();
1536 bool OFieldDescControl::isCopyAllowed()
1538 bool bAllowed = (m_pActFocusWindow != nullptr) &&
1539 (m_pActFocusWindow == pDefault || m_pActFocusWindow == pFormatSample ||
1540 m_pActFocusWindow == pTextLen || m_pActFocusWindow == pLength ||
1541 m_pActFocusWindow == pScale || m_pActFocusWindow == m_pColumnName ||
1542 m_pActFocusWindow == m_pAutoIncrementValue) &&
1543 !static_cast<Edit*>(m_pActFocusWindow.get())->GetSelected().isEmpty();
1545 return bAllowed;
1548 bool OFieldDescControl::isCutAllowed()
1550 bool bAllowed = (m_pActFocusWindow != nullptr) &&
1551 (m_pActFocusWindow == pDefault || m_pActFocusWindow == pFormatSample ||
1552 m_pActFocusWindow == pTextLen || m_pActFocusWindow == pLength ||
1553 m_pActFocusWindow == pScale || m_pActFocusWindow == m_pColumnName ||
1554 m_pActFocusWindow == m_pAutoIncrementValue) &&
1555 !static_cast<Edit*>(m_pActFocusWindow.get())->GetSelected().isEmpty();
1556 return bAllowed;
1559 bool OFieldDescControl::isPasteAllowed()
1561 bool bAllowed = (m_pActFocusWindow != nullptr) &&
1562 (m_pActFocusWindow == pDefault || m_pActFocusWindow == pFormatSample ||
1563 m_pActFocusWindow == pTextLen || m_pActFocusWindow == pLength ||
1564 m_pActFocusWindow == pScale || m_pActFocusWindow == m_pColumnName ||
1565 m_pActFocusWindow == m_pAutoIncrementValue);
1566 if ( bAllowed )
1568 TransferableDataHelper aTransferData(TransferableDataHelper::CreateFromSystemClipboard(GetParent()));
1569 bAllowed = aTransferData.HasFormat(SotClipboardFormatId::STRING);
1571 return bAllowed;
1574 void OFieldDescControl::cut()
1576 if(isCutAllowed())
1577 static_cast<Edit*>(m_pActFocusWindow.get())->Cut();
1580 void OFieldDescControl::copy()
1582 if(isCopyAllowed()) // this only checks if the focus window is valid
1583 static_cast<Edit*>(m_pActFocusWindow.get())->Copy();
1586 void OFieldDescControl::paste()
1588 if(m_pActFocusWindow) // this only checks if the focus window is valid
1589 static_cast<Edit*>(m_pActFocusWindow.get())->Paste();
1592 bool OFieldDescControl::isTextFormat(const OFieldDescription* _pFieldDescr, sal_uInt32& _nFormatKey) const
1594 _nFormatKey = _pFieldDescr->GetFormatKey();
1595 bool bTextFormat = true;
1599 if (!_nFormatKey)
1601 Reference< css::util::XNumberFormatTypes> xNumberTypes(GetFormatter()->getNumberFormatsSupplier()->getNumberFormats(),UNO_QUERY);
1602 OSL_ENSURE(xNumberTypes.is(),"XNumberFormatTypes is null!");
1604 _nFormatKey = ::dbtools::getDefaultNumberFormat( _pFieldDescr->GetType(),
1605 _pFieldDescr->GetScale(),
1606 _pFieldDescr->IsCurrency(),
1607 xNumberTypes,
1608 GetLocale());
1610 sal_Int32 nNumberFormat = ::comphelper::getNumberFormatType(GetFormatter(),_nFormatKey);
1611 bTextFormat = (nNumberFormat == css::util::NumberFormat::TEXT);
1613 catch(const Exception&)
1618 return bTextFormat;
1621 OUString OFieldDescControl::getControlDefault( const OFieldDescription* _pFieldDescr, bool _bCheck) const
1623 OUString sDefault;
1624 bool bCheck = !_bCheck || _pFieldDescr->GetControlDefault().hasValue();
1625 if ( bCheck )
1627 sal_uInt32 nFormatKey;
1631 double nValue = 0.0;
1632 bool bTextFormat = isTextFormat(_pFieldDescr,nFormatKey);
1633 if ( _pFieldDescr->GetControlDefault() >>= sDefault )
1635 if ( !bTextFormat )
1637 if ( !sDefault.isEmpty() )
1641 nValue = GetFormatter()->convertStringToNumber(nFormatKey,sDefault);
1643 catch(const Exception&)
1645 return OUString(); // return empty string for format example
1650 else
1651 _pFieldDescr->GetControlDefault() >>= nValue;
1653 Reference< css::util::XNumberFormatter> xNumberFormatter = GetFormatter();
1654 Reference<XPropertySet> xFormSet = xNumberFormatter->getNumberFormatsSupplier()->getNumberFormats()->getByKey(nFormatKey);
1655 OSL_ENSURE(xFormSet.is(),"XPropertySet is null!");
1656 OUString sFormat;
1657 xFormSet->getPropertyValue("FormatString") >>= sFormat;
1659 if ( !bTextFormat )
1661 Locale aLocale;
1662 ::comphelper::getNumberFormatProperty(xNumberFormatter,nFormatKey,"Locale") >>= aLocale;
1664 sal_Int32 nNumberFormat = ::comphelper::getNumberFormatType(xNumberFormatter,nFormatKey);
1665 if( (nNumberFormat & css::util::NumberFormat::DATE) == css::util::NumberFormat::DATE
1666 || (nNumberFormat & css::util::NumberFormat::DATETIME) == css::util::NumberFormat::DATETIME )
1668 nValue = DBTypeConversion::toNullDate(DBTypeConversion::getNULLDate(xNumberFormatter->getNumberFormatsSupplier()),nValue);
1671 Reference< css::util::XNumberFormatPreviewer> xPreviewer(xNumberFormatter,UNO_QUERY);
1672 OSL_ENSURE(xPreviewer.is(),"XNumberFormatPreviewer is null!");
1673 sDefault = xPreviewer->convertNumberToPreviewString(sFormat,nValue,aLocale,true);
1675 else if ( !(_bCheck && sDefault.isEmpty()) )
1676 sDefault = xNumberFormatter->formatString(nFormatKey, sDefault.isEmpty() ? sFormat : sDefault);
1678 catch(const Exception&)
1684 return sDefault;
1687 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */