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