]> git.saurik.com Git - wxWidgets.git/blob - src/generic/prop.cpp
Moved gsockunx.h from src/unix to include/wx/unix
[wxWidgets.git] / src / generic / prop.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: prop.cpp
3 // Purpose: Propert sheet classes implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "prop.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/debug.h"
28 #include "wx/prop.h"
29
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35
36 IMPLEMENT_DYNAMIC_CLASS(wxPropertyValue, wxObject)
37
38 wxPropertyValue::wxPropertyValue(void)
39 {
40 m_type = wxPropertyValueNull;
41 m_next = NULL;
42 m_last = NULL;
43 m_value.first = NULL;
44 m_clientData = NULL;
45 m_modifiedFlag = FALSE;
46 }
47
48 wxPropertyValue::wxPropertyValue(const wxPropertyValue& copyFrom)
49 {
50 m_modifiedFlag = FALSE;
51 Copy((wxPropertyValue& )copyFrom);
52 }
53
54 wxPropertyValue::wxPropertyValue(const wxChar *val)
55 {
56 m_modifiedFlag = FALSE;
57 m_type = wxPropertyValueString;
58
59 m_value.string = copystring(val);
60 m_clientData = NULL;
61 m_next = NULL;
62 m_last = NULL;
63 }
64
65 wxPropertyValue::wxPropertyValue(const wxString& val)
66 {
67 m_modifiedFlag = FALSE;
68 m_type = wxPropertyValueString;
69
70 m_value.string = copystring((const wxChar *)val);
71 m_clientData = NULL;
72 m_next = NULL;
73 m_last = NULL;
74 }
75
76 wxPropertyValue::wxPropertyValue(long the_integer)
77 {
78 m_modifiedFlag = FALSE;
79 m_type = wxPropertyValueInteger;
80 m_value.integer = the_integer;
81 m_clientData = NULL;
82 m_next = NULL;
83 }
84
85 wxPropertyValue::wxPropertyValue(bool val)
86 {
87 m_modifiedFlag = FALSE;
88 m_type = wxPropertyValuebool;
89 m_value.integer = val;
90 m_clientData = NULL;
91 m_next = NULL;
92 }
93
94 wxPropertyValue::wxPropertyValue(float the_real)
95 {
96 m_modifiedFlag = FALSE;
97 m_type = wxPropertyValueReal;
98 m_value.real = the_real;
99 m_clientData = NULL;
100 m_next = NULL;
101 }
102
103 wxPropertyValue::wxPropertyValue(double the_real)
104 {
105 m_modifiedFlag = FALSE;
106 m_type = wxPropertyValueReal;
107 m_value.real = (float)the_real;
108 m_clientData = NULL;
109 m_next = NULL;
110 }
111
112 // Pointer versions: we have a pointer to the real C++ value.
113 wxPropertyValue::wxPropertyValue(wxChar **val)
114 {
115 m_modifiedFlag = FALSE;
116 m_type = wxPropertyValueStringPtr;
117
118 m_value.stringPtr = val;
119 m_clientData = NULL;
120 m_next = NULL;
121 m_last = NULL;
122 }
123
124 wxPropertyValue::wxPropertyValue(long *val)
125 {
126 m_modifiedFlag = FALSE;
127 m_type = wxPropertyValueIntegerPtr;
128 m_value.integerPtr = val;
129 m_clientData = NULL;
130 m_next = NULL;
131 }
132
133 wxPropertyValue::wxPropertyValue(bool *val)
134 {
135 m_modifiedFlag = FALSE;
136 m_type = wxPropertyValueboolPtr;
137 m_value.boolPtr = val;
138 m_clientData = NULL;
139 m_next = NULL;
140 }
141
142 wxPropertyValue::wxPropertyValue(float *val)
143 {
144 m_modifiedFlag = FALSE;
145 m_type = wxPropertyValueRealPtr;
146 m_value.realPtr = val;
147 m_clientData = NULL;
148 m_next = NULL;
149 }
150
151 wxPropertyValue::wxPropertyValue(wxList *the_list)
152 {
153 m_modifiedFlag = FALSE;
154 m_type = wxPropertyValueList;
155 m_clientData = NULL;
156 m_last = NULL;
157 m_value.first = NULL;
158
159 wxNode *node = the_list->First();
160 while (node)
161 {
162 wxPropertyValue *expr = (wxPropertyValue *)node->Data();
163 Append(expr);
164 node = node->Next();
165 }
166
167 delete the_list;
168 }
169
170 wxPropertyValue::wxPropertyValue(wxStringList *the_list)
171 {
172 m_modifiedFlag = FALSE;
173 m_type = wxPropertyValueList;
174 m_clientData = NULL;
175 m_last = NULL;
176 m_value.first = NULL;
177
178 wxNode *node = the_list->First();
179 while (node)
180 {
181 char *s = (char *)node->Data();
182 Append(new wxPropertyValue(s));
183 node = node->Next();
184 }
185 delete the_list;
186 }
187
188 wxPropertyValue::~wxPropertyValue(void)
189 {
190 switch (m_type)
191 {
192 case wxPropertyValueInteger:
193 case wxPropertyValuebool:
194 case wxPropertyValueReal:
195 {
196 break;
197 }
198 case wxPropertyValueString:
199 {
200 delete[] m_value.string;
201 break;
202 }
203 case wxPropertyValueList:
204 {
205 wxPropertyValue *expr = m_value.first;
206 while (expr)
207 {
208 wxPropertyValue *expr1 = expr->m_next;
209
210 delete expr;
211 expr = expr1;
212 }
213 break;
214 }
215 default:
216 case wxPropertyValueNull: break;
217 }
218 }
219
220 void wxPropertyValue::Append(wxPropertyValue *expr)
221 {
222 m_modifiedFlag = TRUE;
223 if (!m_value.first)
224 m_value.first = expr;
225
226 if (m_last)
227 m_last->m_next = expr;
228 m_last = expr;
229 }
230
231 void wxPropertyValue::Insert(wxPropertyValue *expr)
232 {
233 m_modifiedFlag = TRUE;
234 expr->m_next = m_value.first;
235 m_value.first = expr;
236
237 if (!m_last)
238 m_last = expr;
239 }
240
241 // Delete from list
242 void wxPropertyValue::Delete(wxPropertyValue *node)
243 {
244 wxPropertyValue *expr = GetFirst();
245
246 wxPropertyValue *previous = NULL;
247 while (expr && (expr != node))
248 {
249 previous = expr;
250 expr = expr->GetNext();
251 }
252
253 if (expr)
254 {
255 if (previous)
256 previous->m_next = expr->m_next;
257
258 // If node was the first in the list,
259 // make the list point to the NEXT one.
260 if (GetFirst() == expr)
261 {
262 m_value.first = expr->m_next;
263 }
264
265 // If node was the last in the list,
266 // make the list 'last' pointer point to the PREVIOUS one.
267 if (GetLast() == expr)
268 {
269 if (previous)
270 m_last = previous;
271 else
272 m_last = NULL;
273 }
274 m_modifiedFlag = TRUE;
275 delete expr;
276 }
277
278 }
279
280 void wxPropertyValue::ClearList(void)
281 {
282 wxPropertyValue *val = GetFirst();
283 if (val)
284 m_modifiedFlag = TRUE;
285
286 while (val)
287 {
288 wxPropertyValue *next = val->GetNext();
289 delete val;
290 val = next;
291 }
292 m_value.first = NULL;
293 m_last = NULL;
294 }
295
296 wxPropertyValue *wxPropertyValue::NewCopy(void) const
297 {
298 switch (m_type)
299 {
300 case wxPropertyValueInteger:
301 return new wxPropertyValue(m_value.integer);
302 case wxPropertyValuebool:
303 return new wxPropertyValue((bool) (m_value.integer != 0));
304 case wxPropertyValueReal:
305 return new wxPropertyValue(m_value.real);
306 case wxPropertyValueString:
307 return new wxPropertyValue(m_value.string);
308 case wxPropertyValueList:
309 {
310 wxPropertyValue *expr = m_value.first;
311 wxPropertyValue *new_list = new wxPropertyValue;
312 new_list->SetType(wxPropertyValueList);
313 while (expr)
314 {
315 wxPropertyValue *expr2 = expr->NewCopy();
316 new_list->Append(expr2);
317 expr = expr->m_next;
318 }
319 return new_list;
320 }
321 case wxPropertyValueIntegerPtr:
322 return new wxPropertyValue(m_value.integerPtr);
323 case wxPropertyValueRealPtr:
324 return new wxPropertyValue(m_value.realPtr);
325 case wxPropertyValueboolPtr:
326 return new wxPropertyValue(m_value.boolPtr);
327 case wxPropertyValueStringPtr:
328 return new wxPropertyValue(m_value.stringPtr);
329
330 case wxPropertyValueNull:
331 wxFAIL_MSG( _T("Should never get here!\n" ) );
332 break;
333 }
334 return NULL;
335 }
336
337 void wxPropertyValue::Copy(wxPropertyValue& copyFrom)
338 {
339 m_type = copyFrom.Type();
340
341 switch (m_type)
342 {
343 case wxPropertyValueInteger:
344 (*this) = copyFrom.IntegerValue();
345 return ;
346
347 case wxPropertyValueReal:
348 (*this) = copyFrom.RealValue();
349 return ;
350
351 case wxPropertyValueString:
352 (*this) = wxString(copyFrom.StringValue());
353 return ;
354
355 case wxPropertyValuebool:
356 (*this) = copyFrom.BoolValue();
357 return ;
358
359 // Pointers
360 case wxPropertyValueboolPtr:
361 (*this) = copyFrom.BoolValuePtr();
362 return ;
363 case wxPropertyValueRealPtr:
364 (*this) = copyFrom.RealValuePtr();
365 return ;
366 case wxPropertyValueIntegerPtr:
367 (*this) = copyFrom.IntegerValuePtr();
368 return ;
369 case wxPropertyValueStringPtr:
370 {
371 wxChar** s = copyFrom.StringValuePtr();
372
373 #if 0
374 // what is this? are you trying to assign a bool or a string? VA can't figure it out..
375 #if defined(__VISAGECPP__) || defined( __VISUALC__ )
376 (*this) = s;
377 #else
378 (*this) = s != 0;
379 #endif
380 #endif // if 0
381
382 // TODO: check if this is right. MB
383 //
384 (*this) = s;
385
386 return ;
387 }
388
389 case wxPropertyValueList:
390 {
391 m_value.first = NULL;
392 m_next = NULL;
393 m_last = NULL;
394 wxPropertyValue *expr = copyFrom.m_value.first;
395 while (expr)
396 {
397 wxPropertyValue *expr2 = expr->NewCopy();
398 Append(expr2);
399 expr = expr->m_next;
400 }
401 return;
402 }
403 case wxPropertyValueNull:
404 wxFAIL_MSG( _T("Should never get here!\n" ) );
405 break;
406 }
407 }
408
409 // Return nth argument of a clause (starting from 1)
410 wxPropertyValue *wxPropertyValue::Arg(wxPropertyValueType type, int arg) const
411 {
412 wxPropertyValue *expr = m_value.first;
413 for (int i = 1; i < arg; i++)
414 if (expr)
415 expr = expr->m_next;
416
417 if (expr && (expr->m_type == type))
418 return expr;
419 else
420 return NULL;
421 }
422
423 // Return nth argument of a list expression (starting from zero)
424 wxPropertyValue *wxPropertyValue::Nth(int arg) const
425 {
426 if (m_type != wxPropertyValueList)
427 return NULL;
428
429 wxPropertyValue *expr = m_value.first;
430 for (int i = 0; i < arg; i++)
431 if (expr)
432 expr = expr->m_next;
433 else return NULL;
434
435 if (expr)
436 return expr;
437 else
438 return NULL;
439 }
440
441 // Returns the number of elements in a list expression
442 int wxPropertyValue::Number(void) const
443 {
444 if (m_type != wxPropertyValueList)
445 return 0;
446
447 int i = 0;
448 wxPropertyValue *expr = m_value.first;
449 while (expr)
450 {
451 expr = expr->m_next;
452 i ++;
453 }
454 return i;
455 }
456
457 void wxPropertyValue::WritePropertyClause(wxString& stream) // Write this expression as a top-level clause
458 {
459 if (m_type != wxPropertyValueList)
460 return;
461
462 wxPropertyValue *node = m_value.first;
463 if (node)
464 {
465 node->WritePropertyType(stream);
466 stream.Append( _T("(") );
467 node = node->m_next;
468 bool first = TRUE;
469 while (node)
470 {
471 if (!first)
472 stream.Append( _T(" ") );
473 node->WritePropertyType(stream);
474 node = node->m_next;
475 if (node)
476 stream.Append( _T(",\n" ) );
477 first = FALSE;
478 }
479 stream.Append( _T(").\n\n") );
480 }
481 }
482
483 void wxPropertyValue::WritePropertyType(wxString& stream) // Write as any other subexpression
484 {
485 wxString tmp;
486 switch (m_type)
487 {
488 case wxPropertyValueInteger:
489 {
490 tmp.Printf( _T("%ld"), m_value.integer );
491 stream.Append( tmp );
492 break;
493 }
494 case wxPropertyValueIntegerPtr:
495 {
496 tmp.Printf( _T("%ld"), *m_value.integerPtr );
497 stream.Append( tmp );
498 break;
499 }
500 case wxPropertyValuebool:
501 {
502 if (m_value.integer)
503 stream.Append( _T("True") );
504 else
505 stream.Append( _T("False") );
506 break;
507 }
508 case wxPropertyValueboolPtr:
509 {
510 if (*m_value.integerPtr)
511 stream.Append( _T("True") );
512 else
513 stream.Append( _T("False") );
514 break;
515 }
516 case wxPropertyValueReal:
517 {
518 double d = m_value.real;
519 tmp.Printf( _T("%.6g"), d );
520 stream.Append( tmp );
521 break;
522 }
523 case wxPropertyValueRealPtr:
524 {
525 double d = *m_value.realPtr;
526 tmp.Printf( _T("%.6g"), d );
527 stream.Append( tmp );
528 break;
529 }
530 case wxPropertyValueString:
531 {
532 stream.Append( m_value.string );
533 break;
534 }
535 case wxPropertyValueStringPtr:
536 {
537 wxFAIL_MSG( _T("wxPropertyValue::WritePropertyType( wxPropertyValueStringPtr ) not implemented") );
538 /*
539 int i;
540 int len = strlen(*(m_value.stringPtr));
541 for (i = 0; i < len; i++)
542 {
543 char ch = *(m_value.stringPtr)[i];
544
545 }
546 */
547 break;
548 }
549 case wxPropertyValueList:
550 {
551 if (!m_value.first)
552 stream.Append( _T("[]") );
553 else
554 {
555 wxPropertyValue *expr = m_value.first;
556
557 stream.Append( _T("[") );
558 while (expr)
559 {
560 expr->WritePropertyType(stream);
561 expr = expr->m_next;
562 if (expr)
563 stream.Append( _T(", ") );
564 }
565 stream.Append( _T("]") );
566 }
567 break;
568 }
569 case wxPropertyValueNull: break;
570 }
571 }
572
573 wxString wxPropertyValue::GetStringRepresentation(void)
574 {
575 wxString str;
576 WritePropertyType(str);
577 return str;
578 }
579
580 void wxPropertyValue::operator=(const wxPropertyValue& val)
581 {
582 m_modifiedFlag = TRUE;
583 Copy((wxPropertyValue&)val);
584 }
585
586 // void wxPropertyValue::operator=(const char *val)
587 void wxPropertyValue::operator=(const wxString& val1)
588 {
589 const wxChar *val = (const wxChar *)val1;
590
591 m_modifiedFlag = TRUE;
592 if (m_type == wxPropertyValueNull)
593 m_type = wxPropertyValueString;
594
595 if (m_type == wxPropertyValueString)
596 {
597 if (val)
598 m_value.string = copystring(val);
599 else
600 m_value.string = NULL;
601 }
602 else if (m_type == wxPropertyValueStringPtr)
603 {
604 if (*m_value.stringPtr)
605 delete[] *m_value.stringPtr;
606 if (val)
607 *m_value.stringPtr = copystring(val);
608 else
609 *m_value.stringPtr = NULL;
610 }
611
612 m_clientData = NULL;
613 m_next = NULL;
614 m_last = NULL;
615
616 }
617
618 void wxPropertyValue::operator=(const long val)
619 {
620 m_modifiedFlag = TRUE;
621 if (m_type == wxPropertyValueNull)
622 m_type = wxPropertyValueInteger;
623
624 if (m_type == wxPropertyValueInteger)
625 m_value.integer = val;
626 else if (m_type == wxPropertyValueIntegerPtr)
627 *m_value.integerPtr = val;
628 else if (m_type == wxPropertyValueReal)
629 m_value.real = (float)val;
630 else if (m_type == wxPropertyValueRealPtr)
631 *m_value.realPtr = (float)val;
632
633 m_clientData = NULL;
634 m_next = NULL;
635 }
636
637 void wxPropertyValue::operator=(const bool val)
638 {
639 m_modifiedFlag = TRUE;
640 if (m_type == wxPropertyValueNull)
641 m_type = wxPropertyValuebool;
642
643 if (m_type == wxPropertyValuebool)
644 m_value.integer = (long)val;
645 else if (m_type == wxPropertyValueboolPtr)
646 *m_value.boolPtr = val;
647
648 m_clientData = NULL;
649 m_next = NULL;
650 }
651
652 void wxPropertyValue::operator=(const float val)
653 {
654 m_modifiedFlag = TRUE;
655 if (m_type == wxPropertyValueNull)
656 m_type = wxPropertyValueReal;
657
658 if (m_type == wxPropertyValueInteger)
659 m_value.integer = (long)val;
660 else if (m_type == wxPropertyValueIntegerPtr)
661 *m_value.integerPtr = (long)val;
662 else if (m_type == wxPropertyValueReal)
663 m_value.real = val;
664 else if (m_type == wxPropertyValueRealPtr)
665 *m_value.realPtr = val;
666
667 m_clientData = NULL;
668 m_next = NULL;
669 }
670
671 void wxPropertyValue::operator=(const wxChar **val)
672 {
673 m_modifiedFlag = TRUE;
674 m_type = wxPropertyValueStringPtr;
675
676 if (val)
677 m_value.stringPtr = (wxChar **)val;
678 else
679 m_value.stringPtr = NULL;
680 m_clientData = NULL;
681 m_next = NULL;
682 m_last = NULL;
683
684 }
685
686 void wxPropertyValue::operator=(const long *val)
687 {
688 m_modifiedFlag = TRUE;
689 m_type = wxPropertyValueIntegerPtr;
690 m_value.integerPtr = (long *)val;
691 m_clientData = NULL;
692 m_next = NULL;
693 }
694
695 void wxPropertyValue::operator=(const bool *val)
696 {
697 m_modifiedFlag = TRUE;
698 m_type = wxPropertyValueboolPtr;
699 m_value.boolPtr = (bool *)val;
700 m_clientData = NULL;
701 m_next = NULL;
702 }
703
704 void wxPropertyValue::operator=(const float *val)
705 {
706 m_modifiedFlag = TRUE;
707 m_type = wxPropertyValueRealPtr;
708 m_value.realPtr = (float *)val;
709 m_clientData = NULL;
710 m_next = NULL;
711 }
712
713 long wxPropertyValue::IntegerValue(void) const
714 {
715 if (m_type == wxPropertyValueInteger)
716 return m_value.integer;
717 else if (m_type == wxPropertyValueReal)
718 return (long)m_value.real;
719 else if (m_type == wxPropertyValueIntegerPtr)
720 return *m_value.integerPtr;
721 else if (m_type == wxPropertyValueRealPtr)
722 return (long)(*m_value.realPtr);
723 else return 0;
724 }
725
726 long *wxPropertyValue::IntegerValuePtr(void) const
727 {
728 return m_value.integerPtr;
729 }
730
731 float wxPropertyValue::RealValue(void) const {
732 if (m_type == wxPropertyValueReal)
733 return m_value.real;
734 else if (m_type == wxPropertyValueRealPtr)
735 return *m_value.realPtr;
736 else if (m_type == wxPropertyValueInteger)
737 return (float)m_value.integer;
738 else if (m_type == wxPropertyValueIntegerPtr)
739 return (float)*(m_value.integerPtr);
740 else return 0.0;
741 }
742
743 float *wxPropertyValue::RealValuePtr(void) const
744 {
745 return m_value.realPtr;
746 }
747
748 bool wxPropertyValue::BoolValue(void) const {
749 if (m_type == wxPropertyValueReal)
750 return (m_value.real != 0.0);
751 if (m_type == wxPropertyValueRealPtr)
752 return (*(m_value.realPtr) != 0.0);
753 else if (m_type == wxPropertyValueInteger)
754 return (m_value.integer != 0);
755 else if (m_type == wxPropertyValueIntegerPtr)
756 return (*(m_value.integerPtr) != 0);
757 else if (m_type == wxPropertyValuebool)
758 return (m_value.integer != 0);
759 else if (m_type == wxPropertyValueboolPtr)
760 return (*(m_value.boolPtr) != 0);
761 else return FALSE;
762 }
763
764 bool *wxPropertyValue::BoolValuePtr(void) const
765 {
766 return m_value.boolPtr;
767 }
768
769 wxChar *wxPropertyValue::StringValue(void) const {
770 if (m_type == wxPropertyValueString)
771 return m_value.string;
772 else if (m_type == wxPropertyValueStringPtr)
773 return *(m_value.stringPtr);
774 else return NULL;
775 }
776
777 wxChar **wxPropertyValue::StringValuePtr(void) const
778 {
779 return m_value.stringPtr;
780 }
781
782 /*
783 * A property (name plus value)
784 */
785
786 IMPLEMENT_DYNAMIC_CLASS(wxProperty, wxObject)
787
788 wxProperty::wxProperty(void)
789 {
790 m_propertyRole = wxEmptyString;
791 m_propertyValidator = NULL;
792 m_propertyWindow = NULL;
793 m_enabled = TRUE;
794 }
795
796 wxProperty::wxProperty(wxProperty& copyFrom)
797 {
798 m_value = copyFrom.GetValue();
799 m_name = copyFrom.GetName();
800 m_propertyRole = copyFrom.GetRole();
801 m_propertyValidator = copyFrom.GetValidator();
802 m_enabled = copyFrom.IsEnabled();
803 m_propertyWindow = NULL;
804 }
805
806 wxProperty::wxProperty(wxString nm, wxString role, wxPropertyValidator *ed):m_name(nm), m_propertyRole(role)
807 {
808 m_propertyValidator = ed;
809 m_propertyWindow = NULL;
810 m_enabled = TRUE;
811 }
812
813 wxProperty::wxProperty(wxString nm, const wxPropertyValue& val, wxString role, wxPropertyValidator *ed):
814 m_value(val), m_name(nm), m_propertyRole(role)
815 {
816 m_propertyValidator = ed;
817 m_propertyWindow = NULL;
818 m_enabled = TRUE;
819 }
820
821 wxProperty::~wxProperty(void)
822 {
823 if (m_propertyValidator)
824 delete m_propertyValidator;
825 }
826
827 wxPropertyValue& wxProperty::GetValue(void) const
828 {
829 return (wxPropertyValue&) m_value;
830 }
831
832 wxPropertyValidator *wxProperty::GetValidator(void) const
833 {
834 return m_propertyValidator;
835 }
836
837 wxString& wxProperty::GetName(void) const
838 {
839 return (wxString&) m_name;
840 }
841
842 wxString& wxProperty::GetRole(void) const
843 {
844 return (wxString&) m_propertyRole;
845 }
846
847 void wxProperty::SetValue(const wxPropertyValue& val)
848 {
849 m_value = val;
850 }
851
852 void wxProperty::SetValidator(wxPropertyValidator *ed)
853 {
854 m_propertyValidator = ed;
855 }
856
857 void wxProperty::SetRole(wxString& role)
858 {
859 m_propertyRole = role;
860 }
861
862 void wxProperty::SetName(wxString& nm)
863 {
864 m_name = nm;
865 }
866
867 void wxProperty::operator=(const wxPropertyValue& val)
868 {
869 m_value = val;
870 }
871
872 /*
873 * Base property view class
874 */
875
876 IMPLEMENT_DYNAMIC_CLASS(wxPropertyView, wxEvtHandler)
877
878 wxPropertyView::wxPropertyView(long flags)
879 {
880 m_buttonFlags = flags;
881 m_propertySheet = NULL;
882 m_currentValidator = NULL;
883 m_currentProperty = NULL;
884 }
885
886 wxPropertyView::~wxPropertyView(void)
887 {
888 }
889
890 void wxPropertyView::AddRegistry(wxPropertyValidatorRegistry *registry)
891 {
892 m_validatorRegistryList.Append(registry);
893 }
894
895 wxPropertyValidator *wxPropertyView::FindPropertyValidator(wxProperty *property)
896 {
897 if (property->GetValidator())
898 return property->GetValidator();
899
900 wxNode *node = m_validatorRegistryList.First();
901 while (node)
902 {
903 wxPropertyValidatorRegistry *registry = (wxPropertyValidatorRegistry *)node->Data();
904 wxPropertyValidator *validator = registry->GetValidator(property->GetRole());
905 if (validator)
906 return validator;
907 node = node->Next();
908 }
909 return NULL;
910 /*
911 if (!wxDefaultPropertyValidator)
912 wxDefaultPropertyValidator = new wxPropertyListValidator;
913 return wxDefaultPropertyValidator;
914 */
915 }
916
917 /*
918 * Property sheet
919 */
920
921 IMPLEMENT_DYNAMIC_CLASS(wxPropertySheet, wxObject)
922
923 wxPropertySheet::wxPropertySheet(const wxString& name)
924 :m_properties(wxKEY_STRING),m_name(name)
925 {
926 }
927
928 wxPropertySheet::~wxPropertySheet(void)
929 {
930 Clear();
931 }
932
933 void wxPropertySheet::UpdateAllViews( wxPropertyView *WXUNUSED(thisView) )
934 {
935 }
936
937 // Add a property
938 void wxPropertySheet::AddProperty(wxProperty *property)
939 {
940 m_properties.Append((const wxChar*) property->GetName(), property);
941 }
942
943 // Get property by name
944 wxProperty *wxPropertySheet::GetProperty(const wxString& name) const
945 {
946 wxNode *node = m_properties.Find((const wxChar*) name);
947 if (!node)
948 return NULL;
949 else
950 return (wxProperty *)node->Data();
951 }
952
953 bool wxPropertySheet::SetProperty(const wxString& name, const wxPropertyValue& value)
954 {
955 wxProperty* prop = GetProperty(name);
956 if(prop){
957 prop->SetValue(value);
958 return TRUE;
959 }else{
960 return FALSE;
961 }
962 }
963
964 void wxPropertySheet::RemoveProperty(const wxString& name)
965 {
966 wxNode *node = m_properties.Find(name);
967 if(node)
968 {
969 wxProperty *prop = (wxProperty *)node->Data();
970 delete prop;
971 m_properties.DeleteNode(node);
972 }
973 }
974
975 bool wxPropertySheet::HasProperty(const wxString& name) const
976 {
977 return (GetProperty(name)?TRUE:FALSE);
978 }
979
980 // Clear all properties
981 void wxPropertySheet::Clear(void)
982 {
983 wxNode *node = m_properties.First();
984 while (node)
985 {
986 wxProperty *prop = (wxProperty *)node->Data();
987 wxNode *next = node->Next();
988 delete prop;
989 delete node;
990 node = next;
991 }
992 }
993
994 // Sets/clears the modified flag for each property value
995 void wxPropertySheet::SetAllModified(bool flag)
996 {
997 wxNode *node = m_properties.First();
998 while (node)
999 {
1000 wxProperty *prop = (wxProperty *)node->Data();
1001 prop->GetValue().SetModified(flag);
1002 node = node->Next();
1003 }
1004 }
1005
1006 /*
1007 * Property validator registry
1008 *
1009 */
1010
1011 IMPLEMENT_DYNAMIC_CLASS(wxPropertyValidatorRegistry, wxHashTable)
1012
1013 wxPropertyValidatorRegistry::wxPropertyValidatorRegistry(void):wxHashTable(wxKEY_STRING)
1014 {
1015 }
1016
1017 wxPropertyValidatorRegistry::~wxPropertyValidatorRegistry(void)
1018 {
1019 ClearRegistry();
1020 }
1021
1022 void wxPropertyValidatorRegistry::RegisterValidator(const wxString& typeName, wxPropertyValidator *validator)
1023 {
1024 Put((const wxChar*) typeName, validator);
1025 }
1026
1027 wxPropertyValidator *wxPropertyValidatorRegistry::GetValidator(const wxString& typeName)
1028 {
1029 return (wxPropertyValidator *)Get((const wxChar*) typeName);
1030 }
1031
1032 void wxPropertyValidatorRegistry::ClearRegistry(void)
1033 {
1034 BeginFind();
1035 wxNode *node;
1036 while ((node = Next()) != NULL)
1037 {
1038 delete (wxPropertyValidator *)node->Data();
1039 }
1040 }
1041
1042 /*
1043 * Property validator
1044 */
1045
1046
1047 IMPLEMENT_ABSTRACT_CLASS(wxPropertyValidator, wxEvtHandler)
1048
1049 wxPropertyValidator::wxPropertyValidator(long flags)
1050 {
1051 m_validatorFlags = flags;
1052 m_validatorProperty = NULL;
1053 }
1054
1055 wxPropertyValidator::~wxPropertyValidator(void)
1056 {}
1057
1058 bool wxPropertyValidator::StringToFloat (wxChar *s, float *number) {
1059 double num;
1060 bool ok = StringToDouble (s, &num);
1061 *number = (float) num;
1062 return ok;
1063 }
1064
1065 bool wxPropertyValidator::StringToDouble (wxChar *s, double *number) {
1066 bool ok = TRUE;
1067 wxChar *value_ptr;
1068 *number = wxStrtod (s, &value_ptr);
1069 if (value_ptr) {
1070 int len = wxStrlen (value_ptr);
1071 for (int i = 0; i < len; i++) {
1072 ok = (wxIsspace (value_ptr[i]) != 0);
1073 if (!ok) return FALSE;
1074 }
1075 }
1076 return ok;
1077 }
1078
1079 bool wxPropertyValidator::StringToInt (wxChar *s, int *number) {
1080 long num;
1081 bool ok = StringToLong (s, &num);
1082 *number = (int) num;
1083 return ok;
1084 }
1085
1086 bool wxPropertyValidator::StringToLong (wxChar *s, long *number) {
1087 bool ok = TRUE;
1088 wxChar *value_ptr;
1089 *number = wxStrtol (s, &value_ptr, 10);
1090 if (value_ptr) {
1091 int len = wxStrlen (value_ptr);
1092 for (int i = 0; i < len; i++) {
1093 ok = (wxIsspace (value_ptr[i]) != 0);
1094 if (!ok) return FALSE;
1095 }
1096 }
1097 return ok;
1098 }
1099
1100 wxChar *wxPropertyValidator::FloatToString (float number) {
1101 static wxChar buf[20];
1102 wxSprintf (buf, _T("%.6g"), number);
1103 return buf;
1104 }
1105
1106 wxChar *wxPropertyValidator::DoubleToString (double number) {
1107 static wxChar buf[20];
1108 wxSprintf (buf, _T("%.6g"), number);
1109 return buf;
1110 }
1111
1112 wxChar *wxPropertyValidator::IntToString (int number) {
1113 return ::IntToString (number);
1114 }
1115
1116 wxChar *wxPropertyValidator::LongToString (long number) {
1117 return ::LongToString (number);
1118 }