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