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