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