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