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