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