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 wxPropertyGridIterator
it( this, wxPG_ITERATE_DEFAULT
|wxPG_ITERATE_CATEGORIES
);
251 for ( ; !it
.AtEnd(); it
.Next() )
253 wxPGProperty
* p
= it
.GetProperty();
254 wxPGProperty
* parent
= p
->GetParent();
255 if ( p
->HasFlag(wxPG_PROP_MISC_PARENT
) &&
256 ( parent
== m_properties
|| (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 if ( p
->GetBaseName().Len() ) m_dictName
.erase( p
->GetBaseName() );
457 if ( newName
.Len() ) m_dictName
[newName
] = (void*) p
;
459 p
->DoSetName(newName
);
462 // -----------------------------------------------------------------------
463 // wxPropertyGridPageState global operations
464 // -----------------------------------------------------------------------
466 // -----------------------------------------------------------------------
467 // Item iteration macros
468 // NB: Nowadays only needed for alphabetic/categoric mode switching.
469 // -----------------------------------------------------------------------
471 //#define II_INVALID_I 0x00FFFFFF
473 #define ITEM_ITERATION_VARIABLES \
474 wxPGProperty* parent; \
478 #define ITEM_ITERATION_INIT_FROM_THE_TOP \
479 parent = m_properties; \
483 #define ITEM_ITERATION_INIT(startparent, startindex, state) \
484 parent = startparent; \
485 i = (unsigned int)startindex; \
486 if ( parent == (wxPGProperty*) NULL ) \
488 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;
900 wxLogDebug(wxT("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"), width
, clientWidth
);
905 for ( i
=0; i
<m_colWidths
.size(); i
++ )
907 int min
= GetColumnMinWidth(i
);
908 if ( m_colWidths
[i
] <= min
)
910 m_colWidths
[i
] = min
;
914 if ( pg
->HasFlag(wxPG_SPLITTER_AUTO_CENTER
) )
916 if ( m_colWidths
[i
] >= highestColWidth
)
918 highestColWidth
= m_colWidths
[i
];
929 int colsWidth
= pg
->m_marginWidth
;
930 for ( i
=0; i
<m_colWidths
.size(); i
++ )
931 colsWidth
+= m_colWidths
[i
];
935 wxLogDebug(wxT(" HasVirtualWidth: %i colsWidth: %i"),(int)pg
->HasVirtualWidth(),colsWidth
);
938 // Then mode-based requirement
939 if ( !pg
->HasVirtualWidth() )
941 int widthHigher
= width
- colsWidth
;
943 // Adapt colsWidth to width
944 if ( colsWidth
< width
)
949 wxLogDebug(wxT(" Adjust last column to %i"), m_colWidths
[lastColumn
] + widthHigher
);
951 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + widthHigher
;
953 else if ( colsWidth
> width
)
956 if ( reduceCol
!= -1 )
960 wxLogDebug(wxT(" Reduce column %i (by %i)"), reduceCol
, -widthHigher
);
962 // Reduce widest column, and recheck
963 m_colWidths
[reduceCol
] = m_colWidths
[reduceCol
] + widthHigher
;
970 // Only check colsWidth against clientWidth
971 if ( colsWidth
< clientWidth
)
973 m_colWidths
[lastColumn
] = m_colWidths
[lastColumn
] + (clientWidth
-colsWidth
);
978 // If width changed, recalculate virtual size
979 if ( pg
->GetState() == this )
980 pg
->RecalculateVirtualSize();
985 for ( i
=0; i
<m_colWidths
.size(); i
++ )
986 wxLogDebug(wxT("col%i: %i"),i
,m_colWidths
[i
]);
989 // Auto center splitter
990 if ( !(pg
->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER
) &&
991 m_colWidths
.size() == 2 )
993 float centerX
= (float)(pg
->m_width
/2);
996 if ( m_fSplitterX
< 0.0 )
1000 else if ( widthChange
)
1002 //float centerX = float(pg->GetSize().x) * 0.5;
1005 splitterX
= m_fSplitterX
+ (float(widthChange
) * 0.5);
1006 float deviation
= fabs(centerX
- splitterX
);
1008 // If deviating from center, adjust towards it
1009 if ( deviation
> 20.0 )
1011 if ( splitterX
> centerX
)
1019 // No width change, just keep sure we keep splitter position intact
1020 splitterX
= m_fSplitterX
;
1021 float deviation
= fabs(centerX
- splitterX
);
1022 if ( deviation
> 50.0 )
1024 splitterX
= centerX
;
1028 DoSetSplitterPosition((int)splitterX
, 0, false, true);
1030 m_fSplitterX
= splitterX
; // needed to retain accuracy
1034 void wxPropertyGridPageState::SetColumnCount( int colCount
)
1036 wxASSERT( colCount
>= 2 );
1037 m_colWidths
.SetCount( colCount
, wxPG_DRAG_MARGIN
);
1038 if ( m_colWidths
.size() > (unsigned int)colCount
)
1039 m_colWidths
.RemoveAt( m_colWidths
.size(), m_colWidths
.size() - colCount
);
1041 if ( m_pPropGrid
->GetState() == this )
1042 m_pPropGrid
->RecalculateVirtualSize();
1044 CheckColumnWidths();
1047 // Returns column index, -1 for margin
1048 int wxPropertyGridPageState::HitTestH( int x
, int* pSplitterHit
, int* pSplitterHitOffset
) const
1050 int cx
= GetGrid()->m_marginWidth
;
1052 int prevSplitter
= -1;
1057 if ( col
>= (int)m_colWidths
.size() )
1063 cx
+= m_colWidths
[col
];
1066 // Near prev. splitter
1069 int diff
= x
- prevSplitter
;
1070 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1072 *pSplitterHit
= col
- 1;
1073 *pSplitterHitOffset
= diff
;
1078 // Near next splitter
1079 int nextSplitter
= cx
;
1080 if ( col
< (int)(m_colWidths
.size()-1) )
1082 int diff
= x
- nextSplitter
;
1083 if ( abs(diff
) < wxPG_SPLITTERX_DETECTMARGIN1
)
1085 *pSplitterHit
= col
;
1086 *pSplitterHitOffset
= diff
;
1095 // -----------------------------------------------------------------------
1096 // wxPropertyGridPageState property value setting and getting
1097 // -----------------------------------------------------------------------
1099 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty
* p
, const wxString
& value
)
1103 int flags
= wxPG_REPORT_ERROR
|wxPG_FULL_VALUE
|wxPG_PROGRAMMATIC_VALUE
;
1105 wxVariant variant
= p
->GetValueRef();
1108 if ( p
->GetMaxLength() <= 0 )
1109 res
= p
->StringToValue( variant
, value
, flags
);
1111 res
= p
->StringToValue( variant
, value
.Mid(0,p
->GetMaxLength()), flags
);
1115 p
->SetValue(variant
);
1116 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1117 p
->UpdateControl(m_pPropGrid
->GetEditorControl());
1125 // -----------------------------------------------------------------------
1127 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty
* p
, wxVariant
& value
)
1132 if ( m_selected
==p
&& this==m_pPropGrid
->GetState() )
1133 p
->UpdateControl(m_pPropGrid
->GetEditorControl());
1140 // -----------------------------------------------------------------------
1142 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty
* p
, wxObject
* value
)
1146 // wnd_primary has to be given so the control can be updated as well.
1148 DoSetPropertyValue(p
, v
);
1154 // -----------------------------------------------------------------------
1156 void wxPropertyGridPageState::DoSetPropertyValueUnspecified( wxPGProperty
* p
)
1158 wxCHECK_RET( p
, wxT("invalid property id") );
1160 if ( !p
->IsValueUnspecified() )
1162 // Value should be set first - editor class methods may need it
1163 p
->m_value
.MakeNull();
1165 wxASSERT( m_pPropGrid
);
1167 if ( m_pPropGrid
->GetState() == this )
1169 if ( m_pPropGrid
->m_selected
== p
&& m_pPropGrid
->m_wndEditor
)
1171 p
->GetEditorClass()->SetValueToUnspecified(p
, m_pPropGrid
->GetEditorControl());
1176 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1177 DoSetPropertyValueUnspecified( p
->Item(i
) );
1181 // -----------------------------------------------------------------------
1182 // wxPropertyGridPageState property operations
1183 // -----------------------------------------------------------------------
1185 bool wxPropertyGridPageState::DoCollapse( wxPGProperty
* p
)
1187 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1189 if ( !p
->GetChildCount() ) return false;
1191 if ( !p
->IsExpanded() ) return false;
1193 p
->SetExpanded(false);
1195 VirtualHeightChanged();
1200 // -----------------------------------------------------------------------
1202 bool wxPropertyGridPageState::DoExpand( wxPGProperty
* p
)
1204 wxCHECK_MSG( p
, false, wxT("invalid property id") );
1206 if ( !p
->GetChildCount() ) return false;
1208 if ( p
->IsExpanded() ) return false;
1210 p
->SetExpanded(true);
1212 VirtualHeightChanged();
1217 // -----------------------------------------------------------------------
1219 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty
* p
, unsigned int flags
)
1221 if ( this == m_pPropGrid
->GetState() )
1222 return m_pPropGrid
->DoSelectProperty( p
, flags
);
1228 // -----------------------------------------------------------------------
1230 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty
* p
, bool hide
, int flags
)
1233 p
->ClearFlag( wxPG_PROP_HIDDEN
);
1235 p
->SetFlag( wxPG_PROP_HIDDEN
);
1237 if ( flags
& wxPG_RECURSE
)
1240 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1241 DoHideProperty(p
->Item(i
), hide
, flags
| wxPG_RECURSE_STARTS
);
1244 VirtualHeightChanged();
1249 // -----------------------------------------------------------------------
1251 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty
* p
, bool enable
)
1257 if ( !(p
->m_flags
& wxPG_PROP_DISABLED
) )
1262 p
->m_flags
&= ~(wxPG_PROP_DISABLED
);
1266 if ( p
->m_flags
& wxPG_PROP_DISABLED
)
1271 p
->m_flags
|= wxPG_PROP_DISABLED
;
1275 // Apply same to sub-properties as well
1277 for ( i
= 0; i
< p
->GetChildCount(); i
++ )
1278 DoEnableProperty( p
->Item(i
), enable
);
1285 // -----------------------------------------------------------------------
1286 // wxPropertyGridPageState wxVariant related routines
1287 // -----------------------------------------------------------------------
1289 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1290 // Never includes sub-properties (unless they are parented by wxParentProperty).
1291 wxVariant
wxPropertyGridPageState::DoGetPropertyValues( const wxString
& listname
,
1292 wxPGProperty
* baseparent
,
1295 wxPGProperty
* pwc
= (wxPGProperty
*) baseparent
;
1297 // Root is the default base-parent.
1301 wxVariantList tempList
;
1302 wxVariant
v( tempList
, listname
);
1304 if ( pwc
->GetChildCount() )
1306 if ( flags
& wxPG_KEEP_STRUCTURE
)
1308 wxASSERT( !pwc
->HasFlag(wxPG_PROP_AGGREGATE
) );
1311 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1313 wxPGProperty
* p
= pwc
->Item(i
);
1314 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1316 wxVariant variant
= p
->GetValue();
1317 variant
.SetName( p
->GetBaseName() );
1318 v
.Append( variant
);
1322 v
.Append( DoGetPropertyValues(p
->m_name
,p
,flags
|wxPG_KEEP_STRUCTURE
) );
1324 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1325 v
.Append( p
->GetAttributesAsList() );
1330 wxPropertyGridConstIterator
it( this, wxPG_ITERATE_DEFAULT
, pwc
->Item(0) );
1331 it
.SetBaseParent( pwc
);
1333 for ( ; !it
.AtEnd(); it
.Next() )
1335 const wxPGProperty
* p
= it
.GetProperty();
1337 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1338 if ( !p
->GetChildCount() || p
->HasFlag(wxPG_PROP_AGGREGATE
) )
1340 wxVariant variant
= p
->GetValue();
1341 variant
.SetName( p
->GetName() );
1342 v
.Append( variant
);
1343 if ( (flags
& wxPG_INC_ATTRIBUTES
) && p
->m_attributes
.GetCount() )
1344 v
.Append( p
->GetAttributesAsList() );
1353 // -----------------------------------------------------------------------
1355 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList
& list
, wxPGProperty
* defaultCategory
)
1357 unsigned char origFrozen
= 1;
1359 if ( m_pPropGrid
->GetState() == this )
1361 origFrozen
= m_pPropGrid
->m_frozen
;
1362 if ( !origFrozen
) m_pPropGrid
->Freeze();
1365 wxPropertyCategory
* use_category
= (wxPropertyCategory
*)defaultCategory
;
1367 if ( !use_category
)
1368 use_category
= (wxPropertyCategory
*)m_properties
;
1370 // Let's iterate over the list of variants.
1371 wxVariantList::const_iterator node
;
1372 int numSpecialEntries
= 0;
1375 // Second pass for special entries
1376 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1378 wxVariant
*current
= (wxVariant
*)*node
;
1380 // Make sure it is wxVariant.
1381 wxASSERT( current
);
1382 wxASSERT( wxStrcmp(current
->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1384 const wxString
& name
= current
->GetName();
1385 if ( name
.length() > 0 )
1388 // '@' signified a special entry
1389 if ( name
[0] == wxS('@') )
1391 numSpecialEntries
++;
1395 wxPGProperty
* foundProp
= BaseGetPropertyByName(name
);
1398 wxPGProperty
* p
= foundProp
;
1400 // If it was a list, we still have to go through it.
1401 if ( wxStrcmp(current
->GetType(), wxS("list")) == 0 )
1403 DoSetPropertyValues( current
->GetList(),
1404 p
->IsCategory()?p
:((wxPGProperty
*)NULL
)
1410 if ( wxStrcmp(current
->GetType(), p
->GetValue().GetType()) != 0)
1412 wxLogDebug(wxT("wxPropertyGridPageState::DoSetPropertyValues Warning: Setting value of property \"%s\" from variant"),
1413 p
->GetName().c_str());
1417 p
->SetValue(*current
);
1423 if ( current
->GetType() != wxS("list") )
1429 // Yes, it is; create a sub category and append contents there.
1430 wxPGProperty
* newCat
= DoInsert(use_category
,-1,new wxPropertyCategory(current
->GetName(),wxPG_LABEL
));
1431 DoSetPropertyValues( current
->GetList(), newCat
);
1438 if ( numSpecialEntries
)
1440 for ( node
= list
.begin(); node
!= list
.end(); ++node
)
1442 wxVariant
*current
= (wxVariant
*)*node
;
1444 const wxString
& name
= current
->GetName();
1445 if ( name
.length() > 0 )
1448 // '@' signified a special entry
1449 if ( name
[0] == wxS('@') )
1451 numSpecialEntries
--;
1453 size_t pos2
= name
.rfind(wxS('@'));
1454 if ( pos2
> 0 && pos2
< (name
.size()-1) )
1456 wxString propName
= name
.substr(1, pos2
-1);
1457 wxString entryType
= name
.substr(pos2
+1, wxString::npos
);
1459 if ( entryType
== wxS("attr") )
1462 // List of attributes
1463 wxPGProperty
* foundProp
= BaseGetPropertyByName(propName
);
1466 wxASSERT( current
->GetType() == wxPG_VARIANT_TYPE_LIST
);
1468 wxVariantList
& list2
= current
->GetList();
1469 wxVariantList::const_iterator node2
;
1471 for ( node2
= list2
.begin(); node2
!= list2
.end(); ++node2
)
1473 wxVariant
*attr
= (wxVariant
*)*node2
;
1474 foundProp
->SetAttribute( attr
->GetName(), *attr
);
1479 // ERROR: No such property: 'propName'
1485 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1490 if ( !numSpecialEntries
)
1497 m_pPropGrid
->Thaw();
1499 if ( this == m_pPropGrid
->GetState() )
1501 m_selected
->UpdateControl(m_pPropGrid
->GetEditorControl());
1507 // -----------------------------------------------------------------------
1508 // wxPropertyGridPageState property adding and removal
1509 // -----------------------------------------------------------------------
1511 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty
* property
,
1512 wxPGProperty
* scheduledParent
)
1514 wxPropertyGrid
* propGrid
= m_pPropGrid
;
1516 // This will allow better behavior.
1517 if ( scheduledParent
== m_properties
)
1518 scheduledParent
= (wxPGProperty
*) NULL
;
1520 if ( scheduledParent
&& !scheduledParent
->IsCategory() )
1522 wxASSERT_MSG( property
->GetBaseName().length(),
1523 "Property's children must have unique, non-empty names within their scope" );
1526 property
->m_parentState
= this;
1528 if ( property
->IsCategory() )
1531 // Parent of a category must be either root or another category
1532 // (otherwise Bad Things might happen).
1533 wxASSERT_MSG( scheduledParent
== NULL
||
1534 scheduledParent
== m_properties
||
1535 scheduledParent
->IsCategory(),
1536 wxT("Parent of a category must be either root or another category."));
1538 // If we already have category with same name, delete given property
1539 // and use it instead as most recent caption item.
1540 wxPGProperty
* found_id
= BaseGetPropertyByName( property
->GetBaseName() );
1543 wxPropertyCategory
* pwc
= (wxPropertyCategory
*) found_id
;
1544 if ( pwc
->IsCategory() ) // Must be a category.
1547 m_currentCategory
= pwc
;
1554 // Warn for identical names in debug mode.
1555 if ( BaseGetPropertyByName(property
->GetName()) &&
1556 (!scheduledParent
|| scheduledParent
->IsCategory()) )
1558 wxLogError(wxT("wxPropertyGrid: Warning - item with name \"%s\" already exists."),
1559 property
->GetName().c_str());
1560 wxPGGlobalVars
->m_warnings
++;
1564 // Make sure nothing is selected.
1566 propGrid
->ClearSelection(false);
1568 // NULL parent == root parent
1569 if ( !scheduledParent
)
1570 scheduledParent
= DoGetRoot();
1572 property
->m_parent
= scheduledParent
;
1574 property
->InitAfterAdded(this, propGrid
);
1576 if ( property
->IsCategory() )
1578 wxPropertyCategory
* pc
= wxStaticCast(property
, wxPropertyCategory
);
1580 m_currentCategory
= pc
;
1582 // Calculate text extent for category caption
1584 pc
->CalculateTextExtent(propGrid
, propGrid
->GetCaptionFont());
1590 // -----------------------------------------------------------------------
1592 wxPGProperty
* wxPropertyGridPageState::DoAppend( wxPGProperty
* property
)
1594 wxPropertyCategory
* cur_cat
= m_currentCategory
;
1595 if ( property
->IsCategory() )
1596 cur_cat
= (wxPropertyCategory
*) NULL
;
1598 return DoInsert( cur_cat
, -1, property
);
1601 // -----------------------------------------------------------------------
1603 wxPGProperty
* wxPropertyGridPageState::DoInsert( wxPGProperty
* parent
, int index
, wxPGProperty
* property
)
1606 parent
= m_properties
;
1608 wxCHECK_MSG( !parent
->HasFlag(wxPG_PROP_AGGREGATE
),
1610 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1612 bool res
= PrepareToAddItem( property
, (wxPropertyCategory
*)parent
);
1614 // PrepareToAddItem() may just decide to use use current category
1615 // instead of adding new one.
1617 return m_currentCategory
;
1619 // Note that item must be added into current mode later.
1621 // If parent is wxParentProperty, just stick it in...
1622 // If parent is root (m_properties), then...
1623 // In categoric mode: Add as last item in m_abcArray (if not category).
1624 // Add to given index in m_regularArray.
1625 // In non-cat mode: Add as last item in m_regularArray.
1626 // Add to given index in m_abcArray.
1627 // If parent is category, then...
1628 // 1) Add to given category in given index.
1629 // 2) Add as last item in m_abcArray.
1631 if ( !parent
->IsCategory() && !parent
->IsRoot() )
1633 // Parent is wxParentingProperty: Just stick it in...
1634 parent
->AddChild2( property
, index
);
1638 // Parent is Category or Root.
1640 if ( m_properties
== &m_regularArray
)
1644 // Only add non-categories to m_abcArray.
1645 if ( m_abcArray
&& !property
->IsCategory() )
1646 m_abcArray
->AddChild2( property
, -1, false );
1648 // Add to current mode.
1649 parent
->AddChild2( property
, index
);
1654 // Non-categorized mode.
1656 if ( parent
!= m_properties
)
1657 // Parent is category.
1658 parent
->AddChild2( property
, index
, false );
1661 m_regularArray
.AddChild2( property
, -1, false );
1663 // Add to current mode (no categories).
1664 if ( !property
->IsCategory() )
1665 m_abcArray
->AddChild2( property
, index
);
1670 if ( property
->IsCategory() )
1672 // This is a category caption item.
1674 // Last caption is not the bottom one (this info required by append)
1675 m_lastCaptionBottomnest
= 0;
1678 // Only add name to hashmap if parent is root or category
1679 if ( (parent
->IsCategory() || parent
->IsRoot()) && property
->m_name
.length() )
1680 m_dictName
[property
->m_name
] = (void*) property
;
1682 VirtualHeightChanged();
1684 property
->UpdateParentValues();
1691 // -----------------------------------------------------------------------
1693 void wxPropertyGridPageState::DoDelete( wxPGProperty
* item
, bool doDelete
)
1695 wxCHECK_RET( item
->GetParent(),
1696 wxT("this property was already deleted") );
1698 wxCHECK_RET( item
!= &m_regularArray
&& item
!= m_abcArray
,
1699 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1702 unsigned int indinparent
= item
->GetIndexInParent();
1704 wxPGProperty
* pwc
= (wxPGProperty
*)item
;
1706 wxCHECK_RET( !item
->GetParent()->HasFlag(wxPG_PROP_AGGREGATE
),
1707 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1709 if ( item
->IsCategory() )
1711 // deleting a category
1713 // erase category entries from the hash table
1714 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1716 wxPGProperty
* sp
= pwc
->Item( i
);
1717 if ( sp
->GetBaseName().Len() ) m_dictName
.erase(sp
->GetBaseName());
1720 if ( pwc
== m_currentCategory
)
1721 m_currentCategory
= (wxPropertyCategory
*) NULL
;
1725 // Remove children from non-categorized array.
1726 for ( i
=0; i
<pwc
->GetChildCount(); i
++ )
1728 wxPGProperty
* p
= pwc
->Item( i
);
1729 wxASSERT( p
!= NULL
);
1730 if ( !p
->IsCategory() )
1731 m_abcArray
->RemoveChild(p
);
1734 if ( IsInNonCatMode() )
1735 m_abcArray
->FixIndicesOfChildren();
1739 if ( !IsInNonCatMode() )
1741 // categorized mode - non-categorized array
1743 // Remove from non-cat array, but only if parent is in it
1744 if ( !item
->IsCategory() && item
->GetParent()->IsCategory() )
1747 m_abcArray
->RemoveChild(item
);
1750 // categorized mode - categorized array
1751 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1752 parentsChildren
.erase( parentsChildren
.begin() + indinparent
);
1753 item
->m_parent
->FixIndicesOfChildren();
1757 // non-categorized mode - categorized array
1759 // We need to find location of item.
1760 wxPGProperty
* cat_parent
= &m_regularArray
;
1761 int cat_index
= m_regularArray
.GetChildCount();
1763 for ( i
= 0; i
< m_regularArray
.GetChildCount(); i
++ )
1765 wxPGProperty
* p
= m_regularArray
.Item(i
);
1766 if ( p
== item
) { cat_index
= i
; break; }
1767 if ( p
->IsCategory() )
1769 int subind
= ((wxPGProperty
*)p
)->Index(item
);
1770 if ( subind
!= wxNOT_FOUND
)
1772 cat_parent
= ((wxPGProperty
*)p
);
1778 cat_parent
->m_children
.erase(cat_parent
->m_children
.begin()+cat_index
);
1780 // non-categorized mode - non-categorized array
1781 if ( !item
->IsCategory() )
1783 wxASSERT( item
->m_parent
== m_abcArray
);
1784 wxArrayPGProperty
& parentsChildren
= item
->m_parent
->m_children
;
1785 parentsChildren
.erase(parentsChildren
.begin() + indinparent
);
1786 item
->m_parent
->FixIndicesOfChildren(indinparent
);
1790 if ( item
->GetBaseName().Len() ) m_dictName
.erase(item
->GetBaseName());
1792 // We can actually delete it now
1796 m_itemsAdded
= 1; // Not a logical assignment (but required nonetheless).
1798 VirtualHeightChanged();
1801 // -----------------------------------------------------------------------
1803 #endif // wxUSE_PROPGRID