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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
|
/* -*- 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 <FieldDescControl.hxx>
#include <FieldControls.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <TableDesignHelpBar.hxx>
#include <vcl/svapp.hxx>
#include <FieldDescriptions.hxx>
#include <svl/numuno.hxx>
#include <vcl/transfer.hxx>
#include <com/sun/star/sdbc/ColumnValue.hpp>
#include <com/sun/star/util/NumberFormat.hpp>
#include <com/sun/star/util/XNumberFormatPreviewer.hpp>
#include <com/sun/star/util/XNumberFormatTypes.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <QEnumTypes.hxx>
#include <helpids.h>
#include <connectivity/dbtools.hxx>
#include <connectivity/dbconversion.hxx>
#include <comphelper/numbers.hxx>
#include <comphelper/types.hxx>
#include <UITools.hxx>
#include <strings.hrc>
#include <osl/diagnose.h>
using namespace dbaui;
using namespace dbtools;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::util;
namespace
{
template< typename T1, typename T2> void lcl_HideAndDeleteControl(short& _nPos,std::unique_ptr<T1>& _pControl, std::unique_ptr<T2>& _pControlText)
{
if ( _pControl )
{
--_nPos;
_pControl->hide();
_pControlText->hide();
_pControl.reset();
_pControlText.reset();
}
}
}
OFieldDescControl::OFieldDescControl(weld::Container* pPage, OTableDesignHelpBar* pHelpBar)
: m_xBuilder(Application::CreateBuilder(pPage, "dbaccess/ui/fielddescpage.ui"))
, m_xContainer(m_xBuilder->weld_container("FieldDescPage"))
, m_pHelp( pHelpBar )
, m_pLastFocusWindow(nullptr)
, m_pActFocusWindow(nullptr)
, m_nPos(-1)
, aYes(DBA_RES(STR_VALUE_YES))
, aNo(DBA_RES(STR_VALUE_NO))
, m_nEditWidth(50)
, pActFieldDescr(nullptr)
{
if (m_pHelp)
m_pHelp->connect_focus_out(LINK(this, OFieldDescControl, HelpFocusOut));
}
OFieldDescControl::~OFieldDescControl()
{
dispose();
}
void OFieldDescControl::dispose()
{
// Destroy children
DeactivateAggregate( tpDefault );
DeactivateAggregate( tpRequired );
DeactivateAggregate( tpTextLen );
DeactivateAggregate( tpNumType );
DeactivateAggregate( tpScale );
DeactivateAggregate( tpLength );
DeactivateAggregate( tpFormat );
DeactivateAggregate( tpAutoIncrement );
DeactivateAggregate( tpBoolDefault );
DeactivateAggregate( tpColumnName );
DeactivateAggregate( tpType );
DeactivateAggregate( tpAutoIncrementValue );
m_pHelp = nullptr;
m_pLastFocusWindow = nullptr;
m_pActFocusWindow = nullptr;
m_xDefaultText.reset();
m_xRequiredText.reset();
m_xAutoIncrementText.reset();
m_xTextLenText.reset();
m_xNumTypeText.reset();
m_xLengthText.reset();
m_xScaleText.reset();
m_xFormatText.reset();
m_xBoolDefaultText.reset();
m_xColumnNameText.reset();
m_xTypeText.reset();
m_xAutoIncrementValueText.reset();
m_xRequired.reset();
m_xNumType.reset();
m_xAutoIncrement.reset();
m_xDefault.reset();
m_xTextLen.reset();
m_xLength.reset();
m_xScale.reset();
m_xFormatSample.reset();
m_xBoolDefault.reset();
m_xColumnName.reset();
m_xType.reset();
m_xAutoIncrementValue.reset();
m_xFormat.reset();
m_xContainer.reset();
m_xBuilder.reset();
}
OUString OFieldDescControl::BoolStringPersistent(std::u16string_view rUIString) const
{
if (rUIString == aNo)
return OUString('0');
if (rUIString == aYes)
return OUString('1');
return OUString();
}
OUString OFieldDescControl::BoolStringUI(const OUString& rPersistentString) const
{
// Older versions may store a language dependent string as a default
if (rPersistentString == aYes || rPersistentString == aNo)
return rPersistentString;
if (rPersistentString == "0")
return aNo;
if (rPersistentString == "1")
return aYes;
return DBA_RES(STR_VALUE_NONE);
}
void OFieldDescControl::Init()
{
Reference< css::util::XNumberFormatter > xFormatter = GetFormatter();
::dbaui::setEvalDateFormatForFormatter(xFormatter);
}
void OFieldDescControl::SetReadOnly( bool bReadOnly )
{
// Enable/disable Controls
struct final
{
OWidgetBase * aggregate;
weld::Widget * text;
} const aggregates[] = {
{m_xRequired.get(), m_xRequiredText.get()}
, {m_xNumType.get(), m_xNumTypeText.get()}
, {m_xAutoIncrement.get(), m_xAutoIncrementText.get()}
, {m_xDefault.get(), m_xDefaultText.get()}
, {m_xTextLen.get(), m_xTextLenText.get()}
, {m_xLength.get(), m_xLengthText.get()}
, {m_xScale.get(), m_xScaleText.get()}
, {m_xColumnName.get(), m_xColumnNameText.get()}
, {m_xType.get(), m_xTypeText.get()}
, {m_xAutoIncrementValue.get(), m_xAutoIncrementValueText.get()}};
for (auto const & aggregate: aggregates)
{
if (aggregate.text)
aggregate.text->set_sensitive(!bReadOnly);
if (aggregate.aggregate)
aggregate.aggregate->set_sensitive(!bReadOnly);
}
if (m_xFormat)
{
assert(m_xFormatText);
m_xFormat->set_sensitive(!bReadOnly);
m_xFormatText->set_sensitive(!bReadOnly);
}
}
void OFieldDescControl::SetControlText( sal_uInt16 nControlId, const OUString& rText )
{
// Set the Controls' texts
switch( nControlId )
{
case FIELD_PROPERTY_BOOL_DEFAULT:
if (m_xBoolDefault)
{
OUString sOld = m_xBoolDefault->get_active_text();
m_xBoolDefault->set_active_text(rText);
if (sOld != rText)
ChangeHdl(m_xBoolDefault->GetComboBox());
}
break;
case FIELD_PROPERTY_DEFAULT:
if (m_xDefault)
{
m_xDefault->set_text(rText);
UpdateFormatSample(pActFieldDescr);
}
break;
case FIELD_PROPERTY_REQUIRED:
if (m_xRequired)
m_xRequired->set_active_text(rText);
break;
case FIELD_PROPERTY_TEXTLEN:
if (m_xTextLen)
m_xTextLen->set_text(rText);
break;
case FIELD_PROPERTY_NUMTYPE:
if (m_xNumType)
m_xNumType->set_active_text(rText);
break;
case FIELD_PROPERTY_AUTOINC:
if (m_xAutoIncrement)
{
OUString sOld = m_xAutoIncrement->get_active_text();
m_xAutoIncrement->set_active_text(rText);
if (sOld != rText)
ChangeHdl(m_xAutoIncrement->GetComboBox());
}
break;
case FIELD_PROPERTY_LENGTH:
if (m_xLength)
m_xLength->set_text(rText);
break;
case FIELD_PROPERTY_SCALE:
if (m_xScale)
m_xScale->set_text(rText);
break;
case FIELD_PROPERTY_FORMAT:
if (pActFieldDescr)
UpdateFormatSample(pActFieldDescr);
break;
case FIELD_PROPERTY_COLUMNNAME:
if (m_xColumnName)
m_xColumnName->set_text(rText);
break;
case FIELD_PROPERTY_TYPE:
if (m_xType)
m_xType->set_active_text(rText);
break;
case FIELD_PROPERTY_AUTOINCREMENT:
if (m_xAutoIncrementValue)
m_xAutoIncrementValue->set_text(rText);
break;
}
}
IMPL_LINK_NOARG(OFieldDescControl, FormatClickHdl, weld::Button&, void)
{
// Create temporary Column, which is used for data exchange with Dialog
if( !pActFieldDescr )
return;
sal_Int32 nOldFormatKey(pActFieldDescr->GetFormatKey());
SvxCellHorJustify rOldJustify = pActFieldDescr->GetHorJustify();
Reference< XNumberFormatsSupplier > xSupplier = GetFormatter()->getNumberFormatsSupplier();
SvNumberFormatsSupplierObj* pSupplierImpl = comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>( xSupplier );
if (!pSupplierImpl)
return;
SvNumberFormatter* pFormatter = pSupplierImpl->GetNumberFormatter();
if(!::dbaui::callColumnFormatDialog(m_xContainer.get(),pFormatter,pActFieldDescr->GetType(),nOldFormatKey,rOldJustify,true))
return;
bool bModified = false;
if(nOldFormatKey != pActFieldDescr->GetFormatKey())
{
pActFieldDescr->SetFormatKey( nOldFormatKey );
bModified = true;
}
if(rOldJustify != pActFieldDescr->GetHorJustify())
{
pActFieldDescr->SetHorJustify( rOldJustify );
bModified = true;
}
if(bModified)
{
SetModified(true);
UpdateFormatSample(pActFieldDescr);
}
}
void OFieldDescControl::SetModified(bool /*bModified*/)
{
}
IMPL_LINK(OFieldDescControl, ChangeHdl, weld::ComboBox&, rListBox, void)
{
if (!pActFieldDescr)
return;
if (rListBox.get_value_changed_from_saved())
SetModified(true);
// Special treatment for Bool fields
if (m_xRequired && &rListBox == m_xRequired->GetWidget() && m_xBoolDefault)
{
// If m_xRequired = sal_True then the sal_Bool field must NOT contain <<none>>
OUString sDef = BoolStringUI(::comphelper::getString(pActFieldDescr->GetControlDefault()));
if (m_xRequired->get_active() == 0) // Yes
{
m_xBoolDefault->remove_text(DBA_RES(STR_VALUE_NONE));
if (sDef != aYes && sDef != aNo)
m_xBoolDefault->set_active(1); // No as a default
else
m_xBoolDefault->set_active_text(sDef);
}
else if (m_xBoolDefault->get_count() < 3)
{
m_xBoolDefault->append_text(DBA_RES(STR_VALUE_NONE));
m_xBoolDefault->set_active_text(sDef);
}
}
// A special treatment only for AutoIncrement
if (m_xAutoIncrement && &rListBox == m_xAutoIncrement->GetWidget())
{
if (rListBox.get_active() == 1)
{ // no
DeactivateAggregate( tpAutoIncrementValue );
if(pActFieldDescr->IsPrimaryKey())
DeactivateAggregate( tpRequired );
else if( pActFieldDescr->getTypeInfo()->bNullable )
{
ActivateAggregate( tpRequired );
if (m_xRequired)
{
if( pActFieldDescr->IsNullable() )
m_xRequired->set_active(1); // no
else
m_xRequired->set_active(0); // yes
}
}
ActivateAggregate( tpDefault );
}
else
{
DeactivateAggregate( tpRequired );
DeactivateAggregate( tpDefault );
ActivateAggregate( tpAutoIncrementValue );
}
}
if (m_xType && &rListBox == m_xType->GetWidget())
{
TOTypeInfoSP pTypeInfo = getTypeInfo(m_xType->get_active());
pActFieldDescr->FillFromTypeInfo(pTypeInfo,true,false); // SetType(pTypeInfo);
DisplayData(pActFieldDescr);
CellModified(-1, m_xType->GetPos());
}
}
void OFieldDescControl::ActivateAggregate( EControlType eType )
{
// Create Controls
switch( eType )
{
case tpDefault:
if (m_xDefault)
return;
m_nPos++;
m_xDefaultText = m_xBuilder->weld_label("DefaultValueText");
m_xDefaultText->show();
m_xDefault = std::make_unique<OPropEditCtrl>(
m_xBuilder->weld_entry("DefaultValue"), STR_HELP_DEFAULT_VALUE, FIELD_PROPERTY_DEFAULT);
InitializeControl(m_xDefault->GetWidget(),HID_TAB_ENT_DEFAULT);
m_xDefault->show();
break;
case tpAutoIncrementValue:
if (m_xAutoIncrementValue || !isAutoIncrementValueEnabled())
return;
m_nPos++;
m_xAutoIncrementValueText = m_xBuilder->weld_label("AutoIncrementValueText");
m_xAutoIncrementValueText->show();
m_xAutoIncrementValue = std::make_unique<OPropEditCtrl>(
m_xBuilder->weld_spin_button("AutoIncrementValue"), STR_HELP_AUTOINCREMENT_VALUE,
FIELD_PROPERTY_AUTOINCREMENT);
m_xAutoIncrementValue->set_text( getAutoIncrementValue() );
InitializeControl(m_xAutoIncrementValue->GetWidget(),HID_TAB_AUTOINCREMENTVALUE);
m_xAutoIncrementValue->show();
break;
case tpRequired:
{
if (m_xRequired)
return;
Reference< XDatabaseMetaData> xMetaData = getMetaData();
if(xMetaData.is() && xMetaData->supportsNonNullableColumns())
{
m_nPos++;
m_xRequiredText = m_xBuilder->weld_label("RequiredText");
m_xRequiredText->show();
m_xRequired = std::make_unique<OPropListBoxCtrl>(
m_xBuilder->weld_combo_box("Required"), STR_HELP_AUTOINCREMENT_VALUE,
FIELD_PROPERTY_AUTOINCREMENT);
m_xRequired->append_text(aYes);
m_xRequired->append_text(aNo);
m_xRequired->set_active(1);
InitializeControl(m_xRequired.get(),HID_TAB_ENT_REQUIRED, true);
m_xRequired->show();
}
}
break;
case tpAutoIncrement:
{
if (m_xAutoIncrement)
return;
m_nPos++;
m_xAutoIncrementText = m_xBuilder->weld_label("AutoIncrementText");
m_xAutoIncrementText->show();
m_xAutoIncrement = std::make_unique<OPropListBoxCtrl>(
m_xBuilder->weld_combo_box("AutoIncrement"), STR_HELP_AUTOINCREMENT,
FIELD_PROPERTY_AUTOINC);
m_xAutoIncrement->append_text(aYes);
m_xAutoIncrement->append_text(aNo);
m_xAutoIncrement->set_active(0);
InitializeControl(m_xAutoIncrement.get(),HID_TAB_ENT_AUTOINCREMENT, true);
m_xAutoIncrement->show();
}
break;
case tpTextLen:
if (m_xTextLen)
return;
m_nPos++;
m_xTextLenText = m_xBuilder->weld_label("TextLengthText");
m_xTextLenText->show();
m_xTextLen = CreateNumericControl("TextLength", STR_HELP_TEXT_LENGTH, FIELD_PROPERTY_TEXTLEN,HID_TAB_ENT_TEXT_LEN);
break;
case tpType:
if (m_xType)
return;
m_nPos++;
m_xTypeText = m_xBuilder->weld_label("TypeText");
m_xTypeText->show();
m_xType = std::make_unique<OPropListBoxCtrl>(
m_xBuilder->weld_combo_box("Type"), STR_HELP_AUTOINCREMENT, FIELD_PROPERTY_TYPE);
{
const OTypeInfoMap* pTypeInfo = getTypeInfo();
for (auto const& elem : *pTypeInfo)
m_xType->append_text(elem.second->aUIName);
}
m_xType->set_active(0);
InitializeControl(m_xType.get(),HID_TAB_ENT_TYPE, true);
m_xType->show();
break;
case tpColumnName:
if (m_xColumnName)
return;
m_nPos++;
{
sal_Int32 nMax(0);
OUString aTmpString;
try
{
Reference< XDatabaseMetaData> xMetaData = getMetaData();
if ( xMetaData.is() )
{
nMax = xMetaData->getMaxColumnNameLength();
aTmpString = xMetaData->getExtraNameCharacters();
}
}
catch (const Exception&)
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
m_xColumnNameText = m_xBuilder->weld_label("ColumnNameText");
m_xColumnNameText->show();
m_xColumnName = std::make_unique<OPropColumnEditCtrl>(
m_xBuilder->weld_entry("ColumnName"), aTmpString,
STR_HELP_DEFAULT_VALUE, FIELD_PROPERTY_COLUMNNAME);
m_xColumnName->set_max_length(nMax);
m_xColumnName->setCheck( isSQL92CheckEnabled(getConnection()) );
}
InitializeControl(m_xColumnName->GetWidget(),HID_TAB_ENT_COLUMNNAME);
m_xColumnName->show();
break;
case tpNumType:
if (m_xNumType)
return;
m_nPos++;
m_xNumTypeText = m_xBuilder->weld_label("NumTypeText");
m_xNumTypeText->show();
m_xNumType = std::make_unique<OPropListBoxCtrl>(
m_xBuilder->weld_combo_box("NumType"), STR_HELP_NUMERIC_TYPE, FIELD_PROPERTY_NUMTYPE);
m_xNumType->append_text("Byte");
m_xNumType->append_text("SmallInt");
m_xNumType->append_text("Integer");
m_xNumType->append_text("Single");
m_xNumType->append_text("Double");
m_xNumType->set_active(2);
InitializeControl(m_xNumType.get(),HID_TAB_ENT_NUMTYP, true);
m_xNumType->show();
break;
case tpLength:
if (m_xLength)
return;
m_nPos++;
m_xLengthText = m_xBuilder->weld_label("LengthText");
m_xLengthText->show();
m_xLength = CreateNumericControl("Length", STR_HELP_LENGTH, FIELD_PROPERTY_LENGTH,HID_TAB_ENT_LEN);
break;
case tpScale:
if (m_xScale)
return;
m_nPos++;
m_xScaleText = m_xBuilder->weld_label("ScaleText");
m_xScaleText->show();
m_xScale = CreateNumericControl("Scale", STR_HELP_SCALE, FIELD_PROPERTY_SCALE,HID_TAB_ENT_SCALE);
break;
case tpFormat:
if (!m_xFormat)
{
m_nPos++;
m_xFormatText = m_xBuilder->weld_label("FormatTextText");
m_xFormatText->show();
m_xFormatSample = std::make_unique<OPropEditCtrl>(
m_xBuilder->weld_entry("FormatText"), STR_HELP_FORMAT_CODE, -1);
m_xFormatSample->set_editable(false);
m_xFormatSample->set_sensitive(false);
InitializeControl(m_xFormatSample->GetWidget(),HID_TAB_ENT_FORMAT_SAMPLE);
m_xFormatSample->show();
m_xFormat = m_xBuilder->weld_button("FormatButton");
m_xFormat->connect_clicked( LINK( this, OFieldDescControl, FormatClickHdl ) );
InitializeControl(m_xFormat.get(),HID_TAB_ENT_FORMAT);
m_xFormat->show();
}
UpdateFormatSample(pActFieldDescr);
break;
case tpBoolDefault:
if (m_xBoolDefault)
return;
m_nPos++;
m_xBoolDefaultText = m_xBuilder->weld_label("BoolDefaultText");
m_xBoolDefaultText->show();
m_xBoolDefault = std::make_unique<OPropListBoxCtrl>(
m_xBuilder->weld_combo_box("BoolDefault"), STR_HELP_BOOL_DEFAULT,
FIELD_PROPERTY_BOOL_DEFAULT);
m_xBoolDefault->append_text(DBA_RES(STR_VALUE_NONE));
m_xBoolDefault->append_text(aYes);
m_xBoolDefault->append_text(aNo);
InitializeControl(m_xBoolDefault->GetWidget(),HID_TAB_ENT_BOOL_DEFAULT);
m_xBoolDefault->show();
break;
}
}
void OFieldDescControl::InitializeControl(OPropListBoxCtrl* _pControl,const OUString& _sHelpId,bool _bAddChangeHandler)
{
if ( _bAddChangeHandler )
_pControl->GetComboBox().connect_changed(LINK(this,OFieldDescControl,ChangeHdl));
InitializeControl(_pControl->GetWidget(), _sHelpId);
}
void OFieldDescControl::InitializeControl(weld::Widget* pControl,const OUString& _sHelpId)
{
pControl->set_help_id(_sHelpId);
pControl->connect_focus_in(LINK(this, OFieldDescControl, OnControlFocusGot));
pControl->connect_focus_out(LINK(this, OFieldDescControl, OnControlFocusLost));
if (dynamic_cast<weld::Entry*>(pControl))
{
int nWidthRequest = Application::GetDefaultDevice()->LogicToPixel(Size(m_nEditWidth, 0), MapMode(MapUnit::MapAppFont)).Width();
pControl->set_size_request(nWidthRequest, -1);
}
}
std::unique_ptr<OPropNumericEditCtrl> OFieldDescControl::CreateNumericControl(const OUString& rId, TranslateId pHelpId, short _nProperty, const OUString& _sHelpId)
{
auto xControl = std::make_unique<OPropNumericEditCtrl>(
m_xBuilder->weld_spin_button(rId), pHelpId, _nProperty);
xControl->set_digits(0);
xControl->set_range(0, 0x7FFFFFFF); // Should be changed outside, if needed
xControl->show();
InitializeControl(xControl->GetWidget(),_sHelpId);
return xControl;
}
void OFieldDescControl::DeactivateAggregate( EControlType eType )
{
m_pLastFocusWindow = nullptr;
// Destroy Controls
switch( eType )
{
case tpDefault:
lcl_HideAndDeleteControl(m_nPos,m_xDefault,m_xDefaultText);
break;
case tpAutoIncrementValue:
lcl_HideAndDeleteControl(m_nPos,m_xAutoIncrementValue,m_xAutoIncrementValueText);
break;
case tpColumnName:
lcl_HideAndDeleteControl(m_nPos,m_xColumnName,m_xColumnNameText);
break;
case tpType:
lcl_HideAndDeleteControl(m_nPos,m_xType,m_xTypeText);
break;
case tpAutoIncrement:
lcl_HideAndDeleteControl(m_nPos,m_xAutoIncrement,m_xAutoIncrementText);
break;
case tpRequired:
lcl_HideAndDeleteControl(m_nPos,m_xRequired,m_xRequiredText);
break;
case tpTextLen:
lcl_HideAndDeleteControl(m_nPos,m_xTextLen,m_xTextLenText);
break;
case tpNumType:
lcl_HideAndDeleteControl(m_nPos,m_xNumType,m_xNumTypeText);
break;
case tpLength:
lcl_HideAndDeleteControl(m_nPos,m_xLength,m_xLengthText);
break;
case tpScale:
lcl_HideAndDeleteControl(m_nPos,m_xScale,m_xScaleText);
break;
case tpFormat:
// TODO: we have to check if we have to increment m_nPos again
lcl_HideAndDeleteControl(m_nPos,m_xFormat,m_xFormatText);
if (m_xFormatSample)
{
m_xFormatSample->hide();
m_xFormatSample.reset();
}
break;
case tpBoolDefault:
lcl_HideAndDeleteControl(m_nPos,m_xBoolDefault,m_xBoolDefaultText);
break;
}
}
void OFieldDescControl::DisplayData(OFieldDescription* pFieldDescr )
{
pActFieldDescr = pFieldDescr;
if(!pFieldDescr)
{
if (m_pHelp)
m_pHelp->SetHelpText( OUString() );
DeactivateAggregate( tpDefault );
DeactivateAggregate( tpRequired );
DeactivateAggregate( tpTextLen );
DeactivateAggregate( tpNumType );
DeactivateAggregate( tpScale );
DeactivateAggregate( tpLength );
DeactivateAggregate( tpFormat );
DeactivateAggregate( tpAutoIncrement );
DeactivateAggregate( tpBoolDefault );
DeactivateAggregate( tpColumnName );
DeactivateAggregate( tpType );
DeactivateAggregate( tpAutoIncrementValue );
m_pPreviousType = TOTypeInfoSP();
// Reset the saved focus' pointer
m_pLastFocusWindow = nullptr;
return;
}
TOTypeInfoSP pFieldType(pFieldDescr->getTypeInfo());
ActivateAggregate( tpColumnName );
ActivateAggregate( tpType );
OSL_ENSURE(pFieldType,"We need a type information here!");
// If the type has changed, substitute Controls
if( m_pPreviousType != pFieldType )
{
// Reset the saved focus' pointer
m_pLastFocusWindow = nullptr;
// Controls, which must NOT be displayed again
DeactivateAggregate( tpNumType );
// determine which controls we should show and which not
// 1. the required control
if ( pFieldType->bNullable )
ActivateAggregate( tpRequired );
else
DeactivateAggregate( tpRequired );
// 2. the autoincrement
if ( pFieldType->bAutoIncrement )
{
DeactivateAggregate( tpRequired );
DeactivateAggregate( tpDefault );
ActivateAggregate( tpAutoIncrement );
ActivateAggregate( tpAutoIncrementValue );
}
else
{
DeactivateAggregate( tpAutoIncrement );
DeactivateAggregate( tpAutoIncrementValue );
if(pFieldType->bNullable)
ActivateAggregate( tpRequired );
else
DeactivateAggregate( tpRequired );
ActivateAggregate( tpDefault );
}
// 3. the scale and precision
if (pFieldType->nPrecision)
{
ActivateAggregate( tpLength );
m_xLength->set_max(std::max<sal_Int32>(pFieldType->nPrecision,pFieldDescr->GetPrecision()));
m_xLength->set_editable(!pFieldType->aCreateParams.isEmpty());
}
else
DeactivateAggregate( tpLength );
if (pFieldType->nMaximumScale)
{
ActivateAggregate( tpScale );
m_xScale->set_range(pFieldType->nMinimumScale,
std::max<sal_Int32>(pFieldType->nMaximumScale,pFieldDescr->GetScale()));
m_xScale->set_editable(!pFieldType->aCreateParams.isEmpty() && pFieldType->aCreateParams != "PRECISION");
}
else
DeactivateAggregate( tpScale );
// and now look for type specific things
switch( pFieldType->nType )
{
case DataType::CHAR:
case DataType::VARCHAR:
case DataType::LONGVARCHAR:
DeactivateAggregate( tpLength );
DeactivateAggregate( tpBoolDefault );
ActivateAggregate( tpDefault );
ActivateAggregate( tpFormat );
if (pFieldType->nPrecision)
{
ActivateAggregate( tpTextLen );
m_xTextLen->set_max(std::max<sal_Int32>(pFieldType->nPrecision,pFieldDescr->GetPrecision()));
m_xTextLen->set_editable(!pFieldType->aCreateParams.isEmpty());
}
else
DeactivateAggregate( tpTextLen );
break;
case DataType::DATE:
case DataType::TIME:
case DataType::TIMESTAMP:
DeactivateAggregate( tpLength ); // we don't need a length for date types
DeactivateAggregate( tpTextLen );
DeactivateAggregate( tpBoolDefault );
ActivateAggregate( tpDefault );
ActivateAggregate( tpFormat );
break;
case DataType::BIT:
if ( !pFieldType->aCreateParams.isEmpty() )
{
DeactivateAggregate( tpFormat );
DeactivateAggregate( tpTextLen );
DeactivateAggregate( tpBoolDefault );
break;
}
[[fallthrough]];
case DataType::BOOLEAN:
DeactivateAggregate( tpTextLen );
DeactivateAggregate( tpFormat );
DeactivateAggregate( tpDefault );
ActivateAggregate( tpBoolDefault );
break;
case DataType::DECIMAL:
case DataType::NUMERIC:
case DataType::BIGINT:
case DataType::FLOAT:
case DataType::DOUBLE:
case DataType::TINYINT:
case DataType::SMALLINT:
case DataType::INTEGER:
case DataType::REAL:
DeactivateAggregate( tpTextLen );
DeactivateAggregate( tpBoolDefault );
ActivateAggregate( tpFormat );
break;
case DataType::BINARY:
case DataType::VARBINARY:
DeactivateAggregate( tpDefault );
DeactivateAggregate( tpRequired );
DeactivateAggregate( tpTextLen );
DeactivateAggregate( tpBoolDefault );
ActivateAggregate( tpFormat );
break;
case DataType::LONGVARBINARY:
case DataType::SQLNULL:
case DataType::OBJECT:
case DataType::DISTINCT:
case DataType::STRUCT:
case DataType::ARRAY:
case DataType::BLOB:
case DataType::CLOB:
case DataType::REF:
case DataType::OTHER:
DeactivateAggregate( tpFormat );
DeactivateAggregate( tpTextLen );
DeactivateAggregate( tpBoolDefault );
break;
default:
OSL_FAIL("Unknown type");
}
m_pPreviousType = pFieldType;
}
if (pFieldDescr->IsPrimaryKey())
{
DeactivateAggregate(tpRequired);
}
else if (!m_xAutoIncrement && pFieldType)
{
if (pFieldType->bNullable)
ActivateAggregate(tpRequired);
else
DeactivateAggregate(tpRequired);
}
// Initialize Controls
if (m_xAutoIncrement)
{
if ( pFieldDescr->IsAutoIncrement() )
{
m_xAutoIncrement->set_active(0); // yes
ActivateAggregate( tpAutoIncrementValue );
if (m_xAutoIncrementValue)
m_xAutoIncrementValue->set_text(pFieldDescr->GetAutoIncrementValue());
DeactivateAggregate( tpRequired );
DeactivateAggregate( tpDefault );
}
else
{
// disable autoincrement value because it should only be visible when autoincrement is to true
DeactivateAggregate( tpAutoIncrementValue );
m_xAutoIncrement->set_active(1); // no
ActivateAggregate( tpDefault );
// Affects pRequired
if(!pFieldDescr->IsPrimaryKey())
ActivateAggregate( tpRequired );
}
}
if (m_xDefault)
{
m_xDefault->set_text(getControlDefault(pFieldDescr));
m_xDefault->save_value();
}
if (m_xBoolDefault)
{
// If m_xRequired = sal_True then the sal_Bool field must NOT contain <<none>>
OUString sValue;
pFieldDescr->GetControlDefault() >>= sValue;
OUString sDef = BoolStringUI(sValue);
// Make sure that <<none>> is only present if the field can be NULL
if ( ( pFieldType && !pFieldType->bNullable ) || !pFieldDescr->IsNullable() )
{
pFieldDescr->SetIsNullable(ColumnValue::NO_NULLS); // The type says so
m_xBoolDefault->remove_text(DBA_RES(STR_VALUE_NONE));
if ( sDef != aYes && sDef != aNo )
m_xBoolDefault->set_active(1); // No as a default
else
m_xBoolDefault->set_active_text(sDef);
pFieldDescr->SetControlDefault(Any(BoolStringPersistent(m_xBoolDefault->get_active_text())));
}
else if (m_xBoolDefault->get_count() < 3)
{
m_xBoolDefault->append_text(DBA_RES(STR_VALUE_NONE));
m_xBoolDefault->set_active_text(sDef);
}
else
m_xBoolDefault->set_active_text(sDef);
}
if (m_xRequired)
{
if( pFieldDescr->IsNullable() )
m_xRequired->set_active(1); // no
else
m_xRequired->set_active(0); // yes
}
if (m_xTextLen)
{
m_xTextLen->set_text(OUString::number(pFieldDescr->GetPrecision()));
m_xTextLen->save_value();
}
if( m_xNumType )
{
OSL_FAIL("OFieldDescControl::DisplayData: invalid num type!");
}
if (m_xLength)
m_xLength->set_text(OUString::number(pFieldDescr->GetPrecision()));
if (m_xScale)
m_xScale->set_text(OUString::number(pFieldDescr->GetScale()));
if (m_xFormat)
UpdateFormatSample(pFieldDescr);
if (m_xColumnName)
m_xColumnName->set_text(pFieldDescr->GetName());
if (m_xType)
{
sal_Int32 nPos = pFieldType ? m_xType->find_text(pFieldDescr->getTypeInfo()->aUIName) : -1;
if (nPos == -1)
{
const OTypeInfoMap* pMap = getTypeInfo();
OTypeInfoMap::const_iterator aIter = pMap->find(pFieldType ? pFieldDescr->getTypeInfo()->nType : pFieldDescr->GetType());
if(aIter == pMap->end() && !pMap->empty())
{
aIter = pMap->begin();
if(pFieldDescr->GetPrecision() > aIter->second->nPrecision)
pFieldDescr->SetPrecision(aIter->second->nPrecision);
if(pFieldDescr->GetScale() > aIter->second->nMaximumScale)
pFieldDescr->SetScale(0);
if(!aIter->second->bNullable && pFieldDescr->IsNullable())
pFieldDescr->SetIsNullable(ColumnValue::NO_NULLS);
if(!aIter->second->bAutoIncrement && pFieldDescr->IsAutoIncrement())
pFieldDescr->SetAutoIncrement(false);
}
if ( aIter != pMap->end() )
{
pFieldDescr->SetType(aIter->second);
}
}
m_xType->set_active_text(pFieldDescr->getTypeInfo()->aUIName);
}
// Enable/disable Controls
bool bRead(IsReadOnly());
SetReadOnly( bRead );
}
IMPL_LINK(OFieldDescControl, OnControlFocusGot, weld::Widget&, rControl, void )
{
OUString strHelpText;
if (m_xTextLen && &rControl == m_xTextLen->GetWidget())
{
m_xTextLen->save_value();
strHelpText = m_xTextLen->GetHelp();
}
else if (m_xLength && &rControl == m_xLength->GetWidget())
{
m_xLength->save_value();
strHelpText = m_xLength->GetHelp();
}
else if (m_xScale && &rControl == m_xScale->GetWidget())
{
m_xScale->save_value();
strHelpText = m_xScale->GetHelp();
}
else if (m_xColumnName && &rControl == m_xColumnName->GetWidget())
{
m_xColumnName->save_value();
strHelpText = m_xColumnName->GetHelp();
}
else if (m_xDefault && &rControl == m_xDefault->GetWidget())
{
m_xDefault->save_value();
strHelpText = m_xDefault->GetHelp();
}
else if (m_xFormatSample && &rControl == m_xFormatSample->GetWidget())
{
m_xFormatSample->save_value();
strHelpText = m_xFormatSample->GetHelp();
}
else if (m_xAutoIncrementValue && &rControl == m_xAutoIncrementValue->GetWidget())
{
m_xAutoIncrementValue->save_value();
strHelpText = m_xAutoIncrementValue->GetHelp();
}
else if (m_xRequired && &rControl == m_xRequired->GetWidget())
{
m_xRequired->save_value();
strHelpText = m_xRequired->GetHelp();
}
else if (m_xNumType && &rControl == m_xNumType->GetWidget())
{
m_xNumType->save_value();
strHelpText = m_xNumType->GetHelp();
}
else if (m_xAutoIncrement && &rControl == m_xAutoIncrement->GetWidget())
{
m_xAutoIncrement->save_value();
strHelpText = m_xAutoIncrement->GetHelp();
}
else if (m_xBoolDefault && &rControl == m_xBoolDefault->GetWidget())
{
m_xBoolDefault->save_value();
strHelpText = m_xBoolDefault->GetHelp();
}
else if (m_xType && &rControl == m_xType->GetWidget())
{
m_xType->save_value();
strHelpText = m_xType->GetHelp();
}
else if (m_xFormat && &rControl == m_xFormat.get())
strHelpText = DBA_RES(STR_HELP_FORMAT_BUTTON);
if (!strHelpText.isEmpty() && m_pHelp)
m_pHelp->SetHelpText(strHelpText);
m_pActFocusWindow = &rControl;
m_aControlFocusIn.Call(rControl);
}
IMPL_LINK(OFieldDescControl, OnControlFocusLost, weld::Widget&, rControl, void )
{
if (m_xLength && &rControl == m_xLength->GetWidget() && m_xLength->get_value_changed_from_saved())
CellModified(-1, m_xLength->GetPos());
else if (m_xTextLen && &rControl == m_xTextLen->GetWidget() && m_xTextLen->get_value_changed_from_saved())
CellModified(-1, m_xTextLen->GetPos());
else if (m_xScale && &rControl == m_xScale->GetWidget() && m_xScale->get_value_changed_from_saved())
CellModified(-1, m_xScale->GetPos());
else if (m_xColumnName && &rControl == m_xColumnName->GetWidget() && m_xColumnName->get_value_changed_from_saved())
CellModified(-1, m_xColumnName->GetPos());
else if (m_xDefault && &rControl == m_xDefault->GetWidget() && m_xDefault->get_value_changed_from_saved())
CellModified(-1, m_xDefault->GetPos());
else if (m_xFormatSample && &rControl == m_xFormatSample->GetWidget() && m_xFormatSample->get_value_changed_from_saved())
CellModified(-1, m_xFormatSample->GetPos());
else if (m_xAutoIncrementValue && &rControl == m_xAutoIncrementValue->GetWidget() && m_xAutoIncrementValue->get_value_changed_from_saved())
CellModified(-1, m_xAutoIncrementValue->GetPos());
else if (m_xRequired && &rControl == m_xRequired->GetWidget() && m_xRequired->get_value_changed_from_saved())
CellModified(-1, m_xRequired->GetPos());
else if (m_xNumType && &rControl == m_xNumType->GetWidget() && m_xNumType->get_value_changed_from_saved())
CellModified(-1, m_xNumType->GetPos());
else if (m_xAutoIncrement && &rControl == m_xAutoIncrement->GetWidget() && m_xAutoIncrement->get_value_changed_from_saved())
CellModified(-1, m_xAutoIncrement->GetPos());
else if (m_xBoolDefault && &rControl == m_xBoolDefault->GetWidget() && m_xBoolDefault->get_value_changed_from_saved())
CellModified(-1, m_xBoolDefault->GetPos());
else if (m_xType && &rControl == m_xType->GetWidget() && m_xType->get_value_changed_from_saved())
CellModified(-1, m_xType->GetPos());
else if (m_xDefault && &rControl == m_xDefault->GetWidget())
UpdateFormatSample(pActFieldDescr);
implFocusLost(&rControl);
}
void OFieldDescControl::SaveData( OFieldDescription* pFieldDescr )
{
if( !pFieldDescr )
return;
// Read out Controls
OUString sDefault;
if (m_xDefault)
{
// tdf#138409 take the control default in the UI Locale format, e.g. 12,34 and return a string
// suitable as the database default, e.g. 12.34
sDefault = CanonicalizeToControlDefault(pFieldDescr, m_xDefault->get_text());
}
else if (m_xBoolDefault)
{
sDefault = BoolStringPersistent(m_xBoolDefault->get_active_text());
}
if ( !sDefault.isEmpty() )
pFieldDescr->SetControlDefault(Any(sDefault));
else
pFieldDescr->SetControlDefault(Any());
if((m_xRequired && m_xRequired->get_active() == 0) || pFieldDescr->IsPrimaryKey() || (m_xBoolDefault && m_xBoolDefault->get_count() == 2)) // yes
pFieldDescr->SetIsNullable( ColumnValue::NO_NULLS );
else
pFieldDescr->SetIsNullable( ColumnValue::NULLABLE );
if (m_xAutoIncrement)
pFieldDescr->SetAutoIncrement(m_xAutoIncrement->get_active() == 0);
if( m_xTextLen )
pFieldDescr->SetPrecision( static_cast<sal_Int32>(m_xTextLen->get_value()) );
else if (m_xLength)
pFieldDescr->SetPrecision( static_cast<sal_Int32>(m_xLength->get_value()) );
if (m_xScale)
pFieldDescr->SetScale( static_cast<sal_Int32>(m_xScale->get_value()) );
if (m_xColumnName)
pFieldDescr->SetName(m_xColumnName->get_text());
if (m_xAutoIncrementValue && isAutoIncrementValueEnabled())
pFieldDescr->SetAutoIncrementValue(m_xAutoIncrementValue->get_text());
}
void OFieldDescControl::UpdateFormatSample(OFieldDescription const * pFieldDescr)
{
if (pFieldDescr && m_xFormatSample)
m_xFormatSample->set_text(getControlDefault(pFieldDescr,false));
}
void OFieldDescControl::GrabFocus()
{
m_xContainer->grab_focus();
// Set the Focus to the Control that has been active last
if (m_pLastFocusWindow)
{
m_pLastFocusWindow->grab_focus();
m_pLastFocusWindow = nullptr;
}
}
void OFieldDescControl::implFocusLost(weld::Widget* _pWhich)
{
// Remember the active Control
if (!m_pLastFocusWindow)
m_pLastFocusWindow = _pWhich;
// Reset HelpText
if (m_pHelp && !m_pHelp->HasFocus())
m_pHelp->SetHelpText( OUString() );
}
IMPL_LINK_NOARG(OFieldDescControl, HelpFocusOut, weld::Widget&, void)
{
m_pHelp->SetHelpText(OUString());
}
bool OFieldDescControl::IsFocusInEditableWidget() const
{
if (m_xDefault && m_pActFocusWindow == m_xDefault->GetWidget())
return true;
if (m_xFormatSample && m_pActFocusWindow == m_xFormatSample->GetWidget())
return true;
if (m_xTextLen && m_pActFocusWindow == m_xTextLen->GetWidget())
return true;
if (m_xLength && m_pActFocusWindow == m_xLength->GetWidget())
return true;
if (m_xScale && m_pActFocusWindow == m_xScale->GetWidget())
return true;
if (m_xColumnName && m_pActFocusWindow == m_xColumnName->GetWidget())
return true;
if (m_xAutoIncrementValue && m_pActFocusWindow == m_xAutoIncrementValue->GetWidget())
return true;
return false;
}
bool OFieldDescControl::HasChildPathFocus() const
{
return m_xContainer && m_xContainer->has_child_focus();
}
bool OFieldDescControl::isCopyAllowed()
{
int nStartPos, nEndPos;
bool bAllowed = (m_pActFocusWindow != nullptr) && IsFocusInEditableWidget() &&
dynamic_cast<weld::Entry&>(*m_pActFocusWindow).get_selection_bounds(nStartPos, nEndPos);
return bAllowed;
}
bool OFieldDescControl::isCutAllowed()
{
int nStartPos, nEndPos;
bool bAllowed = (m_pActFocusWindow != nullptr) && IsFocusInEditableWidget() &&
dynamic_cast<weld::Entry&>(*m_pActFocusWindow).get_selection_bounds(nStartPos, nEndPos);
return bAllowed;
}
bool OFieldDescControl::isPasteAllowed()
{
bool bAllowed = (m_pActFocusWindow != nullptr) && IsFocusInEditableWidget();
if ( bAllowed )
{
TransferableDataHelper aTransferData(TransferableDataHelper::CreateFromClipboard(m_pActFocusWindow->get_clipboard()));
bAllowed = aTransferData.HasFormat(SotClipboardFormatId::STRING);
}
return bAllowed;
}
void OFieldDescControl::cut()
{
if (isCutAllowed())
dynamic_cast<weld::Entry&>(*m_pActFocusWindow).cut_clipboard();
}
void OFieldDescControl::copy()
{
if (isCopyAllowed()) // this only checks if the focus window is valid
dynamic_cast<weld::Entry&>(*m_pActFocusWindow).copy_clipboard();
}
void OFieldDescControl::paste()
{
if (m_pActFocusWindow) // this only checks if the focus window is valid
dynamic_cast<weld::Entry&>(*m_pActFocusWindow).paste_clipboard();
}
bool OFieldDescControl::isTextFormat(const OFieldDescription* _pFieldDescr, sal_uInt32& _nFormatKey) const
{
_nFormatKey = _pFieldDescr->GetFormatKey();
bool bTextFormat = true;
try
{
if (!_nFormatKey)
{
Reference< css::util::XNumberFormatTypes> xNumberTypes(GetFormatter()->getNumberFormatsSupplier()->getNumberFormats(),UNO_QUERY);
OSL_ENSURE(xNumberTypes.is(),"XNumberFormatTypes is null!");
_nFormatKey = ::dbtools::getDefaultNumberFormat( _pFieldDescr->GetType(),
_pFieldDescr->GetScale(),
_pFieldDescr->IsCurrency(),
xNumberTypes,
GetLocale());
}
sal_Int32 nNumberFormat = ::comphelper::getNumberFormatType(GetFormatter(),_nFormatKey);
bTextFormat = (nNumberFormat == css::util::NumberFormat::TEXT);
}
catch(const Exception&)
{
}
return bTextFormat;
}
OUString OFieldDescControl::getControlDefault( const OFieldDescription* _pFieldDescr, bool _bCheck) const
{
OUString sDefault;
bool bCheck = !_bCheck || _pFieldDescr->GetControlDefault().hasValue();
if ( bCheck )
{
try
{
double nValue = 0.0;
sal_uInt32 nFormatKey;
bool bTextFormat = isTextFormat(_pFieldDescr,nFormatKey);
if ( _pFieldDescr->GetControlDefault() >>= sDefault )
{
if ( !bTextFormat )
{
if ( !sDefault.isEmpty() )
{
try
{
nValue = GetFormatter()->convertStringToNumber(nFormatKey,sDefault);
}
catch(const Exception&)
{
return OUString(); // return empty string for format example
}
}
}
}
else
_pFieldDescr->GetControlDefault() >>= nValue;
Reference< css::util::XNumberFormatter> xNumberFormatter = GetFormatter();
Reference<XPropertySet> xFormSet = xNumberFormatter->getNumberFormatsSupplier()->getNumberFormats()->getByKey(nFormatKey);
OSL_ENSURE(xFormSet.is(),"XPropertySet is null!");
OUString sFormat;
xFormSet->getPropertyValue("FormatString") >>= sFormat;
if ( !bTextFormat )
{
Locale aLocale;
::comphelper::getNumberFormatProperty(xNumberFormatter,nFormatKey,"Locale") >>= aLocale;
sal_Int32 nNumberFormat = ::comphelper::getNumberFormatType(xNumberFormatter,nFormatKey);
if( (nNumberFormat & css::util::NumberFormat::DATE) == css::util::NumberFormat::DATE
|| (nNumberFormat & css::util::NumberFormat::DATETIME) == css::util::NumberFormat::DATETIME )
{
nValue = DBTypeConversion::toNullDate(DBTypeConversion::getNULLDate(xNumberFormatter->getNumberFormatsSupplier()),nValue);
}
Reference< css::util::XNumberFormatPreviewer> xPreviewer(xNumberFormatter,UNO_QUERY);
OSL_ENSURE(xPreviewer.is(),"XNumberFormatPreviewer is null!");
sDefault = xPreviewer->convertNumberToPreviewString(sFormat,nValue,aLocale,true);
}
else if ( !(_bCheck && sDefault.isEmpty()) )
sDefault = xNumberFormatter->formatString(nFormatKey, sDefault.isEmpty() ? sFormat : sDefault);
}
catch(const Exception&)
{
}
}
return sDefault;
}
// tdf#138409 intended to be effectively the reverse of getControlDefault to
// turn a user's possibly 12,34 format into 12.34 format for numerical types
OUString OFieldDescControl::CanonicalizeToControlDefault(const OFieldDescription* pFieldDescr, const OUString& rDefault) const
{
if (rDefault.isEmpty())
return rDefault;
bool bIsNumericalType = false;
switch (pFieldDescr->GetType())
{
case DataType::TINYINT:
case DataType::SMALLINT:
case DataType::INTEGER:
case DataType::BIGINT:
case DataType::FLOAT:
case DataType::REAL:
case DataType::DOUBLE:
case DataType::NUMERIC:
case DataType::DECIMAL:
bIsNumericalType = true;
break;
}
if (!bIsNumericalType)
return rDefault;
try
{
sal_uInt32 nFormatKey;
bool bTextFormat = isTextFormat(pFieldDescr, nFormatKey);
if (bTextFormat)
return rDefault;
double nValue = GetFormatter()->convertStringToNumber(nFormatKey, rDefault);
return OUString::number(nValue);
}
catch(const Exception&)
{
}
return rDefault;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|