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