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