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 // -----------------------------------------------------------------------
611 static bool wxPG_SortFunc_ByFunction(wxPGProperty
*p1
, wxPGProperty
*p2
)
613 wxPropertyGrid
* pg
= p1
->GetGrid();
614 wxPGSortCallback sortFunction
= pg
->GetSortFunction();
615 return sortFunction(pg
, p1
, p2
) < 0;
618 static bool wxPG_SortFunc_ByLabel(wxPGProperty
*p1
, wxPGProperty
*p2
)
620 return p1
->GetLabel().CmpNoCase( p2
->GetLabel() ) < 0;
625 static int wxPG_SortFunc_ByFunction(wxPGProperty
**pp1
, wxPGProperty
**pp2
)
627 wxPGProperty
*p1
= *pp1
;
628 wxPGProperty
*p2
= *pp2
;
629 wxPropertyGrid
* pg
= p1
->GetGrid();
630 wxPGSortCallback sortFunction
= pg
->GetSortFunction();
631 return sortFunction(pg
, p1
, p2
);
634 static int wxPG_SortFunc_ByLabel(wxPGProperty
**pp1
, wxPGProperty
**pp2
)
636 wxPGProperty
*p1
= *pp1
;
637 wxPGProperty
*p2
= *pp2
;
638 return p1
->GetLabel().CmpNoCase( p2
->GetLabel() );
643 void wxPropertyGridPageState::DoSortChildren( wxPGProperty
* p
,
649 // Can only sort items with children
650 if ( !p
->GetChildCount() )
653 // Never sort children of aggregate properties
654 if ( p
->HasFlag(wxPG_PROP_AGGREGATE
) )
657 if ( (flags
& wxPG_SORT_TOP_LEVEL_ONLY
)
658 && !p
->IsCategory() && !p
->IsRoot() )
662 if ( GetGrid()->GetSortFunction() )
663 std::sort(p
->m_children
.begin(), p
->m_children
.end(),
664 wxPG_SortFunc_ByFunction
);
666 std::sort(p
->m_children
.begin(), p
->m_children
.end(),
667 wxPG_SortFunc_ByLabel
);
669 if ( GetGrid()->GetSortFunction() )
670 p
->m_children
.Sort( wxPG_SortFunc_ByFunction
);
672 p
->m_children
.Sort( wxPG_SortFunc_ByLabel
);
676 p
->FixIndicesOfChildren();
678 if ( flags
& wxPG_RECURSE
)
680 // Apply sort recursively
681 for ( unsigned int i
=0; i
<p
->GetChildCount(); i
++ )
682 DoSortChildren(p
->Item(i
), flags
);
686 // -----------------------------------------------------------------------
688 void wxPropertyGridPageState::DoSort( int flags
)
690 DoSortChildren( m_properties
, flags
| wxPG_RECURSE
);
692 // We used to sort categories as well here also if in non-categorized
693 // mode, but doing would naturally cause child indices to become
697 // -----------------------------------------------------------------------
699 bool wxPropertyGridPageState::PrepareAfterItemsAdded()
701 if ( !m_itemsAdded
) return false;
703 wxPropertyGrid
* pg
= GetGrid();
707 if ( pg
->HasFlag(wxPG_AUTO_SORT
) )
708 DoSort(wxPG_SORT_TOP_LEVEL_ONLY
);
713 // -----------------------------------------------------------------------
714 // wxPropertyGridPageState splitter, column and hittest functions
715 // -----------------------------------------------------------------------
717 wxPGProperty
* wxPropertyGridPageState::DoGetItemAtY( int y
) const
724 return m_properties
->GetItemAtY(y
, GetGrid()->m_lineHeight
, &a
);
727 // -----------------------------------------------------------------------
729 wxPropertyGridHitTestResult
wxPropertyGridPageState::HitTest( const wxPoint
&pt
) const
731 wxPropertyGridHitTestResult result
;
732 result
.column
= HitTestH( pt
.x
, &result
.splitter
, &result
.splitterHitOffset
);
733 result
.property
= DoGetItemAtY( pt
.y
);
737 // -----------------------------------------------------------------------
739 // Used by SetSplitterLeft() and DotFitColumns()
740 int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC
& dc
,
745 wxPropertyGrid
* pg
= m_pPropGrid
;
750 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
752 wxPGProperty
* p
= pwc
->Item(i
);
753 if ( !p
->IsCategory() )
755 const wxPGCell
* cell
= NULL
;
757 p
->GetDisplayInfo(col
, -1, 0, &text
, &cell
);
758 dc
.GetTextExtent(text
, &w
, &h
);
760 w
+= ( ((int)p
->m_depth
-1) * pg
->m_subgroup_extramargin
);
763 // TODO: Add bitmap support.
765 w
+= (wxPG_XBEFORETEXT
*2);
771 if ( p
->GetChildCount() &&
772 ( subProps
|| p
->IsCategory() ) )
774 w
= GetColumnFitWidth( dc
, p
, col
, subProps
);
784 int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn
) const
786 int n
= GetGrid()->m_marginWidth
;
788 for ( i
=0; i
<=splitterColumn
; i
++ )
793 int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column
) ) const
795 return wxPG_DRAG_MARGIN
;
798 void wxPropertyGridPageState::PropagateColSizeDec( int column
, int decrease
, int dir
)
800 int origWidth
= m_colWidths
[column
];
801 m_colWidths
[column
] -= decrease
;
802 int min
= GetColumnMinWidth(column
);
804 if ( m_colWidths
[column
] < min
)
806 more
= decrease
- (origWidth
- min
);
807 m_colWidths
[column
] = min
;
811 // FIXME: Causes erratic splitter changing, so as a workaround
812 // disabled if two or less columns.
814 if ( m_colWidths
.size() <= 2 )
818 if ( more
&& column
< (int)m_colWidths
.size() && column
>= 0 )
819 PropagateColSizeDec( column
, more
, dir
);
822 void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos
, int splitterColumn
, bool WXUNUSED(allPages
), bool fromAutoCenter
)
824 wxPropertyGrid
* pg
= GetGrid();
826 int adjust
= newXPos
- DoGetSplitterPosition(splitterColumn
);
828 if ( !pg
->HasVirtualWidth() )
834 otherColumn
= splitterColumn
+ 1;
835 if ( otherColumn
== (int)m_colWidths
.size() )
837 m_colWidths
[splitterColumn
] += adjust
;
838 PropagateColSizeDec( otherColumn
, adjust
, 1 );
842 otherColumn
= splitterColumn
+ 1;
843 if ( otherColumn
== (int)m_colWidths
.size() )
845 m_colWidths
[otherColumn
] -= adjust
;
846 PropagateColSizeDec( splitterColumn
, -adjust
, -1 );
851 m_colWidths
[splitterColumn
] += adjust
;
854 if ( splitterColumn
== 0 )
855 m_fSplitterX
= (double) newXPos
;
857 if ( !fromAutoCenter
)
859 // Don't allow initial splitter auto-positioning after this.
860 if ( pg
->GetState() == this )
861 pg
->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
867 // Moves splitter so that all labels are visible, but just.
868 void wxPropertyGridPageState::SetSplitterLeft( bool subProps
)
870 wxPropertyGrid
* pg
= GetGrid();
872 dc
.SetFont(pg
->GetFont());
874 int maxW
= GetColumnFitWidth(dc
, m_properties
, 0, subProps
);
878 maxW
+= pg
->m_marginWidth
;
879 DoSetSplitterPosition( maxW
);
882 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
885 wxSize
wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize
) )
887 wxPropertyGrid
* pg
= GetGrid();
889 dc
.SetFont(pg
->GetFont());
891 int marginWidth
= pg
->m_marginWidth
;
892 int accWid
= marginWidth
;
893 int maxColWidth
= 500;
895 for ( unsigned int col
=0; col
< GetColumnCount(); col
++ )
897 int fitWid
= GetColumnFitWidth(dc
, m_properties
, col
, true);
898 int colMinWidth
= GetColumnMinWidth(col
);
899 if ( fitWid
< colMinWidth
)
900 fitWid
= colMinWidth
;
901 else if ( fitWid
> maxColWidth
)
902 fitWid
= maxColWidth
;
904 m_colWidths
[col
] = fitWid
;
909 // Expand last one to fill the width
910 int remaining
= m_width
- accWid
;
911 m_colWidths
[GetColumnCount()-1] += remaining
;
913 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
915 int firstSplitterX
= marginWidth
+ m_colWidths
[0];
916 m_fSplitterX
= (double) firstSplitterX
;
918 // Don't allow initial splitter auto-positioning after this.
919 if ( pg
->GetState() == this )
921 pg
->SetSplitterPosition(firstSplitterX
, false);
926 pg
->GetVirtualSize(&x
, &y
);
928 return wxSize(accWid
, y
);
931 void wxPropertyGridPageState::CheckColumnWidths( int widthChange
)
936 wxPropertyGrid
* pg
= GetGrid();
939 unsigned int lastColumn
= m_colWidths
.size() - 1;
941 int clientWidth
= pg
->GetClientSize().x
;
944 // Column to reduce, if needed. Take last one that exceeds minimum width.
947 wxLogTrace("propgrid",
948 wxS("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"),
953 for ( i
=0; i
<m_colWidths
.size(); i
++ )
955 int min
= GetColumnMinWidth(i
);
956 if ( m_colWidths
[i
] <= min
)
958 m_colWidths
[i
] = min
;
962 // Always reduce the last column that is larger than minimum size
963 // (looks nicer, even with auto-centering enabled).
968 int colsWidth
= pg
->m_marginWidth
;
969 for ( i
=0; i
<m_colWidths
.size(); i
++ )
970 colsWidth
+= m_colWidths
[i
];
972 wxLogTrace("propgrid",
973 wxS(" HasVirtualWidth: %i colsWidth: %i"),
974 (int)pg
->HasVirtualWidth(), colsWidth
);
976 // Then mode-based requirement
977 if ( !pg
->HasVirtualWidth() )
979 int widthHigher
= width
- colsWidth
;
981 // Adapt colsWidth to width
982 if ( colsWidth
< width
)
985 wxLogTrace("propgrid",
986 wxS(" Adjust last column to %i"),
987 m_colWidths
[lastColumn
] + widthHigher
);
988 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + widthHigher
;
990 else if ( colsWidth
> width
)
993 if ( reduceCol
!= -1 )
995 wxLogTrace("propgrid",
996 wxT(" Reduce column %i (by %i)"),
997 reduceCol
, -widthHigher
);
999 // Reduce widest column, and recheck
1000 m_colWidths
[reduceCol
] = m_colWidths
[reduceCol
] + widthHigher
;
1001 CheckColumnWidths();
1007 // Only check colsWidth against clientWidth
1008 if ( colsWidth
< clientWidth
)
1010 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + (clientWidth
-colsWidth
);
1013 m_width
= colsWidth
;
1015 // If width changed, recalculate virtual size
1016 if ( pg
->GetState() == this )
1017 pg
->RecalculateVirtualSize();
1020 for ( i
=0; i
<m_colWidths
.size(); i
++ )
1022 wxLogTrace("propgrid", wxS("col%i: %i"), i
, m_colWidths
[i
]);
1025 // Auto center splitter
1026 if ( !(pg
->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) &&
1027 m_colWidths
.size() == 2 )
1029 float centerX
= (float)(pg
->m_width
/2);
1032 if ( m_fSplitterX
< 0.0 )
1034 splitterX
= centerX
;
1036 else if ( widthChange
)
1038 //float centerX = float(pg->GetSize().x) * 0.5;
1041 splitterX
= m_fSplitterX
+ (float(widthChange
) * 0.5);
1042 float deviation
= fabs(centerX
- splitterX
);
1044 // If deviating from center, adjust towards it
1045 if ( deviation
> 20.0 )
1047 if ( splitterX
> centerX
)
1055 // No width change, just keep sure we keep splitter position intact
1056 splitterX
= m_fSplitterX
;
1057 float deviation
= fabs(centerX
- splitterX
);
1058 if ( deviation
> 50.0 )
1060 splitterX
= centerX
;
1064 DoSetSplitterPosition((int)splitterX
, 0, false, true);
1066 m_fSplitterX
= splitterX
; // needed to retain accuracy
1070 void wxPropertyGridPageState::SetColumnCount( int colCount
)
1072 wxASSERT( colCount
>= 2 );
1073 m_colWidths
.SetCount( colCount
, wxPG_DRAG_MARGIN
);
1074 if ( m_colWidths
.size() > (unsigned int)colCount
)
1075 m_colWidths
.RemoveAt( m_colWidths
.size(), m_colWidths
.size() - colCount
);
1077 if ( m_pPropGrid
->GetState() == this )
1078 m_pPropGrid
->RecalculateVirtualSize();
1080 CheckColumnWidths();
1083 // Returns column index, -1 for margin
1084 int wxPropertyGridPageState::HitTestH( int x
, int* pSplitterHit
, int* pSplitterHitOffset
) const
1086 int cx
= GetGrid()->m_marginWidth
;
1088 int prevSplitter
= -1;
1093 if ( col
>= (int)m_colWidths
.size() )
1099 cx
+= m_colWidths
[col
];
1102 // Near prev. splitter
1105 int diff
= x
- prevSplitter
;
1106 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1108 *pSplitterHit
= col
- 1;
1109 *pSplitterHitOffset
= diff
;
1114 // Near next splitter
1115 int nextSplitter
= cx
;
1116 if ( col
< (int)(m_colWidths
.size()-1) )
1118 int diff
= x
- nextSplitter
;
1119 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1121 *pSplitterHit
= col
;
1122 *pSplitterHitOffset
= diff
;
1131 // -----------------------------------------------------------------------
1132 // wxPropertyGridPageState property value setting and getting
1133 // -----------------------------------------------------------------------
1135 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty
* p
, const wxString
& value
)
1139 int flags
= wxPG_REPORT_ERROR
|wxPG_FULL_VALUE
|wxPG_PROGRAMMATIC_VALUE
;
1141 wxVariant variant
= p
->GetValueRef();
1144 if ( p
->GetMaxLength() <= 0 )
1145 res
= p
->StringToValue( variant
, value
, flags
);
1147 res
= p
->StringToValue( variant
, value
.Mid(0,p
->GetMaxLength()), flags
);
1151 p
->SetValue(variant
);
1152 if ( p
== m_pPropGrid
->GetSelection() &&
1153 this == m_pPropGrid
->GetState() )
1154 m_pPropGrid
->RefreshEditor();
1162 // -----------------------------------------------------------------------
1164 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty
* p
, wxVariant
& value
)
1169 if ( p
== m_pPropGrid
->GetSelection() &&
1170 this == m_pPropGrid
->GetState() )
1171 m_pPropGrid
->RefreshEditor();
1178 // -----------------------------------------------------------------------
1180 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty
* p
, wxObject
* value
)
1184 // wnd_primary has to be given so the control can be updated as well.
1186 DoSetPropertyValue(p
, v
);
1192 // -----------------------------------------------------------------------
1193 // wxPropertyGridPageState property operations
1194 // -----------------------------------------------------------------------
1196 bool wxPropertyGridPageState::DoIsPropertySelected( wxPGProperty
* prop
) const
1198 const wxArrayPGProperty
& selection
= m_selection
;
1200 for ( unsigned int i
=0; i
<selection
.size(); i
++ )
1202 if ( selection
[i
] == prop
)
1209 // -----------------------------------------------------------------------
1211 bool wxPropertyGridPageState::DoCollapse( wxPGProperty
* p
)
1213 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1215 if ( !p
->GetChildCount() ) return false;
1217 if ( !p
->IsExpanded() ) return false;
1219 p
->SetExpanded(false);
1221 VirtualHeightChanged();
1226 // -----------------------------------------------------------------------
1228 bool wxPropertyGridPageState::DoExpand( wxPGProperty
* p
)
1230 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1232 if ( !p
->GetChildCount() ) return false;
1234 if ( p
->IsExpanded() ) return false;
1236 p
->SetExpanded(true);
1238 VirtualHeightChanged();
1243 // -----------------------------------------------------------------------
1245 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty
* p
, unsigned int flags
)
1247 if ( this == m_pPropGrid
->GetState() )
1248 return m_pPropGrid
->DoSelectProperty( p
, flags
);
1254 // -----------------------------------------------------------------------
1256 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty
* p
, bool hide
, int flags
)
1259 p
->ClearFlag( wxPG_PROP_HIDDEN
);
1261 p
->SetFlag( wxPG_PROP_HIDDEN
);
1263 if ( flags
& wxPG_RECURSE
)
1266 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1267 DoHideProperty(p
->Item(i
), hide
, flags
| wxPG_RECURSE_STARTS
);
1270 VirtualHeightChanged();
1275 // -----------------------------------------------------------------------
1277 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty
* p
, bool enable
)
1283 if ( !(p
->m_flags
& wxPG_PROP_DISABLED
) )
1288 p
->m_flags
&= ~(wxPG_PROP_DISABLED
);
1292 if ( p
->m_flags
& wxPG_PROP_DISABLED
)
1297 p
->m_flags
|= wxPG_PROP_DISABLED
;
1301 // Apply same to sub-properties as well
1303 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1304 DoEnableProperty( p
->Item(i
), enable
);
1311 // -----------------------------------------------------------------------
1312 // wxPropertyGridPageState wxVariant related routines
1313 // -----------------------------------------------------------------------
1315 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1316 // Never includes sub-properties (unless they are parented by wxParentProperty).
1317 wxVariant
wxPropertyGridPageState::DoGetPropertyValues( const wxString
& listname
,
1318 wxPGProperty
* baseparent
,
1321 wxPGProperty
* pwc
= (wxPGProperty
*) baseparent
;
1323 // Root is the default base-parent.
1327 wxVariantList tempList
;
1328 wxVariant
v( tempList
, listname
);
1330 if ( pwc
->GetChildCount() )
1332 if ( flags
& wxPG_KEEP_STRUCTURE
)
1334 wxASSERT( !pwc
->HasFlag(wxPG_PROP_AGGREGATE
) );
1337 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1339 wxPGProperty
* p
= pwc
->Item(i
);
1340 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1342 wxVariant variant
= p
->GetValue();
1343 variant
.SetName( p
->GetBaseName() );
1344 v
.Append( variant
);
1348 v
.Append( DoGetPropertyValues(p
->m_name
,p
,flags
|wxPG_KEEP_STRUCTURE
) );
1350 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1351 v
.Append( p
->GetAttributesAsList() );
1356 wxPropertyGridConstIterator
it( this, wxPG_ITERATE_DEFAULT
, pwc
->Item(0) );
1357 it
.SetBaseParent( pwc
);
1359 for ( ; !it
.AtEnd(); it
.Next() )
1361 const wxPGProperty
* p
= it
.GetProperty();
1363 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1364 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1366 wxVariant variant
= p
->GetValue();
1367 variant
.SetName( p
->GetName() );
1368 v
.Append( variant
);
1369 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1370 v
.Append( p
->GetAttributesAsList() );
1379 // -----------------------------------------------------------------------
1381 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList
& list
, wxPGProperty
* defaultCategory
)
1383 unsigned char origFrozen
= 1;
1385 if ( m_pPropGrid
->GetState() == this )
1387 origFrozen
= m_pPropGrid
->m_frozen
;
1388 if ( !origFrozen
) m_pPropGrid
->Freeze();
1391 wxPropertyCategory
* use_category
= (wxPropertyCategory
*)defaultCategory
;
1393 if ( !use_category
)
1394 use_category
= (wxPropertyCategory
*)m_properties
;
1396 // Let's iterate over the list of variants.
1397 wxVariantList::const_iterator node
;
1398 int numSpecialEntries
= 0;
1401 // Second pass for special entries
1402 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1404 wxVariant
*current
= (wxVariant
*)*node
;
1406 // Make sure it is wxVariant.
1407 wxASSERT( current
);
1408 wxASSERT( wxStrcmp(current
->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1410 const wxString
& name
= current
->GetName();
1411 if ( name
.length() > 0 )
1414 // '@' signified a special entry
1415 if ( name
[0] == wxS('@') )
1417 numSpecialEntries
++;
1421 wxPGProperty
* foundProp
= BaseGetPropertyByName(name
);
1424 wxPGProperty
* p
= foundProp
;
1426 // If it was a list, we still have to go through it.
1427 if ( wxStrcmp(current
->GetType(), wxS("list")) == 0 )
1429 DoSetPropertyValues( current
->GetList(),
1430 p
->IsCategory()?p
:(NULL
)
1435 wxASSERT_LEVEL_2_MSG(
1436 wxStrcmp(current
->GetType(), p
->GetValue().GetType()) == 0,
1438 wxS("setting value of property \"%s\" from variant"),
1439 p
->GetName().c_str())
1442 p
->SetValue(*current
);
1448 if ( current
->GetType() != wxS("list") )
1454 // Yes, it is; create a sub category and append contents there.
1455 wxPGProperty
* newCat
= DoInsert(use_category
,-1,new wxPropertyCategory(current
->GetName(),wxPG_LABEL
));
1456 DoSetPropertyValues( current
->GetList(), newCat
);
1463 if ( numSpecialEntries
)
1465 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1467 wxVariant
*current
= (wxVariant
*)*node
;
1469 const wxString
& name
= current
->GetName();
1470 if ( name
.length() > 0 )
1473 // '@' signified a special entry
1474 if ( name
[0] == wxS('@') )
1476 numSpecialEntries
--;
1478 size_t pos2
= name
.rfind(wxS('@'));
1479 if ( pos2
> 0 && pos2
< (name
.size()-1) )
1481 wxString propName
= name
.substr(1, pos2
-1);
1482 wxString entryType
= name
.substr(pos2
+1, wxString::npos
);
1484 if ( entryType
== wxS("attr") )
1487 // List of attributes
1488 wxPGProperty
* foundProp
= BaseGetPropertyByName(propName
);
1491 wxASSERT( current
->GetType() == wxPG_VARIANT_TYPE_LIST
);
1493 wxVariantList
& list2
= current
->GetList();
1494 wxVariantList::const_iterator node2
;
1496 for ( node2
= list2
.begin(); node2
!= list2
.end(); ++node2
)
1498 wxVariant
*attr
= (wxVariant
*)*node2
;
1499 foundProp
->SetAttribute( attr
->GetName(), *attr
);
1504 // ERROR: No such property: 'propName'
1510 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1515 if ( !numSpecialEntries
)
1522 m_pPropGrid
->Thaw();
1524 if ( this == m_pPropGrid
->GetState() )
1525 m_pPropGrid
->RefreshEditor();
1530 // -----------------------------------------------------------------------
1531 // wxPropertyGridPageState property adding and removal
1532 // -----------------------------------------------------------------------
1534 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty
* property
,
1535 wxPGProperty
* scheduledParent
)
1537 wxPropertyGrid
* propGrid
= m_pPropGrid
;
1539 // This will allow better behavior.
1540 if ( scheduledParent
== m_properties
)
1541 scheduledParent
= NULL
;
1543 if ( scheduledParent
&& !scheduledParent
->IsCategory() )
1545 wxASSERT_MSG( property
->GetBaseName().length(),
1546 "Property's children must have unique, non-empty names within their scope" );
1549 property
->m_parentState
= this;
1551 if ( property
->IsCategory() )
1554 // Parent of a category must be either root or another category
1555 // (otherwise Bad Things might happen).
1556 wxASSERT_MSG( scheduledParent
== NULL
||
1557 scheduledParent
== m_properties
||
1558 scheduledParent
->IsCategory(),
1559 wxT("Parent of a category must be either root or another category."));
1561 // If we already have category with same name, delete given property
1562 // and use it instead as most recent caption item.
1563 wxPGProperty
* found_id
= BaseGetPropertyByName( property
->GetBaseName() );
1566 wxPropertyCategory
* pwc
= (wxPropertyCategory
*) found_id
;
1567 if ( pwc
->IsCategory() ) // Must be a category.
1570 m_currentCategory
= pwc
;
1577 // Warn for identical names in debug mode.
1578 if ( BaseGetPropertyByName(property
->GetName()) &&
1579 (!scheduledParent
|| scheduledParent
->IsCategory()) )
1581 wxFAIL_MSG(wxString::Format(
1582 "wxPropertyGrid item with name \"%s\" already exists",
1583 property
->GetName()));
1585 wxPGGlobalVars
->m_warnings
++;
1587 #endif // wxDEBUG_LEVEL
1589 // Make sure nothing is selected.
1591 propGrid
->ClearSelection(false);
1593 // NULL parent == root parent
1594 if ( !scheduledParent
)
1595 scheduledParent
= DoGetRoot();
1597 property
->m_parent
= scheduledParent
;
1599 property
->InitAfterAdded(this, propGrid
);
1601 if ( property
->IsCategory() )
1603 wxPropertyCategory
* pc
= wxStaticCast(property
, wxPropertyCategory
);
1605 m_currentCategory
= pc
;
1607 // Calculate text extent for category caption
1609 pc
->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
1615 // -----------------------------------------------------------------------
1617 wxPGProperty
* wxPropertyGridPageState::DoAppend( wxPGProperty
* property
)
1619 wxPropertyCategory
* cur_cat
= m_currentCategory
;
1620 if ( property
->IsCategory() )
1623 return DoInsert( cur_cat
, -1, property
);
1626 // -----------------------------------------------------------------------
1628 wxPGProperty
* wxPropertyGridPageState::DoInsert( wxPGProperty
* parent
, int index
, wxPGProperty
* property
)
1631 parent
= m_properties
;
1633 wxCHECK_MSG( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1635 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1637 bool res
= PrepareToAddItem( property
, (wxPropertyCategory
*)parent
);
1639 // PrepareToAddItem() may just decide to use use current category
1640 // instead of adding new one.
1642 return m_currentCategory
;
1644 bool parentIsRoot
= parent
->IsRoot();
1645 bool parentIsCategory
= parent
->IsCategory();
1647 // Note that item must be added into current mode later.
1649 // If parent is wxParentProperty, just stick it in...
1650 // If parent is root (m_properties), then...
1651 // In categoric mode: Add as last item in m_abcArray (if not category).
1652 // Add to given index in m_regularArray.
1653 // In non-cat mode: Add as last item in m_regularArray.
1654 // Add to given index in m_abcArray.
1655 // If parent is category, then...
1656 // 1) Add to given category in given index.
1657 // 2) Add as last item in m_abcArray.
1659 if ( m_properties
== &m_regularArray
)
1661 // We are currently in Categorized mode
1663 // Only add non-categories to m_abcArray.
1664 if ( m_abcArray
&& !property
->IsCategory() &&
1665 (parentIsCategory
|| parentIsRoot
) )
1667 m_abcArray
->DoAddChild( property
, -1, false );
1670 // Add to current mode.
1671 parent
->DoAddChild( property
, index
, true );
1675 // We are currently in Non-categorized/Alphabetic mode
1677 if ( parentIsCategory
)
1678 // Parent is category.
1679 parent
->DoAddChild( property
, index
, false );
1680 else if ( parentIsRoot
)
1682 m_regularArray
.DoAddChild( property
, -1, false );
1684 // Add to current mode
1685 if ( !property
->IsCategory() )
1686 m_abcArray
->DoAddChild( property
, index
, true );
1690 if ( property
->IsCategory() )
1692 // This is a category caption item.
1694 // Last caption is not the bottom one (this info required by append)
1695 m_lastCaptionBottomnest
= 0;
1698 // Only add name to hashmap if parent is root or category
1699 if ( property
->m_name
.length() &&
1700 (parentIsCategory
|| parentIsRoot
) )
1701 m_dictName
[property
->m_name
] = (void*) property
;
1703 VirtualHeightChanged();
1705 property
->UpdateParentValues();
1712 // -----------------------------------------------------------------------
1714 void wxPropertyGridPageState::DoDelete( wxPGProperty
* item
, bool doDelete
)
1716 wxCHECK_RET( item
->GetParent(),
1717 wxT("this property was already deleted") );
1719 wxCHECK_RET( item
!= &m_regularArray
&& item
!= m_abcArray
,
1720 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1722 unsigned int indinparent
= item
->GetIndexInParent();
1724 wxPGProperty
* pwc
= (wxPGProperty
*)item
;
1725 wxPGProperty
* parent
= item
->GetParent();
1727 wxCHECK_RET( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1728 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1730 wxASSERT( item
->GetParentState() == this );
1732 wxPropertyGrid
* pg
= GetGrid();
1734 if ( DoIsPropertySelected(item
) )
1736 if ( pg
&& pg
->GetState() == this )
1738 pg
->DoRemoveFromSelection(item
,
1739 wxPG_SEL_DELETING
|wxPG_SEL_NOVALIDATE
);
1743 m_selection
.Remove(item
);
1747 item
->SetFlag(wxPG_PROP_BEING_DELETED
);
1750 if ( item
->GetChildCount() && !item
->HasFlag(wxPG_PROP_AGGREGATE
) )
1752 // deleting a category
1753 if ( item
->IsCategory() )
1755 if ( pwc
== m_currentCategory
)
1756 m_currentCategory
= NULL
;
1759 item
->DeleteChildren();
1762 if ( !IsInNonCatMode() )
1764 // categorized mode - non-categorized array
1766 // Remove from non-cat array
1767 if ( !item
->IsCategory() &&
1768 (parent
->IsCategory() || parent
->IsRoot()) )
1771 m_abcArray
->RemoveChild(item
);
1774 // categorized mode - categorized array
1775 wxArrayPGProperty
& parentsChildren
= parent
->m_children
;
1776 parentsChildren
.erase( parentsChildren
.begin() + indinparent
);
1777 item
->m_parent
->FixIndicesOfChildren();
1781 // non-categorized mode - categorized array
1783 // We need to find location of item.
1784 wxPGProperty
* cat_parent
= &m_regularArray
;
1785 int cat_index
= m_regularArray
.GetChildCount();
1787 for ( i
= 0; i
< m_regularArray
.GetChildCount(); i
++ )
1789 wxPGProperty
* p
= m_regularArray
.Item(i
);
1790 if ( p
== item
) { cat_index
= i
; break; }
1791 if ( p
->IsCategory() )
1793 int subind
= ((wxPGProperty
*)p
)->Index(item
);
1794 if ( subind
!= wxNOT_FOUND
)
1796 cat_parent
= ((wxPGProperty
*)p
);
1802 cat_parent
->m_children
.erase(cat_parent
->m_children
.begin()+cat_index
);
1804 // non-categorized mode - non-categorized array
1805 if ( !item
->IsCategory() )
1807 wxASSERT( item
->m_parent
== m_abcArray
);
1808 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1809 parentsChildren
.erase(parentsChildren
.begin() + indinparent
);
1810 item
->m_parent
->FixIndicesOfChildren(indinparent
);
1814 if ( item
->GetBaseName().length() &&
1815 (parent
->IsCategory() || parent
->IsRoot()) )
1816 m_dictName
.erase(item
->GetBaseName());
1818 // We need to clear parent grid's m_propHover, if it matches item
1819 if ( pg
&& pg
->m_propHover
== item
)
1820 pg
->m_propHover
= NULL
;
1822 // We can actually delete it now
1826 m_itemsAdded
= 1; // Not a logical assignment (but required nonetheless).
1828 VirtualHeightChanged();
1831 // -----------------------------------------------------------------------
1833 #endif // wxUSE_PROPGRID