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
->DoAddChild(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 unsigned int lastColumn
= m_colWidths
.size() - 1;
942 int clientWidth
= pg
->GetClientSize().x
;
945 // Column to reduce, if needed. Take last one that exceeds minimum width.
948 wxLogTrace("propgrid",
949 wxS("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"),
954 for ( i
=0; i
<m_colWidths
.size(); i
++ )
956 int min
= GetColumnMinWidth(i
);
957 if ( m_colWidths
[i
] <= min
)
959 m_colWidths
[i
] = min
;
963 // Always reduce the last column that is larger than minimum size
964 // (looks nicer, even with auto-centering enabled).
969 int colsWidth
= pg
->m_marginWidth
;
970 for ( i
=0; i
<m_colWidths
.size(); i
++ )
971 colsWidth
+= m_colWidths
[i
];
973 wxLogTrace("propgrid",
974 wxS(" HasVirtualWidth: %i colsWidth: %i"),
975 (int)pg
->HasVirtualWidth(), colsWidth
);
977 // Then mode-based requirement
978 if ( !pg
->HasVirtualWidth() )
980 int widthHigher
= width
- colsWidth
;
982 // Adapt colsWidth to width
983 if ( colsWidth
< width
)
986 wxLogTrace("propgrid",
987 wxS(" Adjust last column to %i"),
988 m_colWidths
[lastColumn
] + widthHigher
);
989 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + widthHigher
;
991 else if ( colsWidth
> width
)
994 if ( reduceCol
!= -1 )
996 wxLogTrace("propgrid",
997 wxT(" Reduce column %i (by %i)"),
998 reduceCol
, -widthHigher
);
1000 // Reduce widest column, and recheck
1001 m_colWidths
[reduceCol
] = m_colWidths
[reduceCol
] + widthHigher
;
1002 CheckColumnWidths();
1008 // Only check colsWidth against clientWidth
1009 if ( colsWidth
< clientWidth
)
1011 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + (clientWidth
-colsWidth
);
1014 m_width
= colsWidth
;
1016 // If width changed, recalculate virtual size
1017 if ( pg
->GetState() == this )
1018 pg
->RecalculateVirtualSize();
1021 for ( i
=0; i
<m_colWidths
.size(); i
++ )
1023 wxLogTrace("propgrid", wxS("col%i: %i"), i
, m_colWidths
[i
]);
1026 // Auto center splitter
1027 if ( !(pg
->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) &&
1028 m_colWidths
.size() == 2 )
1030 float centerX
= (float)(pg
->m_width
/2);
1033 if ( m_fSplitterX
< 0.0 )
1035 splitterX
= centerX
;
1037 else if ( widthChange
)
1039 //float centerX = float(pg->GetSize().x) * 0.5;
1042 splitterX
= m_fSplitterX
+ (float(widthChange
) * 0.5);
1043 float deviation
= fabs(centerX
- splitterX
);
1045 // If deviating from center, adjust towards it
1046 if ( deviation
> 20.0 )
1048 if ( splitterX
> centerX
)
1056 // No width change, just keep sure we keep splitter position intact
1057 splitterX
= m_fSplitterX
;
1058 float deviation
= fabs(centerX
- splitterX
);
1059 if ( deviation
> 50.0 )
1061 splitterX
= centerX
;
1065 DoSetSplitterPosition((int)splitterX
, 0, false, true);
1067 m_fSplitterX
= splitterX
; // needed to retain accuracy
1071 void wxPropertyGridPageState::SetColumnCount( int colCount
)
1073 wxASSERT( colCount
>= 2 );
1074 m_colWidths
.SetCount( colCount
, wxPG_DRAG_MARGIN
);
1075 if ( m_colWidths
.size() > (unsigned int)colCount
)
1076 m_colWidths
.RemoveAt( m_colWidths
.size(), m_colWidths
.size() - colCount
);
1078 if ( m_pPropGrid
->GetState() == this )
1079 m_pPropGrid
->RecalculateVirtualSize();
1081 CheckColumnWidths();
1084 // Returns column index, -1 for margin
1085 int wxPropertyGridPageState::HitTestH( int x
, int* pSplitterHit
, int* pSplitterHitOffset
) const
1087 int cx
= GetGrid()->m_marginWidth
;
1089 int prevSplitter
= -1;
1094 if ( col
>= (int)m_colWidths
.size() )
1100 cx
+= m_colWidths
[col
];
1103 // Near prev. splitter
1106 int diff
= x
- prevSplitter
;
1107 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1109 *pSplitterHit
= col
- 1;
1110 *pSplitterHitOffset
= diff
;
1115 // Near next splitter
1116 int nextSplitter
= cx
;
1117 if ( col
< (int)(m_colWidths
.size()-1) )
1119 int diff
= x
- nextSplitter
;
1120 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1122 *pSplitterHit
= col
;
1123 *pSplitterHitOffset
= diff
;
1132 // -----------------------------------------------------------------------
1133 // wxPropertyGridPageState property value setting and getting
1134 // -----------------------------------------------------------------------
1136 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty
* p
, const wxString
& value
)
1140 int flags
= wxPG_REPORT_ERROR
|wxPG_FULL_VALUE
|wxPG_PROGRAMMATIC_VALUE
;
1142 wxVariant variant
= p
->GetValueRef();
1145 if ( p
->GetMaxLength() <= 0 )
1146 res
= p
->StringToValue( variant
, value
, flags
);
1148 res
= p
->StringToValue( variant
, value
.Mid(0,p
->GetMaxLength()), flags
);
1152 p
->SetValue(variant
);
1153 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1154 m_pPropGrid
->RefreshEditor();
1162 // -----------------------------------------------------------------------
1164 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty
* p
, wxVariant
& value
)
1169 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1170 m_pPropGrid
->RefreshEditor();
1177 // -----------------------------------------------------------------------
1179 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty
* p
, wxObject
* value
)
1183 // wnd_primary has to be given so the control can be updated as well.
1185 DoSetPropertyValue(p
, v
);
1191 // -----------------------------------------------------------------------
1192 // wxPropertyGridPageState property operations
1193 // -----------------------------------------------------------------------
1195 bool wxPropertyGridPageState::DoCollapse( wxPGProperty
* p
)
1197 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1199 if ( !p
->GetChildCount() ) return false;
1201 if ( !p
->IsExpanded() ) return false;
1203 p
->SetExpanded(false);
1205 VirtualHeightChanged();
1210 // -----------------------------------------------------------------------
1212 bool wxPropertyGridPageState::DoExpand( wxPGProperty
* p
)
1214 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1216 if ( !p
->GetChildCount() ) return false;
1218 if ( p
->IsExpanded() ) return false;
1220 p
->SetExpanded(true);
1222 VirtualHeightChanged();
1227 // -----------------------------------------------------------------------
1229 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty
* p
, unsigned int flags
)
1231 if ( this == m_pPropGrid
->GetState() )
1232 return m_pPropGrid
->DoSelectProperty( p
, flags
);
1238 // -----------------------------------------------------------------------
1240 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty
* p
, bool hide
, int flags
)
1243 p
->ClearFlag( wxPG_PROP_HIDDEN
);
1245 p
->SetFlag( wxPG_PROP_HIDDEN
);
1247 if ( flags
& wxPG_RECURSE
)
1250 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1251 DoHideProperty(p
->Item(i
), hide
, flags
| wxPG_RECURSE_STARTS
);
1254 VirtualHeightChanged();
1259 // -----------------------------------------------------------------------
1261 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty
* p
, bool enable
)
1267 if ( !(p
->m_flags
& wxPG_PROP_DISABLED
) )
1272 p
->m_flags
&= ~(wxPG_PROP_DISABLED
);
1276 if ( p
->m_flags
& wxPG_PROP_DISABLED
)
1281 p
->m_flags
|= wxPG_PROP_DISABLED
;
1285 // Apply same to sub-properties as well
1287 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1288 DoEnableProperty( p
->Item(i
), enable
);
1295 // -----------------------------------------------------------------------
1296 // wxPropertyGridPageState wxVariant related routines
1297 // -----------------------------------------------------------------------
1299 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1300 // Never includes sub-properties (unless they are parented by wxParentProperty).
1301 wxVariant
wxPropertyGridPageState::DoGetPropertyValues( const wxString
& listname
,
1302 wxPGProperty
* baseparent
,
1305 wxPGProperty
* pwc
= (wxPGProperty
*) baseparent
;
1307 // Root is the default base-parent.
1311 wxVariantList tempList
;
1312 wxVariant
v( tempList
, listname
);
1314 if ( pwc
->GetChildCount() )
1316 if ( flags
& wxPG_KEEP_STRUCTURE
)
1318 wxASSERT( !pwc
->HasFlag(wxPG_PROP_AGGREGATE
) );
1321 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1323 wxPGProperty
* p
= pwc
->Item(i
);
1324 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1326 wxVariant variant
= p
->GetValue();
1327 variant
.SetName( p
->GetBaseName() );
1328 v
.Append( variant
);
1332 v
.Append( DoGetPropertyValues(p
->m_name
,p
,flags
|wxPG_KEEP_STRUCTURE
) );
1334 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1335 v
.Append( p
->GetAttributesAsList() );
1340 wxPropertyGridConstIterator
it( this, wxPG_ITERATE_DEFAULT
, pwc
->Item(0) );
1341 it
.SetBaseParent( pwc
);
1343 for ( ; !it
.AtEnd(); it
.Next() )
1345 const wxPGProperty
* p
= it
.GetProperty();
1347 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1348 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1350 wxVariant variant
= p
->GetValue();
1351 variant
.SetName( p
->GetName() );
1352 v
.Append( variant
);
1353 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1354 v
.Append( p
->GetAttributesAsList() );
1363 // -----------------------------------------------------------------------
1365 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList
& list
, wxPGProperty
* defaultCategory
)
1367 unsigned char origFrozen
= 1;
1369 if ( m_pPropGrid
->GetState() == this )
1371 origFrozen
= m_pPropGrid
->m_frozen
;
1372 if ( !origFrozen
) m_pPropGrid
->Freeze();
1375 wxPropertyCategory
* use_category
= (wxPropertyCategory
*)defaultCategory
;
1377 if ( !use_category
)
1378 use_category
= (wxPropertyCategory
*)m_properties
;
1380 // Let's iterate over the list of variants.
1381 wxVariantList::const_iterator node
;
1382 int numSpecialEntries
= 0;
1385 // Second pass for special entries
1386 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1388 wxVariant
*current
= (wxVariant
*)*node
;
1390 // Make sure it is wxVariant.
1391 wxASSERT( current
);
1392 wxASSERT( wxStrcmp(current
->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1394 const wxString
& name
= current
->GetName();
1395 if ( name
.length() > 0 )
1398 // '@' signified a special entry
1399 if ( name
[0] == wxS('@') )
1401 numSpecialEntries
++;
1405 wxPGProperty
* foundProp
= BaseGetPropertyByName(name
);
1408 wxPGProperty
* p
= foundProp
;
1410 // If it was a list, we still have to go through it.
1411 if ( wxStrcmp(current
->GetType(), wxS("list")) == 0 )
1413 DoSetPropertyValues( current
->GetList(),
1414 p
->IsCategory()?p
:(NULL
)
1419 wxASSERT_LEVEL_2_MSG(
1420 wxStrcmp(current
->GetType(), p
->GetValue().GetType()) == 0,
1422 wxS("setting value of property \"%s\" from variant"),
1423 p
->GetName().c_str())
1426 p
->SetValue(*current
);
1432 if ( current
->GetType() != wxS("list") )
1438 // Yes, it is; create a sub category and append contents there.
1439 wxPGProperty
* newCat
= DoInsert(use_category
,-1,new wxPropertyCategory(current
->GetName(),wxPG_LABEL
));
1440 DoSetPropertyValues( current
->GetList(), newCat
);
1447 if ( numSpecialEntries
)
1449 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1451 wxVariant
*current
= (wxVariant
*)*node
;
1453 const wxString
& name
= current
->GetName();
1454 if ( name
.length() > 0 )
1457 // '@' signified a special entry
1458 if ( name
[0] == wxS('@') )
1460 numSpecialEntries
--;
1462 size_t pos2
= name
.rfind(wxS('@'));
1463 if ( pos2
> 0 && pos2
< (name
.size()-1) )
1465 wxString propName
= name
.substr(1, pos2
-1);
1466 wxString entryType
= name
.substr(pos2
+1, wxString::npos
);
1468 if ( entryType
== wxS("attr") )
1471 // List of attributes
1472 wxPGProperty
* foundProp
= BaseGetPropertyByName(propName
);
1475 wxASSERT( current
->GetType() == wxPG_VARIANT_TYPE_LIST
);
1477 wxVariantList
& list2
= current
->GetList();
1478 wxVariantList::const_iterator node2
;
1480 for ( node2
= list2
.begin(); node2
!= list2
.end(); ++node2
)
1482 wxVariant
*attr
= (wxVariant
*)*node2
;
1483 foundProp
->SetAttribute( attr
->GetName(), *attr
);
1488 // ERROR: No such property: 'propName'
1494 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1499 if ( !numSpecialEntries
)
1506 m_pPropGrid
->Thaw();
1508 if ( this == m_pPropGrid
->GetState() )
1509 m_pPropGrid
->RefreshEditor();
1514 // -----------------------------------------------------------------------
1515 // wxPropertyGridPageState property adding and removal
1516 // -----------------------------------------------------------------------
1518 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty
* property
,
1519 wxPGProperty
* scheduledParent
)
1521 wxPropertyGrid
* propGrid
= m_pPropGrid
;
1523 // This will allow better behavior.
1524 if ( scheduledParent
== m_properties
)
1525 scheduledParent
= NULL
;
1527 if ( scheduledParent
&& !scheduledParent
->IsCategory() )
1529 wxASSERT_MSG( property
->GetBaseName().length(),
1530 "Property's children must have unique, non-empty names within their scope" );
1533 property
->m_parentState
= this;
1535 if ( property
->IsCategory() )
1538 // Parent of a category must be either root or another category
1539 // (otherwise Bad Things might happen).
1540 wxASSERT_MSG( scheduledParent
== NULL
||
1541 scheduledParent
== m_properties
||
1542 scheduledParent
->IsCategory(),
1543 wxT("Parent of a category must be either root or another category."));
1545 // If we already have category with same name, delete given property
1546 // and use it instead as most recent caption item.
1547 wxPGProperty
* found_id
= BaseGetPropertyByName( property
->GetBaseName() );
1550 wxPropertyCategory
* pwc
= (wxPropertyCategory
*) found_id
;
1551 if ( pwc
->IsCategory() ) // Must be a category.
1554 m_currentCategory
= pwc
;
1561 // Warn for identical names in debug mode.
1562 if ( BaseGetPropertyByName(property
->GetName()) &&
1563 (!scheduledParent
|| scheduledParent
->IsCategory()) )
1565 wxFAIL_MSG(wxString::Format(
1566 "wxPropertyGrid item with name \"%s\" already exists",
1567 property
->GetName()));
1569 wxPGGlobalVars
->m_warnings
++;
1571 #endif // wxDEBUG_LEVEL
1573 // Make sure nothing is selected.
1575 propGrid
->ClearSelection(false);
1577 // NULL parent == root parent
1578 if ( !scheduledParent
)
1579 scheduledParent
= DoGetRoot();
1581 property
->m_parent
= scheduledParent
;
1583 property
->InitAfterAdded(this, propGrid
);
1585 if ( property
->IsCategory() )
1587 wxPropertyCategory
* pc
= wxStaticCast(property
, wxPropertyCategory
);
1589 m_currentCategory
= pc
;
1591 // Calculate text extent for category caption
1593 pc
->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
1599 // -----------------------------------------------------------------------
1601 wxPGProperty
* wxPropertyGridPageState::DoAppend( wxPGProperty
* property
)
1603 wxPropertyCategory
* cur_cat
= m_currentCategory
;
1604 if ( property
->IsCategory() )
1607 return DoInsert( cur_cat
, -1, property
);
1610 // -----------------------------------------------------------------------
1612 wxPGProperty
* wxPropertyGridPageState::DoInsert( wxPGProperty
* parent
, int index
, wxPGProperty
* property
)
1615 parent
= m_properties
;
1617 wxCHECK_MSG( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1619 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1621 bool res
= PrepareToAddItem( property
, (wxPropertyCategory
*)parent
);
1623 // PrepareToAddItem() may just decide to use use current category
1624 // instead of adding new one.
1626 return m_currentCategory
;
1628 bool parentIsRoot
= parent
->IsRoot();
1629 bool parentIsCategory
= parent
->IsCategory();
1631 // Note that item must be added into current mode later.
1633 // If parent is wxParentProperty, just stick it in...
1634 // If parent is root (m_properties), then...
1635 // In categoric mode: Add as last item in m_abcArray (if not category).
1636 // Add to given index in m_regularArray.
1637 // In non-cat mode: Add as last item in m_regularArray.
1638 // Add to given index in m_abcArray.
1639 // If parent is category, then...
1640 // 1) Add to given category in given index.
1641 // 2) Add as last item in m_abcArray.
1643 if ( m_properties
== &m_regularArray
)
1645 // We are currently in Categorized mode
1647 // Only add non-categories to m_abcArray.
1648 if ( m_abcArray
&& !property
->IsCategory() &&
1649 (parentIsCategory
|| parentIsRoot
) )
1651 m_abcArray
->DoAddChild( property
, -1, false );
1654 // Add to current mode.
1655 parent
->DoAddChild( property
, index
, true );
1659 // We are currently in Non-categorized/Alphabetic mode
1661 if ( parentIsCategory
)
1662 // Parent is category.
1663 parent
->DoAddChild( property
, index
, false );
1664 else if ( parentIsRoot
)
1666 m_regularArray
.DoAddChild( property
, -1, false );
1668 // Add to current mode
1669 if ( !property
->IsCategory() )
1670 m_abcArray
->DoAddChild( property
, index
, true );
1674 if ( property
->IsCategory() )
1676 // This is a category caption item.
1678 // Last caption is not the bottom one (this info required by append)
1679 m_lastCaptionBottomnest
= 0;
1682 // Only add name to hashmap if parent is root or category
1683 if ( property
->m_name
.length() &&
1684 (parentIsCategory
|| parentIsRoot
) )
1685 m_dictName
[property
->m_name
] = (void*) property
;
1687 VirtualHeightChanged();
1689 property
->UpdateParentValues();
1696 // -----------------------------------------------------------------------
1698 void wxPropertyGridPageState::DoDelete( wxPGProperty
* item
, bool doDelete
)
1700 wxCHECK_RET( item
->GetParent(),
1701 wxT("this property was already deleted") );
1703 wxCHECK_RET( item
!= &m_regularArray
&& item
!= m_abcArray
,
1704 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1706 unsigned int indinparent
= item
->GetIndexInParent();
1708 wxPGProperty
* pwc
= (wxPGProperty
*)item
;
1709 wxPGProperty
* parent
= item
->GetParent();
1711 wxCHECK_RET( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1712 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1715 if ( item
->GetChildCount() && !item
->HasFlag(wxPG_PROP_AGGREGATE
) )
1717 // deleting a category
1718 if ( item
->IsCategory() )
1720 if ( pwc
== m_currentCategory
)
1721 m_currentCategory
= NULL
;
1724 item
->DeleteChildren();
1727 if ( !IsInNonCatMode() )
1729 // categorized mode - non-categorized array
1731 // Remove from non-cat array
1732 if ( !item
->IsCategory() &&
1733 (parent
->IsCategory() || parent
->IsRoot()) )
1736 m_abcArray
->RemoveChild(item
);
1739 // categorized mode - categorized array
1740 wxArrayPGProperty
& parentsChildren
= parent
->m_children
;
1741 parentsChildren
.erase( parentsChildren
.begin() + indinparent
);
1742 item
->m_parent
->FixIndicesOfChildren();
1746 // non-categorized mode - categorized array
1748 // We need to find location of item.
1749 wxPGProperty
* cat_parent
= &m_regularArray
;
1750 int cat_index
= m_regularArray
.GetChildCount();
1752 for ( i
= 0; i
< m_regularArray
.GetChildCount(); i
++ )
1754 wxPGProperty
* p
= m_regularArray
.Item(i
);
1755 if ( p
== item
) { cat_index
= i
; break; }
1756 if ( p
->IsCategory() )
1758 int subind
= ((wxPGProperty
*)p
)->Index(item
);
1759 if ( subind
!= wxNOT_FOUND
)
1761 cat_parent
= ((wxPGProperty
*)p
);
1767 cat_parent
->m_children
.erase(cat_parent
->m_children
.begin()+cat_index
);
1769 // non-categorized mode - non-categorized array
1770 if ( !item
->IsCategory() )
1772 wxASSERT( item
->m_parent
== m_abcArray
);
1773 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1774 parentsChildren
.erase(parentsChildren
.begin() + indinparent
);
1775 item
->m_parent
->FixIndicesOfChildren(indinparent
);
1779 if ( item
->GetBaseName().length() &&
1780 (parent
->IsCategory() || parent
->IsRoot()) )
1781 m_dictName
.erase(item
->GetBaseName());
1783 // We can actually delete it now
1787 m_itemsAdded
= 1; // Not a logical assignment (but required nonetheless).
1789 VirtualHeightChanged();
1792 // -----------------------------------------------------------------------
1794 #endif // wxUSE_PROPGRID