mention wxTreeCtrl::SelectItem() fix of r60921
[wxWidgets.git] / src / propgrid / props.cpp
CommitLineData
1c4293cb
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/propgrid/props.cpp
3// Purpose: Basic Property Classes
4// Author: Jaakko Salli
5// Modified by:
6// Created: 2005-05-14
ea5af9c5 7// RCS-ID: $Id$
1c4293cb
VZ
8// Copyright: (c) Jaakko Salli
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
f4bc1aa2
JS
19#if wxUSE_PROPGRID
20
1c4293cb
VZ
21#ifndef WX_PRECOMP
22 #include "wx/defs.h"
23 #include "wx/object.h"
24 #include "wx/hash.h"
25 #include "wx/string.h"
26 #include "wx/log.h"
27 #include "wx/event.h"
28 #include "wx/window.h"
29 #include "wx/panel.h"
30 #include "wx/dc.h"
31 #include "wx/dcclient.h"
32 #include "wx/dcmemory.h"
33 #include "wx/button.h"
34 #include "wx/pen.h"
35 #include "wx/brush.h"
36 #include "wx/cursor.h"
37 #include "wx/dialog.h"
38 #include "wx/settings.h"
39 #include "wx/msgdlg.h"
40 #include "wx/choice.h"
41 #include "wx/stattext.h"
42 #include "wx/scrolwin.h"
43 #include "wx/dirdlg.h"
44 #include "wx/combobox.h"
45 #include "wx/layout.h"
46 #include "wx/sizer.h"
47 #include "wx/textdlg.h"
48 #include "wx/filedlg.h"
1c4293cb
VZ
49 #include "wx/intl.h"
50#endif
51
3b211af1 52#include "wx/filename.h"
1c4293cb 53
3b211af1 54#include "wx/propgrid/propgrid.h"
1c4293cb
VZ
55
56#define wxPG_CUSTOM_IMAGE_WIDTH 20 // for wxColourProperty etc.
57
58
59// -----------------------------------------------------------------------
60// wxStringProperty
61// -----------------------------------------------------------------------
62
63WX_PG_IMPLEMENT_PROPERTY_CLASS(wxStringProperty,wxPGProperty,
64 wxString,const wxString&,TextCtrl)
65
66wxStringProperty::wxStringProperty( const wxString& label,
67 const wxString& name,
68 const wxString& value )
69 : wxPGProperty(label,name)
70{
71 SetValue(value);
72}
73
74void wxStringProperty::OnSetValue()
75{
76 if ( !m_value.IsNull() && m_value.GetString() == wxS("<composed>") )
77 SetFlag(wxPG_PROP_COMPOSED_VALUE);
78
79 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE) )
80 {
81 wxString s;
c82a80e8 82 DoGenerateComposedValue(s);
1c4293cb
VZ
83 m_value = s;
84 }
85}
86
87wxStringProperty::~wxStringProperty() { }
88
1425eca5
JS
89wxString wxStringProperty::ValueToString( wxVariant& value,
90 int argFlags ) const
1c4293cb 91{
1425eca5 92 wxString s = value.GetString();
1c4293cb
VZ
93
94 if ( GetChildCount() && HasFlag(wxPG_PROP_COMPOSED_VALUE) )
95 {
96 // Value stored in m_value is non-editable, non-full value
97 if ( (argFlags & wxPG_FULL_VALUE) || (argFlags & wxPG_EDITABLE_VALUE) )
1425eca5
JS
98 {
99 // Calling this under incorrect conditions will fail
100 wxASSERT_MSG( argFlags & wxPG_VALUE_IS_CURRENT,
101 "Sorry, currently default wxPGProperty::ValueToString() "
102 "implementation only works if value is m_value." );
103
c82a80e8 104 DoGenerateComposedValue(s, argFlags);
1425eca5 105 }
1c4293cb
VZ
106
107 return s;
108 }
109
110 // If string is password and value is for visual purposes,
111 // then return asterisks instead the actual string.
112 if ( (m_flags & wxPG_PROP_PASSWORD) && !(argFlags & (wxPG_FULL_VALUE|wxPG_EDITABLE_VALUE)) )
113 return wxString(wxChar('*'), s.Length());
114
115 return s;
116}
117
118bool wxStringProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
119{
120 if ( GetChildCount() && HasFlag(wxPG_PROP_COMPOSED_VALUE) )
121 return wxPGProperty::StringToValue(variant, text, argFlags);
122
0c931dd4 123 if ( variant != text )
1c4293cb
VZ
124 {
125 variant = text;
126 return true;
127 }
128
129 return false;
130}
131
132bool wxStringProperty::DoSetAttribute( const wxString& name, wxVariant& value )
133{
134 if ( name == wxPG_STRING_PASSWORD )
135 {
136 m_flags &= ~(wxPG_PROP_PASSWORD);
137 if ( wxPGVariantToInt(value) ) m_flags |= wxPG_PROP_PASSWORD;
138 RecreateEditor();
139 return false;
140 }
141 return true;
142}
143
144// -----------------------------------------------------------------------
145// wxIntProperty
146// -----------------------------------------------------------------------
147
148WX_PG_IMPLEMENT_PROPERTY_CLASS(wxIntProperty,wxPGProperty,
149 long,long,TextCtrl)
150
151wxIntProperty::wxIntProperty( const wxString& label, const wxString& name,
152 long value ) : wxPGProperty(label,name)
153{
154 SetValue(value);
155}
156
157wxIntProperty::wxIntProperty( const wxString& label, const wxString& name,
158 const wxLongLong& value ) : wxPGProperty(label,name)
159{
0372d42e 160 SetValue(WXVARIANT(value));
1c4293cb
VZ
161}
162
163wxIntProperty::~wxIntProperty() { }
164
1425eca5
JS
165wxString wxIntProperty::ValueToString( wxVariant& value,
166 int WXUNUSED(argFlags) ) const
1c4293cb 167{
1425eca5 168 if ( value.GetType() == wxPG_VARIANT_TYPE_LONG )
0372d42e 169 {
1425eca5 170 return wxString::Format(wxS("%li"),value.GetLong());
0372d42e 171 }
1425eca5 172 else if ( value.GetType() == wxLongLong_VariantType )
0372d42e
JS
173 {
174 wxLongLong ll;
1425eca5 175 ll << value;
0372d42e
JS
176 return ll.ToString();
177 }
1c4293cb 178
0372d42e 179 return wxEmptyString;
1c4293cb
VZ
180}
181
182bool wxIntProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
183{
184 wxString s;
185 long value32;
186
187 if ( text.length() == 0 )
188 {
189 variant.MakeNull();
190 return true;
191 }
192
193 // We know it is a number, but let's still check
194 // the return value.
195 if ( text.IsNumber() )
196 {
197 // Remove leading zeroes, so that the number is not interpreted as octal
198 wxString::const_iterator i = text.begin();
199 wxString::const_iterator iMax = text.end() - 1; // Let's allow one, last zero though
200
201 int firstNonZeroPos = 0;
202
b7bc9d80 203 for ( ; i != iMax; ++i )
1c4293cb
VZ
204 {
205 wxChar c = *i;
206 if ( c != wxS('0') && c != wxS(' ') )
207 break;
208 firstNonZeroPos++;
209 }
210
211 wxString useText = text.substr(firstNonZeroPos, text.length() - firstNonZeroPos);
212
0372d42e
JS
213 wxString variantType = variant.GetType();
214 bool isPrevLong = variantType == wxPG_VARIANT_TYPE_LONG;
1c4293cb
VZ
215
216 wxLongLong_t value64 = 0;
217
218 if ( useText.ToLongLong(&value64, 10) &&
219 ( value64 >= INT_MAX || value64 <= INT_MIN )
220 )
221 {
0372d42e
JS
222 bool doChangeValue = isPrevLong;
223
224 if ( !isPrevLong && variantType == wxLongLong_VariantType )
225 {
226 wxLongLong oldValue;
227 oldValue << variant;
228 if ( oldValue.GetValue() != value64 )
229 doChangeValue = true;
230 }
231
232 if ( doChangeValue )
1c4293cb 233 {
0372d42e
JS
234 wxLongLong ll(value64);
235 variant << ll;
1c4293cb
VZ
236 return true;
237 }
238 }
239
240 if ( useText.ToLong( &value32, 0 ) )
241 {
0c931dd4 242 if ( !isPrevLong || variant != value32 )
1c4293cb
VZ
243 {
244 variant = value32;
245 return true;
246 }
247 }
248 }
249 else if ( argFlags & wxPG_REPORT_ERROR )
250 {
251 }
252 return false;
253}
254
255bool wxIntProperty::IntToValue( wxVariant& variant, int value, int WXUNUSED(argFlags) ) const
256{
0c931dd4 257 if ( variant.GetType() != wxPG_VARIANT_TYPE_LONG || variant != (long)value )
1c4293cb
VZ
258 {
259 variant = (long)value;
260 return true;
261 }
262 return false;
263}
264
265bool wxIntProperty::DoValidation( const wxPGProperty* property, wxLongLong_t& value, wxPGValidationInfo* pValidationInfo, int mode )
266{
267 // Check for min/max
268 wxLongLong_t min = wxINT64_MIN;
269 wxLongLong_t max = wxINT64_MAX;
270 wxVariant variant;
271 bool minOk = false;
272 bool maxOk = false;
273
274 variant = property->GetAttribute(wxPGGlobalVars->m_strMin);
275 if ( !variant.IsNull() )
276 {
277 wxPGVariantToLongLong(variant, &min);
278 minOk = true;
279 }
280
281 variant = property->GetAttribute(wxPGGlobalVars->m_strMax);
282 if ( !variant.IsNull() )
283 {
284 wxPGVariantToLongLong(variant, &max);
285 maxOk = true;
286 }
287
288 if ( minOk )
289 {
290 if ( value < min )
291 {
292 if ( mode == wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE )
2a8312bc
JS
293 pValidationInfo->SetFailureMessage(
294 wxString::Format(_("Value must be %lld or higher"),min)
295 );
1c4293cb
VZ
296 else if ( mode == wxPG_PROPERTY_VALIDATION_SATURATE )
297 value = min;
298 else
299 value = max - (min - value);
300 return false;
301 }
302 }
303
304 if ( maxOk )
305 {
306 if ( value > max )
307 {
308 if ( mode == wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE )
2a8312bc
JS
309 pValidationInfo->SetFailureMessage(
310 wxString::Format(_("Value must be %lld or higher"),min)
311 );
1c4293cb
VZ
312 else if ( mode == wxPG_PROPERTY_VALIDATION_SATURATE )
313 value = max;
314 else
315 value = min + (value - max);
316 return false;
317 }
318 }
319 return true;
320}
321
322bool wxIntProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& validationInfo ) const
323{
324 wxLongLong_t ll;
325 if ( wxPGVariantToLongLong(value, &ll) )
326 return DoValidation(this, ll, &validationInfo, wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE);
327 return true;
328}
329
330wxValidator* wxIntProperty::GetClassValidator()
331{
332#if wxUSE_VALIDATORS
333 WX_PG_DOGETVALIDATOR_ENTRY()
334
335 // Atleast wxPython 2.6.2.1 required that the string argument is given
336 static wxString v;
337 wxTextValidator* validator = new wxTextValidator(wxFILTER_NUMERIC,&v);
338
339 WX_PG_DOGETVALIDATOR_EXIT(validator)
340#else
341 return NULL;
342#endif
343}
344
345wxValidator* wxIntProperty::DoGetValidator() const
346{
347 return GetClassValidator();
348}
349
350// -----------------------------------------------------------------------
351// wxUIntProperty
352// -----------------------------------------------------------------------
353
354
355#define wxPG_UINT_TEMPLATE_MAX 8
356
357static const wxChar* gs_uintTemplates32[wxPG_UINT_TEMPLATE_MAX] = {
358 wxT("%x"),wxT("0x%x"),wxT("$%x"),
359 wxT("%X"),wxT("0x%X"),wxT("$%X"),
360 wxT("%u"),wxT("%o")
361};
362
363static const wxChar* gs_uintTemplates64[wxPG_UINT_TEMPLATE_MAX] = {
364 wxT("%") wxLongLongFmtSpec wxT("x"),
365 wxT("0x%") wxLongLongFmtSpec wxT("x"),
366 wxT("$%") wxLongLongFmtSpec wxT("x"),
367 wxT("%") wxLongLongFmtSpec wxT("X"),
368 wxT("0x%") wxLongLongFmtSpec wxT("X"),
369 wxT("$%") wxLongLongFmtSpec wxT("X"),
370 wxT("%") wxLongLongFmtSpec wxT("u"),
371 wxT("%") wxLongLongFmtSpec wxT("o")
372};
373
374WX_PG_IMPLEMENT_PROPERTY_CLASS(wxUIntProperty,wxPGProperty,
375 long,unsigned long,TextCtrl)
376
377void wxUIntProperty::Init()
378{
379 m_base = 6; // This is magic number for dec base (must be same as in setattribute)
380 m_realBase = 10;
381 m_prefix = wxPG_PREFIX_NONE;
382}
383
384wxUIntProperty::wxUIntProperty( const wxString& label, const wxString& name,
385 unsigned long value ) : wxPGProperty(label,name)
386{
387 Init();
388 SetValue((long)value);
389}
390
391wxUIntProperty::wxUIntProperty( const wxString& label, const wxString& name,
392 const wxULongLong& value ) : wxPGProperty(label,name)
393{
394 Init();
0372d42e 395 SetValue(WXVARIANT(value));
1c4293cb
VZ
396}
397
398wxUIntProperty::~wxUIntProperty() { }
399
1425eca5
JS
400wxString wxUIntProperty::ValueToString( wxVariant& value,
401 int WXUNUSED(argFlags) ) const
1c4293cb
VZ
402{
403 size_t index = m_base + m_prefix;
404 if ( index >= wxPG_UINT_TEMPLATE_MAX )
405 index = wxPG_BASE_DEC;
406
1425eca5 407 if ( value.GetType() == wxPG_VARIANT_TYPE_LONG )
0372d42e 408 {
1425eca5 409 return wxString::Format(gs_uintTemplates32[index], (unsigned long)value.GetLong());
0372d42e
JS
410 }
411
412 wxULongLong ull;
1425eca5 413 ull << value;
0372d42e
JS
414
415 return wxString::Format(gs_uintTemplates64[index], ull.GetValue());
1c4293cb
VZ
416}
417
418bool wxUIntProperty::StringToValue( wxVariant& variant, const wxString& text, int WXUNUSED(argFlags) ) const
419{
0372d42e
JS
420 wxString variantType = variant.GetType();
421 bool isPrevLong = variantType == wxPG_VARIANT_TYPE_LONG;
1c4293cb
VZ
422
423 if ( text.length() == 0 )
424 {
425 variant.MakeNull();
426 return true;
427 }
428
429 size_t start = 0;
430 if ( text[0] == wxS('$') )
431 start++;
432
433 wxULongLong_t value64 = 0;
434 wxString s = text.substr(start, text.length() - start);
435
436 if ( s.ToULongLong(&value64, (unsigned int)m_realBase) )
437 {
438 if ( value64 >= LONG_MAX )
439 {
0372d42e
JS
440 bool doChangeValue = isPrevLong;
441
442 if ( !isPrevLong && variantType == wxULongLong_VariantType )
1c4293cb 443 {
0372d42e
JS
444 wxULongLong oldValue;
445 oldValue << variant;
446 if ( oldValue.GetValue() != value64 )
447 doChangeValue = true;
448 }
449
450 if ( doChangeValue )
451 {
452 wxULongLong ull(value64);
453 variant << ull;
1c4293cb
VZ
454 return true;
455 }
456 }
457 else
458 {
459 unsigned long value32 = wxLongLong(value64).GetLo();
0c931dd4 460 if ( !isPrevLong || m_value != (long)value32 )
1c4293cb
VZ
461 {
462 variant = (long)value32;
463 return true;
464 }
465 }
466
467 }
468 return false;
469}
470
471bool wxUIntProperty::IntToValue( wxVariant& variant, int number, int WXUNUSED(argFlags) ) const
472{
0c931dd4 473 if ( variant != (long)number )
1c4293cb
VZ
474 {
475 variant = (long)number;
476 return true;
477 }
478 return false;
479}
480
1c4293cb
VZ
481bool wxUIntProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& validationInfo ) const
482{
483 // Check for min/max
484 wxULongLong_t ll;
485 if ( wxPGVariantToULongLong(value, &ll) )
486 {
febd3dca 487 wxULongLong_t min = 0;
1c4293cb
VZ
488 wxULongLong_t max = wxUINT64_MAX;
489 wxVariant variant;
490
491 variant = GetAttribute(wxPGGlobalVars->m_strMin);
492 if ( !variant.IsNull() )
493 {
494 wxPGVariantToULongLong(variant, &min);
495 if ( ll < min )
496 {
2a8312bc
JS
497 validationInfo.SetFailureMessage(
498 wxString::Format(_("Value must be %llu or higher"),min)
499 );
1c4293cb
VZ
500 return false;
501 }
502 }
503 variant = GetAttribute(wxPGGlobalVars->m_strMax);
504 if ( !variant.IsNull() )
505 {
506 wxPGVariantToULongLong(variant, &max);
507 if ( ll > max )
508 {
2a8312bc
JS
509 validationInfo.SetFailureMessage(
510 wxString::Format(_("Value must be %llu or less"),max)
511 );
1c4293cb
VZ
512 return false;
513 }
514 }
515 }
516 return true;
517}
518
519bool wxUIntProperty::DoSetAttribute( const wxString& name, wxVariant& value )
520{
521 if ( name == wxPG_UINT_BASE )
522 {
523 int val = value.GetLong();
524
525 m_realBase = (wxByte) val;
526 if ( m_realBase > 16 )
527 m_realBase = 16;
528
529 //
530 // Translate logical base to a template array index
531 m_base = 7; // oct
532 if ( val == wxPG_BASE_HEX )
533 m_base = 3;
534 else if ( val == wxPG_BASE_DEC )
535 m_base = 6;
536 else if ( val == wxPG_BASE_HEXL )
537 m_base = 0;
538 return true;
539 }
540 else if ( name == wxPG_UINT_PREFIX )
541 {
542 m_prefix = (wxByte) value.GetLong();
543 return true;
544 }
545 return false;
546}
547
548// -----------------------------------------------------------------------
549// wxFloatProperty
550// -----------------------------------------------------------------------
551
552WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFloatProperty,wxPGProperty,
553 double,double,TextCtrl)
554
555wxFloatProperty::wxFloatProperty( const wxString& label,
556 const wxString& name,
557 double value )
558 : wxPGProperty(label,name)
559{
560 m_precision = -1;
561 SetValue(value);
562}
563
564wxFloatProperty::~wxFloatProperty() { }
565
566// This helper method provides standard way for floating point-using
567// properties to convert values to string.
568void wxPropertyGrid::DoubleToString(wxString& target,
569 double value,
570 int precision,
571 bool removeZeroes,
572 wxString* precTemplate)
573{
574 if ( precision >= 0 )
575 {
576 wxString text1;
577 if (!precTemplate)
578 precTemplate = &text1;
579
580 if ( !precTemplate->length() )
581 {
582 *precTemplate = wxS("%.");
583 *precTemplate << wxString::Format( wxS("%i"), precision );
584 *precTemplate << wxS('f');
585 }
586
587 target.Printf( precTemplate->c_str(), value );
588 }
589 else
590 {
591 target.Printf( wxS("%f"), value );
592 }
593
594 if ( removeZeroes && precision != 0 && target.length() )
595 {
596 // Remove excess zeroes (do not remove this code just yet,
597 // since sprintf can't do the same consistently across platforms).
598 wxString::const_iterator i = target.end() - 1;
599 size_t new_len = target.length() - 1;
600
b7bc9d80 601 for ( ; i != target.begin(); --i )
1c4293cb
VZ
602 {
603 if ( *i != wxS('0') )
604 break;
605 new_len--;
606 }
607
608 wxChar cur_char = *i;
609 if ( cur_char != wxS('.') && cur_char != wxS(',') )
610 new_len++;
611
612 if ( new_len != target.length() )
613 target.resize(new_len);
614 }
615}
616
1425eca5
JS
617wxString wxFloatProperty::ValueToString( wxVariant& value,
618 int argFlags ) const
1c4293cb
VZ
619{
620 wxString text;
1425eca5 621 if ( !value.IsNull() )
1c4293cb
VZ
622 {
623 wxPropertyGrid::DoubleToString(text,
1425eca5 624 value,
1c4293cb
VZ
625 m_precision,
626 !(argFlags & wxPG_FULL_VALUE),
d3b9f782 627 NULL);
1c4293cb
VZ
628 }
629 return text;
630}
631
632bool wxFloatProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
633{
634 wxString s;
635 double value;
636
637 if ( text.length() == 0 )
638 {
639 variant.MakeNull();
640 return true;
641 }
642
643 bool res = text.ToDouble(&value);
644 if ( res )
645 {
0c931dd4 646 if ( variant != value )
1c4293cb
VZ
647 {
648 variant = value;
649 return true;
650 }
651 }
652 else if ( argFlags & wxPG_REPORT_ERROR )
653 {
654 }
655 return false;
656}
657
658bool wxFloatProperty::DoValidation( const wxPGProperty* property, double& value, wxPGValidationInfo* pValidationInfo, int mode )
659{
660 // Check for min/max
661 double min = (double)wxINT64_MIN;
662 double max = (double)wxINT64_MAX;
663 wxVariant variant;
664 bool minOk = false;
665 bool maxOk = false;
666
667 variant = property->GetAttribute(wxPGGlobalVars->m_strMin);
668 if ( !variant.IsNull() )
669 {
670 wxPGVariantToDouble(variant, &min);
671 minOk = true;
672 }
673
674 variant = property->GetAttribute(wxPGGlobalVars->m_strMax);
675 if ( !variant.IsNull() )
676 {
677 wxPGVariantToDouble(variant, &max);
678 maxOk = true;
679 }
680
681 if ( minOk )
682 {
683 if ( value < min )
684 {
685 if ( mode == wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE )
2a8312bc
JS
686 pValidationInfo->SetFailureMessage(
687 wxString::Format(_("Value must be %f or higher"),min)
688 );
1c4293cb
VZ
689 else if ( mode == wxPG_PROPERTY_VALIDATION_SATURATE )
690 value = min;
691 else
692 value = max - (min - value);
693 return false;
694 }
695 }
696
697 if ( maxOk )
698 {
699 wxPGVariantToDouble(variant, &max);
700 if ( value > max )
701 {
702 if ( mode == wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE )
2a8312bc
JS
703 pValidationInfo->SetFailureMessage(
704 wxString::Format(_("Value must be %f or less"),max)
705 );
1c4293cb
VZ
706 else if ( mode == wxPG_PROPERTY_VALIDATION_SATURATE )
707 value = max;
708 else
709 value = min + (value - max);
710 return false;
711 }
712 }
713 return true;
714}
715
716bool wxFloatProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& validationInfo ) const
717{
718 double fpv;
719 if ( wxPGVariantToDouble(value, &fpv) )
720 return DoValidation(this, fpv, &validationInfo, wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE);
721 return true;
722}
723
724bool wxFloatProperty::DoSetAttribute( const wxString& name, wxVariant& value )
725{
726 if ( name == wxPG_FLOAT_PRECISION )
727 {
728 m_precision = value.GetLong();
729 return true;
730 }
731 return false;
732}
733
734wxValidator* wxFloatProperty::DoGetValidator() const
735{
736 return wxIntProperty::GetClassValidator();
737}
738
739// -----------------------------------------------------------------------
740// wxBoolProperty
741// -----------------------------------------------------------------------
742
743// We cannot use standard WX_PG_IMPLEMENT_PROPERTY_CLASS macro, since
744// there is a custom GetEditorClass.
745
746IMPLEMENT_DYNAMIC_CLASS(wxBoolProperty, wxPGProperty)
747
748const wxPGEditor* wxBoolProperty::DoGetEditorClass() const
749{
750 // Select correct editor control.
751#if wxPG_INCLUDE_CHECKBOX
752 if ( !(m_flags & wxPG_PROP_USE_CHECKBOX) )
c26873c8
JS
753 return wxPGEditor_Choice;
754 return wxPGEditor_CheckBox;
1c4293cb 755#else
c26873c8 756 return wxPGEditor_Choice;
1c4293cb
VZ
757#endif
758}
759
760wxBoolProperty::wxBoolProperty( const wxString& label, const wxString& name, bool value ) :
761 wxPGProperty(label,name)
762{
939d9364
JS
763 m_choices.Assign(wxPGGlobalVars->m_boolChoices);
764
1c4293cb
VZ
765 SetValue(wxPGVariant_Bool(value));
766
767 m_flags |= wxPG_PROP_USE_DCC;
768}
769
770wxBoolProperty::~wxBoolProperty() { }
771
1425eca5
JS
772wxString wxBoolProperty::ValueToString( wxVariant& value,
773 int argFlags ) const
1c4293cb 774{
1425eca5 775 bool boolValue = value.GetBool();
1c4293cb
VZ
776
777 // As a fragment of composite string value,
778 // make it a little more readable.
779 if ( argFlags & wxPG_COMPOSITE_FRAGMENT )
780 {
1425eca5 781 if ( boolValue )
1c4293cb
VZ
782 {
783 return m_label;
784 }
785 else
786 {
787 if ( argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT )
788 return wxEmptyString;
789
18b5bcb0 790 wxString notFmt;
1c4293cb
VZ
791 if ( wxPGGlobalVars->m_autoGetTranslation )
792 notFmt = _("Not %s");
793 else
18b5bcb0 794 notFmt = wxS("Not %s");
1c4293cb 795
18b5bcb0 796 return wxString::Format(notFmt.c_str(), m_label.c_str());
1c4293cb
VZ
797 }
798 }
799
800 if ( !(argFlags & wxPG_FULL_VALUE) )
801 {
1425eca5 802 return wxPGGlobalVars->m_boolChoices[boolValue?1:0].GetText();
1c4293cb
VZ
803 }
804
805 wxString text;
806
1425eca5 807 if ( boolValue ) text = wxS("true");
1c4293cb
VZ
808 else text = wxS("false");
809
810 return text;
811}
812
1c4293cb
VZ
813bool wxBoolProperty::StringToValue( wxVariant& variant, const wxString& text, int WXUNUSED(argFlags) ) const
814{
0c931dd4 815 bool boolValue = false;
1c4293cb
VZ
816 if ( text.CmpNoCase(wxPGGlobalVars->m_boolChoices[1].GetText()) == 0 ||
817 text.CmpNoCase(wxS("true")) == 0 ||
818 text.CmpNoCase(m_label) == 0 )
0c931dd4 819 boolValue = true;
1c4293cb
VZ
820
821 if ( text.length() == 0 )
822 {
823 variant.MakeNull();
824 return true;
825 }
826
0c931dd4 827 if ( variant != boolValue )
1c4293cb 828 {
0c931dd4 829 variant = wxPGVariant_Bool(boolValue);
1c4293cb
VZ
830 return true;
831 }
832 return false;
833}
834
835bool wxBoolProperty::IntToValue( wxVariant& variant, int value, int ) const
836{
837 bool boolValue = value ? true : false;
1c4293cb 838
0c931dd4 839 if ( variant != boolValue )
1c4293cb
VZ
840 {
841 variant = wxPGVariant_Bool(boolValue);
842 return true;
843 }
844 return false;
845}
846
847bool wxBoolProperty::DoSetAttribute( const wxString& name, wxVariant& value )
848{
849#if wxPG_INCLUDE_CHECKBOX
850 if ( name == wxPG_BOOL_USE_CHECKBOX )
851 {
852 int ival = wxPGVariantToInt(value);
853 if ( ival )
854 m_flags |= wxPG_PROP_USE_CHECKBOX;
855 else
856 m_flags &= ~(wxPG_PROP_USE_CHECKBOX);
857 return true;
858 }
859#endif
860 if ( name == wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING )
861 {
862 int ival = wxPGVariantToInt(value);
863 if ( ival )
864 m_flags |= wxPG_PROP_USE_DCC;
865 else
866 m_flags &= ~(wxPG_PROP_USE_DCC);
867 return true;
868 }
869 return false;
870}
871
872// -----------------------------------------------------------------------
78f2d746 873// wxEnumProperty
1c4293cb
VZ
874// -----------------------------------------------------------------------
875
78f2d746
JS
876IMPLEMENT_DYNAMIC_CLASS(wxEnumProperty, wxPGProperty)
877
878WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEnumProperty,long,Choice)
879
880wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name, const wxChar** labels,
881 const long* values, int value ) : wxPGProperty(label,name)
882{
883 SetIndex(0);
884
885 if ( labels )
886 {
887 m_choices.Add(labels,values);
888
889 if ( GetItemCount() )
890 SetValue( (long)value );
891 }
892}
893
894wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name, const wxChar** labels,
895 const long* values, wxPGChoices* choicesCache, int value )
896 : wxPGProperty(label,name)
897{
898 SetIndex(0);
899
900 wxASSERT( choicesCache );
901
902 if ( choicesCache->IsOk() )
903 {
904 m_choices.Assign( *choicesCache );
905 m_value = wxPGVariant_Zero;
906 }
907 else if ( labels )
908 {
909 m_choices.Add(labels,values);
910
911 if ( GetItemCount() )
912 SetValue( (long)value );
913 }
914}
915
916wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name,
917 const wxArrayString& labels, const wxArrayInt& values, int value )
918 : wxPGProperty(label,name)
919{
920 SetIndex(0);
921
922 if ( &labels && labels.size() )
923 {
924 m_choices.Set(labels, values);
925
926 if ( GetItemCount() )
927 SetValue( (long)value );
928 }
929}
1c4293cb 930
78f2d746
JS
931wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name,
932 wxPGChoices& choices, int value )
1c4293cb
VZ
933 : wxPGProperty(label,name)
934{
78f2d746
JS
935 m_choices.Assign( choices );
936
937 if ( GetItemCount() )
938 SetValue( (long)value );
1c4293cb
VZ
939}
940
78f2d746 941int wxEnumProperty::GetIndexForValue( int value ) const
1c4293cb 942{
78f2d746
JS
943 if ( !m_choices.IsOk() )
944 return -1;
945
946 int intVal = m_choices.Index(value);
947 if ( intVal >= 0 )
948 return intVal;
949
1c4293cb
VZ
950 return value;
951}
952
78f2d746
JS
953wxEnumProperty::~wxEnumProperty ()
954{
955}
956
957int wxEnumProperty::ms_nextIndex = -2;
958
959void wxEnumProperty::OnSetValue()
1c4293cb 960{
0372d42e
JS
961 wxString variantType = m_value.GetType();
962
963 if ( variantType == wxPG_VARIANT_TYPE_LONG )
1c4293cb 964 ValueFromInt_( m_value, m_value.GetLong(), wxPG_FULL_VALUE );
0372d42e 965 else if ( variantType == wxPG_VARIANT_TYPE_STRING )
1c4293cb
VZ
966 ValueFromString_( m_value, m_value.GetString(), 0 );
967 else
b7bc9d80 968 wxFAIL;
1c4293cb
VZ
969
970 if ( ms_nextIndex != -2 )
971 {
972 m_index = ms_nextIndex;
973 ms_nextIndex = -2;
974 }
975}
976
78f2d746 977bool wxEnumProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& WXUNUSED(validationInfo) ) const
1c4293cb
VZ
978{
979 // Make sure string value is in the list,
980 // unless property has string as preferred value type
981 // To reduce code size, use conversion here as well
0372d42e 982 if ( value.GetType() == wxPG_VARIANT_TYPE_STRING &&
1c4293cb
VZ
983 !this->IsKindOf(CLASSINFO(wxEditEnumProperty)) )
984 return ValueFromString_( value, value.GetString(), wxPG_PROPERTY_SPECIFIC );
985
986 return true;
987}
988
78f2d746 989wxString wxEnumProperty::ValueToString( wxVariant& value,
1425eca5 990 int WXUNUSED(argFlags) ) const
1c4293cb 991{
1425eca5
JS
992 if ( value.GetType() == wxPG_VARIANT_TYPE_STRING )
993 return value.GetString();
1c4293cb 994
1425eca5
JS
995 int index = m_choices.Index(value.GetLong());
996 if ( index < 0 )
997 return wxEmptyString;
1c4293cb 998
1425eca5 999 return m_choices.GetLabel(index);
1c4293cb
VZ
1000}
1001
78f2d746 1002bool wxEnumProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
1c4293cb
VZ
1003{
1004 return ValueFromString_( variant, text, argFlags );
1005}
1006
78f2d746 1007bool wxEnumProperty::IntToValue( wxVariant& variant, int intVal, int argFlags ) const
1c4293cb
VZ
1008{
1009 return ValueFromInt_( variant, intVal, argFlags );
1010}
1011
78f2d746 1012bool wxEnumProperty::ValueFromString_( wxVariant& value, const wxString& text, int argFlags ) const
1c4293cb 1013{
1c4293cb
VZ
1014 int useIndex = -1;
1015 long useValue = 0;
1016
78f2d746 1017 for ( unsigned int i=0; i<m_choices.GetCount(); i++ )
1c4293cb 1018 {
78f2d746
JS
1019 const wxString& entryLabel = m_choices.GetLabel(i);
1020 if ( text.CmpNoCase(entryLabel) == 0 )
1c4293cb
VZ
1021 {
1022 useIndex = (int)i;
78f2d746 1023 useValue = m_choices.GetValue(i);
1c4293cb
VZ
1024 break;
1025 }
1c4293cb
VZ
1026 }
1027
1028 bool asText = false;
1029
1030 bool isEdit = this->IsKindOf(CLASSINFO(wxEditEnumProperty));
1031
1032 // If text not any of the choices, store as text instead
1033 // (but only if we are wxEditEnumProperty)
1034 if ( useIndex == -1 &&
0372d42e 1035 (value.GetType() != wxPG_VARIANT_TYPE_STRING || (m_value.GetString() != text)) &&
1c4293cb
VZ
1036 isEdit )
1037 {
1038 asText = true;
1039 }
1040
1041 int setAsNextIndex = -2;
1042
1043 if ( asText )
1044 {
1045 setAsNextIndex = -1;
1046 value = text;
1047 }
31f0b483 1048 else if ( useIndex != GetIndex() )
1c4293cb
VZ
1049 {
1050 if ( useIndex != -1 )
1051 {
1052 setAsNextIndex = useIndex;
1053 value = (long)useValue;
1054 }
1055 else
1056 {
1057 setAsNextIndex = -1;
1058 value = wxPGVariant_MinusOne;
1059 }
1060 }
1061
1062 if ( setAsNextIndex != -2 )
1063 {
1064 // If wxPG_PROPERTY_SPECIFIC is set, then this is done for
1065 // validation purposes only, and index must not be changed
1066 if ( !(argFlags & wxPG_PROPERTY_SPECIFIC) )
1067 ms_nextIndex = setAsNextIndex;
1068
1069 if ( isEdit || setAsNextIndex != -1 )
1070 return true;
1071 else
1072 return false;
1073 }
1074 return false;
1075}
1076
78f2d746 1077bool wxEnumProperty::ValueFromInt_( wxVariant& variant, int intVal, int argFlags ) const
1c4293cb
VZ
1078{
1079 // If wxPG_FULL_VALUE is *not* in argFlags, then intVal is index from combo box.
1080 //
1081 ms_nextIndex = -2;
1082
1083 if ( argFlags & wxPG_FULL_VALUE )
1084 {
1085 ms_nextIndex = GetIndexForValue( intVal );
1086 }
1087 else
1088 {
31f0b483 1089 if ( intVal != GetIndex() )
1c4293cb
VZ
1090 {
1091 ms_nextIndex = intVal;
1092 }
1093 }
1094
1095 if ( ms_nextIndex != -2 )
1096 {
1097 if ( !(argFlags & wxPG_FULL_VALUE) )
78f2d746 1098 intVal = m_choices.GetValue(intVal);
1c4293cb
VZ
1099
1100 variant = (long)intVal;
1101
1102 return true;
1103 }
1104
1105 return false;
1106}
1107
d8812c6e
JS
1108void
1109wxEnumProperty::OnValidationFailure( wxVariant& WXUNUSED(pendingValue) )
1110{
1111 // Revert index
d8812c6e
JS
1112 ResetNextIndex();
1113}
1114
78f2d746 1115void wxEnumProperty::SetIndex( int index )
1c4293cb
VZ
1116{
1117 ms_nextIndex = -2;
1118 m_index = index;
1119}
1120
78f2d746 1121int wxEnumProperty::GetIndex() const
1c4293cb 1122{
31f0b483
JS
1123 if ( m_value.IsNull() )
1124 return -1;
1125
1c4293cb
VZ
1126 if ( ms_nextIndex != -2 )
1127 return ms_nextIndex;
31f0b483 1128
1c4293cb
VZ
1129 return m_index;
1130}
1131
1c4293cb
VZ
1132// -----------------------------------------------------------------------
1133// wxEditEnumProperty
1134// -----------------------------------------------------------------------
1135
1136IMPLEMENT_DYNAMIC_CLASS(wxEditEnumProperty, wxPGProperty)
1137
1138WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEditEnumProperty,wxString,ComboBox)
1139
1140wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name, const wxChar** labels,
1141 const long* values, const wxString& value )
1142 : wxEnumProperty(label,name,labels,values,0)
1143{
1144 SetValue( value );
1145}
1146
1147wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name, const wxChar** labels,
1148 const long* values, wxPGChoices* choicesCache, const wxString& value )
1149 : wxEnumProperty(label,name,labels,values,choicesCache,0)
1150{
1151 SetValue( value );
1152}
1153
1154wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name,
1155 const wxArrayString& labels, const wxArrayInt& values, const wxString& value )
1156 : wxEnumProperty(label,name,labels,values,0)
1157{
1158 SetValue( value );
1159}
1160
1161wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name,
1162 wxPGChoices& choices, const wxString& value )
1163 : wxEnumProperty(label,name,choices,0)
1164{
1165 SetValue( value );
1166}
1167
1168wxEditEnumProperty::~wxEditEnumProperty()
1169{
1170}
1171
1172// -----------------------------------------------------------------------
1173// wxFlagsProperty
1174// -----------------------------------------------------------------------
1175
1176IMPLEMENT_DYNAMIC_CLASS(wxFlagsProperty,wxPGProperty)
1177
1178WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxFlagsProperty,long,TextCtrl)
1179
1180void wxFlagsProperty::Init()
1181{
1c4293cb
VZ
1182 long value = m_value;
1183
1184 //
1185 // Generate children
1186 //
1187 unsigned int i;
1188
63f62e76 1189 unsigned int prevChildCount = m_children.size();
1c4293cb
VZ
1190
1191 int oldSel = -1;
1192 if ( prevChildCount )
1193 {
1194 wxPropertyGridPageState* state = GetParentState();
1195
1196 // State safety check (it may be NULL in immediate parent)
1197 wxASSERT( state );
1198
1199 if ( state )
1200 {
1201 wxPGProperty* selected = state->GetSelection();
1202 if ( selected )
1203 {
1204 if ( selected->GetParent() == this )
b96a14e3 1205 oldSel = selected->GetIndexInParent();
1c4293cb
VZ
1206 else if ( selected == this )
1207 oldSel = -2;
1208 }
1209 }
1210 state->DoClearSelection();
1211 }
1212
1213 // Delete old children
1214 for ( i=0; i<prevChildCount; i++ )
63f62e76 1215 delete m_children[i];
1c4293cb 1216
63f62e76 1217 m_children.clear();
1c4293cb 1218
16372f0d
JS
1219 // Relay wxPG_BOOL_USE_CHECKBOX and wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING
1220 // to child bool property controls.
1221 long attrUseCheckBox = GetAttributeAsLong(wxPG_BOOL_USE_CHECKBOX, 0);
1222 long attrUseDCC = GetAttributeAsLong(wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING,
1223 0);
1224
1c4293cb
VZ
1225 if ( m_choices.IsOk() )
1226 {
1227 const wxPGChoices& choices = m_choices;
1228
1229 for ( i=0; i<GetItemCount(); i++ )
1230 {
1231 bool child_val;
98c04633 1232 child_val = ( value & choices.GetValue(i) )?true:false;
1c4293cb
VZ
1233
1234 wxPGProperty* boolProp;
d665918b 1235 wxString label = GetLabel(i);
1c4293cb
VZ
1236
1237 #if wxUSE_INTL
1238 if ( wxPGGlobalVars->m_autoGetTranslation )
1239 {
d665918b 1240 boolProp = new wxBoolProperty( ::wxGetTranslation(label), label, child_val );
1c4293cb
VZ
1241 }
1242 else
1243 #endif
1244 {
d665918b 1245 boolProp = new wxBoolProperty( label, label, child_val );
1c4293cb 1246 }
16372f0d
JS
1247 if ( attrUseCheckBox )
1248 boolProp->SetAttribute(wxPG_BOOL_USE_CHECKBOX,
1249 true);
1250 if ( attrUseDCC )
1251 boolProp->SetAttribute(wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING,
1252 true);
48a32cf6 1253 AddPrivateChild(boolProp);
1c4293cb
VZ
1254 }
1255
1256 m_oldChoicesData = m_choices.GetDataPtr();
1257 }
1258
1259 m_oldValue = m_value;
1260
1261 if ( prevChildCount )
1262 SubPropsChanged(oldSel);
1263}
1264
1265wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name,
1266 const wxChar** labels, const long* values, long value ) : wxPGProperty(label,name)
1267{
d3b9f782 1268 m_oldChoicesData = NULL;
1c4293cb
VZ
1269
1270 if ( labels )
1271 {
1272 m_choices.Set(labels,values);
1273
1274 wxASSERT( GetItemCount() );
1275
1276 SetValue( value );
1277 }
1278 else
1279 {
1280 m_value = wxPGVariant_Zero;
1281 }
1282}
1283
1284wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name,
1285 const wxArrayString& labels, const wxArrayInt& values, int value )
1286 : wxPGProperty(label,name)
1287{
d3b9f782 1288 m_oldChoicesData = NULL;
1c4293cb
VZ
1289
1290 if ( &labels && labels.size() )
1291 {
1292 m_choices.Set(labels,values);
1293
1294 wxASSERT( GetItemCount() );
1295
1296 SetValue( (long)value );
1297 }
1298 else
1299 {
1300 m_value = wxPGVariant_Zero;
1301 }
1302}
1303
1304wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name,
1305 wxPGChoices& choices, long value )
1306 : wxPGProperty(label,name)
1307{
d3b9f782 1308 m_oldChoicesData = NULL;
1c4293cb
VZ
1309
1310 if ( choices.IsOk() )
1311 {
1312 m_choices.Assign(choices);
1313
1314 wxASSERT( GetItemCount() );
1315
1316 SetValue( value );
1317 }
1318 else
1319 {
1320 m_value = wxPGVariant_Zero;
1321 }
1322}
1323
1324wxFlagsProperty::~wxFlagsProperty()
1325{
1326}
1327
1328void wxFlagsProperty::OnSetValue()
1329{
1330 if ( !m_choices.IsOk() || !GetItemCount() )
1331 {
1332 m_value = wxPGVariant_Zero;
1333 }
1334 else
1335 {
1336 long val = m_value.GetLong();
1337
1338 long fullFlags = 0;
1339
1340 // normalize the value (i.e. remove extra flags)
1341 unsigned int i;
1342 const wxPGChoices& choices = m_choices;
1343 for ( i = 0; i < GetItemCount(); i++ )
1344 {
98c04633 1345 fullFlags |= choices.GetValue(i);
1c4293cb
VZ
1346 }
1347
1348 val &= fullFlags;
1349
1350 m_value = val;
1351
1352 // Need to (re)init now?
1353 if ( GetChildCount() != GetItemCount() ||
1354 m_choices.GetDataPtr() != m_oldChoicesData )
1355 {
1356 Init();
1357 }
1358 }
1359
1360 long newFlags = m_value;
1361
1362 if ( newFlags != m_oldValue )
1363 {
1364 // Set child modified states
1365 unsigned int i;
1366 const wxPGChoices& choices = m_choices;
1367 for ( i = 0; i<GetItemCount(); i++ )
1368 {
1369 int flag;
1370
98c04633 1371 flag = choices.GetValue(i);
1c4293cb
VZ
1372
1373 if ( (newFlags & flag) != (m_oldValue & flag) )
1374 Item(i)->SetFlag( wxPG_PROP_MODIFIED );
1375 }
1376
1377 m_oldValue = newFlags;
1378 }
1379}
1380
1425eca5
JS
1381wxString wxFlagsProperty::ValueToString( wxVariant& value,
1382 int WXUNUSED(argFlags) ) const
1c4293cb
VZ
1383{
1384 wxString text;
1385
1386 if ( !m_choices.IsOk() )
1387 return text;
1388
1425eca5 1389 long flags = value;
1c4293cb
VZ
1390 unsigned int i;
1391 const wxPGChoices& choices = m_choices;
1392
1393 for ( i = 0; i < GetItemCount(); i++ )
1394 {
1395 int doAdd;
98c04633 1396 doAdd = ( flags & choices.GetValue(i) );
1c4293cb
VZ
1397
1398 if ( doAdd )
1399 {
1400 text += choices.GetLabel(i);
1401 text += wxS(", ");
1402 }
1403 }
1404
1405 // remove last comma
1406 if ( text.Len() > 1 )
1407 text.Truncate ( text.Len() - 2 );
1408
1409 return text;
1410}
1411
1412// Translate string into flag tokens
1413bool wxFlagsProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const
1414{
1415 if ( !m_choices.IsOk() )
1416 return false;
1417
1418 long newFlags = 0;
1c4293cb
VZ
1419
1420 // semicolons are no longer valid delimeters
1421 WX_PG_TOKENIZER1_BEGIN(text,wxS(','))
1422
1423 if ( token.length() )
1424 {
1425 // Determine which one it is
1426 long bit = IdToBit( token );
1427
1428 if ( bit != -1 )
1429 {
1430 // Changed?
1431 newFlags |= bit;
1432 }
1433 else
1434 {
1435 break;
1436 }
1437 }
1438
1439 WX_PG_TOKENIZER1_END()
1440
0c931dd4
JS
1441 if ( variant != (long)newFlags )
1442 {
1443 variant = (long)newFlags;
1c4293cb 1444 return true;
0c931dd4 1445 }
1c4293cb
VZ
1446
1447 return false;
1448}
1449
1450// Converts string id to a relevant bit.
1451long wxFlagsProperty::IdToBit( const wxString& id ) const
1452{
1453 unsigned int i;
1454 for ( i = 0; i < GetItemCount(); i++ )
1455 {
1456 if ( id == GetLabel(i) )
1457 {
98c04633 1458 return m_choices.GetValue(i);
1c4293cb
VZ
1459 }
1460 }
1461 return -1;
1462}
1463
1464void wxFlagsProperty::RefreshChildren()
1465{
1466 if ( !m_choices.IsOk() || !GetChildCount() ) return;
1467
1468 int flags = m_value.GetLong();
1469
1470 const wxPGChoices& choices = m_choices;
1471 unsigned int i;
1472 for ( i = 0; i < GetItemCount(); i++ )
1473 {
1474 long flag;
1475
98c04633 1476 flag = choices.GetValue(i);
1c4293cb
VZ
1477
1478 long subVal = flags & flag;
1479 wxPGProperty* p = Item(i);
1480
1481 if ( subVal != (m_oldValue & flag) )
1482 p->SetFlag( wxPG_PROP_MODIFIED );
1483
1484 p->SetValue( subVal?true:false );
1485 }
1486
1487 m_oldValue = flags;
1488}
1489
1490void wxFlagsProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const
1491{
1492 long oldValue = thisValue.GetLong();
1493 long val = childValue.GetLong();
98c04633 1494 unsigned long vi = m_choices.GetValue(childIndex);
1c4293cb
VZ
1495 if ( val )
1496 thisValue = (long)(oldValue | vi);
1497 else
1498 thisValue = (long)(oldValue & ~(vi));
1499}
1500
16372f0d
JS
1501bool wxFlagsProperty::DoSetAttribute( const wxString& name, wxVariant& value )
1502{
1503 if ( name == wxPG_BOOL_USE_CHECKBOX ||
1504 name == wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING )
1505 {
1506 for ( size_t i=0; i<GetChildCount(); i++ )
1507 {
1508 Item(i)->SetAttribute(name, value);
1509 }
1510 // Must return false so that the attribute is stored in
1511 // flag property's actual property storage
1512 return false;
1513 }
1514 return false;
1515}
1516
1c4293cb
VZ
1517// -----------------------------------------------------------------------
1518// wxDirProperty
1519// -----------------------------------------------------------------------
1520
d53f610c 1521IMPLEMENT_DYNAMIC_CLASS(wxDirProperty, wxLongStringProperty)
1c4293cb
VZ
1522
1523wxDirProperty::wxDirProperty( const wxString& name, const wxString& label, const wxString& value )
1524 : wxLongStringProperty(name,label,value)
1525{
3a89adc1 1526 m_flags |= wxPG_PROP_NO_ESCAPE;
1c4293cb 1527}
3a89adc1 1528
1c4293cb
VZ
1529wxDirProperty::~wxDirProperty() { }
1530
1531wxValidator* wxDirProperty::DoGetValidator() const
1532{
1533 return wxFileProperty::GetClassValidator();
1534}
1535
1536bool wxDirProperty::OnButtonClick( wxPropertyGrid* propGrid, wxString& value )
1537{
3a89adc1 1538 // Update property value from editor, if necessary
1c4293cb
VZ
1539 wxSize dlg_sz(300,400);
1540
bd8b65be
VZ
1541 wxString dlgMessage(m_dlgMessage);
1542 if ( dlgMessage.empty() )
1543 dlgMessage = _("Choose a directory:");
1c4293cb 1544 wxDirDialog dlg( propGrid,
bd8b65be 1545 dlgMessage,
1c4293cb
VZ
1546 value,
1547 0,
1548#if !wxPG_SMALL_SCREEN
1549 propGrid->GetGoodEditorDialogPosition(this,dlg_sz),
bd8b65be 1550 dlg_sz
1c4293cb
VZ
1551#else
1552 wxDefaultPosition,
bd8b65be 1553 wxDefaultSize
1c4293cb 1554#endif
bd8b65be 1555 );
1c4293cb
VZ
1556
1557 if ( dlg.ShowModal() == wxID_OK )
1558 {
1559 value = dlg.GetPath();
1560 return true;
1561 }
1562 return false;
1563}
1564
1565bool wxDirProperty::DoSetAttribute( const wxString& name, wxVariant& value )
1566{
1567 if ( name == wxPG_DIR_DIALOG_MESSAGE )
1568 {
1569 m_dlgMessage = value.GetString();
1570 return true;
1571 }
1572 return false;
1573}
1574
1575// -----------------------------------------------------------------------
1576// wxPGFileDialogAdapter
1577// -----------------------------------------------------------------------
1578
1579bool wxPGFileDialogAdapter::DoShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property )
1580{
1581 wxFileProperty* fileProp = NULL;
1582 wxString path;
1583 int indFilter = -1;
1584
1585 if ( property->IsKindOf(CLASSINFO(wxFileProperty)) )
1586 {
1587 fileProp = ((wxFileProperty*)property);
1425eca5
JS
1588 wxFileName filename = fileProp->GetValue().GetString();
1589 path = filename.GetPath();
1c4293cb
VZ
1590 indFilter = fileProp->m_indFilter;
1591
1592 if ( !path.length() && fileProp->m_basePath.length() )
1593 path = fileProp->m_basePath;
1594 }
1595 else
1596 {
1597 wxFileName fn(property->GetValue().GetString());
1598 path = fn.GetPath();
1599 }
1600
1601 wxFileDialog dlg( propGrid->GetPanel(),
1602 property->GetAttribute(wxS("DialogTitle"), _("Choose a file")),
1603 property->GetAttribute(wxS("InitialPath"), path),
1604 wxEmptyString,
1605 property->GetAttribute(wxPG_FILE_WILDCARD, _("All files (*.*)|*.*")),
1606 0,
1607 wxDefaultPosition );
1608
1609 if ( indFilter >= 0 )
1610 dlg.SetFilterIndex( indFilter );
1611
1612 if ( dlg.ShowModal() == wxID_OK )
1613 {
1614 if ( fileProp )
1615 fileProp->m_indFilter = dlg.GetFilterIndex();
1616 SetValue( dlg.GetPath() );
1617 return true;
1618 }
1619 return false;
1620}
1621
1622// -----------------------------------------------------------------------
1623// wxFileProperty
1624// -----------------------------------------------------------------------
1625
1626WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFileProperty,wxPGProperty,
1627 wxString,const wxString&,TextCtrlAndButton)
1628
1629wxFileProperty::wxFileProperty( const wxString& label, const wxString& name,
1630 const wxString& value ) : wxPGProperty(label,name)
1631{
1632 m_flags |= wxPG_PROP_SHOW_FULL_FILENAME;
1633 m_indFilter = -1;
1634 SetAttribute( wxPG_FILE_WILDCARD, _("All files (*.*)|*.*") );
1635
1636 SetValue(value);
1637}
1638
1639wxFileProperty::~wxFileProperty() {}
1640
1641#if wxUSE_VALIDATORS
1642
1643wxValidator* wxFileProperty::GetClassValidator()
1644{
1645 WX_PG_DOGETVALIDATOR_ENTRY()
1646
1647 // Atleast wxPython 2.6.2.1 required that the string argument is given
1648 static wxString v;
1649 wxTextValidator* validator = new wxTextValidator(wxFILTER_EXCLUDE_CHAR_LIST,&v);
1650
1651 wxArrayString exChars;
1652 exChars.Add(wxS("?"));
1653 exChars.Add(wxS("*"));
1654 exChars.Add(wxS("|"));
1655 exChars.Add(wxS("<"));
1656 exChars.Add(wxS(">"));
1657 exChars.Add(wxS("\""));
1658
1659 validator->SetExcludes(exChars);
1660
1661 WX_PG_DOGETVALIDATOR_EXIT(validator)
1662}
1663
1664wxValidator* wxFileProperty::DoGetValidator() const
1665{
1666 return GetClassValidator();
1667}
1668
1669#endif
1670
1671void wxFileProperty::OnSetValue()
1672{
1673 const wxString& fnstr = m_value.GetString();
1674
1425eca5 1675 wxFileName filename = fnstr;
1c4293cb 1676
1425eca5 1677 if ( !filename.HasName() )
1c4293cb
VZ
1678 {
1679 m_value = wxPGVariant_EmptyString;
1c4293cb
VZ
1680 }
1681
1682 // Find index for extension.
1683 if ( m_indFilter < 0 && fnstr.length() )
1684 {
1425eca5 1685 wxString ext = filename.GetExt();
1c4293cb
VZ
1686 int curind = 0;
1687 size_t pos = 0;
1688 size_t len = m_wildcard.length();
1689
1690 pos = m_wildcard.find(wxS("|"), pos);
1691 while ( pos != wxString::npos && pos < (len-3) )
1692 {
1693 size_t ext_begin = pos + 3;
1694
1695 pos = m_wildcard.find(wxS("|"), ext_begin);
1696 if ( pos == wxString::npos )
1697 pos = len;
1698 wxString found_ext = m_wildcard.substr(ext_begin, pos-ext_begin);
1699
1700 if ( found_ext.length() > 0 )
1701 {
1702 if ( found_ext[0] == wxS('*') )
1703 {
1704 m_indFilter = curind;
1705 break;
1706 }
1707 if ( ext.CmpNoCase(found_ext) == 0 )
1708 {
1709 m_indFilter = curind;
1710 break;
1711 }
1712 }
1713
1714 if ( pos != len )
1715 pos = m_wildcard.find(wxS("|"), pos+1);
1716
1717 curind++;
1718 }
1719 }
1720}
1721
1425eca5 1722wxFileName wxFileProperty::GetFileName() const
1c4293cb 1723{
1425eca5
JS
1724 wxFileName filename;
1725
1726 if ( !m_value.IsNull() )
1727 filename = m_value.GetString();
1728
1729 return filename;
1730}
1731
1732wxString wxFileProperty::ValueToString( wxVariant& value,
1733 int argFlags ) const
1734{
1735 wxFileName filename = value.GetString();
1736
1737 if ( !filename.HasName() )
1738 return wxEmptyString;
1739
1740 wxString fullName = filename.GetFullName();
1c4293cb 1741 if ( !fullName.length() )
1425eca5 1742 return wxEmptyString;
1c4293cb
VZ
1743
1744 if ( argFlags & wxPG_FULL_VALUE )
1745 {
1425eca5 1746 return filename.GetFullPath();
1c4293cb
VZ
1747 }
1748 else if ( m_flags & wxPG_PROP_SHOW_FULL_FILENAME )
1749 {
1750 if ( m_basePath.Length() )
1751 {
1425eca5 1752 wxFileName fn2(filename);
1c4293cb
VZ
1753 fn2.MakeRelativeTo(m_basePath);
1754 return fn2.GetFullPath();
1755 }
1425eca5 1756 return filename.GetFullPath();
1c4293cb
VZ
1757 }
1758
1425eca5 1759 return filename.GetFullName();
1c4293cb
VZ
1760}
1761
1762wxPGEditorDialogAdapter* wxFileProperty::GetEditorDialog() const
1763{
1764 return new wxPGFileDialogAdapter();
1765}
1766
1767bool wxFileProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
1768{
1425eca5
JS
1769 wxFileName filename = variant.GetString();
1770
1c4293cb
VZ
1771 if ( (m_flags & wxPG_PROP_SHOW_FULL_FILENAME) || (argFlags & wxPG_FULL_VALUE) )
1772 {
1425eca5 1773 if ( filename != text )
1c4293cb
VZ
1774 {
1775 variant = text;
1776 return true;
1777 }
1778 }
1779 else
1780 {
1425eca5 1781 if ( filename.GetFullName() != text )
1c4293cb 1782 {
1425eca5 1783 wxFileName fn = filename;
1c4293cb
VZ
1784 fn.SetFullName(text);
1785 variant = fn.GetFullPath();
1786 return true;
1787 }
1788 }
1789
1790 return false;
1791}
1792
1793bool wxFileProperty::DoSetAttribute( const wxString& name, wxVariant& value )
1794{
1795 // Return false on some occasions to make sure those attribs will get
1796 // stored in m_attributes.
1797 if ( name == wxPG_FILE_SHOW_FULL_PATH )
1798 {
1799 if ( wxPGVariantToInt(value) )
1800 m_flags |= wxPG_PROP_SHOW_FULL_FILENAME;
1801 else
1802 m_flags &= ~(wxPG_PROP_SHOW_FULL_FILENAME);
1803 return true;
1804 }
1805 else if ( name == wxPG_FILE_WILDCARD )
1806 {
1807 m_wildcard = value.GetString();
1808 }
1809 else if ( name == wxPG_FILE_SHOW_RELATIVE_PATH )
1810 {
1811 m_basePath = value.GetString();
1812
1813 // Make sure wxPG_FILE_SHOW_FULL_PATH is also set
1814 m_flags |= wxPG_PROP_SHOW_FULL_FILENAME;
1815 }
1816 else if ( name == wxPG_FILE_INITIAL_PATH )
1817 {
1818 m_initialPath = value.GetString();
1819 return true;
1820 }
1821 else if ( name == wxPG_FILE_DIALOG_TITLE )
1822 {
1823 m_dlgTitle = value.GetString();
1824 return true;
1825 }
1826 return false;
1827}
1828
1829// -----------------------------------------------------------------------
1830// wxPGLongStringDialogAdapter
1831// -----------------------------------------------------------------------
1832
1833bool wxPGLongStringDialogAdapter::DoShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property )
1834{
1835 wxString val1 = property->GetValueAsString(0);
1836 wxString val_orig = val1;
1837
1838 wxString value;
1839 if ( !property->HasFlag(wxPG_PROP_NO_ESCAPE) )
1840 wxPropertyGrid::ExpandEscapeSequences(value, val1);
1841 else
1842 value = wxString(val1);
1843
1844 // Run editor dialog.
1845 if ( wxLongStringProperty::DisplayEditorDialog(property, propGrid, value) )
1846 {
1847 if ( !property->HasFlag(wxPG_PROP_NO_ESCAPE) )
1848 wxPropertyGrid::CreateEscapeSequences(val1,value);
1849 else
1850 val1 = value;
1851
1852 if ( val1 != val_orig )
1853 {
1854 SetValue( val1 );
1855 return true;
1856 }
1857 }
1858 return false;
1859}
1860
1861// -----------------------------------------------------------------------
1862// wxLongStringProperty
1863// -----------------------------------------------------------------------
1864
1865WX_PG_IMPLEMENT_PROPERTY_CLASS(wxLongStringProperty,wxPGProperty,
1866 wxString,const wxString&,TextCtrlAndButton)
1867
1868wxLongStringProperty::wxLongStringProperty( const wxString& label, const wxString& name,
1869 const wxString& value ) : wxPGProperty(label,name)
1870{
1871 SetValue(value);
1872}
1873
1874wxLongStringProperty::~wxLongStringProperty() {}
1875
1425eca5
JS
1876wxString wxLongStringProperty::ValueToString( wxVariant& value,
1877 int WXUNUSED(argFlags) ) const
1c4293cb 1878{
1425eca5 1879 return value;
1c4293cb
VZ
1880}
1881
1882bool wxLongStringProperty::OnEvent( wxPropertyGrid* propGrid, wxWindow* WXUNUSED(primary),
1883 wxEvent& event )
1884{
1885 if ( propGrid->IsMainButtonEvent(event) )
1886 {
1887 // Update the value
703ee9f5 1888 wxVariant useValue = propGrid->GetUncommittedPropertyValue();
1c4293cb 1889
9b5bafcf 1890 wxString val1 = useValue.GetString();
1c4293cb
VZ
1891 wxString val_orig = val1;
1892
1893 wxString value;
1894 if ( !(m_flags & wxPG_PROP_NO_ESCAPE) )
1895 wxPropertyGrid::ExpandEscapeSequences(value,val1);
1896 else
1897 value = wxString(val1);
1898
1899 // Run editor dialog.
1900 if ( OnButtonClick(propGrid,value) )
1901 {
1902 if ( !(m_flags & wxPG_PROP_NO_ESCAPE) )
1903 wxPropertyGrid::CreateEscapeSequences(val1,value);
1904 else
1905 val1 = value;
1906
1907 if ( val1 != val_orig )
1908 {
1909 SetValueInEvent( val1 );
1910 return true;
1911 }
1912 }
1913 }
1914 return false;
1915}
1916
1917bool wxLongStringProperty::OnButtonClick( wxPropertyGrid* propGrid, wxString& value )
1918{
1919 return DisplayEditorDialog(this, propGrid, value);
1920}
1921
1922bool wxLongStringProperty::DisplayEditorDialog( wxPGProperty* prop, wxPropertyGrid* propGrid, wxString& value )
1923
1924{
1925 // launch editor dialog
1926 wxDialog* dlg = new wxDialog(propGrid,-1,prop->GetLabel(),wxDefaultPosition,wxDefaultSize,
1927 wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxCLIP_CHILDREN);
1928
1929 dlg->SetFont(propGrid->GetFont()); // To allow entering chars of the same set as the propGrid
1930
1931 // Multi-line text editor dialog.
1932#if !wxPG_SMALL_SCREEN
1933 const int spacing = 8;
1934#else
1935 const int spacing = 4;
1936#endif
1937 wxBoxSizer* topsizer = new wxBoxSizer( wxVERTICAL );
1938 wxBoxSizer* rowsizer = new wxBoxSizer( wxHORIZONTAL );
1939 wxTextCtrl* ed = new wxTextCtrl(dlg,11,value,
1940 wxDefaultPosition,wxDefaultSize,wxTE_MULTILINE);
1941
1942 rowsizer->Add( ed, 1, wxEXPAND|wxALL, spacing );
1943 topsizer->Add( rowsizer, 1, wxEXPAND, 0 );
29d50f3e
JS
1944
1945 wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
1946 buttonSizer->AddButton(new wxButton(dlg, wxID_OK));
1947 buttonSizer->AddButton(new wxButton(dlg, wxID_CANCEL));
1948 buttonSizer->Realize();
1949 topsizer->Add( buttonSizer, 0,
1950 wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxRIGHT,
1951 spacing );
1c4293cb
VZ
1952
1953 dlg->SetSizer( topsizer );
1954 topsizer->SetSizeHints( dlg );
1955
1956#if !wxPG_SMALL_SCREEN
1957 dlg->SetSize(400,300);
1958
1959 dlg->Move( propGrid->GetGoodEditorDialogPosition(prop,dlg->GetSize()) );
1960#endif
1961
1962 int res = dlg->ShowModal();
1963
1964 if ( res == wxID_OK )
1965 {
1966 value = ed->GetValue();
1967 dlg->Destroy();
1968 return true;
1969 }
1970 dlg->Destroy();
1971 return false;
1972}
1973
1974bool wxLongStringProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const
1975{
0c931dd4 1976 if ( variant != text )
1c4293cb
VZ
1977 {
1978 variant = text;
1979 return true;
1980 }
1981 return false;
1982}
1983
1984// -----------------------------------------------------------------------
1985// wxArrayEditorDialog
1986// -----------------------------------------------------------------------
1987
1988BEGIN_EVENT_TABLE(wxArrayEditorDialog, wxDialog)
1989 EVT_IDLE(wxArrayEditorDialog::OnIdle)
1990 EVT_LISTBOX(24, wxArrayEditorDialog::OnListBoxClick)
1991 EVT_TEXT_ENTER(21, wxArrayEditorDialog::OnAddClick)
1992 EVT_BUTTON(22, wxArrayEditorDialog::OnAddClick)
1993 EVT_BUTTON(23, wxArrayEditorDialog::OnDeleteClick)
1994 EVT_BUTTON(25, wxArrayEditorDialog::OnUpClick)
1995 EVT_BUTTON(26, wxArrayEditorDialog::OnDownClick)
1996 EVT_BUTTON(27, wxArrayEditorDialog::OnUpdateClick)
1997 //EVT_BUTTON(28, wxArrayEditorDialog::OnCustomEditClick)
1998END_EVENT_TABLE()
1999
2000IMPLEMENT_ABSTRACT_CLASS(wxArrayEditorDialog, wxDialog)
2001
3b211af1 2002#include "wx/statline.h"
1c4293cb
VZ
2003
2004// -----------------------------------------------------------------------
2005
2006void wxArrayEditorDialog::OnIdle(wxIdleEvent& event)
2007{
2008 //
2009 // Do control focus detection here.
2010 //
2011
2012 wxWindow* focused = FindFocus();
2013
2014 // This strange focus thing is a workaround for wxGTK wxListBox focus
2015 // reporting bug.
2016 if ( m_curFocus == 0 && focused != m_edValue &&
2017 focused != m_butAdd && focused != m_butUpdate &&
2018 m_lbStrings->GetSelection() >= 0 )
2019 {
2020 // ListBox was just focused.
2021 m_butAdd->Enable(false);
2022 m_butUpdate->Enable(false);
2023 m_butRemove->Enable(true);
2024 m_butUp->Enable(true);
2025 m_butDown->Enable(true);
2026 m_curFocus = 1;
2027 }
2028 else if ( (m_curFocus == 1 && focused == m_edValue) /*|| m_curFocus == 2*/ )
2029 {
2030 // TextCtrl was just focused.
2031 m_butAdd->Enable(true);
2032 bool upd_enable = false;
2033 if ( m_lbStrings->GetCount() && m_lbStrings->GetSelection() >= 0 )
2034 upd_enable = true;
2035 m_butUpdate->Enable(upd_enable);
2036 m_butRemove->Enable(false);
2037 m_butUp->Enable(false);
2038 m_butDown->Enable(false);
2039 m_curFocus = 0;
2040 }
2041
2042 event.Skip();
2043}
2044
2045// -----------------------------------------------------------------------
2046
2047wxArrayEditorDialog::wxArrayEditorDialog()
2048 : wxDialog()
2049{
2050 Init();
2051}
2052
2053// -----------------------------------------------------------------------
2054
2055void wxArrayEditorDialog::Init()
2056{
2057 m_custBtText = (const wxChar*) NULL;
2058}
2059
2060// -----------------------------------------------------------------------
2061
2062wxArrayEditorDialog::wxArrayEditorDialog( wxWindow *parent,
2063 const wxString& message,
2064 const wxString& caption,
2065 long style,
2066 const wxPoint& pos,
2067 const wxSize& sz )
2068 : wxDialog()
2069{
2070 Init();
2071 Create(parent,message,caption,style,pos,sz);
2072}
2073
2074// -----------------------------------------------------------------------
2075
2076bool wxArrayEditorDialog::Create( wxWindow *parent,
2077 const wxString& message,
2078 const wxString& caption,
2079 long style,
2080 const wxPoint& pos,
2081 const wxSize& sz )
2082{
2083 // On wxMAC the dialog shows incorrectly if style is not exactly wxCAPTION
2084 // FIXME: This should be only a temporary fix.
2085#ifdef __WXMAC__
b0f0eda8 2086 wxUnusedVar(style);
1c4293cb
VZ
2087 int useStyle = wxCAPTION;
2088#else
2089 int useStyle = style;
2090#endif
2091
2092 bool res = wxDialog::Create(parent, wxID_ANY, caption, pos, sz, useStyle);
2093
2094 SetFont(parent->GetFont()); // To allow entering chars of the same set as the propGrid
2095
2096#if !wxPG_SMALL_SCREEN
2097 const int spacing = 4;
2098#else
2099 const int spacing = 3;
2100#endif
2101
2102 m_modified = false;
2103
2104 m_curFocus = 1;
2105
1c4293cb
VZ
2106 wxBoxSizer* topsizer = new wxBoxSizer( wxVERTICAL );
2107
2108 // Message
2109 if ( message.length() )
2110 topsizer->Add( new wxStaticText(this,-1,message),
2111 0, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxALL, spacing );
2112
2113 // String editor
2114 wxBoxSizer* rowsizer = new wxBoxSizer( wxHORIZONTAL );
2115 m_edValue = new wxTextCtrl(this,21,wxEmptyString,
2116 wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER);
2117 wxValidator* validator = GetTextCtrlValidator();
2118 if ( validator )
2119 {
2120 m_edValue->SetValidator( *validator );
2121 delete validator;
2122 }
2123 rowsizer->Add( m_edValue,
2124 1, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxALL, spacing );
2125
2126 // Add button
2127 m_butAdd = new wxButton(this,22,_("Add"));
2128 rowsizer->Add( m_butAdd,
2129 0, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT, spacing );
2130 topsizer->Add( rowsizer, 0, wxEXPAND, spacing );
2131
2132 // Separator line
2133 topsizer->Add( new wxStaticLine(this,-1),
2134 0, wxEXPAND|wxBOTTOM|wxLEFT|wxRIGHT, spacing );
2135
2136 rowsizer = new wxBoxSizer( wxHORIZONTAL );
2137
2138 // list box
2139 m_lbStrings = new wxListBox(this, 24, wxDefaultPosition, wxDefaultSize);
2140 unsigned int i;
2141 for ( i=0; i<ArrayGetCount(); i++ )
2142 m_lbStrings->Append( ArrayGet(i) );
2143 rowsizer->Add( m_lbStrings, 1, wxEXPAND|wxRIGHT, spacing );
2144
2145 // Manipulator buttons
2146 wxBoxSizer* colsizer = new wxBoxSizer( wxVERTICAL );
d3b9f782 2147 m_butCustom = NULL;
1c4293cb
VZ
2148 if ( m_custBtText )
2149 {
2150 m_butCustom = new wxButton(this,28,::wxGetTranslation(m_custBtText));
2151 colsizer->Add( m_butCustom,
2152 0, wxALIGN_CENTER|wxTOP/*wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT*/,
2153 spacing );
2154 }
2155 m_butUpdate = new wxButton(this,27,_("Update"));
2156 colsizer->Add( m_butUpdate,
2157 0, wxALIGN_CENTER|wxTOP, spacing );
2158 m_butRemove = new wxButton(this,23,_("Remove"));
2159 colsizer->Add( m_butRemove,
2160 0, wxALIGN_CENTER|wxTOP, spacing );
2161 m_butUp = new wxButton(this,25,_("Up"));
2162 colsizer->Add( m_butUp,
2163 0, wxALIGN_CENTER|wxTOP, spacing );
2164 m_butDown = new wxButton(this,26,_("Down"));
2165 colsizer->Add( m_butDown,
2166 0, wxALIGN_CENTER|wxTOP, spacing );
2167 rowsizer->Add( colsizer, 0, 0, spacing );
2168
2169 topsizer->Add( rowsizer, 1, wxLEFT|wxRIGHT|wxEXPAND, spacing );
2170
2171 // Separator line
2172 topsizer->Add( new wxStaticLine(this,-1),
2173 0, wxEXPAND|wxTOP|wxLEFT|wxRIGHT, spacing );
2174
29d50f3e
JS
2175 // Standard dialog buttons
2176 wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
2177 buttonSizer->AddButton(new wxButton(this, wxID_OK));
2178 buttonSizer->AddButton(new wxButton(this, wxID_CANCEL));
2179 buttonSizer->Realize();
2180 topsizer->Add( buttonSizer, 0,
2181 wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxALL,
2182 spacing );
1c4293cb
VZ
2183
2184 m_edValue->SetFocus();
2185
2186 SetSizer( topsizer );
2187 topsizer->SetSizeHints( this );
2188
2189#if !wxPG_SMALL_SCREEN
2190 if ( sz.x == wxDefaultSize.x &&
2191 sz.y == wxDefaultSize.y )
2192 SetSize( wxSize(275,360) );
2193 else
2194 SetSize(sz);
2195#endif
2196
2197 return res;
2198}
2199
2200// -----------------------------------------------------------------------
2201
2202void wxArrayEditorDialog::OnAddClick(wxCommandEvent& )
2203{
2204 wxString text = m_edValue->GetValue();
2205 if ( text.length() )
2206 {
2207 if ( ArrayInsert( text, -1 ) )
2208 {
2209 m_lbStrings->Append( text );
2210 m_modified = true;
2211 m_edValue->Clear();
2212 }
2213 }
2214}
2215
2216// -----------------------------------------------------------------------
2217
2218void wxArrayEditorDialog::OnDeleteClick(wxCommandEvent& )
2219{
2220 int index = m_lbStrings->GetSelection();
2221 if ( index >= 0 )
2222 {
2223 ArrayRemoveAt( index );
2224 m_lbStrings->Delete ( index );
2225 m_modified = true;
2226 }
2227}
2228
2229// -----------------------------------------------------------------------
2230
2231void wxArrayEditorDialog::OnUpClick(wxCommandEvent& )
2232{
2233 int index = m_lbStrings->GetSelection();
2234 if ( index > 0 )
2235 {
2236 ArraySwap(index-1,index);
2237 /*wxString old_str = m_array[index-1];
2238 wxString new_str = m_array[index];
2239 m_array[index-1] = new_str;
2240 m_array[index] = old_str;*/
2241 m_lbStrings->SetString ( index-1, ArrayGet(index-1) );
2242 m_lbStrings->SetString ( index, ArrayGet(index) );
2243 m_lbStrings->SetSelection ( index-1 );
2244 m_modified = true;
2245 }
2246}
2247
2248// -----------------------------------------------------------------------
2249
2250void wxArrayEditorDialog::OnDownClick(wxCommandEvent& )
2251{
2252 int index = m_lbStrings->GetSelection();
2253 int lastStringIndex = ((int) m_lbStrings->GetCount()) - 1;
2254 if ( index >= 0 && index < lastStringIndex )
2255 {
2256 ArraySwap(index,index+1);
2257 /*wxString old_str = m_array[index+1];
2258 wxString new_str = m_array[index];
2259 m_array[index+1] = new_str;
2260 m_array[index] = old_str;*/
2261 m_lbStrings->SetString ( index+1, ArrayGet(index+1) );
2262 m_lbStrings->SetString ( index, ArrayGet(index) );
2263 m_lbStrings->SetSelection ( index+1 );
2264 m_modified = true;
2265 }
2266}
2267
2268// -----------------------------------------------------------------------
2269
2270void wxArrayEditorDialog::OnUpdateClick(wxCommandEvent& )
2271{
2272 int index = m_lbStrings->GetSelection();
2273 if ( index >= 0 )
2274 {
2275 wxString str = m_edValue->GetValue();
2276 if ( ArraySet(index,str) )
2277 {
2278 m_lbStrings->SetString ( index, str );
2279 //m_array[index] = str;
2280 m_modified = true;
2281 }
2282 }
2283}
2284
2285// -----------------------------------------------------------------------
2286
2287void wxArrayEditorDialog::OnListBoxClick(wxCommandEvent& )
2288{
2289 int index = m_lbStrings->GetSelection();
2290 if ( index >= 0 )
2291 {
2292 m_edValue->SetValue( m_lbStrings->GetString(index) );
2293 }
2294}
2295
2296// -----------------------------------------------------------------------
2297// wxPGArrayStringEditorDialog
2298// -----------------------------------------------------------------------
2299
2300IMPLEMENT_DYNAMIC_CLASS(wxPGArrayStringEditorDialog, wxArrayEditorDialog)
2301
2302BEGIN_EVENT_TABLE(wxPGArrayStringEditorDialog, wxArrayEditorDialog)
2303 EVT_BUTTON(28, wxPGArrayStringEditorDialog::OnCustomEditClick)
2304END_EVENT_TABLE()
2305
2306// -----------------------------------------------------------------------
2307
2308wxString wxPGArrayStringEditorDialog::ArrayGet( size_t index )
2309{
2310 return m_array[index];
2311}
2312
2313size_t wxPGArrayStringEditorDialog::ArrayGetCount()
2314{
2315 return m_array.size();
2316}
2317
2318bool wxPGArrayStringEditorDialog::ArrayInsert( const wxString& str, int index )
2319{
2320 if (index<0)
2321 m_array.Add(str);
2322 else
2323 m_array.Insert(str,index);
2324 return true;
2325}
2326
2327bool wxPGArrayStringEditorDialog::ArraySet( size_t index, const wxString& str )
2328{
2329 m_array[index] = str;
2330 return true;
2331}
2332
2333void wxPGArrayStringEditorDialog::ArrayRemoveAt( int index )
2334{
2335 m_array.RemoveAt(index);
2336}
2337
2338void wxPGArrayStringEditorDialog::ArraySwap( size_t first, size_t second )
2339{
2340 wxString old_str = m_array[first];
2341 wxString new_str = m_array[second];
2342 m_array[first] = new_str;
2343 m_array[second] = old_str;
2344}
2345
2346wxPGArrayStringEditorDialog::wxPGArrayStringEditorDialog()
2347 : wxArrayEditorDialog()
2348{
2349 Init();
2350}
2351
2352void wxPGArrayStringEditorDialog::Init()
2353{
d3b9f782 2354 m_pCallingClass = NULL;
1c4293cb
VZ
2355}
2356
2357void wxPGArrayStringEditorDialog::OnCustomEditClick(wxCommandEvent& )
2358{
2359 wxASSERT( m_pCallingClass );
2360 wxString str = m_edValue->GetValue();
2361 if ( m_pCallingClass->OnCustomStringEdit(m_parent,str) )
2362 {
2363 //m_edValue->SetValue ( str );
2364 m_lbStrings->Append ( str );
2365 m_array.Add ( str );
2366 m_modified = true;
2367 }
2368}
2369
2370// -----------------------------------------------------------------------
2371// wxArrayStringProperty
2372// -----------------------------------------------------------------------
2373
2374WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayStringProperty, // Property name
2375 wxPGProperty, // Property we inherit from
2376 wxArrayString, // Value type name
2377 const wxArrayString&, // Value type, as given in constructor
2378 TextCtrlAndButton) // Initial editor
2379
2380wxArrayStringProperty::wxArrayStringProperty( const wxString& label,
2381 const wxString& name,
2382 const wxArrayString& array )
2383 : wxPGProperty(label,name)
2384{
2385 SetValue( array );
2386}
2387
2388wxArrayStringProperty::~wxArrayStringProperty() { }
2389
2390void wxArrayStringProperty::OnSetValue()
2391{
2392 GenerateValueAsString();
2393}
2394
1425eca5
JS
2395#define ARRSTRPROP_ARRAY_TO_STRING(STRING,ARRAY) \
2396 wxPropertyGrid::ArrayStringToString(STRING,ARRAY,wxS('"'),wxS('"'),1)
2397
2398wxString wxArrayStringProperty::ValueToString( wxVariant& WXUNUSED(value),
2399 int argFlags ) const
1c4293cb 2400{
1425eca5
JS
2401 //
2402 // If this is called from GetValueAsString(), return cached string
2403 if ( argFlags & wxPG_VALUE_IS_CURRENT )
2404 {
2405 return m_display;
2406 }
2407
2408 wxArrayString arr = m_value.GetArrayString();
2409 wxString s;
2410 ARRSTRPROP_ARRAY_TO_STRING(s, arr);
2411 return s;
1c4293cb
VZ
2412}
2413
2414// Converts wxArrayString to a string separated by delimeters and spaces.
2415// preDelim is useful for "str1" "str2" style. Set flags to 1 to do slash
2416// conversion.
2417void wxPropertyGrid::ArrayStringToString( wxString& dst, const wxArrayString& src,
2418 wxChar preDelim, wxChar postDelim,
2419 int flags )
2420{
2421 wxString pdr;
2422
2423 unsigned int i;
2424 unsigned int itemCount = src.size();
2425
b7bc9d80 2426 wxChar preas[2] = { 0, 0 };
1c4293cb
VZ
2427
2428 dst.Empty();
2429
b7bc9d80 2430 if ( flags & 1 )
1c4293cb
VZ
2431 {
2432 preas[0] = preDelim;
1c4293cb
VZ
2433 pdr = wxS("\\");
2434 pdr += preDelim;
2435 }
2436
2437 if ( itemCount )
2438 dst.append( preas );
2439
2440 wxASSERT( postDelim );
2441 wxString postDelimStr(postDelim);
2442 //wxString preDelimStr(preDelim);
2443
2444 for ( i = 0; i < itemCount; i++ )
2445 {
2446 wxString str( src.Item(i) );
2447
2448 // Do some character conversion.
2449 // Convertes \ to \\ and <preDelim> to \<preDelim>
2450 // Useful when preDelim and postDelim are "\"".
2451 if ( flags & 1 )
2452 {
2453 str.Replace( wxS("\\"), wxS("\\\\"), true );
2454 if ( pdr.length() )
2455 str.Replace( preas, pdr, true );
2456 }
2457
2458 dst.append( str );
2459
2460 if ( i < (itemCount-1) )
2461 {
2462 dst.append( postDelimStr );
2463 dst.append( wxS(" ") );
2464 dst.append( preas );
2465 }
2466 else if ( preDelim )
2467 dst.append( postDelimStr );
2468 }
2469}
2470
1c4293cb
VZ
2471void wxArrayStringProperty::GenerateValueAsString()
2472{
2473 wxArrayString arr = m_value.GetArrayString();
1425eca5 2474 ARRSTRPROP_ARRAY_TO_STRING(m_display, arr);
1c4293cb
VZ
2475}
2476
2477// Default implementation doesn't do anything.
2478bool wxArrayStringProperty::OnCustomStringEdit( wxWindow*, wxString& )
2479{
2480 return false;
2481}
2482
2483wxArrayEditorDialog* wxArrayStringProperty::CreateEditorDialog()
2484{
2485 return new wxPGArrayStringEditorDialog();
2486}
2487
2488bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid* propGrid,
2489 wxWindow* WXUNUSED(primaryCtrl),
2490 const wxChar* cbt )
2491{
2492 // Update the value
703ee9f5 2493 wxVariant useValue = propGrid->GetUncommittedPropertyValue();
1c4293cb
VZ
2494
2495 if ( !propGrid->EditorValidate() )
2496 return false;
2497
2498 // Create editor dialog.
2499 wxArrayEditorDialog* dlg = CreateEditorDialog();
2500#if wxUSE_VALIDATORS
2501 wxValidator* validator = GetValidator();
2502 wxPGInDialogValidator dialogValidator;
2503#endif
2504
2505 wxPGArrayStringEditorDialog* strEdDlg = wxDynamicCast(dlg, wxPGArrayStringEditorDialog);
2506
2507 if ( strEdDlg )
2508 strEdDlg->SetCustomButton(cbt, this);
2509
9b5bafcf 2510 dlg->SetDialogValue( useValue );
1c4293cb
VZ
2511 dlg->Create(propGrid, wxEmptyString, m_label);
2512
2513#if !wxPG_SMALL_SCREEN
2514 dlg->Move( propGrid->GetGoodEditorDialogPosition(this,dlg->GetSize()) );
2515#endif
2516
2517 bool retVal;
2518
2519 for (;;)
2520 {
2521 retVal = false;
2522
2523 int res = dlg->ShowModal();
2524
2525 if ( res == wxID_OK && dlg->IsModified() )
2526 {
2527 wxVariant value = dlg->GetDialogValue();
2528 if ( !value.IsNull() )
2529 {
2530 wxArrayString actualValue = value.GetArrayString();
2531 wxString tempStr;
1425eca5 2532 ARRSTRPROP_ARRAY_TO_STRING(tempStr, actualValue);
1c4293cb
VZ
2533 #if wxUSE_VALIDATORS
2534 if ( dialogValidator.DoValidate( propGrid, validator, tempStr ) )
2535 #endif
2536 {
2537 SetValueInEvent( actualValue );
2538 retVal = true;
2539 break;
2540 }
2541 }
2542 else
2543 break;
2544 }
2545 else
2546 break;
2547 }
2548
2549 delete dlg;
2550
2551 return retVal;
2552}
2553
2554bool wxArrayStringProperty::OnEvent( wxPropertyGrid* propGrid,
2555 wxWindow* primary,
2556 wxEvent& event )
2557{
2558 if ( propGrid->IsMainButtonEvent(event) )
2559 return OnButtonClick(propGrid,primary,(const wxChar*) NULL);
2560 return false;
2561}
2562
2563bool wxArrayStringProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const
2564{
2565 wxArrayString arr;
2566
2567 WX_PG_TOKENIZER2_BEGIN(text,wxS('"'))
2568
2569 // Need to replace backslashes with empty characters
2570 // (opposite what is done in GenerateValueString).
2571 token.Replace ( wxS("\\"), wxEmptyString, true );
2572
2573 arr.Add( token );
2574
2575 WX_PG_TOKENIZER2_END()
2576
2577 variant = arr;
2578
2579 return true;
2580}
2581
2582// -----------------------------------------------------------------------
2583// wxPGInDialogValidator
2584// -----------------------------------------------------------------------
2585
2586#if wxUSE_VALIDATORS
2587bool wxPGInDialogValidator::DoValidate( wxPropertyGrid* propGrid,
2588 wxValidator* validator,
2589 const wxString& value )
2590{
2591 if ( !validator )
2592 return true;
2593
2594 wxTextCtrl* tc = m_textCtrl;
2595
2596 if ( !tc )
2597 {
2598 {
2599 tc = new wxTextCtrl( propGrid, wxPG_SUBID_TEMP1, wxEmptyString,
2600 wxPoint(30000,30000));
2601 tc->Hide();
2602 }
2603
2604 m_textCtrl = tc;
2605 }
2606
2607 tc->SetValue(value);
2608
2609 validator->SetWindow(tc);
2610 bool res = validator->Validate(propGrid);
2611
2612 return res;
2613}
2614#else
2615bool wxPGInDialogValidator::DoValidate( wxPropertyGrid* WXUNUSED(propGrid),
2616 wxValidator* WXUNUSED(validator),
2617 const wxString& WXUNUSED(value) )
2618{
2619 return true;
2620}
2621#endif
2622
2623// -----------------------------------------------------------------------
2624
f4bc1aa2 2625#endif // wxUSE_PROPGRID