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