Removed wxPGProperty:GetArrIndex() in favor of GetIndexInParent()
[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
480 void wxPGProperty::UpdateControl( wxWindow* primary )
481 {
482 if ( primary )
483 GetEditorClass()->UpdateControl(this, primary);
484 }
485
486 bool wxPGProperty::ValidateValue( wxVariant& WXUNUSED(value), wxPGValidationInfo& WXUNUSED(validationInfo) ) const
487 {
488 return true;
489 }
490
491 void wxPGProperty::OnSetValue()
492 {
493 }
494
495 void wxPGProperty::RefreshChildren ()
496 {
497 }
498
499 wxString wxPGProperty::GetColumnText( unsigned int col ) const
500 {
501 wxPGCell* cell = GetCell(col);
502 if ( cell )
503 {
504 return cell->GetText();
505 }
506 else
507 {
508 if ( col == 0 )
509 return GetLabel();
510 else if ( col == 1 )
511 return GetDisplayedString();
512 else if ( col == 2 )
513 return GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
514 }
515
516 return wxEmptyString;
517 }
518
519 void wxPGProperty::GenerateComposedValue( wxString& text, int argFlags ) const
520 {
521 int i;
522 int iMax = m_children.GetCount();
523
524 text.clear();
525 if ( iMax == 0 )
526 return;
527
528 if ( iMax > PWC_CHILD_SUMMARY_LIMIT &&
529 !(argFlags & wxPG_FULL_VALUE) )
530 iMax = PWC_CHILD_SUMMARY_LIMIT;
531
532 int iMaxMinusOne = iMax-1;
533
534 if ( !IsTextEditable() )
535 argFlags |= wxPG_UNEDITABLE_COMPOSITE_FRAGMENT;
536
537 wxPGProperty* curChild = (wxPGProperty*) m_children.Item(0);
538
539 for ( i = 0; i < iMax; i++ )
540 {
541 wxString s;
542 if ( !curChild->IsValueUnspecified() )
543 s = curChild->GetValueString(argFlags|wxPG_COMPOSITE_FRAGMENT);
544
545 bool skip = false;
546 if ( (argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT) && !s.length() )
547 skip = true;
548
549 if ( !curChild->GetChildCount() || skip )
550 text += s;
551 else
552 text += wxS("[") + s + wxS("]");
553
554 if ( i < iMaxMinusOne )
555 {
556 if ( text.length() > PWC_CHILD_SUMMARY_CHAR_LIMIT &&
557 !(argFlags & wxPG_EDITABLE_VALUE) &&
558 !(argFlags & wxPG_FULL_VALUE) )
559 break;
560
561 if ( !skip )
562 {
563 if ( !curChild->GetChildCount() )
564 text += wxS("; ");
565 else
566 text += wxS(" ");
567 }
568
569 curChild = (wxPGProperty*) m_children.Item(i+1);
570 }
571 }
572
573 // Remove superfluous semicolon and space
574 wxString rest;
575 if ( text.EndsWith(wxS("; "), &rest) )
576 text = rest;
577
578 if ( (unsigned int)i < m_children.GetCount() )
579 text += wxS("; ...");
580 }
581
582 wxString wxPGProperty::GetValueAsString( int argFlags ) const
583 {
584 wxCHECK_MSG( GetChildCount() > 0,
585 wxString(),
586 wxT("If user property does not have any children, it must override GetValueAsString") );
587
588 wxString text;
589 GenerateComposedValue(text, argFlags);
590 return text;
591 }
592
593 wxString wxPGProperty::GetValueString( int argFlags ) const
594 {
595 if ( IsValueUnspecified() )
596 return wxEmptyString;
597
598 if ( m_commonValue == -1 )
599 return GetValueAsString(argFlags);
600
601 //
602 // Return common value's string representation
603 wxPropertyGrid* pg = GetGrid();
604 const wxPGCommonValue* cv = pg->GetCommonValue(m_commonValue);
605
606 if ( argFlags & wxPG_FULL_VALUE )
607 {
608 return cv->GetLabel();
609 }
610 else if ( argFlags & wxPG_EDITABLE_VALUE )
611 {
612 return cv->GetEditableText();
613 }
614 else
615 {
616 return cv->GetLabel();
617 }
618 }
619
620 bool wxPGProperty::IntToValue( wxVariant& variant, int number, int WXUNUSED(argFlags) ) const
621 {
622 variant = (long)number;
623 return true;
624 }
625
626 // Convert semicolon delimited tokens into child values.
627 bool wxPGProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
628 {
629 if ( !GetChildCount() )
630 return false;
631
632 unsigned int curChild = 0;
633
634 unsigned int iMax = m_children.GetCount();
635
636 if ( iMax > PWC_CHILD_SUMMARY_LIMIT &&
637 !(argFlags & wxPG_FULL_VALUE) )
638 iMax = PWC_CHILD_SUMMARY_LIMIT;
639
640 bool changed = false;
641
642 wxString token;
643 size_t pos = 0;
644
645 // Its best only to add non-empty group items
646 bool addOnlyIfNotEmpty = false;
647 const wxChar delimeter = wxS(';');
648
649 size_t tokenStart = 0xFFFFFF;
650
651 wxVariantList temp_list;
652 wxVariant list(temp_list);
653
654 int propagatedFlags = argFlags & wxPG_REPORT_ERROR;
655
656 #ifdef __WXDEBUG__
657 bool debug_print = false;
658 #endif
659
660 #ifdef __WXDEBUG__
661 if ( debug_print )
662 wxLogDebug(wxT(">> %s.StringToValue('%s')"),GetLabel().c_str(),text.c_str());
663 #endif
664
665 wxString::const_iterator it = text.begin();
666 wxUniChar a;
667
668 if ( it != text.end() )
669 a = *it;
670 else
671 a = 0;
672
673 for ( ;; )
674 {
675 if ( tokenStart != 0xFFFFFF )
676 {
677 // Token is running
678 if ( a == delimeter || a == 0 )
679 {
680 token = text.substr(tokenStart,pos-tokenStart);
681 token.Trim(true);
682 size_t len = token.length();
683
684 if ( !addOnlyIfNotEmpty || len > 0 )
685 {
686 const wxPGProperty* child = Item(curChild);
687 #ifdef __WXDEBUG__
688 if ( debug_print )
689 wxLogDebug(wxT("token = '%s', child = %s"),token.c_str(),child->GetLabel().c_str());
690 #endif
691
692 if ( len > 0 )
693 {
694 bool wasUnspecified = child->IsValueUnspecified();
695
696 wxVariant variant(child->GetValueRef());
697 if ( child->StringToValue(variant, token, propagatedFlags|wxPG_COMPOSITE_FRAGMENT) )
698 {
699 variant.SetName(child->GetBaseName());
700
701 // Clear unspecified flag only if OnSetValue() didn't
702 // affect it.
703 if ( child->IsValueUnspecified() &&
704 (wasUnspecified || !UsesAutoUnspecified()) )
705 {
706 variant = child->GetDefaultValue();
707 }
708
709 list.Append(variant);
710
711 changed = true;
712 }
713 }
714 else
715 {
716 // Empty, becomes unspecified
717 wxVariant variant2;
718 variant2.SetName(child->GetBaseName());
719 list.Append(variant2);
720 changed = true;
721 }
722
723 curChild++;
724 if ( curChild >= iMax )
725 break;
726 }
727
728 tokenStart = 0xFFFFFF;
729 }
730 }
731 else
732 {
733 // Token is not running
734 if ( a != wxS(' ') )
735 {
736
737 addOnlyIfNotEmpty = false;
738
739 // Is this a group of tokens?
740 if ( a == wxS('[') )
741 {
742 int depth = 1;
743
744 if ( it != text.end() ) it++;
745 pos++;
746 size_t startPos = pos;
747
748 // Group item - find end
749 while ( it != text.end() && depth > 0 )
750 {
751 a = *it;
752 it++;
753 pos++;
754
755 if ( a == wxS(']') )
756 depth--;
757 else if ( a == wxS('[') )
758 depth++;
759 }
760
761 token = text.substr(startPos,pos-startPos-1);
762
763 if ( !token.length() )
764 break;
765
766 const wxPGProperty* child = Item(curChild);
767
768 wxVariant oldChildValue = child->GetValue();
769 wxVariant variant(oldChildValue);
770 bool stvRes = child->StringToValue( variant, token, propagatedFlags );
771 if ( stvRes || (variant != oldChildValue) )
772 {
773 if ( stvRes )
774 changed = true;
775 }
776 else
777 {
778 // Failed, becomes unspecified
779 variant.MakeNull();
780 changed = true;
781 }
782
783 variant.SetName(child->GetBaseName());
784 list.Append(variant);
785
786 curChild++;
787 if ( curChild >= iMax )
788 break;
789
790 addOnlyIfNotEmpty = true;
791
792 tokenStart = 0xFFFFFF;
793 }
794 else
795 {
796 tokenStart = pos;
797
798 if ( a == delimeter )
799 {
800 pos--;
801 it--;
802 }
803 }
804 }
805 }
806
807 if ( a == 0 )
808 break;
809
810 it++;
811 if ( it != text.end() )
812 {
813 a = *it;
814 }
815 else
816 {
817 a = 0;
818 }
819 pos++;
820 }
821
822 if ( changed )
823 variant = list;
824
825 return changed;
826 }
827
828 bool wxPGProperty::SetValueFromString( const wxString& text, int argFlags )
829 {
830 wxVariant variant(m_value);
831 bool res = StringToValue(variant, text, argFlags);
832 if ( res )
833 SetValue(variant);
834 return res;
835 }
836
837 bool wxPGProperty::SetValueFromInt( long number, int argFlags )
838 {
839 wxVariant variant(m_value);
840 bool res = IntToValue(variant, number, argFlags);
841 if ( res )
842 SetValue(variant);
843 return res;
844 }
845
846 wxSize wxPGProperty::OnMeasureImage( int WXUNUSED(item) ) const
847 {
848 if ( m_valueBitmap )
849 return wxSize(m_valueBitmap->GetWidth(),-1);
850
851 return wxSize(0,0);
852 }
853
854 wxPGCellRenderer* wxPGProperty::GetCellRenderer( int WXUNUSED(column) ) const
855 {
856 return wxPGGlobalVars->m_defaultRenderer;
857 }
858
859 void wxPGProperty::OnCustomPaint( wxDC& dc,
860 const wxRect& rect,
861 wxPGPaintData& )
862 {
863 wxBitmap* bmp = m_valueBitmap;
864
865 wxCHECK_RET( bmp && bmp->Ok(), wxT("invalid bitmap") );
866
867 wxCHECK_RET( rect.x >= 0, wxT("unexpected measure call") );
868
869 dc.DrawBitmap(*bmp,rect.x,rect.y);
870 }
871
872 const wxPGEditor* wxPGProperty::DoGetEditorClass() const
873 {
874 return wxPG_EDITOR(TextCtrl);
875 }
876
877 // Default extra property event handling - that is, none at all.
878 bool wxPGProperty::OnEvent( wxPropertyGrid*, wxWindow*, wxEvent& )
879 {
880 return false;
881 }
882
883
884 void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
885 {
886 if ( !value.IsNull() )
887 {
888 wxVariant tempListVariant;
889
890 SetCommonValue(-1);
891 // List variants are reserved a special purpose
892 // as intermediate containers for child values
893 // of properties with children.
894 if ( value.GetType() == wxPG_VARIANT_TYPE_LIST )
895 {
896 //
897 // However, situation is different for composed string properties
898 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE) )
899 {
900 tempListVariant = value;
901 pList = &tempListVariant;
902 }
903
904 wxVariant newValue;
905 AdaptListToValue(value, &newValue);
906 value = newValue;
907 //wxLogDebug(wxT(">> %s.SetValue() adapted list value to type '%s'"),GetName().c_str(),value.GetType().c_str());
908 }
909
910 if ( HasFlag( wxPG_PROP_AGGREGATE) )
911 flags |= wxPG_SETVAL_AGGREGATED;
912
913 if ( pList && !pList->IsNull() )
914 {
915 wxASSERT( pList->GetType() == wxPG_VARIANT_TYPE_LIST );
916 wxASSERT( GetChildCount() );
917 wxASSERT( !IsCategory() );
918
919 wxVariantList& list = pList->GetList();
920 wxVariantList::iterator node;
921 unsigned int i = 0;
922
923 //wxLogDebug(wxT(">> %s.SetValue() pList parsing"),GetName().c_str());
924
925 // Children in list can be in any order, but we will give hint to
926 // GetPropertyByNameWH(). This optimizes for full list parsing.
927 for ( node = list.begin(); node != list.end(); node++ )
928 {
929 wxVariant& childValue = *((wxVariant*)*node);
930 wxPGProperty* child = GetPropertyByNameWH(childValue.GetName(), i);
931 if ( child )
932 {
933 //wxLogDebug(wxT("%i: child = %s, childValue.GetType()=%s"),i,child->GetBaseName().c_str(),childValue.GetType().c_str());
934 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
935 {
936 if ( child->HasFlag(wxPG_PROP_AGGREGATE) && !(flags & wxPG_SETVAL_AGGREGATED) )
937 {
938 wxVariant listRefCopy = childValue;
939 child->SetValue(childValue, &listRefCopy, flags|wxPG_SETVAL_FROM_PARENT);
940 }
941 else
942 {
943 wxVariant oldVal = child->GetValue();
944 child->SetValue(oldVal, &childValue, flags|wxPG_SETVAL_FROM_PARENT);
945 }
946 }
947 else if ( child->GetValue() != childValue )
948 {
949 // For aggregate properties, we will trust RefreshChildren()
950 // to update child values.
951 if ( !HasFlag(wxPG_PROP_AGGREGATE) )
952 child->SetValue(childValue, NULL, flags|wxPG_SETVAL_FROM_PARENT);
953 if ( flags & wxPG_SETVAL_BY_USER )
954 child->SetFlag(wxPG_PROP_MODIFIED);
955 }
956 }
957 i++;
958 }
959 }
960
961 if ( !value.IsNull() )
962 {
963 m_value = value;
964 OnSetValue();
965
966 if ( !(flags & wxPG_SETVAL_FROM_PARENT) )
967 UpdateParentValues();
968 }
969
970 if ( flags & wxPG_SETVAL_BY_USER )
971 SetFlag(wxPG_PROP_MODIFIED);
972
973 if ( HasFlag(wxPG_PROP_AGGREGATE) )
974 RefreshChildren();
975 }
976 else
977 {
978 if ( m_commonValue != -1 )
979 {
980 wxPropertyGrid* pg = GetGrid();
981 if ( !pg || m_commonValue != pg->GetUnspecifiedCommonValue() )
982 SetCommonValue(-1);
983 }
984
985 m_value = value;
986
987 // Set children to unspecified, but only if aggregate or
988 // value is <composed>
989 if ( AreChildrenComponents() )
990 {
991 unsigned int i;
992 for ( i=0; i<GetChildCount(); i++ )
993 Item(i)->SetValue(value, NULL, flags|wxPG_SETVAL_FROM_PARENT);
994 }
995 }
996
997 //
998 // Update editor control
999 //
1000
1001 // We need to check for these, otherwise GetGrid() may fail.
1002 if ( flags & wxPG_SETVAL_REFRESH_EDITOR )
1003 RefreshEditor();
1004 }
1005
1006
1007 void wxPGProperty::SetValueInEvent( wxVariant value ) const
1008 {
1009 GetGrid()->ValueChangeInEvent(value);
1010 }
1011
1012 void wxPGProperty::SetFlagRecursively( FlagType flag, bool set )
1013 {
1014 if ( set )
1015 SetFlag(flag);
1016 else
1017 ClearFlag(flag);
1018
1019 unsigned int i;
1020 for ( i = 0; i < GetChildCount(); i++ )
1021 Item(i)->SetFlagRecursively(flag, set);
1022 }
1023
1024 void wxPGProperty::RefreshEditor()
1025 {
1026 if ( m_parent && GetParentState() )
1027 {
1028 wxPropertyGrid* pg = GetParentState()->GetGrid();
1029 if ( pg->GetSelectedProperty() == this )
1030 {
1031 wxWindow* editor = pg->GetEditorControl();
1032 if ( editor )
1033 GetEditorClass()->UpdateControl( this, editor );
1034 }
1035 }
1036 }
1037
1038
1039 wxVariant wxPGProperty::GetDefaultValue() const
1040 {
1041 wxVariant defVal = GetAttribute(wxS("DefaultValue"));
1042 if ( !defVal.IsNull() )
1043 return defVal;
1044
1045 wxVariant value = GetValue();
1046
1047 if ( !value.IsNull() )
1048 {
1049 wxString valueType(value.GetType());
1050
1051 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1052 return wxPGVariant_Zero;
1053 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1054 return wxPGVariant_EmptyString;
1055 if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1056 return wxPGVariant_False;
1057 if ( valueType == wxPG_VARIANT_TYPE_DOUBLE )
1058 return wxVariant(0.0);
1059 if ( valueType == wxPG_VARIANT_TYPE_ARRSTRING )
1060 return wxVariant(wxArrayString());
1061 if ( valueType == wxS("wxLongLong") )
1062 return WXVARIANT(wxLongLong(0));
1063 if ( valueType == wxS("wxULongLong") )
1064 return WXVARIANT(wxULongLong(0));
1065 if ( valueType == wxS("wxColour") )
1066 return WXVARIANT(*wxBLACK);
1067 #if wxUSE_DATETIME
1068 if ( valueType == wxPG_VARIANT_TYPE_DATETIME )
1069 return wxVariant(wxDateTime::Now());
1070 #endif
1071 if ( valueType == wxS("wxFont") )
1072 return WXVARIANT(*wxNORMAL_FONT);
1073 if ( valueType == wxS("wxPoint") )
1074 return WXVARIANT(wxPoint(0, 0));
1075 if ( valueType == wxS("wxSize") )
1076 return WXVARIANT(wxSize(0, 0));
1077 }
1078
1079 return wxVariant();
1080 }
1081
1082 void wxPGProperty::SetCell( int column, wxPGCell* cellObj )
1083 {
1084 if ( column >= (int)m_cells.size() )
1085 m_cells.SetCount(column+1, NULL);
1086
1087 delete (wxPGCell*) m_cells[column];
1088 m_cells[column] = cellObj;
1089 }
1090
1091 wxPGEditorDialogAdapter* wxPGProperty::GetEditorDialog() const
1092 {
1093 return NULL;
1094 }
1095
1096 bool wxPGProperty::DoSetAttribute( const wxString& WXUNUSED(name), wxVariant& WXUNUSED(value) )
1097 {
1098 return false;
1099 }
1100
1101 void wxPGProperty::SetAttribute( const wxString& name, wxVariant value )
1102 {
1103 if ( DoSetAttribute( name, value ) )
1104 {
1105 // Support working without grid, when possible
1106 if ( wxPGGlobalVars->HasExtraStyle( wxPG_EX_WRITEONLY_BUILTIN_ATTRIBUTES ) )
1107 return;
1108 }
1109
1110 m_attributes.Set( name, value );
1111 }
1112
1113 void wxPGProperty::SetAttributes( const wxPGAttributeStorage& attributes )
1114 {
1115 wxPGAttributeStorage::const_iterator it = attributes.StartIteration();
1116 wxVariant variant;
1117
1118 while ( attributes.GetNext(it, variant) )
1119 SetAttribute( variant.GetName(), variant );
1120 }
1121
1122 wxVariant wxPGProperty::DoGetAttribute( const wxString& WXUNUSED(name) ) const
1123 {
1124 return wxVariant();
1125 }
1126
1127
1128 wxVariant wxPGProperty::GetAttribute( const wxString& name ) const
1129 {
1130 return m_attributes.FindValue(name);
1131 }
1132
1133 wxString wxPGProperty::GetAttribute( const wxString& name, const wxString& defVal ) const
1134 {
1135 wxVariant variant = m_attributes.FindValue(name);
1136
1137 if ( !variant.IsNull() )
1138 return variant.GetString();
1139
1140 return defVal;
1141 }
1142
1143 long wxPGProperty::GetAttributeAsLong( const wxString& name, long defVal ) const
1144 {
1145 wxVariant variant = m_attributes.FindValue(name);
1146
1147 return wxPGVariantToInt(variant, defVal);
1148 }
1149
1150 double wxPGProperty::GetAttributeAsDouble( const wxString& name, double defVal ) const
1151 {
1152 double retVal;
1153 wxVariant variant = m_attributes.FindValue(name);
1154
1155 if ( wxPGVariantToDouble(variant, &retVal) )
1156 return retVal;
1157
1158 return defVal;
1159 }
1160
1161 wxVariant wxPGProperty::GetAttributesAsList() const
1162 {
1163 wxVariantList tempList;
1164 wxVariant v( tempList, wxString::Format(wxS("@%s@attr"),m_name.c_str()) );
1165
1166 wxPGAttributeStorage::const_iterator it = m_attributes.StartIteration();
1167 wxVariant variant;
1168
1169 while ( m_attributes.GetNext(it, variant) )
1170 v.Append(variant);
1171
1172 return v;
1173 }
1174
1175 // Slots of utility flags are NULL
1176 const unsigned int gs_propFlagToStringSize = 14;
1177
1178 static const wxChar* gs_propFlagToString[gs_propFlagToStringSize] = {
1179 NULL,
1180 wxT("DISABLED"),
1181 wxT("HIDDEN"),
1182 NULL,
1183 wxT("NOEDITOR"),
1184 wxT("COLLAPSED"),
1185 NULL,
1186 NULL,
1187 NULL,
1188 NULL,
1189 NULL,
1190 NULL,
1191 NULL,
1192 NULL
1193 };
1194
1195 wxString wxPGProperty::GetFlagsAsString( FlagType flagsMask ) const
1196 {
1197 wxString s;
1198 int relevantFlags = m_flags & flagsMask & wxPG_STRING_STORED_FLAGS;
1199 FlagType a = 1;
1200
1201 unsigned int i = 0;
1202 for ( i=0; i<gs_propFlagToStringSize; i++ )
1203 {
1204 if ( relevantFlags & a )
1205 {
1206 const wxChar* fs = gs_propFlagToString[i];
1207 wxASSERT(fs);
1208 if ( s.length() )
1209 s << wxS("|");
1210 s << fs;
1211 }
1212 a = a << 1;
1213 }
1214
1215 return s;
1216 }
1217
1218 void wxPGProperty::SetFlagsFromString( const wxString& str )
1219 {
1220 FlagType flags = 0;
1221
1222 WX_PG_TOKENIZER1_BEGIN(str, wxS('|'))
1223 unsigned int i;
1224 for ( i=0; i<gs_propFlagToStringSize; i++ )
1225 {
1226 const wxChar* fs = gs_propFlagToString[i];
1227 if ( fs && str == fs )
1228 {
1229 flags |= (1<<i);
1230 break;
1231 }
1232 }
1233 WX_PG_TOKENIZER1_END()
1234
1235 m_flags = (m_flags & ~wxPG_STRING_STORED_FLAGS) | flags;
1236 }
1237
1238 wxValidator* wxPGProperty::DoGetValidator() const
1239 {
1240 return (wxValidator*) NULL;
1241 }
1242
1243 int wxPGProperty::InsertChoice( const wxString& label, int index, int value )
1244 {
1245 wxPropertyGrid* pg = GetGrid();
1246 int sel = GetChoiceSelection();
1247
1248 int newSel = sel;
1249
1250 if ( index == wxNOT_FOUND )
1251 index = m_choices.GetCount();
1252
1253 if ( index <= sel )
1254 newSel++;
1255
1256 m_choices.Insert(label, index, value);
1257
1258 if ( sel != newSel )
1259 SetChoiceSelection(newSel);
1260
1261 if ( this == pg->GetSelection() )
1262 GetEditorClass()->InsertItem(pg->GetEditorControl(),label,index);
1263
1264 return index;
1265 }
1266
1267
1268 void wxPGProperty::DeleteChoice( int index )
1269 {
1270 wxPropertyGrid* pg = GetGrid();
1271
1272 int sel = GetChoiceSelection();
1273 int newSel = sel;
1274
1275 // Adjust current value
1276 if ( sel == index )
1277 {
1278 SetValueToUnspecified();
1279 newSel = 0;
1280 }
1281 else if ( index < sel )
1282 {
1283 newSel--;
1284 }
1285
1286 m_choices.RemoveAt(index);
1287
1288 if ( sel != newSel )
1289 SetChoiceSelection(newSel);
1290
1291 if ( this == pg->GetSelection() )
1292 GetEditorClass()->DeleteItem(pg->GetEditorControl(), index);
1293 }
1294
1295 int wxPGProperty::GetChoiceSelection() const
1296 {
1297 wxVariant value = GetValue();
1298 wxString valueType = value.GetType();
1299 int index = wxNOT_FOUND;
1300
1301 if ( IsValueUnspecified() || !m_choices.GetCount() )
1302 return wxNOT_FOUND;
1303
1304 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1305 {
1306 index = value.GetLong();
1307 }
1308 else if ( valueType == wxPG_VARIANT_TYPE_STRING )
1309 {
1310 index = m_choices.Index(value.GetString());
1311 }
1312 else if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1313 {
1314 index = value.GetBool()? 1 : 0;
1315 }
1316
1317 return index;
1318 }
1319
1320 void wxPGProperty::SetChoiceSelection( int newValue )
1321 {
1322 // Changes value of a property with choices, but only
1323 // works if the value type is long or string.
1324 wxString valueType = GetValue().GetType();
1325
1326 wxCHECK_RET( m_choices.IsOk(), wxT("invalid choiceinfo") );
1327
1328 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1329 {
1330 SetValue( m_choices.GetLabel(newValue) );
1331 }
1332 else // if ( valueType == wxPG_VARIANT_TYPE_LONG )
1333 {
1334 SetValue( (long) newValue );
1335 }
1336 }
1337
1338 bool wxPGProperty::SetChoices( wxPGChoices& choices )
1339 {
1340 m_choices.Assign(choices);
1341
1342 {
1343 // This may be needed to trigger some initialization
1344 // (but don't do it if property is somewhat uninitialized)
1345 wxVariant defVal = GetDefaultValue();
1346 if ( defVal.IsNull() )
1347 return false;
1348
1349 SetValue(defVal);
1350 }
1351
1352 return true;
1353 }
1354
1355
1356 const wxPGEditor* wxPGProperty::GetEditorClass() const
1357 {
1358 const wxPGEditor* editor;
1359
1360 if ( !m_customEditor )
1361 {
1362 editor = DoGetEditorClass();
1363 }
1364 else
1365 editor = m_customEditor;
1366
1367 //
1368 // Maybe override editor if common value specified
1369 if ( GetDisplayedCommonValueCount() )
1370 {
1371 // TextCtrlAndButton -> ComboBoxAndButton
1372 if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlAndButtonEditor)) )
1373 editor = wxPG_EDITOR(ChoiceAndButton);
1374
1375 // TextCtrl -> ComboBox
1376 else if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlEditor)) )
1377 editor = wxPG_EDITOR(ComboBox);
1378 }
1379
1380 return editor;
1381 }
1382
1383 bool wxPGProperty::HasVisibleChildren() const
1384 {
1385 unsigned int i;
1386
1387 for ( i=0; i<GetChildCount(); i++ )
1388 {
1389 wxPGProperty* child = Item(i);
1390
1391 if ( !child->HasFlag(wxPG_PROP_HIDDEN) )
1392 return true;
1393 }
1394
1395 return false;
1396 }
1397
1398 bool wxPGProperty::PrepareValueForDialogEditing( wxPropertyGrid* propGrid )
1399 {
1400 return propGrid->EditorValidate();
1401 }
1402
1403
1404 bool wxPGProperty::RecreateEditor()
1405 {
1406 wxPropertyGrid* pg = GetGrid();
1407 wxASSERT(pg);
1408
1409 wxPGProperty* selected = pg->GetSelection();
1410 if ( this == selected )
1411 {
1412 pg->DoSelectProperty(this, wxPG_SEL_FORCE);
1413 return true;
1414 }
1415 return false;
1416 }
1417
1418
1419 void wxPGProperty::SetValueImage( wxBitmap& bmp )
1420 {
1421 delete m_valueBitmap;
1422
1423 if ( &bmp && bmp.Ok() )
1424 {
1425 // Resize the image
1426 wxSize maxSz = GetGrid()->GetImageSize();
1427 wxSize imSz(bmp.GetWidth(),bmp.GetHeight());
1428
1429 if ( imSz.x != maxSz.x || imSz.y != maxSz.y )
1430 {
1431 // Create a memory DC
1432 wxBitmap* bmpNew = new wxBitmap(maxSz.x,maxSz.y,bmp.GetDepth());
1433
1434 wxMemoryDC dc;
1435 dc.SelectObject(*bmpNew);
1436
1437 // Scale
1438 // FIXME: This is ugly - use image or wait for scaling patch.
1439 double scaleX = (double)maxSz.x / (double)imSz.x;
1440 double scaleY = (double)maxSz.y / (double)imSz.y;
1441
1442 dc.SetUserScale(scaleX,scaleY);
1443
1444 dc.DrawBitmap( bmp, 0, 0 );
1445
1446 m_valueBitmap = bmpNew;
1447 }
1448 else
1449 {
1450 m_valueBitmap = new wxBitmap(bmp);
1451 }
1452
1453 m_flags |= wxPG_PROP_CUSTOMIMAGE;
1454 }
1455 else
1456 {
1457 m_valueBitmap = NULL;
1458 m_flags &= ~(wxPG_PROP_CUSTOMIMAGE);
1459 }
1460 }
1461
1462
1463 wxPGProperty* wxPGProperty::GetMainParent() const
1464 {
1465 const wxPGProperty* curChild = this;
1466 const wxPGProperty* curParent = m_parent;
1467
1468 while ( curParent && !curParent->IsCategory() )
1469 {
1470 curChild = curParent;
1471 curParent = curParent->m_parent;
1472 }
1473
1474 return (wxPGProperty*) curChild;
1475 }
1476
1477
1478 const wxPGProperty* wxPGProperty::GetLastVisibleSubItem() const
1479 {
1480 //
1481 // Returns last visible sub-item, recursively.
1482 if ( !IsExpanded() || !GetChildCount() )
1483 return this;
1484
1485 return Last()->GetLastVisibleSubItem();
1486 }
1487
1488
1489 bool wxPGProperty::IsVisible() const
1490 {
1491 const wxPGProperty* parent;
1492
1493 if ( HasFlag(wxPG_PROP_HIDDEN) )
1494 return false;
1495
1496 for ( parent = GetParent(); parent != NULL; parent = parent->GetParent() )
1497 {
1498 if ( !parent->IsExpanded() || parent->HasFlag(wxPG_PROP_HIDDEN) )
1499 return false;
1500 }
1501
1502 return true;
1503 }
1504
1505 wxPropertyGrid* wxPGProperty::GetGridIfDisplayed() const
1506 {
1507 wxPropertyGridPageState* state = GetParentState();
1508 wxPropertyGrid* propGrid = state->GetGrid();
1509 if ( state == propGrid->GetState() )
1510 return propGrid;
1511 return NULL;
1512 }
1513
1514
1515 int wxPGProperty::GetY2( int lh ) const
1516 {
1517 const wxPGProperty* parent;
1518 const wxPGProperty* child = this;
1519
1520 int y = 0;
1521
1522 for ( parent = GetParent(); parent != NULL; parent = child->GetParent() )
1523 {
1524 if ( !parent->IsExpanded() )
1525 return -1;
1526 y += parent->GetChildrenHeight(lh, child->GetIndexInParent());
1527 y += lh;
1528 child = parent;
1529 }
1530
1531 y -= lh; // need to reduce one level
1532
1533 return y;
1534 }
1535
1536
1537 int wxPGProperty::GetY() const
1538 {
1539 return GetY2(GetGrid()->GetRowHeight());
1540 }
1541
1542 // This is used by Insert etc.
1543 void wxPGProperty::AddChild2( wxPGProperty* prop, int index, bool correct_mode )
1544 {
1545 if ( index < 0 || (size_t)index >= m_children.GetCount() )
1546 {
1547 if ( correct_mode ) prop->m_arrIndex = m_children.GetCount();
1548 m_children.Add( prop );
1549 }
1550 else
1551 {
1552 m_children.Insert( prop, index );
1553 if ( correct_mode ) FixIndexesOfChildren( index );
1554 }
1555
1556 prop->m_parent = this;
1557 }
1558
1559 // This is used by properties that have fixed sub-properties
1560 void wxPGProperty::AddChild( wxPGProperty* prop )
1561 {
1562 wxASSERT_MSG( prop->GetBaseName().length(),
1563 "Property's children must have unique, non-empty names within their scope" );
1564
1565 prop->m_arrIndex = m_children.GetCount();
1566 m_children.Add( prop );
1567
1568 int custImgHeight = prop->OnMeasureImage().y;
1569 if ( custImgHeight < 0 /*|| custImgHeight > 1*/ )
1570 prop->m_flags |= wxPG_PROP_CUSTOMIMAGE;
1571
1572 prop->m_parent = this;
1573 }
1574
1575
1576 void wxPGProperty::AdaptListToValue( wxVariant& list, wxVariant* value ) const
1577 {
1578 wxASSERT( GetChildCount() );
1579 wxASSERT( !IsCategory() );
1580
1581 *value = GetValue();
1582
1583 if ( !list.GetCount() )
1584 return;
1585
1586 wxASSERT( GetChildCount() >= (unsigned int)list.GetCount() );
1587
1588 bool allChildrenSpecified;
1589
1590 // Don't fully update aggregate properties unless all children have
1591 // specified value
1592 if ( HasFlag(wxPG_PROP_AGGREGATE) )
1593 allChildrenSpecified = AreAllChildrenSpecified(&list);
1594 else
1595 allChildrenSpecified = true;
1596
1597 wxVariant childValue = list[0];
1598 unsigned int i;
1599 unsigned int n = 0;
1600
1601 //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
1602
1603 for ( i=0; i<GetChildCount(); i++ )
1604 {
1605 const wxPGProperty* child = Item(i);
1606
1607 if ( childValue.GetName() == child->GetBaseName() )
1608 {
1609 //wxLogDebug(wxT(" %s(n=%i), %s"),childValue.GetName().c_str(),n,childValue.GetType().c_str());
1610
1611 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
1612 {
1613 wxVariant cv2(child->GetValue());
1614 child->AdaptListToValue(childValue, &cv2);
1615 childValue = cv2;
1616 }
1617
1618 if ( allChildrenSpecified )
1619 ChildChanged(*value, i, childValue);
1620 n++;
1621 if ( n == (unsigned int)list.GetCount() )
1622 break;
1623 childValue = list[n];
1624 }
1625 }
1626 }
1627
1628
1629 void wxPGProperty::FixIndexesOfChildren( size_t starthere )
1630 {
1631 size_t i;
1632 for ( i=starthere;i<GetChildCount();i++)
1633 Item(i)->m_arrIndex = i;
1634 }
1635
1636
1637 // Returns (direct) child property with given name (or NULL if not found)
1638 wxPGProperty* wxPGProperty::GetPropertyByName( const wxString& name ) const
1639 {
1640 size_t i;
1641
1642 for ( i=0; i<GetChildCount(); i++ )
1643 {
1644 wxPGProperty* p = Item(i);
1645 if ( p->m_name == name )
1646 return p;
1647 }
1648
1649 // Does it have point, then?
1650 int pos = name.Find(wxS('.'));
1651 if ( pos <= 0 )
1652 return (wxPGProperty*) NULL;
1653
1654 wxPGProperty* p = GetPropertyByName(name. substr(0,pos));
1655
1656 if ( !p || !p->GetChildCount() )
1657 return NULL;
1658
1659 return p->GetPropertyByName(name.substr(pos+1,name.length()-pos-1));
1660 }
1661
1662 wxPGProperty* wxPGProperty::GetPropertyByNameWH( const wxString& name, unsigned int hintIndex ) const
1663 {
1664 unsigned int i = hintIndex;
1665
1666 if ( i >= GetChildCount() )
1667 i = 0;
1668
1669 unsigned int lastIndex = i - 1;
1670
1671 if ( lastIndex >= GetChildCount() )
1672 lastIndex = GetChildCount() - 1;
1673
1674 for (;;)
1675 {
1676 wxPGProperty* p = Item(i);
1677 if ( p->m_name == name )
1678 return p;
1679
1680 if ( i == lastIndex )
1681 break;
1682
1683 i++;
1684 if ( i == GetChildCount() )
1685 i = 0;
1686 };
1687
1688 return NULL;
1689 }
1690
1691 int wxPGProperty::GetChildrenHeight( int lh, int iMax_ ) const
1692 {
1693 // Returns height of children, recursively, and
1694 // by taking expanded/collapsed status into account.
1695 //
1696 // iMax is used when finding property y-positions.
1697 //
1698 unsigned int i = 0;
1699 int h = 0;
1700
1701 if ( iMax_ == -1 )
1702 iMax_ = GetChildCount();
1703
1704 unsigned int iMax = iMax_;
1705
1706 wxASSERT( iMax <= GetChildCount() );
1707
1708 if ( !IsExpanded() && GetParent() )
1709 return 0;
1710
1711 while ( i < iMax )
1712 {
1713 wxPGProperty* pwc = (wxPGProperty*) Item(i);
1714
1715 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1716 {
1717 if ( !pwc->IsExpanded() ||
1718 pwc->GetChildCount() == 0 )
1719 h += lh;
1720 else
1721 h += pwc->GetChildrenHeight(lh) + lh;
1722 }
1723
1724 i++;
1725 }
1726
1727 return h;
1728 }
1729
1730 wxPGProperty* wxPGProperty::GetItemAtY( unsigned int y, unsigned int lh, unsigned int* nextItemY ) const
1731 {
1732 wxASSERT( nextItemY );
1733
1734 // Linear search at the moment
1735 //
1736 // nextItemY = y of next visible property, final value will be written back.
1737 wxPGProperty* result = NULL;
1738 wxPGProperty* current = NULL;
1739 unsigned int iy = *nextItemY;
1740 unsigned int i = 0;
1741 unsigned int iMax = GetChildCount();
1742
1743 while ( i < iMax )
1744 {
1745 wxPGProperty* pwc = Item(i);
1746
1747 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1748 {
1749 // Found?
1750 if ( y < iy )
1751 {
1752 result = current;
1753 break;
1754 }
1755
1756 iy += lh;
1757
1758 if ( pwc->IsExpanded() &&
1759 pwc->GetChildCount() > 0 )
1760 {
1761 result = (wxPGProperty*) pwc->GetItemAtY( y, lh, &iy );
1762 if ( result )
1763 break;
1764 }
1765
1766 current = pwc;
1767 }
1768
1769 i++;
1770 }
1771
1772 // Found?
1773 if ( !result && y < iy )
1774 result = current;
1775
1776 *nextItemY = iy;
1777
1778 /*
1779 if ( current )
1780 wxLogDebug(wxT("%s::GetItemAtY(%i) -> %s"),this->GetLabel().c_str(),y,current->GetLabel().c_str());
1781 else
1782 wxLogDebug(wxT("%s::GetItemAtY(%i) -> NULL"),this->GetLabel().c_str(),y);
1783 */
1784
1785 return (wxPGProperty*) result;
1786 }
1787
1788 void wxPGProperty::Empty()
1789 {
1790 size_t i;
1791 if ( !HasFlag(wxPG_PROP_CHILDREN_ARE_COPIES) )
1792 {
1793 for ( i=0; i<GetChildCount(); i++ )
1794 {
1795 wxPGProperty* p = (wxPGProperty*) Item(i);
1796 delete p;
1797 }
1798 }
1799
1800 m_children.Empty();
1801 }
1802
1803 void wxPGProperty::ChildChanged( wxVariant& WXUNUSED(thisValue),
1804 int WXUNUSED(childIndex),
1805 wxVariant& WXUNUSED(childValue) ) const
1806 {
1807 }
1808
1809 bool wxPGProperty::AreAllChildrenSpecified( wxVariant* pendingList ) const
1810 {
1811 unsigned int i;
1812
1813 const wxVariantList* pList = NULL;
1814 wxVariantList::const_iterator node;
1815
1816 if ( pendingList )
1817 {
1818 pList = &pendingList->GetList();
1819 node = pList->begin();
1820 }
1821
1822 for ( i=0; i<GetChildCount(); i++ )
1823 {
1824 wxPGProperty* child = Item(i);
1825 const wxVariant* listValue = NULL;
1826 wxVariant value;
1827
1828 if ( pendingList )
1829 {
1830 const wxString& childName = child->GetBaseName();
1831
1832 for ( ; node != pList->end(); node++ )
1833 {
1834 const wxVariant& item = *((const wxVariant*)*node);
1835 if ( item.GetName() == childName )
1836 {
1837 listValue = &item;
1838 value = item;
1839 break;
1840 }
1841 }
1842 }
1843
1844 if ( !listValue )
1845 value = child->GetValue();
1846
1847 if ( value.IsNull() )
1848 return false;
1849
1850 // Check recursively
1851 if ( child->GetChildCount() )
1852 {
1853 const wxVariant* childList = NULL;
1854
1855 if ( listValue && listValue->GetType() == wxPG_VARIANT_TYPE_LIST )
1856 childList = listValue;
1857
1858 if ( !child->AreAllChildrenSpecified((wxVariant*)childList) )
1859 return false;
1860 }
1861 }
1862
1863 return true;
1864 }
1865
1866 wxPGProperty* wxPGProperty::UpdateParentValues()
1867 {
1868 wxPGProperty* parent = m_parent;
1869 if ( parent && parent->HasFlag(wxPG_PROP_COMPOSED_VALUE) &&
1870 !parent->IsCategory() && !parent->IsRoot() )
1871 {
1872 wxString s;
1873 parent->GenerateComposedValue(s, 0);
1874 parent->m_value = s;
1875 return parent->UpdateParentValues();
1876 }
1877 return this;
1878 }
1879
1880 bool wxPGProperty::IsTextEditable() const
1881 {
1882 if ( HasFlag(wxPG_PROP_READONLY) )
1883 return false;
1884
1885 if ( HasFlag(wxPG_PROP_NOEDITOR) &&
1886 (GetChildCount() ||
1887 wxString(GetEditorClass()->GetClassInfo()->GetClassName()).EndsWith(wxS("Button")))
1888 )
1889 return false;
1890
1891 return true;
1892 }
1893
1894 // Call for after sub-properties added with AddChild
1895 void wxPGProperty::PrepareSubProperties()
1896 {
1897 wxPropertyGridPageState* state = GetParentState();
1898
1899 wxASSERT(state);
1900
1901 if ( !GetChildCount() )
1902 return;
1903
1904 wxByte depth = m_depth + 1;
1905 wxByte depthBgCol = m_depthBgCol;
1906
1907 FlagType inheritFlags = m_flags & wxPG_INHERITED_PROPFLAGS;
1908
1909 wxByte bgColIndex = m_bgColIndex;
1910 wxByte fgColIndex = m_fgColIndex;
1911
1912 //
1913 // Set some values to the children
1914 //
1915 size_t i = 0;
1916 wxPGProperty* nparent = this;
1917
1918 while ( i < nparent->GetChildCount() )
1919 {
1920 wxPGProperty* np = nparent->Item(i);
1921
1922 np->m_parentState = state;
1923 np->m_flags |= inheritFlags; // Hideable also if parent.
1924 np->m_depth = depth;
1925 np->m_depthBgCol = depthBgCol;
1926 np->m_bgColIndex = bgColIndex;
1927 np->m_fgColIndex = fgColIndex;
1928
1929 // Also handle children of children
1930 if ( np->GetChildCount() > 0 )
1931 {
1932 nparent = np;
1933 i = 0;
1934
1935 // Init
1936 nparent->SetParentalType(wxPG_PROP_AGGREGATE);
1937 nparent->SetExpanded(false);
1938 depth++;
1939 }
1940 else
1941 {
1942 // Next sibling
1943 i++;
1944 }
1945
1946 // After reaching last sibling, go back to processing
1947 // siblings of the parent
1948 while ( i >= nparent->GetChildCount() )
1949 {
1950 // Exit the loop when top parent hit
1951 if ( nparent == this )
1952 break;
1953
1954 depth--;
1955
1956 i = nparent->GetIndexInParent() + 1;
1957 nparent = nparent->GetParent();
1958 }
1959 }
1960 }
1961
1962 // Call after fixed sub-properties added/removed after creation.
1963 // if oldSelInd >= 0 and < new max items, then selection is
1964 // moved to it. Note: oldSelInd -2 indicates that this property
1965 // should be selected.
1966 void wxPGProperty::SubPropsChanged( int oldSelInd )
1967 {
1968 wxPropertyGridPageState* state = GetParentState();
1969 wxPropertyGrid* grid = state->GetGrid();
1970
1971 PrepareSubProperties();
1972
1973 wxPGProperty* sel = (wxPGProperty*) NULL;
1974 if ( oldSelInd >= (int)m_children.GetCount() )
1975 oldSelInd = (int)m_children.GetCount() - 1;
1976
1977 if ( oldSelInd >= 0 )
1978 sel = (wxPGProperty*) m_children[oldSelInd];
1979 else if ( oldSelInd == -2 )
1980 sel = this;
1981
1982 if ( sel )
1983 state->DoSelectProperty(sel);
1984
1985 if ( state == grid->GetState() )
1986 {
1987 grid->GetPanel()->Refresh();
1988 }
1989 }
1990
1991 // -----------------------------------------------------------------------
1992 // wxPGRootProperty
1993 // -----------------------------------------------------------------------
1994
1995 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPGRootProperty,none,TextCtrl)
1996 IMPLEMENT_DYNAMIC_CLASS(wxPGRootProperty, wxPGProperty)
1997
1998
1999 wxPGRootProperty::wxPGRootProperty()
2000 : wxPGProperty()
2001 {
2002 #ifdef __WXDEBUG__
2003 m_name = wxS("<root>");
2004 #endif
2005 SetParentalType(0);
2006 m_depth = 0;
2007 }
2008
2009
2010 wxPGRootProperty::~wxPGRootProperty()
2011 {
2012 }
2013
2014
2015 // -----------------------------------------------------------------------
2016 // wxPropertyCategory
2017 // -----------------------------------------------------------------------
2018
2019 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPropertyCategory,none,TextCtrl)
2020 IMPLEMENT_DYNAMIC_CLASS(wxPropertyCategory, wxPGProperty)
2021
2022 void wxPropertyCategory::Init()
2023 {
2024 // don't set colour - prepareadditem method should do this
2025 SetParentalType(wxPG_PROP_CATEGORY);
2026 m_capFgColIndex = 1;
2027 m_textExtent = -1;
2028 }
2029
2030 wxPropertyCategory::wxPropertyCategory()
2031 : wxPGProperty()
2032 {
2033 Init();
2034 }
2035
2036
2037 wxPropertyCategory::wxPropertyCategory( const wxString &label, const wxString& name )
2038 : wxPGProperty(label,name)
2039 {
2040 Init();
2041 }
2042
2043
2044 wxPropertyCategory::~wxPropertyCategory()
2045 {
2046 }
2047
2048
2049 wxString wxPropertyCategory::GetValueAsString( int ) const
2050 {
2051 return wxEmptyString;
2052 }
2053
2054 int wxPropertyCategory::GetTextExtent( const wxWindow* wnd, const wxFont& font ) const
2055 {
2056 if ( m_textExtent > 0 )
2057 return m_textExtent;
2058 int x = 0, y = 0;
2059 ((wxWindow*)wnd)->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2060 return x;
2061 }
2062
2063 void wxPropertyCategory::CalculateTextExtent( wxWindow* wnd, const wxFont& font )
2064 {
2065 int x = 0, y = 0;
2066 wnd->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2067 m_textExtent = x;
2068 }
2069
2070 // -----------------------------------------------------------------------
2071 // wxPGAttributeStorage
2072 // -----------------------------------------------------------------------
2073
2074 wxPGAttributeStorage::wxPGAttributeStorage()
2075 {
2076 }
2077
2078 wxPGAttributeStorage::~wxPGAttributeStorage()
2079 {
2080 wxPGHashMapS2P::iterator it;
2081
2082 for ( it = m_map.begin(); it != m_map.end(); it++ )
2083 {
2084 wxVariantData* data = (wxVariantData*) it->second;
2085 data->DecRef();
2086 }
2087 }
2088
2089 void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value )
2090 {
2091 wxVariantData* data = value.GetData();
2092
2093 // Free old, if any
2094 wxPGHashMapS2P::iterator it = m_map.find(name);
2095 if ( it != m_map.end() )
2096 {
2097 ((wxVariantData*)it->second)->DecRef();
2098
2099 if ( !data )
2100 {
2101 // If Null variant, just remove from set
2102 m_map.erase(it);
2103 return;
2104 }
2105 }
2106
2107 if ( data )
2108 {
2109 data->IncRef();
2110
2111 m_map[name] = data;
2112 }
2113 }
2114
2115 #endif // wxUSE_PROPGRID