Made code that uses wxArrayPGProperty more STL compliant (still can't use wxVector...
[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 static int wxPG_SortFunc_ByFunction(wxPGProperty **pp1, wxPGProperty **pp2)
609 {
610 wxPGProperty *p1 = *pp1;
611 wxPGProperty *p2 = *pp2;
612 wxPropertyGrid* pg = p1->GetGrid();
613 wxPGSortCallback sortFunction = pg->GetSortFunction();
614 return sortFunction(pg, p1, p2);
615 }
616
617 static int wxPG_SortFunc_ByLabel(wxPGProperty **pp1, wxPGProperty **pp2)
618 {
619 wxPGProperty *p1 = *pp1;
620 wxPGProperty *p2 = *pp2;
621 return p1->GetLabel().CmpNoCase( p2->GetLabel() );
622 }
623
624 #if 0
625 //
626 // For wxVector w/ wxUSE_STL=1, you would use code like this instead:
627 //
628
629 #include <algorithm>
630
631 static bool wxPG_SortFunc_ByFunction(wxPGProperty *p1, wxPGProperty *p2)
632 {
633 wxPropertyGrid* pg = p1->GetGrid();
634 wxPGSortCallback sortFunction = pg->GetSortFunction();
635 return sortFunction(pg, p1, p2) < 0;
636 }
637
638 static bool wxPG_SortFunc_ByLabel(wxPGProperty *p1, wxPGProperty *p2)
639 {
640 return p1->GetLabel().CmpNoCase( p2->GetLabel() ) < 0;
641 }
642 #endif
643
644 void wxPropertyGridPageState::DoSortChildren( wxPGProperty* p,
645 int flags )
646 {
647 if ( !p )
648 p = m_properties;
649
650 // Can only sort items with children
651 if ( !p->GetChildCount() )
652 return;
653
654 // Never sort children of aggregate properties
655 if ( p->HasFlag(wxPG_PROP_AGGREGATE) )
656 return;
657
658 if ( (flags & wxPG_SORT_TOP_LEVEL_ONLY)
659 && !p->IsCategory() && !p->IsRoot() )
660 return;
661
662 if ( GetGrid()->GetSortFunction() )
663 p->m_children.Sort( wxPG_SortFunc_ByFunction );
664 else
665 p->m_children.Sort( wxPG_SortFunc_ByLabel );
666
667 #if 0
668 //
669 // For wxVector w/ wxUSE_STL=1, you would use code like this instead:
670 //
671 if ( GetGrid()->GetSortFunction() )
672 std::sort(p->m_children.begin(), p->m_children.end(),
673 wxPG_SortFunc_ByFunction);
674 else
675 std::sort(p->m_children.begin(), p->m_children.end(),
676 wxPG_SortFunc_ByLabel);
677 #endif
678
679 // Fix indices
680 p->FixIndicesOfChildren();
681
682 if ( flags & wxPG_RECURSE )
683 {
684 // Apply sort recursively
685 for ( unsigned int i=0; i<p->GetChildCount(); i++ )
686 DoSortChildren(p->Item(i), flags);
687 }
688 }
689
690 // -----------------------------------------------------------------------
691
692 void wxPropertyGridPageState::DoSort( int flags )
693 {
694 DoSortChildren( m_properties, flags | wxPG_RECURSE );
695
696 // We used to sort categories as well here also if in non-categorized
697 // mode, but doing would naturally cause child indices to become
698 // corrupted.
699 }
700
701 // -----------------------------------------------------------------------
702
703 bool wxPropertyGridPageState::PrepareAfterItemsAdded()
704 {
705 if ( !m_itemsAdded ) return false;
706
707 wxPropertyGrid* pg = GetGrid();
708
709 m_itemsAdded = 0;
710
711 if ( pg->HasFlag(wxPG_AUTO_SORT) )
712 DoSort(wxPG_SORT_TOP_LEVEL_ONLY);
713
714 return true;
715 }
716
717 // -----------------------------------------------------------------------
718 // wxPropertyGridPageState splitter, column and hittest functions
719 // -----------------------------------------------------------------------
720
721 wxPGProperty* wxPropertyGridPageState::DoGetItemAtY( int y ) const
722 {
723 // Outside?
724 if ( y < 0 )
725 return NULL;
726
727 unsigned int a = 0;
728 return m_properties->GetItemAtY(y, GetGrid()->m_lineHeight, &a);
729 }
730
731 // -----------------------------------------------------------------------
732
733 wxPropertyGridHitTestResult wxPropertyGridPageState::HitTest( const wxPoint&pt ) const
734 {
735 wxPropertyGridHitTestResult result;
736 result.column = HitTestH( pt.x, &result.splitter, &result.splitterHitOffset );
737 result.property = DoGetItemAtY( pt.y );
738 return result;
739 }
740
741 // -----------------------------------------------------------------------
742
743 // Used by SetSplitterLeft() and DotFitColumns()
744 int wxPropertyGridPageState::GetColumnFitWidth(wxClientDC& dc,
745 wxPGProperty* pwc,
746 unsigned int col,
747 bool subProps) const
748 {
749 wxPropertyGrid* pg = m_pPropGrid;
750 size_t i;
751 int maxW = 0;
752 int w, h;
753
754 for ( i=0; i<pwc->GetChildCount(); i++ )
755 {
756 wxPGProperty* p = pwc->Item(i);
757 if ( !p->IsCategory() )
758 {
759 const wxPGCell* cell = NULL;
760 wxString text;
761 p->GetDisplayInfo(col, -1, 0, &text, &cell);
762 dc.GetTextExtent(text, &w, &h);
763 if ( col == 0 )
764 w += ( ((int)p->m_depth-1) * pg->m_subgroup_extramargin );
765
766 //
767 // TODO: Add bitmap support.
768
769 w += (wxPG_XBEFORETEXT*2);
770
771 if ( w > maxW )
772 maxW = w;
773 }
774
775 if ( p->GetChildCount() &&
776 ( subProps || p->IsCategory() ) )
777 {
778 w = GetColumnFitWidth( dc, p, col, subProps );
779
780 if ( w > maxW )
781 maxW = w;
782 }
783 }
784
785 return maxW;
786 }
787
788 int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn ) const
789 {
790 int n = GetGrid()->m_marginWidth;
791 int i;
792 for ( i=0; i<=splitterColumn; i++ )
793 n += m_colWidths[i];
794 return n;
795 }
796
797 int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column) ) const
798 {
799 return wxPG_DRAG_MARGIN;
800 }
801
802 void wxPropertyGridPageState::PropagateColSizeDec( int column, int decrease, int dir )
803 {
804 int origWidth = m_colWidths[column];
805 m_colWidths[column] -= decrease;
806 int min = GetColumnMinWidth(column);
807 int more = 0;
808 if ( m_colWidths[column] < min )
809 {
810 more = decrease - (origWidth - min);
811 m_colWidths[column] = min;
812 }
813
814 //
815 // FIXME: Causes erratic splitter changing, so as a workaround
816 // disabled if two or less columns.
817
818 if ( m_colWidths.size() <= 2 )
819 return;
820
821 column += dir;
822 if ( more && column < (int)m_colWidths.size() && column >= 0 )
823 PropagateColSizeDec( column, more, dir );
824 }
825
826 void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos, int splitterColumn, bool WXUNUSED(allPages), bool fromAutoCenter )
827 {
828 wxPropertyGrid* pg = GetGrid();
829
830 int adjust = newXPos - DoGetSplitterPosition(splitterColumn);
831
832 if ( !pg->HasVirtualWidth() )
833 {
834 // No virtual width
835 int otherColumn;
836 if ( adjust > 0 )
837 {
838 otherColumn = splitterColumn + 1;
839 if ( otherColumn == (int)m_colWidths.size() )
840 otherColumn = 0;
841 m_colWidths[splitterColumn] += adjust;
842 PropagateColSizeDec( otherColumn, adjust, 1 );
843 }
844 else
845 {
846 otherColumn = splitterColumn + 1;
847 if ( otherColumn == (int)m_colWidths.size() )
848 otherColumn = 0;
849 m_colWidths[otherColumn] -= adjust;
850 PropagateColSizeDec( splitterColumn, -adjust, -1 );
851 }
852 }
853 else
854 {
855 m_colWidths[splitterColumn] += adjust;
856 }
857
858 if ( splitterColumn == 0 )
859 m_fSplitterX = (double) newXPos;
860
861 if ( !fromAutoCenter )
862 {
863 // Don't allow initial splitter auto-positioning after this.
864 if ( pg->GetState() == this )
865 pg->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET);
866
867 CheckColumnWidths();
868 }
869 }
870
871 // Moves splitter so that all labels are visible, but just.
872 void wxPropertyGridPageState::SetSplitterLeft( bool subProps )
873 {
874 wxPropertyGrid* pg = GetGrid();
875 wxClientDC dc(pg);
876 dc.SetFont(pg->GetFont());
877
878 int maxW = GetColumnFitWidth(dc, m_properties, 0, subProps);
879
880 if ( maxW > 0 )
881 {
882 maxW += pg->m_marginWidth;
883 DoSetSplitterPosition( maxW );
884 }
885
886 pg->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
887 }
888
889 wxSize wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize) )
890 {
891 wxPropertyGrid* pg = GetGrid();
892 wxClientDC dc(pg);
893 dc.SetFont(pg->GetFont());
894
895 int marginWidth = pg->m_marginWidth;
896 int accWid = marginWidth;
897 int maxColWidth = 500;
898
899 for ( unsigned int col=0; col < GetColumnCount(); col++ )
900 {
901 int fitWid = GetColumnFitWidth(dc, m_properties, col, true);
902 int colMinWidth = GetColumnMinWidth(col);
903 if ( fitWid < colMinWidth )
904 fitWid = colMinWidth;
905 else if ( fitWid > maxColWidth )
906 fitWid = maxColWidth;
907
908 m_colWidths[col] = fitWid;
909
910 accWid += fitWid;
911 }
912
913 // Expand last one to fill the width
914 int remaining = m_width - accWid;
915 m_colWidths[GetColumnCount()-1] += remaining;
916
917 pg->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
918
919 int firstSplitterX = marginWidth + m_colWidths[0];
920 m_fSplitterX = (double) firstSplitterX;
921
922 // Don't allow initial splitter auto-positioning after this.
923 if ( pg->GetState() == this )
924 {
925 pg->SetSplitterPosition(firstSplitterX, false);
926 pg->Refresh();
927 }
928
929 int x, y;
930 pg->GetVirtualSize(&x, &y);
931
932 return wxSize(accWid, y);
933 }
934
935 void wxPropertyGridPageState::CheckColumnWidths( int widthChange )
936 {
937 if ( m_width == 0 )
938 return;
939
940 wxPropertyGrid* pg = GetGrid();
941
942 unsigned int i;
943 unsigned int lastColumn = m_colWidths.size() - 1;
944 int width = m_width;
945 int clientWidth = pg->GetClientSize().x;
946
947 //
948 // Column to reduce, if needed. Take last one that exceeds minimum width.
949 int reduceCol = -1;
950
951 wxLogTrace("propgrid",
952 wxS("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"),
953 width, clientWidth);
954
955 //
956 // Check min sizes
957 for ( i=0; i<m_colWidths.size(); i++ )
958 {
959 int min = GetColumnMinWidth(i);
960 if ( m_colWidths[i] <= min )
961 {
962 m_colWidths[i] = min;
963 }
964 else
965 {
966 // Always reduce the last column that is larger than minimum size
967 // (looks nicer, even with auto-centering enabled).
968 reduceCol = i;
969 }
970 }
971
972 int colsWidth = pg->m_marginWidth;
973 for ( i=0; i<m_colWidths.size(); i++ )
974 colsWidth += m_colWidths[i];
975
976 wxLogTrace("propgrid",
977 wxS(" HasVirtualWidth: %i colsWidth: %i"),
978 (int)pg->HasVirtualWidth(), colsWidth);
979
980 // Then mode-based requirement
981 if ( !pg->HasVirtualWidth() )
982 {
983 int widthHigher = width - colsWidth;
984
985 // Adapt colsWidth to width
986 if ( colsWidth < width )
987 {
988 // Increase column
989 wxLogTrace("propgrid",
990 wxS(" Adjust last column to %i"),
991 m_colWidths[lastColumn] + widthHigher);
992 m_colWidths[lastColumn] = m_colWidths[lastColumn] + widthHigher;
993 }
994 else if ( colsWidth > width )
995 {
996 // Reduce column
997 if ( reduceCol != -1 )
998 {
999 wxLogTrace("propgrid",
1000 wxT(" Reduce column %i (by %i)"),
1001 reduceCol, -widthHigher);
1002
1003 // Reduce widest column, and recheck
1004 m_colWidths[reduceCol] = m_colWidths[reduceCol] + widthHigher;
1005 CheckColumnWidths();
1006 }
1007 }
1008 }
1009 else
1010 {
1011 // Only check colsWidth against clientWidth
1012 if ( colsWidth < clientWidth )
1013 {
1014 m_colWidths[lastColumn] = m_colWidths[lastColumn] + (clientWidth-colsWidth);
1015 }
1016
1017 m_width = colsWidth;
1018
1019 // If width changed, recalculate virtual size
1020 if ( pg->GetState() == this )
1021 pg->RecalculateVirtualSize();
1022 }
1023
1024 for ( i=0; i<m_colWidths.size(); i++ )
1025 {
1026 wxLogTrace("propgrid", wxS("col%i: %i"), i, m_colWidths[i]);
1027 }
1028
1029 // Auto center splitter
1030 if ( !(pg->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER) &&
1031 m_colWidths.size() == 2 )
1032 {
1033 float centerX = (float)(pg->m_width/2);
1034 float splitterX;
1035
1036 if ( m_fSplitterX < 0.0 )
1037 {
1038 splitterX = centerX;
1039 }
1040 else if ( widthChange )
1041 {
1042 //float centerX = float(pg->GetSize().x) * 0.5;
1043
1044 // Recenter?
1045 splitterX = m_fSplitterX + (float(widthChange) * 0.5);
1046 float deviation = fabs(centerX - splitterX);
1047
1048 // If deviating from center, adjust towards it
1049 if ( deviation > 20.0 )
1050 {
1051 if ( splitterX > centerX)
1052 splitterX -= 2;
1053 else
1054 splitterX += 2;
1055 }
1056 }
1057 else
1058 {
1059 // No width change, just keep sure we keep splitter position intact
1060 splitterX = m_fSplitterX;
1061 float deviation = fabs(centerX - splitterX);
1062 if ( deviation > 50.0 )
1063 {
1064 splitterX = centerX;
1065 }
1066 }
1067
1068 DoSetSplitterPosition((int)splitterX, 0, false, true);
1069
1070 m_fSplitterX = splitterX; // needed to retain accuracy
1071 }
1072 }
1073
1074 void wxPropertyGridPageState::SetColumnCount( int colCount )
1075 {
1076 wxASSERT( colCount >= 2 );
1077 m_colWidths.SetCount( colCount, wxPG_DRAG_MARGIN );
1078 if ( m_colWidths.size() > (unsigned int)colCount )
1079 m_colWidths.RemoveAt( m_colWidths.size(), m_colWidths.size() - colCount );
1080
1081 if ( m_pPropGrid->GetState() == this )
1082 m_pPropGrid->RecalculateVirtualSize();
1083 else
1084 CheckColumnWidths();
1085 }
1086
1087 // Returns column index, -1 for margin
1088 int wxPropertyGridPageState::HitTestH( int x, int* pSplitterHit, int* pSplitterHitOffset ) const
1089 {
1090 int cx = GetGrid()->m_marginWidth;
1091 int col = -1;
1092 int prevSplitter = -1;
1093
1094 while ( x > cx )
1095 {
1096 col++;
1097 if ( col >= (int)m_colWidths.size() )
1098 {
1099 *pSplitterHit = -1;
1100 return col;
1101 }
1102 prevSplitter = cx;
1103 cx += m_colWidths[col];
1104 }
1105
1106 // Near prev. splitter
1107 if ( col >= 1 )
1108 {
1109 int diff = x - prevSplitter;
1110 if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 )
1111 {
1112 *pSplitterHit = col - 1;
1113 *pSplitterHitOffset = diff;
1114 return col;
1115 }
1116 }
1117
1118 // Near next splitter
1119 int nextSplitter = cx;
1120 if ( col < (int)(m_colWidths.size()-1) )
1121 {
1122 int diff = x - nextSplitter;
1123 if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 )
1124 {
1125 *pSplitterHit = col;
1126 *pSplitterHitOffset = diff;
1127 return col;
1128 }
1129 }
1130
1131 *pSplitterHit = -1;
1132 return col;
1133 }
1134
1135 // -----------------------------------------------------------------------
1136 // wxPropertyGridPageState property value setting and getting
1137 // -----------------------------------------------------------------------
1138
1139 bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty* p, const wxString& value )
1140 {
1141 if ( p )
1142 {
1143 int flags = wxPG_REPORT_ERROR|wxPG_FULL_VALUE|wxPG_PROGRAMMATIC_VALUE;
1144
1145 wxVariant variant = p->GetValueRef();
1146 bool res;
1147
1148 if ( p->GetMaxLength() <= 0 )
1149 res = p->StringToValue( variant, value, flags );
1150 else
1151 res = p->StringToValue( variant, value.Mid(0,p->GetMaxLength()), flags );
1152
1153 if ( res )
1154 {
1155 p->SetValue(variant);
1156 if ( p == m_pPropGrid->GetSelection() &&
1157 this == m_pPropGrid->GetState() )
1158 m_pPropGrid->RefreshEditor();
1159 }
1160
1161 return true;
1162 }
1163 return false;
1164 }
1165
1166 // -----------------------------------------------------------------------
1167
1168 bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty* p, wxVariant& value )
1169 {
1170 if ( p )
1171 {
1172 p->SetValue(value);
1173 if ( p == m_pPropGrid->GetSelection() &&
1174 this == m_pPropGrid->GetState() )
1175 m_pPropGrid->RefreshEditor();
1176
1177 return true;
1178 }
1179 return false;
1180 }
1181
1182 // -----------------------------------------------------------------------
1183
1184 bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty* p, wxObject* value )
1185 {
1186 if ( p )
1187 {
1188 // wnd_primary has to be given so the control can be updated as well.
1189 wxVariant v(value);
1190 DoSetPropertyValue(p, v);
1191 return true;
1192 }
1193 return false;
1194 }
1195
1196 // -----------------------------------------------------------------------
1197 // wxPropertyGridPageState property operations
1198 // -----------------------------------------------------------------------
1199
1200 bool wxPropertyGridPageState::DoIsPropertySelected( wxPGProperty* prop ) const
1201 {
1202 const wxArrayPGProperty& selection = m_selection;
1203
1204 for ( unsigned int i=0; i<selection.size(); i++ )
1205 {
1206 if ( selection[i] == prop )
1207 return true;
1208 }
1209
1210 return false;
1211 }
1212
1213 // -----------------------------------------------------------------------
1214
1215 void wxPropertyGridPageState::DoRemoveFromSelection( wxPGProperty* prop )
1216 {
1217 for ( unsigned int i=0; i<m_selection.size(); i++ )
1218 {
1219 if ( m_selection[i] == prop )
1220 {
1221 m_selection.erase( m_selection.begin() + i );
1222 return;
1223 }
1224 }
1225 }
1226
1227 // -----------------------------------------------------------------------
1228
1229 bool wxPropertyGridPageState::DoCollapse( wxPGProperty* p )
1230 {
1231 wxCHECK_MSG( p, false, wxT("invalid property id") );
1232
1233 if ( !p->GetChildCount() ) return false;
1234
1235 if ( !p->IsExpanded() ) return false;
1236
1237 p->SetExpanded(false);
1238
1239 VirtualHeightChanged();
1240
1241 return true;
1242 }
1243
1244 // -----------------------------------------------------------------------
1245
1246 bool wxPropertyGridPageState::DoExpand( wxPGProperty* p )
1247 {
1248 wxCHECK_MSG( p, false, wxT("invalid property id") );
1249
1250 if ( !p->GetChildCount() ) return false;
1251
1252 if ( p->IsExpanded() ) return false;
1253
1254 p->SetExpanded(true);
1255
1256 VirtualHeightChanged();
1257
1258 return true;
1259 }
1260
1261 // -----------------------------------------------------------------------
1262
1263 bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty* p, unsigned int flags )
1264 {
1265 if ( this == m_pPropGrid->GetState() )
1266 return m_pPropGrid->DoSelectProperty( p, flags );
1267
1268 DoSetSelection(p);
1269 return true;
1270 }
1271
1272 // -----------------------------------------------------------------------
1273
1274 bool wxPropertyGridPageState::DoHideProperty( wxPGProperty* p, bool hide, int flags )
1275 {
1276 if ( !hide )
1277 p->ClearFlag( wxPG_PROP_HIDDEN );
1278 else
1279 p->SetFlag( wxPG_PROP_HIDDEN );
1280
1281 if ( flags & wxPG_RECURSE )
1282 {
1283 unsigned int i;
1284 for ( i = 0; i < p->GetChildCount(); i++ )
1285 DoHideProperty(p->Item(i), hide, flags | wxPG_RECURSE_STARTS);
1286 }
1287
1288 VirtualHeightChanged();
1289
1290 return true;
1291 }
1292
1293 // -----------------------------------------------------------------------
1294
1295 bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty* p, bool enable )
1296 {
1297 if ( p )
1298 {
1299 if ( enable )
1300 {
1301 if ( !(p->m_flags & wxPG_PROP_DISABLED) )
1302 return false;
1303
1304 // Enabling
1305
1306 p->m_flags &= ~(wxPG_PROP_DISABLED);
1307 }
1308 else
1309 {
1310 if ( p->m_flags & wxPG_PROP_DISABLED )
1311 return false;
1312
1313 // Disabling
1314
1315 p->m_flags |= wxPG_PROP_DISABLED;
1316
1317 }
1318
1319 // Apply same to sub-properties as well
1320 unsigned int i;
1321 for ( i = 0; i < p->GetChildCount(); i++ )
1322 DoEnableProperty( p->Item(i), enable );
1323
1324 return true;
1325 }
1326 return false;
1327 }
1328
1329 // -----------------------------------------------------------------------
1330 // wxPropertyGridPageState wxVariant related routines
1331 // -----------------------------------------------------------------------
1332
1333 // Returns list of wxVariant objects (non-categories and non-sub-properties only).
1334 // Never includes sub-properties (unless they are parented by wxParentProperty).
1335 wxVariant wxPropertyGridPageState::DoGetPropertyValues( const wxString& listname,
1336 wxPGProperty* baseparent,
1337 long flags ) const
1338 {
1339 wxPGProperty* pwc = (wxPGProperty*) baseparent;
1340
1341 // Root is the default base-parent.
1342 if ( !pwc )
1343 pwc = m_properties;
1344
1345 wxVariantList tempList;
1346 wxVariant v( tempList, listname );
1347
1348 if ( pwc->GetChildCount() )
1349 {
1350 if ( flags & wxPG_KEEP_STRUCTURE )
1351 {
1352 wxASSERT( !pwc->HasFlag(wxPG_PROP_AGGREGATE) );
1353
1354 size_t i;
1355 for ( i=0; i<pwc->GetChildCount(); i++ )
1356 {
1357 wxPGProperty* p = pwc->Item(i);
1358 if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) )
1359 {
1360 wxVariant variant = p->GetValue();
1361 variant.SetName( p->GetBaseName() );
1362 v.Append( variant );
1363 }
1364 else
1365 {
1366 v.Append( DoGetPropertyValues(p->m_name,p,flags|wxPG_KEEP_STRUCTURE) );
1367 }
1368 if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() )
1369 v.Append( p->GetAttributesAsList() );
1370 }
1371 }
1372 else
1373 {
1374 wxPropertyGridConstIterator it( this, wxPG_ITERATE_DEFAULT, pwc->Item(0) );
1375 it.SetBaseParent( pwc );
1376
1377 for ( ; !it.AtEnd(); it.Next() )
1378 {
1379 const wxPGProperty* p = it.GetProperty();
1380
1381 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1382 if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) )
1383 {
1384 wxVariant variant = p->GetValue();
1385 variant.SetName( p->GetName() );
1386 v.Append( variant );
1387 if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() )
1388 v.Append( p->GetAttributesAsList() );
1389 }
1390 }
1391 }
1392 }
1393
1394 return v;
1395 }
1396
1397 // -----------------------------------------------------------------------
1398
1399 void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList& list, wxPGProperty* defaultCategory )
1400 {
1401 unsigned char origFrozen = 1;
1402
1403 if ( m_pPropGrid->GetState() == this )
1404 {
1405 origFrozen = m_pPropGrid->m_frozen;
1406 if ( !origFrozen ) m_pPropGrid->Freeze();
1407 }
1408
1409 wxPropertyCategory* use_category = (wxPropertyCategory*)defaultCategory;
1410
1411 if ( !use_category )
1412 use_category = (wxPropertyCategory*)m_properties;
1413
1414 // Let's iterate over the list of variants.
1415 wxVariantList::const_iterator node;
1416 int numSpecialEntries = 0;
1417
1418 //
1419 // Second pass for special entries
1420 for ( node = list.begin(); node != list.end(); ++node )
1421 {
1422 wxVariant *current = (wxVariant*)*node;
1423
1424 // Make sure it is wxVariant.
1425 wxASSERT( current );
1426 wxASSERT( wxStrcmp(current->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1427
1428 const wxString& name = current->GetName();
1429 if ( name.length() > 0 )
1430 {
1431 //
1432 // '@' signified a special entry
1433 if ( name[0] == wxS('@') )
1434 {
1435 numSpecialEntries++;
1436 }
1437 else
1438 {
1439 wxPGProperty* foundProp = BaseGetPropertyByName(name);
1440 if ( foundProp )
1441 {
1442 wxPGProperty* p = foundProp;
1443
1444 // If it was a list, we still have to go through it.
1445 if ( wxStrcmp(current->GetType(), wxS("list")) == 0 )
1446 {
1447 DoSetPropertyValues( current->GetList(),
1448 p->IsCategory()?p:(NULL)
1449 );
1450 }
1451 else
1452 {
1453 wxASSERT_LEVEL_2_MSG(
1454 wxStrcmp(current->GetType(), p->GetValue().GetType()) == 0,
1455 wxString::Format(
1456 wxS("setting value of property \"%s\" from variant"),
1457 p->GetName().c_str())
1458 );
1459
1460 p->SetValue(*current);
1461 }
1462 }
1463 else
1464 {
1465 // Is it list?
1466 if ( current->GetType() != wxS("list") )
1467 {
1468 // Not.
1469 }
1470 else
1471 {
1472 // Yes, it is; create a sub category and append contents there.
1473 wxPGProperty* newCat = DoInsert(use_category,-1,new wxPropertyCategory(current->GetName(),wxPG_LABEL));
1474 DoSetPropertyValues( current->GetList(), newCat );
1475 }
1476 }
1477 }
1478 }
1479 }
1480
1481 if ( numSpecialEntries )
1482 {
1483 for ( node = list.begin(); node != list.end(); ++node )
1484 {
1485 wxVariant *current = (wxVariant*)*node;
1486
1487 const wxString& name = current->GetName();
1488 if ( name.length() > 0 )
1489 {
1490 //
1491 // '@' signified a special entry
1492 if ( name[0] == wxS('@') )
1493 {
1494 numSpecialEntries--;
1495
1496 size_t pos2 = name.rfind(wxS('@'));
1497 if ( pos2 > 0 && pos2 < (name.size()-1) )
1498 {
1499 wxString propName = name.substr(1, pos2-1);
1500 wxString entryType = name.substr(pos2+1, wxString::npos);
1501
1502 if ( entryType == wxS("attr") )
1503 {
1504 //
1505 // List of attributes
1506 wxPGProperty* foundProp = BaseGetPropertyByName(propName);
1507 if ( foundProp )
1508 {
1509 wxASSERT( current->GetType() == wxPG_VARIANT_TYPE_LIST );
1510
1511 wxVariantList& list2 = current->GetList();
1512 wxVariantList::const_iterator node2;
1513
1514 for ( node2 = list2.begin(); node2 != list2.end(); ++node2 )
1515 {
1516 wxVariant *attr = (wxVariant*)*node2;
1517 foundProp->SetAttribute( attr->GetName(), *attr );
1518 }
1519 }
1520 else
1521 {
1522 // ERROR: No such property: 'propName'
1523 }
1524 }
1525 }
1526 else
1527 {
1528 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1529 }
1530 }
1531 }
1532
1533 if ( !numSpecialEntries )
1534 break;
1535 }
1536 }
1537
1538 if ( !origFrozen )
1539 {
1540 m_pPropGrid->Thaw();
1541
1542 if ( this == m_pPropGrid->GetState() )
1543 m_pPropGrid->RefreshEditor();
1544 }
1545
1546 }
1547
1548 // -----------------------------------------------------------------------
1549 // wxPropertyGridPageState property adding and removal
1550 // -----------------------------------------------------------------------
1551
1552 bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty* property,
1553 wxPGProperty* scheduledParent )
1554 {
1555 wxPropertyGrid* propGrid = m_pPropGrid;
1556
1557 // This will allow better behavior.
1558 if ( scheduledParent == m_properties )
1559 scheduledParent = NULL;
1560
1561 if ( scheduledParent && !scheduledParent->IsCategory() )
1562 {
1563 wxASSERT_MSG( property->GetBaseName().length(),
1564 "Property's children must have unique, non-empty names within their scope" );
1565 }
1566
1567 property->m_parentState = this;
1568
1569 if ( property->IsCategory() )
1570 {
1571
1572 // Parent of a category must be either root or another category
1573 // (otherwise Bad Things might happen).
1574 wxASSERT_MSG( scheduledParent == NULL ||
1575 scheduledParent == m_properties ||
1576 scheduledParent->IsCategory(),
1577 wxT("Parent of a category must be either root or another category."));
1578
1579 // If we already have category with same name, delete given property
1580 // and use it instead as most recent caption item.
1581 wxPGProperty* found_id = BaseGetPropertyByName( property->GetBaseName() );
1582 if ( found_id )
1583 {
1584 wxPropertyCategory* pwc = (wxPropertyCategory*) found_id;
1585 if ( pwc->IsCategory() ) // Must be a category.
1586 {
1587 delete property;
1588 m_currentCategory = pwc;
1589 return false;
1590 }
1591 }
1592 }
1593
1594 #if wxDEBUG_LEVEL
1595 // Warn for identical names in debug mode.
1596 if ( BaseGetPropertyByName(property->GetName()) &&
1597 (!scheduledParent || scheduledParent->IsCategory()) )
1598 {
1599 wxFAIL_MSG(wxString::Format(
1600 "wxPropertyGrid item with name \"%s\" already exists",
1601 property->GetName()));
1602
1603 wxPGGlobalVars->m_warnings++;
1604 }
1605 #endif // wxDEBUG_LEVEL
1606
1607 // Make sure nothing is selected.
1608 if ( propGrid )
1609 propGrid->ClearSelection(false);
1610
1611 // NULL parent == root parent
1612 if ( !scheduledParent )
1613 scheduledParent = DoGetRoot();
1614
1615 property->m_parent = scheduledParent;
1616
1617 property->InitAfterAdded(this, propGrid);
1618
1619 if ( property->IsCategory() )
1620 {
1621 wxPropertyCategory* pc = wxStaticCast(property, wxPropertyCategory);
1622
1623 m_currentCategory = pc;
1624
1625 // Calculate text extent for category caption
1626 if ( propGrid )
1627 pc->CalculateTextExtent(propGrid, propGrid->GetCaptionFont());
1628 }
1629
1630 return true;
1631 }
1632
1633 // -----------------------------------------------------------------------
1634
1635 wxPGProperty* wxPropertyGridPageState::DoAppend( wxPGProperty* property )
1636 {
1637 wxPropertyCategory* cur_cat = m_currentCategory;
1638 if ( property->IsCategory() )
1639 cur_cat = NULL;
1640
1641 return DoInsert( cur_cat, -1, property );
1642 }
1643
1644 // -----------------------------------------------------------------------
1645
1646 wxPGProperty* wxPropertyGridPageState::DoInsert( wxPGProperty* parent, int index, wxPGProperty* property )
1647 {
1648 if ( !parent )
1649 parent = m_properties;
1650
1651 wxCHECK_MSG( !parent->HasFlag(wxPG_PROP_AGGREGATE),
1652 wxNullProperty,
1653 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1654
1655 bool res = PrepareToAddItem( property, (wxPropertyCategory*)parent );
1656
1657 // PrepareToAddItem() may just decide to use use current category
1658 // instead of adding new one.
1659 if ( !res )
1660 return m_currentCategory;
1661
1662 bool parentIsRoot = parent->IsRoot();
1663 bool parentIsCategory = parent->IsCategory();
1664
1665 // Note that item must be added into current mode later.
1666
1667 // If parent is wxParentProperty, just stick it in...
1668 // If parent is root (m_properties), then...
1669 // In categoric mode: Add as last item in m_abcArray (if not category).
1670 // Add to given index in m_regularArray.
1671 // In non-cat mode: Add as last item in m_regularArray.
1672 // Add to given index in m_abcArray.
1673 // If parent is category, then...
1674 // 1) Add to given category in given index.
1675 // 2) Add as last item in m_abcArray.
1676
1677 if ( m_properties == &m_regularArray )
1678 {
1679 // We are currently in Categorized mode
1680
1681 // Only add non-categories to m_abcArray.
1682 if ( m_abcArray && !property->IsCategory() &&
1683 (parentIsCategory || parentIsRoot) )
1684 {
1685 m_abcArray->DoAddChild( property, -1, false );
1686 }
1687
1688 // Add to current mode.
1689 parent->DoAddChild( property, index, true );
1690 }
1691 else
1692 {
1693 // We are currently in Non-categorized/Alphabetic mode
1694
1695 if ( parentIsCategory )
1696 // Parent is category.
1697 parent->DoAddChild( property, index, false );
1698 else if ( parentIsRoot )
1699 // Parent is root.
1700 m_regularArray.DoAddChild( property, -1, false );
1701
1702 // Add to current mode
1703 if ( !property->IsCategory() )
1704 m_abcArray->DoAddChild( property, index, true );
1705 }
1706
1707 // category stuff
1708 if ( property->IsCategory() )
1709 {
1710 // This is a category caption item.
1711
1712 // Last caption is not the bottom one (this info required by append)
1713 m_lastCaptionBottomnest = 0;
1714 }
1715
1716 // Only add name to hashmap if parent is root or category
1717 if ( property->m_name.length() &&
1718 (parentIsCategory || parentIsRoot) )
1719 m_dictName[property->m_name] = (void*) property;
1720
1721 VirtualHeightChanged();
1722
1723 property->UpdateParentValues();
1724
1725 m_itemsAdded = 1;
1726
1727 return property;
1728 }
1729
1730 // -----------------------------------------------------------------------
1731
1732 void wxPropertyGridPageState::DoDelete( wxPGProperty* item, bool doDelete )
1733 {
1734 wxCHECK_RET( item->GetParent(),
1735 wxT("this property was already deleted") );
1736
1737 wxCHECK_RET( item != &m_regularArray && item != m_abcArray,
1738 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1739
1740 unsigned int indinparent = item->GetIndexInParent();
1741
1742 wxPGProperty* pwc = (wxPGProperty*)item;
1743 wxPGProperty* parent = item->GetParent();
1744
1745 wxCHECK_RET( !parent->HasFlag(wxPG_PROP_AGGREGATE),
1746 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1747
1748 wxASSERT( item->GetParentState() == this );
1749
1750 wxPropertyGrid* pg = GetGrid();
1751
1752 if ( DoIsPropertySelected(item) )
1753 {
1754 if ( pg && pg->GetState() == this )
1755 {
1756 pg->DoRemoveFromSelection(item,
1757 wxPG_SEL_DELETING|wxPG_SEL_NOVALIDATE);
1758 }
1759 else
1760 {
1761 DoRemoveFromSelection(item);
1762 }
1763 }
1764
1765 item->SetFlag(wxPG_PROP_BEING_DELETED);
1766
1767 // Delete children
1768 if ( item->GetChildCount() && !item->HasFlag(wxPG_PROP_AGGREGATE) )
1769 {
1770 // deleting a category
1771 if ( item->IsCategory() )
1772 {
1773 if ( pwc == m_currentCategory )
1774 m_currentCategory = NULL;
1775 }
1776
1777 item->DeleteChildren();
1778 }
1779
1780 if ( !IsInNonCatMode() )
1781 {
1782 // categorized mode - non-categorized array
1783
1784 // Remove from non-cat array
1785 if ( !item->IsCategory() &&
1786 (parent->IsCategory() || parent->IsRoot()) )
1787 {
1788 if ( m_abcArray )
1789 m_abcArray->RemoveChild(item);
1790 }
1791
1792 // categorized mode - categorized array
1793 wxArrayPGProperty& parentsChildren = parent->m_children;
1794 parentsChildren.erase( parentsChildren.begin() + indinparent );
1795 item->m_parent->FixIndicesOfChildren();
1796 }
1797 else
1798 {
1799 // non-categorized mode - categorized array
1800
1801 // We need to find location of item.
1802 wxPGProperty* cat_parent = &m_regularArray;
1803 int cat_index = m_regularArray.GetChildCount();
1804 size_t i;
1805 for ( i = 0; i < m_regularArray.GetChildCount(); i++ )
1806 {
1807 wxPGProperty* p = m_regularArray.Item(i);
1808 if ( p == item ) { cat_index = i; break; }
1809 if ( p->IsCategory() )
1810 {
1811 int subind = ((wxPGProperty*)p)->Index(item);
1812 if ( subind != wxNOT_FOUND )
1813 {
1814 cat_parent = ((wxPGProperty*)p);
1815 cat_index = subind;
1816 break;
1817 }
1818 }
1819 }
1820 cat_parent->m_children.erase(cat_parent->m_children.begin()+cat_index);
1821
1822 // non-categorized mode - non-categorized array
1823 if ( !item->IsCategory() )
1824 {
1825 wxASSERT( item->m_parent == m_abcArray );
1826 wxArrayPGProperty& parentsChildren = item->m_parent->m_children;
1827 parentsChildren.erase(parentsChildren.begin() + indinparent);
1828 item->m_parent->FixIndicesOfChildren(indinparent);
1829 }
1830 }
1831
1832 if ( item->GetBaseName().length() &&
1833 (parent->IsCategory() || parent->IsRoot()) )
1834 m_dictName.erase(item->GetBaseName());
1835
1836 // We need to clear parent grid's m_propHover, if it matches item
1837 if ( pg && pg->m_propHover == item )
1838 pg->m_propHover = NULL;
1839
1840 // We can actually delete it now
1841 if ( doDelete )
1842 delete item;
1843
1844 m_itemsAdded = 1; // Not a logical assignment (but required nonetheless).
1845
1846 VirtualHeightChanged();
1847 }
1848
1849 // -----------------------------------------------------------------------
1850
1851 #endif // wxUSE_PROPGRID