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