Allow wxPropertyGridInterface::EnableProperty() to work even if property->GetGrid...
[wxWidgets.git] / src / propgrid / propgridiface.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/propgridiface.cpp
3 // Purpose: wxPropertyGridInterface 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 #if wxUSE_PROPGRID
20
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"
32 #include "wx/button.h"
33 #include "wx/pen.h"
34 #include "wx/brush.h"
35 #include "wx/settings.h"
36 #include "wx/sizer.h"
37 #include "wx/intl.h"
38 #endif
39
40 #include "wx/propgrid/property.h"
41 #include "wx/propgrid/propgrid.h"
42
43
44 const wxChar *wxPGTypeName_long = wxT("long");
45 const wxChar *wxPGTypeName_bool = wxT("bool");
46 const wxChar *wxPGTypeName_double = wxT("double");
47 const wxChar *wxPGTypeName_wxString = wxT("string");
48 const wxChar *wxPGTypeName_void = wxT("void*");
49 const wxChar *wxPGTypeName_wxArrayString = wxT("arrstring");
50
51
52 // ----------------------------------------------------------------------------
53 // VariantDatas
54 // ----------------------------------------------------------------------------
55
56 WX_PG_IMPLEMENT_VARIANT_DATA_EXPORTED(wxPoint, WXDLLIMPEXP_PROPGRID)
57 WX_PG_IMPLEMENT_VARIANT_DATA_EXPORTED(wxSize, WXDLLIMPEXP_PROPGRID)
58 WX_PG_IMPLEMENT_VARIANT_DATA_EXPORTED_DUMMY_EQ(wxArrayInt, WXDLLIMPEXP_PROPGRID)
59 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxFont, WXDLLIMPEXP_PROPGRID)
60
61 // -----------------------------------------------------------------------
62 // wxPGPropArgCls
63 // -----------------------------------------------------------------------
64
65 wxPGProperty* wxPGPropArgCls::GetPtr( wxPropertyGridInterface* iface ) const
66 {
67 if ( m_flags == IsProperty )
68 {
69 wxASSERT_MSG( m_ptr.property, wxT("invalid property ptr") );
70 return m_ptr.property;
71 }
72 else if ( m_flags & IsWxString )
73 return iface->GetPropertyByNameA(*m_ptr.stringName);
74 else if ( m_flags & IsCharPtr )
75 return iface->GetPropertyByNameA(m_ptr.charName);
76 #if wxUSE_WCHAR_T
77 else if ( m_flags & IsWCharPtr )
78 return iface->GetPropertyByNameA(m_ptr.wcharName);
79 #endif
80
81 return NULL;
82 }
83
84 // -----------------------------------------------------------------------
85 // wxPropertyGridInterface
86 // -----------------------------------------------------------------------
87
88 void wxPropertyGridInterface::RefreshGrid( wxPropertyGridPageState* state )
89 {
90 if ( !state )
91 state = m_pState;
92
93 wxPropertyGrid* grid = state->GetGrid();
94 if ( grid->GetState() == state && !grid->IsFrozen() )
95 {
96 grid->Refresh();
97 }
98 }
99
100 // -----------------------------------------------------------------------
101
102 wxPGProperty* wxPropertyGridInterface::Append( wxPGProperty* property )
103 {
104 wxPGProperty* retp = m_pState->DoAppend(property);
105
106 wxPropertyGrid* grid = m_pState->GetGrid();
107 if ( grid )
108 grid->RefreshGrid();
109
110 return retp;
111 }
112
113 // -----------------------------------------------------------------------
114
115 wxPGProperty* wxPropertyGridInterface::AppendIn( wxPGPropArg id, wxPGProperty* newproperty )
116 {
117 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
118 wxPGProperty* pwc = (wxPGProperty*) p;
119 wxPGProperty* retp = m_pState->DoInsert(pwc, pwc->GetChildCount(), newproperty);
120 return retp;
121 }
122
123 // -----------------------------------------------------------------------
124
125 wxPGProperty* wxPropertyGridInterface::Insert( wxPGPropArg id, wxPGProperty* property )
126 {
127 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
128 wxPGProperty* retp = m_pState->DoInsert(p->GetParent(), p->GetIndexInParent(), property);
129 RefreshGrid();
130 return retp;
131 }
132
133 // -----------------------------------------------------------------------
134
135 wxPGProperty* wxPropertyGridInterface::Insert( wxPGPropArg id, int index, wxPGProperty* newproperty )
136 {
137 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
138 wxPGProperty* retp = m_pState->DoInsert((wxPGProperty*)p,index,newproperty);
139 RefreshGrid();
140 return retp;
141 }
142
143 // -----------------------------------------------------------------------
144
145 void wxPropertyGridInterface::DeleteProperty( wxPGPropArg id )
146 {
147 wxPG_PROP_ARG_CALL_PROLOG()
148
149 wxPropertyGridPageState* state = p->GetParentState();
150
151 state->DoDelete( p, true );
152
153 RefreshGrid(state);
154 }
155
156 // -----------------------------------------------------------------------
157
158 wxPGProperty* wxPropertyGridInterface::RemoveProperty( wxPGPropArg id )
159 {
160 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
161
162 wxCHECK( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE),
163 wxNullProperty);
164
165 wxPropertyGridPageState* state = p->GetParentState();
166
167 state->DoDelete( p, false );
168
169 // Mark the property as 'unattached'
170 p->m_parentState = NULL;
171 p->m_parent = NULL;
172
173 RefreshGrid(state);
174
175 return p;
176 }
177
178 // -----------------------------------------------------------------------
179
180 wxPGProperty* wxPropertyGridInterface::ReplaceProperty( wxPGPropArg id, wxPGProperty* property )
181 {
182 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
183
184 wxPGProperty* replaced = p;
185 wxCHECK_MSG( replaced && property,
186 wxNullProperty,
187 wxT("NULL property") );
188 wxCHECK_MSG( !replaced->IsCategory(),
189 wxNullProperty,
190 wxT("cannot replace this type of property") );
191 wxCHECK_MSG( !m_pState->IsInNonCatMode(),
192 wxNullProperty,
193 wxT("cannot replace properties in alphabetic mode") );
194
195 // Get address to the slot
196 wxPGProperty* parent = replaced->GetParent();
197 int ind = replaced->GetIndexInParent();
198
199 wxPropertyGridPageState* state = replaced->GetParentState();
200 DeleteProperty(replaced); // Must use generic Delete
201 state->DoInsert(parent,ind,property);
202
203 return property;
204 }
205
206 // -----------------------------------------------------------------------
207 // wxPropertyGridInterface property operations
208 // -----------------------------------------------------------------------
209
210 wxPGProperty* wxPropertyGridInterface::GetSelection() const
211 {
212 return m_pState->GetSelection();
213 }
214
215 // -----------------------------------------------------------------------
216
217 bool wxPropertyGridInterface::ClearSelection( bool validation )
218 {
219 bool res = DoClearSelection(validation, wxPG_SEL_DONT_SEND_EVENT);
220 wxPropertyGrid* pg = GetPropertyGrid();
221 if ( pg )
222 pg->Refresh();
223 return res;
224 }
225
226 // -----------------------------------------------------------------------
227
228 bool wxPropertyGridInterface::DoClearSelection( bool validation,
229 int selFlags )
230 {
231 if ( !validation )
232 selFlags |= wxPG_SEL_NOVALIDATE;
233
234 wxPropertyGridPageState* state = m_pState;
235
236 if ( state )
237 {
238 wxPropertyGrid* pg = state->GetGrid();
239 if ( pg->GetState() == state )
240 return pg->DoSelectProperty(NULL, selFlags);
241 else
242 state->DoSetSelection(NULL);
243 }
244
245 return true;
246 }
247
248 // -----------------------------------------------------------------------
249
250 void wxPropertyGridInterface::LimitPropertyEditing( wxPGPropArg id, bool limit )
251 {
252 wxPG_PROP_ARG_CALL_PROLOG()
253
254 m_pState->DoLimitPropertyEditing(p, limit);
255 RefreshProperty(p);
256 }
257
258 // -----------------------------------------------------------------------
259
260 bool wxPropertyGridInterface::EnableProperty( wxPGPropArg id, bool enable )
261 {
262 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
263
264 wxPropertyGridPageState* state = p->GetParentState();
265 wxPropertyGrid* grid = state->GetGrid();
266
267 if ( enable )
268 {
269 if ( !(p->m_flags & wxPG_PROP_DISABLED) )
270 return false;
271
272 // If active, Set active Editor.
273 if ( grid && grid->GetState() == state && p == grid->GetSelection() )
274 grid->DoSelectProperty( p, wxPG_SEL_FORCE );
275 }
276 else
277 {
278 if ( p->m_flags & wxPG_PROP_DISABLED )
279 return false;
280
281 // If active, Disable as active Editor.
282 if ( grid && grid->GetState() == state && p == grid->GetSelection() )
283 grid->DoSelectProperty( p, wxPG_SEL_FORCE );
284 }
285
286 state->DoEnableProperty(p, enable);
287
288 RefreshProperty( p );
289
290 return true;
291 }
292
293 // -----------------------------------------------------------------------
294
295 bool wxPropertyGridInterface::ExpandAll( bool doExpand )
296 {
297 wxPropertyGridPageState* state = m_pState;
298
299 if ( !state->DoGetRoot()->GetChildCount() )
300 return true;
301
302 wxPropertyGrid* pg = state->GetGrid();
303
304 if ( GetSelection() && GetSelection() != state->DoGetRoot() &&
305 !doExpand )
306 {
307 pg->DoClearSelection();
308 }
309
310 wxPGVIterator it;
311
312 for ( it = GetVIterator( wxPG_ITERATE_ALL ); !it.AtEnd(); it.Next() )
313 {
314 wxPGProperty* p = (wxPGProperty*) it.GetProperty();
315 if ( p->GetChildCount() )
316 {
317 if ( doExpand )
318 {
319 if ( !p->IsExpanded() )
320 {
321 state->DoExpand(p);
322 }
323 }
324 else
325 {
326 if ( p->IsExpanded() )
327 {
328 state->DoCollapse(p);
329 }
330 }
331 }
332 }
333
334 pg->RecalculateVirtualSize();
335
336 RefreshGrid();
337
338 return true;
339 }
340
341 // -----------------------------------------------------------------------
342
343 void wxPropertyGridInterface::ClearModifiedStatus()
344 {
345 unsigned int pageIndex = 0;
346
347 for (;;)
348 {
349 wxPropertyGridPageState* page = GetPageState(pageIndex);
350 if ( !page ) break;
351
352 page->DoGetRoot()->SetFlagRecursively(wxPG_PROP_MODIFIED, false);
353 page->m_anyModified = false;
354
355 pageIndex++;
356 }
357
358 // Update active editor control, if any
359 GetPropertyGrid()->RefreshEditor();
360 }
361
362 // -----------------------------------------------------------------------
363 // wxPropertyGridInterface property value setting and getting
364 // -----------------------------------------------------------------------
365
366 void wxPGGetFailed( const wxPGProperty* p, const wxString& typestr )
367 {
368 wxPGTypeOperationFailed(p, typestr, wxS("Get"));
369 }
370
371 // -----------------------------------------------------------------------
372
373 void wxPGTypeOperationFailed( const wxPGProperty* p,
374 const wxString& typestr,
375 const wxString& op )
376 {
377 wxASSERT( p != NULL );
378 wxLogError( _("Type operation \"%s\" failed: Property labeled \"%s\" is of type \"%s\", NOT \"%s\"."),
379 op.c_str(), p->GetLabel().c_str(), p->GetValue().GetType().c_str(), typestr.c_str() );
380 }
381
382 // -----------------------------------------------------------------------
383
384 void wxPropertyGridInterface::SetPropVal( wxPGPropArg id, wxVariant& value )
385 {
386 wxPG_PROP_ARG_CALL_PROLOG()
387
388 if ( p )
389 p->SetValue(value);
390 }
391
392 // -----------------------------------------------------------------------
393
394 void wxPropertyGridInterface::SetPropertyValueString( wxPGPropArg id, const wxString& value )
395 {
396 wxPG_PROP_ARG_CALL_PROLOG()
397
398 if ( p )
399 m_pState->DoSetPropertyValueString(p, value);
400 }
401
402 // -----------------------------------------------------------------------
403
404 void wxPropertyGridInterface::SetValidationFailureBehavior( int vfbFlags )
405 {
406 GetPropertyGrid()->m_permanentValidationFailureBehavior = vfbFlags;
407 }
408
409 // -----------------------------------------------------------------------
410
411 wxPGProperty* wxPropertyGridInterface::GetPropertyByNameA( const wxString& name ) const
412 {
413 wxPGProperty* p = GetPropertyByName(name);
414 wxASSERT_MSG(p,wxString::Format(wxT("no property with name '%s'"),name.c_str()));
415 return p;
416 }
417
418 // ----------------------------------------------------------------------------
419
420 wxPGProperty* wxPropertyGridInterface::GetPropertyByLabel( const wxString& label ) const
421 {
422 wxPGVIterator it;
423
424 for ( it = GetVIterator( wxPG_ITERATE_PROPERTIES ); !it.AtEnd(); it.Next() )
425 {
426 if ( it.GetProperty()->GetLabel() == label )
427 return it.GetProperty();
428 }
429
430 return wxNullProperty;
431 }
432
433 // ----------------------------------------------------------------------------
434
435 void wxPropertyGridInterface::DoSetPropertyAttribute( wxPGPropArg id, const wxString& name,
436 wxVariant& value, long argFlags )
437 {
438 wxPG_PROP_ARG_CALL_PROLOG()
439
440 p->SetAttribute( name, value );
441
442 if ( argFlags & wxPG_RECURSE )
443 {
444 unsigned int i;
445 for ( i = 0; i < p->GetChildCount(); i++ )
446 DoSetPropertyAttribute(p->Item(i), name, value, argFlags);
447 }
448 }
449
450 // -----------------------------------------------------------------------
451
452 void wxPropertyGridInterface::SetPropertyAttributeAll( const wxString& attrName,
453 wxVariant value )
454 {
455 unsigned int pageIndex = 0;
456
457 for (;;)
458 {
459 wxPropertyGridPageState* page = GetPageState(pageIndex);
460 if ( !page ) break;
461
462 DoSetPropertyAttribute(page->DoGetRoot(), attrName, value, wxPG_RECURSE);
463
464 pageIndex++;
465 }
466 }
467
468 // -----------------------------------------------------------------------
469
470 void wxPropertyGridInterface::GetPropertiesWithFlag( wxArrayPGProperty* targetArr,
471 wxPGProperty::FlagType flags,
472 bool inverse,
473 int iterFlags ) const
474 {
475 wxASSERT( targetArr );
476 wxPGVIterator it = GetVIterator( iterFlags );
477
478 for ( ;
479 !it.AtEnd();
480 it.Next() )
481 {
482 const wxPGProperty* property = it.GetProperty();
483
484 if ( !inverse )
485 {
486 if ( (property->GetFlags() & flags) == flags )
487 targetArr->push_back((wxPGProperty*)property);
488 }
489 else
490 {
491 if ( (property->GetFlags() & flags) != flags )
492 targetArr->push_back((wxPGProperty*)property);
493 }
494 }
495 }
496
497 // -----------------------------------------------------------------------
498
499 void wxPropertyGridInterface::SetBoolChoices( const wxString& trueChoice,
500 const wxString& falseChoice )
501 {
502 wxPGGlobalVars->m_boolChoices[0] = falseChoice;
503 wxPGGlobalVars->m_boolChoices[1] = trueChoice;
504 }
505
506 // -----------------------------------------------------------------------
507
508 wxPGProperty* wxPropertyGridInterface::DoGetPropertyByName( const wxString& name ) const
509 {
510 return m_pState->BaseGetPropertyByName(name);
511 }
512
513 // -----------------------------------------------------------------------
514
515 wxPGProperty* wxPropertyGridInterface::GetPropertyByName( const wxString& name,
516 const wxString& subname ) const
517 {
518 wxPGProperty* p = DoGetPropertyByName(name);
519 if ( !p || !p->GetChildCount() )
520 return wxNullProperty;
521
522 return p->GetPropertyByName(subname);
523 }
524
525 // -----------------------------------------------------------------------
526
527 // Since GetPropertyByName is used *a lot*, this makes sense
528 // since non-virtual method can be called with less code.
529 wxPGProperty* wxPropertyGridInterface::GetPropertyByName( const wxString& name ) const
530 {
531 wxPGProperty* p = DoGetPropertyByName(name);
532 if ( p )
533 return p;
534
535 // Check if its "Property.SubProperty" format
536 int pos = name.Find(wxT('.'));
537 if ( pos <= 0 )
538 return NULL;
539
540 return GetPropertyByName(name.substr(0,pos),
541 name.substr(pos+1,name.length()-pos-1));
542 }
543
544 // -----------------------------------------------------------------------
545
546 bool wxPropertyGridInterface::HideProperty( wxPGPropArg id, bool hide, int flags )
547 {
548 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
549
550 wxPropertyGrid* pg = m_pState->GetGrid();
551
552 if ( pg == p->GetGrid() )
553 return pg->DoHideProperty(p, hide, flags);
554 else
555 m_pState->DoHideProperty(p, hide, flags);
556
557 return true;
558 }
559
560 // -----------------------------------------------------------------------
561
562 bool wxPropertyGridInterface::Collapse( wxPGPropArg id )
563 {
564 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
565 wxPropertyGrid* pg = p->GetGridIfDisplayed();
566 if ( pg )
567 return pg->DoCollapse(p);
568
569 return p->GetParentState()->DoCollapse(p);
570 }
571
572 // -----------------------------------------------------------------------
573
574 bool wxPropertyGridInterface::Expand( wxPGPropArg id )
575 {
576 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
577 wxPropertyGrid* pg = p->GetGridIfDisplayed();
578 if ( pg )
579 return pg->DoExpand(p);
580
581 return p->GetParentState()->DoExpand(p);
582 }
583
584 // -----------------------------------------------------------------------
585
586 void wxPropertyGridInterface::Sort( int flags )
587 {
588 wxPropertyGrid* pg = GetPropertyGrid();
589
590 pg->DoClearSelection();
591
592 unsigned int pageIndex = 0;
593
594 for (;;)
595 {
596 wxPropertyGridPageState* page = GetPageState(pageIndex);
597 if ( !page ) break;
598 page->DoSort(flags);
599 pageIndex++;
600 }
601 }
602
603 // -----------------------------------------------------------------------
604
605 void wxPropertyGridInterface::SetPropertyLabel( wxPGPropArg id, const wxString& newproplabel )
606 {
607 wxPG_PROP_ARG_CALL_PROLOG()
608
609 p->SetLabel( newproplabel );
610
611 wxPropertyGridPageState* state = p->GetParentState();
612 wxPropertyGrid* pg = state->GetGrid();
613
614 if ( pg->HasFlag(wxPG_AUTO_SORT) )
615 pg->SortChildren(p->GetParent());
616
617 if ( pg->GetState() == state )
618 {
619 if ( pg->HasFlag(wxPG_AUTO_SORT) )
620 pg->Refresh();
621 else
622 pg->DrawItem( p );
623 }
624 }
625
626 // -----------------------------------------------------------------------
627
628 bool wxPropertyGridInterface::SetPropertyMaxLength( wxPGPropArg id, int maxLen )
629 {
630 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
631
632 wxPropertyGrid* pg = m_pState->GetGrid();
633
634 p->m_maxLen = (short) maxLen;
635
636 // Adjust control if selected currently
637 if ( pg == p->GetGrid() && p == m_pState->GetSelection() )
638 {
639 wxWindow* wnd = pg->GetEditorControl();
640 wxTextCtrl* tc = wxDynamicCast(wnd,wxTextCtrl);
641 if ( tc )
642 tc->SetMaxLength( maxLen );
643 else
644 // Not a text ctrl
645 return false;
646 }
647
648 return true;
649 }
650
651 // -----------------------------------------------------------------------
652
653 void
654 wxPropertyGridInterface::SetPropertyBackgroundColour( wxPGPropArg id,
655 const wxColour& colour,
656 int flags )
657 {
658 wxPG_PROP_ARG_CALL_PROLOG()
659 p->SetBackgroundColour(colour, flags);
660 RefreshProperty(p);
661 }
662
663 // -----------------------------------------------------------------------
664
665 void wxPropertyGridInterface::SetPropertyTextColour( wxPGPropArg id,
666 const wxColour& colour,
667 int flags )
668 {
669 wxPG_PROP_ARG_CALL_PROLOG()
670 p->SetTextColour(colour, flags);
671 RefreshProperty(p);
672 }
673
674 // -----------------------------------------------------------------------
675
676 void wxPropertyGridInterface::SetPropertyColoursToDefault( wxPGPropArg id )
677 {
678 wxPG_PROP_ARG_CALL_PROLOG()
679
680 p->m_cells.clear();
681 }
682
683 // -----------------------------------------------------------------------
684
685 void wxPropertyGridInterface::SetPropertyCell( wxPGPropArg id,
686 int column,
687 const wxString& text,
688 const wxBitmap& bitmap,
689 const wxColour& fgCol,
690 const wxColour& bgCol )
691 {
692 wxPG_PROP_ARG_CALL_PROLOG()
693
694 wxPGCell& cell = p->GetCell(column);
695 if ( text.length() && text != wxPG_LABEL )
696 cell.SetText(text);
697 if ( bitmap.IsOk() )
698 cell.SetBitmap(bitmap);
699 if ( fgCol != wxNullColour )
700 cell.SetFgCol(fgCol);
701 if ( bgCol != wxNullColour )
702 cell.SetBgCol(bgCol);
703 }
704
705 // -----------------------------------------------------------------------
706 // GetPropertyValueAsXXX methods
707
708 #define IMPLEMENT_GET_VALUE(T,TRET,BIGNAME,DEFRETVAL) \
709 TRET wxPropertyGridInterface::GetPropertyValueAs##BIGNAME( wxPGPropArg id ) const \
710 { \
711 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(DEFRETVAL) \
712 wxVariant value = p->GetValue(); \
713 if ( wxStrcmp(value.GetType(), wxPGTypeName_##T) != 0 ) \
714 { \
715 wxPGGetFailed(p,wxPGTypeName_##T); \
716 return (TRET)DEFRETVAL; \
717 } \
718 return (TRET)value.Get##BIGNAME(); \
719 }
720
721 // String is different than others.
722 wxString wxPropertyGridInterface::GetPropertyValueAsString( wxPGPropArg id ) const
723 {
724 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxEmptyString)
725 return p->GetValueAsString(wxPG_FULL_VALUE);
726 }
727
728 bool wxPropertyGridInterface::GetPropertyValueAsBool( wxPGPropArg id ) const
729 {
730 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
731 wxVariant value = p->GetValue();
732 if ( wxStrcmp(value.GetType(), wxPGTypeName_bool) == 0 )
733 {
734 return value.GetBool();
735 }
736 if ( wxStrcmp(value.GetType(), wxPGTypeName_long) == 0 )
737 {
738 return value.GetLong()?true:false;
739 }
740 wxPGGetFailed(p,wxPGTypeName_bool);
741 return false;
742 }
743
744 IMPLEMENT_GET_VALUE(long,long,Long,0)
745 IMPLEMENT_GET_VALUE(double,double,Double,0.0)
746
747 bool wxPropertyGridInterface::IsPropertyExpanded( wxPGPropArg id ) const
748 {
749 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
750 return p->IsExpanded();
751 }
752
753 // -----------------------------------------------------------------------
754 // wxPropertyGridInterface wrappers
755 // -----------------------------------------------------------------------
756
757 bool wxPropertyGridInterface::ChangePropertyValue( wxPGPropArg id, wxVariant newValue )
758 {
759 return GetPropertyGrid()->ChangePropertyValue(id, newValue);
760 }
761
762 // -----------------------------------------------------------------------
763
764 void wxPropertyGridInterface::BeginAddChildren( wxPGPropArg id )
765 {
766 wxPG_PROP_ARG_CALL_PROLOG()
767 wxCHECK_RET( p->HasFlag(wxPG_PROP_AGGREGATE), wxT("only call on properties with fixed children") );
768 p->ClearFlag(wxPG_PROP_AGGREGATE);
769 p->SetFlag(wxPG_PROP_MISC_PARENT);
770 }
771
772 // -----------------------------------------------------------------------
773
774 bool wxPropertyGridInterface::EditorValidate()
775 {
776 return GetPropertyGrid()->DoEditorValidate();
777 }
778
779 // -----------------------------------------------------------------------
780
781 void wxPropertyGridInterface::EndAddChildren( wxPGPropArg id )
782 {
783 wxPG_PROP_ARG_CALL_PROLOG()
784 wxCHECK_RET( p->HasFlag(wxPG_PROP_MISC_PARENT), wxT("only call on properties for which BeginAddChildren was called prior") );
785 p->ClearFlag(wxPG_PROP_MISC_PARENT);
786 p->SetFlag(wxPG_PROP_AGGREGATE);
787 }
788
789 // -----------------------------------------------------------------------
790 // wxPGVIterator_State
791 // -----------------------------------------------------------------------
792
793 // Default returned by wxPropertyGridInterface::GetVIterator().
794 class wxPGVIteratorBase_State : public wxPGVIteratorBase
795 {
796 public:
797 wxPGVIteratorBase_State( wxPropertyGridPageState* state, int flags )
798 {
799 m_it.Init( state, flags );
800 }
801 virtual ~wxPGVIteratorBase_State() { }
802 virtual void Next() { m_it.Next(); }
803 };
804
805 wxPGVIterator wxPropertyGridInterface::GetVIterator( int flags ) const
806 {
807 return wxPGVIterator( new wxPGVIteratorBase_State( m_pState, flags ) );
808 }
809
810 // -----------------------------------------------------------------------
811 // wxPGEditableState related functions
812 // -----------------------------------------------------------------------
813
814 // EscapeDelimiters() changes ";" into "\;" and "|" into "\|"
815 // in the input string. This is an internal functions which is
816 // used for saving states
817 // NB: Similar function exists in aui/framemanager.cpp
818 static wxString EscapeDelimiters(const wxString& s)
819 {
820 wxString result;
821 result.Alloc(s.length());
822 const wxChar* ch = s.c_str();
823 while (*ch)
824 {
825 if (*ch == wxT(';') || *ch == wxT('|') || *ch == wxT(','))
826 result += wxT('\\');
827 result += *ch;
828 ++ch;
829 }
830 return result;
831 }
832
833 wxString wxPropertyGridInterface::SaveEditableState( int includedStates ) const
834 {
835 wxString result;
836
837 //
838 // Save state on page basis
839 unsigned int pageIndex = 0;
840 wxArrayPtrVoid pageStates;
841
842 for (;;)
843 {
844 wxPropertyGridPageState* page = GetPageState(pageIndex);
845 if ( !page ) break;
846
847 pageStates.Add(page);
848
849 pageIndex++;
850 }
851
852 for ( pageIndex=0; pageIndex < pageStates.size(); pageIndex++ )
853 {
854 wxPropertyGridPageState* pageState = (wxPropertyGridPageState*) pageStates[pageIndex];
855
856 if ( includedStates & SelectionState )
857 {
858 wxString sel;
859 if ( pageState->GetSelection() )
860 sel = pageState->GetSelection()->GetName();
861 result += wxS("selection=");
862 result += EscapeDelimiters(sel);
863 result += wxS(";");
864 }
865 if ( includedStates & ExpandedState )
866 {
867 wxArrayPGProperty ptrs;
868 wxPropertyGridConstIterator it =
869 wxPropertyGridConstIterator( pageState,
870 wxPG_ITERATE_ALL_PARENTS_RECURSIVELY|wxPG_ITERATE_HIDDEN,
871 wxNullProperty );
872
873 result += wxS("expanded=");
874
875 for ( ;
876 !it.AtEnd();
877 it.Next() )
878 {
879 const wxPGProperty* p = it.GetProperty();
880
881 if ( !p->HasFlag(wxPG_PROP_COLLAPSED) )
882 result += EscapeDelimiters(p->GetName());
883 result += wxS(",");
884
885 }
886
887 if ( result.Last() == wxS(',') )
888 result.RemoveLast();
889
890 result += wxS(";");
891 }
892 if ( includedStates & ScrollPosState )
893 {
894 int x, y;
895 GetPropertyGrid()->GetViewStart(&x,&y);
896 result += wxString::Format(wxS("scrollpos=%i,%i;"), x, y);
897 }
898 if ( includedStates & SplitterPosState )
899 {
900 result += wxS("splitterpos=");
901
902 for ( size_t i=0; i<pageState->GetColumnCount(); i++ )
903 result += wxString::Format(wxS("%i,"), pageState->DoGetSplitterPosition(i));
904
905 result.RemoveLast(); // Remove last comma
906 result += wxS(";");
907 }
908 if ( includedStates & PageState )
909 {
910 result += wxS("ispageselected=");
911
912 if ( GetPageState(-1) == pageState )
913 result += wxS("1;");
914 else
915 result += wxS("0;");
916 }
917 if ( includedStates & DescBoxState )
918 {
919 wxVariant v = GetEditableStateItem(wxS("descboxheight"));
920 if ( !v.IsNull() )
921 result += wxString::Format(wxS("descboxheight=%i;"), (int)v.GetLong());
922 }
923 result.RemoveLast(); // Remove last semicolon
924 result += wxS("|");
925 }
926
927 // Remove last '|'
928 if ( result.length() )
929 result.RemoveLast();
930
931 return result;
932 }
933
934 bool wxPropertyGridInterface::RestoreEditableState( const wxString& src, int restoreStates )
935 {
936 wxPropertyGrid* pg = GetPropertyGrid();
937 wxPGProperty* newSelection = NULL;
938 size_t pageIndex;
939 long vx = -1;
940 long vy = -1;
941 long selectedPage = -1;
942 bool pgSelectionSet = false;
943 bool res = true;
944
945 pg->Freeze();
946 wxArrayString pageStrings = ::wxSplit(src, wxS('|'), wxS('\\'));
947
948 for ( pageIndex=0; pageIndex<pageStrings.size(); pageIndex++ )
949 {
950 wxPropertyGridPageState* pageState = GetPageState(pageIndex);
951 if ( !pageState )
952 break;
953
954 wxArrayString kvpairStrings = ::wxSplit(pageStrings[pageIndex], wxS(';'), wxS('\\'));
955
956 for ( size_t i=0; i<kvpairStrings.size(); i++ )
957 {
958 const wxString& kvs = kvpairStrings[i];
959 int eq_pos = kvs.Find(wxS('='));
960 if ( eq_pos != wxNOT_FOUND )
961 {
962 wxString key = kvs.substr(0, eq_pos);
963 wxString value = kvs.substr(eq_pos+1);
964
965 // Further split value by commas
966 wxArrayString values = ::wxSplit(value, wxS(','), wxS('\\'));
967
968 if ( key == wxS("expanded") )
969 {
970 if ( restoreStates & ExpandedState )
971 {
972 wxPropertyGridIterator it =
973 wxPropertyGridIterator( pageState,
974 wxPG_ITERATE_ALL,
975 wxNullProperty );
976
977 // First collapse all
978 for ( ; !it.AtEnd(); it.Next() )
979 {
980 wxPGProperty* p = it.GetProperty();
981 pageState->DoCollapse(p);
982 }
983
984 // Then expand those which names are in values
985 for ( size_t n=0; n<values.size(); n++ )
986 {
987 const wxString& name = values[n];
988 wxPGProperty* prop = GetPropertyByName(name);
989 if ( prop )
990 pageState->DoExpand(prop);
991 }
992 }
993 }
994 else if ( key == wxS("scrollpos") )
995 {
996 if ( restoreStates & ScrollPosState )
997 {
998 if ( values.size() == 2 )
999 {
1000 values[0].ToLong(&vx);
1001 values[1].ToLong(&vy);
1002 }
1003 else
1004 {
1005 res = false;
1006 }
1007 }
1008 }
1009 else if ( key == wxS("splitterpos") )
1010 {
1011 if ( restoreStates & SplitterPosState )
1012 {
1013 for ( size_t n=1; n<values.size(); n++ )
1014 {
1015 long pos = 0;
1016 values[n].ToLong(&pos);
1017 if ( pos > 0 )
1018 pageState->DoSetSplitterPosition(pos, n);
1019 }
1020 }
1021 }
1022 else if ( key == wxS("selection") )
1023 {
1024 if ( restoreStates & SelectionState )
1025 {
1026 if ( values.size() > 0 )
1027 {
1028 if ( pageState->IsDisplayed() )
1029 {
1030 if ( values[0].length() )
1031 newSelection = GetPropertyByName(value);
1032 pgSelectionSet = true;
1033 }
1034 else
1035 {
1036 if ( values[0].length() )
1037 pageState->DoSetSelection(GetPropertyByName(value));
1038 else
1039 pageState->DoClearSelection();
1040 }
1041 }
1042 }
1043 }
1044 else if ( key == wxS("ispageselected") )
1045 {
1046 if ( restoreStates & PageState )
1047 {
1048 long pageSelStatus;
1049 if ( values.size() == 1 && values[0].ToLong(&pageSelStatus) )
1050 {
1051 if ( pageSelStatus )
1052 selectedPage = pageIndex;
1053 }
1054 else
1055 {
1056 res = false;
1057 }
1058 }
1059 }
1060 else if ( key == wxS("descboxheight") )
1061 {
1062 if ( restoreStates & DescBoxState )
1063 {
1064 long descBoxHeight;
1065 if ( values.size() == 1 && values[0].ToLong(&descBoxHeight) )
1066 {
1067 SetEditableStateItem(wxS("descboxheight"), descBoxHeight);
1068 }
1069 else
1070 {
1071 res = false;
1072 }
1073 }
1074 }
1075 else
1076 {
1077 res = false;
1078 }
1079 }
1080 }
1081 }
1082
1083 //
1084 // Force recalculation of virtual heights of all pages
1085 // (may be needed on unclean source string).
1086 pageIndex = 0;
1087 wxPropertyGridPageState* pageState = GetPageState(pageIndex);
1088 while ( pageState )
1089 {
1090 pageState->VirtualHeightChanged();
1091 pageIndex += 1;
1092 pageState = GetPageState(pageIndex);
1093 }
1094
1095 pg->Thaw();
1096
1097 //
1098 // Selection of visible grid page must be set after Thaw() call
1099 if ( pgSelectionSet )
1100 {
1101 if ( newSelection )
1102 pg->DoSelectProperty(newSelection);
1103 else
1104 pg->DoClearSelection();
1105 }
1106
1107 if ( selectedPage != -1 )
1108 {
1109 DoSelectPage(selectedPage);
1110 }
1111
1112 if ( vx >= 0 )
1113 {
1114 pg->Scroll(vx, vy);
1115 }
1116
1117 return res;
1118 }
1119
1120 #endif // wxUSE_PROPGRID
1121