1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/propgridpagestate.cpp
3 // Purpose: wxPropertyGridPageState class
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"
35 #include "wx/stopwatch.h"
38 // This define is necessary to prevent macro clearing
39 #define __wxPG_SOURCE_FILE__
41 #include "wx/propgrid/propgridpagestate.h"
42 #include "wx/propgrid/propgrid.h"
43 #include "wx/propgrid/editors.h"
45 #define wxPG_DEFAULT_SPLITTERX 110
48 // -----------------------------------------------------------------------
49 // wxPropertyGridIterator
50 // -----------------------------------------------------------------------
52 void wxPropertyGridIteratorBase::Init( wxPropertyGridPageState
* state
, int flags
, wxPGProperty
* property
, int dir
)
54 wxASSERT( dir
== 1 || dir
== -1 );
57 m_baseParent
= state
->DoGetRoot();
58 if ( !property
&& m_baseParent
->GetChildCount() )
59 property
= m_baseParent
->Item(0);
61 m_property
= property
;
63 wxPG_ITERATOR_CREATE_MASKS(flags
, m_itemExMask
, m_parentExMask
)
65 // Need to skip first?
66 if ( property
&& (property
->GetFlags() & m_itemExMask
) )
75 void wxPropertyGridIteratorBase::Init( wxPropertyGridPageState
* state
, int flags
, int startPos
, int dir
)
77 wxPGProperty
* property
= NULL
;
79 if ( startPos
== wxTOP
)
84 else if ( startPos
== wxBOTTOM
)
86 property
= state
->GetLastItem(flags
);
92 wxFAIL_MSG("Only supported starting positions are wxTOP and wxBOTTOM");
95 Init( state
, flags
, property
, dir
);
98 void wxPropertyGridIteratorBase::Assign( const wxPropertyGridIteratorBase
& it
)
100 m_property
= it
.m_property
;
101 m_state
= it
.m_state
;
102 m_baseParent
= it
.m_baseParent
;
103 m_itemExMask
= it
.m_itemExMask
;
104 m_parentExMask
= it
.m_parentExMask
;
107 void wxPropertyGridIteratorBase::Prev()
109 wxPGProperty
* property
= m_property
;
110 wxASSERT( property
);
112 wxPGProperty
* parent
= property
->GetParent();
114 unsigned int index
= property
->GetIndexInParent();
121 property
= parent
->Item(index
);
123 // Go to last children?
124 if ( property
->GetChildCount() &&
125 wxPG_ITERATOR_PARENTEXMASK_TEST(property
, m_parentExMask
) )
128 property
= property
->Last();
134 if ( parent
== m_baseParent
)
145 m_property
= property
;
147 // If property does not match our criteria, skip it
148 if ( property
->GetFlags() & m_itemExMask
)
152 void wxPropertyGridIteratorBase::Next( bool iterateChildren
)
154 wxPGProperty
* property
= m_property
;
155 wxASSERT( property
);
157 if ( property
->GetChildCount() &&
158 wxPG_ITERATOR_PARENTEXMASK_TEST(property
, m_parentExMask
) &&
162 property
= property
->Item(0);
166 wxPGProperty
* parent
= property
->GetParent();
168 unsigned int index
= property
->GetIndexInParent() + 1;
170 if ( index
< parent
->GetChildCount() )
173 property
= parent
->Item(index
);
177 // Next sibling of parent
178 if ( parent
== m_baseParent
)
191 m_property
= property
;
193 // If property does not match our criteria, skip it
194 if ( property
->GetFlags() & m_itemExMask
)
198 // -----------------------------------------------------------------------
199 // wxPropertyGridPageState
200 // -----------------------------------------------------------------------
202 wxPropertyGridPageState::wxPropertyGridPageState()
205 m_regularArray
.SetParentState(this);
206 m_properties
= &m_regularArray
;
208 m_currentCategory
= NULL
;
211 m_lastCaptionBottomnest
= 1;
215 m_colWidths
.push_back( wxPG_DEFAULT_SPLITTERX
);
216 m_colWidths
.push_back( wxPG_DEFAULT_SPLITTERX
);
217 m_fSplitterX
= wxPG_DEFAULT_SPLITTERX
;
220 // -----------------------------------------------------------------------
222 wxPropertyGridPageState::~wxPropertyGridPageState()
227 // -----------------------------------------------------------------------
229 void wxPropertyGridPageState::InitNonCatMode()
233 m_abcArray
= new wxPGRootProperty(wxS("<Root_NonCat>"));
234 m_abcArray
->SetParentState(this);
235 m_abcArray
->SetFlag(wxPG_PROP_CHILDREN_ARE_COPIES
);
238 // Must be called when state::m_properties still points to regularArray.
239 wxPGProperty
* oldProperties
= m_properties
;
241 // Must use temp value in state::m_properties for item iteration loop
242 // to run as expected.
243 m_properties
= &m_regularArray
;
245 if ( m_properties
->GetChildCount() )
248 // Prepare m_abcArray
249 wxPropertyGridIterator
it( this, wxPG_ITERATE_PROPERTIES
);
251 for ( ; !it
.AtEnd(); it
.Next() )
253 wxPGProperty
* p
= it
.GetProperty();
254 wxPGProperty
* parent
= p
->GetParent();
255 if ( parent
->IsCategory() || parent
->IsRoot() )
257 m_abcArray
->DoAddChild(p
);
258 p
->m_parent
= &m_regularArray
;
263 m_properties
= oldProperties
;
266 // -----------------------------------------------------------------------
268 void wxPropertyGridPageState::DoClear()
270 if ( m_pPropGrid
&& m_pPropGrid
->GetState() == this )
272 m_pPropGrid
->ClearSelection(false);
279 m_regularArray
.Empty();
285 m_currentCategory
= NULL
;
286 m_lastCaptionBottomnest
= 1;
293 // -----------------------------------------------------------------------
295 void wxPropertyGridPageState::CalculateFontAndBitmapStuff( int WXUNUSED(vspacing
) )
297 wxPropertyGrid
* propGrid
= GetGrid();
299 VirtualHeightChanged();
301 // Recalculate caption text extents.
304 for ( i
=0;i
<m_regularArray
.GetChildCount();i
++ )
306 wxPGProperty
* p
=m_regularArray
.Item(i
);
308 if ( p
->IsCategory() )
309 ((wxPropertyCategory
*)p
)->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
313 // -----------------------------------------------------------------------
315 void wxPropertyGridPageState::SetVirtualWidth( int width
)
317 wxASSERT( width
>= 0 );
318 wxPropertyGrid
* pg
= GetGrid();
319 int gw
= pg
->GetClientSize().x
;
326 // -----------------------------------------------------------------------
328 void wxPropertyGridPageState::OnClientWidthChange( int newWidth
, int widthChange
, bool fromOnResize
)
330 wxPropertyGrid
* pg
= GetGrid();
332 if ( pg
->HasVirtualWidth() )
334 if ( m_width
< newWidth
)
335 SetVirtualWidth( newWidth
);
337 CheckColumnWidths(widthChange
);
341 SetVirtualWidth( newWidth
);
343 // This should be done before splitter auto centering
344 // NOTE: Splitter auto-centering is done in this function.
347 CheckColumnWidths(widthChange
);
349 if ( !(GetGrid()->GetInternalFlags() & wxPG_FL_SPLITTER_PRE_SET
) &&
350 (GetGrid()->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) )
352 long timeSinceCreation
= (::wxGetLocalTimeMillis() - GetGrid()->m_timeCreated
).ToLong();
354 // If too long, don't set splitter
355 if ( timeSinceCreation
< 250 )
357 if ( m_properties
->GetChildCount() )
359 SetSplitterLeft( false );
363 DoSetSplitterPosition( newWidth
/ 2 );
364 GetGrid()->ClearInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
371 // -----------------------------------------------------------------------
372 // wxPropertyGridPageState item iteration methods
373 // -----------------------------------------------------------------------
375 wxPGProperty
* wxPropertyGridPageState::GetLastItem( int flags
)
377 if ( !m_properties
->GetChildCount() )
380 wxPG_ITERATOR_CREATE_MASKS(flags
, int itemExMask
, int parentExMask
)
382 // First, get last child of last parent
383 wxPGProperty
* pwc
= (wxPGProperty
*)m_properties
->Last();
384 while ( pwc
->GetChildCount() &&
385 wxPG_ITERATOR_PARENTEXMASK_TEST(pwc
, parentExMask
) )
386 pwc
= (wxPGProperty
*) pwc
->Last();
388 // Then, if it doesn't fit our criteria, back up until we find something that does
389 if ( pwc
->GetFlags() & itemExMask
)
391 wxPropertyGridIterator
it( this, flags
, pwc
);
392 for ( ; !it
.AtEnd(); it
.Prev() )
394 pwc
= (wxPGProperty
*) it
.GetProperty();
400 wxPropertyCategory
* wxPropertyGridPageState::GetPropertyCategory( const wxPGProperty
* p
) const
402 const wxPGProperty
* parent
= (const wxPGProperty
*)p
;
403 const wxPGProperty
* grandparent
= (const wxPGProperty
*)parent
->GetParent();
406 parent
= grandparent
;
407 grandparent
= (wxPGProperty
*)parent
->GetParent();
408 if ( parent
->IsCategory() && grandparent
)
409 return (wxPropertyCategory
*)parent
;
410 } while ( grandparent
);
415 // -----------------------------------------------------------------------
416 // wxPropertyGridPageState GetPropertyXXX methods
417 // -----------------------------------------------------------------------
419 wxPGProperty
* wxPropertyGridPageState::GetPropertyByLabel( const wxString
& label
,
420 wxPGProperty
* parent
) const
425 if ( !parent
) parent
= (wxPGProperty
*) &m_regularArray
;
427 for ( i
=0; i
<parent
->GetChildCount(); i
++ )
429 wxPGProperty
* p
= parent
->Item(i
);
430 if ( p
->m_label
== label
)
432 // Check children recursively.
433 if ( p
->GetChildCount() )
435 p
= GetPropertyByLabel(label
,(wxPGProperty
*)p
);
444 // -----------------------------------------------------------------------
446 wxPGProperty
* wxPropertyGridPageState::BaseGetPropertyByName( const wxString
& name
) const
448 wxPGHashMapS2P::const_iterator it
;
449 it
= m_dictName
.find(name
);
450 if ( it
!= m_dictName
.end() )
451 return (wxPGProperty
*) it
->second
;
455 // -----------------------------------------------------------------------
457 void wxPropertyGridPageState::DoSetPropertyName( wxPGProperty
* p
,
458 const wxString
& newName
)
460 wxCHECK_RET( p
, wxT("invalid property id") );
462 wxPGProperty
* parent
= p
->GetParent();
464 if ( parent
->IsCategory() || parent
->IsRoot() )
466 if ( p
->GetBaseName().length() )
467 m_dictName
.erase( p
->GetBaseName() );
468 if ( newName
.length() )
469 m_dictName
[newName
] = (void*) p
;
472 p
->DoSetName(newName
);
475 // -----------------------------------------------------------------------
476 // wxPropertyGridPageState global operations
477 // -----------------------------------------------------------------------
479 // -----------------------------------------------------------------------
480 // Item iteration macros
481 // NB: Nowadays only needed for alphabetic/categoric mode switching.
482 // -----------------------------------------------------------------------
484 //#define II_INVALID_I 0x00FFFFFF
486 #define ITEM_ITERATION_VARIABLES \
487 wxPGProperty* parent; \
491 #define ITEM_ITERATION_INIT_FROM_THE_TOP \
492 parent = m_properties; \
496 #define ITEM_ITERATION_INIT(startparent, startindex, state) \
497 parent = startparent; \
498 i = (unsigned int)startindex; \
499 if ( parent == NULL ) \
501 parent = state->m_properties; \
506 #define ITEM_ITERATION_LOOP_BEGIN \
509 iMax = parent->GetChildCount(); \
512 wxPGProperty* p = parent->Item(i);
514 #define ITEM_ITERATION_LOOP_END \
515 if ( p->GetChildCount() ) \
518 parent = (wxPGProperty*)p; \
519 iMax = parent->GetChildCount(); \
524 i = parent->m_arrIndex + 1; \
525 parent = parent->m_parent; \
527 while ( parent != NULL );
529 bool wxPropertyGridPageState::EnableCategories( bool enable
)
532 // NB: We can't use wxPropertyGridIterator in this
533 // function, since it depends on m_arrIndexes,
534 // which, among other things, is being fixed here.
536 ITEM_ITERATION_VARIABLES
544 if ( !IsInNonCatMode() )
547 m_properties
= &m_regularArray
;
549 // fix parents, indexes, and depths
550 ITEM_ITERATION_INIT_FROM_THE_TOP
552 ITEM_ITERATION_LOOP_BEGIN
556 p
->m_parent
= parent
;
558 // If parent was category, and this is not,
559 // then the depth stays the same.
560 if ( parent
->IsCategory() &&
562 p
->m_depth
= parent
->m_depth
;
564 p
->m_depth
= parent
->m_depth
+ 1;
566 ITEM_ITERATION_LOOP_END
572 // Disable categories
575 if ( IsInNonCatMode() )
578 // Create array, if necessary.
582 m_properties
= m_abcArray
;
584 // fix parents, indexes, and depths
585 ITEM_ITERATION_INIT_FROM_THE_TOP
587 ITEM_ITERATION_LOOP_BEGIN
591 p
->m_parent
= parent
;
593 p
->m_depth
= parent
->m_depth
+ 1;
595 ITEM_ITERATION_LOOP_END
598 VirtualHeightChanged();
600 if ( m_pPropGrid
->GetState() == this )
601 m_pPropGrid
->RecalculateVirtualSize();
606 // -----------------------------------------------------------------------
608 static int wxPG_SortFunc_ByFunction(wxPGProperty
**pp1
, wxPGProperty
**pp2
)
610 wxPGProperty
*p1
= *pp1
;
611 wxPGProperty
*p2
= *pp2
;
612 wxPropertyGrid
* pg
= p1
->GetGrid();
613 wxPGSortCallback sortFunction
= pg
->GetSortFunction();
614 return sortFunction(pg
, p1
, p2
);
617 static int wxPG_SortFunc_ByLabel(wxPGProperty
**pp1
, wxPGProperty
**pp2
)
619 wxPGProperty
*p1
= *pp1
;
620 wxPGProperty
*p2
= *pp2
;
621 return p1
->GetLabel().CmpNoCase( p2
->GetLabel() );
626 // For wxVector w/ wxUSE_STL=1, you would use code like this instead:
631 static bool wxPG_SortFunc_ByFunction(wxPGProperty
*p1
, wxPGProperty
*p2
)
633 wxPropertyGrid
* pg
= p1
->GetGrid();
634 wxPGSortCallback sortFunction
= pg
->GetSortFunction();
635 return sortFunction(pg
, p1
, p2
) < 0;
638 static bool wxPG_SortFunc_ByLabel(wxPGProperty
*p1
, wxPGProperty
*p2
)
640 return p1
->GetLabel().CmpNoCase( p2
->GetLabel() ) < 0;
644 void wxPropertyGridPageState::DoSortChildren( wxPGProperty
* p
,
650 // Can only sort items with children
651 if ( !p
->GetChildCount() )
654 // Never sort children of aggregate properties
655 if ( p
->HasFlag(wxPG_PROP_AGGREGATE
) )
658 if ( (flags
& wxPG_SORT_TOP_LEVEL_ONLY
)
659 && !p
->IsCategory() && !p
->IsRoot() )
662 if ( GetGrid()->GetSortFunction() )
663 p
->m_children
.Sort( wxPG_SortFunc_ByFunction
);
665 p
->m_children
.Sort( wxPG_SortFunc_ByLabel
);
669 // For wxVector w/ wxUSE_STL=1, you would use code like this instead:
671 if ( GetGrid()->GetSortFunction() )
672 std::sort(p
->m_children
.begin(), p
->m_children
.end(),
673 wxPG_SortFunc_ByFunction
);
675 std::sort(p
->m_children
.begin(), p
->m_children
.end(),
676 wxPG_SortFunc_ByLabel
);
680 p
->FixIndicesOfChildren();
682 if ( flags
& wxPG_RECURSE
)
684 // Apply sort recursively
685 for ( unsigned int i
=0; i
<p
->GetChildCount(); i
++ )
686 DoSortChildren(p
->Item(i
), flags
);
690 // -----------------------------------------------------------------------
692 void wxPropertyGridPageState::DoSort( int flags
)
694 DoSortChildren( m_properties
, flags
| wxPG_RECURSE
);
696 // We used to sort categories as well here also if in non-categorized
697 // mode, but doing would naturally cause child indices to become
701 // -----------------------------------------------------------------------
703 bool wxPropertyGridPageState::PrepareAfterItemsAdded()
705 if ( !m_itemsAdded
) return false;
707 wxPropertyGrid
* pg
= GetGrid();
711 if ( pg
->HasFlag(wxPG_AUTO_SORT
) )
712 DoSort(wxPG_SORT_TOP_LEVEL_ONLY
);
717 // -----------------------------------------------------------------------
718 // wxPropertyGridPageState splitter, column and hittest functions
719 // -----------------------------------------------------------------------
721 wxPGProperty
* wxPropertyGridPageState::DoGetItemAtY( int y
) const
728 return m_properties
->GetItemAtY(y
, GetGrid()->m_lineHeight
, &a
);
731 // -----------------------------------------------------------------------
733 wxPropertyGridHitTestResult
wxPropertyGridPageState::HitTest( const wxPoint
&pt
) const
735 wxPropertyGridHitTestResult result
;
736 result
.column
= HitTestH( pt
.x
, &result
.splitter
, &result
.splitterHitOffset
);
737 result
.property
= DoGetItemAtY( pt
.y
);
741 // -----------------------------------------------------------------------
743 // Used by SetSplitterLeft() and DotFitColumns()
744 int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC
& dc
,
749 wxPropertyGrid
* pg
= m_pPropGrid
;
754 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
756 wxPGProperty
* p
= pwc
->Item(i
);
757 if ( !p
->IsCategory() )
759 const wxPGCell
* cell
= NULL
;
761 p
->GetDisplayInfo(col
, -1, 0, &text
, &cell
);
762 dc
.GetTextExtent(text
, &w
, &h
);
764 w
+= ( ((int)p
->m_depth
-1) * pg
->m_subgroup_extramargin
);
767 // TODO: Add bitmap support.
769 w
+= (wxPG_XBEFORETEXT
*2);
775 if ( p
->GetChildCount() &&
776 ( subProps
|| p
->IsCategory() ) )
778 w
= GetColumnFitWidth( dc
, p
, col
, subProps
);
788 int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn
) const
790 int n
= GetGrid()->m_marginWidth
;
792 for ( i
=0; i
<=splitterColumn
; i
++ )
797 int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column
) ) const
799 return wxPG_DRAG_MARGIN
;
802 void wxPropertyGridPageState::PropagateColSizeDec( int column
, int decrease
, int dir
)
804 int origWidth
= m_colWidths
[column
];
805 m_colWidths
[column
] -= decrease
;
806 int min
= GetColumnMinWidth(column
);
808 if ( m_colWidths
[column
] < min
)
810 more
= decrease
- (origWidth
- min
);
811 m_colWidths
[column
] = min
;
815 // FIXME: Causes erratic splitter changing, so as a workaround
816 // disabled if two or less columns.
818 if ( m_colWidths
.size() <= 2 )
822 if ( more
&& column
< (int)m_colWidths
.size() && column
>= 0 )
823 PropagateColSizeDec( column
, more
, dir
);
826 void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos
, int splitterColumn
, bool WXUNUSED(allPages
), bool fromAutoCenter
)
828 wxPropertyGrid
* pg
= GetGrid();
830 int adjust
= newXPos
- DoGetSplitterPosition(splitterColumn
);
832 if ( !pg
->HasVirtualWidth() )
838 otherColumn
= splitterColumn
+ 1;
839 if ( otherColumn
== (int)m_colWidths
.size() )
841 m_colWidths
[splitterColumn
] += adjust
;
842 PropagateColSizeDec( otherColumn
, adjust
, 1 );
846 otherColumn
= splitterColumn
+ 1;
847 if ( otherColumn
== (int)m_colWidths
.size() )
849 m_colWidths
[otherColumn
] -= adjust
;
850 PropagateColSizeDec( splitterColumn
, -adjust
, -1 );
855 m_colWidths
[splitterColumn
] += adjust
;
858 if ( splitterColumn
== 0 )
859 m_fSplitterX
= (double) newXPos
;
861 if ( !fromAutoCenter
)
863 // Don't allow initial splitter auto-positioning after this.
864 if ( pg
->GetState() == this )
865 pg
->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
871 // Moves splitter so that all labels are visible, but just.
872 void wxPropertyGridPageState::SetSplitterLeft( bool subProps
)
874 wxPropertyGrid
* pg
= GetGrid();
876 dc
.SetFont(pg
->GetFont());
878 int maxW
= GetColumnFitWidth(dc
, m_properties
, 0, subProps
);
882 maxW
+= pg
->m_marginWidth
;
883 DoSetSplitterPosition( maxW
);
886 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
889 wxSize
wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize
) )
891 wxPropertyGrid
* pg
= GetGrid();
893 dc
.SetFont(pg
->GetFont());
895 int marginWidth
= pg
->m_marginWidth
;
896 int accWid
= marginWidth
;
897 int maxColWidth
= 500;
899 for ( unsigned int col
=0; col
< GetColumnCount(); col
++ )
901 int fitWid
= GetColumnFitWidth(dc
, m_properties
, col
, true);
902 int colMinWidth
= GetColumnMinWidth(col
);
903 if ( fitWid
< colMinWidth
)
904 fitWid
= colMinWidth
;
905 else if ( fitWid
> maxColWidth
)
906 fitWid
= maxColWidth
;
908 m_colWidths
[col
] = fitWid
;
913 // Expand last one to fill the width
914 int remaining
= m_width
- accWid
;
915 m_colWidths
[GetColumnCount()-1] += remaining
;
917 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
919 int firstSplitterX
= marginWidth
+ m_colWidths
[0];
920 m_fSplitterX
= (double) firstSplitterX
;
922 // Don't allow initial splitter auto-positioning after this.
923 if ( pg
->GetState() == this )
925 pg
->SetSplitterPosition(firstSplitterX
, false);
930 pg
->GetVirtualSize(&x
, &y
);
932 return wxSize(accWid
, y
);
935 void wxPropertyGridPageState::CheckColumnWidths( int widthChange
)
940 wxPropertyGrid
* pg
= GetGrid();
943 unsigned int lastColumn
= m_colWidths
.size() - 1;
945 int clientWidth
= pg
->GetClientSize().x
;
948 // Column to reduce, if needed. Take last one that exceeds minimum width.
951 wxLogTrace("propgrid",
952 wxS("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"),
957 for ( i
=0; i
<m_colWidths
.size(); i
++ )
959 int min
= GetColumnMinWidth(i
);
960 if ( m_colWidths
[i
] <= min
)
962 m_colWidths
[i
] = min
;
966 // Always reduce the last column that is larger than minimum size
967 // (looks nicer, even with auto-centering enabled).
972 int colsWidth
= pg
->m_marginWidth
;
973 for ( i
=0; i
<m_colWidths
.size(); i
++ )
974 colsWidth
+= m_colWidths
[i
];
976 wxLogTrace("propgrid",
977 wxS(" HasVirtualWidth: %i colsWidth: %i"),
978 (int)pg
->HasVirtualWidth(), colsWidth
);
980 // Then mode-based requirement
981 if ( !pg
->HasVirtualWidth() )
983 int widthHigher
= width
- colsWidth
;
985 // Adapt colsWidth to width
986 if ( colsWidth
< width
)
989 wxLogTrace("propgrid",
990 wxS(" Adjust last column to %i"),
991 m_colWidths
[lastColumn
] + widthHigher
);
992 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + widthHigher
;
994 else if ( colsWidth
> width
)
997 if ( reduceCol
!= -1 )
999 wxLogTrace("propgrid",
1000 wxT(" Reduce column %i (by %i)"),
1001 reduceCol
, -widthHigher
);
1003 // Reduce widest column, and recheck
1004 m_colWidths
[reduceCol
] = m_colWidths
[reduceCol
] + widthHigher
;
1005 CheckColumnWidths();
1011 // Only check colsWidth against clientWidth
1012 if ( colsWidth
< clientWidth
)
1014 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + (clientWidth
-colsWidth
);
1017 m_width
= colsWidth
;
1019 // If width changed, recalculate virtual size
1020 if ( pg
->GetState() == this )
1021 pg
->RecalculateVirtualSize();
1024 for ( i
=0; i
<m_colWidths
.size(); i
++ )
1026 wxLogTrace("propgrid", wxS("col%i: %i"), i
, m_colWidths
[i
]);
1029 // Auto center splitter
1030 if ( !(pg
->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) &&
1031 m_colWidths
.size() == 2 )
1033 float centerX
= (float)(pg
->m_width
/2);
1036 if ( m_fSplitterX
< 0.0 )
1038 splitterX
= centerX
;
1040 else if ( widthChange
)
1042 //float centerX = float(pg->GetSize().x) * 0.5;
1045 splitterX
= m_fSplitterX
+ (float(widthChange
) * 0.5);
1046 float deviation
= fabs(centerX
- splitterX
);
1048 // If deviating from center, adjust towards it
1049 if ( deviation
> 20.0 )
1051 if ( splitterX
> centerX
)
1059 // No width change, just keep sure we keep splitter position intact
1060 splitterX
= m_fSplitterX
;
1061 float deviation
= fabs(centerX
- splitterX
);
1062 if ( deviation
> 50.0 )
1064 splitterX
= centerX
;
1068 DoSetSplitterPosition((int)splitterX
, 0, false, true);
1070 m_fSplitterX
= splitterX
; // needed to retain accuracy
1074 void wxPropertyGridPageState::SetColumnCount( int colCount
)
1076 wxASSERT( colCount
>= 2 );
1077 m_colWidths
.SetCount( colCount
, wxPG_DRAG_MARGIN
);
1078 if ( m_colWidths
.size() > (unsigned int)colCount
)
1079 m_colWidths
.RemoveAt( m_colWidths
.size(), m_colWidths
.size() - colCount
);
1081 if ( m_pPropGrid
->GetState() == this )
1082 m_pPropGrid
->RecalculateVirtualSize();
1084 CheckColumnWidths();
1087 // Returns column index, -1 for margin
1088 int wxPropertyGridPageState::HitTestH( int x
, int* pSplitterHit
, int* pSplitterHitOffset
) const
1090 int cx
= GetGrid()->m_marginWidth
;
1092 int prevSplitter
= -1;
1097 if ( col
>= (int)m_colWidths
.size() )
1103 cx
+= m_colWidths
[col
];
1106 // Near prev. splitter
1109 int diff
= x
- prevSplitter
;
1110 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1112 *pSplitterHit
= col
- 1;
1113 *pSplitterHitOffset
= diff
;
1118 // Near next splitter
1119 int nextSplitter
= cx
;
1120 if ( col
< (int)(m_colWidths
.size()-1) )
1122 int diff
= x
- nextSplitter
;
1123 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1125 *pSplitterHit
= col
;
1126 *pSplitterHitOffset
= diff
;
1135 // -----------------------------------------------------------------------
1136 // wxPropertyGridPageState property value setting and getting
1137 // -----------------------------------------------------------------------
1139 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty
* p
, const wxString
& value
)
1143 int flags
= wxPG_REPORT_ERROR
|wxPG_FULL_VALUE
|wxPG_PROGRAMMATIC_VALUE
;
1145 wxVariant variant
= p
->GetValueRef();
1148 if ( p
->GetMaxLength() <= 0 )
1149 res
= p
->StringToValue( variant
, value
, flags
);
1151 res
= p
->StringToValue( variant
, value
.Mid(0,p
->GetMaxLength()), flags
);
1155 p
->SetValue(variant
);
1156 if ( p
== m_pPropGrid
->GetSelection() &&
1157 this == m_pPropGrid
->GetState() )
1158 m_pPropGrid
->RefreshEditor();
1166 // -----------------------------------------------------------------------
1168 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty
* p
, wxVariant
& value
)
1173 if ( p
== m_pPropGrid
->GetSelection() &&
1174 this == m_pPropGrid
->GetState() )
1175 m_pPropGrid
->RefreshEditor();
1182 // -----------------------------------------------------------------------
1184 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty
* p
, wxObject
* value
)
1188 // wnd_primary has to be given so the control can be updated as well.
1190 DoSetPropertyValue(p
, v
);
1196 // -----------------------------------------------------------------------
1197 // wxPropertyGridPageState property operations
1198 // -----------------------------------------------------------------------
1200 bool wxPropertyGridPageState::DoIsPropertySelected( wxPGProperty
* prop
) const
1202 const wxArrayPGProperty
& selection
= m_selection
;
1204 for ( unsigned int i
=0; i
<selection
.size(); i
++ )
1206 if ( selection
[i
] == prop
)
1213 // -----------------------------------------------------------------------
1215 void wxPropertyGridPageState::DoRemoveFromSelection( wxPGProperty
* prop
)
1217 for ( unsigned int i
=0; i
<m_selection
.size(); i
++ )
1219 if ( m_selection
[i
] == prop
)
1221 m_selection
.erase( m_selection
.begin() + i
);
1227 // -----------------------------------------------------------------------
1229 bool wxPropertyGridPageState::DoCollapse( wxPGProperty
* p
)
1231 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1233 if ( !p
->GetChildCount() ) return false;
1235 if ( !p
->IsExpanded() ) return false;
1237 p
->SetExpanded(false);
1239 VirtualHeightChanged();
1244 // -----------------------------------------------------------------------
1246 bool wxPropertyGridPageState::DoExpand( wxPGProperty
* p
)
1248 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1250 if ( !p
->GetChildCount() ) return false;
1252 if ( p
->IsExpanded() ) return false;
1254 p
->SetExpanded(true);
1256 VirtualHeightChanged();
1261 // -----------------------------------------------------------------------
1263 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty
* p
, unsigned int flags
)
1265 if ( this == m_pPropGrid
->GetState() )
1266 return m_pPropGrid
->DoSelectProperty( p
, flags
);
1272 // -----------------------------------------------------------------------
1274 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty
* p
, bool hide
, int flags
)
1277 p
->ClearFlag( wxPG_PROP_HIDDEN
);
1279 p
->SetFlag( wxPG_PROP_HIDDEN
);
1281 if ( flags
& wxPG_RECURSE
)
1284 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1285 DoHideProperty(p
->Item(i
), hide
, flags
| wxPG_RECURSE_STARTS
);
1288 VirtualHeightChanged();
1293 // -----------------------------------------------------------------------
1295 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty
* p
, bool enable
)
1301 if ( !(p
->m_flags
& wxPG_PROP_DISABLED
) )
1306 p
->m_flags
&= ~(wxPG_PROP_DISABLED
);
1310 if ( p
->m_flags
& wxPG_PROP_DISABLED
)
1315 p
->m_flags
|= wxPG_PROP_DISABLED
;
1319 // Apply same to sub-properties as well
1321 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1322 DoEnableProperty( p
->Item(i
), enable
);
1329 // -----------------------------------------------------------------------
1330 // wxPropertyGridPageState wxVariant related routines
1331 // -----------------------------------------------------------------------
1333 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1334 // Never includes sub-properties (unless they are parented by wxParentProperty).
1335 wxVariant
wxPropertyGridPageState::DoGetPropertyValues( const wxString
& listname
,
1336 wxPGProperty
* baseparent
,
1339 wxPGProperty
* pwc
= (wxPGProperty
*) baseparent
;
1341 // Root is the default base-parent.
1345 wxVariantList tempList
;
1346 wxVariant
v( tempList
, listname
);
1348 if ( pwc
->GetChildCount() )
1350 if ( flags
& wxPG_KEEP_STRUCTURE
)
1352 wxASSERT( !pwc
->HasFlag(wxPG_PROP_AGGREGATE
) );
1355 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1357 wxPGProperty
* p
= pwc
->Item(i
);
1358 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1360 wxVariant variant
= p
->GetValue();
1361 variant
.SetName( p
->GetBaseName() );
1362 v
.Append( variant
);
1366 v
.Append( DoGetPropertyValues(p
->m_name
,p
,flags
|wxPG_KEEP_STRUCTURE
) );
1368 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1369 v
.Append( p
->GetAttributesAsList() );
1374 wxPropertyGridConstIterator
it( this, wxPG_ITERATE_DEFAULT
, pwc
->Item(0) );
1375 it
.SetBaseParent( pwc
);
1377 for ( ; !it
.AtEnd(); it
.Next() )
1379 const wxPGProperty
* p
= it
.GetProperty();
1381 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1382 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1384 wxVariant variant
= p
->GetValue();
1385 variant
.SetName( p
->GetName() );
1386 v
.Append( variant
);
1387 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1388 v
.Append( p
->GetAttributesAsList() );
1397 // -----------------------------------------------------------------------
1399 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList
& list
, wxPGProperty
* defaultCategory
)
1401 unsigned char origFrozen
= 1;
1403 if ( m_pPropGrid
->GetState() == this )
1405 origFrozen
= m_pPropGrid
->m_frozen
;
1406 if ( !origFrozen
) m_pPropGrid
->Freeze();
1409 wxPropertyCategory
* use_category
= (wxPropertyCategory
*)defaultCategory
;
1411 if ( !use_category
)
1412 use_category
= (wxPropertyCategory
*)m_properties
;
1414 // Let's iterate over the list of variants.
1415 wxVariantList::const_iterator node
;
1416 int numSpecialEntries
= 0;
1419 // Second pass for special entries
1420 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1422 wxVariant
*current
= (wxVariant
*)*node
;
1424 // Make sure it is wxVariant.
1425 wxASSERT( current
);
1426 wxASSERT( wxStrcmp(current
->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1428 const wxString
& name
= current
->GetName();
1429 if ( name
.length() > 0 )
1432 // '@' signified a special entry
1433 if ( name
[0] == wxS('@') )
1435 numSpecialEntries
++;
1439 wxPGProperty
* foundProp
= BaseGetPropertyByName(name
);
1442 wxPGProperty
* p
= foundProp
;
1444 // If it was a list, we still have to go through it.
1445 if ( wxStrcmp(current
->GetType(), wxS("list")) == 0 )
1447 DoSetPropertyValues( current
->GetList(),
1448 p
->IsCategory()?p
:(NULL
)
1453 wxASSERT_LEVEL_2_MSG(
1454 wxStrcmp(current
->GetType(), p
->GetValue().GetType()) == 0,
1456 wxS("setting value of property \"%s\" from variant"),
1457 p
->GetName().c_str())
1460 p
->SetValue(*current
);
1466 if ( current
->GetType() != wxS("list") )
1472 // Yes, it is; create a sub category and append contents there.
1473 wxPGProperty
* newCat
= DoInsert(use_category
,-1,new wxPropertyCategory(current
->GetName(),wxPG_LABEL
));
1474 DoSetPropertyValues( current
->GetList(), newCat
);
1481 if ( numSpecialEntries
)
1483 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1485 wxVariant
*current
= (wxVariant
*)*node
;
1487 const wxString
& name
= current
->GetName();
1488 if ( name
.length() > 0 )
1491 // '@' signified a special entry
1492 if ( name
[0] == wxS('@') )
1494 numSpecialEntries
--;
1496 size_t pos2
= name
.rfind(wxS('@'));
1497 if ( pos2
> 0 && pos2
< (name
.size()-1) )
1499 wxString propName
= name
.substr(1, pos2
-1);
1500 wxString entryType
= name
.substr(pos2
+1, wxString::npos
);
1502 if ( entryType
== wxS("attr") )
1505 // List of attributes
1506 wxPGProperty
* foundProp
= BaseGetPropertyByName(propName
);
1509 wxASSERT( current
->GetType() == wxPG_VARIANT_TYPE_LIST
);
1511 wxVariantList
& list2
= current
->GetList();
1512 wxVariantList::const_iterator node2
;
1514 for ( node2
= list2
.begin(); node2
!= list2
.end(); ++node2
)
1516 wxVariant
*attr
= (wxVariant
*)*node2
;
1517 foundProp
->SetAttribute( attr
->GetName(), *attr
);
1522 // ERROR: No such property: 'propName'
1528 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1533 if ( !numSpecialEntries
)
1540 m_pPropGrid
->Thaw();
1542 if ( this == m_pPropGrid
->GetState() )
1543 m_pPropGrid
->RefreshEditor();
1548 // -----------------------------------------------------------------------
1549 // wxPropertyGridPageState property adding and removal
1550 // -----------------------------------------------------------------------
1552 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty
* property
,
1553 wxPGProperty
* scheduledParent
)
1555 wxPropertyGrid
* propGrid
= m_pPropGrid
;
1557 // This will allow better behavior.
1558 if ( scheduledParent
== m_properties
)
1559 scheduledParent
= NULL
;
1561 if ( scheduledParent
&& !scheduledParent
->IsCategory() )
1563 wxASSERT_MSG( property
->GetBaseName().length(),
1564 "Property's children must have unique, non-empty names within their scope" );
1567 property
->m_parentState
= this;
1569 if ( property
->IsCategory() )
1572 // Parent of a category must be either root or another category
1573 // (otherwise Bad Things might happen).
1574 wxASSERT_MSG( scheduledParent
== NULL
||
1575 scheduledParent
== m_properties
||
1576 scheduledParent
->IsCategory(),
1577 wxT("Parent of a category must be either root or another category."));
1579 // If we already have category with same name, delete given property
1580 // and use it instead as most recent caption item.
1581 wxPGProperty
* found_id
= BaseGetPropertyByName( property
->GetBaseName() );
1584 wxPropertyCategory
* pwc
= (wxPropertyCategory
*) found_id
;
1585 if ( pwc
->IsCategory() ) // Must be a category.
1588 m_currentCategory
= pwc
;
1595 // Warn for identical names in debug mode.
1596 if ( BaseGetPropertyByName(property
->GetName()) &&
1597 (!scheduledParent
|| scheduledParent
->IsCategory()) )
1599 wxFAIL_MSG(wxString::Format(
1600 "wxPropertyGrid item with name \"%s\" already exists",
1601 property
->GetName()));
1603 wxPGGlobalVars
->m_warnings
++;
1605 #endif // wxDEBUG_LEVEL
1607 // Make sure nothing is selected.
1609 propGrid
->ClearSelection(false);
1611 // NULL parent == root parent
1612 if ( !scheduledParent
)
1613 scheduledParent
= DoGetRoot();
1615 property
->m_parent
= scheduledParent
;
1617 property
->InitAfterAdded(this, propGrid
);
1619 if ( property
->IsCategory() )
1621 wxPropertyCategory
* pc
= wxStaticCast(property
, wxPropertyCategory
);
1623 m_currentCategory
= pc
;
1625 // Calculate text extent for category caption
1627 pc
->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
1633 // -----------------------------------------------------------------------
1635 wxPGProperty
* wxPropertyGridPageState::DoAppend( wxPGProperty
* property
)
1637 wxPropertyCategory
* cur_cat
= m_currentCategory
;
1638 if ( property
->IsCategory() )
1641 return DoInsert( cur_cat
, -1, property
);
1644 // -----------------------------------------------------------------------
1646 wxPGProperty
* wxPropertyGridPageState::DoInsert( wxPGProperty
* parent
, int index
, wxPGProperty
* property
)
1649 parent
= m_properties
;
1651 wxCHECK_MSG( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1653 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1655 bool res
= PrepareToAddItem( property
, (wxPropertyCategory
*)parent
);
1657 // PrepareToAddItem() may just decide to use use current category
1658 // instead of adding new one.
1660 return m_currentCategory
;
1662 bool parentIsRoot
= parent
->IsRoot();
1663 bool parentIsCategory
= parent
->IsCategory();
1665 // Note that item must be added into current mode later.
1667 // If parent is wxParentProperty, just stick it in...
1668 // If parent is root (m_properties), then...
1669 // In categoric mode: Add as last item in m_abcArray (if not category).
1670 // Add to given index in m_regularArray.
1671 // In non-cat mode: Add as last item in m_regularArray.
1672 // Add to given index in m_abcArray.
1673 // If parent is category, then...
1674 // 1) Add to given category in given index.
1675 // 2) Add as last item in m_abcArray.
1677 if ( m_properties
== &m_regularArray
)
1679 // We are currently in Categorized mode
1681 // Only add non-categories to m_abcArray.
1682 if ( m_abcArray
&& !property
->IsCategory() &&
1683 (parentIsCategory
|| parentIsRoot
) )
1685 m_abcArray
->DoAddChild( property
, -1, false );
1688 // Add to current mode.
1689 parent
->DoAddChild( property
, index
, true );
1693 // We are currently in Non-categorized/Alphabetic mode
1695 if ( parentIsCategory
)
1696 // Parent is category.
1697 parent
->DoAddChild( property
, index
, false );
1698 else if ( parentIsRoot
)
1700 m_regularArray
.DoAddChild( property
, -1, false );
1702 // Add to current mode
1703 if ( !property
->IsCategory() )
1704 m_abcArray
->DoAddChild( property
, index
, true );
1708 if ( property
->IsCategory() )
1710 // This is a category caption item.
1712 // Last caption is not the bottom one (this info required by append)
1713 m_lastCaptionBottomnest
= 0;
1716 // Only add name to hashmap if parent is root or category
1717 if ( property
->m_name
.length() &&
1718 (parentIsCategory
|| parentIsRoot
) )
1719 m_dictName
[property
->m_name
] = (void*) property
;
1721 VirtualHeightChanged();
1723 property
->UpdateParentValues();
1730 // -----------------------------------------------------------------------
1732 void wxPropertyGridPageState::DoDelete( wxPGProperty
* item
, bool doDelete
)
1734 wxCHECK_RET( item
->GetParent(),
1735 wxT("this property was already deleted") );
1737 wxCHECK_RET( item
!= &m_regularArray
&& item
!= m_abcArray
,
1738 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1740 unsigned int indinparent
= item
->GetIndexInParent();
1742 wxPGProperty
* pwc
= (wxPGProperty
*)item
;
1743 wxPGProperty
* parent
= item
->GetParent();
1745 wxCHECK_RET( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1746 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1748 wxASSERT( item
->GetParentState() == this );
1750 wxPropertyGrid
* pg
= GetGrid();
1752 if ( DoIsPropertySelected(item
) )
1754 if ( pg
&& pg
->GetState() == this )
1756 pg
->DoRemoveFromSelection(item
,
1757 wxPG_SEL_DELETING
|wxPG_SEL_NOVALIDATE
);
1761 DoRemoveFromSelection(item
);
1765 item
->SetFlag(wxPG_PROP_BEING_DELETED
);
1768 if ( item
->GetChildCount() && !item
->HasFlag(wxPG_PROP_AGGREGATE
) )
1770 // deleting a category
1771 if ( item
->IsCategory() )
1773 if ( pwc
== m_currentCategory
)
1774 m_currentCategory
= NULL
;
1777 item
->DeleteChildren();
1780 if ( !IsInNonCatMode() )
1782 // categorized mode - non-categorized array
1784 // Remove from non-cat array
1785 if ( !item
->IsCategory() &&
1786 (parent
->IsCategory() || parent
->IsRoot()) )
1789 m_abcArray
->RemoveChild(item
);
1792 // categorized mode - categorized array
1793 wxArrayPGProperty
& parentsChildren
= parent
->m_children
;
1794 parentsChildren
.erase( parentsChildren
.begin() + indinparent
);
1795 item
->m_parent
->FixIndicesOfChildren();
1799 // non-categorized mode - categorized array
1801 // We need to find location of item.
1802 wxPGProperty
* cat_parent
= &m_regularArray
;
1803 int cat_index
= m_regularArray
.GetChildCount();
1805 for ( i
= 0; i
< m_regularArray
.GetChildCount(); i
++ )
1807 wxPGProperty
* p
= m_regularArray
.Item(i
);
1808 if ( p
== item
) { cat_index
= i
; break; }
1809 if ( p
->IsCategory() )
1811 int subind
= ((wxPGProperty
*)p
)->Index(item
);
1812 if ( subind
!= wxNOT_FOUND
)
1814 cat_parent
= ((wxPGProperty
*)p
);
1820 cat_parent
->m_children
.erase(cat_parent
->m_children
.begin()+cat_index
);
1822 // non-categorized mode - non-categorized array
1823 if ( !item
->IsCategory() )
1825 wxASSERT( item
->m_parent
== m_abcArray
);
1826 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1827 parentsChildren
.erase(parentsChildren
.begin() + indinparent
);
1828 item
->m_parent
->FixIndicesOfChildren(indinparent
);
1832 if ( item
->GetBaseName().length() &&
1833 (parent
->IsCategory() || parent
->IsRoot()) )
1834 m_dictName
.erase(item
->GetBaseName());
1836 // We need to clear parent grid's m_propHover, if it matches item
1837 if ( pg
&& pg
->m_propHover
== item
)
1838 pg
->m_propHover
= NULL
;
1840 // We can actually delete it now
1844 m_itemsAdded
= 1; // Not a logical assignment (but required nonetheless).
1846 VirtualHeightChanged();
1849 // -----------------------------------------------------------------------
1851 #endif // wxUSE_PROPGRID