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