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