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
;
212 m_lastCaptionBottomnest
= 1;
216 m_colWidths
.push_back( wxPG_DEFAULT_SPLITTERX
);
217 m_colWidths
.push_back( wxPG_DEFAULT_SPLITTERX
);
218 m_fSplitterX
= wxPG_DEFAULT_SPLITTERX
;
221 // -----------------------------------------------------------------------
223 wxPropertyGridPageState::~wxPropertyGridPageState()
228 // -----------------------------------------------------------------------
230 void wxPropertyGridPageState::InitNonCatMode()
234 m_abcArray
= new wxPGRootProperty(wxS("<Root_NonCat>"));
235 m_abcArray
->SetParentState(this);
236 m_abcArray
->SetFlag(wxPG_PROP_CHILDREN_ARE_COPIES
);
239 // Must be called when state::m_properties still points to regularArray.
240 wxPGProperty
* oldProperties
= m_properties
;
242 // Must use temp value in state::m_properties for item iteration loop
243 // to run as expected.
244 m_properties
= &m_regularArray
;
246 if ( m_properties
->GetChildCount() )
249 // Prepare m_abcArray
250 wxPropertyGridIterator
it( this, wxPG_ITERATE_PROPERTIES
);
252 for ( ; !it
.AtEnd(); it
.Next() )
254 wxPGProperty
* p
= it
.GetProperty();
255 wxPGProperty
* parent
= p
->GetParent();
256 if ( parent
->IsCategory() || parent
->IsRoot() )
258 m_abcArray
->AddChild2(p
);
259 p
->m_parent
= &m_regularArray
;
264 m_properties
= oldProperties
;
267 // -----------------------------------------------------------------------
269 void wxPropertyGridPageState::DoClear()
271 if ( m_pPropGrid
&& m_pPropGrid
->GetState() == this )
273 m_pPropGrid
->ClearSelection(false);
280 m_regularArray
.Empty();
286 m_currentCategory
= NULL
;
287 m_lastCaptionBottomnest
= 1;
294 // -----------------------------------------------------------------------
296 void wxPropertyGridPageState::CalculateFontAndBitmapStuff( int WXUNUSED(vspacing
) )
298 wxPropertyGrid
* propGrid
= GetGrid();
300 VirtualHeightChanged();
302 // Recalculate caption text extents.
305 for ( i
=0;i
<m_regularArray
.GetChildCount();i
++ )
307 wxPGProperty
* p
=m_regularArray
.Item(i
);
309 if ( p
->IsCategory() )
310 ((wxPropertyCategory
*)p
)->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
314 // -----------------------------------------------------------------------
316 void wxPropertyGridPageState::SetVirtualWidth( int width
)
318 wxASSERT( width
>= 0 );
319 wxPropertyGrid
* pg
= GetGrid();
320 int gw
= pg
->GetClientSize().x
;
327 // -----------------------------------------------------------------------
329 void wxPropertyGridPageState::OnClientWidthChange( int newWidth
, int widthChange
, bool fromOnResize
)
331 wxPropertyGrid
* pg
= GetGrid();
333 if ( pg
->HasVirtualWidth() )
335 if ( m_width
< newWidth
)
336 SetVirtualWidth( newWidth
);
338 CheckColumnWidths(widthChange
);
342 SetVirtualWidth( newWidth
);
344 // This should be done before splitter auto centering
345 // NOTE: Splitter auto-centering is done in this function.
348 CheckColumnWidths(widthChange
);
350 if ( !(GetGrid()->GetInternalFlags() & wxPG_FL_SPLITTER_PRE_SET
) &&
351 (GetGrid()->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) )
353 long timeSinceCreation
= (::wxGetLocalTimeMillis() - GetGrid()->m_timeCreated
).ToLong();
355 // If too long, don't set splitter
356 if ( timeSinceCreation
< 3000 )
358 if ( m_properties
->GetChildCount() || timeSinceCreation
> 750 )
360 SetSplitterLeft( false );
364 DoSetSplitterPosition( newWidth
/ 2 );
365 GetGrid()->ClearInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
372 // -----------------------------------------------------------------------
373 // wxPropertyGridPageState item iteration methods
374 // -----------------------------------------------------------------------
376 wxPGProperty
* wxPropertyGridPageState::GetLastItem( int flags
)
378 if ( !m_properties
->GetChildCount() )
381 wxPG_ITERATOR_CREATE_MASKS(flags
, int itemExMask
, int parentExMask
)
383 // First, get last child of last parent
384 wxPGProperty
* pwc
= (wxPGProperty
*)m_properties
->Last();
385 while ( pwc
->GetChildCount() &&
386 wxPG_ITERATOR_PARENTEXMASK_TEST(pwc
, parentExMask
) )
387 pwc
= (wxPGProperty
*) pwc
->Last();
389 // Then, if it doesn't fit our criteria, back up until we find something that does
390 if ( pwc
->GetFlags() & itemExMask
)
392 wxPropertyGridIterator
it( this, flags
, pwc
);
393 for ( ; !it
.AtEnd(); it
.Prev() )
395 pwc
= (wxPGProperty
*) it
.GetProperty();
401 wxPropertyCategory
* wxPropertyGridPageState::GetPropertyCategory( const wxPGProperty
* p
) const
403 const wxPGProperty
* parent
= (const wxPGProperty
*)p
;
404 const wxPGProperty
* grandparent
= (const wxPGProperty
*)parent
->GetParent();
407 parent
= grandparent
;
408 grandparent
= (wxPGProperty
*)parent
->GetParent();
409 if ( parent
->IsCategory() && grandparent
)
410 return (wxPropertyCategory
*)parent
;
411 } while ( grandparent
);
416 // -----------------------------------------------------------------------
417 // wxPropertyGridPageState GetPropertyXXX methods
418 // -----------------------------------------------------------------------
420 wxPGProperty
* wxPropertyGridPageState::GetPropertyByLabel( const wxString
& label
,
421 wxPGProperty
* parent
) const
426 if ( !parent
) parent
= (wxPGProperty
*) &m_regularArray
;
428 for ( i
=0; i
<parent
->GetChildCount(); i
++ )
430 wxPGProperty
* p
= parent
->Item(i
);
431 if ( p
->m_label
== label
)
433 // Check children recursively.
434 if ( p
->GetChildCount() )
436 p
= GetPropertyByLabel(label
,(wxPGProperty
*)p
);
445 // -----------------------------------------------------------------------
447 wxPGProperty
* wxPropertyGridPageState::BaseGetPropertyByName( const wxString
& name
) const
449 wxPGHashMapS2P::const_iterator it
;
450 it
= m_dictName
.find(name
);
451 if ( it
!= m_dictName
.end() )
452 return (wxPGProperty
*) it
->second
;
456 // -----------------------------------------------------------------------
458 void wxPropertyGridPageState::DoSetPropertyName( wxPGProperty
* p
,
459 const wxString
& newName
)
461 wxCHECK_RET( p
, wxT("invalid property id") );
463 wxPGProperty
* parent
= p
->GetParent();
465 if ( parent
->IsCategory() || parent
->IsRoot() )
467 if ( p
->GetBaseName().length() )
468 m_dictName
.erase( p
->GetBaseName() );
469 if ( newName
.length() )
470 m_dictName
[newName
] = (void*) p
;
473 p
->DoSetName(newName
);
476 // -----------------------------------------------------------------------
477 // wxPropertyGridPageState global operations
478 // -----------------------------------------------------------------------
480 // -----------------------------------------------------------------------
481 // Item iteration macros
482 // NB: Nowadays only needed for alphabetic/categoric mode switching.
483 // -----------------------------------------------------------------------
485 //#define II_INVALID_I 0x00FFFFFF
487 #define ITEM_ITERATION_VARIABLES \
488 wxPGProperty* parent; \
492 #define ITEM_ITERATION_INIT_FROM_THE_TOP \
493 parent = m_properties; \
497 #define ITEM_ITERATION_INIT(startparent, startindex, state) \
498 parent = startparent; \
499 i = (unsigned int)startindex; \
500 if ( parent == NULL ) \
502 parent = state->m_properties; \
507 #define ITEM_ITERATION_LOOP_BEGIN \
510 iMax = parent->GetChildCount(); \
513 wxPGProperty* p = parent->Item(i);
515 #define ITEM_ITERATION_LOOP_END \
516 if ( p->GetChildCount() ) \
519 parent = (wxPGProperty*)p; \
520 iMax = parent->GetChildCount(); \
525 i = parent->m_arrIndex + 1; \
526 parent = parent->m_parent; \
528 while ( parent != NULL );
530 bool wxPropertyGridPageState::EnableCategories( bool enable
)
533 // NB: We can't use wxPropertyGridIterator in this
534 // function, since it depends on m_arrIndexes,
535 // which, among other things, is being fixed here.
537 ITEM_ITERATION_VARIABLES
545 if ( !IsInNonCatMode() )
548 m_properties
= &m_regularArray
;
550 // fix parents, indexes, and depths
551 ITEM_ITERATION_INIT_FROM_THE_TOP
553 ITEM_ITERATION_LOOP_BEGIN
557 p
->m_parent
= parent
;
559 // If parent was category, and this is not,
560 // then the depth stays the same.
561 if ( parent
->IsCategory() &&
563 p
->m_depth
= parent
->m_depth
;
565 p
->m_depth
= parent
->m_depth
+ 1;
567 ITEM_ITERATION_LOOP_END
573 // Disable categories
576 if ( IsInNonCatMode() )
579 // Create array, if necessary.
583 m_properties
= m_abcArray
;
585 // fix parents, indexes, and depths
586 ITEM_ITERATION_INIT_FROM_THE_TOP
588 ITEM_ITERATION_LOOP_BEGIN
592 p
->m_parent
= parent
;
594 p
->m_depth
= parent
->m_depth
+ 1;
596 ITEM_ITERATION_LOOP_END
599 VirtualHeightChanged();
601 if ( m_pPropGrid
->GetState() == this )
602 m_pPropGrid
->RecalculateVirtualSize();
607 // -----------------------------------------------------------------------
612 static bool wxPG_SortFunc_ByFunction(wxPGProperty
*p1
, wxPGProperty
*p2
)
614 wxPropertyGrid
* pg
= p1
->GetGrid();
615 wxPGSortCallback sortFunction
= pg
->GetSortFunction();
616 return sortFunction(pg
, p1
, p2
) < 0;
619 static bool wxPG_SortFunc_ByLabel(wxPGProperty
*p1
, wxPGProperty
*p2
)
621 return p1
->GetLabel().CmpNoCase( p2
->GetLabel() ) < 0;
626 static int wxPG_SortFunc_ByFunction(wxPGProperty
**pp1
, wxPGProperty
**pp2
)
628 wxPGProperty
*p1
= *pp1
;
629 wxPGProperty
*p2
= *pp2
;
630 wxPropertyGrid
* pg
= p1
->GetGrid();
631 wxPGSortCallback sortFunction
= pg
->GetSortFunction();
632 return sortFunction(pg
, p1
, p2
);
635 static int wxPG_SortFunc_ByLabel(wxPGProperty
**pp1
, wxPGProperty
**pp2
)
637 wxPGProperty
*p1
= *pp1
;
638 wxPGProperty
*p2
= *pp2
;
639 return p1
->GetLabel().CmpNoCase( p2
->GetLabel() );
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() )
663 if ( GetGrid()->GetSortFunction() )
664 std::sort(p
->m_children
.begin(), p
->m_children
.end(),
665 wxPG_SortFunc_ByFunction
);
667 std::sort(p
->m_children
.begin(), p
->m_children
.end(),
668 wxPG_SortFunc_ByLabel
);
670 if ( GetGrid()->GetSortFunction() )
671 p
->m_children
.Sort( wxPG_SortFunc_ByFunction
);
673 p
->m_children
.Sort( wxPG_SortFunc_ByLabel
);
677 p
->FixIndicesOfChildren();
679 if ( flags
& wxPG_RECURSE
)
681 // Apply sort recursively
682 for ( unsigned int i
=0; i
<p
->GetChildCount(); i
++ )
683 DoSortChildren(p
->Item(i
), flags
);
687 // -----------------------------------------------------------------------
689 void wxPropertyGridPageState::DoSort( int flags
)
691 DoSortChildren( m_properties
, flags
| wxPG_RECURSE
);
693 // We used to sort categories as well here also if in non-categorized
694 // mode, but doing would naturally cause child indices to become
698 // -----------------------------------------------------------------------
700 bool wxPropertyGridPageState::PrepareAfterItemsAdded()
702 if ( !m_itemsAdded
) return false;
704 wxPropertyGrid
* pg
= GetGrid();
708 if ( pg
->HasFlag(wxPG_AUTO_SORT
) )
709 DoSort(wxPG_SORT_TOP_LEVEL_ONLY
);
714 // -----------------------------------------------------------------------
715 // wxPropertyGridPageState splitter, column and hittest functions
716 // -----------------------------------------------------------------------
718 wxPGProperty
* wxPropertyGridPageState::DoGetItemAtY( int y
) const
725 return m_properties
->GetItemAtY(y
, GetGrid()->m_lineHeight
, &a
);
728 // -----------------------------------------------------------------------
730 wxPropertyGridHitTestResult
wxPropertyGridPageState::HitTest( const wxPoint
&pt
) const
732 wxPropertyGridHitTestResult result
;
733 result
.column
= HitTestH( pt
.x
, &result
.splitter
, &result
.splitterHitOffset
);
734 result
.property
= DoGetItemAtY( pt
.y
);
738 // -----------------------------------------------------------------------
740 // Used by SetSplitterLeft() and DotFitColumns()
741 int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC
& dc
,
746 wxPropertyGrid
* pg
= m_pPropGrid
;
751 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
753 wxPGProperty
* p
= pwc
->Item(i
);
754 if ( !p
->IsCategory() )
756 const wxPGCell
* cell
= NULL
;
758 p
->GetDisplayInfo(col
, -1, 0, &text
, &cell
);
759 dc
.GetTextExtent(text
, &w
, &h
);
761 w
+= ( ((int)p
->m_depth
-1) * pg
->m_subgroup_extramargin
);
764 // TODO: Add bitmap support.
766 w
+= (wxPG_XBEFORETEXT
*2);
772 if ( p
->GetChildCount() &&
773 ( subProps
|| p
->IsCategory() ) )
775 w
= GetColumnFitWidth( dc
, p
, col
, subProps
);
785 int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn
) const
787 int n
= GetGrid()->m_marginWidth
;
789 for ( i
=0; i
<=splitterColumn
; i
++ )
794 int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column
) ) const
796 return wxPG_DRAG_MARGIN
;
799 void wxPropertyGridPageState::PropagateColSizeDec( int column
, int decrease
, int dir
)
801 int origWidth
= m_colWidths
[column
];
802 m_colWidths
[column
] -= decrease
;
803 int min
= GetColumnMinWidth(column
);
805 if ( m_colWidths
[column
] < min
)
807 more
= decrease
- (origWidth
- min
);
808 m_colWidths
[column
] = min
;
812 // FIXME: Causes erratic splitter changing, so as a workaround
813 // disabled if two or less columns.
815 if ( m_colWidths
.size() <= 2 )
819 if ( more
&& column
< (int)m_colWidths
.size() && column
>= 0 )
820 PropagateColSizeDec( column
, more
, dir
);
823 void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos
, int splitterColumn
, bool WXUNUSED(allPages
), bool fromAutoCenter
)
825 wxPropertyGrid
* pg
= GetGrid();
827 int adjust
= newXPos
- DoGetSplitterPosition(splitterColumn
);
829 if ( !pg
->HasVirtualWidth() )
835 otherColumn
= splitterColumn
+ 1;
836 if ( otherColumn
== (int)m_colWidths
.size() )
838 m_colWidths
[splitterColumn
] += adjust
;
839 PropagateColSizeDec( otherColumn
, adjust
, 1 );
843 otherColumn
= splitterColumn
+ 1;
844 if ( otherColumn
== (int)m_colWidths
.size() )
846 m_colWidths
[otherColumn
] -= adjust
;
847 PropagateColSizeDec( splitterColumn
, -adjust
, -1 );
852 m_colWidths
[splitterColumn
] += adjust
;
855 if ( splitterColumn
== 0 )
856 m_fSplitterX
= (double) newXPos
;
858 if ( !fromAutoCenter
)
860 // Don't allow initial splitter auto-positioning after this.
861 if ( pg
->GetState() == this )
862 pg
->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
868 // Moves splitter so that all labels are visible, but just.
869 void wxPropertyGridPageState::SetSplitterLeft( bool subProps
)
871 wxPropertyGrid
* pg
= GetGrid();
873 dc
.SetFont(pg
->GetFont());
875 int maxW
= GetColumnFitWidth(dc
, m_properties
, 0, subProps
);
879 maxW
+= pg
->m_marginWidth
;
880 DoSetSplitterPosition( maxW
);
883 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
886 wxSize
wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize
) )
888 wxPropertyGrid
* pg
= GetGrid();
890 dc
.SetFont(pg
->GetFont());
892 int marginWidth
= pg
->m_marginWidth
;
893 int accWid
= marginWidth
;
894 int maxColWidth
= 500;
896 for ( unsigned int col
=0; col
< GetColumnCount(); col
++ )
898 int fitWid
= GetColumnFitWidth(dc
, m_properties
, col
, true);
899 int colMinWidth
= GetColumnMinWidth(col
);
900 if ( fitWid
< colMinWidth
)
901 fitWid
= colMinWidth
;
902 else if ( fitWid
> maxColWidth
)
903 fitWid
= maxColWidth
;
905 m_colWidths
[col
] = fitWid
;
910 // Expand last one to fill the width
911 int remaining
= m_width
- accWid
;
912 m_colWidths
[GetColumnCount()-1] += remaining
;
914 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
916 int firstSplitterX
= marginWidth
+ m_colWidths
[0];
917 m_fSplitterX
= (double) firstSplitterX
;
919 // Don't allow initial splitter auto-positioning after this.
920 if ( pg
->GetState() == this )
922 pg
->SetSplitterPosition(firstSplitterX
, false);
927 pg
->GetVirtualSize(&x
, &y
);
929 return wxSize(accWid
, y
);
932 void wxPropertyGridPageState::CheckColumnWidths( int widthChange
)
937 wxPropertyGrid
* pg
= GetGrid();
940 const bool debug
= false;
944 unsigned int lastColumn
= m_colWidths
.size() - 1;
946 int clientWidth
= pg
->GetClientSize().x
;
949 // Column to reduce, if needed. Take last one that exceeds minimum width.
950 // Except if auto splitter centering is used, in which case use widest.
952 int highestColWidth
= 0;
956 wxLogDebug(wxT("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"), width
, clientWidth
);
961 for ( i
=0; i
<m_colWidths
.size(); i
++ )
963 int min
= GetColumnMinWidth(i
);
964 if ( m_colWidths
[i
] <= min
)
966 m_colWidths
[i
] = min
;
970 if ( pg
->HasFlag(wxPG_SPLITTER_AUTO_CENTER
) )
972 if ( m_colWidths
[i
] >= highestColWidth
)
974 highestColWidth
= m_colWidths
[i
];
985 int colsWidth
= pg
->m_marginWidth
;
986 for ( i
=0; i
<m_colWidths
.size(); i
++ )
987 colsWidth
+= m_colWidths
[i
];
991 wxLogDebug(wxT(" HasVirtualWidth: %i colsWidth: %i"),(int)pg
->HasVirtualWidth(),colsWidth
);
994 // Then mode-based requirement
995 if ( !pg
->HasVirtualWidth() )
997 int widthHigher
= width
- colsWidth
;
999 // Adapt colsWidth to width
1000 if ( colsWidth
< width
)
1005 wxLogDebug(wxT(" Adjust last column to %i"), m_colWidths
[lastColumn
] + widthHigher
);
1007 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + widthHigher
;
1009 else if ( colsWidth
> width
)
1012 if ( reduceCol
!= -1 )
1016 wxLogDebug(wxT(" Reduce column %i (by %i)"), reduceCol
, -widthHigher
);
1018 // Reduce widest column, and recheck
1019 m_colWidths
[reduceCol
] = m_colWidths
[reduceCol
] + widthHigher
;
1020 CheckColumnWidths();
1026 // Only check colsWidth against clientWidth
1027 if ( colsWidth
< clientWidth
)
1029 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + (clientWidth
-colsWidth
);
1032 m_width
= colsWidth
;
1034 // If width changed, recalculate virtual size
1035 if ( pg
->GetState() == this )
1036 pg
->RecalculateVirtualSize();
1041 for ( i
=0; i
<m_colWidths
.size(); i
++ )
1042 wxLogDebug(wxT("col%i: %i"),i
,m_colWidths
[i
]);
1045 // Auto center splitter
1046 if ( !(pg
->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) &&
1047 m_colWidths
.size() == 2 )
1049 float centerX
= (float)(pg
->m_width
/2);
1052 if ( m_fSplitterX
< 0.0 )
1054 splitterX
= centerX
;
1056 else if ( widthChange
)
1058 //float centerX = float(pg->GetSize().x) * 0.5;
1061 splitterX
= m_fSplitterX
+ (float(widthChange
) * 0.5);
1062 float deviation
= fabs(centerX
- splitterX
);
1064 // If deviating from center, adjust towards it
1065 if ( deviation
> 20.0 )
1067 if ( splitterX
> centerX
)
1075 // No width change, just keep sure we keep splitter position intact
1076 splitterX
= m_fSplitterX
;
1077 float deviation
= fabs(centerX
- splitterX
);
1078 if ( deviation
> 50.0 )
1080 splitterX
= centerX
;
1084 DoSetSplitterPosition((int)splitterX
, 0, false, true);
1086 m_fSplitterX
= splitterX
; // needed to retain accuracy
1090 void wxPropertyGridPageState::SetColumnCount( int colCount
)
1092 wxASSERT( colCount
>= 2 );
1093 m_colWidths
.SetCount( colCount
, wxPG_DRAG_MARGIN
);
1094 if ( m_colWidths
.size() > (unsigned int)colCount
)
1095 m_colWidths
.RemoveAt( m_colWidths
.size(), m_colWidths
.size() - colCount
);
1097 if ( m_pPropGrid
->GetState() == this )
1098 m_pPropGrid
->RecalculateVirtualSize();
1100 CheckColumnWidths();
1103 // Returns column index, -1 for margin
1104 int wxPropertyGridPageState::HitTestH( int x
, int* pSplitterHit
, int* pSplitterHitOffset
) const
1106 int cx
= GetGrid()->m_marginWidth
;
1108 int prevSplitter
= -1;
1113 if ( col
>= (int)m_colWidths
.size() )
1119 cx
+= m_colWidths
[col
];
1122 // Near prev. splitter
1125 int diff
= x
- prevSplitter
;
1126 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1128 *pSplitterHit
= col
- 1;
1129 *pSplitterHitOffset
= diff
;
1134 // Near next splitter
1135 int nextSplitter
= cx
;
1136 if ( col
< (int)(m_colWidths
.size()-1) )
1138 int diff
= x
- nextSplitter
;
1139 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1141 *pSplitterHit
= col
;
1142 *pSplitterHitOffset
= diff
;
1151 // -----------------------------------------------------------------------
1152 // wxPropertyGridPageState property value setting and getting
1153 // -----------------------------------------------------------------------
1155 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty
* p
, const wxString
& value
)
1159 int flags
= wxPG_REPORT_ERROR
|wxPG_FULL_VALUE
|wxPG_PROGRAMMATIC_VALUE
;
1161 wxVariant variant
= p
->GetValueRef();
1164 if ( p
->GetMaxLength() <= 0 )
1165 res
= p
->StringToValue( variant
, value
, flags
);
1167 res
= p
->StringToValue( variant
, value
.Mid(0,p
->GetMaxLength()), flags
);
1171 p
->SetValue(variant
);
1172 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1173 m_pPropGrid
->RefreshEditor();
1181 // -----------------------------------------------------------------------
1183 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty
* p
, wxVariant
& value
)
1188 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1189 m_pPropGrid
->RefreshEditor();
1196 // -----------------------------------------------------------------------
1198 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty
* p
, wxObject
* value
)
1202 // wnd_primary has to be given so the control can be updated as well.
1204 DoSetPropertyValue(p
, v
);
1210 // -----------------------------------------------------------------------
1211 // wxPropertyGridPageState property operations
1212 // -----------------------------------------------------------------------
1214 bool wxPropertyGridPageState::DoCollapse( wxPGProperty
* p
)
1216 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1218 if ( !p
->GetChildCount() ) return false;
1220 if ( !p
->IsExpanded() ) return false;
1222 p
->SetExpanded(false);
1224 VirtualHeightChanged();
1229 // -----------------------------------------------------------------------
1231 bool wxPropertyGridPageState::DoExpand( wxPGProperty
* p
)
1233 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1235 if ( !p
->GetChildCount() ) return false;
1237 if ( p
->IsExpanded() ) return false;
1239 p
->SetExpanded(true);
1241 VirtualHeightChanged();
1246 // -----------------------------------------------------------------------
1248 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty
* p
, unsigned int flags
)
1250 if ( this == m_pPropGrid
->GetState() )
1251 return m_pPropGrid
->DoSelectProperty( p
, flags
);
1257 // -----------------------------------------------------------------------
1259 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty
* p
, bool hide
, int flags
)
1262 p
->ClearFlag( wxPG_PROP_HIDDEN
);
1264 p
->SetFlag( wxPG_PROP_HIDDEN
);
1266 if ( flags
& wxPG_RECURSE
)
1269 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1270 DoHideProperty(p
->Item(i
), hide
, flags
| wxPG_RECURSE_STARTS
);
1273 VirtualHeightChanged();
1278 // -----------------------------------------------------------------------
1280 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty
* p
, bool enable
)
1286 if ( !(p
->m_flags
& wxPG_PROP_DISABLED
) )
1291 p
->m_flags
&= ~(wxPG_PROP_DISABLED
);
1295 if ( p
->m_flags
& wxPG_PROP_DISABLED
)
1300 p
->m_flags
|= wxPG_PROP_DISABLED
;
1304 // Apply same to sub-properties as well
1306 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1307 DoEnableProperty( p
->Item(i
), enable
);
1314 // -----------------------------------------------------------------------
1315 // wxPropertyGridPageState wxVariant related routines
1316 // -----------------------------------------------------------------------
1318 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1319 // Never includes sub-properties (unless they are parented by wxParentProperty).
1320 wxVariant
wxPropertyGridPageState::DoGetPropertyValues( const wxString
& listname
,
1321 wxPGProperty
* baseparent
,
1324 wxPGProperty
* pwc
= (wxPGProperty
*) baseparent
;
1326 // Root is the default base-parent.
1330 wxVariantList tempList
;
1331 wxVariant
v( tempList
, listname
);
1333 if ( pwc
->GetChildCount() )
1335 if ( flags
& wxPG_KEEP_STRUCTURE
)
1337 wxASSERT( !pwc
->HasFlag(wxPG_PROP_AGGREGATE
) );
1340 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1342 wxPGProperty
* p
= pwc
->Item(i
);
1343 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1345 wxVariant variant
= p
->GetValue();
1346 variant
.SetName( p
->GetBaseName() );
1347 v
.Append( variant
);
1351 v
.Append( DoGetPropertyValues(p
->m_name
,p
,flags
|wxPG_KEEP_STRUCTURE
) );
1353 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1354 v
.Append( p
->GetAttributesAsList() );
1359 wxPropertyGridConstIterator
it( this, wxPG_ITERATE_DEFAULT
, pwc
->Item(0) );
1360 it
.SetBaseParent( pwc
);
1362 for ( ; !it
.AtEnd(); it
.Next() )
1364 const wxPGProperty
* p
= it
.GetProperty();
1366 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1367 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1369 wxVariant variant
= p
->GetValue();
1370 variant
.SetName( p
->GetName() );
1371 v
.Append( variant
);
1372 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1373 v
.Append( p
->GetAttributesAsList() );
1382 // -----------------------------------------------------------------------
1384 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList
& list
, wxPGProperty
* defaultCategory
)
1386 unsigned char origFrozen
= 1;
1388 if ( m_pPropGrid
->GetState() == this )
1390 origFrozen
= m_pPropGrid
->m_frozen
;
1391 if ( !origFrozen
) m_pPropGrid
->Freeze();
1394 wxPropertyCategory
* use_category
= (wxPropertyCategory
*)defaultCategory
;
1396 if ( !use_category
)
1397 use_category
= (wxPropertyCategory
*)m_properties
;
1399 // Let's iterate over the list of variants.
1400 wxVariantList::const_iterator node
;
1401 int numSpecialEntries
= 0;
1404 // Second pass for special entries
1405 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1407 wxVariant
*current
= (wxVariant
*)*node
;
1409 // Make sure it is wxVariant.
1410 wxASSERT( current
);
1411 wxASSERT( wxStrcmp(current
->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1413 const wxString
& name
= current
->GetName();
1414 if ( name
.length() > 0 )
1417 // '@' signified a special entry
1418 if ( name
[0] == wxS('@') )
1420 numSpecialEntries
++;
1424 wxPGProperty
* foundProp
= BaseGetPropertyByName(name
);
1427 wxPGProperty
* p
= foundProp
;
1429 // If it was a list, we still have to go through it.
1430 if ( wxStrcmp(current
->GetType(), wxS("list")) == 0 )
1432 DoSetPropertyValues( current
->GetList(),
1433 p
->IsCategory()?p
:(NULL
)
1439 if ( wxStrcmp(current
->GetType(), p
->GetValue().GetType()) != 0)
1441 wxLogDebug(wxT("wxPropertyGridPageState::DoSetPropertyValues Warning: Setting value of property \"%s\" from variant"),
1442 p
->GetName().c_str());
1446 p
->SetValue(*current
);
1452 if ( current
->GetType() != wxS("list") )
1458 // Yes, it is; create a sub category and append contents there.
1459 wxPGProperty
* newCat
= DoInsert(use_category
,-1,new wxPropertyCategory(current
->GetName(),wxPG_LABEL
));
1460 DoSetPropertyValues( current
->GetList(), newCat
);
1467 if ( numSpecialEntries
)
1469 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1471 wxVariant
*current
= (wxVariant
*)*node
;
1473 const wxString
& name
= current
->GetName();
1474 if ( name
.length() > 0 )
1477 // '@' signified a special entry
1478 if ( name
[0] == wxS('@') )
1480 numSpecialEntries
--;
1482 size_t pos2
= name
.rfind(wxS('@'));
1483 if ( pos2
> 0 && pos2
< (name
.size()-1) )
1485 wxString propName
= name
.substr(1, pos2
-1);
1486 wxString entryType
= name
.substr(pos2
+1, wxString::npos
);
1488 if ( entryType
== wxS("attr") )
1491 // List of attributes
1492 wxPGProperty
* foundProp
= BaseGetPropertyByName(propName
);
1495 wxASSERT( current
->GetType() == wxPG_VARIANT_TYPE_LIST
);
1497 wxVariantList
& list2
= current
->GetList();
1498 wxVariantList::const_iterator node2
;
1500 for ( node2
= list2
.begin(); node2
!= list2
.end(); ++node2
)
1502 wxVariant
*attr
= (wxVariant
*)*node2
;
1503 foundProp
->SetAttribute( attr
->GetName(), *attr
);
1508 // ERROR: No such property: 'propName'
1514 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1519 if ( !numSpecialEntries
)
1526 m_pPropGrid
->Thaw();
1528 if ( this == m_pPropGrid
->GetState() )
1529 m_pPropGrid
->RefreshEditor();
1534 // -----------------------------------------------------------------------
1535 // wxPropertyGridPageState property adding and removal
1536 // -----------------------------------------------------------------------
1538 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty
* property
,
1539 wxPGProperty
* scheduledParent
)
1541 wxPropertyGrid
* propGrid
= m_pPropGrid
;
1543 // This will allow better behavior.
1544 if ( scheduledParent
== m_properties
)
1545 scheduledParent
= NULL
;
1547 if ( scheduledParent
&& !scheduledParent
->IsCategory() )
1549 wxASSERT_MSG( property
->GetBaseName().length(),
1550 "Property's children must have unique, non-empty names within their scope" );
1553 property
->m_parentState
= this;
1555 if ( property
->IsCategory() )
1558 // Parent of a category must be either root or another category
1559 // (otherwise Bad Things might happen).
1560 wxASSERT_MSG( scheduledParent
== NULL
||
1561 scheduledParent
== m_properties
||
1562 scheduledParent
->IsCategory(),
1563 wxT("Parent of a category must be either root or another category."));
1565 // If we already have category with same name, delete given property
1566 // and use it instead as most recent caption item.
1567 wxPGProperty
* found_id
= BaseGetPropertyByName( property
->GetBaseName() );
1570 wxPropertyCategory
* pwc
= (wxPropertyCategory
*) found_id
;
1571 if ( pwc
->IsCategory() ) // Must be a category.
1574 m_currentCategory
= pwc
;
1581 // Warn for identical names in debug mode.
1582 if ( BaseGetPropertyByName(property
->GetName()) &&
1583 (!scheduledParent
|| scheduledParent
->IsCategory()) )
1585 wxLogError(wxT("wxPropertyGrid: Warning - item with name \"%s\" already exists."),
1586 property
->GetName().c_str());
1587 wxPGGlobalVars
->m_warnings
++;
1591 // Make sure nothing is selected.
1593 propGrid
->ClearSelection(false);
1595 // NULL parent == root parent
1596 if ( !scheduledParent
)
1597 scheduledParent
= DoGetRoot();
1599 property
->m_parent
= scheduledParent
;
1601 property
->InitAfterAdded(this, propGrid
);
1603 if ( property
->IsCategory() )
1605 wxPropertyCategory
* pc
= wxStaticCast(property
, wxPropertyCategory
);
1607 m_currentCategory
= pc
;
1609 // Calculate text extent for category caption
1611 pc
->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
1617 // -----------------------------------------------------------------------
1619 wxPGProperty
* wxPropertyGridPageState::DoAppend( wxPGProperty
* property
)
1621 wxPropertyCategory
* cur_cat
= m_currentCategory
;
1622 if ( property
->IsCategory() )
1625 return DoInsert( cur_cat
, -1, property
);
1628 // -----------------------------------------------------------------------
1630 wxPGProperty
* wxPropertyGridPageState::DoInsert( wxPGProperty
* parent
, int index
, wxPGProperty
* property
)
1633 parent
= m_properties
;
1635 wxCHECK_MSG( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1637 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1639 bool res
= PrepareToAddItem( property
, (wxPropertyCategory
*)parent
);
1641 // PrepareToAddItem() may just decide to use use current category
1642 // instead of adding new one.
1644 return m_currentCategory
;
1646 bool parentIsRoot
= parent
->IsRoot();
1647 bool parentIsCategory
= parent
->IsCategory();
1649 // Note that item must be added into current mode later.
1651 // If parent is wxParentProperty, just stick it in...
1652 // If parent is root (m_properties), then...
1653 // In categoric mode: Add as last item in m_abcArray (if not category).
1654 // Add to given index in m_regularArray.
1655 // In non-cat mode: Add as last item in m_regularArray.
1656 // Add to given index in m_abcArray.
1657 // If parent is category, then...
1658 // 1) Add to given category in given index.
1659 // 2) Add as last item in m_abcArray.
1661 if ( m_properties
== &m_regularArray
)
1663 // We are currently in Categorized mode
1665 // Only add non-categories to m_abcArray.
1666 if ( m_abcArray
&& !property
->IsCategory() &&
1667 (parentIsCategory
|| parentIsRoot
) )
1669 m_abcArray
->AddChild2( property
, -1, false );
1672 // Add to current mode.
1673 parent
->AddChild2( property
, index
, true );
1677 // We are currently in Non-categorized/Alphabetic mode
1679 if ( parentIsCategory
)
1680 // Parent is category.
1681 parent
->AddChild2( property
, index
, false );
1682 else if ( parentIsRoot
)
1684 m_regularArray
.AddChild2( property
, -1, false );
1686 // Add to current mode
1687 if ( !property
->IsCategory() )
1688 m_abcArray
->AddChild2( property
, index
, true );
1692 if ( property
->IsCategory() )
1694 // This is a category caption item.
1696 // Last caption is not the bottom one (this info required by append)
1697 m_lastCaptionBottomnest
= 0;
1700 // Only add name to hashmap if parent is root or category
1701 if ( property
->m_name
.length() &&
1702 (parentIsCategory
|| parentIsRoot
) )
1703 m_dictName
[property
->m_name
] = (void*) property
;
1705 VirtualHeightChanged();
1707 property
->UpdateParentValues();
1714 // -----------------------------------------------------------------------
1716 void wxPropertyGridPageState::DoDelete( wxPGProperty
* item
, bool doDelete
)
1718 wxCHECK_RET( item
->GetParent(),
1719 wxT("this property was already deleted") );
1721 wxCHECK_RET( item
!= &m_regularArray
&& item
!= m_abcArray
,
1722 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1724 unsigned int indinparent
= item
->GetIndexInParent();
1726 wxPGProperty
* pwc
= (wxPGProperty
*)item
;
1727 wxPGProperty
* parent
= item
->GetParent();
1729 wxCHECK_RET( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1730 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1733 if ( item
->GetChildCount() && !item
->HasFlag(wxPG_PROP_AGGREGATE
) )
1735 // deleting a category
1736 if ( item
->IsCategory() )
1738 if ( pwc
== m_currentCategory
)
1739 m_currentCategory
= NULL
;
1742 item
->DeleteChildren();
1745 if ( !IsInNonCatMode() )
1747 // categorized mode - non-categorized array
1749 // Remove from non-cat array
1750 if ( !item
->IsCategory() &&
1751 (parent
->IsCategory() || parent
->IsRoot()) )
1754 m_abcArray
->RemoveChild(item
);
1757 // categorized mode - categorized array
1758 wxArrayPGProperty
& parentsChildren
= parent
->m_children
;
1759 parentsChildren
.erase( parentsChildren
.begin() + indinparent
);
1760 item
->m_parent
->FixIndicesOfChildren();
1764 // non-categorized mode - categorized array
1766 // We need to find location of item.
1767 wxPGProperty
* cat_parent
= &m_regularArray
;
1768 int cat_index
= m_regularArray
.GetChildCount();
1770 for ( i
= 0; i
< m_regularArray
.GetChildCount(); i
++ )
1772 wxPGProperty
* p
= m_regularArray
.Item(i
);
1773 if ( p
== item
) { cat_index
= i
; break; }
1774 if ( p
->IsCategory() )
1776 int subind
= ((wxPGProperty
*)p
)->Index(item
);
1777 if ( subind
!= wxNOT_FOUND
)
1779 cat_parent
= ((wxPGProperty
*)p
);
1785 cat_parent
->m_children
.erase(cat_parent
->m_children
.begin()+cat_index
);
1787 // non-categorized mode - non-categorized array
1788 if ( !item
->IsCategory() )
1790 wxASSERT( item
->m_parent
== m_abcArray
);
1791 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1792 parentsChildren
.erase(parentsChildren
.begin() + indinparent
);
1793 item
->m_parent
->FixIndicesOfChildren(indinparent
);
1797 if ( item
->GetBaseName().length() &&
1798 (parent
->IsCategory() || parent
->IsRoot()) )
1799 m_dictName
.erase(item
->GetBaseName());
1801 // We can actually delete it now
1805 m_itemsAdded
= 1; // Not a logical assignment (but required nonetheless).
1807 VirtualHeightChanged();
1810 // -----------------------------------------------------------------------
1812 #endif // wxUSE_PROPGRID