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