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