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