Moved Get/SetPropertyText/BackgroundColour() member functions from wxPropertyGrid...
[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 {
319 bool selRes = grid->DoSelectProperty(NULL, wxPG_SEL_DELETING);
320 wxPG_CHECK_RET_DBG( selRes,
321 wxT("failed to deselect a property (editor probably had invalid value)") );
322 }
323
324 state->DoDelete( p );
325
326 RefreshGrid(state);
327 }
328
329 // -----------------------------------------------------------------------
330
331 wxPGProperty* wxPropertyGridInterface::ReplaceProperty( wxPGPropArg id, wxPGProperty* property )
332 {
333 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
334
335 wxPGProperty* replaced = p;
336 wxCHECK_MSG( replaced && property,
337 wxNullProperty,
338 wxT("NULL property") );
339 wxCHECK_MSG( !replaced->IsCategory(),
340 wxNullProperty,
341 wxT("cannot replace this type of property") );
342 wxCHECK_MSG( !m_pState->IsInNonCatMode(),
343 wxNullProperty,
344 wxT("cannot replace properties in alphabetic mode") );
345
346 // Get address to the slot
347 wxPGProperty* parent = replaced->GetParent();
348 int ind = replaced->GetIndexInParent();
349
350 wxPropertyGridPageState* state = replaced->GetParentState();
351 DeleteProperty(replaced); // Must use generic Delete
352 state->DoInsert(parent,ind,property);
353
354 return property;
355 }
356
357 // -----------------------------------------------------------------------
358 // wxPropertyGridInterface property operations
359 // -----------------------------------------------------------------------
360
361 bool wxPropertyGridInterface::ClearSelection()
362 {
363 wxPropertyGridPageState* state = m_pState;
364 wxPropertyGrid* pg = state->GetGrid();
365 if ( pg->GetState() == state )
366 return pg->DoClearSelection();
367 else
368 state->SetSelection(NULL);
369 return true;
370 }
371
372 // -----------------------------------------------------------------------
373
374 void wxPropertyGridInterface::LimitPropertyEditing( wxPGPropArg id, bool limit )
375 {
376 wxPG_PROP_ARG_CALL_PROLOG()
377
378 m_pState->DoLimitPropertyEditing(p, limit);
379 RefreshProperty(p);
380 }
381
382 // -----------------------------------------------------------------------
383
384 bool wxPropertyGridInterface::EnableProperty( wxPGPropArg id, bool enable )
385 {
386 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
387
388 wxPropertyGridPageState* state = p->GetParentState();
389 wxPropertyGrid* grid = state->GetGrid();
390
391 if ( enable )
392 {
393 if ( !(p->m_flags & wxPG_PROP_DISABLED) )
394 return false;
395
396 // If active, Set active Editor.
397 if ( grid->GetState() == state && p == grid->GetSelection() )
398 grid->DoSelectProperty( p, wxPG_SEL_FORCE );
399 }
400 else
401 {
402 if ( p->m_flags & wxPG_PROP_DISABLED )
403 return false;
404
405 // If active, Disable as active Editor.
406 if ( grid->GetState() == state && p == grid->GetSelection() )
407 grid->DoSelectProperty( p, wxPG_SEL_FORCE );
408 }
409
410 state->DoEnableProperty(p, enable);
411
412 RefreshProperty( p );
413
414 return true;
415 }
416
417 // -----------------------------------------------------------------------
418
419 bool wxPropertyGridInterface::ExpandAll( bool doExpand )
420 {
421 wxPropertyGridPageState* state = m_pState;
422
423 if ( !state->DoGetRoot()->GetChildCount() )
424 return true;
425
426 wxPropertyGrid* pg = state->GetGrid();
427
428 if ( GetSelection() && GetSelection() != state->DoGetRoot() &&
429 !doExpand )
430 {
431 if ( !pg->ClearSelection() )
432 return false;
433 }
434
435 wxPGVIterator it;
436
437 for ( it = GetVIterator( wxPG_ITERATE_ALL ); !it.AtEnd(); it.Next() )
438 {
439 wxPGProperty* p = (wxPGProperty*) it.GetProperty();
440 if ( p->GetChildCount() )
441 {
442 if ( doExpand )
443 {
444 if ( !p->IsExpanded() )
445 {
446 state->DoExpand(p);
447 }
448 }
449 else
450 {
451 if ( p->IsExpanded() )
452 {
453 state->DoCollapse(p);
454 }
455 }
456 }
457 }
458
459 pg->RecalculateVirtualSize();
460
461 RefreshGrid();
462
463 return true;
464 }
465
466 // -----------------------------------------------------------------------
467
468 void wxPropertyGridInterface::SetPropertyValueUnspecified( wxPGPropArg id )
469 {
470 wxPG_PROP_ARG_CALL_PROLOG()
471 wxPropertyGrid* propGrid = p->GetGridIfDisplayed();
472 if ( propGrid )
473 propGrid->DoSetPropertyValueUnspecified(p);
474 else
475 p->GetParentState()->DoSetPropertyValueUnspecified(p);
476 }
477
478 // -----------------------------------------------------------------------
479
480 void wxPropertyGridInterface::ClearModifiedStatus()
481 {
482 unsigned int pageIndex = 0;
483
484 for (;;)
485 {
486 wxPropertyGridPageState* page = GetPageState(pageIndex);
487 if ( !page ) break;
488
489 page->DoGetRoot()->SetFlagRecursively(wxPG_PROP_MODIFIED, false);
490
491 pageIndex++;
492 }
493 }
494
495 // -----------------------------------------------------------------------
496 // wxPropertyGridInterface property value setting and getting
497 // -----------------------------------------------------------------------
498
499 void wxPGGetFailed( const wxPGProperty* p, const wxString& typestr )
500 {
501 wxPGTypeOperationFailed(p, typestr, wxS("Get"));
502 }
503
504 // -----------------------------------------------------------------------
505
506 void wxPGTypeOperationFailed( const wxPGProperty* p,
507 const wxString& typestr,
508 const wxString& op )
509 {
510 wxASSERT( p != NULL );
511 wxLogError( _("Type operation \"%s\" failed: Property labeled \"%s\" is of type \"%s\", NOT \"%s\"."),
512 op.c_str(), p->GetLabel().c_str(), p->GetValue().GetType().c_str(), typestr.c_str() );
513 }
514
515 // -----------------------------------------------------------------------
516
517 void wxPropertyGridInterface::SetPropVal( wxPGPropArg id, wxVariant& value )
518 {
519 wxPG_PROP_ARG_CALL_PROLOG()
520
521 if ( p )
522 {
523 p->SetValue(value);
524 wxPropertyGrid* propGrid = p->GetGridIfDisplayed();
525 if ( propGrid )
526 propGrid->DrawItemAndValueRelated( p );
527
528 }
529 }
530
531 // -----------------------------------------------------------------------
532
533 void wxPropertyGridInterface::SetPropertyValueString( wxPGPropArg id, const wxString& value )
534 {
535 wxPG_PROP_ARG_CALL_PROLOG()
536
537 if ( m_pState->DoSetPropertyValueString(p,value) )
538 {
539 wxPropertyGrid* propGrid = p->GetGridIfDisplayed();
540 if ( propGrid )
541 propGrid->DrawItemAndValueRelated( p );
542 }
543 }
544
545 // -----------------------------------------------------------------------
546
547 void wxPropertyGridInterface::SetValidationFailureBehavior( int vfbFlags )
548 {
549 GetPropertyGrid()->m_permanentValidationFailureBehavior = vfbFlags;
550 }
551
552 // -----------------------------------------------------------------------
553
554 wxPGProperty* wxPropertyGridInterface::GetPropertyByNameA( const wxString& name ) const
555 {
556 wxPGProperty* p = GetPropertyByName(name);
557 wxASSERT_MSG(p,wxString::Format(wxT("no property with name '%s'"),name.c_str()));
558 return p;
559 }
560
561 // ----------------------------------------------------------------------------
562
563 wxPGProperty* wxPropertyGridInterface::GetPropertyByLabel( const wxString& label ) const
564 {
565 wxPGVIterator it;
566
567 for ( it = GetVIterator( wxPG_ITERATE_PROPERTIES ); !it.AtEnd(); it.Next() )
568 {
569 if ( it.GetProperty()->GetLabel() == label )
570 return it.GetProperty();
571 }
572
573 return wxNullProperty;
574 }
575
576 // ----------------------------------------------------------------------------
577
578 void wxPropertyGridInterface::DoSetPropertyAttribute( wxPGPropArg id, const wxString& name,
579 wxVariant& value, long argFlags )
580 {
581 wxPG_PROP_ARG_CALL_PROLOG()
582
583 p->SetAttribute( name, value );
584
585 if ( argFlags & wxPG_RECURSE )
586 {
587 unsigned int i;
588 for ( i = 0; i < p->GetChildCount(); i++ )
589 DoSetPropertyAttribute(p->Item(i), name, value, argFlags);
590 }
591 }
592
593 // -----------------------------------------------------------------------
594
595 void wxPropertyGridInterface::SetPropertyAttributeAll( const wxString& attrName,
596 wxVariant value )
597 {
598 unsigned int pageIndex = 0;
599
600 for (;;)
601 {
602 wxPropertyGridPageState* page = GetPageState(pageIndex);
603 if ( !page ) break;
604
605 DoSetPropertyAttribute(page->DoGetRoot(), attrName, value, wxPG_RECURSE);
606
607 pageIndex++;
608 }
609 }
610
611 // -----------------------------------------------------------------------
612
613 void wxPropertyGridInterface::GetPropertiesWithFlag( wxArrayPGProperty* targetArr,
614 wxPGProperty::FlagType flags,
615 bool inverse,
616 int iterFlags ) const
617 {
618 wxASSERT( targetArr );
619 wxPGVIterator it = GetVIterator( iterFlags );
620
621 for ( ;
622 !it.AtEnd();
623 it.Next() )
624 {
625 const wxPGProperty* property = it.GetProperty();
626
627 if ( !inverse )
628 {
629 if ( (property->GetFlags() & flags) == flags )
630 targetArr->push_back((wxPGProperty*)property);
631 }
632 else
633 {
634 if ( (property->GetFlags() & flags) != flags )
635 targetArr->push_back((wxPGProperty*)property);
636 }
637 }
638 }
639
640 // -----------------------------------------------------------------------
641
642 void wxPropertyGridInterface::SetBoolChoices( const wxString& trueChoice,
643 const wxString& falseChoice )
644 {
645 wxPGGlobalVars->m_boolChoices[0] = falseChoice;
646 wxPGGlobalVars->m_boolChoices[1] = trueChoice;
647 }
648
649 // -----------------------------------------------------------------------
650
651 wxPGProperty* wxPropertyGridInterface::DoGetPropertyByName( const wxString& name ) const
652 {
653 return m_pState->BaseGetPropertyByName(name);
654 }
655
656 // -----------------------------------------------------------------------
657
658 wxPGProperty* wxPropertyGridInterface::GetPropertyByName( const wxString& name,
659 const wxString& subname ) const
660 {
661 wxPGProperty* p = DoGetPropertyByName(name);
662 if ( !p || !p->GetChildCount() )
663 return wxNullProperty;
664
665 return p->GetPropertyByName(subname);
666 }
667
668 // -----------------------------------------------------------------------
669
670 // Since GetPropertyByName is used *a lot*, this makes sense
671 // since non-virtual method can be called with less code.
672 wxPGProperty* wxPropertyGridInterface::GetPropertyByName( const wxString& name ) const
673 {
674 wxPGProperty* p = DoGetPropertyByName(name);
675 if ( p )
676 return p;
677
678 // Check if its "Property.SubProperty" format
679 int pos = name.Find(wxT('.'));
680 if ( pos <= 0 )
681 return NULL;
682
683 return GetPropertyByName(name.substr(0,pos),
684 name.substr(pos+1,name.length()-pos-1));
685 }
686
687 // -----------------------------------------------------------------------
688
689 bool wxPropertyGridInterface::HideProperty( wxPGPropArg id, bool hide, int flags )
690 {
691 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
692
693 wxPropertyGrid* pg = m_pState->GetGrid();
694
695 if ( pg == p->GetGrid() )
696 return pg->DoHideProperty(p, hide, flags);
697 else
698 m_pState->DoHideProperty(p, hide, flags);
699
700 return true;
701 }
702
703 // -----------------------------------------------------------------------
704
705 bool wxPropertyGridInterface::Collapse( wxPGPropArg id )
706 {
707 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
708 wxPropertyGrid* pg = p->GetGridIfDisplayed();
709 if ( pg )
710 return pg->DoCollapse(p);
711
712 return p->GetParentState()->DoCollapse(p);
713 }
714
715 // -----------------------------------------------------------------------
716
717 bool wxPropertyGridInterface::Expand( wxPGPropArg id )
718 {
719 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
720 wxPropertyGrid* pg = p->GetGridIfDisplayed();
721 if ( pg )
722 return pg->DoExpand(p);
723
724 return p->GetParentState()->DoExpand(p);
725 }
726
727 // -----------------------------------------------------------------------
728
729 void wxPropertyGridInterface::SetPropertyLabel( wxPGPropArg id, const wxString& newproplabel )
730 {
731 wxPG_PROP_ARG_CALL_PROLOG()
732
733 p->SetLabel( newproplabel );
734
735 wxPropertyGridPageState* state = p->GetParentState();
736 wxPropertyGrid* pg = state->GetGrid();
737
738 if ( pg->HasFlag(wxPG_AUTO_SORT) )
739 pg->SortChildren(p->GetParent());
740
741 if ( pg->GetState() == state )
742 {
743 if ( pg->HasFlag(wxPG_AUTO_SORT) )
744 pg->Refresh();
745 else
746 pg->DrawItem( p );
747 }
748 }
749
750 // -----------------------------------------------------------------------
751
752 bool wxPropertyGridInterface::SetPropertyMaxLength( wxPGPropArg id, int maxLen )
753 {
754 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
755
756 wxPropertyGrid* pg = m_pState->GetGrid();
757
758 p->m_maxLen = (short) maxLen;
759
760 // Adjust control if selected currently
761 if ( pg == p->GetGrid() && p == m_pState->GetSelection() )
762 {
763 wxWindow* wnd = pg->GetEditorControl();
764 wxTextCtrl* tc = wxDynamicCast(wnd,wxTextCtrl);
765 if ( tc )
766 tc->SetMaxLength( maxLen );
767 else
768 // Not a text ctrl
769 return false;
770 }
771
772 return true;
773 }
774
775 // -----------------------------------------------------------------------
776
777 void
778 wxPropertyGridInterface::SetPropertyBackgroundColour( wxPGPropArg id,
779 const wxColour& colour,
780 bool recursively )
781 {
782 wxPG_PROP_ARG_CALL_PROLOG()
783 p->SetBackgroundColour( colour, recursively );
784 RefreshProperty( p );
785 }
786
787 // -----------------------------------------------------------------------
788
789 void wxPropertyGridInterface::SetPropertyTextColour( wxPGPropArg id,
790 const wxColour& colour,
791 bool recursively )
792 {
793 wxPG_PROP_ARG_CALL_PROLOG()
794 p->SetTextColour( colour, recursively );
795 RefreshProperty( p );
796 }
797
798 // -----------------------------------------------------------------------
799
800 void wxPropertyGridInterface::SetPropertyColoursToDefault( wxPGPropArg id )
801 {
802 wxPG_PROP_ARG_CALL_PROLOG()
803
804 p->m_cells.clear();
805 }
806
807 // -----------------------------------------------------------------------
808
809 void wxPropertyGridInterface::SetPropertyCell( wxPGPropArg id,
810 int column,
811 const wxString& text,
812 const wxBitmap& bitmap,
813 const wxColour& fgCol,
814 const wxColour& bgCol )
815 {
816 wxPG_PROP_ARG_CALL_PROLOG()
817
818 wxPGCell& cell = p->GetCell(column);
819 if ( text.length() && text != wxPG_LABEL )
820 cell.SetText(text);
821 if ( bitmap.IsOk() )
822 cell.SetBitmap(bitmap);
823 if ( fgCol != wxNullColour )
824 cell.SetFgCol(fgCol);
825 if ( bgCol != wxNullColour )
826 cell.SetBgCol(bgCol);
827 }
828
829 // -----------------------------------------------------------------------
830 // GetPropertyValueAsXXX methods
831
832 #define IMPLEMENT_GET_VALUE(T,TRET,BIGNAME,DEFRETVAL) \
833 TRET wxPropertyGridInterface::GetPropertyValueAs##BIGNAME( wxPGPropArg id ) const \
834 { \
835 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(DEFRETVAL) \
836 wxVariant value = p->GetValue(); \
837 if ( wxStrcmp(value.GetType(), wxPGTypeName_##T) != 0 ) \
838 { \
839 wxPGGetFailed(p,wxPGTypeName_##T); \
840 return (TRET)DEFRETVAL; \
841 } \
842 return (TRET)value.Get##BIGNAME(); \
843 }
844
845 // String is different than others.
846 wxString wxPropertyGridInterface::GetPropertyValueAsString( wxPGPropArg id ) const
847 {
848 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxEmptyString)
849 return p->GetValueAsString(wxPG_FULL_VALUE);
850 }
851
852 bool wxPropertyGridInterface::GetPropertyValueAsBool( wxPGPropArg id ) const
853 {
854 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
855 wxVariant value = p->GetValue();
856 if ( wxStrcmp(value.GetType(), wxPGTypeName_bool) == 0 )
857 {
858 return value.GetBool();
859 }
860 if ( wxStrcmp(value.GetType(), wxPGTypeName_long) == 0 )
861 {
862 return value.GetLong()?true:false;
863 }
864 wxPGGetFailed(p,wxPGTypeName_bool);
865 return false;
866 }
867
868 IMPLEMENT_GET_VALUE(long,long,Long,0)
869 IMPLEMENT_GET_VALUE(double,double,Double,0.0)
870
871 bool wxPropertyGridInterface::IsPropertyExpanded( wxPGPropArg id ) const
872 {
873 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
874 return p->IsExpanded();
875 }
876
877 // -----------------------------------------------------------------------
878 // wxPropertyGridInterface wrappers
879 // -----------------------------------------------------------------------
880
881 bool wxPropertyGridInterface::ChangePropertyValue( wxPGPropArg id, wxVariant newValue )
882 {
883 return GetPropertyGrid()->ChangePropertyValue(id, newValue);
884 }
885
886 // -----------------------------------------------------------------------
887
888 void wxPropertyGridInterface::BeginAddChildren( wxPGPropArg id )
889 {
890 wxPG_PROP_ARG_CALL_PROLOG()
891 wxCHECK_RET( p->HasFlag(wxPG_PROP_AGGREGATE), wxT("only call on properties with fixed children") );
892 p->ClearFlag(wxPG_PROP_AGGREGATE);
893 p->SetFlag(wxPG_PROP_MISC_PARENT);
894 }
895
896 // -----------------------------------------------------------------------
897
898 bool wxPropertyGridInterface::EditorValidate()
899 {
900 return GetPropertyGrid()->DoEditorValidate();
901 }
902
903 // -----------------------------------------------------------------------
904
905 void wxPropertyGridInterface::EndAddChildren( wxPGPropArg id )
906 {
907 wxPG_PROP_ARG_CALL_PROLOG()
908 wxCHECK_RET( p->HasFlag(wxPG_PROP_MISC_PARENT), wxT("only call on properties for which BeginAddChildren was called prior") );
909 p->ClearFlag(wxPG_PROP_MISC_PARENT);
910 p->SetFlag(wxPG_PROP_AGGREGATE);
911 }
912
913 // -----------------------------------------------------------------------
914 // wxPGVIterator_State
915 // -----------------------------------------------------------------------
916
917 // Default returned by wxPropertyGridInterface::GetVIterator().
918 class wxPGVIteratorBase_State : public wxPGVIteratorBase
919 {
920 public:
921 wxPGVIteratorBase_State( wxPropertyGridPageState* state, int flags )
922 {
923 m_it.Init( state, flags );
924 }
925 virtual ~wxPGVIteratorBase_State() { }
926 virtual void Next() { m_it.Next(); }
927 };
928
929 wxPGVIterator wxPropertyGridInterface::GetVIterator( int flags ) const
930 {
931 return wxPGVIterator( new wxPGVIteratorBase_State( m_pState, flags ) );
932 }
933
934 // -----------------------------------------------------------------------
935 // wxPGEditableState related functions
936 // -----------------------------------------------------------------------
937
938 // EscapeDelimiters() changes ";" into "\;" and "|" into "\|"
939 // in the input string. This is an internal functions which is
940 // used for saving states
941 // NB: Similar function exists in aui/framemanager.cpp
942 static wxString EscapeDelimiters(const wxString& s)
943 {
944 wxString result;
945 result.Alloc(s.length());
946 const wxChar* ch = s.c_str();
947 while (*ch)
948 {
949 if (*ch == wxT(';') || *ch == wxT('|') || *ch == wxT(','))
950 result += wxT('\\');
951 result += *ch;
952 ++ch;
953 }
954 return result;
955 }
956
957 wxString wxPropertyGridInterface::SaveEditableState( int includedStates ) const
958 {
959 wxString result;
960
961 //
962 // Save state on page basis
963 unsigned int pageIndex = 0;
964 wxArrayPtrVoid pageStates;
965
966 for (;;)
967 {
968 wxPropertyGridPageState* page = GetPageState(pageIndex);
969 if ( !page ) break;
970
971 pageStates.Add(page);
972
973 pageIndex++;
974 }
975
976 for ( pageIndex=0; pageIndex < pageStates.size(); pageIndex++ )
977 {
978 wxPropertyGridPageState* pageState = (wxPropertyGridPageState*) pageStates[pageIndex];
979
980 if ( includedStates & SelectionState )
981 {
982 wxString sel;
983 if ( pageState->GetSelection() )
984 sel = pageState->GetSelection()->GetName();
985 result += wxS("selection=");
986 result += EscapeDelimiters(sel);
987 result += wxS(";");
988 }
989 if ( includedStates & ExpandedState )
990 {
991 wxArrayPGProperty ptrs;
992 wxPropertyGridConstIterator it =
993 wxPropertyGridConstIterator( pageState,
994 wxPG_ITERATE_ALL_PARENTS_RECURSIVELY|wxPG_ITERATE_HIDDEN,
995 wxNullProperty );
996
997 result += wxS("expanded=");
998
999 for ( ;
1000 !it.AtEnd();
1001 it.Next() )
1002 {
1003 const wxPGProperty* p = it.GetProperty();
1004
1005 if ( !p->HasFlag(wxPG_PROP_COLLAPSED) )
1006 result += EscapeDelimiters(p->GetName());
1007 result += wxS(",");
1008
1009 }
1010
1011 if ( result.Last() == wxS(',') )
1012 result.RemoveLast();
1013
1014 result += wxS(";");
1015 }
1016 if ( includedStates & ScrollPosState )
1017 {
1018 int x, y;
1019 GetPropertyGrid()->GetViewStart(&x,&y);
1020 result += wxString::Format(wxS("scrollpos=%i,%i;"), x, y);
1021 }
1022 if ( includedStates & SplitterPosState )
1023 {
1024 result += wxS("splitterpos=");
1025
1026 for ( size_t i=0; i<pageState->GetColumnCount(); i++ )
1027 result += wxString::Format(wxS("%i,"), pageState->DoGetSplitterPosition(i));
1028
1029 result.RemoveLast(); // Remove last comma
1030 result += wxS(";");
1031 }
1032 if ( includedStates & PageState )
1033 {
1034 result += wxS("ispageselected=");
1035
1036 if ( GetPageState(-1) == pageState )
1037 result += wxS("1;");
1038 else
1039 result += wxS("0;");
1040 }
1041 if ( includedStates & DescBoxState )
1042 {
1043 wxVariant v = GetEditableStateItem(wxS("descboxheight"));
1044 if ( !v.IsNull() )
1045 result += wxString::Format(wxS("descboxheight=%i;"), (int)v.GetLong());
1046 }
1047 result.RemoveLast(); // Remove last semicolon
1048 result += wxS("|");
1049 }
1050
1051 // Remove last '|'
1052 if ( result.length() )
1053 result.RemoveLast();
1054
1055 return result;
1056 }
1057
1058 bool wxPropertyGridInterface::RestoreEditableState( const wxString& src, int restoreStates )
1059 {
1060 wxPropertyGrid* pg = GetPropertyGrid();
1061 wxPGProperty* newSelection = NULL;
1062 size_t pageIndex;
1063 long vx = -1;
1064 long vy = -1;
1065 long selectedPage = -1;
1066 bool pgSelectionSet = false;
1067 bool res = true;
1068
1069 pg->Freeze();
1070 wxArrayString pageStrings = ::wxSplit(src, wxS('|'), wxS('\\'));
1071
1072 for ( pageIndex=0; pageIndex<pageStrings.size(); pageIndex++ )
1073 {
1074 wxPropertyGridPageState* pageState = GetPageState(pageIndex);
1075 if ( !pageState )
1076 break;
1077
1078 wxArrayString kvpairStrings = ::wxSplit(pageStrings[pageIndex], wxS(';'), wxS('\\'));
1079
1080 for ( size_t i=0; i<kvpairStrings.size(); i++ )
1081 {
1082 const wxString& kvs = kvpairStrings[i];
1083 int eq_pos = kvs.Find(wxS('='));
1084 if ( eq_pos != wxNOT_FOUND )
1085 {
1086 wxString key = kvs.substr(0, eq_pos);
1087 wxString value = kvs.substr(eq_pos+1);
1088
1089 // Further split value by commas
1090 wxArrayString values = ::wxSplit(value, wxS(','), wxS('\\'));
1091
1092 if ( key == wxS("expanded") )
1093 {
1094 if ( restoreStates & ExpandedState )
1095 {
1096 wxPropertyGridIterator it =
1097 wxPropertyGridIterator( pageState,
1098 wxPG_ITERATE_ALL,
1099 wxNullProperty );
1100
1101 // First collapse all
1102 for ( ; !it.AtEnd(); it.Next() )
1103 {
1104 wxPGProperty* p = it.GetProperty();
1105 pageState->DoCollapse(p);
1106 }
1107
1108 // Then expand those which names are in values
1109 for ( size_t n=0; n<values.size(); n++ )
1110 {
1111 const wxString& name = values[n];
1112 wxPGProperty* prop = GetPropertyByName(name);
1113 if ( prop )
1114 pageState->DoExpand(prop);
1115 }
1116 }
1117 }
1118 else if ( key == wxS("scrollpos") )
1119 {
1120 if ( restoreStates & ScrollPosState )
1121 {
1122 if ( values.size() == 2 )
1123 {
1124 values[0].ToLong(&vx);
1125 values[1].ToLong(&vy);
1126 }
1127 else
1128 {
1129 res = false;
1130 }
1131 }
1132 }
1133 else if ( key == wxS("splitterpos") )
1134 {
1135 if ( restoreStates & SplitterPosState )
1136 {
1137 for ( size_t n=1; n<values.size(); n++ )
1138 {
1139 long pos = 0;
1140 values[n].ToLong(&pos);
1141 if ( pos > 0 )
1142 pageState->DoSetSplitterPosition(pos, n);
1143 }
1144 }
1145 }
1146 else if ( key == wxS("selection") )
1147 {
1148 if ( restoreStates & SelectionState )
1149 {
1150 if ( values.size() > 0 )
1151 {
1152 if ( pageState->IsDisplayed() )
1153 {
1154 if ( values[0].length() )
1155 newSelection = GetPropertyByName(value);
1156 pgSelectionSet = true;
1157 }
1158 else
1159 {
1160 if ( values[0].length() )
1161 pageState->SetSelection(GetPropertyByName(value));
1162 else
1163 pageState->DoClearSelection();
1164 }
1165 }
1166 }
1167 }
1168 else if ( key == wxS("ispageselected") )
1169 {
1170 if ( restoreStates & PageState )
1171 {
1172 long pageSelStatus;
1173 if ( values.size() == 1 && values[0].ToLong(&pageSelStatus) )
1174 {
1175 if ( pageSelStatus )
1176 selectedPage = pageIndex;
1177 }
1178 else
1179 {
1180 res = false;
1181 }
1182 }
1183 }
1184 else if ( key == wxS("descboxheight") )
1185 {
1186 if ( restoreStates & DescBoxState )
1187 {
1188 long descBoxHeight;
1189 if ( values.size() == 1 && values[0].ToLong(&descBoxHeight) )
1190 {
1191 SetEditableStateItem(wxS("descboxheight"), descBoxHeight);
1192 }
1193 else
1194 {
1195 res = false;
1196 }
1197 }
1198 }
1199 else
1200 {
1201 res = false;
1202 }
1203 }
1204 }
1205 }
1206
1207 //
1208 // Force recalculation of virtual heights of all pages
1209 // (may be needed on unclean source string).
1210 pageIndex = 0;
1211 wxPropertyGridPageState* pageState = GetPageState(pageIndex);
1212 while ( pageState )
1213 {
1214 pageState->VirtualHeightChanged();
1215 pageIndex += 1;
1216 pageState = GetPageState(pageIndex);
1217 }
1218
1219 pg->Thaw();
1220
1221 //
1222 // Selection of visible grid page must be set after Thaw() call
1223 if ( pgSelectionSet )
1224 {
1225 if ( newSelection )
1226 pg->SelectProperty(newSelection);
1227 else
1228 pg->ClearSelection();
1229 }
1230
1231 if ( selectedPage != -1 )
1232 {
1233 DoSelectPage(selectedPage);
1234 }
1235
1236 if ( vx >= 0 )
1237 {
1238 pg->Scroll(vx, vy);
1239 }
1240
1241 return res;
1242 }
1243
1244 #endif // wxUSE_PROPGRID
1245