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 ( wxPGVariantToInt(value
) ) 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() == wxLongLong_VariantType
)
176 return ll
.ToString();
179 return wxEmptyString
;
182 bool wxIntProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
187 if ( text
.length() == 0 )
193 // We know it is a number, but let's still check
195 if ( text
.IsNumber() )
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
201 int firstNonZeroPos
= 0;
203 for ( ; i
!= iMax
; ++i
)
206 if ( c
!= wxS('0') && c
!= wxS(' ') )
211 wxString useText
= text
.substr(firstNonZeroPos
, text
.length() - firstNonZeroPos
);
213 wxString variantType
= variant
.GetType();
214 bool isPrevLong
= variantType
== wxPG_VARIANT_TYPE_LONG
;
216 wxLongLong_t value64
= 0;
218 if ( useText
.ToLongLong(&value64
, 10) &&
219 ( value64
>= INT_MAX
|| value64
<= INT_MIN
)
222 bool doChangeValue
= isPrevLong
;
224 if ( !isPrevLong
&& variantType
== wxLongLong_VariantType
)
228 if ( oldValue
.GetValue() != value64
)
229 doChangeValue
= true;
234 wxLongLong
ll(value64
);
240 if ( useText
.ToLong( &value32
, 0 ) )
242 if ( !isPrevLong
|| variant
!= value32
)
249 else if ( argFlags
& wxPG_REPORT_ERROR
)
255 bool wxIntProperty::IntToValue( wxVariant
& variant
, int value
, int WXUNUSED(argFlags
) ) const
257 if ( variant
.GetType() != wxPG_VARIANT_TYPE_LONG
|| variant
!= (long)value
)
259 variant
= (long)value
;
265 bool wxIntProperty::DoValidation( const wxPGProperty
* property
, wxLongLong_t
& value
, wxPGValidationInfo
* pValidationInfo
, int mode
)
268 wxLongLong_t min
= wxINT64_MIN
;
269 wxLongLong_t max
= wxINT64_MAX
;
274 variant
= property
->GetAttribute(wxPGGlobalVars
->m_strMin
);
275 if ( !variant
.IsNull() )
277 wxPGVariantToLongLong(variant
, &min
);
281 variant
= property
->GetAttribute(wxPGGlobalVars
->m_strMax
);
282 if ( !variant
.IsNull() )
284 wxPGVariantToLongLong(variant
, &max
);
292 if ( mode
== wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
)
293 pValidationInfo
->SetFailureMessage(
294 wxString::Format(_("Value must be %lld or higher"),min
)
296 else if ( mode
== wxPG_PROPERTY_VALIDATION_SATURATE
)
299 value
= max
- (min
- value
);
308 if ( mode
== wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
)
309 pValidationInfo
->SetFailureMessage(
310 wxString::Format(_("Value must be %lld or higher"),min
)
312 else if ( mode
== wxPG_PROPERTY_VALIDATION_SATURATE
)
315 value
= min
+ (value
- max
);
322 bool wxIntProperty::ValidateValue( wxVariant
& value
, wxPGValidationInfo
& validationInfo
) const
325 if ( wxPGVariantToLongLong(value
, &ll
) )
326 return DoValidation(this, ll
, &validationInfo
, wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
);
330 wxValidator
* wxIntProperty::GetClassValidator()
333 WX_PG_DOGETVALIDATOR_ENTRY()
335 // Atleast wxPython 2.6.2.1 required that the string argument is given
337 wxTextValidator
* validator
= new wxTextValidator(wxFILTER_NUMERIC
,&v
);
339 WX_PG_DOGETVALIDATOR_EXIT(validator
)
345 wxValidator
* wxIntProperty::DoGetValidator() const
347 return GetClassValidator();
350 // -----------------------------------------------------------------------
352 // -----------------------------------------------------------------------
355 #define wxPG_UINT_TEMPLATE_MAX 8
357 static const wxChar
* gs_uintTemplates32
[wxPG_UINT_TEMPLATE_MAX
] = {
358 wxT("%x"),wxT("0x%x"),wxT("$%x"),
359 wxT("%X"),wxT("0x%X"),wxT("$%X"),
363 static 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")
374 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxUIntProperty
,wxPGProperty
,
375 long,unsigned long,TextCtrl
)
377 void wxUIntProperty::Init()
379 m_base
= 6; // This is magic number for dec base (must be same as in setattribute)
381 m_prefix
= wxPG_PREFIX_NONE
;
384 wxUIntProperty::wxUIntProperty( const wxString
& label
, const wxString
& name
,
385 unsigned long value
) : wxPGProperty(label
,name
)
388 SetValue((long)value
);
391 wxUIntProperty::wxUIntProperty( const wxString
& label
, const wxString
& name
,
392 const wxULongLong
& value
) : wxPGProperty(label
,name
)
395 SetValue(WXVARIANT(value
));
398 wxUIntProperty::~wxUIntProperty() { }
400 wxString
wxUIntProperty::ValueToString( wxVariant
& value
,
401 int WXUNUSED(argFlags
) ) const
403 size_t index
= m_base
+ m_prefix
;
404 if ( index
>= wxPG_UINT_TEMPLATE_MAX
)
405 index
= wxPG_BASE_DEC
;
407 if ( value
.GetType() == wxPG_VARIANT_TYPE_LONG
)
409 return wxString::Format(gs_uintTemplates32
[index
], (unsigned long)value
.GetLong());
415 return wxString::Format(gs_uintTemplates64
[index
], ull
.GetValue());
418 bool wxUIntProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int WXUNUSED(argFlags
) ) const
420 wxString variantType
= variant
.GetType();
421 bool isPrevLong
= variantType
== wxPG_VARIANT_TYPE_LONG
;
423 if ( text
.length() == 0 )
430 if ( text
[0] == wxS('$') )
433 wxULongLong_t value64
= 0;
434 wxString s
= text
.substr(start
, text
.length() - start
);
436 if ( s
.ToULongLong(&value64
, (unsigned int)m_realBase
) )
438 if ( value64
>= LONG_MAX
)
440 bool doChangeValue
= isPrevLong
;
442 if ( !isPrevLong
&& variantType
== wxULongLong_VariantType
)
444 wxULongLong oldValue
;
446 if ( oldValue
.GetValue() != value64
)
447 doChangeValue
= true;
452 wxULongLong
ull(value64
);
459 unsigned long value32
= wxLongLong(value64
).GetLo();
460 if ( !isPrevLong
|| m_value
!= (long)value32
)
462 variant
= (long)value32
;
471 bool wxUIntProperty::IntToValue( wxVariant
& variant
, int number
, int WXUNUSED(argFlags
) ) const
473 if ( variant
!= (long)number
)
475 variant
= (long)number
;
482 #define wxUINT64_MAX ULLONG_MAX
483 #define wxUINT64_MIN wxULL(0)
485 #define wxUINT64_MAX wxULL(0xFFFFFFFFFFFFFFFF)
486 #define wxUINT64_MIN wxULL(0)
489 bool wxUIntProperty::ValidateValue( wxVariant
& value
, wxPGValidationInfo
& validationInfo
) const
493 if ( wxPGVariantToULongLong(value
, &ll
) )
495 wxULongLong_t min
= wxUINT64_MIN
;
496 wxULongLong_t max
= wxUINT64_MAX
;
499 variant
= GetAttribute(wxPGGlobalVars
->m_strMin
);
500 if ( !variant
.IsNull() )
502 wxPGVariantToULongLong(variant
, &min
);
505 validationInfo
.SetFailureMessage(
506 wxString::Format(_("Value must be %llu or higher"),min
)
511 variant
= GetAttribute(wxPGGlobalVars
->m_strMax
);
512 if ( !variant
.IsNull() )
514 wxPGVariantToULongLong(variant
, &max
);
517 validationInfo
.SetFailureMessage(
518 wxString::Format(_("Value must be %llu or less"),max
)
527 bool wxUIntProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
529 if ( name
== wxPG_UINT_BASE
)
531 int val
= value
.GetLong();
533 m_realBase
= (wxByte
) val
;
534 if ( m_realBase
> 16 )
538 // Translate logical base to a template array index
540 if ( val
== wxPG_BASE_HEX
)
542 else if ( val
== wxPG_BASE_DEC
)
544 else if ( val
== wxPG_BASE_HEXL
)
548 else if ( name
== wxPG_UINT_PREFIX
)
550 m_prefix
= (wxByte
) value
.GetLong();
556 // -----------------------------------------------------------------------
558 // -----------------------------------------------------------------------
560 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFloatProperty
,wxPGProperty
,
561 double,double,TextCtrl
)
563 wxFloatProperty::wxFloatProperty( const wxString
& label
,
564 const wxString
& name
,
566 : wxPGProperty(label
,name
)
572 wxFloatProperty::~wxFloatProperty() { }
574 // This helper method provides standard way for floating point-using
575 // properties to convert values to string.
576 void wxPropertyGrid::DoubleToString(wxString
& target
,
580 wxString
* precTemplate
)
582 if ( precision
>= 0 )
586 precTemplate
= &text1
;
588 if ( !precTemplate
->length() )
590 *precTemplate
= wxS("%.");
591 *precTemplate
<< wxString::Format( wxS("%i"), precision
);
592 *precTemplate
<< wxS('f');
595 target
.Printf( precTemplate
->c_str(), value
);
599 target
.Printf( wxS("%f"), value
);
602 if ( removeZeroes
&& precision
!= 0 && target
.length() )
604 // Remove excess zeroes (do not remove this code just yet,
605 // since sprintf can't do the same consistently across platforms).
606 wxString::const_iterator i
= target
.end() - 1;
607 size_t new_len
= target
.length() - 1;
609 for ( ; i
!= target
.begin(); --i
)
611 if ( *i
!= wxS('0') )
616 wxChar cur_char
= *i
;
617 if ( cur_char
!= wxS('.') && cur_char
!= wxS(',') )
620 if ( new_len
!= target
.length() )
621 target
.resize(new_len
);
625 wxString
wxFloatProperty::ValueToString( wxVariant
& value
,
629 if ( !value
.IsNull() )
631 wxPropertyGrid::DoubleToString(text
,
634 !(argFlags
& wxPG_FULL_VALUE
),
640 bool wxFloatProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
645 if ( text
.length() == 0 )
651 bool res
= text
.ToDouble(&value
);
654 if ( variant
!= value
)
660 else if ( argFlags
& wxPG_REPORT_ERROR
)
666 bool wxFloatProperty::DoValidation( const wxPGProperty
* property
, double& value
, wxPGValidationInfo
* pValidationInfo
, int mode
)
669 double min
= (double)wxINT64_MIN
;
670 double max
= (double)wxINT64_MAX
;
675 variant
= property
->GetAttribute(wxPGGlobalVars
->m_strMin
);
676 if ( !variant
.IsNull() )
678 wxPGVariantToDouble(variant
, &min
);
682 variant
= property
->GetAttribute(wxPGGlobalVars
->m_strMax
);
683 if ( !variant
.IsNull() )
685 wxPGVariantToDouble(variant
, &max
);
693 if ( mode
== wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
)
694 pValidationInfo
->SetFailureMessage(
695 wxString::Format(_("Value must be %f or higher"),min
)
697 else if ( mode
== wxPG_PROPERTY_VALIDATION_SATURATE
)
700 value
= max
- (min
- value
);
707 wxPGVariantToDouble(variant
, &max
);
710 if ( mode
== wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
)
711 pValidationInfo
->SetFailureMessage(
712 wxString::Format(_("Value must be %f or less"),max
)
714 else if ( mode
== wxPG_PROPERTY_VALIDATION_SATURATE
)
717 value
= min
+ (value
- max
);
724 bool wxFloatProperty::ValidateValue( wxVariant
& value
, wxPGValidationInfo
& validationInfo
) const
727 if ( wxPGVariantToDouble(value
, &fpv
) )
728 return DoValidation(this, fpv
, &validationInfo
, wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE
);
732 bool wxFloatProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
734 if ( name
== wxPG_FLOAT_PRECISION
)
736 m_precision
= value
.GetLong();
742 wxValidator
* wxFloatProperty::DoGetValidator() const
744 return wxIntProperty::GetClassValidator();
747 // -----------------------------------------------------------------------
749 // -----------------------------------------------------------------------
751 // We cannot use standard WX_PG_IMPLEMENT_PROPERTY_CLASS macro, since
752 // there is a custom GetEditorClass.
754 IMPLEMENT_DYNAMIC_CLASS(wxBoolProperty
, wxPGProperty
)
756 const wxPGEditor
* wxBoolProperty::DoGetEditorClass() const
758 // Select correct editor control.
759 #if wxPG_INCLUDE_CHECKBOX
760 if ( !(m_flags
& wxPG_PROP_USE_CHECKBOX
) )
761 return wxPGEditor_Choice
;
762 return wxPGEditor_CheckBox
;
764 return wxPGEditor_Choice
;
768 wxBoolProperty::wxBoolProperty( const wxString
& label
, const wxString
& name
, bool value
) :
769 wxPGProperty(label
,name
)
771 m_choices
.Assign(wxPGGlobalVars
->m_boolChoices
);
773 SetValue(wxPGVariant_Bool(value
));
775 m_flags
|= wxPG_PROP_USE_DCC
;
778 wxBoolProperty::~wxBoolProperty() { }
780 wxString
wxBoolProperty::ValueToString( wxVariant
& value
,
783 bool boolValue
= value
.GetBool();
785 // As a fragment of composite string value,
786 // make it a little more readable.
787 if ( argFlags
& wxPG_COMPOSITE_FRAGMENT
)
795 if ( argFlags
& wxPG_UNEDITABLE_COMPOSITE_FRAGMENT
)
796 return wxEmptyString
;
799 if ( wxPGGlobalVars
->m_autoGetTranslation
)
800 notFmt
= _("Not %s");
802 notFmt
= wxS("Not %s");
804 return wxString::Format(notFmt
.c_str(), m_label
.c_str());
808 if ( !(argFlags
& wxPG_FULL_VALUE
) )
810 return wxPGGlobalVars
->m_boolChoices
[boolValue
?1:0].GetText();
815 if ( boolValue
) text
= wxS("true");
816 else text
= wxS("false");
821 bool wxBoolProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int WXUNUSED(argFlags
) ) const
823 bool boolValue
= false;
824 if ( text
.CmpNoCase(wxPGGlobalVars
->m_boolChoices
[1].GetText()) == 0 ||
825 text
.CmpNoCase(wxS("true")) == 0 ||
826 text
.CmpNoCase(m_label
) == 0 )
829 if ( text
.length() == 0 )
835 if ( variant
!= boolValue
)
837 variant
= wxPGVariant_Bool(boolValue
);
843 bool wxBoolProperty::IntToValue( wxVariant
& variant
, int value
, int ) const
845 bool boolValue
= value
? true : false;
847 if ( variant
!= boolValue
)
849 variant
= wxPGVariant_Bool(boolValue
);
855 bool wxBoolProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
857 #if wxPG_INCLUDE_CHECKBOX
858 if ( name
== wxPG_BOOL_USE_CHECKBOX
)
860 int ival
= wxPGVariantToInt(value
);
862 m_flags
|= wxPG_PROP_USE_CHECKBOX
;
864 m_flags
&= ~(wxPG_PROP_USE_CHECKBOX
);
868 if ( name
== wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING
)
870 int ival
= wxPGVariantToInt(value
);
872 m_flags
|= wxPG_PROP_USE_DCC
;
874 m_flags
&= ~(wxPG_PROP_USE_DCC
);
880 // -----------------------------------------------------------------------
882 // -----------------------------------------------------------------------
884 IMPLEMENT_DYNAMIC_CLASS(wxEnumProperty
, wxPGProperty
)
886 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEnumProperty
,long,Choice
)
888 wxEnumProperty::wxEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
** labels
,
889 const long* values
, int value
) : wxPGProperty(label
,name
)
895 m_choices
.Add(labels
,values
);
897 if ( GetItemCount() )
898 SetValue( (long)value
);
902 wxEnumProperty::wxEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
** labels
,
903 const long* values
, wxPGChoices
* choicesCache
, int value
)
904 : wxPGProperty(label
,name
)
908 wxASSERT( choicesCache
);
910 if ( choicesCache
->IsOk() )
912 m_choices
.Assign( *choicesCache
);
913 m_value
= wxPGVariant_Zero
;
917 m_choices
.Add(labels
,values
);
919 if ( GetItemCount() )
920 SetValue( (long)value
);
924 wxEnumProperty::wxEnumProperty( const wxString
& label
, const wxString
& name
,
925 const wxArrayString
& labels
, const wxArrayInt
& values
, int value
)
926 : wxPGProperty(label
,name
)
930 if ( &labels
&& labels
.size() )
932 m_choices
.Set(labels
, values
);
934 if ( GetItemCount() )
935 SetValue( (long)value
);
939 wxEnumProperty::wxEnumProperty( const wxString
& label
, const wxString
& name
,
940 wxPGChoices
& choices
, int value
)
941 : wxPGProperty(label
,name
)
943 m_choices
.Assign( choices
);
945 if ( GetItemCount() )
946 SetValue( (long)value
);
949 int wxEnumProperty::GetIndexForValue( int value
) const
951 if ( !m_choices
.IsOk() )
954 int intVal
= m_choices
.Index(value
);
961 wxEnumProperty::~wxEnumProperty ()
965 int wxEnumProperty::ms_nextIndex
= -2;
966 int wxEnumProperty::ms_prevIndex
= -1;
968 void wxEnumProperty::OnSetValue()
970 wxString variantType
= m_value
.GetType();
972 if ( variantType
== wxPG_VARIANT_TYPE_LONG
)
973 ValueFromInt_( m_value
, m_value
.GetLong(), wxPG_FULL_VALUE
);
974 else if ( variantType
== wxPG_VARIANT_TYPE_STRING
)
975 ValueFromString_( m_value
, m_value
.GetString(), 0 );
979 if ( ms_nextIndex
!= -2 )
981 m_index
= ms_nextIndex
;
986 bool wxEnumProperty::ValidateValue( wxVariant
& value
, wxPGValidationInfo
& WXUNUSED(validationInfo
) ) const
988 // Make sure string value is in the list,
989 // unless property has string as preferred value type
990 // To reduce code size, use conversion here as well
991 if ( value
.GetType() == wxPG_VARIANT_TYPE_STRING
&&
992 !this->IsKindOf(CLASSINFO(wxEditEnumProperty
)) )
993 return ValueFromString_( value
, value
.GetString(), wxPG_PROPERTY_SPECIFIC
);
998 wxString
wxEnumProperty::ValueToString( wxVariant
& value
,
999 int WXUNUSED(argFlags
) ) const
1001 if ( value
.GetType() == wxPG_VARIANT_TYPE_STRING
)
1002 return value
.GetString();
1004 int index
= m_choices
.Index(value
.GetLong());
1006 return wxEmptyString
;
1008 return m_choices
.GetLabel(index
);
1011 bool wxEnumProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
1013 return ValueFromString_( variant
, text
, argFlags
);
1016 bool wxEnumProperty::IntToValue( wxVariant
& variant
, int intVal
, int argFlags
) const
1018 return ValueFromInt_( variant
, intVal
, argFlags
);
1021 bool wxEnumProperty::ValueFromString_( wxVariant
& value
, const wxString
& text
, int argFlags
) const
1026 for ( unsigned int i
=0; i
<m_choices
.GetCount(); i
++ )
1028 const wxString
& entryLabel
= m_choices
.GetLabel(i
);
1029 if ( text
.CmpNoCase(entryLabel
) == 0 )
1032 useValue
= m_choices
.GetValue(i
);
1037 bool asText
= false;
1039 bool isEdit
= this->IsKindOf(CLASSINFO(wxEditEnumProperty
));
1041 // If text not any of the choices, store as text instead
1042 // (but only if we are wxEditEnumProperty)
1043 if ( useIndex
== -1 &&
1044 (value
.GetType() != wxPG_VARIANT_TYPE_STRING
|| (m_value
.GetString() != text
)) &&
1050 int setAsNextIndex
= -2;
1054 setAsNextIndex
= -1;
1057 else if ( m_index
!= useIndex
)
1059 if ( useIndex
!= -1 )
1061 setAsNextIndex
= useIndex
;
1062 value
= (long)useValue
;
1066 setAsNextIndex
= -1;
1067 value
= wxPGVariant_MinusOne
;
1071 if ( setAsNextIndex
!= -2 )
1073 // If wxPG_PROPERTY_SPECIFIC is set, then this is done for
1074 // validation purposes only, and index must not be changed
1075 if ( !(argFlags
& wxPG_PROPERTY_SPECIFIC
) )
1076 ms_nextIndex
= setAsNextIndex
;
1078 if ( isEdit
|| setAsNextIndex
!= -1 )
1086 bool wxEnumProperty::ValueFromInt_( wxVariant
& variant
, int intVal
, int argFlags
) const
1088 // If wxPG_FULL_VALUE is *not* in argFlags, then intVal is index from combo box.
1092 if ( argFlags
& wxPG_FULL_VALUE
)
1094 ms_nextIndex
= GetIndexForValue( intVal
);
1098 if ( m_index
!= intVal
)
1100 ms_nextIndex
= intVal
;
1104 if ( ms_nextIndex
!= -2 )
1106 if ( !(argFlags
& wxPG_FULL_VALUE
) )
1107 intVal
= m_choices
.GetValue(intVal
);
1109 variant
= (long)intVal
;
1118 wxEnumProperty::OnValidationFailure( wxVariant
& WXUNUSED(pendingValue
) )
1121 m_index
= ms_prevIndex
;
1125 void wxEnumProperty::SetIndex( int index
)
1127 ms_prevIndex
= m_index
;
1132 int wxEnumProperty::GetIndex() const
1134 if ( ms_nextIndex
!= -2 )
1135 return ms_nextIndex
;
1139 // -----------------------------------------------------------------------
1140 // wxEditEnumProperty
1141 // -----------------------------------------------------------------------
1143 IMPLEMENT_DYNAMIC_CLASS(wxEditEnumProperty
, wxPGProperty
)
1145 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEditEnumProperty
,wxString
,ComboBox
)
1147 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
** labels
,
1148 const long* values
, const wxString
& value
)
1149 : wxEnumProperty(label
,name
,labels
,values
,0)
1154 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
** labels
,
1155 const long* values
, wxPGChoices
* choicesCache
, const wxString
& value
)
1156 : wxEnumProperty(label
,name
,labels
,values
,choicesCache
,0)
1161 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
,
1162 const wxArrayString
& labels
, const wxArrayInt
& values
, const wxString
& value
)
1163 : wxEnumProperty(label
,name
,labels
,values
,0)
1168 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
,
1169 wxPGChoices
& choices
, const wxString
& value
)
1170 : wxEnumProperty(label
,name
,choices
,0)
1175 wxEditEnumProperty::~wxEditEnumProperty()
1179 // -----------------------------------------------------------------------
1181 // -----------------------------------------------------------------------
1183 IMPLEMENT_DYNAMIC_CLASS(wxFlagsProperty
,wxPGProperty
)
1185 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxFlagsProperty
,long,TextCtrl
)
1187 void wxFlagsProperty::Init()
1189 SetParentalType(wxPG_PROP_AGGREGATE
);
1191 long value
= m_value
;
1194 // Generate children
1198 unsigned int prevChildCount
= m_children
.size();
1201 if ( prevChildCount
)
1203 wxPropertyGridPageState
* state
= GetParentState();
1205 // State safety check (it may be NULL in immediate parent)
1210 wxPGProperty
* selected
= state
->GetSelection();
1213 if ( selected
->GetParent() == this )
1214 oldSel
= selected
->GetIndexInParent();
1215 else if ( selected
== this )
1219 state
->DoClearSelection();
1222 // Delete old children
1223 for ( i
=0; i
<prevChildCount
; i
++ )
1224 delete m_children
[i
];
1228 if ( m_choices
.IsOk() )
1230 const wxPGChoices
& choices
= m_choices
;
1232 for ( i
=0; i
<GetItemCount(); i
++ )
1235 child_val
= ( value
& choices
.GetValue(i
) )?true:false;
1237 wxPGProperty
* boolProp
;
1238 wxString label
= GetLabel(i
);
1241 if ( wxPGGlobalVars
->m_autoGetTranslation
)
1243 boolProp
= new wxBoolProperty( ::wxGetTranslation(label
), label
, child_val
);
1248 boolProp
= new wxBoolProperty( label
, label
, child_val
);
1253 m_oldChoicesData
= m_choices
.GetDataPtr();
1256 m_oldValue
= m_value
;
1258 if ( prevChildCount
)
1259 SubPropsChanged(oldSel
);
1262 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1263 const wxChar
** labels
, const long* values
, long value
) : wxPGProperty(label
,name
)
1265 m_oldChoicesData
= (wxPGChoicesData
*) NULL
;
1269 m_choices
.Set(labels
,values
);
1271 wxASSERT( GetItemCount() );
1277 m_value
= wxPGVariant_Zero
;
1281 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1282 const wxArrayString
& labels
, const wxArrayInt
& values
, int value
)
1283 : wxPGProperty(label
,name
)
1285 m_oldChoicesData
= (wxPGChoicesData
*) NULL
;
1287 if ( &labels
&& labels
.size() )
1289 m_choices
.Set(labels
,values
);
1291 wxASSERT( GetItemCount() );
1293 SetValue( (long)value
);
1297 m_value
= wxPGVariant_Zero
;
1301 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1302 wxPGChoices
& choices
, long value
)
1303 : wxPGProperty(label
,name
)
1305 m_oldChoicesData
= (wxPGChoicesData
*) NULL
;
1307 if ( choices
.IsOk() )
1309 m_choices
.Assign(choices
);
1311 wxASSERT( GetItemCount() );
1317 m_value
= wxPGVariant_Zero
;
1321 wxFlagsProperty::~wxFlagsProperty()
1325 void wxFlagsProperty::OnSetValue()
1327 if ( !m_choices
.IsOk() || !GetItemCount() )
1329 m_value
= wxPGVariant_Zero
;
1333 long val
= m_value
.GetLong();
1337 // normalize the value (i.e. remove extra flags)
1339 const wxPGChoices
& choices
= m_choices
;
1340 for ( i
= 0; i
< GetItemCount(); i
++ )
1342 fullFlags
|= choices
.GetValue(i
);
1349 // Need to (re)init now?
1350 if ( GetChildCount() != GetItemCount() ||
1351 m_choices
.GetDataPtr() != m_oldChoicesData
)
1357 long newFlags
= m_value
;
1359 if ( newFlags
!= m_oldValue
)
1361 // Set child modified states
1363 const wxPGChoices
& choices
= m_choices
;
1364 for ( i
= 0; i
<GetItemCount(); i
++ )
1368 flag
= choices
.GetValue(i
);
1370 if ( (newFlags
& flag
) != (m_oldValue
& flag
) )
1371 Item(i
)->SetFlag( wxPG_PROP_MODIFIED
);
1374 m_oldValue
= newFlags
;
1378 wxString
wxFlagsProperty::ValueToString( wxVariant
& value
,
1379 int WXUNUSED(argFlags
) ) const
1383 if ( !m_choices
.IsOk() )
1388 const wxPGChoices
& choices
= m_choices
;
1390 for ( i
= 0; i
< GetItemCount(); i
++ )
1393 doAdd
= ( flags
& choices
.GetValue(i
) );
1397 text
+= choices
.GetLabel(i
);
1402 // remove last comma
1403 if ( text
.Len() > 1 )
1404 text
.Truncate ( text
.Len() - 2 );
1409 // Translate string into flag tokens
1410 bool wxFlagsProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
1412 if ( !m_choices
.IsOk() )
1417 // semicolons are no longer valid delimeters
1418 WX_PG_TOKENIZER1_BEGIN(text
,wxS(','))
1420 if ( token
.length() )
1422 // Determine which one it is
1423 long bit
= IdToBit( token
);
1436 WX_PG_TOKENIZER1_END()
1438 if ( variant
!= (long)newFlags
)
1440 variant
= (long)newFlags
;
1447 // Converts string id to a relevant bit.
1448 long wxFlagsProperty::IdToBit( const wxString
& id
) const
1451 for ( i
= 0; i
< GetItemCount(); i
++ )
1453 if ( id
== GetLabel(i
) )
1455 return m_choices
.GetValue(i
);
1461 void wxFlagsProperty::RefreshChildren()
1463 if ( !m_choices
.IsOk() || !GetChildCount() ) return;
1465 int flags
= m_value
.GetLong();
1467 const wxPGChoices
& choices
= m_choices
;
1469 for ( i
= 0; i
< GetItemCount(); i
++ )
1473 flag
= choices
.GetValue(i
);
1475 long subVal
= flags
& flag
;
1476 wxPGProperty
* p
= Item(i
);
1478 if ( subVal
!= (m_oldValue
& flag
) )
1479 p
->SetFlag( wxPG_PROP_MODIFIED
);
1481 p
->SetValue( subVal
?true:false );
1487 void wxFlagsProperty::ChildChanged( wxVariant
& thisValue
, int childIndex
, wxVariant
& childValue
) const
1489 long oldValue
= thisValue
.GetLong();
1490 long val
= childValue
.GetLong();
1491 unsigned long vi
= m_choices
.GetValue(childIndex
);
1493 thisValue
= (long)(oldValue
| vi
);
1495 thisValue
= (long)(oldValue
& ~(vi
));
1498 // -----------------------------------------------------------------------
1500 // -----------------------------------------------------------------------
1502 IMPLEMENT_DYNAMIC_CLASS(wxDirProperty
, wxLongStringProperty
)
1504 wxDirProperty::wxDirProperty( const wxString
& name
, const wxString
& label
, const wxString
& value
)
1505 : wxLongStringProperty(name
,label
,value
)
1507 m_flags
|= wxPG_PROP_NO_ESCAPE
;
1510 wxDirProperty::~wxDirProperty() { }
1512 wxValidator
* wxDirProperty::DoGetValidator() const
1514 return wxFileProperty::GetClassValidator();
1517 bool wxDirProperty::OnButtonClick( wxPropertyGrid
* propGrid
, wxString
& value
)
1519 // Update property value from editor, if necessary
1520 wxSize
dlg_sz(300,400);
1522 wxString
dlgMessage(m_dlgMessage
);
1523 if ( dlgMessage
.empty() )
1524 dlgMessage
= _("Choose a directory:");
1525 wxDirDialog
dlg( propGrid
,
1529 #if !wxPG_SMALL_SCREEN
1530 propGrid
->GetGoodEditorDialogPosition(this,dlg_sz
),
1538 if ( dlg
.ShowModal() == wxID_OK
)
1540 value
= dlg
.GetPath();
1546 bool wxDirProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
1548 if ( name
== wxPG_DIR_DIALOG_MESSAGE
)
1550 m_dlgMessage
= value
.GetString();
1556 // -----------------------------------------------------------------------
1557 // wxPGFileDialogAdapter
1558 // -----------------------------------------------------------------------
1560 bool wxPGFileDialogAdapter::DoShowDialog( wxPropertyGrid
* propGrid
, wxPGProperty
* property
)
1562 wxFileProperty
* fileProp
= NULL
;
1566 if ( property
->IsKindOf(CLASSINFO(wxFileProperty
)) )
1568 fileProp
= ((wxFileProperty
*)property
);
1569 wxFileName filename
= fileProp
->GetValue().GetString();
1570 path
= filename
.GetPath();
1571 indFilter
= fileProp
->m_indFilter
;
1573 if ( !path
.length() && fileProp
->m_basePath
.length() )
1574 path
= fileProp
->m_basePath
;
1578 wxFileName
fn(property
->GetValue().GetString());
1579 path
= fn
.GetPath();
1582 wxFileDialog
dlg( propGrid
->GetPanel(),
1583 property
->GetAttribute(wxS("DialogTitle"), _("Choose a file")),
1584 property
->GetAttribute(wxS("InitialPath"), path
),
1586 property
->GetAttribute(wxPG_FILE_WILDCARD
, _("All files (*.*)|*.*")),
1588 wxDefaultPosition
);
1590 if ( indFilter
>= 0 )
1591 dlg
.SetFilterIndex( indFilter
);
1593 if ( dlg
.ShowModal() == wxID_OK
)
1596 fileProp
->m_indFilter
= dlg
.GetFilterIndex();
1597 SetValue( dlg
.GetPath() );
1603 // -----------------------------------------------------------------------
1605 // -----------------------------------------------------------------------
1607 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFileProperty
,wxPGProperty
,
1608 wxString
,const wxString
&,TextCtrlAndButton
)
1610 wxFileProperty::wxFileProperty( const wxString
& label
, const wxString
& name
,
1611 const wxString
& value
) : wxPGProperty(label
,name
)
1613 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1615 SetAttribute( wxPG_FILE_WILDCARD
, _("All files (*.*)|*.*") );
1620 wxFileProperty::~wxFileProperty() {}
1622 #if wxUSE_VALIDATORS
1624 wxValidator
* wxFileProperty::GetClassValidator()
1626 WX_PG_DOGETVALIDATOR_ENTRY()
1628 // Atleast wxPython 2.6.2.1 required that the string argument is given
1630 wxTextValidator
* validator
= new wxTextValidator(wxFILTER_EXCLUDE_CHAR_LIST
,&v
);
1632 wxArrayString exChars
;
1633 exChars
.Add(wxS("?"));
1634 exChars
.Add(wxS("*"));
1635 exChars
.Add(wxS("|"));
1636 exChars
.Add(wxS("<"));
1637 exChars
.Add(wxS(">"));
1638 exChars
.Add(wxS("\""));
1640 validator
->SetExcludes(exChars
);
1642 WX_PG_DOGETVALIDATOR_EXIT(validator
)
1645 wxValidator
* wxFileProperty::DoGetValidator() const
1647 return GetClassValidator();
1652 void wxFileProperty::OnSetValue()
1654 const wxString
& fnstr
= m_value
.GetString();
1656 wxFileName filename
= fnstr
;
1658 if ( !filename
.HasName() )
1660 m_value
= wxPGVariant_EmptyString
;
1663 // Find index for extension.
1664 if ( m_indFilter
< 0 && fnstr
.length() )
1666 wxString ext
= filename
.GetExt();
1669 size_t len
= m_wildcard
.length();
1671 pos
= m_wildcard
.find(wxS("|"), pos
);
1672 while ( pos
!= wxString::npos
&& pos
< (len
-3) )
1674 size_t ext_begin
= pos
+ 3;
1676 pos
= m_wildcard
.find(wxS("|"), ext_begin
);
1677 if ( pos
== wxString::npos
)
1679 wxString found_ext
= m_wildcard
.substr(ext_begin
, pos
-ext_begin
);
1681 if ( found_ext
.length() > 0 )
1683 if ( found_ext
[0] == wxS('*') )
1685 m_indFilter
= curind
;
1688 if ( ext
.CmpNoCase(found_ext
) == 0 )
1690 m_indFilter
= curind
;
1696 pos
= m_wildcard
.find(wxS("|"), pos
+1);
1703 wxFileName
wxFileProperty::GetFileName() const
1705 wxFileName filename
;
1707 if ( !m_value
.IsNull() )
1708 filename
= m_value
.GetString();
1713 wxString
wxFileProperty::ValueToString( wxVariant
& value
,
1714 int argFlags
) const
1716 wxFileName filename
= value
.GetString();
1718 if ( !filename
.HasName() )
1719 return wxEmptyString
;
1721 wxString fullName
= filename
.GetFullName();
1722 if ( !fullName
.length() )
1723 return wxEmptyString
;
1725 if ( argFlags
& wxPG_FULL_VALUE
)
1727 return filename
.GetFullPath();
1729 else if ( m_flags
& wxPG_PROP_SHOW_FULL_FILENAME
)
1731 if ( m_basePath
.Length() )
1733 wxFileName
fn2(filename
);
1734 fn2
.MakeRelativeTo(m_basePath
);
1735 return fn2
.GetFullPath();
1737 return filename
.GetFullPath();
1740 return filename
.GetFullName();
1743 wxPGEditorDialogAdapter
* wxFileProperty::GetEditorDialog() const
1745 return new wxPGFileDialogAdapter();
1748 bool wxFileProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
1750 wxFileName filename
= variant
.GetString();
1752 if ( (m_flags
& wxPG_PROP_SHOW_FULL_FILENAME
) || (argFlags
& wxPG_FULL_VALUE
) )
1754 if ( filename
!= text
)
1762 if ( filename
.GetFullName() != text
)
1764 wxFileName fn
= filename
;
1765 fn
.SetFullName(text
);
1766 variant
= fn
.GetFullPath();
1774 bool wxFileProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
1776 // Return false on some occasions to make sure those attribs will get
1777 // stored in m_attributes.
1778 if ( name
== wxPG_FILE_SHOW_FULL_PATH
)
1780 if ( wxPGVariantToInt(value
) )
1781 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1783 m_flags
&= ~(wxPG_PROP_SHOW_FULL_FILENAME
);
1786 else if ( name
== wxPG_FILE_WILDCARD
)
1788 m_wildcard
= value
.GetString();
1790 else if ( name
== wxPG_FILE_SHOW_RELATIVE_PATH
)
1792 m_basePath
= value
.GetString();
1794 // Make sure wxPG_FILE_SHOW_FULL_PATH is also set
1795 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1797 else if ( name
== wxPG_FILE_INITIAL_PATH
)
1799 m_initialPath
= value
.GetString();
1802 else if ( name
== wxPG_FILE_DIALOG_TITLE
)
1804 m_dlgTitle
= value
.GetString();
1810 // -----------------------------------------------------------------------
1811 // wxPGLongStringDialogAdapter
1812 // -----------------------------------------------------------------------
1814 bool wxPGLongStringDialogAdapter::DoShowDialog( wxPropertyGrid
* propGrid
, wxPGProperty
* property
)
1816 wxString val1
= property
->GetValueAsString(0);
1817 wxString val_orig
= val1
;
1820 if ( !property
->HasFlag(wxPG_PROP_NO_ESCAPE
) )
1821 wxPropertyGrid::ExpandEscapeSequences(value
, val1
);
1823 value
= wxString(val1
);
1825 // Run editor dialog.
1826 if ( wxLongStringProperty::DisplayEditorDialog(property
, propGrid
, value
) )
1828 if ( !property
->HasFlag(wxPG_PROP_NO_ESCAPE
) )
1829 wxPropertyGrid::CreateEscapeSequences(val1
,value
);
1833 if ( val1
!= val_orig
)
1842 // -----------------------------------------------------------------------
1843 // wxLongStringProperty
1844 // -----------------------------------------------------------------------
1846 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxLongStringProperty
,wxPGProperty
,
1847 wxString
,const wxString
&,TextCtrlAndButton
)
1849 wxLongStringProperty::wxLongStringProperty( const wxString
& label
, const wxString
& name
,
1850 const wxString
& value
) : wxPGProperty(label
,name
)
1855 wxLongStringProperty::~wxLongStringProperty() {}
1857 wxString
wxLongStringProperty::ValueToString( wxVariant
& value
,
1858 int WXUNUSED(argFlags
) ) const
1863 bool wxLongStringProperty::OnEvent( wxPropertyGrid
* propGrid
, wxWindow
* WXUNUSED(primary
),
1866 if ( propGrid
->IsMainButtonEvent(event
) )
1869 wxVariant useValue
= propGrid
->GetUncommittedPropertyValue();
1871 wxString val1
= useValue
.GetString();
1872 wxString val_orig
= val1
;
1875 if ( !(m_flags
& wxPG_PROP_NO_ESCAPE
) )
1876 wxPropertyGrid::ExpandEscapeSequences(value
,val1
);
1878 value
= wxString(val1
);
1880 // Run editor dialog.
1881 if ( OnButtonClick(propGrid
,value
) )
1883 if ( !(m_flags
& wxPG_PROP_NO_ESCAPE
) )
1884 wxPropertyGrid::CreateEscapeSequences(val1
,value
);
1888 if ( val1
!= val_orig
)
1890 SetValueInEvent( val1
);
1898 bool wxLongStringProperty::OnButtonClick( wxPropertyGrid
* propGrid
, wxString
& value
)
1900 return DisplayEditorDialog(this, propGrid
, value
);
1903 bool wxLongStringProperty::DisplayEditorDialog( wxPGProperty
* prop
, wxPropertyGrid
* propGrid
, wxString
& value
)
1906 // launch editor dialog
1907 wxDialog
* dlg
= new wxDialog(propGrid
,-1,prop
->GetLabel(),wxDefaultPosition
,wxDefaultSize
,
1908 wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
|wxCLIP_CHILDREN
);
1910 dlg
->SetFont(propGrid
->GetFont()); // To allow entering chars of the same set as the propGrid
1912 // Multi-line text editor dialog.
1913 #if !wxPG_SMALL_SCREEN
1914 const int spacing
= 8;
1916 const int spacing
= 4;
1918 wxBoxSizer
* topsizer
= new wxBoxSizer( wxVERTICAL
);
1919 wxBoxSizer
* rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
1920 wxTextCtrl
* ed
= new wxTextCtrl(dlg
,11,value
,
1921 wxDefaultPosition
,wxDefaultSize
,wxTE_MULTILINE
);
1923 rowsizer
->Add( ed
, 1, wxEXPAND
|wxALL
, spacing
);
1924 topsizer
->Add( rowsizer
, 1, wxEXPAND
, 0 );
1925 rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
1926 const int but_sz_flags
=
1927 wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
|wxBOTTOM
|wxLEFT
|wxRIGHT
;
1928 rowsizer
->Add( new wxButton(dlg
,wxID_OK
,_("Ok")),
1929 0, but_sz_flags
, spacing
);
1930 rowsizer
->Add( new wxButton(dlg
,wxID_CANCEL
,_("Cancel")),
1931 0, but_sz_flags
, spacing
);
1932 topsizer
->Add( rowsizer
, 0, wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
, 0 );
1934 dlg
->SetSizer( topsizer
);
1935 topsizer
->SetSizeHints( dlg
);
1937 #if !wxPG_SMALL_SCREEN
1938 dlg
->SetSize(400,300);
1940 dlg
->Move( propGrid
->GetGoodEditorDialogPosition(prop
,dlg
->GetSize()) );
1943 int res
= dlg
->ShowModal();
1945 if ( res
== wxID_OK
)
1947 value
= ed
->GetValue();
1955 bool wxLongStringProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
1957 if ( variant
!= text
)
1965 // -----------------------------------------------------------------------
1966 // wxArrayEditorDialog
1967 // -----------------------------------------------------------------------
1969 BEGIN_EVENT_TABLE(wxArrayEditorDialog
, wxDialog
)
1970 EVT_IDLE(wxArrayEditorDialog::OnIdle
)
1971 EVT_LISTBOX(24, wxArrayEditorDialog::OnListBoxClick
)
1972 EVT_TEXT_ENTER(21, wxArrayEditorDialog::OnAddClick
)
1973 EVT_BUTTON(22, wxArrayEditorDialog::OnAddClick
)
1974 EVT_BUTTON(23, wxArrayEditorDialog::OnDeleteClick
)
1975 EVT_BUTTON(25, wxArrayEditorDialog::OnUpClick
)
1976 EVT_BUTTON(26, wxArrayEditorDialog::OnDownClick
)
1977 EVT_BUTTON(27, wxArrayEditorDialog::OnUpdateClick
)
1978 //EVT_BUTTON(28, wxArrayEditorDialog::OnCustomEditClick)
1981 IMPLEMENT_ABSTRACT_CLASS(wxArrayEditorDialog
, wxDialog
)
1983 #include "wx/statline.h"
1985 // -----------------------------------------------------------------------
1987 void wxArrayEditorDialog::OnIdle(wxIdleEvent
& event
)
1990 // Do control focus detection here.
1993 wxWindow
* focused
= FindFocus();
1995 // This strange focus thing is a workaround for wxGTK wxListBox focus
1997 if ( m_curFocus
== 0 && focused
!= m_edValue
&&
1998 focused
!= m_butAdd
&& focused
!= m_butUpdate
&&
1999 m_lbStrings
->GetSelection() >= 0 )
2001 // ListBox was just focused.
2002 m_butAdd
->Enable(false);
2003 m_butUpdate
->Enable(false);
2004 m_butRemove
->Enable(true);
2005 m_butUp
->Enable(true);
2006 m_butDown
->Enable(true);
2009 else if ( (m_curFocus
== 1 && focused
== m_edValue
) /*|| m_curFocus == 2*/ )
2011 // TextCtrl was just focused.
2012 m_butAdd
->Enable(true);
2013 bool upd_enable
= false;
2014 if ( m_lbStrings
->GetCount() && m_lbStrings
->GetSelection() >= 0 )
2016 m_butUpdate
->Enable(upd_enable
);
2017 m_butRemove
->Enable(false);
2018 m_butUp
->Enable(false);
2019 m_butDown
->Enable(false);
2026 // -----------------------------------------------------------------------
2028 wxArrayEditorDialog::wxArrayEditorDialog()
2034 // -----------------------------------------------------------------------
2036 void wxArrayEditorDialog::Init()
2038 m_custBtText
= (const wxChar
*) NULL
;
2041 // -----------------------------------------------------------------------
2043 wxArrayEditorDialog::wxArrayEditorDialog( wxWindow
*parent
,
2044 const wxString
& message
,
2045 const wxString
& caption
,
2052 Create(parent
,message
,caption
,style
,pos
,sz
);
2055 // -----------------------------------------------------------------------
2057 bool wxArrayEditorDialog::Create( wxWindow
*parent
,
2058 const wxString
& message
,
2059 const wxString
& caption
,
2064 // On wxMAC the dialog shows incorrectly if style is not exactly wxCAPTION
2065 // FIXME: This should be only a temporary fix.
2068 int useStyle
= wxCAPTION
;
2070 int useStyle
= style
;
2073 bool res
= wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, sz
, useStyle
);
2075 SetFont(parent
->GetFont()); // To allow entering chars of the same set as the propGrid
2077 #if !wxPG_SMALL_SCREEN
2078 const int spacing
= 4;
2080 const int spacing
= 3;
2087 const int but_sz_flags
=
2088 wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
|wxALL
; //wxBOTTOM|wxLEFT|wxRIGHT;
2090 wxBoxSizer
* topsizer
= new wxBoxSizer( wxVERTICAL
);
2093 if ( message
.length() )
2094 topsizer
->Add( new wxStaticText(this,-1,message
),
2095 0, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxALL
, spacing
);
2098 wxBoxSizer
* rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
2099 m_edValue
= new wxTextCtrl(this,21,wxEmptyString
,
2100 wxDefaultPosition
,wxDefaultSize
,wxTE_PROCESS_ENTER
);
2101 wxValidator
* validator
= GetTextCtrlValidator();
2104 m_edValue
->SetValidator( *validator
);
2107 rowsizer
->Add( m_edValue
,
2108 1, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxALL
, spacing
);
2111 m_butAdd
= new wxButton(this,22,_("Add"));
2112 rowsizer
->Add( m_butAdd
,
2113 0, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxTOP
|wxBOTTOM
|wxRIGHT
, spacing
);
2114 topsizer
->Add( rowsizer
, 0, wxEXPAND
, spacing
);
2117 topsizer
->Add( new wxStaticLine(this,-1),
2118 0, wxEXPAND
|wxBOTTOM
|wxLEFT
|wxRIGHT
, spacing
);
2120 rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
2123 m_lbStrings
= new wxListBox(this, 24, wxDefaultPosition
, wxDefaultSize
);
2125 for ( i
=0; i
<ArrayGetCount(); i
++ )
2126 m_lbStrings
->Append( ArrayGet(i
) );
2127 rowsizer
->Add( m_lbStrings
, 1, wxEXPAND
|wxRIGHT
, spacing
);
2129 // Manipulator buttons
2130 wxBoxSizer
* colsizer
= new wxBoxSizer( wxVERTICAL
);
2131 m_butCustom
= (wxButton
*) NULL
;
2134 m_butCustom
= new wxButton(this,28,::wxGetTranslation(m_custBtText
));
2135 colsizer
->Add( m_butCustom
,
2136 0, wxALIGN_CENTER
|wxTOP
/*wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT*/,
2139 m_butUpdate
= new wxButton(this,27,_("Update"));
2140 colsizer
->Add( m_butUpdate
,
2141 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2142 m_butRemove
= new wxButton(this,23,_("Remove"));
2143 colsizer
->Add( m_butRemove
,
2144 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2145 m_butUp
= new wxButton(this,25,_("Up"));
2146 colsizer
->Add( m_butUp
,
2147 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2148 m_butDown
= new wxButton(this,26,_("Down"));
2149 colsizer
->Add( m_butDown
,
2150 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2151 rowsizer
->Add( colsizer
, 0, 0, spacing
);
2153 topsizer
->Add( rowsizer
, 1, wxLEFT
|wxRIGHT
|wxEXPAND
, spacing
);
2156 topsizer
->Add( new wxStaticLine(this,-1),
2157 0, wxEXPAND
|wxTOP
|wxLEFT
|wxRIGHT
, spacing
);
2160 rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
2162 const int but_sz_flags =
2163 wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT;
2165 rowsizer
->Add( new wxButton(this,wxID_OK
,_("Ok")),
2166 0, but_sz_flags
, spacing
);
2167 rowsizer
->Add( new wxButton(this,wxID_CANCEL
,_("Cancel")),
2168 0, but_sz_flags
, spacing
);
2169 topsizer
->Add( rowsizer
, 0, wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
, 0 );
2171 m_edValue
->SetFocus();
2173 SetSizer( topsizer
);
2174 topsizer
->SetSizeHints( this );
2176 #if !wxPG_SMALL_SCREEN
2177 if ( sz
.x
== wxDefaultSize
.x
&&
2178 sz
.y
== wxDefaultSize
.y
)
2179 SetSize( wxSize(275,360) );
2187 // -----------------------------------------------------------------------
2189 void wxArrayEditorDialog::OnAddClick(wxCommandEvent
& )
2191 wxString text
= m_edValue
->GetValue();
2192 if ( text
.length() )
2194 if ( ArrayInsert( text
, -1 ) )
2196 m_lbStrings
->Append( text
);
2203 // -----------------------------------------------------------------------
2205 void wxArrayEditorDialog::OnDeleteClick(wxCommandEvent
& )
2207 int index
= m_lbStrings
->GetSelection();
2210 ArrayRemoveAt( index
);
2211 m_lbStrings
->Delete ( index
);
2216 // -----------------------------------------------------------------------
2218 void wxArrayEditorDialog::OnUpClick(wxCommandEvent
& )
2220 int index
= m_lbStrings
->GetSelection();
2223 ArraySwap(index
-1,index
);
2224 /*wxString old_str = m_array[index-1];
2225 wxString new_str = m_array[index];
2226 m_array[index-1] = new_str;
2227 m_array[index] = old_str;*/
2228 m_lbStrings
->SetString ( index
-1, ArrayGet(index
-1) );
2229 m_lbStrings
->SetString ( index
, ArrayGet(index
) );
2230 m_lbStrings
->SetSelection ( index
-1 );
2235 // -----------------------------------------------------------------------
2237 void wxArrayEditorDialog::OnDownClick(wxCommandEvent
& )
2239 int index
= m_lbStrings
->GetSelection();
2240 int lastStringIndex
= ((int) m_lbStrings
->GetCount()) - 1;
2241 if ( index
>= 0 && index
< lastStringIndex
)
2243 ArraySwap(index
,index
+1);
2244 /*wxString old_str = m_array[index+1];
2245 wxString new_str = m_array[index];
2246 m_array[index+1] = new_str;
2247 m_array[index] = old_str;*/
2248 m_lbStrings
->SetString ( index
+1, ArrayGet(index
+1) );
2249 m_lbStrings
->SetString ( index
, ArrayGet(index
) );
2250 m_lbStrings
->SetSelection ( index
+1 );
2255 // -----------------------------------------------------------------------
2257 void wxArrayEditorDialog::OnUpdateClick(wxCommandEvent
& )
2259 int index
= m_lbStrings
->GetSelection();
2262 wxString str
= m_edValue
->GetValue();
2263 if ( ArraySet(index
,str
) )
2265 m_lbStrings
->SetString ( index
, str
);
2266 //m_array[index] = str;
2272 // -----------------------------------------------------------------------
2274 void wxArrayEditorDialog::OnListBoxClick(wxCommandEvent
& )
2276 int index
= m_lbStrings
->GetSelection();
2279 m_edValue
->SetValue( m_lbStrings
->GetString(index
) );
2283 // -----------------------------------------------------------------------
2284 // wxPGArrayStringEditorDialog
2285 // -----------------------------------------------------------------------
2287 IMPLEMENT_DYNAMIC_CLASS(wxPGArrayStringEditorDialog
, wxArrayEditorDialog
)
2289 BEGIN_EVENT_TABLE(wxPGArrayStringEditorDialog
, wxArrayEditorDialog
)
2290 EVT_BUTTON(28, wxPGArrayStringEditorDialog::OnCustomEditClick
)
2293 // -----------------------------------------------------------------------
2295 wxString
wxPGArrayStringEditorDialog::ArrayGet( size_t index
)
2297 return m_array
[index
];
2300 size_t wxPGArrayStringEditorDialog::ArrayGetCount()
2302 return m_array
.size();
2305 bool wxPGArrayStringEditorDialog::ArrayInsert( const wxString
& str
, int index
)
2310 m_array
.Insert(str
,index
);
2314 bool wxPGArrayStringEditorDialog::ArraySet( size_t index
, const wxString
& str
)
2316 m_array
[index
] = str
;
2320 void wxPGArrayStringEditorDialog::ArrayRemoveAt( int index
)
2322 m_array
.RemoveAt(index
);
2325 void wxPGArrayStringEditorDialog::ArraySwap( size_t first
, size_t second
)
2327 wxString old_str
= m_array
[first
];
2328 wxString new_str
= m_array
[second
];
2329 m_array
[first
] = new_str
;
2330 m_array
[second
] = old_str
;
2333 wxPGArrayStringEditorDialog::wxPGArrayStringEditorDialog()
2334 : wxArrayEditorDialog()
2339 void wxPGArrayStringEditorDialog::Init()
2341 m_pCallingClass
= (wxArrayStringProperty
*) NULL
;
2344 void wxPGArrayStringEditorDialog::OnCustomEditClick(wxCommandEvent
& )
2346 wxASSERT( m_pCallingClass
);
2347 wxString str
= m_edValue
->GetValue();
2348 if ( m_pCallingClass
->OnCustomStringEdit(m_parent
,str
) )
2350 //m_edValue->SetValue ( str );
2351 m_lbStrings
->Append ( str
);
2352 m_array
.Add ( str
);
2357 // -----------------------------------------------------------------------
2358 // wxArrayStringProperty
2359 // -----------------------------------------------------------------------
2361 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayStringProperty
, // Property name
2362 wxPGProperty
, // Property we inherit from
2363 wxArrayString
, // Value type name
2364 const wxArrayString
&, // Value type, as given in constructor
2365 TextCtrlAndButton
) // Initial editor
2367 wxArrayStringProperty::wxArrayStringProperty( const wxString
& label
,
2368 const wxString
& name
,
2369 const wxArrayString
& array
)
2370 : wxPGProperty(label
,name
)
2375 wxArrayStringProperty::~wxArrayStringProperty() { }
2377 void wxArrayStringProperty::OnSetValue()
2379 GenerateValueAsString();
2382 #define ARRSTRPROP_ARRAY_TO_STRING(STRING,ARRAY) \
2383 wxPropertyGrid::ArrayStringToString(STRING,ARRAY,wxS('"'),wxS('"'),1)
2385 wxString
wxArrayStringProperty::ValueToString( wxVariant
& WXUNUSED(value
),
2386 int argFlags
) const
2389 // If this is called from GetValueAsString(), return cached string
2390 if ( argFlags
& wxPG_VALUE_IS_CURRENT
)
2395 wxArrayString arr
= m_value
.GetArrayString();
2397 ARRSTRPROP_ARRAY_TO_STRING(s
, arr
);
2401 // Converts wxArrayString to a string separated by delimeters and spaces.
2402 // preDelim is useful for "str1" "str2" style. Set flags to 1 to do slash
2404 void wxPropertyGrid::ArrayStringToString( wxString
& dst
, const wxArrayString
& src
,
2405 wxChar preDelim
, wxChar postDelim
,
2411 unsigned int itemCount
= src
.size();
2413 wxChar preas
[2] = { 0, 0 };
2419 preas
[0] = preDelim
;
2425 dst
.append( preas
);
2427 wxASSERT( postDelim
);
2428 wxString
postDelimStr(postDelim
);
2429 //wxString preDelimStr(preDelim);
2431 for ( i
= 0; i
< itemCount
; i
++ )
2433 wxString
str( src
.Item(i
) );
2435 // Do some character conversion.
2436 // Convertes \ to \\ and <preDelim> to \<preDelim>
2437 // Useful when preDelim and postDelim are "\"".
2440 str
.Replace( wxS("\\"), wxS("\\\\"), true );
2442 str
.Replace( preas
, pdr
, true );
2447 if ( i
< (itemCount
-1) )
2449 dst
.append( postDelimStr
);
2450 dst
.append( wxS(" ") );
2451 dst
.append( preas
);
2453 else if ( preDelim
)
2454 dst
.append( postDelimStr
);
2458 void wxArrayStringProperty::GenerateValueAsString()
2460 wxArrayString arr
= m_value
.GetArrayString();
2461 ARRSTRPROP_ARRAY_TO_STRING(m_display
, arr
);
2464 // Default implementation doesn't do anything.
2465 bool wxArrayStringProperty::OnCustomStringEdit( wxWindow
*, wxString
& )
2470 wxArrayEditorDialog
* wxArrayStringProperty::CreateEditorDialog()
2472 return new wxPGArrayStringEditorDialog();
2475 bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid
* propGrid
,
2476 wxWindow
* WXUNUSED(primaryCtrl
),
2480 wxVariant useValue
= propGrid
->GetUncommittedPropertyValue();
2482 if ( !propGrid
->EditorValidate() )
2485 // Create editor dialog.
2486 wxArrayEditorDialog
* dlg
= CreateEditorDialog();
2487 #if wxUSE_VALIDATORS
2488 wxValidator
* validator
= GetValidator();
2489 wxPGInDialogValidator dialogValidator
;
2492 wxPGArrayStringEditorDialog
* strEdDlg
= wxDynamicCast(dlg
, wxPGArrayStringEditorDialog
);
2495 strEdDlg
->SetCustomButton(cbt
, this);
2497 dlg
->SetDialogValue( useValue
);
2498 dlg
->Create(propGrid
, wxEmptyString
, m_label
);
2500 #if !wxPG_SMALL_SCREEN
2501 dlg
->Move( propGrid
->GetGoodEditorDialogPosition(this,dlg
->GetSize()) );
2510 int res
= dlg
->ShowModal();
2512 if ( res
== wxID_OK
&& dlg
->IsModified() )
2514 wxVariant value
= dlg
->GetDialogValue();
2515 if ( !value
.IsNull() )
2517 wxArrayString actualValue
= value
.GetArrayString();
2519 ARRSTRPROP_ARRAY_TO_STRING(tempStr
, actualValue
);
2520 #if wxUSE_VALIDATORS
2521 if ( dialogValidator
.DoValidate( propGrid
, validator
, tempStr
) )
2524 SetValueInEvent( actualValue
);
2541 bool wxArrayStringProperty::OnEvent( wxPropertyGrid
* propGrid
,
2545 if ( propGrid
->IsMainButtonEvent(event
) )
2546 return OnButtonClick(propGrid
,primary
,(const wxChar
*) NULL
);
2550 bool wxArrayStringProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
2554 WX_PG_TOKENIZER2_BEGIN(text
,wxS('"'))
2556 // Need to replace backslashes with empty characters
2557 // (opposite what is done in GenerateValueString).
2558 token
.Replace ( wxS("\\"), wxEmptyString
, true );
2562 WX_PG_TOKENIZER2_END()
2569 // -----------------------------------------------------------------------
2570 // wxPGInDialogValidator
2571 // -----------------------------------------------------------------------
2573 #if wxUSE_VALIDATORS
2574 bool wxPGInDialogValidator::DoValidate( wxPropertyGrid
* propGrid
,
2575 wxValidator
* validator
,
2576 const wxString
& value
)
2581 wxTextCtrl
* tc
= m_textCtrl
;
2586 tc
= new wxTextCtrl( propGrid
, wxPG_SUBID_TEMP1
, wxEmptyString
,
2587 wxPoint(30000,30000));
2594 tc
->SetValue(value
);
2596 validator
->SetWindow(tc
);
2597 bool res
= validator
->Validate(propGrid
);
2602 bool wxPGInDialogValidator::DoValidate( wxPropertyGrid
* WXUNUSED(propGrid
),
2603 wxValidator
* WXUNUSED(validator
),
2604 const wxString
& WXUNUSED(value
) )
2610 // -----------------------------------------------------------------------
2612 #endif // wxUSE_PROPGRID