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;
967 void wxEnumProperty::OnSetValue()
969 wxString variantType
= m_value
.GetType();
971 if ( variantType
== wxPG_VARIANT_TYPE_LONG
)
972 ValueFromInt_( m_value
, m_value
.GetLong(), wxPG_FULL_VALUE
);
973 else if ( variantType
== wxPG_VARIANT_TYPE_STRING
)
974 ValueFromString_( m_value
, m_value
.GetString(), 0 );
978 if ( ms_nextIndex
!= -2 )
980 m_index
= ms_nextIndex
;
985 bool wxEnumProperty::ValidateValue( wxVariant
& value
, wxPGValidationInfo
& WXUNUSED(validationInfo
) ) const
987 // Make sure string value is in the list,
988 // unless property has string as preferred value type
989 // To reduce code size, use conversion here as well
990 if ( value
.GetType() == wxPG_VARIANT_TYPE_STRING
&&
991 !this->IsKindOf(CLASSINFO(wxEditEnumProperty
)) )
992 return ValueFromString_( value
, value
.GetString(), wxPG_PROPERTY_SPECIFIC
);
997 wxString
wxEnumProperty::ValueToString( wxVariant
& value
,
998 int WXUNUSED(argFlags
) ) const
1000 if ( value
.GetType() == wxPG_VARIANT_TYPE_STRING
)
1001 return value
.GetString();
1003 int index
= m_choices
.Index(value
.GetLong());
1005 return wxEmptyString
;
1007 return m_choices
.GetLabel(index
);
1010 bool wxEnumProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
1012 return ValueFromString_( variant
, text
, argFlags
);
1015 bool wxEnumProperty::IntToValue( wxVariant
& variant
, int intVal
, int argFlags
) const
1017 return ValueFromInt_( variant
, intVal
, argFlags
);
1020 bool wxEnumProperty::ValueFromString_( wxVariant
& value
, const wxString
& text
, int argFlags
) const
1025 for ( unsigned int i
=0; i
<m_choices
.GetCount(); i
++ )
1027 const wxString
& entryLabel
= m_choices
.GetLabel(i
);
1028 if ( text
.CmpNoCase(entryLabel
) == 0 )
1031 useValue
= m_choices
.GetValue(i
);
1036 bool asText
= false;
1038 bool isEdit
= this->IsKindOf(CLASSINFO(wxEditEnumProperty
));
1040 // If text not any of the choices, store as text instead
1041 // (but only if we are wxEditEnumProperty)
1042 if ( useIndex
== -1 &&
1043 (value
.GetType() != wxPG_VARIANT_TYPE_STRING
|| (m_value
.GetString() != text
)) &&
1049 int setAsNextIndex
= -2;
1053 setAsNextIndex
= -1;
1056 else if ( useIndex
!= GetIndex() )
1058 if ( useIndex
!= -1 )
1060 setAsNextIndex
= useIndex
;
1061 value
= (long)useValue
;
1065 setAsNextIndex
= -1;
1066 value
= wxPGVariant_MinusOne
;
1070 if ( setAsNextIndex
!= -2 )
1072 // If wxPG_PROPERTY_SPECIFIC is set, then this is done for
1073 // validation purposes only, and index must not be changed
1074 if ( !(argFlags
& wxPG_PROPERTY_SPECIFIC
) )
1075 ms_nextIndex
= setAsNextIndex
;
1077 if ( isEdit
|| setAsNextIndex
!= -1 )
1085 bool wxEnumProperty::ValueFromInt_( wxVariant
& variant
, int intVal
, int argFlags
) const
1087 // If wxPG_FULL_VALUE is *not* in argFlags, then intVal is index from combo box.
1091 if ( argFlags
& wxPG_FULL_VALUE
)
1093 ms_nextIndex
= GetIndexForValue( intVal
);
1097 if ( intVal
!= GetIndex() )
1099 ms_nextIndex
= intVal
;
1103 if ( ms_nextIndex
!= -2 )
1105 if ( !(argFlags
& wxPG_FULL_VALUE
) )
1106 intVal
= m_choices
.GetValue(intVal
);
1108 variant
= (long)intVal
;
1117 wxEnumProperty::OnValidationFailure( wxVariant
& WXUNUSED(pendingValue
) )
1123 void wxEnumProperty::SetIndex( int index
)
1129 int wxEnumProperty::GetIndex() const
1131 if ( m_value
.IsNull() )
1134 if ( ms_nextIndex
!= -2 )
1135 return ms_nextIndex
;
1140 // -----------------------------------------------------------------------
1141 // wxEditEnumProperty
1142 // -----------------------------------------------------------------------
1144 IMPLEMENT_DYNAMIC_CLASS(wxEditEnumProperty
, wxPGProperty
)
1146 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEditEnumProperty
,wxString
,ComboBox
)
1148 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
** labels
,
1149 const long* values
, const wxString
& value
)
1150 : wxEnumProperty(label
,name
,labels
,values
,0)
1155 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
, const wxChar
** labels
,
1156 const long* values
, wxPGChoices
* choicesCache
, const wxString
& value
)
1157 : wxEnumProperty(label
,name
,labels
,values
,choicesCache
,0)
1162 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
,
1163 const wxArrayString
& labels
, const wxArrayInt
& values
, const wxString
& value
)
1164 : wxEnumProperty(label
,name
,labels
,values
,0)
1169 wxEditEnumProperty::wxEditEnumProperty( const wxString
& label
, const wxString
& name
,
1170 wxPGChoices
& choices
, const wxString
& value
)
1171 : wxEnumProperty(label
,name
,choices
,0)
1176 wxEditEnumProperty::~wxEditEnumProperty()
1180 // -----------------------------------------------------------------------
1182 // -----------------------------------------------------------------------
1184 IMPLEMENT_DYNAMIC_CLASS(wxFlagsProperty
,wxPGProperty
)
1186 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxFlagsProperty
,long,TextCtrl
)
1188 void wxFlagsProperty::Init()
1190 SetParentalType(wxPG_PROP_AGGREGATE
);
1192 long value
= m_value
;
1195 // Generate children
1199 unsigned int prevChildCount
= m_children
.size();
1202 if ( prevChildCount
)
1204 wxPropertyGridPageState
* state
= GetParentState();
1206 // State safety check (it may be NULL in immediate parent)
1211 wxPGProperty
* selected
= state
->GetSelection();
1214 if ( selected
->GetParent() == this )
1215 oldSel
= selected
->GetIndexInParent();
1216 else if ( selected
== this )
1220 state
->DoClearSelection();
1223 // Delete old children
1224 for ( i
=0; i
<prevChildCount
; i
++ )
1225 delete m_children
[i
];
1229 if ( m_choices
.IsOk() )
1231 const wxPGChoices
& choices
= m_choices
;
1233 for ( i
=0; i
<GetItemCount(); i
++ )
1236 child_val
= ( value
& choices
.GetValue(i
) )?true:false;
1238 wxPGProperty
* boolProp
;
1239 wxString label
= GetLabel(i
);
1242 if ( wxPGGlobalVars
->m_autoGetTranslation
)
1244 boolProp
= new wxBoolProperty( ::wxGetTranslation(label
), label
, child_val
);
1249 boolProp
= new wxBoolProperty( label
, label
, child_val
);
1254 m_oldChoicesData
= m_choices
.GetDataPtr();
1257 m_oldValue
= m_value
;
1259 if ( prevChildCount
)
1260 SubPropsChanged(oldSel
);
1263 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1264 const wxChar
** labels
, const long* values
, long value
) : wxPGProperty(label
,name
)
1266 m_oldChoicesData
= NULL
;
1270 m_choices
.Set(labels
,values
);
1272 wxASSERT( GetItemCount() );
1278 m_value
= wxPGVariant_Zero
;
1282 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1283 const wxArrayString
& labels
, const wxArrayInt
& values
, int value
)
1284 : wxPGProperty(label
,name
)
1286 m_oldChoicesData
= NULL
;
1288 if ( &labels
&& labels
.size() )
1290 m_choices
.Set(labels
,values
);
1292 wxASSERT( GetItemCount() );
1294 SetValue( (long)value
);
1298 m_value
= wxPGVariant_Zero
;
1302 wxFlagsProperty::wxFlagsProperty( const wxString
& label
, const wxString
& name
,
1303 wxPGChoices
& choices
, long value
)
1304 : wxPGProperty(label
,name
)
1306 m_oldChoicesData
= NULL
;
1308 if ( choices
.IsOk() )
1310 m_choices
.Assign(choices
);
1312 wxASSERT( GetItemCount() );
1318 m_value
= wxPGVariant_Zero
;
1322 wxFlagsProperty::~wxFlagsProperty()
1326 void wxFlagsProperty::OnSetValue()
1328 if ( !m_choices
.IsOk() || !GetItemCount() )
1330 m_value
= wxPGVariant_Zero
;
1334 long val
= m_value
.GetLong();
1338 // normalize the value (i.e. remove extra flags)
1340 const wxPGChoices
& choices
= m_choices
;
1341 for ( i
= 0; i
< GetItemCount(); i
++ )
1343 fullFlags
|= choices
.GetValue(i
);
1350 // Need to (re)init now?
1351 if ( GetChildCount() != GetItemCount() ||
1352 m_choices
.GetDataPtr() != m_oldChoicesData
)
1358 long newFlags
= m_value
;
1360 if ( newFlags
!= m_oldValue
)
1362 // Set child modified states
1364 const wxPGChoices
& choices
= m_choices
;
1365 for ( i
= 0; i
<GetItemCount(); i
++ )
1369 flag
= choices
.GetValue(i
);
1371 if ( (newFlags
& flag
) != (m_oldValue
& flag
) )
1372 Item(i
)->SetFlag( wxPG_PROP_MODIFIED
);
1375 m_oldValue
= newFlags
;
1379 wxString
wxFlagsProperty::ValueToString( wxVariant
& value
,
1380 int WXUNUSED(argFlags
) ) const
1384 if ( !m_choices
.IsOk() )
1389 const wxPGChoices
& choices
= m_choices
;
1391 for ( i
= 0; i
< GetItemCount(); i
++ )
1394 doAdd
= ( flags
& choices
.GetValue(i
) );
1398 text
+= choices
.GetLabel(i
);
1403 // remove last comma
1404 if ( text
.Len() > 1 )
1405 text
.Truncate ( text
.Len() - 2 );
1410 // Translate string into flag tokens
1411 bool wxFlagsProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
1413 if ( !m_choices
.IsOk() )
1418 // semicolons are no longer valid delimeters
1419 WX_PG_TOKENIZER1_BEGIN(text
,wxS(','))
1421 if ( token
.length() )
1423 // Determine which one it is
1424 long bit
= IdToBit( token
);
1437 WX_PG_TOKENIZER1_END()
1439 if ( variant
!= (long)newFlags
)
1441 variant
= (long)newFlags
;
1448 // Converts string id to a relevant bit.
1449 long wxFlagsProperty::IdToBit( const wxString
& id
) const
1452 for ( i
= 0; i
< GetItemCount(); i
++ )
1454 if ( id
== GetLabel(i
) )
1456 return m_choices
.GetValue(i
);
1462 void wxFlagsProperty::RefreshChildren()
1464 if ( !m_choices
.IsOk() || !GetChildCount() ) return;
1466 int flags
= m_value
.GetLong();
1468 const wxPGChoices
& choices
= m_choices
;
1470 for ( i
= 0; i
< GetItemCount(); i
++ )
1474 flag
= choices
.GetValue(i
);
1476 long subVal
= flags
& flag
;
1477 wxPGProperty
* p
= Item(i
);
1479 if ( subVal
!= (m_oldValue
& flag
) )
1480 p
->SetFlag( wxPG_PROP_MODIFIED
);
1482 p
->SetValue( subVal
?true:false );
1488 void wxFlagsProperty::ChildChanged( wxVariant
& thisValue
, int childIndex
, wxVariant
& childValue
) const
1490 long oldValue
= thisValue
.GetLong();
1491 long val
= childValue
.GetLong();
1492 unsigned long vi
= m_choices
.GetValue(childIndex
);
1494 thisValue
= (long)(oldValue
| vi
);
1496 thisValue
= (long)(oldValue
& ~(vi
));
1499 // -----------------------------------------------------------------------
1501 // -----------------------------------------------------------------------
1503 IMPLEMENT_DYNAMIC_CLASS(wxDirProperty
, wxLongStringProperty
)
1505 wxDirProperty::wxDirProperty( const wxString
& name
, const wxString
& label
, const wxString
& value
)
1506 : wxLongStringProperty(name
,label
,value
)
1508 m_flags
|= wxPG_PROP_NO_ESCAPE
;
1511 wxDirProperty::~wxDirProperty() { }
1513 wxValidator
* wxDirProperty::DoGetValidator() const
1515 return wxFileProperty::GetClassValidator();
1518 bool wxDirProperty::OnButtonClick( wxPropertyGrid
* propGrid
, wxString
& value
)
1520 // Update property value from editor, if necessary
1521 wxSize
dlg_sz(300,400);
1523 wxString
dlgMessage(m_dlgMessage
);
1524 if ( dlgMessage
.empty() )
1525 dlgMessage
= _("Choose a directory:");
1526 wxDirDialog
dlg( propGrid
,
1530 #if !wxPG_SMALL_SCREEN
1531 propGrid
->GetGoodEditorDialogPosition(this,dlg_sz
),
1539 if ( dlg
.ShowModal() == wxID_OK
)
1541 value
= dlg
.GetPath();
1547 bool wxDirProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
1549 if ( name
== wxPG_DIR_DIALOG_MESSAGE
)
1551 m_dlgMessage
= value
.GetString();
1557 // -----------------------------------------------------------------------
1558 // wxPGFileDialogAdapter
1559 // -----------------------------------------------------------------------
1561 bool wxPGFileDialogAdapter::DoShowDialog( wxPropertyGrid
* propGrid
, wxPGProperty
* property
)
1563 wxFileProperty
* fileProp
= NULL
;
1567 if ( property
->IsKindOf(CLASSINFO(wxFileProperty
)) )
1569 fileProp
= ((wxFileProperty
*)property
);
1570 wxFileName filename
= fileProp
->GetValue().GetString();
1571 path
= filename
.GetPath();
1572 indFilter
= fileProp
->m_indFilter
;
1574 if ( !path
.length() && fileProp
->m_basePath
.length() )
1575 path
= fileProp
->m_basePath
;
1579 wxFileName
fn(property
->GetValue().GetString());
1580 path
= fn
.GetPath();
1583 wxFileDialog
dlg( propGrid
->GetPanel(),
1584 property
->GetAttribute(wxS("DialogTitle"), _("Choose a file")),
1585 property
->GetAttribute(wxS("InitialPath"), path
),
1587 property
->GetAttribute(wxPG_FILE_WILDCARD
, _("All files (*.*)|*.*")),
1589 wxDefaultPosition
);
1591 if ( indFilter
>= 0 )
1592 dlg
.SetFilterIndex( indFilter
);
1594 if ( dlg
.ShowModal() == wxID_OK
)
1597 fileProp
->m_indFilter
= dlg
.GetFilterIndex();
1598 SetValue( dlg
.GetPath() );
1604 // -----------------------------------------------------------------------
1606 // -----------------------------------------------------------------------
1608 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFileProperty
,wxPGProperty
,
1609 wxString
,const wxString
&,TextCtrlAndButton
)
1611 wxFileProperty::wxFileProperty( const wxString
& label
, const wxString
& name
,
1612 const wxString
& value
) : wxPGProperty(label
,name
)
1614 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1616 SetAttribute( wxPG_FILE_WILDCARD
, _("All files (*.*)|*.*") );
1621 wxFileProperty::~wxFileProperty() {}
1623 #if wxUSE_VALIDATORS
1625 wxValidator
* wxFileProperty::GetClassValidator()
1627 WX_PG_DOGETVALIDATOR_ENTRY()
1629 // Atleast wxPython 2.6.2.1 required that the string argument is given
1631 wxTextValidator
* validator
= new wxTextValidator(wxFILTER_EXCLUDE_CHAR_LIST
,&v
);
1633 wxArrayString exChars
;
1634 exChars
.Add(wxS("?"));
1635 exChars
.Add(wxS("*"));
1636 exChars
.Add(wxS("|"));
1637 exChars
.Add(wxS("<"));
1638 exChars
.Add(wxS(">"));
1639 exChars
.Add(wxS("\""));
1641 validator
->SetExcludes(exChars
);
1643 WX_PG_DOGETVALIDATOR_EXIT(validator
)
1646 wxValidator
* wxFileProperty::DoGetValidator() const
1648 return GetClassValidator();
1653 void wxFileProperty::OnSetValue()
1655 const wxString
& fnstr
= m_value
.GetString();
1657 wxFileName filename
= fnstr
;
1659 if ( !filename
.HasName() )
1661 m_value
= wxPGVariant_EmptyString
;
1664 // Find index for extension.
1665 if ( m_indFilter
< 0 && fnstr
.length() )
1667 wxString ext
= filename
.GetExt();
1670 size_t len
= m_wildcard
.length();
1672 pos
= m_wildcard
.find(wxS("|"), pos
);
1673 while ( pos
!= wxString::npos
&& pos
< (len
-3) )
1675 size_t ext_begin
= pos
+ 3;
1677 pos
= m_wildcard
.find(wxS("|"), ext_begin
);
1678 if ( pos
== wxString::npos
)
1680 wxString found_ext
= m_wildcard
.substr(ext_begin
, pos
-ext_begin
);
1682 if ( found_ext
.length() > 0 )
1684 if ( found_ext
[0] == wxS('*') )
1686 m_indFilter
= curind
;
1689 if ( ext
.CmpNoCase(found_ext
) == 0 )
1691 m_indFilter
= curind
;
1697 pos
= m_wildcard
.find(wxS("|"), pos
+1);
1704 wxFileName
wxFileProperty::GetFileName() const
1706 wxFileName filename
;
1708 if ( !m_value
.IsNull() )
1709 filename
= m_value
.GetString();
1714 wxString
wxFileProperty::ValueToString( wxVariant
& value
,
1715 int argFlags
) const
1717 wxFileName filename
= value
.GetString();
1719 if ( !filename
.HasName() )
1720 return wxEmptyString
;
1722 wxString fullName
= filename
.GetFullName();
1723 if ( !fullName
.length() )
1724 return wxEmptyString
;
1726 if ( argFlags
& wxPG_FULL_VALUE
)
1728 return filename
.GetFullPath();
1730 else if ( m_flags
& wxPG_PROP_SHOW_FULL_FILENAME
)
1732 if ( m_basePath
.Length() )
1734 wxFileName
fn2(filename
);
1735 fn2
.MakeRelativeTo(m_basePath
);
1736 return fn2
.GetFullPath();
1738 return filename
.GetFullPath();
1741 return filename
.GetFullName();
1744 wxPGEditorDialogAdapter
* wxFileProperty::GetEditorDialog() const
1746 return new wxPGFileDialogAdapter();
1749 bool wxFileProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
1751 wxFileName filename
= variant
.GetString();
1753 if ( (m_flags
& wxPG_PROP_SHOW_FULL_FILENAME
) || (argFlags
& wxPG_FULL_VALUE
) )
1755 if ( filename
!= text
)
1763 if ( filename
.GetFullName() != text
)
1765 wxFileName fn
= filename
;
1766 fn
.SetFullName(text
);
1767 variant
= fn
.GetFullPath();
1775 bool wxFileProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
1777 // Return false on some occasions to make sure those attribs will get
1778 // stored in m_attributes.
1779 if ( name
== wxPG_FILE_SHOW_FULL_PATH
)
1781 if ( wxPGVariantToInt(value
) )
1782 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1784 m_flags
&= ~(wxPG_PROP_SHOW_FULL_FILENAME
);
1787 else if ( name
== wxPG_FILE_WILDCARD
)
1789 m_wildcard
= value
.GetString();
1791 else if ( name
== wxPG_FILE_SHOW_RELATIVE_PATH
)
1793 m_basePath
= value
.GetString();
1795 // Make sure wxPG_FILE_SHOW_FULL_PATH is also set
1796 m_flags
|= wxPG_PROP_SHOW_FULL_FILENAME
;
1798 else if ( name
== wxPG_FILE_INITIAL_PATH
)
1800 m_initialPath
= value
.GetString();
1803 else if ( name
== wxPG_FILE_DIALOG_TITLE
)
1805 m_dlgTitle
= value
.GetString();
1811 // -----------------------------------------------------------------------
1812 // wxPGLongStringDialogAdapter
1813 // -----------------------------------------------------------------------
1815 bool wxPGLongStringDialogAdapter::DoShowDialog( wxPropertyGrid
* propGrid
, wxPGProperty
* property
)
1817 wxString val1
= property
->GetValueAsString(0);
1818 wxString val_orig
= val1
;
1821 if ( !property
->HasFlag(wxPG_PROP_NO_ESCAPE
) )
1822 wxPropertyGrid::ExpandEscapeSequences(value
, val1
);
1824 value
= wxString(val1
);
1826 // Run editor dialog.
1827 if ( wxLongStringProperty::DisplayEditorDialog(property
, propGrid
, value
) )
1829 if ( !property
->HasFlag(wxPG_PROP_NO_ESCAPE
) )
1830 wxPropertyGrid::CreateEscapeSequences(val1
,value
);
1834 if ( val1
!= val_orig
)
1843 // -----------------------------------------------------------------------
1844 // wxLongStringProperty
1845 // -----------------------------------------------------------------------
1847 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxLongStringProperty
,wxPGProperty
,
1848 wxString
,const wxString
&,TextCtrlAndButton
)
1850 wxLongStringProperty::wxLongStringProperty( const wxString
& label
, const wxString
& name
,
1851 const wxString
& value
) : wxPGProperty(label
,name
)
1856 wxLongStringProperty::~wxLongStringProperty() {}
1858 wxString
wxLongStringProperty::ValueToString( wxVariant
& value
,
1859 int WXUNUSED(argFlags
) ) const
1864 bool wxLongStringProperty::OnEvent( wxPropertyGrid
* propGrid
, wxWindow
* WXUNUSED(primary
),
1867 if ( propGrid
->IsMainButtonEvent(event
) )
1870 wxVariant useValue
= propGrid
->GetUncommittedPropertyValue();
1872 wxString val1
= useValue
.GetString();
1873 wxString val_orig
= val1
;
1876 if ( !(m_flags
& wxPG_PROP_NO_ESCAPE
) )
1877 wxPropertyGrid::ExpandEscapeSequences(value
,val1
);
1879 value
= wxString(val1
);
1881 // Run editor dialog.
1882 if ( OnButtonClick(propGrid
,value
) )
1884 if ( !(m_flags
& wxPG_PROP_NO_ESCAPE
) )
1885 wxPropertyGrid::CreateEscapeSequences(val1
,value
);
1889 if ( val1
!= val_orig
)
1891 SetValueInEvent( val1
);
1899 bool wxLongStringProperty::OnButtonClick( wxPropertyGrid
* propGrid
, wxString
& value
)
1901 return DisplayEditorDialog(this, propGrid
, value
);
1904 bool wxLongStringProperty::DisplayEditorDialog( wxPGProperty
* prop
, wxPropertyGrid
* propGrid
, wxString
& value
)
1907 // launch editor dialog
1908 wxDialog
* dlg
= new wxDialog(propGrid
,-1,prop
->GetLabel(),wxDefaultPosition
,wxDefaultSize
,
1909 wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
|wxCLIP_CHILDREN
);
1911 dlg
->SetFont(propGrid
->GetFont()); // To allow entering chars of the same set as the propGrid
1913 // Multi-line text editor dialog.
1914 #if !wxPG_SMALL_SCREEN
1915 const int spacing
= 8;
1917 const int spacing
= 4;
1919 wxBoxSizer
* topsizer
= new wxBoxSizer( wxVERTICAL
);
1920 wxBoxSizer
* rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
1921 wxTextCtrl
* ed
= new wxTextCtrl(dlg
,11,value
,
1922 wxDefaultPosition
,wxDefaultSize
,wxTE_MULTILINE
);
1924 rowsizer
->Add( ed
, 1, wxEXPAND
|wxALL
, spacing
);
1925 topsizer
->Add( rowsizer
, 1, wxEXPAND
, 0 );
1926 rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
1927 const int but_sz_flags
=
1928 wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
|wxBOTTOM
|wxLEFT
|wxRIGHT
;
1929 rowsizer
->Add( new wxButton(dlg
,wxID_OK
,_("Ok")),
1930 0, but_sz_flags
, spacing
);
1931 rowsizer
->Add( new wxButton(dlg
,wxID_CANCEL
,_("Cancel")),
1932 0, but_sz_flags
, spacing
);
1933 topsizer
->Add( rowsizer
, 0, wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
, 0 );
1935 dlg
->SetSizer( topsizer
);
1936 topsizer
->SetSizeHints( dlg
);
1938 #if !wxPG_SMALL_SCREEN
1939 dlg
->SetSize(400,300);
1941 dlg
->Move( propGrid
->GetGoodEditorDialogPosition(prop
,dlg
->GetSize()) );
1944 int res
= dlg
->ShowModal();
1946 if ( res
== wxID_OK
)
1948 value
= ed
->GetValue();
1956 bool wxLongStringProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
1958 if ( variant
!= text
)
1966 // -----------------------------------------------------------------------
1967 // wxArrayEditorDialog
1968 // -----------------------------------------------------------------------
1970 BEGIN_EVENT_TABLE(wxArrayEditorDialog
, wxDialog
)
1971 EVT_IDLE(wxArrayEditorDialog::OnIdle
)
1972 EVT_LISTBOX(24, wxArrayEditorDialog::OnListBoxClick
)
1973 EVT_TEXT_ENTER(21, wxArrayEditorDialog::OnAddClick
)
1974 EVT_BUTTON(22, wxArrayEditorDialog::OnAddClick
)
1975 EVT_BUTTON(23, wxArrayEditorDialog::OnDeleteClick
)
1976 EVT_BUTTON(25, wxArrayEditorDialog::OnUpClick
)
1977 EVT_BUTTON(26, wxArrayEditorDialog::OnDownClick
)
1978 EVT_BUTTON(27, wxArrayEditorDialog::OnUpdateClick
)
1979 //EVT_BUTTON(28, wxArrayEditorDialog::OnCustomEditClick)
1982 IMPLEMENT_ABSTRACT_CLASS(wxArrayEditorDialog
, wxDialog
)
1984 #include "wx/statline.h"
1986 // -----------------------------------------------------------------------
1988 void wxArrayEditorDialog::OnIdle(wxIdleEvent
& event
)
1991 // Do control focus detection here.
1994 wxWindow
* focused
= FindFocus();
1996 // This strange focus thing is a workaround for wxGTK wxListBox focus
1998 if ( m_curFocus
== 0 && focused
!= m_edValue
&&
1999 focused
!= m_butAdd
&& focused
!= m_butUpdate
&&
2000 m_lbStrings
->GetSelection() >= 0 )
2002 // ListBox was just focused.
2003 m_butAdd
->Enable(false);
2004 m_butUpdate
->Enable(false);
2005 m_butRemove
->Enable(true);
2006 m_butUp
->Enable(true);
2007 m_butDown
->Enable(true);
2010 else if ( (m_curFocus
== 1 && focused
== m_edValue
) /*|| m_curFocus == 2*/ )
2012 // TextCtrl was just focused.
2013 m_butAdd
->Enable(true);
2014 bool upd_enable
= false;
2015 if ( m_lbStrings
->GetCount() && m_lbStrings
->GetSelection() >= 0 )
2017 m_butUpdate
->Enable(upd_enable
);
2018 m_butRemove
->Enable(false);
2019 m_butUp
->Enable(false);
2020 m_butDown
->Enable(false);
2027 // -----------------------------------------------------------------------
2029 wxArrayEditorDialog::wxArrayEditorDialog()
2035 // -----------------------------------------------------------------------
2037 void wxArrayEditorDialog::Init()
2039 m_custBtText
= (const wxChar
*) NULL
;
2042 // -----------------------------------------------------------------------
2044 wxArrayEditorDialog::wxArrayEditorDialog( wxWindow
*parent
,
2045 const wxString
& message
,
2046 const wxString
& caption
,
2053 Create(parent
,message
,caption
,style
,pos
,sz
);
2056 // -----------------------------------------------------------------------
2058 bool wxArrayEditorDialog::Create( wxWindow
*parent
,
2059 const wxString
& message
,
2060 const wxString
& caption
,
2065 // On wxMAC the dialog shows incorrectly if style is not exactly wxCAPTION
2066 // FIXME: This should be only a temporary fix.
2069 int useStyle
= wxCAPTION
;
2071 int useStyle
= style
;
2074 bool res
= wxDialog::Create(parent
, wxID_ANY
, caption
, pos
, sz
, useStyle
);
2076 SetFont(parent
->GetFont()); // To allow entering chars of the same set as the propGrid
2078 #if !wxPG_SMALL_SCREEN
2079 const int spacing
= 4;
2081 const int spacing
= 3;
2088 const int but_sz_flags
=
2089 wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
|wxALL
; //wxBOTTOM|wxLEFT|wxRIGHT;
2091 wxBoxSizer
* topsizer
= new wxBoxSizer( wxVERTICAL
);
2094 if ( message
.length() )
2095 topsizer
->Add( new wxStaticText(this,-1,message
),
2096 0, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxALL
, spacing
);
2099 wxBoxSizer
* rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
2100 m_edValue
= new wxTextCtrl(this,21,wxEmptyString
,
2101 wxDefaultPosition
,wxDefaultSize
,wxTE_PROCESS_ENTER
);
2102 wxValidator
* validator
= GetTextCtrlValidator();
2105 m_edValue
->SetValidator( *validator
);
2108 rowsizer
->Add( m_edValue
,
2109 1, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxALL
, spacing
);
2112 m_butAdd
= new wxButton(this,22,_("Add"));
2113 rowsizer
->Add( m_butAdd
,
2114 0, wxALIGN_LEFT
|wxALIGN_CENTRE_VERTICAL
|wxTOP
|wxBOTTOM
|wxRIGHT
, spacing
);
2115 topsizer
->Add( rowsizer
, 0, wxEXPAND
, spacing
);
2118 topsizer
->Add( new wxStaticLine(this,-1),
2119 0, wxEXPAND
|wxBOTTOM
|wxLEFT
|wxRIGHT
, spacing
);
2121 rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
2124 m_lbStrings
= new wxListBox(this, 24, wxDefaultPosition
, wxDefaultSize
);
2126 for ( i
=0; i
<ArrayGetCount(); i
++ )
2127 m_lbStrings
->Append( ArrayGet(i
) );
2128 rowsizer
->Add( m_lbStrings
, 1, wxEXPAND
|wxRIGHT
, spacing
);
2130 // Manipulator buttons
2131 wxBoxSizer
* colsizer
= new wxBoxSizer( wxVERTICAL
);
2135 m_butCustom
= new wxButton(this,28,::wxGetTranslation(m_custBtText
));
2136 colsizer
->Add( m_butCustom
,
2137 0, wxALIGN_CENTER
|wxTOP
/*wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT*/,
2140 m_butUpdate
= new wxButton(this,27,_("Update"));
2141 colsizer
->Add( m_butUpdate
,
2142 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2143 m_butRemove
= new wxButton(this,23,_("Remove"));
2144 colsizer
->Add( m_butRemove
,
2145 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2146 m_butUp
= new wxButton(this,25,_("Up"));
2147 colsizer
->Add( m_butUp
,
2148 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2149 m_butDown
= new wxButton(this,26,_("Down"));
2150 colsizer
->Add( m_butDown
,
2151 0, wxALIGN_CENTER
|wxTOP
, spacing
);
2152 rowsizer
->Add( colsizer
, 0, 0, spacing
);
2154 topsizer
->Add( rowsizer
, 1, wxLEFT
|wxRIGHT
|wxEXPAND
, spacing
);
2157 topsizer
->Add( new wxStaticLine(this,-1),
2158 0, wxEXPAND
|wxTOP
|wxLEFT
|wxRIGHT
, spacing
);
2161 rowsizer
= new wxBoxSizer( wxHORIZONTAL
);
2163 const int but_sz_flags =
2164 wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT;
2166 rowsizer
->Add( new wxButton(this,wxID_OK
,_("Ok")),
2167 0, but_sz_flags
, spacing
);
2168 rowsizer
->Add( new wxButton(this,wxID_CANCEL
,_("Cancel")),
2169 0, but_sz_flags
, spacing
);
2170 topsizer
->Add( rowsizer
, 0, wxALIGN_RIGHT
|wxALIGN_CENTRE_VERTICAL
, 0 );
2172 m_edValue
->SetFocus();
2174 SetSizer( topsizer
);
2175 topsizer
->SetSizeHints( this );
2177 #if !wxPG_SMALL_SCREEN
2178 if ( sz
.x
== wxDefaultSize
.x
&&
2179 sz
.y
== wxDefaultSize
.y
)
2180 SetSize( wxSize(275,360) );
2188 // -----------------------------------------------------------------------
2190 void wxArrayEditorDialog::OnAddClick(wxCommandEvent
& )
2192 wxString text
= m_edValue
->GetValue();
2193 if ( text
.length() )
2195 if ( ArrayInsert( text
, -1 ) )
2197 m_lbStrings
->Append( text
);
2204 // -----------------------------------------------------------------------
2206 void wxArrayEditorDialog::OnDeleteClick(wxCommandEvent
& )
2208 int index
= m_lbStrings
->GetSelection();
2211 ArrayRemoveAt( index
);
2212 m_lbStrings
->Delete ( index
);
2217 // -----------------------------------------------------------------------
2219 void wxArrayEditorDialog::OnUpClick(wxCommandEvent
& )
2221 int index
= m_lbStrings
->GetSelection();
2224 ArraySwap(index
-1,index
);
2225 /*wxString old_str = m_array[index-1];
2226 wxString new_str = m_array[index];
2227 m_array[index-1] = new_str;
2228 m_array[index] = old_str;*/
2229 m_lbStrings
->SetString ( index
-1, ArrayGet(index
-1) );
2230 m_lbStrings
->SetString ( index
, ArrayGet(index
) );
2231 m_lbStrings
->SetSelection ( index
-1 );
2236 // -----------------------------------------------------------------------
2238 void wxArrayEditorDialog::OnDownClick(wxCommandEvent
& )
2240 int index
= m_lbStrings
->GetSelection();
2241 int lastStringIndex
= ((int) m_lbStrings
->GetCount()) - 1;
2242 if ( index
>= 0 && index
< lastStringIndex
)
2244 ArraySwap(index
,index
+1);
2245 /*wxString old_str = m_array[index+1];
2246 wxString new_str = m_array[index];
2247 m_array[index+1] = new_str;
2248 m_array[index] = old_str;*/
2249 m_lbStrings
->SetString ( index
+1, ArrayGet(index
+1) );
2250 m_lbStrings
->SetString ( index
, ArrayGet(index
) );
2251 m_lbStrings
->SetSelection ( index
+1 );
2256 // -----------------------------------------------------------------------
2258 void wxArrayEditorDialog::OnUpdateClick(wxCommandEvent
& )
2260 int index
= m_lbStrings
->GetSelection();
2263 wxString str
= m_edValue
->GetValue();
2264 if ( ArraySet(index
,str
) )
2266 m_lbStrings
->SetString ( index
, str
);
2267 //m_array[index] = str;
2273 // -----------------------------------------------------------------------
2275 void wxArrayEditorDialog::OnListBoxClick(wxCommandEvent
& )
2277 int index
= m_lbStrings
->GetSelection();
2280 m_edValue
->SetValue( m_lbStrings
->GetString(index
) );
2284 // -----------------------------------------------------------------------
2285 // wxPGArrayStringEditorDialog
2286 // -----------------------------------------------------------------------
2288 IMPLEMENT_DYNAMIC_CLASS(wxPGArrayStringEditorDialog
, wxArrayEditorDialog
)
2290 BEGIN_EVENT_TABLE(wxPGArrayStringEditorDialog
, wxArrayEditorDialog
)
2291 EVT_BUTTON(28, wxPGArrayStringEditorDialog::OnCustomEditClick
)
2294 // -----------------------------------------------------------------------
2296 wxString
wxPGArrayStringEditorDialog::ArrayGet( size_t index
)
2298 return m_array
[index
];
2301 size_t wxPGArrayStringEditorDialog::ArrayGetCount()
2303 return m_array
.size();
2306 bool wxPGArrayStringEditorDialog::ArrayInsert( const wxString
& str
, int index
)
2311 m_array
.Insert(str
,index
);
2315 bool wxPGArrayStringEditorDialog::ArraySet( size_t index
, const wxString
& str
)
2317 m_array
[index
] = str
;
2321 void wxPGArrayStringEditorDialog::ArrayRemoveAt( int index
)
2323 m_array
.RemoveAt(index
);
2326 void wxPGArrayStringEditorDialog::ArraySwap( size_t first
, size_t second
)
2328 wxString old_str
= m_array
[first
];
2329 wxString new_str
= m_array
[second
];
2330 m_array
[first
] = new_str
;
2331 m_array
[second
] = old_str
;
2334 wxPGArrayStringEditorDialog::wxPGArrayStringEditorDialog()
2335 : wxArrayEditorDialog()
2340 void wxPGArrayStringEditorDialog::Init()
2342 m_pCallingClass
= NULL
;
2345 void wxPGArrayStringEditorDialog::OnCustomEditClick(wxCommandEvent
& )
2347 wxASSERT( m_pCallingClass
);
2348 wxString str
= m_edValue
->GetValue();
2349 if ( m_pCallingClass
->OnCustomStringEdit(m_parent
,str
) )
2351 //m_edValue->SetValue ( str );
2352 m_lbStrings
->Append ( str
);
2353 m_array
.Add ( str
);
2358 // -----------------------------------------------------------------------
2359 // wxArrayStringProperty
2360 // -----------------------------------------------------------------------
2362 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayStringProperty
, // Property name
2363 wxPGProperty
, // Property we inherit from
2364 wxArrayString
, // Value type name
2365 const wxArrayString
&, // Value type, as given in constructor
2366 TextCtrlAndButton
) // Initial editor
2368 wxArrayStringProperty::wxArrayStringProperty( const wxString
& label
,
2369 const wxString
& name
,
2370 const wxArrayString
& array
)
2371 : wxPGProperty(label
,name
)
2376 wxArrayStringProperty::~wxArrayStringProperty() { }
2378 void wxArrayStringProperty::OnSetValue()
2380 GenerateValueAsString();
2383 #define ARRSTRPROP_ARRAY_TO_STRING(STRING,ARRAY) \
2384 wxPropertyGrid::ArrayStringToString(STRING,ARRAY,wxS('"'),wxS('"'),1)
2386 wxString
wxArrayStringProperty::ValueToString( wxVariant
& WXUNUSED(value
),
2387 int argFlags
) const
2390 // If this is called from GetValueAsString(), return cached string
2391 if ( argFlags
& wxPG_VALUE_IS_CURRENT
)
2396 wxArrayString arr
= m_value
.GetArrayString();
2398 ARRSTRPROP_ARRAY_TO_STRING(s
, arr
);
2402 // Converts wxArrayString to a string separated by delimeters and spaces.
2403 // preDelim is useful for "str1" "str2" style. Set flags to 1 to do slash
2405 void wxPropertyGrid::ArrayStringToString( wxString
& dst
, const wxArrayString
& src
,
2406 wxChar preDelim
, wxChar postDelim
,
2412 unsigned int itemCount
= src
.size();
2414 wxChar preas
[2] = { 0, 0 };
2420 preas
[0] = preDelim
;
2426 dst
.append( preas
);
2428 wxASSERT( postDelim
);
2429 wxString
postDelimStr(postDelim
);
2430 //wxString preDelimStr(preDelim);
2432 for ( i
= 0; i
< itemCount
; i
++ )
2434 wxString
str( src
.Item(i
) );
2436 // Do some character conversion.
2437 // Convertes \ to \\ and <preDelim> to \<preDelim>
2438 // Useful when preDelim and postDelim are "\"".
2441 str
.Replace( wxS("\\"), wxS("\\\\"), true );
2443 str
.Replace( preas
, pdr
, true );
2448 if ( i
< (itemCount
-1) )
2450 dst
.append( postDelimStr
);
2451 dst
.append( wxS(" ") );
2452 dst
.append( preas
);
2454 else if ( preDelim
)
2455 dst
.append( postDelimStr
);
2459 void wxArrayStringProperty::GenerateValueAsString()
2461 wxArrayString arr
= m_value
.GetArrayString();
2462 ARRSTRPROP_ARRAY_TO_STRING(m_display
, arr
);
2465 // Default implementation doesn't do anything.
2466 bool wxArrayStringProperty::OnCustomStringEdit( wxWindow
*, wxString
& )
2471 wxArrayEditorDialog
* wxArrayStringProperty::CreateEditorDialog()
2473 return new wxPGArrayStringEditorDialog();
2476 bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid
* propGrid
,
2477 wxWindow
* WXUNUSED(primaryCtrl
),
2481 wxVariant useValue
= propGrid
->GetUncommittedPropertyValue();
2483 if ( !propGrid
->EditorValidate() )
2486 // Create editor dialog.
2487 wxArrayEditorDialog
* dlg
= CreateEditorDialog();
2488 #if wxUSE_VALIDATORS
2489 wxValidator
* validator
= GetValidator();
2490 wxPGInDialogValidator dialogValidator
;
2493 wxPGArrayStringEditorDialog
* strEdDlg
= wxDynamicCast(dlg
, wxPGArrayStringEditorDialog
);
2496 strEdDlg
->SetCustomButton(cbt
, this);
2498 dlg
->SetDialogValue( useValue
);
2499 dlg
->Create(propGrid
, wxEmptyString
, m_label
);
2501 #if !wxPG_SMALL_SCREEN
2502 dlg
->Move( propGrid
->GetGoodEditorDialogPosition(this,dlg
->GetSize()) );
2511 int res
= dlg
->ShowModal();
2513 if ( res
== wxID_OK
&& dlg
->IsModified() )
2515 wxVariant value
= dlg
->GetDialogValue();
2516 if ( !value
.IsNull() )
2518 wxArrayString actualValue
= value
.GetArrayString();
2520 ARRSTRPROP_ARRAY_TO_STRING(tempStr
, actualValue
);
2521 #if wxUSE_VALIDATORS
2522 if ( dialogValidator
.DoValidate( propGrid
, validator
, tempStr
) )
2525 SetValueInEvent( actualValue
);
2542 bool wxArrayStringProperty::OnEvent( wxPropertyGrid
* propGrid
,
2546 if ( propGrid
->IsMainButtonEvent(event
) )
2547 return OnButtonClick(propGrid
,primary
,(const wxChar
*) NULL
);
2551 bool wxArrayStringProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
2555 WX_PG_TOKENIZER2_BEGIN(text
,wxS('"'))
2557 // Need to replace backslashes with empty characters
2558 // (opposite what is done in GenerateValueString).
2559 token
.Replace ( wxS("\\"), wxEmptyString
, true );
2563 WX_PG_TOKENIZER2_END()
2570 // -----------------------------------------------------------------------
2571 // wxPGInDialogValidator
2572 // -----------------------------------------------------------------------
2574 #if wxUSE_VALIDATORS
2575 bool wxPGInDialogValidator::DoValidate( wxPropertyGrid
* propGrid
,
2576 wxValidator
* validator
,
2577 const wxString
& value
)
2582 wxTextCtrl
* tc
= m_textCtrl
;
2587 tc
= new wxTextCtrl( propGrid
, wxPG_SUBID_TEMP1
, wxEmptyString
,
2588 wxPoint(30000,30000));
2595 tc
->SetValue(value
);
2597 validator
->SetWindow(tc
);
2598 bool res
= validator
->Validate(propGrid
);
2603 bool wxPGInDialogValidator::DoValidate( wxPropertyGrid
* WXUNUSED(propGrid
),
2604 wxValidator
* WXUNUSED(validator
),
2605 const wxString
& WXUNUSED(value
) )
2611 // -----------------------------------------------------------------------
2613 #endif // wxUSE_PROPGRID