Eliminated C++ RTTI (typeid etc) use. Eliminated wxPGVariantData (default default...
[wxWidgets.git] / src / propgrid / property.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/property.cpp
3 // Purpose: wxPGProperty and related support classes
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: 2008-08-23
7 // RCS-ID: $Id:
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
19 #ifndef WX_PRECOMP
20 #include "wx/defs.h"
21 #include "wx/object.h"
22 #include "wx/hash.h"
23 #include "wx/string.h"
24 #include "wx/log.h"
25 #include "wx/event.h"
26 #include "wx/window.h"
27 #include "wx/panel.h"
28 #include "wx/dc.h"
29 #include "wx/dcmemory.h"
30 #include "wx/button.h"
31 #include "wx/pen.h"
32 #include "wx/brush.h"
33 #include "wx/cursor.h"
34 #include "wx/dialog.h"
35 #include "wx/settings.h"
36 #include "wx/msgdlg.h"
37 #include "wx/choice.h"
38 #include "wx/stattext.h"
39 #include "wx/scrolwin.h"
40 #include "wx/dirdlg.h"
41 #include "wx/layout.h"
42 #include "wx/sizer.h"
43 #include "wx/textdlg.h"
44 #include "wx/filedlg.h"
45 #include "wx/statusbr.h"
46 #include "wx/intl.h"
47 #include "wx/frame.h"
48 #endif
49
50 #include <wx/propgrid/propgrid.h>
51
52 #include <typeinfo>
53
54
55 #define PWC_CHILD_SUMMARY_LIMIT 16 // Maximum number of children summarized in a parent property's
56 // value field.
57
58 #define PWC_CHILD_SUMMARY_CHAR_LIMIT 64 // Character limit of summary field when not editing
59
60
61 // -----------------------------------------------------------------------
62
63 static void wxPGDrawFocusRect( wxDC& dc, const wxRect& rect )
64 {
65 #if defined(__WXMSW__) && !defined(__WXWINCE__)
66 // FIXME: Use DrawFocusRect code above (currently it draws solid line
67 // for caption focus but works ok for other stuff).
68 // Also, it seems that this code may not work in future wx versions.
69 dc.SetLogicalFunction(wxINVERT);
70
71 wxPen pen(*wxBLACK,1,wxDOT);
72 pen.SetCap(wxCAP_BUTT);
73 dc.SetPen(pen);
74 dc.SetBrush(*wxTRANSPARENT_BRUSH);
75
76 dc.DrawRectangle(rect);
77
78 dc.SetLogicalFunction(wxCOPY);
79 #else
80 dc.SetLogicalFunction(wxINVERT);
81
82 dc.SetPen(wxPen(*wxBLACK,1,wxDOT));
83 dc.SetBrush(*wxTRANSPARENT_BRUSH);
84
85 dc.DrawRectangle(rect);
86
87 dc.SetLogicalFunction(wxCOPY);
88 #endif
89 }
90
91 // -----------------------------------------------------------------------
92 // wxPGCellRenderer
93 // -----------------------------------------------------------------------
94
95 wxSize wxPGCellRenderer::GetImageSize( const wxPGProperty* WXUNUSED(property),
96 int WXUNUSED(column),
97 int WXUNUSED(item) ) const
98 {
99 return wxSize(0, 0);
100 }
101
102 void wxPGCellRenderer::DrawText( wxDC& dc, const wxRect& rect,
103 int xOffset, const wxString& text ) const
104 {
105 if ( xOffset )
106 xOffset += wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
107 dc.DrawText( text,
108 rect.x+xOffset+wxPG_XBEFORETEXT,
109 rect.y+((rect.height-dc.GetCharHeight())/2) );
110 }
111
112 void wxPGCellRenderer::DrawEditorValue( wxDC& dc, const wxRect& rect,
113 int xOffset, const wxString& text,
114 wxPGProperty* property,
115 const wxPGEditor* editor ) const
116 {
117 if ( xOffset )
118 xOffset += wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
119
120 int yOffset = ((rect.height-dc.GetCharHeight())/2);
121
122 if ( editor )
123 {
124 wxRect rect2(rect);
125 rect2.x += xOffset;
126 rect2.y += yOffset;
127 rect2.height -= yOffset;
128 editor->DrawValue( dc, rect2, property, text );
129 }
130 else
131 {
132 dc.DrawText( text,
133 rect.x+xOffset+wxPG_XBEFORETEXT,
134 rect.y+yOffset );
135 }
136 }
137
138 void wxPGCellRenderer::DrawCaptionSelectionRect( wxDC& dc, int x, int y, int w, int h ) const
139 {
140 wxRect focusRect(x,y+((h-dc.GetCharHeight())/2),w,h);
141 wxPGDrawFocusRect(dc,focusRect);
142 }
143
144 int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell& cell, int flags ) const
145 {
146 int imageOffset = 0;
147
148 if ( !(flags & Selected) )
149 {
150 // Draw using wxPGCell information, if available
151 wxColour fgCol = cell.GetFgCol();
152 if ( fgCol.Ok() )
153 dc.SetTextForeground(fgCol);
154
155 wxColour bgCol = cell.GetBgCol();
156 if ( bgCol.Ok() )
157 {
158 dc.SetPen(bgCol);
159 dc.SetBrush(bgCol);
160 dc.DrawRectangle(rect);
161 }
162 }
163
164 const wxBitmap& bmp = cell.GetBitmap();
165 if ( bmp.Ok() &&
166 // In control, do not draw oversized bitmap
167 (!(flags & Control) || bmp.GetHeight() < rect.height )
168 )
169 {
170 dc.DrawBitmap( bmp,
171 rect.x + wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
172 rect.y + wxPG_CUSTOM_IMAGE_SPACINGY,
173 true );
174 imageOffset = bmp.GetWidth();
175 }
176
177 return imageOffset;
178 }
179
180 // -----------------------------------------------------------------------
181 // wxPGDefaultRenderer
182 // -----------------------------------------------------------------------
183
184 void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
185 const wxPropertyGrid* propertyGrid, wxPGProperty* property,
186 int column, int item, int flags ) const
187 {
188 bool isUnspecified = property->IsValueUnspecified();
189
190 if ( column == 1 && item == -1 )
191 {
192 int cmnVal = property->GetCommonValue();
193 if ( cmnVal >= 0 )
194 {
195 // Common Value
196 if ( !isUnspecified )
197 DrawText( dc, rect, 0, propertyGrid->GetCommonValueLabel(cmnVal) );
198 return;
199 }
200 }
201
202 const wxPGEditor* editor = NULL;
203 const wxPGCell* cell = property->GetCell(column);
204
205 wxString text;
206 int imageOffset = 0;
207
208 // Use choice cell?
209 if ( column == 1 && (flags & Control) )
210 {
211 const wxPGCell* ccell = property->GetCurrentChoice();
212 if ( ccell &&
213 ( ccell->GetBitmap().IsOk() || ccell->GetFgCol().IsOk() || ccell->GetBgCol().IsOk() )
214 )
215 cell = ccell;
216 }
217
218 if ( cell )
219 {
220 int preDrawFlags = flags;
221
222 if ( propertyGrid->GetInternalFlags() & wxPG_FL_CELL_OVERRIDES_SEL )
223 preDrawFlags = preDrawFlags & ~(Selected);
224
225 imageOffset = PreDrawCell( dc, rect, *cell, preDrawFlags );
226 text = cell->GetText();
227 if ( text == wxS("@!") )
228 {
229 if ( column == 0 )
230 text = property->GetLabel();
231 else if ( column == 1 )
232 text = property->GetValueString();
233 else
234 text = wxEmptyString;
235 }
236 }
237 else if ( column == 0 )
238 {
239 // Caption
240 DrawText( dc, rect, 0, property->GetLabel() );
241 }
242 else if ( column == 1 )
243 {
244 if ( !isUnspecified )
245 {
246 editor = property->GetColumnEditor(column);
247
248 // Regular property value
249
250 wxSize imageSize = propertyGrid->GetImageSize(property, item);
251
252 wxPGPaintData paintdata;
253 paintdata.m_parent = propertyGrid;
254 paintdata.m_choiceItem = item;
255
256 if ( imageSize.x > 0 )
257 {
258 wxRect imageRect(rect.x + wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
259 rect.y+wxPG_CUSTOM_IMAGE_SPACINGY,
260 wxPG_CUSTOM_IMAGE_WIDTH,
261 rect.height-(wxPG_CUSTOM_IMAGE_SPACINGY*2));
262
263 /*if ( imageSize.x == wxPG_FULL_CUSTOM_PAINT_WIDTH )
264 {
265 imageRect.width = m_width - imageRect.x;
266 }*/
267
268 dc.SetPen( wxPen(propertyGrid->GetCellTextColour(), 1, wxSOLID) );
269
270 paintdata.m_drawnWidth = imageSize.x;
271 paintdata.m_drawnHeight = imageSize.y;
272
273 if ( !isUnspecified )
274 {
275 property->OnCustomPaint( dc, imageRect, paintdata );
276 }
277 else
278 {
279 dc.SetBrush(*wxWHITE_BRUSH);
280 dc.DrawRectangle(imageRect);
281 }
282
283 imageOffset = paintdata.m_drawnWidth;
284 }
285
286 text = property->GetValueString();
287
288 // Add units string?
289 if ( propertyGrid->GetColumnCount() <= 2 )
290 {
291 wxString unitsString = property->GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
292 if ( unitsString.length() )
293 text = wxString::Format(wxS("%s %s"), text.c_str(), unitsString.c_str() );
294 }
295 }
296
297 if ( text.length() == 0 )
298 {
299 // Try to show inline help if no text
300 wxVariant vInlineHelp = property->GetAttribute(wxPGGlobalVars->m_strInlineHelp);
301 if ( !vInlineHelp.IsNull() )
302 {
303 text = vInlineHelp.GetString();
304 dc.SetTextForeground(propertyGrid->GetCellDisabledTextColour());
305 }
306 }
307 }
308 else if ( column == 2 )
309 {
310 // Add units string?
311 if ( !text.length() )
312 text = property->GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
313 }
314
315 DrawEditorValue( dc, rect, imageOffset, text, property, editor );
316
317 // active caption gets nice dotted rectangle
318 if ( property->IsCategory() /*&& column == 0*/ )
319 {
320 if ( flags & Selected )
321 {
322 if ( imageOffset > 0 )
323 imageOffset += wxCC_CUSTOM_IMAGE_MARGIN2 + 4;
324
325 DrawCaptionSelectionRect( dc,
326 rect.x+wxPG_XBEFORETEXT-wxPG_CAPRECTXMARGIN+imageOffset,
327 rect.y-wxPG_CAPRECTYMARGIN+1,
328 ((wxPropertyCategory*)property)->GetTextExtent(propertyGrid,
329 propertyGrid->GetCaptionFont())
330 +(wxPG_CAPRECTXMARGIN*2),
331 propertyGrid->GetFontHeight()+(wxPG_CAPRECTYMARGIN*2) );
332 }
333 }
334 }
335
336 wxSize wxPGDefaultRenderer::GetImageSize( const wxPGProperty* property,
337 int column,
338 int item ) const
339 {
340 if ( property && column == 1 )
341 {
342 if ( item == -1 )
343 {
344 wxBitmap* bmp = property->GetValueImage();
345
346 if ( bmp && bmp->Ok() )
347 return wxSize(bmp->GetWidth(),bmp->GetHeight());
348 }
349 }
350 return wxSize(0,0);
351 }
352
353 // -----------------------------------------------------------------------
354 // wxPGCell
355 // -----------------------------------------------------------------------
356
357 wxPGCell::wxPGCell()
358 {
359 }
360
361 wxPGCell::wxPGCell( const wxString& text,
362 const wxBitmap& bitmap,
363 const wxColour& fgCol,
364 const wxColour& bgCol )
365 : m_bitmap(bitmap), m_fgCol(fgCol), m_bgCol(bgCol)
366 {
367 m_text = text;
368 }
369
370 // -----------------------------------------------------------------------
371 // wxPGProperty
372 // -----------------------------------------------------------------------
373
374 IMPLEMENT_ABSTRACT_CLASS(wxPGProperty, wxObject)
375
376 wxString* wxPGProperty::sm_wxPG_LABEL = NULL;
377
378 void wxPGProperty::Init()
379 {
380 m_commonValue = -1;
381 m_arrIndex = 0xFFFF;
382 m_parent = NULL;
383
384 m_parentState = (wxPropertyGridPageState*) NULL;
385
386 m_clientData = NULL;
387 m_clientObject = NULL;
388
389 m_customEditor = (wxPGEditor*) NULL;
390 #if wxUSE_VALIDATORS
391 m_validator = (wxValidator*) NULL;
392 #endif
393 m_valueBitmap = (wxBitmap*) NULL;
394
395 m_maxLen = 0; // infinite maximum length
396
397 m_flags = wxPG_PROP_PROPERTY;
398
399 m_depth = 1;
400 m_bgColIndex = 0;
401 m_fgColIndex = 0;
402
403 SetExpanded(true);
404 }
405
406
407 void wxPGProperty::Init( const wxString& label, const wxString& name )
408 {
409 // We really need to check if &label and &name are NULL pointers
410 // (this can if we are called before property grid has been initalized)
411
412 if ( (&label) != NULL && label != wxPG_LABEL )
413 m_label = label;
414
415 if ( (&name) != NULL && name != wxPG_LABEL )
416 DoSetName( name );
417 else
418 DoSetName( m_label );
419
420 Init();
421 }
422
423 wxPGProperty::wxPGProperty()
424 : wxObject()
425 {
426 Init();
427 }
428
429
430 wxPGProperty::wxPGProperty( const wxString& label, const wxString& name )
431 : wxObject()
432 {
433 Init( label, name );
434 }
435
436
437 wxPGProperty::~wxPGProperty()
438 {
439 delete m_clientObject;
440
441 Empty(); // this deletes items
442
443 delete m_valueBitmap;
444 #if wxUSE_VALIDATORS
445 delete m_validator;
446 #endif
447
448 unsigned int i;
449
450 for ( i=0; i<m_cells.size(); i++ )
451 delete (wxPGCell*) m_cells[i];
452
453 // This makes it easier for us to detect dangling pointers
454 m_parent = NULL;
455 }
456
457
458 bool wxPGProperty::IsSomeParent( wxPGProperty* candidate ) const
459 {
460 wxPGProperty* parent = m_parent;
461 do
462 {
463 if ( parent == candidate )
464 return true;
465 parent = parent->m_parent;
466 } while ( parent );
467 return false;
468 }
469
470
471 wxString wxPGProperty::GetName() const
472 {
473 wxPGProperty* parent = GetParent();
474
475 if ( !m_name.length() || !parent || parent->IsCategory() || parent->IsRoot() )
476 return m_name;
477
478 return m_parent->GetName() + wxS(".") + m_name;
479 }
480
481 wxPropertyGrid* wxPGProperty::GetGrid() const
482 {
483 if ( !m_parentState )
484 return NULL;
485 return m_parentState->GetGrid();
486 }
487
488
489 void wxPGProperty::UpdateControl( wxWindow* primary )
490 {
491 if ( primary )
492 GetEditorClass()->UpdateControl(this, primary);
493 }
494
495 bool wxPGProperty::ValidateValue( wxVariant& WXUNUSED(value), wxPGValidationInfo& WXUNUSED(validationInfo) ) const
496 {
497 return true;
498 }
499
500 void wxPGProperty::OnSetValue()
501 {
502 }
503
504 void wxPGProperty::RefreshChildren ()
505 {
506 }
507
508 wxString wxPGProperty::GetColumnText( unsigned int col ) const
509 {
510 wxPGCell* cell = GetCell(col);
511 if ( cell )
512 {
513 return cell->GetText();
514 }
515 else
516 {
517 if ( col == 0 )
518 return GetLabel();
519 else if ( col == 1 )
520 return GetDisplayedString();
521 else if ( col == 2 )
522 return GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
523 }
524
525 return wxEmptyString;
526 }
527
528 void wxPGProperty::GenerateComposedValue( wxString& text, int argFlags ) const
529 {
530 int i;
531 int iMax = m_children.GetCount();
532
533 text.clear();
534 if ( iMax == 0 )
535 return;
536
537 if ( iMax > PWC_CHILD_SUMMARY_LIMIT &&
538 !(argFlags & wxPG_FULL_VALUE) )
539 iMax = PWC_CHILD_SUMMARY_LIMIT;
540
541 int iMaxMinusOne = iMax-1;
542
543 if ( !IsTextEditable() )
544 argFlags |= wxPG_UNEDITABLE_COMPOSITE_FRAGMENT;
545
546 wxPGProperty* curChild = (wxPGProperty*) m_children.Item(0);
547
548 for ( i = 0; i < iMax; i++ )
549 {
550 wxString s;
551 if ( !curChild->IsValueUnspecified() )
552 s = curChild->GetValueString(argFlags|wxPG_COMPOSITE_FRAGMENT);
553
554 bool skip = false;
555 if ( (argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT) && !s.length() )
556 skip = true;
557
558 if ( !curChild->GetChildCount() || skip )
559 text += s;
560 else
561 text += wxS("[") + s + wxS("]");
562
563 if ( i < iMaxMinusOne )
564 {
565 if ( text.length() > PWC_CHILD_SUMMARY_CHAR_LIMIT &&
566 !(argFlags & wxPG_EDITABLE_VALUE) &&
567 !(argFlags & wxPG_FULL_VALUE) )
568 break;
569
570 if ( !skip )
571 {
572 if ( !curChild->GetChildCount() )
573 text += wxS("; ");
574 else
575 text += wxS(" ");
576 }
577
578 curChild = (wxPGProperty*) m_children.Item(i+1);
579 }
580 }
581
582 // Remove superfluous semicolon and space
583 wxString rest;
584 if ( text.EndsWith(wxS("; "), &rest) )
585 text = rest;
586
587 if ( (unsigned int)i < m_children.GetCount() )
588 text += wxS("; ...");
589 }
590
591 wxString wxPGProperty::GetValueAsString( int argFlags ) const
592 {
593 wxCHECK_MSG( GetChildCount() > 0,
594 wxString(),
595 wxT("If user property does not have any children, it must override GetValueAsString") );
596
597 wxString text;
598 GenerateComposedValue(text, argFlags);
599 return text;
600 }
601
602 wxString wxPGProperty::GetValueString( int argFlags ) const
603 {
604 if ( IsValueUnspecified() )
605 return wxEmptyString;
606
607 if ( m_commonValue == -1 )
608 return GetValueAsString(argFlags);
609
610 //
611 // Return common value's string representation
612 wxPropertyGrid* pg = GetGrid();
613 const wxPGCommonValue* cv = pg->GetCommonValue(m_commonValue);
614
615 if ( argFlags & wxPG_FULL_VALUE )
616 {
617 return cv->GetLabel();
618 }
619 else if ( argFlags & wxPG_EDITABLE_VALUE )
620 {
621 return cv->GetEditableText();
622 }
623 else
624 {
625 return cv->GetLabel();
626 }
627 }
628
629 bool wxPGProperty::IntToValue( wxVariant& variant, int number, int WXUNUSED(argFlags) ) const
630 {
631 variant = (long)number;
632 return true;
633 }
634
635 // Convert semicolon delimited tokens into child values.
636 bool wxPGProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
637 {
638 if ( !GetChildCount() )
639 return false;
640
641 unsigned int curChild = 0;
642
643 unsigned int iMax = m_children.GetCount();
644
645 if ( iMax > PWC_CHILD_SUMMARY_LIMIT &&
646 !(argFlags & wxPG_FULL_VALUE) )
647 iMax = PWC_CHILD_SUMMARY_LIMIT;
648
649 bool changed = false;
650
651 wxString token;
652 size_t pos = 0;
653
654 // Its best only to add non-empty group items
655 bool addOnlyIfNotEmpty = false;
656 const wxChar delimeter = wxS(';');
657
658 size_t tokenStart = 0xFFFFFF;
659
660 wxVariantList temp_list;
661 wxVariant list(temp_list);
662
663 int propagatedFlags = argFlags & wxPG_REPORT_ERROR;
664
665 #ifdef __WXDEBUG__
666 bool debug_print = false;
667 #endif
668
669 #ifdef __WXDEBUG__
670 if ( debug_print )
671 wxLogDebug(wxT(">> %s.StringToValue('%s')"),GetLabel().c_str(),text.c_str());
672 #endif
673
674 wxString::const_iterator it = text.begin();
675 wxUniChar a;
676
677 if ( it != text.end() )
678 a = *it;
679 else
680 a = 0;
681
682 for ( ;; )
683 {
684 if ( tokenStart != 0xFFFFFF )
685 {
686 // Token is running
687 if ( a == delimeter || a == 0 )
688 {
689 token = text.substr(tokenStart,pos-tokenStart);
690 token.Trim(true);
691 size_t len = token.length();
692
693 if ( !addOnlyIfNotEmpty || len > 0 )
694 {
695 const wxPGProperty* child = Item(curChild);
696 #ifdef __WXDEBUG__
697 if ( debug_print )
698 wxLogDebug(wxT("token = '%s', child = %s"),token.c_str(),child->GetLabel().c_str());
699 #endif
700
701 if ( len > 0 )
702 {
703 bool wasUnspecified = child->IsValueUnspecified();
704
705 wxVariant variant(child->GetValueRef());
706 if ( child->StringToValue(variant, token, propagatedFlags|wxPG_COMPOSITE_FRAGMENT) )
707 {
708 variant.SetName(child->GetBaseName());
709
710 // Clear unspecified flag only if OnSetValue() didn't
711 // affect it.
712 if ( child->IsValueUnspecified() &&
713 (wasUnspecified || !UsesAutoUnspecified()) )
714 {
715 variant = child->GetDefaultValue();
716 }
717
718 list.Append(variant);
719
720 changed = true;
721 }
722 }
723 else
724 {
725 // Empty, becomes unspecified
726 wxVariant variant2;
727 variant2.SetName(child->GetBaseName());
728 list.Append(variant2);
729 changed = true;
730 }
731
732 curChild++;
733 if ( curChild >= iMax )
734 break;
735 }
736
737 tokenStart = 0xFFFFFF;
738 }
739 }
740 else
741 {
742 // Token is not running
743 if ( a != wxS(' ') )
744 {
745
746 addOnlyIfNotEmpty = false;
747
748 // Is this a group of tokens?
749 if ( a == wxS('[') )
750 {
751 int depth = 1;
752
753 if ( it != text.end() ) it++;
754 pos++;
755 size_t startPos = pos;
756
757 // Group item - find end
758 while ( it != text.end() && depth > 0 )
759 {
760 a = *it;
761 it++;
762 pos++;
763
764 if ( a == wxS(']') )
765 depth--;
766 else if ( a == wxS('[') )
767 depth++;
768 }
769
770 token = text.substr(startPos,pos-startPos-1);
771
772 if ( !token.length() )
773 break;
774
775 const wxPGProperty* child = Item(curChild);
776
777 wxVariant variant(child->GetValueRef());
778 if ( child->StringToValue( variant, token, propagatedFlags ) )
779 {
780 variant.SetName(child->GetBaseName());
781 list.Append(variant);
782 changed = true;
783 }
784 else
785 {
786 // Failed, becomes unspecified
787 wxVariant variant2;
788 variant2.SetName(child->GetBaseName());
789 list.Append(variant2);
790 changed = true;
791 }
792
793 curChild++;
794 if ( curChild >= iMax )
795 break;
796
797 addOnlyIfNotEmpty = true;
798
799 tokenStart = 0xFFFFFF;
800 }
801 else
802 {
803 tokenStart = pos;
804
805 if ( a == delimeter )
806 {
807 pos--;
808 it--;
809 }
810 }
811 }
812 }
813
814 if ( a == 0 )
815 break;
816
817 it++;
818 if ( it != text.end() )
819 {
820 a = *it;
821 }
822 else
823 {
824 a = 0;
825 }
826 pos++;
827 }
828
829 if ( changed )
830 variant = list;
831
832 return changed;
833 }
834
835 bool wxPGProperty::SetValueFromString( const wxString& text, int argFlags )
836 {
837 wxVariant variant(m_value);
838 bool res = StringToValue(variant, text, argFlags);
839 if ( res )
840 SetValue(variant);
841 return res;
842 }
843
844 bool wxPGProperty::SetValueFromInt( long number, int argFlags )
845 {
846 wxVariant variant(m_value);
847 bool res = IntToValue(variant, number, argFlags);
848 if ( res )
849 SetValue(variant);
850 return res;
851 }
852
853 wxSize wxPGProperty::OnMeasureImage( int WXUNUSED(item) ) const
854 {
855 if ( m_valueBitmap )
856 return wxSize(m_valueBitmap->GetWidth(),-1);
857
858 return wxSize(0,0);
859 }
860
861 wxPGCellRenderer* wxPGProperty::GetCellRenderer( int WXUNUSED(column) ) const
862 {
863 return wxPGGlobalVars->m_defaultRenderer;
864 }
865
866 void wxPGProperty::OnCustomPaint( wxDC& dc,
867 const wxRect& rect,
868 wxPGPaintData& )
869 {
870 wxBitmap* bmp = m_valueBitmap;
871
872 wxCHECK_RET( bmp && bmp->Ok(), wxT("invalid bitmap") );
873
874 wxCHECK_RET( rect.x >= 0, wxT("unexpected measure call") );
875
876 dc.DrawBitmap(*bmp,rect.x,rect.y);
877 }
878
879 const wxPGEditor* wxPGProperty::DoGetEditorClass() const
880 {
881 return wxPG_EDITOR(TextCtrl);
882 }
883
884 // Default extra property event handling - that is, none at all.
885 bool wxPGProperty::OnEvent( wxPropertyGrid*, wxWindow*, wxEvent& )
886 {
887 return false;
888 }
889
890
891 void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
892 {
893 if ( !value.IsNull() )
894 {
895 wxVariant tempListVariant;
896
897 SetCommonValue(-1);
898 // List variants are reserved a special purpose
899 // as intermediate containers for child values
900 // of properties with children.
901 if ( value.GetType() == wxPG_VARIANT_TYPE_LIST )
902 {
903 //
904 // However, situation is different for composed string properties
905 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE) )
906 {
907 tempListVariant = value;
908 pList = &tempListVariant;
909 }
910
911 wxVariant newValue;
912 AdaptListToValue(value, &newValue);
913 value = newValue;
914 //wxLogDebug(wxT(">> %s.SetValue() adapted list value to type '%s'"),GetName().c_str(),value.GetType().c_str());
915 }
916
917 if ( HasFlag( wxPG_PROP_AGGREGATE) )
918 flags |= wxPG_SETVAL_AGGREGATED;
919
920 if ( pList && !pList->IsNull() )
921 {
922 wxASSERT( pList->GetType() == wxPG_VARIANT_TYPE_LIST );
923 wxASSERT( GetChildCount() );
924 wxASSERT( !IsCategory() );
925
926 wxVariantList& list = pList->GetList();
927 wxVariantList::iterator node;
928 unsigned int i = 0;
929
930 //wxLogDebug(wxT(">> %s.SetValue() pList parsing"),GetName().c_str());
931
932 // Children in list can be in any order, but we will give hint to
933 // GetPropertyByNameWH(). This optimizes for full list parsing.
934 for ( node = list.begin(); node != list.end(); node++ )
935 {
936 wxVariant& childValue = *((wxVariant*)*node);
937 wxPGProperty* child = GetPropertyByNameWH(childValue.GetName(), i);
938 if ( child )
939 {
940 //wxLogDebug(wxT("%i: child = %s, childValue.GetType()=%s"),i,child->GetBaseName().c_str(),childValue.GetType().c_str());
941 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
942 {
943 if ( child->HasFlag(wxPG_PROP_AGGREGATE) && !(flags & wxPG_SETVAL_AGGREGATED) )
944 {
945 wxVariant listRefCopy = childValue;
946 child->SetValue(childValue, &listRefCopy, flags|wxPG_SETVAL_FROM_PARENT);
947 }
948 else
949 {
950 wxVariant oldVal = child->GetValue();
951 child->SetValue(oldVal, &childValue, flags|wxPG_SETVAL_FROM_PARENT);
952 }
953 }
954 else if ( child->GetValue() != childValue )
955 {
956 // For aggregate properties, we will trust RefreshChildren()
957 // to update child values.
958 if ( !HasFlag(wxPG_PROP_AGGREGATE) )
959 child->SetValue(childValue, NULL, flags|wxPG_SETVAL_FROM_PARENT);
960 if ( flags & wxPG_SETVAL_BY_USER )
961 child->SetFlag(wxPG_PROP_MODIFIED);
962 }
963 }
964 i++;
965 }
966 }
967
968 if ( !value.IsNull() )
969 {
970 m_value = value;
971 OnSetValue();
972
973 if ( !(flags & wxPG_SETVAL_FROM_PARENT) )
974 UpdateParentValues();
975 }
976
977 if ( flags & wxPG_SETVAL_BY_USER )
978 SetFlag(wxPG_PROP_MODIFIED);
979
980 if ( HasFlag(wxPG_PROP_AGGREGATE) )
981 RefreshChildren();
982 }
983 else
984 {
985 if ( m_commonValue != -1 )
986 {
987 wxPropertyGrid* pg = GetGrid();
988 if ( !pg || m_commonValue != pg->GetUnspecifiedCommonValue() )
989 SetCommonValue(-1);
990 }
991
992 m_value = value;
993
994 // Set children to unspecified, but only if aggregate or
995 // value is <composed>
996 if ( AreChildrenComponents() )
997 {
998 unsigned int i;
999 for ( i=0; i<GetChildCount(); i++ )
1000 Item(i)->SetValue(value, NULL, flags|wxPG_SETVAL_FROM_PARENT);
1001 }
1002 }
1003
1004 //
1005 // Update editor control
1006 //
1007
1008 // We need to check for these, otherwise GetGrid() may fail.
1009 if ( flags & wxPG_SETVAL_REFRESH_EDITOR )
1010 RefreshEditor();
1011 }
1012
1013
1014 void wxPGProperty::SetValueInEvent( wxVariant value ) const
1015 {
1016 GetGrid()->ValueChangeInEvent(value);
1017 }
1018
1019 void wxPGProperty::SetFlagRecursively( FlagType flag, bool set )
1020 {
1021 if ( set )
1022 SetFlag(flag);
1023 else
1024 ClearFlag(flag);
1025
1026 unsigned int i;
1027 for ( i = 0; i < GetChildCount(); i++ )
1028 Item(i)->SetFlagRecursively(flag, set);
1029 }
1030
1031 void wxPGProperty::RefreshEditor()
1032 {
1033 if ( m_parent && GetParentState() )
1034 {
1035 wxPropertyGrid* pg = GetParentState()->GetGrid();
1036 if ( pg->GetSelectedProperty() == this )
1037 {
1038 wxWindow* editor = pg->GetEditorControl();
1039 if ( editor )
1040 GetEditorClass()->UpdateControl( this, editor );
1041 }
1042 }
1043 }
1044
1045
1046 wxVariant wxPGProperty::GetDefaultValue() const
1047 {
1048 wxVariant defVal = GetAttribute(wxS("DefaultValue"));
1049 if ( !defVal.IsNull() )
1050 return defVal;
1051
1052 wxVariant value = GetValue();
1053
1054 if ( !value.IsNull() )
1055 {
1056 wxString valueType(value.GetType());
1057
1058 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1059 return wxPGVariant_Zero;
1060 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1061 return wxPGVariant_EmptyString;
1062 if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1063 return wxPGVariant_False;
1064 if ( valueType == wxPG_VARIANT_TYPE_DOUBLE )
1065 return wxVariant(0.0);
1066 if ( valueType == wxPG_VARIANT_TYPE_ARRSTRING )
1067 return wxVariant(wxArrayString());
1068 if ( valueType == wxS("wxLongLong") )
1069 return WXVARIANT(wxLongLong(0));
1070 if ( valueType == wxS("wxULongLong") )
1071 return WXVARIANT(wxULongLong(0));
1072 if ( valueType == wxS("wxColour") )
1073 return WXVARIANT(*wxBLACK);
1074 #if wxUSE_DATETIME
1075 if ( valueType == wxPG_VARIANT_TYPE_DATETIME )
1076 return wxVariant(wxDateTime::Now());
1077 #endif
1078 if ( valueType == wxS("wxFont") )
1079 return WXVARIANT(*wxNORMAL_FONT);
1080 if ( valueType == wxS("wxPoint") )
1081 return WXVARIANT(wxPoint(0, 0));
1082 if ( valueType == wxS("wxSize") )
1083 return WXVARIANT(wxSize(0, 0));
1084 }
1085
1086 return wxVariant();
1087 }
1088
1089 void wxPGProperty::SetCell( int column, wxPGCell* cellObj )
1090 {
1091 if ( column >= (int)m_cells.size() )
1092 m_cells.SetCount(column+1, NULL);
1093
1094 delete (wxPGCell*) m_cells[column];
1095 m_cells[column] = cellObj;
1096 }
1097
1098 void wxPGProperty::SetChoiceSelection( int newValue, const wxPGChoiceInfo& choiceInfo )
1099 {
1100 // Changes value of a property with choices, but only
1101 // works if the value type is long or string.
1102 wxString ts = GetValue().GetType();
1103
1104 wxCHECK_RET( choiceInfo.m_choices, wxT("invalid choiceinfo") );
1105
1106 if ( ts == wxS("long") )
1107 {
1108 SetValue( (long) newValue );
1109 }
1110 else if ( ts == wxS("string") )
1111 {
1112 SetValue( choiceInfo.m_choices->GetLabel(newValue) );
1113 }
1114 }
1115
1116
1117 wxString wxPGProperty::GetChoiceString( unsigned int index )
1118 {
1119 wxPGChoiceInfo ci;
1120 GetChoiceInfo(&ci);
1121 wxASSERT(ci.m_choices);
1122 return ci.m_choices->GetLabel(index);
1123 }
1124
1125 int wxPGProperty::InsertChoice( const wxString& label, int index, int value )
1126 {
1127 wxPropertyGrid* pg = GetGrid();
1128
1129 wxPGChoiceInfo ci;
1130 ci.m_choices = (wxPGChoices*) NULL;
1131 int sel = GetChoiceInfo(&ci);
1132
1133 if ( ci.m_choices )
1134 {
1135 int newSel = sel;
1136
1137 if ( index < 0 )
1138 index = ci.m_choices->GetCount();
1139
1140 if ( index <= sel )
1141 newSel++;
1142
1143 ci.m_choices->Insert(label, index, value);
1144
1145 if ( sel != newSel )
1146 SetChoiceSelection(newSel, ci);
1147
1148 if ( this == pg->GetSelection() )
1149 GetEditorClass()->InsertItem(pg->GetEditorControl(),label,index);
1150
1151 return index;
1152 }
1153
1154 return -1;
1155 }
1156
1157
1158 void wxPGProperty::DeleteChoice( int index )
1159 {
1160 wxPropertyGrid* pg = GetGrid();
1161
1162 wxPGChoiceInfo ci;
1163 ci.m_choices = (wxPGChoices*) NULL;
1164 int sel = GetChoiceInfo(&ci);
1165
1166 if ( ci.m_choices )
1167 {
1168 int newSel = sel;
1169
1170 // Adjust current value
1171 if ( sel == index )
1172 {
1173 SetValueToUnspecified();
1174 newSel = 0;
1175 }
1176 else if ( index < sel )
1177 {
1178 newSel--;
1179 }
1180
1181 ci.m_choices->RemoveAt(index);
1182
1183 if ( sel != newSel )
1184 SetChoiceSelection(newSel, ci);
1185
1186 if ( this == pg->GetSelection() )
1187 GetEditorClass()->DeleteItem(pg->GetEditorControl(), index);
1188 }
1189 }
1190
1191 int wxPGProperty::GetChoiceInfo( wxPGChoiceInfo* WXUNUSED(info) )
1192 {
1193 return -1;
1194 }
1195
1196 wxPGEditorDialogAdapter* wxPGProperty::GetEditorDialog() const
1197 {
1198 return NULL;
1199 }
1200
1201 bool wxPGProperty::DoSetAttribute( const wxString& WXUNUSED(name), wxVariant& WXUNUSED(value) )
1202 {
1203 return false;
1204 }
1205
1206 void wxPGProperty::SetAttribute( const wxString& name, wxVariant value )
1207 {
1208 if ( DoSetAttribute( name, value ) )
1209 {
1210 // Support working without grid, when possible
1211 if ( wxPGGlobalVars->HasExtraStyle( wxPG_EX_WRITEONLY_BUILTIN_ATTRIBUTES ) )
1212 return;
1213 }
1214
1215 m_attributes.Set( name, value );
1216 }
1217
1218 void wxPGProperty::SetAttributes( const wxPGAttributeStorage& attributes )
1219 {
1220 wxPGAttributeStorage::const_iterator it = attributes.StartIteration();
1221 wxVariant variant;
1222
1223 while ( attributes.GetNext(it, variant) )
1224 SetAttribute( variant.GetName(), variant );
1225 }
1226
1227 wxVariant wxPGProperty::DoGetAttribute( const wxString& WXUNUSED(name) ) const
1228 {
1229 return wxVariant();
1230 }
1231
1232
1233 wxVariant wxPGProperty::GetAttribute( const wxString& name ) const
1234 {
1235 return m_attributes.FindValue(name);
1236 }
1237
1238 wxString wxPGProperty::GetAttribute( const wxString& name, const wxString& defVal ) const
1239 {
1240 wxVariant variant = m_attributes.FindValue(name);
1241
1242 if ( !variant.IsNull() )
1243 return variant.GetString();
1244
1245 return defVal;
1246 }
1247
1248 long wxPGProperty::GetAttributeAsLong( const wxString& name, long defVal ) const
1249 {
1250 wxVariant variant = m_attributes.FindValue(name);
1251
1252 return wxPGVariantToInt(variant, defVal);
1253 }
1254
1255 double wxPGProperty::GetAttributeAsDouble( const wxString& name, double defVal ) const
1256 {
1257 double retVal;
1258 wxVariant variant = m_attributes.FindValue(name);
1259
1260 if ( wxPGVariantToDouble(variant, &retVal) )
1261 return retVal;
1262
1263 return defVal;
1264 }
1265
1266 wxVariant wxPGProperty::GetAttributesAsList() const
1267 {
1268 wxVariantList tempList;
1269 wxVariant v( tempList, wxString::Format(wxS("@%s@attr"),m_name.c_str()) );
1270
1271 wxPGAttributeStorage::const_iterator it = m_attributes.StartIteration();
1272 wxVariant variant;
1273
1274 while ( m_attributes.GetNext(it, variant) )
1275 v.Append(variant);
1276
1277 return v;
1278 }
1279
1280 // Slots of utility flags are NULL
1281 const unsigned int gs_propFlagToStringSize = 14;
1282
1283 static const wxChar* gs_propFlagToString[gs_propFlagToStringSize] = {
1284 NULL,
1285 wxT("DISABLED"),
1286 wxT("HIDDEN"),
1287 NULL,
1288 wxT("NOEDITOR"),
1289 wxT("COLLAPSED"),
1290 NULL,
1291 NULL,
1292 NULL,
1293 NULL,
1294 NULL,
1295 NULL,
1296 NULL,
1297 NULL
1298 };
1299
1300 wxString wxPGProperty::GetFlagsAsString( FlagType flagsMask ) const
1301 {
1302 wxString s;
1303 int relevantFlags = m_flags & flagsMask & wxPG_STRING_STORED_FLAGS;
1304 FlagType a = 1;
1305
1306 unsigned int i = 0;
1307 for ( i=0; i<gs_propFlagToStringSize; i++ )
1308 {
1309 if ( relevantFlags & a )
1310 {
1311 const wxChar* fs = gs_propFlagToString[i];
1312 wxASSERT(fs);
1313 if ( s.length() )
1314 s << wxS("|");
1315 s << fs;
1316 }
1317 a = a << 1;
1318 }
1319
1320 return s;
1321 }
1322
1323 void wxPGProperty::SetFlagsFromString( const wxString& str )
1324 {
1325 FlagType flags = 0;
1326
1327 WX_PG_TOKENIZER1_BEGIN(str, wxS('|'))
1328 unsigned int i;
1329 for ( i=0; i<gs_propFlagToStringSize; i++ )
1330 {
1331 const wxChar* fs = gs_propFlagToString[i];
1332 if ( fs && str == fs )
1333 {
1334 flags |= (1<<i);
1335 break;
1336 }
1337 }
1338 WX_PG_TOKENIZER1_END()
1339
1340 m_flags = (m_flags & ~wxPG_STRING_STORED_FLAGS) | flags;
1341 }
1342
1343 wxValidator* wxPGProperty::DoGetValidator() const
1344 {
1345 return (wxValidator*) NULL;
1346 }
1347
1348 wxPGChoices& wxPGProperty::GetChoices()
1349 {
1350 wxPGChoiceInfo choiceInfo;
1351 choiceInfo.m_choices = NULL;
1352 GetChoiceInfo(&choiceInfo);
1353 return *choiceInfo.m_choices;
1354 }
1355
1356 const wxPGChoices& wxPGProperty::GetChoices() const
1357 {
1358 return (const wxPGChoices&) ((wxPGProperty*)this)->GetChoices();
1359 }
1360
1361 unsigned int wxPGProperty::GetChoiceCount() const
1362 {
1363 const wxPGChoices& choices = GetChoices();
1364 if ( &choices && choices.IsOk() )
1365 return choices.GetCount();
1366 return 0;
1367 }
1368
1369 const wxPGChoiceEntry* wxPGProperty::GetCurrentChoice() const
1370 {
1371 wxPGChoiceInfo ci;
1372 ci.m_choices = (wxPGChoices*) NULL;
1373 int index = ((wxPGProperty*)this)->GetChoiceInfo(&ci);
1374 if ( index == -1 || !ci.m_choices || index >= (int)ci.m_choices->GetCount() )
1375 return NULL;
1376
1377 return &(*ci.m_choices)[index];
1378 }
1379
1380 bool wxPGProperty::SetChoices( wxPGChoices& choices )
1381 {
1382 wxPGChoiceInfo ci;
1383 ci.m_choices = (wxPGChoices*) NULL;
1384
1385 // Unref existing
1386 GetChoiceInfo(&ci);
1387 if ( ci.m_choices )
1388 {
1389 ci.m_choices->Assign(choices);
1390
1391 //if ( m_parent )
1392 {
1393 // This may be needed to trigger some initialization
1394 // (but don't do it if property is somewhat uninitialized)
1395 wxVariant defVal = GetDefaultValue();
1396 if ( defVal.IsNull() )
1397 return false;
1398
1399 SetValue(defVal);
1400
1401 return true;
1402 }
1403 }
1404 return false;
1405 }
1406
1407
1408 const wxPGEditor* wxPGProperty::GetEditorClass() const
1409 {
1410 const wxPGEditor* editor;
1411
1412 if ( !m_customEditor )
1413 {
1414 editor = DoGetEditorClass();
1415 }
1416 else
1417 editor = m_customEditor;
1418
1419 //
1420 // Maybe override editor if common value specified
1421 if ( GetDisplayedCommonValueCount() )
1422 {
1423 // TextCtrlAndButton -> ComboBoxAndButton
1424 if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlAndButtonEditor)) )
1425 editor = wxPG_EDITOR(ChoiceAndButton);
1426
1427 // TextCtrl -> ComboBox
1428 else if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlEditor)) )
1429 editor = wxPG_EDITOR(ComboBox);
1430 }
1431
1432 return editor;
1433 }
1434
1435
1436 // Privatizes set of choices
1437 void wxPGProperty::SetChoicesExclusive()
1438 {
1439 wxPGChoiceInfo ci;
1440 ci.m_choices = (wxPGChoices*) NULL;
1441
1442 GetChoiceInfo(&ci);
1443 if ( ci.m_choices )
1444 ci.m_choices->SetExclusive();
1445 }
1446
1447 bool wxPGProperty::HasVisibleChildren() const
1448 {
1449 unsigned int i;
1450
1451 for ( i=0; i<GetChildCount(); i++ )
1452 {
1453 wxPGProperty* child = Item(i);
1454
1455 if ( !child->HasFlag(wxPG_PROP_HIDDEN) )
1456 return true;
1457 }
1458
1459 return false;
1460 }
1461
1462 bool wxPGProperty::PrepareValueForDialogEditing( wxPropertyGrid* propGrid )
1463 {
1464 return propGrid->EditorValidate();
1465 }
1466
1467
1468 bool wxPGProperty::RecreateEditor()
1469 {
1470 wxPropertyGrid* pg = GetGrid();
1471 wxASSERT(pg);
1472
1473 wxPGProperty* selected = pg->GetSelection();
1474 if ( this == selected )
1475 {
1476 pg->DoSelectProperty(this, wxPG_SEL_FORCE);
1477 return true;
1478 }
1479 return false;
1480 }
1481
1482
1483 void wxPGProperty::SetValueImage( wxBitmap& bmp )
1484 {
1485 delete m_valueBitmap;
1486
1487 if ( &bmp && bmp.Ok() )
1488 {
1489 // Resize the image
1490 wxSize maxSz = GetGrid()->GetImageSize();
1491 wxSize imSz(bmp.GetWidth(),bmp.GetHeight());
1492
1493 if ( imSz.x != maxSz.x || imSz.y != maxSz.y )
1494 {
1495 // Create a memory DC
1496 wxBitmap* bmpNew = new wxBitmap(maxSz.x,maxSz.y,bmp.GetDepth());
1497
1498 wxMemoryDC dc;
1499 dc.SelectObject(*bmpNew);
1500
1501 // Scale
1502 // FIXME: This is ugly - use image or wait for scaling patch.
1503 double scaleX = (double)maxSz.x / (double)imSz.x;
1504 double scaleY = (double)maxSz.y / (double)imSz.y;
1505
1506 dc.SetUserScale(scaleX,scaleY);
1507
1508 dc.DrawBitmap( bmp, 0, 0 );
1509
1510 m_valueBitmap = bmpNew;
1511 }
1512 else
1513 {
1514 m_valueBitmap = new wxBitmap(bmp);
1515 }
1516
1517 m_flags |= wxPG_PROP_CUSTOMIMAGE;
1518 }
1519 else
1520 {
1521 m_valueBitmap = NULL;
1522 m_flags &= ~(wxPG_PROP_CUSTOMIMAGE);
1523 }
1524 }
1525
1526
1527 wxPGProperty* wxPGProperty::GetMainParent() const
1528 {
1529 const wxPGProperty* curChild = this;
1530 const wxPGProperty* curParent = m_parent;
1531
1532 while ( curParent && !curParent->IsCategory() )
1533 {
1534 curChild = curParent;
1535 curParent = curParent->m_parent;
1536 }
1537
1538 return (wxPGProperty*) curChild;
1539 }
1540
1541
1542 const wxPGProperty* wxPGProperty::GetLastVisibleSubItem() const
1543 {
1544 //
1545 // Returns last visible sub-item, recursively.
1546 if ( !IsExpanded() || !GetChildCount() )
1547 return this;
1548
1549 return Last()->GetLastVisibleSubItem();
1550 }
1551
1552
1553 bool wxPGProperty::IsVisible() const
1554 {
1555 const wxPGProperty* parent;
1556
1557 if ( HasFlag(wxPG_PROP_HIDDEN) )
1558 return false;
1559
1560 for ( parent = GetParent(); parent != NULL; parent = parent->GetParent() )
1561 {
1562 if ( !parent->IsExpanded() || parent->HasFlag(wxPG_PROP_HIDDEN) )
1563 return false;
1564 }
1565
1566 return true;
1567 }
1568
1569 wxPropertyGrid* wxPGProperty::GetGridIfDisplayed() const
1570 {
1571 wxPropertyGridPageState* state = GetParentState();
1572 wxPropertyGrid* propGrid = state->GetGrid();
1573 if ( state == propGrid->GetState() )
1574 return propGrid;
1575 return NULL;
1576 }
1577
1578
1579 int wxPGProperty::GetY2( int lh ) const
1580 {
1581 const wxPGProperty* parent;
1582 const wxPGProperty* child = this;
1583
1584 int y = 0;
1585
1586 for ( parent = GetParent(); parent != NULL; parent = child->GetParent() )
1587 {
1588 if ( !parent->IsExpanded() )
1589 return -1;
1590 y += parent->GetChildrenHeight(lh, child->GetIndexInParent());
1591 y += lh;
1592 child = parent;
1593 }
1594
1595 y -= lh; // need to reduce one level
1596
1597 return y;
1598 }
1599
1600
1601 int wxPGProperty::GetY() const
1602 {
1603 return GetY2(GetGrid()->GetRowHeight());
1604 }
1605
1606 // This is used by Insert etc.
1607 void wxPGProperty::AddChild2( wxPGProperty* prop, int index, bool correct_mode )
1608 {
1609 if ( index < 0 || (size_t)index >= m_children.GetCount() )
1610 {
1611 if ( correct_mode ) prop->m_arrIndex = m_children.GetCount();
1612 m_children.Add( prop );
1613 }
1614 else
1615 {
1616 m_children.Insert( prop, index );
1617 if ( correct_mode ) FixIndexesOfChildren( index );
1618 }
1619
1620 prop->m_parent = this;
1621 }
1622
1623 // This is used by properties that have fixed sub-properties
1624 void wxPGProperty::AddChild( wxPGProperty* prop )
1625 {
1626 wxASSERT_MSG( prop->GetBaseName().length(),
1627 "Property's children must have unique, non-empty names within their scope" );
1628
1629 prop->m_arrIndex = m_children.GetCount();
1630 m_children.Add( prop );
1631
1632 int custImgHeight = prop->OnMeasureImage().y;
1633 if ( custImgHeight < 0 /*|| custImgHeight > 1*/ )
1634 prop->m_flags |= wxPG_PROP_CUSTOMIMAGE;
1635
1636 prop->m_parent = this;
1637 }
1638
1639
1640 void wxPGProperty::AdaptListToValue( wxVariant& list, wxVariant* value ) const
1641 {
1642 wxASSERT( GetChildCount() );
1643 wxASSERT( !IsCategory() );
1644
1645 *value = GetValue();
1646
1647 if ( !list.GetCount() )
1648 return;
1649
1650 wxASSERT( GetChildCount() >= (unsigned int)list.GetCount() );
1651
1652 bool allChildrenSpecified;
1653
1654 // Don't fully update aggregate properties unless all children have
1655 // specified value
1656 if ( HasFlag(wxPG_PROP_AGGREGATE) )
1657 allChildrenSpecified = AreAllChildrenSpecified(&list);
1658 else
1659 allChildrenSpecified = true;
1660
1661 wxVariant childValue = list[0];
1662 unsigned int i;
1663 unsigned int n = 0;
1664
1665 //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
1666
1667 for ( i=0; i<GetChildCount(); i++ )
1668 {
1669 const wxPGProperty* child = Item(i);
1670
1671 if ( childValue.GetName() == child->GetBaseName() )
1672 {
1673 //wxLogDebug(wxT(" %s(n=%i), %s"),childValue.GetName().c_str(),n,childValue.GetType().c_str());
1674
1675 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
1676 {
1677 wxVariant cv2(child->GetValue());
1678 child->AdaptListToValue(childValue, &cv2);
1679 childValue = cv2;
1680 }
1681
1682 if ( allChildrenSpecified )
1683 ChildChanged(*value, i, childValue);
1684 n++;
1685 if ( n == (unsigned int)list.GetCount() )
1686 break;
1687 childValue = list[n];
1688 }
1689 }
1690 }
1691
1692
1693 void wxPGProperty::FixIndexesOfChildren( size_t starthere )
1694 {
1695 size_t i;
1696 for ( i=starthere;i<GetChildCount();i++)
1697 Item(i)->m_arrIndex = i;
1698 }
1699
1700
1701 // Returns (direct) child property with given name (or NULL if not found)
1702 wxPGProperty* wxPGProperty::GetPropertyByName( const wxString& name ) const
1703 {
1704 size_t i;
1705
1706 for ( i=0; i<GetChildCount(); i++ )
1707 {
1708 wxPGProperty* p = Item(i);
1709 if ( p->m_name == name )
1710 return p;
1711 }
1712
1713 // Does it have point, then?
1714 int pos = name.Find(wxS('.'));
1715 if ( pos <= 0 )
1716 return (wxPGProperty*) NULL;
1717
1718 wxPGProperty* p = GetPropertyByName(name. substr(0,pos));
1719
1720 if ( !p || !p->GetChildCount() )
1721 return NULL;
1722
1723 return p->GetPropertyByName(name.substr(pos+1,name.length()-pos-1));
1724 }
1725
1726 wxPGProperty* wxPGProperty::GetPropertyByNameWH( const wxString& name, unsigned int hintIndex ) const
1727 {
1728 unsigned int i = hintIndex;
1729
1730 if ( i >= GetChildCount() )
1731 i = 0;
1732
1733 unsigned int lastIndex = i - 1;
1734
1735 if ( lastIndex >= GetChildCount() )
1736 lastIndex = GetChildCount() - 1;
1737
1738 for (;;)
1739 {
1740 wxPGProperty* p = Item(i);
1741 if ( p->m_name == name )
1742 return p;
1743
1744 if ( i == lastIndex )
1745 break;
1746
1747 i++;
1748 if ( i == GetChildCount() )
1749 i = 0;
1750 };
1751
1752 return NULL;
1753 }
1754
1755 int wxPGProperty::GetChildrenHeight( int lh, int iMax_ ) const
1756 {
1757 // Returns height of children, recursively, and
1758 // by taking expanded/collapsed status into account.
1759 //
1760 // iMax is used when finding property y-positions.
1761 //
1762 unsigned int i = 0;
1763 int h = 0;
1764
1765 if ( iMax_ == -1 )
1766 iMax_ = GetChildCount();
1767
1768 unsigned int iMax = iMax_;
1769
1770 wxASSERT( iMax <= GetChildCount() );
1771
1772 if ( !IsExpanded() && GetParent() )
1773 return 0;
1774
1775 while ( i < iMax )
1776 {
1777 wxPGProperty* pwc = (wxPGProperty*) Item(i);
1778
1779 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1780 {
1781 if ( !pwc->IsExpanded() ||
1782 pwc->GetChildCount() == 0 )
1783 h += lh;
1784 else
1785 h += pwc->GetChildrenHeight(lh) + lh;
1786 }
1787
1788 i++;
1789 }
1790
1791 return h;
1792 }
1793
1794 wxPGProperty* wxPGProperty::GetItemAtY( unsigned int y, unsigned int lh, unsigned int* nextItemY ) const
1795 {
1796 wxASSERT( nextItemY );
1797
1798 // Linear search at the moment
1799 //
1800 // nextItemY = y of next visible property, final value will be written back.
1801 wxPGProperty* result = NULL;
1802 wxPGProperty* current = NULL;
1803 unsigned int iy = *nextItemY;
1804 unsigned int i = 0;
1805 unsigned int iMax = GetChildCount();
1806
1807 while ( i < iMax )
1808 {
1809 wxPGProperty* pwc = Item(i);
1810
1811 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1812 {
1813 // Found?
1814 if ( y < iy )
1815 {
1816 result = current;
1817 break;
1818 }
1819
1820 iy += lh;
1821
1822 if ( pwc->IsExpanded() &&
1823 pwc->GetChildCount() > 0 )
1824 {
1825 result = (wxPGProperty*) pwc->GetItemAtY( y, lh, &iy );
1826 if ( result )
1827 break;
1828 }
1829
1830 current = pwc;
1831 }
1832
1833 i++;
1834 }
1835
1836 // Found?
1837 if ( !result && y < iy )
1838 result = current;
1839
1840 *nextItemY = iy;
1841
1842 /*
1843 if ( current )
1844 wxLogDebug(wxT("%s::GetItemAtY(%i) -> %s"),this->GetLabel().c_str(),y,current->GetLabel().c_str());
1845 else
1846 wxLogDebug(wxT("%s::GetItemAtY(%i) -> NULL"),this->GetLabel().c_str(),y);
1847 */
1848
1849 return (wxPGProperty*) result;
1850 }
1851
1852 void wxPGProperty::Empty()
1853 {
1854 size_t i;
1855 if ( !HasFlag(wxPG_PROP_CHILDREN_ARE_COPIES) )
1856 {
1857 for ( i=0; i<GetChildCount(); i++ )
1858 {
1859 wxPGProperty* p = (wxPGProperty*) Item(i);
1860 delete p;
1861 }
1862 }
1863
1864 m_children.Empty();
1865 }
1866
1867 void wxPGProperty::ChildChanged( wxVariant& WXUNUSED(thisValue),
1868 int WXUNUSED(childIndex),
1869 wxVariant& WXUNUSED(childValue) ) const
1870 {
1871 }
1872
1873 bool wxPGProperty::AreAllChildrenSpecified( wxVariant* pendingList ) const
1874 {
1875 unsigned int i;
1876
1877 const wxVariantList* pList = NULL;
1878 wxVariantList::const_iterator node;
1879
1880 if ( pendingList )
1881 {
1882 pList = &pendingList->GetList();
1883 node = pList->begin();
1884 }
1885
1886 for ( i=0; i<GetChildCount(); i++ )
1887 {
1888 wxPGProperty* child = Item(i);
1889 const wxVariant* listValue = NULL;
1890 wxVariant value;
1891
1892 if ( pendingList )
1893 {
1894 const wxString& childName = child->GetBaseName();
1895
1896 for ( ; node != pList->end(); node++ )
1897 {
1898 const wxVariant& item = *((const wxVariant*)*node);
1899 if ( item.GetName() == childName )
1900 {
1901 listValue = &item;
1902 value = item;
1903 break;
1904 }
1905 }
1906 }
1907
1908 if ( !listValue )
1909 value = child->GetValue();
1910
1911 if ( value.IsNull() )
1912 return false;
1913
1914 // Check recursively
1915 if ( child->GetChildCount() )
1916 {
1917 const wxVariant* childList = NULL;
1918
1919 if ( listValue && listValue->GetType() == wxPG_VARIANT_TYPE_LIST )
1920 childList = listValue;
1921
1922 if ( !child->AreAllChildrenSpecified((wxVariant*)childList) )
1923 return false;
1924 }
1925 }
1926
1927 return true;
1928 }
1929
1930 wxPGProperty* wxPGProperty::UpdateParentValues()
1931 {
1932 wxPGProperty* parent = m_parent;
1933 if ( parent && parent->HasFlag(wxPG_PROP_COMPOSED_VALUE) &&
1934 !parent->IsCategory() && !parent->IsRoot() )
1935 {
1936 wxString s;
1937 parent->GenerateComposedValue(s, 0);
1938 parent->m_value = s;
1939 return parent->UpdateParentValues();
1940 }
1941 return this;
1942 }
1943
1944 bool wxPGProperty::IsTextEditable() const
1945 {
1946 if ( HasFlag(wxPG_PROP_READONLY) )
1947 return false;
1948
1949 if ( HasFlag(wxPG_PROP_NOEDITOR) &&
1950 (GetChildCount() ||
1951 wxString(GetEditorClass()->GetClassInfo()->GetClassName()).EndsWith(wxS("Button")))
1952 )
1953 return false;
1954
1955 return true;
1956 }
1957
1958 // Call for after sub-properties added with AddChild
1959 void wxPGProperty::PrepareSubProperties()
1960 {
1961 wxPropertyGridPageState* state = GetParentState();
1962
1963 wxASSERT(state);
1964
1965 if ( !GetChildCount() )
1966 return;
1967
1968 wxByte depth = m_depth + 1;
1969 wxByte depthBgCol = m_depthBgCol;
1970
1971 FlagType inheritFlags = m_flags & wxPG_INHERITED_PROPFLAGS;
1972
1973 wxByte bgColIndex = m_bgColIndex;
1974 wxByte fgColIndex = m_fgColIndex;
1975
1976 //
1977 // Set some values to the children
1978 //
1979 size_t i = 0;
1980 wxPGProperty* nparent = this;
1981
1982 while ( i < nparent->GetChildCount() )
1983 {
1984 wxPGProperty* np = nparent->Item(i);
1985
1986 np->m_parentState = state;
1987 np->m_flags |= inheritFlags; // Hideable also if parent.
1988 np->m_depth = depth;
1989 np->m_depthBgCol = depthBgCol;
1990 np->m_bgColIndex = bgColIndex;
1991 np->m_fgColIndex = fgColIndex;
1992
1993 // Also handle children of children
1994 if ( np->GetChildCount() > 0 )
1995 {
1996 nparent = np;
1997 i = 0;
1998
1999 // Init
2000 nparent->SetParentalType(wxPG_PROP_AGGREGATE);
2001 nparent->SetExpanded(false);
2002 depth++;
2003 }
2004 else
2005 {
2006 // Next sibling
2007 i++;
2008 }
2009
2010 // After reaching last sibling, go back to processing
2011 // siblings of the parent
2012 while ( i >= nparent->GetChildCount() )
2013 {
2014 // Exit the loop when top parent hit
2015 if ( nparent == this )
2016 break;
2017
2018 depth--;
2019
2020 i = nparent->GetArrIndex() + 1;
2021 nparent = nparent->GetParent();
2022 }
2023 }
2024 }
2025
2026 // Call after fixed sub-properties added/removed after creation.
2027 // if oldSelInd >= 0 and < new max items, then selection is
2028 // moved to it. Note: oldSelInd -2 indicates that this property
2029 // should be selected.
2030 void wxPGProperty::SubPropsChanged( int oldSelInd )
2031 {
2032 wxPropertyGridPageState* state = GetParentState();
2033 wxPropertyGrid* grid = state->GetGrid();
2034
2035 PrepareSubProperties();
2036
2037 wxPGProperty* sel = (wxPGProperty*) NULL;
2038 if ( oldSelInd >= (int)m_children.GetCount() )
2039 oldSelInd = (int)m_children.GetCount() - 1;
2040
2041 if ( oldSelInd >= 0 )
2042 sel = (wxPGProperty*) m_children[oldSelInd];
2043 else if ( oldSelInd == -2 )
2044 sel = this;
2045
2046 if ( sel )
2047 state->DoSelectProperty(sel);
2048
2049 if ( state == grid->GetState() )
2050 {
2051 grid->GetPanel()->Refresh();
2052 }
2053 }
2054
2055 // -----------------------------------------------------------------------
2056 // wxPGRootProperty
2057 // -----------------------------------------------------------------------
2058
2059 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPGRootProperty,none,TextCtrl)
2060 IMPLEMENT_DYNAMIC_CLASS(wxPGRootProperty, wxPGProperty)
2061
2062
2063 wxPGRootProperty::wxPGRootProperty()
2064 : wxPGProperty()
2065 {
2066 #ifdef __WXDEBUG__
2067 m_name = wxS("<root>");
2068 #endif
2069 SetParentalType(0);
2070 m_depth = 0;
2071 }
2072
2073
2074 wxPGRootProperty::~wxPGRootProperty()
2075 {
2076 }
2077
2078
2079 // -----------------------------------------------------------------------
2080 // wxPropertyCategory
2081 // -----------------------------------------------------------------------
2082
2083 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPropertyCategory,none,TextCtrl)
2084 IMPLEMENT_DYNAMIC_CLASS(wxPropertyCategory, wxPGProperty)
2085
2086 void wxPropertyCategory::Init()
2087 {
2088 // don't set colour - prepareadditem method should do this
2089 SetParentalType(wxPG_PROP_CATEGORY);
2090 m_capFgColIndex = 1;
2091 m_textExtent = -1;
2092 }
2093
2094 wxPropertyCategory::wxPropertyCategory()
2095 : wxPGProperty()
2096 {
2097 Init();
2098 }
2099
2100
2101 wxPropertyCategory::wxPropertyCategory( const wxString &label, const wxString& name )
2102 : wxPGProperty(label,name)
2103 {
2104 Init();
2105 }
2106
2107
2108 wxPropertyCategory::~wxPropertyCategory()
2109 {
2110 }
2111
2112
2113 wxString wxPropertyCategory::GetValueAsString( int ) const
2114 {
2115 return wxEmptyString;
2116 }
2117
2118 int wxPropertyCategory::GetTextExtent( const wxWindow* wnd, const wxFont& font ) const
2119 {
2120 if ( m_textExtent > 0 )
2121 return m_textExtent;
2122 int x = 0, y = 0;
2123 ((wxWindow*)wnd)->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2124 return x;
2125 }
2126
2127 void wxPropertyCategory::CalculateTextExtent( wxWindow* wnd, const wxFont& font )
2128 {
2129 int x = 0, y = 0;
2130 wnd->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2131 m_textExtent = x;
2132 }
2133
2134 // -----------------------------------------------------------------------
2135 // wxPGAttributeStorage
2136 // -----------------------------------------------------------------------
2137
2138 wxPGAttributeStorage::wxPGAttributeStorage()
2139 {
2140 }
2141
2142 wxPGAttributeStorage::~wxPGAttributeStorage()
2143 {
2144 wxPGHashMapS2P::iterator it;
2145
2146 for ( it = m_map.begin(); it != m_map.end(); it++ )
2147 {
2148 wxVariantData* data = (wxVariantData*) it->second;
2149 data->DecRef();
2150 }
2151 }
2152
2153 void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value )
2154 {
2155 wxVariantData* data = value.GetData();
2156
2157 // Free old, if any
2158 wxPGHashMapS2P::iterator it = m_map.find(name);
2159 if ( it != m_map.end() )
2160 ((wxVariantData*)it->second)->DecRef();
2161
2162 if ( data )
2163 data->IncRef();
2164
2165 m_map[name] = data;
2166 }
2167