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