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