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 AddChild( 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 // Update value from last minute changes
135 PrepareValueForDialogEditing(propgrid
);
138 fontData
<< m_value_wxFontData
;
140 fontData
.SetInitialFont(fontData
.GetChosenFont());
142 wxFontDialog
dlg(propgrid
, fontData
);
144 if ( dlg
.ShowModal() == wxID_OK
)
147 variant
<< dlg
.GetFontData();
148 SetValueInEvent( variant
);
155 void wxFontDataProperty::RefreshChildren()
157 wxFontProperty::RefreshChildren();
158 if ( GetChildCount() < 6 ) // Number is count of wxFontProperty's children + 1.
160 wxFontData fontData
; fontData
<< m_value_wxFontData
;
161 wxVariant variant
; variant
<< fontData
.GetColour();
162 Item(6)->SetValue( variant
);
165 void wxFontDataProperty::ChildChanged( wxVariant
& thisValue
, int childIndex
, wxVariant
& childValue
) const
168 fontData
<< thisValue
;
172 switch ( childIndex
)
176 fontData
.SetColour( col
);
179 // Transfer from subset to superset.
180 wxFont font
= fontData
.GetChosenFont();
181 variant
= WXVARIANT(font
);
182 wxFontProperty::ChildChanged( variant
, childIndex
, childValue
);
184 fontData
.SetChosenFont(font
);
187 thisValue
<< fontData
;
190 // -----------------------------------------------------------------------
192 // -----------------------------------------------------------------------
194 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxSizeProperty
,wxPGProperty
,
195 wxSize
,const wxSize
&,TextCtrl
)
197 wxSizeProperty::wxSizeProperty( const wxString
& label
, const wxString
& name
,
198 const wxSize
& value
) : wxPGProperty(label
,name
)
201 AddChild( new wxIntProperty(wxT("Width"),wxPG_LABEL
,value
.x
) );
202 AddChild( new wxIntProperty(wxT("Height"),wxPG_LABEL
,value
.y
) );
205 wxSizeProperty::~wxSizeProperty() { }
207 void wxSizeProperty::RefreshChildren()
209 if ( !GetChildCount() ) return;
210 const wxSize
& size
= wxSizeRefFromVariant(m_value
);
211 Item(0)->SetValue( (long)size
.x
);
212 Item(1)->SetValue( (long)size
.y
);
215 void wxSizeProperty::ChildChanged( wxVariant
& thisValue
, int childIndex
, wxVariant
& childValue
) const
217 wxSize
& size
= wxSizeRefFromVariant(thisValue
);
218 int val
= wxPGVariantToInt(childValue
);
219 switch ( childIndex
)
221 case 0: size
.x
= val
; break;
222 case 1: size
.y
= val
; break;
226 // -----------------------------------------------------------------------
228 // -----------------------------------------------------------------------
230 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxPointProperty
,wxPGProperty
,
231 wxPoint
,const wxPoint
&,TextCtrl
)
233 wxPointProperty::wxPointProperty( const wxString
& label
, const wxString
& name
,
234 const wxPoint
& value
) : wxPGProperty(label
,name
)
237 AddChild( new wxIntProperty(wxT("X"),wxPG_LABEL
,value
.x
) );
238 AddChild( new wxIntProperty(wxT("Y"),wxPG_LABEL
,value
.y
) );
241 wxPointProperty::~wxPointProperty() { }
243 void wxPointProperty::RefreshChildren()
245 if ( !GetChildCount() ) return;
246 const wxPoint
& point
= wxPointRefFromVariant(m_value
);
247 Item(0)->SetValue( (long)point
.x
);
248 Item(1)->SetValue( (long)point
.y
);
251 void wxPointProperty::ChildChanged( wxVariant
& thisValue
, int childIndex
, wxVariant
& childValue
) const
253 wxPoint
& point
= wxPointRefFromVariant(thisValue
);
254 int val
= wxPGVariantToInt(childValue
);
255 switch ( childIndex
)
257 case 0: point
.x
= val
; break;
258 case 1: point
.y
= val
; break;
263 // -----------------------------------------------------------------------
265 // -----------------------------------------------------------------------
267 WX_PG_IMPLEMENT_ARRAYSTRING_PROPERTY_WITH_VALIDATOR(wxDirsProperty
,wxT(','),wxT("Browse"))
271 wxValidator
* wxDirsProperty::DoGetValidator() const
273 return wxFileProperty::GetClassValidator();
279 bool wxDirsProperty::OnCustomStringEdit( wxWindow
* parent
, wxString
& value
)
281 wxDirDialog
dlg(parent
,
282 _("Select a directory to be added to the list:"),
286 if ( dlg
.ShowModal() == wxID_OK
)
288 value
= dlg
.GetPath();
294 // -----------------------------------------------------------------------
295 // wxArrayDoubleEditorDialog
296 // -----------------------------------------------------------------------
300 // You can *almost* convert wxArrayDoubleEditorDialog to wxArrayXXXEditorDialog
301 // by replacing each ArrayDouble with ArrayXXX.
304 class wxArrayDoubleEditorDialog
: public wxArrayEditorDialog
307 wxArrayDoubleEditorDialog();
311 wxArrayDoubleEditorDialog(wxWindow
*parent
,
312 const wxString
& message
,
313 const wxString
& caption
,
314 wxArrayDouble
& array
,
315 long style
= wxAEDIALOG_STYLE
,
316 const wxPoint
& pos
= wxDefaultPosition
,
317 const wxSize
& sz
= wxDefaultSize
);
319 bool Create(wxWindow
*parent
,
320 const wxString
& message
,
321 const wxString
& caption
,
322 wxArrayDouble
& array
,
323 long style
= wxAEDIALOG_STYLE
,
324 const wxPoint
& pos
= wxDefaultPosition
,
325 const wxSize
& sz
= wxDefaultSize
);
327 const wxArrayDouble
& GetArray() const { return m_array
; }
329 // Extra method for this type of array
330 void SetPrecision ( int precision
)
332 m_precision
= precision
;
333 m_dtoaTemplate
.Empty();
337 // Mandatory array of type
338 wxArrayDouble m_array
;
340 // Use this to avoid extra wxString creation+Printf
341 // on double-to-wxString conversion.
342 wxString m_dtoaTemplate
;
346 // Mandatory overridden methods
347 virtual wxString
ArrayGet( size_t index
);
348 virtual size_t ArrayGetCount();
349 virtual bool ArrayInsert( const wxString
& str
, int index
);
350 virtual bool ArraySet( size_t index
, const wxString
& str
);
351 virtual void ArrayRemoveAt( int index
);
352 virtual void ArraySwap( size_t first
, size_t second
);
355 DECLARE_DYNAMIC_CLASS_NO_COPY(wxArrayDoubleEditorDialog
)
358 IMPLEMENT_DYNAMIC_CLASS(wxArrayDoubleEditorDialog
, wxArrayEditorDialog
)
361 // Array dialog array access and manipulation
364 wxString
wxArrayDoubleEditorDialog::ArrayGet( size_t index
)
367 wxPropertyGrid::DoubleToString(str
,m_array
[index
],m_precision
,true,&m_dtoaTemplate
);
371 size_t wxArrayDoubleEditorDialog::ArrayGetCount()
373 return m_array
.GetCount();
376 bool wxArrayDoubleEditorDialog::ArrayInsert( const wxString
& str
, int index
)
379 if ( !str
.ToDouble(&d
) )
385 m_array
.Insert(d
,index
);
389 bool wxArrayDoubleEditorDialog::ArraySet( size_t index
, const wxString
& str
)
392 if ( !str
.ToDouble(&d
) )
398 void wxArrayDoubleEditorDialog::ArrayRemoveAt( int index
)
400 m_array
.RemoveAt(index
);
403 void wxArrayDoubleEditorDialog::ArraySwap( size_t first
, size_t second
)
405 double a
= m_array
[first
];
406 double b
= m_array
[second
];
412 // Array dialog construction etc.
415 wxArrayDoubleEditorDialog::wxArrayDoubleEditorDialog()
416 : wxArrayEditorDialog()
421 void wxArrayDoubleEditorDialog::Init()
423 wxArrayEditorDialog::Init();
427 wxArrayDoubleEditorDialog::wxArrayDoubleEditorDialog(wxWindow
*parent
,
428 const wxString
& message
,
429 const wxString
& caption
,
430 wxArrayDouble
& array
,
434 : wxArrayEditorDialog()
437 Create(parent
,message
,caption
,array
,style
,pos
,sz
);
440 bool wxArrayDoubleEditorDialog::Create(wxWindow
*parent
,
441 const wxString
& message
,
442 const wxString
& caption
,
443 wxArrayDouble
& array
,
451 return wxArrayEditorDialog::Create (parent
,message
,caption
,style
,pos
,sz
);
454 // -----------------------------------------------------------------------
455 // wxArrayDoubleProperty
456 // -----------------------------------------------------------------------
458 #include <math.h> // for fabs
460 // Comparison required by value type implementation.
461 bool operator == (const wxArrayDouble
& a
, const wxArrayDouble
& b
)
463 if ( a
.GetCount() != b
.GetCount() )
468 for ( i
=0; i
<a
.GetCount(); i
++ )
470 // Can't do direct equality comparison with floating point numbers.
471 if ( fabs(a
[i
] - b
[i
]) > 0.0000000001 )
473 //wxLogDebug(wxT("%f != %f"),a[i],b[i]);
480 WX_PG_IMPLEMENT_VARIANT_DATA_DUMMY_EQ(wxArrayDouble
)
482 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayDoubleProperty
,
485 const wxArrayDouble
&,
489 wxArrayDoubleProperty::wxArrayDoubleProperty (const wxString
& label
,
490 const wxString
& name
,
491 const wxArrayDouble
& array
)
492 : wxPGProperty(label
,name
)
497 // Need to figure out delimiter needed for this locale
498 // (ie. can't use comma when comma acts as decimal point in float).
499 wxChar use_delimiter
= wxT(',');
501 if (wxString::Format(wxT("%.2f"),12.34).Find(use_delimiter
) >= 0)
502 use_delimiter
= wxT(';');
504 m_delimiter
= use_delimiter
;
506 SetValue( WXVARIANT(array
) );
509 wxArrayDoubleProperty::~wxArrayDoubleProperty () { }
511 void wxArrayDoubleProperty::OnSetValue()
513 GenerateValueAsString( m_display
, m_precision
, true );
516 wxString
wxArrayDoubleProperty::GetValueAsString( int arg_flags
) const
518 if ( !(arg_flags
& wxPG_FULL_VALUE
))
522 GenerateValueAsString(s
,-1,false);
526 void wxArrayDoubleProperty::GenerateValueAsString( wxString
& target
, int prec
, bool removeZeroes
) const
529 wxString template_str
;
530 wxChar between
[3] = wxT(", ");
533 between
[0] = m_delimiter
;
537 const wxArrayDouble
& value
= wxArrayDoubleRefFromVariant(m_value
);
539 for ( i
=0; i
<value
.GetCount(); i
++ )
542 wxPropertyGrid::DoubleToString(s
,value
[i
],prec
,removeZeroes
,&template_str
);
546 if ( i
<(value
.GetCount()-1) )
551 bool wxArrayDoubleProperty::OnEvent( wxPropertyGrid
* propgrid
,
552 wxWindow
* WXUNUSED(primary
),
555 if ( propgrid
->IsMainButtonEvent(event
) )
557 wxArrayDouble
& value
= wxArrayDoubleRefFromVariant(m_value
);
559 // Update the value in case of last minute changes
560 PrepareValueForDialogEditing(propgrid
);
562 // Create editor dialog.
563 wxArrayDoubleEditorDialog dlg
;
564 dlg
.SetPrecision(m_precision
);
565 dlg
.Create( propgrid
, wxEmptyString
, m_label
, value
);
566 dlg
.Move( propgrid
->GetGoodEditorDialogPosition(this,dlg
.GetSize()) );
568 // Execute editor dialog
569 int res
= dlg
.ShowModal();
570 if ( res
== wxID_OK
&& dlg
.IsModified() )
572 SetValueInEvent( WXVARIANT(dlg
.GetArray()) );
580 bool wxArrayDoubleProperty::StringToValue( wxVariant
& variant
, const wxString
& text
, int ) const
584 // Add values to a temporary array so that in case
585 // of error we can opt not to use them.
586 wxArrayDouble new_array
;
590 wxChar delimiter
= m_delimiter
;
592 WX_PG_TOKENIZER1_BEGIN(text
,delimiter
)
594 if ( token
.length() )
597 // If token was invalid, exit the loop now
598 if ( !token
.ToDouble(&tval
) )
600 tstr
.Printf ( _("\"%s\" is not a floating-point number."), token
.c_str() );
604 // TODO: Put validator code here
610 WX_PG_TOKENIZER1_END()
612 // When invalid token found, show error message and don't change anything
619 if ( !(wxArrayDoubleRefFromVariant(m_value
) == new_array
) )
621 variant
= WXVARIANT(new_array
);
628 bool wxArrayDoubleProperty::DoSetAttribute( const wxString
& name
, wxVariant
& value
)
630 if ( name
== wxPG_FLOAT_PRECISION
)
632 m_precision
= value
.GetLong();
633 GenerateValueAsString( m_display
, m_precision
, true );