In wxPropertyGridPageState::DoDelete(), clear grid's m_propHover if it matches the...
[wxWidgets.git] / src / propgrid / propgridpagestate.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/propgridpagestate.cpp
3 // Purpose: wxPropertyGridPageState class
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: 2008-08-24
7 // RCS-ID: $Id$
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_PROPGRID
20
21 #ifndef WX_PRECOMP
22 #include "wx/defs.h"
23 #include "wx/object.h"
24 #include "wx/hash.h"
25 #include "wx/string.h"
26 #include "wx/log.h"
27 #include "wx/event.h"
28 #include "wx/window.h"
29 #include "wx/panel.h"
30 #include "wx/dc.h"
31 #include "wx/dcmemory.h"
32 #include "wx/pen.h"
33 #include "wx/brush.h"
34 #include "wx/intl.h"
35 #include "wx/stopwatch.h"
36 #endif
37
38 // This define is necessary to prevent macro clearing
39 #define __wxPG_SOURCE_FILE__
40
41 #include "wx/propgrid/propgridpagestate.h"
42 #include "wx/propgrid/propgrid.h"
43 #include "wx/propgrid/editors.h"
44
45 #define wxPG_DEFAULT_SPLITTERX 110
46
47
48 // -----------------------------------------------------------------------
49 // wxPropertyGridIterator
50 // -----------------------------------------------------------------------
51
52 void wxPropertyGridIteratorBase::Init( wxPropertyGridPageState* state, int flags, wxPGProperty* property, int dir )
53 {
54 wxASSERT( dir == 1 || dir == -1 );
55
56 m_state = state;
57 m_baseParent = state->DoGetRoot();
58 if ( !property && m_baseParent->GetChildCount() )
59 property = m_baseParent->Item(0);
60
61 m_property = property;
62
63 wxPG_ITERATOR_CREATE_MASKS(flags, m_itemExMask, m_parentExMask)
64
65 // Need to skip first?
66 if ( property && (property->GetFlags() & m_itemExMask) )
67 {
68 if ( dir == 1 )
69 Next();
70 else
71 Prev();
72 }
73 }
74
75 void wxPropertyGridIteratorBase::Init( wxPropertyGridPageState* state, int flags, int startPos, int dir )
76 {
77 wxPGProperty* property = NULL;
78
79 if ( startPos == wxTOP )
80 {
81 if ( dir == 0 )
82 dir = 1;
83 }
84 else if ( startPos == wxBOTTOM )
85 {
86 property = state->GetLastItem(flags);
87 if ( dir == 0 )
88 dir = -1;
89 }
90 else
91 {
92 wxFAIL_MSG("Only supported starting positions are wxTOP and wxBOTTOM");
93 }
94
95 Init( state, flags, property, dir );
96 }
97
98 void wxPropertyGridIteratorBase::Assign( const wxPropertyGridIteratorBase& it )
99 {
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;
105 }
106
107 void wxPropertyGridIteratorBase::Prev()
108 {
109 wxPGProperty* property = m_property;
110 wxASSERT( property );
111
112 wxPGProperty* parent = property->GetParent();
113 wxASSERT( parent );
114 unsigned int index = property->GetIndexInParent();
115
116 if ( index > 0 )
117 {
118 // Previous sibling
119 index--;
120
121 property = parent->Item(index);
122
123 // Go to last children?
124 if ( property->GetChildCount() &&
125 wxPG_ITERATOR_PARENTEXMASK_TEST(property, m_parentExMask) )
126 {
127 // First child
128 property = property->Last();
129 }
130 }
131 else
132 {
133 // Up to a parent
134 if ( parent == m_baseParent )
135 {
136 m_property = NULL;
137 return;
138 }
139 else
140 {
141 property = parent;
142 }
143 }
144
145 m_property = property;
146
147 // If property does not match our criteria, skip it
148 if ( property->GetFlags() & m_itemExMask )
149 Prev();
150 }
151
152 void wxPropertyGridIteratorBase::Next( bool iterateChildren )
153 {
154 wxPGProperty* property = m_property;
155 wxASSERT( property );
156
157 if ( property->GetChildCount() &&
158 wxPG_ITERATOR_PARENTEXMASK_TEST(property, m_parentExMask) &&
159 iterateChildren )
160 {
161 // First child
162 property = property->Item(0);
163 }
164 else
165 {
166 wxPGProperty* parent = property->GetParent();
167 wxASSERT( parent );
168 unsigned int index = property->GetIndexInParent() + 1;
169
170 if ( index < parent->GetChildCount() )
171 {
172 // Next sibling
173 property = parent->Item(index);
174 }
175 else
176 {
177 // Next sibling of parent
178 if ( parent == m_baseParent )
179 {
180 m_property = NULL;
181 }
182 else
183 {
184 m_property = parent;
185 Next(false);
186 }
187 return;
188 }
189 }
190
191 m_property = property;
192
193 // If property does not match our criteria, skip it
194 if ( property->GetFlags() & m_itemExMask )
195 Next();
196 }
197
198 // -----------------------------------------------------------------------
199 // wxPropertyGridPageState
200 // -----------------------------------------------------------------------
201
202 wxPropertyGridPageState::wxPropertyGridPageState()
203 {
204 m_pPropGrid = NULL;
205 m_regularArray.SetParentState(this);
206 m_properties = &m_regularArray;
207 m_abcArray = NULL;
208 m_currentCategory = NULL;
209 m_selected = NULL;
210 m_width = 0;
211 m_virtualHeight = 0;
212 m_lastCaptionBottomnest = 1;
213 m_itemsAdded = 0;
214 m_anyModified = 0;
215 m_vhCalcPending = 0;
216 m_colWidths.push_back( wxPG_DEFAULT_SPLITTERX );
217 m_colWidths.push_back( wxPG_DEFAULT_SPLITTERX );
218 m_fSplitterX = wxPG_DEFAULT_SPLITTERX;
219 }
220
221 // -----------------------------------------------------------------------
222
223 wxPropertyGridPageState::~wxPropertyGridPageState()
224 {
225 delete m_abcArray;
226 }
227
228 // -----------------------------------------------------------------------
229
230 void wxPropertyGridPageState::InitNonCatMode()
231 {
232 if ( !m_abcArray )
233 {
234 m_abcArray = new wxPGRootProperty(wxS("<Root_NonCat>"));
235 m_abcArray->SetParentState(this);
236 m_abcArray->SetFlag(wxPG_PROP_CHILDREN_ARE_COPIES);
237 }
238
239 // Must be called when state::m_properties still points to regularArray.
240 wxPGProperty* oldProperties = m_properties;
241
242 // Must use temp value in state::m_properties for item iteration loop
243 // to run as expected.
244 m_properties = &m_regularArray;
245
246 if ( m_properties->GetChildCount() )
247 {
248 //
249 // Prepare m_abcArray
250 wxPropertyGridIterator it( this, wxPG_ITERATE_PROPERTIES );
251
252 for ( ; !it.AtEnd(); it.Next() )
253 {
254 wxPGProperty* p = it.GetProperty();
255 wxPGProperty* parent = p->GetParent();
256 if ( parent->IsCategory() || parent->IsRoot() )
257 {
258 m_abcArray->DoAddChild(p);
259 p->m_parent = &m_regularArray;
260 }
261 }
262 }
263
264 m_properties = oldProperties;
265 }
266
267 // -----------------------------------------------------------------------
268
269 void wxPropertyGridPageState::DoClear()
270 {
271 if ( m_pPropGrid && m_pPropGrid->GetState() == this )
272 {
273 m_pPropGrid->ClearSelection(false);
274 }
275 else
276 {
277 m_selected = NULL;
278 }
279
280 m_regularArray.Empty();
281 if ( m_abcArray )
282 m_abcArray->Empty();
283
284 m_dictName.clear();
285
286 m_currentCategory = NULL;
287 m_lastCaptionBottomnest = 1;
288 m_itemsAdded = 0;
289
290 m_virtualHeight = 0;
291 m_vhCalcPending = 0;
292 }
293
294 // -----------------------------------------------------------------------
295
296 void wxPropertyGridPageState::CalculateFontAndBitmapStuff( int WXUNUSED(vspacing) )
297 {
298 wxPropertyGrid* propGrid = GetGrid();
299
300 VirtualHeightChanged();
301
302 // Recalculate caption text extents.
303 unsigned int i;
304
305 for ( i=0;i<m_regularArray.GetChildCount();i++ )
306 {
307 wxPGProperty* p =m_regularArray.Item(i);
308
309 if ( p->IsCategory() )
310 ((wxPropertyCategory*)p)->CalculateTextExtent(propGrid, propGrid->GetCaptionFont());
311 }
312 }
313
314 // -----------------------------------------------------------------------
315
316 void wxPropertyGridPageState::SetVirtualWidth( int width )
317 {
318 wxASSERT( width >= 0 );
319 wxPropertyGrid* pg = GetGrid();
320 int gw = pg->GetClientSize().x;
321 if ( width < gw )
322 width = gw;
323
324 m_width = width;
325 }
326
327 // -----------------------------------------------------------------------
328
329 void wxPropertyGridPageState::OnClientWidthChange( int newWidth, int widthChange, bool fromOnResize )
330 {
331 wxPropertyGrid* pg = GetGrid();
332
333 if ( pg->HasVirtualWidth() )
334 {
335 if ( m_width < newWidth )
336 SetVirtualWidth( newWidth );
337
338 CheckColumnWidths(widthChange);
339 }
340 else
341 {
342 SetVirtualWidth( newWidth );
343
344 // This should be done before splitter auto centering
345 // NOTE: Splitter auto-centering is done in this function.
346 if ( !fromOnResize )
347 widthChange = 0;
348 CheckColumnWidths(widthChange);
349
350 if ( !(GetGrid()->GetInternalFlags() & wxPG_FL_SPLITTER_PRE_SET) &&
351 (GetGrid()->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER) )
352 {
353 long timeSinceCreation = (::wxGetLocalTimeMillis() - GetGrid()->m_timeCreated).ToLong();
354
355 // If too long, don't set splitter
356 if ( timeSinceCreation < 250 )
357 {
358 if ( m_properties->GetChildCount() )
359 {
360 SetSplitterLeft( false );
361 }
362 else
363 {
364 DoSetSplitterPosition( newWidth / 2 );
365 GetGrid()->ClearInternalFlag(wxPG_FL_SPLITTER_PRE_SET);
366 }
367 }
368 }
369 }
370 }
371
372 // -----------------------------------------------------------------------
373 // wxPropertyGridPageState item iteration methods
374 // -----------------------------------------------------------------------
375
376 wxPGProperty* wxPropertyGridPageState::GetLastItem( int flags )
377 {
378 if ( !m_properties->GetChildCount() )
379 return NULL;
380
381 wxPG_ITERATOR_CREATE_MASKS(flags, int itemExMask, int parentExMask)
382
383 // First, get last child of last parent
384 wxPGProperty* pwc = (wxPGProperty*)m_properties->Last();
385 while ( pwc->GetChildCount() &&
386 wxPG_ITERATOR_PARENTEXMASK_TEST(pwc, parentExMask) )
387 pwc = (wxPGProperty*) pwc->Last();
388
389 // Then, if it doesn't fit our criteria, back up until we find something that does
390 if ( pwc->GetFlags() & itemExMask )
391 {
392 wxPropertyGridIterator it( this, flags, pwc );
393 for ( ; !it.AtEnd(); it.Prev() )
394 ;
395 pwc = (wxPGProperty*) it.GetProperty();
396 }
397
398 return pwc;
399 }
400
401 wxPropertyCategory* wxPropertyGridPageState::GetPropertyCategory( const wxPGProperty* p ) const
402 {
403 const wxPGProperty* parent = (const wxPGProperty*)p;
404 const wxPGProperty* grandparent = (const wxPGProperty*)parent->GetParent();
405 do
406 {
407 parent = grandparent;
408 grandparent = (wxPGProperty*)parent->GetParent();
409 if ( parent->IsCategory() && grandparent )
410 return (wxPropertyCategory*)parent;
411 } while ( grandparent );
412
413 return NULL;
414 }
415
416 // -----------------------------------------------------------------------
417 // wxPropertyGridPageState GetPropertyXXX methods
418 // -----------------------------------------------------------------------
419
420 wxPGProperty* wxPropertyGridPageState::GetPropertyByLabel( const wxString& label,
421 wxPGProperty* parent ) const
422 {
423
424 size_t i;
425
426 if ( !parent ) parent = (wxPGProperty*) &m_regularArray;
427
428 for ( i=0; i<parent->GetChildCount(); i++ )
429 {
430 wxPGProperty* p = parent->Item(i);
431 if ( p->m_label == label )
432 return p;
433 // Check children recursively.
434 if ( p->GetChildCount() )
435 {
436 p = GetPropertyByLabel(label,(wxPGProperty*)p);
437 if ( p )
438 return p;
439 }
440 }
441
442 return NULL;
443 }
444
445 // -----------------------------------------------------------------------
446
447 wxPGProperty* wxPropertyGridPageState::BaseGetPropertyByName( const wxString& name ) const
448 {
449 wxPGHashMapS2P::const_iterator it;
450 it = m_dictName.find(name);
451 if ( it != m_dictName.end() )
452 return (wxPGProperty*) it->second;
453 return NULL;
454 }
455
456 // -----------------------------------------------------------------------
457
458 void wxPropertyGridPageState::DoSetPropertyName( wxPGProperty* p,
459 const wxString& newName )
460 {
461 wxCHECK_RET( p, wxT("invalid property id") );
462
463 wxPGProperty* parent = p->GetParent();
464
465 if ( parent->IsCategory() || parent->IsRoot() )
466 {
467 if ( p->GetBaseName().length() )
468 m_dictName.erase( p->GetBaseName() );
469 if ( newName.length() )
470 m_dictName[newName] = (void*) p;
471 }
472
473 p->DoSetName(newName);
474 }
475
476 // -----------------------------------------------------------------------
477 // wxPropertyGridPageState global operations
478 // -----------------------------------------------------------------------
479
480 // -----------------------------------------------------------------------
481 // Item iteration macros
482 // NB: Nowadays only needed for alphabetic/categoric mode switching.
483 // -----------------------------------------------------------------------
484
485 //#define II_INVALID_I 0x00FFFFFF
486
487 #define ITEM_ITERATION_VARIABLES \
488 wxPGProperty* parent; \
489 unsigned int i; \
490 unsigned int iMax;
491
492 #define ITEM_ITERATION_INIT_FROM_THE_TOP \
493 parent = m_properties; \
494 i = 0;
495
496 #if 0
497 #define ITEM_ITERATION_INIT(startparent, startindex, state) \
498 parent = startparent; \
499 i = (unsigned int)startindex; \
500 if ( parent == NULL ) \
501 { \
502 parent = state->m_properties; \
503 i = 0; \
504 }
505 #endif
506
507 #define ITEM_ITERATION_LOOP_BEGIN \
508 do \
509 { \
510 iMax = parent->GetChildCount(); \
511 while ( i < iMax ) \
512 { \
513 wxPGProperty* p = parent->Item(i);
514
515 #define ITEM_ITERATION_LOOP_END \
516 if ( p->GetChildCount() ) \
517 { \
518 i = 0; \
519 parent = (wxPGProperty*)p; \
520 iMax = parent->GetChildCount(); \
521 } \
522 else \
523 i++; \
524 } \
525 i = parent->m_arrIndex + 1; \
526 parent = parent->m_parent; \
527 } \
528 while ( parent != NULL );
529
530 bool wxPropertyGridPageState::EnableCategories( bool enable )
531 {
532 //
533 // NB: We can't use wxPropertyGridIterator in this
534 // function, since it depends on m_arrIndexes,
535 // which, among other things, is being fixed here.
536 //
537 ITEM_ITERATION_VARIABLES
538
539 if ( enable )
540 {
541 //
542 // Enable categories
543 //
544
545 if ( !IsInNonCatMode() )
546 return false;
547
548 m_properties = &m_regularArray;
549
550 // fix parents, indexes, and depths
551 ITEM_ITERATION_INIT_FROM_THE_TOP
552
553 ITEM_ITERATION_LOOP_BEGIN
554
555 p->m_arrIndex = i;
556
557 p->m_parent = parent;
558
559 // If parent was category, and this is not,
560 // then the depth stays the same.
561 if ( parent->IsCategory() &&
562 !p->IsCategory() )
563 p->m_depth = parent->m_depth;
564 else
565 p->m_depth = parent->m_depth + 1;
566
567 ITEM_ITERATION_LOOP_END
568
569 }
570 else
571 {
572 //
573 // Disable categories
574 //
575
576 if ( IsInNonCatMode() )
577 return false;
578
579 // Create array, if necessary.
580 if ( !m_abcArray )
581 InitNonCatMode();
582
583 m_properties = m_abcArray;
584
585 // fix parents, indexes, and depths
586 ITEM_ITERATION_INIT_FROM_THE_TOP
587
588 ITEM_ITERATION_LOOP_BEGIN
589
590 p->m_arrIndex = i;
591
592 p->m_parent = parent;
593
594 p->m_depth = parent->m_depth + 1;
595
596 ITEM_ITERATION_LOOP_END
597 }
598
599 VirtualHeightChanged();
600
601 if ( m_pPropGrid->GetState() == this )
602 m_pPropGrid->RecalculateVirtualSize();
603
604 return true;
605 }
606
607 // -----------------------------------------------------------------------
608
609 #if wxUSE_STL
610 #include <algorithm>
611
612 static bool wxPG_SortFunc_ByFunction(wxPGProperty *p1, wxPGProperty *p2)
613 {
614 wxPropertyGrid* pg = p1->GetGrid();
615 wxPGSortCallback sortFunction = pg->GetSortFunction();
616 return sortFunction(pg, p1, p2) < 0;
617 }
618
619 static bool wxPG_SortFunc_ByLabel(wxPGProperty *p1, wxPGProperty *p2)
620 {
621 return p1->GetLabel().CmpNoCase( p2->GetLabel() ) < 0;
622 }
623
624 #else
625
626 static int wxPG_SortFunc_ByFunction(wxPGProperty **pp1, wxPGProperty **pp2)
627 {
628 wxPGProperty *p1 = *pp1;
629 wxPGProperty *p2 = *pp2;
630 wxPropertyGrid* pg = p1->GetGrid();
631 wxPGSortCallback sortFunction = pg->GetSortFunction();
632 return sortFunction(pg, p1, p2);
633 }
634
635 static int wxPG_SortFunc_ByLabel(wxPGProperty **pp1, wxPGProperty **pp2)
636 {
637 wxPGProperty *p1 = *pp1;
638 wxPGProperty *p2 = *pp2;
639 return p1->GetLabel().CmpNoCase( p2->GetLabel() );
640 }
641
642 #endif
643
644 void wxPropertyGridPageState::DoSortChildren( wxPGProperty* p,
645 int flags )
646 {
647 if ( !p )
648 p = m_properties;
649
650 // Can only sort items with children
651 if ( !p->GetChildCount() )
652 return;
653
654 // Never sort children of aggregate properties
655 if ( p->HasFlag(wxPG_PROP_AGGREGATE) )
656 return;
657
658 if ( (flags & wxPG_SORT_TOP_LEVEL_ONLY)
659 && !p->IsCategory() && !p->IsRoot() )
660 return;
661
662 #if wxUSE_STL
663 if ( GetGrid()->GetSortFunction() )
664 std::sort(p->m_children.begin(), p->m_children.end(),
665 wxPG_SortFunc_ByFunction);
666 else
667 std::sort(p->m_children.begin(), p->m_children.end(),
668 wxPG_SortFunc_ByLabel);
669 #else
670 if ( GetGrid()->GetSortFunction() )
671 p->m_children.Sort( wxPG_SortFunc_ByFunction );
672 else
673 p->m_children.Sort( wxPG_SortFunc_ByLabel );
674 #endif
675
676 // Fix indices
677 p->FixIndicesOfChildren();
678
679 if ( flags & wxPG_RECURSE )
680 {
681 // Apply sort recursively
682 for ( unsigned int i=0; i<p->GetChildCount(); i++ )
683 DoSortChildren(p->Item(i), flags);
684 }
685 }
686
687 // -----------------------------------------------------------------------
688
689 void wxPropertyGridPageState::DoSort( int flags )
690 {
691 DoSortChildren( m_properties, flags | wxPG_RECURSE );
692
693 // We used to sort categories as well here also if in non-categorized
694 // mode, but doing would naturally cause child indices to become
695 // corrupted.
696 }
697
698 // -----------------------------------------------------------------------
699
700 bool wxPropertyGridPageState::PrepareAfterItemsAdded()
701 {
702 if ( !m_itemsAdded ) return false;
703
704 wxPropertyGrid* pg = GetGrid();
705
706 m_itemsAdded = 0;
707
708 if ( pg->HasFlag(wxPG_AUTO_SORT) )
709 DoSort(wxPG_SORT_TOP_LEVEL_ONLY);
710
711 return true;
712 }
713
714 // -----------------------------------------------------------------------
715 // wxPropertyGridPageState splitter, column and hittest functions
716 // -----------------------------------------------------------------------
717
718 wxPGProperty* wxPropertyGridPageState::DoGetItemAtY( int y ) const
719 {
720 // Outside?
721 if ( y < 0 )
722 return NULL;
723
724 unsigned int a = 0;
725 return m_properties->GetItemAtY(y, GetGrid()->m_lineHeight, &a);
726 }
727
728 // -----------------------------------------------------------------------
729
730 wxPropertyGridHitTestResult wxPropertyGridPageState::HitTest( const wxPoint&pt ) const
731 {
732 wxPropertyGridHitTestResult result;
733 result.column = HitTestH( pt.x, &result.splitter, &result.splitterHitOffset );
734 result.property = DoGetItemAtY( pt.y );
735 return result;
736 }
737
738 // -----------------------------------------------------------------------
739
740 // Used by SetSplitterLeft() and DotFitColumns()
741 int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC& dc,
742 wxPGProperty* pwc,
743 unsigned int col,
744 bool subProps) const
745 {
746 wxPropertyGrid* pg = m_pPropGrid;
747 size_t i;
748 int maxW = 0;
749 int w, h;
750
751 for ( i=0; i<pwc->GetChildCount(); i++ )
752 {
753 wxPGProperty* p = pwc->Item(i);
754 if ( !p->IsCategory() )
755 {
756 const wxPGCell* cell = NULL;
757 wxString text;
758 p->GetDisplayInfo(col, -1, 0, &text, &cell);
759 dc.GetTextExtent(text, &w, &h);
760 if ( col == 0 )
761 w += ( ((int)p->m_depth-1) * pg->m_subgroup_extramargin );
762
763 //
764 // TODO: Add bitmap support.
765
766 w += (wxPG_XBEFORETEXT*2);
767
768 if ( w > maxW )
769 maxW = w;
770 }
771
772 if ( p->GetChildCount() &&
773 ( subProps || p->IsCategory() ) )
774 {
775 w = GetColumnFitWidth( dc, p, col, subProps );
776
777 if ( w > maxW )
778 maxW = w;
779 }
780 }
781
782 return maxW;
783 }
784
785 int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn ) const
786 {
787 int n = GetGrid()->m_marginWidth;
788 int i;
789 for ( i=0; i<=splitterColumn; i++ )
790 n += m_colWidths[i];
791 return n;
792 }
793
794 int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column) ) const
795 {
796 return wxPG_DRAG_MARGIN;
797 }
798
799 void wxPropertyGridPageState::PropagateColSizeDec( int column, int decrease, int dir )
800 {
801 int origWidth = m_colWidths[column];
802 m_colWidths[column] -= decrease;
803 int min = GetColumnMinWidth(column);
804 int more = 0;
805 if ( m_colWidths[column] < min )
806 {
807 more = decrease - (origWidth - min);
808 m_colWidths[column] = min;
809 }
810
811 //
812 // FIXME: Causes erratic splitter changing, so as a workaround
813 // disabled if two or less columns.
814
815 if ( m_colWidths.size() <= 2 )
816 return;
817
818 column += dir;
819 if ( more && column < (int)m_colWidths.size() && column >= 0 )
820 PropagateColSizeDec( column, more, dir );
821 }
822
823 void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos, int splitterColumn, bool WXUNUSED(allPages), bool fromAutoCenter )
824 {
825 wxPropertyGrid* pg = GetGrid();
826
827 int adjust = newXPos - DoGetSplitterPosition(splitterColumn);
828
829 if ( !pg->HasVirtualWidth() )
830 {
831 // No virtual width
832 int otherColumn;
833 if ( adjust > 0 )
834 {
835 otherColumn = splitterColumn + 1;
836 if ( otherColumn == (int)m_colWidths.size() )
837 otherColumn = 0;
838 m_colWidths[splitterColumn] += adjust;
839 PropagateColSizeDec( otherColumn, adjust, 1 );
840 }
841 else
842 {
843 otherColumn = splitterColumn + 1;
844 if ( otherColumn == (int)m_colWidths.size() )
845 otherColumn = 0;
846 m_colWidths[otherColumn] -= adjust;
847 PropagateColSizeDec( splitterColumn, -adjust, -1 );
848 }
849 }
850 else
851 {
852 m_colWidths[splitterColumn] += adjust;
853 }
854
855 if ( splitterColumn == 0 )
856 m_fSplitterX = (double) newXPos;
857
858 if ( !fromAutoCenter )
859 {
860 // Don't allow initial splitter auto-positioning after this.
861 if ( pg->GetState() == this )
862 pg->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET);
863
864 CheckColumnWidths();
865 }
866 }
867
868 // Moves splitter so that all labels are visible, but just.
869 void wxPropertyGridPageState::SetSplitterLeft( bool subProps )
870 {
871 wxPropertyGrid* pg = GetGrid();
872 wxClientDC dc(pg);
873 dc.SetFont(pg->GetFont());
874
875 int maxW = GetColumnFitWidth(dc, m_properties, 0, subProps);
876
877 if ( maxW > 0 )
878 {
879 maxW += pg->m_marginWidth;
880 DoSetSplitterPosition( maxW );
881 }
882
883 pg->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
884 }
885
886 wxSize wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize) )
887 {
888 wxPropertyGrid* pg = GetGrid();
889 wxClientDC dc(pg);
890 dc.SetFont(pg->GetFont());
891
892 int marginWidth = pg->m_marginWidth;
893 int accWid = marginWidth;
894 int maxColWidth = 500;
895
896 for ( unsigned int col=0; col < GetColumnCount(); col++ )
897 {
898 int fitWid = GetColumnFitWidth(dc, m_properties, col, true);
899 int colMinWidth = GetColumnMinWidth(col);
900 if ( fitWid < colMinWidth )
901 fitWid = colMinWidth;
902 else if ( fitWid > maxColWidth )
903 fitWid = maxColWidth;
904
905 m_colWidths[col] = fitWid;
906
907 accWid += fitWid;
908 }
909
910 // Expand last one to fill the width
911 int remaining = m_width - accWid;
912 m_colWidths[GetColumnCount()-1] += remaining;
913
914 pg->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
915
916 int firstSplitterX = marginWidth + m_colWidths[0];
917 m_fSplitterX = (double) firstSplitterX;
918
919 // Don't allow initial splitter auto-positioning after this.
920 if ( pg->GetState() == this )
921 {
922 pg->SetSplitterPosition(firstSplitterX, false);
923 pg->Refresh();
924 }
925
926 int x, y;
927 pg->GetVirtualSize(&x, &y);
928
929 return wxSize(accWid, y);
930 }
931
932 void wxPropertyGridPageState::CheckColumnWidths( int widthChange )
933 {
934 if ( m_width == 0 )
935 return;
936
937 wxPropertyGrid* pg = GetGrid();
938
939 unsigned int i;
940 unsigned int lastColumn = m_colWidths.size() - 1;
941 int width = m_width;
942 int clientWidth = pg->GetClientSize().x;
943
944 //
945 // Column to reduce, if needed. Take last one that exceeds minimum width.
946 int reduceCol = -1;
947
948 wxLogTrace("propgrid",
949 wxS("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"),
950 width, clientWidth);
951
952 //
953 // Check min sizes
954 for ( i=0; i<m_colWidths.size(); i++ )
955 {
956 int min = GetColumnMinWidth(i);
957 if ( m_colWidths[i] <= min )
958 {
959 m_colWidths[i] = min;
960 }
961 else
962 {
963 // Always reduce the last column that is larger than minimum size
964 // (looks nicer, even with auto-centering enabled).
965 reduceCol = i;
966 }
967 }
968
969 int colsWidth = pg->m_marginWidth;
970 for ( i=0; i<m_colWidths.size(); i++ )
971 colsWidth += m_colWidths[i];
972
973 wxLogTrace("propgrid",
974 wxS(" HasVirtualWidth: %i colsWidth: %i"),
975 (int)pg->HasVirtualWidth(), colsWidth);
976
977 // Then mode-based requirement
978 if ( !pg->HasVirtualWidth() )
979 {
980 int widthHigher = width - colsWidth;
981
982 // Adapt colsWidth to width
983 if ( colsWidth < width )
984 {
985 // Increase column
986 wxLogTrace("propgrid",
987 wxS(" Adjust last column to %i"),
988 m_colWidths[lastColumn] + widthHigher);
989 m_colWidths[lastColumn] = m_colWidths[lastColumn] + widthHigher;
990 }
991 else if ( colsWidth > width )
992 {
993 // Reduce column
994 if ( reduceCol != -1 )
995 {
996 wxLogTrace("propgrid",
997 wxT(" Reduce column %i (by %i)"),
998 reduceCol, -widthHigher);
999
1000 // Reduce widest column, and recheck
1001 m_colWidths[reduceCol] = m_colWidths[reduceCol] + widthHigher;
1002 CheckColumnWidths();
1003 }
1004 }
1005 }
1006 else
1007 {
1008 // Only check colsWidth against clientWidth
1009 if ( colsWidth < clientWidth )
1010 {
1011 m_colWidths[lastColumn] = m_colWidths[lastColumn] + (clientWidth-colsWidth);
1012 }
1013
1014 m_width = colsWidth;
1015
1016 // If width changed, recalculate virtual size
1017 if ( pg->GetState() == this )
1018 pg->RecalculateVirtualSize();
1019 }
1020
1021 for ( i=0; i<m_colWidths.size(); i++ )
1022 {
1023 wxLogTrace("propgrid", wxS("col%i: %i"), i, m_colWidths[i]);
1024 }
1025
1026 // Auto center splitter
1027 if ( !(pg->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER) &&
1028 m_colWidths.size() == 2 )
1029 {
1030 float centerX = (float)(pg->m_width/2);
1031 float splitterX;
1032
1033 if ( m_fSplitterX < 0.0 )
1034 {
1035 splitterX = centerX;
1036 }
1037 else if ( widthChange )
1038 {
1039 //float centerX = float(pg->GetSize().x) * 0.5;
1040
1041 // Recenter?
1042 splitterX = m_fSplitterX + (float(widthChange) * 0.5);
1043 float deviation = fabs(centerX - splitterX);
1044
1045 // If deviating from center, adjust towards it
1046 if ( deviation > 20.0 )
1047 {
1048 if ( splitterX > centerX)
1049 splitterX -= 2;
1050 else
1051 splitterX += 2;
1052 }
1053 }
1054 else
1055 {
1056 // No width change, just keep sure we keep splitter position intact
1057 splitterX = m_fSplitterX;
1058 float deviation = fabs(centerX - splitterX);
1059 if ( deviation > 50.0 )
1060 {
1061 splitterX = centerX;
1062 }
1063 }
1064
1065 DoSetSplitterPosition((int)splitterX, 0, false, true);
1066
1067 m_fSplitterX = splitterX; // needed to retain accuracy
1068 }
1069 }
1070
1071 void wxPropertyGridPageState::SetColumnCount( int colCount )
1072 {
1073 wxASSERT( colCount >= 2 );
1074 m_colWidths.SetCount( colCount, wxPG_DRAG_MARGIN );
1075 if ( m_colWidths.size() > (unsigned int)colCount )
1076 m_colWidths.RemoveAt( m_colWidths.size(), m_colWidths.size() - colCount );
1077
1078 if ( m_pPropGrid->GetState() == this )
1079 m_pPropGrid->RecalculateVirtualSize();
1080 else
1081 CheckColumnWidths();
1082 }
1083
1084 // Returns column index, -1 for margin
1085 int wxPropertyGridPageState::HitTestH( int x, int* pSplitterHit, int* pSplitterHitOffset ) const
1086 {
1087 int cx = GetGrid()->m_marginWidth;
1088 int col = -1;
1089 int prevSplitter = -1;
1090
1091 while ( x > cx )
1092 {
1093 col++;
1094 if ( col >= (int)m_colWidths.size() )
1095 {
1096 *pSplitterHit = -1;
1097 return col;
1098 }
1099 prevSplitter = cx;
1100 cx += m_colWidths[col];
1101 }
1102
1103 // Near prev. splitter
1104 if ( col >= 1 )
1105 {
1106 int diff = x - prevSplitter;
1107 if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 )
1108 {
1109 *pSplitterHit = col - 1;
1110 *pSplitterHitOffset = diff;
1111 return col;
1112 }
1113 }
1114
1115 // Near next splitter
1116 int nextSplitter = cx;
1117 if ( col < (int)(m_colWidths.size()-1) )
1118 {
1119 int diff = x - nextSplitter;
1120 if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 )
1121 {
1122 *pSplitterHit = col;
1123 *pSplitterHitOffset = diff;
1124 return col;
1125 }
1126 }
1127
1128 *pSplitterHit = -1;
1129 return col;
1130 }
1131
1132 // -----------------------------------------------------------------------
1133 // wxPropertyGridPageState property value setting and getting
1134 // -----------------------------------------------------------------------
1135
1136 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty* p, const wxString& value )
1137 {
1138 if ( p )
1139 {
1140 int flags = wxPG_REPORT_ERROR|wxPG_FULL_VALUE|wxPG_PROGRAMMATIC_VALUE;
1141
1142 wxVariant variant = p->GetValueRef();
1143 bool res;
1144
1145 if ( p->GetMaxLength() <= 0 )
1146 res = p->StringToValue( variant, value, flags );
1147 else
1148 res = p->StringToValue( variant, value.Mid(0,p->GetMaxLength()), flags );
1149
1150 if ( res )
1151 {
1152 p->SetValue(variant);
1153 if ( m_selected==p && this==m_pPropGrid->GetState() )
1154 m_pPropGrid->RefreshEditor();
1155 }
1156
1157 return true;
1158 }
1159 return false;
1160 }
1161
1162 // -----------------------------------------------------------------------
1163
1164 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty* p, wxVariant& value )
1165 {
1166 if ( p )
1167 {
1168 p->SetValue(value);
1169 if ( m_selected==p && this==m_pPropGrid->GetState() )
1170 m_pPropGrid->RefreshEditor();
1171
1172 return true;
1173 }
1174 return false;
1175 }
1176
1177 // -----------------------------------------------------------------------
1178
1179 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty* p, wxObject* value )
1180 {
1181 if ( p )
1182 {
1183 // wnd_primary has to be given so the control can be updated as well.
1184 wxVariant v(value);
1185 DoSetPropertyValue(p, v);
1186 return true;
1187 }
1188 return false;
1189 }
1190
1191 // -----------------------------------------------------------------------
1192 // wxPropertyGridPageState property operations
1193 // -----------------------------------------------------------------------
1194
1195 bool wxPropertyGridPageState::DoCollapse( wxPGProperty* p )
1196 {
1197 wxCHECK_MSG( p, false, wxT("invalid property id") );
1198
1199 if ( !p->GetChildCount() ) return false;
1200
1201 if ( !p->IsExpanded() ) return false;
1202
1203 p->SetExpanded(false);
1204
1205 VirtualHeightChanged();
1206
1207 return true;
1208 }
1209
1210 // -----------------------------------------------------------------------
1211
1212 bool wxPropertyGridPageState::DoExpand( wxPGProperty* p )
1213 {
1214 wxCHECK_MSG( p, false, wxT("invalid property id") );
1215
1216 if ( !p->GetChildCount() ) return false;
1217
1218 if ( p->IsExpanded() ) return false;
1219
1220 p->SetExpanded(true);
1221
1222 VirtualHeightChanged();
1223
1224 return true;
1225 }
1226
1227 // -----------------------------------------------------------------------
1228
1229 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty* p, unsigned int flags )
1230 {
1231 if ( this == m_pPropGrid->GetState() )
1232 return m_pPropGrid->DoSelectProperty( p, flags );
1233
1234 m_selected = p;
1235 return true;
1236 }
1237
1238 // -----------------------------------------------------------------------
1239
1240 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty* p, bool hide, int flags )
1241 {
1242 if ( !hide )
1243 p->ClearFlag( wxPG_PROP_HIDDEN );
1244 else
1245 p->SetFlag( wxPG_PROP_HIDDEN );
1246
1247 if ( flags & wxPG_RECURSE )
1248 {
1249 unsigned int i;
1250 for ( i = 0; i < p->GetChildCount(); i++ )
1251 DoHideProperty(p->Item(i), hide, flags | wxPG_RECURSE_STARTS);
1252 }
1253
1254 VirtualHeightChanged();
1255
1256 return true;
1257 }
1258
1259 // -----------------------------------------------------------------------
1260
1261 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty* p, bool enable )
1262 {
1263 if ( p )
1264 {
1265 if ( enable )
1266 {
1267 if ( !(p->m_flags & wxPG_PROP_DISABLED) )
1268 return false;
1269
1270 // Enabling
1271
1272 p->m_flags &= ~(wxPG_PROP_DISABLED);
1273 }
1274 else
1275 {
1276 if ( p->m_flags & wxPG_PROP_DISABLED )
1277 return false;
1278
1279 // Disabling
1280
1281 p->m_flags |= wxPG_PROP_DISABLED;
1282
1283 }
1284
1285 // Apply same to sub-properties as well
1286 unsigned int i;
1287 for ( i = 0; i < p->GetChildCount(); i++ )
1288 DoEnableProperty( p->Item(i), enable );
1289
1290 return true;
1291 }
1292 return false;
1293 }
1294
1295 // -----------------------------------------------------------------------
1296 // wxPropertyGridPageState wxVariant related routines
1297 // -----------------------------------------------------------------------
1298
1299 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1300 // Never includes sub-properties (unless they are parented by wxParentProperty).
1301 wxVariant wxPropertyGridPageState::DoGetPropertyValues( const wxString& listname,
1302 wxPGProperty* baseparent,
1303 long flags ) const
1304 {
1305 wxPGProperty* pwc = (wxPGProperty*) baseparent;
1306
1307 // Root is the default base-parent.
1308 if ( !pwc )
1309 pwc = m_properties;
1310
1311 wxVariantList tempList;
1312 wxVariant v( tempList, listname );
1313
1314 if ( pwc->GetChildCount() )
1315 {
1316 if ( flags & wxPG_KEEP_STRUCTURE )
1317 {
1318 wxASSERT( !pwc->HasFlag(wxPG_PROP_AGGREGATE) );
1319
1320 size_t i;
1321 for ( i=0; i<pwc->GetChildCount(); i++ )
1322 {
1323 wxPGProperty* p = pwc->Item(i);
1324 if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) )
1325 {
1326 wxVariant variant = p->GetValue();
1327 variant.SetName( p->GetBaseName() );
1328 v.Append( variant );
1329 }
1330 else
1331 {
1332 v.Append( DoGetPropertyValues(p->m_name,p,flags|wxPG_KEEP_STRUCTURE) );
1333 }
1334 if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() )
1335 v.Append( p->GetAttributesAsList() );
1336 }
1337 }
1338 else
1339 {
1340 wxPropertyGridConstIterator it( this, wxPG_ITERATE_DEFAULT, pwc->Item(0) );
1341 it.SetBaseParent( pwc );
1342
1343 for ( ; !it.AtEnd(); it.Next() )
1344 {
1345 const wxPGProperty* p = it.GetProperty();
1346
1347 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1348 if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) )
1349 {
1350 wxVariant variant = p->GetValue();
1351 variant.SetName( p->GetName() );
1352 v.Append( variant );
1353 if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() )
1354 v.Append( p->GetAttributesAsList() );
1355 }
1356 }
1357 }
1358 }
1359
1360 return v;
1361 }
1362
1363 // -----------------------------------------------------------------------
1364
1365 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList& list, wxPGProperty* defaultCategory )
1366 {
1367 unsigned char origFrozen = 1;
1368
1369 if ( m_pPropGrid->GetState() == this )
1370 {
1371 origFrozen = m_pPropGrid->m_frozen;
1372 if ( !origFrozen ) m_pPropGrid->Freeze();
1373 }
1374
1375 wxPropertyCategory* use_category = (wxPropertyCategory*)defaultCategory;
1376
1377 if ( !use_category )
1378 use_category = (wxPropertyCategory*)m_properties;
1379
1380 // Let's iterate over the list of variants.
1381 wxVariantList::const_iterator node;
1382 int numSpecialEntries = 0;
1383
1384 //
1385 // Second pass for special entries
1386 for ( node = list.begin(); node != list.end(); ++node )
1387 {
1388 wxVariant *current = (wxVariant*)*node;
1389
1390 // Make sure it is wxVariant.
1391 wxASSERT( current );
1392 wxASSERT( wxStrcmp(current->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1393
1394 const wxString& name = current->GetName();
1395 if ( name.length() > 0 )
1396 {
1397 //
1398 // '@' signified a special entry
1399 if ( name[0] == wxS('@') )
1400 {
1401 numSpecialEntries++;
1402 }
1403 else
1404 {
1405 wxPGProperty* foundProp = BaseGetPropertyByName(name);
1406 if ( foundProp )
1407 {
1408 wxPGProperty* p = foundProp;
1409
1410 // If it was a list, we still have to go through it.
1411 if ( wxStrcmp(current->GetType(), wxS("list")) == 0 )
1412 {
1413 DoSetPropertyValues( current->GetList(),
1414 p->IsCategory()?p:(NULL)
1415 );
1416 }
1417 else
1418 {
1419 wxASSERT_LEVEL_2_MSG(
1420 wxStrcmp(current->GetType(), p->GetValue().GetType()) == 0,
1421 wxString::Format(
1422 wxS("setting value of property \"%s\" from variant"),
1423 p->GetName().c_str())
1424 );
1425
1426 p->SetValue(*current);
1427 }
1428 }
1429 else
1430 {
1431 // Is it list?
1432 if ( current->GetType() != wxS("list") )
1433 {
1434 // Not.
1435 }
1436 else
1437 {
1438 // Yes, it is; create a sub category and append contents there.
1439 wxPGProperty* newCat = DoInsert(use_category,-1,new wxPropertyCategory(current->GetName(),wxPG_LABEL));
1440 DoSetPropertyValues( current->GetList(), newCat );
1441 }
1442 }
1443 }
1444 }
1445 }
1446
1447 if ( numSpecialEntries )
1448 {
1449 for ( node = list.begin(); node != list.end(); ++node )
1450 {
1451 wxVariant *current = (wxVariant*)*node;
1452
1453 const wxString& name = current->GetName();
1454 if ( name.length() > 0 )
1455 {
1456 //
1457 // '@' signified a special entry
1458 if ( name[0] == wxS('@') )
1459 {
1460 numSpecialEntries--;
1461
1462 size_t pos2 = name.rfind(wxS('@'));
1463 if ( pos2 > 0 && pos2 < (name.size()-1) )
1464 {
1465 wxString propName = name.substr(1, pos2-1);
1466 wxString entryType = name.substr(pos2+1, wxString::npos);
1467
1468 if ( entryType == wxS("attr") )
1469 {
1470 //
1471 // List of attributes
1472 wxPGProperty* foundProp = BaseGetPropertyByName(propName);
1473 if ( foundProp )
1474 {
1475 wxASSERT( current->GetType() == wxPG_VARIANT_TYPE_LIST );
1476
1477 wxVariantList& list2 = current->GetList();
1478 wxVariantList::const_iterator node2;
1479
1480 for ( node2 = list2.begin(); node2 != list2.end(); ++node2 )
1481 {
1482 wxVariant *attr = (wxVariant*)*node2;
1483 foundProp->SetAttribute( attr->GetName(), *attr );
1484 }
1485 }
1486 else
1487 {
1488 // ERROR: No such property: 'propName'
1489 }
1490 }
1491 }
1492 else
1493 {
1494 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1495 }
1496 }
1497 }
1498
1499 if ( !numSpecialEntries )
1500 break;
1501 }
1502 }
1503
1504 if ( !origFrozen )
1505 {
1506 m_pPropGrid->Thaw();
1507
1508 if ( this == m_pPropGrid->GetState() )
1509 m_pPropGrid->RefreshEditor();
1510 }
1511
1512 }
1513
1514 // -----------------------------------------------------------------------
1515 // wxPropertyGridPageState property adding and removal
1516 // -----------------------------------------------------------------------
1517
1518 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty* property,
1519 wxPGProperty* scheduledParent )
1520 {
1521 wxPropertyGrid* propGrid = m_pPropGrid;
1522
1523 // This will allow better behavior.
1524 if ( scheduledParent == m_properties )
1525 scheduledParent = NULL;
1526
1527 if ( scheduledParent && !scheduledParent->IsCategory() )
1528 {
1529 wxASSERT_MSG( property->GetBaseName().length(),
1530 "Property's children must have unique, non-empty names within their scope" );
1531 }
1532
1533 property->m_parentState = this;
1534
1535 if ( property->IsCategory() )
1536 {
1537
1538 // Parent of a category must be either root or another category
1539 // (otherwise Bad Things might happen).
1540 wxASSERT_MSG( scheduledParent == NULL ||
1541 scheduledParent == m_properties ||
1542 scheduledParent->IsCategory(),
1543 wxT("Parent of a category must be either root or another category."));
1544
1545 // If we already have category with same name, delete given property
1546 // and use it instead as most recent caption item.
1547 wxPGProperty* found_id = BaseGetPropertyByName( property->GetBaseName() );
1548 if ( found_id )
1549 {
1550 wxPropertyCategory* pwc = (wxPropertyCategory*) found_id;
1551 if ( pwc->IsCategory() ) // Must be a category.
1552 {
1553 delete property;
1554 m_currentCategory = pwc;
1555 return false;
1556 }
1557 }
1558 }
1559
1560 #if wxDEBUG_LEVEL
1561 // Warn for identical names in debug mode.
1562 if ( BaseGetPropertyByName(property->GetName()) &&
1563 (!scheduledParent || scheduledParent->IsCategory()) )
1564 {
1565 wxFAIL_MSG(wxString::Format(
1566 "wxPropertyGrid item with name \"%s\" already exists",
1567 property->GetName()));
1568
1569 wxPGGlobalVars->m_warnings++;
1570 }
1571 #endif // wxDEBUG_LEVEL
1572
1573 // Make sure nothing is selected.
1574 if ( propGrid )
1575 propGrid->ClearSelection(false);
1576
1577 // NULL parent == root parent
1578 if ( !scheduledParent )
1579 scheduledParent = DoGetRoot();
1580
1581 property->m_parent = scheduledParent;
1582
1583 property->InitAfterAdded(this, propGrid);
1584
1585 if ( property->IsCategory() )
1586 {
1587 wxPropertyCategory* pc = wxStaticCast(property, wxPropertyCategory);
1588
1589 m_currentCategory = pc;
1590
1591 // Calculate text extent for category caption
1592 if ( propGrid )
1593 pc->CalculateTextExtent(propGrid, propGrid->GetCaptionFont());
1594 }
1595
1596 return true;
1597 }
1598
1599 // -----------------------------------------------------------------------
1600
1601 wxPGProperty* wxPropertyGridPageState::DoAppend( wxPGProperty* property )
1602 {
1603 wxPropertyCategory* cur_cat = m_currentCategory;
1604 if ( property->IsCategory() )
1605 cur_cat = NULL;
1606
1607 return DoInsert( cur_cat, -1, property );
1608 }
1609
1610 // -----------------------------------------------------------------------
1611
1612 wxPGProperty* wxPropertyGridPageState::DoInsert( wxPGProperty* parent, int index, wxPGProperty* property )
1613 {
1614 if ( !parent )
1615 parent = m_properties;
1616
1617 wxCHECK_MSG( !parent->HasFlag(wxPG_PROP_AGGREGATE),
1618 wxNullProperty,
1619 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1620
1621 bool res = PrepareToAddItem( property, (wxPropertyCategory*)parent );
1622
1623 // PrepareToAddItem() may just decide to use use current category
1624 // instead of adding new one.
1625 if ( !res )
1626 return m_currentCategory;
1627
1628 bool parentIsRoot = parent->IsRoot();
1629 bool parentIsCategory = parent->IsCategory();
1630
1631 // Note that item must be added into current mode later.
1632
1633 // If parent is wxParentProperty, just stick it in...
1634 // If parent is root (m_properties), then...
1635 // In categoric mode: Add as last item in m_abcArray (if not category).
1636 // Add to given index in m_regularArray.
1637 // In non-cat mode: Add as last item in m_regularArray.
1638 // Add to given index in m_abcArray.
1639 // If parent is category, then...
1640 // 1) Add to given category in given index.
1641 // 2) Add as last item in m_abcArray.
1642
1643 if ( m_properties == &m_regularArray )
1644 {
1645 // We are currently in Categorized mode
1646
1647 // Only add non-categories to m_abcArray.
1648 if ( m_abcArray && !property->IsCategory() &&
1649 (parentIsCategory || parentIsRoot) )
1650 {
1651 m_abcArray->DoAddChild( property, -1, false );
1652 }
1653
1654 // Add to current mode.
1655 parent->DoAddChild( property, index, true );
1656 }
1657 else
1658 {
1659 // We are currently in Non-categorized/Alphabetic mode
1660
1661 if ( parentIsCategory )
1662 // Parent is category.
1663 parent->DoAddChild( property, index, false );
1664 else if ( parentIsRoot )
1665 // Parent is root.
1666 m_regularArray.DoAddChild( property, -1, false );
1667
1668 // Add to current mode
1669 if ( !property->IsCategory() )
1670 m_abcArray->DoAddChild( property, index, true );
1671 }
1672
1673 // category stuff
1674 if ( property->IsCategory() )
1675 {
1676 // This is a category caption item.
1677
1678 // Last caption is not the bottom one (this info required by append)
1679 m_lastCaptionBottomnest = 0;
1680 }
1681
1682 // Only add name to hashmap if parent is root or category
1683 if ( property->m_name.length() &&
1684 (parentIsCategory || parentIsRoot) )
1685 m_dictName[property->m_name] = (void*) property;
1686
1687 VirtualHeightChanged();
1688
1689 property->UpdateParentValues();
1690
1691 m_itemsAdded = 1;
1692
1693 return property;
1694 }
1695
1696 // -----------------------------------------------------------------------
1697
1698 void wxPropertyGridPageState::DoDelete( wxPGProperty* item, bool doDelete )
1699 {
1700 wxCHECK_RET( item->GetParent(),
1701 wxT("this property was already deleted") );
1702
1703 wxCHECK_RET( item != &m_regularArray && item != m_abcArray,
1704 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1705
1706 unsigned int indinparent = item->GetIndexInParent();
1707
1708 wxPGProperty* pwc = (wxPGProperty*)item;
1709 wxPGProperty* parent = item->GetParent();
1710
1711 wxCHECK_RET( !parent->HasFlag(wxPG_PROP_AGGREGATE),
1712 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1713
1714 // Delete children
1715 if ( item->GetChildCount() && !item->HasFlag(wxPG_PROP_AGGREGATE) )
1716 {
1717 // deleting a category
1718 if ( item->IsCategory() )
1719 {
1720 if ( pwc == m_currentCategory )
1721 m_currentCategory = NULL;
1722 }
1723
1724 item->DeleteChildren();
1725 }
1726
1727 if ( !IsInNonCatMode() )
1728 {
1729 // categorized mode - non-categorized array
1730
1731 // Remove from non-cat array
1732 if ( !item->IsCategory() &&
1733 (parent->IsCategory() || parent->IsRoot()) )
1734 {
1735 if ( m_abcArray )
1736 m_abcArray->RemoveChild(item);
1737 }
1738
1739 // categorized mode - categorized array
1740 wxArrayPGProperty& parentsChildren = parent->m_children;
1741 parentsChildren.erase( parentsChildren.begin() + indinparent );
1742 item->m_parent->FixIndicesOfChildren();
1743 }
1744 else
1745 {
1746 // non-categorized mode - categorized array
1747
1748 // We need to find location of item.
1749 wxPGProperty* cat_parent = &m_regularArray;
1750 int cat_index = m_regularArray.GetChildCount();
1751 size_t i;
1752 for ( i = 0; i < m_regularArray.GetChildCount(); i++ )
1753 {
1754 wxPGProperty* p = m_regularArray.Item(i);
1755 if ( p == item ) { cat_index = i; break; }
1756 if ( p->IsCategory() )
1757 {
1758 int subind = ((wxPGProperty*)p)->Index(item);
1759 if ( subind != wxNOT_FOUND )
1760 {
1761 cat_parent = ((wxPGProperty*)p);
1762 cat_index = subind;
1763 break;
1764 }
1765 }
1766 }
1767 cat_parent->m_children.erase(cat_parent->m_children.begin()+cat_index);
1768
1769 // non-categorized mode - non-categorized array
1770 if ( !item->IsCategory() )
1771 {
1772 wxASSERT( item->m_parent == m_abcArray );
1773 wxArrayPGProperty& parentsChildren = item->m_parent->m_children;
1774 parentsChildren.erase(parentsChildren.begin() + indinparent);
1775 item->m_parent->FixIndicesOfChildren(indinparent);
1776 }
1777 }
1778
1779 if ( item->GetBaseName().length() &&
1780 (parent->IsCategory() || parent->IsRoot()) )
1781 m_dictName.erase(item->GetBaseName());
1782
1783 wxPropertyGrid* pg = GetGrid();
1784
1785 // We need to clear parent grid's m_propHover, if it matches item
1786 if ( pg && pg->m_propHover == item )
1787 pg->m_propHover = NULL;
1788
1789 // We can actually delete it now
1790 if ( doDelete )
1791 delete item;
1792
1793 m_itemsAdded = 1; // Not a logical assignment (but required nonetheless).
1794
1795 VirtualHeightChanged();
1796 }
1797
1798 // -----------------------------------------------------------------------
1799
1800 #endif // wxUSE_PROPGRID