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