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