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