GetPendingEditedValue() -> GetUncommittedPropertyValue() (more consistent API naming...
[wxWidgets.git] / samples / propgrid / sampleprops.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/propgrid/sampleprops.cpp
3 // Purpose: wxPropertyGrid Sample Properties
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: 2006-03-05
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 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all "standard" wxWidgets headers)
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
25 // -----------------------------------------------------------------------
26
27
28 #include <wx/propgrid/propgrid.h>
29 #include <wx/propgrid/advprops.h>
30
31 #ifndef WX_PROPGRID_SAMPLEPROPS_H
32 #include "sampleprops.h"
33 #endif
34
35
36 // -----------------------------------------------------------------------
37 // wxFontDataProperty
38 // -----------------------------------------------------------------------
39
40 // Dummy comparison required by value type implementation.
41 bool operator == (const wxFontData&, const wxFontData&)
42 {
43 return FALSE;
44 }
45
46 // Custom version of wxFontProperty that also holds colour in the value.
47 // Original version by Vladimir Vainer.
48
49 #include <wx/fontdlg.h>
50
51 IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(wxFontData)
52
53 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFontDataProperty,wxFontProperty,
54 wxFontData,const wxFontData&,TextCtrlAndButton)
55
56 wxFontDataProperty::wxFontDataProperty( const wxString& label, const wxString& name,
57 const wxFontData& value ) : wxFontProperty(label,name,value.GetInitialFont())
58 {
59 wxFontData fontData(value);
60
61 // Fix value.
62 fontData.SetChosenFont(value.GetInitialFont());
63 if ( !fontData.GetColour().Ok() )
64 fontData.SetColour(*wxBLACK);
65
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;
69
70 // Add extra children.
71 AddChild( new wxColourProperty(_("Colour"), wxPG_LABEL,
72 fontData.GetColour() ) );
73 }
74
75 wxFontDataProperty::~wxFontDataProperty () { }
76
77 void wxFontDataProperty::OnSetValue()
78 {
79 if ( m_value.GetType() != "wxFontData" )
80 {
81 if ( m_value.GetType() == "wxFont" )
82 {
83 wxFont font;
84 font << m_value;
85 wxFontData fontData;
86 fontData.SetChosenFont(font);
87 if ( !m_value_wxFontData.IsNull() )
88 {
89 wxFontData oldFontData;
90 oldFontData << m_value_wxFontData;
91 fontData.SetColour(oldFontData.GetColour());
92 }
93 else
94 {
95 fontData.SetColour(*wxBLACK);
96 }
97 wxVariant variant;
98 variant << fontData;
99 m_value_wxFontData = variant;
100 }
101 else
102 {
103 wxFAIL_MSG(wxT("Value to wxFontDataProperty must be eithe wxFontData or wxFont"));
104 }
105 }
106 else
107 {
108 // Set m_value to wxFont so that wxFontProperty methods will work
109 // correctly.
110 m_value_wxFontData = m_value;
111
112 wxFontData fontData;
113 fontData << m_value_wxFontData;
114
115 wxFont font = fontData.GetChosenFont();
116 if ( !font.Ok() )
117 font = wxFont(10,wxSWISS,wxNORMAL,wxNORMAL);
118
119 m_value = WXVARIANT(font);
120 }
121 }
122
123 wxVariant wxFontDataProperty::DoGetValue() const
124 {
125 return m_value_wxFontData;
126 }
127
128 // Must re-create font dialog displayer.
129 bool wxFontDataProperty::OnEvent( wxPropertyGrid* propgrid,
130 wxWindow* WXUNUSED(primary), wxEvent& event )
131 {
132 if ( propgrid->IsMainButtonEvent(event) )
133 {
134 wxVariant useValue = propgrid->GetUncommittedPropertyValue();
135
136 wxFontData fontData;
137 fontData << useValue;
138
139 fontData.SetInitialFont(fontData.GetChosenFont());
140
141 wxFontDialog dlg(propgrid, fontData);
142
143 if ( dlg.ShowModal() == wxID_OK )
144 {
145 wxVariant variant;
146 variant << dlg.GetFontData();
147 SetValueInEvent( variant );
148 return true;
149 }
150 }
151 return false;
152 }
153
154 void wxFontDataProperty::RefreshChildren()
155 {
156 wxFontProperty::RefreshChildren();
157 if ( GetChildCount() < 6 ) // Number is count of wxFontProperty's children + 1.
158 return;
159 wxFontData fontData; fontData << m_value_wxFontData;
160 wxVariant variant; variant << fontData.GetColour();
161 Item(6)->SetValue( variant );
162 }
163
164 void wxFontDataProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const
165 {
166 wxFontData fontData;
167 fontData << thisValue;
168 wxColour col;
169 wxVariant variant;
170
171 switch ( childIndex )
172 {
173 case 6:
174 col << childValue;
175 fontData.SetColour( col );
176 break;
177 default:
178 // Transfer from subset to superset.
179 wxFont font = fontData.GetChosenFont();
180 variant = WXVARIANT(font);
181 wxFontProperty::ChildChanged( variant, childIndex, childValue );
182 font << variant;
183 fontData.SetChosenFont(font);
184 }
185
186 thisValue << fontData;
187 }
188
189 // -----------------------------------------------------------------------
190 // wxSizeProperty
191 // -----------------------------------------------------------------------
192
193 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxSizeProperty,wxPGProperty,
194 wxSize,const wxSize&,TextCtrl)
195
196 wxSizeProperty::wxSizeProperty( const wxString& label, const wxString& name,
197 const wxSize& value) : wxPGProperty(label,name)
198 {
199 SetValueI(value);
200 AddChild( new wxIntProperty(wxT("Width"),wxPG_LABEL,value.x) );
201 AddChild( new wxIntProperty(wxT("Height"),wxPG_LABEL,value.y) );
202 }
203
204 wxSizeProperty::~wxSizeProperty() { }
205
206 void wxSizeProperty::RefreshChildren()
207 {
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 );
212 }
213
214 void wxSizeProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const
215 {
216 wxSize& size = wxSizeRefFromVariant(thisValue);
217 int val = wxPGVariantToInt(childValue);
218 switch ( childIndex )
219 {
220 case 0: size.x = val; break;
221 case 1: size.y = val; break;
222 }
223 }
224
225 // -----------------------------------------------------------------------
226 // wxPointProperty
227 // -----------------------------------------------------------------------
228
229 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxPointProperty,wxPGProperty,
230 wxPoint,const wxPoint&,TextCtrl)
231
232 wxPointProperty::wxPointProperty( const wxString& label, const wxString& name,
233 const wxPoint& value) : wxPGProperty(label,name)
234 {
235 SetValueI(value);
236 AddChild( new wxIntProperty(wxT("X"),wxPG_LABEL,value.x) );
237 AddChild( new wxIntProperty(wxT("Y"),wxPG_LABEL,value.y) );
238 }
239
240 wxPointProperty::~wxPointProperty() { }
241
242 void wxPointProperty::RefreshChildren()
243 {
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 );
248 }
249
250 void wxPointProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const
251 {
252 wxPoint& point = wxPointRefFromVariant(thisValue);
253 int val = wxPGVariantToInt(childValue);
254 switch ( childIndex )
255 {
256 case 0: point.x = val; break;
257 case 1: point.y = val; break;
258 }
259 }
260
261
262 // -----------------------------------------------------------------------
263 // Dirs Property
264 // -----------------------------------------------------------------------
265
266 WX_PG_IMPLEMENT_ARRAYSTRING_PROPERTY_WITH_VALIDATOR(wxDirsProperty,wxT(','),wxT("Browse"))
267
268 #if wxUSE_VALIDATORS
269
270 wxValidator* wxDirsProperty::DoGetValidator() const
271 {
272 return wxFileProperty::GetClassValidator();
273 }
274
275 #endif
276
277
278 bool wxDirsProperty::OnCustomStringEdit( wxWindow* parent, wxString& value )
279 {
280 wxDirDialog dlg(parent,
281 _("Select a directory to be added to the list:"),
282 value,
283 0);
284
285 if ( dlg.ShowModal() == wxID_OK )
286 {
287 value = dlg.GetPath();
288 return TRUE;
289 }
290 return FALSE;
291 }
292
293 // -----------------------------------------------------------------------
294 // wxArrayDoubleEditorDialog
295 // -----------------------------------------------------------------------
296
297
298 //
299 // You can *almost* convert wxArrayDoubleEditorDialog to wxArrayXXXEditorDialog
300 // by replacing each ArrayDouble with ArrayXXX.
301 //
302
303 class wxArrayDoubleEditorDialog : public wxArrayEditorDialog
304 {
305 public:
306 wxArrayDoubleEditorDialog();
307
308 void Init();
309
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 );
317
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 );
325
326 const wxArrayDouble& GetArray() const { return m_array; }
327
328 // Extra method for this type of array
329 void SetPrecision ( int precision )
330 {
331 m_precision = precision;
332 m_dtoaTemplate.Empty();
333 }
334
335 protected:
336 // Mandatory array of type
337 wxArrayDouble m_array;
338
339 // Use this to avoid extra wxString creation+Printf
340 // on double-to-wxString conversion.
341 wxString m_dtoaTemplate;
342
343 int m_precision;
344
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 );
352
353 private:
354 DECLARE_DYNAMIC_CLASS_NO_COPY(wxArrayDoubleEditorDialog)
355 };
356
357 IMPLEMENT_DYNAMIC_CLASS(wxArrayDoubleEditorDialog, wxArrayEditorDialog)
358
359 //
360 // Array dialog array access and manipulation
361 //
362
363 wxString wxArrayDoubleEditorDialog::ArrayGet( size_t index )
364 {
365 wxString str;
366 wxPropertyGrid::DoubleToString(str,m_array[index],m_precision,true,&m_dtoaTemplate);
367 return str;
368 }
369
370 size_t wxArrayDoubleEditorDialog::ArrayGetCount()
371 {
372 return m_array.GetCount();
373 }
374
375 bool wxArrayDoubleEditorDialog::ArrayInsert( const wxString& str, int index )
376 {
377 double d;
378 if ( !str.ToDouble(&d) )
379 return FALSE;
380
381 if (index<0)
382 m_array.Add(d);
383 else
384 m_array.Insert(d,index);
385 return TRUE;
386 }
387
388 bool wxArrayDoubleEditorDialog::ArraySet( size_t index, const wxString& str )
389 {
390 double d;
391 if ( !str.ToDouble(&d) )
392 return FALSE;
393 m_array[index] = d;
394 return TRUE;
395 }
396
397 void wxArrayDoubleEditorDialog::ArrayRemoveAt( int index )
398 {
399 m_array.RemoveAt(index);
400 }
401
402 void wxArrayDoubleEditorDialog::ArraySwap( size_t first, size_t second )
403 {
404 double a = m_array[first];
405 double b = m_array[second];
406 m_array[first] = b;
407 m_array[second] = a;
408 }
409
410 //
411 // Array dialog construction etc.
412 //
413
414 wxArrayDoubleEditorDialog::wxArrayDoubleEditorDialog()
415 : wxArrayEditorDialog()
416 {
417 Init();
418 }
419
420 void wxArrayDoubleEditorDialog::Init()
421 {
422 wxArrayEditorDialog::Init();
423 SetPrecision(-1);
424 }
425
426 wxArrayDoubleEditorDialog::wxArrayDoubleEditorDialog(wxWindow *parent,
427 const wxString& message,
428 const wxString& caption,
429 wxArrayDouble& array,
430 long style,
431 const wxPoint& pos,
432 const wxSize& sz )
433 : wxArrayEditorDialog()
434 {
435 Init();
436 Create(parent,message,caption,array,style,pos,sz);
437 }
438
439 bool wxArrayDoubleEditorDialog::Create(wxWindow *parent,
440 const wxString& message,
441 const wxString& caption,
442 wxArrayDouble& array,
443 long style,
444 const wxPoint& pos,
445 const wxSize& sz )
446 {
447
448 m_array = array;
449
450 return wxArrayEditorDialog::Create (parent,message,caption,style,pos,sz);
451 }
452
453 // -----------------------------------------------------------------------
454 // wxArrayDoubleProperty
455 // -----------------------------------------------------------------------
456
457 #include <math.h> // for fabs
458
459 // Comparison required by value type implementation.
460 bool operator == (const wxArrayDouble& a, const wxArrayDouble& b)
461 {
462 if ( a.GetCount() != b.GetCount() )
463 return FALSE;
464
465 size_t i;
466
467 for ( i=0; i<a.GetCount(); i++ )
468 {
469 // Can't do direct equality comparison with floating point numbers.
470 if ( fabs(a[i] - b[i]) > 0.0000000001 )
471 {
472 //wxLogDebug(wxT("%f != %f"),a[i],b[i]);
473 return FALSE;
474 }
475 }
476 return TRUE;
477 }
478
479 WX_PG_IMPLEMENT_VARIANT_DATA_DUMMY_EQ(wxArrayDouble)
480
481 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayDoubleProperty,
482 wxPGProperty,
483 wxArrayDouble,
484 const wxArrayDouble&,
485 TextCtrlAndButton)
486
487
488 wxArrayDoubleProperty::wxArrayDoubleProperty (const wxString& label,
489 const wxString& name,
490 const wxArrayDouble& array )
491 : wxPGProperty(label,name)
492 {
493 m_precision = -1;
494
495 //
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(',');
499
500 if (wxString::Format(wxT("%.2f"),12.34).Find(use_delimiter) >= 0)
501 use_delimiter = wxT(';');
502
503 m_delimiter = use_delimiter;
504
505 SetValue( WXVARIANT(array) );
506 }
507
508 wxArrayDoubleProperty::~wxArrayDoubleProperty () { }
509
510 void wxArrayDoubleProperty::OnSetValue()
511 {
512 GenerateValueAsString( m_display, m_precision, true );
513 }
514
515 wxString wxArrayDoubleProperty::GetValueAsString( int arg_flags ) const
516 {
517 if ( !(arg_flags & wxPG_FULL_VALUE ))
518 return m_display;
519
520 wxString s;
521 GenerateValueAsString(s,-1,false);
522 return s;
523 }
524
525 void wxArrayDoubleProperty::GenerateValueAsString( wxString& target, int prec, bool removeZeroes ) const
526 {
527 wxString s;
528 wxString template_str;
529 wxChar between[3] = wxT(", ");
530 size_t i;
531
532 between[0] = m_delimiter;
533
534 target.Empty();
535
536 const wxArrayDouble& value = wxArrayDoubleRefFromVariant(m_value);
537
538 for ( i=0; i<value.GetCount(); i++ )
539 {
540
541 wxPropertyGrid::DoubleToString(s,value[i],prec,removeZeroes,&template_str);
542
543 target += s;
544
545 if ( i<(value.GetCount()-1) )
546 target += between;
547 }
548 }
549
550 bool wxArrayDoubleProperty::OnEvent( wxPropertyGrid* propgrid,
551 wxWindow* WXUNUSED(primary),
552 wxEvent& event)
553 {
554 if ( propgrid->IsMainButtonEvent(event) )
555 {
556 // Update the value in case of last minute changes
557 wxVariant useValue = propgrid->GetUncommittedPropertyValue();
558
559 wxArrayDouble& value = wxArrayDoubleRefFromVariant(useValue);
560
561 // Create editor dialog.
562 wxArrayDoubleEditorDialog dlg;
563 dlg.SetPrecision(m_precision);
564 dlg.Create( propgrid, wxEmptyString, m_label, value );
565 dlg.Move( propgrid->GetGoodEditorDialogPosition(this,dlg.GetSize()) );
566
567 // Execute editor dialog
568 int res = dlg.ShowModal();
569 if ( res == wxID_OK && dlg.IsModified() )
570 {
571 SetValueInEvent( WXVARIANT(dlg.GetArray()) );
572 return true;
573 }
574 return false;
575 }
576 return false;
577 }
578
579 bool wxArrayDoubleProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const
580 {
581 double tval;
582 wxString tstr;
583 // Add values to a temporary array so that in case
584 // of error we can opt not to use them.
585 wxArrayDouble new_array;
586
587 bool ok = true;
588
589 wxChar delimiter = m_delimiter;
590
591 WX_PG_TOKENIZER1_BEGIN(text,delimiter)
592
593 if ( token.length() )
594 {
595
596 // If token was invalid, exit the loop now
597 if ( !token.ToDouble(&tval) )
598 {
599 tstr.Printf ( _("\"%s\" is not a floating-point number."), token.c_str() );
600 ok = false;
601 break;
602 }
603 // TODO: Put validator code here
604
605 new_array.Add(tval);
606
607 }
608
609 WX_PG_TOKENIZER1_END()
610
611 // When invalid token found, show error message and don't change anything
612 if ( !ok )
613 {
614 //ShowError( tstr );
615 return false;
616 }
617
618 if ( !(wxArrayDoubleRefFromVariant(m_value) == new_array) )
619 {
620 variant = WXVARIANT(new_array);
621 return true;
622 }
623
624 return false;
625 }
626
627 bool wxArrayDoubleProperty::DoSetAttribute( const wxString& name, wxVariant& value )
628 {
629 if ( name == wxPG_FLOAT_PRECISION )
630 {
631 m_precision = value.GetLong();
632 GenerateValueAsString( m_display, m_precision, true );
633 return true;
634 }
635 return false;
636 }
637