]>
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; | |
c82a80e8 | 82 | DoGenerateComposedValue(s); |
1c4293cb VZ |
83 | m_value = s; |
84 | } | |
85 | } | |
86 | ||
87 | wxStringProperty::~wxStringProperty() { } | |
88 | ||
1425eca5 JS |
89 | wxString wxStringProperty::ValueToString( wxVariant& value, |
90 | int argFlags ) const | |
1c4293cb | 91 | { |
1425eca5 | 92 | wxString s = value.GetString(); |
1c4293cb VZ |
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) ) | |
1425eca5 JS |
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 | ||
c82a80e8 | 104 | DoGenerateComposedValue(s, argFlags); |
1425eca5 | 105 | } |
1c4293cb VZ |
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 | ||
0c931dd4 | 123 | if ( variant != text ) |
1c4293cb VZ |
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 | { | |
0372d42e | 160 | SetValue(WXVARIANT(value)); |
1c4293cb VZ |
161 | } |
162 | ||
163 | wxIntProperty::~wxIntProperty() { } | |
164 | ||
1425eca5 JS |
165 | wxString wxIntProperty::ValueToString( wxVariant& value, |
166 | int WXUNUSED(argFlags) ) const | |
1c4293cb | 167 | { |
1425eca5 | 168 | if ( value.GetType() == wxPG_VARIANT_TYPE_LONG ) |
0372d42e | 169 | { |
1425eca5 | 170 | return wxString::Format(wxS("%li"),value.GetLong()); |
0372d42e | 171 | } |
1425eca5 | 172 | else if ( value.GetType() == wxLongLong_VariantType ) |
0372d42e JS |
173 | { |
174 | wxLongLong ll; | |
1425eca5 | 175 | ll << value; |
0372d42e JS |
176 | return ll.ToString(); |
177 | } | |
1c4293cb | 178 | |
0372d42e | 179 | return wxEmptyString; |
1c4293cb VZ |
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 | ||
b7bc9d80 | 203 | for ( ; i != iMax; ++i ) |
1c4293cb VZ |
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 | ||
0372d42e JS |
213 | wxString variantType = variant.GetType(); |
214 | bool isPrevLong = variantType == wxPG_VARIANT_TYPE_LONG; | |
1c4293cb VZ |
215 | |
216 | wxLongLong_t value64 = 0; | |
217 | ||
218 | if ( useText.ToLongLong(&value64, 10) && | |
219 | ( value64 >= INT_MAX || value64 <= INT_MIN ) | |
220 | ) | |
221 | { | |
0372d42e JS |
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 ) | |
1c4293cb | 233 | { |
0372d42e JS |
234 | wxLongLong ll(value64); |
235 | variant << ll; | |
1c4293cb VZ |
236 | return true; |
237 | } | |
238 | } | |
239 | ||
240 | if ( useText.ToLong( &value32, 0 ) ) | |
241 | { | |
0c931dd4 | 242 | if ( !isPrevLong || variant != value32 ) |
1c4293cb VZ |
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 | { | |
0c931dd4 | 257 | if ( variant.GetType() != wxPG_VARIANT_TYPE_LONG || variant != (long)value ) |
1c4293cb VZ |
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 ) | |
2a8312bc JS |
293 | pValidationInfo->SetFailureMessage( |
294 | wxString::Format(_("Value must be %lld or higher"),min) | |
295 | ); | |
1c4293cb VZ |
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 ) | |
2a8312bc JS |
309 | pValidationInfo->SetFailureMessage( |
310 | wxString::Format(_("Value must be %lld or higher"),min) | |
311 | ); | |
1c4293cb VZ |
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(); | |
0372d42e | 395 | SetValue(WXVARIANT(value)); |
1c4293cb VZ |
396 | } |
397 | ||
398 | wxUIntProperty::~wxUIntProperty() { } | |
399 | ||
1425eca5 JS |
400 | wxString wxUIntProperty::ValueToString( wxVariant& value, |
401 | int WXUNUSED(argFlags) ) const | |
1c4293cb VZ |
402 | { |
403 | size_t index = m_base + m_prefix; | |
404 | if ( index >= wxPG_UINT_TEMPLATE_MAX ) | |
405 | index = wxPG_BASE_DEC; | |
406 | ||
1425eca5 | 407 | if ( value.GetType() == wxPG_VARIANT_TYPE_LONG ) |
0372d42e | 408 | { |
1425eca5 | 409 | return wxString::Format(gs_uintTemplates32[index], (unsigned long)value.GetLong()); |
0372d42e JS |
410 | } |
411 | ||
412 | wxULongLong ull; | |
1425eca5 | 413 | ull << value; |
0372d42e JS |
414 | |
415 | return wxString::Format(gs_uintTemplates64[index], ull.GetValue()); | |
1c4293cb VZ |
416 | } |
417 | ||
418 | bool wxUIntProperty::StringToValue( wxVariant& variant, const wxString& text, int WXUNUSED(argFlags) ) const | |
419 | { | |
0372d42e JS |
420 | wxString variantType = variant.GetType(); |
421 | bool isPrevLong = variantType == wxPG_VARIANT_TYPE_LONG; | |
1c4293cb VZ |
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 | { | |
0372d42e JS |
440 | bool doChangeValue = isPrevLong; |
441 | ||
442 | if ( !isPrevLong && variantType == wxULongLong_VariantType ) | |
1c4293cb | 443 | { |
0372d42e JS |
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; | |
1c4293cb VZ |
454 | return true; |
455 | } | |
456 | } | |
457 | else | |
458 | { | |
459 | unsigned long value32 = wxLongLong(value64).GetLo(); | |
0c931dd4 | 460 | if ( !isPrevLong || m_value != (long)value32 ) |
1c4293cb VZ |
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 | { | |
0c931dd4 | 473 | if ( variant != (long)number ) |
1c4293cb VZ |
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 | { | |
2a8312bc JS |
505 | validationInfo.SetFailureMessage( |
506 | wxString::Format(_("Value must be %llu or higher"),min) | |
507 | ); | |
1c4293cb VZ |
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 | { | |
2a8312bc JS |
517 | validationInfo.SetFailureMessage( |
518 | wxString::Format(_("Value must be %llu or less"),max) | |
519 | ); | |
1c4293cb VZ |
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 | ||
b7bc9d80 | 609 | for ( ; i != target.begin(); --i ) |
1c4293cb VZ |
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 | ||
1425eca5 JS |
625 | wxString wxFloatProperty::ValueToString( wxVariant& value, |
626 | int argFlags ) const | |
1c4293cb VZ |
627 | { |
628 | wxString text; | |
1425eca5 | 629 | if ( !value.IsNull() ) |
1c4293cb VZ |
630 | { |
631 | wxPropertyGrid::DoubleToString(text, | |
1425eca5 | 632 | value, |
1c4293cb VZ |
633 | m_precision, |
634 | !(argFlags & wxPG_FULL_VALUE), | |
d3b9f782 | 635 | NULL); |
1c4293cb VZ |
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 | { | |
0c931dd4 | 654 | if ( variant != value ) |
1c4293cb VZ |
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 ) | |
2a8312bc JS |
694 | pValidationInfo->SetFailureMessage( |
695 | wxString::Format(_("Value must be %f or higher"),min) | |
696 | ); | |
1c4293cb VZ |
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 ) | |
2a8312bc JS |
711 | pValidationInfo->SetFailureMessage( |
712 | wxString::Format(_("Value must be %f or less"),max) | |
713 | ); | |
1c4293cb VZ |
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) ) | |
c26873c8 JS |
761 | return wxPGEditor_Choice; |
762 | return wxPGEditor_CheckBox; | |
1c4293cb | 763 | #else |
c26873c8 | 764 | return wxPGEditor_Choice; |
1c4293cb VZ |
765 | #endif |
766 | } | |
767 | ||
768 | wxBoolProperty::wxBoolProperty( const wxString& label, const wxString& name, bool value ) : | |
769 | wxPGProperty(label,name) | |
770 | { | |
939d9364 JS |
771 | m_choices.Assign(wxPGGlobalVars->m_boolChoices); |
772 | ||
1c4293cb VZ |
773 | SetValue(wxPGVariant_Bool(value)); |
774 | ||
775 | m_flags |= wxPG_PROP_USE_DCC; | |
776 | } | |
777 | ||
778 | wxBoolProperty::~wxBoolProperty() { } | |
779 | ||
1425eca5 JS |
780 | wxString wxBoolProperty::ValueToString( wxVariant& value, |
781 | int argFlags ) const | |
1c4293cb | 782 | { |
1425eca5 | 783 | bool boolValue = value.GetBool(); |
1c4293cb VZ |
784 | |
785 | // As a fragment of composite string value, | |
786 | // make it a little more readable. | |
787 | if ( argFlags & wxPG_COMPOSITE_FRAGMENT ) | |
788 | { | |
1425eca5 | 789 | if ( boolValue ) |
1c4293cb VZ |
790 | { |
791 | return m_label; | |
792 | } | |
793 | else | |
794 | { | |
795 | if ( argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT ) | |
796 | return wxEmptyString; | |
797 | ||
18b5bcb0 | 798 | wxString notFmt; |
1c4293cb VZ |
799 | if ( wxPGGlobalVars->m_autoGetTranslation ) |
800 | notFmt = _("Not %s"); | |
801 | else | |
18b5bcb0 | 802 | notFmt = wxS("Not %s"); |
1c4293cb | 803 | |
18b5bcb0 | 804 | return wxString::Format(notFmt.c_str(), m_label.c_str()); |
1c4293cb VZ |
805 | } |
806 | } | |
807 | ||
808 | if ( !(argFlags & wxPG_FULL_VALUE) ) | |
809 | { | |
1425eca5 | 810 | return wxPGGlobalVars->m_boolChoices[boolValue?1:0].GetText(); |
1c4293cb VZ |
811 | } |
812 | ||
813 | wxString text; | |
814 | ||
1425eca5 | 815 | if ( boolValue ) text = wxS("true"); |
1c4293cb VZ |
816 | else text = wxS("false"); |
817 | ||
818 | return text; | |
819 | } | |
820 | ||
1c4293cb VZ |
821 | bool wxBoolProperty::StringToValue( wxVariant& variant, const wxString& text, int WXUNUSED(argFlags) ) const |
822 | { | |
0c931dd4 | 823 | bool boolValue = false; |
1c4293cb VZ |
824 | if ( text.CmpNoCase(wxPGGlobalVars->m_boolChoices[1].GetText()) == 0 || |
825 | text.CmpNoCase(wxS("true")) == 0 || | |
826 | text.CmpNoCase(m_label) == 0 ) | |
0c931dd4 | 827 | boolValue = true; |
1c4293cb VZ |
828 | |
829 | if ( text.length() == 0 ) | |
830 | { | |
831 | variant.MakeNull(); | |
832 | return true; | |
833 | } | |
834 | ||
0c931dd4 | 835 | if ( variant != boolValue ) |
1c4293cb | 836 | { |
0c931dd4 | 837 | variant = wxPGVariant_Bool(boolValue); |
1c4293cb VZ |
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; | |
1c4293cb | 846 | |
0c931dd4 | 847 | if ( variant != boolValue ) |
1c4293cb VZ |
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 | // ----------------------------------------------------------------------- | |
78f2d746 | 881 | // wxEnumProperty |
1c4293cb VZ |
882 | // ----------------------------------------------------------------------- |
883 | ||
78f2d746 JS |
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 | } | |
1c4293cb | 938 | |
78f2d746 JS |
939 | wxEnumProperty::wxEnumProperty( const wxString& label, const wxString& name, |
940 | wxPGChoices& choices, int value ) | |
1c4293cb VZ |
941 | : wxPGProperty(label,name) |
942 | { | |
78f2d746 JS |
943 | m_choices.Assign( choices ); |
944 | ||
945 | if ( GetItemCount() ) | |
946 | SetValue( (long)value ); | |
1c4293cb VZ |
947 | } |
948 | ||
78f2d746 | 949 | int wxEnumProperty::GetIndexForValue( int value ) const |
1c4293cb | 950 | { |
78f2d746 JS |
951 | if ( !m_choices.IsOk() ) |
952 | return -1; | |
953 | ||
954 | int intVal = m_choices.Index(value); | |
955 | if ( intVal >= 0 ) | |
956 | return intVal; | |
957 | ||
1c4293cb VZ |
958 | return value; |
959 | } | |
960 | ||
78f2d746 JS |
961 | wxEnumProperty::~wxEnumProperty () |
962 | { | |
963 | } | |
964 | ||
965 | int wxEnumProperty::ms_nextIndex = -2; | |
966 | ||
967 | void wxEnumProperty::OnSetValue() | |
1c4293cb | 968 | { |
0372d42e JS |
969 | wxString variantType = m_value.GetType(); |
970 | ||
971 | if ( variantType == wxPG_VARIANT_TYPE_LONG ) | |
1c4293cb | 972 | ValueFromInt_( m_value, m_value.GetLong(), wxPG_FULL_VALUE ); |
0372d42e | 973 | else if ( variantType == wxPG_VARIANT_TYPE_STRING ) |
1c4293cb VZ |
974 | ValueFromString_( m_value, m_value.GetString(), 0 ); |
975 | else | |
b7bc9d80 | 976 | wxFAIL; |
1c4293cb VZ |
977 | |
978 | if ( ms_nextIndex != -2 ) | |
979 | { | |
980 | m_index = ms_nextIndex; | |
981 | ms_nextIndex = -2; | |
982 | } | |
983 | } | |
984 | ||
78f2d746 | 985 | bool wxEnumProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& WXUNUSED(validationInfo) ) const |
1c4293cb VZ |
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 | |
0372d42e | 990 | if ( value.GetType() == wxPG_VARIANT_TYPE_STRING && |
1c4293cb VZ |
991 | !this->IsKindOf(CLASSINFO(wxEditEnumProperty)) ) |
992 | return ValueFromString_( value, value.GetString(), wxPG_PROPERTY_SPECIFIC ); | |
993 | ||
994 | return true; | |
995 | } | |
996 | ||
78f2d746 | 997 | wxString wxEnumProperty::ValueToString( wxVariant& value, |
1425eca5 | 998 | int WXUNUSED(argFlags) ) const |
1c4293cb | 999 | { |
1425eca5 JS |
1000 | if ( value.GetType() == wxPG_VARIANT_TYPE_STRING ) |
1001 | return value.GetString(); | |
1c4293cb | 1002 | |
1425eca5 JS |
1003 | int index = m_choices.Index(value.GetLong()); |
1004 | if ( index < 0 ) | |
1005 | return wxEmptyString; | |
1c4293cb | 1006 | |
1425eca5 | 1007 | return m_choices.GetLabel(index); |
1c4293cb VZ |
1008 | } |
1009 | ||
78f2d746 | 1010 | bool wxEnumProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const |
1c4293cb VZ |
1011 | { |
1012 | return ValueFromString_( variant, text, argFlags ); | |
1013 | } | |
1014 | ||
78f2d746 | 1015 | bool wxEnumProperty::IntToValue( wxVariant& variant, int intVal, int argFlags ) const |
1c4293cb VZ |
1016 | { |
1017 | return ValueFromInt_( variant, intVal, argFlags ); | |
1018 | } | |
1019 | ||
78f2d746 | 1020 | bool wxEnumProperty::ValueFromString_( wxVariant& value, const wxString& text, int argFlags ) const |
1c4293cb | 1021 | { |
1c4293cb VZ |
1022 | int useIndex = -1; |
1023 | long useValue = 0; | |
1024 | ||
78f2d746 | 1025 | for ( unsigned int i=0; i<m_choices.GetCount(); i++ ) |
1c4293cb | 1026 | { |
78f2d746 JS |
1027 | const wxString& entryLabel = m_choices.GetLabel(i); |
1028 | if ( text.CmpNoCase(entryLabel) == 0 ) | |
1c4293cb VZ |
1029 | { |
1030 | useIndex = (int)i; | |
78f2d746 | 1031 | useValue = m_choices.GetValue(i); |
1c4293cb VZ |
1032 | break; |
1033 | } | |
1c4293cb VZ |
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 && | |
0372d42e | 1043 | (value.GetType() != wxPG_VARIANT_TYPE_STRING || (m_value.GetString() != text)) && |
1c4293cb VZ |
1044 | isEdit ) |
1045 | { | |
1046 | asText = true; | |
1047 | } | |
1048 | ||
1049 | int setAsNextIndex = -2; | |
1050 | ||
1051 | if ( asText ) | |
1052 | { | |
1053 | setAsNextIndex = -1; | |
1054 | value = text; | |
1055 | } | |
31f0b483 | 1056 | else if ( useIndex != GetIndex() ) |
1c4293cb VZ |
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 | ||
78f2d746 | 1085 | bool wxEnumProperty::ValueFromInt_( wxVariant& variant, int intVal, int argFlags ) const |
1c4293cb VZ |
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 | { | |
31f0b483 | 1097 | if ( intVal != GetIndex() ) |
1c4293cb VZ |
1098 | { |
1099 | ms_nextIndex = intVal; | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | if ( ms_nextIndex != -2 ) | |
1104 | { | |
1105 | if ( !(argFlags & wxPG_FULL_VALUE) ) | |
78f2d746 | 1106 | intVal = m_choices.GetValue(intVal); |
1c4293cb VZ |
1107 | |
1108 | variant = (long)intVal; | |
1109 | ||
1110 | return true; | |
1111 | } | |
1112 | ||
1113 | return false; | |
1114 | } | |
1115 | ||
d8812c6e JS |
1116 | void |
1117 | wxEnumProperty::OnValidationFailure( wxVariant& WXUNUSED(pendingValue) ) | |
1118 | { | |
1119 | // Revert index | |
d8812c6e JS |
1120 | ResetNextIndex(); |
1121 | } | |
1122 | ||
78f2d746 | 1123 | void wxEnumProperty::SetIndex( int index ) |
1c4293cb VZ |
1124 | { |
1125 | ms_nextIndex = -2; | |
1126 | m_index = index; | |
1127 | } | |
1128 | ||
78f2d746 | 1129 | int wxEnumProperty::GetIndex() const |
1c4293cb | 1130 | { |
31f0b483 JS |
1131 | if ( m_value.IsNull() ) |
1132 | return -1; | |
1133 | ||
1c4293cb VZ |
1134 | if ( ms_nextIndex != -2 ) |
1135 | return ms_nextIndex; | |
31f0b483 | 1136 | |
1c4293cb VZ |
1137 | return m_index; |
1138 | } | |
1139 | ||
1c4293cb VZ |
1140 | // ----------------------------------------------------------------------- |
1141 | // wxEditEnumProperty | |
1142 | // ----------------------------------------------------------------------- | |
1143 | ||
1144 | IMPLEMENT_DYNAMIC_CLASS(wxEditEnumProperty, wxPGProperty) | |
1145 | ||
1146 | WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEditEnumProperty,wxString,ComboBox) | |
1147 | ||
1148 | wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name, const wxChar** labels, | |
1149 | const long* values, const wxString& value ) | |
1150 | : wxEnumProperty(label,name,labels,values,0) | |
1151 | { | |
1152 | SetValue( value ); | |
1153 | } | |
1154 | ||
1155 | wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name, const wxChar** labels, | |
1156 | const long* values, wxPGChoices* choicesCache, const wxString& value ) | |
1157 | : wxEnumProperty(label,name,labels,values,choicesCache,0) | |
1158 | { | |
1159 | SetValue( value ); | |
1160 | } | |
1161 | ||
1162 | wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name, | |
1163 | const wxArrayString& labels, const wxArrayInt& values, const wxString& value ) | |
1164 | : wxEnumProperty(label,name,labels,values,0) | |
1165 | { | |
1166 | SetValue( value ); | |
1167 | } | |
1168 | ||
1169 | wxEditEnumProperty::wxEditEnumProperty( const wxString& label, const wxString& name, | |
1170 | wxPGChoices& choices, const wxString& value ) | |
1171 | : wxEnumProperty(label,name,choices,0) | |
1172 | { | |
1173 | SetValue( value ); | |
1174 | } | |
1175 | ||
1176 | wxEditEnumProperty::~wxEditEnumProperty() | |
1177 | { | |
1178 | } | |
1179 | ||
1180 | // ----------------------------------------------------------------------- | |
1181 | // wxFlagsProperty | |
1182 | // ----------------------------------------------------------------------- | |
1183 | ||
1184 | IMPLEMENT_DYNAMIC_CLASS(wxFlagsProperty,wxPGProperty) | |
1185 | ||
1186 | WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxFlagsProperty,long,TextCtrl) | |
1187 | ||
1188 | void wxFlagsProperty::Init() | |
1189 | { | |
1c4293cb VZ |
1190 | long value = m_value; |
1191 | ||
1192 | // | |
1193 | // Generate children | |
1194 | // | |
1195 | unsigned int i; | |
1196 | ||
63f62e76 | 1197 | unsigned int prevChildCount = m_children.size(); |
1c4293cb VZ |
1198 | |
1199 | int oldSel = -1; | |
1200 | if ( prevChildCount ) | |
1201 | { | |
1202 | wxPropertyGridPageState* state = GetParentState(); | |
1203 | ||
1204 | // State safety check (it may be NULL in immediate parent) | |
1205 | wxASSERT( state ); | |
1206 | ||
1207 | if ( state ) | |
1208 | { | |
1209 | wxPGProperty* selected = state->GetSelection(); | |
1210 | if ( selected ) | |
1211 | { | |
1212 | if ( selected->GetParent() == this ) | |
b96a14e3 | 1213 | oldSel = selected->GetIndexInParent(); |
1c4293cb VZ |
1214 | else if ( selected == this ) |
1215 | oldSel = -2; | |
1216 | } | |
1217 | } | |
1218 | state->DoClearSelection(); | |
1219 | } | |
1220 | ||
1221 | // Delete old children | |
1222 | for ( i=0; i<prevChildCount; i++ ) | |
63f62e76 | 1223 | delete m_children[i]; |
1c4293cb | 1224 | |
63f62e76 | 1225 | m_children.clear(); |
1c4293cb VZ |
1226 | |
1227 | if ( m_choices.IsOk() ) | |
1228 | { | |
1229 | const wxPGChoices& choices = m_choices; | |
1230 | ||
1231 | for ( i=0; i<GetItemCount(); i++ ) | |
1232 | { | |
1233 | bool child_val; | |
98c04633 | 1234 | child_val = ( value & choices.GetValue(i) )?true:false; |
1c4293cb VZ |
1235 | |
1236 | wxPGProperty* boolProp; | |
d665918b | 1237 | wxString label = GetLabel(i); |
1c4293cb VZ |
1238 | |
1239 | #if wxUSE_INTL | |
1240 | if ( wxPGGlobalVars->m_autoGetTranslation ) | |
1241 | { | |
d665918b | 1242 | boolProp = new wxBoolProperty( ::wxGetTranslation(label), label, child_val ); |
1c4293cb VZ |
1243 | } |
1244 | else | |
1245 | #endif | |
1246 | { | |
d665918b | 1247 | boolProp = new wxBoolProperty( label, label, child_val ); |
1c4293cb | 1248 | } |
48a32cf6 | 1249 | AddPrivateChild(boolProp); |
1c4293cb VZ |
1250 | } |
1251 | ||
1252 | m_oldChoicesData = m_choices.GetDataPtr(); | |
1253 | } | |
1254 | ||
1255 | m_oldValue = m_value; | |
1256 | ||
1257 | if ( prevChildCount ) | |
1258 | SubPropsChanged(oldSel); | |
1259 | } | |
1260 | ||
1261 | wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name, | |
1262 | const wxChar** labels, const long* values, long value ) : wxPGProperty(label,name) | |
1263 | { | |
d3b9f782 | 1264 | m_oldChoicesData = NULL; |
1c4293cb VZ |
1265 | |
1266 | if ( labels ) | |
1267 | { | |
1268 | m_choices.Set(labels,values); | |
1269 | ||
1270 | wxASSERT( GetItemCount() ); | |
1271 | ||
1272 | SetValue( value ); | |
1273 | } | |
1274 | else | |
1275 | { | |
1276 | m_value = wxPGVariant_Zero; | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name, | |
1281 | const wxArrayString& labels, const wxArrayInt& values, int value ) | |
1282 | : wxPGProperty(label,name) | |
1283 | { | |
d3b9f782 | 1284 | m_oldChoicesData = NULL; |
1c4293cb VZ |
1285 | |
1286 | if ( &labels && labels.size() ) | |
1287 | { | |
1288 | m_choices.Set(labels,values); | |
1289 | ||
1290 | wxASSERT( GetItemCount() ); | |
1291 | ||
1292 | SetValue( (long)value ); | |
1293 | } | |
1294 | else | |
1295 | { | |
1296 | m_value = wxPGVariant_Zero; | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | wxFlagsProperty::wxFlagsProperty( const wxString& label, const wxString& name, | |
1301 | wxPGChoices& choices, long value ) | |
1302 | : wxPGProperty(label,name) | |
1303 | { | |
d3b9f782 | 1304 | m_oldChoicesData = NULL; |
1c4293cb VZ |
1305 | |
1306 | if ( choices.IsOk() ) | |
1307 | { | |
1308 | m_choices.Assign(choices); | |
1309 | ||
1310 | wxASSERT( GetItemCount() ); | |
1311 | ||
1312 | SetValue( value ); | |
1313 | } | |
1314 | else | |
1315 | { | |
1316 | m_value = wxPGVariant_Zero; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | wxFlagsProperty::~wxFlagsProperty() | |
1321 | { | |
1322 | } | |
1323 | ||
1324 | void wxFlagsProperty::OnSetValue() | |
1325 | { | |
1326 | if ( !m_choices.IsOk() || !GetItemCount() ) | |
1327 | { | |
1328 | m_value = wxPGVariant_Zero; | |
1329 | } | |
1330 | else | |
1331 | { | |
1332 | long val = m_value.GetLong(); | |
1333 | ||
1334 | long fullFlags = 0; | |
1335 | ||
1336 | // normalize the value (i.e. remove extra flags) | |
1337 | unsigned int i; | |
1338 | const wxPGChoices& choices = m_choices; | |
1339 | for ( i = 0; i < GetItemCount(); i++ ) | |
1340 | { | |
98c04633 | 1341 | fullFlags |= choices.GetValue(i); |
1c4293cb VZ |
1342 | } |
1343 | ||
1344 | val &= fullFlags; | |
1345 | ||
1346 | m_value = val; | |
1347 | ||
1348 | // Need to (re)init now? | |
1349 | if ( GetChildCount() != GetItemCount() || | |
1350 | m_choices.GetDataPtr() != m_oldChoicesData ) | |
1351 | { | |
1352 | Init(); | |
1353 | } | |
1354 | } | |
1355 | ||
1356 | long newFlags = m_value; | |
1357 | ||
1358 | if ( newFlags != m_oldValue ) | |
1359 | { | |
1360 | // Set child modified states | |
1361 | unsigned int i; | |
1362 | const wxPGChoices& choices = m_choices; | |
1363 | for ( i = 0; i<GetItemCount(); i++ ) | |
1364 | { | |
1365 | int flag; | |
1366 | ||
98c04633 | 1367 | flag = choices.GetValue(i); |
1c4293cb VZ |
1368 | |
1369 | if ( (newFlags & flag) != (m_oldValue & flag) ) | |
1370 | Item(i)->SetFlag( wxPG_PROP_MODIFIED ); | |
1371 | } | |
1372 | ||
1373 | m_oldValue = newFlags; | |
1374 | } | |
1375 | } | |
1376 | ||
1425eca5 JS |
1377 | wxString wxFlagsProperty::ValueToString( wxVariant& value, |
1378 | int WXUNUSED(argFlags) ) const | |
1c4293cb VZ |
1379 | { |
1380 | wxString text; | |
1381 | ||
1382 | if ( !m_choices.IsOk() ) | |
1383 | return text; | |
1384 | ||
1425eca5 | 1385 | long flags = value; |
1c4293cb VZ |
1386 | unsigned int i; |
1387 | const wxPGChoices& choices = m_choices; | |
1388 | ||
1389 | for ( i = 0; i < GetItemCount(); i++ ) | |
1390 | { | |
1391 | int doAdd; | |
98c04633 | 1392 | doAdd = ( flags & choices.GetValue(i) ); |
1c4293cb VZ |
1393 | |
1394 | if ( doAdd ) | |
1395 | { | |
1396 | text += choices.GetLabel(i); | |
1397 | text += wxS(", "); | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | // remove last comma | |
1402 | if ( text.Len() > 1 ) | |
1403 | text.Truncate ( text.Len() - 2 ); | |
1404 | ||
1405 | return text; | |
1406 | } | |
1407 | ||
1408 | // Translate string into flag tokens | |
1409 | bool wxFlagsProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const | |
1410 | { | |
1411 | if ( !m_choices.IsOk() ) | |
1412 | return false; | |
1413 | ||
1414 | long newFlags = 0; | |
1c4293cb VZ |
1415 | |
1416 | // semicolons are no longer valid delimeters | |
1417 | WX_PG_TOKENIZER1_BEGIN(text,wxS(',')) | |
1418 | ||
1419 | if ( token.length() ) | |
1420 | { | |
1421 | // Determine which one it is | |
1422 | long bit = IdToBit( token ); | |
1423 | ||
1424 | if ( bit != -1 ) | |
1425 | { | |
1426 | // Changed? | |
1427 | newFlags |= bit; | |
1428 | } | |
1429 | else | |
1430 | { | |
1431 | break; | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | WX_PG_TOKENIZER1_END() | |
1436 | ||
0c931dd4 JS |
1437 | if ( variant != (long)newFlags ) |
1438 | { | |
1439 | variant = (long)newFlags; | |
1c4293cb | 1440 | return true; |
0c931dd4 | 1441 | } |
1c4293cb VZ |
1442 | |
1443 | return false; | |
1444 | } | |
1445 | ||
1446 | // Converts string id to a relevant bit. | |
1447 | long wxFlagsProperty::IdToBit( const wxString& id ) const | |
1448 | { | |
1449 | unsigned int i; | |
1450 | for ( i = 0; i < GetItemCount(); i++ ) | |
1451 | { | |
1452 | if ( id == GetLabel(i) ) | |
1453 | { | |
98c04633 | 1454 | return m_choices.GetValue(i); |
1c4293cb VZ |
1455 | } |
1456 | } | |
1457 | return -1; | |
1458 | } | |
1459 | ||
1460 | void wxFlagsProperty::RefreshChildren() | |
1461 | { | |
1462 | if ( !m_choices.IsOk() || !GetChildCount() ) return; | |
1463 | ||
1464 | int flags = m_value.GetLong(); | |
1465 | ||
1466 | const wxPGChoices& choices = m_choices; | |
1467 | unsigned int i; | |
1468 | for ( i = 0; i < GetItemCount(); i++ ) | |
1469 | { | |
1470 | long flag; | |
1471 | ||
98c04633 | 1472 | flag = choices.GetValue(i); |
1c4293cb VZ |
1473 | |
1474 | long subVal = flags & flag; | |
1475 | wxPGProperty* p = Item(i); | |
1476 | ||
1477 | if ( subVal != (m_oldValue & flag) ) | |
1478 | p->SetFlag( wxPG_PROP_MODIFIED ); | |
1479 | ||
1480 | p->SetValue( subVal?true:false ); | |
1481 | } | |
1482 | ||
1483 | m_oldValue = flags; | |
1484 | } | |
1485 | ||
1486 | void wxFlagsProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const | |
1487 | { | |
1488 | long oldValue = thisValue.GetLong(); | |
1489 | long val = childValue.GetLong(); | |
98c04633 | 1490 | unsigned long vi = m_choices.GetValue(childIndex); |
1c4293cb VZ |
1491 | if ( val ) |
1492 | thisValue = (long)(oldValue | vi); | |
1493 | else | |
1494 | thisValue = (long)(oldValue & ~(vi)); | |
1495 | } | |
1496 | ||
1c4293cb VZ |
1497 | // ----------------------------------------------------------------------- |
1498 | // wxDirProperty | |
1499 | // ----------------------------------------------------------------------- | |
1500 | ||
d53f610c | 1501 | IMPLEMENT_DYNAMIC_CLASS(wxDirProperty, wxLongStringProperty) |
1c4293cb VZ |
1502 | |
1503 | wxDirProperty::wxDirProperty( const wxString& name, const wxString& label, const wxString& value ) | |
1504 | : wxLongStringProperty(name,label,value) | |
1505 | { | |
3a89adc1 | 1506 | m_flags |= wxPG_PROP_NO_ESCAPE; |
1c4293cb | 1507 | } |
3a89adc1 | 1508 | |
1c4293cb VZ |
1509 | wxDirProperty::~wxDirProperty() { } |
1510 | ||
1511 | wxValidator* wxDirProperty::DoGetValidator() const | |
1512 | { | |
1513 | return wxFileProperty::GetClassValidator(); | |
1514 | } | |
1515 | ||
1516 | bool wxDirProperty::OnButtonClick( wxPropertyGrid* propGrid, wxString& value ) | |
1517 | { | |
3a89adc1 | 1518 | // Update property value from editor, if necessary |
1c4293cb VZ |
1519 | wxSize dlg_sz(300,400); |
1520 | ||
bd8b65be VZ |
1521 | wxString dlgMessage(m_dlgMessage); |
1522 | if ( dlgMessage.empty() ) | |
1523 | dlgMessage = _("Choose a directory:"); | |
1c4293cb | 1524 | wxDirDialog dlg( propGrid, |
bd8b65be | 1525 | dlgMessage, |
1c4293cb VZ |
1526 | value, |
1527 | 0, | |
1528 | #if !wxPG_SMALL_SCREEN | |
1529 | propGrid->GetGoodEditorDialogPosition(this,dlg_sz), | |
bd8b65be | 1530 | dlg_sz |
1c4293cb VZ |
1531 | #else |
1532 | wxDefaultPosition, | |
bd8b65be | 1533 | wxDefaultSize |
1c4293cb | 1534 | #endif |
bd8b65be | 1535 | ); |
1c4293cb VZ |
1536 | |
1537 | if ( dlg.ShowModal() == wxID_OK ) | |
1538 | { | |
1539 | value = dlg.GetPath(); | |
1540 | return true; | |
1541 | } | |
1542 | return false; | |
1543 | } | |
1544 | ||
1545 | bool wxDirProperty::DoSetAttribute( const wxString& name, wxVariant& value ) | |
1546 | { | |
1547 | if ( name == wxPG_DIR_DIALOG_MESSAGE ) | |
1548 | { | |
1549 | m_dlgMessage = value.GetString(); | |
1550 | return true; | |
1551 | } | |
1552 | return false; | |
1553 | } | |
1554 | ||
1555 | // ----------------------------------------------------------------------- | |
1556 | // wxPGFileDialogAdapter | |
1557 | // ----------------------------------------------------------------------- | |
1558 | ||
1559 | bool wxPGFileDialogAdapter::DoShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property ) | |
1560 | { | |
1561 | wxFileProperty* fileProp = NULL; | |
1562 | wxString path; | |
1563 | int indFilter = -1; | |
1564 | ||
1565 | if ( property->IsKindOf(CLASSINFO(wxFileProperty)) ) | |
1566 | { | |
1567 | fileProp = ((wxFileProperty*)property); | |
1425eca5 JS |
1568 | wxFileName filename = fileProp->GetValue().GetString(); |
1569 | path = filename.GetPath(); | |
1c4293cb VZ |
1570 | indFilter = fileProp->m_indFilter; |
1571 | ||
1572 | if ( !path.length() && fileProp->m_basePath.length() ) | |
1573 | path = fileProp->m_basePath; | |
1574 | } | |
1575 | else | |
1576 | { | |
1577 | wxFileName fn(property->GetValue().GetString()); | |
1578 | path = fn.GetPath(); | |
1579 | } | |
1580 | ||
1581 | wxFileDialog dlg( propGrid->GetPanel(), | |
1582 | property->GetAttribute(wxS("DialogTitle"), _("Choose a file")), | |
1583 | property->GetAttribute(wxS("InitialPath"), path), | |
1584 | wxEmptyString, | |
1585 | property->GetAttribute(wxPG_FILE_WILDCARD, _("All files (*.*)|*.*")), | |
1586 | 0, | |
1587 | wxDefaultPosition ); | |
1588 | ||
1589 | if ( indFilter >= 0 ) | |
1590 | dlg.SetFilterIndex( indFilter ); | |
1591 | ||
1592 | if ( dlg.ShowModal() == wxID_OK ) | |
1593 | { | |
1594 | if ( fileProp ) | |
1595 | fileProp->m_indFilter = dlg.GetFilterIndex(); | |
1596 | SetValue( dlg.GetPath() ); | |
1597 | return true; | |
1598 | } | |
1599 | return false; | |
1600 | } | |
1601 | ||
1602 | // ----------------------------------------------------------------------- | |
1603 | // wxFileProperty | |
1604 | // ----------------------------------------------------------------------- | |
1605 | ||
1606 | WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFileProperty,wxPGProperty, | |
1607 | wxString,const wxString&,TextCtrlAndButton) | |
1608 | ||
1609 | wxFileProperty::wxFileProperty( const wxString& label, const wxString& name, | |
1610 | const wxString& value ) : wxPGProperty(label,name) | |
1611 | { | |
1612 | m_flags |= wxPG_PROP_SHOW_FULL_FILENAME; | |
1613 | m_indFilter = -1; | |
1614 | SetAttribute( wxPG_FILE_WILDCARD, _("All files (*.*)|*.*") ); | |
1615 | ||
1616 | SetValue(value); | |
1617 | } | |
1618 | ||
1619 | wxFileProperty::~wxFileProperty() {} | |
1620 | ||
1621 | #if wxUSE_VALIDATORS | |
1622 | ||
1623 | wxValidator* wxFileProperty::GetClassValidator() | |
1624 | { | |
1625 | WX_PG_DOGETVALIDATOR_ENTRY() | |
1626 | ||
1627 | // Atleast wxPython 2.6.2.1 required that the string argument is given | |
1628 | static wxString v; | |
1629 | wxTextValidator* validator = new wxTextValidator(wxFILTER_EXCLUDE_CHAR_LIST,&v); | |
1630 | ||
1631 | wxArrayString exChars; | |
1632 | exChars.Add(wxS("?")); | |
1633 | exChars.Add(wxS("*")); | |
1634 | exChars.Add(wxS("|")); | |
1635 | exChars.Add(wxS("<")); | |
1636 | exChars.Add(wxS(">")); | |
1637 | exChars.Add(wxS("\"")); | |
1638 | ||
1639 | validator->SetExcludes(exChars); | |
1640 | ||
1641 | WX_PG_DOGETVALIDATOR_EXIT(validator) | |
1642 | } | |
1643 | ||
1644 | wxValidator* wxFileProperty::DoGetValidator() const | |
1645 | { | |
1646 | return GetClassValidator(); | |
1647 | } | |
1648 | ||
1649 | #endif | |
1650 | ||
1651 | void wxFileProperty::OnSetValue() | |
1652 | { | |
1653 | const wxString& fnstr = m_value.GetString(); | |
1654 | ||
1425eca5 | 1655 | wxFileName filename = fnstr; |
1c4293cb | 1656 | |
1425eca5 | 1657 | if ( !filename.HasName() ) |
1c4293cb VZ |
1658 | { |
1659 | m_value = wxPGVariant_EmptyString; | |
1c4293cb VZ |
1660 | } |
1661 | ||
1662 | // Find index for extension. | |
1663 | if ( m_indFilter < 0 && fnstr.length() ) | |
1664 | { | |
1425eca5 | 1665 | wxString ext = filename.GetExt(); |
1c4293cb VZ |
1666 | int curind = 0; |
1667 | size_t pos = 0; | |
1668 | size_t len = m_wildcard.length(); | |
1669 | ||
1670 | pos = m_wildcard.find(wxS("|"), pos); | |
1671 | while ( pos != wxString::npos && pos < (len-3) ) | |
1672 | { | |
1673 | size_t ext_begin = pos + 3; | |
1674 | ||
1675 | pos = m_wildcard.find(wxS("|"), ext_begin); | |
1676 | if ( pos == wxString::npos ) | |
1677 | pos = len; | |
1678 | wxString found_ext = m_wildcard.substr(ext_begin, pos-ext_begin); | |
1679 | ||
1680 | if ( found_ext.length() > 0 ) | |
1681 | { | |
1682 | if ( found_ext[0] == wxS('*') ) | |
1683 | { | |
1684 | m_indFilter = curind; | |
1685 | break; | |
1686 | } | |
1687 | if ( ext.CmpNoCase(found_ext) == 0 ) | |
1688 | { | |
1689 | m_indFilter = curind; | |
1690 | break; | |
1691 | } | |
1692 | } | |
1693 | ||
1694 | if ( pos != len ) | |
1695 | pos = m_wildcard.find(wxS("|"), pos+1); | |
1696 | ||
1697 | curind++; | |
1698 | } | |
1699 | } | |
1700 | } | |
1701 | ||
1425eca5 | 1702 | wxFileName wxFileProperty::GetFileName() const |
1c4293cb | 1703 | { |
1425eca5 JS |
1704 | wxFileName filename; |
1705 | ||
1706 | if ( !m_value.IsNull() ) | |
1707 | filename = m_value.GetString(); | |
1708 | ||
1709 | return filename; | |
1710 | } | |
1711 | ||
1712 | wxString wxFileProperty::ValueToString( wxVariant& value, | |
1713 | int argFlags ) const | |
1714 | { | |
1715 | wxFileName filename = value.GetString(); | |
1716 | ||
1717 | if ( !filename.HasName() ) | |
1718 | return wxEmptyString; | |
1719 | ||
1720 | wxString fullName = filename.GetFullName(); | |
1c4293cb | 1721 | if ( !fullName.length() ) |
1425eca5 | 1722 | return wxEmptyString; |
1c4293cb VZ |
1723 | |
1724 | if ( argFlags & wxPG_FULL_VALUE ) | |
1725 | { | |
1425eca5 | 1726 | return filename.GetFullPath(); |
1c4293cb VZ |
1727 | } |
1728 | else if ( m_flags & wxPG_PROP_SHOW_FULL_FILENAME ) | |
1729 | { | |
1730 | if ( m_basePath.Length() ) | |
1731 | { | |
1425eca5 | 1732 | wxFileName fn2(filename); |
1c4293cb VZ |
1733 | fn2.MakeRelativeTo(m_basePath); |
1734 | return fn2.GetFullPath(); | |
1735 | } | |
1425eca5 | 1736 | return filename.GetFullPath(); |
1c4293cb VZ |
1737 | } |
1738 | ||
1425eca5 | 1739 | return filename.GetFullName(); |
1c4293cb VZ |
1740 | } |
1741 | ||
1742 | wxPGEditorDialogAdapter* wxFileProperty::GetEditorDialog() const | |
1743 | { | |
1744 | return new wxPGFileDialogAdapter(); | |
1745 | } | |
1746 | ||
1747 | bool wxFileProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const | |
1748 | { | |
1425eca5 JS |
1749 | wxFileName filename = variant.GetString(); |
1750 | ||
1c4293cb VZ |
1751 | if ( (m_flags & wxPG_PROP_SHOW_FULL_FILENAME) || (argFlags & wxPG_FULL_VALUE) ) |
1752 | { | |
1425eca5 | 1753 | if ( filename != text ) |
1c4293cb VZ |
1754 | { |
1755 | variant = text; | |
1756 | return true; | |
1757 | } | |
1758 | } | |
1759 | else | |
1760 | { | |
1425eca5 | 1761 | if ( filename.GetFullName() != text ) |
1c4293cb | 1762 | { |
1425eca5 | 1763 | wxFileName fn = filename; |
1c4293cb VZ |
1764 | fn.SetFullName(text); |
1765 | variant = fn.GetFullPath(); | |
1766 | return true; | |
1767 | } | |
1768 | } | |
1769 | ||
1770 | return false; | |
1771 | } | |
1772 | ||
1773 | bool wxFileProperty::DoSetAttribute( const wxString& name, wxVariant& value ) | |
1774 | { | |
1775 | // Return false on some occasions to make sure those attribs will get | |
1776 | // stored in m_attributes. | |
1777 | if ( name == wxPG_FILE_SHOW_FULL_PATH ) | |
1778 | { | |
1779 | if ( wxPGVariantToInt(value) ) | |
1780 | m_flags |= wxPG_PROP_SHOW_FULL_FILENAME; | |
1781 | else | |
1782 | m_flags &= ~(wxPG_PROP_SHOW_FULL_FILENAME); | |
1783 | return true; | |
1784 | } | |
1785 | else if ( name == wxPG_FILE_WILDCARD ) | |
1786 | { | |
1787 | m_wildcard = value.GetString(); | |
1788 | } | |
1789 | else if ( name == wxPG_FILE_SHOW_RELATIVE_PATH ) | |
1790 | { | |
1791 | m_basePath = value.GetString(); | |
1792 | ||
1793 | // Make sure wxPG_FILE_SHOW_FULL_PATH is also set | |
1794 | m_flags |= wxPG_PROP_SHOW_FULL_FILENAME; | |
1795 | } | |
1796 | else if ( name == wxPG_FILE_INITIAL_PATH ) | |
1797 | { | |
1798 | m_initialPath = value.GetString(); | |
1799 | return true; | |
1800 | } | |
1801 | else if ( name == wxPG_FILE_DIALOG_TITLE ) | |
1802 | { | |
1803 | m_dlgTitle = value.GetString(); | |
1804 | return true; | |
1805 | } | |
1806 | return false; | |
1807 | } | |
1808 | ||
1809 | // ----------------------------------------------------------------------- | |
1810 | // wxPGLongStringDialogAdapter | |
1811 | // ----------------------------------------------------------------------- | |
1812 | ||
1813 | bool wxPGLongStringDialogAdapter::DoShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property ) | |
1814 | { | |
1815 | wxString val1 = property->GetValueAsString(0); | |
1816 | wxString val_orig = val1; | |
1817 | ||
1818 | wxString value; | |
1819 | if ( !property->HasFlag(wxPG_PROP_NO_ESCAPE) ) | |
1820 | wxPropertyGrid::ExpandEscapeSequences(value, val1); | |
1821 | else | |
1822 | value = wxString(val1); | |
1823 | ||
1824 | // Run editor dialog. | |
1825 | if ( wxLongStringProperty::DisplayEditorDialog(property, propGrid, value) ) | |
1826 | { | |
1827 | if ( !property->HasFlag(wxPG_PROP_NO_ESCAPE) ) | |
1828 | wxPropertyGrid::CreateEscapeSequences(val1,value); | |
1829 | else | |
1830 | val1 = value; | |
1831 | ||
1832 | if ( val1 != val_orig ) | |
1833 | { | |
1834 | SetValue( val1 ); | |
1835 | return true; | |
1836 | } | |
1837 | } | |
1838 | return false; | |
1839 | } | |
1840 | ||
1841 | // ----------------------------------------------------------------------- | |
1842 | // wxLongStringProperty | |
1843 | // ----------------------------------------------------------------------- | |
1844 | ||
1845 | WX_PG_IMPLEMENT_PROPERTY_CLASS(wxLongStringProperty,wxPGProperty, | |
1846 | wxString,const wxString&,TextCtrlAndButton) | |
1847 | ||
1848 | wxLongStringProperty::wxLongStringProperty( const wxString& label, const wxString& name, | |
1849 | const wxString& value ) : wxPGProperty(label,name) | |
1850 | { | |
1851 | SetValue(value); | |
1852 | } | |
1853 | ||
1854 | wxLongStringProperty::~wxLongStringProperty() {} | |
1855 | ||
1425eca5 JS |
1856 | wxString wxLongStringProperty::ValueToString( wxVariant& value, |
1857 | int WXUNUSED(argFlags) ) const | |
1c4293cb | 1858 | { |
1425eca5 | 1859 | return value; |
1c4293cb VZ |
1860 | } |
1861 | ||
1862 | bool wxLongStringProperty::OnEvent( wxPropertyGrid* propGrid, wxWindow* WXUNUSED(primary), | |
1863 | wxEvent& event ) | |
1864 | { | |
1865 | if ( propGrid->IsMainButtonEvent(event) ) | |
1866 | { | |
1867 | // Update the value | |
703ee9f5 | 1868 | wxVariant useValue = propGrid->GetUncommittedPropertyValue(); |
1c4293cb | 1869 | |
9b5bafcf | 1870 | wxString val1 = useValue.GetString(); |
1c4293cb VZ |
1871 | wxString val_orig = val1; |
1872 | ||
1873 | wxString value; | |
1874 | if ( !(m_flags & wxPG_PROP_NO_ESCAPE) ) | |
1875 | wxPropertyGrid::ExpandEscapeSequences(value,val1); | |
1876 | else | |
1877 | value = wxString(val1); | |
1878 | ||
1879 | // Run editor dialog. | |
1880 | if ( OnButtonClick(propGrid,value) ) | |
1881 | { | |
1882 | if ( !(m_flags & wxPG_PROP_NO_ESCAPE) ) | |
1883 | wxPropertyGrid::CreateEscapeSequences(val1,value); | |
1884 | else | |
1885 | val1 = value; | |
1886 | ||
1887 | if ( val1 != val_orig ) | |
1888 | { | |
1889 | SetValueInEvent( val1 ); | |
1890 | return true; | |
1891 | } | |
1892 | } | |
1893 | } | |
1894 | return false; | |
1895 | } | |
1896 | ||
1897 | bool wxLongStringProperty::OnButtonClick( wxPropertyGrid* propGrid, wxString& value ) | |
1898 | { | |
1899 | return DisplayEditorDialog(this, propGrid, value); | |
1900 | } | |
1901 | ||
1902 | bool wxLongStringProperty::DisplayEditorDialog( wxPGProperty* prop, wxPropertyGrid* propGrid, wxString& value ) | |
1903 | ||
1904 | { | |
1905 | // launch editor dialog | |
1906 | wxDialog* dlg = new wxDialog(propGrid,-1,prop->GetLabel(),wxDefaultPosition,wxDefaultSize, | |
1907 | wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxCLIP_CHILDREN); | |
1908 | ||
1909 | dlg->SetFont(propGrid->GetFont()); // To allow entering chars of the same set as the propGrid | |
1910 | ||
1911 | // Multi-line text editor dialog. | |
1912 | #if !wxPG_SMALL_SCREEN | |
1913 | const int spacing = 8; | |
1914 | #else | |
1915 | const int spacing = 4; | |
1916 | #endif | |
1917 | wxBoxSizer* topsizer = new wxBoxSizer( wxVERTICAL ); | |
1918 | wxBoxSizer* rowsizer = new wxBoxSizer( wxHORIZONTAL ); | |
1919 | wxTextCtrl* ed = new wxTextCtrl(dlg,11,value, | |
1920 | wxDefaultPosition,wxDefaultSize,wxTE_MULTILINE); | |
1921 | ||
1922 | rowsizer->Add( ed, 1, wxEXPAND|wxALL, spacing ); | |
1923 | topsizer->Add( rowsizer, 1, wxEXPAND, 0 ); | |
1924 | rowsizer = new wxBoxSizer( wxHORIZONTAL ); | |
1925 | const int but_sz_flags = | |
1926 | wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT; | |
1927 | rowsizer->Add( new wxButton(dlg,wxID_OK,_("Ok")), | |
1928 | 0, but_sz_flags, spacing ); | |
1929 | rowsizer->Add( new wxButton(dlg,wxID_CANCEL,_("Cancel")), | |
1930 | 0, but_sz_flags, spacing ); | |
1931 | topsizer->Add( rowsizer, 0, wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL, 0 ); | |
1932 | ||
1933 | dlg->SetSizer( topsizer ); | |
1934 | topsizer->SetSizeHints( dlg ); | |
1935 | ||
1936 | #if !wxPG_SMALL_SCREEN | |
1937 | dlg->SetSize(400,300); | |
1938 | ||
1939 | dlg->Move( propGrid->GetGoodEditorDialogPosition(prop,dlg->GetSize()) ); | |
1940 | #endif | |
1941 | ||
1942 | int res = dlg->ShowModal(); | |
1943 | ||
1944 | if ( res == wxID_OK ) | |
1945 | { | |
1946 | value = ed->GetValue(); | |
1947 | dlg->Destroy(); | |
1948 | return true; | |
1949 | } | |
1950 | dlg->Destroy(); | |
1951 | return false; | |
1952 | } | |
1953 | ||
1954 | bool wxLongStringProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const | |
1955 | { | |
0c931dd4 | 1956 | if ( variant != text ) |
1c4293cb VZ |
1957 | { |
1958 | variant = text; | |
1959 | return true; | |
1960 | } | |
1961 | return false; | |
1962 | } | |
1963 | ||
1964 | // ----------------------------------------------------------------------- | |
1965 | // wxArrayEditorDialog | |
1966 | // ----------------------------------------------------------------------- | |
1967 | ||
1968 | BEGIN_EVENT_TABLE(wxArrayEditorDialog, wxDialog) | |
1969 | EVT_IDLE(wxArrayEditorDialog::OnIdle) | |
1970 | EVT_LISTBOX(24, wxArrayEditorDialog::OnListBoxClick) | |
1971 | EVT_TEXT_ENTER(21, wxArrayEditorDialog::OnAddClick) | |
1972 | EVT_BUTTON(22, wxArrayEditorDialog::OnAddClick) | |
1973 | EVT_BUTTON(23, wxArrayEditorDialog::OnDeleteClick) | |
1974 | EVT_BUTTON(25, wxArrayEditorDialog::OnUpClick) | |
1975 | EVT_BUTTON(26, wxArrayEditorDialog::OnDownClick) | |
1976 | EVT_BUTTON(27, wxArrayEditorDialog::OnUpdateClick) | |
1977 | //EVT_BUTTON(28, wxArrayEditorDialog::OnCustomEditClick) | |
1978 | END_EVENT_TABLE() | |
1979 | ||
1980 | IMPLEMENT_ABSTRACT_CLASS(wxArrayEditorDialog, wxDialog) | |
1981 | ||
3b211af1 | 1982 | #include "wx/statline.h" |
1c4293cb VZ |
1983 | |
1984 | // ----------------------------------------------------------------------- | |
1985 | ||
1986 | void wxArrayEditorDialog::OnIdle(wxIdleEvent& event) | |
1987 | { | |
1988 | // | |
1989 | // Do control focus detection here. | |
1990 | // | |
1991 | ||
1992 | wxWindow* focused = FindFocus(); | |
1993 | ||
1994 | // This strange focus thing is a workaround for wxGTK wxListBox focus | |
1995 | // reporting bug. | |
1996 | if ( m_curFocus == 0 && focused != m_edValue && | |
1997 | focused != m_butAdd && focused != m_butUpdate && | |
1998 | m_lbStrings->GetSelection() >= 0 ) | |
1999 | { | |
2000 | // ListBox was just focused. | |
2001 | m_butAdd->Enable(false); | |
2002 | m_butUpdate->Enable(false); | |
2003 | m_butRemove->Enable(true); | |
2004 | m_butUp->Enable(true); | |
2005 | m_butDown->Enable(true); | |
2006 | m_curFocus = 1; | |
2007 | } | |
2008 | else if ( (m_curFocus == 1 && focused == m_edValue) /*|| m_curFocus == 2*/ ) | |
2009 | { | |
2010 | // TextCtrl was just focused. | |
2011 | m_butAdd->Enable(true); | |
2012 | bool upd_enable = false; | |
2013 | if ( m_lbStrings->GetCount() && m_lbStrings->GetSelection() >= 0 ) | |
2014 | upd_enable = true; | |
2015 | m_butUpdate->Enable(upd_enable); | |
2016 | m_butRemove->Enable(false); | |
2017 | m_butUp->Enable(false); | |
2018 | m_butDown->Enable(false); | |
2019 | m_curFocus = 0; | |
2020 | } | |
2021 | ||
2022 | event.Skip(); | |
2023 | } | |
2024 | ||
2025 | // ----------------------------------------------------------------------- | |
2026 | ||
2027 | wxArrayEditorDialog::wxArrayEditorDialog() | |
2028 | : wxDialog() | |
2029 | { | |
2030 | Init(); | |
2031 | } | |
2032 | ||
2033 | // ----------------------------------------------------------------------- | |
2034 | ||
2035 | void wxArrayEditorDialog::Init() | |
2036 | { | |
2037 | m_custBtText = (const wxChar*) NULL; | |
2038 | } | |
2039 | ||
2040 | // ----------------------------------------------------------------------- | |
2041 | ||
2042 | wxArrayEditorDialog::wxArrayEditorDialog( wxWindow *parent, | |
2043 | const wxString& message, | |
2044 | const wxString& caption, | |
2045 | long style, | |
2046 | const wxPoint& pos, | |
2047 | const wxSize& sz ) | |
2048 | : wxDialog() | |
2049 | { | |
2050 | Init(); | |
2051 | Create(parent,message,caption,style,pos,sz); | |
2052 | } | |
2053 | ||
2054 | // ----------------------------------------------------------------------- | |
2055 | ||
2056 | bool wxArrayEditorDialog::Create( wxWindow *parent, | |
2057 | const wxString& message, | |
2058 | const wxString& caption, | |
2059 | long style, | |
2060 | const wxPoint& pos, | |
2061 | const wxSize& sz ) | |
2062 | { | |
2063 | // On wxMAC the dialog shows incorrectly if style is not exactly wxCAPTION | |
2064 | // FIXME: This should be only a temporary fix. | |
2065 | #ifdef __WXMAC__ | |
b0f0eda8 | 2066 | wxUnusedVar(style); |
1c4293cb VZ |
2067 | int useStyle = wxCAPTION; |
2068 | #else | |
2069 | int useStyle = style; | |
2070 | #endif | |
2071 | ||
2072 | bool res = wxDialog::Create(parent, wxID_ANY, caption, pos, sz, useStyle); | |
2073 | ||
2074 | SetFont(parent->GetFont()); // To allow entering chars of the same set as the propGrid | |
2075 | ||
2076 | #if !wxPG_SMALL_SCREEN | |
2077 | const int spacing = 4; | |
2078 | #else | |
2079 | const int spacing = 3; | |
2080 | #endif | |
2081 | ||
2082 | m_modified = false; | |
2083 | ||
2084 | m_curFocus = 1; | |
2085 | ||
2086 | const int but_sz_flags = | |
2087 | wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxALL; //wxBOTTOM|wxLEFT|wxRIGHT; | |
2088 | ||
2089 | wxBoxSizer* topsizer = new wxBoxSizer( wxVERTICAL ); | |
2090 | ||
2091 | // Message | |
2092 | if ( message.length() ) | |
2093 | topsizer->Add( new wxStaticText(this,-1,message), | |
2094 | 0, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxALL, spacing ); | |
2095 | ||
2096 | // String editor | |
2097 | wxBoxSizer* rowsizer = new wxBoxSizer( wxHORIZONTAL ); | |
2098 | m_edValue = new wxTextCtrl(this,21,wxEmptyString, | |
2099 | wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER); | |
2100 | wxValidator* validator = GetTextCtrlValidator(); | |
2101 | if ( validator ) | |
2102 | { | |
2103 | m_edValue->SetValidator( *validator ); | |
2104 | delete validator; | |
2105 | } | |
2106 | rowsizer->Add( m_edValue, | |
2107 | 1, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxALL, spacing ); | |
2108 | ||
2109 | // Add button | |
2110 | m_butAdd = new wxButton(this,22,_("Add")); | |
2111 | rowsizer->Add( m_butAdd, | |
2112 | 0, wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT, spacing ); | |
2113 | topsizer->Add( rowsizer, 0, wxEXPAND, spacing ); | |
2114 | ||
2115 | // Separator line | |
2116 | topsizer->Add( new wxStaticLine(this,-1), | |
2117 | 0, wxEXPAND|wxBOTTOM|wxLEFT|wxRIGHT, spacing ); | |
2118 | ||
2119 | rowsizer = new wxBoxSizer( wxHORIZONTAL ); | |
2120 | ||
2121 | // list box | |
2122 | m_lbStrings = new wxListBox(this, 24, wxDefaultPosition, wxDefaultSize); | |
2123 | unsigned int i; | |
2124 | for ( i=0; i<ArrayGetCount(); i++ ) | |
2125 | m_lbStrings->Append( ArrayGet(i) ); | |
2126 | rowsizer->Add( m_lbStrings, 1, wxEXPAND|wxRIGHT, spacing ); | |
2127 | ||
2128 | // Manipulator buttons | |
2129 | wxBoxSizer* colsizer = new wxBoxSizer( wxVERTICAL ); | |
d3b9f782 | 2130 | m_butCustom = NULL; |
1c4293cb VZ |
2131 | if ( m_custBtText ) |
2132 | { | |
2133 | m_butCustom = new wxButton(this,28,::wxGetTranslation(m_custBtText)); | |
2134 | colsizer->Add( m_butCustom, | |
2135 | 0, wxALIGN_CENTER|wxTOP/*wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL|wxTOP|wxBOTTOM|wxRIGHT*/, | |
2136 | spacing ); | |
2137 | } | |
2138 | m_butUpdate = new wxButton(this,27,_("Update")); | |
2139 | colsizer->Add( m_butUpdate, | |
2140 | 0, wxALIGN_CENTER|wxTOP, spacing ); | |
2141 | m_butRemove = new wxButton(this,23,_("Remove")); | |
2142 | colsizer->Add( m_butRemove, | |
2143 | 0, wxALIGN_CENTER|wxTOP, spacing ); | |
2144 | m_butUp = new wxButton(this,25,_("Up")); | |
2145 | colsizer->Add( m_butUp, | |
2146 | 0, wxALIGN_CENTER|wxTOP, spacing ); | |
2147 | m_butDown = new wxButton(this,26,_("Down")); | |
2148 | colsizer->Add( m_butDown, | |
2149 | 0, wxALIGN_CENTER|wxTOP, spacing ); | |
2150 | rowsizer->Add( colsizer, 0, 0, spacing ); | |
2151 | ||
2152 | topsizer->Add( rowsizer, 1, wxLEFT|wxRIGHT|wxEXPAND, spacing ); | |
2153 | ||
2154 | // Separator line | |
2155 | topsizer->Add( new wxStaticLine(this,-1), | |
2156 | 0, wxEXPAND|wxTOP|wxLEFT|wxRIGHT, spacing ); | |
2157 | ||
2158 | // buttons | |
2159 | rowsizer = new wxBoxSizer( wxHORIZONTAL ); | |
2160 | /* | |
2161 | const int but_sz_flags = | |
2162 | wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT; | |
2163 | */ | |
2164 | rowsizer->Add( new wxButton(this,wxID_OK,_("Ok")), | |
2165 | 0, but_sz_flags, spacing ); | |
2166 | rowsizer->Add( new wxButton(this,wxID_CANCEL,_("Cancel")), | |
2167 | 0, but_sz_flags, spacing ); | |
2168 | topsizer->Add( rowsizer, 0, wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL, 0 ); | |
2169 | ||
2170 | m_edValue->SetFocus(); | |
2171 | ||
2172 | SetSizer( topsizer ); | |
2173 | topsizer->SetSizeHints( this ); | |
2174 | ||
2175 | #if !wxPG_SMALL_SCREEN | |
2176 | if ( sz.x == wxDefaultSize.x && | |
2177 | sz.y == wxDefaultSize.y ) | |
2178 | SetSize( wxSize(275,360) ); | |
2179 | else | |
2180 | SetSize(sz); | |
2181 | #endif | |
2182 | ||
2183 | return res; | |
2184 | } | |
2185 | ||
2186 | // ----------------------------------------------------------------------- | |
2187 | ||
2188 | void wxArrayEditorDialog::OnAddClick(wxCommandEvent& ) | |
2189 | { | |
2190 | wxString text = m_edValue->GetValue(); | |
2191 | if ( text.length() ) | |
2192 | { | |
2193 | if ( ArrayInsert( text, -1 ) ) | |
2194 | { | |
2195 | m_lbStrings->Append( text ); | |
2196 | m_modified = true; | |
2197 | m_edValue->Clear(); | |
2198 | } | |
2199 | } | |
2200 | } | |
2201 | ||
2202 | // ----------------------------------------------------------------------- | |
2203 | ||
2204 | void wxArrayEditorDialog::OnDeleteClick(wxCommandEvent& ) | |
2205 | { | |
2206 | int index = m_lbStrings->GetSelection(); | |
2207 | if ( index >= 0 ) | |
2208 | { | |
2209 | ArrayRemoveAt( index ); | |
2210 | m_lbStrings->Delete ( index ); | |
2211 | m_modified = true; | |
2212 | } | |
2213 | } | |
2214 | ||
2215 | // ----------------------------------------------------------------------- | |
2216 | ||
2217 | void wxArrayEditorDialog::OnUpClick(wxCommandEvent& ) | |
2218 | { | |
2219 | int index = m_lbStrings->GetSelection(); | |
2220 | if ( index > 0 ) | |
2221 | { | |
2222 | ArraySwap(index-1,index); | |
2223 | /*wxString old_str = m_array[index-1]; | |
2224 | wxString new_str = m_array[index]; | |
2225 | m_array[index-1] = new_str; | |
2226 | m_array[index] = old_str;*/ | |
2227 | m_lbStrings->SetString ( index-1, ArrayGet(index-1) ); | |
2228 | m_lbStrings->SetString ( index, ArrayGet(index) ); | |
2229 | m_lbStrings->SetSelection ( index-1 ); | |
2230 | m_modified = true; | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | // ----------------------------------------------------------------------- | |
2235 | ||
2236 | void wxArrayEditorDialog::OnDownClick(wxCommandEvent& ) | |
2237 | { | |
2238 | int index = m_lbStrings->GetSelection(); | |
2239 | int lastStringIndex = ((int) m_lbStrings->GetCount()) - 1; | |
2240 | if ( index >= 0 && index < lastStringIndex ) | |
2241 | { | |
2242 | ArraySwap(index,index+1); | |
2243 | /*wxString old_str = m_array[index+1]; | |
2244 | wxString new_str = m_array[index]; | |
2245 | m_array[index+1] = new_str; | |
2246 | m_array[index] = old_str;*/ | |
2247 | m_lbStrings->SetString ( index+1, ArrayGet(index+1) ); | |
2248 | m_lbStrings->SetString ( index, ArrayGet(index) ); | |
2249 | m_lbStrings->SetSelection ( index+1 ); | |
2250 | m_modified = true; | |
2251 | } | |
2252 | } | |
2253 | ||
2254 | // ----------------------------------------------------------------------- | |
2255 | ||
2256 | void wxArrayEditorDialog::OnUpdateClick(wxCommandEvent& ) | |
2257 | { | |
2258 | int index = m_lbStrings->GetSelection(); | |
2259 | if ( index >= 0 ) | |
2260 | { | |
2261 | wxString str = m_edValue->GetValue(); | |
2262 | if ( ArraySet(index,str) ) | |
2263 | { | |
2264 | m_lbStrings->SetString ( index, str ); | |
2265 | //m_array[index] = str; | |
2266 | m_modified = true; | |
2267 | } | |
2268 | } | |
2269 | } | |
2270 | ||
2271 | // ----------------------------------------------------------------------- | |
2272 | ||
2273 | void wxArrayEditorDialog::OnListBoxClick(wxCommandEvent& ) | |
2274 | { | |
2275 | int index = m_lbStrings->GetSelection(); | |
2276 | if ( index >= 0 ) | |
2277 | { | |
2278 | m_edValue->SetValue( m_lbStrings->GetString(index) ); | |
2279 | } | |
2280 | } | |
2281 | ||
2282 | // ----------------------------------------------------------------------- | |
2283 | // wxPGArrayStringEditorDialog | |
2284 | // ----------------------------------------------------------------------- | |
2285 | ||
2286 | IMPLEMENT_DYNAMIC_CLASS(wxPGArrayStringEditorDialog, wxArrayEditorDialog) | |
2287 | ||
2288 | BEGIN_EVENT_TABLE(wxPGArrayStringEditorDialog, wxArrayEditorDialog) | |
2289 | EVT_BUTTON(28, wxPGArrayStringEditorDialog::OnCustomEditClick) | |
2290 | END_EVENT_TABLE() | |
2291 | ||
2292 | // ----------------------------------------------------------------------- | |
2293 | ||
2294 | wxString wxPGArrayStringEditorDialog::ArrayGet( size_t index ) | |
2295 | { | |
2296 | return m_array[index]; | |
2297 | } | |
2298 | ||
2299 | size_t wxPGArrayStringEditorDialog::ArrayGetCount() | |
2300 | { | |
2301 | return m_array.size(); | |
2302 | } | |
2303 | ||
2304 | bool wxPGArrayStringEditorDialog::ArrayInsert( const wxString& str, int index ) | |
2305 | { | |
2306 | if (index<0) | |
2307 | m_array.Add(str); | |
2308 | else | |
2309 | m_array.Insert(str,index); | |
2310 | return true; | |
2311 | } | |
2312 | ||
2313 | bool wxPGArrayStringEditorDialog::ArraySet( size_t index, const wxString& str ) | |
2314 | { | |
2315 | m_array[index] = str; | |
2316 | return true; | |
2317 | } | |
2318 | ||
2319 | void wxPGArrayStringEditorDialog::ArrayRemoveAt( int index ) | |
2320 | { | |
2321 | m_array.RemoveAt(index); | |
2322 | } | |
2323 | ||
2324 | void wxPGArrayStringEditorDialog::ArraySwap( size_t first, size_t second ) | |
2325 | { | |
2326 | wxString old_str = m_array[first]; | |
2327 | wxString new_str = m_array[second]; | |
2328 | m_array[first] = new_str; | |
2329 | m_array[second] = old_str; | |
2330 | } | |
2331 | ||
2332 | wxPGArrayStringEditorDialog::wxPGArrayStringEditorDialog() | |
2333 | : wxArrayEditorDialog() | |
2334 | { | |
2335 | Init(); | |
2336 | } | |
2337 | ||
2338 | void wxPGArrayStringEditorDialog::Init() | |
2339 | { | |
d3b9f782 | 2340 | m_pCallingClass = NULL; |
1c4293cb VZ |
2341 | } |
2342 | ||
2343 | void wxPGArrayStringEditorDialog::OnCustomEditClick(wxCommandEvent& ) | |
2344 | { | |
2345 | wxASSERT( m_pCallingClass ); | |
2346 | wxString str = m_edValue->GetValue(); | |
2347 | if ( m_pCallingClass->OnCustomStringEdit(m_parent,str) ) | |
2348 | { | |
2349 | //m_edValue->SetValue ( str ); | |
2350 | m_lbStrings->Append ( str ); | |
2351 | m_array.Add ( str ); | |
2352 | m_modified = true; | |
2353 | } | |
2354 | } | |
2355 | ||
2356 | // ----------------------------------------------------------------------- | |
2357 | // wxArrayStringProperty | |
2358 | // ----------------------------------------------------------------------- | |
2359 | ||
2360 | WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayStringProperty, // Property name | |
2361 | wxPGProperty, // Property we inherit from | |
2362 | wxArrayString, // Value type name | |
2363 | const wxArrayString&, // Value type, as given in constructor | |
2364 | TextCtrlAndButton) // Initial editor | |
2365 | ||
2366 | wxArrayStringProperty::wxArrayStringProperty( const wxString& label, | |
2367 | const wxString& name, | |
2368 | const wxArrayString& array ) | |
2369 | : wxPGProperty(label,name) | |
2370 | { | |
2371 | SetValue( array ); | |
2372 | } | |
2373 | ||
2374 | wxArrayStringProperty::~wxArrayStringProperty() { } | |
2375 | ||
2376 | void wxArrayStringProperty::OnSetValue() | |
2377 | { | |
2378 | GenerateValueAsString(); | |
2379 | } | |
2380 | ||
1425eca5 JS |
2381 | #define ARRSTRPROP_ARRAY_TO_STRING(STRING,ARRAY) \ |
2382 | wxPropertyGrid::ArrayStringToString(STRING,ARRAY,wxS('"'),wxS('"'),1) | |
2383 | ||
2384 | wxString wxArrayStringProperty::ValueToString( wxVariant& WXUNUSED(value), | |
2385 | int argFlags ) const | |
1c4293cb | 2386 | { |
1425eca5 JS |
2387 | // |
2388 | // If this is called from GetValueAsString(), return cached string | |
2389 | if ( argFlags & wxPG_VALUE_IS_CURRENT ) | |
2390 | { | |
2391 | return m_display; | |
2392 | } | |
2393 | ||
2394 | wxArrayString arr = m_value.GetArrayString(); | |
2395 | wxString s; | |
2396 | ARRSTRPROP_ARRAY_TO_STRING(s, arr); | |
2397 | return s; | |
1c4293cb VZ |
2398 | } |
2399 | ||
2400 | // Converts wxArrayString to a string separated by delimeters and spaces. | |
2401 | // preDelim is useful for "str1" "str2" style. Set flags to 1 to do slash | |
2402 | // conversion. | |
2403 | void wxPropertyGrid::ArrayStringToString( wxString& dst, const wxArrayString& src, | |
2404 | wxChar preDelim, wxChar postDelim, | |
2405 | int flags ) | |
2406 | { | |
2407 | wxString pdr; | |
2408 | ||
2409 | unsigned int i; | |
2410 | unsigned int itemCount = src.size(); | |
2411 | ||
b7bc9d80 | 2412 | wxChar preas[2] = { 0, 0 }; |
1c4293cb VZ |
2413 | |
2414 | dst.Empty(); | |
2415 | ||
b7bc9d80 | 2416 | if ( flags & 1 ) |
1c4293cb VZ |
2417 | { |
2418 | preas[0] = preDelim; | |
1c4293cb VZ |
2419 | pdr = wxS("\\"); |
2420 | pdr += preDelim; | |
2421 | } | |
2422 | ||
2423 | if ( itemCount ) | |
2424 | dst.append( preas ); | |
2425 | ||
2426 | wxASSERT( postDelim ); | |
2427 | wxString postDelimStr(postDelim); | |
2428 | //wxString preDelimStr(preDelim); | |
2429 | ||
2430 | for ( i = 0; i < itemCount; i++ ) | |
2431 | { | |
2432 | wxString str( src.Item(i) ); | |
2433 | ||
2434 | // Do some character conversion. | |
2435 | // Convertes \ to \\ and <preDelim> to \<preDelim> | |
2436 | // Useful when preDelim and postDelim are "\"". | |
2437 | if ( flags & 1 ) | |
2438 | { | |
2439 | str.Replace( wxS("\\"), wxS("\\\\"), true ); | |
2440 | if ( pdr.length() ) | |
2441 | str.Replace( preas, pdr, true ); | |
2442 | } | |
2443 | ||
2444 | dst.append( str ); | |
2445 | ||
2446 | if ( i < (itemCount-1) ) | |
2447 | { | |
2448 | dst.append( postDelimStr ); | |
2449 | dst.append( wxS(" ") ); | |
2450 | dst.append( preas ); | |
2451 | } | |
2452 | else if ( preDelim ) | |
2453 | dst.append( postDelimStr ); | |
2454 | } | |
2455 | } | |
2456 | ||
1c4293cb VZ |
2457 | void wxArrayStringProperty::GenerateValueAsString() |
2458 | { | |
2459 | wxArrayString arr = m_value.GetArrayString(); | |
1425eca5 | 2460 | ARRSTRPROP_ARRAY_TO_STRING(m_display, arr); |
1c4293cb VZ |
2461 | } |
2462 | ||
2463 | // Default implementation doesn't do anything. | |
2464 | bool wxArrayStringProperty::OnCustomStringEdit( wxWindow*, wxString& ) | |
2465 | { | |
2466 | return false; | |
2467 | } | |
2468 | ||
2469 | wxArrayEditorDialog* wxArrayStringProperty::CreateEditorDialog() | |
2470 | { | |
2471 | return new wxPGArrayStringEditorDialog(); | |
2472 | } | |
2473 | ||
2474 | bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid* propGrid, | |
2475 | wxWindow* WXUNUSED(primaryCtrl), | |
2476 | const wxChar* cbt ) | |
2477 | { | |
2478 | // Update the value | |
703ee9f5 | 2479 | wxVariant useValue = propGrid->GetUncommittedPropertyValue(); |
1c4293cb VZ |
2480 | |
2481 | if ( !propGrid->EditorValidate() ) | |
2482 | return false; | |
2483 | ||
2484 | // Create editor dialog. | |
2485 | wxArrayEditorDialog* dlg = CreateEditorDialog(); | |
2486 | #if wxUSE_VALIDATORS | |
2487 | wxValidator* validator = GetValidator(); | |
2488 | wxPGInDialogValidator dialogValidator; | |
2489 | #endif | |
2490 | ||
2491 | wxPGArrayStringEditorDialog* strEdDlg = wxDynamicCast(dlg, wxPGArrayStringEditorDialog); | |
2492 | ||
2493 | if ( strEdDlg ) | |
2494 | strEdDlg->SetCustomButton(cbt, this); | |
2495 | ||
9b5bafcf | 2496 | dlg->SetDialogValue( useValue ); |
1c4293cb VZ |
2497 | dlg->Create(propGrid, wxEmptyString, m_label); |
2498 | ||
2499 | #if !wxPG_SMALL_SCREEN | |
2500 | dlg->Move( propGrid->GetGoodEditorDialogPosition(this,dlg->GetSize()) ); | |
2501 | #endif | |
2502 | ||
2503 | bool retVal; | |
2504 | ||
2505 | for (;;) | |
2506 | { | |
2507 | retVal = false; | |
2508 | ||
2509 | int res = dlg->ShowModal(); | |
2510 | ||
2511 | if ( res == wxID_OK && dlg->IsModified() ) | |
2512 | { | |
2513 | wxVariant value = dlg->GetDialogValue(); | |
2514 | if ( !value.IsNull() ) | |
2515 | { | |
2516 | wxArrayString actualValue = value.GetArrayString(); | |
2517 | wxString tempStr; | |
1425eca5 | 2518 | ARRSTRPROP_ARRAY_TO_STRING(tempStr, actualValue); |
1c4293cb VZ |
2519 | #if wxUSE_VALIDATORS |
2520 | if ( dialogValidator.DoValidate( propGrid, validator, tempStr ) ) | |
2521 | #endif | |
2522 | { | |
2523 | SetValueInEvent( actualValue ); | |
2524 | retVal = true; | |
2525 | break; | |
2526 | } | |
2527 | } | |
2528 | else | |
2529 | break; | |
2530 | } | |
2531 | else | |
2532 | break; | |
2533 | } | |
2534 | ||
2535 | delete dlg; | |
2536 | ||
2537 | return retVal; | |
2538 | } | |
2539 | ||
2540 | bool wxArrayStringProperty::OnEvent( wxPropertyGrid* propGrid, | |
2541 | wxWindow* primary, | |
2542 | wxEvent& event ) | |
2543 | { | |
2544 | if ( propGrid->IsMainButtonEvent(event) ) | |
2545 | return OnButtonClick(propGrid,primary,(const wxChar*) NULL); | |
2546 | return false; | |
2547 | } | |
2548 | ||
2549 | bool wxArrayStringProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const | |
2550 | { | |
2551 | wxArrayString arr; | |
2552 | ||
2553 | WX_PG_TOKENIZER2_BEGIN(text,wxS('"')) | |
2554 | ||
2555 | // Need to replace backslashes with empty characters | |
2556 | // (opposite what is done in GenerateValueString). | |
2557 | token.Replace ( wxS("\\"), wxEmptyString, true ); | |
2558 | ||
2559 | arr.Add( token ); | |
2560 | ||
2561 | WX_PG_TOKENIZER2_END() | |
2562 | ||
2563 | variant = arr; | |
2564 | ||
2565 | return true; | |
2566 | } | |
2567 | ||
2568 | // ----------------------------------------------------------------------- | |
2569 | // wxPGInDialogValidator | |
2570 | // ----------------------------------------------------------------------- | |
2571 | ||
2572 | #if wxUSE_VALIDATORS | |
2573 | bool wxPGInDialogValidator::DoValidate( wxPropertyGrid* propGrid, | |
2574 | wxValidator* validator, | |
2575 | const wxString& value ) | |
2576 | { | |
2577 | if ( !validator ) | |
2578 | return true; | |
2579 | ||
2580 | wxTextCtrl* tc = m_textCtrl; | |
2581 | ||
2582 | if ( !tc ) | |
2583 | { | |
2584 | { | |
2585 | tc = new wxTextCtrl( propGrid, wxPG_SUBID_TEMP1, wxEmptyString, | |
2586 | wxPoint(30000,30000)); | |
2587 | tc->Hide(); | |
2588 | } | |
2589 | ||
2590 | m_textCtrl = tc; | |
2591 | } | |
2592 | ||
2593 | tc->SetValue(value); | |
2594 | ||
2595 | validator->SetWindow(tc); | |
2596 | bool res = validator->Validate(propGrid); | |
2597 | ||
2598 | return res; | |
2599 | } | |
2600 | #else | |
2601 | bool wxPGInDialogValidator::DoValidate( wxPropertyGrid* WXUNUSED(propGrid), | |
2602 | wxValidator* WXUNUSED(validator), | |
2603 | const wxString& WXUNUSED(value) ) | |
2604 | { | |
2605 | return true; | |
2606 | } | |
2607 | #endif | |
2608 | ||
2609 | // ----------------------------------------------------------------------- | |
2610 | ||
f4bc1aa2 | 2611 | #endif // wxUSE_PROPGRID |