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