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