1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/property.cpp
3 // Purpose: wxPGProperty and related support 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/dcmemory.h"
34 #include "wx/settings.h"
38 #include "wx/propgrid/propgrid.h"
41 #define PWC_CHILD_SUMMARY_LIMIT 16 // Maximum number of children summarized in a parent property's
44 #define PWC_CHILD_SUMMARY_CHAR_LIMIT 64 // Character limit of summary field when not editing
46 #if wxPG_COMPATIBILITY_1_4
48 // Used to establish backwards compatiblity
49 const char* g_invalidStringContent
= "@__TOTALLY_INVALID_STRING__@";
53 // -----------------------------------------------------------------------
55 static void wxPGDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
57 #if defined(__WXMSW__) && !defined(__WXWINCE__)
58 // FIXME: Use DrawFocusRect code above (currently it draws solid line
59 // for caption focus but works ok for other stuff).
60 // Also, it seems that this code may not work in future wx versions.
61 dc
.SetLogicalFunction(wxINVERT
);
63 wxPen
pen(*wxBLACK
,1,wxDOT
);
64 pen
.SetCap(wxCAP_BUTT
);
66 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
68 dc
.DrawRectangle(rect
);
70 dc
.SetLogicalFunction(wxCOPY
);
72 dc
.SetLogicalFunction(wxINVERT
);
74 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
75 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
77 dc
.DrawRectangle(rect
);
79 dc
.SetLogicalFunction(wxCOPY
);
83 // -----------------------------------------------------------------------
85 // -----------------------------------------------------------------------
87 wxSize
wxPGCellRenderer::GetImageSize( const wxPGProperty
* WXUNUSED(property
),
89 int WXUNUSED(item
) ) const
94 void wxPGCellRenderer::DrawText( wxDC
& dc
, const wxRect
& rect
,
95 int xOffset
, const wxString
& text
) const
98 rect
.x
+xOffset
+wxPG_XBEFORETEXT
,
99 rect
.y
+((rect
.height
-dc
.GetCharHeight())/2) );
102 void wxPGCellRenderer::DrawEditorValue( wxDC
& dc
, const wxRect
& rect
,
103 int xOffset
, const wxString
& text
,
104 wxPGProperty
* property
,
105 const wxPGEditor
* editor
) const
107 int yOffset
= ((rect
.height
-dc
.GetCharHeight())/2);
114 rect2
.height
-= yOffset
;
115 editor
->DrawValue( dc
, rect2
, property
, text
);
120 rect
.x
+xOffset
+wxPG_XBEFORETEXT
,
125 void wxPGCellRenderer::DrawCaptionSelectionRect( wxDC
& dc
, int x
, int y
, int w
, int h
) const
127 wxRect
focusRect(x
,y
+((h
-dc
.GetCharHeight())/2),w
,h
);
128 wxPGDrawFocusRect(dc
,focusRect
);
131 int wxPGCellRenderer::PreDrawCell( wxDC
& dc
, const wxRect
& rect
, const wxPGCell
& cell
, int flags
) const
135 // If possible, use cell colours
136 if ( !(flags
& DontUseCellBgCol
) )
138 dc
.SetPen(cell
.GetBgCol());
139 dc
.SetBrush(cell
.GetBgCol());
142 if ( !(flags
& DontUseCellFgCol
) )
144 dc
.SetTextForeground(cell
.GetFgCol());
147 // Draw Background, but only if not rendering in control
148 // (as control already has rendered correct background).
149 if ( !(flags
& (Control
|ChoicePopup
)) )
150 dc
.DrawRectangle(rect
);
152 const wxBitmap
& bmp
= cell
.GetBitmap();
154 // Do not draw oversized bitmap outside choice popup
155 ((flags
& ChoicePopup
) || bmp
.GetHeight() < rect
.height
)
159 rect
.x
+ wxPG_CONTROL_MARGIN
+ wxCC_CUSTOM_IMAGE_MARGIN1
,
160 rect
.y
+ wxPG_CUSTOM_IMAGE_SPACINGY
,
162 imageWidth
= bmp
.GetWidth();
168 // -----------------------------------------------------------------------
169 // wxPGDefaultRenderer
170 // -----------------------------------------------------------------------
172 void wxPGDefaultRenderer::Render( wxDC
& dc
, const wxRect
& rect
,
173 const wxPropertyGrid
* propertyGrid
, wxPGProperty
* property
,
174 int column
, int item
, int flags
) const
176 bool isUnspecified
= property
->IsValueUnspecified();
178 if ( column
== 1 && item
== -1 )
180 int cmnVal
= property
->GetCommonValue();
184 if ( !isUnspecified
)
185 DrawText( dc
, rect
, 0, propertyGrid
->GetCommonValueLabel(cmnVal
) );
190 const wxPGEditor
* editor
= NULL
;
191 const wxPGCell
* cell
= NULL
;
195 int preDrawFlags
= flags
;
197 property
->GetDisplayInfo(column
, item
, flags
, &text
, &cell
);
199 imageWidth
= PreDrawCell( dc
, rect
, *cell
, preDrawFlags
);
203 if ( !isUnspecified
)
205 editor
= property
->GetColumnEditor(column
);
207 // Regular property value
209 wxSize imageSize
= propertyGrid
->GetImageSize(property
, item
);
211 wxPGPaintData paintdata
;
212 paintdata
.m_parent
= propertyGrid
;
213 paintdata
.m_choiceItem
= item
;
215 if ( imageSize
.x
> 0 )
217 wxRect
imageRect(rect
.x
+ wxPG_CONTROL_MARGIN
+ wxCC_CUSTOM_IMAGE_MARGIN1
,
218 rect
.y
+wxPG_CUSTOM_IMAGE_SPACINGY
,
219 wxPG_CUSTOM_IMAGE_WIDTH
,
220 rect
.height
-(wxPG_CUSTOM_IMAGE_SPACINGY
*2));
222 dc
.SetPen( wxPen(propertyGrid
->GetCellTextColour(), 1, wxSOLID
) );
224 paintdata
.m_drawnWidth
= imageSize
.x
;
225 paintdata
.m_drawnHeight
= imageSize
.y
;
227 property
->OnCustomPaint( dc
, imageRect
, paintdata
);
229 imageWidth
= paintdata
.m_drawnWidth
;
232 text
= property
->GetValueAsString();
235 if ( propertyGrid
->GetColumnCount() <= 2 )
237 wxString unitsString
= property
->GetAttribute(wxPGGlobalVars
->m_strUnits
, wxEmptyString
);
238 if ( unitsString
.length() )
239 text
= wxString::Format(wxS("%s %s"), text
.c_str(), unitsString
.c_str() );
243 if ( text
.length() == 0 )
245 // Try to show inline help if no text
246 wxVariant vInlineHelp
= property
->GetAttribute(wxPGGlobalVars
->m_strInlineHelp
);
247 if ( !vInlineHelp
.IsNull() )
249 text
= vInlineHelp
.GetString();
250 dc
.SetTextForeground(propertyGrid
->GetCellDisabledTextColour());
255 int imageOffset
= property
->GetImageOffset(imageWidth
);
257 DrawEditorValue( dc
, rect
, imageOffset
, text
, property
, editor
);
259 // active caption gets nice dotted rectangle
260 if ( property
->IsCategory() /*&& column == 0*/ )
262 if ( flags
& Selected
)
264 if ( imageWidth
> 0 )
266 imageOffset
-= DEFAULT_IMAGE_OFFSET_INCREMENT
;
267 imageWidth
+= wxCC_CUSTOM_IMAGE_MARGIN2
+ 4;
270 DrawCaptionSelectionRect( dc
,
271 rect
.x
+wxPG_XBEFORETEXT
-wxPG_CAPRECTXMARGIN
+imageOffset
,
272 rect
.y
-wxPG_CAPRECTYMARGIN
+1,
273 ((wxPropertyCategory
*)property
)->GetTextExtent(propertyGrid
,
274 propertyGrid
->GetCaptionFont())
275 +(wxPG_CAPRECTXMARGIN
*2),
276 propertyGrid
->GetFontHeight()+(wxPG_CAPRECTYMARGIN
*2) );
281 wxSize
wxPGDefaultRenderer::GetImageSize( const wxPGProperty
* property
,
285 if ( property
&& column
== 1 )
289 wxBitmap
* bmp
= property
->GetValueImage();
291 if ( bmp
&& bmp
->Ok() )
292 return wxSize(bmp
->GetWidth(),bmp
->GetHeight());
298 // -----------------------------------------------------------------------
300 // -----------------------------------------------------------------------
302 wxPGCellData::wxPGCellData()
305 m_hasValidText
= false;
308 // -----------------------------------------------------------------------
310 // -----------------------------------------------------------------------
317 wxPGCell::wxPGCell( const wxString
& text
,
318 const wxBitmap
& bitmap
,
319 const wxColour
& fgCol
,
320 const wxColour
& bgCol
)
323 wxPGCellData
* data
= new wxPGCellData();
326 data
->m_bitmap
= bitmap
;
327 data
->m_fgCol
= fgCol
;
328 data
->m_bgCol
= bgCol
;
329 data
->m_hasValidText
= true;
332 wxObjectRefData
*wxPGCell::CloneRefData( const wxObjectRefData
*data
) const
334 wxPGCellData
* c
= new wxPGCellData();
335 const wxPGCellData
* o
= (const wxPGCellData
*) data
;
336 c
->m_text
= o
->m_text
;
337 c
->m_bitmap
= o
->m_bitmap
;
338 c
->m_fgCol
= o
->m_fgCol
;
339 c
->m_bgCol
= o
->m_bgCol
;
340 c
->m_hasValidText
= o
->m_hasValidText
;
344 void wxPGCell::SetText( const wxString
& text
)
348 GetData()->SetText(text
);
351 void wxPGCell::SetBitmap( const wxBitmap
& bitmap
)
355 GetData()->SetBitmap(bitmap
);
358 void wxPGCell::SetFgCol( const wxColour
& col
)
362 GetData()->SetFgCol(col
);
365 void wxPGCell::SetBgCol( const wxColour
& col
)
369 GetData()->SetBgCol(col
);
372 void wxPGCell::MergeFrom( const wxPGCell
& srcCell
)
376 wxPGCellData
* data
= GetData();
378 if ( srcCell
.HasText() )
379 data
->SetText(srcCell
.GetText());
381 if ( srcCell
.GetFgCol().IsOk() )
382 data
->SetFgCol(srcCell
.GetFgCol());
384 if ( srcCell
.GetBgCol().IsOk() )
385 data
->SetBgCol(srcCell
.GetBgCol());
387 if ( srcCell
.GetBitmap().IsOk() )
388 data
->SetBitmap(srcCell
.GetBitmap());
391 // -----------------------------------------------------------------------
393 // -----------------------------------------------------------------------
395 IMPLEMENT_ABSTRACT_CLASS(wxPGProperty
, wxObject
)
397 wxString
* wxPGProperty::sm_wxPG_LABEL
= NULL
;
399 void wxPGProperty::Init()
405 m_parentState
= NULL
;
408 m_clientObject
= NULL
;
410 m_customEditor
= NULL
;
414 m_valueBitmap
= NULL
;
416 m_maxLen
= 0; // infinite maximum length
418 m_flags
= wxPG_PROP_PROPERTY
;
426 void wxPGProperty::Init( const wxString
& label
, const wxString
& name
)
428 // We really need to check if &label and &name are NULL pointers
429 // (this can if we are called before property grid has been initalized)
431 if ( (&label
) != NULL
&& label
!= wxPG_LABEL
)
434 if ( (&name
) != NULL
&& name
!= wxPG_LABEL
)
437 DoSetName( m_label
);
442 void wxPGProperty::InitAfterAdded( wxPropertyGridPageState
* pageState
,
443 wxPropertyGrid
* propgrid
)
446 // Called after property has been added to grid or page
447 // (so propgrid can be NULL, too).
449 wxPGProperty
* parent
= m_parent
;
450 bool parentIsRoot
= parent
->IsKindOf(CLASSINFO(wxPGRootProperty
));
452 m_parentState
= pageState
;
454 #if wxPG_COMPATIBILITY_1_4
455 // Make sure deprecated virtual functions are not implemented
456 wxString s
= GetValueAsString( 0xFFFF );
457 wxASSERT_MSG( s
== g_invalidStringContent
,
458 "Implement ValueToString() instead of GetValueAsString()" );
461 if ( !parentIsRoot
&& !parent
->IsCategory() )
463 m_cells
= parent
->m_cells
;
466 // If in hideable adding mode, or if assigned parent is hideable, then
467 // make this one hideable.
469 ( !parentIsRoot
&& parent
->HasFlag(wxPG_PROP_HIDDEN
) ) ||
470 ( propgrid
&& (propgrid
->HasInternalFlag(wxPG_FL_ADDING_HIDEABLES
)) )
472 SetFlag( wxPG_PROP_HIDDEN
);
474 // Set custom image flag.
475 int custImgHeight
= OnMeasureImage().y
;
476 if ( custImgHeight
< 0 )
478 SetFlag(wxPG_PROP_CUSTOMIMAGE
);
481 if ( propgrid
&& (propgrid
->HasFlag(wxPG_LIMITED_EDITING
)) )
482 SetFlag(wxPG_PROP_NOEDITOR
);
484 // Make sure parent has some parental flags
485 if ( !parent
->HasFlag(wxPG_PROP_PARENTAL_FLAGS
) )
486 parent
->SetParentalType(wxPG_PROP_MISC_PARENT
);
490 // This is not a category.
494 unsigned char depth
= 1;
497 depth
= parent
->m_depth
;
498 if ( !parent
->IsCategory() )
502 unsigned char greyDepth
= depth
;
506 wxPropertyCategory
* pc
;
508 if ( parent
->IsCategory() )
509 pc
= (wxPropertyCategory
* ) parent
;
511 // This conditional compile is necessary to
512 // bypass some compiler bug.
513 pc
= pageState
->GetPropertyCategory(parent
);
516 greyDepth
= pc
->GetDepth();
518 greyDepth
= parent
->m_depthBgCol
;
521 m_depthBgCol
= greyDepth
;
525 // This is a category.
528 unsigned char depth
= 1;
531 depth
= parent
->m_depth
+ 1;
534 m_depthBgCol
= depth
;
538 // Has initial children
539 if ( GetChildCount() )
541 // Check parental flags
542 wxASSERT_MSG( ((m_flags
& wxPG_PROP_PARENTAL_FLAGS
) ==
543 wxPG_PROP_AGGREGATE
) ||
544 ((m_flags
& wxPG_PROP_PARENTAL_FLAGS
) ==
545 wxPG_PROP_MISC_PARENT
),
546 "wxPGProperty parental flags set incorrectly at "
549 if ( HasFlag(wxPG_PROP_AGGREGATE
) )
551 // Properties with private children are not expanded by default.
554 else if ( propgrid
&& propgrid
->HasFlag(wxPG_HIDE_MARGIN
) )
556 // ...unless it cannot be expanded by user and therefore must
557 // remain visible at all times
562 // Prepare children recursively
563 for ( unsigned int i
=0; i
<GetChildCount(); i
++ )
565 wxPGProperty
* child
= Item(i
);
566 child
->InitAfterAdded(pageState
, pageState
->GetGrid());
569 if ( propgrid
&& (propgrid
->GetExtraStyle() & wxPG_EX_AUTO_UNSPECIFIED_VALUES
) )
570 SetFlagRecursively(wxPG_PROP_AUTO_UNSPECIFIED
, true);
574 wxPGProperty::wxPGProperty()
581 wxPGProperty::wxPGProperty( const wxString
& label
, const wxString
& name
)
588 wxPGProperty::~wxPGProperty()
590 delete m_clientObject
;
592 Empty(); // this deletes items
594 delete m_valueBitmap
;
599 // This makes it easier for us to detect dangling pointers
604 bool wxPGProperty::IsSomeParent( wxPGProperty
* candidate
) const
606 wxPGProperty
* parent
= m_parent
;
609 if ( parent
== candidate
)
611 parent
= parent
->m_parent
;
617 wxString
wxPGProperty::GetName() const
619 wxPGProperty
* parent
= GetParent();
621 if ( !m_name
.length() || !parent
|| parent
->IsCategory() || parent
->IsRoot() )
624 return m_parent
->GetName() + wxS(".") + m_name
;
627 wxPropertyGrid
* wxPGProperty::GetGrid() const
629 if ( !m_parentState
)
631 return m_parentState
->GetGrid();
634 int wxPGProperty::Index( const wxPGProperty
* p
) const
636 for ( unsigned int i
= 0; i
<m_children
.size(); i
++ )
638 if ( p
== m_children
[i
] )
644 bool wxPGProperty::ValidateValue( wxVariant
& WXUNUSED(value
), wxPGValidationInfo
& WXUNUSED(validationInfo
) ) const
649 void wxPGProperty::OnSetValue()
653 void wxPGProperty::RefreshChildren ()
657 void wxPGProperty::OnValidationFailure( wxVariant
& WXUNUSED(pendingValue
) )
661 void wxPGProperty::GetDisplayInfo( unsigned int column
,
665 const wxPGCell
** pCell
)
667 const wxPGCell
* cell
= NULL
;
669 if ( !(flags
& wxPGCellRenderer::ChoicePopup
) )
671 // Not painting listi of choice popups, so get text from property
672 cell
= &GetCell(column
);
673 if ( cell
->HasText() )
675 *pString
= cell
->GetText();
680 *pString
= GetLabel();
681 else if ( column
== 1 )
682 *pString
= GetDisplayedString();
683 else if ( column
== 2 )
684 *pString
= GetAttribute(wxPGGlobalVars
->m_strUnits
, wxEmptyString
);
689 wxASSERT( column
== 1 );
691 if ( choiceIndex
!= wxNOT_FOUND
)
693 const wxPGChoiceEntry
& entry
= m_choices
[choiceIndex
];
694 if ( entry
.GetBitmap().IsOk() ||
695 entry
.GetFgCol().IsOk() ||
696 entry
.GetBgCol().IsOk() )
698 *pString
= m_choices
.GetLabel(choiceIndex
);
703 cell
= &GetCell(column
);
705 wxASSERT_MSG( cell
->GetData(),
706 wxString::Format("Invalid cell for property %s",
707 GetName().c_str()) );
713 wxString wxPGProperty::GetColumnText( unsigned int col, int choiceIndex ) const
716 if ( col != 1 || choiceIndex == wxNOT_FOUND )
718 const wxPGCell& cell = GetCell(col);
719 if ( cell->HasText() )
721 return cell->GetText();
728 return GetDisplayedString();
730 return GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
736 return m_choices.GetLabel(choiceIndex);
739 return wxEmptyString;
743 void wxPGProperty::DoGenerateComposedValue( wxString
& text
,
745 const wxVariantList
* valueOverrides
,
746 wxPGHashMapS2S
* childResults
) const
749 int iMax
= m_children
.size();
755 if ( iMax
> PWC_CHILD_SUMMARY_LIMIT
&&
756 !(argFlags
& wxPG_FULL_VALUE
) )
757 iMax
= PWC_CHILD_SUMMARY_LIMIT
;
759 int iMaxMinusOne
= iMax
-1;
761 if ( !IsTextEditable() )
762 argFlags
|= wxPG_UNEDITABLE_COMPOSITE_FRAGMENT
;
764 wxPGProperty
* curChild
= m_children
[0];
766 bool overridesLeft
= false;
767 wxVariant overrideValue
;
768 wxVariantList::const_iterator node
;
770 if ( valueOverrides
)
772 node
= valueOverrides
->begin();
773 if ( node
!= valueOverrides
->end() )
775 overrideValue
= *node
;
776 overridesLeft
= true;
780 for ( i
= 0; i
< iMax
; i
++ )
782 wxVariant childValue
;
784 wxString childLabel
= curChild
->GetLabel();
786 // Check for value override
787 if ( overridesLeft
&& overrideValue
.GetName() == childLabel
)
789 if ( !overrideValue
.IsNull() )
790 childValue
= overrideValue
;
792 childValue
= curChild
->GetValue();
794 if ( node
!= valueOverrides
->end() )
795 overrideValue
= *node
;
797 overridesLeft
= false;
801 childValue
= curChild
->GetValue();
805 if ( !childValue
.IsNull() )
807 if ( overridesLeft
&&
808 curChild
->HasFlag(wxPG_PROP_COMPOSED_VALUE
) &&
809 childValue
.GetType() == wxPG_VARIANT_TYPE_LIST
)
811 wxVariantList
& childList
= childValue
.GetList();
812 DoGenerateComposedValue(s
, argFlags
|wxPG_COMPOSITE_FRAGMENT
,
813 &childList
, childResults
);
817 s
= curChild
->ValueToString(childValue
,
818 argFlags
|wxPG_COMPOSITE_FRAGMENT
);
822 if ( childResults
&& curChild
->GetChildCount() )
823 (*childResults
)[curChild
->GetName()] = s
;
826 if ( (argFlags
& wxPG_UNEDITABLE_COMPOSITE_FRAGMENT
) && !s
.length() )
829 if ( !curChild
->GetChildCount() || skip
)
832 text
+= wxS("[") + s
+ wxS("]");
834 if ( i
< iMaxMinusOne
)
836 if ( text
.length() > PWC_CHILD_SUMMARY_CHAR_LIMIT
&&
837 !(argFlags
& wxPG_EDITABLE_VALUE
) &&
838 !(argFlags
& wxPG_FULL_VALUE
) )
843 if ( !curChild
->GetChildCount() )
849 curChild
= m_children
[i
+1];
853 if ( (unsigned int)i
< m_children
.size() )
855 if ( !text
.EndsWith(wxS("; ")) )
856 text
+= wxS("; ...");
862 wxString
wxPGProperty::ValueToString( wxVariant
& WXUNUSED(value
),
865 wxCHECK_MSG( GetChildCount() > 0,
867 "If user property does not have any children, it must "
868 "override GetValueAsString" );
870 // FIXME: Currently code below only works if value is actually m_value
871 wxASSERT_MSG( argFlags
& wxPG_VALUE_IS_CURRENT
,
872 "Sorry, currently default wxPGProperty::ValueToString() "
873 "implementation only works if value is m_value." );
876 DoGenerateComposedValue(text
, argFlags
);
880 wxString
wxPGProperty::GetValueAsString( int argFlags
) const
882 #if wxPG_COMPATIBILITY_1_4
883 // This is backwards compatibility test
884 // That is, to make sure this function is not overridden
885 // (instead, ValueToString() should be).
886 if ( argFlags
== 0xFFFF )
888 // Do not override! (for backwards compliancy)
889 return g_invalidStringContent
;
893 if ( IsValueUnspecified() )
894 return wxEmptyString
;
896 if ( m_commonValue
== -1 )
898 wxVariant
value(GetValue());
899 return ValueToString(value
, argFlags
|wxPG_VALUE_IS_CURRENT
);
903 // Return common value's string representation
904 wxPropertyGrid
* pg
= GetGrid();
905 const wxPGCommonValue
* cv
= pg
->GetCommonValue(m_commonValue
);
907 if ( argFlags
& wxPG_FULL_VALUE
)
909 return cv
->GetLabel();
911 else if ( argFlags
& wxPG_EDITABLE_VALUE
)
913 return cv
->GetEditableText();
917 return cv
->GetLabel();
921 wxString
wxPGProperty::GetValueString( int argFlags
) const
923 return GetValueAsString(argFlags
);
926 bool wxPGProperty::IntToValue( wxVariant
& variant
, int number
, int WXUNUSED(argFlags
) ) const
928 variant
= (long)number
;
932 // Convert semicolon delimited tokens into child values.
933 bool wxPGProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int argFlags
) const
935 if ( !GetChildCount() )
938 unsigned int curChild
= 0;
940 unsigned int iMax
= m_children
.size();
942 if ( iMax
> PWC_CHILD_SUMMARY_LIMIT
&&
943 !(argFlags
& wxPG_FULL_VALUE
) )
944 iMax
= PWC_CHILD_SUMMARY_LIMIT
;
946 bool changed
= false;
951 // Its best only to add non-empty group items
952 bool addOnlyIfNotEmpty
= false;
953 const wxChar delimeter
= wxS(';');
955 size_t tokenStart
= 0xFFFFFF;
957 wxVariantList temp_list
;
958 wxVariant
list(temp_list
);
960 int propagatedFlags
= argFlags
& (wxPG_REPORT_ERROR
|wxPG_PROGRAMMATIC_VALUE
);
962 wxLogTrace("propgrid",
963 wxT(">> %s.StringToValue('%s')"), GetLabel(), text
);
965 wxString::const_iterator it
= text
.begin();
968 if ( it
!= text
.end() )
975 // How many units we iterate string forward at the end of loop?
976 // We need to keep track of this or risk going to negative
977 // with it-- operation.
978 unsigned int strPosIncrement
= 1;
980 if ( tokenStart
!= 0xFFFFFF )
983 if ( a
== delimeter
|| a
== 0 )
985 token
= text
.substr(tokenStart
,pos
-tokenStart
);
987 size_t len
= token
.length();
989 if ( !addOnlyIfNotEmpty
|| len
> 0 )
991 const wxPGProperty
* child
= Item(curChild
);
992 wxVariant
variant(child
->GetValue());
993 wxString childName
= child
->GetBaseName();
995 wxLogTrace("propgrid",
996 wxT("token = '%s', child = %s"),
999 // Add only if editable or setting programmatically
1000 if ( (argFlags
& wxPG_PROGRAMMATIC_VALUE
) ||
1001 !child
->HasFlag(wxPG_PROP_DISABLED
|wxPG_PROP_READONLY
) )
1005 if ( child
->StringToValue(variant
, token
,
1006 propagatedFlags
|wxPG_COMPOSITE_FRAGMENT
) )
1008 // We really need to set the variant's name
1009 // *after* child->StringToValue() has been
1010 // called, since variant's value may be set by
1011 // assigning another variant into it, which
1012 // then usually causes name to be copied (ie.
1013 // usually cleared) as well. wxBoolProperty
1014 // being case in point with its use of
1015 // wxPGVariant_Bool macro as an optimization.
1016 variant
.SetName(childName
);
1017 list
.Append(variant
);
1024 // Empty, becomes unspecified
1026 variant
.SetName(childName
);
1027 list
.Append(variant
);
1033 if ( curChild
>= iMax
)
1037 tokenStart
= 0xFFFFFF;
1042 // Token is not running
1043 if ( a
!= wxS(' ') )
1046 addOnlyIfNotEmpty
= false;
1048 // Is this a group of tokens?
1049 if ( a
== wxS('[') )
1053 if ( it
!= text
.end() ) ++it
;
1055 size_t startPos
= pos
;
1057 // Group item - find end
1058 while ( it
!= text
.end() && depth
> 0 )
1064 if ( a
== wxS(']') )
1066 else if ( a
== wxS('[') )
1070 token
= text
.substr(startPos
,pos
-startPos
-1);
1072 if ( !token
.length() )
1075 const wxPGProperty
* child
= Item(curChild
);
1077 wxVariant oldChildValue
= child
->GetValue();
1078 wxVariant
variant(oldChildValue
);
1080 if ( (argFlags
& wxPG_PROGRAMMATIC_VALUE
) ||
1081 !child
->HasFlag(wxPG_PROP_DISABLED
|wxPG_PROP_READONLY
) )
1083 wxString childName
= child
->GetBaseName();
1085 bool stvRes
= child
->StringToValue( variant
, token
,
1087 if ( stvRes
|| (variant
!= oldChildValue
) )
1089 variant
.SetName(childName
);
1090 list
.Append(variant
);
1101 if ( curChild
>= iMax
)
1104 addOnlyIfNotEmpty
= true;
1106 tokenStart
= 0xFFFFFF;
1112 if ( a
== delimeter
)
1113 strPosIncrement
-= 1;
1121 it
+= strPosIncrement
;
1123 if ( it
!= text
.end() )
1132 pos
+= strPosIncrement
;
1141 bool wxPGProperty::SetValueFromString( const wxString
& text
, int argFlags
)
1143 wxVariant
variant(m_value
);
1144 bool res
= StringToValue(variant
, text
, argFlags
);
1150 bool wxPGProperty::SetValueFromInt( long number
, int argFlags
)
1152 wxVariant
variant(m_value
);
1153 bool res
= IntToValue(variant
, number
, argFlags
);
1159 wxSize
wxPGProperty::OnMeasureImage( int WXUNUSED(item
) ) const
1161 if ( m_valueBitmap
)
1162 return wxSize(m_valueBitmap
->GetWidth(),-1);
1167 int wxPGProperty::GetImageOffset( int imageWidth
) const
1169 int imageOffset
= 0;
1173 // Do not increment offset too much for wide images
1174 if ( imageWidth
<= (wxPG_CUSTOM_IMAGE_WIDTH
+5) )
1175 imageOffset
= imageWidth
+ DEFAULT_IMAGE_OFFSET_INCREMENT
;
1177 imageOffset
= imageWidth
+ 1;
1183 wxPGCellRenderer
* wxPGProperty::GetCellRenderer( int WXUNUSED(column
) ) const
1185 return wxPGGlobalVars
->m_defaultRenderer
;
1188 void wxPGProperty::OnCustomPaint( wxDC
& dc
,
1192 wxBitmap
* bmp
= m_valueBitmap
;
1194 wxCHECK_RET( bmp
&& bmp
->Ok(), wxT("invalid bitmap") );
1196 wxCHECK_RET( rect
.x
>= 0, wxT("unexpected measure call") );
1198 dc
.DrawBitmap(*bmp
,rect
.x
,rect
.y
);
1201 const wxPGEditor
* wxPGProperty::DoGetEditorClass() const
1203 return wxPGEditor_TextCtrl
;
1206 // Default extra property event handling - that is, none at all.
1207 bool wxPGProperty::OnEvent( wxPropertyGrid
*, wxWindow
*, wxEvent
& )
1213 void wxPGProperty::SetValue( wxVariant value
, wxVariant
* pList
, int flags
)
1215 // If auto unspecified values are not wanted (via window or property style),
1216 // then get default value instead of wxNullVariant.
1217 if ( value
.IsNull() && (flags
& wxPG_SETVAL_BY_USER
) &&
1218 !UsesAutoUnspecified() )
1220 value
= GetDefaultValue();
1223 if ( !value
.IsNull() )
1225 wxVariant tempListVariant
;
1228 // List variants are reserved a special purpose
1229 // as intermediate containers for child values
1230 // of properties with children.
1231 if ( value
.GetType() == wxPG_VARIANT_TYPE_LIST
)
1234 // However, situation is different for composed string properties
1235 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE
) )
1237 tempListVariant
= value
;
1238 pList
= &tempListVariant
;
1242 AdaptListToValue(value
, &newValue
);
1244 //wxLogDebug(wxT(">> %s.SetValue() adapted list value to type '%s'"),GetName().c_str(),value.GetType().c_str());
1247 if ( HasFlag( wxPG_PROP_AGGREGATE
) )
1248 flags
|= wxPG_SETVAL_AGGREGATED
;
1250 if ( pList
&& !pList
->IsNull() )
1252 wxASSERT( pList
->GetType() == wxPG_VARIANT_TYPE_LIST
);
1253 wxASSERT( GetChildCount() );
1254 wxASSERT( !IsCategory() );
1256 wxVariantList
& list
= pList
->GetList();
1257 wxVariantList::iterator node
;
1260 //wxLogDebug(wxT(">> %s.SetValue() pList parsing"),GetName().c_str());
1262 // Children in list can be in any order, but we will give hint to
1263 // GetPropertyByNameWH(). This optimizes for full list parsing.
1264 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1266 wxVariant
& childValue
= *((wxVariant
*)*node
);
1267 wxPGProperty
* child
= GetPropertyByNameWH(childValue
.GetName(), i
);
1270 //wxLogDebug(wxT("%i: child = %s, childValue.GetType()=%s"),i,child->GetBaseName().c_str(),childValue.GetType().c_str());
1271 if ( childValue
.GetType() == wxPG_VARIANT_TYPE_LIST
)
1273 if ( child
->HasFlag(wxPG_PROP_AGGREGATE
) && !(flags
& wxPG_SETVAL_AGGREGATED
) )
1275 wxVariant listRefCopy
= childValue
;
1276 child
->SetValue(childValue
, &listRefCopy
, flags
|wxPG_SETVAL_FROM_PARENT
);
1280 wxVariant oldVal
= child
->GetValue();
1281 child
->SetValue(oldVal
, &childValue
, flags
|wxPG_SETVAL_FROM_PARENT
);
1284 else if ( child
->GetValue() != childValue
)
1286 // For aggregate properties, we will trust RefreshChildren()
1287 // to update child values.
1288 if ( !HasFlag(wxPG_PROP_AGGREGATE
) )
1289 child
->SetValue(childValue
, NULL
, flags
|wxPG_SETVAL_FROM_PARENT
);
1290 if ( flags
& wxPG_SETVAL_BY_USER
)
1291 child
->SetFlag(wxPG_PROP_MODIFIED
);
1298 if ( !value
.IsNull() )
1304 if ( flags
& wxPG_SETVAL_BY_USER
)
1305 SetFlag(wxPG_PROP_MODIFIED
);
1307 if ( HasFlag(wxPG_PROP_AGGREGATE
) )
1312 if ( m_commonValue
!= -1 )
1314 wxPropertyGrid
* pg
= GetGrid();
1315 if ( !pg
|| m_commonValue
!= pg
->GetUnspecifiedCommonValue() )
1321 // Set children to unspecified, but only if aggregate or
1322 // value is <composed>
1323 if ( AreChildrenComponents() )
1326 for ( i
=0; i
<GetChildCount(); i
++ )
1327 Item(i
)->SetValue(value
, NULL
, flags
|wxPG_SETVAL_FROM_PARENT
);
1331 if ( !(flags
& wxPG_SETVAL_FROM_PARENT
) )
1332 UpdateParentValues();
1335 // Update editor control
1338 // We need to check for these, otherwise GetGrid() may fail.
1339 if ( flags
& wxPG_SETVAL_REFRESH_EDITOR
)
1342 wxPropertyGrid
* pg
= GetGridIfDisplayed();
1344 pg
->DrawItemAndValueRelated(this);
1349 void wxPGProperty::SetValueInEvent( wxVariant value
) const
1351 GetGrid()->ValueChangeInEvent(value
);
1354 void wxPGProperty::SetFlagRecursively( FlagType flag
, bool set
)
1356 ChangeFlag(flag
, set
);
1359 for ( i
= 0; i
< GetChildCount(); i
++ )
1360 Item(i
)->SetFlagRecursively(flag
, set
);
1363 void wxPGProperty::RefreshEditor()
1368 wxPropertyGrid
* pg
= GetGrid();
1369 if ( pg
&& pg
->GetSelectedProperty() == this )
1370 pg
->RefreshEditor();
1373 wxVariant
wxPGProperty::GetDefaultValue() const
1375 wxVariant defVal
= GetAttribute(wxPG_ATTR_DEFAULT_VALUE
);
1376 if ( !defVal
.IsNull() )
1379 wxVariant value
= GetValue();
1381 if ( !value
.IsNull() )
1383 wxString
valueType(value
.GetType());
1385 if ( valueType
== wxPG_VARIANT_TYPE_LONG
)
1386 return wxPGVariant_Zero
;
1387 if ( valueType
== wxPG_VARIANT_TYPE_STRING
)
1388 return wxPGVariant_EmptyString
;
1389 if ( valueType
== wxPG_VARIANT_TYPE_BOOL
)
1390 return wxPGVariant_False
;
1391 if ( valueType
== wxPG_VARIANT_TYPE_DOUBLE
)
1392 return wxVariant(0.0);
1393 if ( valueType
== wxPG_VARIANT_TYPE_ARRSTRING
)
1394 return wxVariant(wxArrayString());
1395 if ( valueType
== wxS("wxLongLong") )
1396 return WXVARIANT(wxLongLong(0));
1397 if ( valueType
== wxS("wxULongLong") )
1398 return WXVARIANT(wxULongLong(0));
1399 if ( valueType
== wxS("wxColour") )
1400 return WXVARIANT(*wxBLACK
);
1402 if ( valueType
== wxPG_VARIANT_TYPE_DATETIME
)
1403 return wxVariant(wxDateTime::Now());
1405 if ( valueType
== wxS("wxFont") )
1406 return WXVARIANT(*wxNORMAL_FONT
);
1407 if ( valueType
== wxS("wxPoint") )
1408 return WXVARIANT(wxPoint(0, 0));
1409 if ( valueType
== wxS("wxSize") )
1410 return WXVARIANT(wxSize(0, 0));
1416 void wxPGProperty::EnsureCells( unsigned int column
)
1418 if ( column
>= m_cells
.size() )
1420 // Fill empty slots with default cells
1421 wxPropertyGrid
* pg
= GetGrid();
1422 wxPGCell defaultCell
;
1424 if ( !HasFlag(wxPG_PROP_CATEGORY
) )
1425 defaultCell
= pg
->GetPropertyDefaultCell();
1427 defaultCell
= pg
->GetCategoryDefaultCell();
1429 // TODO: Replace with resize() call
1430 unsigned int cellCountMax
= column
+1;
1432 for ( unsigned int i
=m_cells
.size(); i
<cellCountMax
; i
++ )
1433 m_cells
.push_back(defaultCell
);
1437 void wxPGProperty::SetCell( int column
,
1438 const wxPGCell
& cell
)
1440 EnsureCells(column
);
1442 m_cells
[column
] = cell
;
1445 void wxPGProperty::AdaptiveSetCell( unsigned int firstCol
,
1446 unsigned int lastCol
,
1447 const wxPGCell
& cell
,
1448 const wxPGCell
& srcData
,
1449 wxPGCellData
* unmodCellData
,
1450 FlagType ignoreWithFlags
,
1454 // Sets cell in memory optimizing fashion. That is, if
1455 // current cell data matches unmodCellData, we will
1456 // simply get reference to data from cell. Otherwise,
1457 // cell information from srcData is merged into current.
1460 if ( !(m_flags
& ignoreWithFlags
) && !IsRoot() )
1462 EnsureCells(lastCol
);
1464 for ( unsigned int col
=firstCol
; col
<=lastCol
; col
++ )
1466 if ( m_cells
[col
].GetData() == unmodCellData
)
1468 // Data matches... use cell directly
1469 m_cells
[col
] = cell
;
1473 // Data did not match... merge valid information
1474 m_cells
[col
].MergeFrom(srcData
);
1481 for ( unsigned int i
=0; i
<GetChildCount(); i
++ )
1482 Item(i
)->AdaptiveSetCell( firstCol
,
1492 const wxPGCell
& wxPGProperty::GetCell( unsigned int column
) const
1494 if ( m_cells
.size() > column
)
1495 return m_cells
[column
];
1497 wxPropertyGrid
* pg
= GetGrid();
1500 return pg
->GetCategoryDefaultCell();
1502 return pg
->GetPropertyDefaultCell();
1505 wxPGCell
& wxPGProperty::GetCell( unsigned int column
)
1507 EnsureCells(column
);
1508 return m_cells
[column
];
1511 void wxPGProperty::SetBackgroundColour( const wxColour
& colour
,
1514 wxPGProperty
* firstProp
= this;
1517 // If category is tried to set recursively, skip it and only
1518 // affect the children.
1521 while ( firstProp
->IsCategory() )
1523 if ( !firstProp
->GetChildCount() )
1525 firstProp
= firstProp
->Item(0);
1529 wxPGCell
& firstCell
= firstProp
->GetCell(0);
1530 wxPGCellData
* firstCellData
= firstCell
.GetData();
1532 wxPGCell
newCell(firstCell
);
1533 newCell
.SetBgCol(colour
);
1535 srcCell
.SetBgCol(colour
);
1538 GetParentState()->GetColumnCount()-1,
1542 recursively
? wxPG_PROP_CATEGORY
: 0,
1546 void wxPGProperty::SetTextColour( const wxColour
& colour
,
1549 wxPGProperty
* firstProp
= this;
1552 // If category is tried to set recursively, skip it and only
1553 // affect the children.
1556 while ( firstProp
->IsCategory() )
1558 if ( !firstProp
->GetChildCount() )
1560 firstProp
= firstProp
->Item(0);
1564 wxPGCell
& firstCell
= firstProp
->GetCell(0);
1565 wxPGCellData
* firstCellData
= firstCell
.GetData();
1567 wxPGCell
newCell(firstCell
);
1568 newCell
.SetFgCol(colour
);
1570 srcCell
.SetFgCol(colour
);
1573 GetParentState()->GetColumnCount()-1,
1577 recursively
? wxPG_PROP_CATEGORY
: 0,
1581 wxPGEditorDialogAdapter
* wxPGProperty::GetEditorDialog() const
1586 bool wxPGProperty::DoSetAttribute( const wxString
& WXUNUSED(name
), wxVariant
& WXUNUSED(value
) )
1591 void wxPGProperty::SetAttribute( const wxString
& name
, wxVariant value
)
1593 if ( DoSetAttribute( name
, value
) )
1595 // Support working without grid, when possible
1596 if ( wxPGGlobalVars
->HasExtraStyle( wxPG_EX_WRITEONLY_BUILTIN_ATTRIBUTES
) )
1600 m_attributes
.Set( name
, value
);
1603 void wxPGProperty::SetAttributes( const wxPGAttributeStorage
& attributes
)
1605 wxPGAttributeStorage::const_iterator it
= attributes
.StartIteration();
1608 while ( attributes
.GetNext(it
, variant
) )
1609 SetAttribute( variant
.GetName(), variant
);
1612 wxVariant
wxPGProperty::DoGetAttribute( const wxString
& WXUNUSED(name
) ) const
1618 wxVariant
wxPGProperty::GetAttribute( const wxString
& name
) const
1620 return m_attributes
.FindValue(name
);
1623 wxString
wxPGProperty::GetAttribute( const wxString
& name
, const wxString
& defVal
) const
1625 wxVariant variant
= m_attributes
.FindValue(name
);
1627 if ( !variant
.IsNull() )
1628 return variant
.GetString();
1633 long wxPGProperty::GetAttributeAsLong( const wxString
& name
, long defVal
) const
1635 wxVariant variant
= m_attributes
.FindValue(name
);
1637 return wxPGVariantToInt(variant
, defVal
);
1640 double wxPGProperty::GetAttributeAsDouble( const wxString
& name
, double defVal
) const
1643 wxVariant variant
= m_attributes
.FindValue(name
);
1645 if ( wxPGVariantToDouble(variant
, &retVal
) )
1651 wxVariant
wxPGProperty::GetAttributesAsList() const
1653 wxVariantList tempList
;
1654 wxVariant
v( tempList
, wxString::Format(wxS("@%s@attr"),m_name
.c_str()) );
1656 wxPGAttributeStorage::const_iterator it
= m_attributes
.StartIteration();
1659 while ( m_attributes
.GetNext(it
, variant
) )
1665 // Slots of utility flags are NULL
1666 const unsigned int gs_propFlagToStringSize
= 14;
1668 static const wxChar
* gs_propFlagToString
[gs_propFlagToStringSize
] = {
1685 wxString
wxPGProperty::GetFlagsAsString( FlagType flagsMask
) const
1688 int relevantFlags
= m_flags
& flagsMask
& wxPG_STRING_STORED_FLAGS
;
1692 for ( i
=0; i
<gs_propFlagToStringSize
; i
++ )
1694 if ( relevantFlags
& a
)
1696 const wxChar
* fs
= gs_propFlagToString
[i
];
1708 void wxPGProperty::SetFlagsFromString( const wxString
& str
)
1712 WX_PG_TOKENIZER1_BEGIN(str
, wxS('|'))
1714 for ( i
=0; i
<gs_propFlagToStringSize
; i
++ )
1716 const wxChar
* fs
= gs_propFlagToString
[i
];
1717 if ( fs
&& str
== fs
)
1723 WX_PG_TOKENIZER1_END()
1725 m_flags
= (m_flags
& ~wxPG_STRING_STORED_FLAGS
) | flags
;
1728 wxValidator
* wxPGProperty::DoGetValidator() const
1733 int wxPGProperty::InsertChoice( const wxString
& label
, int index
, int value
)
1735 wxPropertyGrid
* pg
= GetGrid();
1736 int sel
= GetChoiceSelection();
1740 if ( index
== wxNOT_FOUND
)
1741 index
= m_choices
.GetCount();
1746 m_choices
.Insert(label
, index
, value
);
1748 if ( sel
!= newSel
)
1749 SetChoiceSelection(newSel
);
1751 if ( this == pg
->GetSelection() )
1752 GetEditorClass()->InsertItem(pg
->GetEditorControl(),label
,index
);
1758 void wxPGProperty::DeleteChoice( int index
)
1760 wxPropertyGrid
* pg
= GetGrid();
1762 int sel
= GetChoiceSelection();
1765 // Adjust current value
1768 SetValueToUnspecified();
1771 else if ( index
< sel
)
1776 m_choices
.RemoveAt(index
);
1778 if ( sel
!= newSel
)
1779 SetChoiceSelection(newSel
);
1781 if ( this == pg
->GetSelection() )
1782 GetEditorClass()->DeleteItem(pg
->GetEditorControl(), index
);
1785 int wxPGProperty::GetChoiceSelection() const
1787 wxVariant value
= GetValue();
1788 wxString valueType
= value
.GetType();
1789 int index
= wxNOT_FOUND
;
1791 if ( IsValueUnspecified() || !m_choices
.GetCount() )
1794 if ( valueType
== wxPG_VARIANT_TYPE_LONG
)
1796 index
= value
.GetLong();
1798 else if ( valueType
== wxPG_VARIANT_TYPE_STRING
)
1800 index
= m_choices
.Index(value
.GetString());
1802 else if ( valueType
== wxPG_VARIANT_TYPE_BOOL
)
1804 index
= value
.GetBool()? 1 : 0;
1810 void wxPGProperty::SetChoiceSelection( int newValue
)
1812 // Changes value of a property with choices, but only
1813 // works if the value type is long or string.
1814 wxString valueType
= GetValue().GetType();
1816 wxCHECK_RET( m_choices
.IsOk(), wxT("invalid choiceinfo") );
1818 if ( valueType
== wxPG_VARIANT_TYPE_STRING
)
1820 SetValue( m_choices
.GetLabel(newValue
) );
1822 else // if ( valueType == wxPG_VARIANT_TYPE_LONG )
1824 SetValue( (long) newValue
);
1828 bool wxPGProperty::SetChoices( wxPGChoices
& choices
)
1830 m_choices
.Assign(choices
);
1833 // This may be needed to trigger some initialization
1834 // (but don't do it if property is somewhat uninitialized)
1835 wxVariant defVal
= GetDefaultValue();
1836 if ( defVal
.IsNull() )
1846 const wxPGEditor
* wxPGProperty::GetEditorClass() const
1848 const wxPGEditor
* editor
;
1850 if ( !m_customEditor
)
1852 editor
= DoGetEditorClass();
1855 editor
= m_customEditor
;
1858 // Maybe override editor if common value specified
1859 if ( GetDisplayedCommonValueCount() )
1861 // TextCtrlAndButton -> ComboBoxAndButton
1862 if ( editor
->IsKindOf(CLASSINFO(wxPGTextCtrlAndButtonEditor
)) )
1863 editor
= wxPGEditor_ChoiceAndButton
;
1865 // TextCtrl -> ComboBox
1866 else if ( editor
->IsKindOf(CLASSINFO(wxPGTextCtrlEditor
)) )
1867 editor
= wxPGEditor_ComboBox
;
1873 bool wxPGProperty::HasVisibleChildren() const
1877 for ( i
=0; i
<GetChildCount(); i
++ )
1879 wxPGProperty
* child
= Item(i
);
1881 if ( !child
->HasFlag(wxPG_PROP_HIDDEN
) )
1888 bool wxPGProperty::RecreateEditor()
1890 wxPropertyGrid
* pg
= GetGrid();
1893 wxPGProperty
* selected
= pg
->GetSelection();
1894 if ( this == selected
)
1896 pg
->DoSelectProperty(this, wxPG_SEL_FORCE
);
1903 void wxPGProperty::SetValueImage( wxBitmap
& bmp
)
1905 delete m_valueBitmap
;
1907 if ( &bmp
&& bmp
.Ok() )
1910 wxSize maxSz
= GetGrid()->GetImageSize();
1911 wxSize
imSz(bmp
.GetWidth(),bmp
.GetHeight());
1913 if ( imSz
.y
!= maxSz
.y
)
1915 // Create a memory DC
1916 wxBitmap
* bmpNew
= new wxBitmap(maxSz
.x
,maxSz
.y
,bmp
.GetDepth());
1919 dc
.SelectObject(*bmpNew
);
1922 // FIXME: This is ugly - use image or wait for scaling patch.
1923 double scaleY
= (double)maxSz
.y
/ (double)imSz
.y
;
1925 dc
.SetUserScale(scaleY
, scaleY
);
1927 dc
.DrawBitmap(bmp
, 0, 0);
1929 m_valueBitmap
= bmpNew
;
1933 m_valueBitmap
= new wxBitmap(bmp
);
1936 m_flags
|= wxPG_PROP_CUSTOMIMAGE
;
1940 m_valueBitmap
= NULL
;
1941 m_flags
&= ~(wxPG_PROP_CUSTOMIMAGE
);
1946 wxPGProperty
* wxPGProperty::GetMainParent() const
1948 const wxPGProperty
* curChild
= this;
1949 const wxPGProperty
* curParent
= m_parent
;
1951 while ( curParent
&& !curParent
->IsCategory() )
1953 curChild
= curParent
;
1954 curParent
= curParent
->m_parent
;
1957 return (wxPGProperty
*) curChild
;
1961 const wxPGProperty
* wxPGProperty::GetLastVisibleSubItem() const
1964 // Returns last visible sub-item, recursively.
1965 if ( !IsExpanded() || !GetChildCount() )
1968 return Last()->GetLastVisibleSubItem();
1972 bool wxPGProperty::IsVisible() const
1974 const wxPGProperty
* parent
;
1976 if ( HasFlag(wxPG_PROP_HIDDEN
) )
1979 for ( parent
= GetParent(); parent
!= NULL
; parent
= parent
->GetParent() )
1981 if ( !parent
->IsExpanded() || parent
->HasFlag(wxPG_PROP_HIDDEN
) )
1988 wxPropertyGrid
* wxPGProperty::GetGridIfDisplayed() const
1990 wxPropertyGridPageState
* state
= GetParentState();
1993 wxPropertyGrid
* propGrid
= state
->GetGrid();
1994 if ( state
== propGrid
->GetState() )
2000 int wxPGProperty::GetY2( int lh
) const
2002 const wxPGProperty
* parent
;
2003 const wxPGProperty
* child
= this;
2007 for ( parent
= GetParent(); parent
!= NULL
; parent
= child
->GetParent() )
2009 if ( !parent
->IsExpanded() )
2011 y
+= parent
->GetChildrenHeight(lh
, child
->GetIndexInParent());
2016 y
-= lh
; // need to reduce one level
2022 int wxPGProperty::GetY() const
2024 return GetY2(GetGrid()->GetRowHeight());
2027 // This is used by Insert etc.
2028 void wxPGProperty::DoAddChild( wxPGProperty
* prop
, int index
,
2031 if ( index
< 0 || (size_t)index
>= m_children
.size() )
2033 if ( correct_mode
) prop
->m_arrIndex
= m_children
.size();
2034 m_children
.push_back( prop
);
2038 m_children
.insert( m_children
.begin()+index
, prop
);
2039 if ( correct_mode
) FixIndicesOfChildren( index
);
2042 prop
->m_parent
= this;
2045 void wxPGProperty::DoPreAddChild( int index
, wxPGProperty
* prop
)
2047 wxASSERT_MSG( prop
->GetBaseName().length(),
2048 "Property's children must have unique, non-empty "
2049 "names within their scope" );
2051 prop
->m_arrIndex
= index
;
2052 m_children
.insert( m_children
.begin()+index
,
2055 int custImgHeight
= prop
->OnMeasureImage().y
;
2056 if ( custImgHeight
< 0 /*|| custImgHeight > 1*/ )
2057 prop
->m_flags
|= wxPG_PROP_CUSTOMIMAGE
;
2059 prop
->m_parent
= this;
2062 void wxPGProperty::AddPrivateChild( wxPGProperty
* prop
)
2064 if ( !(m_flags
& wxPG_PROP_PARENTAL_FLAGS
) )
2065 SetParentalType(wxPG_PROP_AGGREGATE
);
2067 wxASSERT_MSG( (m_flags
& wxPG_PROP_PARENTAL_FLAGS
) ==
2068 wxPG_PROP_AGGREGATE
,
2069 "Do not mix up AddPrivateChild() calls with other "
2070 "property adders." );
2072 DoPreAddChild( m_children
.size(), prop
);
2075 #if wxPG_COMPATIBILITY_1_4
2076 void wxPGProperty::AddChild( wxPGProperty
* prop
)
2078 AddPrivateChild(prop
);
2082 wxPGProperty
* wxPGProperty::InsertChild( int index
,
2083 wxPGProperty
* childProperty
)
2086 index
= m_children
.size();
2088 if ( m_parentState
)
2090 m_parentState
->DoInsert(this, index
, childProperty
);
2094 if ( !(m_flags
& wxPG_PROP_PARENTAL_FLAGS
) )
2095 SetParentalType(wxPG_PROP_MISC_PARENT
);
2097 wxASSERT_MSG( (m_flags
& wxPG_PROP_PARENTAL_FLAGS
) ==
2098 wxPG_PROP_MISC_PARENT
,
2099 "Do not mix up AddPrivateChild() calls with other "
2100 "property adders." );
2102 DoPreAddChild( index
, childProperty
);
2105 return childProperty
;
2108 void wxPGProperty::RemoveChild( wxPGProperty
* p
)
2110 wxArrayPGProperty::iterator it
;
2111 wxArrayPGProperty
& children
= m_children
;
2113 for ( it
=children
.begin(); it
!= children
.end(); it
++ )
2117 m_children
.erase(it
);
2123 void wxPGProperty::AdaptListToValue( wxVariant
& list
, wxVariant
* value
) const
2125 wxASSERT( GetChildCount() );
2126 wxASSERT( !IsCategory() );
2128 *value
= GetValue();
2130 if ( !list
.GetCount() )
2133 wxASSERT( GetChildCount() >= (unsigned int)list
.GetCount() );
2135 bool allChildrenSpecified
;
2137 // Don't fully update aggregate properties unless all children have
2139 if ( HasFlag(wxPG_PROP_AGGREGATE
) )
2140 allChildrenSpecified
= AreAllChildrenSpecified(&list
);
2142 allChildrenSpecified
= true;
2144 wxVariant childValue
= list
[0];
2148 //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
2150 for ( i
=0; i
<GetChildCount(); i
++ )
2152 const wxPGProperty
* child
= Item(i
);
2154 if ( childValue
.GetName() == child
->GetBaseName() )
2156 //wxLogDebug(wxT(" %s(n=%i), %s"),childValue.GetName().c_str(),n,childValue.GetType().c_str());
2158 if ( childValue
.GetType() == wxPG_VARIANT_TYPE_LIST
)
2160 wxVariant
cv2(child
->GetValue());
2161 child
->AdaptListToValue(childValue
, &cv2
);
2165 if ( allChildrenSpecified
)
2167 *value
= ChildChanged(*value
, i
, childValue
);
2171 if ( n
== (unsigned int)list
.GetCount() )
2173 childValue
= list
[n
];
2179 void wxPGProperty::FixIndicesOfChildren( unsigned int starthere
)
2182 for ( i
=starthere
;i
<GetChildCount();i
++)
2183 Item(i
)->m_arrIndex
= i
;
2187 // Returns (direct) child property with given name (or NULL if not found)
2188 wxPGProperty
* wxPGProperty::GetPropertyByName( const wxString
& name
) const
2192 for ( i
=0; i
<GetChildCount(); i
++ )
2194 wxPGProperty
* p
= Item(i
);
2195 if ( p
->m_name
== name
)
2199 // Does it have point, then?
2200 int pos
= name
.Find(wxS('.'));
2204 wxPGProperty
* p
= GetPropertyByName(name
. substr(0,pos
));
2206 if ( !p
|| !p
->GetChildCount() )
2209 return p
->GetPropertyByName(name
.substr(pos
+1,name
.length()-pos
-1));
2212 wxPGProperty
* wxPGProperty::GetPropertyByNameWH( const wxString
& name
, unsigned int hintIndex
) const
2214 unsigned int i
= hintIndex
;
2216 if ( i
>= GetChildCount() )
2219 unsigned int lastIndex
= i
- 1;
2221 if ( lastIndex
>= GetChildCount() )
2222 lastIndex
= GetChildCount() - 1;
2226 wxPGProperty
* p
= Item(i
);
2227 if ( p
->m_name
== name
)
2230 if ( i
== lastIndex
)
2234 if ( i
== GetChildCount() )
2241 int wxPGProperty::GetChildrenHeight( int lh
, int iMax_
) const
2243 // Returns height of children, recursively, and
2244 // by taking expanded/collapsed status into account.
2246 // iMax is used when finding property y-positions.
2252 iMax_
= GetChildCount();
2254 unsigned int iMax
= iMax_
;
2256 wxASSERT( iMax
<= GetChildCount() );
2258 if ( !IsExpanded() && GetParent() )
2263 wxPGProperty
* pwc
= (wxPGProperty
*) Item(i
);
2265 if ( !pwc
->HasFlag(wxPG_PROP_HIDDEN
) )
2267 if ( !pwc
->IsExpanded() ||
2268 pwc
->GetChildCount() == 0 )
2271 h
+= pwc
->GetChildrenHeight(lh
) + lh
;
2280 wxPGProperty
* wxPGProperty::GetItemAtY( unsigned int y
,
2282 unsigned int* nextItemY
) const
2284 wxASSERT( nextItemY
);
2286 // Linear search at the moment
2288 // nextItemY = y of next visible property, final value will be written back.
2289 wxPGProperty
* result
= NULL
;
2290 wxPGProperty
* current
= NULL
;
2291 unsigned int iy
= *nextItemY
;
2293 unsigned int iMax
= GetChildCount();
2297 wxPGProperty
* pwc
= Item(i
);
2299 if ( !pwc
->HasFlag(wxPG_PROP_HIDDEN
) )
2310 if ( pwc
->IsExpanded() &&
2311 pwc
->GetChildCount() > 0 )
2313 result
= (wxPGProperty
*) pwc
->GetItemAtY( y
, lh
, &iy
);
2325 if ( !result
&& y
< iy
)
2332 wxLogDebug(wxT("%s::GetItemAtY(%i) -> %s"),this->GetLabel().c_str(),y,current->GetLabel().c_str());
2334 wxLogDebug(wxT("%s::GetItemAtY(%i) -> NULL"),this->GetLabel().c_str(),y);
2337 return (wxPGProperty
*) result
;
2340 void wxPGProperty::Empty()
2343 if ( !HasFlag(wxPG_PROP_CHILDREN_ARE_COPIES
) )
2345 for ( i
=0; i
<GetChildCount(); i
++ )
2347 delete m_children
[i
];
2354 wxPGProperty
* wxPGProperty::GetItemAtY( unsigned int y
) const
2356 unsigned int nextItem
;
2357 return GetItemAtY( y
, GetGrid()->GetRowHeight(), &nextItem
);
2360 void wxPGProperty::DeleteChildren()
2362 wxPropertyGridPageState
* state
= m_parentState
;
2364 while ( GetChildCount() )
2366 wxPGProperty
* child
= Item(GetChildCount()-1);
2367 state
->DoDelete(child
, true);
2371 wxVariant
wxPGProperty::ChildChanged( wxVariant
& WXUNUSED(thisValue
),
2372 int WXUNUSED(childIndex
),
2373 wxVariant
& WXUNUSED(childValue
) ) const
2375 return wxNullVariant
;
2378 bool wxPGProperty::AreAllChildrenSpecified( wxVariant
* pendingList
) const
2382 const wxVariantList
* pList
= NULL
;
2383 wxVariantList::const_iterator node
;
2387 pList
= &pendingList
->GetList();
2388 node
= pList
->begin();
2391 for ( i
=0; i
<GetChildCount(); i
++ )
2393 wxPGProperty
* child
= Item(i
);
2394 const wxVariant
* listValue
= NULL
;
2399 const wxString
& childName
= child
->GetBaseName();
2401 for ( ; node
!= pList
->end(); ++node
)
2403 const wxVariant
& item
= *((const wxVariant
*)*node
);
2404 if ( item
.GetName() == childName
)
2414 value
= child
->GetValue();
2416 if ( value
.IsNull() )
2419 // Check recursively
2420 if ( child
->GetChildCount() )
2422 const wxVariant
* childList
= NULL
;
2424 if ( listValue
&& listValue
->GetType() == wxPG_VARIANT_TYPE_LIST
)
2425 childList
= listValue
;
2427 if ( !child
->AreAllChildrenSpecified((wxVariant
*)childList
) )
2435 wxPGProperty
* wxPGProperty::UpdateParentValues()
2437 wxPGProperty
* parent
= m_parent
;
2438 if ( parent
&& parent
->HasFlag(wxPG_PROP_COMPOSED_VALUE
) &&
2439 !parent
->IsCategory() && !parent
->IsRoot() )
2442 parent
->DoGenerateComposedValue(s
);
2443 parent
->m_value
= s
;
2444 return parent
->UpdateParentValues();
2449 bool wxPGProperty::IsTextEditable() const
2451 if ( HasFlag(wxPG_PROP_READONLY
) )
2454 if ( HasFlag(wxPG_PROP_NOEDITOR
) &&
2456 wxString(GetEditorClass()->GetClassInfo()->GetClassName()).EndsWith(wxS("Button")))
2463 // Call after fixed sub-properties added/removed after creation.
2464 // if oldSelInd >= 0 and < new max items, then selection is
2465 // moved to it. Note: oldSelInd -2 indicates that this property
2466 // should be selected.
2467 void wxPGProperty::SubPropsChanged( int oldSelInd
)
2469 wxPropertyGridPageState
* state
= GetParentState();
2470 wxPropertyGrid
* grid
= state
->GetGrid();
2473 // Re-repare children (recursively)
2474 for ( unsigned int i
=0; i
<GetChildCount(); i
++ )
2476 wxPGProperty
* child
= Item(i
);
2477 child
->InitAfterAdded(state
, grid
);
2480 wxPGProperty
* sel
= NULL
;
2481 if ( oldSelInd
>= (int)m_children
.size() )
2482 oldSelInd
= (int)m_children
.size() - 1;
2484 if ( oldSelInd
>= 0 )
2485 sel
= m_children
[oldSelInd
];
2486 else if ( oldSelInd
== -2 )
2490 state
->DoSelectProperty(sel
);
2492 if ( state
== grid
->GetState() )
2494 grid
->GetPanel()->Refresh();
2498 // -----------------------------------------------------------------------
2500 // -----------------------------------------------------------------------
2502 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPGRootProperty
,none
,TextCtrl
)
2503 IMPLEMENT_DYNAMIC_CLASS(wxPGRootProperty
, wxPGProperty
)
2506 wxPGRootProperty::wxPGRootProperty( const wxString
& name
)
2516 wxPGRootProperty::~wxPGRootProperty()
2521 // -----------------------------------------------------------------------
2522 // wxPropertyCategory
2523 // -----------------------------------------------------------------------
2525 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPropertyCategory
,none
,TextCtrl
)
2526 IMPLEMENT_DYNAMIC_CLASS(wxPropertyCategory
, wxPGProperty
)
2528 void wxPropertyCategory::Init()
2530 // don't set colour - prepareadditem method should do this
2531 SetParentalType(wxPG_PROP_CATEGORY
);
2532 m_capFgColIndex
= 1;
2536 wxPropertyCategory::wxPropertyCategory()
2543 wxPropertyCategory::wxPropertyCategory( const wxString
&label
, const wxString
& name
)
2544 : wxPGProperty(label
,name
)
2550 wxPropertyCategory::~wxPropertyCategory()
2555 wxString
wxPropertyCategory::ValueToString( wxVariant
& WXUNUSED(value
),
2556 int WXUNUSED(argFlags
) ) const
2558 return wxEmptyString
;
2561 int wxPropertyCategory::GetTextExtent( const wxWindow
* wnd
, const wxFont
& font
) const
2563 if ( m_textExtent
> 0 )
2564 return m_textExtent
;
2566 ((wxWindow
*)wnd
)->GetTextExtent( m_label
, &x
, &y
, 0, 0, &font
);
2570 void wxPropertyCategory::CalculateTextExtent( wxWindow
* wnd
, const wxFont
& font
)
2573 wnd
->GetTextExtent( m_label
, &x
, &y
, 0, 0, &font
);
2577 // -----------------------------------------------------------------------
2579 // -----------------------------------------------------------------------
2581 wxPGChoiceEntry
& wxPGChoices::Add( const wxString
& label
, int value
)
2585 wxPGChoiceEntry
entry(label
, value
);
2586 return m_data
->Insert( -1, entry
);
2589 // -----------------------------------------------------------------------
2591 wxPGChoiceEntry
& wxPGChoices::Add( const wxString
& label
, const wxBitmap
& bitmap
, int value
)
2595 wxPGChoiceEntry
entry(label
, value
);
2596 entry
.SetBitmap(bitmap
);
2597 return m_data
->Insert( -1, entry
);
2600 // -----------------------------------------------------------------------
2602 wxPGChoiceEntry
& wxPGChoices::Insert( const wxPGChoiceEntry
& entry
, int index
)
2606 return m_data
->Insert( index
, entry
);
2609 // -----------------------------------------------------------------------
2611 wxPGChoiceEntry
& wxPGChoices::Insert( const wxString
& label
, int index
, int value
)
2615 wxPGChoiceEntry
entry(label
, value
);
2616 return m_data
->Insert( index
, entry
);
2619 // -----------------------------------------------------------------------
2621 wxPGChoiceEntry
& wxPGChoices::AddAsSorted( const wxString
& label
, int value
)
2627 while ( index
< GetCount() )
2629 int cmpRes
= GetLabel(index
).Cmp(label
);
2635 wxPGChoiceEntry
entry(label
, value
);
2636 return m_data
->Insert( index
, entry
);
2639 // -----------------------------------------------------------------------
2641 void wxPGChoices::Add( const wxChar
** labels
, const ValArrItem
* values
)
2645 unsigned int itemcount
= 0;
2646 const wxChar
** p
= &labels
[0];
2647 while ( *p
) { p
++; itemcount
++; }
2650 for ( i
= 0; i
< itemcount
; i
++ )
2655 wxPGChoiceEntry
entry(labels
[i
], value
);
2656 m_data
->Insert( i
, entry
);
2660 // -----------------------------------------------------------------------
2662 void wxPGChoices::Add( const wxArrayString
& arr
, const wxArrayInt
& arrint
)
2667 unsigned int itemcount
= arr
.size();
2669 for ( i
= 0; i
< itemcount
; i
++ )
2672 if ( &arrint
&& arrint
.size() )
2674 wxPGChoiceEntry
entry(arr
[i
], value
);
2675 m_data
->Insert( i
, entry
);
2679 // -----------------------------------------------------------------------
2681 void wxPGChoices::RemoveAt(size_t nIndex
, size_t count
)
2685 wxASSERT( m_data
->m_refCount
!= 0xFFFFFFF );
2686 m_data
->m_items
.erase(m_data
->m_items
.begin()+nIndex
,
2687 m_data
->m_items
.begin()+nIndex
+count
);
2690 // -----------------------------------------------------------------------
2692 void wxPGChoices::Clear()
2694 if ( m_data
!= wxPGChoicesEmptyData
)
2701 // -----------------------------------------------------------------------
2703 int wxPGChoices::Index( const wxString
& str
) const
2708 for ( i
=0; i
< m_data
->GetCount(); i
++ )
2710 const wxPGChoiceEntry
& entry
= m_data
->Item(i
);
2711 if ( entry
.HasText() && entry
.GetText() == str
)
2718 // -----------------------------------------------------------------------
2720 int wxPGChoices::Index( int val
) const
2725 for ( i
=0; i
< m_data
->GetCount(); i
++ )
2727 const wxPGChoiceEntry
& entry
= m_data
->Item(i
);
2728 if ( entry
.GetValue() == val
)
2735 // -----------------------------------------------------------------------
2737 wxArrayString
wxPGChoices::GetLabels() const
2742 if ( this && IsOk() )
2743 for ( i
=0; i
<GetCount(); i
++ )
2744 arr
.push_back(GetLabel(i
));
2749 // -----------------------------------------------------------------------
2751 wxArrayInt
wxPGChoices::GetValuesForStrings( const wxArrayString
& strings
) const
2758 for ( i
=0; i
< strings
.size(); i
++ )
2760 int index
= Index(strings
[i
]);
2762 arr
.Add(GetValue(index
));
2764 arr
.Add(wxPG_INVALID_VALUE
);
2771 // -----------------------------------------------------------------------
2773 wxArrayInt
wxPGChoices::GetIndicesForStrings( const wxArrayString
& strings
,
2774 wxArrayString
* unmatched
) const
2781 for ( i
=0; i
< strings
.size(); i
++ )
2783 const wxString
& str
= strings
[i
];
2784 int index
= Index(str
);
2787 else if ( unmatched
)
2788 unmatched
->Add(str
);
2795 // -----------------------------------------------------------------------
2797 void wxPGChoices::AllocExclusive()
2801 if ( m_data
->m_refCount
!= 1 )
2803 wxPGChoicesData
* data
= new wxPGChoicesData();
2804 data
->CopyDataFrom(m_data
);
2810 // -----------------------------------------------------------------------
2812 void wxPGChoices::AssignData( wxPGChoicesData
* data
)
2816 if ( data
!= wxPGChoicesEmptyData
)
2823 // -----------------------------------------------------------------------
2825 void wxPGChoices::Init()
2827 m_data
= wxPGChoicesEmptyData
;
2830 // -----------------------------------------------------------------------
2832 void wxPGChoices::Free()
2834 if ( m_data
!= wxPGChoicesEmptyData
)
2837 m_data
= wxPGChoicesEmptyData
;
2841 // -----------------------------------------------------------------------
2842 // wxPGAttributeStorage
2843 // -----------------------------------------------------------------------
2845 wxPGAttributeStorage::wxPGAttributeStorage()
2849 wxPGAttributeStorage::~wxPGAttributeStorage()
2851 wxPGHashMapS2P::iterator it
;
2853 for ( it
= m_map
.begin(); it
!= m_map
.end(); ++it
)
2855 wxVariantData
* data
= (wxVariantData
*) it
->second
;
2860 void wxPGAttributeStorage::Set( const wxString
& name
, const wxVariant
& value
)
2862 wxVariantData
* data
= value
.GetData();
2865 wxPGHashMapS2P::iterator it
= m_map
.find(name
);
2866 if ( it
!= m_map
.end() )
2868 ((wxVariantData
*)it
->second
)->DecRef();
2872 // If Null variant, just remove from set
2886 #endif // wxUSE_PROPGRID