Reworked the way child properties can be added to a property that has not yet been...
[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 dc.DrawText( text,
98 rect.x+xOffset+wxPG_XBEFORETEXT,
99 rect.y+((rect.height-dc.GetCharHeight())/2) );
100 }
101
102 void wxPGCellRenderer::DrawEditorValue( wxDC& dc, const wxRect& rect,
103 int xOffset, const wxString& text,
104 wxPGProperty* property,
105 const wxPGEditor* editor ) const
106 {
107 int yOffset = ((rect.height-dc.GetCharHeight())/2);
108
109 if ( editor )
110 {
111 wxRect rect2(rect);
112 rect2.x += xOffset;
113 rect2.y += yOffset;
114 rect2.height -= yOffset;
115 editor->DrawValue( dc, rect2, property, text );
116 }
117 else
118 {
119 dc.DrawText( text,
120 rect.x+xOffset+wxPG_XBEFORETEXT,
121 rect.y+yOffset );
122 }
123 }
124
125 void wxPGCellRenderer::DrawCaptionSelectionRect( wxDC& dc, int x, int y, int w, int h ) const
126 {
127 wxRect focusRect(x,y+((h-dc.GetCharHeight())/2),w,h);
128 wxPGDrawFocusRect(dc,focusRect);
129 }
130
131 int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell& cell, int flags ) const
132 {
133 int imageWidth = 0;
134
135 // If possible, use cell colours
136 if ( !(flags & DontUseCellBgCol) )
137 {
138 dc.SetPen(cell.GetBgCol());
139 dc.SetBrush(cell.GetBgCol());
140 }
141
142 if ( !(flags & DontUseCellFgCol) )
143 {
144 dc.SetTextForeground(cell.GetFgCol());
145 }
146
147 // Draw Background, but only if not rendering in control
148 // (as control already has rendered correct background).
149 if ( !(flags & (Control|ChoicePopup)) )
150 dc.DrawRectangle(rect);
151
152 const wxBitmap& bmp = cell.GetBitmap();
153 if ( bmp.Ok() &&
154 // Do not draw oversized bitmap outside choice popup
155 ((flags & ChoicePopup) || bmp.GetHeight() < rect.height )
156 )
157 {
158 dc.DrawBitmap( bmp,
159 rect.x + wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
160 rect.y + wxPG_CUSTOM_IMAGE_SPACINGY,
161 true );
162 imageWidth = bmp.GetWidth();
163 }
164
165 return imageWidth;
166 }
167
168 // -----------------------------------------------------------------------
169 // wxPGDefaultRenderer
170 // -----------------------------------------------------------------------
171
172 void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
173 const wxPropertyGrid* propertyGrid, wxPGProperty* property,
174 int column, int item, int flags ) const
175 {
176 bool isUnspecified = property->IsValueUnspecified();
177
178 if ( column == 1 && item == -1 )
179 {
180 int cmnVal = property->GetCommonValue();
181 if ( cmnVal >= 0 )
182 {
183 // Common Value
184 if ( !isUnspecified )
185 DrawText( dc, rect, 0, propertyGrid->GetCommonValueLabel(cmnVal) );
186 return;
187 }
188 }
189
190 const wxPGEditor* editor = NULL;
191 const wxPGCell* cell = NULL;
192
193 wxString text;
194 int imageWidth = 0;
195 int preDrawFlags = flags;
196
197 property->GetDisplayInfo(column, item, flags, &text, &cell);
198
199 imageWidth = PreDrawCell( dc, rect, *cell, preDrawFlags );
200
201 if ( column == 1 )
202 {
203 if ( !isUnspecified )
204 {
205 editor = property->GetColumnEditor(column);
206
207 // Regular property value
208
209 wxSize imageSize = propertyGrid->GetImageSize(property, item);
210
211 wxPGPaintData paintdata;
212 paintdata.m_parent = propertyGrid;
213 paintdata.m_choiceItem = item;
214
215 if ( imageSize.x > 0 )
216 {
217 wxRect imageRect(rect.x + wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
218 rect.y+wxPG_CUSTOM_IMAGE_SPACINGY,
219 wxPG_CUSTOM_IMAGE_WIDTH,
220 rect.height-(wxPG_CUSTOM_IMAGE_SPACINGY*2));
221
222 dc.SetPen( wxPen(propertyGrid->GetCellTextColour(), 1, wxSOLID) );
223
224 paintdata.m_drawnWidth = imageSize.x;
225 paintdata.m_drawnHeight = imageSize.y;
226
227 property->OnCustomPaint( dc, imageRect, paintdata );
228
229 imageWidth = paintdata.m_drawnWidth;
230 }
231
232 text = property->GetValueAsString();
233
234 // Add units string?
235 if ( propertyGrid->GetColumnCount() <= 2 )
236 {
237 wxString unitsString = property->GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
238 if ( unitsString.length() )
239 text = wxString::Format(wxS("%s %s"), text.c_str(), unitsString.c_str() );
240 }
241 }
242
243 if ( text.length() == 0 )
244 {
245 // Try to show inline help if no text
246 wxVariant vInlineHelp = property->GetAttribute(wxPGGlobalVars->m_strInlineHelp);
247 if ( !vInlineHelp.IsNull() )
248 {
249 text = vInlineHelp.GetString();
250 dc.SetTextForeground(propertyGrid->GetCellDisabledTextColour());
251 }
252 }
253 }
254
255 int imageOffset = property->GetImageOffset(imageWidth);
256
257 DrawEditorValue( dc, rect, imageOffset, text, property, editor );
258
259 // active caption gets nice dotted rectangle
260 if ( property->IsCategory() /*&& column == 0*/ )
261 {
262 if ( flags & Selected )
263 {
264 if ( imageWidth > 0 )
265 {
266 imageOffset -= DEFAULT_IMAGE_OFFSET_INCREMENT;
267 imageWidth += wxCC_CUSTOM_IMAGE_MARGIN2 + 4;
268 }
269
270 DrawCaptionSelectionRect( dc,
271 rect.x+wxPG_XBEFORETEXT-wxPG_CAPRECTXMARGIN+imageOffset,
272 rect.y-wxPG_CAPRECTYMARGIN+1,
273 ((wxPropertyCategory*)property)->GetTextExtent(propertyGrid,
274 propertyGrid->GetCaptionFont())
275 +(wxPG_CAPRECTXMARGIN*2),
276 propertyGrid->GetFontHeight()+(wxPG_CAPRECTYMARGIN*2) );
277 }
278 }
279 }
280
281 wxSize wxPGDefaultRenderer::GetImageSize( const wxPGProperty* property,
282 int column,
283 int item ) const
284 {
285 if ( property && column == 1 )
286 {
287 if ( item == -1 )
288 {
289 wxBitmap* bmp = property->GetValueImage();
290
291 if ( bmp && bmp->Ok() )
292 return wxSize(bmp->GetWidth(),bmp->GetHeight());
293 }
294 }
295 return wxSize(0,0);
296 }
297
298 // -----------------------------------------------------------------------
299 // wxPGCellData
300 // -----------------------------------------------------------------------
301
302 wxPGCellData::wxPGCellData()
303 : wxObjectRefData()
304 {
305 m_hasValidText = false;
306 }
307
308 // -----------------------------------------------------------------------
309 // wxPGCell
310 // -----------------------------------------------------------------------
311
312 wxPGCell::wxPGCell()
313 : wxObject()
314 {
315 }
316
317 wxPGCell::wxPGCell( const wxString& text,
318 const wxBitmap& bitmap,
319 const wxColour& fgCol,
320 const wxColour& bgCol )
321 : wxObject()
322 {
323 wxPGCellData* data = new wxPGCellData();
324 m_refData = data;
325 data->m_text = text;
326 data->m_bitmap = bitmap;
327 data->m_fgCol = fgCol;
328 data->m_bgCol = bgCol;
329 data->m_hasValidText = true;
330 }
331
332 wxObjectRefData *wxPGCell::CloneRefData( const wxObjectRefData *data ) const
333 {
334 wxPGCellData* c = new wxPGCellData();
335 const wxPGCellData* o = (const wxPGCellData*) data;
336 c->m_text = o->m_text;
337 c->m_bitmap = o->m_bitmap;
338 c->m_fgCol = o->m_fgCol;
339 c->m_bgCol = o->m_bgCol;
340 c->m_hasValidText = o->m_hasValidText;
341 return c;
342 }
343
344 void wxPGCell::SetText( const wxString& text )
345 {
346 AllocExclusive();
347
348 GetData()->SetText(text);
349 }
350
351 void wxPGCell::SetBitmap( const wxBitmap& bitmap )
352 {
353 AllocExclusive();
354
355 GetData()->SetBitmap(bitmap);
356 }
357
358 void wxPGCell::SetFgCol( const wxColour& col )
359 {
360 AllocExclusive();
361
362 GetData()->SetFgCol(col);
363 }
364
365 void wxPGCell::SetBgCol( const wxColour& col )
366 {
367 AllocExclusive();
368
369 GetData()->SetBgCol(col);
370 }
371
372 void wxPGCell::MergeFrom( const wxPGCell& srcCell )
373 {
374 AllocExclusive();
375
376 wxPGCellData* data = GetData();
377
378 if ( srcCell.HasText() )
379 data->SetText(srcCell.GetText());
380
381 if ( srcCell.GetFgCol().IsOk() )
382 data->SetFgCol(srcCell.GetFgCol());
383
384 if ( srcCell.GetBgCol().IsOk() )
385 data->SetBgCol(srcCell.GetBgCol());
386
387 if ( srcCell.GetBitmap().IsOk() )
388 data->SetBitmap(srcCell.GetBitmap());
389 }
390
391 // -----------------------------------------------------------------------
392 // wxPGProperty
393 // -----------------------------------------------------------------------
394
395 IMPLEMENT_ABSTRACT_CLASS(wxPGProperty, wxObject)
396
397 wxString* wxPGProperty::sm_wxPG_LABEL = NULL;
398
399 void wxPGProperty::Init()
400 {
401 m_commonValue = -1;
402 m_arrIndex = 0xFFFF;
403 m_parent = NULL;
404
405 m_parentState = NULL;
406
407 m_clientData = NULL;
408 m_clientObject = NULL;
409
410 m_customEditor = NULL;
411 #if wxUSE_VALIDATORS
412 m_validator = NULL;
413 #endif
414 m_valueBitmap = NULL;
415
416 m_maxLen = 0; // infinite maximum length
417
418 m_flags = wxPG_PROP_PROPERTY;
419
420 m_depth = 1;
421
422 SetExpanded(true);
423 }
424
425
426 void wxPGProperty::Init( const wxString& label, const wxString& name )
427 {
428 // We really need to check if &label and &name are NULL pointers
429 // (this can if we are called before property grid has been initalized)
430
431 if ( (&label) != NULL && label != wxPG_LABEL )
432 m_label = label;
433
434 if ( (&name) != NULL && name != wxPG_LABEL )
435 DoSetName( name );
436 else
437 DoSetName( m_label );
438
439 Init();
440 }
441
442 void wxPGProperty::InitAfterAdded( wxPropertyGridPageState* pageState,
443 wxPropertyGrid* propgrid )
444 {
445 //
446 // Called after property has been added to grid or page
447 // (so propgrid can be NULL, too).
448
449 wxPGProperty* parent = m_parent;
450 bool parentIsRoot = parent->IsKindOf(CLASSINFO(wxPGRootProperty));
451
452 m_parentState = pageState;
453
454 #if wxPG_COMPATIBILITY_1_4
455 // Make sure deprecated virtual functions are not implemented
456 wxString s = GetValueAsString( 0xFFFF );
457 wxASSERT_MSG( s == g_invalidStringContent,
458 "Implement ValueToString() instead of GetValueAsString()" );
459 #endif
460
461 if ( !parentIsRoot && !parent->IsCategory() )
462 {
463 m_cells = parent->m_cells;
464 }
465
466 // If in hideable adding mode, or if assigned parent is hideable, then
467 // make this one hideable.
468 if (
469 ( !parentIsRoot && parent->HasFlag(wxPG_PROP_HIDDEN) ) ||
470 ( propgrid && (propgrid->HasInternalFlag(wxPG_FL_ADDING_HIDEABLES)) )
471 )
472 SetFlag( wxPG_PROP_HIDDEN );
473
474 // Set custom image flag.
475 int custImgHeight = OnMeasureImage().y;
476 if ( custImgHeight < 0 )
477 {
478 SetFlag(wxPG_PROP_CUSTOMIMAGE);
479 }
480
481 if ( propgrid && (propgrid->HasFlag(wxPG_LIMITED_EDITING)) )
482 SetFlag(wxPG_PROP_NOEDITOR);
483
484 // Make sure parent has some parental flags
485 if ( !parent->HasFlag(wxPG_PROP_PARENTAL_FLAGS) )
486 parent->SetParentalType(wxPG_PROP_MISC_PARENT);
487
488 if ( !IsCategory() )
489 {
490 // This is not a category.
491
492 // Depth.
493 //
494 unsigned char depth = 1;
495 if ( !parentIsRoot )
496 {
497 depth = parent->m_depth;
498 if ( !parent->IsCategory() )
499 depth++;
500 }
501 m_depth = depth;
502 unsigned char greyDepth = depth;
503
504 if ( !parentIsRoot )
505 {
506 wxPropertyCategory* pc;
507
508 if ( parent->IsCategory() )
509 pc = (wxPropertyCategory* ) parent;
510 else
511 // This conditional compile is necessary to
512 // bypass some compiler bug.
513 pc = pageState->GetPropertyCategory(parent);
514
515 if ( pc )
516 greyDepth = pc->GetDepth();
517 else
518 greyDepth = parent->m_depthBgCol;
519 }
520
521 m_depthBgCol = greyDepth;
522 }
523 else
524 {
525 // This is a category.
526
527 // depth
528 unsigned char depth = 1;
529 if ( !parentIsRoot )
530 {
531 depth = parent->m_depth + 1;
532 }
533 m_depth = depth;
534 m_depthBgCol = depth;
535 }
536
537 //
538 // Has initial children
539 if ( GetChildCount() )
540 {
541 // Check parental flags
542 wxASSERT_MSG( ((m_flags & wxPG_PROP_PARENTAL_FLAGS) ==
543 wxPG_PROP_AGGREGATE) ||
544 ((m_flags & wxPG_PROP_PARENTAL_FLAGS) ==
545 wxPG_PROP_MISC_PARENT),
546 "wxPGProperty parental flags set incorrectly at "
547 "this time" );
548
549 if ( HasFlag(wxPG_PROP_AGGREGATE) )
550 {
551 // Properties with private children are not expanded by default.
552 SetExpanded(false);
553 }
554 else if ( propgrid && propgrid->HasFlag(wxPG_HIDE_MARGIN) )
555 {
556 // ...unless it cannot be expanded by user and therefore must
557 // remain visible at all times
558 SetExpanded(true);
559 }
560
561 //
562 // Prepare children recursively
563 for ( unsigned int i=0; i<GetChildCount(); i++ )
564 {
565 wxPGProperty* child = Item(i);
566 child->InitAfterAdded(pageState, pageState->GetGrid());
567 }
568
569 if ( propgrid && (propgrid->GetExtraStyle() & wxPG_EX_AUTO_UNSPECIFIED_VALUES) )
570 SetFlagRecursively(wxPG_PROP_AUTO_UNSPECIFIED, true);
571 }
572 }
573
574 wxPGProperty::wxPGProperty()
575 : wxObject()
576 {
577 Init();
578 }
579
580
581 wxPGProperty::wxPGProperty( const wxString& label, const wxString& name )
582 : wxObject()
583 {
584 Init( label, name );
585 }
586
587
588 wxPGProperty::~wxPGProperty()
589 {
590 delete m_clientObject;
591
592 Empty(); // this deletes items
593
594 delete m_valueBitmap;
595 #if wxUSE_VALIDATORS
596 delete m_validator;
597 #endif
598
599 // This makes it easier for us to detect dangling pointers
600 m_parent = NULL;
601 }
602
603
604 bool wxPGProperty::IsSomeParent( wxPGProperty* candidate ) const
605 {
606 wxPGProperty* parent = m_parent;
607 do
608 {
609 if ( parent == candidate )
610 return true;
611 parent = parent->m_parent;
612 } while ( parent );
613 return false;
614 }
615
616
617 wxString wxPGProperty::GetName() const
618 {
619 wxPGProperty* parent = GetParent();
620
621 if ( !m_name.length() || !parent || parent->IsCategory() || parent->IsRoot() )
622 return m_name;
623
624 return m_parent->GetName() + wxS(".") + m_name;
625 }
626
627 wxPropertyGrid* wxPGProperty::GetGrid() const
628 {
629 if ( !m_parentState )
630 return NULL;
631 return m_parentState->GetGrid();
632 }
633
634 int wxPGProperty::Index( const wxPGProperty* p ) const
635 {
636 for ( unsigned int i = 0; i<m_children.size(); i++ )
637 {
638 if ( p == m_children[i] )
639 return i;
640 }
641 return wxNOT_FOUND;
642 }
643
644 bool wxPGProperty::ValidateValue( wxVariant& WXUNUSED(value), wxPGValidationInfo& WXUNUSED(validationInfo) ) const
645 {
646 return true;
647 }
648
649 void wxPGProperty::OnSetValue()
650 {
651 }
652
653 void wxPGProperty::RefreshChildren ()
654 {
655 }
656
657 void wxPGProperty::OnValidationFailure( wxVariant& WXUNUSED(pendingValue) )
658 {
659 }
660
661 void wxPGProperty::GetDisplayInfo( unsigned int column,
662 int choiceIndex,
663 int flags,
664 wxString* pString,
665 const wxPGCell** pCell )
666 {
667 const wxPGCell* cell = NULL;
668
669 if ( !(flags & wxPGCellRenderer::ChoicePopup) )
670 {
671 // Not painting listi of choice popups, so get text from property
672 cell = &GetCell(column);
673 if ( cell->HasText() )
674 {
675 *pString = cell->GetText();
676 }
677 else
678 {
679 if ( column == 0 )
680 *pString = GetLabel();
681 else if ( column == 1 )
682 *pString = GetDisplayedString();
683 else if ( column == 2 )
684 *pString = GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
685 }
686 }
687 else
688 {
689 wxASSERT( column == 1 );
690
691 if ( choiceIndex != wxNOT_FOUND )
692 {
693 const wxPGChoiceEntry& entry = m_choices[choiceIndex];
694 if ( entry.GetBitmap().IsOk() ||
695 entry.GetFgCol().IsOk() ||
696 entry.GetBgCol().IsOk() )
697 cell = &entry;
698 *pString = m_choices.GetLabel(choiceIndex);
699 }
700 }
701
702 if ( !cell )
703 cell = &GetCell(column);
704
705 wxASSERT_MSG( cell->GetData(),
706 wxString::Format("Invalid cell for property %s",
707 GetName().c_str()) );
708
709 *pCell = cell;
710 }
711
712 /*
713 wxString wxPGProperty::GetColumnText( unsigned int col, int choiceIndex ) const
714 {
715
716 if ( col != 1 || choiceIndex == wxNOT_FOUND )
717 {
718 const wxPGCell& cell = GetCell(col);
719 if ( cell->HasText() )
720 {
721 return cell->GetText();
722 }
723 else
724 {
725 if ( col == 0 )
726 return GetLabel();
727 else if ( col == 1 )
728 return GetDisplayedString();
729 else if ( col == 2 )
730 return GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
731 }
732 }
733 else
734 {
735 // Use choice
736 return m_choices.GetLabel(choiceIndex);
737 }
738
739 return wxEmptyString;
740 }
741 */
742
743 void wxPGProperty::DoGenerateComposedValue( wxString& text,
744 int argFlags,
745 const wxVariantList* valueOverrides,
746 wxPGHashMapS2S* childResults ) const
747 {
748 int i;
749 int iMax = m_children.size();
750
751 text.clear();
752 if ( iMax == 0 )
753 return;
754
755 if ( iMax > PWC_CHILD_SUMMARY_LIMIT &&
756 !(argFlags & wxPG_FULL_VALUE) )
757 iMax = PWC_CHILD_SUMMARY_LIMIT;
758
759 int iMaxMinusOne = iMax-1;
760
761 if ( !IsTextEditable() )
762 argFlags |= wxPG_UNEDITABLE_COMPOSITE_FRAGMENT;
763
764 wxPGProperty* curChild = m_children[0];
765
766 bool overridesLeft = false;
767 wxVariant overrideValue;
768 wxVariantList::const_iterator node;
769
770 if ( valueOverrides )
771 {
772 node = valueOverrides->begin();
773 if ( node != valueOverrides->end() )
774 {
775 overrideValue = *node;
776 overridesLeft = true;
777 }
778 }
779
780 for ( i = 0; i < iMax; i++ )
781 {
782 wxVariant childValue;
783
784 wxString childLabel = curChild->GetLabel();
785
786 // Check for value override
787 if ( overridesLeft && overrideValue.GetName() == childLabel )
788 {
789 if ( !overrideValue.IsNull() )
790 childValue = overrideValue;
791 else
792 childValue = curChild->GetValue();
793 ++node;
794 if ( node != valueOverrides->end() )
795 overrideValue = *node;
796 else
797 overridesLeft = false;
798 }
799 else
800 {
801 childValue = curChild->GetValue();
802 }
803
804 wxString s;
805 if ( !childValue.IsNull() )
806 {
807 if ( overridesLeft &&
808 curChild->HasFlag(wxPG_PROP_COMPOSED_VALUE) &&
809 childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
810 {
811 wxVariantList& childList = childValue.GetList();
812 DoGenerateComposedValue(s, argFlags|wxPG_COMPOSITE_FRAGMENT,
813 &childList, childResults);
814 }
815 else
816 {
817 s = curChild->ValueToString(childValue,
818 argFlags|wxPG_COMPOSITE_FRAGMENT);
819 }
820 }
821
822 if ( childResults && curChild->GetChildCount() )
823 (*childResults)[curChild->GetName()] = s;
824
825 bool skip = false;
826 if ( (argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT) && !s.length() )
827 skip = true;
828
829 if ( !curChild->GetChildCount() || skip )
830 text += s;
831 else
832 text += wxS("[") + s + wxS("]");
833
834 if ( i < iMaxMinusOne )
835 {
836 if ( text.length() > PWC_CHILD_SUMMARY_CHAR_LIMIT &&
837 !(argFlags & wxPG_EDITABLE_VALUE) &&
838 !(argFlags & wxPG_FULL_VALUE) )
839 break;
840
841 if ( !skip )
842 {
843 if ( !curChild->GetChildCount() )
844 text += wxS("; ");
845 else
846 text += wxS(" ");
847 }
848
849 curChild = m_children[i+1];
850 }
851 }
852
853 if ( (unsigned int)i < m_children.size() )
854 {
855 if ( !text.EndsWith(wxS("; ")) )
856 text += wxS("; ...");
857 else
858 text += wxS("...");
859 }
860 }
861
862 wxString wxPGProperty::ValueToString( wxVariant& WXUNUSED(value),
863 int argFlags ) const
864 {
865 wxCHECK_MSG( GetChildCount() > 0,
866 wxString(),
867 "If user property does not have any children, it must "
868 "override GetValueAsString" );
869
870 // FIXME: Currently code below only works if value is actually m_value
871 wxASSERT_MSG( argFlags & wxPG_VALUE_IS_CURRENT,
872 "Sorry, currently default wxPGProperty::ValueToString() "
873 "implementation only works if value is m_value." );
874
875 wxString text;
876 DoGenerateComposedValue(text, argFlags);
877 return text;
878 }
879
880 wxString wxPGProperty::GetValueAsString( int argFlags ) const
881 {
882 #if wxPG_COMPATIBILITY_1_4
883 // This is backwards compatibility test
884 // That is, to make sure this function is not overridden
885 // (instead, ValueToString() should be).
886 if ( argFlags == 0xFFFF )
887 {
888 // Do not override! (for backwards compliancy)
889 return g_invalidStringContent;
890 }
891 #endif
892
893 if ( IsValueUnspecified() )
894 return wxEmptyString;
895
896 if ( m_commonValue == -1 )
897 {
898 wxVariant value(GetValue());
899 return ValueToString(value, argFlags|wxPG_VALUE_IS_CURRENT);
900 }
901
902 //
903 // Return common value's string representation
904 wxPropertyGrid* pg = GetGrid();
905 const wxPGCommonValue* cv = pg->GetCommonValue(m_commonValue);
906
907 if ( argFlags & wxPG_FULL_VALUE )
908 {
909 return cv->GetLabel();
910 }
911 else if ( argFlags & wxPG_EDITABLE_VALUE )
912 {
913 return cv->GetEditableText();
914 }
915 else
916 {
917 return cv->GetLabel();
918 }
919 }
920
921 wxString wxPGProperty::GetValueString( int argFlags ) const
922 {
923 return GetValueAsString(argFlags);
924 }
925
926 bool wxPGProperty::IntToValue( wxVariant& variant, int number, int WXUNUSED(argFlags) ) const
927 {
928 variant = (long)number;
929 return true;
930 }
931
932 // Convert semicolon delimited tokens into child values.
933 bool wxPGProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
934 {
935 if ( !GetChildCount() )
936 return false;
937
938 unsigned int curChild = 0;
939
940 unsigned int iMax = m_children.size();
941
942 if ( iMax > PWC_CHILD_SUMMARY_LIMIT &&
943 !(argFlags & wxPG_FULL_VALUE) )
944 iMax = PWC_CHILD_SUMMARY_LIMIT;
945
946 bool changed = false;
947
948 wxString token;
949 size_t pos = 0;
950
951 // Its best only to add non-empty group items
952 bool addOnlyIfNotEmpty = false;
953 const wxChar delimeter = wxS(';');
954
955 size_t tokenStart = 0xFFFFFF;
956
957 wxVariantList temp_list;
958 wxVariant list(temp_list);
959
960 int propagatedFlags = argFlags & (wxPG_REPORT_ERROR|wxPG_PROGRAMMATIC_VALUE);
961
962 #ifdef __WXDEBUG__
963 bool debug_print = false;
964 #endif
965
966 #ifdef __WXDEBUG__
967 if ( debug_print )
968 wxLogDebug(wxT(">> %s.StringToValue('%s')"),GetLabel().c_str(),text.c_str());
969 #endif
970
971 wxString::const_iterator it = text.begin();
972 wxUniChar a;
973
974 if ( it != text.end() )
975 a = *it;
976 else
977 a = 0;
978
979 for ( ;; )
980 {
981 // How many units we iterate string forward at the end of loop?
982 // We need to keep track of this or risk going to negative
983 // with it-- operation.
984 unsigned int strPosIncrement = 1;
985
986 if ( tokenStart != 0xFFFFFF )
987 {
988 // Token is running
989 if ( a == delimeter || a == 0 )
990 {
991 token = text.substr(tokenStart,pos-tokenStart);
992 token.Trim(true);
993 size_t len = token.length();
994
995 if ( !addOnlyIfNotEmpty || len > 0 )
996 {
997 const wxPGProperty* child = Item(curChild);
998 wxVariant variant(child->GetValue());
999 wxString childName = child->GetBaseName();
1000
1001 #ifdef __WXDEBUG__
1002 if ( debug_print )
1003 wxLogDebug(wxT("token = '%s', child = %s"),
1004 token.c_str(), childName.c_str());
1005 #endif
1006
1007 // Add only if editable or setting programmatically
1008 if ( (argFlags & wxPG_PROGRAMMATIC_VALUE) ||
1009 !child->HasFlag(wxPG_PROP_DISABLED|wxPG_PROP_READONLY) )
1010 {
1011 if ( len > 0 )
1012 {
1013 if ( child->StringToValue(variant, token,
1014 propagatedFlags|wxPG_COMPOSITE_FRAGMENT) )
1015 {
1016 // We really need to set the variant's name
1017 // *after* child->StringToValue() has been
1018 // called, since variant's value may be set by
1019 // assigning another variant into it, which
1020 // then usually causes name to be copied (ie.
1021 // usually cleared) as well. wxBoolProperty
1022 // being case in point with its use of
1023 // wxPGVariant_Bool macro as an optimization.
1024 variant.SetName(childName);
1025 list.Append(variant);
1026
1027 changed = true;
1028 }
1029 }
1030 else
1031 {
1032 // Empty, becomes unspecified
1033 variant.MakeNull();
1034 variant.SetName(childName);
1035 list.Append(variant);
1036 changed = true;
1037 }
1038 }
1039
1040 curChild++;
1041 if ( curChild >= iMax )
1042 break;
1043 }
1044
1045 tokenStart = 0xFFFFFF;
1046 }
1047 }
1048 else
1049 {
1050 // Token is not running
1051 if ( a != wxS(' ') )
1052 {
1053
1054 addOnlyIfNotEmpty = false;
1055
1056 // Is this a group of tokens?
1057 if ( a == wxS('[') )
1058 {
1059 int depth = 1;
1060
1061 if ( it != text.end() ) ++it;
1062 pos++;
1063 size_t startPos = pos;
1064
1065 // Group item - find end
1066 while ( it != text.end() && depth > 0 )
1067 {
1068 a = *it;
1069 ++it;
1070 pos++;
1071
1072 if ( a == wxS(']') )
1073 depth--;
1074 else if ( a == wxS('[') )
1075 depth++;
1076 }
1077
1078 token = text.substr(startPos,pos-startPos-1);
1079
1080 if ( !token.length() )
1081 break;
1082
1083 const wxPGProperty* child = Item(curChild);
1084
1085 wxVariant oldChildValue = child->GetValue();
1086 wxVariant variant(oldChildValue);
1087
1088 if ( (argFlags & wxPG_PROGRAMMATIC_VALUE) ||
1089 !child->HasFlag(wxPG_PROP_DISABLED|wxPG_PROP_READONLY) )
1090 {
1091 wxString childName = child->GetBaseName();
1092
1093 bool stvRes = child->StringToValue( variant, token,
1094 propagatedFlags );
1095 if ( stvRes || (variant != oldChildValue) )
1096 {
1097 variant.SetName(childName);
1098 list.Append(variant);
1099
1100 changed = true;
1101 }
1102 else
1103 {
1104 // No changes...
1105 }
1106 }
1107
1108 curChild++;
1109 if ( curChild >= iMax )
1110 break;
1111
1112 addOnlyIfNotEmpty = true;
1113
1114 tokenStart = 0xFFFFFF;
1115 }
1116 else
1117 {
1118 tokenStart = pos;
1119
1120 if ( a == delimeter )
1121 strPosIncrement -= 1;
1122 }
1123 }
1124 }
1125
1126 if ( a == 0 )
1127 break;
1128
1129 it += strPosIncrement;
1130
1131 if ( it != text.end() )
1132 {
1133 a = *it;
1134 }
1135 else
1136 {
1137 a = 0;
1138 }
1139
1140 pos += strPosIncrement;
1141 }
1142
1143 if ( changed )
1144 variant = list;
1145
1146 return changed;
1147 }
1148
1149 bool wxPGProperty::SetValueFromString( const wxString& text, int argFlags )
1150 {
1151 wxVariant variant(m_value);
1152 bool res = StringToValue(variant, text, argFlags);
1153 if ( res )
1154 SetValue(variant);
1155 return res;
1156 }
1157
1158 bool wxPGProperty::SetValueFromInt( long number, int argFlags )
1159 {
1160 wxVariant variant(m_value);
1161 bool res = IntToValue(variant, number, argFlags);
1162 if ( res )
1163 SetValue(variant);
1164 return res;
1165 }
1166
1167 wxSize wxPGProperty::OnMeasureImage( int WXUNUSED(item) ) const
1168 {
1169 if ( m_valueBitmap )
1170 return wxSize(m_valueBitmap->GetWidth(),-1);
1171
1172 return wxSize(0,0);
1173 }
1174
1175 int wxPGProperty::GetImageOffset( int imageWidth ) const
1176 {
1177 int imageOffset = 0;
1178
1179 if ( imageWidth )
1180 {
1181 // Do not increment offset too much for wide images
1182 if ( imageWidth <= (wxPG_CUSTOM_IMAGE_WIDTH+5) )
1183 imageOffset = imageWidth + DEFAULT_IMAGE_OFFSET_INCREMENT;
1184 else
1185 imageOffset = imageWidth + 1;
1186 }
1187
1188 return imageOffset;
1189 }
1190
1191 wxPGCellRenderer* wxPGProperty::GetCellRenderer( int WXUNUSED(column) ) const
1192 {
1193 return wxPGGlobalVars->m_defaultRenderer;
1194 }
1195
1196 void wxPGProperty::OnCustomPaint( wxDC& dc,
1197 const wxRect& rect,
1198 wxPGPaintData& )
1199 {
1200 wxBitmap* bmp = m_valueBitmap;
1201
1202 wxCHECK_RET( bmp && bmp->Ok(), wxT("invalid bitmap") );
1203
1204 wxCHECK_RET( rect.x >= 0, wxT("unexpected measure call") );
1205
1206 dc.DrawBitmap(*bmp,rect.x,rect.y);
1207 }
1208
1209 const wxPGEditor* wxPGProperty::DoGetEditorClass() const
1210 {
1211 return wxPGEditor_TextCtrl;
1212 }
1213
1214 // Default extra property event handling - that is, none at all.
1215 bool wxPGProperty::OnEvent( wxPropertyGrid*, wxWindow*, wxEvent& )
1216 {
1217 return false;
1218 }
1219
1220
1221 void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
1222 {
1223 // If auto unspecified values are not wanted (via window or property style),
1224 // then get default value instead of wxNullVariant.
1225 if ( value.IsNull() && (flags & wxPG_SETVAL_BY_USER) &&
1226 !UsesAutoUnspecified() )
1227 {
1228 value = GetDefaultValue();
1229 }
1230
1231 if ( !value.IsNull() )
1232 {
1233 wxVariant tempListVariant;
1234
1235 SetCommonValue(-1);
1236 // List variants are reserved a special purpose
1237 // as intermediate containers for child values
1238 // of properties with children.
1239 if ( value.GetType() == wxPG_VARIANT_TYPE_LIST )
1240 {
1241 //
1242 // However, situation is different for composed string properties
1243 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE) )
1244 {
1245 tempListVariant = value;
1246 pList = &tempListVariant;
1247 }
1248
1249 wxVariant newValue;
1250 AdaptListToValue(value, &newValue);
1251 value = newValue;
1252 //wxLogDebug(wxT(">> %s.SetValue() adapted list value to type '%s'"),GetName().c_str(),value.GetType().c_str());
1253 }
1254
1255 if ( HasFlag( wxPG_PROP_AGGREGATE) )
1256 flags |= wxPG_SETVAL_AGGREGATED;
1257
1258 if ( pList && !pList->IsNull() )
1259 {
1260 wxASSERT( pList->GetType() == wxPG_VARIANT_TYPE_LIST );
1261 wxASSERT( GetChildCount() );
1262 wxASSERT( !IsCategory() );
1263
1264 wxVariantList& list = pList->GetList();
1265 wxVariantList::iterator node;
1266 unsigned int i = 0;
1267
1268 //wxLogDebug(wxT(">> %s.SetValue() pList parsing"),GetName().c_str());
1269
1270 // Children in list can be in any order, but we will give hint to
1271 // GetPropertyByNameWH(). This optimizes for full list parsing.
1272 for ( node = list.begin(); node != list.end(); ++node )
1273 {
1274 wxVariant& childValue = *((wxVariant*)*node);
1275 wxPGProperty* child = GetPropertyByNameWH(childValue.GetName(), i);
1276 if ( child )
1277 {
1278 //wxLogDebug(wxT("%i: child = %s, childValue.GetType()=%s"),i,child->GetBaseName().c_str(),childValue.GetType().c_str());
1279 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
1280 {
1281 if ( child->HasFlag(wxPG_PROP_AGGREGATE) && !(flags & wxPG_SETVAL_AGGREGATED) )
1282 {
1283 wxVariant listRefCopy = childValue;
1284 child->SetValue(childValue, &listRefCopy, flags|wxPG_SETVAL_FROM_PARENT);
1285 }
1286 else
1287 {
1288 wxVariant oldVal = child->GetValue();
1289 child->SetValue(oldVal, &childValue, flags|wxPG_SETVAL_FROM_PARENT);
1290 }
1291 }
1292 else if ( child->GetValue() != childValue )
1293 {
1294 // For aggregate properties, we will trust RefreshChildren()
1295 // to update child values.
1296 if ( !HasFlag(wxPG_PROP_AGGREGATE) )
1297 child->SetValue(childValue, NULL, flags|wxPG_SETVAL_FROM_PARENT);
1298 if ( flags & wxPG_SETVAL_BY_USER )
1299 child->SetFlag(wxPG_PROP_MODIFIED);
1300 }
1301 }
1302 i++;
1303 }
1304 }
1305
1306 if ( !value.IsNull() )
1307 {
1308 m_value = value;
1309 OnSetValue();
1310 }
1311
1312 if ( flags & wxPG_SETVAL_BY_USER )
1313 SetFlag(wxPG_PROP_MODIFIED);
1314
1315 if ( HasFlag(wxPG_PROP_AGGREGATE) )
1316 RefreshChildren();
1317 }
1318 else
1319 {
1320 if ( m_commonValue != -1 )
1321 {
1322 wxPropertyGrid* pg = GetGrid();
1323 if ( !pg || m_commonValue != pg->GetUnspecifiedCommonValue() )
1324 SetCommonValue(-1);
1325 }
1326
1327 m_value = value;
1328
1329 // Set children to unspecified, but only if aggregate or
1330 // value is <composed>
1331 if ( AreChildrenComponents() )
1332 {
1333 unsigned int i;
1334 for ( i=0; i<GetChildCount(); i++ )
1335 Item(i)->SetValue(value, NULL, flags|wxPG_SETVAL_FROM_PARENT);
1336 }
1337 }
1338
1339 if ( !(flags & wxPG_SETVAL_FROM_PARENT) )
1340 UpdateParentValues();
1341
1342 //
1343 // Update editor control
1344 //
1345
1346 // We need to check for these, otherwise GetGrid() may fail.
1347 if ( flags & wxPG_SETVAL_REFRESH_EDITOR )
1348 {
1349 RefreshEditor();
1350 wxPropertyGrid* pg = GetGridIfDisplayed();
1351 if ( pg )
1352 pg->DrawItemAndValueRelated(this);
1353 }
1354 }
1355
1356
1357 void wxPGProperty::SetValueInEvent( wxVariant value ) const
1358 {
1359 GetGrid()->ValueChangeInEvent(value);
1360 }
1361
1362 void wxPGProperty::SetFlagRecursively( FlagType flag, bool set )
1363 {
1364 if ( set )
1365 SetFlag(flag);
1366 else
1367 ClearFlag(flag);
1368
1369 unsigned int i;
1370 for ( i = 0; i < GetChildCount(); i++ )
1371 Item(i)->SetFlagRecursively(flag, set);
1372 }
1373
1374 void wxPGProperty::RefreshEditor()
1375 {
1376 if ( !m_parent )
1377 return;
1378
1379 wxPropertyGrid* pg = GetGrid();
1380 if ( pg && pg->GetSelectedProperty() == this )
1381 pg->RefreshEditor();
1382 }
1383
1384 wxVariant wxPGProperty::GetDefaultValue() const
1385 {
1386 wxVariant defVal = GetAttribute(wxS("DefaultValue"));
1387 if ( !defVal.IsNull() )
1388 return defVal;
1389
1390 wxVariant value = GetValue();
1391
1392 if ( !value.IsNull() )
1393 {
1394 wxString valueType(value.GetType());
1395
1396 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1397 return wxPGVariant_Zero;
1398 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1399 return wxPGVariant_EmptyString;
1400 if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1401 return wxPGVariant_False;
1402 if ( valueType == wxPG_VARIANT_TYPE_DOUBLE )
1403 return wxVariant(0.0);
1404 if ( valueType == wxPG_VARIANT_TYPE_ARRSTRING )
1405 return wxVariant(wxArrayString());
1406 if ( valueType == wxS("wxLongLong") )
1407 return WXVARIANT(wxLongLong(0));
1408 if ( valueType == wxS("wxULongLong") )
1409 return WXVARIANT(wxULongLong(0));
1410 if ( valueType == wxS("wxColour") )
1411 return WXVARIANT(*wxBLACK);
1412 #if wxUSE_DATETIME
1413 if ( valueType == wxPG_VARIANT_TYPE_DATETIME )
1414 return wxVariant(wxDateTime::Now());
1415 #endif
1416 if ( valueType == wxS("wxFont") )
1417 return WXVARIANT(*wxNORMAL_FONT);
1418 if ( valueType == wxS("wxPoint") )
1419 return WXVARIANT(wxPoint(0, 0));
1420 if ( valueType == wxS("wxSize") )
1421 return WXVARIANT(wxSize(0, 0));
1422 }
1423
1424 return wxVariant();
1425 }
1426
1427 void wxPGProperty::EnsureCells( unsigned int column )
1428 {
1429 if ( column >= m_cells.size() )
1430 {
1431 // Fill empty slots with default cells
1432 wxPropertyGrid* pg = GetGrid();
1433 wxPGCell defaultCell;
1434
1435 if ( !HasFlag(wxPG_PROP_CATEGORY) )
1436 defaultCell = pg->GetPropertyDefaultCell();
1437 else
1438 defaultCell = pg->GetCategoryDefaultCell();
1439
1440 // TODO: Replace with resize() call
1441 unsigned int cellCountMax = column+1;
1442
1443 for ( unsigned int i=m_cells.size(); i<cellCountMax; i++ )
1444 m_cells.push_back(defaultCell);
1445 }
1446 }
1447
1448 void wxPGProperty::SetCell( int column,
1449 const wxPGCell& cell )
1450 {
1451 EnsureCells(column);
1452
1453 m_cells[column] = cell;
1454 }
1455
1456 void wxPGProperty::AdaptiveSetCell( unsigned int firstCol,
1457 unsigned int lastCol,
1458 const wxPGCell& cell,
1459 const wxPGCell& srcData,
1460 wxPGCellData* unmodCellData,
1461 FlagType ignoreWithFlags,
1462 bool recursively )
1463 {
1464 //
1465 // Sets cell in memory optimizing fashion. That is, if
1466 // current cell data matches unmodCellData, we will
1467 // simply get reference to data from cell. Otherwise,
1468 // cell information from srcData is merged into current.
1469 //
1470
1471 if ( !(m_flags & ignoreWithFlags) && !IsRoot() )
1472 {
1473 EnsureCells(lastCol);
1474
1475 for ( unsigned int col=firstCol; col<=lastCol; col++ )
1476 {
1477 if ( m_cells[col].GetData() == unmodCellData )
1478 {
1479 // Data matches... use cell directly
1480 m_cells[col] = cell;
1481 }
1482 else
1483 {
1484 // Data did not match... merge valid information
1485 m_cells[col].MergeFrom(srcData);
1486 }
1487 }
1488 }
1489
1490 if ( recursively )
1491 {
1492 for ( unsigned int i=0; i<GetChildCount(); i++ )
1493 Item(i)->AdaptiveSetCell( firstCol,
1494 lastCol,
1495 cell,
1496 srcData,
1497 unmodCellData,
1498 ignoreWithFlags,
1499 recursively );
1500 }
1501 }
1502
1503 const wxPGCell& wxPGProperty::GetCell( unsigned int column ) const
1504 {
1505 if ( m_cells.size() > column )
1506 return m_cells[column];
1507
1508 wxPropertyGrid* pg = GetGrid();
1509
1510 if ( IsCategory() )
1511 return pg->GetCategoryDefaultCell();
1512
1513 return pg->GetPropertyDefaultCell();
1514 }
1515
1516 wxPGCell& wxPGProperty::GetCell( unsigned int column )
1517 {
1518 EnsureCells(column);
1519 return m_cells[column];
1520 }
1521
1522 void wxPGProperty::SetBackgroundColour( const wxColour& colour,
1523 bool recursively )
1524 {
1525 wxPGProperty* firstProp = this;
1526
1527 //
1528 // If category is tried to set recursively, skip it and only
1529 // affect the children.
1530 if ( recursively )
1531 {
1532 while ( firstProp->IsCategory() )
1533 {
1534 if ( !firstProp->GetChildCount() )
1535 return;
1536 firstProp = firstProp->Item(0);
1537 }
1538 }
1539
1540 wxPGCell& firstCell = firstProp->GetCell(0);
1541 wxPGCellData* firstCellData = firstCell.GetData();
1542
1543 wxPGCell newCell(firstCell);
1544 newCell.SetBgCol(colour);
1545 wxPGCell srcCell;
1546 srcCell.SetBgCol(colour);
1547
1548 AdaptiveSetCell( 0,
1549 GetParentState()->GetColumnCount()-1,
1550 newCell,
1551 srcCell,
1552 firstCellData,
1553 recursively ? wxPG_PROP_CATEGORY : 0,
1554 recursively );
1555 }
1556
1557 void wxPGProperty::SetTextColour( const wxColour& colour,
1558 bool recursively )
1559 {
1560 wxPGProperty* firstProp = this;
1561
1562 //
1563 // If category is tried to set recursively, skip it and only
1564 // affect the children.
1565 if ( recursively )
1566 {
1567 while ( firstProp->IsCategory() )
1568 {
1569 if ( !firstProp->GetChildCount() )
1570 return;
1571 firstProp = firstProp->Item(0);
1572 }
1573 }
1574
1575 wxPGCell& firstCell = firstProp->GetCell(0);
1576 wxPGCellData* firstCellData = firstCell.GetData();
1577
1578 wxPGCell newCell(firstCell);
1579 newCell.SetFgCol(colour);
1580 wxPGCell srcCell;
1581 srcCell.SetFgCol(colour);
1582
1583 AdaptiveSetCell( 0,
1584 GetParentState()->GetColumnCount()-1,
1585 newCell,
1586 srcCell,
1587 firstCellData,
1588 recursively ? wxPG_PROP_CATEGORY : 0,
1589 recursively );
1590 }
1591
1592 wxPGEditorDialogAdapter* wxPGProperty::GetEditorDialog() const
1593 {
1594 return NULL;
1595 }
1596
1597 bool wxPGProperty::DoSetAttribute( const wxString& WXUNUSED(name), wxVariant& WXUNUSED(value) )
1598 {
1599 return false;
1600 }
1601
1602 void wxPGProperty::SetAttribute( const wxString& name, wxVariant value )
1603 {
1604 if ( DoSetAttribute( name, value ) )
1605 {
1606 // Support working without grid, when possible
1607 if ( wxPGGlobalVars->HasExtraStyle( wxPG_EX_WRITEONLY_BUILTIN_ATTRIBUTES ) )
1608 return;
1609 }
1610
1611 m_attributes.Set( name, value );
1612 }
1613
1614 void wxPGProperty::SetAttributes( const wxPGAttributeStorage& attributes )
1615 {
1616 wxPGAttributeStorage::const_iterator it = attributes.StartIteration();
1617 wxVariant variant;
1618
1619 while ( attributes.GetNext(it, variant) )
1620 SetAttribute( variant.GetName(), variant );
1621 }
1622
1623 wxVariant wxPGProperty::DoGetAttribute( const wxString& WXUNUSED(name) ) const
1624 {
1625 return wxVariant();
1626 }
1627
1628
1629 wxVariant wxPGProperty::GetAttribute( const wxString& name ) const
1630 {
1631 return m_attributes.FindValue(name);
1632 }
1633
1634 wxString wxPGProperty::GetAttribute( const wxString& name, const wxString& defVal ) const
1635 {
1636 wxVariant variant = m_attributes.FindValue(name);
1637
1638 if ( !variant.IsNull() )
1639 return variant.GetString();
1640
1641 return defVal;
1642 }
1643
1644 long wxPGProperty::GetAttributeAsLong( const wxString& name, long defVal ) const
1645 {
1646 wxVariant variant = m_attributes.FindValue(name);
1647
1648 return wxPGVariantToInt(variant, defVal);
1649 }
1650
1651 double wxPGProperty::GetAttributeAsDouble( const wxString& name, double defVal ) const
1652 {
1653 double retVal;
1654 wxVariant variant = m_attributes.FindValue(name);
1655
1656 if ( wxPGVariantToDouble(variant, &retVal) )
1657 return retVal;
1658
1659 return defVal;
1660 }
1661
1662 wxVariant wxPGProperty::GetAttributesAsList() const
1663 {
1664 wxVariantList tempList;
1665 wxVariant v( tempList, wxString::Format(wxS("@%s@attr"),m_name.c_str()) );
1666
1667 wxPGAttributeStorage::const_iterator it = m_attributes.StartIteration();
1668 wxVariant variant;
1669
1670 while ( m_attributes.GetNext(it, variant) )
1671 v.Append(variant);
1672
1673 return v;
1674 }
1675
1676 // Slots of utility flags are NULL
1677 const unsigned int gs_propFlagToStringSize = 14;
1678
1679 static const wxChar* gs_propFlagToString[gs_propFlagToStringSize] = {
1680 NULL,
1681 wxT("DISABLED"),
1682 wxT("HIDDEN"),
1683 NULL,
1684 wxT("NOEDITOR"),
1685 wxT("COLLAPSED"),
1686 NULL,
1687 NULL,
1688 NULL,
1689 NULL,
1690 NULL,
1691 NULL,
1692 NULL,
1693 NULL
1694 };
1695
1696 wxString wxPGProperty::GetFlagsAsString( FlagType flagsMask ) const
1697 {
1698 wxString s;
1699 int relevantFlags = m_flags & flagsMask & wxPG_STRING_STORED_FLAGS;
1700 FlagType a = 1;
1701
1702 unsigned int i = 0;
1703 for ( i=0; i<gs_propFlagToStringSize; i++ )
1704 {
1705 if ( relevantFlags & a )
1706 {
1707 const wxChar* fs = gs_propFlagToString[i];
1708 wxASSERT(fs);
1709 if ( s.length() )
1710 s << wxS("|");
1711 s << fs;
1712 }
1713 a = a << 1;
1714 }
1715
1716 return s;
1717 }
1718
1719 void wxPGProperty::SetFlagsFromString( const wxString& str )
1720 {
1721 FlagType flags = 0;
1722
1723 WX_PG_TOKENIZER1_BEGIN(str, wxS('|'))
1724 unsigned int i;
1725 for ( i=0; i<gs_propFlagToStringSize; i++ )
1726 {
1727 const wxChar* fs = gs_propFlagToString[i];
1728 if ( fs && str == fs )
1729 {
1730 flags |= (1<<i);
1731 break;
1732 }
1733 }
1734 WX_PG_TOKENIZER1_END()
1735
1736 m_flags = (m_flags & ~wxPG_STRING_STORED_FLAGS) | flags;
1737 }
1738
1739 wxValidator* wxPGProperty::DoGetValidator() const
1740 {
1741 return NULL;
1742 }
1743
1744 int wxPGProperty::InsertChoice( const wxString& label, int index, int value )
1745 {
1746 wxPropertyGrid* pg = GetGrid();
1747 int sel = GetChoiceSelection();
1748
1749 int newSel = sel;
1750
1751 if ( index == wxNOT_FOUND )
1752 index = m_choices.GetCount();
1753
1754 if ( index <= sel )
1755 newSel++;
1756
1757 m_choices.Insert(label, index, value);
1758
1759 if ( sel != newSel )
1760 SetChoiceSelection(newSel);
1761
1762 if ( this == pg->GetSelection() )
1763 GetEditorClass()->InsertItem(pg->GetEditorControl(),label,index);
1764
1765 return index;
1766 }
1767
1768
1769 void wxPGProperty::DeleteChoice( int index )
1770 {
1771 wxPropertyGrid* pg = GetGrid();
1772
1773 int sel = GetChoiceSelection();
1774 int newSel = sel;
1775
1776 // Adjust current value
1777 if ( sel == index )
1778 {
1779 SetValueToUnspecified();
1780 newSel = 0;
1781 }
1782 else if ( index < sel )
1783 {
1784 newSel--;
1785 }
1786
1787 m_choices.RemoveAt(index);
1788
1789 if ( sel != newSel )
1790 SetChoiceSelection(newSel);
1791
1792 if ( this == pg->GetSelection() )
1793 GetEditorClass()->DeleteItem(pg->GetEditorControl(), index);
1794 }
1795
1796 int wxPGProperty::GetChoiceSelection() const
1797 {
1798 wxVariant value = GetValue();
1799 wxString valueType = value.GetType();
1800 int index = wxNOT_FOUND;
1801
1802 if ( IsValueUnspecified() || !m_choices.GetCount() )
1803 return wxNOT_FOUND;
1804
1805 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1806 {
1807 index = value.GetLong();
1808 }
1809 else if ( valueType == wxPG_VARIANT_TYPE_STRING )
1810 {
1811 index = m_choices.Index(value.GetString());
1812 }
1813 else if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1814 {
1815 index = value.GetBool()? 1 : 0;
1816 }
1817
1818 return index;
1819 }
1820
1821 void wxPGProperty::SetChoiceSelection( int newValue )
1822 {
1823 // Changes value of a property with choices, but only
1824 // works if the value type is long or string.
1825 wxString valueType = GetValue().GetType();
1826
1827 wxCHECK_RET( m_choices.IsOk(), wxT("invalid choiceinfo") );
1828
1829 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1830 {
1831 SetValue( m_choices.GetLabel(newValue) );
1832 }
1833 else // if ( valueType == wxPG_VARIANT_TYPE_LONG )
1834 {
1835 SetValue( (long) newValue );
1836 }
1837 }
1838
1839 bool wxPGProperty::SetChoices( wxPGChoices& choices )
1840 {
1841 m_choices.Assign(choices);
1842
1843 {
1844 // This may be needed to trigger some initialization
1845 // (but don't do it if property is somewhat uninitialized)
1846 wxVariant defVal = GetDefaultValue();
1847 if ( defVal.IsNull() )
1848 return false;
1849
1850 SetValue(defVal);
1851 }
1852
1853 return true;
1854 }
1855
1856
1857 const wxPGEditor* wxPGProperty::GetEditorClass() const
1858 {
1859 const wxPGEditor* editor;
1860
1861 if ( !m_customEditor )
1862 {
1863 editor = DoGetEditorClass();
1864 }
1865 else
1866 editor = m_customEditor;
1867
1868 //
1869 // Maybe override editor if common value specified
1870 if ( GetDisplayedCommonValueCount() )
1871 {
1872 // TextCtrlAndButton -> ComboBoxAndButton
1873 if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlAndButtonEditor)) )
1874 editor = wxPGEditor_ChoiceAndButton;
1875
1876 // TextCtrl -> ComboBox
1877 else if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlEditor)) )
1878 editor = wxPGEditor_ComboBox;
1879 }
1880
1881 return editor;
1882 }
1883
1884 bool wxPGProperty::HasVisibleChildren() const
1885 {
1886 unsigned int i;
1887
1888 for ( i=0; i<GetChildCount(); i++ )
1889 {
1890 wxPGProperty* child = Item(i);
1891
1892 if ( !child->HasFlag(wxPG_PROP_HIDDEN) )
1893 return true;
1894 }
1895
1896 return false;
1897 }
1898
1899 bool wxPGProperty::RecreateEditor()
1900 {
1901 wxPropertyGrid* pg = GetGrid();
1902 wxASSERT(pg);
1903
1904 wxPGProperty* selected = pg->GetSelection();
1905 if ( this == selected )
1906 {
1907 pg->DoSelectProperty(this, wxPG_SEL_FORCE);
1908 return true;
1909 }
1910 return false;
1911 }
1912
1913
1914 void wxPGProperty::SetValueImage( wxBitmap& bmp )
1915 {
1916 delete m_valueBitmap;
1917
1918 if ( &bmp && bmp.Ok() )
1919 {
1920 // Resize the image
1921 wxSize maxSz = GetGrid()->GetImageSize();
1922 wxSize imSz(bmp.GetWidth(),bmp.GetHeight());
1923
1924 if ( imSz.y != maxSz.y )
1925 {
1926 // Create a memory DC
1927 wxBitmap* bmpNew = new wxBitmap(maxSz.x,maxSz.y,bmp.GetDepth());
1928
1929 wxMemoryDC dc;
1930 dc.SelectObject(*bmpNew);
1931
1932 // Scale
1933 // FIXME: This is ugly - use image or wait for scaling patch.
1934 double scaleY = (double)maxSz.y / (double)imSz.y;
1935
1936 dc.SetUserScale(scaleY, scaleY);
1937
1938 dc.DrawBitmap(bmp, 0, 0);
1939
1940 m_valueBitmap = bmpNew;
1941 }
1942 else
1943 {
1944 m_valueBitmap = new wxBitmap(bmp);
1945 }
1946
1947 m_flags |= wxPG_PROP_CUSTOMIMAGE;
1948 }
1949 else
1950 {
1951 m_valueBitmap = NULL;
1952 m_flags &= ~(wxPG_PROP_CUSTOMIMAGE);
1953 }
1954 }
1955
1956
1957 wxPGProperty* wxPGProperty::GetMainParent() const
1958 {
1959 const wxPGProperty* curChild = this;
1960 const wxPGProperty* curParent = m_parent;
1961
1962 while ( curParent && !curParent->IsCategory() )
1963 {
1964 curChild = curParent;
1965 curParent = curParent->m_parent;
1966 }
1967
1968 return (wxPGProperty*) curChild;
1969 }
1970
1971
1972 const wxPGProperty* wxPGProperty::GetLastVisibleSubItem() const
1973 {
1974 //
1975 // Returns last visible sub-item, recursively.
1976 if ( !IsExpanded() || !GetChildCount() )
1977 return this;
1978
1979 return Last()->GetLastVisibleSubItem();
1980 }
1981
1982
1983 bool wxPGProperty::IsVisible() const
1984 {
1985 const wxPGProperty* parent;
1986
1987 if ( HasFlag(wxPG_PROP_HIDDEN) )
1988 return false;
1989
1990 for ( parent = GetParent(); parent != NULL; parent = parent->GetParent() )
1991 {
1992 if ( !parent->IsExpanded() || parent->HasFlag(wxPG_PROP_HIDDEN) )
1993 return false;
1994 }
1995
1996 return true;
1997 }
1998
1999 wxPropertyGrid* wxPGProperty::GetGridIfDisplayed() const
2000 {
2001 wxPropertyGridPageState* state = GetParentState();
2002 if ( !state )
2003 return NULL;
2004 wxPropertyGrid* propGrid = state->GetGrid();
2005 if ( state == propGrid->GetState() )
2006 return propGrid;
2007 return NULL;
2008 }
2009
2010
2011 int wxPGProperty::GetY2( int lh ) const
2012 {
2013 const wxPGProperty* parent;
2014 const wxPGProperty* child = this;
2015
2016 int y = 0;
2017
2018 for ( parent = GetParent(); parent != NULL; parent = child->GetParent() )
2019 {
2020 if ( !parent->IsExpanded() )
2021 return -1;
2022 y += parent->GetChildrenHeight(lh, child->GetIndexInParent());
2023 y += lh;
2024 child = parent;
2025 }
2026
2027 y -= lh; // need to reduce one level
2028
2029 return y;
2030 }
2031
2032
2033 int wxPGProperty::GetY() const
2034 {
2035 return GetY2(GetGrid()->GetRowHeight());
2036 }
2037
2038 // This is used by Insert etc.
2039 void wxPGProperty::DoAddChild( wxPGProperty* prop, int index,
2040 bool correct_mode )
2041 {
2042 if ( index < 0 || (size_t)index >= m_children.size() )
2043 {
2044 if ( correct_mode ) prop->m_arrIndex = m_children.size();
2045 m_children.push_back( prop );
2046 }
2047 else
2048 {
2049 m_children.insert( m_children.begin()+index, prop);
2050 if ( correct_mode ) FixIndicesOfChildren( index );
2051 }
2052
2053 prop->m_parent = this;
2054 }
2055
2056 void wxPGProperty::DoPreAddChild( int index, wxPGProperty* prop )
2057 {
2058 wxASSERT_MSG( prop->GetBaseName().length(),
2059 "Property's children must have unique, non-empty "
2060 "names within their scope" );
2061
2062 prop->m_arrIndex = index;
2063 m_children.insert( m_children.begin()+index,
2064 prop );
2065
2066 int custImgHeight = prop->OnMeasureImage().y;
2067 if ( custImgHeight < 0 /*|| custImgHeight > 1*/ )
2068 prop->m_flags |= wxPG_PROP_CUSTOMIMAGE;
2069
2070 prop->m_parent = this;
2071 }
2072
2073 void wxPGProperty::AddPrivateChild( wxPGProperty* prop )
2074 {
2075 if ( !(m_flags & wxPG_PROP_PARENTAL_FLAGS) )
2076 SetParentalType(wxPG_PROP_AGGREGATE);
2077
2078 wxASSERT_MSG( (m_flags & wxPG_PROP_PARENTAL_FLAGS) ==
2079 wxPG_PROP_AGGREGATE,
2080 "Do not mix up AddPrivateChild() calls with other "
2081 "property adders." );
2082
2083 DoPreAddChild( m_children.size(), prop );
2084 }
2085
2086 #if wxPG_COMPATIBILITY_1_4
2087 void wxPGProperty::AddChild( wxPGProperty* prop )
2088 {
2089 AddPrivateChild(prop);
2090 }
2091 #endif
2092
2093 wxPGProperty* wxPGProperty::InsertChild( int index,
2094 wxPGProperty* childProperty )
2095 {
2096 if ( index < 0 )
2097 index = m_children.size();
2098
2099 if ( m_parentState )
2100 {
2101 m_parentState->DoInsert(this, index, childProperty);
2102 }
2103 else
2104 {
2105 if ( !(m_flags & wxPG_PROP_PARENTAL_FLAGS) )
2106 SetParentalType(wxPG_PROP_MISC_PARENT);
2107
2108 wxASSERT_MSG( (m_flags & wxPG_PROP_PARENTAL_FLAGS) ==
2109 wxPG_PROP_MISC_PARENT,
2110 "Do not mix up AddPrivateChild() calls with other "
2111 "property adders." );
2112
2113 DoPreAddChild( index, childProperty );
2114 }
2115
2116 return childProperty;
2117 }
2118
2119 void wxPGProperty::RemoveChild( wxPGProperty* p )
2120 {
2121 wxArrayPGProperty::iterator it;
2122 wxArrayPGProperty& children = m_children;
2123
2124 for ( it=children.begin(); it != children.end(); it++ )
2125 {
2126 if ( *it == p )
2127 {
2128 m_children.erase(it);
2129 break;
2130 }
2131 }
2132 }
2133
2134 void wxPGProperty::AdaptListToValue( wxVariant& list, wxVariant* value ) const
2135 {
2136 wxASSERT( GetChildCount() );
2137 wxASSERT( !IsCategory() );
2138
2139 *value = GetValue();
2140
2141 if ( !list.GetCount() )
2142 return;
2143
2144 wxASSERT( GetChildCount() >= (unsigned int)list.GetCount() );
2145
2146 bool allChildrenSpecified;
2147
2148 // Don't fully update aggregate properties unless all children have
2149 // specified value
2150 if ( HasFlag(wxPG_PROP_AGGREGATE) )
2151 allChildrenSpecified = AreAllChildrenSpecified(&list);
2152 else
2153 allChildrenSpecified = true;
2154
2155 wxVariant childValue = list[0];
2156 unsigned int i;
2157 unsigned int n = 0;
2158
2159 //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
2160
2161 for ( i=0; i<GetChildCount(); i++ )
2162 {
2163 const wxPGProperty* child = Item(i);
2164
2165 if ( childValue.GetName() == child->GetBaseName() )
2166 {
2167 //wxLogDebug(wxT(" %s(n=%i), %s"),childValue.GetName().c_str(),n,childValue.GetType().c_str());
2168
2169 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
2170 {
2171 wxVariant cv2(child->GetValue());
2172 child->AdaptListToValue(childValue, &cv2);
2173 childValue = cv2;
2174 }
2175
2176 if ( allChildrenSpecified )
2177 ChildChanged(*value, i, childValue);
2178 n++;
2179 if ( n == (unsigned int)list.GetCount() )
2180 break;
2181 childValue = list[n];
2182 }
2183 }
2184 }
2185
2186
2187 void wxPGProperty::FixIndicesOfChildren( unsigned int starthere )
2188 {
2189 size_t i;
2190 for ( i=starthere;i<GetChildCount();i++)
2191 Item(i)->m_arrIndex = i;
2192 }
2193
2194
2195 // Returns (direct) child property with given name (or NULL if not found)
2196 wxPGProperty* wxPGProperty::GetPropertyByName( const wxString& name ) const
2197 {
2198 size_t i;
2199
2200 for ( i=0; i<GetChildCount(); i++ )
2201 {
2202 wxPGProperty* p = Item(i);
2203 if ( p->m_name == name )
2204 return p;
2205 }
2206
2207 // Does it have point, then?
2208 int pos = name.Find(wxS('.'));
2209 if ( pos <= 0 )
2210 return NULL;
2211
2212 wxPGProperty* p = GetPropertyByName(name. substr(0,pos));
2213
2214 if ( !p || !p->GetChildCount() )
2215 return NULL;
2216
2217 return p->GetPropertyByName(name.substr(pos+1,name.length()-pos-1));
2218 }
2219
2220 wxPGProperty* wxPGProperty::GetPropertyByNameWH( const wxString& name, unsigned int hintIndex ) const
2221 {
2222 unsigned int i = hintIndex;
2223
2224 if ( i >= GetChildCount() )
2225 i = 0;
2226
2227 unsigned int lastIndex = i - 1;
2228
2229 if ( lastIndex >= GetChildCount() )
2230 lastIndex = GetChildCount() - 1;
2231
2232 for (;;)
2233 {
2234 wxPGProperty* p = Item(i);
2235 if ( p->m_name == name )
2236 return p;
2237
2238 if ( i == lastIndex )
2239 break;
2240
2241 i++;
2242 if ( i == GetChildCount() )
2243 i = 0;
2244 };
2245
2246 return NULL;
2247 }
2248
2249 int wxPGProperty::GetChildrenHeight( int lh, int iMax_ ) const
2250 {
2251 // Returns height of children, recursively, and
2252 // by taking expanded/collapsed status into account.
2253 //
2254 // iMax is used when finding property y-positions.
2255 //
2256 unsigned int i = 0;
2257 int h = 0;
2258
2259 if ( iMax_ == -1 )
2260 iMax_ = GetChildCount();
2261
2262 unsigned int iMax = iMax_;
2263
2264 wxASSERT( iMax <= GetChildCount() );
2265
2266 if ( !IsExpanded() && GetParent() )
2267 return 0;
2268
2269 while ( i < iMax )
2270 {
2271 wxPGProperty* pwc = (wxPGProperty*) Item(i);
2272
2273 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
2274 {
2275 if ( !pwc->IsExpanded() ||
2276 pwc->GetChildCount() == 0 )
2277 h += lh;
2278 else
2279 h += pwc->GetChildrenHeight(lh) + lh;
2280 }
2281
2282 i++;
2283 }
2284
2285 return h;
2286 }
2287
2288 wxPGProperty* wxPGProperty::GetItemAtY( unsigned int y, unsigned int lh, unsigned int* nextItemY ) const
2289 {
2290 wxASSERT( nextItemY );
2291
2292 // Linear search at the moment
2293 //
2294 // nextItemY = y of next visible property, final value will be written back.
2295 wxPGProperty* result = NULL;
2296 wxPGProperty* current = NULL;
2297 unsigned int iy = *nextItemY;
2298 unsigned int i = 0;
2299 unsigned int iMax = GetChildCount();
2300
2301 while ( i < iMax )
2302 {
2303 wxPGProperty* pwc = Item(i);
2304
2305 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
2306 {
2307 // Found?
2308 if ( y < iy )
2309 {
2310 result = current;
2311 break;
2312 }
2313
2314 iy += lh;
2315
2316 if ( pwc->IsExpanded() &&
2317 pwc->GetChildCount() > 0 )
2318 {
2319 result = (wxPGProperty*) pwc->GetItemAtY( y, lh, &iy );
2320 if ( result )
2321 break;
2322 }
2323
2324 current = pwc;
2325 }
2326
2327 i++;
2328 }
2329
2330 // Found?
2331 if ( !result && y < iy )
2332 result = current;
2333
2334 *nextItemY = iy;
2335
2336 /*
2337 if ( current )
2338 wxLogDebug(wxT("%s::GetItemAtY(%i) -> %s"),this->GetLabel().c_str(),y,current->GetLabel().c_str());
2339 else
2340 wxLogDebug(wxT("%s::GetItemAtY(%i) -> NULL"),this->GetLabel().c_str(),y);
2341 */
2342
2343 return (wxPGProperty*) result;
2344 }
2345
2346 void wxPGProperty::Empty()
2347 {
2348 size_t i;
2349 if ( !HasFlag(wxPG_PROP_CHILDREN_ARE_COPIES) )
2350 {
2351 for ( i=0; i<GetChildCount(); i++ )
2352 {
2353 delete m_children[i];
2354 }
2355 }
2356
2357 m_children.clear();
2358 }
2359
2360 void wxPGProperty::DeleteChildren()
2361 {
2362 wxPropertyGridPageState* state = m_parentState;
2363
2364 while ( GetChildCount() )
2365 {
2366 wxPGProperty* child = Item(GetChildCount()-1);
2367 state->DoDelete(child, true);
2368 }
2369 }
2370
2371 void wxPGProperty::ChildChanged( wxVariant& WXUNUSED(thisValue),
2372 int WXUNUSED(childIndex),
2373 wxVariant& WXUNUSED(childValue) ) const
2374 {
2375 }
2376
2377 bool wxPGProperty::AreAllChildrenSpecified( wxVariant* pendingList ) const
2378 {
2379 unsigned int i;
2380
2381 const wxVariantList* pList = NULL;
2382 wxVariantList::const_iterator node;
2383
2384 if ( pendingList )
2385 {
2386 pList = &pendingList->GetList();
2387 node = pList->begin();
2388 }
2389
2390 for ( i=0; i<GetChildCount(); i++ )
2391 {
2392 wxPGProperty* child = Item(i);
2393 const wxVariant* listValue = NULL;
2394 wxVariant value;
2395
2396 if ( pendingList )
2397 {
2398 const wxString& childName = child->GetBaseName();
2399
2400 for ( ; node != pList->end(); ++node )
2401 {
2402 const wxVariant& item = *((const wxVariant*)*node);
2403 if ( item.GetName() == childName )
2404 {
2405 listValue = &item;
2406 value = item;
2407 break;
2408 }
2409 }
2410 }
2411
2412 if ( !listValue )
2413 value = child->GetValue();
2414
2415 if ( value.IsNull() )
2416 return false;
2417
2418 // Check recursively
2419 if ( child->GetChildCount() )
2420 {
2421 const wxVariant* childList = NULL;
2422
2423 if ( listValue && listValue->GetType() == wxPG_VARIANT_TYPE_LIST )
2424 childList = listValue;
2425
2426 if ( !child->AreAllChildrenSpecified((wxVariant*)childList) )
2427 return false;
2428 }
2429 }
2430
2431 return true;
2432 }
2433
2434 wxPGProperty* wxPGProperty::UpdateParentValues()
2435 {
2436 wxPGProperty* parent = m_parent;
2437 if ( parent && parent->HasFlag(wxPG_PROP_COMPOSED_VALUE) &&
2438 !parent->IsCategory() && !parent->IsRoot() )
2439 {
2440 wxString s;
2441 parent->DoGenerateComposedValue(s);
2442 parent->m_value = s;
2443 return parent->UpdateParentValues();
2444 }
2445 return this;
2446 }
2447
2448 bool wxPGProperty::IsTextEditable() const
2449 {
2450 if ( HasFlag(wxPG_PROP_READONLY) )
2451 return false;
2452
2453 if ( HasFlag(wxPG_PROP_NOEDITOR) &&
2454 (GetChildCount() ||
2455 wxString(GetEditorClass()->GetClassInfo()->GetClassName()).EndsWith(wxS("Button")))
2456 )
2457 return false;
2458
2459 return true;
2460 }
2461
2462 // Call after fixed sub-properties added/removed after creation.
2463 // if oldSelInd >= 0 and < new max items, then selection is
2464 // moved to it. Note: oldSelInd -2 indicates that this property
2465 // should be selected.
2466 void wxPGProperty::SubPropsChanged( int oldSelInd )
2467 {
2468 wxPropertyGridPageState* state = GetParentState();
2469 wxPropertyGrid* grid = state->GetGrid();
2470
2471 //
2472 // Re-repare children (recursively)
2473 for ( unsigned int i=0; i<GetChildCount(); i++ )
2474 {
2475 wxPGProperty* child = Item(i);
2476 child->InitAfterAdded(state, grid);
2477 }
2478
2479 wxPGProperty* sel = NULL;
2480 if ( oldSelInd >= (int)m_children.size() )
2481 oldSelInd = (int)m_children.size() - 1;
2482
2483 if ( oldSelInd >= 0 )
2484 sel = m_children[oldSelInd];
2485 else if ( oldSelInd == -2 )
2486 sel = this;
2487
2488 if ( sel )
2489 state->DoSelectProperty(sel);
2490
2491 if ( state == grid->GetState() )
2492 {
2493 grid->GetPanel()->Refresh();
2494 }
2495 }
2496
2497 // -----------------------------------------------------------------------
2498 // wxPGRootProperty
2499 // -----------------------------------------------------------------------
2500
2501 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPGRootProperty,none,TextCtrl)
2502 IMPLEMENT_DYNAMIC_CLASS(wxPGRootProperty, wxPGProperty)
2503
2504
2505 wxPGRootProperty::wxPGRootProperty( const wxString& name )
2506 : wxPGProperty()
2507 {
2508 #ifdef __WXDEBUG__
2509 m_name = name;
2510 m_label = m_name;
2511 #else
2512 wxUnusedVar(name);
2513 #endif
2514 SetParentalType(0);
2515 m_depth = 0;
2516 }
2517
2518
2519 wxPGRootProperty::~wxPGRootProperty()
2520 {
2521 }
2522
2523
2524 // -----------------------------------------------------------------------
2525 // wxPropertyCategory
2526 // -----------------------------------------------------------------------
2527
2528 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPropertyCategory,none,TextCtrl)
2529 IMPLEMENT_DYNAMIC_CLASS(wxPropertyCategory, wxPGProperty)
2530
2531 void wxPropertyCategory::Init()
2532 {
2533 // don't set colour - prepareadditem method should do this
2534 SetParentalType(wxPG_PROP_CATEGORY);
2535 m_capFgColIndex = 1;
2536 m_textExtent = -1;
2537 }
2538
2539 wxPropertyCategory::wxPropertyCategory()
2540 : wxPGProperty()
2541 {
2542 Init();
2543 }
2544
2545
2546 wxPropertyCategory::wxPropertyCategory( const wxString &label, const wxString& name )
2547 : wxPGProperty(label,name)
2548 {
2549 Init();
2550 }
2551
2552
2553 wxPropertyCategory::~wxPropertyCategory()
2554 {
2555 }
2556
2557
2558 wxString wxPropertyCategory::ValueToString( wxVariant& WXUNUSED(value),
2559 int WXUNUSED(argFlags) ) const
2560 {
2561 return wxEmptyString;
2562 }
2563
2564 int wxPropertyCategory::GetTextExtent( const wxWindow* wnd, const wxFont& font ) const
2565 {
2566 if ( m_textExtent > 0 )
2567 return m_textExtent;
2568 int x = 0, y = 0;
2569 ((wxWindow*)wnd)->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2570 return x;
2571 }
2572
2573 void wxPropertyCategory::CalculateTextExtent( wxWindow* wnd, const wxFont& font )
2574 {
2575 int x = 0, y = 0;
2576 wnd->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2577 m_textExtent = x;
2578 }
2579
2580 // -----------------------------------------------------------------------
2581 // wxPGChoices
2582 // -----------------------------------------------------------------------
2583
2584 wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, int value )
2585 {
2586 AllocExclusive();
2587
2588 wxPGChoiceEntry entry(label, value);
2589 return m_data->Insert( -1, entry );
2590 }
2591
2592 // -----------------------------------------------------------------------
2593
2594 wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, const wxBitmap& bitmap, int value )
2595 {
2596 AllocExclusive();
2597
2598 wxPGChoiceEntry entry(label, value);
2599 entry.SetBitmap(bitmap);
2600 return m_data->Insert( -1, entry );
2601 }
2602
2603 // -----------------------------------------------------------------------
2604
2605 wxPGChoiceEntry& wxPGChoices::Insert( const wxPGChoiceEntry& entry, int index )
2606 {
2607 AllocExclusive();
2608
2609 return m_data->Insert( index, entry );
2610 }
2611
2612 // -----------------------------------------------------------------------
2613
2614 wxPGChoiceEntry& wxPGChoices::Insert( const wxString& label, int index, int value )
2615 {
2616 AllocExclusive();
2617
2618 wxPGChoiceEntry entry(label, value);
2619 return m_data->Insert( index, entry );
2620 }
2621
2622 // -----------------------------------------------------------------------
2623
2624 wxPGChoiceEntry& wxPGChoices::AddAsSorted( const wxString& label, int value )
2625 {
2626 AllocExclusive();
2627
2628 size_t index = 0;
2629
2630 while ( index < GetCount() )
2631 {
2632 int cmpRes = GetLabel(index).Cmp(label);
2633 if ( cmpRes > 0 )
2634 break;
2635 index++;
2636 }
2637
2638 wxPGChoiceEntry entry(label, value);
2639 return m_data->Insert( index, entry );
2640 }
2641
2642 // -----------------------------------------------------------------------
2643
2644 void wxPGChoices::Add( const wxChar** labels, const ValArrItem* values )
2645 {
2646 AllocExclusive();
2647
2648 unsigned int itemcount = 0;
2649 const wxChar** p = &labels[0];
2650 while ( *p ) { p++; itemcount++; }
2651
2652 unsigned int i;
2653 for ( i = 0; i < itemcount; i++ )
2654 {
2655 int value = i;
2656 if ( values )
2657 value = values[i];
2658 wxPGChoiceEntry entry(labels[i], value);
2659 m_data->Insert( i, entry );
2660 }
2661 }
2662
2663 // -----------------------------------------------------------------------
2664
2665 void wxPGChoices::Add( const wxArrayString& arr, const wxArrayInt& arrint )
2666 {
2667 AllocExclusive();
2668
2669 unsigned int i;
2670 unsigned int itemcount = arr.size();
2671
2672 for ( i = 0; i < itemcount; i++ )
2673 {
2674 int value = i;
2675 if ( &arrint && arrint.size() )
2676 value = arrint[i];
2677 wxPGChoiceEntry entry(arr[i], value);
2678 m_data->Insert( i, entry );
2679 }
2680 }
2681
2682 // -----------------------------------------------------------------------
2683
2684 void wxPGChoices::RemoveAt(size_t nIndex, size_t count)
2685 {
2686 AllocExclusive();
2687
2688 wxASSERT( m_data->m_refCount != 0xFFFFFFF );
2689 m_data->m_items.erase(m_data->m_items.begin()+nIndex,
2690 m_data->m_items.begin()+nIndex+count);
2691 }
2692
2693 // -----------------------------------------------------------------------
2694
2695 void wxPGChoices::Clear()
2696 {
2697 if ( m_data != wxPGChoicesEmptyData )
2698 {
2699 AllocExclusive();
2700 m_data->Clear();
2701 }
2702 }
2703
2704 // -----------------------------------------------------------------------
2705
2706 int wxPGChoices::Index( const wxString& str ) const
2707 {
2708 if ( IsOk() )
2709 {
2710 unsigned int i;
2711 for ( i=0; i< m_data->GetCount(); i++ )
2712 {
2713 const wxPGChoiceEntry& entry = m_data->Item(i);
2714 if ( entry.HasText() && entry.GetText() == str )
2715 return i;
2716 }
2717 }
2718 return -1;
2719 }
2720
2721 // -----------------------------------------------------------------------
2722
2723 int wxPGChoices::Index( int val ) const
2724 {
2725 if ( IsOk() )
2726 {
2727 unsigned int i;
2728 for ( i=0; i< m_data->GetCount(); i++ )
2729 {
2730 const wxPGChoiceEntry& entry = m_data->Item(i);
2731 if ( entry.GetValue() == val )
2732 return i;
2733 }
2734 }
2735 return -1;
2736 }
2737
2738 // -----------------------------------------------------------------------
2739
2740 wxArrayString wxPGChoices::GetLabels() const
2741 {
2742 wxArrayString arr;
2743 unsigned int i;
2744
2745 if ( this && IsOk() )
2746 for ( i=0; i<GetCount(); i++ )
2747 arr.push_back(GetLabel(i));
2748
2749 return arr;
2750 }
2751
2752 // -----------------------------------------------------------------------
2753
2754 wxArrayInt wxPGChoices::GetValuesForStrings( const wxArrayString& strings ) const
2755 {
2756 wxArrayInt arr;
2757
2758 if ( IsOk() )
2759 {
2760 unsigned int i;
2761 for ( i=0; i< strings.size(); i++ )
2762 {
2763 int index = Index(strings[i]);
2764 if ( index >= 0 )
2765 arr.Add(GetValue(index));
2766 else
2767 arr.Add(wxPG_INVALID_VALUE);
2768 }
2769 }
2770
2771 return arr;
2772 }
2773
2774 // -----------------------------------------------------------------------
2775
2776 wxArrayInt wxPGChoices::GetIndicesForStrings( const wxArrayString& strings,
2777 wxArrayString* unmatched ) const
2778 {
2779 wxArrayInt arr;
2780
2781 if ( IsOk() )
2782 {
2783 unsigned int i;
2784 for ( i=0; i< strings.size(); i++ )
2785 {
2786 const wxString& str = strings[i];
2787 int index = Index(str);
2788 if ( index >= 0 )
2789 arr.Add(index);
2790 else if ( unmatched )
2791 unmatched->Add(str);
2792 }
2793 }
2794
2795 return arr;
2796 }
2797
2798 // -----------------------------------------------------------------------
2799
2800 void wxPGChoices::AllocExclusive()
2801 {
2802 EnsureData();
2803
2804 if ( m_data->m_refCount != 1 )
2805 {
2806 wxPGChoicesData* data = new wxPGChoicesData();
2807 data->CopyDataFrom(m_data);
2808 Free();
2809 m_data = data;
2810 }
2811 }
2812
2813 // -----------------------------------------------------------------------
2814
2815 void wxPGChoices::AssignData( wxPGChoicesData* data )
2816 {
2817 Free();
2818
2819 if ( data != wxPGChoicesEmptyData )
2820 {
2821 m_data = data;
2822 data->m_refCount++;
2823 }
2824 }
2825
2826 // -----------------------------------------------------------------------
2827
2828 void wxPGChoices::Init()
2829 {
2830 m_data = wxPGChoicesEmptyData;
2831 }
2832
2833 // -----------------------------------------------------------------------
2834
2835 void wxPGChoices::Free()
2836 {
2837 if ( m_data != wxPGChoicesEmptyData )
2838 {
2839 m_data->DecRef();
2840 m_data = wxPGChoicesEmptyData;
2841 }
2842 }
2843
2844 // -----------------------------------------------------------------------
2845 // wxPGAttributeStorage
2846 // -----------------------------------------------------------------------
2847
2848 wxPGAttributeStorage::wxPGAttributeStorage()
2849 {
2850 }
2851
2852 wxPGAttributeStorage::~wxPGAttributeStorage()
2853 {
2854 wxPGHashMapS2P::iterator it;
2855
2856 for ( it = m_map.begin(); it != m_map.end(); ++it )
2857 {
2858 wxVariantData* data = (wxVariantData*) it->second;
2859 data->DecRef();
2860 }
2861 }
2862
2863 void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value )
2864 {
2865 wxVariantData* data = value.GetData();
2866
2867 // Free old, if any
2868 wxPGHashMapS2P::iterator it = m_map.find(name);
2869 if ( it != m_map.end() )
2870 {
2871 ((wxVariantData*)it->second)->DecRef();
2872
2873 if ( !data )
2874 {
2875 // If Null variant, just remove from set
2876 m_map.erase(it);
2877 return;
2878 }
2879 }
2880
2881 if ( data )
2882 {
2883 data->IncRef();
2884
2885 m_map[name] = data;
2886 }
2887 }
2888
2889 #endif // wxUSE_PROPGRID