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