Fix RCS-IDs
[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 auto unspecified values are not wanted (via window or property style),
896 // then get default value instead of wxNullVariant.
897 if ( value.IsNull() && (flags & wxPG_SETVAL_BY_USER) &&
898 !UsesAutoUnspecified() )
899 {
900 value = GetDefaultValue();
901 }
902
903 if ( !value.IsNull() )
904 {
905 wxVariant tempListVariant;
906
907 SetCommonValue(-1);
908 // List variants are reserved a special purpose
909 // as intermediate containers for child values
910 // of properties with children.
911 if ( value.GetType() == wxPG_VARIANT_TYPE_LIST )
912 {
913 //
914 // However, situation is different for composed string properties
915 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE) )
916 {
917 tempListVariant = value;
918 pList = &tempListVariant;
919 }
920
921 wxVariant newValue;
922 AdaptListToValue(value, &newValue);
923 value = newValue;
924 //wxLogDebug(wxT(">> %s.SetValue() adapted list value to type '%s'"),GetName().c_str(),value.GetType().c_str());
925 }
926
927 if ( HasFlag( wxPG_PROP_AGGREGATE) )
928 flags |= wxPG_SETVAL_AGGREGATED;
929
930 if ( pList && !pList->IsNull() )
931 {
932 wxASSERT( pList->GetType() == wxPG_VARIANT_TYPE_LIST );
933 wxASSERT( GetChildCount() );
934 wxASSERT( !IsCategory() );
935
936 wxVariantList& list = pList->GetList();
937 wxVariantList::iterator node;
938 unsigned int i = 0;
939
940 //wxLogDebug(wxT(">> %s.SetValue() pList parsing"),GetName().c_str());
941
942 // Children in list can be in any order, but we will give hint to
943 // GetPropertyByNameWH(). This optimizes for full list parsing.
944 for ( node = list.begin(); node != list.end(); node++ )
945 {
946 wxVariant& childValue = *((wxVariant*)*node);
947 wxPGProperty* child = GetPropertyByNameWH(childValue.GetName(), i);
948 if ( child )
949 {
950 //wxLogDebug(wxT("%i: child = %s, childValue.GetType()=%s"),i,child->GetBaseName().c_str(),childValue.GetType().c_str());
951 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
952 {
953 if ( child->HasFlag(wxPG_PROP_AGGREGATE) && !(flags & wxPG_SETVAL_AGGREGATED) )
954 {
955 wxVariant listRefCopy = childValue;
956 child->SetValue(childValue, &listRefCopy, flags|wxPG_SETVAL_FROM_PARENT);
957 }
958 else
959 {
960 wxVariant oldVal = child->GetValue();
961 child->SetValue(oldVal, &childValue, flags|wxPG_SETVAL_FROM_PARENT);
962 }
963 }
964 else if ( child->GetValue() != childValue )
965 {
966 // For aggregate properties, we will trust RefreshChildren()
967 // to update child values.
968 if ( !HasFlag(wxPG_PROP_AGGREGATE) )
969 child->SetValue(childValue, NULL, flags|wxPG_SETVAL_FROM_PARENT);
970 if ( flags & wxPG_SETVAL_BY_USER )
971 child->SetFlag(wxPG_PROP_MODIFIED);
972 }
973 }
974 i++;
975 }
976 }
977
978 if ( !value.IsNull() )
979 {
980 m_value = value;
981 OnSetValue();
982
983 if ( !(flags & wxPG_SETVAL_FROM_PARENT) )
984 UpdateParentValues();
985 }
986
987 if ( flags & wxPG_SETVAL_BY_USER )
988 SetFlag(wxPG_PROP_MODIFIED);
989
990 if ( HasFlag(wxPG_PROP_AGGREGATE) )
991 RefreshChildren();
992 }
993 else
994 {
995 if ( m_commonValue != -1 )
996 {
997 wxPropertyGrid* pg = GetGrid();
998 if ( !pg || m_commonValue != pg->GetUnspecifiedCommonValue() )
999 SetCommonValue(-1);
1000 }
1001
1002 m_value = value;
1003
1004 // Set children to unspecified, but only if aggregate or
1005 // value is <composed>
1006 if ( AreChildrenComponents() )
1007 {
1008 unsigned int i;
1009 for ( i=0; i<GetChildCount(); i++ )
1010 Item(i)->SetValue(value, NULL, flags|wxPG_SETVAL_FROM_PARENT);
1011 }
1012 }
1013
1014 //
1015 // Update editor control
1016 //
1017
1018 // We need to check for these, otherwise GetGrid() may fail.
1019 if ( flags & wxPG_SETVAL_REFRESH_EDITOR )
1020 RefreshEditor();
1021 }
1022
1023
1024 void wxPGProperty::SetValueInEvent( wxVariant value ) const
1025 {
1026 GetGrid()->ValueChangeInEvent(value);
1027 }
1028
1029 void wxPGProperty::SetFlagRecursively( FlagType flag, bool set )
1030 {
1031 if ( set )
1032 SetFlag(flag);
1033 else
1034 ClearFlag(flag);
1035
1036 unsigned int i;
1037 for ( i = 0; i < GetChildCount(); i++ )
1038 Item(i)->SetFlagRecursively(flag, set);
1039 }
1040
1041 void wxPGProperty::RefreshEditor()
1042 {
1043 if ( m_parent && GetParentState() )
1044 {
1045 wxPropertyGrid* pg = GetParentState()->GetGrid();
1046 if ( pg->GetSelectedProperty() == this )
1047 {
1048 wxWindow* editor = pg->GetEditorControl();
1049 if ( editor )
1050 GetEditorClass()->UpdateControl( this, editor );
1051 }
1052 }
1053 }
1054
1055
1056 wxVariant wxPGProperty::GetDefaultValue() const
1057 {
1058 wxVariant defVal = GetAttribute(wxS("DefaultValue"));
1059 if ( !defVal.IsNull() )
1060 return defVal;
1061
1062 wxVariant value = GetValue();
1063
1064 if ( !value.IsNull() )
1065 {
1066 wxString valueType(value.GetType());
1067
1068 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1069 return wxPGVariant_Zero;
1070 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1071 return wxPGVariant_EmptyString;
1072 if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1073 return wxPGVariant_False;
1074 if ( valueType == wxPG_VARIANT_TYPE_DOUBLE )
1075 return wxVariant(0.0);
1076 if ( valueType == wxPG_VARIANT_TYPE_ARRSTRING )
1077 return wxVariant(wxArrayString());
1078 if ( valueType == wxS("wxLongLong") )
1079 return WXVARIANT(wxLongLong(0));
1080 if ( valueType == wxS("wxULongLong") )
1081 return WXVARIANT(wxULongLong(0));
1082 if ( valueType == wxS("wxColour") )
1083 return WXVARIANT(*wxBLACK);
1084 #if wxUSE_DATETIME
1085 if ( valueType == wxPG_VARIANT_TYPE_DATETIME )
1086 return wxVariant(wxDateTime::Now());
1087 #endif
1088 if ( valueType == wxS("wxFont") )
1089 return WXVARIANT(*wxNORMAL_FONT);
1090 if ( valueType == wxS("wxPoint") )
1091 return WXVARIANT(wxPoint(0, 0));
1092 if ( valueType == wxS("wxSize") )
1093 return WXVARIANT(wxSize(0, 0));
1094 }
1095
1096 return wxVariant();
1097 }
1098
1099 void wxPGProperty::SetCell( int column, wxPGCell* cellObj )
1100 {
1101 if ( column >= (int)m_cells.size() )
1102 m_cells.SetCount(column+1, NULL);
1103
1104 delete (wxPGCell*) m_cells[column];
1105 m_cells[column] = cellObj;
1106 }
1107
1108 wxPGEditorDialogAdapter* wxPGProperty::GetEditorDialog() const
1109 {
1110 return NULL;
1111 }
1112
1113 bool wxPGProperty::DoSetAttribute( const wxString& WXUNUSED(name), wxVariant& WXUNUSED(value) )
1114 {
1115 return false;
1116 }
1117
1118 void wxPGProperty::SetAttribute( const wxString& name, wxVariant value )
1119 {
1120 if ( DoSetAttribute( name, value ) )
1121 {
1122 // Support working without grid, when possible
1123 if ( wxPGGlobalVars->HasExtraStyle( wxPG_EX_WRITEONLY_BUILTIN_ATTRIBUTES ) )
1124 return;
1125 }
1126
1127 m_attributes.Set( name, value );
1128 }
1129
1130 void wxPGProperty::SetAttributes( const wxPGAttributeStorage& attributes )
1131 {
1132 wxPGAttributeStorage::const_iterator it = attributes.StartIteration();
1133 wxVariant variant;
1134
1135 while ( attributes.GetNext(it, variant) )
1136 SetAttribute( variant.GetName(), variant );
1137 }
1138
1139 wxVariant wxPGProperty::DoGetAttribute( const wxString& WXUNUSED(name) ) const
1140 {
1141 return wxVariant();
1142 }
1143
1144
1145 wxVariant wxPGProperty::GetAttribute( const wxString& name ) const
1146 {
1147 return m_attributes.FindValue(name);
1148 }
1149
1150 wxString wxPGProperty::GetAttribute( const wxString& name, const wxString& defVal ) const
1151 {
1152 wxVariant variant = m_attributes.FindValue(name);
1153
1154 if ( !variant.IsNull() )
1155 return variant.GetString();
1156
1157 return defVal;
1158 }
1159
1160 long wxPGProperty::GetAttributeAsLong( const wxString& name, long defVal ) const
1161 {
1162 wxVariant variant = m_attributes.FindValue(name);
1163
1164 return wxPGVariantToInt(variant, defVal);
1165 }
1166
1167 double wxPGProperty::GetAttributeAsDouble( const wxString& name, double defVal ) const
1168 {
1169 double retVal;
1170 wxVariant variant = m_attributes.FindValue(name);
1171
1172 if ( wxPGVariantToDouble(variant, &retVal) )
1173 return retVal;
1174
1175 return defVal;
1176 }
1177
1178 wxVariant wxPGProperty::GetAttributesAsList() const
1179 {
1180 wxVariantList tempList;
1181 wxVariant v( tempList, wxString::Format(wxS("@%s@attr"),m_name.c_str()) );
1182
1183 wxPGAttributeStorage::const_iterator it = m_attributes.StartIteration();
1184 wxVariant variant;
1185
1186 while ( m_attributes.GetNext(it, variant) )
1187 v.Append(variant);
1188
1189 return v;
1190 }
1191
1192 // Slots of utility flags are NULL
1193 const unsigned int gs_propFlagToStringSize = 14;
1194
1195 static const wxChar* gs_propFlagToString[gs_propFlagToStringSize] = {
1196 NULL,
1197 wxT("DISABLED"),
1198 wxT("HIDDEN"),
1199 NULL,
1200 wxT("NOEDITOR"),
1201 wxT("COLLAPSED"),
1202 NULL,
1203 NULL,
1204 NULL,
1205 NULL,
1206 NULL,
1207 NULL,
1208 NULL,
1209 NULL
1210 };
1211
1212 wxString wxPGProperty::GetFlagsAsString( FlagType flagsMask ) const
1213 {
1214 wxString s;
1215 int relevantFlags = m_flags & flagsMask & wxPG_STRING_STORED_FLAGS;
1216 FlagType a = 1;
1217
1218 unsigned int i = 0;
1219 for ( i=0; i<gs_propFlagToStringSize; i++ )
1220 {
1221 if ( relevantFlags & a )
1222 {
1223 const wxChar* fs = gs_propFlagToString[i];
1224 wxASSERT(fs);
1225 if ( s.length() )
1226 s << wxS("|");
1227 s << fs;
1228 }
1229 a = a << 1;
1230 }
1231
1232 return s;
1233 }
1234
1235 void wxPGProperty::SetFlagsFromString( const wxString& str )
1236 {
1237 FlagType flags = 0;
1238
1239 WX_PG_TOKENIZER1_BEGIN(str, wxS('|'))
1240 unsigned int i;
1241 for ( i=0; i<gs_propFlagToStringSize; i++ )
1242 {
1243 const wxChar* fs = gs_propFlagToString[i];
1244 if ( fs && str == fs )
1245 {
1246 flags |= (1<<i);
1247 break;
1248 }
1249 }
1250 WX_PG_TOKENIZER1_END()
1251
1252 m_flags = (m_flags & ~wxPG_STRING_STORED_FLAGS) | flags;
1253 }
1254
1255 wxValidator* wxPGProperty::DoGetValidator() const
1256 {
1257 return (wxValidator*) NULL;
1258 }
1259
1260 int wxPGProperty::InsertChoice( const wxString& label, int index, int value )
1261 {
1262 wxPropertyGrid* pg = GetGrid();
1263 int sel = GetChoiceSelection();
1264
1265 int newSel = sel;
1266
1267 if ( index == wxNOT_FOUND )
1268 index = m_choices.GetCount();
1269
1270 if ( index <= sel )
1271 newSel++;
1272
1273 m_choices.Insert(label, index, value);
1274
1275 if ( sel != newSel )
1276 SetChoiceSelection(newSel);
1277
1278 if ( this == pg->GetSelection() )
1279 GetEditorClass()->InsertItem(pg->GetEditorControl(),label,index);
1280
1281 return index;
1282 }
1283
1284
1285 void wxPGProperty::DeleteChoice( int index )
1286 {
1287 wxPropertyGrid* pg = GetGrid();
1288
1289 int sel = GetChoiceSelection();
1290 int newSel = sel;
1291
1292 // Adjust current value
1293 if ( sel == index )
1294 {
1295 SetValueToUnspecified();
1296 newSel = 0;
1297 }
1298 else if ( index < sel )
1299 {
1300 newSel--;
1301 }
1302
1303 m_choices.RemoveAt(index);
1304
1305 if ( sel != newSel )
1306 SetChoiceSelection(newSel);
1307
1308 if ( this == pg->GetSelection() )
1309 GetEditorClass()->DeleteItem(pg->GetEditorControl(), index);
1310 }
1311
1312 int wxPGProperty::GetChoiceSelection() const
1313 {
1314 wxVariant value = GetValue();
1315 wxString valueType = value.GetType();
1316 int index = wxNOT_FOUND;
1317
1318 if ( IsValueUnspecified() || !m_choices.GetCount() )
1319 return wxNOT_FOUND;
1320
1321 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1322 {
1323 index = value.GetLong();
1324 }
1325 else if ( valueType == wxPG_VARIANT_TYPE_STRING )
1326 {
1327 index = m_choices.Index(value.GetString());
1328 }
1329 else if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1330 {
1331 index = value.GetBool()? 1 : 0;
1332 }
1333
1334 return index;
1335 }
1336
1337 void wxPGProperty::SetChoiceSelection( int newValue )
1338 {
1339 // Changes value of a property with choices, but only
1340 // works if the value type is long or string.
1341 wxString valueType = GetValue().GetType();
1342
1343 wxCHECK_RET( m_choices.IsOk(), wxT("invalid choiceinfo") );
1344
1345 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1346 {
1347 SetValue( m_choices.GetLabel(newValue) );
1348 }
1349 else // if ( valueType == wxPG_VARIANT_TYPE_LONG )
1350 {
1351 SetValue( (long) newValue );
1352 }
1353 }
1354
1355 bool wxPGProperty::SetChoices( wxPGChoices& choices )
1356 {
1357 m_choices.Assign(choices);
1358
1359 {
1360 // This may be needed to trigger some initialization
1361 // (but don't do it if property is somewhat uninitialized)
1362 wxVariant defVal = GetDefaultValue();
1363 if ( defVal.IsNull() )
1364 return false;
1365
1366 SetValue(defVal);
1367 }
1368
1369 return true;
1370 }
1371
1372
1373 const wxPGEditor* wxPGProperty::GetEditorClass() const
1374 {
1375 const wxPGEditor* editor;
1376
1377 if ( !m_customEditor )
1378 {
1379 editor = DoGetEditorClass();
1380 }
1381 else
1382 editor = m_customEditor;
1383
1384 //
1385 // Maybe override editor if common value specified
1386 if ( GetDisplayedCommonValueCount() )
1387 {
1388 // TextCtrlAndButton -> ComboBoxAndButton
1389 if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlAndButtonEditor)) )
1390 editor = wxPG_EDITOR(ChoiceAndButton);
1391
1392 // TextCtrl -> ComboBox
1393 else if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlEditor)) )
1394 editor = wxPG_EDITOR(ComboBox);
1395 }
1396
1397 return editor;
1398 }
1399
1400 bool wxPGProperty::HasVisibleChildren() const
1401 {
1402 unsigned int i;
1403
1404 for ( i=0; i<GetChildCount(); i++ )
1405 {
1406 wxPGProperty* child = Item(i);
1407
1408 if ( !child->HasFlag(wxPG_PROP_HIDDEN) )
1409 return true;
1410 }
1411
1412 return false;
1413 }
1414
1415 bool wxPGProperty::PrepareValueForDialogEditing( wxPropertyGrid* propGrid )
1416 {
1417 return propGrid->EditorValidate();
1418 }
1419
1420
1421 bool wxPGProperty::RecreateEditor()
1422 {
1423 wxPropertyGrid* pg = GetGrid();
1424 wxASSERT(pg);
1425
1426 wxPGProperty* selected = pg->GetSelection();
1427 if ( this == selected )
1428 {
1429 pg->DoSelectProperty(this, wxPG_SEL_FORCE);
1430 return true;
1431 }
1432 return false;
1433 }
1434
1435
1436 void wxPGProperty::SetValueImage( wxBitmap& bmp )
1437 {
1438 delete m_valueBitmap;
1439
1440 if ( &bmp && bmp.Ok() )
1441 {
1442 // Resize the image
1443 wxSize maxSz = GetGrid()->GetImageSize();
1444 wxSize imSz(bmp.GetWidth(),bmp.GetHeight());
1445
1446 if ( imSz.x != maxSz.x || imSz.y != maxSz.y )
1447 {
1448 // Create a memory DC
1449 wxBitmap* bmpNew = new wxBitmap(maxSz.x,maxSz.y,bmp.GetDepth());
1450
1451 wxMemoryDC dc;
1452 dc.SelectObject(*bmpNew);
1453
1454 // Scale
1455 // FIXME: This is ugly - use image or wait for scaling patch.
1456 double scaleX = (double)maxSz.x / (double)imSz.x;
1457 double scaleY = (double)maxSz.y / (double)imSz.y;
1458
1459 dc.SetUserScale(scaleX,scaleY);
1460
1461 dc.DrawBitmap( bmp, 0, 0 );
1462
1463 m_valueBitmap = bmpNew;
1464 }
1465 else
1466 {
1467 m_valueBitmap = new wxBitmap(bmp);
1468 }
1469
1470 m_flags |= wxPG_PROP_CUSTOMIMAGE;
1471 }
1472 else
1473 {
1474 m_valueBitmap = NULL;
1475 m_flags &= ~(wxPG_PROP_CUSTOMIMAGE);
1476 }
1477 }
1478
1479
1480 wxPGProperty* wxPGProperty::GetMainParent() const
1481 {
1482 const wxPGProperty* curChild = this;
1483 const wxPGProperty* curParent = m_parent;
1484
1485 while ( curParent && !curParent->IsCategory() )
1486 {
1487 curChild = curParent;
1488 curParent = curParent->m_parent;
1489 }
1490
1491 return (wxPGProperty*) curChild;
1492 }
1493
1494
1495 const wxPGProperty* wxPGProperty::GetLastVisibleSubItem() const
1496 {
1497 //
1498 // Returns last visible sub-item, recursively.
1499 if ( !IsExpanded() || !GetChildCount() )
1500 return this;
1501
1502 return Last()->GetLastVisibleSubItem();
1503 }
1504
1505
1506 bool wxPGProperty::IsVisible() const
1507 {
1508 const wxPGProperty* parent;
1509
1510 if ( HasFlag(wxPG_PROP_HIDDEN) )
1511 return false;
1512
1513 for ( parent = GetParent(); parent != NULL; parent = parent->GetParent() )
1514 {
1515 if ( !parent->IsExpanded() || parent->HasFlag(wxPG_PROP_HIDDEN) )
1516 return false;
1517 }
1518
1519 return true;
1520 }
1521
1522 wxPropertyGrid* wxPGProperty::GetGridIfDisplayed() const
1523 {
1524 wxPropertyGridPageState* state = GetParentState();
1525 wxPropertyGrid* propGrid = state->GetGrid();
1526 if ( state == propGrid->GetState() )
1527 return propGrid;
1528 return NULL;
1529 }
1530
1531
1532 int wxPGProperty::GetY2( int lh ) const
1533 {
1534 const wxPGProperty* parent;
1535 const wxPGProperty* child = this;
1536
1537 int y = 0;
1538
1539 for ( parent = GetParent(); parent != NULL; parent = child->GetParent() )
1540 {
1541 if ( !parent->IsExpanded() )
1542 return -1;
1543 y += parent->GetChildrenHeight(lh, child->GetIndexInParent());
1544 y += lh;
1545 child = parent;
1546 }
1547
1548 y -= lh; // need to reduce one level
1549
1550 return y;
1551 }
1552
1553
1554 int wxPGProperty::GetY() const
1555 {
1556 return GetY2(GetGrid()->GetRowHeight());
1557 }
1558
1559 // This is used by Insert etc.
1560 void wxPGProperty::AddChild2( wxPGProperty* prop, int index, bool correct_mode )
1561 {
1562 if ( index < 0 || (size_t)index >= m_children.size() )
1563 {
1564 if ( correct_mode ) prop->m_arrIndex = m_children.size();
1565 m_children.push_back( prop );
1566 }
1567 else
1568 {
1569 m_children.insert( m_children.begin()+index, prop);
1570 if ( correct_mode ) FixIndexesOfChildren( index );
1571 }
1572
1573 prop->m_parent = this;
1574 }
1575
1576 // This is used by properties that have fixed sub-properties
1577 void wxPGProperty::AddChild( wxPGProperty* prop )
1578 {
1579 wxASSERT_MSG( prop->GetBaseName().length(),
1580 "Property's children must have unique, non-empty names within their scope" );
1581
1582 prop->m_arrIndex = m_children.size();
1583 m_children.push_back( prop );
1584
1585 int custImgHeight = prop->OnMeasureImage().y;
1586 if ( custImgHeight < 0 /*|| custImgHeight > 1*/ )
1587 prop->m_flags |= wxPG_PROP_CUSTOMIMAGE;
1588
1589 prop->m_parent = this;
1590 }
1591
1592 void wxPGProperty::RemoveChild( wxPGProperty* p )
1593 {
1594 wxArrayPGProperty::iterator it;
1595 wxArrayPGProperty& children = m_children;
1596
1597 for ( it=children.begin(); it != children.end(); it++ )
1598 {
1599 if ( *it == p )
1600 {
1601 m_children.erase(it);
1602 break;
1603 }
1604 }
1605 }
1606
1607 void wxPGProperty::AdaptListToValue( wxVariant& list, wxVariant* value ) const
1608 {
1609 wxASSERT( GetChildCount() );
1610 wxASSERT( !IsCategory() );
1611
1612 *value = GetValue();
1613
1614 if ( !list.GetCount() )
1615 return;
1616
1617 wxASSERT( GetChildCount() >= (unsigned int)list.GetCount() );
1618
1619 bool allChildrenSpecified;
1620
1621 // Don't fully update aggregate properties unless all children have
1622 // specified value
1623 if ( HasFlag(wxPG_PROP_AGGREGATE) )
1624 allChildrenSpecified = AreAllChildrenSpecified(&list);
1625 else
1626 allChildrenSpecified = true;
1627
1628 wxVariant childValue = list[0];
1629 unsigned int i;
1630 unsigned int n = 0;
1631
1632 //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
1633
1634 for ( i=0; i<GetChildCount(); i++ )
1635 {
1636 const wxPGProperty* child = Item(i);
1637
1638 if ( childValue.GetName() == child->GetBaseName() )
1639 {
1640 //wxLogDebug(wxT(" %s(n=%i), %s"),childValue.GetName().c_str(),n,childValue.GetType().c_str());
1641
1642 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
1643 {
1644 wxVariant cv2(child->GetValue());
1645 child->AdaptListToValue(childValue, &cv2);
1646 childValue = cv2;
1647 }
1648
1649 if ( allChildrenSpecified )
1650 ChildChanged(*value, i, childValue);
1651 n++;
1652 if ( n == (unsigned int)list.GetCount() )
1653 break;
1654 childValue = list[n];
1655 }
1656 }
1657 }
1658
1659
1660 void wxPGProperty::FixIndexesOfChildren( size_t starthere )
1661 {
1662 size_t i;
1663 for ( i=starthere;i<GetChildCount();i++)
1664 Item(i)->m_arrIndex = i;
1665 }
1666
1667
1668 // Returns (direct) child property with given name (or NULL if not found)
1669 wxPGProperty* wxPGProperty::GetPropertyByName( const wxString& name ) const
1670 {
1671 size_t i;
1672
1673 for ( i=0; i<GetChildCount(); i++ )
1674 {
1675 wxPGProperty* p = Item(i);
1676 if ( p->m_name == name )
1677 return p;
1678 }
1679
1680 // Does it have point, then?
1681 int pos = name.Find(wxS('.'));
1682 if ( pos <= 0 )
1683 return (wxPGProperty*) NULL;
1684
1685 wxPGProperty* p = GetPropertyByName(name. substr(0,pos));
1686
1687 if ( !p || !p->GetChildCount() )
1688 return NULL;
1689
1690 return p->GetPropertyByName(name.substr(pos+1,name.length()-pos-1));
1691 }
1692
1693 wxPGProperty* wxPGProperty::GetPropertyByNameWH( const wxString& name, unsigned int hintIndex ) const
1694 {
1695 unsigned int i = hintIndex;
1696
1697 if ( i >= GetChildCount() )
1698 i = 0;
1699
1700 unsigned int lastIndex = i - 1;
1701
1702 if ( lastIndex >= GetChildCount() )
1703 lastIndex = GetChildCount() - 1;
1704
1705 for (;;)
1706 {
1707 wxPGProperty* p = Item(i);
1708 if ( p->m_name == name )
1709 return p;
1710
1711 if ( i == lastIndex )
1712 break;
1713
1714 i++;
1715 if ( i == GetChildCount() )
1716 i = 0;
1717 };
1718
1719 return NULL;
1720 }
1721
1722 int wxPGProperty::GetChildrenHeight( int lh, int iMax_ ) const
1723 {
1724 // Returns height of children, recursively, and
1725 // by taking expanded/collapsed status into account.
1726 //
1727 // iMax is used when finding property y-positions.
1728 //
1729 unsigned int i = 0;
1730 int h = 0;
1731
1732 if ( iMax_ == -1 )
1733 iMax_ = GetChildCount();
1734
1735 unsigned int iMax = iMax_;
1736
1737 wxASSERT( iMax <= GetChildCount() );
1738
1739 if ( !IsExpanded() && GetParent() )
1740 return 0;
1741
1742 while ( i < iMax )
1743 {
1744 wxPGProperty* pwc = (wxPGProperty*) Item(i);
1745
1746 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1747 {
1748 if ( !pwc->IsExpanded() ||
1749 pwc->GetChildCount() == 0 )
1750 h += lh;
1751 else
1752 h += pwc->GetChildrenHeight(lh) + lh;
1753 }
1754
1755 i++;
1756 }
1757
1758 return h;
1759 }
1760
1761 wxPGProperty* wxPGProperty::GetItemAtY( unsigned int y, unsigned int lh, unsigned int* nextItemY ) const
1762 {
1763 wxASSERT( nextItemY );
1764
1765 // Linear search at the moment
1766 //
1767 // nextItemY = y of next visible property, final value will be written back.
1768 wxPGProperty* result = NULL;
1769 wxPGProperty* current = NULL;
1770 unsigned int iy = *nextItemY;
1771 unsigned int i = 0;
1772 unsigned int iMax = GetChildCount();
1773
1774 while ( i < iMax )
1775 {
1776 wxPGProperty* pwc = Item(i);
1777
1778 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1779 {
1780 // Found?
1781 if ( y < iy )
1782 {
1783 result = current;
1784 break;
1785 }
1786
1787 iy += lh;
1788
1789 if ( pwc->IsExpanded() &&
1790 pwc->GetChildCount() > 0 )
1791 {
1792 result = (wxPGProperty*) pwc->GetItemAtY( y, lh, &iy );
1793 if ( result )
1794 break;
1795 }
1796
1797 current = pwc;
1798 }
1799
1800 i++;
1801 }
1802
1803 // Found?
1804 if ( !result && y < iy )
1805 result = current;
1806
1807 *nextItemY = iy;
1808
1809 /*
1810 if ( current )
1811 wxLogDebug(wxT("%s::GetItemAtY(%i) -> %s"),this->GetLabel().c_str(),y,current->GetLabel().c_str());
1812 else
1813 wxLogDebug(wxT("%s::GetItemAtY(%i) -> NULL"),this->GetLabel().c_str(),y);
1814 */
1815
1816 return (wxPGProperty*) result;
1817 }
1818
1819 void wxPGProperty::Empty()
1820 {
1821 size_t i;
1822 if ( !HasFlag(wxPG_PROP_CHILDREN_ARE_COPIES) )
1823 {
1824 for ( i=0; i<GetChildCount(); i++ )
1825 {
1826 delete m_children[i];
1827 }
1828 }
1829
1830 m_children.clear();
1831 }
1832
1833 void wxPGProperty::ChildChanged( wxVariant& WXUNUSED(thisValue),
1834 int WXUNUSED(childIndex),
1835 wxVariant& WXUNUSED(childValue) ) const
1836 {
1837 }
1838
1839 bool wxPGProperty::AreAllChildrenSpecified( wxVariant* pendingList ) const
1840 {
1841 unsigned int i;
1842
1843 const wxVariantList* pList = NULL;
1844 wxVariantList::const_iterator node;
1845
1846 if ( pendingList )
1847 {
1848 pList = &pendingList->GetList();
1849 node = pList->begin();
1850 }
1851
1852 for ( i=0; i<GetChildCount(); i++ )
1853 {
1854 wxPGProperty* child = Item(i);
1855 const wxVariant* listValue = NULL;
1856 wxVariant value;
1857
1858 if ( pendingList )
1859 {
1860 const wxString& childName = child->GetBaseName();
1861
1862 for ( ; node != pList->end(); node++ )
1863 {
1864 const wxVariant& item = *((const wxVariant*)*node);
1865 if ( item.GetName() == childName )
1866 {
1867 listValue = &item;
1868 value = item;
1869 break;
1870 }
1871 }
1872 }
1873
1874 if ( !listValue )
1875 value = child->GetValue();
1876
1877 if ( value.IsNull() )
1878 return false;
1879
1880 // Check recursively
1881 if ( child->GetChildCount() )
1882 {
1883 const wxVariant* childList = NULL;
1884
1885 if ( listValue && listValue->GetType() == wxPG_VARIANT_TYPE_LIST )
1886 childList = listValue;
1887
1888 if ( !child->AreAllChildrenSpecified((wxVariant*)childList) )
1889 return false;
1890 }
1891 }
1892
1893 return true;
1894 }
1895
1896 wxPGProperty* wxPGProperty::UpdateParentValues()
1897 {
1898 wxPGProperty* parent = m_parent;
1899 if ( parent && parent->HasFlag(wxPG_PROP_COMPOSED_VALUE) &&
1900 !parent->IsCategory() && !parent->IsRoot() )
1901 {
1902 wxString s;
1903 parent->GenerateComposedValue(s, 0);
1904 parent->m_value = s;
1905 return parent->UpdateParentValues();
1906 }
1907 return this;
1908 }
1909
1910 bool wxPGProperty::IsTextEditable() const
1911 {
1912 if ( HasFlag(wxPG_PROP_READONLY) )
1913 return false;
1914
1915 if ( HasFlag(wxPG_PROP_NOEDITOR) &&
1916 (GetChildCount() ||
1917 wxString(GetEditorClass()->GetClassInfo()->GetClassName()).EndsWith(wxS("Button")))
1918 )
1919 return false;
1920
1921 return true;
1922 }
1923
1924 // Call for after sub-properties added with AddChild
1925 void wxPGProperty::PrepareSubProperties()
1926 {
1927 wxPropertyGridPageState* state = GetParentState();
1928
1929 wxASSERT(state);
1930
1931 if ( !GetChildCount() )
1932 return;
1933
1934 wxByte depth = m_depth + 1;
1935 wxByte depthBgCol = m_depthBgCol;
1936
1937 FlagType inheritFlags = m_flags & wxPG_INHERITED_PROPFLAGS;
1938
1939 wxByte bgColIndex = m_bgColIndex;
1940 wxByte fgColIndex = m_fgColIndex;
1941
1942 //
1943 // Set some values to the children
1944 //
1945 size_t i = 0;
1946 wxPGProperty* nparent = this;
1947
1948 while ( i < nparent->GetChildCount() )
1949 {
1950 wxPGProperty* np = nparent->Item(i);
1951
1952 np->m_parentState = state;
1953 np->m_flags |= inheritFlags; // Hideable also if parent.
1954 np->m_depth = depth;
1955 np->m_depthBgCol = depthBgCol;
1956 np->m_bgColIndex = bgColIndex;
1957 np->m_fgColIndex = fgColIndex;
1958
1959 // Also handle children of children
1960 if ( np->GetChildCount() > 0 )
1961 {
1962 nparent = np;
1963 i = 0;
1964
1965 // Init
1966 nparent->SetParentalType(wxPG_PROP_AGGREGATE);
1967 nparent->SetExpanded(false);
1968 depth++;
1969 }
1970 else
1971 {
1972 // Next sibling
1973 i++;
1974 }
1975
1976 // After reaching last sibling, go back to processing
1977 // siblings of the parent
1978 while ( i >= nparent->GetChildCount() )
1979 {
1980 // Exit the loop when top parent hit
1981 if ( nparent == this )
1982 break;
1983
1984 depth--;
1985
1986 i = nparent->GetIndexInParent() + 1;
1987 nparent = nparent->GetParent();
1988 }
1989 }
1990 }
1991
1992 // Call after fixed sub-properties added/removed after creation.
1993 // if oldSelInd >= 0 and < new max items, then selection is
1994 // moved to it. Note: oldSelInd -2 indicates that this property
1995 // should be selected.
1996 void wxPGProperty::SubPropsChanged( int oldSelInd )
1997 {
1998 wxPropertyGridPageState* state = GetParentState();
1999 wxPropertyGrid* grid = state->GetGrid();
2000
2001 PrepareSubProperties();
2002
2003 wxPGProperty* sel = (wxPGProperty*) NULL;
2004 if ( oldSelInd >= (int)m_children.size() )
2005 oldSelInd = (int)m_children.size() - 1;
2006
2007 if ( oldSelInd >= 0 )
2008 sel = m_children[oldSelInd];
2009 else if ( oldSelInd == -2 )
2010 sel = this;
2011
2012 if ( sel )
2013 state->DoSelectProperty(sel);
2014
2015 if ( state == grid->GetState() )
2016 {
2017 grid->GetPanel()->Refresh();
2018 }
2019 }
2020
2021 // -----------------------------------------------------------------------
2022 // wxPGRootProperty
2023 // -----------------------------------------------------------------------
2024
2025 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPGRootProperty,none,TextCtrl)
2026 IMPLEMENT_DYNAMIC_CLASS(wxPGRootProperty, wxPGProperty)
2027
2028
2029 wxPGRootProperty::wxPGRootProperty()
2030 : wxPGProperty()
2031 {
2032 #ifdef __WXDEBUG__
2033 m_name = wxS("<root>");
2034 #endif
2035 SetParentalType(0);
2036 m_depth = 0;
2037 }
2038
2039
2040 wxPGRootProperty::~wxPGRootProperty()
2041 {
2042 }
2043
2044
2045 // -----------------------------------------------------------------------
2046 // wxPropertyCategory
2047 // -----------------------------------------------------------------------
2048
2049 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPropertyCategory,none,TextCtrl)
2050 IMPLEMENT_DYNAMIC_CLASS(wxPropertyCategory, wxPGProperty)
2051
2052 void wxPropertyCategory::Init()
2053 {
2054 // don't set colour - prepareadditem method should do this
2055 SetParentalType(wxPG_PROP_CATEGORY);
2056 m_capFgColIndex = 1;
2057 m_textExtent = -1;
2058 }
2059
2060 wxPropertyCategory::wxPropertyCategory()
2061 : wxPGProperty()
2062 {
2063 Init();
2064 }
2065
2066
2067 wxPropertyCategory::wxPropertyCategory( const wxString &label, const wxString& name )
2068 : wxPGProperty(label,name)
2069 {
2070 Init();
2071 }
2072
2073
2074 wxPropertyCategory::~wxPropertyCategory()
2075 {
2076 }
2077
2078
2079 wxString wxPropertyCategory::GetValueAsString( int ) const
2080 {
2081 return wxEmptyString;
2082 }
2083
2084 int wxPropertyCategory::GetTextExtent( const wxWindow* wnd, const wxFont& font ) const
2085 {
2086 if ( m_textExtent > 0 )
2087 return m_textExtent;
2088 int x = 0, y = 0;
2089 ((wxWindow*)wnd)->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2090 return x;
2091 }
2092
2093 void wxPropertyCategory::CalculateTextExtent( wxWindow* wnd, const wxFont& font )
2094 {
2095 int x = 0, y = 0;
2096 wnd->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2097 m_textExtent = x;
2098 }
2099
2100 // -----------------------------------------------------------------------
2101 // wxPGAttributeStorage
2102 // -----------------------------------------------------------------------
2103
2104 wxPGAttributeStorage::wxPGAttributeStorage()
2105 {
2106 }
2107
2108 wxPGAttributeStorage::~wxPGAttributeStorage()
2109 {
2110 wxPGHashMapS2P::iterator it;
2111
2112 for ( it = m_map.begin(); it != m_map.end(); it++ )
2113 {
2114 wxVariantData* data = (wxVariantData*) it->second;
2115 data->DecRef();
2116 }
2117 }
2118
2119 void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value )
2120 {
2121 wxVariantData* data = value.GetData();
2122
2123 // Free old, if any
2124 wxPGHashMapS2P::iterator it = m_map.find(name);
2125 if ( it != m_map.end() )
2126 {
2127 ((wxVariantData*)it->second)->DecRef();
2128
2129 if ( !data )
2130 {
2131 // If Null variant, just remove from set
2132 m_map.erase(it);
2133 return;
2134 }
2135 }
2136
2137 if ( data )
2138 {
2139 data->IncRef();
2140
2141 m_map[name] = data;
2142 }
2143 }
2144
2145 #endif // wxUSE_PROPGRID