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