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()
204 m_pPropGrid
= (wxPropertyGrid
*) NULL
;
205 m_regularArray
.SetParentState(this);
206 m_properties
= &m_regularArray
;
207 m_abcArray
= (wxPGRootProperty
*) NULL
;
208 m_currentCategory
= (wxPropertyCategory
*) NULL
;
209 m_selected
= (wxPGProperty
*) 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();
235 m_abcArray
->SetParentState(this);
236 m_abcArray
->SetFlag(wxPG_PROP_CHILDREN_ARE_COPIES
);
239 // Must be called when state::m_properties still points to regularArray.
240 wxPGProperty
* oldProperties
= m_properties
;
242 // Must use temp value in state::m_properties for item iteration loop
243 // to run as expected.
244 m_properties
= &m_regularArray
;
246 if ( m_properties
->GetChildCount() )
249 // Prepare m_abcArray
250 wxPropertyGridIterator
it( this, wxPG_ITERATE_PROPERTIES
);
252 for ( ; !it
.AtEnd(); it
.Next() )
254 wxPGProperty
* p
= it
.GetProperty();
255 wxPGProperty
* parent
= p
->GetParent();
256 if ( parent
->IsCategory() || parent
->IsRoot() )
258 m_abcArray
->AddChild2(p
);
259 p
->m_parent
= &m_regularArray
;
264 m_properties
= oldProperties
;
267 // -----------------------------------------------------------------------
269 void wxPropertyGridPageState::DoClear()
271 m_regularArray
.Empty();
277 m_currentCategory
= (wxPropertyCategory
*) NULL
;
278 m_lastCaptionBottomnest
= 1;
284 m_selected
= (wxPGProperty
*) NULL
;
287 // -----------------------------------------------------------------------
289 void wxPropertyGridPageState::CalculateFontAndBitmapStuff( int WXUNUSED(vspacing
) )
291 wxPropertyGrid
* propGrid
= GetGrid();
293 VirtualHeightChanged();
295 // Recalculate caption text extents.
298 for ( i
=0;i
<m_regularArray
.GetChildCount();i
++ )
300 wxPGProperty
* p
=m_regularArray
.Item(i
);
302 if ( p
->IsCategory() )
303 ((wxPropertyCategory
*)p
)->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
307 // -----------------------------------------------------------------------
309 void wxPropertyGridPageState::SetVirtualWidth( int width
)
311 wxASSERT( width
>= 0 );
312 wxPropertyGrid
* pg
= GetGrid();
313 int gw
= pg
->GetClientSize().x
;
320 // -----------------------------------------------------------------------
322 void wxPropertyGridPageState::OnClientWidthChange( int newWidth
, int widthChange
, bool fromOnResize
)
324 wxPropertyGrid
* pg
= GetGrid();
326 if ( pg
->HasVirtualWidth() )
328 if ( m_width
< newWidth
)
329 SetVirtualWidth( newWidth
);
331 CheckColumnWidths(widthChange
);
335 SetVirtualWidth( newWidth
);
337 // This should be done before splitter auto centering
338 // NOTE: Splitter auto-centering is done in this function.
341 CheckColumnWidths(widthChange
);
343 if ( !(GetGrid()->GetInternalFlags() & wxPG_FL_SPLITTER_PRE_SET
) &&
344 (GetGrid()->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) )
346 long timeSinceCreation
= (::wxGetLocalTimeMillis() - GetGrid()->m_timeCreated
).ToLong();
348 // If too long, don't set splitter
349 if ( timeSinceCreation
< 3000 )
351 if ( m_properties
->GetChildCount() || timeSinceCreation
> 750 )
353 SetSplitterLeft( false );
357 DoSetSplitterPosition( newWidth
/ 2 );
358 GetGrid()->ClearInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
365 // -----------------------------------------------------------------------
366 // wxPropertyGridPageState item iteration methods
367 // -----------------------------------------------------------------------
369 wxPGProperty
* wxPropertyGridPageState::GetLastItem( int flags
)
371 if ( !m_properties
->GetChildCount() )
372 return (wxPGProperty
*) NULL
;
374 wxPG_ITERATOR_CREATE_MASKS(flags
, int itemExMask
, int parentExMask
)
376 // First, get last child of last parent
377 wxPGProperty
* pwc
= (wxPGProperty
*)m_properties
->Last();
378 while ( pwc
->GetChildCount() &&
379 wxPG_ITERATOR_PARENTEXMASK_TEST(pwc
, parentExMask
) )
380 pwc
= (wxPGProperty
*) pwc
->Last();
382 // Then, if it doesn't fit our criteria, back up until we find something that does
383 if ( pwc
->GetFlags() & itemExMask
)
385 wxPropertyGridIterator
it( this, flags
, pwc
);
386 for ( ; !it
.AtEnd(); it
.Prev() )
388 pwc
= (wxPGProperty
*) it
.GetProperty();
394 wxPropertyCategory
* wxPropertyGridPageState::GetPropertyCategory( const wxPGProperty
* p
) const
396 const wxPGProperty
* parent
= (const wxPGProperty
*)p
;
397 const wxPGProperty
* grandparent
= (const wxPGProperty
*)parent
->GetParent();
400 parent
= grandparent
;
401 grandparent
= (wxPGProperty
*)parent
->GetParent();
402 if ( parent
->IsCategory() && grandparent
)
403 return (wxPropertyCategory
*)parent
;
404 } while ( grandparent
);
406 return (wxPropertyCategory
*) NULL
;
409 // -----------------------------------------------------------------------
410 // wxPropertyGridPageState GetPropertyXXX methods
411 // -----------------------------------------------------------------------
413 wxPGProperty
* wxPropertyGridPageState::GetPropertyByLabel( const wxString
& label
,
414 wxPGProperty
* parent
) const
419 if ( !parent
) parent
= (wxPGProperty
*) &m_regularArray
;
421 for ( i
=0; i
<parent
->GetChildCount(); i
++ )
423 wxPGProperty
* p
= parent
->Item(i
);
424 if ( p
->m_label
== label
)
426 // Check children recursively.
427 if ( p
->GetChildCount() )
429 p
= GetPropertyByLabel(label
,(wxPGProperty
*)p
);
438 // -----------------------------------------------------------------------
440 wxPGProperty
* wxPropertyGridPageState::BaseGetPropertyByName( const wxString
& name
) const
442 wxPGHashMapS2P::const_iterator it
;
443 it
= m_dictName
.find(name
);
444 if ( it
!= m_dictName
.end() )
445 return (wxPGProperty
*) it
->second
;
446 return (wxPGProperty
*) NULL
;
449 // -----------------------------------------------------------------------
451 void wxPropertyGridPageState::DoSetPropertyName( wxPGProperty
* p
,
452 const wxString
& newName
)
454 wxCHECK_RET( p
, wxT("invalid property id") );
456 wxPGProperty
* parent
= p
->GetParent();
458 if ( parent
->IsCategory() || parent
->IsRoot() )
460 if ( p
->GetBaseName().length() )
461 m_dictName
.erase( p
->GetBaseName() );
462 if ( newName
.length() )
463 m_dictName
[newName
] = (void*) p
;
466 p
->DoSetName(newName
);
469 // -----------------------------------------------------------------------
470 // wxPropertyGridPageState global operations
471 // -----------------------------------------------------------------------
473 // -----------------------------------------------------------------------
474 // Item iteration macros
475 // NB: Nowadays only needed for alphabetic/categoric mode switching.
476 // -----------------------------------------------------------------------
478 //#define II_INVALID_I 0x00FFFFFF
480 #define ITEM_ITERATION_VARIABLES \
481 wxPGProperty* parent; \
485 #define ITEM_ITERATION_INIT_FROM_THE_TOP \
486 parent = m_properties; \
490 #define ITEM_ITERATION_INIT(startparent, startindex, state) \
491 parent = startparent; \
492 i = (unsigned int)startindex; \
493 if ( parent == (wxPGProperty*) NULL ) \
495 parent = state->m_properties; \
500 #define ITEM_ITERATION_LOOP_BEGIN \
503 iMax = parent->GetChildCount(); \
506 wxPGProperty* p = parent->Item(i);
508 #define ITEM_ITERATION_LOOP_END \
509 if ( p->GetChildCount() ) \
512 parent = (wxPGProperty*)p; \
513 iMax = parent->GetChildCount(); \
518 i = parent->m_arrIndex + 1; \
519 parent = parent->m_parent; \
521 while ( parent != NULL );
523 bool wxPropertyGridPageState::EnableCategories( bool enable
)
526 // NB: We can't use wxPropertyGridIterator in this
527 // function, since it depends on m_arrIndexes,
528 // which, among other things, is being fixed here.
530 ITEM_ITERATION_VARIABLES
538 if ( !IsInNonCatMode() )
541 m_properties
= &m_regularArray
;
543 // fix parents, indexes, and depths
544 ITEM_ITERATION_INIT_FROM_THE_TOP
546 ITEM_ITERATION_LOOP_BEGIN
550 p
->m_parent
= parent
;
552 // If parent was category, and this is not,
553 // then the depth stays the same.
554 if ( parent
->IsCategory() &&
556 p
->m_depth
= parent
->m_depth
;
558 p
->m_depth
= parent
->m_depth
+ 1;
560 ITEM_ITERATION_LOOP_END
566 // Disable categories
569 if ( IsInNonCatMode() )
572 // Create array, if necessary.
576 m_properties
= m_abcArray
;
578 // fix parents, indexes, and depths
579 ITEM_ITERATION_INIT_FROM_THE_TOP
581 ITEM_ITERATION_LOOP_BEGIN
585 p
->m_parent
= parent
;
587 p
->m_depth
= parent
->m_depth
+ 1;
589 ITEM_ITERATION_LOOP_END
592 VirtualHeightChanged();
594 if ( m_pPropGrid
->GetState() == this )
595 m_pPropGrid
->RecalculateVirtualSize();
600 // -----------------------------------------------------------------------
605 static bool wxPG_SortFunc(wxPGProperty
*p1
, wxPGProperty
*p2
)
607 return p1
->GetLabel() < p2
->GetLabel();
612 static int wxPG_SortFunc(wxPGProperty
**p1
, wxPGProperty
**p2
)
614 wxPGProperty
*pp1
= *p1
;
615 wxPGProperty
*pp2
= *p2
;
616 return pp1
->GetLabel().compare( pp2
->GetLabel() );
621 void wxPropertyGridPageState::SortChildren( wxPGProperty
* p
)
624 p
= (wxPGProperty
*)m_properties
;
626 if ( !p
->GetChildCount() )
629 wxPGProperty
* pwc
= (wxPGProperty
*)p
;
631 // Can only sort items with children
632 if ( pwc
->GetChildCount() < 1 )
636 std::sort(pwc
->m_children
.begin(), pwc
->m_children
.end(), wxPG_SortFunc
);
638 pwc
->m_children
.Sort( wxPG_SortFunc
);
642 pwc
->FixIndicesOfChildren();
646 // -----------------------------------------------------------------------
648 void wxPropertyGridPageState::Sort()
650 SortChildren( m_properties
);
652 // Sort categories as well
653 if ( !IsInNonCatMode() )
656 for ( i
=0;i
<m_properties
->GetChildCount();i
++)
658 wxPGProperty
* p
= m_properties
->Item(i
);
659 if ( p
->IsCategory() )
665 // -----------------------------------------------------------------------
666 // wxPropertyGridPageState splitter, column and hittest functions
667 // -----------------------------------------------------------------------
669 wxPGProperty
* wxPropertyGridPageState::DoGetItemAtY( int y
) const
673 return (wxPGProperty
*) NULL
;
676 return m_properties
->GetItemAtY(y
, GetGrid()->m_lineHeight
, &a
);
679 // -----------------------------------------------------------------------
681 wxPropertyGridHitTestResult
wxPropertyGridPageState::HitTest( const wxPoint
&pt
) const
683 wxPropertyGridHitTestResult result
;
684 result
.column
= HitTestH( pt
.x
, &result
.splitter
, &result
.splitterHitOffset
);
685 result
.property
= DoGetItemAtY( pt
.y
);
689 // -----------------------------------------------------------------------
691 // Used by SetSplitterLeft() and DotFitColumns()
692 int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC
& dc
,
697 wxPropertyGrid
* pg
= m_pPropGrid
;
702 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
704 wxPGProperty
* p
= pwc
->Item(i
);
705 if ( !p
->IsCategory() )
707 const wxPGCell
* cell
= NULL
;
709 p
->GetDisplayInfo(col
, -1, 0, &text
, &cell
);
710 dc
.GetTextExtent(text
, &w
, &h
);
712 w
+= ( ((int)p
->m_depth
-1) * pg
->m_subgroup_extramargin
);
715 // TODO: Add bitmap support.
717 w
+= (wxPG_XBEFORETEXT
*2);
723 if ( p
->GetChildCount() &&
724 ( subProps
|| p
->IsCategory() ) )
726 w
= GetColumnFitWidth( dc
, p
, col
, subProps
);
736 int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn
) const
738 int n
= GetGrid()->m_marginWidth
;
740 for ( i
=0; i
<=splitterColumn
; i
++ )
745 int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column
) ) const
747 return wxPG_DRAG_MARGIN
;
750 void wxPropertyGridPageState::PropagateColSizeDec( int column
, int decrease
, int dir
)
752 int origWidth
= m_colWidths
[column
];
753 m_colWidths
[column
] -= decrease
;
754 int min
= GetColumnMinWidth(column
);
756 if ( m_colWidths
[column
] < min
)
758 more
= decrease
- (origWidth
- min
);
759 m_colWidths
[column
] = min
;
763 // FIXME: Causes erratic splitter changing, so as a workaround
764 // disabled if two or less columns.
766 if ( m_colWidths
.size() <= 2 )
770 if ( more
&& column
< (int)m_colWidths
.size() && column
>= 0 )
771 PropagateColSizeDec( column
, more
, dir
);
774 void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos
, int splitterColumn
, bool WXUNUSED(allPages
), bool fromAutoCenter
)
776 wxPropertyGrid
* pg
= GetGrid();
778 int adjust
= newXPos
- DoGetSplitterPosition(splitterColumn
);
780 if ( !pg
->HasVirtualWidth() )
786 otherColumn
= splitterColumn
+ 1;
787 if ( otherColumn
== (int)m_colWidths
.size() )
789 m_colWidths
[splitterColumn
] += adjust
;
790 PropagateColSizeDec( otherColumn
, adjust
, 1 );
794 otherColumn
= splitterColumn
+ 1;
795 if ( otherColumn
== (int)m_colWidths
.size() )
797 m_colWidths
[otherColumn
] -= adjust
;
798 PropagateColSizeDec( splitterColumn
, -adjust
, -1 );
803 m_colWidths
[splitterColumn
] += adjust
;
806 if ( splitterColumn
== 0 )
807 m_fSplitterX
= (double) newXPos
;
809 if ( !fromAutoCenter
)
811 // Don't allow initial splitter auto-positioning after this.
812 if ( pg
->GetState() == this )
813 pg
->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET
);
819 // Moves splitter so that all labels are visible, but just.
820 void wxPropertyGridPageState::SetSplitterLeft( bool subProps
)
822 wxPropertyGrid
* pg
= GetGrid();
824 dc
.SetFont(pg
->m_font
);
826 int maxW
= GetColumnFitWidth(dc
, m_properties
, 0, subProps
);
830 maxW
+= pg
->m_marginWidth
;
831 DoSetSplitterPosition( maxW
);
834 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
837 wxSize
wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize
) )
839 wxPropertyGrid
* pg
= GetGrid();
841 dc
.SetFont(pg
->m_font
);
843 int marginWidth
= pg
->m_marginWidth
;
844 int accWid
= marginWidth
;
845 int maxColWidth
= 500;
847 for ( unsigned int col
=0; col
< GetColumnCount(); col
++ )
849 int fitWid
= GetColumnFitWidth(dc
, m_properties
, col
, true);
850 int colMinWidth
= GetColumnMinWidth(col
);
851 if ( fitWid
< colMinWidth
)
852 fitWid
= colMinWidth
;
853 else if ( fitWid
> maxColWidth
)
854 fitWid
= maxColWidth
;
856 m_colWidths
[col
] = fitWid
;
861 // Expand last one to fill the width
862 int remaining
= m_width
- accWid
;
863 m_colWidths
[GetColumnCount()-1] += remaining
;
865 pg
->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER
);
867 int firstSplitterX
= marginWidth
+ m_colWidths
[0];
868 m_fSplitterX
= (double) firstSplitterX
;
870 // Don't allow initial splitter auto-positioning after this.
871 if ( pg
->GetState() == this )
873 pg
->SetSplitterPosition(firstSplitterX
, false);
878 pg
->GetVirtualSize(&x
, &y
);
880 return wxSize(accWid
, y
);
883 void wxPropertyGridPageState::CheckColumnWidths( int widthChange
)
888 wxPropertyGrid
* pg
= GetGrid();
891 const bool debug
= false;
895 unsigned int lastColumn
= m_colWidths
.size() - 1;
897 int clientWidth
= pg
->GetClientSize().x
;
900 // Column to reduce, if needed. Take last one that exceeds minimum width.
901 // Except if auto splitter centering is used, in which case use widest.
903 int highestColWidth
= 0;
907 wxLogDebug(wxT("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"), width
, clientWidth
);
912 for ( i
=0; i
<m_colWidths
.size(); i
++ )
914 int min
= GetColumnMinWidth(i
);
915 if ( m_colWidths
[i
] <= min
)
917 m_colWidths
[i
] = min
;
921 if ( pg
->HasFlag(wxPG_SPLITTER_AUTO_CENTER
) )
923 if ( m_colWidths
[i
] >= highestColWidth
)
925 highestColWidth
= m_colWidths
[i
];
936 int colsWidth
= pg
->m_marginWidth
;
937 for ( i
=0; i
<m_colWidths
.size(); i
++ )
938 colsWidth
+= m_colWidths
[i
];
942 wxLogDebug(wxT(" HasVirtualWidth: %i colsWidth: %i"),(int)pg
->HasVirtualWidth(),colsWidth
);
945 // Then mode-based requirement
946 if ( !pg
->HasVirtualWidth() )
948 int widthHigher
= width
- colsWidth
;
950 // Adapt colsWidth to width
951 if ( colsWidth
< width
)
956 wxLogDebug(wxT(" Adjust last column to %i"), m_colWidths
[lastColumn
] + widthHigher
);
958 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + widthHigher
;
960 else if ( colsWidth
> width
)
963 if ( reduceCol
!= -1 )
967 wxLogDebug(wxT(" Reduce column %i (by %i)"), reduceCol
, -widthHigher
);
969 // Reduce widest column, and recheck
970 m_colWidths
[reduceCol
] = m_colWidths
[reduceCol
] + widthHigher
;
977 // Only check colsWidth against clientWidth
978 if ( colsWidth
< clientWidth
)
980 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + (clientWidth
-colsWidth
);
985 // If width changed, recalculate virtual size
986 if ( pg
->GetState() == this )
987 pg
->RecalculateVirtualSize();
992 for ( i
=0; i
<m_colWidths
.size(); i
++ )
993 wxLogDebug(wxT("col%i: %i"),i
,m_colWidths
[i
]);
996 // Auto center splitter
997 if ( !(pg
->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) &&
998 m_colWidths
.size() == 2 )
1000 float centerX
= (float)(pg
->m_width
/2);
1003 if ( m_fSplitterX
< 0.0 )
1005 splitterX
= centerX
;
1007 else if ( widthChange
)
1009 //float centerX = float(pg->GetSize().x) * 0.5;
1012 splitterX
= m_fSplitterX
+ (float(widthChange
) * 0.5);
1013 float deviation
= fabs(centerX
- splitterX
);
1015 // If deviating from center, adjust towards it
1016 if ( deviation
> 20.0 )
1018 if ( splitterX
> centerX
)
1026 // No width change, just keep sure we keep splitter position intact
1027 splitterX
= m_fSplitterX
;
1028 float deviation
= fabs(centerX
- splitterX
);
1029 if ( deviation
> 50.0 )
1031 splitterX
= centerX
;
1035 DoSetSplitterPosition((int)splitterX
, 0, false, true);
1037 m_fSplitterX
= splitterX
; // needed to retain accuracy
1041 void wxPropertyGridPageState::SetColumnCount( int colCount
)
1043 wxASSERT( colCount
>= 2 );
1044 m_colWidths
.SetCount( colCount
, wxPG_DRAG_MARGIN
);
1045 if ( m_colWidths
.size() > (unsigned int)colCount
)
1046 m_colWidths
.RemoveAt( m_colWidths
.size(), m_colWidths
.size() - colCount
);
1048 if ( m_pPropGrid
->GetState() == this )
1049 m_pPropGrid
->RecalculateVirtualSize();
1051 CheckColumnWidths();
1054 // Returns column index, -1 for margin
1055 int wxPropertyGridPageState::HitTestH( int x
, int* pSplitterHit
, int* pSplitterHitOffset
) const
1057 int cx
= GetGrid()->m_marginWidth
;
1059 int prevSplitter
= -1;
1064 if ( col
>= (int)m_colWidths
.size() )
1070 cx
+= m_colWidths
[col
];
1073 // Near prev. splitter
1076 int diff
= x
- prevSplitter
;
1077 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1079 *pSplitterHit
= col
- 1;
1080 *pSplitterHitOffset
= diff
;
1085 // Near next splitter
1086 int nextSplitter
= cx
;
1087 if ( col
< (int)(m_colWidths
.size()-1) )
1089 int diff
= x
- nextSplitter
;
1090 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1092 *pSplitterHit
= col
;
1093 *pSplitterHitOffset
= diff
;
1102 // -----------------------------------------------------------------------
1103 // wxPropertyGridPageState property value setting and getting
1104 // -----------------------------------------------------------------------
1106 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty
* p
, const wxString
& value
)
1110 int flags
= wxPG_REPORT_ERROR
|wxPG_FULL_VALUE
|wxPG_PROGRAMMATIC_VALUE
;
1112 wxVariant variant
= p
->GetValueRef();
1115 if ( p
->GetMaxLength() <= 0 )
1116 res
= p
->StringToValue( variant
, value
, flags
);
1118 res
= p
->StringToValue( variant
, value
.Mid(0,p
->GetMaxLength()), flags
);
1122 p
->SetValue(variant
);
1123 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1124 m_pPropGrid
->RefreshEditor();
1132 // -----------------------------------------------------------------------
1134 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty
* p
, wxVariant
& value
)
1139 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1140 m_pPropGrid
->RefreshEditor();
1147 // -----------------------------------------------------------------------
1149 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty
* p
, wxObject
* value
)
1153 // wnd_primary has to be given so the control can be updated as well.
1155 DoSetPropertyValue(p
, v
);
1161 // -----------------------------------------------------------------------
1163 void wxPropertyGridPageState::DoSetPropertyValueUnspecified( wxPGProperty
* p
)
1165 wxCHECK_RET( p
, wxT("invalid property id") );
1167 if ( !p
->IsValueUnspecified() )
1169 // Value should be set first - editor class methods may need it
1170 p
->m_value
.MakeNull();
1172 wxASSERT( m_pPropGrid
);
1174 if ( m_pPropGrid
->GetState() == this )
1176 if ( m_pPropGrid
->m_selected
== p
&& m_pPropGrid
->m_wndEditor
)
1178 p
->GetEditorClass()->SetValueToUnspecified(p
, m_pPropGrid
->GetEditorControl());
1183 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1184 DoSetPropertyValueUnspecified( p
->Item(i
) );
1188 // -----------------------------------------------------------------------
1189 // wxPropertyGridPageState property operations
1190 // -----------------------------------------------------------------------
1192 bool wxPropertyGridPageState::DoCollapse( wxPGProperty
* p
)
1194 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1196 if ( !p
->GetChildCount() ) return false;
1198 if ( !p
->IsExpanded() ) return false;
1200 p
->SetExpanded(false);
1202 VirtualHeightChanged();
1207 // -----------------------------------------------------------------------
1209 bool wxPropertyGridPageState::DoExpand( wxPGProperty
* p
)
1211 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1213 if ( !p
->GetChildCount() ) return false;
1215 if ( p
->IsExpanded() ) return false;
1217 p
->SetExpanded(true);
1219 VirtualHeightChanged();
1224 // -----------------------------------------------------------------------
1226 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty
* p
, unsigned int flags
)
1228 if ( this == m_pPropGrid
->GetState() )
1229 return m_pPropGrid
->DoSelectProperty( p
, flags
);
1235 // -----------------------------------------------------------------------
1237 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty
* p
, bool hide
, int flags
)
1240 p
->ClearFlag( wxPG_PROP_HIDDEN
);
1242 p
->SetFlag( wxPG_PROP_HIDDEN
);
1244 if ( flags
& wxPG_RECURSE
)
1247 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1248 DoHideProperty(p
->Item(i
), hide
, flags
| wxPG_RECURSE_STARTS
);
1251 VirtualHeightChanged();
1256 // -----------------------------------------------------------------------
1258 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty
* p
, bool enable
)
1264 if ( !(p
->m_flags
& wxPG_PROP_DISABLED
) )
1269 p
->m_flags
&= ~(wxPG_PROP_DISABLED
);
1273 if ( p
->m_flags
& wxPG_PROP_DISABLED
)
1278 p
->m_flags
|= wxPG_PROP_DISABLED
;
1282 // Apply same to sub-properties as well
1284 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1285 DoEnableProperty( p
->Item(i
), enable
);
1292 // -----------------------------------------------------------------------
1293 // wxPropertyGridPageState wxVariant related routines
1294 // -----------------------------------------------------------------------
1296 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1297 // Never includes sub-properties (unless they are parented by wxParentProperty).
1298 wxVariant
wxPropertyGridPageState::DoGetPropertyValues( const wxString
& listname
,
1299 wxPGProperty
* baseparent
,
1302 wxPGProperty
* pwc
= (wxPGProperty
*) baseparent
;
1304 // Root is the default base-parent.
1308 wxVariantList tempList
;
1309 wxVariant
v( tempList
, listname
);
1311 if ( pwc
->GetChildCount() )
1313 if ( flags
& wxPG_KEEP_STRUCTURE
)
1315 wxASSERT( !pwc
->HasFlag(wxPG_PROP_AGGREGATE
) );
1318 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1320 wxPGProperty
* p
= pwc
->Item(i
);
1321 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1323 wxVariant variant
= p
->GetValue();
1324 variant
.SetName( p
->GetBaseName() );
1325 v
.Append( variant
);
1329 v
.Append( DoGetPropertyValues(p
->m_name
,p
,flags
|wxPG_KEEP_STRUCTURE
) );
1331 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1332 v
.Append( p
->GetAttributesAsList() );
1337 wxPropertyGridConstIterator
it( this, wxPG_ITERATE_DEFAULT
, pwc
->Item(0) );
1338 it
.SetBaseParent( pwc
);
1340 for ( ; !it
.AtEnd(); it
.Next() )
1342 const wxPGProperty
* p
= it
.GetProperty();
1344 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1345 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1347 wxVariant variant
= p
->GetValue();
1348 variant
.SetName( p
->GetName() );
1349 v
.Append( variant
);
1350 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1351 v
.Append( p
->GetAttributesAsList() );
1360 // -----------------------------------------------------------------------
1362 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList
& list
, wxPGProperty
* defaultCategory
)
1364 unsigned char origFrozen
= 1;
1366 if ( m_pPropGrid
->GetState() == this )
1368 origFrozen
= m_pPropGrid
->m_frozen
;
1369 if ( !origFrozen
) m_pPropGrid
->Freeze();
1372 wxPropertyCategory
* use_category
= (wxPropertyCategory
*)defaultCategory
;
1374 if ( !use_category
)
1375 use_category
= (wxPropertyCategory
*)m_properties
;
1377 // Let's iterate over the list of variants.
1378 wxVariantList::const_iterator node
;
1379 int numSpecialEntries
= 0;
1382 // Second pass for special entries
1383 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1385 wxVariant
*current
= (wxVariant
*)*node
;
1387 // Make sure it is wxVariant.
1388 wxASSERT( current
);
1389 wxASSERT( wxStrcmp(current
->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1391 const wxString
& name
= current
->GetName();
1392 if ( name
.length() > 0 )
1395 // '@' signified a special entry
1396 if ( name
[0] == wxS('@') )
1398 numSpecialEntries
++;
1402 wxPGProperty
* foundProp
= BaseGetPropertyByName(name
);
1405 wxPGProperty
* p
= foundProp
;
1407 // If it was a list, we still have to go through it.
1408 if ( wxStrcmp(current
->GetType(), wxS("list")) == 0 )
1410 DoSetPropertyValues( current
->GetList(),
1411 p
->IsCategory()?p
:((wxPGProperty
*)NULL
)
1417 if ( wxStrcmp(current
->GetType(), p
->GetValue().GetType()) != 0)
1419 wxLogDebug(wxT("wxPropertyGridPageState::DoSetPropertyValues Warning: Setting value of property \"%s\" from variant"),
1420 p
->GetName().c_str());
1424 p
->SetValue(*current
);
1430 if ( current
->GetType() != wxS("list") )
1436 // Yes, it is; create a sub category and append contents there.
1437 wxPGProperty
* newCat
= DoInsert(use_category
,-1,new wxPropertyCategory(current
->GetName(),wxPG_LABEL
));
1438 DoSetPropertyValues( current
->GetList(), newCat
);
1445 if ( numSpecialEntries
)
1447 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1449 wxVariant
*current
= (wxVariant
*)*node
;
1451 const wxString
& name
= current
->GetName();
1452 if ( name
.length() > 0 )
1455 // '@' signified a special entry
1456 if ( name
[0] == wxS('@') )
1458 numSpecialEntries
--;
1460 size_t pos2
= name
.rfind(wxS('@'));
1461 if ( pos2
> 0 && pos2
< (name
.size()-1) )
1463 wxString propName
= name
.substr(1, pos2
-1);
1464 wxString entryType
= name
.substr(pos2
+1, wxString::npos
);
1466 if ( entryType
== wxS("attr") )
1469 // List of attributes
1470 wxPGProperty
* foundProp
= BaseGetPropertyByName(propName
);
1473 wxASSERT( current
->GetType() == wxPG_VARIANT_TYPE_LIST
);
1475 wxVariantList
& list2
= current
->GetList();
1476 wxVariantList::const_iterator node2
;
1478 for ( node2
= list2
.begin(); node2
!= list2
.end(); ++node2
)
1480 wxVariant
*attr
= (wxVariant
*)*node2
;
1481 foundProp
->SetAttribute( attr
->GetName(), *attr
);
1486 // ERROR: No such property: 'propName'
1492 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1497 if ( !numSpecialEntries
)
1504 m_pPropGrid
->Thaw();
1506 if ( this == m_pPropGrid
->GetState() )
1507 m_pPropGrid
->RefreshEditor();
1512 // -----------------------------------------------------------------------
1513 // wxPropertyGridPageState property adding and removal
1514 // -----------------------------------------------------------------------
1516 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty
* property
,
1517 wxPGProperty
* scheduledParent
)
1519 wxPropertyGrid
* propGrid
= m_pPropGrid
;
1521 // This will allow better behavior.
1522 if ( scheduledParent
== m_properties
)
1523 scheduledParent
= (wxPGProperty
*) NULL
;
1525 if ( scheduledParent
&& !scheduledParent
->IsCategory() )
1527 wxASSERT_MSG( property
->GetBaseName().length(),
1528 "Property's children must have unique, non-empty names within their scope" );
1531 property
->m_parentState
= this;
1533 if ( property
->IsCategory() )
1536 // Parent of a category must be either root or another category
1537 // (otherwise Bad Things might happen).
1538 wxASSERT_MSG( scheduledParent
== NULL
||
1539 scheduledParent
== m_properties
||
1540 scheduledParent
->IsCategory(),
1541 wxT("Parent of a category must be either root or another category."));
1543 // If we already have category with same name, delete given property
1544 // and use it instead as most recent caption item.
1545 wxPGProperty
* found_id
= BaseGetPropertyByName( property
->GetBaseName() );
1548 wxPropertyCategory
* pwc
= (wxPropertyCategory
*) found_id
;
1549 if ( pwc
->IsCategory() ) // Must be a category.
1552 m_currentCategory
= pwc
;
1559 // Warn for identical names in debug mode.
1560 if ( BaseGetPropertyByName(property
->GetName()) &&
1561 (!scheduledParent
|| scheduledParent
->IsCategory()) )
1563 wxLogError(wxT("wxPropertyGrid: Warning - item with name \"%s\" already exists."),
1564 property
->GetName().c_str());
1565 wxPGGlobalVars
->m_warnings
++;
1569 // Make sure nothing is selected.
1571 propGrid
->ClearSelection(false);
1573 // NULL parent == root parent
1574 if ( !scheduledParent
)
1575 scheduledParent
= DoGetRoot();
1577 property
->m_parent
= scheduledParent
;
1579 property
->InitAfterAdded(this, propGrid
);
1581 if ( property
->IsCategory() )
1583 wxPropertyCategory
* pc
= wxStaticCast(property
, wxPropertyCategory
);
1585 m_currentCategory
= pc
;
1587 // Calculate text extent for category caption
1589 pc
->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
1595 // -----------------------------------------------------------------------
1597 wxPGProperty
* wxPropertyGridPageState::DoAppend( wxPGProperty
* property
)
1599 wxPropertyCategory
* cur_cat
= m_currentCategory
;
1600 if ( property
->IsCategory() )
1601 cur_cat
= (wxPropertyCategory
*) NULL
;
1603 return DoInsert( cur_cat
, -1, property
);
1606 // -----------------------------------------------------------------------
1608 wxPGProperty
* wxPropertyGridPageState::DoInsert( wxPGProperty
* parent
, int index
, wxPGProperty
* property
)
1611 parent
= m_properties
;
1613 wxCHECK_MSG( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1615 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1617 bool res
= PrepareToAddItem( property
, (wxPropertyCategory
*)parent
);
1619 // PrepareToAddItem() may just decide to use use current category
1620 // instead of adding new one.
1622 return m_currentCategory
;
1624 // Note that item must be added into current mode later.
1626 // If parent is wxParentProperty, just stick it in...
1627 // If parent is root (m_properties), then...
1628 // In categoric mode: Add as last item in m_abcArray (if not category).
1629 // Add to given index in m_regularArray.
1630 // In non-cat mode: Add as last item in m_regularArray.
1631 // Add to given index in m_abcArray.
1632 // If parent is category, then...
1633 // 1) Add to given category in given index.
1634 // 2) Add as last item in m_abcArray.
1636 if ( !parent
->IsCategory() && !parent
->IsRoot() )
1638 // Parent is wxParentingProperty: Just stick it in...
1639 parent
->AddChild2( property
, index
);
1643 // Parent is Category or Root.
1645 if ( m_properties
== &m_regularArray
)
1649 // Only add non-categories to m_abcArray.
1650 if ( m_abcArray
&& !property
->IsCategory() )
1651 m_abcArray
->AddChild2( property
, -1, false );
1653 // Add to current mode.
1654 parent
->AddChild2( property
, index
);
1659 // Non-categorized mode.
1661 if ( parent
!= m_properties
)
1662 // Parent is category.
1663 parent
->AddChild2( property
, index
, false );
1666 m_regularArray
.AddChild2( property
, -1, false );
1668 // Add to current mode (no categories).
1669 if ( !property
->IsCategory() )
1670 m_abcArray
->AddChild2( property
, index
);
1675 if ( property
->IsCategory() )
1677 // This is a category caption item.
1679 // Last caption is not the bottom one (this info required by append)
1680 m_lastCaptionBottomnest
= 0;
1683 // Only add name to hashmap if parent is root or category
1684 if ( property
->m_name
.length() &&
1685 (parent
->IsCategory() || parent
->IsRoot()) )
1686 m_dictName
[property
->m_name
] = (void*) property
;
1688 VirtualHeightChanged();
1690 property
->UpdateParentValues();
1697 // -----------------------------------------------------------------------
1699 void wxPropertyGridPageState::DoDelete( wxPGProperty
* item
, bool doDelete
)
1701 wxCHECK_RET( item
->GetParent(),
1702 wxT("this property was already deleted") );
1704 wxCHECK_RET( item
!= &m_regularArray
&& item
!= m_abcArray
,
1705 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1707 unsigned int indinparent
= item
->GetIndexInParent();
1709 wxPGProperty
* pwc
= (wxPGProperty
*)item
;
1710 wxPGProperty
* parent
= item
->GetParent();
1712 wxCHECK_RET( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1713 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1716 if ( item
->GetChildCount() && !item
->HasFlag(wxPG_PROP_AGGREGATE
) )
1718 // deleting a category
1719 if ( item
->IsCategory() )
1721 if ( pwc
== m_currentCategory
)
1722 m_currentCategory
= (wxPropertyCategory
*) NULL
;
1725 item
->DeleteChildren();
1728 if ( !IsInNonCatMode() )
1730 // categorized mode - non-categorized array
1732 // Remove from non-cat array
1733 if ( !item
->IsCategory() &&
1734 (parent
->IsCategory() || parent
->IsRoot()) )
1737 m_abcArray
->RemoveChild(item
);
1740 // categorized mode - categorized array
1741 wxArrayPGProperty
& parentsChildren
= parent
->m_children
;
1742 parentsChildren
.erase( parentsChildren
.begin() + indinparent
);
1743 item
->m_parent
->FixIndicesOfChildren();
1747 // non-categorized mode - categorized array
1749 // We need to find location of item.
1750 wxPGProperty
* cat_parent
= &m_regularArray
;
1751 int cat_index
= m_regularArray
.GetChildCount();
1753 for ( i
= 0; i
< m_regularArray
.GetChildCount(); i
++ )
1755 wxPGProperty
* p
= m_regularArray
.Item(i
);
1756 if ( p
== item
) { cat_index
= i
; break; }
1757 if ( p
->IsCategory() )
1759 int subind
= ((wxPGProperty
*)p
)->Index(item
);
1760 if ( subind
!= wxNOT_FOUND
)
1762 cat_parent
= ((wxPGProperty
*)p
);
1768 cat_parent
->m_children
.erase(cat_parent
->m_children
.begin()+cat_index
);
1770 // non-categorized mode - non-categorized array
1771 if ( !item
->IsCategory() )
1773 wxASSERT( item
->m_parent
== m_abcArray
);
1774 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1775 parentsChildren
.erase(parentsChildren
.begin() + indinparent
);
1776 item
->m_parent
->FixIndicesOfChildren(indinparent
);
1780 if ( item
->GetBaseName().length() &&
1781 (parent
->IsCategory() || parent
->IsRoot()) )
1782 m_dictName
.erase(item
->GetBaseName());
1784 // We can actually delete it now
1788 m_itemsAdded
= 1; // Not a logical assignment (but required nonetheless).
1790 VirtualHeightChanged();
1793 // -----------------------------------------------------------------------
1795 #endif // wxUSE_PROPGRID