Fully conform to the documented statement according to which names of properties...
[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 = (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;
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();
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->AddChild2(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 m_regularArray.Empty();
272 if ( m_abcArray )
273 m_abcArray->Empty();
274
275 m_dictName.clear();
276
277 m_currentCategory = (wxPropertyCategory*) NULL;
278 m_lastCaptionBottomnest = 1;
279 m_itemsAdded = 0;
280
281 m_virtualHeight = 0;
282 m_vhCalcPending = 0;
283
284 m_selected = (wxPGProperty*) NULL;
285 }
286
287 // -----------------------------------------------------------------------
288
289 void wxPropertyGridPageState::CalculateFontAndBitmapStuff( int WXUNUSED(vspacing) )
290 {
291 wxPropertyGrid* propGrid = GetGrid();
292
293 VirtualHeightChanged();
294
295 // Recalculate caption text extents.
296 unsigned int i;
297
298 for ( i=0;i<m_regularArray.GetChildCount();i++ )
299 {
300 wxPGProperty* p =m_regularArray.Item(i);
301
302 if ( p->IsCategory() )
303 ((wxPropertyCategory*)p)->CalculateTextExtent(propGrid, propGrid->GetCaptionFont());
304 }
305 }
306
307 // -----------------------------------------------------------------------
308
309 void wxPropertyGridPageState::SetVirtualWidth( int width )
310 {
311 wxASSERT( width >= 0 );
312 wxPropertyGrid* pg = GetGrid();
313 int gw = pg->GetClientSize().x;
314 if ( width < gw )
315 width = gw;
316
317 m_width = width;
318 }
319
320 // -----------------------------------------------------------------------
321
322 void wxPropertyGridPageState::OnClientWidthChange( int newWidth, int widthChange, bool fromOnResize )
323 {
324 wxPropertyGrid* pg = GetGrid();
325
326 if ( pg->HasVirtualWidth() )
327 {
328 if ( m_width < newWidth )
329 SetVirtualWidth( newWidth );
330
331 CheckColumnWidths(widthChange);
332 }
333 else
334 {
335 SetVirtualWidth( newWidth );
336
337 // This should be done before splitter auto centering
338 // NOTE: Splitter auto-centering is done in this function.
339 if ( !fromOnResize )
340 widthChange = 0;
341 CheckColumnWidths(widthChange);
342
343 if ( !(GetGrid()->GetInternalFlags() & wxPG_FL_SPLITTER_PRE_SET) &&
344 (GetGrid()->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER) )
345 {
346 long timeSinceCreation = (::wxGetLocalTimeMillis() - GetGrid()->m_timeCreated).ToLong();
347
348 // If too long, don't set splitter
349 if ( timeSinceCreation < 3000 )
350 {
351 if ( m_properties->GetChildCount() || timeSinceCreation > 750 )
352 {
353 SetSplitterLeft( false );
354 }
355 else
356 {
357 DoSetSplitterPosition( newWidth / 2 );
358 GetGrid()->ClearInternalFlag(wxPG_FL_SPLITTER_PRE_SET);
359 }
360 }
361 }
362 }
363 }
364
365 // -----------------------------------------------------------------------
366 // wxPropertyGridPageState item iteration methods
367 // -----------------------------------------------------------------------
368
369 wxPGProperty* wxPropertyGridPageState::GetLastItem( int flags )
370 {
371 if ( !m_properties->GetChildCount() )
372 return (wxPGProperty*) NULL;
373
374 wxPG_ITERATOR_CREATE_MASKS(flags, int itemExMask, int parentExMask)
375
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();
381
382 // Then, if it doesn't fit our criteria, back up until we find something that does
383 if ( pwc->GetFlags() & itemExMask )
384 {
385 wxPropertyGridIterator it( this, flags, pwc );
386 for ( ; !it.AtEnd(); it.Prev() )
387 ;
388 pwc = (wxPGProperty*) it.GetProperty();
389 }
390
391 return pwc;
392 }
393
394 wxPropertyCategory* wxPropertyGridPageState::GetPropertyCategory( const wxPGProperty* p ) const
395 {
396 const wxPGProperty* parent = (const wxPGProperty*)p;
397 const wxPGProperty* grandparent = (const wxPGProperty*)parent->GetParent();
398 do
399 {
400 parent = grandparent;
401 grandparent = (wxPGProperty*)parent->GetParent();
402 if ( parent->IsCategory() && grandparent )
403 return (wxPropertyCategory*)parent;
404 } while ( grandparent );
405
406 return (wxPropertyCategory*) NULL;
407 }
408
409 // -----------------------------------------------------------------------
410 // wxPropertyGridPageState GetPropertyXXX methods
411 // -----------------------------------------------------------------------
412
413 wxPGProperty* wxPropertyGridPageState::GetPropertyByLabel( const wxString& label,
414 wxPGProperty* parent ) const
415 {
416
417 size_t i;
418
419 if ( !parent ) parent = (wxPGProperty*) &m_regularArray;
420
421 for ( i=0; i<parent->GetChildCount(); i++ )
422 {
423 wxPGProperty* p = parent->Item(i);
424 if ( p->m_label == label )
425 return p;
426 // Check children recursively.
427 if ( p->GetChildCount() )
428 {
429 p = GetPropertyByLabel(label,(wxPGProperty*)p);
430 if ( p )
431 return p;
432 }
433 }
434
435 return NULL;
436 }
437
438 // -----------------------------------------------------------------------
439
440 wxPGProperty* wxPropertyGridPageState::BaseGetPropertyByName( const wxString& name ) const
441 {
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;
447 }
448
449 // -----------------------------------------------------------------------
450
451 void wxPropertyGridPageState::DoSetPropertyName( wxPGProperty* p,
452 const wxString& newName )
453 {
454 wxCHECK_RET( p, wxT("invalid property id") );
455
456 wxPGProperty* parent = p->GetParent();
457
458 if ( parent->IsCategory() || parent->IsRoot() )
459 {
460 if ( p->GetBaseName().length() )
461 m_dictName.erase( p->GetBaseName() );
462 if ( newName.length() )
463 m_dictName[newName] = (void*) p;
464 }
465
466 p->DoSetName(newName);
467 }
468
469 // -----------------------------------------------------------------------
470 // wxPropertyGridPageState global operations
471 // -----------------------------------------------------------------------
472
473 // -----------------------------------------------------------------------
474 // Item iteration macros
475 // NB: Nowadays only needed for alphabetic/categoric mode switching.
476 // -----------------------------------------------------------------------
477
478 //#define II_INVALID_I 0x00FFFFFF
479
480 #define ITEM_ITERATION_VARIABLES \
481 wxPGProperty* parent; \
482 unsigned int i; \
483 unsigned int iMax;
484
485 #define ITEM_ITERATION_INIT_FROM_THE_TOP \
486 parent = m_properties; \
487 i = 0;
488
489 #if 0
490 #define ITEM_ITERATION_INIT(startparent, startindex, state) \
491 parent = startparent; \
492 i = (unsigned int)startindex; \
493 if ( parent == (wxPGProperty*) NULL ) \
494 { \
495 parent = state->m_properties; \
496 i = 0; \
497 }
498 #endif
499
500 #define ITEM_ITERATION_LOOP_BEGIN \
501 do \
502 { \
503 iMax = parent->GetChildCount(); \
504 while ( i < iMax ) \
505 { \
506 wxPGProperty* p = parent->Item(i);
507
508 #define ITEM_ITERATION_LOOP_END \
509 if ( p->GetChildCount() ) \
510 { \
511 i = 0; \
512 parent = (wxPGProperty*)p; \
513 iMax = parent->GetChildCount(); \
514 } \
515 else \
516 i++; \
517 } \
518 i = parent->m_arrIndex + 1; \
519 parent = parent->m_parent; \
520 } \
521 while ( parent != NULL );
522
523 bool wxPropertyGridPageState::EnableCategories( bool enable )
524 {
525 //
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.
529 //
530 ITEM_ITERATION_VARIABLES
531
532 if ( enable )
533 {
534 //
535 // Enable categories
536 //
537
538 if ( !IsInNonCatMode() )
539 return false;
540
541 m_properties = &m_regularArray;
542
543 // fix parents, indexes, and depths
544 ITEM_ITERATION_INIT_FROM_THE_TOP
545
546 ITEM_ITERATION_LOOP_BEGIN
547
548 p->m_arrIndex = i;
549
550 p->m_parent = parent;
551
552 // If parent was category, and this is not,
553 // then the depth stays the same.
554 if ( parent->IsCategory() &&
555 !p->IsCategory() )
556 p->m_depth = parent->m_depth;
557 else
558 p->m_depth = parent->m_depth + 1;
559
560 ITEM_ITERATION_LOOP_END
561
562 }
563 else
564 {
565 //
566 // Disable categories
567 //
568
569 if ( IsInNonCatMode() )
570 return false;
571
572 // Create array, if necessary.
573 if ( !m_abcArray )
574 InitNonCatMode();
575
576 m_properties = m_abcArray;
577
578 // fix parents, indexes, and depths
579 ITEM_ITERATION_INIT_FROM_THE_TOP
580
581 ITEM_ITERATION_LOOP_BEGIN
582
583 p->m_arrIndex = i;
584
585 p->m_parent = parent;
586
587 p->m_depth = parent->m_depth + 1;
588
589 ITEM_ITERATION_LOOP_END
590 }
591
592 VirtualHeightChanged();
593
594 if ( m_pPropGrid->GetState() == this )
595 m_pPropGrid->RecalculateVirtualSize();
596
597 return true;
598 }
599
600 // -----------------------------------------------------------------------
601
602 #if wxUSE_STL
603 #include <algorithm>
604
605 static bool wxPG_SortFunc(wxPGProperty *p1, wxPGProperty *p2)
606 {
607 return p1->GetLabel() < p2->GetLabel();
608 }
609
610 #else
611
612 static int wxPG_SortFunc(wxPGProperty **p1, wxPGProperty **p2)
613 {
614 wxPGProperty *pp1 = *p1;
615 wxPGProperty *pp2 = *p2;
616 return pp1->GetLabel().compare( pp2->GetLabel() );
617 }
618
619 #endif
620
621 void wxPropertyGridPageState::SortChildren( wxPGProperty* p )
622 {
623 if ( !p )
624 p = (wxPGProperty*)m_properties;
625
626 if ( !p->GetChildCount() )
627 return;
628
629 wxPGProperty* pwc = (wxPGProperty*)p;
630
631 // Can only sort items with children
632 if ( pwc->GetChildCount() < 1 )
633 return;
634
635 #if wxUSE_STL
636 std::sort(pwc->m_children.begin(), pwc->m_children.end(), wxPG_SortFunc);
637 #else
638 pwc->m_children.Sort( wxPG_SortFunc );
639 #endif
640
641 // Fix indexes
642 pwc->FixIndicesOfChildren();
643
644 }
645
646 // -----------------------------------------------------------------------
647
648 void wxPropertyGridPageState::Sort()
649 {
650 SortChildren( m_properties );
651
652 // Sort categories as well
653 if ( !IsInNonCatMode() )
654 {
655 size_t i;
656 for ( i=0;i<m_properties->GetChildCount();i++)
657 {
658 wxPGProperty* p = m_properties->Item(i);
659 if ( p->IsCategory() )
660 SortChildren( p );
661 }
662 }
663 }
664
665 // -----------------------------------------------------------------------
666 // wxPropertyGridPageState splitter, column and hittest functions
667 // -----------------------------------------------------------------------
668
669 wxPGProperty* wxPropertyGridPageState::DoGetItemAtY( int y ) const
670 {
671 // Outside?
672 if ( y < 0 )
673 return (wxPGProperty*) NULL;
674
675 unsigned int a = 0;
676 return m_properties->GetItemAtY(y, GetGrid()->m_lineHeight, &a);
677 }
678
679 // -----------------------------------------------------------------------
680
681 wxPropertyGridHitTestResult wxPropertyGridPageState::HitTest( const wxPoint&pt ) const
682 {
683 wxPropertyGridHitTestResult result;
684 result.column = HitTestH( pt.x, &result.splitter, &result.splitterHitOffset );
685 result.property = DoGetItemAtY( pt.y );
686 return result;
687 }
688
689 // -----------------------------------------------------------------------
690
691 // Used by SetSplitterLeft() and DotFitColumns()
692 int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC& dc,
693 wxPGProperty* pwc,
694 unsigned int col,
695 bool subProps) const
696 {
697 wxPropertyGrid* pg = m_pPropGrid;
698 size_t i;
699 int maxW = 0;
700 int w, h;
701
702 for ( i=0; i<pwc->GetChildCount(); i++ )
703 {
704 wxPGProperty* p = pwc->Item(i);
705 if ( !p->IsCategory() )
706 {
707 const wxPGCell* cell = NULL;
708 wxString text;
709 p->GetDisplayInfo(col, -1, 0, &text, &cell);
710 dc.GetTextExtent(text, &w, &h);
711 if ( col == 0 )
712 w += ( ((int)p->m_depth-1) * pg->m_subgroup_extramargin );
713
714 //
715 // TODO: Add bitmap support.
716
717 w += (wxPG_XBEFORETEXT*2);
718
719 if ( w > maxW )
720 maxW = w;
721 }
722
723 if ( p->GetChildCount() &&
724 ( subProps || p->IsCategory() ) )
725 {
726 w = GetColumnFitWidth( dc, p, col, subProps );
727
728 if ( w > maxW )
729 maxW = w;
730 }
731 }
732
733 return maxW;
734 }
735
736 int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn ) const
737 {
738 int n = GetGrid()->m_marginWidth;
739 int i;
740 for ( i=0; i<=splitterColumn; i++ )
741 n += m_colWidths[i];
742 return n;
743 }
744
745 int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column) ) const
746 {
747 return wxPG_DRAG_MARGIN;
748 }
749
750 void wxPropertyGridPageState::PropagateColSizeDec( int column, int decrease, int dir )
751 {
752 int origWidth = m_colWidths[column];
753 m_colWidths[column] -= decrease;
754 int min = GetColumnMinWidth(column);
755 int more = 0;
756 if ( m_colWidths[column] < min )
757 {
758 more = decrease - (origWidth - min);
759 m_colWidths[column] = min;
760 }
761
762 //
763 // FIXME: Causes erratic splitter changing, so as a workaround
764 // disabled if two or less columns.
765
766 if ( m_colWidths.size() <= 2 )
767 return;
768
769 column += dir;
770 if ( more && column < (int)m_colWidths.size() && column >= 0 )
771 PropagateColSizeDec( column, more, dir );
772 }
773
774 void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos, int splitterColumn, bool WXUNUSED(allPages), bool fromAutoCenter )
775 {
776 wxPropertyGrid* pg = GetGrid();
777
778 int adjust = newXPos - DoGetSplitterPosition(splitterColumn);
779
780 if ( !pg->HasVirtualWidth() )
781 {
782 // No virtual width
783 int otherColumn;
784 if ( adjust > 0 )
785 {
786 otherColumn = splitterColumn + 1;
787 if ( otherColumn == (int)m_colWidths.size() )
788 otherColumn = 0;
789 m_colWidths[splitterColumn] += adjust;
790 PropagateColSizeDec( otherColumn, adjust, 1 );
791 }
792 else
793 {
794 otherColumn = splitterColumn + 1;
795 if ( otherColumn == (int)m_colWidths.size() )
796 otherColumn = 0;
797 m_colWidths[otherColumn] -= adjust;
798 PropagateColSizeDec( splitterColumn, -adjust, -1 );
799 }
800 }
801 else
802 {
803 m_colWidths[splitterColumn] += adjust;
804 }
805
806 if ( splitterColumn == 0 )
807 m_fSplitterX = (double) newXPos;
808
809 if ( !fromAutoCenter )
810 {
811 // Don't allow initial splitter auto-positioning after this.
812 if ( pg->GetState() == this )
813 pg->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET);
814
815 CheckColumnWidths();
816 }
817 }
818
819 // Moves splitter so that all labels are visible, but just.
820 void wxPropertyGridPageState::SetSplitterLeft( bool subProps )
821 {
822 wxPropertyGrid* pg = GetGrid();
823 wxClientDC dc(pg);
824 dc.SetFont(pg->m_font);
825
826 int maxW = GetColumnFitWidth(dc, m_properties, 0, subProps);
827
828 if ( maxW > 0 )
829 {
830 maxW += pg->m_marginWidth;
831 DoSetSplitterPosition( maxW );
832 }
833
834 pg->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
835 }
836
837 wxSize wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize) )
838 {
839 wxPropertyGrid* pg = GetGrid();
840 wxClientDC dc(pg);
841 dc.SetFont(pg->m_font);
842
843 int marginWidth = pg->m_marginWidth;
844 int accWid = marginWidth;
845 int maxColWidth = 500;
846
847 for ( unsigned int col=0; col < GetColumnCount(); col++ )
848 {
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;
855
856 m_colWidths[col] = fitWid;
857
858 accWid += fitWid;
859 }
860
861 // Expand last one to fill the width
862 int remaining = m_width - accWid;
863 m_colWidths[GetColumnCount()-1] += remaining;
864
865 pg->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
866
867 int firstSplitterX = marginWidth + m_colWidths[0];
868 m_fSplitterX = (double) firstSplitterX;
869
870 // Don't allow initial splitter auto-positioning after this.
871 if ( pg->GetState() == this )
872 {
873 pg->SetSplitterPosition(firstSplitterX, false);
874 pg->Refresh();
875 }
876
877 int x, y;
878 pg->GetVirtualSize(&x, &y);
879
880 return wxSize(accWid, y);
881 }
882
883 void wxPropertyGridPageState::CheckColumnWidths( int widthChange )
884 {
885 if ( m_width == 0 )
886 return;
887
888 wxPropertyGrid* pg = GetGrid();
889
890 #ifdef __WXDEBUG__
891 const bool debug = false;
892 #endif
893
894 unsigned int i;
895 unsigned int lastColumn = m_colWidths.size() - 1;
896 int width = m_width;
897 int clientWidth = pg->GetClientSize().x;
898
899 //
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.
902 int reduceCol = -1;
903 int highestColWidth = 0;
904
905 #ifdef __WXDEBUG__
906 if ( debug )
907 wxLogDebug(wxT("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"), width, clientWidth);
908 #endif
909
910 //
911 // Check min sizes
912 for ( i=0; i<m_colWidths.size(); i++ )
913 {
914 int min = GetColumnMinWidth(i);
915 if ( m_colWidths[i] <= min )
916 {
917 m_colWidths[i] = min;
918 }
919 else
920 {
921 if ( pg->HasFlag(wxPG_SPLITTER_AUTO_CENTER) )
922 {
923 if ( m_colWidths[i] >= highestColWidth )
924 {
925 highestColWidth = m_colWidths[i];
926 reduceCol = i;
927 }
928 }
929 else
930 {
931 reduceCol = i;
932 }
933 }
934 }
935
936 int colsWidth = pg->m_marginWidth;
937 for ( i=0; i<m_colWidths.size(); i++ )
938 colsWidth += m_colWidths[i];
939
940 #ifdef __WXDEBUG__
941 if ( debug )
942 wxLogDebug(wxT(" HasVirtualWidth: %i colsWidth: %i"),(int)pg->HasVirtualWidth(),colsWidth);
943 #endif
944
945 // Then mode-based requirement
946 if ( !pg->HasVirtualWidth() )
947 {
948 int widthHigher = width - colsWidth;
949
950 // Adapt colsWidth to width
951 if ( colsWidth < width )
952 {
953 // Increase column
954 #ifdef __WXDEBUG__
955 if ( debug )
956 wxLogDebug(wxT(" Adjust last column to %i"), m_colWidths[lastColumn] + widthHigher);
957 #endif
958 m_colWidths[lastColumn] = m_colWidths[lastColumn] + widthHigher;
959 }
960 else if ( colsWidth > width )
961 {
962 // Reduce column
963 if ( reduceCol != -1 )
964 {
965 #ifdef __WXDEBUG__
966 if ( debug )
967 wxLogDebug(wxT(" Reduce column %i (by %i)"), reduceCol, -widthHigher);
968 #endif
969 // Reduce widest column, and recheck
970 m_colWidths[reduceCol] = m_colWidths[reduceCol] + widthHigher;
971 CheckColumnWidths();
972 }
973 }
974 }
975 else
976 {
977 // Only check colsWidth against clientWidth
978 if ( colsWidth < clientWidth )
979 {
980 m_colWidths[lastColumn] = m_colWidths[lastColumn] + (clientWidth-colsWidth);
981 }
982
983 m_width = colsWidth;
984
985 // If width changed, recalculate virtual size
986 if ( pg->GetState() == this )
987 pg->RecalculateVirtualSize();
988 }
989
990 #ifdef __WXDEBUG__
991 if ( debug )
992 for ( i=0; i<m_colWidths.size(); i++ )
993 wxLogDebug(wxT("col%i: %i"),i,m_colWidths[i]);
994 #endif
995
996 // Auto center splitter
997 if ( !(pg->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER) &&
998 m_colWidths.size() == 2 )
999 {
1000 float centerX = (float)(pg->m_width/2);
1001 float splitterX;
1002
1003 if ( m_fSplitterX < 0.0 )
1004 {
1005 splitterX = centerX;
1006 }
1007 else if ( widthChange )
1008 {
1009 //float centerX = float(pg->GetSize().x) * 0.5;
1010
1011 // Recenter?
1012 splitterX = m_fSplitterX + (float(widthChange) * 0.5);
1013 float deviation = fabs(centerX - splitterX);
1014
1015 // If deviating from center, adjust towards it
1016 if ( deviation > 20.0 )
1017 {
1018 if ( splitterX > centerX)
1019 splitterX -= 2;
1020 else
1021 splitterX += 2;
1022 }
1023 }
1024 else
1025 {
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 )
1030 {
1031 splitterX = centerX;
1032 }
1033 }
1034
1035 DoSetSplitterPosition((int)splitterX, 0, false, true);
1036
1037 m_fSplitterX = splitterX; // needed to retain accuracy
1038 }
1039 }
1040
1041 void wxPropertyGridPageState::SetColumnCount( int colCount )
1042 {
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 );
1047
1048 if ( m_pPropGrid->GetState() == this )
1049 m_pPropGrid->RecalculateVirtualSize();
1050 else
1051 CheckColumnWidths();
1052 }
1053
1054 // Returns column index, -1 for margin
1055 int wxPropertyGridPageState::HitTestH( int x, int* pSplitterHit, int* pSplitterHitOffset ) const
1056 {
1057 int cx = GetGrid()->m_marginWidth;
1058 int col = -1;
1059 int prevSplitter = -1;
1060
1061 while ( x > cx )
1062 {
1063 col++;
1064 if ( col >= (int)m_colWidths.size() )
1065 {
1066 *pSplitterHit = -1;
1067 return col;
1068 }
1069 prevSplitter = cx;
1070 cx += m_colWidths[col];
1071 }
1072
1073 // Near prev. splitter
1074 if ( col >= 1 )
1075 {
1076 int diff = x - prevSplitter;
1077 if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 )
1078 {
1079 *pSplitterHit = col - 1;
1080 *pSplitterHitOffset = diff;
1081 return col;
1082 }
1083 }
1084
1085 // Near next splitter
1086 int nextSplitter = cx;
1087 if ( col < (int)(m_colWidths.size()-1) )
1088 {
1089 int diff = x - nextSplitter;
1090 if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 )
1091 {
1092 *pSplitterHit = col;
1093 *pSplitterHitOffset = diff;
1094 return col;
1095 }
1096 }
1097
1098 *pSplitterHit = -1;
1099 return col;
1100 }
1101
1102 // -----------------------------------------------------------------------
1103 // wxPropertyGridPageState property value setting and getting
1104 // -----------------------------------------------------------------------
1105
1106 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty* p, const wxString& value )
1107 {
1108 if ( p )
1109 {
1110 int flags = wxPG_REPORT_ERROR|wxPG_FULL_VALUE|wxPG_PROGRAMMATIC_VALUE;
1111
1112 wxVariant variant = p->GetValueRef();
1113 bool res;
1114
1115 if ( p->GetMaxLength() <= 0 )
1116 res = p->StringToValue( variant, value, flags );
1117 else
1118 res = p->StringToValue( variant, value.Mid(0,p->GetMaxLength()), flags );
1119
1120 if ( res )
1121 {
1122 p->SetValue(variant);
1123 if ( m_selected==p && this==m_pPropGrid->GetState() )
1124 p->UpdateControl(m_pPropGrid->GetEditorControl());
1125 }
1126
1127 return true;
1128 }
1129 return false;
1130 }
1131
1132 // -----------------------------------------------------------------------
1133
1134 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty* p, wxVariant& value )
1135 {
1136 if ( p )
1137 {
1138 p->SetValue(value);
1139 if ( m_selected==p && this==m_pPropGrid->GetState() )
1140 p->UpdateControl(m_pPropGrid->GetEditorControl());
1141
1142 return true;
1143 }
1144 return false;
1145 }
1146
1147 // -----------------------------------------------------------------------
1148
1149 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty* p, wxObject* value )
1150 {
1151 if ( p )
1152 {
1153 // wnd_primary has to be given so the control can be updated as well.
1154 wxVariant v(value);
1155 DoSetPropertyValue(p, v);
1156 return true;
1157 }
1158 return false;
1159 }
1160
1161 // -----------------------------------------------------------------------
1162
1163 void wxPropertyGridPageState::DoSetPropertyValueUnspecified( wxPGProperty* p )
1164 {
1165 wxCHECK_RET( p, wxT("invalid property id") );
1166
1167 if ( !p->IsValueUnspecified() )
1168 {
1169 // Value should be set first - editor class methods may need it
1170 p->m_value.MakeNull();
1171
1172 wxASSERT( m_pPropGrid );
1173
1174 if ( m_pPropGrid->GetState() == this )
1175 {
1176 if ( m_pPropGrid->m_selected == p && m_pPropGrid->m_wndEditor )
1177 {
1178 p->GetEditorClass()->SetValueToUnspecified(p, m_pPropGrid->GetEditorControl());
1179 }
1180 }
1181
1182 unsigned int i;
1183 for ( i = 0; i < p->GetChildCount(); i++ )
1184 DoSetPropertyValueUnspecified( p->Item(i) );
1185 }
1186 }
1187
1188 // -----------------------------------------------------------------------
1189 // wxPropertyGridPageState property operations
1190 // -----------------------------------------------------------------------
1191
1192 bool wxPropertyGridPageState::DoCollapse( wxPGProperty* p )
1193 {
1194 wxCHECK_MSG( p, false, wxT("invalid property id") );
1195
1196 if ( !p->GetChildCount() ) return false;
1197
1198 if ( !p->IsExpanded() ) return false;
1199
1200 p->SetExpanded(false);
1201
1202 VirtualHeightChanged();
1203
1204 return true;
1205 }
1206
1207 // -----------------------------------------------------------------------
1208
1209 bool wxPropertyGridPageState::DoExpand( wxPGProperty* p )
1210 {
1211 wxCHECK_MSG( p, false, wxT("invalid property id") );
1212
1213 if ( !p->GetChildCount() ) return false;
1214
1215 if ( p->IsExpanded() ) return false;
1216
1217 p->SetExpanded(true);
1218
1219 VirtualHeightChanged();
1220
1221 return true;
1222 }
1223
1224 // -----------------------------------------------------------------------
1225
1226 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty* p, unsigned int flags )
1227 {
1228 if ( this == m_pPropGrid->GetState() )
1229 return m_pPropGrid->DoSelectProperty( p, flags );
1230
1231 m_selected = p;
1232 return true;
1233 }
1234
1235 // -----------------------------------------------------------------------
1236
1237 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty* p, bool hide, int flags )
1238 {
1239 if ( !hide )
1240 p->ClearFlag( wxPG_PROP_HIDDEN );
1241 else
1242 p->SetFlag( wxPG_PROP_HIDDEN );
1243
1244 if ( flags & wxPG_RECURSE )
1245 {
1246 unsigned int i;
1247 for ( i = 0; i < p->GetChildCount(); i++ )
1248 DoHideProperty(p->Item(i), hide, flags | wxPG_RECURSE_STARTS);
1249 }
1250
1251 VirtualHeightChanged();
1252
1253 return true;
1254 }
1255
1256 // -----------------------------------------------------------------------
1257
1258 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty* p, bool enable )
1259 {
1260 if ( p )
1261 {
1262 if ( enable )
1263 {
1264 if ( !(p->m_flags & wxPG_PROP_DISABLED) )
1265 return false;
1266
1267 // Enabling
1268
1269 p->m_flags &= ~(wxPG_PROP_DISABLED);
1270 }
1271 else
1272 {
1273 if ( p->m_flags & wxPG_PROP_DISABLED )
1274 return false;
1275
1276 // Disabling
1277
1278 p->m_flags |= wxPG_PROP_DISABLED;
1279
1280 }
1281
1282 // Apply same to sub-properties as well
1283 unsigned int i;
1284 for ( i = 0; i < p->GetChildCount(); i++ )
1285 DoEnableProperty( p->Item(i), enable );
1286
1287 return true;
1288 }
1289 return false;
1290 }
1291
1292 // -----------------------------------------------------------------------
1293 // wxPropertyGridPageState wxVariant related routines
1294 // -----------------------------------------------------------------------
1295
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,
1300 long flags ) const
1301 {
1302 wxPGProperty* pwc = (wxPGProperty*) baseparent;
1303
1304 // Root is the default base-parent.
1305 if ( !pwc )
1306 pwc = m_properties;
1307
1308 wxVariantList tempList;
1309 wxVariant v( tempList, listname );
1310
1311 if ( pwc->GetChildCount() )
1312 {
1313 if ( flags & wxPG_KEEP_STRUCTURE )
1314 {
1315 wxASSERT( !pwc->HasFlag(wxPG_PROP_AGGREGATE) );
1316
1317 size_t i;
1318 for ( i=0; i<pwc->GetChildCount(); i++ )
1319 {
1320 wxPGProperty* p = pwc->Item(i);
1321 if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) )
1322 {
1323 wxVariant variant = p->GetValue();
1324 variant.SetName( p->GetBaseName() );
1325 v.Append( variant );
1326 }
1327 else
1328 {
1329 v.Append( DoGetPropertyValues(p->m_name,p,flags|wxPG_KEEP_STRUCTURE) );
1330 }
1331 if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() )
1332 v.Append( p->GetAttributesAsList() );
1333 }
1334 }
1335 else
1336 {
1337 wxPropertyGridConstIterator it( this, wxPG_ITERATE_DEFAULT, pwc->Item(0) );
1338 it.SetBaseParent( pwc );
1339
1340 for ( ; !it.AtEnd(); it.Next() )
1341 {
1342 const wxPGProperty* p = it.GetProperty();
1343
1344 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1345 if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) )
1346 {
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() );
1352 }
1353 }
1354 }
1355 }
1356
1357 return v;
1358 }
1359
1360 // -----------------------------------------------------------------------
1361
1362 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList& list, wxPGProperty* defaultCategory )
1363 {
1364 unsigned char origFrozen = 1;
1365
1366 if ( m_pPropGrid->GetState() == this )
1367 {
1368 origFrozen = m_pPropGrid->m_frozen;
1369 if ( !origFrozen ) m_pPropGrid->Freeze();
1370 }
1371
1372 wxPropertyCategory* use_category = (wxPropertyCategory*)defaultCategory;
1373
1374 if ( !use_category )
1375 use_category = (wxPropertyCategory*)m_properties;
1376
1377 // Let's iterate over the list of variants.
1378 wxVariantList::const_iterator node;
1379 int numSpecialEntries = 0;
1380
1381 //
1382 // Second pass for special entries
1383 for ( node = list.begin(); node != list.end(); ++node )
1384 {
1385 wxVariant *current = (wxVariant*)*node;
1386
1387 // Make sure it is wxVariant.
1388 wxASSERT( current );
1389 wxASSERT( wxStrcmp(current->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1390
1391 const wxString& name = current->GetName();
1392 if ( name.length() > 0 )
1393 {
1394 //
1395 // '@' signified a special entry
1396 if ( name[0] == wxS('@') )
1397 {
1398 numSpecialEntries++;
1399 }
1400 else
1401 {
1402 wxPGProperty* foundProp = BaseGetPropertyByName(name);
1403 if ( foundProp )
1404 {
1405 wxPGProperty* p = foundProp;
1406
1407 // If it was a list, we still have to go through it.
1408 if ( wxStrcmp(current->GetType(), wxS("list")) == 0 )
1409 {
1410 DoSetPropertyValues( current->GetList(),
1411 p->IsCategory()?p:((wxPGProperty*)NULL)
1412 );
1413 }
1414 else
1415 {
1416 #ifdef __WXDEBUG__
1417 if ( wxStrcmp(current->GetType(), p->GetValue().GetType()) != 0)
1418 {
1419 wxLogDebug(wxT("wxPropertyGridPageState::DoSetPropertyValues Warning: Setting value of property \"%s\" from variant"),
1420 p->GetName().c_str());
1421 }
1422 #endif
1423
1424 p->SetValue(*current);
1425 }
1426 }
1427 else
1428 {
1429 // Is it list?
1430 if ( current->GetType() != wxS("list") )
1431 {
1432 // Not.
1433 }
1434 else
1435 {
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 );
1439 }
1440 }
1441 }
1442 }
1443 }
1444
1445 if ( numSpecialEntries )
1446 {
1447 for ( node = list.begin(); node != list.end(); ++node )
1448 {
1449 wxVariant *current = (wxVariant*)*node;
1450
1451 const wxString& name = current->GetName();
1452 if ( name.length() > 0 )
1453 {
1454 //
1455 // '@' signified a special entry
1456 if ( name[0] == wxS('@') )
1457 {
1458 numSpecialEntries--;
1459
1460 size_t pos2 = name.rfind(wxS('@'));
1461 if ( pos2 > 0 && pos2 < (name.size()-1) )
1462 {
1463 wxString propName = name.substr(1, pos2-1);
1464 wxString entryType = name.substr(pos2+1, wxString::npos);
1465
1466 if ( entryType == wxS("attr") )
1467 {
1468 //
1469 // List of attributes
1470 wxPGProperty* foundProp = BaseGetPropertyByName(propName);
1471 if ( foundProp )
1472 {
1473 wxASSERT( current->GetType() == wxPG_VARIANT_TYPE_LIST );
1474
1475 wxVariantList& list2 = current->GetList();
1476 wxVariantList::const_iterator node2;
1477
1478 for ( node2 = list2.begin(); node2 != list2.end(); ++node2 )
1479 {
1480 wxVariant *attr = (wxVariant*)*node2;
1481 foundProp->SetAttribute( attr->GetName(), *attr );
1482 }
1483 }
1484 else
1485 {
1486 // ERROR: No such property: 'propName'
1487 }
1488 }
1489 }
1490 else
1491 {
1492 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1493 }
1494 }
1495 }
1496
1497 if ( !numSpecialEntries )
1498 break;
1499 }
1500 }
1501
1502 if ( !origFrozen )
1503 {
1504 m_pPropGrid->Thaw();
1505
1506 if ( this == m_pPropGrid->GetState() )
1507 {
1508 m_selected->UpdateControl(m_pPropGrid->GetEditorControl());
1509 }
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 = (wxPGProperty*) 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 #ifdef __WXDEBUG__
1561 // Warn for identical names in debug mode.
1562 if ( BaseGetPropertyByName(property->GetName()) &&
1563 (!scheduledParent || scheduledParent->IsCategory()) )
1564 {
1565 wxLogError(wxT("wxPropertyGrid: Warning - item with name \"%s\" already exists."),
1566 property->GetName().c_str());
1567 wxPGGlobalVars->m_warnings++;
1568 }
1569 #endif
1570
1571 // Make sure nothing is selected.
1572 if ( propGrid )
1573 propGrid->ClearSelection(false);
1574
1575 // NULL parent == root parent
1576 if ( !scheduledParent )
1577 scheduledParent = DoGetRoot();
1578
1579 property->m_parent = scheduledParent;
1580
1581 property->InitAfterAdded(this, propGrid);
1582
1583 if ( property->IsCategory() )
1584 {
1585 wxPropertyCategory* pc = wxStaticCast(property, wxPropertyCategory);
1586
1587 m_currentCategory = pc;
1588
1589 // Calculate text extent for category caption
1590 if ( propGrid )
1591 pc->CalculateTextExtent(propGrid, propGrid->GetCaptionFont());
1592 }
1593
1594 return true;
1595 }
1596
1597 // -----------------------------------------------------------------------
1598
1599 wxPGProperty* wxPropertyGridPageState::DoAppend( wxPGProperty* property )
1600 {
1601 wxPropertyCategory* cur_cat = m_currentCategory;
1602 if ( property->IsCategory() )
1603 cur_cat = (wxPropertyCategory*) NULL;
1604
1605 return DoInsert( cur_cat, -1, property );
1606 }
1607
1608 // -----------------------------------------------------------------------
1609
1610 wxPGProperty* wxPropertyGridPageState::DoInsert( wxPGProperty* parent, int index, wxPGProperty* property )
1611 {
1612 if ( !parent )
1613 parent = m_properties;
1614
1615 wxCHECK_MSG( !parent->HasFlag(wxPG_PROP_AGGREGATE),
1616 wxNullProperty,
1617 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1618
1619 bool res = PrepareToAddItem( property, (wxPropertyCategory*)parent );
1620
1621 // PrepareToAddItem() may just decide to use use current category
1622 // instead of adding new one.
1623 if ( !res )
1624 return m_currentCategory;
1625
1626 // Note that item must be added into current mode later.
1627
1628 // If parent is wxParentProperty, just stick it in...
1629 // If parent is root (m_properties), then...
1630 // In categoric mode: Add as last item in m_abcArray (if not category).
1631 // Add to given index in m_regularArray.
1632 // In non-cat mode: Add as last item in m_regularArray.
1633 // Add to given index in m_abcArray.
1634 // If parent is category, then...
1635 // 1) Add to given category in given index.
1636 // 2) Add as last item in m_abcArray.
1637
1638 if ( !parent->IsCategory() && !parent->IsRoot() )
1639 {
1640 // Parent is wxParentingProperty: Just stick it in...
1641 parent->AddChild2( property, index );
1642 }
1643 else
1644 {
1645 // Parent is Category or Root.
1646
1647 if ( m_properties == &m_regularArray )
1648 {
1649 // Categorized mode
1650
1651 // Only add non-categories to m_abcArray.
1652 if ( m_abcArray && !property->IsCategory() )
1653 m_abcArray->AddChild2( property, -1, false );
1654
1655 // Add to current mode.
1656 parent->AddChild2( property, index );
1657
1658 }
1659 else
1660 {
1661 // Non-categorized mode.
1662
1663 if ( parent != m_properties )
1664 // Parent is category.
1665 parent->AddChild2( property, index, false );
1666 else
1667 // Parent is root.
1668 m_regularArray.AddChild2( property, -1, false );
1669
1670 // Add to current mode (no categories).
1671 if ( !property->IsCategory() )
1672 m_abcArray->AddChild2( property, index );
1673 }
1674 }
1675
1676 // category stuff
1677 if ( property->IsCategory() )
1678 {
1679 // This is a category caption item.
1680
1681 // Last caption is not the bottom one (this info required by append)
1682 m_lastCaptionBottomnest = 0;
1683 }
1684
1685 // Only add name to hashmap if parent is root or category
1686 if ( property->m_name.length() &&
1687 (parent->IsCategory() || parent->IsRoot()) )
1688 m_dictName[property->m_name] = (void*) property;
1689
1690 VirtualHeightChanged();
1691
1692 property->UpdateParentValues();
1693
1694 m_itemsAdded = 1;
1695
1696 return property;
1697 }
1698
1699 // -----------------------------------------------------------------------
1700
1701 void wxPropertyGridPageState::DoDelete( wxPGProperty* item, bool doDelete )
1702 {
1703 wxCHECK_RET( item->GetParent(),
1704 wxT("this property was already deleted") );
1705
1706 wxCHECK_RET( item != &m_regularArray && item != m_abcArray,
1707 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1708
1709 unsigned int indinparent = item->GetIndexInParent();
1710
1711 wxPGProperty* pwc = (wxPGProperty*)item;
1712 wxPGProperty* parent = item->GetParent();
1713
1714 wxCHECK_RET( !parent->HasFlag(wxPG_PROP_AGGREGATE),
1715 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1716
1717 // Delete children
1718 if ( item->GetChildCount() && !item->HasFlag(wxPG_PROP_AGGREGATE) )
1719 {
1720 // deleting a category
1721 if ( item->IsCategory() )
1722 {
1723 if ( pwc == m_currentCategory )
1724 m_currentCategory = (wxPropertyCategory*) NULL;
1725 }
1726
1727 item->DeleteChildren();
1728 }
1729
1730 if ( !IsInNonCatMode() )
1731 {
1732 // categorized mode - non-categorized array
1733
1734 // Remove from non-cat array
1735 if ( !item->IsCategory() &&
1736 (parent->IsCategory() || parent->IsRoot()) )
1737 {
1738 if ( m_abcArray )
1739 m_abcArray->RemoveChild(item);
1740 }
1741
1742 // categorized mode - categorized array
1743 wxArrayPGProperty& parentsChildren = parent->m_children;
1744 parentsChildren.erase( parentsChildren.begin() + indinparent );
1745 item->m_parent->FixIndicesOfChildren();
1746 }
1747 else
1748 {
1749 // non-categorized mode - categorized array
1750
1751 // We need to find location of item.
1752 wxPGProperty* cat_parent = &m_regularArray;
1753 int cat_index = m_regularArray.GetChildCount();
1754 size_t i;
1755 for ( i = 0; i < m_regularArray.GetChildCount(); i++ )
1756 {
1757 wxPGProperty* p = m_regularArray.Item(i);
1758 if ( p == item ) { cat_index = i; break; }
1759 if ( p->IsCategory() )
1760 {
1761 int subind = ((wxPGProperty*)p)->Index(item);
1762 if ( subind != wxNOT_FOUND )
1763 {
1764 cat_parent = ((wxPGProperty*)p);
1765 cat_index = subind;
1766 break;
1767 }
1768 }
1769 }
1770 cat_parent->m_children.erase(cat_parent->m_children.begin()+cat_index);
1771
1772 // non-categorized mode - non-categorized array
1773 if ( !item->IsCategory() )
1774 {
1775 wxASSERT( item->m_parent == m_abcArray );
1776 wxArrayPGProperty& parentsChildren = item->m_parent->m_children;
1777 parentsChildren.erase(parentsChildren.begin() + indinparent);
1778 item->m_parent->FixIndicesOfChildren(indinparent);
1779 }
1780 }
1781
1782 if ( item->GetBaseName().length() &&
1783 (parent->IsCategory() || parent->IsRoot()) )
1784 m_dictName.erase(item->GetBaseName());
1785
1786 // We can actually delete it now
1787 if ( doDelete )
1788 delete item;
1789
1790 m_itemsAdded = 1; // Not a logical assignment (but required nonetheless).
1791
1792 VirtualHeightChanged();
1793 }
1794
1795 // -----------------------------------------------------------------------
1796
1797 #endif // wxUSE_PROPGRID