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