]> git.saurik.com Git - wxWidgets.git/blob - src/propgrid/props.cpp
correct typo: s/wxUSE_MENU/&S/
[wxWidgets.git] / src / propgrid / props.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/props.cpp
3 // Purpose: Basic Property Classes
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: 2005-05-14
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/dcclient.h"
32 #include "wx/dcmemory.h"
33 #include "wx/button.h"
34 #include "wx/pen.h"
35 #include "wx/brush.h"
36 #include "wx/cursor.h"
37 #include "wx/dialog.h"
38 #include "wx/settings.h"
39 #include "wx/msgdlg.h"
40 #include "wx/choice.h"
41 #include "wx/stattext.h"
42 #include "wx/scrolwin.h"
43 #include "wx/dirdlg.h"
44 #include "wx/combobox.h"
45 #include "wx/layout.h"
46 #include "wx/sizer.h"
47 #include "wx/textdlg.h"
48 #include "wx/filedlg.h"
49 #include "wx/intl.h"
50 #endif
51
52 #include "wx/filename.h"
53
54 #include "wx/propgrid/propgrid.h"
55
56 #define wxPG_CUSTOM_IMAGE_WIDTH 20 // for wxColourProperty etc.
57
58
59 // -----------------------------------------------------------------------
60 // wxStringProperty
61 // -----------------------------------------------------------------------
62
63 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxStringProperty,wxPGProperty,
64 wxString,const wxString&,TextCtrl)
65
66 wxStringProperty::wxStringProperty( const wxString& label,
67 const wxString& name,
68 const wxString& value )
69 : wxPGProperty(label,name)
70 {
71 SetValue(value);
72 }
73
74 void wxStringProperty::OnSetValue()
75 {
76 if ( !m_value.IsNull() && m_value.GetString() == wxS("<composed>") )
77 SetFlag(wxPG_PROP_COMPOSED_VALUE);
78
79 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE) )
80 {
81 wxString s;
82 DoGenerateComposedValue(s);
83 m_value = s;
84 }
85 }
86
87 wxStringProperty::~wxStringProperty() { }
88
89 wxString wxStringProperty::ValueToString( wxVariant& value,
90 int argFlags ) const
91 {
92 wxString s = value.GetString();
93
94 if ( GetChildCount() && HasFlag(wxPG_PROP_COMPOSED_VALUE) )
95 {
96 // Value stored in m_value is non-editable, non-full value
97 if ( (argFlags & wxPG_FULL_VALUE) || (argFlags & wxPG_EDITABLE_VALUE) )
98 {
99 // Calling this under incorrect conditions will fail
100 wxASSERT_MSG( argFlags & wxPG_VALUE_IS_CURRENT,
101 "Sorry, currently default wxPGProperty::ValueToString() "
102 "implementation only works if value is m_value." );
103
104 DoGenerateComposedValue(s, argFlags);
105 }
106
107 return s;
108 }
109
110 // If string is password and value is for visual purposes,
111 // then return asterisks instead the actual string.
112 if ( (m_flags & wxPG_PROP_PASSWORD) && !(argFlags & (wxPG_FULL_VALUE|wxPG_EDITABLE_VALUE)) )
113 return wxString(wxChar('*'), s.Length());
114
115 return s;
116 }
117
118 bool wxStringProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
119 {
120 if ( GetChildCount() && HasFlag(wxPG_PROP_COMPOSED_VALUE) )
121 return wxPGProperty::StringToValue(variant, text, argFlags);
122
123 if ( variant != text )
124 {
125 variant = text;
126 return true;
127 }
128
129 return false;
130 }
131
132 bool wxStringProperty::DoSetAttribute( const wxString& name, wxVariant& value )
133 {
134 if ( name == wxPG_STRING_PASSWORD )
135 {
136 m_flags &= ~(wxPG_PROP_PASSWORD);
137 if ( wxPGVariantToInt(value) ) m_flags |= wxPG_PROP_PASSWORD;
138 RecreateEditor();
139 return false;
140 }
141 return true;
142 }
143
144 // -----------------------------------------------------------------------
145 // wxIntProperty
146 // -----------------------------------------------------------------------
147
148 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxIntProperty,wxPGProperty,
149 long,long,TextCtrl)
150
151 wxIntProperty::wxIntProperty( const wxString& label, const wxString& name,
152 long value ) : wxPGProperty(label,name)
153 {
154 SetValue(value);
155 }
156
157 wxIntProperty::wxIntProperty( const wxString& label, const wxString& name,
158 const wxLongLong& value ) : wxPGProperty(label,name)
159 {
160 SetValue(WXVARIANT(value));
161 }
162
163 wxIntProperty::~wxIntProperty() { }
164
165 wxString wxIntProperty::ValueToString( wxVariant& value,
166 int WXUNUSED(argFlags) ) const
167 {
168 if ( value.GetType() == wxPG_VARIANT_TYPE_LONG )
169 {
170 return wxString::Format(wxS("%li"),value.GetLong());
171 }
172 else if ( value.GetType() == wxLongLong_VariantType )
173 {
174 wxLongLong ll;
175 ll << value;
176 return ll.ToString();
177 }
178
179 return wxEmptyString;
180 }
181
182 bool wxIntProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
183 {
184 wxString s;
185 long value32;
186
187 if ( text.length() == 0 )
188 {
189 variant.MakeNull();
190 return true;
191 }
192
193 // We know it is a number, but let's still check
194 // the return value.
195 if ( text.IsNumber() )
196 {
197 // Remove leading zeroes, so that the number is not interpreted as octal
198 wxString::const_iterator i = text.begin();
199 wxString::const_iterator iMax = text.end() - 1; // Let's allow one, last zero though
200
201 int firstNonZeroPos = 0;
202
203 for ( ; i != iMax; ++i )
204 {
205 wxChar c = *i;
206 if ( c != wxS('0') && c != wxS(' ') )
207 break;
208 firstNonZeroPos++;
209 }
210
211 wxString useText = text.substr(firstNonZeroPos, text.length() - firstNonZeroPos);
212
213 wxString variantType = variant.GetType();
214 bool isPrevLong = variantType == wxPG_VARIANT_TYPE_LONG;
215
216 wxLongLong_t value64 = 0;
217
218 if ( useText.ToLongLong(&value64, 10) &&
219 ( value64 >= INT_MAX || value64 <= INT_MIN )
220 )
221 {
222 bool doChangeValue = isPrevLong;
223
224 if ( !isPrevLong && variantType == wxLongLong_VariantType )
225 {
226 wxLongLong oldValue;
227 oldValue << variant;
228 if ( oldValue.GetValue() != value64 )
229 doChangeValue = true;
230 }
231
232 if ( doChangeValue )
233 {
234 wxLongLong ll(value64);
235 variant << ll;
236 return true;
237 }
238 }
239
240 if ( useText.ToLong( &value32, 0 ) )
241 {
242 if ( !isPrevLong || variant != value32 )
243 {
244 variant = value32;
245 return true;
246 }
247 }
248 }
249 else if ( argFlags & wxPG_REPORT_ERROR )
250 {
251 }
252 return false;
253 }
254
255 bool wxIntProperty::IntToValue( wxVariant& variant, int value, int WXUNUSED(argFlags) ) const
256 {
257 if ( variant.GetType() != wxPG_VARIANT_TYPE_LONG || variant != (long)value )
258 {
259 variant = (long)value;
260 return true;
261 }
262 return false;
263 }
264
265 bool wxIntProperty::DoValidation( const wxPGProperty* property, wxLongLong_t& value, wxPGValidationInfo* pValidationInfo, int mode )
266 {
267 // Check for min/max
268 wxLongLong_t min = wxINT64_MIN;
269 wxLongLong_t max = wxINT64_MAX;
270 wxVariant variant;
271 bool minOk = false;
272 bool maxOk = false;
273
274 variant = property->GetAttribute(wxPGGlobalVars->m_strMin);
275 if ( !variant.IsNull() )
276 {
277 wxPGVariantToLongLong(variant, &min);
278 minOk = true;
279 }
280
281 variant = property->GetAttribute(wxPGGlobalVars->m_strMax);
282 if ( !variant.IsNull() )
283 {
284 wxPGVariantToLongLong(variant, &max);
285 maxOk = true;
286 }
287
288 if ( minOk )
289 {
290 if ( value < min )
291 {
292 if ( mode == wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE )
293 pValidationInfo->SetFailureMessage(
294 wxString::Format(_("Value must be %lld or higher"),min)
295 );
296 else if ( mode == wxPG_PROPERTY_VALIDATION_SATURATE )
297 value = min;
298 else
299 value = max - (min - value);
300 return false;
301 }
302 }
303
304 if ( maxOk )
305 {
306 if ( value > max )
307 {
308 if ( mode == wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE )
309 pValidationInfo->SetFailureMessage(
310 wxString::Format(_("Value must be %lld or higher"),min)
311 );
312 else if ( mode == wxPG_PROPERTY_VALIDATION_SATURATE )
313 value = max;
314 else
315 value = min + (value - max);
316 return false;
317 }
318 }
319 return true;
320 }
321
322 bool wxIntProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& validationInfo ) const
323 {
324 wxLongLong_t ll;
325 if ( wxPGVariantToLongLong(value, &ll) )
326 return DoValidation(this, ll, &validationInfo, wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE);
327 return true;
328 }
329
330 wxValidator* wxIntProperty::GetClassValidator()
331 {
332 #if wxUSE_VALIDATORS
333 WX_PG_DOGETVALIDATOR_ENTRY()
334
335 // Atleast wxPython 2.6.2.1 required that the string argument is given
336 static wxString v;
337 wxTextValidator* validator = new wxTextValidator(wxFILTER_NUMERIC,&v);
338
339 WX_PG_DOGETVALIDATOR_EXIT(validator)
340 #else
341 return NULL;
342 #endif
343 }
344
345 wxValidator* wxIntProperty::DoGetValidator() const
346 {
347 return GetClassValidator();
348 }
349
350 // -----------------------------------------------------------------------
351 // wxUIntProperty
352 // -----------------------------------------------------------------------
353
354
355 #define wxPG_UINT_TEMPLATE_MAX 8
356
357 static const wxChar* gs_uintTemplates32[wxPG_UINT_TEMPLATE_MAX] = {
358 wxT("%x"),wxT("0x%x"),wxT("$%x"),
359 wxT("%X"),wxT("0x%X"),wxT("$%X"),
360 wxT("%u"),wxT("%o")
361 };
362
363 static const wxChar* gs_uintTemplates64[wxPG_UINT_TEMPLATE_MAX] = {
364 wxT("%") wxLongLongFmtSpec wxT("x"),
365 wxT("0x%") wxLongLongFmtSpec wxT("x"),
366 wxT("$%") wxLongLongFmtSpec wxT("x"),
367 wxT("%") wxLongLongFmtSpec wxT("X"),
368 wxT("0x%") wxLongLongFmtSpec wxT("X"),
369 wxT("$%") wxLongLongFmtSpec wxT("X"),
370 wxT("%") wxLongLongFmtSpec wxT("u"),
371 wxT("%") wxLongLongFmtSpec wxT("o")
372 };
373
374 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxUIntProperty,wxPGProperty,
375 long,unsigned long,TextCtrl)
376
377 void wxUIntProperty::Init()
378 {
379 m_base = 6; // This is magic number for dec base (must be same as in setattribute)
380 m_realBase = 10;
381 m_prefix = wxPG_PREFIX_NONE;
382 }
383
384 wxUIntProperty::wxUIntProperty( const wxString& label, const wxString& name,
385 unsigned long value ) : wxPGProperty(label,name)
386 {
387 Init();
388 SetValue((long)value);
389 }
390
391 wxUIntProperty::wxUIntProperty( const wxString& label, const wxString& name,
392 const wxULongLong& value ) : wxPGProperty(label,name)
393 {
394 Init();
395 SetValue(WXVARIANT(value));
396 }
397
398 wxUIntProperty::~wxUIntProperty() { }
399
400 wxString wxUIntProperty::ValueToString( wxVariant& value,
401 int WXUNUSED(argFlags) ) const
402 {
403 size_t index = m_base + m_prefix;
404 if ( index >= wxPG_UINT_TEMPLATE_MAX )
405 index = wxPG_BASE_DEC;
406
407 if ( value.GetType() == wxPG_VARIANT_TYPE_LONG )
408 {
409 return wxString::Format(gs_uintTemplates32[index], (unsigned long)value.GetLong());
410 }
411
412 wxULongLong ull;
413 ull << value;
414
415 return wxString::Format(gs_uintTemplates64[index], ull.GetValue());
416 }
417
418 bool wxUIntProperty::StringToValue( wxVariant& variant, const wxString& text, int WXUNUSED(argFlags) ) const
419 {
420 wxString variantType = variant.GetType();
421 bool isPrevLong = variantType == wxPG_VARIANT_TYPE_LONG;
422
423 if ( text.length() == 0 )
424 {
425 variant.MakeNull();
426 return true;
427 }
428
429 size_t start = 0;
430 if ( text[0] == wxS('$') )
431 start++;
432
433 wxULongLong_t value64 = 0;
434 wxString s = text.substr(start, text.length() - start);
435
436 if ( s.ToULongLong(&value64, (unsigned int)m_realBase) )
437 {
438 if ( value64 >= LONG_MAX )
439 {
440 bool doChangeValue = isPrevLong;
441
442 if ( !isPrevLong && variantType == wxULongLong_VariantType )
443 {
444 wxULongLong oldValue;
445 oldValue << variant;
446 if ( oldValue.GetValue() != value64 )
447 doChangeValue = true;
448 }
449
450 if ( doChangeValue )
451 {
452 wxULongLong ull(value64);
453 variant << ull;
454 return true;
455 }
456 }
457 else
458 {
459 unsigned long value32 = wxLongLong(value64).GetLo();
460 if ( !isPrevLong || m_value != (long)value32 )
461 {
462 variant = (long)value32;
463 return true;
464 }
465 }
466
467 }
468 return false;
469 }
470
471 bool wxUIntProperty::IntToValue( wxVariant& variant, int number, int WXUNUSED(argFlags) ) const
472 {
473 if ( variant != (long)number )
474 {
475 variant = (long)number;
476 return true;
477 }
478 return false;
479 }
480
481 #ifdef ULLONG_MAX
482 #define wxUINT64_MAX ULLONG_MAX
483 #define wxUINT64_MIN wxULL(0)
484 #else
485 #define wxUINT64_MAX wxULL(0xFFFFFFFFFFFFFFFF)
486 #define wxUINT64_MIN wxULL(0)
487 #endif
488
489 bool wxUIntProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& validationInfo ) const
490 {
491 // Check for min/max
492 wxULongLong_t ll;
493 if ( wxPGVariantToULongLong(value, &ll) )
494 {
495 wxULongLong_t min = wxUINT64_MIN;
496 wxULongLong_t max = wxUINT64_MAX;
497 wxVariant variant;
498
499 variant = GetAttribute(wxPGGlobalVars->m_strMin);
500 if ( !variant.IsNull() )
501 {
502 wxPGVariantToULongLong(variant, &min);
503 if ( ll < min )
504 {
505 validationInfo.SetFailureMessage(
506 wxString::Format(_("Value must be %llu or higher"),min)
507 );
508 return false;
509 }
510 }
511 variant = GetAttribute(wxPGGlobalVars->m_strMax);
512 if ( !variant.IsNull() )
513 {
514 wxPGVariantToULongLong(variant, &max);
515 if ( ll > max )
516 {
517 validationInfo.SetFailureMessage(
518 wxString::Format(_("Value must be %llu or less"),max)
519 );
520 return false;
521 }
522 }
523 }
524 return true;
525 }
526
527 bool wxUIntProperty::DoSetAttribute( const wxString& name, wxVariant& value )
528 {
529 if ( name == wxPG_UINT_BASE )
530 {
531 int val = value.GetLong();
532
533 m_realBase = (wxByte) val;
534 if ( m_realBase > 16 )
535 m_realBase = 16;
536
537 //
538 // Translate logical base to a template array index
539 m_base = 7; // oct
540 if ( val == wxPG_BASE_HEX )
541 m_base = 3;
542 else if ( val == wxPG_BASE_DEC )
543 m_base = 6;
544 else if ( val == wxPG_BASE_HEXL )
545 m_base = 0;
546 return true;
547 }
548 else if ( name == wxPG_UINT_PREFIX )
549 {
550 m_prefix = (wxByte) value.GetLong();
551 return true;
552 }
553 return false;
554 }
555
556 // -----------------------------------------------------------------------
557 // wxFloatProperty
558 // -----------------------------------------------------------------------
559
560 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFloatProperty,wxPGProperty,
561 double,double,TextCtrl)
562
563 wxFloatProperty::wxFloatProperty( const wxString& label,
564 const wxString& name,
565 double value )
566 : wxPGProperty(label,name)
567 {
568 m_precision = -1;
569 SetValue(value);
570 }
571
572 wxFloatProperty::~wxFloatProperty() { }
573
574 // This helper method provides standard way for floating point-using
575 // properties to convert values to string.
576 void wxPropertyGrid::DoubleToString(wxString& target,
577 double value,
578 int precision,
579 bool removeZeroes,
580 wxString* precTemplate)
581 {
582 if ( precision >= 0 )
583 {
584 wxString text1;
585 if (!precTemplate)
586 precTemplate = &text1;
587
588 if ( !precTemplate->length() )
589 {
590 *precTemplate = wxS("%.");
591 *precTemplate << wxString::Format( wxS("%i"), precision );
592 *precTemplate << wxS('f');
593 }
594
595 target.Printf( precTemplate->c_str(), value );
596 }
597 else
598 {
599 target.Printf( wxS("%f"), value );
600 }
601
602 if ( removeZeroes && precision != 0 && target.length() )
603 {
604 // Remove excess zeroes (do not remove this code just yet,
605 // since sprintf can't do the same consistently across platforms).
606 wxString::const_iterator i = target.end() - 1;
607 size_t new_len = target.length() - 1;
608
609 for ( ; i != target.begin(); --i )
610 {
611 if ( *i != wxS('0') )
612 break;
613 new_len--;
614 }
615
616 wxChar cur_char = *i;
617 if ( cur_char != wxS('.') && cur_char != wxS(',') )
618 new_len++;
619
620 if ( new_len != target.length() )
621 target.resize(new_len);
622 }
623 }
624
625 wxString wxFloatProperty::ValueToString( wxVariant& value,
626 int argFlags ) const
627 {
628 wxString text;
629 if ( !value.IsNull() )
630 {
631 wxPropertyGrid::DoubleToString(text,
632 value,
633 m_precision,
634 !(argFlags & wxPG_FULL_VALUE),
635 (wxString*) NULL);
636 }
637 return text;
638 }
639
640 bool wxFloatProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
641 {
642 wxString s;
643 double value;
644
645 if ( text.length() == 0 )
646 {
647 variant.MakeNull();
648 return true;
649 }
650
651 bool res = text.ToDouble(&value);
652 if ( res )
653 {
654 if ( variant != value )
655 {
656 variant = value;
657 return true;
658 }
659 }
660 else if ( argFlags & wxPG_REPORT_ERROR )
661 {
662 }
663 return false;
664 }
665
666 bool wxFloatProperty::DoValidation( const wxPGProperty* property, double& value, wxPGValidationInfo* pValidationInfo, int mode )
667 {
668 // Check for min/max
669 double min = (double)wxINT64_MIN;
670 double max = (double)wxINT64_MAX;
671 wxVariant variant;
672 bool minOk = false;
673 bool maxOk = false;
674
675 variant = property->GetAttribute(wxPGGlobalVars->m_strMin);
676 if ( !variant.IsNull() )
677 {
678 wxPGVariantToDouble(variant, &min);
679 minOk = true;
680 }
681
682 variant = property->GetAttribute(wxPGGlobalVars->m_strMax);
683 if ( !variant.IsNull() )
684 {
685 wxPGVariantToDouble(variant, &max);
686 maxOk = true;
687 }
688
689 if ( minOk )
690 {
691 if ( value < min )
692 {
693 if ( mode == wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE )
694 pValidationInfo->SetFailureMessage(
695 wxString::Format(_("Value must be %f or higher"),min)
696 );
697 else if ( mode == wxPG_PROPERTY_VALIDATION_SATURATE )
698 value = min;
699 else
700 value = max - (min - value);
701 return false;
702 }
703 }
704
705 if ( maxOk )
706 {
707 wxPGVariantToDouble(variant, &max);
708 if ( value > max )
709 {
710 if ( mode == wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE )
711 pValidationInfo->SetFailureMessage(
712 wxString::Format(_("Value must be %f or less"),max)
713 );
714 else if ( mode == wxPG_PROPERTY_VALIDATION_SATURATE )
715 value = max;
716 else
717 value = min + (value - max);
718 return false;
719 }
720 }
721 return true;
722 }
723
724 bool wxFloatProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& validationInfo ) const
725 {
726 double fpv;
727 if ( wxPGVariantToDouble(value, &fpv) )
728 return DoValidation(this, fpv, &validationInfo, wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE);
729 return true;
730 }
731
732 bool wxFloatProperty::DoSetAttribute( const wxString& name, wxVariant& value )
733 {
734 if ( name == wxPG_FLOAT_PRECISION )
735 {
736 m_precision = value.GetLong();
737 return true;
738 }
739 return false;
740 }
741
742 wxValidator* wxFloatProperty::DoGetValidator() const
743 {
744 return wxIntProperty::GetClassValidator();
745 }
746
747 // -----------------------------------------------------------------------
748 // wxBoolProperty
749 // -----------------------------------------------------------------------
750
751 // We cannot use standard WX_PG_IMPLEMENT_PROPERTY_CLASS macro, since
752 // there is a custom GetEditorClass.
753
754 IMPLEMENT_DYNAMIC_CLASS(wxBoolProperty, wxPGProperty)
755
756 const wxPGEditor* wxBoolProperty::DoGetEditorClass() const
757 {
758 // Select correct editor control.
759 #if wxPG_INCLUDE_CHECKBOX
760 if ( !(m_flags & wxPG_PROP_USE_CHECKBOX) )
761 return wxPGEditor_Choice;
762 return wxPGEditor_CheckBox;
763 #else
764 return wxPGEditor_Choice;
765 #endif
766 }
767
768 wxBoolProperty::wxBoolProperty( const wxString& label, const wxString& name, bool value ) :
769 wxPGProperty(label,name)
770 {
771 m_choices.Assign(wxPGGlobalVars->m_boolChoices);
772
773 SetValue(wxPGVariant_Bool(value));
774
775 m_flags |= wxPG_PROP_USE_DCC;
776 }
777
778 wxBoolProperty::~wxBoolProperty() { }
779
780 wxString wxBoolProperty::ValueToString( wxVariant& value,
781 int argFlags ) const
782 {
783 bool boolValue = value.GetBool();
784
785 // As a fragment of composite string value,
786 // make it a little more readable.
787 if ( argFlags & wxPG_COMPOSITE_FRAGMENT )
788 {
789 if ( boolValue )
790 {
791 return m_label;
792 }
793 else
794 {
795 if ( argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT )
796 return wxEmptyString;
797
798 wxString notFmt;
799 if ( wxPGGlobalVars->m_autoGetTranslation )
800 notFmt = _("Not %s");
801 else
802 notFmt = wxS("Not %s");
803
804 return wxString::Format(notFmt.c_str(), m_label.c_str());
805 }
806 }
807
808 if ( !(argFlags & wxPG_FULL_VALUE) )
809 {
810 return wxPGGlobalVars->m_boolChoices[boolValue?1:0].GetText();
811 }
812
813 wxString text;
814
815 if ( boolValue ) text = wxS("true");
816 else text = wxS("false");
817
818 return text;
819 }
820
821 bool wxBoolProperty::StringToValue( wxVariant& variant, const wxString& text, int WXUNUSED(argFlags) ) const
822 {
823 bool boolValue = false;
824 if ( text.CmpNoCase(wxPGGlobalVars->m_boolChoices[1].GetText()) == 0 ||
825 text.CmpNoCase(wxS("true")) == 0 ||
826 text.CmpNoCase(m_label) == 0 )
827 boolValue = true;
828
829 if ( text.length() == 0 )
830 {
831 variant.MakeNull();
832 return true;
833 }
834
835 if ( variant != boolValue )
836 {
837 variant = wxPGVariant_Bool(boolValue);
838 return true;
839 }
840 return false;
841 }
842
843 bool wxBoolProperty::IntToValue( wxVariant& variant, int value, int ) const
844 {
845 bool boolValue = value ? true : false;
846
847 if ( variant != boolValue )
848 {
849 variant = wxPGVariant_Bool(boolValue);
850 return true;
851 }
852 return false;
853 }
854
855 bool wxBoolProperty::DoSetAttribute( const wxString& name, wxVariant& value )
856 {
857 #if wxPG_INCLUDE_CHECKBOX
858 if ( name == wxPG_BOOL_USE_CHECKBOX )
859 {
860 int ival = wxPGVariantToInt(value);
861 if ( ival )
862 m_flags |= wxPG_PROP_USE_CHECKBOX;
863 else
864 m_flags &= ~(wxPG_PROP_USE_CHECKBOX);
865 return true;
866 }
867 #endif
868 if ( name == wxPG_BOOL_USE_DOUBLE_CLICK_CYCLING )
869 {
870 int ival = wxPGVariantToInt(value);
871 if ( ival )
872 m_flags |= wxPG_PROP_USE_DCC;
873 else
874 m_flags &= ~(wxPG_PROP_USE_DCC);
875 return true;
876 }
877 return false;
878 }
879
880 // -----------------------------------------------------------------------
881 // wxEnumProperty
882 // -----------------------------------------------------------------------
883
884 IMPLEMENT_DYNAMIC_CLASS(wxEnumProperty, wxPGProperty)
885
886 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEnumProperty,long,Choice)
887
888 wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name, const wxChar** labels,
889 const long* values, int value ) : wxPGProperty(label,name)
890 {
891 SetIndex(0);
892
893 if ( labels )
894 {
895 m_choices.Add(labels,values);
896
897 if ( GetItemCount() )
898 SetValue( (long)value );
899 }
900 }
901
902 wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name, const wxChar** labels,
903 const long* values, wxPGChoices* choicesCache, int value )
904 : wxPGProperty(label,name)
905 {
906 SetIndex(0);
907
908 wxASSERT( choicesCache );
909
910 if ( choicesCache->IsOk() )
911 {
912 m_choices.Assign( *choicesCache );
913 m_value = wxPGVariant_Zero;
914 }
915 else if ( labels )
916 {
917 m_choices.Add(labels,values);
918
919 if ( GetItemCount() )
920 SetValue( (long)value );
921 }
922 }
923
924 wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name,
925 const wxArrayString& labels, const wxArrayInt& values, int value )
926 : wxPGProperty(label,name)
927 {
928 SetIndex(0);
929
930 if ( &labels && labels.size() )
931 {
932 m_choices.Set(labels, values);
933
934 if ( GetItemCount() )
935 SetValue( (long)value );
936 }
937 }
938
939 wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name,
940 wxPGChoices& choices, int value )
941 : wxPGProperty(label,name)
942 {
943 m_choices.Assign( choices );
944
945 if ( GetItemCount() )
946 SetValue( (long)value );
947 }
948
949 int wxEnumProperty::GetIndexForValue( int value ) const
950 {
951 if ( !m_choices.IsOk() )
952 return -1;
953
954 int intVal = m_choices.Index(value);
955 if ( intVal >= 0 )
956 return intVal;
957
958 return value;
959 }
960
961 wxEnumProperty::~wxEnumProperty ()
962 {
963 }
964
965 int wxEnumProperty::ms_nextIndex = -2;
966
967 void wxEnumProperty::OnSetValue()
968 {
969 wxString variantType = m_value.GetType();
970
971 if ( variantType == wxPG_VARIANT_TYPE_LONG )
972 ValueFromInt_( m_value, m_value.GetLong(), wxPG_FULL_VALUE );
973 else if ( variantType == wxPG_VARIANT_TYPE_STRING )
974 ValueFromString_( m_value, m_value.GetString(), 0 );
975 else
976 wxFAIL;
977
978 if ( ms_nextIndex != -2 )
979 {
980 m_index = ms_nextIndex;
981 ms_nextIndex = -2;
982 }
983 }
984
985 bool wxEnumProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& WXUNUSED(validationInfo) ) const
986 {
987 // Make sure string value is in the list,
988 // unless property has string as preferred value type
989 // To reduce code size, use conversion here as well
990 if ( value.GetType() == wxPG_VARIANT_TYPE_STRING &&
991 !this->IsKindOf(CLASSINFO(wxEditEnumProperty)) )
992 return ValueFromString_( value, value.GetString(), wxPG_PROPERTY_SPECIFIC );
993
994 return true;
995 }
996
997 wxString wxEnumProperty::ValueToString( wxVariant& value,
998 int WXUNUSED(argFlags) ) const
999 {
1000 if ( value.GetType() == wxPG_VARIANT_TYPE_STRING )
1001 return value.GetString();
1002
1003 int index = m_choices.Index(value.GetLong());
1004 if ( index < 0 )
1005 return wxEmptyString;
1006
1007 return m_choices.GetLabel(index);
1008 }
1009
1010 bool wxEnumProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
1011 {
1012 return ValueFromString_( variant, text, argFlags );
1013 }
1014
1015 bool wxEnumProperty::IntToValue( wxVariant& variant, int intVal, int argFlags ) const
1016 {
1017 return ValueFromInt_( variant, intVal, argFlags );
1018 }
1019
1020 bool wxEnumProperty::ValueFromString_( wxVariant& value, const wxString& text, int argFlags ) const
1021 {
1022 int useIndex = -1;
1023 long useValue = 0;
1024
1025 for ( unsigned int i=0; i<m_choices.GetCount(); i++ )
1026 {
1027 const wxString& entryLabel = m_choices.GetLabel(i);
1028 if ( text.CmpNoCase(entryLabel) == 0 )
1029 {
1030 useIndex = (int)i;
1031 useValue = m_choices.GetValue(i);
1032 break;
1033 }
1034 }
1035
1036 bool asText = false;
1037
1038 bool isEdit = this->IsKindOf(CLASSINFO(wxEditEnumProperty));
1039
1040 // If text not any of the choices, store as text instead
1041 // (but only if we are wxEditEnumProperty)
1042 if ( useIndex == -1 &&
1043 (value.GetType() != wxPG_VARIANT_TYPE_STRING || (m_value.GetString() != text)) &&
1044 isEdit )
1045 {
1046 asText = true;
1047 }
1048
1049 int setAsNextIndex = -2;
1050
1051 if ( asText )
1052 {
1053 setAsNextIndex = -1;
1054 value = text;
1055 }
1056 else if ( m_index != useIndex )
1057 {
1058 if ( useIndex != -1 )
1059 {
1060 setAsNextIndex = useIndex;
1061 value = (long)useValue;
1062 }
1063 else
1064 {
1065 setAsNextIndex = -1;
1066 value = wxPGVariant_MinusOne;
1067 }
1068 }
1069
1070 if ( setAsNextIndex != -2 )
1071 {
1072 // If wxPG_PROPERTY_SPECIFIC is set, then this is done for
1073 // validation purposes only, and index must not be changed
1074 if ( !(argFlags & wxPG_PROPERTY_SPECIFIC) )
1075 ms_nextIndex = setAsNextIndex;
1076
1077 if ( isEdit || setAsNextIndex != -1 )
1078 return true;
1079 else
1080 return false;
1081 }
1082 return false;
1083 }
1084
1085 bool wxEnumProperty::ValueFromInt_( wxVariant& variant, int intVal, int argFlags ) const
1086 {
1087 // If wxPG_FULL_VALUE is *not* in argFlags, then intVal is index from combo box.
1088 //
1089 ms_nextIndex = -2;
1090
1091 if ( argFlags & wxPG_FULL_VALUE )
1092 {
1093 ms_nextIndex = GetIndexForValue( intVal );
1094 }
1095 else
1096 {
1097 if ( m_index != intVal )
1098 {
1099 ms_nextIndex = intVal;
1100 }
1101 }
1102
1103 if ( ms_nextIndex != -2 )
1104 {
1105 if ( !(argFlags & wxPG_FULL_VALUE) )
1106 intVal = m_choices.GetValue(intVal);
1107
1108 variant = (long)intVal;
1109
1110 return true;
1111 }
1112
1113 return false;
1114 }
1115
1116 void wxEnumProperty::SetIndex( int index )
1117 {
1118 ms_nextIndex = -2;
1119 m_index = index;
1120 }
1121
1122 int wxEnumProperty::GetIndex() const
1123 {
1124 if ( ms_nextIndex != -2 )
1125 return ms_nextIndex;
1126 return m_index;
1127 }
1128
1129 // -----------------------------------------------------------------------
1130 // wxEditEnumProperty
1131 // -----------------------------------------------------------------------
1132
1133 IMPLEMENT_DYNAMIC_CLASS(wxEditEnumProperty, wxPGProperty)
1134
1135 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEditEnumProperty,wxString,ComboBox)
1136
1137 wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name, const wxChar** labels,
1138 const long* values, const wxString& value )
1139 : wxEnumProperty(label,name,labels,values,0)
1140 {
1141 SetValue( value );
1142 }
1143
1144 wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name, const wxChar** labels,
1145 const long* values, wxPGChoices* choicesCache, const wxString& value )
1146 : wxEnumProperty(label,name,labels,values,choicesCache,0)
1147 {
1148 SetValue( value );
1149 }
1150
1151 wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name,
1152 const wxArrayString& labels, const wxArrayInt& values, const wxString& value )
1153 : wxEnumProperty(label,name,labels,values,0)
1154 {
1155 SetValue( value );
1156 }
1157
1158 wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name,
1159 wxPGChoices& choices, const wxString& value )
1160 : wxEnumProperty(label,name,choices,0)
1161 {
1162 SetValue( value );
1163 }
1164
1165 wxEditEnumProperty::~wxEditEnumProperty()
1166 {
1167 }
1168
1169 // -----------------------------------------------------------------------
1170 // wxFlagsProperty
1171 // -----------------------------------------------------------------------
1172
1173 IMPLEMENT_DYNAMIC_CLASS(wxFlagsProperty,wxPGProperty)
1174
1175 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxFlagsProperty,long,TextCtrl)
1176
1177 void wxFlagsProperty::Init()
1178 {
1179 SetParentalType(wxPG_PROP_AGGREGATE);
1180
1181 long value = m_value;
1182
1183 //
1184 // Generate children
1185 //
1186 unsigned int i;
1187
1188 unsigned int prevChildCount = m_children.size();
1189
1190 int oldSel = -1;
1191 if ( prevChildCount )
1192 {
1193 wxPropertyGridPageState* state = GetParentState();
1194
1195 // State safety check (it may be NULL in immediate parent)
1196 wxASSERT( state );
1197
1198 if ( state )
1199 {
1200 wxPGProperty* selected = state->GetSelection();
1201 if ( selected )
1202 {
1203 if ( selected->GetParent() == this )
1204 oldSel = selected->GetIndexInParent();
1205 else if ( selected == this )
1206 oldSel = -2;
1207 }
1208 }
1209 state->DoClearSelection();
1210 }
1211
1212 // Delete old children
1213 for ( i=0; i<prevChildCount; i++ )
1214 delete m_children[i];
1215
1216 m_children.clear();
1217
1218 if ( m_choices.IsOk() )
1219 {
1220 const wxPGChoices& choices = m_choices;
1221
1222 for ( i=0; i<GetItemCount(); i++ )
1223 {
1224 bool child_val;
1225 child_val = ( value & choices.GetValue(i) )?true:false;
1226
1227 wxPGProperty* boolProp;
1228 wxString label = GetLabel(i);
1229
1230 #if wxUSE_INTL
1231 if ( wxPGGlobalVars->m_autoGetTranslation )
1232 {
1233 boolProp = new wxBoolProperty( ::wxGetTranslation(label), label, child_val );
1234 }
1235 else
1236 #endif
1237 {
1238 boolProp = new wxBoolProperty( label, label, child_val );
1239 }
1240 AddChild(boolProp);
1241 }
1242
1243 m_oldChoicesData = m_choices.GetDataPtr();
1244 }
1245
1246 m_oldValue = m_value;
1247
1248 if ( prevChildCount )
1249 SubPropsChanged(oldSel);
1250 }
1251
1252 wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name,
1253 const wxChar** labels, const long* values, long value ) : wxPGProperty(label,name)
1254 {
1255 m_oldChoicesData = (wxPGChoicesData*) NULL;
1256
1257 if ( labels )
1258 {
1259 m_choices.Set(labels,values);
1260
1261 wxASSERT( GetItemCount() );
1262
1263 SetValue( value );
1264 }
1265 else
1266 {
1267 m_value = wxPGVariant_Zero;
1268 }
1269 }
1270
1271 wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name,
1272 const wxArrayString& labels, const wxArrayInt& values, int value )
1273 : wxPGProperty(label,name)
1274 {
1275 m_oldChoicesData = (wxPGChoicesData*) NULL;
1276
1277 if ( &labels && labels.size() )
1278 {
1279 m_choices.Set(labels,values);
1280
1281 wxASSERT( GetItemCount() );
1282
1283 SetValue( (long)value );
1284 }
1285 else
1286 {
1287 m_value = wxPGVariant_Zero;
1288 }
1289 }
1290
1291 wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name,
1292 wxPGChoices& choices, long value )
1293 : wxPGProperty(label,name)
1294 {
1295 m_oldChoicesData = (wxPGChoicesData*) NULL;
1296
1297 if ( choices.IsOk() )
1298 {
1299 m_choices.Assign(choices);
1300
1301 wxASSERT( GetItemCount() );
1302
1303 SetValue( value );
1304 }
1305 else
1306 {
1307 m_value = wxPGVariant_Zero;
1308 }
1309 }
1310
1311 wxFlagsProperty::~wxFlagsProperty()
1312 {
1313 }
1314
1315 void wxFlagsProperty::OnSetValue()
1316 {
1317 if ( !m_choices.IsOk() || !GetItemCount() )
1318 {
1319 m_value = wxPGVariant_Zero;
1320 }
1321 else
1322 {
1323 long val = m_value.GetLong();
1324
1325 long fullFlags = 0;
1326
1327 // normalize the value (i.e. remove extra flags)
1328 unsigned int i;
1329 const wxPGChoices& choices = m_choices;
1330 for ( i = 0; i < GetItemCount(); i++ )
1331 {
1332 fullFlags |= choices.GetValue(i);
1333 }
1334
1335 val &= fullFlags;
1336
1337 m_value = val;
1338
1339 // Need to (re)init now?
1340 if ( GetChildCount() != GetItemCount() ||
1341 m_choices.GetDataPtr() != m_oldChoicesData )
1342 {
1343 Init();
1344 }
1345 }
1346
1347 long newFlags = m_value;
1348
1349 if ( newFlags != m_oldValue )
1350 {
1351 // Set child modified states
1352 unsigned int i;
1353 const wxPGChoices& choices = m_choices;
1354 for ( i = 0; i<GetItemCount(); i++ )
1355 {
1356 int flag;
1357
1358 flag = choices.GetValue(i);
1359
1360 if ( (newFlags & flag) != (m_oldValue & flag) )
1361 Item(i)->SetFlag( wxPG_PROP_MODIFIED );
1362 }
1363
1364 m_oldValue = newFlags;
1365 }
1366 }
1367
1368 wxString wxFlagsProperty::ValueToString( wxVariant& value,
1369 int WXUNUSED(argFlags) ) const
1370 {
1371 wxString text;
1372
1373 if ( !m_choices.IsOk() )
1374 return text;
1375
1376 long flags = value;
1377 unsigned int i;
1378 const wxPGChoices& choices = m_choices;
1379
1380 for ( i = 0; i < GetItemCount(); i++ )
1381 {
1382 int doAdd;
1383 doAdd = ( flags & choices.GetValue(i) );
1384
1385 if ( doAdd )
1386 {
1387 text += choices.GetLabel(i);
1388 text += wxS(", ");
1389 }
1390 }
1391
1392 // remove last comma
1393 if ( text.Len() > 1 )
1394 text.Truncate ( text.Len() - 2 );
1395
1396 return text;
1397 }
1398
1399 // Translate string into flag tokens
1400 bool wxFlagsProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const
1401 {
1402 if ( !m_choices.IsOk() )
1403 return false;
1404
1405 long newFlags = 0;
1406
1407 // semicolons are no longer valid delimeters
1408 WX_PG_TOKENIZER1_BEGIN(text,wxS(','))
1409
1410 if ( token.length() )
1411 {
1412 // Determine which one it is
1413 long bit = IdToBit( token );
1414
1415 if ( bit != -1 )
1416 {
1417 // Changed?
1418 newFlags |= bit;
1419 }
1420 else
1421 {
1422 break;
1423 }
1424 }
1425
1426 WX_PG_TOKENIZER1_END()
1427
1428 if ( variant != (long)newFlags )
1429 {
1430 variant = (long)newFlags;
1431 return true;
1432 }
1433
1434 return false;
1435 }
1436
1437 // Converts string id to a relevant bit.
1438 long wxFlagsProperty::IdToBit( const wxString& id ) const
1439 {
1440 unsigned int i;
1441 for ( i = 0; i < GetItemCount(); i++ )
1442 {
1443 if ( id == GetLabel(i) )
1444 {
1445 return m_choices.GetValue(i);
1446 }
1447 }
1448 return -1;
1449 }
1450
1451 void wxFlagsProperty::RefreshChildren()
1452 {
1453 if ( !m_choices.IsOk() || !GetChildCount() ) return;
1454
1455 int flags = m_value.GetLong();
1456
1457 const wxPGChoices& choices = m_choices;
1458 unsigned int i;
1459 for ( i = 0; i < GetItemCount(); i++ )
1460 {
1461 long flag;
1462
1463 flag = choices.GetValue(i);
1464
1465 long subVal = flags & flag;
1466 wxPGProperty* p = Item(i);
1467
1468 if ( subVal != (m_oldValue & flag) )
1469 p->SetFlag( wxPG_PROP_MODIFIED );
1470
1471 p->SetValue( subVal?true:false );
1472 }
1473
1474 m_oldValue = flags;
1475 }
1476
1477 void wxFlagsProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const
1478 {
1479 long oldValue = thisValue.GetLong();
1480 long val = childValue.GetLong();
1481 unsigned long vi = m_choices.GetValue(childIndex);
1482 if ( val )
1483 thisValue = (long)(oldValue | vi);
1484 else
1485 thisValue = (long)(oldValue & ~(vi));
1486 }
1487
1488 // -----------------------------------------------------------------------
1489 // wxDirProperty
1490 // -----------------------------------------------------------------------
1491
1492 IMPLEMENT_DYNAMIC_CLASS(wxDirProperty, wxLongStringProperty)
1493
1494 wxDirProperty::wxDirProperty( const wxString& name, const wxString& label, const wxString& value )
1495 : wxLongStringProperty(name,label,value)
1496 {
1497 m_flags |= wxPG_PROP_NO_ESCAPE;
1498 }
1499
1500 wxDirProperty::~wxDirProperty() { }
1501
1502 wxValidator* wxDirProperty::DoGetValidator() const
1503 {
1504 return wxFileProperty::GetClassValidator();
1505 }
1506
1507 bool wxDirProperty::OnButtonClick( wxPropertyGrid* propGrid, wxString& value )
1508 {
1509 // Update property value from editor, if necessary
1510 wxSize dlg_sz(300,400);
1511
1512 wxString dlgMessage(m_dlgMessage);
1513 if ( dlgMessage.empty() )
1514 dlgMessage = _("Choose a directory:");
1515 wxDirDialog dlg( propGrid,
1516 dlgMessage,
1517 value,
1518 0,
1519 #if !wxPG_SMALL_SCREEN
1520 propGrid->GetGoodEditorDialogPosition(this,dlg_sz),
1521 dlg_sz
1522 #else
1523 wxDefaultPosition,
1524 wxDefaultSize
1525 #endif
1526 );
1527
1528 if ( dlg.ShowModal() == wxID_OK )
1529 {
1530 value = dlg.GetPath();
1531 return true;
1532 }
1533 return false;
1534 }
1535
1536 bool wxDirProperty::DoSetAttribute( const wxString& name, wxVariant& value )
1537 {
1538 if ( name == wxPG_DIR_DIALOG_MESSAGE )
1539 {
1540 m_dlgMessage = value.GetString();
1541 return true;
1542 }
1543 return false;
1544 }
1545
1546 // -----------------------------------------------------------------------
1547 // wxPGFileDialogAdapter
1548 // -----------------------------------------------------------------------
1549
1550 bool wxPGFileDialogAdapter::DoShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property )
1551 {
1552 wxFileProperty* fileProp = NULL;
1553 wxString path;
1554 int indFilter = -1;
1555
1556 if ( property->IsKindOf(CLASSINFO(wxFileProperty)) )
1557 {
1558 fileProp = ((wxFileProperty*)property);
1559 wxFileName filename = fileProp->GetValue().GetString();
1560 path = filename.GetPath();
1561 indFilter = fileProp->m_indFilter;
1562
1563 if ( !path.length() && fileProp->m_basePath.length() )
1564 path = fileProp->m_basePath;
1565 }
1566 else
1567 {
1568 wxFileName fn(property->GetValue().GetString());
1569 path = fn.GetPath();
1570 }
1571
1572 wxFileDialog dlg( propGrid->GetPanel(),
1573 property->GetAttribute(wxS("DialogTitle"), _("Choose a file")),
1574 property->GetAttribute(wxS("InitialPath"), path),
1575 wxEmptyString,
1576 property->GetAttribute(wxPG_FILE_WILDCARD, _("All files (*.*)|*.*")),
1577 0,
1578 wxDefaultPosition );
1579
1580 if ( indFilter >= 0 )
1581 dlg.SetFilterIndex( indFilter );
1582
1583 if ( dlg.ShowModal() == wxID_OK )
1584 {
1585 if ( fileProp )
1586 fileProp->m_indFilter = dlg.GetFilterIndex();
1587 SetValue( dlg.GetPath() );
1588 return true;
1589 }
1590 return false;
1591 }
1592
1593 // -----------------------------------------------------------------------
1594 // wxFileProperty
1595 // -----------------------------------------------------------------------
1596
1597 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFileProperty,wxPGProperty,
1598 wxString,const wxString&,TextCtrlAndButton)
1599
1600 wxFileProperty::wxFileProperty( const wxString& label, const wxString& name,
1601 const wxString& value ) : wxPGProperty(label,name)
1602 {
1603 m_flags |= wxPG_PROP_SHOW_FULL_FILENAME;
1604 m_indFilter = -1;
1605 SetAttribute( wxPG_FILE_WILDCARD, _("All files (*.*)|*.*") );
1606
1607 SetValue(value);
1608 }
1609
1610 wxFileProperty::~wxFileProperty() {}
1611
1612 #if wxUSE_VALIDATORS
1613
1614 wxValidator* wxFileProperty::GetClassValidator()
1615 {
1616 WX_PG_DOGETVALIDATOR_ENTRY()
1617
1618 // Atleast wxPython 2.6.2.1 required that the string argument is given
1619 static wxString v;
1620 wxTextValidator* validator = new wxTextValidator(wxFILTER_EXCLUDE_CHAR_LIST,&v);
1621
1622 wxArrayString exChars;
1623 exChars.Add(wxS("?"));
1624 exChars.Add(wxS("*"));
1625 exChars.Add(wxS("|"));
1626 exChars.Add(wxS("<"));
1627 exChars.Add(wxS(">"));
1628 exChars.Add(wxS("\""));
1629
1630 validator->SetExcludes(exChars);
1631
1632 WX_PG_DOGETVALIDATOR_EXIT(validator)
1633 }
1634
1635 wxValidator* wxFileProperty::DoGetValidator() const
1636 {
1637 return GetClassValidator();
1638 }
1639
1640 #endif
1641
1642 void wxFileProperty::OnSetValue()
1643 {
1644 const wxString& fnstr = m_value.GetString();
1645
1646 wxFileName filename = fnstr;
1647
1648 if ( !filename.HasName() )
1649 {
1650 m_value = wxPGVariant_EmptyString;
1651 }
1652
1653 // Find index for extension.
1654 if ( m_indFilter < 0 && fnstr.length() )
1655 {
1656 wxString ext = filename.GetExt();
1657 int curind = 0;
1658 size_t pos = 0;
1659 size_t len = m_wildcard.length();
1660
1661 pos = m_wildcard.find(wxS("|"), pos);
1662 while ( pos != wxString::npos && pos < (len-3) )
1663 {
1664 size_t ext_begin = pos + 3;
1665
1666 pos = m_wildcard.find(wxS("|"), ext_begin);
1667 if ( pos == wxString::npos )
1668 pos = len;
1669 wxString found_ext = m_wildcard.substr(ext_begin, pos-ext_begin);
1670
1671 if ( found_ext.length() > 0 )
1672 {
1673 if ( found_ext[0] == wxS('*') )
1674 {
1675 m_indFilter = curind;
1676 break;
1677 }
1678 if ( ext.CmpNoCase(found_ext) == 0 )
1679 {
1680 m_indFilter = curind;
1681 break;
1682 }
1683 }
1684
1685 if ( pos != len )
1686 pos = m_wildcard.find(wxS("|"), pos+1);
1687
1688 curind++;
1689 }
1690 }
1691 }
1692
1693 wxFileName wxFileProperty::GetFileName() const
1694 {
1695 wxFileName filename;
1696
1697 if ( !m_value.IsNull() )
1698 filename = m_value.GetString();
1699
1700 return filename;
1701 }
1702
1703 wxString wxFileProperty::ValueToString( wxVariant& value,
1704 int argFlags ) const
1705 {
1706 wxFileName filename = value.GetString();
1707
1708 if ( !filename.HasName() )
1709 return wxEmptyString;
1710
1711 wxString fullName = filename.GetFullName();
1712 if ( !fullName.length() )
1713 return wxEmptyString;
1714
1715 if ( argFlags & wxPG_FULL_VALUE )
1716 {
1717 return filename.GetFullPath();
1718 }
1719 else if ( m_flags & wxPG_PROP_SHOW_FULL_FILENAME )
1720 {
1721 if ( m_basePath.Length() )
1722 {
1723 wxFileName fn2(filename);
1724 fn2.MakeRelativeTo(m_basePath);
1725 return fn2.GetFullPath();
1726 }
1727 return filename.GetFullPath();
1728 }
1729
1730 return filename.GetFullName();
1731 }
1732
1733 wxPGEditorDialogAdapter* wxFileProperty::GetEditorDialog() const
1734 {
1735 return new wxPGFileDialogAdapter();
1736 }
1737
1738 bool wxFileProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
1739 {
1740 wxFileName filename = variant.GetString();
1741
1742 if ( (m_flags & wxPG_PROP_SHOW_FULL_FILENAME) || (argFlags & wxPG_FULL_VALUE) )
1743 {
1744 if ( filename != text )
1745 {
1746 variant = text;
1747 return true;
1748 }
1749 }
1750 else
1751 {
1752 if ( filename.GetFullName() != text )
1753 {
1754 wxFileName fn = filename;
1755 fn.SetFullName(text);
1756 variant = fn.GetFullPath();
1757 return true;
1758 }
1759 }
1760
1761 return false;
1762 }
1763
1764 bool wxFileProperty::DoSetAttribute( const wxString& name, wxVariant& value )
1765 {
1766 // Return false on some occasions to make sure those attribs will get
1767 // stored in m_attributes.
1768 if ( name == wxPG_FILE_SHOW_FULL_PATH )
1769 {
1770 if ( wxPGVariantToInt(value) )
1771 m_flags |= wxPG_PROP_SHOW_FULL_FILENAME;
1772 else
1773 m_flags &= ~(wxPG_PROP_SHOW_FULL_FILENAME);
1774 return true;
1775 }
1776 else if ( name == wxPG_FILE_WILDCARD )
1777 {
1778 m_wildcard = value.GetString();
1779 }
1780 else if ( name == wxPG_FILE_SHOW_RELATIVE_PATH )
1781 {
1782 m_basePath = value.GetString();
1783
1784 // Make sure wxPG_FILE_SHOW_FULL_PATH is also set
1785 m_flags |= wxPG_PROP_SHOW_FULL_FILENAME;
1786 }
1787 else if ( name == wxPG_FILE_INITIAL_PATH )
1788 {
1789 m_initialPath = value.GetString();
1790 return true;
1791 }
1792 else if ( name == wxPG_FILE_DIALOG_TITLE )
1793 {
1794 m_dlgTitle = value.GetString();
1795 return true;
1796 }
1797 return false;
1798 }
1799
1800 // -----------------------------------------------------------------------
1801 // wxPGLongStringDialogAdapter
1802 // -----------------------------------------------------------------------
1803
1804 bool wxPGLongStringDialogAdapter::DoShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property )
1805 {
1806 wxString val1 = property->GetValueAsString(0);
1807 wxString val_orig = val1;
1808
1809 wxString value;
1810 if ( !property->HasFlag(wxPG_PROP_NO_ESCAPE) )
1811 wxPropertyGrid::ExpandEscapeSequences(value, val1);
1812 else
1813 value = wxString(val1);
1814
1815 // Run editor dialog.
1816 if ( wxLongStringProperty::DisplayEditorDialog(property, propGrid, value) )
1817 {
1818 if ( !property->HasFlag(wxPG_PROP_NO_ESCAPE) )
1819 wxPropertyGrid::CreateEscapeSequences(val1,value);
1820 else
1821 val1 = value;
1822
1823 if ( val1 != val_orig )
1824 {
1825 SetValue( val1 );
1826 return true;
1827 }
1828 }
1829 return false;
1830 }
1831
1832 // -----------------------------------------------------------------------
1833 // wxLongStringProperty
1834 // -----------------------------------------------------------------------
1835
1836 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxLongStringProperty,wxPGProperty,
1837 wxString,const wxString&,TextCtrlAndButton)
1838
1839 wxLongStringProperty::wxLongStringProperty( const wxString& label, const wxString& name,
1840 const wxString& value ) : wxPGProperty(label,name)
1841 {
1842 SetValue(value);
1843 }
1844
1845 wxLongStringProperty::~wxLongStringProperty() {}
1846
1847 wxString wxLongStringProperty::ValueToString( wxVariant& value,
1848 int WXUNUSED(argFlags) ) const
1849 {
1850 return value;
1851 }
1852
1853 bool wxLongStringProperty::OnEvent( wxPropertyGrid* propGrid, wxWindow* WXUNUSED(primary),
1854 wxEvent& event )
1855 {
1856 if ( propGrid->IsMainButtonEvent(event) )
1857 {
1858 // Update the value
1859 wxVariant useValue = propGrid->GetUncommittedPropertyValue();
1860
1861 wxString val1 = useValue.GetString();
1862 wxString val_orig = val1;
1863
1864 wxString value;
1865 if ( !(m_flags & wxPG_PROP_NO_ESCAPE) )
1866 wxPropertyGrid::ExpandEscapeSequences(value,val1);
1867 else
1868 value = wxString(val1);
1869
1870 // Run editor dialog.
1871 if ( OnButtonClick(propGrid,value) )
1872 {
1873 if ( !(m_flags & wxPG_PROP_NO_ESCAPE) )
1874 wxPropertyGrid::CreateEscapeSequences(val1,value);
1875 else
1876 val1 = value;
1877
1878 if ( val1 != val_orig )
1879 {
1880 SetValueInEvent( val1 );
1881 return true;
1882 }
1883 }
1884 }
1885 return false;
1886 }
1887
1888 bool wxLongStringProperty::OnButtonClick( wxPropertyGrid* propGrid, wxString& value )
1889 {
1890 return DisplayEditorDialog(this, propGrid, value);
1891 }
1892
1893 bool wxLongStringProperty::DisplayEditorDialog( wxPGProperty* prop, wxPropertyGrid* propGrid, wxString& value )
1894
1895 {
1896 // launch editor dialog
1897 wxDialog* dlg = new wxDialog(propGrid,-1,prop->GetLabel(),wxDefaultPosition,wxDefaultSize,
1898 wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxCLIP_CHILDREN);
1899
1900 dlg->SetFont(propGrid->GetFont()); // To allow entering chars of the same set as the propGrid
1901
1902 // Multi-line text editor dialog.
1903 #if !wxPG_SMALL_SCREEN
1904 const int spacing = 8;
1905 #else
1906 const int spacing = 4;
1907 #endif
1908 wxBoxSizer* topsizer = new wxBoxSizer( wxVERTICAL );
1909 wxBoxSizer* rowsizer = new wxBoxSizer( wxHORIZONTAL );
1910 wxTextCtrl* ed = new wxTextCtrl(dlg,11,value,
1911 wxDefaultPosition,wxDefaultSize,wxTE_MULTILINE);
1912
1913 rowsizer->Add( ed, 1, wxEXPAND|wxALL, spacing );
1914 topsizer->Add( rowsizer, 1, wxEXPAND, 0 );
1915 rowsizer = new wxBoxSizer( wxHORIZONTAL );
1916 const int but_sz_flags =
1917 wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT;
1918 rowsizer->Add( new wxButton(dlg,wxID_OK,_("Ok")),
1919 0, but_sz_flags, spacing );
1920 rowsizer->Add( new wxButton(dlg,wxID_CANCEL,_("Cancel")),
1921 0, but_sz_flags, spacing );
1922 topsizer->Add( rowsizer, 0, wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL, 0 );
1923
1924 dlg->SetSizer( topsizer );
1925 topsizer->SetSizeHints( dlg );
1926
1927 #if !wxPG_SMALL_SCREEN
1928 dlg->SetSize(400,300);
1929
1930 dlg->Move( propGrid->GetGoodEditorDialogPosition(prop,dlg->GetSize()) );
1931 #endif
1932
1933 int res = dlg->ShowModal();
1934
1935 if ( res == wxID_OK )
1936 {
1937 value = ed->GetValue();
1938 dlg->Destroy();
1939 return true;
1940 }
1941 dlg->Destroy();
1942 return false;
1943 }
1944
1945 bool wxLongStringProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const
1946 {
1947 if ( variant != text )
1948 {
1949 variant = text;
1950 return true;
1951 }
1952 return false;
1953 }
1954
1955 // -----------------------------------------------------------------------
1956 // wxArrayEditorDialog
1957 // -----------------------------------------------------------------------
1958
1959 BEGIN_EVENT_TABLE(wxArrayEditorDialog, wxDialog)
1960 EVT_IDLE(wxArrayEditorDialog::OnIdle)
1961 EVT_LISTBOX(24, wxArrayEditorDialog::OnListBoxClick)
1962 EVT_TEXT_ENTER(21, wxArrayEditorDialog::OnAddClick)
1963 EVT_BUTTON(22, wxArrayEditorDialog::OnAddClick)
1964 EVT_BUTTON(23, wxArrayEditorDialog::OnDeleteClick)
1965 EVT_BUTTON(25, wxArrayEditorDialog::OnUpClick)
1966 EVT_BUTTON(26, wxArrayEditorDialog::OnDownClick)
1967 EVT_BUTTON(27, wxArrayEditorDialog::OnUpdateClick)
1968 //EVT_BUTTON(28, wxArrayEditorDialog::OnCustomEditClick)
1969 END_EVENT_TABLE()
1970
1971 IMPLEMENT_ABSTRACT_CLASS(wxArrayEditorDialog, wxDialog)
1972
1973 #include "wx/statline.h"
1974
1975 // -----------------------------------------------------------------------
1976
1977 void wxArrayEditorDialog::OnIdle(wxIdleEvent& event)
1978 {
1979 //
1980 // Do control focus detection here.
1981 //
1982
1983 wxWindow* focused = FindFocus();
1984
1985 // This strange focus thing is a workaround for wxGTK wxListBox focus
1986 // reporting bug.
1987 if ( m_curFocus == 0 && focused != m_edValue &&
1988 focused != m_butAdd && focused != m_butUpdate &&
1989 m_lbStrings->GetSelection() >= 0 )
1990 {
1991 // ListBox was just focused.
1992 m_butAdd->Enable(false);
1993 m_butUpdate->Enable(false);
1994 m_butRemove->Enable(true);
1995 m_butUp->Enable(true);
1996 m_butDown->Enable(true);
1997 m_curFocus = 1;
1998 }
1999 else if ( (m_curFocus == 1 && focused == m_edValue) /*|| m_curFocus == 2*/ )
2000 {
2001 // TextCtrl was just focused.
2002 m_butAdd->Enable(true);
2003 bool upd_enable = false;
2004 if ( m_lbStrings->GetCount() && m_lbStrings->GetSelection() >= 0 )
2005 upd_enable = true;
2006 m_butUpdate->Enable(upd_enable);
2007 m_butRemove->Enable(false);
2008 m_butUp->Enable(false);
2009 m_butDown->Enable(false);
2010 m_curFocus = 0;
2011 }
2012
2013 event.Skip();
2014 }
2015
2016 // -----------------------------------------------------------------------
2017
2018 wxArrayEditorDialog::wxArrayEditorDialog()
2019 : wxDialog()
2020 {
2021 Init();
2022 }
2023
2024 // -----------------------------------------------------------------------
2025
2026 void wxArrayEditorDialog::Init()
2027 {
2028 m_custBtText = (const wxChar*) NULL;
2029 }
2030
2031 // -----------------------------------------------------------------------
2032
2033 wxArrayEditorDialog::wxArrayEditorDialog( wxWindow *parent,
2034 const wxString& message,
2035 const wxString& caption,
2036 long style,
2037 const wxPoint& pos,
2038 const wxSize& sz )
2039 : wxDialog()
2040 {
2041 Init();
2042 Create(parent,message,caption,style,pos,sz);
2043 }
2044
2045 // -----------------------------------------------------------------------
2046
2047 bool wxArrayEditorDialog::Create( wxWindow *parent,
2048 const wxString& message,
2049 const wxString& caption,
2050 long style,
2051 const wxPoint& pos,
2052 const wxSize& sz )
2053 {
2054 // On wxMAC the dialog shows incorrectly if style is not exactly wxCAPTION
2055 // FIXME: This should be only a temporary fix.
2056 #ifdef __WXMAC__
2057 wxUnusedVar(style);
2058 int useStyle = wxCAPTION;
2059 #else
2060 int useStyle = style;
2061 #endif
2062
2063 bool res = wxDialog::Create(parent, wxID_ANY, caption, pos, sz, useStyle);
2064
2065 SetFont(parent->GetFont()); // To allow entering chars of the same set as the propGrid
2066
2067 #if !wxPG_SMALL_SCREEN
2068 const int spacing = 4;
2069 #else
2070 const int spacing = 3;
2071 #endif
2072
2073 m_modified = false;
2074
2075 m_curFocus = 1;
2076
2077 const int but_sz_flags =
2078 wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxALL; //wxBOTTOM|wxLEFT|wxRIGHT;
2079
2080 wxBoxSizer* topsizer = new wxBoxSizer( wxVERTICAL );
2081
2082 // Message
2083 if ( message.length() )
2084 topsizer->Add( new wxStaticText(this,-1,message),
2085 0, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxALL, spacing );
2086
2087 // String editor
2088 wxBoxSizer* rowsizer = new wxBoxSizer( wxHORIZONTAL );
2089 m_edValue = new wxTextCtrl(this,21,wxEmptyString,
2090 wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER);
2091 wxValidator* validator = GetTextCtrlValidator();
2092 if ( validator )
2093 {
2094 m_edValue->SetValidator( *validator );
2095 delete validator;
2096 }
2097 rowsizer->Add( m_edValue,
2098 1, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxALL, spacing );
2099
2100 // Add button
2101 m_butAdd = new wxButton(this,22,_("Add"));
2102 rowsizer->Add( m_butAdd,
2103 0, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT, spacing );
2104 topsizer->Add( rowsizer, 0, wxEXPAND, spacing );
2105
2106 // Separator line
2107 topsizer->Add( new wxStaticLine(this,-1),
2108 0, wxEXPAND|wxBOTTOM|wxLEFT|wxRIGHT, spacing );
2109
2110 rowsizer = new wxBoxSizer( wxHORIZONTAL );
2111
2112 // list box
2113 m_lbStrings = new wxListBox(this, 24, wxDefaultPosition, wxDefaultSize);
2114 unsigned int i;
2115 for ( i=0; i<ArrayGetCount(); i++ )
2116 m_lbStrings->Append( ArrayGet(i) );
2117 rowsizer->Add( m_lbStrings, 1, wxEXPAND|wxRIGHT, spacing );
2118
2119 // Manipulator buttons
2120 wxBoxSizer* colsizer = new wxBoxSizer( wxVERTICAL );
2121 m_butCustom = (wxButton*) NULL;
2122 if ( m_custBtText )
2123 {
2124 m_butCustom = new wxButton(this,28,::wxGetTranslation(m_custBtText));
2125 colsizer->Add( m_butCustom,
2126 0, wxALIGN_CENTER|wxTOP/*wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT*/,
2127 spacing );
2128 }
2129 m_butUpdate = new wxButton(this,27,_("Update"));
2130 colsizer->Add( m_butUpdate,
2131 0, wxALIGN_CENTER|wxTOP, spacing );
2132 m_butRemove = new wxButton(this,23,_("Remove"));
2133 colsizer->Add( m_butRemove,
2134 0, wxALIGN_CENTER|wxTOP, spacing );
2135 m_butUp = new wxButton(this,25,_("Up"));
2136 colsizer->Add( m_butUp,
2137 0, wxALIGN_CENTER|wxTOP, spacing );
2138 m_butDown = new wxButton(this,26,_("Down"));
2139 colsizer->Add( m_butDown,
2140 0, wxALIGN_CENTER|wxTOP, spacing );
2141 rowsizer->Add( colsizer, 0, 0, spacing );
2142
2143 topsizer->Add( rowsizer, 1, wxLEFT|wxRIGHT|wxEXPAND, spacing );
2144
2145 // Separator line
2146 topsizer->Add( new wxStaticLine(this,-1),
2147 0, wxEXPAND|wxTOP|wxLEFT|wxRIGHT, spacing );
2148
2149 // buttons
2150 rowsizer = new wxBoxSizer( wxHORIZONTAL );
2151 /*
2152 const int but_sz_flags =
2153 wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT;
2154 */
2155 rowsizer->Add( new wxButton(this,wxID_OK,_("Ok")),
2156 0, but_sz_flags, spacing );
2157 rowsizer->Add( new wxButton(this,wxID_CANCEL,_("Cancel")),
2158 0, but_sz_flags, spacing );
2159 topsizer->Add( rowsizer, 0, wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL, 0 );
2160
2161 m_edValue->SetFocus();
2162
2163 SetSizer( topsizer );
2164 topsizer->SetSizeHints( this );
2165
2166 #if !wxPG_SMALL_SCREEN
2167 if ( sz.x == wxDefaultSize.x &&
2168 sz.y == wxDefaultSize.y )
2169 SetSize( wxSize(275,360) );
2170 else
2171 SetSize(sz);
2172 #endif
2173
2174 return res;
2175 }
2176
2177 // -----------------------------------------------------------------------
2178
2179 void wxArrayEditorDialog::OnAddClick(wxCommandEvent& )
2180 {
2181 wxString text = m_edValue->GetValue();
2182 if ( text.length() )
2183 {
2184 if ( ArrayInsert( text, -1 ) )
2185 {
2186 m_lbStrings->Append( text );
2187 m_modified = true;
2188 m_edValue->Clear();
2189 }
2190 }
2191 }
2192
2193 // -----------------------------------------------------------------------
2194
2195 void wxArrayEditorDialog::OnDeleteClick(wxCommandEvent& )
2196 {
2197 int index = m_lbStrings->GetSelection();
2198 if ( index >= 0 )
2199 {
2200 ArrayRemoveAt( index );
2201 m_lbStrings->Delete ( index );
2202 m_modified = true;
2203 }
2204 }
2205
2206 // -----------------------------------------------------------------------
2207
2208 void wxArrayEditorDialog::OnUpClick(wxCommandEvent& )
2209 {
2210 int index = m_lbStrings->GetSelection();
2211 if ( index > 0 )
2212 {
2213 ArraySwap(index-1,index);
2214 /*wxString old_str = m_array[index-1];
2215 wxString new_str = m_array[index];
2216 m_array[index-1] = new_str;
2217 m_array[index] = old_str;*/
2218 m_lbStrings->SetString ( index-1, ArrayGet(index-1) );
2219 m_lbStrings->SetString ( index, ArrayGet(index) );
2220 m_lbStrings->SetSelection ( index-1 );
2221 m_modified = true;
2222 }
2223 }
2224
2225 // -----------------------------------------------------------------------
2226
2227 void wxArrayEditorDialog::OnDownClick(wxCommandEvent& )
2228 {
2229 int index = m_lbStrings->GetSelection();
2230 int lastStringIndex = ((int) m_lbStrings->GetCount()) - 1;
2231 if ( index >= 0 && index < lastStringIndex )
2232 {
2233 ArraySwap(index,index+1);
2234 /*wxString old_str = m_array[index+1];
2235 wxString new_str = m_array[index];
2236 m_array[index+1] = new_str;
2237 m_array[index] = old_str;*/
2238 m_lbStrings->SetString ( index+1, ArrayGet(index+1) );
2239 m_lbStrings->SetString ( index, ArrayGet(index) );
2240 m_lbStrings->SetSelection ( index+1 );
2241 m_modified = true;
2242 }
2243 }
2244
2245 // -----------------------------------------------------------------------
2246
2247 void wxArrayEditorDialog::OnUpdateClick(wxCommandEvent& )
2248 {
2249 int index = m_lbStrings->GetSelection();
2250 if ( index >= 0 )
2251 {
2252 wxString str = m_edValue->GetValue();
2253 if ( ArraySet(index,str) )
2254 {
2255 m_lbStrings->SetString ( index, str );
2256 //m_array[index] = str;
2257 m_modified = true;
2258 }
2259 }
2260 }
2261
2262 // -----------------------------------------------------------------------
2263
2264 void wxArrayEditorDialog::OnListBoxClick(wxCommandEvent& )
2265 {
2266 int index = m_lbStrings->GetSelection();
2267 if ( index >= 0 )
2268 {
2269 m_edValue->SetValue( m_lbStrings->GetString(index) );
2270 }
2271 }
2272
2273 // -----------------------------------------------------------------------
2274 // wxPGArrayStringEditorDialog
2275 // -----------------------------------------------------------------------
2276
2277 IMPLEMENT_DYNAMIC_CLASS(wxPGArrayStringEditorDialog, wxArrayEditorDialog)
2278
2279 BEGIN_EVENT_TABLE(wxPGArrayStringEditorDialog, wxArrayEditorDialog)
2280 EVT_BUTTON(28, wxPGArrayStringEditorDialog::OnCustomEditClick)
2281 END_EVENT_TABLE()
2282
2283 // -----------------------------------------------------------------------
2284
2285 wxString wxPGArrayStringEditorDialog::ArrayGet( size_t index )
2286 {
2287 return m_array[index];
2288 }
2289
2290 size_t wxPGArrayStringEditorDialog::ArrayGetCount()
2291 {
2292 return m_array.size();
2293 }
2294
2295 bool wxPGArrayStringEditorDialog::ArrayInsert( const wxString& str, int index )
2296 {
2297 if (index<0)
2298 m_array.Add(str);
2299 else
2300 m_array.Insert(str,index);
2301 return true;
2302 }
2303
2304 bool wxPGArrayStringEditorDialog::ArraySet( size_t index, const wxString& str )
2305 {
2306 m_array[index] = str;
2307 return true;
2308 }
2309
2310 void wxPGArrayStringEditorDialog::ArrayRemoveAt( int index )
2311 {
2312 m_array.RemoveAt(index);
2313 }
2314
2315 void wxPGArrayStringEditorDialog::ArraySwap( size_t first, size_t second )
2316 {
2317 wxString old_str = m_array[first];
2318 wxString new_str = m_array[second];
2319 m_array[first] = new_str;
2320 m_array[second] = old_str;
2321 }
2322
2323 wxPGArrayStringEditorDialog::wxPGArrayStringEditorDialog()
2324 : wxArrayEditorDialog()
2325 {
2326 Init();
2327 }
2328
2329 void wxPGArrayStringEditorDialog::Init()
2330 {
2331 m_pCallingClass = (wxArrayStringProperty*) NULL;
2332 }
2333
2334 void wxPGArrayStringEditorDialog::OnCustomEditClick(wxCommandEvent& )
2335 {
2336 wxASSERT( m_pCallingClass );
2337 wxString str = m_edValue->GetValue();
2338 if ( m_pCallingClass->OnCustomStringEdit(m_parent,str) )
2339 {
2340 //m_edValue->SetValue ( str );
2341 m_lbStrings->Append ( str );
2342 m_array.Add ( str );
2343 m_modified = true;
2344 }
2345 }
2346
2347 // -----------------------------------------------------------------------
2348 // wxArrayStringProperty
2349 // -----------------------------------------------------------------------
2350
2351 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayStringProperty, // Property name
2352 wxPGProperty, // Property we inherit from
2353 wxArrayString, // Value type name
2354 const wxArrayString&, // Value type, as given in constructor
2355 TextCtrlAndButton) // Initial editor
2356
2357 wxArrayStringProperty::wxArrayStringProperty( const wxString& label,
2358 const wxString& name,
2359 const wxArrayString& array )
2360 : wxPGProperty(label,name)
2361 {
2362 SetValue( array );
2363 }
2364
2365 wxArrayStringProperty::~wxArrayStringProperty() { }
2366
2367 void wxArrayStringProperty::OnSetValue()
2368 {
2369 GenerateValueAsString();
2370 }
2371
2372 #define ARRSTRPROP_ARRAY_TO_STRING(STRING,ARRAY) \
2373 wxPropertyGrid::ArrayStringToString(STRING,ARRAY,wxS('"'),wxS('"'),1)
2374
2375 wxString wxArrayStringProperty::ValueToString( wxVariant& WXUNUSED(value),
2376 int argFlags ) const
2377 {
2378 //
2379 // If this is called from GetValueAsString(), return cached string
2380 if ( argFlags & wxPG_VALUE_IS_CURRENT )
2381 {
2382 return m_display;
2383 }
2384
2385 wxArrayString arr = m_value.GetArrayString();
2386 wxString s;
2387 ARRSTRPROP_ARRAY_TO_STRING(s, arr);
2388 return s;
2389 }
2390
2391 // Converts wxArrayString to a string separated by delimeters and spaces.
2392 // preDelim is useful for "str1" "str2" style. Set flags to 1 to do slash
2393 // conversion.
2394 void wxPropertyGrid::ArrayStringToString( wxString& dst, const wxArrayString& src,
2395 wxChar preDelim, wxChar postDelim,
2396 int flags )
2397 {
2398 wxString pdr;
2399
2400 unsigned int i;
2401 unsigned int itemCount = src.size();
2402
2403 wxChar preas[2] = { 0, 0 };
2404
2405 dst.Empty();
2406
2407 if ( flags & 1 )
2408 {
2409 preas[0] = preDelim;
2410 pdr = wxS("\\");
2411 pdr += preDelim;
2412 }
2413
2414 if ( itemCount )
2415 dst.append( preas );
2416
2417 wxASSERT( postDelim );
2418 wxString postDelimStr(postDelim);
2419 //wxString preDelimStr(preDelim);
2420
2421 for ( i = 0; i < itemCount; i++ )
2422 {
2423 wxString str( src.Item(i) );
2424
2425 // Do some character conversion.
2426 // Convertes \ to \\ and <preDelim> to \<preDelim>
2427 // Useful when preDelim and postDelim are "\"".
2428 if ( flags & 1 )
2429 {
2430 str.Replace( wxS("\\"), wxS("\\\\"), true );
2431 if ( pdr.length() )
2432 str.Replace( preas, pdr, true );
2433 }
2434
2435 dst.append( str );
2436
2437 if ( i < (itemCount-1) )
2438 {
2439 dst.append( postDelimStr );
2440 dst.append( wxS(" ") );
2441 dst.append( preas );
2442 }
2443 else if ( preDelim )
2444 dst.append( postDelimStr );
2445 }
2446 }
2447
2448 void wxArrayStringProperty::GenerateValueAsString()
2449 {
2450 wxArrayString arr = m_value.GetArrayString();
2451 ARRSTRPROP_ARRAY_TO_STRING(m_display, arr);
2452 }
2453
2454 // Default implementation doesn't do anything.
2455 bool wxArrayStringProperty::OnCustomStringEdit( wxWindow*, wxString& )
2456 {
2457 return false;
2458 }
2459
2460 wxArrayEditorDialog* wxArrayStringProperty::CreateEditorDialog()
2461 {
2462 return new wxPGArrayStringEditorDialog();
2463 }
2464
2465 bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid* propGrid,
2466 wxWindow* WXUNUSED(primaryCtrl),
2467 const wxChar* cbt )
2468 {
2469 // Update the value
2470 wxVariant useValue = propGrid->GetUncommittedPropertyValue();
2471
2472 if ( !propGrid->EditorValidate() )
2473 return false;
2474
2475 // Create editor dialog.
2476 wxArrayEditorDialog* dlg = CreateEditorDialog();
2477 #if wxUSE_VALIDATORS
2478 wxValidator* validator = GetValidator();
2479 wxPGInDialogValidator dialogValidator;
2480 #endif
2481
2482 wxPGArrayStringEditorDialog* strEdDlg = wxDynamicCast(dlg, wxPGArrayStringEditorDialog);
2483
2484 if ( strEdDlg )
2485 strEdDlg->SetCustomButton(cbt, this);
2486
2487 dlg->SetDialogValue( useValue );
2488 dlg->Create(propGrid, wxEmptyString, m_label);
2489
2490 #if !wxPG_SMALL_SCREEN
2491 dlg->Move( propGrid->GetGoodEditorDialogPosition(this,dlg->GetSize()) );
2492 #endif
2493
2494 bool retVal;
2495
2496 for (;;)
2497 {
2498 retVal = false;
2499
2500 int res = dlg->ShowModal();
2501
2502 if ( res == wxID_OK && dlg->IsModified() )
2503 {
2504 wxVariant value = dlg->GetDialogValue();
2505 if ( !value.IsNull() )
2506 {
2507 wxArrayString actualValue = value.GetArrayString();
2508 wxString tempStr;
2509 ARRSTRPROP_ARRAY_TO_STRING(tempStr, actualValue);
2510 #if wxUSE_VALIDATORS
2511 if ( dialogValidator.DoValidate( propGrid, validator, tempStr ) )
2512 #endif
2513 {
2514 SetValueInEvent( actualValue );
2515 retVal = true;
2516 break;
2517 }
2518 }
2519 else
2520 break;
2521 }
2522 else
2523 break;
2524 }
2525
2526 delete dlg;
2527
2528 return retVal;
2529 }
2530
2531 bool wxArrayStringProperty::OnEvent( wxPropertyGrid* propGrid,
2532 wxWindow* primary,
2533 wxEvent& event )
2534 {
2535 if ( propGrid->IsMainButtonEvent(event) )
2536 return OnButtonClick(propGrid,primary,(const wxChar*) NULL);
2537 return false;
2538 }
2539
2540 bool wxArrayStringProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const
2541 {
2542 wxArrayString arr;
2543
2544 WX_PG_TOKENIZER2_BEGIN(text,wxS('"'))
2545
2546 // Need to replace backslashes with empty characters
2547 // (opposite what is done in GenerateValueString).
2548 token.Replace ( wxS("\\"), wxEmptyString, true );
2549
2550 arr.Add( token );
2551
2552 WX_PG_TOKENIZER2_END()
2553
2554 variant = arr;
2555
2556 return true;
2557 }
2558
2559 // -----------------------------------------------------------------------
2560 // wxPGInDialogValidator
2561 // -----------------------------------------------------------------------
2562
2563 #if wxUSE_VALIDATORS
2564 bool wxPGInDialogValidator::DoValidate( wxPropertyGrid* propGrid,
2565 wxValidator* validator,
2566 const wxString& value )
2567 {
2568 if ( !validator )
2569 return true;
2570
2571 wxTextCtrl* tc = m_textCtrl;
2572
2573 if ( !tc )
2574 {
2575 {
2576 tc = new wxTextCtrl( propGrid, wxPG_SUBID_TEMP1, wxEmptyString,
2577 wxPoint(30000,30000));
2578 tc->Hide();
2579 }
2580
2581 m_textCtrl = tc;
2582 }
2583
2584 tc->SetValue(value);
2585
2586 validator->SetWindow(tc);
2587 bool res = validator->Validate(propGrid);
2588
2589 return res;
2590 }
2591 #else
2592 bool wxPGInDialogValidator::DoValidate( wxPropertyGrid* WXUNUSED(propGrid),
2593 wxValidator* WXUNUSED(validator),
2594 const wxString& WXUNUSED(value) )
2595 {
2596 return true;
2597 }
2598 #endif
2599
2600 // -----------------------------------------------------------------------
2601
2602 #endif // wxUSE_PROPGRID