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
;
79 if ( startPos
== wxTOP
)
85 else if ( startPos
== wxBOTTOM
)
87 property
= state
->GetLastItem(flags
);
93 wxASSERT_MSG( false, wxT("Only supported stating positions are wxTOP and wxBOTTOM") );
97 Init( state
, flags
, property
, dir
);
100 void wxPropertyGridIteratorBase::Assign( const wxPropertyGridIteratorBase
& it
)
102 m_property
= it
.m_property
;
103 m_state
= it
.m_state
;
104 m_baseParent
= it
.m_baseParent
;
105 m_itemExMask
= it
.m_itemExMask
;
106 m_parentExMask
= it
.m_parentExMask
;
109 void wxPropertyGridIteratorBase::Prev()
111 wxPGProperty
* property
= m_property
;
112 wxASSERT( property
);
114 wxPGProperty
* parent
= property
->GetParent();
116 unsigned int index
= property
->GetIndexInParent();
123 property
= parent
->Item(index
);
125 // Go to last children?
126 if ( property
->GetChildCount() &&
127 wxPG_ITERATOR_PARENTEXMASK_TEST(property
, m_parentExMask
) )
130 property
= property
->Last();
136 if ( parent
== m_baseParent
)
147 m_property
= property
;
149 // If property does not match our criteria, skip it
150 if ( property
->GetFlags() & m_itemExMask
)
154 void wxPropertyGridIteratorBase::Next( bool iterateChildren
)
156 wxPGProperty
* property
= m_property
;
157 wxASSERT( property
);
159 if ( property
->GetChildCount() &&
160 wxPG_ITERATOR_PARENTEXMASK_TEST(property
, m_parentExMask
) &&
164 property
= property
->Item(0);
168 wxPGProperty
* parent
= property
->GetParent();
170 unsigned int index
= property
->GetIndexInParent() + 1;
172 if ( index
< parent
->GetChildCount() )
175 property
= parent
->Item(index
);
179 // Next sibling of parent
180 if ( parent
== m_baseParent
)
193 m_property
= property
;
195 // If property does not match our criteria, skip it
196 if ( property
->GetFlags() & m_itemExMask
)
200 // -----------------------------------------------------------------------
201 // wxPropertyGridPageState
202 // -----------------------------------------------------------------------
204 wxPropertyGridPageState::wxPropertyGridPageState()
206 m_pPropGrid
= (wxPropertyGrid
*) NULL
;
207 m_regularArray
.SetParentState(this);
208 m_properties
= &m_regularArray
;
209 m_abcArray
= (wxPGRootProperty
*) NULL
;
210 m_currentCategory
= (wxPropertyCategory
*) NULL
;
211 m_selected
= (wxPGProperty
*) NULL
;
214 m_lastCaptionBottomnest
= 1;
218 m_colWidths
.push_back( wxPG_DEFAULT_SPLITTERX
);
219 m_colWidths
.push_back( wxPG_DEFAULT_SPLITTERX
);
220 m_fSplitterX
= wxPG_DEFAULT_SPLITTERX
;
223 // -----------------------------------------------------------------------
225 wxPropertyGridPageState::~wxPropertyGridPageState()
230 // -----------------------------------------------------------------------
232 void wxPropertyGridPageState::InitNonCatMode()
236 m_abcArray
= new wxPGRootProperty();
237 m_abcArray
->SetParentState(this);
238 m_abcArray
->SetFlag(wxPG_PROP_CHILDREN_ARE_COPIES
);
241 // Must be called when state::m_properties still points to regularArray.
242 wxPGProperty
* oldProperties
= m_properties
;
244 // Must use temp value in state::m_properties for item iteration loop
245 // to run as expected.
246 m_properties
= &m_regularArray
;
248 if ( m_properties
->GetChildCount() )
251 wxPropertyGridIterator
it( this, wxPG_ITERATE_DEFAULT
|wxPG_ITERATE_CATEGORIES
);
253 for ( ; !it
.AtEnd(); it
.Next() )
255 wxPGProperty
* p
= it
.GetProperty();
256 wxPGProperty
* parent
= p
->GetParent();
257 if ( p
->HasFlag(wxPG_PROP_MISC_PARENT
) &&
258 ( parent
== m_properties
|| (parent
->IsCategory() || parent
->IsRoot()) ) )
260 m_abcArray
->AddChild2( p
);
261 p
->m_parent
= &m_regularArray
;
266 m_properties
= oldProperties
;
269 // -----------------------------------------------------------------------
271 void wxPropertyGridPageState::DoClear()
273 m_regularArray
.Empty();
279 m_currentCategory
= (wxPropertyCategory
*) NULL
;
280 m_lastCaptionBottomnest
= 1;
286 m_selected
= (wxPGProperty
*) NULL
;
289 // -----------------------------------------------------------------------
291 void wxPropertyGridPageState::CalculateFontAndBitmapStuff( int WXUNUSED(vspacing
) )
293 wxPropertyGrid
* propGrid
= GetGrid();
295 VirtualHeightChanged();
297 // Recalculate caption text extents.
300 for ( i
=0;i
<m_regularArray
.GetChildCount();i
++ )
302 wxPGProperty
* p
=m_regularArray
.Item(i
);
304 if ( p
->IsCategory() )
305 ((wxPropertyCategory
*)p
)->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
309 // -----------------------------------------------------------------------
311 void wxPropertyGridPageState::SetVirtualWidth( int width
)
313 wxASSERT( width
>= 0 );
314 wxPropertyGrid
* pg
= GetGrid();
315 int gw
= pg
->GetClientSize().x
;
322 // -----------------------------------------------------------------------
324 void wxPropertyGridPageState::OnClientWidthChange( int newWidth
, int widthChange
, bool fromOnResize
)
326 wxPropertyGrid
* pg
= GetGrid();
328 if ( pg
->HasVirtualWidth() )
330 if ( m_width
< newWidth
)
331 SetVirtualWidth( newWidth
);
333 CheckColumnWidths(widthChange
);
337 SetVirtualWidth( newWidth
);
339 // This should be done before splitter auto centering
340 // NOTE: Splitter auto-centering is done in this function.
343 CheckColumnWidths(widthChange
);
345 if ( !(GetGrid()->GetInternalFlags() & wxPG_FL_SPLITTER_PRE_SET
) &&
346 (GetGrid()->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) )
348 long timeSinceCreation
= (::wxGetLocalTimeMillis() - GetGrid()->m_timeCreated
).ToLong();
350 // If too long, don't set splitter
351 if ( timeSinceCreation
< 3000 )
353 if ( m_properties
->GetChildCount() || timeSinceCreation
> 750 )
355 SetSplitterLeft( false );
359 DoSetSplitterPosition( newWidth
/ 2 );
360 GetGrid()->ClearInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
367 // -----------------------------------------------------------------------
368 // wxPropertyGridPageState item iteration methods
369 // -----------------------------------------------------------------------
371 wxPGProperty
* wxPropertyGridPageState::GetLastItem( int flags
)
373 if ( !m_properties
->GetChildCount() )
374 return (wxPGProperty
*) NULL
;
376 wxPG_ITERATOR_CREATE_MASKS(flags
, int itemExMask
, int parentExMask
)
378 // First, get last child of last parent
379 wxPGProperty
* pwc
= (wxPGProperty
*)m_properties
->Last();
380 while ( pwc
->GetChildCount() &&
381 wxPG_ITERATOR_PARENTEXMASK_TEST(pwc
, parentExMask
) )
382 pwc
= (wxPGProperty
*) pwc
->Last();
384 // Then, if it doesn't fit our criteria, back up until we find something that does
385 if ( pwc
->GetFlags() & itemExMask
)
387 wxPropertyGridIterator
it( this, flags
, pwc
);
388 for ( ; !it
.AtEnd(); it
.Prev() )
390 pwc
= (wxPGProperty
*) it
.GetProperty();
396 wxPropertyCategory
* wxPropertyGridPageState::GetPropertyCategory( const wxPGProperty
* p
) const
398 const wxPGProperty
* parent
= (const wxPGProperty
*)p
;
399 const wxPGProperty
* grandparent
= (const wxPGProperty
*)parent
->GetParent();
402 parent
= grandparent
;
403 grandparent
= (wxPGProperty
*)parent
->GetParent();
404 if ( parent
->IsCategory() && grandparent
)
405 return (wxPropertyCategory
*)parent
;
406 } while ( grandparent
);
408 return (wxPropertyCategory
*) NULL
;
411 // -----------------------------------------------------------------------
412 // wxPropertyGridPageState GetPropertyXXX methods
413 // -----------------------------------------------------------------------
415 wxPGProperty
* wxPropertyGridPageState::GetPropertyByLabel( const wxString
& label
,
416 wxPGProperty
* parent
) const
421 if ( !parent
) parent
= (wxPGProperty
*) &m_regularArray
;
423 for ( i
=0; i
<parent
->GetChildCount(); i
++ )
425 wxPGProperty
* p
= parent
->Item(i
);
426 if ( p
->m_label
== label
)
428 // Check children recursively.
429 if ( p
->GetChildCount() )
431 p
= GetPropertyByLabel(label
,(wxPGProperty
*)p
);
440 // -----------------------------------------------------------------------
442 wxPGProperty
* wxPropertyGridPageState::BaseGetPropertyByName( const wxString
& name
) const
444 wxPGHashMapS2P::const_iterator it
;
445 it
= m_dictName
.find(name
);
446 if ( it
!= m_dictName
.end() )
447 return (wxPGProperty
*) it
->second
;
448 return (wxPGProperty
*) NULL
;
451 // -----------------------------------------------------------------------
453 void wxPropertyGridPageState::DoSetPropertyName( wxPGProperty
* p
,
454 const wxString
& newName
)
456 wxCHECK_RET( p
, wxT("invalid property id") );
458 if ( p
->GetBaseName().Len() ) m_dictName
.erase( p
->GetBaseName() );
459 if ( newName
.Len() ) m_dictName
[newName
] = (void*) p
;
461 p
->DoSetName(newName
);
464 // -----------------------------------------------------------------------
465 // wxPropertyGridPageState global operations
466 // -----------------------------------------------------------------------
468 // -----------------------------------------------------------------------
469 // Item iteration macros
470 // NB: Nowadays only needed for alphabetic/categoric mode switching.
471 // -----------------------------------------------------------------------
473 #define II_INVALID_I 0x00FFFFFF
475 #define ITEM_ITERATION_VARIABLES \
476 wxPGProperty* parent; \
480 #define ITEM_ITERATION_INIT_FROM_THE_TOP \
481 parent = m_properties; \
484 #define ITEM_ITERATION_INIT(startparent, startindex, state) \
485 parent = startparent; \
486 i = (unsigned int)startindex; \
487 if ( parent == (wxPGProperty*) NULL ) \
489 parent = state->m_properties; \
493 #define ITEM_ITERATION_LOOP_BEGIN \
496 iMax = parent->GetChildCount(); \
499 wxPGProperty* p = parent->Item(i);
501 #define ITEM_ITERATION_LOOP_END \
502 if ( p->GetChildCount() ) \
505 parent = (wxPGProperty*)p; \
506 iMax = parent->GetChildCount(); \
511 i = parent->m_arrIndex + 1; \
512 parent = parent->m_parent; \
514 while ( parent != NULL );
516 bool wxPropertyGridPageState::EnableCategories( bool enable
)
519 // NB: We can't use wxPropertyGridIterator in this
520 // function, since it depends on m_arrIndexes,
521 // which, among other things, is being fixed here.
523 ITEM_ITERATION_VARIABLES
531 if ( !IsInNonCatMode() )
534 m_properties
= &m_regularArray
;
536 // fix parents, indexes, and depths
537 ITEM_ITERATION_INIT_FROM_THE_TOP
539 ITEM_ITERATION_LOOP_BEGIN
543 p
->m_parent
= parent
;
545 // If parent was category, and this is not,
546 // then the depth stays the same.
547 if ( parent
->IsCategory() &&
549 p
->m_depth
= parent
->m_depth
;
551 p
->m_depth
= parent
->m_depth
+ 1;
553 ITEM_ITERATION_LOOP_END
559 // Disable categories
562 if ( IsInNonCatMode() )
565 // Create array, if necessary.
569 m_properties
= m_abcArray
;
571 // fix parents, indexes, and depths
572 ITEM_ITERATION_INIT_FROM_THE_TOP
574 ITEM_ITERATION_LOOP_BEGIN
578 p
->m_parent
= parent
;
580 p
->m_depth
= parent
->m_depth
+ 1;
582 ITEM_ITERATION_LOOP_END
585 VirtualHeightChanged();
587 if ( m_pPropGrid
->GetState() == this )
588 m_pPropGrid
->RecalculateVirtualSize();
593 // -----------------------------------------------------------------------
598 static bool wxPG_SortFunc(wxPGProperty
*p1
, wxPGProperty
*p2
)
600 return p1
->GetLabel() < p2
->GetLabel();
605 static int wxPG_SortFunc(wxPGProperty
**p1
, wxPGProperty
**p2
)
607 wxPGProperty
*pp1
= *p1
;
608 wxPGProperty
*pp2
= *p2
;
609 return pp1
->GetLabel().compare( pp2
->GetLabel() );
614 void wxPropertyGridPageState::SortChildren( wxPGProperty
* p
)
617 p
= (wxPGProperty
*)m_properties
;
619 if ( !p
->GetChildCount() )
622 wxPGProperty
* pwc
= (wxPGProperty
*)p
;
624 // Can only sort items with children
625 if ( pwc
->GetChildCount() < 1 )
629 std::sort(pwc
->m_children
.begin(), pwc
->m_children
.end(), wxPG_SortFunc
);
631 pwc
->m_children
.Sort( wxPG_SortFunc
);
635 pwc
->FixIndicesOfChildren();
639 // -----------------------------------------------------------------------
641 void wxPropertyGridPageState::Sort()
643 SortChildren( m_properties
);
645 // Sort categories as well
646 if ( !IsInNonCatMode() )
649 for ( i
=0;i
<m_properties
->GetChildCount();i
++)
651 wxPGProperty
* p
= m_properties
->Item(i
);
652 if ( p
->IsCategory() )
658 // -----------------------------------------------------------------------
659 // wxPropertyGridPageState splitter, column and hittest functions
660 // -----------------------------------------------------------------------
662 wxPGProperty
* wxPropertyGridPageState::DoGetItemAtY( int y
) const
666 return (wxPGProperty
*) NULL
;
669 return m_properties
->GetItemAtY(y
, GetGrid()->m_lineHeight
, &a
);
672 // -----------------------------------------------------------------------
674 wxPropertyGridHitTestResult
wxPropertyGridPageState::HitTest( const wxPoint
&pt
) const
676 wxPropertyGridHitTestResult result
;
677 result
.column
= HitTestH( pt
.x
, &result
.splitter
, &result
.splitterHitOffset
);
678 result
.property
= DoGetItemAtY( pt
.y
);
682 // -----------------------------------------------------------------------
684 // Used by SetSplitterLeft() and DotFitColumns()
685 int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC
& dc
,
690 wxPropertyGrid
* pg
= m_pPropGrid
;
695 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
697 wxPGProperty
* p
= pwc
->Item(i
);
698 if ( !p
->IsCategory() )
700 const wxPGCell
* cell
= NULL
;
702 p
->GetDisplayInfo(col
, -1, 0, &text
, &cell
);
703 dc
.GetTextExtent(text
, &w
, &h
);
705 w
+= ( ((int)p
->m_depth
-1) * pg
->m_subgroup_extramargin
);
708 // TODO: Add bitmap support.
710 w
+= (wxPG_XBEFORETEXT
*2);
716 if ( p
->GetChildCount() &&
717 ( subProps
|| p
->IsCategory() ) )
719 w
= GetColumnFitWidth( dc
, p
, col
, subProps
);
729 int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn
) const
731 int n
= GetGrid()->m_marginWidth
;
733 for ( i
=0; i
<=splitterColumn
; i
++ )
738 int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column
) ) const
740 return wxPG_DRAG_MARGIN
;
743 void wxPropertyGridPageState::PropagateColSizeDec( int column
, int decrease
, int dir
)
745 int origWidth
= m_colWidths
[column
];
746 m_colWidths
[column
] -= decrease
;
747 int min
= GetColumnMinWidth(column
);
749 if ( m_colWidths
[column
] < min
)
751 more
= decrease
- (origWidth
- min
);
752 m_colWidths
[column
] = min
;
756 // FIXME: Causes erratic splitter changing, so as a workaround
757 // disabled if two or less columns.
759 if ( m_colWidths
.size() <= 2 )
763 if ( more
&& column
< (int)m_colWidths
.size() && column
>= 0 )
764 PropagateColSizeDec( column
, more
, dir
);
767 void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos
, int splitterColumn
, bool WXUNUSED(allPages
), bool fromAutoCenter
)
769 wxPropertyGrid
* pg
= GetGrid();
771 int adjust
= newXPos
- DoGetSplitterPosition(splitterColumn
);
773 if ( !pg
->HasVirtualWidth() )
779 otherColumn
= splitterColumn
+ 1;
780 if ( otherColumn
== (int)m_colWidths
.size() )
782 m_colWidths
[splitterColumn
] += adjust
;
783 PropagateColSizeDec( otherColumn
, adjust
, 1 );
787 otherColumn
= splitterColumn
+ 1;
788 if ( otherColumn
== (int)m_colWidths
.size() )
790 m_colWidths
[otherColumn
] -= adjust
;
791 PropagateColSizeDec( splitterColumn
, -adjust
, -1 );
796 m_colWidths
[splitterColumn
] += adjust
;
799 if ( splitterColumn
== 0 )
800 m_fSplitterX
= (double) newXPos
;
802 if ( !fromAutoCenter
)
804 // Don't allow initial splitter auto-positioning after this.
805 if ( pg
->GetState() == this )
806 pg
->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
812 // Moves splitter so that all labels are visible, but just.
813 void wxPropertyGridPageState::SetSplitterLeft( bool subProps
)
815 wxPropertyGrid
* pg
= GetGrid();
817 dc
.SetFont(pg
->m_font
);
819 int maxW
= GetColumnFitWidth(dc
, m_properties
, 0, subProps
);
823 maxW
+= pg
->m_marginWidth
;
824 DoSetSplitterPosition( maxW
);
827 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
830 wxSize
wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize
) )
832 wxPropertyGrid
* pg
= GetGrid();
834 dc
.SetFont(pg
->m_font
);
836 int marginWidth
= pg
->m_marginWidth
;
837 int accWid
= marginWidth
;
838 int maxColWidth
= 500;
840 for ( unsigned int col
=0; col
< GetColumnCount(); col
++ )
842 int fitWid
= GetColumnFitWidth(dc
, m_properties
, col
, true);
843 int colMinWidth
= GetColumnMinWidth(col
);
844 if ( fitWid
< colMinWidth
)
845 fitWid
= colMinWidth
;
846 else if ( fitWid
> maxColWidth
)
847 fitWid
= maxColWidth
;
849 m_colWidths
[col
] = fitWid
;
854 // Expand last one to fill the width
855 int remaining
= m_width
- accWid
;
856 m_colWidths
[GetColumnCount()-1] += remaining
;
858 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
860 int firstSplitterX
= marginWidth
+ m_colWidths
[0];
861 m_fSplitterX
= (double) firstSplitterX
;
863 // Don't allow initial splitter auto-positioning after this.
864 if ( pg
->GetState() == this )
866 pg
->SetSplitterPosition(firstSplitterX
, false);
871 pg
->GetVirtualSize(&x
, &y
);
873 return wxSize(accWid
, y
);
876 void wxPropertyGridPageState::CheckColumnWidths( int widthChange
)
881 wxPropertyGrid
* pg
= GetGrid();
884 const bool debug
= false;
888 unsigned int lastColumn
= m_colWidths
.size() - 1;
890 int clientWidth
= pg
->GetClientSize().x
;
893 // Column to reduce, if needed. Take last one that exceeds minimum width.
894 // Except if auto splitter centering is used, in which case use widest.
896 int highestColWidth
= 0;
898 bool minimizedCols
= false;
902 wxLogDebug(wxT("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"), width
, clientWidth
);
907 for ( i
=0; i
<m_colWidths
.size(); i
++ )
909 int min
= GetColumnMinWidth(i
);
910 if ( m_colWidths
[i
] <= min
)
912 m_colWidths
[i
] = min
;
913 minimizedCols
= true;
917 if ( pg
->HasFlag(wxPG_SPLITTER_AUTO_CENTER
) )
919 if ( m_colWidths
[i
] >= highestColWidth
)
921 highestColWidth
= m_colWidths
[i
];
932 int colsWidth
= pg
->m_marginWidth
;
933 for ( i
=0; i
<m_colWidths
.size(); i
++ )
934 colsWidth
+= m_colWidths
[i
];
938 wxLogDebug(wxT(" HasVirtualWidth: %i colsWidth: %i"),(int)pg
->HasVirtualWidth(),colsWidth
);
941 // Then mode-based requirement
942 if ( !pg
->HasVirtualWidth() )
944 int widthHigher
= width
- colsWidth
;
946 // Adapt colsWidth to width
947 if ( colsWidth
< width
)
952 wxLogDebug(wxT(" Adjust last column to %i"), m_colWidths
[lastColumn
] + widthHigher
);
954 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + widthHigher
;
956 else if ( colsWidth
> width
)
959 if ( reduceCol
!= -1 )
963 wxLogDebug(wxT(" Reduce column %i (by %i)"), reduceCol
, -widthHigher
);
965 // Reduce widest column, and recheck
966 m_colWidths
[reduceCol
] = m_colWidths
[reduceCol
] + widthHigher
;
973 // Only check colsWidth against clientWidth
974 if ( colsWidth
< clientWidth
)
976 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + (clientWidth
-colsWidth
);
981 // If width changed, recalculate virtual size
982 if ( pg
->GetState() == this )
983 pg
->RecalculateVirtualSize();
988 for ( i
=0; i
<m_colWidths
.size(); i
++ )
989 wxLogDebug(wxT("col%i: %i"),i
,m_colWidths
[i
]);
992 // Auto center splitter
993 if ( !(pg
->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) &&
994 m_colWidths
.size() == 2 )
996 float centerX
= (float)(pg
->m_width
/2);
999 if ( m_fSplitterX
< 0.0 )
1001 splitterX
= centerX
;
1003 else if ( widthChange
)
1005 //float centerX = float(pg->GetSize().x) * 0.5;
1008 splitterX
= m_fSplitterX
+ (float(widthChange
) * 0.5);
1009 float deviation
= fabs(centerX
- splitterX
);
1011 // If deviating from center, adjust towards it
1012 if ( deviation
> 20.0 )
1014 if ( splitterX
> centerX
)
1022 // No width change, just keep sure we keep splitter position intact
1023 splitterX
= m_fSplitterX
;
1024 float deviation
= fabs(centerX
- splitterX
);
1025 if ( deviation
> 50.0 )
1027 splitterX
= centerX
;
1031 DoSetSplitterPosition((int)splitterX
, 0, false, true);
1033 m_fSplitterX
= splitterX
; // needed to retain accuracy
1037 void wxPropertyGridPageState::SetColumnCount( int colCount
)
1039 wxASSERT( colCount
>= 2 );
1040 m_colWidths
.SetCount( colCount
, wxPG_DRAG_MARGIN
);
1041 if ( m_colWidths
.size() > (unsigned int)colCount
)
1042 m_colWidths
.RemoveAt( m_colWidths
.size(), m_colWidths
.size() - colCount
);
1044 if ( m_pPropGrid
->GetState() == this )
1045 m_pPropGrid
->RecalculateVirtualSize();
1047 CheckColumnWidths();
1050 // Returns column index, -1 for margin
1051 int wxPropertyGridPageState::HitTestH( int x
, int* pSplitterHit
, int* pSplitterHitOffset
) const
1053 int cx
= GetGrid()->m_marginWidth
;
1055 int prevSplitter
= -1;
1060 if ( col
>= (int)m_colWidths
.size() )
1066 cx
+= m_colWidths
[col
];
1069 // Near prev. splitter
1072 int diff
= x
- prevSplitter
;
1073 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1075 *pSplitterHit
= col
- 1;
1076 *pSplitterHitOffset
= diff
;
1081 // Near next splitter
1082 int nextSplitter
= cx
;
1083 if ( col
< (int)(m_colWidths
.size()-1) )
1085 int diff
= x
- nextSplitter
;
1086 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1088 *pSplitterHit
= col
;
1089 *pSplitterHitOffset
= diff
;
1098 // -----------------------------------------------------------------------
1099 // wxPropertyGridPageState property value setting and getting
1100 // -----------------------------------------------------------------------
1102 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty
* p
, const wxString
& value
)
1106 int flags
= wxPG_REPORT_ERROR
|wxPG_FULL_VALUE
|wxPG_PROGRAMMATIC_VALUE
;
1108 wxVariant variant
= p
->GetValueRef();
1111 if ( p
->GetMaxLength() <= 0 )
1112 res
= p
->StringToValue( variant
, value
, flags
);
1114 res
= p
->StringToValue( variant
, value
.Mid(0,p
->GetMaxLength()), flags
);
1118 p
->SetValue(variant
);
1119 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1120 p
->UpdateControl(m_pPropGrid
->GetEditorControl());
1128 // -----------------------------------------------------------------------
1130 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty
* p
, wxVariant
& value
)
1135 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1136 p
->UpdateControl(m_pPropGrid
->GetEditorControl());
1143 // -----------------------------------------------------------------------
1145 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty
* p
, wxObject
* value
)
1149 // wnd_primary has to be given so the control can be updated as well.
1151 DoSetPropertyValue(p
, v
);
1157 // -----------------------------------------------------------------------
1159 void wxPropertyGridPageState::DoSetPropertyValueUnspecified( wxPGProperty
* p
)
1161 wxCHECK_RET( p
, wxT("invalid property id") );
1163 if ( !p
->IsValueUnspecified() )
1165 // Value should be set first - editor class methods may need it
1166 p
->m_value
.MakeNull();
1168 wxASSERT( m_pPropGrid
);
1170 if ( m_pPropGrid
->GetState() == this )
1172 if ( m_pPropGrid
->m_selected
== p
&& m_pPropGrid
->m_wndEditor
)
1174 p
->GetEditorClass()->SetValueToUnspecified(p
, m_pPropGrid
->GetEditorControl());
1179 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1180 DoSetPropertyValueUnspecified( p
->Item(i
) );
1184 // -----------------------------------------------------------------------
1185 // wxPropertyGridPageState property operations
1186 // -----------------------------------------------------------------------
1188 bool wxPropertyGridPageState::DoCollapse( wxPGProperty
* p
)
1190 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1192 if ( !p
->GetChildCount() ) return false;
1194 if ( !p
->IsExpanded() ) return false;
1196 p
->SetExpanded(false);
1198 VirtualHeightChanged();
1203 // -----------------------------------------------------------------------
1205 bool wxPropertyGridPageState::DoExpand( wxPGProperty
* p
)
1207 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1209 if ( !p
->GetChildCount() ) return false;
1211 if ( p
->IsExpanded() ) return false;
1213 p
->SetExpanded(true);
1215 VirtualHeightChanged();
1220 // -----------------------------------------------------------------------
1222 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty
* p
, unsigned int flags
)
1224 if ( this == m_pPropGrid
->GetState() )
1225 return m_pPropGrid
->DoSelectProperty( p
, flags
);
1231 // -----------------------------------------------------------------------
1233 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty
* p
, bool hide
, int flags
)
1236 p
->ClearFlag( wxPG_PROP_HIDDEN
);
1238 p
->SetFlag( wxPG_PROP_HIDDEN
);
1240 if ( flags
& wxPG_RECURSE
)
1243 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1244 DoHideProperty(p
->Item(i
), hide
, flags
| wxPG_RECURSE_STARTS
);
1247 VirtualHeightChanged();
1252 // -----------------------------------------------------------------------
1254 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty
* p
, bool enable
)
1260 if ( !(p
->m_flags
& wxPG_PROP_DISABLED
) )
1265 p
->m_flags
&= ~(wxPG_PROP_DISABLED
);
1269 if ( p
->m_flags
& wxPG_PROP_DISABLED
)
1274 p
->m_flags
|= wxPG_PROP_DISABLED
;
1278 // Apply same to sub-properties as well
1280 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1281 DoEnableProperty( p
->Item(i
), enable
);
1288 // -----------------------------------------------------------------------
1289 // wxPropertyGridPageState wxVariant related routines
1290 // -----------------------------------------------------------------------
1292 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1293 // Never includes sub-properties (unless they are parented by wxParentProperty).
1294 wxVariant
wxPropertyGridPageState::DoGetPropertyValues( const wxString
& listname
,
1295 wxPGProperty
* baseparent
,
1298 wxPGProperty
* pwc
= (wxPGProperty
*) baseparent
;
1300 // Root is the default base-parent.
1304 wxVariantList tempList
;
1305 wxVariant
v( tempList
, listname
);
1307 if ( pwc
->GetChildCount() )
1309 if ( flags
& wxPG_KEEP_STRUCTURE
)
1311 wxASSERT( !pwc
->HasFlag(wxPG_PROP_AGGREGATE
) );
1314 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1316 wxPGProperty
* p
= pwc
->Item(i
);
1317 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1319 wxVariant variant
= p
->GetValue();
1320 variant
.SetName( p
->GetBaseName() );
1321 v
.Append( variant
);
1325 v
.Append( DoGetPropertyValues(p
->m_name
,p
,flags
|wxPG_KEEP_STRUCTURE
) );
1327 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1328 v
.Append( p
->GetAttributesAsList() );
1333 wxPropertyGridConstIterator
it( this, wxPG_ITERATE_DEFAULT
, pwc
->Item(0) );
1334 it
.SetBaseParent( pwc
);
1336 for ( ; !it
.AtEnd(); it
.Next() )
1338 const wxPGProperty
* p
= it
.GetProperty();
1340 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1341 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1343 wxVariant variant
= p
->GetValue();
1344 variant
.SetName( p
->GetName() );
1345 v
.Append( variant
);
1346 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1347 v
.Append( p
->GetAttributesAsList() );
1356 // -----------------------------------------------------------------------
1358 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList
& list
, wxPGProperty
* defaultCategory
)
1360 unsigned char origFrozen
= 1;
1362 if ( m_pPropGrid
->GetState() == this )
1364 origFrozen
= m_pPropGrid
->m_frozen
;
1365 if ( !origFrozen
) m_pPropGrid
->Freeze();
1368 wxPropertyCategory
* use_category
= (wxPropertyCategory
*)defaultCategory
;
1370 if ( !use_category
)
1371 use_category
= (wxPropertyCategory
*)m_properties
;
1373 // Let's iterate over the list of variants.
1374 wxVariantList::const_iterator node
;
1375 int numSpecialEntries
= 0;
1378 // Second pass for special entries
1379 for ( node
= list
.begin(); node
!= list
.end(); node
++ )
1381 wxVariant
*current
= (wxVariant
*)*node
;
1383 // Make sure it is wxVariant.
1384 wxASSERT( current
);
1385 wxASSERT( wxStrcmp(current
->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1387 const wxString
& name
= current
->GetName();
1388 if ( name
.length() > 0 )
1391 // '@' signified a special entry
1392 if ( name
[0] == wxS('@') )
1394 numSpecialEntries
++;
1398 wxPGProperty
* foundProp
= BaseGetPropertyByName(name
);
1401 wxPGProperty
* p
= foundProp
;
1403 // If it was a list, we still have to go through it.
1404 if ( wxStrcmp(current
->GetType(), wxS("list")) == 0 )
1406 DoSetPropertyValues( current
->GetList(),
1407 p
->IsCategory()?p
:((wxPGProperty
*)NULL
)
1413 if ( wxStrcmp(current
->GetType(), p
->GetValue().GetType()) != 0)
1415 wxLogDebug(wxT("wxPropertyGridPageState::DoSetPropertyValues Warning: Setting value of property \"%s\" from variant"),
1416 p
->GetName().c_str());
1420 p
->SetValue(*current
);
1426 if ( current
->GetType() != wxS("list") )
1432 // Yes, it is; create a sub category and append contents there.
1433 wxPGProperty
* newCat
= DoInsert(use_category
,-1,new wxPropertyCategory(current
->GetName(),wxPG_LABEL
));
1434 DoSetPropertyValues( current
->GetList(), newCat
);
1441 if ( numSpecialEntries
)
1443 for ( node
= list
.begin(); node
!= list
.end(); node
++ )
1445 wxVariant
*current
= (wxVariant
*)*node
;
1447 const wxString
& name
= current
->GetName();
1448 if ( name
.length() > 0 )
1451 // '@' signified a special entry
1452 if ( name
[0] == wxS('@') )
1454 numSpecialEntries
--;
1456 size_t pos2
= name
.rfind(wxS('@'));
1457 if ( pos2
> 0 && pos2
< (name
.size()-1) )
1459 wxString propName
= name
.substr(1, pos2
-1);
1460 wxString entryType
= name
.substr(pos2
+1, wxString::npos
);
1462 if ( entryType
== wxS("attr") )
1465 // List of attributes
1466 wxPGProperty
* foundProp
= BaseGetPropertyByName(propName
);
1469 wxASSERT( current
->GetType() == wxPG_VARIANT_TYPE_LIST
);
1471 wxVariantList
& list2
= current
->GetList();
1472 wxVariantList::const_iterator node2
;
1474 for ( node2
= list2
.begin(); node2
!= list2
.end(); node2
++ )
1476 wxVariant
*attr
= (wxVariant
*)*node2
;
1477 foundProp
->SetAttribute( attr
->GetName(), *attr
);
1482 // ERROR: No such property: 'propName'
1488 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1493 if ( !numSpecialEntries
)
1500 m_pPropGrid
->Thaw();
1502 if ( this == m_pPropGrid
->GetState() )
1504 m_selected
->UpdateControl(m_pPropGrid
->GetEditorControl());
1510 // -----------------------------------------------------------------------
1511 // wxPropertyGridPageState property adding and removal
1512 // -----------------------------------------------------------------------
1514 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty
* property
,
1515 wxPGProperty
* scheduledParent
)
1517 wxPropertyGrid
* propGrid
= m_pPropGrid
;
1519 // This will allow better behavior.
1520 if ( scheduledParent
== m_properties
)
1521 scheduledParent
= (wxPGProperty
*) NULL
;
1523 if ( scheduledParent
&& !scheduledParent
->IsCategory() )
1525 wxASSERT_MSG( property
->GetBaseName().length(),
1526 "Property's children must have unique, non-empty names within their scope" );
1529 property
->m_parentState
= this;
1531 if ( property
->IsCategory() )
1534 // Parent of a category must be either root or another category
1535 // (otherwise Bad Things might happen).
1536 wxASSERT_MSG( scheduledParent
== NULL
||
1537 scheduledParent
== m_properties
||
1538 scheduledParent
->IsCategory(),
1539 wxT("Parent of a category must be either root or another category."));
1541 // If we already have category with same name, delete given property
1542 // and use it instead as most recent caption item.
1543 wxPGProperty
* found_id
= BaseGetPropertyByName( property
->GetBaseName() );
1546 wxPropertyCategory
* pwc
= (wxPropertyCategory
*) found_id
;
1547 if ( pwc
->IsCategory() ) // Must be a category.
1550 m_currentCategory
= pwc
;
1557 // Warn for identical names in debug mode.
1558 if ( BaseGetPropertyByName(property
->GetName()) &&
1559 (!scheduledParent
|| scheduledParent
->IsCategory()) )
1561 wxLogError(wxT("wxPropertyGrid: Warning - item with name \"%s\" already exists."),
1562 property
->GetName().c_str());
1563 wxPGGlobalVars
->m_warnings
++;
1567 // Make sure nothing is selected.
1568 if ( propGrid
&& propGrid
->m_selected
)
1570 bool selRes
= propGrid
->ClearSelection();
1571 wxPG_CHECK_MSG_DBG( selRes
,
1573 wxT("failed to deselect a property (editor probably had invalid value)") );
1576 // NULL parent == root parent
1577 if ( !scheduledParent
)
1578 scheduledParent
= DoGetRoot();
1580 property
->m_parent
= scheduledParent
;
1582 property
->InitAfterAdded(this, propGrid
);
1584 if ( property
->IsCategory() )
1586 wxPropertyCategory
* pc
= wxStaticCast(property
, wxPropertyCategory
);
1588 m_currentCategory
= pc
;
1590 // Calculate text extent for category caption
1592 pc
->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
1598 // -----------------------------------------------------------------------
1600 wxPGProperty
* wxPropertyGridPageState::DoAppend( wxPGProperty
* property
)
1602 wxPropertyCategory
* cur_cat
= m_currentCategory
;
1603 if ( property
->IsCategory() )
1604 cur_cat
= (wxPropertyCategory
*) NULL
;
1606 return DoInsert( cur_cat
, -1, property
);
1609 // -----------------------------------------------------------------------
1611 wxPGProperty
* wxPropertyGridPageState::DoInsert( wxPGProperty
* parent
, int index
, wxPGProperty
* property
)
1614 parent
= m_properties
;
1616 wxCHECK_MSG( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1618 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1620 bool res
= PrepareToAddItem( property
, (wxPropertyCategory
*)parent
);
1622 // PrepareToAddItem() may just decide to use use current category
1623 // instead of adding new one.
1625 return m_currentCategory
;
1627 // Note that item must be added into current mode later.
1629 // If parent is wxParentProperty, just stick it in...
1630 // If parent is root (m_properties), then...
1631 // In categoric mode: Add as last item in m_abcArray (if not category).
1632 // Add to given index in m_regularArray.
1633 // In non-cat mode: Add as last item in m_regularArray.
1634 // Add to given index in m_abcArray.
1635 // If parent is category, then...
1636 // 1) Add to given category in given index.
1637 // 2) Add as last item in m_abcArray.
1639 if ( !parent
->IsCategory() && !parent
->IsRoot() )
1641 // Parent is wxParentingProperty: Just stick it in...
1642 parent
->AddChild2( property
, index
);
1646 // Parent is Category or Root.
1648 if ( m_properties
== &m_regularArray
)
1652 // Only add non-categories to m_abcArray.
1653 if ( m_abcArray
&& !property
->IsCategory() )
1654 m_abcArray
->AddChild2( property
, -1, false );
1656 // Add to current mode.
1657 parent
->AddChild2( property
, index
);
1662 // Non-categorized mode.
1664 if ( parent
!= m_properties
)
1665 // Parent is category.
1666 parent
->AddChild2( property
, index
, false );
1669 m_regularArray
.AddChild2( property
, -1, false );
1671 // Add to current mode (no categories).
1672 if ( !property
->IsCategory() )
1673 m_abcArray
->AddChild2( property
, index
);
1678 if ( property
->IsCategory() )
1680 // This is a category caption item.
1682 // Last caption is not the bottom one (this info required by append)
1683 m_lastCaptionBottomnest
= 0;
1686 // Only add name to hashmap if parent is root or category
1687 if ( (parent
->IsCategory() || parent
->IsRoot()) && property
->m_name
.length() )
1688 m_dictName
[property
->m_name
] = (void*) property
;
1690 VirtualHeightChanged();
1692 property
->UpdateParentValues();
1699 // -----------------------------------------------------------------------
1701 void wxPropertyGridPageState::DoDelete( wxPGProperty
* item
)
1703 wxCHECK_RET( item
->GetParent(),
1704 wxT("this property was already deleted") );
1706 wxCHECK_RET( item
!= &m_regularArray
&& item
!= m_abcArray
,
1707 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1710 unsigned int indinparent
= item
->GetIndexInParent();
1712 wxPGProperty
* pwc
= (wxPGProperty
*)item
;
1714 wxCHECK_RET( !item
->GetParent()->HasFlag(wxPG_PROP_AGGREGATE
),
1715 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1717 if ( item
->IsCategory() )
1719 // deleting a category
1721 // erase category entries from the hash table
1722 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1724 wxPGProperty
* sp
= pwc
->Item( i
);
1725 if ( sp
->GetBaseName().Len() ) m_dictName
.erase(sp
->GetBaseName());
1728 if ( pwc
== m_currentCategory
)
1729 m_currentCategory
= (wxPropertyCategory
*) NULL
;
1733 // Remove children from non-categorized array.
1734 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1736 wxPGProperty
* p
= pwc
->Item( i
);
1737 wxASSERT( p
!= NULL
);
1738 if ( !p
->IsCategory() )
1739 m_abcArray
->RemoveChild(p
);
1742 if ( IsInNonCatMode() )
1743 m_abcArray
->FixIndicesOfChildren();
1747 if ( !IsInNonCatMode() )
1749 // categorized mode - non-categorized array
1751 // Remove from non-cat array, but only if parent is in it
1752 if ( !item
->IsCategory() && item
->GetParent()->IsCategory() )
1755 m_abcArray
->RemoveChild(item
);
1758 // categorized mode - categorized array
1759 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1760 parentsChildren
.erase( parentsChildren
.begin() + indinparent
);
1761 item
->m_parent
->FixIndicesOfChildren();
1765 // non-categorized mode - categorized array
1767 // We need to find location of item.
1768 wxPGProperty
* cat_parent
= &m_regularArray
;
1769 int cat_index
= m_regularArray
.GetChildCount();
1771 for ( i
= 0; i
< m_regularArray
.GetChildCount(); i
++ )
1773 wxPGProperty
* p
= m_regularArray
.Item(i
);
1774 if ( p
== item
) { cat_index
= i
; break; }
1775 if ( p
->IsCategory() )
1777 int subind
= ((wxPGProperty
*)p
)->Index(item
);
1778 if ( subind
!= wxNOT_FOUND
)
1780 cat_parent
= ((wxPGProperty
*)p
);
1786 cat_parent
->m_children
.erase(cat_parent
->m_children
.begin()+cat_index
);
1788 // non-categorized mode - non-categorized array
1789 if ( !item
->IsCategory() )
1791 wxASSERT( item
->m_parent
== m_abcArray
);
1792 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1793 parentsChildren
.erase(parentsChildren
.begin() + indinparent
);
1794 item
->m_parent
->FixIndicesOfChildren(indinparent
);
1798 if ( item
->GetBaseName().Len() ) m_dictName
.erase(item
->GetBaseName());
1800 // We can actually delete it now
1803 m_itemsAdded
= 1; // Not a logical assignment (but required nonetheless).
1805 VirtualHeightChanged();
1808 // -----------------------------------------------------------------------
1810 #endif // wxUSE_PROPGRID