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