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