1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/props.cpp
3 // Purpose: Basic Property Classes
4 // Author: Jaakko Salli
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/object.h"
25 #include "wx/string.h"
28 #include "wx/window.h"
31 #include "wx/dcclient.h"
32 #include "wx/dcmemory.h"
33 #include "wx/button.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"
47 #include "wx/textdlg.h"
48 #include "wx/filedlg.h"
52 #include "wx/filename.h"
54 #include "wx/propgrid/propgrid.h"
56 #define wxPG_CUSTOM_IMAGE_WIDTH 20 // for wxColourProperty etc.
59 // -----------------------------------------------------------------------
61 // -----------------------------------------------------------------------
63 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxStringProperty
,wxPGProperty
,
64 wxString
,const wxString
&,TextCtrl
)
66 wxStringProperty::wxStringProperty( const wxString
& label
,
68 const wxString
& value
)
69 : wxPGProperty(label
,name
)
74 void wxStringProperty::OnSetValue()
76 if ( !m_value
.IsNull() && m_value
.GetString() == wxS("<composed>") )
77 SetFlag(wxPG_PROP_COMPOSED_VALUE
);
79 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE
) )
82 DoGenerateComposedValue(s
);
87 wxStringProperty::~wxStringProperty() { }
89 wxString
wxStringProperty::ValueToString( wxVariant
& value
,
92 wxString s
= value
.GetString();
94 if ( GetChildCount() && HasFlag(wxPG_PROP_COMPOSED_VALUE
) )
96 // Value stored in m_value is non-editable, non-full value
97 if ( (argFlags
& wxPG_FULL_VALUE
) || (argFlags
& wxPG_EDITABLE_VALUE
) )
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." );
104 DoGenerateComposedValue(s
, argFlags
);
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());
118 bool wxStringProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
120 if ( GetChildCount() && HasFlag(wxPG_PROP_COMPOSED_VALUE
) )
121 return wxPGProperty::StringToValue(variant
, text
, argFlags
);
123 if ( variant
!= text
)
132 bool wxStringProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
134 if ( name
== wxPG_STRING_PASSWORD
)
136 m_flags
&= ~(wxPG_PROP_PASSWORD
);
137 if ( value
.GetLong() ) m_flags
|= wxPG_PROP_PASSWORD
;
144 // -----------------------------------------------------------------------
146 // -----------------------------------------------------------------------
148 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxIntProperty
,wxPGProperty
,
151 wxIntProperty::wxIntProperty( const wxString
& label
, const wxString
& name
,
152 long value
) : wxPGProperty(label
,name
)
157 wxIntProperty::wxIntProperty( const wxString
& label
, const wxString
& name
,
158 const wxLongLong
& value
) : wxPGProperty(label
,name
)
160 SetValue(WXVARIANT(value
));
163 wxIntProperty::~wxIntProperty() { }
165 wxString
wxIntProperty::ValueToString( wxVariant
& value
,
166 int WXUNUSED(argFlags
) ) const
168 if ( value
.GetType() == wxPG_VARIANT_TYPE_LONG
)
170 return wxString::Format(wxS("%li"),value
.GetLong());
172 else if ( value
.GetType() == wxPG_VARIANT_TYPE_LONGLONG
)
174 wxLongLong ll
= value
.GetLongLong();
175 return ll
.ToString();
178 return wxEmptyString
;
181 bool wxIntProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
186 if ( text
.length() == 0 )
192 // We know it is a number, but let's still check
194 if ( text
.IsNumber() )
196 // Remove leading zeroes, so that the number is not interpreted as octal
197 wxString::const_iterator i
= text
.begin();
198 wxString::const_iterator iMax
= text
.end() - 1; // Let's allow one, last zero though
200 int firstNonZeroPos
= 0;
202 for ( ; i
!= iMax
; ++i
)
205 if ( c
!= wxS('0') && c
!= wxS(' ') )
210 wxString useText
= text
.substr(firstNonZeroPos
, text
.length() - firstNonZeroPos
);
212 wxString variantType
= variant
.GetType();
213 bool isPrevLong
= variantType
== wxPG_VARIANT_TYPE_LONG
;
215 wxLongLong_t value64
= 0;
217 if ( useText
.ToLongLong(&value64
, 10) &&
218 ( value64
>= INT_MAX
|| value64
<= INT_MIN
)
221 bool doChangeValue
= isPrevLong
;
223 if ( !isPrevLong
&& variantType
== wxPG_VARIANT_TYPE_LONGLONG
)
225 wxLongLong oldValue
= variant
.GetLongLong();
226 if ( oldValue
.GetValue() != value64
)
227 doChangeValue
= true;
232 wxLongLong
ll(value64
);
238 if ( useText
.ToLong( &value32
, 0 ) )
240 if ( !isPrevLong
|| variant
!= value32
)
247 else if ( argFlags
& wxPG_REPORT_ERROR
)
253 bool wxIntProperty::IntToValue( wxVariant
& variant
, int value
, int WXUNUSED(argFlags
) ) const
255 if ( variant
.GetType() != wxPG_VARIANT_TYPE_LONG
|| variant
!= (long)value
)
257 variant
= (long)value
;
263 bool wxIntProperty::DoValidation( const wxPGProperty
* property
, wxLongLong_t
& value
, wxPGValidationInfo
* pValidationInfo
, int mode
)
266 wxLongLong_t min
= wxINT64_MIN
;
267 wxLongLong_t max
= wxINT64_MAX
;
272 variant
= property
->GetAttribute(wxPGGlobalVars
->m_strMin
);
273 if ( !variant
.IsNull() )
275 min
= variant
.GetLongLong().GetValue();
279 variant
= property
->GetAttribute(wxPGGlobalVars
->m_strMax
);
280 if ( !variant
.IsNull() )
282 max
= variant
.GetLongLong().GetValue();
290 if ( mode
== wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
)
291 pValidationInfo
->SetFailureMessage(
292 wxString::Format(_("Value must be %lld or higher"),min
)
294 else if ( mode
== wxPG_PROPERTY_VALIDATION_SATURATE
)
297 value
= max
- (min
- value
);
306 if ( mode
== wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
)
307 pValidationInfo
->SetFailureMessage(
308 wxString::Format(_("Value must be %lld or higher"),min
)
310 else if ( mode
== wxPG_PROPERTY_VALIDATION_SATURATE
)
313 value
= min
+ (value
- max
);
320 bool wxIntProperty::ValidateValue( wxVariant
& value
,
321 wxPGValidationInfo
& validationInfo
) const
323 wxLongLong_t ll
= value
.GetLongLong().GetValue();
324 return DoValidation(this, ll
, &validationInfo
,
325 wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
);
328 wxValidator
* wxIntProperty::GetClassValidator()
331 WX_PG_DOGETVALIDATOR_ENTRY()
333 // Atleast wxPython 2.6.2.1 required that the string argument is given
335 wxTextValidator
* validator
= new wxTextValidator(wxFILTER_NUMERIC
,&v
);
337 WX_PG_DOGETVALIDATOR_EXIT(validator
)
343 wxValidator
* wxIntProperty::DoGetValidator() const
345 return GetClassValidator();
348 // -----------------------------------------------------------------------
350 // -----------------------------------------------------------------------
353 #define wxPG_UINT_TEMPLATE_MAX 8
355 static const wxChar
* const gs_uintTemplates32
[wxPG_UINT_TEMPLATE_MAX
] = {
356 wxT("%x"),wxT("0x%x"),wxT("$%x"),
357 wxT("%X"),wxT("0x%X"),wxT("$%X"),
361 static const char* const gs_uintTemplates64
[wxPG_UINT_TEMPLATE_MAX
] = {
362 "%" wxLongLongFmtSpec
"x",
363 "0x%" wxLongLongFmtSpec
"x",
364 "$%" wxLongLongFmtSpec
"x",
365 "%" wxLongLongFmtSpec
"X",
366 "0x%" wxLongLongFmtSpec
"X",
367 "$%" wxLongLongFmtSpec
"X",
368 "%" wxLongLongFmtSpec
"u",
369 "%" wxLongLongFmtSpec
"o"
372 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxUIntProperty
,wxPGProperty
,
373 long,unsigned long,TextCtrl
)
375 void wxUIntProperty::Init()
377 m_base
= 6; // This is magic number for dec base (must be same as in setattribute)
379 m_prefix
= wxPG_PREFIX_NONE
;
382 wxUIntProperty::wxUIntProperty( const wxString
& label
, const wxString
& name
,
383 unsigned long value
) : wxPGProperty(label
,name
)
386 SetValue((long)value
);
389 wxUIntProperty::wxUIntProperty( const wxString
& label
, const wxString
& name
,
390 const wxULongLong
& value
) : wxPGProperty(label
,name
)
393 SetValue(WXVARIANT(value
));
396 wxUIntProperty::~wxUIntProperty() { }
398 wxString
wxUIntProperty::ValueToString( wxVariant
& value
,
399 int WXUNUSED(argFlags
) ) const
401 size_t index
= m_base
+ m_prefix
;
402 if ( index
>= wxPG_UINT_TEMPLATE_MAX
)
403 index
= wxPG_BASE_DEC
;
405 if ( value
.GetType() == wxPG_VARIANT_TYPE_LONG
)
407 return wxString::Format(gs_uintTemplates32
[index
],
408 (unsigned long)value
.GetLong());
411 wxULongLong ull
= value
.GetULongLong();
413 return wxString::Format(gs_uintTemplates64
[index
], ull
.GetValue());
416 bool wxUIntProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int WXUNUSED(argFlags
) ) const
418 wxString variantType
= variant
.GetType();
419 bool isPrevLong
= variantType
== wxPG_VARIANT_TYPE_LONG
;
421 if ( text
.length() == 0 )
428 if ( text
[0] == wxS('$') )
431 wxULongLong_t value64
= 0;
432 wxString s
= text
.substr(start
, text
.length() - start
);
434 if ( s
.ToULongLong(&value64
, (unsigned int)m_realBase
) )
436 if ( value64
>= LONG_MAX
)
438 bool doChangeValue
= isPrevLong
;
440 if ( !isPrevLong
&& variantType
== wxPG_VARIANT_TYPE_ULONGLONG
)
442 wxULongLong oldValue
= variant
.GetULongLong();
443 if ( oldValue
.GetValue() != value64
)
444 doChangeValue
= true;
449 variant
= wxULongLong(value64
);
455 unsigned long value32
= wxLongLong(value64
).GetLo();
456 if ( !isPrevLong
|| m_value
!= (long)value32
)
458 variant
= (long)value32
;
467 bool wxUIntProperty::IntToValue( wxVariant
& variant
, int number
, int WXUNUSED(argFlags
) ) const
469 if ( variant
!= (long)number
)
471 variant
= (long)number
;
477 bool wxUIntProperty::ValidateValue( wxVariant
& value
, wxPGValidationInfo
& validationInfo
) const
480 wxULongLong_t ll
= value
.GetULongLong().GetValue();
482 wxULongLong_t min
= 0;
483 wxULongLong_t max
= wxUINT64_MAX
;
486 variant
= GetAttribute(wxPGGlobalVars
->m_strMin
);
487 if ( !variant
.IsNull() )
489 min
= variant
.GetULongLong().GetValue();
492 validationInfo
.SetFailureMessage(
493 wxString::Format(_("Value must be %llu or higher"),min
)
498 variant
= GetAttribute(wxPGGlobalVars
->m_strMax
);
499 if ( !variant
.IsNull() )
501 max
= variant
.GetULongLong().GetValue();
504 validationInfo
.SetFailureMessage(
505 wxString::Format(_("Value must be %llu or less"),max
)
514 bool wxUIntProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
516 if ( name
== wxPG_UINT_BASE
)
518 int val
= value
.GetLong();
520 m_realBase
= (wxByte
) val
;
521 if ( m_realBase
> 16 )
525 // Translate logical base to a template array index
527 if ( val
== wxPG_BASE_HEX
)
529 else if ( val
== wxPG_BASE_DEC
)
531 else if ( val
== wxPG_BASE_HEXL
)
535 else if ( name
== wxPG_UINT_PREFIX
)
537 m_prefix
= (wxByte
) value
.GetLong();
543 // -----------------------------------------------------------------------
545 // -----------------------------------------------------------------------
547 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFloatProperty
,wxPGProperty
,
548 double,double,TextCtrl
)
550 wxFloatProperty::wxFloatProperty( const wxString
& label
,
551 const wxString
& name
,
553 : wxPGProperty(label
,name
)
559 wxFloatProperty::~wxFloatProperty() { }
561 // This helper method provides standard way for floating point-using
562 // properties to convert values to string.
563 void wxPropertyGrid::DoubleToString(wxString
& target
,
567 wxString
* precTemplate
)
569 if ( precision
>= 0 )
573 precTemplate
= &text1
;
575 if ( !precTemplate
->length() )
577 *precTemplate
= wxS("%.");
578 *precTemplate
<< wxString::Format( wxS("%i"), precision
);
579 *precTemplate
<< wxS('f');
582 target
.Printf( precTemplate
->c_str(), value
);
586 target
.Printf( wxS("%f"), value
);
589 if ( removeZeroes
&& precision
!= 0 && target
.length() )
591 // Remove excess zeroes (do not remove this code just yet,
592 // since sprintf can't do the same consistently across platforms).
593 wxString::const_iterator i
= target
.end() - 1;
594 size_t new_len
= target
.length() - 1;
596 for ( ; i
!= target
.begin(); --i
)
598 if ( *i
!= wxS('0') )
603 wxChar cur_char
= *i
;
604 if ( cur_char
!= wxS('.') && cur_char
!= wxS(',') )
607 if ( new_len
!= target
.length() )
608 target
.resize(new_len
);
612 wxString
wxFloatProperty::ValueToString( wxVariant
& value
,
616 if ( !value
.IsNull() )
618 wxPropertyGrid::DoubleToString(text
,
621 !(argFlags
& wxPG_FULL_VALUE
),
627 bool wxFloatProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
632 if ( text
.length() == 0 )
638 bool res
= text
.ToDouble(&value
);
641 if ( variant
!= value
)
647 else if ( argFlags
& wxPG_REPORT_ERROR
)
653 bool wxFloatProperty::DoValidation( const wxPGProperty
* property
,
655 wxPGValidationInfo
* pValidationInfo
,
659 double min
= (double)wxINT64_MIN
;
660 double max
= (double)wxINT64_MAX
;
665 variant
= property
->GetAttribute(wxPGGlobalVars
->m_strMin
);
666 if ( !variant
.IsNull() )
668 min
= variant
.GetDouble();
672 variant
= property
->GetAttribute(wxPGGlobalVars
->m_strMax
);
673 if ( !variant
.IsNull() )
675 max
= variant
.GetDouble();
683 if ( mode
== wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
)
684 pValidationInfo
->SetFailureMessage(
685 wxString::Format(_("Value must be %f or higher"),min
)
687 else if ( mode
== wxPG_PROPERTY_VALIDATION_SATURATE
)
690 value
= max
- (min
- value
);
697 max
= variant
.GetDouble();
700 if ( mode
== wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
)
701 pValidationInfo
->SetFailureMessage(
702 wxString::Format(_("Value must be %f or less"),max
)
704 else if ( mode
== wxPG_PROPERTY_VALIDATION_SATURATE
)
707 value
= min
+ (value
- max
);
715 wxFloatProperty::ValidateValue( wxVariant
& value
,
716 wxPGValidationInfo
& validationInfo
) const
718 double fpv
= value
.GetDouble();
719 return DoValidation(this, fpv
, &validationInfo
,
720 wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
);
723 bool wxFloatProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
725 if ( name
== wxPG_FLOAT_PRECISION
)
727 m_precision
= value
.GetLong();
733 wxValidator
* wxFloatProperty::DoGetValidator() const
735 return wxIntProperty::GetClassValidator();
738 // -----------------------------------------------------------------------
740 // -----------------------------------------------------------------------
742 // We cannot use standard WX_PG_IMPLEMENT_PROPERTY_CLASS macro, since
743 // there is a custom GetEditorClass.
745 IMPLEMENT_DYNAMIC_CLASS(wxBoolProperty
, wxPGProperty
)
747 const wxPGEditor
* wxBoolProperty::DoGetEditorClass() const
749 // Select correct editor control.
750 #if wxPG_INCLUDE_CHECKBOX
751 if ( !(m_flags
& wxPG_PROP_USE_CHECKBOX
) )
752 return wxPGEditor_Choice
;
753 return wxPGEditor_CheckBox
;
755 return wxPGEditor_Choice
;
759 wxBoolProperty::wxBoolProperty( const wxString
& label
, const wxString
& name
, bool value
) :
760 wxPGProperty(label
,name
)
762 m_choices
.Assign(wxPGGlobalVars
->m_boolChoices
);
764 SetValue(wxPGVariant_Bool(value
));
766 m_flags
|= wxPG_PROP_USE_DCC
;
769 wxBoolProperty::~wxBoolProperty() { }
771 wxString
wxBoolProperty::ValueToString( wxVariant
& value
,
774 bool boolValue
= value
.GetBool();
776 // As a fragment of composite string value,
777 // make it a little more readable.
778 if ( argFlags
& wxPG_COMPOSITE_FRAGMENT
)
786 if ( argFlags
& wxPG_UNEDITABLE_COMPOSITE_FRAGMENT
)
787 return wxEmptyString
;
790 if ( wxPGGlobalVars
->m_autoGetTranslation
)
791 notFmt
= _("Not %s");
793 notFmt
= wxS("Not %s");
795 return wxString::Format(notFmt
.c_str(), m_label
.c_str());
799 if ( !(argFlags
& wxPG_FULL_VALUE
) )
801 return wxPGGlobalVars
->m_boolChoices
[boolValue
?1:0].GetText();
806 if ( boolValue
) text
= wxS("true");
807 else text
= wxS("false");
812 bool wxBoolProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int WXUNUSED(argFlags
) ) const
814 bool boolValue
= false;
815 if ( text
.CmpNoCase(wxPGGlobalVars
->m_boolChoices
[1].GetText()) == 0 ||
816 text
.CmpNoCase(wxS("true")) == 0 ||
817 text
.CmpNoCase(m_label
) == 0 )
820 if ( text
.length() == 0 )
826 if ( variant
!= boolValue
)
828 variant
= wxPGVariant_Bool(boolValue
);
834 bool wxBoolProperty::IntToValue( wxVariant
& variant
, int value
, int ) const
836 bool boolValue
= value
? true : false;
838 if ( variant
!= boolValue
)
840 variant
= wxPGVariant_Bool(boolValue
);
846 bool wxBoolProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
848 #if wxPG_INCLUDE_CHECKBOX
849 if ( name
== wxPG_BOOL_USE_CHECKBOX
)
851 if ( value
.GetLong() )
852 m_flags
|= wxPG_PROP_USE_CHECKBOX
;
854 m_flags
&= ~(wxPG_PROP_USE_CHECKBOX
);
858 if ( name
== wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING
)
860 if ( value
.GetLong() )
861 m_flags
|= wxPG_PROP_USE_DCC
;
863 m_flags
&= ~(wxPG_PROP_USE_DCC
);
869 // -----------------------------------------------------------------------
871 // -----------------------------------------------------------------------
873 IMPLEMENT_DYNAMIC_CLASS(wxEnumProperty
, wxPGProperty
)
875 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEnumProperty
,long,Choice
)
877 wxEnumProperty::wxEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
* const* labels
,
878 const long* values
, int value
) : wxPGProperty(label
,name
)
884 m_choices
.Add(labels
,values
);
886 if ( GetItemCount() )
887 SetValue( (long)value
);
891 wxEnumProperty::wxEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
* const* labels
,
892 const long* values
, wxPGChoices
* choicesCache
, int value
)
893 : wxPGProperty(label
,name
)
897 wxASSERT( choicesCache
);
899 if ( choicesCache
->IsOk() )
901 m_choices
.Assign( *choicesCache
);
902 m_value
= wxPGVariant_Zero
;
906 m_choices
.Add(labels
,values
);
908 if ( GetItemCount() )
909 SetValue( (long)value
);
913 wxEnumProperty::wxEnumProperty( const wxString
& label
, const wxString
& name
,
914 const wxArrayString
& labels
, const wxArrayInt
& values
, int value
)
915 : wxPGProperty(label
,name
)
919 if ( &labels
&& labels
.size() )
921 m_choices
.Set(labels
, values
);
923 if ( GetItemCount() )
924 SetValue( (long)value
);
928 wxEnumProperty::wxEnumProperty( const wxString
& label
, const wxString
& name
,
929 wxPGChoices
& choices
, int value
)
930 : wxPGProperty(label
,name
)
932 m_choices
.Assign( choices
);
934 if ( GetItemCount() )
935 SetValue( (long)value
);
938 int wxEnumProperty::GetIndexForValue( int value
) const
940 if ( !m_choices
.IsOk() )
943 int intVal
= m_choices
.Index(value
);
950 wxEnumProperty::~wxEnumProperty ()
954 int wxEnumProperty::ms_nextIndex
= -2;
956 void wxEnumProperty::OnSetValue()
958 wxString variantType
= m_value
.GetType();
960 if ( variantType
== wxPG_VARIANT_TYPE_LONG
)
962 ValueFromInt_( m_value
, m_value
.GetLong(), wxPG_FULL_VALUE
);
964 else if ( variantType
== wxPG_VARIANT_TYPE_STRING
)
966 ValueFromString_( m_value
, m_value
.GetString(), 0 );
973 if ( ms_nextIndex
!= -2 )
975 m_index
= ms_nextIndex
;
980 bool wxEnumProperty::ValidateValue( wxVariant
& value
, wxPGValidationInfo
& WXUNUSED(validationInfo
) ) const
982 // Make sure string value is in the list,
983 // unless property has string as preferred value type
984 // To reduce code size, use conversion here as well
985 if ( value
.GetType() == wxPG_VARIANT_TYPE_STRING
&&
986 !this->IsKindOf(CLASSINFO(wxEditEnumProperty
)) )
987 return ValueFromString_( value
, value
.GetString(), wxPG_PROPERTY_SPECIFIC
);
992 wxString
wxEnumProperty::ValueToString( wxVariant
& value
,
993 int WXUNUSED(argFlags
) ) const
995 if ( value
.GetType() == wxPG_VARIANT_TYPE_STRING
)
996 return value
.GetString();
998 int index
= m_choices
.Index(value
.GetLong());
1000 return wxEmptyString
;
1002 return m_choices
.GetLabel(index
);
1005 bool wxEnumProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
1007 return ValueFromString_( variant
, text
, argFlags
);
1010 bool wxEnumProperty::IntToValue( wxVariant
& variant
, int intVal
, int argFlags
) const
1012 return ValueFromInt_( variant
, intVal
, argFlags
);
1015 bool wxEnumProperty::ValueFromString_( wxVariant
& value
, const wxString
& text
, int argFlags
) const
1020 for ( unsigned int i
=0; i
<m_choices
.GetCount(); i
++ )
1022 const wxString
& entryLabel
= m_choices
.GetLabel(i
);
1023 if ( text
.CmpNoCase(entryLabel
) == 0 )
1026 useValue
= m_choices
.GetValue(i
);
1031 bool asText
= false;
1033 bool isEdit
= this->IsKindOf(CLASSINFO(wxEditEnumProperty
));
1035 // If text not any of the choices, store as text instead
1036 // (but only if we are wxEditEnumProperty)
1037 if ( useIndex
== -1 && isEdit
)
1042 int setAsNextIndex
= -2;
1046 setAsNextIndex
= -1;
1049 else if ( useIndex
!= GetIndex() )
1051 if ( useIndex
!= -1 )
1053 setAsNextIndex
= useIndex
;
1054 value
= (long)useValue
;
1058 setAsNextIndex
= -1;
1059 value
= wxPGVariant_MinusOne
;
1063 if ( setAsNextIndex
!= -2 )
1065 // If wxPG_PROPERTY_SPECIFIC is set, then this is done for
1066 // validation purposes only, and index must not be changed
1067 if ( !(argFlags
& wxPG_PROPERTY_SPECIFIC
) )
1068 ms_nextIndex
= setAsNextIndex
;
1070 if ( isEdit
|| setAsNextIndex
!= -1 )
1078 bool wxEnumProperty::ValueFromInt_( wxVariant
& variant
, int intVal
, int argFlags
) const
1080 // If wxPG_FULL_VALUE is *not* in argFlags, then intVal is index from combo box.
1084 if ( argFlags
& wxPG_FULL_VALUE
)
1086 ms_nextIndex
= GetIndexForValue( intVal
);
1090 if ( intVal
!= GetIndex() )
1092 ms_nextIndex
= intVal
;
1096 if ( ms_nextIndex
!= -2 )
1098 if ( !(argFlags
& wxPG_FULL_VALUE
) )
1099 intVal
= m_choices
.GetValue(intVal
);
1101 variant
= (long)intVal
;
1110 wxEnumProperty::OnValidationFailure( wxVariant
& WXUNUSED(pendingValue
) )
1116 void wxEnumProperty::SetIndex( int index
)
1122 int wxEnumProperty::GetIndex() const
1124 if ( m_value
.IsNull() )
1127 if ( ms_nextIndex
!= -2 )
1128 return ms_nextIndex
;
1133 // -----------------------------------------------------------------------
1134 // wxEditEnumProperty
1135 // -----------------------------------------------------------------------
1137 IMPLEMENT_DYNAMIC_CLASS(wxEditEnumProperty
, wxPGProperty
)
1139 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEditEnumProperty
,wxString
,ComboBox
)
1141 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
* const* labels
,
1142 const long* values
, const wxString
& value
)
1143 : wxEnumProperty(label
,name
,labels
,values
,0)
1148 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
* const* labels
,
1149 const long* values
, wxPGChoices
* choicesCache
, const wxString
& value
)
1150 : wxEnumProperty(label
,name
,labels
,values
,choicesCache
,0)
1155 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
,
1156 const wxArrayString
& labels
, const wxArrayInt
& values
, const wxString
& value
)
1157 : wxEnumProperty(label
,name
,labels
,values
,0)
1162 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
,
1163 wxPGChoices
& choices
, const wxString
& value
)
1164 : wxEnumProperty(label
,name
,choices
,0)
1169 wxEditEnumProperty::~wxEditEnumProperty()
1173 // -----------------------------------------------------------------------
1175 // -----------------------------------------------------------------------
1177 IMPLEMENT_DYNAMIC_CLASS(wxFlagsProperty
,wxPGProperty
)
1179 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxFlagsProperty
,long,TextCtrl
)
1181 void wxFlagsProperty::Init()
1183 long value
= m_value
;
1186 // Generate children
1190 unsigned int prevChildCount
= m_children
.size();
1193 if ( prevChildCount
)
1195 wxPropertyGridPageState
* state
= GetParentState();
1197 // State safety check (it may be NULL in immediate parent)
1202 wxPGProperty
* selected
= state
->GetSelection();
1205 if ( selected
->GetParent() == this )
1206 oldSel
= selected
->GetIndexInParent();
1207 else if ( selected
== this )
1211 state
->DoClearSelection();
1214 // Delete old children
1215 for ( i
=0; i
<prevChildCount
; i
++ )
1216 delete m_children
[i
];
1220 // Relay wxPG_BOOL_USE_CHECKBOX and wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING
1221 // to child bool property controls.
1222 long attrUseCheckBox
= GetAttributeAsLong(wxPG_BOOL_USE_CHECKBOX
, 0);
1223 long attrUseDCC
= GetAttributeAsLong(wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING
,
1226 if ( m_choices
.IsOk() )
1228 const wxPGChoices
& choices
= m_choices
;
1230 for ( i
=0; i
<GetItemCount(); i
++ )
1233 child_val
= ( value
& choices
.GetValue(i
) )?true:false;
1235 wxPGProperty
* boolProp
;
1236 wxString label
= GetLabel(i
);
1239 if ( wxPGGlobalVars
->m_autoGetTranslation
)
1241 boolProp
= new wxBoolProperty( ::wxGetTranslation(label
), label
, child_val
);
1246 boolProp
= new wxBoolProperty( label
, label
, child_val
);
1248 if ( attrUseCheckBox
)
1249 boolProp
->SetAttribute(wxPG_BOOL_USE_CHECKBOX
,
1252 boolProp
->SetAttribute(wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING
,
1254 AddPrivateChild(boolProp
);
1257 m_oldChoicesData
= m_choices
.GetDataPtr();
1260 m_oldValue
= m_value
;
1262 if ( prevChildCount
)
1263 SubPropsChanged(oldSel
);
1266 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1267 const wxChar
* const* labels
, const long* values
, long value
) : wxPGProperty(label
,name
)
1269 m_oldChoicesData
= NULL
;
1273 m_choices
.Set(labels
,values
);
1275 wxASSERT( GetItemCount() );
1281 m_value
= wxPGVariant_Zero
;
1285 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1286 const wxArrayString
& labels
, const wxArrayInt
& values
, int value
)
1287 : wxPGProperty(label
,name
)
1289 m_oldChoicesData
= NULL
;
1291 if ( &labels
&& labels
.size() )
1293 m_choices
.Set(labels
,values
);
1295 wxASSERT( GetItemCount() );
1297 SetValue( (long)value
);
1301 m_value
= wxPGVariant_Zero
;
1305 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1306 wxPGChoices
& choices
, long value
)
1307 : wxPGProperty(label
,name
)
1309 m_oldChoicesData
= NULL
;
1311 if ( choices
.IsOk() )
1313 m_choices
.Assign(choices
);
1315 wxASSERT( GetItemCount() );
1321 m_value
= wxPGVariant_Zero
;
1325 wxFlagsProperty::~wxFlagsProperty()
1329 void wxFlagsProperty::OnSetValue()
1331 if ( !m_choices
.IsOk() || !GetItemCount() )
1333 m_value
= wxPGVariant_Zero
;
1337 long val
= m_value
.GetLong();
1341 // normalize the value (i.e. remove extra flags)
1343 const wxPGChoices
& choices
= m_choices
;
1344 for ( i
= 0; i
< GetItemCount(); i
++ )
1346 fullFlags
|= choices
.GetValue(i
);
1353 // Need to (re)init now?
1354 if ( GetChildCount() != GetItemCount() ||
1355 m_choices
.GetDataPtr() != m_oldChoicesData
)
1361 long newFlags
= m_value
;
1363 if ( newFlags
!= m_oldValue
)
1365 // Set child modified states
1367 const wxPGChoices
& choices
= m_choices
;
1368 for ( i
= 0; i
<GetItemCount(); i
++ )
1372 flag
= choices
.GetValue(i
);
1374 if ( (newFlags
& flag
) != (m_oldValue
& flag
) )
1375 Item(i
)->SetFlag( wxPG_PROP_MODIFIED
);
1378 m_oldValue
= newFlags
;
1382 wxString
wxFlagsProperty::ValueToString( wxVariant
& value
,
1383 int WXUNUSED(argFlags
) ) const
1387 if ( !m_choices
.IsOk() )
1392 const wxPGChoices
& choices
= m_choices
;
1394 for ( i
= 0; i
< GetItemCount(); i
++ )
1397 doAdd
= ( flags
& choices
.GetValue(i
) );
1401 text
+= choices
.GetLabel(i
);
1406 // remove last comma
1407 if ( text
.Len() > 1 )
1408 text
.Truncate ( text
.Len() - 2 );
1413 // Translate string into flag tokens
1414 bool wxFlagsProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
1416 if ( !m_choices
.IsOk() )
1421 // semicolons are no longer valid delimeters
1422 WX_PG_TOKENIZER1_BEGIN(text
,wxS(','))
1424 if ( token
.length() )
1426 // Determine which one it is
1427 long bit
= IdToBit( token
);
1440 WX_PG_TOKENIZER1_END()
1442 if ( variant
!= (long)newFlags
)
1444 variant
= (long)newFlags
;
1451 // Converts string id to a relevant bit.
1452 long wxFlagsProperty::IdToBit( const wxString
& id
) const
1455 for ( i
= 0; i
< GetItemCount(); i
++ )
1457 if ( id
== GetLabel(i
) )
1459 return m_choices
.GetValue(i
);
1465 void wxFlagsProperty::RefreshChildren()
1467 if ( !m_choices
.IsOk() || !GetChildCount() ) return;
1469 int flags
= m_value
.GetLong();
1471 const wxPGChoices
& choices
= m_choices
;
1473 for ( i
= 0; i
< GetItemCount(); i
++ )
1477 flag
= choices
.GetValue(i
);
1479 long subVal
= flags
& flag
;
1480 wxPGProperty
* p
= Item(i
);
1482 if ( subVal
!= (m_oldValue
& flag
) )
1483 p
->SetFlag( wxPG_PROP_MODIFIED
);
1485 p
->SetValue( subVal
?true:false );
1491 wxVariant
wxFlagsProperty::ChildChanged( wxVariant
& thisValue
,
1493 wxVariant
& childValue
) const
1495 long oldValue
= thisValue
.GetLong();
1496 long val
= childValue
.GetLong();
1497 unsigned long vi
= m_choices
.GetValue(childIndex
);
1500 return (long) (oldValue
| vi
);
1502 return (long) (oldValue
& ~(vi
));
1505 bool wxFlagsProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
1507 if ( name
== wxPG_BOOL_USE_CHECKBOX
||
1508 name
== wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING
)
1510 for ( size_t i
=0; i
<GetChildCount(); i
++ )
1512 Item(i
)->SetAttribute(name
, value
);
1514 // Must return false so that the attribute is stored in
1515 // flag property's actual property storage
1521 // -----------------------------------------------------------------------
1523 // -----------------------------------------------------------------------
1525 IMPLEMENT_DYNAMIC_CLASS(wxDirProperty
, wxLongStringProperty
)
1527 wxDirProperty::wxDirProperty( const wxString
& name
, const wxString
& label
, const wxString
& value
)
1528 : wxLongStringProperty(name
,label
,value
)
1530 m_flags
|= wxPG_PROP_NO_ESCAPE
;
1533 wxDirProperty::~wxDirProperty() { }
1535 wxValidator
* wxDirProperty::DoGetValidator() const
1537 return wxFileProperty::GetClassValidator();
1540 bool wxDirProperty::OnButtonClick( wxPropertyGrid
* propGrid
, wxString
& value
)
1542 // Update property value from editor, if necessary
1543 wxSize
dlg_sz(300,400);
1545 wxString
dlgMessage(m_dlgMessage
);
1546 if ( dlgMessage
.empty() )
1547 dlgMessage
= _("Choose a directory:");
1548 wxDirDialog
dlg( propGrid
,
1552 #if !wxPG_SMALL_SCREEN
1553 propGrid
->GetGoodEditorDialogPosition(this,dlg_sz
),
1561 if ( dlg
.ShowModal() == wxID_OK
)
1563 value
= dlg
.GetPath();
1569 bool wxDirProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
1571 if ( name
== wxPG_DIR_DIALOG_MESSAGE
)
1573 m_dlgMessage
= value
.GetString();
1579 // -----------------------------------------------------------------------
1580 // wxPGFileDialogAdapter
1581 // -----------------------------------------------------------------------
1583 bool wxPGFileDialogAdapter::DoShowDialog( wxPropertyGrid
* propGrid
, wxPGProperty
* property
)
1585 wxFileProperty
* fileProp
= NULL
;
1589 if ( property
->IsKindOf(CLASSINFO(wxFileProperty
)) )
1591 fileProp
= ((wxFileProperty
*)property
);
1592 wxFileName filename
= fileProp
->GetValue().GetString();
1593 path
= filename
.GetPath();
1594 indFilter
= fileProp
->m_indFilter
;
1596 if ( !path
.length() && fileProp
->m_basePath
.length() )
1597 path
= fileProp
->m_basePath
;
1601 wxFileName
fn(property
->GetValue().GetString());
1602 path
= fn
.GetPath();
1605 wxFileDialog
dlg( propGrid
->GetPanel(),
1606 property
->GetAttribute(wxS("DialogTitle"), _("Choose a file")),
1607 property
->GetAttribute(wxS("InitialPath"), path
),
1609 property
->GetAttribute(wxPG_FILE_WILDCARD
, _("All files (*.*)|*.*")),
1611 wxDefaultPosition
);
1613 if ( indFilter
>= 0 )
1614 dlg
.SetFilterIndex( indFilter
);
1616 if ( dlg
.ShowModal() == wxID_OK
)
1619 fileProp
->m_indFilter
= dlg
.GetFilterIndex();
1620 SetValue( dlg
.GetPath() );
1626 // -----------------------------------------------------------------------
1628 // -----------------------------------------------------------------------
1630 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFileProperty
,wxPGProperty
,
1631 wxString
,const wxString
&,TextCtrlAndButton
)
1633 wxFileProperty::wxFileProperty( const wxString
& label
, const wxString
& name
,
1634 const wxString
& value
) : wxPGProperty(label
,name
)
1636 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1638 SetAttribute( wxPG_FILE_WILDCARD
, _("All files (*.*)|*.*") );
1643 wxFileProperty::~wxFileProperty() {}
1645 wxValidator
* wxFileProperty::GetClassValidator()
1647 #if wxUSE_VALIDATORS
1648 WX_PG_DOGETVALIDATOR_ENTRY()
1650 // Atleast wxPython 2.6.2.1 required that the string argument is given
1652 wxTextValidator
* validator
= new wxTextValidator(wxFILTER_EXCLUDE_CHAR_LIST
,&v
);
1654 wxArrayString exChars
;
1655 exChars
.Add(wxS("?"));
1656 exChars
.Add(wxS("*"));
1657 exChars
.Add(wxS("|"));
1658 exChars
.Add(wxS("<"));
1659 exChars
.Add(wxS(">"));
1660 exChars
.Add(wxS("\""));
1662 validator
->SetExcludes(exChars
);
1664 WX_PG_DOGETVALIDATOR_EXIT(validator
)
1670 wxValidator
* wxFileProperty::DoGetValidator() const
1672 return GetClassValidator();
1675 void wxFileProperty::OnSetValue()
1677 const wxString
& fnstr
= m_value
.GetString();
1679 wxFileName filename
= fnstr
;
1681 if ( !filename
.HasName() )
1683 m_value
= wxPGVariant_EmptyString
;
1686 // Find index for extension.
1687 if ( m_indFilter
< 0 && fnstr
.length() )
1689 wxString ext
= filename
.GetExt();
1692 size_t len
= m_wildcard
.length();
1694 pos
= m_wildcard
.find(wxS("|"), pos
);
1695 while ( pos
!= wxString::npos
&& pos
< (len
-3) )
1697 size_t ext_begin
= pos
+ 3;
1699 pos
= m_wildcard
.find(wxS("|"), ext_begin
);
1700 if ( pos
== wxString::npos
)
1702 wxString found_ext
= m_wildcard
.substr(ext_begin
, pos
-ext_begin
);
1704 if ( found_ext
.length() > 0 )
1706 if ( found_ext
[0] == wxS('*') )
1708 m_indFilter
= curind
;
1711 if ( ext
.CmpNoCase(found_ext
) == 0 )
1713 m_indFilter
= curind
;
1719 pos
= m_wildcard
.find(wxS("|"), pos
+1);
1726 wxFileName
wxFileProperty::GetFileName() const
1728 wxFileName filename
;
1730 if ( !m_value
.IsNull() )
1731 filename
= m_value
.GetString();
1736 wxString
wxFileProperty::ValueToString( wxVariant
& value
,
1737 int argFlags
) const
1739 wxFileName filename
= value
.GetString();
1741 if ( !filename
.HasName() )
1742 return wxEmptyString
;
1744 wxString fullName
= filename
.GetFullName();
1745 if ( !fullName
.length() )
1746 return wxEmptyString
;
1748 if ( argFlags
& wxPG_FULL_VALUE
)
1750 return filename
.GetFullPath();
1752 else if ( m_flags
& wxPG_PROP_SHOW_FULL_FILENAME
)
1754 if ( m_basePath
.Length() )
1756 wxFileName
fn2(filename
);
1757 fn2
.MakeRelativeTo(m_basePath
);
1758 return fn2
.GetFullPath();
1760 return filename
.GetFullPath();
1763 return filename
.GetFullName();
1766 wxPGEditorDialogAdapter
* wxFileProperty::GetEditorDialog() const
1768 return new wxPGFileDialogAdapter();
1771 bool wxFileProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
1773 wxFileName filename
= variant
.GetString();
1775 if ( (m_flags
& wxPG_PROP_SHOW_FULL_FILENAME
) || (argFlags
& wxPG_FULL_VALUE
) )
1777 if ( filename
!= text
)
1785 if ( filename
.GetFullName() != text
)
1787 wxFileName fn
= filename
;
1788 fn
.SetFullName(text
);
1789 variant
= fn
.GetFullPath();
1797 bool wxFileProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
1799 // Return false on some occasions to make sure those attribs will get
1800 // stored in m_attributes.
1801 if ( name
== wxPG_FILE_SHOW_FULL_PATH
)
1803 if ( value
.GetLong() )
1804 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1806 m_flags
&= ~(wxPG_PROP_SHOW_FULL_FILENAME
);
1809 else if ( name
== wxPG_FILE_WILDCARD
)
1811 m_wildcard
= value
.GetString();
1813 else if ( name
== wxPG_FILE_SHOW_RELATIVE_PATH
)
1815 m_basePath
= value
.GetString();
1817 // Make sure wxPG_FILE_SHOW_FULL_PATH is also set
1818 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1820 else if ( name
== wxPG_FILE_INITIAL_PATH
)
1822 m_initialPath
= value
.GetString();
1825 else if ( name
== wxPG_FILE_DIALOG_TITLE
)
1827 m_dlgTitle
= value
.GetString();
1833 // -----------------------------------------------------------------------
1834 // wxPGLongStringDialogAdapter
1835 // -----------------------------------------------------------------------
1837 bool wxPGLongStringDialogAdapter::DoShowDialog( wxPropertyGrid
* propGrid
, wxPGProperty
* property
)
1839 wxString val1
= property
->GetValueAsString(0);
1840 wxString val_orig
= val1
;
1843 if ( !property
->HasFlag(wxPG_PROP_NO_ESCAPE
) )
1844 wxPropertyGrid::ExpandEscapeSequences(value
, val1
);
1846 value
= wxString(val1
);
1848 // Run editor dialog.
1849 if ( wxLongStringProperty::DisplayEditorDialog(property
, propGrid
, value
) )
1851 if ( !property
->HasFlag(wxPG_PROP_NO_ESCAPE
) )
1852 wxPropertyGrid::CreateEscapeSequences(val1
,value
);
1856 if ( val1
!= val_orig
)
1865 // -----------------------------------------------------------------------
1866 // wxLongStringProperty
1867 // -----------------------------------------------------------------------
1869 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxLongStringProperty
,wxPGProperty
,
1870 wxString
,const wxString
&,TextCtrlAndButton
)
1872 wxLongStringProperty::wxLongStringProperty( const wxString
& label
, const wxString
& name
,
1873 const wxString
& value
) : wxPGProperty(label
,name
)
1878 wxLongStringProperty::~wxLongStringProperty() {}
1880 wxString
wxLongStringProperty::ValueToString( wxVariant
& value
,
1881 int WXUNUSED(argFlags
) ) const
1886 bool wxLongStringProperty::OnEvent( wxPropertyGrid
* propGrid
, wxWindow
* WXUNUSED(primary
),
1889 if ( propGrid
->IsMainButtonEvent(event
) )
1892 wxVariant useValue
= propGrid
->GetUncommittedPropertyValue();
1894 wxString val1
= useValue
.GetString();
1895 wxString val_orig
= val1
;
1898 if ( !(m_flags
& wxPG_PROP_NO_ESCAPE
) )
1899 wxPropertyGrid::ExpandEscapeSequences(value
,val1
);
1901 value
= wxString(val1
);
1903 // Run editor dialog.
1904 if ( OnButtonClick(propGrid
,value
) )
1906 if ( !(m_flags
& wxPG_PROP_NO_ESCAPE
) )
1907 wxPropertyGrid::CreateEscapeSequences(val1
,value
);
1911 if ( val1
!= val_orig
)
1913 SetValueInEvent( val1
);
1921 bool wxLongStringProperty::OnButtonClick( wxPropertyGrid
* propGrid
, wxString
& value
)
1923 return DisplayEditorDialog(this, propGrid
, value
);
1926 bool wxLongStringProperty::DisplayEditorDialog( wxPGProperty
* prop
, wxPropertyGrid
* propGrid
, wxString
& value
)
1929 // launch editor dialog
1930 wxDialog
* dlg
= new wxDialog(propGrid
,-1,prop
->GetLabel(),wxDefaultPosition
,wxDefaultSize
,
1931 wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
|wxCLIP_CHILDREN
);
1933 dlg
->SetFont(propGrid
->GetFont()); // To allow entering chars of the same set as the propGrid
1935 // Multi-line text editor dialog.
1936 #if !wxPG_SMALL_SCREEN
1937 const int spacing
= 8;
1939 const int spacing
= 4;
1941 wxBoxSizer
* topsizer
= new wxBoxSizer( wxVERTICAL
);
1942 wxBoxSizer
* rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
1943 wxTextCtrl
* ed
= new wxTextCtrl(dlg
,11,value
,
1944 wxDefaultPosition
,wxDefaultSize
,wxTE_MULTILINE
);
1946 rowsizer
->Add( ed
, 1, wxEXPAND
|wxALL
, spacing
);
1947 topsizer
->Add( rowsizer
, 1, wxEXPAND
, 0 );
1949 wxStdDialogButtonSizer
* buttonSizer
= new wxStdDialogButtonSizer();
1950 buttonSizer
->AddButton(new wxButton(dlg
, wxID_OK
));
1951 buttonSizer
->AddButton(new wxButton(dlg
, wxID_CANCEL
));
1952 buttonSizer
->Realize();
1953 topsizer
->Add( buttonSizer
, 0,
1954 wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
|wxBOTTOM
|wxRIGHT
,
1957 dlg
->SetSizer( topsizer
);
1958 topsizer
->SetSizeHints( dlg
);
1960 #if !wxPG_SMALL_SCREEN
1961 dlg
->SetSize(400,300);
1963 dlg
->Move( propGrid
->GetGoodEditorDialogPosition(prop
,dlg
->GetSize()) );
1966 int res
= dlg
->ShowModal();
1968 if ( res
== wxID_OK
)
1970 value
= ed
->GetValue();
1978 bool wxLongStringProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
1980 if ( variant
!= text
)
1988 // -----------------------------------------------------------------------
1989 // wxArrayEditorDialog
1990 // -----------------------------------------------------------------------
1992 BEGIN_EVENT_TABLE(wxArrayEditorDialog
, wxDialog
)
1993 EVT_IDLE(wxArrayEditorDialog::OnIdle
)
1994 EVT_LISTBOX(24, wxArrayEditorDialog::OnListBoxClick
)
1995 EVT_TEXT_ENTER(21, wxArrayEditorDialog::OnAddClick
)
1996 EVT_BUTTON(22, wxArrayEditorDialog::OnAddClick
)
1997 EVT_BUTTON(23, wxArrayEditorDialog::OnDeleteClick
)
1998 EVT_BUTTON(25, wxArrayEditorDialog::OnUpClick
)
1999 EVT_BUTTON(26, wxArrayEditorDialog::OnDownClick
)
2000 EVT_BUTTON(27, wxArrayEditorDialog::OnUpdateClick
)
2001 //EVT_BUTTON(28, wxArrayEditorDialog::OnCustomEditClick)
2004 IMPLEMENT_ABSTRACT_CLASS(wxArrayEditorDialog
, wxDialog
)
2006 #include "wx/statline.h"
2008 // -----------------------------------------------------------------------
2010 void wxArrayEditorDialog::OnIdle(wxIdleEvent
& event
)
2013 // Do control focus detection here.
2016 wxWindow
* focused
= FindFocus();
2018 // This strange focus thing is a workaround for wxGTK wxListBox focus
2020 if ( m_curFocus
== 0 && focused
!= m_edValue
&&
2021 focused
!= m_butAdd
&& focused
!= m_butUpdate
&&
2022 m_lbStrings
->GetSelection() >= 0 )
2024 // ListBox was just focused.
2025 m_butAdd
->Enable(false);
2026 m_butUpdate
->Enable(false);
2027 m_butRemove
->Enable(true);
2028 m_butUp
->Enable(true);
2029 m_butDown
->Enable(true);
2032 else if ( (m_curFocus
== 1 && focused
== m_edValue
) /*|| m_curFocus == 2*/ )
2034 // TextCtrl was just focused.
2035 m_butAdd
->Enable(true);
2036 bool upd_enable
= false;
2037 if ( m_lbStrings
->GetCount() && m_lbStrings
->GetSelection() >= 0 )
2039 m_butUpdate
->Enable(upd_enable
);
2040 m_butRemove
->Enable(false);
2041 m_butUp
->Enable(false);
2042 m_butDown
->Enable(false);
2049 // -----------------------------------------------------------------------
2051 wxArrayEditorDialog::wxArrayEditorDialog()
2057 // -----------------------------------------------------------------------
2059 void wxArrayEditorDialog::Init()
2061 m_custBtText
= (const wxChar
*) NULL
;
2064 // -----------------------------------------------------------------------
2066 wxArrayEditorDialog::wxArrayEditorDialog( wxWindow
*parent
,
2067 const wxString
& message
,
2068 const wxString
& caption
,
2075 Create(parent
,message
,caption
,style
,pos
,sz
);
2078 // -----------------------------------------------------------------------
2080 bool wxArrayEditorDialog::Create( wxWindow
*parent
,
2081 const wxString
& message
,
2082 const wxString
& caption
,
2087 // On wxMAC the dialog shows incorrectly if style is not exactly wxCAPTION
2088 // FIXME: This should be only a temporary fix.
2091 int useStyle
= wxCAPTION
;
2093 int useStyle
= style
;
2096 bool res
= wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, sz
, useStyle
);
2098 SetFont(parent
->GetFont()); // To allow entering chars of the same set as the propGrid
2100 #if !wxPG_SMALL_SCREEN
2101 const int spacing
= 4;
2103 const int spacing
= 3;
2110 wxBoxSizer
* topsizer
= new wxBoxSizer( wxVERTICAL
);
2113 if ( message
.length() )
2114 topsizer
->Add( new wxStaticText(this,-1,message
),
2115 0, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxALL
, spacing
);
2118 wxBoxSizer
* rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
2119 m_edValue
= new wxTextCtrl(this,21,wxEmptyString
,
2120 wxDefaultPosition
,wxDefaultSize
,wxTE_PROCESS_ENTER
);
2121 #if wxUSE_VALIDATORS
2122 wxValidator
* validator
= GetTextCtrlValidator();
2125 m_edValue
->SetValidator( *validator
);
2129 rowsizer
->Add( m_edValue
,
2130 1, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxALL
, spacing
);
2133 m_butAdd
= new wxButton(this,22,_("Add"));
2134 rowsizer
->Add( m_butAdd
,
2135 0, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxTOP
|wxBOTTOM
|wxRIGHT
, spacing
);
2136 topsizer
->Add( rowsizer
, 0, wxEXPAND
, spacing
);
2139 topsizer
->Add( new wxStaticLine(this,-1),
2140 0, wxEXPAND
|wxBOTTOM
|wxLEFT
|wxRIGHT
, spacing
);
2142 rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
2145 m_lbStrings
= new wxListBox(this, 24, wxDefaultPosition
, wxDefaultSize
);
2147 for ( i
=0; i
<ArrayGetCount(); i
++ )
2148 m_lbStrings
->Append( ArrayGet(i
) );
2149 rowsizer
->Add( m_lbStrings
, 1, wxEXPAND
|wxRIGHT
, spacing
);
2151 // Manipulator buttons
2152 wxBoxSizer
* colsizer
= new wxBoxSizer( wxVERTICAL
);
2156 m_butCustom
= new wxButton(this,28,::wxGetTranslation(m_custBtText
));
2157 colsizer
->Add( m_butCustom
,
2158 0, wxALIGN_CENTER
|wxTOP
/*wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT*/,
2161 m_butUpdate
= new wxButton(this,27,_("Update"));
2162 colsizer
->Add( m_butUpdate
,
2163 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2164 m_butRemove
= new wxButton(this,23,_("Remove"));
2165 colsizer
->Add( m_butRemove
,
2166 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2167 m_butUp
= new wxButton(this,25,_("Up"));
2168 colsizer
->Add( m_butUp
,
2169 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2170 m_butDown
= new wxButton(this,26,_("Down"));
2171 colsizer
->Add( m_butDown
,
2172 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2173 rowsizer
->Add( colsizer
, 0, 0, spacing
);
2175 topsizer
->Add( rowsizer
, 1, wxLEFT
|wxRIGHT
|wxEXPAND
, spacing
);
2178 topsizer
->Add( new wxStaticLine(this,-1),
2179 0, wxEXPAND
|wxTOP
|wxLEFT
|wxRIGHT
, spacing
);
2181 // Standard dialog buttons
2182 wxStdDialogButtonSizer
* buttonSizer
= new wxStdDialogButtonSizer();
2183 buttonSizer
->AddButton(new wxButton(this, wxID_OK
));
2184 buttonSizer
->AddButton(new wxButton(this, wxID_CANCEL
));
2185 buttonSizer
->Realize();
2186 topsizer
->Add( buttonSizer
, 0,
2187 wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
|wxALL
,
2190 m_edValue
->SetFocus();
2192 SetSizer( topsizer
);
2193 topsizer
->SetSizeHints( this );
2195 #if !wxPG_SMALL_SCREEN
2196 if ( sz
.x
== wxDefaultSize
.x
&&
2197 sz
.y
== wxDefaultSize
.y
)
2198 SetSize( wxSize(275,360) );
2206 // -----------------------------------------------------------------------
2208 void wxArrayEditorDialog::OnAddClick(wxCommandEvent
& )
2210 wxString text
= m_edValue
->GetValue();
2211 if ( text
.length() )
2213 if ( ArrayInsert( text
, -1 ) )
2215 m_lbStrings
->Append( text
);
2222 // -----------------------------------------------------------------------
2224 void wxArrayEditorDialog::OnDeleteClick(wxCommandEvent
& )
2226 int index
= m_lbStrings
->GetSelection();
2229 ArrayRemoveAt( index
);
2230 m_lbStrings
->Delete ( index
);
2235 // -----------------------------------------------------------------------
2237 void wxArrayEditorDialog::OnUpClick(wxCommandEvent
& )
2239 int index
= m_lbStrings
->GetSelection();
2242 ArraySwap(index
-1,index
);
2243 /*wxString old_str = m_array[index-1];
2244 wxString new_str = m_array[index];
2245 m_array[index-1] = new_str;
2246 m_array[index] = old_str;*/
2247 m_lbStrings
->SetString ( index
-1, ArrayGet(index
-1) );
2248 m_lbStrings
->SetString ( index
, ArrayGet(index
) );
2249 m_lbStrings
->SetSelection ( index
-1 );
2254 // -----------------------------------------------------------------------
2256 void wxArrayEditorDialog::OnDownClick(wxCommandEvent
& )
2258 int index
= m_lbStrings
->GetSelection();
2259 int lastStringIndex
= ((int) m_lbStrings
->GetCount()) - 1;
2260 if ( index
>= 0 && index
< lastStringIndex
)
2262 ArraySwap(index
,index
+1);
2263 /*wxString old_str = m_array[index+1];
2264 wxString new_str = m_array[index];
2265 m_array[index+1] = new_str;
2266 m_array[index] = old_str;*/
2267 m_lbStrings
->SetString ( index
+1, ArrayGet(index
+1) );
2268 m_lbStrings
->SetString ( index
, ArrayGet(index
) );
2269 m_lbStrings
->SetSelection ( index
+1 );
2274 // -----------------------------------------------------------------------
2276 void wxArrayEditorDialog::OnUpdateClick(wxCommandEvent
& )
2278 int index
= m_lbStrings
->GetSelection();
2281 wxString str
= m_edValue
->GetValue();
2282 if ( ArraySet(index
,str
) )
2284 m_lbStrings
->SetString ( index
, str
);
2285 //m_array[index] = str;
2291 // -----------------------------------------------------------------------
2293 void wxArrayEditorDialog::OnListBoxClick(wxCommandEvent
& )
2295 int index
= m_lbStrings
->GetSelection();
2298 m_edValue
->SetValue( m_lbStrings
->GetString(index
) );
2302 // -----------------------------------------------------------------------
2303 // wxPGArrayStringEditorDialog
2304 // -----------------------------------------------------------------------
2306 IMPLEMENT_DYNAMIC_CLASS(wxPGArrayStringEditorDialog
, wxArrayEditorDialog
)
2308 BEGIN_EVENT_TABLE(wxPGArrayStringEditorDialog
, wxArrayEditorDialog
)
2309 EVT_BUTTON(28, wxPGArrayStringEditorDialog::OnCustomEditClick
)
2312 // -----------------------------------------------------------------------
2314 wxString
wxPGArrayStringEditorDialog::ArrayGet( size_t index
)
2316 return m_array
[index
];
2319 size_t wxPGArrayStringEditorDialog::ArrayGetCount()
2321 return m_array
.size();
2324 bool wxPGArrayStringEditorDialog::ArrayInsert( const wxString
& str
, int index
)
2329 m_array
.Insert(str
,index
);
2333 bool wxPGArrayStringEditorDialog::ArraySet( size_t index
, const wxString
& str
)
2335 m_array
[index
] = str
;
2339 void wxPGArrayStringEditorDialog::ArrayRemoveAt( int index
)
2341 m_array
.RemoveAt(index
);
2344 void wxPGArrayStringEditorDialog::ArraySwap( size_t first
, size_t second
)
2346 wxString old_str
= m_array
[first
];
2347 wxString new_str
= m_array
[second
];
2348 m_array
[first
] = new_str
;
2349 m_array
[second
] = old_str
;
2352 wxPGArrayStringEditorDialog::wxPGArrayStringEditorDialog()
2353 : wxArrayEditorDialog()
2358 void wxPGArrayStringEditorDialog::Init()
2360 m_pCallingClass
= NULL
;
2363 void wxPGArrayStringEditorDialog::OnCustomEditClick(wxCommandEvent
& )
2365 wxASSERT( m_pCallingClass
);
2366 wxString str
= m_edValue
->GetValue();
2367 if ( m_pCallingClass
->OnCustomStringEdit(m_parent
,str
) )
2369 //m_edValue->SetValue ( str );
2370 m_lbStrings
->Append ( str
);
2371 m_array
.Add ( str
);
2376 // -----------------------------------------------------------------------
2377 // wxArrayStringProperty
2378 // -----------------------------------------------------------------------
2380 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayStringProperty
, // Property name
2381 wxPGProperty
, // Property we inherit from
2382 wxArrayString
, // Value type name
2383 const wxArrayString
&, // Value type, as given in constructor
2384 TextCtrlAndButton
) // Initial editor
2386 wxArrayStringProperty::wxArrayStringProperty( const wxString
& label
,
2387 const wxString
& name
,
2388 const wxArrayString
& array
)
2389 : wxPGProperty(label
,name
)
2394 wxArrayStringProperty::~wxArrayStringProperty() { }
2396 void wxArrayStringProperty::OnSetValue()
2398 GenerateValueAsString();
2401 #define ARRSTRPROP_ARRAY_TO_STRING(STRING,ARRAY) \
2402 wxPropertyGrid::ArrayStringToString(STRING,ARRAY,wxS('"'),wxS('"'),1)
2404 wxString
wxArrayStringProperty::ValueToString( wxVariant
& WXUNUSED(value
),
2405 int argFlags
) const
2408 // If this is called from GetValueAsString(), return cached string
2409 if ( argFlags
& wxPG_VALUE_IS_CURRENT
)
2414 wxArrayString arr
= m_value
.GetArrayString();
2416 ARRSTRPROP_ARRAY_TO_STRING(s
, arr
);
2420 // Converts wxArrayString to a string separated by delimeters and spaces.
2421 // preDelim is useful for "str1" "str2" style. Set flags to 1 to do slash
2423 void wxPropertyGrid::ArrayStringToString( wxString
& dst
, const wxArrayString
& src
,
2424 wxChar preDelim
, wxChar postDelim
,
2430 unsigned int itemCount
= src
.size();
2432 wxChar preas
[2] = { 0, 0 };
2438 preas
[0] = preDelim
;
2444 dst
.append( preas
);
2446 wxASSERT( postDelim
);
2447 wxString
postDelimStr(postDelim
);
2448 //wxString preDelimStr(preDelim);
2450 for ( i
= 0; i
< itemCount
; i
++ )
2452 wxString
str( src
.Item(i
) );
2454 // Do some character conversion.
2455 // Convertes \ to \\ and <preDelim> to \<preDelim>
2456 // Useful when preDelim and postDelim are "\"".
2459 str
.Replace( wxS("\\"), wxS("\\\\"), true );
2461 str
.Replace( preas
, pdr
, true );
2466 if ( i
< (itemCount
-1) )
2468 dst
.append( postDelimStr
);
2469 dst
.append( wxS(" ") );
2470 dst
.append( preas
);
2472 else if ( preDelim
)
2473 dst
.append( postDelimStr
);
2477 void wxArrayStringProperty::GenerateValueAsString()
2479 wxArrayString arr
= m_value
.GetArrayString();
2480 ARRSTRPROP_ARRAY_TO_STRING(m_display
, arr
);
2483 // Default implementation doesn't do anything.
2484 bool wxArrayStringProperty::OnCustomStringEdit( wxWindow
*, wxString
& )
2489 wxArrayEditorDialog
* wxArrayStringProperty::CreateEditorDialog()
2491 return new wxPGArrayStringEditorDialog();
2494 bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid
* propGrid
,
2495 wxWindow
* WXUNUSED(primaryCtrl
),
2499 wxVariant useValue
= propGrid
->GetUncommittedPropertyValue();
2501 if ( !propGrid
->EditorValidate() )
2504 // Create editor dialog.
2505 wxArrayEditorDialog
* dlg
= CreateEditorDialog();
2506 #if wxUSE_VALIDATORS
2507 wxValidator
* validator
= GetValidator();
2508 wxPGInDialogValidator dialogValidator
;
2511 wxPGArrayStringEditorDialog
* strEdDlg
= wxDynamicCast(dlg
, wxPGArrayStringEditorDialog
);
2514 strEdDlg
->SetCustomButton(cbt
, this);
2516 dlg
->SetDialogValue( useValue
);
2517 dlg
->Create(propGrid
, wxEmptyString
, m_label
);
2519 #if !wxPG_SMALL_SCREEN
2520 dlg
->Move( propGrid
->GetGoodEditorDialogPosition(this,dlg
->GetSize()) );
2529 int res
= dlg
->ShowModal();
2531 if ( res
== wxID_OK
&& dlg
->IsModified() )
2533 wxVariant value
= dlg
->GetDialogValue();
2534 if ( !value
.IsNull() )
2536 wxArrayString actualValue
= value
.GetArrayString();
2538 ARRSTRPROP_ARRAY_TO_STRING(tempStr
, actualValue
);
2539 #if wxUSE_VALIDATORS
2540 if ( dialogValidator
.DoValidate( propGrid
, validator
, tempStr
) )
2543 SetValueInEvent( actualValue
);
2560 bool wxArrayStringProperty::OnEvent( wxPropertyGrid
* propGrid
,
2564 if ( propGrid
->IsMainButtonEvent(event
) )
2565 return OnButtonClick(propGrid
,primary
,(const wxChar
*) NULL
);
2569 bool wxArrayStringProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
2573 WX_PG_TOKENIZER2_BEGIN(text
,wxS('"'))
2575 // Need to replace backslashes with empty characters
2576 // (opposite what is done in GenerateValueString).
2577 token
.Replace ( wxS("\\\\"), wxS("\\"), true );
2581 WX_PG_TOKENIZER2_END()
2588 // -----------------------------------------------------------------------
2589 // wxPGInDialogValidator
2590 // -----------------------------------------------------------------------
2592 #if wxUSE_VALIDATORS
2593 bool wxPGInDialogValidator::DoValidate( wxPropertyGrid
* propGrid
,
2594 wxValidator
* validator
,
2595 const wxString
& value
)
2600 wxTextCtrl
* tc
= m_textCtrl
;
2605 tc
= new wxTextCtrl( propGrid
, wxPG_SUBID_TEMP1
, wxEmptyString
,
2606 wxPoint(30000,30000));
2613 tc
->SetValue(value
);
2615 validator
->SetWindow(tc
);
2616 bool res
= validator
->Validate(propGrid
);
2621 bool wxPGInDialogValidator::DoValidate( wxPropertyGrid
* WXUNUSED(propGrid
),
2622 wxValidator
* WXUNUSED(validator
),
2623 const wxString
& WXUNUSED(value
) )
2629 // -----------------------------------------------------------------------
2631 #endif // wxUSE_PROPGRID