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