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