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