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