1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/propgrid/sampleprops.cpp
3 // Purpose: wxPropertyGrid Sample Properties
4 // Author: Jaakko Salli
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all "standard" wxWidgets headers)
25 // -----------------------------------------------------------------------
28 #include <wx/propgrid/propgrid.h>
29 #include <wx/propgrid/advprops.h>
31 #ifndef WX_PROPGRID_SAMPLEPROPS_H
32 #include "sampleprops.h"
36 // -----------------------------------------------------------------------
38 // -----------------------------------------------------------------------
40 // Dummy comparison required by value type implementation.
41 bool operator == (const wxFontData
&, const wxFontData
&)
46 // Custom version of wxFontProperty that also holds colour in the value.
47 // Original version by Vladimir Vainer.
49 #include <wx/fontdlg.h>
51 IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(wxFontData
)
53 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFontDataProperty
,wxFontProperty
,
54 wxFontData
,const wxFontData
&,TextCtrlAndButton
)
56 wxFontDataProperty::wxFontDataProperty( const wxString
& label
, const wxString
& name
,
57 const wxFontData
& value
) : wxFontProperty(label
,name
,value
.GetInitialFont())
59 wxFontData
fontData(value
);
62 fontData
.SetChosenFont(value
.GetInitialFont());
63 if ( !fontData
.GetColour().Ok() )
64 fontData
.SetColour(*wxBLACK
);
66 // Set initial value - should be done in a simpler way like this
67 // (instead of calling SetValue) in derived (wxObject) properties.
68 m_value_wxFontData
<< value
;
70 // Add extra children.
71 AddPrivateChild( new wxColourProperty(_("Colour"), wxPG_LABEL
,
72 fontData
.GetColour() ) );
75 wxFontDataProperty::~wxFontDataProperty () { }
77 void wxFontDataProperty::OnSetValue()
79 if ( m_value
.GetType() != "wxFontData" )
81 if ( m_value
.GetType() == "wxFont" )
86 fontData
.SetChosenFont(font
);
87 if ( !m_value_wxFontData
.IsNull() )
89 wxFontData oldFontData
;
90 oldFontData
<< m_value_wxFontData
;
91 fontData
.SetColour(oldFontData
.GetColour());
95 fontData
.SetColour(*wxBLACK
);
99 m_value_wxFontData
= variant
;
103 wxFAIL_MSG(wxT("Value to wxFontDataProperty must be eithe wxFontData or wxFont"));
108 // Set m_value to wxFont so that wxFontProperty methods will work
110 m_value_wxFontData
= m_value
;
113 fontData
<< m_value_wxFontData
;
115 wxFont font
= fontData
.GetChosenFont();
117 font
= wxFont(10,wxSWISS
,wxNORMAL
,wxNORMAL
);
119 m_value
= WXVARIANT(font
);
123 wxVariant
wxFontDataProperty::DoGetValue() const
125 return m_value_wxFontData
;
128 // Must re-create font dialog displayer.
129 bool wxFontDataProperty::OnEvent( wxPropertyGrid
* propgrid
,
130 wxWindow
* WXUNUSED(primary
), wxEvent
& event
)
132 if ( propgrid
->IsMainButtonEvent(event
) )
134 wxVariant useValue
= propgrid
->GetUncommittedPropertyValue();
137 fontData
<< useValue
;
139 fontData
.SetInitialFont(fontData
.GetChosenFont());
141 wxFontDialog
dlg(propgrid
, fontData
);
143 if ( dlg
.ShowModal() == wxID_OK
)
146 variant
<< dlg
.GetFontData();
147 SetValueInEvent( variant
);
154 void wxFontDataProperty::RefreshChildren()
156 wxFontProperty::RefreshChildren();
157 if ( GetChildCount() < 6 ) // Number is count of wxFontProperty's children + 1.
159 wxFontData fontData
; fontData
<< m_value_wxFontData
;
160 wxVariant variant
; variant
<< fontData
.GetColour();
161 Item(6)->SetValue( variant
);
164 void wxFontDataProperty::ChildChanged( wxVariant
& thisValue
, int childIndex
, wxVariant
& childValue
) const
167 fontData
<< thisValue
;
171 switch ( childIndex
)
175 fontData
.SetColour( col
);
178 // Transfer from subset to superset.
179 wxFont font
= fontData
.GetChosenFont();
180 variant
= WXVARIANT(font
);
181 wxFontProperty::ChildChanged( variant
, childIndex
, childValue
);
183 fontData
.SetChosenFont(font
);
186 thisValue
<< fontData
;
189 // -----------------------------------------------------------------------
191 // -----------------------------------------------------------------------
193 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxSizeProperty
,wxPGProperty
,
194 wxSize
,const wxSize
&,TextCtrl
)
196 wxSizeProperty::wxSizeProperty( const wxString
& label
, const wxString
& name
,
197 const wxSize
& value
) : wxPGProperty(label
,name
)
200 AddPrivateChild( new wxIntProperty(wxT("Width"),wxPG_LABEL
,value
.x
) );
201 AddPrivateChild( new wxIntProperty(wxT("Height"),wxPG_LABEL
,value
.y
) );
204 wxSizeProperty::~wxSizeProperty() { }
206 void wxSizeProperty::RefreshChildren()
208 if ( !GetChildCount() ) return;
209 const wxSize
& size
= wxSizeRefFromVariant(m_value
);
210 Item(0)->SetValue( (long)size
.x
);
211 Item(1)->SetValue( (long)size
.y
);
214 void wxSizeProperty::ChildChanged( wxVariant
& thisValue
, int childIndex
, wxVariant
& childValue
) const
216 wxSize
& size
= wxSizeRefFromVariant(thisValue
);
217 int val
= wxPGVariantToInt(childValue
);
218 switch ( childIndex
)
220 case 0: size
.x
= val
; break;
221 case 1: size
.y
= val
; break;
225 // -----------------------------------------------------------------------
227 // -----------------------------------------------------------------------
229 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxPointProperty
,wxPGProperty
,
230 wxPoint
,const wxPoint
&,TextCtrl
)
232 wxPointProperty::wxPointProperty( const wxString
& label
, const wxString
& name
,
233 const wxPoint
& value
) : wxPGProperty(label
,name
)
236 AddPrivateChild( new wxIntProperty(wxT("X"),wxPG_LABEL
,value
.x
) );
237 AddPrivateChild( new wxIntProperty(wxT("Y"),wxPG_LABEL
,value
.y
) );
240 wxPointProperty::~wxPointProperty() { }
242 void wxPointProperty::RefreshChildren()
244 if ( !GetChildCount() ) return;
245 const wxPoint
& point
= wxPointRefFromVariant(m_value
);
246 Item(0)->SetValue( (long)point
.x
);
247 Item(1)->SetValue( (long)point
.y
);
250 void wxPointProperty::ChildChanged( wxVariant
& thisValue
, int childIndex
, wxVariant
& childValue
) const
252 wxPoint
& point
= wxPointRefFromVariant(thisValue
);
253 int val
= wxPGVariantToInt(childValue
);
254 switch ( childIndex
)
256 case 0: point
.x
= val
; break;
257 case 1: point
.y
= val
; break;
262 // -----------------------------------------------------------------------
264 // -----------------------------------------------------------------------
266 WX_PG_IMPLEMENT_ARRAYSTRING_PROPERTY_WITH_VALIDATOR(wxDirsProperty
,wxT(','),wxT("Browse"))
270 wxValidator
* wxDirsProperty::DoGetValidator() const
272 return wxFileProperty::GetClassValidator();
278 bool wxDirsProperty::OnCustomStringEdit( wxWindow
* parent
, wxString
& value
)
280 wxDirDialog
dlg(parent
,
281 _("Select a directory to be added to the list:"),
285 if ( dlg
.ShowModal() == wxID_OK
)
287 value
= dlg
.GetPath();
293 // -----------------------------------------------------------------------
294 // wxArrayDoubleEditorDialog
295 // -----------------------------------------------------------------------
299 // You can *almost* convert wxArrayDoubleEditorDialog to wxArrayXXXEditorDialog
300 // by replacing each ArrayDouble with ArrayXXX.
303 class wxArrayDoubleEditorDialog
: public wxArrayEditorDialog
306 wxArrayDoubleEditorDialog();
310 wxArrayDoubleEditorDialog(wxWindow
*parent
,
311 const wxString
& message
,
312 const wxString
& caption
,
313 wxArrayDouble
& array
,
314 long style
= wxAEDIALOG_STYLE
,
315 const wxPoint
& pos
= wxDefaultPosition
,
316 const wxSize
& sz
= wxDefaultSize
);
318 bool Create(wxWindow
*parent
,
319 const wxString
& message
,
320 const wxString
& caption
,
321 wxArrayDouble
& array
,
322 long style
= wxAEDIALOG_STYLE
,
323 const wxPoint
& pos
= wxDefaultPosition
,
324 const wxSize
& sz
= wxDefaultSize
);
326 const wxArrayDouble
& GetArray() const { return m_array
; }
328 // Extra method for this type of array
329 void SetPrecision ( int precision
)
331 m_precision
= precision
;
332 m_dtoaTemplate
.Empty();
336 // Mandatory array of type
337 wxArrayDouble m_array
;
339 // Use this to avoid extra wxString creation+Printf
340 // on double-to-wxString conversion.
341 wxString m_dtoaTemplate
;
345 // Mandatory overridden methods
346 virtual wxString
ArrayGet( size_t index
);
347 virtual size_t ArrayGetCount();
348 virtual bool ArrayInsert( const wxString
& str
, int index
);
349 virtual bool ArraySet( size_t index
, const wxString
& str
);
350 virtual void ArrayRemoveAt( int index
);
351 virtual void ArraySwap( size_t first
, size_t second
);
354 DECLARE_DYNAMIC_CLASS_NO_COPY(wxArrayDoubleEditorDialog
)
357 IMPLEMENT_DYNAMIC_CLASS(wxArrayDoubleEditorDialog
, wxArrayEditorDialog
)
360 // Array dialog array access and manipulation
363 wxString
wxArrayDoubleEditorDialog::ArrayGet( size_t index
)
366 wxPropertyGrid::DoubleToString(str
,m_array
[index
],m_precision
,true,&m_dtoaTemplate
);
370 size_t wxArrayDoubleEditorDialog::ArrayGetCount()
372 return m_array
.GetCount();
375 bool wxArrayDoubleEditorDialog::ArrayInsert( const wxString
& str
, int index
)
378 if ( !str
.ToDouble(&d
) )
384 m_array
.Insert(d
,index
);
388 bool wxArrayDoubleEditorDialog::ArraySet( size_t index
, const wxString
& str
)
391 if ( !str
.ToDouble(&d
) )
397 void wxArrayDoubleEditorDialog::ArrayRemoveAt( int index
)
399 m_array
.RemoveAt(index
);
402 void wxArrayDoubleEditorDialog::ArraySwap( size_t first
, size_t second
)
404 double a
= m_array
[first
];
405 double b
= m_array
[second
];
411 // Array dialog construction etc.
414 wxArrayDoubleEditorDialog::wxArrayDoubleEditorDialog()
415 : wxArrayEditorDialog()
420 void wxArrayDoubleEditorDialog::Init()
422 wxArrayEditorDialog::Init();
426 wxArrayDoubleEditorDialog::wxArrayDoubleEditorDialog(wxWindow
*parent
,
427 const wxString
& message
,
428 const wxString
& caption
,
429 wxArrayDouble
& array
,
433 : wxArrayEditorDialog()
436 Create(parent
,message
,caption
,array
,style
,pos
,sz
);
439 bool wxArrayDoubleEditorDialog::Create(wxWindow
*parent
,
440 const wxString
& message
,
441 const wxString
& caption
,
442 wxArrayDouble
& array
,
450 return wxArrayEditorDialog::Create (parent
,message
,caption
,style
,pos
,sz
);
453 // -----------------------------------------------------------------------
454 // wxArrayDoubleProperty
455 // -----------------------------------------------------------------------
457 #include <math.h> // for fabs
459 // Comparison required by value type implementation.
460 bool operator == (const wxArrayDouble
& a
, const wxArrayDouble
& b
)
462 if ( a
.GetCount() != b
.GetCount() )
467 for ( i
=0; i
<a
.GetCount(); i
++ )
469 // Can't do direct equality comparison with floating point numbers.
470 if ( fabs(a
[i
] - b
[i
]) > 0.0000000001 )
472 //wxLogDebug(wxT("%f != %f"),a[i],b[i]);
479 WX_PG_IMPLEMENT_VARIANT_DATA_DUMMY_EQ(wxArrayDouble
)
481 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayDoubleProperty
,
484 const wxArrayDouble
&,
488 wxArrayDoubleProperty::wxArrayDoubleProperty (const wxString
& label
,
489 const wxString
& name
,
490 const wxArrayDouble
& array
)
491 : wxPGProperty(label
,name
)
496 // Need to figure out delimiter needed for this locale
497 // (ie. can't use comma when comma acts as decimal point in float).
498 wxChar use_delimiter
= wxT(',');
500 if (wxString::Format(wxT("%.2f"),12.34).Find(use_delimiter
) >= 0)
501 use_delimiter
= wxT(';');
503 m_delimiter
= use_delimiter
;
505 SetValue( WXVARIANT(array
) );
508 wxArrayDoubleProperty::~wxArrayDoubleProperty () { }
510 void wxArrayDoubleProperty::OnSetValue()
512 // Generate cached display string, to optimize grid drawing
513 GenerateValueAsString( m_display
, m_precision
, true );
516 wxString
wxArrayDoubleProperty::ValueToString( wxVariant
& value
,
521 if ( argFlags
& wxPG_FULL_VALUE
)
523 GenerateValueAsString(s
,-1,false);
528 // Display cached string only if value truly matches m_value
529 if ( value
.GetData() == m_value
.GetData() )
532 GenerateValueAsString( s
, m_precision
, true );
538 void wxArrayDoubleProperty::GenerateValueAsString( wxString
& target
, int prec
, bool removeZeroes
) const
541 wxString template_str
;
542 wxChar between
[3] = wxT(", ");
545 between
[0] = m_delimiter
;
549 const wxArrayDouble
& value
= wxArrayDoubleRefFromVariant(m_value
);
551 for ( i
=0; i
<value
.GetCount(); i
++ )
554 wxPropertyGrid::DoubleToString(s
,value
[i
],prec
,removeZeroes
,&template_str
);
558 if ( i
<(value
.GetCount()-1) )
563 bool wxArrayDoubleProperty::OnEvent( wxPropertyGrid
* propgrid
,
564 wxWindow
* WXUNUSED(primary
),
567 if ( propgrid
->IsMainButtonEvent(event
) )
569 // Update the value in case of last minute changes
570 wxVariant useValue
= propgrid
->GetUncommittedPropertyValue();
572 wxArrayDouble
& value
= wxArrayDoubleRefFromVariant(useValue
);
574 // Create editor dialog.
575 wxArrayDoubleEditorDialog dlg
;
576 dlg
.SetPrecision(m_precision
);
577 dlg
.Create( propgrid
, wxEmptyString
, m_label
, value
);
578 dlg
.Move( propgrid
->GetGoodEditorDialogPosition(this,dlg
.GetSize()) );
580 // Execute editor dialog
581 int res
= dlg
.ShowModal();
582 if ( res
== wxID_OK
&& dlg
.IsModified() )
584 SetValueInEvent( WXVARIANT(dlg
.GetArray()) );
592 bool wxArrayDoubleProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
596 // Add values to a temporary array so that in case
597 // of error we can opt not to use them.
598 wxArrayDouble new_array
;
602 wxChar delimiter
= m_delimiter
;
604 WX_PG_TOKENIZER1_BEGIN(text
,delimiter
)
606 if ( token
.length() )
609 // If token was invalid, exit the loop now
610 if ( !token
.ToDouble(&tval
) )
612 tstr
.Printf ( _("\"%s\" is not a floating-point number."), token
.c_str() );
616 // TODO: Put validator code here
622 WX_PG_TOKENIZER1_END()
624 // When invalid token found, show error message and don't change anything
631 if ( !(wxArrayDoubleRefFromVariant(m_value
) == new_array
) )
633 variant
= WXVARIANT(new_array
);
640 bool wxArrayDoubleProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
642 if ( name
== wxPG_FLOAT_PRECISION
)
644 m_precision
= value
.GetLong();
645 GenerateValueAsString( m_display
, m_precision
, true );