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