]> git.saurik.com Git - wxWidgets.git/blame - src/propgrid/propgridpagestate.cpp
Use correct array size for weekday names.
[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
ea5af9c5 7// RCS-ID: $Id$
1c4293cb
VZ
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
f4bc1aa2
JS
19#if wxUSE_PROPGRID
20
1c4293cb
VZ
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"
1c4293cb
VZ
32 #include "wx/pen.h"
33 #include "wx/brush.h"
1c4293cb 34 #include "wx/intl.h"
af276477 35 #include "wx/stopwatch.h"
1c4293cb
VZ
36#endif
37
38// This define is necessary to prevent macro clearing
39#define __wxPG_SOURCE_FILE__
40
3b211af1
SC
41#include "wx/propgrid/propgridpagestate.h"
42#include "wx/propgrid/propgrid.h"
43#include "wx/propgrid/editors.h"
1c4293cb 44
1c4293cb
VZ
45#define wxPG_DEFAULT_SPLITTERX 110
46
47
48// -----------------------------------------------------------------------
49// wxPropertyGridIterator
50// -----------------------------------------------------------------------
51
52void 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
75void wxPropertyGridIteratorBase::Init( wxPropertyGridPageState* state, int flags, int startPos, int dir )
76{
b7bc9d80 77 wxPGProperty* property = NULL;
1c4293cb
VZ
78
79 if ( startPos == wxTOP )
80 {
1c4293cb
VZ
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 {
b7bc9d80 92 wxFAIL_MSG("Only supported starting positions are wxTOP and wxBOTTOM");
1c4293cb
VZ
93 }
94
95 Init( state, flags, property, dir );
96}
97
98void 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
107void 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
152void 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
202wxPropertyGridPageState::wxPropertyGridPageState()
203{
d3b9f782 204 m_pPropGrid = NULL;
1c4293cb
VZ
205 m_regularArray.SetParentState(this);
206 m_properties = &m_regularArray;
d3b9f782
VZ
207 m_abcArray = NULL;
208 m_currentCategory = NULL;
1c4293cb
VZ
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;
58935d4a
JS
218
219 // By default, we only have the 'value' column editable
220 m_editableColumns.push_back(1);
1c4293cb
VZ
221}
222
223// -----------------------------------------------------------------------
224
225wxPropertyGridPageState::~wxPropertyGridPageState()
226{
227 delete m_abcArray;
228}
229
230// -----------------------------------------------------------------------
231
232void wxPropertyGridPageState::InitNonCatMode()
233{
234 if ( !m_abcArray )
235 {
94b8ecf1 236 m_abcArray = new wxPGRootProperty(wxS("<Root_NonCat>"));
1c4293cb
VZ
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 {
91c818f8
JS
250 //
251 // Prepare m_abcArray
252 wxPropertyGridIterator it( this, wxPG_ITERATE_PROPERTIES );
1c4293cb
VZ
253
254 for ( ; !it.AtEnd(); it.Next() )
255 {
256 wxPGProperty* p = it.GetProperty();
257 wxPGProperty* parent = p->GetParent();
91c818f8 258 if ( parent->IsCategory() || parent->IsRoot() )
1c4293cb 259 {
48a32cf6 260 m_abcArray->DoAddChild(p);
1c4293cb
VZ
261 p->m_parent = &m_regularArray;
262 }
263 }
264 }
265
266 m_properties = oldProperties;
267}
268
269// -----------------------------------------------------------------------
270
271void wxPropertyGridPageState::DoClear()
272{
0c35994d 273 if ( m_pPropGrid && m_pPropGrid->GetState() == this )
8dee26e1
JS
274 {
275 m_pPropGrid->ClearSelection(false);
276 }
277 else
278 {
fc72fab6 279 m_selection.clear();
8dee26e1
JS
280 }
281
1c4293cb
VZ
282 m_regularArray.Empty();
283 if ( m_abcArray )
284 m_abcArray->Empty();
285
286 m_dictName.clear();
287
d3b9f782 288 m_currentCategory = NULL;
1c4293cb
VZ
289 m_lastCaptionBottomnest = 1;
290 m_itemsAdded = 0;
291
292 m_virtualHeight = 0;
293 m_vhCalcPending = 0;
1c4293cb
VZ
294}
295
296// -----------------------------------------------------------------------
297
298void 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
318void 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
331void 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
8034d81d 358 if ( timeSinceCreation < 250 )
1c4293cb 359 {
8034d81d 360 if ( m_properties->GetChildCount() )
1c4293cb
VZ
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
378wxPGProperty* wxPropertyGridPageState::GetLastItem( int flags )
379{
380 if ( !m_properties->GetChildCount() )
d3b9f782 381 return NULL;
1c4293cb
VZ
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 );
6bce2ad9
VZ
395 for ( ; !it.AtEnd(); it.Prev() )
396 ;
1c4293cb
VZ
397 pwc = (wxPGProperty*) it.GetProperty();
398 }
399
400 return pwc;
401}
402
403wxPropertyCategory* 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
d3b9f782 415 return NULL;
1c4293cb
VZ
416}
417
418// -----------------------------------------------------------------------
419// wxPropertyGridPageState GetPropertyXXX methods
420// -----------------------------------------------------------------------
421
422wxPGProperty* 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
449wxPGProperty* 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;
d3b9f782 455 return NULL;
1c4293cb
VZ
456}
457
020b0041
JS
458// -----------------------------------------------------------------------
459
460void wxPropertyGridPageState::DoSetPropertyName( wxPGProperty* p,
461 const wxString& newName )
462{
463 wxCHECK_RET( p, wxT("invalid property id") );
464
26740086
JS
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 }
020b0041
JS
474
475 p->DoSetName(newName);
476}
477
1c4293cb
VZ
478// -----------------------------------------------------------------------
479// wxPropertyGridPageState global operations
480// -----------------------------------------------------------------------
481
482// -----------------------------------------------------------------------
483// Item iteration macros
484// NB: Nowadays only needed for alphabetic/categoric mode switching.
485// -----------------------------------------------------------------------
486
b7bc9d80 487//#define II_INVALID_I 0x00FFFFFF
1c4293cb
VZ
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
b7bc9d80 498#if 0
1c4293cb
VZ
499#define ITEM_ITERATION_INIT(startparent, startindex, state) \
500 parent = startparent; \
501 i = (unsigned int)startindex; \
d3b9f782 502 if ( parent == NULL ) \
1c4293cb
VZ
503 { \
504 parent = state->m_properties; \
505 i = 0; \
506 }
b7bc9d80 507#endif
1c4293cb
VZ
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
532bool 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
43396981
JS
611static 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
620static int wxPG_SortFunc_ByLabel(wxPGProperty **pp1, wxPGProperty **pp2)
1c4293cb 621{
43396981
JS
622 wxPGProperty *p1 = *pp1;
623 wxPGProperty *p2 = *pp2;
624 return p1->GetLabel().CmpNoCase( p2->GetLabel() );
1c4293cb
VZ
625}
626
7f3f8f1e
JS
627#if 0
628//
629// For wxVector w/ wxUSE_STL=1, you would use code like this instead:
630//
631
632#include <algorithm>
633
634static 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
641static bool wxPG_SortFunc_ByLabel(wxPGProperty *p1, wxPGProperty *p2)
642{
643 return p1->GetLabel().CmpNoCase( p2->GetLabel() ) < 0;
644}
d8c74d04
JS
645#endif
646
43396981 647void wxPropertyGridPageState::DoSortChildren( wxPGProperty* p,
0eb877f2 648 int flags )
1c4293cb
VZ
649{
650 if ( !p )
43396981 651 p = m_properties;
1c4293cb 652
0eb877f2 653 // Can only sort items with children
1c4293cb
VZ
654 if ( !p->GetChildCount() )
655 return;
656
0eb877f2
JS
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() )
1c4293cb
VZ
663 return;
664
7f3f8f1e
JS
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 //
43396981
JS
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);
d8c74d04 680#endif
1c4293cb 681
0eb877f2 682 // Fix indices
43396981 683 p->FixIndicesOfChildren();
1c4293cb 684
0eb877f2 685 if ( flags & wxPG_RECURSE )
43396981 686 {
0eb877f2 687 // Apply sort recursively
43396981 688 for ( unsigned int i=0; i<p->GetChildCount(); i++ )
0eb877f2 689 DoSortChildren(p->Item(i), flags);
43396981 690 }
1c4293cb
VZ
691}
692
693// -----------------------------------------------------------------------
694
0eb877f2 695void wxPropertyGridPageState::DoSort( int flags )
1c4293cb 696{
0eb877f2 697 DoSortChildren( m_properties, flags | wxPG_RECURSE );
1c4293cb 698
94b8ecf1
JS
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.
1c4293cb
VZ
702}
703
0eb877f2
JS
704// -----------------------------------------------------------------------
705
706bool 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
1c4293cb
VZ
720// -----------------------------------------------------------------------
721// wxPropertyGridPageState splitter, column and hittest functions
722// -----------------------------------------------------------------------
723
724wxPGProperty* wxPropertyGridPageState::DoGetItemAtY( int y ) const
725{
726 // Outside?
727 if ( y < 0 )
d3b9f782 728 return NULL;
1c4293cb
VZ
729
730 unsigned int a = 0;
731 return m_properties->GetItemAtY(y, GetGrid()->m_lineHeight, &a);
732}
733
734// -----------------------------------------------------------------------
735
0ee31682
JS
736wxPropertyGridHitTestResult
737wxPropertyGridPageState::HitTest( const wxPoint&pt ) const
1c4293cb
VZ
738{
739 wxPropertyGridHitTestResult result;
0ee31682
JS
740 result.m_column = HitTestH( pt.x, &result.m_splitter,
741 &result.m_splitterHitOffset );
742 result.m_property = DoGetItemAtY( pt.y );
1c4293cb
VZ
743 return result;
744}
745
746// -----------------------------------------------------------------------
747
748// Used by SetSplitterLeft() and DotFitColumns()
749int 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 {
d7e2b522
JS
764 const wxPGCell* cell = NULL;
765 wxString text;
766 p->GetDisplayInfo(col, -1, 0, &text, &cell);
767 dc.GetTextExtent(text, &w, &h);
1c4293cb
VZ
768 if ( col == 0 )
769 w += ( ((int)p->m_depth-1) * pg->m_subgroup_extramargin );
770
771 //
772 // TODO: Add bitmap support.
773
774 w += (wxPG_XBEFORETEXT*2);
775
776 if ( w > maxW )
777 maxW = w;
778 }
779
780 if ( p->GetChildCount() &&
781 ( subProps || p->IsCategory() ) )
782 {
783 w = GetColumnFitWidth( dc, p, col, subProps );
784
785 if ( w > maxW )
786 maxW = w;
787 }
788 }
789
790 return maxW;
791}
792
793int wxPropertyGridPageState::DoGetSplitterPosition( int splitterColumn ) const
794{
795 int n = GetGrid()->m_marginWidth;
796 int i;
797 for ( i=0; i<=splitterColumn; i++ )
798 n += m_colWidths[i];
799 return n;
800}
801
802int wxPropertyGridPageState::GetColumnMinWidth( int WXUNUSED(column) ) const
803{
804 return wxPG_DRAG_MARGIN;
805}
806
807void wxPropertyGridPageState::PropagateColSizeDec( int column, int decrease, int dir )
808{
809 int origWidth = m_colWidths[column];
810 m_colWidths[column] -= decrease;
811 int min = GetColumnMinWidth(column);
812 int more = 0;
813 if ( m_colWidths[column] < min )
814 {
815 more = decrease - (origWidth - min);
816 m_colWidths[column] = min;
817 }
818
819 //
820 // FIXME: Causes erratic splitter changing, so as a workaround
821 // disabled if two or less columns.
822
823 if ( m_colWidths.size() <= 2 )
824 return;
825
826 column += dir;
827 if ( more && column < (int)m_colWidths.size() && column >= 0 )
828 PropagateColSizeDec( column, more, dir );
829}
830
831void wxPropertyGridPageState::DoSetSplitterPosition( int newXPos, int splitterColumn, bool WXUNUSED(allPages), bool fromAutoCenter )
832{
833 wxPropertyGrid* pg = GetGrid();
834
835 int adjust = newXPos - DoGetSplitterPosition(splitterColumn);
836
837 if ( !pg->HasVirtualWidth() )
838 {
839 // No virtual width
840 int otherColumn;
841 if ( adjust > 0 )
842 {
843 otherColumn = splitterColumn + 1;
844 if ( otherColumn == (int)m_colWidths.size() )
845 otherColumn = 0;
846 m_colWidths[splitterColumn] += adjust;
847 PropagateColSizeDec( otherColumn, adjust, 1 );
848 }
849 else
850 {
851 otherColumn = splitterColumn + 1;
852 if ( otherColumn == (int)m_colWidths.size() )
853 otherColumn = 0;
854 m_colWidths[otherColumn] -= adjust;
855 PropagateColSizeDec( splitterColumn, -adjust, -1 );
856 }
857 }
858 else
859 {
860 m_colWidths[splitterColumn] += adjust;
861 }
862
863 if ( splitterColumn == 0 )
864 m_fSplitterX = (double) newXPos;
865
866 if ( !fromAutoCenter )
867 {
868 // Don't allow initial splitter auto-positioning after this.
869 if ( pg->GetState() == this )
870 pg->SetInternalFlag(wxPG_FL_SPLITTER_PRE_SET);
871
872 CheckColumnWidths();
873 }
874}
875
876// Moves splitter so that all labels are visible, but just.
877void wxPropertyGridPageState::SetSplitterLeft( bool subProps )
878{
879 wxPropertyGrid* pg = GetGrid();
880 wxClientDC dc(pg);
2197ec80 881 dc.SetFont(pg->GetFont());
1c4293cb
VZ
882
883 int maxW = GetColumnFitWidth(dc, m_properties, 0, subProps);
884
885 if ( maxW > 0 )
886 {
887 maxW += pg->m_marginWidth;
888 DoSetSplitterPosition( maxW );
889 }
890
891 pg->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
892}
893
894wxSize wxPropertyGridPageState::DoFitColumns( bool WXUNUSED(allowGridResize) )
895{
896 wxPropertyGrid* pg = GetGrid();
897 wxClientDC dc(pg);
2197ec80 898 dc.SetFont(pg->GetFont());
1c4293cb
VZ
899
900 int marginWidth = pg->m_marginWidth;
901 int accWid = marginWidth;
902 int maxColWidth = 500;
903
904 for ( unsigned int col=0; col < GetColumnCount(); col++ )
905 {
906 int fitWid = GetColumnFitWidth(dc, m_properties, col, true);
907 int colMinWidth = GetColumnMinWidth(col);
908 if ( fitWid < colMinWidth )
909 fitWid = colMinWidth;
910 else if ( fitWid > maxColWidth )
911 fitWid = maxColWidth;
912
913 m_colWidths[col] = fitWid;
914
915 accWid += fitWid;
916 }
917
918 // Expand last one to fill the width
919 int remaining = m_width - accWid;
920 m_colWidths[GetColumnCount()-1] += remaining;
921
922 pg->SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
923
924 int firstSplitterX = marginWidth + m_colWidths[0];
925 m_fSplitterX = (double) firstSplitterX;
926
927 // Don't allow initial splitter auto-positioning after this.
928 if ( pg->GetState() == this )
929 {
930 pg->SetSplitterPosition(firstSplitterX, false);
931 pg->Refresh();
932 }
933
934 int x, y;
935 pg->GetVirtualSize(&x, &y);
936
937 return wxSize(accWid, y);
938}
939
940void wxPropertyGridPageState::CheckColumnWidths( int widthChange )
941{
942 if ( m_width == 0 )
943 return;
944
945 wxPropertyGrid* pg = GetGrid();
946
1c4293cb
VZ
947 unsigned int i;
948 unsigned int lastColumn = m_colWidths.size() - 1;
949 int width = m_width;
950 int clientWidth = pg->GetClientSize().x;
951
952 //
953 // Column to reduce, if needed. Take last one that exceeds minimum width.
1c4293cb 954 int reduceCol = -1;
1c4293cb 955
4b6a582b
VZ
956 wxLogTrace("propgrid",
957 wxS("ColumnWidthCheck (virtualWidth: %i, clientWidth: %i)"),
958 width, clientWidth);
1c4293cb
VZ
959
960 //
961 // Check min sizes
962 for ( i=0; i<m_colWidths.size(); i++ )
963 {
964 int min = GetColumnMinWidth(i);
965 if ( m_colWidths[i] <= min )
966 {
967 m_colWidths[i] = min;
1c4293cb
VZ
968 }
969 else
970 {
bd6ffa9f
JS
971 // Always reduce the last column that is larger than minimum size
972 // (looks nicer, even with auto-centering enabled).
973 reduceCol = i;
1c4293cb
VZ
974 }
975 }
976
977 int colsWidth = pg->m_marginWidth;
978 for ( i=0; i<m_colWidths.size(); i++ )
979 colsWidth += m_colWidths[i];
980
4b6a582b
VZ
981 wxLogTrace("propgrid",
982 wxS(" HasVirtualWidth: %i colsWidth: %i"),
983 (int)pg->HasVirtualWidth(), colsWidth);
1c4293cb
VZ
984
985 // Then mode-based requirement
986 if ( !pg->HasVirtualWidth() )
987 {
988 int widthHigher = width - colsWidth;
989
990 // Adapt colsWidth to width
991 if ( colsWidth < width )
992 {
993 // Increase column
4b6a582b
VZ
994 wxLogTrace("propgrid",
995 wxS(" Adjust last column to %i"),
996 m_colWidths[lastColumn] + widthHigher);
1c4293cb
VZ
997 m_colWidths[lastColumn] = m_colWidths[lastColumn] + widthHigher;
998 }
999 else if ( colsWidth > width )
1000 {
1001 // Reduce column
1002 if ( reduceCol != -1 )
1003 {
4b6a582b
VZ
1004 wxLogTrace("propgrid",
1005 wxT(" Reduce column %i (by %i)"),
1006 reduceCol, -widthHigher);
1007
1c4293cb
VZ
1008 // Reduce widest column, and recheck
1009 m_colWidths[reduceCol] = m_colWidths[reduceCol] + widthHigher;
1010 CheckColumnWidths();
1011 }
1012 }
1013 }
1014 else
1015 {
1016 // Only check colsWidth against clientWidth
1017 if ( colsWidth < clientWidth )
1018 {
1019 m_colWidths[lastColumn] = m_colWidths[lastColumn] + (clientWidth-colsWidth);
1020 }
1021
1022 m_width = colsWidth;
1023
1024 // If width changed, recalculate virtual size
1025 if ( pg->GetState() == this )
1026 pg->RecalculateVirtualSize();
1027 }
1028
4b6a582b
VZ
1029 for ( i=0; i<m_colWidths.size(); i++ )
1030 {
1031 wxLogTrace("propgrid", wxS("col%i: %i"), i, m_colWidths[i]);
1032 }
1c4293cb
VZ
1033
1034 // Auto center splitter
1035 if ( !(pg->GetInternalFlags() & wxPG_FL_DONT_CENTER_SPLITTER) &&
1036 m_colWidths.size() == 2 )
1037 {
1038 float centerX = (float)(pg->m_width/2);
1039 float splitterX;
1040
1041 if ( m_fSplitterX < 0.0 )
1042 {
1043 splitterX = centerX;
1044 }
1045 else if ( widthChange )
1046 {
1047 //float centerX = float(pg->GetSize().x) * 0.5;
1048
1049 // Recenter?
1050 splitterX = m_fSplitterX + (float(widthChange) * 0.5);
1051 float deviation = fabs(centerX - splitterX);
1052
1053 // If deviating from center, adjust towards it
1054 if ( deviation > 20.0 )
1055 {
1056 if ( splitterX > centerX)
1057 splitterX -= 2;
1058 else
1059 splitterX += 2;
1060 }
1061 }
1062 else
1063 {
1064 // No width change, just keep sure we keep splitter position intact
1065 splitterX = m_fSplitterX;
1066 float deviation = fabs(centerX - splitterX);
1067 if ( deviation > 50.0 )
1068 {
1069 splitterX = centerX;
1070 }
1071 }
1072
1073 DoSetSplitterPosition((int)splitterX, 0, false, true);
1074
1075 m_fSplitterX = splitterX; // needed to retain accuracy
1076 }
1077}
1078
1079void wxPropertyGridPageState::SetColumnCount( int colCount )
1080{
1081 wxASSERT( colCount >= 2 );
1082 m_colWidths.SetCount( colCount, wxPG_DRAG_MARGIN );
1083 if ( m_colWidths.size() > (unsigned int)colCount )
1084 m_colWidths.RemoveAt( m_colWidths.size(), m_colWidths.size() - colCount );
1085
1086 if ( m_pPropGrid->GetState() == this )
1087 m_pPropGrid->RecalculateVirtualSize();
1088 else
1089 CheckColumnWidths();
1090}
1091
1092// Returns column index, -1 for margin
1093int wxPropertyGridPageState::HitTestH( int x, int* pSplitterHit, int* pSplitterHitOffset ) const
1094{
1095 int cx = GetGrid()->m_marginWidth;
1096 int col = -1;
1097 int prevSplitter = -1;
1098
1099 while ( x > cx )
1100 {
1101 col++;
1102 if ( col >= (int)m_colWidths.size() )
1103 {
1104 *pSplitterHit = -1;
1105 return col;
1106 }
1107 prevSplitter = cx;
1108 cx += m_colWidths[col];
1109 }
1110
1111 // Near prev. splitter
1112 if ( col >= 1 )
1113 {
1114 int diff = x - prevSplitter;
1115 if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 )
1116 {
1117 *pSplitterHit = col - 1;
1118 *pSplitterHitOffset = diff;
1119 return col;
1120 }
1121 }
1122
1123 // Near next splitter
1124 int nextSplitter = cx;
1125 if ( col < (int)(m_colWidths.size()-1) )
1126 {
1127 int diff = x - nextSplitter;
1128 if ( abs(diff) < wxPG_SPLITTERX_DETECTMARGIN1 )
1129 {
1130 *pSplitterHit = col;
1131 *pSplitterHitOffset = diff;
1132 return col;
1133 }
1134 }
1135
1136 *pSplitterHit = -1;
1137 return col;
1138}
1139
1140// -----------------------------------------------------------------------
1141// wxPropertyGridPageState property value setting and getting
1142// -----------------------------------------------------------------------
1143
1144bool wxPropertyGridPageState::DoSetPropertyValueString( wxPGProperty* p, const wxString& value )
1145{
1146 if ( p )
1147 {
f275b5db 1148 int flags = wxPG_REPORT_ERROR|wxPG_FULL_VALUE|wxPG_PROGRAMMATIC_VALUE;
1c4293cb
VZ
1149
1150 wxVariant variant = p->GetValueRef();
1151 bool res;
1152
1153 if ( p->GetMaxLength() <= 0 )
1154 res = p->StringToValue( variant, value, flags );
1155 else
1156 res = p->StringToValue( variant, value.Mid(0,p->GetMaxLength()), flags );
1157
1158 if ( res )
1159 {
1160 p->SetValue(variant);
fc72fab6
JS
1161 if ( p == m_pPropGrid->GetSelection() &&
1162 this == m_pPropGrid->GetState() )
a6353fe8 1163 m_pPropGrid->RefreshEditor();
1c4293cb
VZ
1164 }
1165
1166 return true;
1167 }
1168 return false;
1169}
1170
1171// -----------------------------------------------------------------------
1172
1173bool wxPropertyGridPageState::DoSetPropertyValue( wxPGProperty* p, wxVariant& value )
1174{
1175 if ( p )
1176 {
1177 p->SetValue(value);
fc72fab6
JS
1178 if ( p == m_pPropGrid->GetSelection() &&
1179 this == m_pPropGrid->GetState() )
a6353fe8 1180 m_pPropGrid->RefreshEditor();
1c4293cb
VZ
1181
1182 return true;
1183 }
1184 return false;
1185}
1186
1187// -----------------------------------------------------------------------
1188
1189bool wxPropertyGridPageState::DoSetPropertyValueWxObjectPtr( wxPGProperty* p, wxObject* value )
1190{
1191 if ( p )
1192 {
1193 // wnd_primary has to be given so the control can be updated as well.
1194 wxVariant v(value);
1195 DoSetPropertyValue(p, v);
1196 return true;
1197 }
1198 return false;
1199}
1200
1c4293cb
VZ
1201// -----------------------------------------------------------------------
1202// wxPropertyGridPageState property operations
1203// -----------------------------------------------------------------------
1204
fc72fab6
JS
1205bool wxPropertyGridPageState::DoIsPropertySelected( wxPGProperty* prop ) const
1206{
1207 const wxArrayPGProperty& selection = m_selection;
1208
1209 for ( unsigned int i=0; i<selection.size(); i++ )
1210 {
1211 if ( selection[i] == prop )
1212 return true;
1213 }
1214
1215 return false;
1216}
1217
1218// -----------------------------------------------------------------------
1219
7f3f8f1e
JS
1220void wxPropertyGridPageState::DoRemoveFromSelection( wxPGProperty* prop )
1221{
1222 for ( unsigned int i=0; i<m_selection.size(); i++ )
1223 {
1224 if ( m_selection[i] == prop )
1225 {
afaf3b70
JS
1226 wxPropertyGrid* pg = m_pPropGrid;
1227 if ( i == 0 && pg->GetState() == this )
1228 {
1229 // If first item (ie. one with the active editor) was
1230 // deselected, then we need to take some extra measures.
1231 wxArrayPGProperty sel = m_selection;
1232 sel.erase( sel.begin() + i );
1233
1234 wxPGProperty* newFirst;
1235 if ( sel.size() )
1236 newFirst = sel[0];
1237 else
1238 newFirst = NULL;
1239
1240 pg->DoSelectProperty(newFirst,
1241 wxPG_SEL_DONT_SEND_EVENT);
1242
1243 m_selection = sel;
1244
1245 pg->Refresh();
1246 }
1247 else
1248 {
1249 m_selection.erase( m_selection.begin() + i );
1250 }
7f3f8f1e
JS
1251 return;
1252 }
1253 }
1254}
1255
1256// -----------------------------------------------------------------------
1257
1c4293cb
VZ
1258bool wxPropertyGridPageState::DoCollapse( wxPGProperty* p )
1259{
1260 wxCHECK_MSG( p, false, wxT("invalid property id") );
1261
1262 if ( !p->GetChildCount() ) return false;
1263
1264 if ( !p->IsExpanded() ) return false;
1265
1266 p->SetExpanded(false);
1267
1268 VirtualHeightChanged();
1269
1270 return true;
1271}
1272
1273// -----------------------------------------------------------------------
1274
1275bool wxPropertyGridPageState::DoExpand( wxPGProperty* p )
1276{
1277 wxCHECK_MSG( p, false, wxT("invalid property id") );
1278
1279 if ( !p->GetChildCount() ) return false;
1280
1281 if ( p->IsExpanded() ) return false;
1282
1283 p->SetExpanded(true);
1284
1285 VirtualHeightChanged();
1286
1287 return true;
1288}
1289
1290// -----------------------------------------------------------------------
1291
1292bool wxPropertyGridPageState::DoSelectProperty( wxPGProperty* p, unsigned int flags )
1293{
1294 if ( this == m_pPropGrid->GetState() )
1295 return m_pPropGrid->DoSelectProperty( p, flags );
1296
fc72fab6 1297 DoSetSelection(p);
1c4293cb
VZ
1298 return true;
1299}
1300
1301// -----------------------------------------------------------------------
1302
1303bool wxPropertyGridPageState::DoHideProperty( wxPGProperty* p, bool hide, int flags )
1304{
1305 if ( !hide )
1306 p->ClearFlag( wxPG_PROP_HIDDEN );
1307 else
1308 p->SetFlag( wxPG_PROP_HIDDEN );
1309
1310 if ( flags & wxPG_RECURSE )
1311 {
1312 unsigned int i;
1313 for ( i = 0; i < p->GetChildCount(); i++ )
1314 DoHideProperty(p->Item(i), hide, flags | wxPG_RECURSE_STARTS);
1315 }
1316
1317 VirtualHeightChanged();
1318
1319 return true;
1320}
1321
1322// -----------------------------------------------------------------------
1323
1324bool wxPropertyGridPageState::DoEnableProperty( wxPGProperty* p, bool enable )
1325{
1326 if ( p )
1327 {
1328 if ( enable )
1329 {
1330 if ( !(p->m_flags & wxPG_PROP_DISABLED) )
1331 return false;
1332
1333 // Enabling
1334
1335 p->m_flags &= ~(wxPG_PROP_DISABLED);
1336 }
1337 else
1338 {
1339 if ( p->m_flags & wxPG_PROP_DISABLED )
1340 return false;
1341
1342 // Disabling
1343
1344 p->m_flags |= wxPG_PROP_DISABLED;
1345
1346 }
1347
1348 // Apply same to sub-properties as well
1349 unsigned int i;
1350 for ( i = 0; i < p->GetChildCount(); i++ )
1351 DoEnableProperty( p->Item(i), enable );
1352
1353 return true;
1354 }
1355 return false;
1356}
1357
1358// -----------------------------------------------------------------------
1359// wxPropertyGridPageState wxVariant related routines
1360// -----------------------------------------------------------------------
1361
1362// Returns list of wxVariant objects (non-categories and non-sub-properties only).
1363// Never includes sub-properties (unless they are parented by wxParentProperty).
1364wxVariant wxPropertyGridPageState::DoGetPropertyValues( const wxString& listname,
1365 wxPGProperty* baseparent,
1366 long flags ) const
1367{
1368 wxPGProperty* pwc = (wxPGProperty*) baseparent;
1369
1370 // Root is the default base-parent.
1371 if ( !pwc )
1372 pwc = m_properties;
1373
1374 wxVariantList tempList;
1375 wxVariant v( tempList, listname );
1376
1377 if ( pwc->GetChildCount() )
1378 {
1379 if ( flags & wxPG_KEEP_STRUCTURE )
1380 {
1381 wxASSERT( !pwc->HasFlag(wxPG_PROP_AGGREGATE) );
1382
1383 size_t i;
1384 for ( i=0; i<pwc->GetChildCount(); i++ )
1385 {
1386 wxPGProperty* p = pwc->Item(i);
1387 if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) )
1388 {
1389 wxVariant variant = p->GetValue();
1390 variant.SetName( p->GetBaseName() );
1391 v.Append( variant );
1392 }
1393 else
1394 {
1395 v.Append( DoGetPropertyValues(p->m_name,p,flags|wxPG_KEEP_STRUCTURE) );
1396 }
1397 if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() )
1398 v.Append( p->GetAttributesAsList() );
1399 }
1400 }
1401 else
1402 {
1403 wxPropertyGridConstIterator it( this, wxPG_ITERATE_DEFAULT, pwc->Item(0) );
1404 it.SetBaseParent( pwc );
1405
1406 for ( ; !it.AtEnd(); it.Next() )
1407 {
1408 const wxPGProperty* p = it.GetProperty();
1409
1410 // Use a trick to ignore wxParentProperty itself, but not its sub-properties.
1411 if ( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE) )
1412 {
1413 wxVariant variant = p->GetValue();
1414 variant.SetName( p->GetName() );
1415 v.Append( variant );
1416 if ( (flags & wxPG_INC_ATTRIBUTES) && p->m_attributes.GetCount() )
1417 v.Append( p->GetAttributesAsList() );
1418 }
1419 }
1420 }
1421 }
1422
1423 return v;
1424}
1425
1426// -----------------------------------------------------------------------
1427
1428void wxPropertyGridPageState::DoSetPropertyValues( const wxVariantList& list, wxPGProperty* defaultCategory )
1429{
1430 unsigned char origFrozen = 1;
1431
1432 if ( m_pPropGrid->GetState() == this )
1433 {
1434 origFrozen = m_pPropGrid->m_frozen;
1435 if ( !origFrozen ) m_pPropGrid->Freeze();
1436 }
1437
1438 wxPropertyCategory* use_category = (wxPropertyCategory*)defaultCategory;
1439
1440 if ( !use_category )
1441 use_category = (wxPropertyCategory*)m_properties;
1442
1443 // Let's iterate over the list of variants.
1444 wxVariantList::const_iterator node;
1445 int numSpecialEntries = 0;
1446
1447 //
1448 // Second pass for special entries
b7bc9d80 1449 for ( node = list.begin(); node != list.end(); ++node )
1c4293cb
VZ
1450 {
1451 wxVariant *current = (wxVariant*)*node;
1452
1453 // Make sure it is wxVariant.
1454 wxASSERT( current );
1455 wxASSERT( wxStrcmp(current->GetClassInfo()->GetClassName(),wxT("wxVariant")) == 0 );
1456
1457 const wxString& name = current->GetName();
1458 if ( name.length() > 0 )
1459 {
1460 //
1461 // '@' signified a special entry
1462 if ( name[0] == wxS('@') )
1463 {
1464 numSpecialEntries++;
1465 }
1466 else
1467 {
1468 wxPGProperty* foundProp = BaseGetPropertyByName(name);
1469 if ( foundProp )
1470 {
1471 wxPGProperty* p = foundProp;
1472
1473 // If it was a list, we still have to go through it.
1474 if ( wxStrcmp(current->GetType(), wxS("list")) == 0 )
1475 {
1476 DoSetPropertyValues( current->GetList(),
d3b9f782 1477 p->IsCategory()?p:(NULL)
1c4293cb
VZ
1478 );
1479 }
1480 else
1481 {
4b6a582b
VZ
1482 wxASSERT_LEVEL_2_MSG(
1483 wxStrcmp(current->GetType(), p->GetValue().GetType()) == 0,
1484 wxString::Format(
1485 wxS("setting value of property \"%s\" from variant"),
1486 p->GetName().c_str())
1487 );
1c4293cb
VZ
1488
1489 p->SetValue(*current);
1490 }
1491 }
1492 else
1493 {
1494 // Is it list?
1495 if ( current->GetType() != wxS("list") )
1496 {
1497 // Not.
1498 }
1499 else
1500 {
1501 // Yes, it is; create a sub category and append contents there.
1502 wxPGProperty* newCat = DoInsert(use_category,-1,new wxPropertyCategory(current->GetName(),wxPG_LABEL));
1503 DoSetPropertyValues( current->GetList(), newCat );
1504 }
1505 }
1506 }
1507 }
1508 }
1509
1510 if ( numSpecialEntries )
1511 {
b7bc9d80 1512 for ( node = list.begin(); node != list.end(); ++node )
1c4293cb
VZ
1513 {
1514 wxVariant *current = (wxVariant*)*node;
1515
1516 const wxString& name = current->GetName();
1517 if ( name.length() > 0 )
1518 {
1519 //
1520 // '@' signified a special entry
1521 if ( name[0] == wxS('@') )
1522 {
1523 numSpecialEntries--;
1524
1525 size_t pos2 = name.rfind(wxS('@'));
1526 if ( pos2 > 0 && pos2 < (name.size()-1) )
1527 {
1528 wxString propName = name.substr(1, pos2-1);
1529 wxString entryType = name.substr(pos2+1, wxString::npos);
1530
1531 if ( entryType == wxS("attr") )
1532 {
1533 //
1534 // List of attributes
1535 wxPGProperty* foundProp = BaseGetPropertyByName(propName);
1536 if ( foundProp )
1537 {
0372d42e 1538 wxASSERT( current->GetType() == wxPG_VARIANT_TYPE_LIST );
1c4293cb
VZ
1539
1540 wxVariantList& list2 = current->GetList();
1541 wxVariantList::const_iterator node2;
1542
b7bc9d80 1543 for ( node2 = list2.begin(); node2 != list2.end(); ++node2 )
1c4293cb
VZ
1544 {
1545 wxVariant *attr = (wxVariant*)*node2;
1546 foundProp->SetAttribute( attr->GetName(), *attr );
1547 }
1548 }
1549 else
1550 {
1551 // ERROR: No such property: 'propName'
1552 }
1553 }
1554 }
1555 else
1556 {
1557 // ERROR: Special entry requires name of format @<propname>@<entrytype>
1558 }
1559 }
1560 }
1561
1562 if ( !numSpecialEntries )
1563 break;
1564 }
1565 }
1566
1567 if ( !origFrozen )
1568 {
1569 m_pPropGrid->Thaw();
1570
1571 if ( this == m_pPropGrid->GetState() )
a6353fe8 1572 m_pPropGrid->RefreshEditor();
1c4293cb
VZ
1573 }
1574
1575}
1576
1577// -----------------------------------------------------------------------
1578// wxPropertyGridPageState property adding and removal
1579// -----------------------------------------------------------------------
1580
2fd4a524
JS
1581bool wxPropertyGridPageState::PrepareToAddItem( wxPGProperty* property,
1582 wxPGProperty* scheduledParent )
1c4293cb
VZ
1583{
1584 wxPropertyGrid* propGrid = m_pPropGrid;
1585
1586 // This will allow better behavior.
1587 if ( scheduledParent == m_properties )
d3b9f782 1588 scheduledParent = NULL;
1c4293cb 1589
d665918b
JS
1590 if ( scheduledParent && !scheduledParent->IsCategory() )
1591 {
1592 wxASSERT_MSG( property->GetBaseName().length(),
1593 "Property's children must have unique, non-empty names within their scope" );
1594 }
1595
1c4293cb
VZ
1596 property->m_parentState = this;
1597
1598 if ( property->IsCategory() )
1599 {
1600
1601 // Parent of a category must be either root or another category
1602 // (otherwise Bad Things might happen).
1603 wxASSERT_MSG( scheduledParent == NULL ||
1604 scheduledParent == m_properties ||
1605 scheduledParent->IsCategory(),
1606 wxT("Parent of a category must be either root or another category."));
1607
1608 // If we already have category with same name, delete given property
1609 // and use it instead as most recent caption item.
1610 wxPGProperty* found_id = BaseGetPropertyByName( property->GetBaseName() );
1611 if ( found_id )
1612 {
1613 wxPropertyCategory* pwc = (wxPropertyCategory*) found_id;
1614 if ( pwc->IsCategory() ) // Must be a category.
1615 {
1616 delete property;
1617 m_currentCategory = pwc;
2fd4a524 1618 return false;
1c4293cb
VZ
1619 }
1620 }
1621 }
1622
657a8a35 1623#if wxDEBUG_LEVEL
1c4293cb
VZ
1624 // Warn for identical names in debug mode.
1625 if ( BaseGetPropertyByName(property->GetName()) &&
1626 (!scheduledParent || scheduledParent->IsCategory()) )
1627 {
657a8a35
VZ
1628 wxFAIL_MSG(wxString::Format(
1629 "wxPropertyGrid item with name \"%s\" already exists",
1630 property->GetName()));
1631
1c4293cb
VZ
1632 wxPGGlobalVars->m_warnings++;
1633 }
657a8a35 1634#endif // wxDEBUG_LEVEL
1c4293cb
VZ
1635
1636 // Make sure nothing is selected.
1621f192
JS
1637 if ( propGrid )
1638 propGrid->ClearSelection(false);
1c4293cb 1639
2fd4a524
JS
1640 // NULL parent == root parent
1641 if ( !scheduledParent )
1642 scheduledParent = DoGetRoot();
1c4293cb 1643
2fd4a524 1644 property->m_parent = scheduledParent;
1c4293cb 1645
2fd4a524 1646 property->InitAfterAdded(this, propGrid);
1c4293cb 1647
2fd4a524 1648 if ( property->IsCategory() )
1c4293cb 1649 {
2fd4a524 1650 wxPropertyCategory* pc = wxStaticCast(property, wxPropertyCategory);
1c4293cb 1651
2fd4a524 1652 m_currentCategory = pc;
1c4293cb 1653
2fd4a524 1654 // Calculate text extent for category caption
1c4293cb
VZ
1655 if ( propGrid )
1656 pc->CalculateTextExtent(propGrid, propGrid->GetCaptionFont());
1c4293cb 1657 }
2fd4a524
JS
1658
1659 return true;
1c4293cb
VZ
1660}
1661
1662// -----------------------------------------------------------------------
1663
1664wxPGProperty* wxPropertyGridPageState::DoAppend( wxPGProperty* property )
1665{
1666 wxPropertyCategory* cur_cat = m_currentCategory;
1667 if ( property->IsCategory() )
d3b9f782 1668 cur_cat = NULL;
1c4293cb
VZ
1669
1670 return DoInsert( cur_cat, -1, property );
1671}
1672
1673// -----------------------------------------------------------------------
1674
1675wxPGProperty* wxPropertyGridPageState::DoInsert( wxPGProperty* parent, int index, wxPGProperty* property )
1676{
1677 if ( !parent )
1678 parent = m_properties;
1679
1680 wxCHECK_MSG( !parent->HasFlag(wxPG_PROP_AGGREGATE),
1681 wxNullProperty,
1682 wxT("when adding properties to fixed parents, use BeginAddChildren and EndAddChildren.") );
1683
2fd4a524 1684 bool res = PrepareToAddItem( property, (wxPropertyCategory*)parent );
1c4293cb 1685
2fd4a524
JS
1686 // PrepareToAddItem() may just decide to use use current category
1687 // instead of adding new one.
1688 if ( !res )
1c4293cb
VZ
1689 return m_currentCategory;
1690
94b8ecf1
JS
1691 bool parentIsRoot = parent->IsRoot();
1692 bool parentIsCategory = parent->IsCategory();
1693
1c4293cb
VZ
1694 // Note that item must be added into current mode later.
1695
1696 // If parent is wxParentProperty, just stick it in...
1697 // If parent is root (m_properties), then...
1698 // In categoric mode: Add as last item in m_abcArray (if not category).
1699 // Add to given index in m_regularArray.
1700 // In non-cat mode: Add as last item in m_regularArray.
1701 // Add to given index in m_abcArray.
1702 // If parent is category, then...
1703 // 1) Add to given category in given index.
1704 // 2) Add as last item in m_abcArray.
1705
94b8ecf1 1706 if ( m_properties == &m_regularArray )
1c4293cb 1707 {
94b8ecf1 1708 // We are currently in Categorized mode
1c4293cb 1709
94b8ecf1
JS
1710 // Only add non-categories to m_abcArray.
1711 if ( m_abcArray && !property->IsCategory() &&
1712 (parentIsCategory || parentIsRoot) )
1c4293cb 1713 {
48a32cf6 1714 m_abcArray->DoAddChild( property, -1, false );
1c4293cb 1715 }
1c4293cb 1716
94b8ecf1 1717 // Add to current mode.
48a32cf6 1718 parent->DoAddChild( property, index, true );
94b8ecf1
JS
1719 }
1720 else
1721 {
1722 // We are currently in Non-categorized/Alphabetic mode
1723
1724 if ( parentIsCategory )
1725 // Parent is category.
48a32cf6 1726 parent->DoAddChild( property, index, false );
94b8ecf1
JS
1727 else if ( parentIsRoot )
1728 // Parent is root.
48a32cf6 1729 m_regularArray.DoAddChild( property, -1, false );
94b8ecf1
JS
1730
1731 // Add to current mode
1732 if ( !property->IsCategory() )
48a32cf6 1733 m_abcArray->DoAddChild( property, index, true );
1c4293cb
VZ
1734 }
1735
1736 // category stuff
1737 if ( property->IsCategory() )
1738 {
1739 // This is a category caption item.
1740
1741 // Last caption is not the bottom one (this info required by append)
1742 m_lastCaptionBottomnest = 0;
1743 }
1744
1745 // Only add name to hashmap if parent is root or category
26740086 1746 if ( property->m_name.length() &&
94b8ecf1 1747 (parentIsCategory || parentIsRoot) )
1c4293cb
VZ
1748 m_dictName[property->m_name] = (void*) property;
1749
1750 VirtualHeightChanged();
1751
1752 property->UpdateParentValues();
1753
1754 m_itemsAdded = 1;
1755
1756 return property;
1757}
1758
1759// -----------------------------------------------------------------------
1760
f915d44b 1761void wxPropertyGridPageState::DoDelete( wxPGProperty* item, bool doDelete )
1c4293cb
VZ
1762{
1763 wxCHECK_RET( item->GetParent(),
1764 wxT("this property was already deleted") );
1765
1766 wxCHECK_RET( item != &m_regularArray && item != m_abcArray,
1767 wxT("wxPropertyGrid: Do not attempt to remove the root item.") );
1768
1c4293cb
VZ
1769 unsigned int indinparent = item->GetIndexInParent();
1770
1771 wxPGProperty* pwc = (wxPGProperty*)item;
91c818f8 1772 wxPGProperty* parent = item->GetParent();
1c4293cb 1773
91c818f8 1774 wxCHECK_RET( !parent->HasFlag(wxPG_PROP_AGGREGATE),
1c4293cb
VZ
1775 wxT("wxPropertyGrid: Do not attempt to remove sub-properties.") );
1776
fc72fab6
JS
1777 wxASSERT( item->GetParentState() == this );
1778
03647350 1779 wxPropertyGrid* pg = GetGrid();
fc72fab6
JS
1780
1781 if ( DoIsPropertySelected(item) )
1782 {
1783 if ( pg && pg->GetState() == this )
1784 {
1785 pg->DoRemoveFromSelection(item,
1786 wxPG_SEL_DELETING|wxPG_SEL_NOVALIDATE);
1787 }
1788 else
1789 {
7f3f8f1e 1790 DoRemoveFromSelection(item);
fc72fab6
JS
1791 }
1792 }
1793
1794 item->SetFlag(wxPG_PROP_BEING_DELETED);
1795
91c818f8
JS
1796 // Delete children
1797 if ( item->GetChildCount() && !item->HasFlag(wxPG_PROP_AGGREGATE) )
1c4293cb
VZ
1798 {
1799 // deleting a category
91c818f8 1800 if ( item->IsCategory() )
1c4293cb 1801 {
91c818f8 1802 if ( pwc == m_currentCategory )
d3b9f782 1803 m_currentCategory = NULL;
1c4293cb
VZ
1804 }
1805
91c818f8 1806 item->DeleteChildren();
1c4293cb
VZ
1807 }
1808
1809 if ( !IsInNonCatMode() )
1810 {
1811 // categorized mode - non-categorized array
1812
91c818f8
JS
1813 // Remove from non-cat array
1814 if ( !item->IsCategory() &&
1815 (parent->IsCategory() || parent->IsRoot()) )
1c4293cb
VZ
1816 {
1817 if ( m_abcArray )
d8c74d04 1818 m_abcArray->RemoveChild(item);
1c4293cb
VZ
1819 }
1820
1821 // categorized mode - categorized array
91c818f8 1822 wxArrayPGProperty& parentsChildren = parent->m_children;
d8c74d04 1823 parentsChildren.erase( parentsChildren.begin() + indinparent );
1b895132 1824 item->m_parent->FixIndicesOfChildren();
1c4293cb
VZ
1825 }
1826 else
1827 {
1828 // non-categorized mode - categorized array
1829
1830 // We need to find location of item.
1831 wxPGProperty* cat_parent = &m_regularArray;
1832 int cat_index = m_regularArray.GetChildCount();
1833 size_t i;
1834 for ( i = 0; i < m_regularArray.GetChildCount(); i++ )
1835 {
1836 wxPGProperty* p = m_regularArray.Item(i);
1837 if ( p == item ) { cat_index = i; break; }
1838 if ( p->IsCategory() )
1839 {
1840 int subind = ((wxPGProperty*)p)->Index(item);
1841 if ( subind != wxNOT_FOUND )
1842 {
1843 cat_parent = ((wxPGProperty*)p);
1844 cat_index = subind;
1845 break;
1846 }
1847 }
1848 }
d8c74d04 1849 cat_parent->m_children.erase(cat_parent->m_children.begin()+cat_index);
1c4293cb
VZ
1850
1851 // non-categorized mode - non-categorized array
1852 if ( !item->IsCategory() )
1853 {
1854 wxASSERT( item->m_parent == m_abcArray );
d8c74d04
JS
1855 wxArrayPGProperty& parentsChildren = item->m_parent->m_children;
1856 parentsChildren.erase(parentsChildren.begin() + indinparent);
1b895132 1857 item->m_parent->FixIndicesOfChildren(indinparent);
1c4293cb
VZ
1858 }
1859 }
1860
03647350 1861 if ( item->GetBaseName().length() &&
26740086
JS
1862 (parent->IsCategory() || parent->IsRoot()) )
1863 m_dictName.erase(item->GetBaseName());
1c4293cb 1864
03647350
VZ
1865 // We need to clear parent grid's m_propHover, if it matches item
1866 if ( pg && pg->m_propHover == item )
1867 pg->m_propHover = NULL;
4d18ddc7 1868
1c4293cb 1869 // We can actually delete it now
f915d44b
JS
1870 if ( doDelete )
1871 delete item;
1c4293cb
VZ
1872
1873 m_itemsAdded = 1; // Not a logical assignment (but required nonetheless).
1874
1875 VirtualHeightChanged();
1876}
1877
1878// -----------------------------------------------------------------------
f4bc1aa2
JS
1879
1880#endif // wxUSE_PROPGRID