]> git.saurik.com Git - wxWidgets.git/blame - src/propgrid/property.cpp
no need to test that the pointer is non-NULL before deleting it (closes #10070)
[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
46
47// -----------------------------------------------------------------------
48
49static void wxPGDrawFocusRect( wxDC& dc, const wxRect& rect )
50{
51#if defined(__WXMSW__) && !defined(__WXWINCE__)
52 // FIXME: Use DrawFocusRect code above (currently it draws solid line
53 // for caption focus but works ok for other stuff).
54 // Also, it seems that this code may not work in future wx versions.
55 dc.SetLogicalFunction(wxINVERT);
56
57 wxPen pen(*wxBLACK,1,wxDOT);
58 pen.SetCap(wxCAP_BUTT);
59 dc.SetPen(pen);
60 dc.SetBrush(*wxTRANSPARENT_BRUSH);
61
62 dc.DrawRectangle(rect);
63
64 dc.SetLogicalFunction(wxCOPY);
65#else
66 dc.SetLogicalFunction(wxINVERT);
67
68 dc.SetPen(wxPen(*wxBLACK,1,wxDOT));
69 dc.SetBrush(*wxTRANSPARENT_BRUSH);
70
71 dc.DrawRectangle(rect);
72
73 dc.SetLogicalFunction(wxCOPY);
74#endif
75}
76
77// -----------------------------------------------------------------------
78// wxPGCellRenderer
79// -----------------------------------------------------------------------
80
81wxSize wxPGCellRenderer::GetImageSize( const wxPGProperty* WXUNUSED(property),
82 int WXUNUSED(column),
83 int WXUNUSED(item) ) const
84{
85 return wxSize(0, 0);
86}
87
88void wxPGCellRenderer::DrawText( wxDC& dc, const wxRect& rect,
89 int xOffset, const wxString& text ) const
90{
91 if ( xOffset )
92 xOffset += wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
93 dc.DrawText( text,
94 rect.x+xOffset+wxPG_XBEFORETEXT,
95 rect.y+((rect.height-dc.GetCharHeight())/2) );
96}
97
98void wxPGCellRenderer::DrawEditorValue( wxDC& dc, const wxRect& rect,
99 int xOffset, const wxString& text,
100 wxPGProperty* property,
101 const wxPGEditor* editor ) const
102{
103 if ( xOffset )
104 xOffset += wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
105
106 int yOffset = ((rect.height-dc.GetCharHeight())/2);
107
108 if ( editor )
109 {
110 wxRect rect2(rect);
111 rect2.x += xOffset;
112 rect2.y += yOffset;
113 rect2.height -= yOffset;
114 editor->DrawValue( dc, rect2, property, text );
115 }
116 else
117 {
118 dc.DrawText( text,
119 rect.x+xOffset+wxPG_XBEFORETEXT,
120 rect.y+yOffset );
121 }
122}
123
124void wxPGCellRenderer::DrawCaptionSelectionRect( wxDC& dc, int x, int y, int w, int h ) const
125{
126 wxRect focusRect(x,y+((h-dc.GetCharHeight())/2),w,h);
127 wxPGDrawFocusRect(dc,focusRect);
128}
129
130int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell& cell, int flags ) const
131{
132 int imageOffset = 0;
133
134 if ( !(flags & Selected) )
135 {
136 // Draw using wxPGCell information, if available
137 wxColour fgCol = cell.GetFgCol();
138 if ( fgCol.Ok() )
139 dc.SetTextForeground(fgCol);
140
141 wxColour bgCol = cell.GetBgCol();
142 if ( bgCol.Ok() )
143 {
144 dc.SetPen(bgCol);
145 dc.SetBrush(bgCol);
146 dc.DrawRectangle(rect);
147 }
148 }
149
150 const wxBitmap& bmp = cell.GetBitmap();
151 if ( bmp.Ok() &&
152 // In control, do not draw oversized bitmap
153 (!(flags & Control) || bmp.GetHeight() < rect.height )
154 )
155 {
156 dc.DrawBitmap( bmp,
157 rect.x + wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
158 rect.y + wxPG_CUSTOM_IMAGE_SPACINGY,
159 true );
160 imageOffset = bmp.GetWidth();
161 }
162
163 return imageOffset;
164}
165
166// -----------------------------------------------------------------------
167// wxPGDefaultRenderer
168// -----------------------------------------------------------------------
169
170void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
171 const wxPropertyGrid* propertyGrid, wxPGProperty* property,
172 int column, int item, int flags ) const
173{
174 bool isUnspecified = property->IsValueUnspecified();
175
176 if ( column == 1 && item == -1 )
177 {
178 int cmnVal = property->GetCommonValue();
179 if ( cmnVal >= 0 )
180 {
181 // Common Value
182 if ( !isUnspecified )
183 DrawText( dc, rect, 0, propertyGrid->GetCommonValueLabel(cmnVal) );
184 return;
185 }
186 }
187
188 const wxPGEditor* editor = NULL;
189 const wxPGCell* cell = property->GetCell(column);
190
191 wxString text;
192 int imageOffset = 0;
193
194 // Use choice cell?
195 if ( column == 1 && (flags & Control) )
196 {
939d9364
JS
197 int selectedIndex = property->GetChoiceSelection();
198 if ( selectedIndex != wxNOT_FOUND )
199 {
200 const wxPGChoices& choices = property->GetChoices();
201 const wxPGCell* ccell = &choices[selectedIndex];
202 if ( ccell &&
203 ( ccell->GetBitmap().IsOk() || ccell->GetFgCol().IsOk() || ccell->GetBgCol().IsOk() )
204 )
205 cell = ccell;
206 }
1c4293cb
VZ
207 }
208
209 if ( cell )
210 {
211 int preDrawFlags = flags;
212
213 if ( propertyGrid->GetInternalFlags() & wxPG_FL_CELL_OVERRIDES_SEL )
214 preDrawFlags = preDrawFlags & ~(Selected);
215
216 imageOffset = PreDrawCell( dc, rect, *cell, preDrawFlags );
217 text = cell->GetText();
218 if ( text == wxS("@!") )
219 {
220 if ( column == 0 )
221 text = property->GetLabel();
222 else if ( column == 1 )
223 text = property->GetValueString();
224 else
225 text = wxEmptyString;
226 }
227 }
228 else if ( column == 0 )
229 {
230 // Caption
231 DrawText( dc, rect, 0, property->GetLabel() );
232 }
233 else if ( column == 1 )
234 {
235 if ( !isUnspecified )
236 {
237 editor = property->GetColumnEditor(column);
238
239 // Regular property value
240
241 wxSize imageSize = propertyGrid->GetImageSize(property, item);
242
243 wxPGPaintData paintdata;
244 paintdata.m_parent = propertyGrid;
245 paintdata.m_choiceItem = item;
246
247 if ( imageSize.x > 0 )
248 {
249 wxRect imageRect(rect.x + wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
250 rect.y+wxPG_CUSTOM_IMAGE_SPACINGY,
251 wxPG_CUSTOM_IMAGE_WIDTH,
252 rect.height-(wxPG_CUSTOM_IMAGE_SPACINGY*2));
253
254 /*if ( imageSize.x == wxPG_FULL_CUSTOM_PAINT_WIDTH )
255 {
256 imageRect.width = m_width - imageRect.x;
257 }*/
258
259 dc.SetPen( wxPen(propertyGrid->GetCellTextColour(), 1, wxSOLID) );
260
261 paintdata.m_drawnWidth = imageSize.x;
262 paintdata.m_drawnHeight = imageSize.y;
263
264 if ( !isUnspecified )
265 {
266 property->OnCustomPaint( dc, imageRect, paintdata );
267 }
268 else
269 {
270 dc.SetBrush(*wxWHITE_BRUSH);
271 dc.DrawRectangle(imageRect);
272 }
273
274 imageOffset = paintdata.m_drawnWidth;
275 }
276
277 text = property->GetValueString();
278
279 // Add units string?
280 if ( propertyGrid->GetColumnCount() <= 2 )
281 {
282 wxString unitsString = property->GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
283 if ( unitsString.length() )
284 text = wxString::Format(wxS("%s %s"), text.c_str(), unitsString.c_str() );
285 }
286 }
287
288 if ( text.length() == 0 )
289 {
290 // Try to show inline help if no text
291 wxVariant vInlineHelp = property->GetAttribute(wxPGGlobalVars->m_strInlineHelp);
292 if ( !vInlineHelp.IsNull() )
293 {
294 text = vInlineHelp.GetString();
295 dc.SetTextForeground(propertyGrid->GetCellDisabledTextColour());
296 }
297 }
298 }
299 else if ( column == 2 )
300 {
301 // Add units string?
302 if ( !text.length() )
303 text = property->GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
304 }
305
306 DrawEditorValue( dc, rect, imageOffset, text, property, editor );
307
308 // active caption gets nice dotted rectangle
309 if ( property->IsCategory() /*&& column == 0*/ )
310 {
311 if ( flags & Selected )
312 {
313 if ( imageOffset > 0 )
314 imageOffset += wxCC_CUSTOM_IMAGE_MARGIN2 + 4;
315
316 DrawCaptionSelectionRect( dc,
317 rect.x+wxPG_XBEFORETEXT-wxPG_CAPRECTXMARGIN+imageOffset,
318 rect.y-wxPG_CAPRECTYMARGIN+1,
319 ((wxPropertyCategory*)property)->GetTextExtent(propertyGrid,
320 propertyGrid->GetCaptionFont())
321 +(wxPG_CAPRECTXMARGIN*2),
322 propertyGrid->GetFontHeight()+(wxPG_CAPRECTYMARGIN*2) );
323 }
324 }
325}
326
327wxSize wxPGDefaultRenderer::GetImageSize( const wxPGProperty* property,
328 int column,
329 int item ) const
330{
331 if ( property && column == 1 )
332 {
333 if ( item == -1 )
334 {
335 wxBitmap* bmp = property->GetValueImage();
336
337 if ( bmp && bmp->Ok() )
338 return wxSize(bmp->GetWidth(),bmp->GetHeight());
339 }
340 }
341 return wxSize(0,0);
342}
343
344// -----------------------------------------------------------------------
345// wxPGCell
346// -----------------------------------------------------------------------
347
348wxPGCell::wxPGCell()
349{
350}
351
352wxPGCell::wxPGCell( const wxString& text,
353 const wxBitmap& bitmap,
354 const wxColour& fgCol,
355 const wxColour& bgCol )
356 : m_bitmap(bitmap), m_fgCol(fgCol), m_bgCol(bgCol)
357{
358 m_text = text;
359}
360
361// -----------------------------------------------------------------------
362// wxPGProperty
363// -----------------------------------------------------------------------
364
365IMPLEMENT_ABSTRACT_CLASS(wxPGProperty, wxObject)
366
367wxString* wxPGProperty::sm_wxPG_LABEL = NULL;
368
369void wxPGProperty::Init()
370{
371 m_commonValue = -1;
372 m_arrIndex = 0xFFFF;
373 m_parent = NULL;
374
375 m_parentState = (wxPropertyGridPageState*) NULL;
376
377 m_clientData = NULL;
378 m_clientObject = NULL;
379
380 m_customEditor = (wxPGEditor*) NULL;
381#if wxUSE_VALIDATORS
382 m_validator = (wxValidator*) NULL;
383#endif
384 m_valueBitmap = (wxBitmap*) NULL;
385
386 m_maxLen = 0; // infinite maximum length
387
388 m_flags = wxPG_PROP_PROPERTY;
389
390 m_depth = 1;
391 m_bgColIndex = 0;
392 m_fgColIndex = 0;
393
394 SetExpanded(true);
395}
396
397
398void wxPGProperty::Init( const wxString& label, const wxString& name )
399{
400 // We really need to check if &label and &name are NULL pointers
401 // (this can if we are called before property grid has been initalized)
402
403 if ( (&label) != NULL && label != wxPG_LABEL )
404 m_label = label;
405
406 if ( (&name) != NULL && name != wxPG_LABEL )
407 DoSetName( name );
408 else
409 DoSetName( m_label );
410
411 Init();
412}
413
414wxPGProperty::wxPGProperty()
415 : wxObject()
416{
417 Init();
418}
419
420
421wxPGProperty::wxPGProperty( const wxString& label, const wxString& name )
422 : wxObject()
423{
424 Init( label, name );
425}
426
427
428wxPGProperty::~wxPGProperty()
429{
430 delete m_clientObject;
431
432 Empty(); // this deletes items
433
434 delete m_valueBitmap;
435#if wxUSE_VALIDATORS
436 delete m_validator;
437#endif
438
439 unsigned int i;
440
441 for ( i=0; i<m_cells.size(); i++ )
36a403e0 442 delete (wxPGCell*) m_cells[i];
1c4293cb
VZ
443
444 // This makes it easier for us to detect dangling pointers
445 m_parent = NULL;
446}
447
448
449bool wxPGProperty::IsSomeParent( wxPGProperty* candidate ) const
450{
451 wxPGProperty* parent = m_parent;
452 do
453 {
454 if ( parent == candidate )
455 return true;
456 parent = parent->m_parent;
457 } while ( parent );
458 return false;
459}
460
461
462wxString wxPGProperty::GetName() const
463{
464 wxPGProperty* parent = GetParent();
465
466 if ( !m_name.length() || !parent || parent->IsCategory() || parent->IsRoot() )
467 return m_name;
468
469 return m_parent->GetName() + wxS(".") + m_name;
470}
471
472wxPropertyGrid* wxPGProperty::GetGrid() const
473{
474 if ( !m_parentState )
475 return NULL;
476 return m_parentState->GetGrid();
477}
478
f7a094e1
JS
479int wxPGProperty::Index( const wxPGProperty* p ) const
480{
481 for ( unsigned int i = 0; i<m_children.size(); i++ )
482 {
483 if ( p == m_children[i] )
484 return i;
485 }
486 return wxNOT_FOUND;
487}
1c4293cb
VZ
488
489void wxPGProperty::UpdateControl( wxWindow* primary )
490{
491 if ( primary )
492 GetEditorClass()->UpdateControl(this, primary);
493}
494
495bool wxPGProperty::ValidateValue( wxVariant& WXUNUSED(value), wxPGValidationInfo& WXUNUSED(validationInfo) ) const
496{
497 return true;
498}
499
500void wxPGProperty::OnSetValue()
501{
502}
503
504void wxPGProperty::RefreshChildren ()
505{
506}
507
508wxString wxPGProperty::GetColumnText( unsigned int col ) const
509{
510 wxPGCell* cell = GetCell(col);
511 if ( cell )
512 {
513 return cell->GetText();
514 }
515 else
516 {
517 if ( col == 0 )
518 return GetLabel();
519 else if ( col == 1 )
520 return GetDisplayedString();
521 else if ( col == 2 )
522 return GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
523 }
524
525 return wxEmptyString;
526}
527
528void wxPGProperty::GenerateComposedValue( wxString& text, int argFlags ) const
529{
530 int i;
f7a094e1 531 int iMax = m_children.size();
1c4293cb
VZ
532
533 text.clear();
534 if ( iMax == 0 )
535 return;
536
537 if ( iMax > PWC_CHILD_SUMMARY_LIMIT &&
538 !(argFlags & wxPG_FULL_VALUE) )
539 iMax = PWC_CHILD_SUMMARY_LIMIT;
540
541 int iMaxMinusOne = iMax-1;
542
543 if ( !IsTextEditable() )
544 argFlags |= wxPG_UNEDITABLE_COMPOSITE_FRAGMENT;
545
f7a094e1 546 wxPGProperty* curChild = m_children[0];
1c4293cb
VZ
547
548 for ( i = 0; i < iMax; i++ )
549 {
550 wxString s;
551 if ( !curChild->IsValueUnspecified() )
552 s = curChild->GetValueString(argFlags|wxPG_COMPOSITE_FRAGMENT);
553
554 bool skip = false;
555 if ( (argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT) && !s.length() )
556 skip = true;
557
558 if ( !curChild->GetChildCount() || skip )
559 text += s;
560 else
561 text += wxS("[") + s + wxS("]");
562
563 if ( i < iMaxMinusOne )
564 {
565 if ( text.length() > PWC_CHILD_SUMMARY_CHAR_LIMIT &&
566 !(argFlags & wxPG_EDITABLE_VALUE) &&
567 !(argFlags & wxPG_FULL_VALUE) )
568 break;
569
570 if ( !skip )
571 {
572 if ( !curChild->GetChildCount() )
573 text += wxS("; ");
574 else
575 text += wxS(" ");
576 }
577
f7a094e1 578 curChild = m_children[i+1];
1c4293cb
VZ
579 }
580 }
581
582 // Remove superfluous semicolon and space
583 wxString rest;
584 if ( text.EndsWith(wxS("; "), &rest) )
585 text = rest;
586
f7a094e1 587 if ( (unsigned int)i < m_children.size() )
1c4293cb
VZ
588 text += wxS("; ...");
589}
590
591wxString wxPGProperty::GetValueAsString( int argFlags ) const
592{
593 wxCHECK_MSG( GetChildCount() > 0,
594 wxString(),
595 wxT("If user property does not have any children, it must override GetValueAsString") );
596
597 wxString text;
598 GenerateComposedValue(text, argFlags);
599 return text;
600}
601
602wxString wxPGProperty::GetValueString( int argFlags ) const
603{
604 if ( IsValueUnspecified() )
605 return wxEmptyString;
606
607 if ( m_commonValue == -1 )
608 return GetValueAsString(argFlags);
609
610 //
611 // Return common value's string representation
612 wxPropertyGrid* pg = GetGrid();
613 const wxPGCommonValue* cv = pg->GetCommonValue(m_commonValue);
614
615 if ( argFlags & wxPG_FULL_VALUE )
616 {
617 return cv->GetLabel();
618 }
619 else if ( argFlags & wxPG_EDITABLE_VALUE )
620 {
621 return cv->GetEditableText();
622 }
623 else
624 {
625 return cv->GetLabel();
626 }
627}
628
629bool wxPGProperty::IntToValue( wxVariant& variant, int number, int WXUNUSED(argFlags) ) const
630{
631 variant = (long)number;
632 return true;
633}
634
635// Convert semicolon delimited tokens into child values.
636bool wxPGProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
637{
638 if ( !GetChildCount() )
639 return false;
640
641 unsigned int curChild = 0;
642
f7a094e1 643 unsigned int iMax = m_children.size();
1c4293cb
VZ
644
645 if ( iMax > PWC_CHILD_SUMMARY_LIMIT &&
646 !(argFlags & wxPG_FULL_VALUE) )
647 iMax = PWC_CHILD_SUMMARY_LIMIT;
648
649 bool changed = false;
650
651 wxString token;
652 size_t pos = 0;
653
654 // Its best only to add non-empty group items
655 bool addOnlyIfNotEmpty = false;
656 const wxChar delimeter = wxS(';');
657
658 size_t tokenStart = 0xFFFFFF;
659
660 wxVariantList temp_list;
661 wxVariant list(temp_list);
662
663 int propagatedFlags = argFlags & wxPG_REPORT_ERROR;
664
665#ifdef __WXDEBUG__
666 bool debug_print = false;
667#endif
668
669#ifdef __WXDEBUG__
670 if ( debug_print )
671 wxLogDebug(wxT(">> %s.StringToValue('%s')"),GetLabel().c_str(),text.c_str());
672#endif
673
674 wxString::const_iterator it = text.begin();
675 wxUniChar a;
676
677 if ( it != text.end() )
678 a = *it;
679 else
680 a = 0;
681
682 for ( ;; )
683 {
684 if ( tokenStart != 0xFFFFFF )
685 {
686 // Token is running
687 if ( a == delimeter || a == 0 )
688 {
689 token = text.substr(tokenStart,pos-tokenStart);
690 token.Trim(true);
691 size_t len = token.length();
692
693 if ( !addOnlyIfNotEmpty || len > 0 )
694 {
695 const wxPGProperty* child = Item(curChild);
696 #ifdef __WXDEBUG__
697 if ( debug_print )
698 wxLogDebug(wxT("token = '%s', child = %s"),token.c_str(),child->GetLabel().c_str());
699 #endif
700
701 if ( len > 0 )
702 {
703 bool wasUnspecified = child->IsValueUnspecified();
704
705 wxVariant variant(child->GetValueRef());
706 if ( child->StringToValue(variant, token, propagatedFlags|wxPG_COMPOSITE_FRAGMENT) )
707 {
d665918b 708 variant.SetName(child->GetBaseName());
1c4293cb
VZ
709
710 // Clear unspecified flag only if OnSetValue() didn't
711 // affect it.
712 if ( child->IsValueUnspecified() &&
713 (wasUnspecified || !UsesAutoUnspecified()) )
714 {
715 variant = child->GetDefaultValue();
716 }
717
718 list.Append(variant);
719
720 changed = true;
721 }
722 }
723 else
724 {
725 // Empty, becomes unspecified
726 wxVariant variant2;
d665918b 727 variant2.SetName(child->GetBaseName());
1c4293cb
VZ
728 list.Append(variant2);
729 changed = true;
730 }
731
732 curChild++;
733 if ( curChild >= iMax )
734 break;
735 }
736
737 tokenStart = 0xFFFFFF;
738 }
739 }
740 else
741 {
742 // Token is not running
743 if ( a != wxS(' ') )
744 {
745
746 addOnlyIfNotEmpty = false;
747
748 // Is this a group of tokens?
749 if ( a == wxS('[') )
750 {
751 int depth = 1;
752
753 if ( it != text.end() ) it++;
754 pos++;
755 size_t startPos = pos;
756
757 // Group item - find end
758 while ( it != text.end() && depth > 0 )
759 {
760 a = *it;
761 it++;
762 pos++;
763
764 if ( a == wxS(']') )
765 depth--;
766 else if ( a == wxS('[') )
767 depth++;
768 }
769
770 token = text.substr(startPos,pos-startPos-1);
771
772 if ( !token.length() )
773 break;
774
775 const wxPGProperty* child = Item(curChild);
776
2bbd3749
JS
777 wxVariant oldChildValue = child->GetValue();
778 wxVariant variant(oldChildValue);
779 bool stvRes = child->StringToValue( variant, token, propagatedFlags );
780 if ( stvRes || (variant != oldChildValue) )
1c4293cb 781 {
2bbd3749
JS
782 if ( stvRes )
783 changed = true;
1c4293cb
VZ
784 }
785 else
786 {
787 // Failed, becomes unspecified
2bbd3749 788 variant.MakeNull();
1c4293cb
VZ
789 changed = true;
790 }
791
2bbd3749
JS
792 variant.SetName(child->GetBaseName());
793 list.Append(variant);
794
1c4293cb
VZ
795 curChild++;
796 if ( curChild >= iMax )
797 break;
798
799 addOnlyIfNotEmpty = true;
800
801 tokenStart = 0xFFFFFF;
802 }
803 else
804 {
805 tokenStart = pos;
806
807 if ( a == delimeter )
808 {
809 pos--;
810 it--;
811 }
812 }
813 }
814 }
815
816 if ( a == 0 )
817 break;
818
819 it++;
820 if ( it != text.end() )
821 {
822 a = *it;
823 }
824 else
825 {
826 a = 0;
827 }
828 pos++;
829 }
830
831 if ( changed )
832 variant = list;
833
834 return changed;
835}
836
837bool wxPGProperty::SetValueFromString( const wxString& text, int argFlags )
838{
839 wxVariant variant(m_value);
840 bool res = StringToValue(variant, text, argFlags);
841 if ( res )
842 SetValue(variant);
843 return res;
844}
845
846bool wxPGProperty::SetValueFromInt( long number, int argFlags )
847{
848 wxVariant variant(m_value);
849 bool res = IntToValue(variant, number, argFlags);
850 if ( res )
851 SetValue(variant);
852 return res;
853}
854
855wxSize wxPGProperty::OnMeasureImage( int WXUNUSED(item) ) const
856{
857 if ( m_valueBitmap )
858 return wxSize(m_valueBitmap->GetWidth(),-1);
859
860 return wxSize(0,0);
861}
862
863wxPGCellRenderer* wxPGProperty::GetCellRenderer( int WXUNUSED(column) ) const
864{
865 return wxPGGlobalVars->m_defaultRenderer;
866}
867
868void wxPGProperty::OnCustomPaint( wxDC& dc,
869 const wxRect& rect,
870 wxPGPaintData& )
871{
872 wxBitmap* bmp = m_valueBitmap;
873
874 wxCHECK_RET( bmp && bmp->Ok(), wxT("invalid bitmap") );
875
876 wxCHECK_RET( rect.x >= 0, wxT("unexpected measure call") );
877
878 dc.DrawBitmap(*bmp,rect.x,rect.y);
879}
880
881const wxPGEditor* wxPGProperty::DoGetEditorClass() const
882{
c26873c8 883 return wxPGEditor_TextCtrl;
1c4293cb
VZ
884}
885
886// Default extra property event handling - that is, none at all.
887bool wxPGProperty::OnEvent( wxPropertyGrid*, wxWindow*, wxEvent& )
888{
889 return false;
890}
891
892
893void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
894{
104837f2
JS
895 // If auto unspecified values are not wanted (via window or property style),
896 // then get default value instead of wxNullVariant.
897 if ( value.IsNull() && (flags & wxPG_SETVAL_BY_USER) &&
898 !UsesAutoUnspecified() )
899 {
900 value = GetDefaultValue();
901 }
902
1c4293cb
VZ
903 if ( !value.IsNull() )
904 {
8f18b252
JS
905 wxVariant tempListVariant;
906
1c4293cb
VZ
907 SetCommonValue(-1);
908 // List variants are reserved a special purpose
909 // as intermediate containers for child values
910 // of properties with children.
0372d42e 911 if ( value.GetType() == wxPG_VARIANT_TYPE_LIST )
1c4293cb 912 {
8f18b252
JS
913 //
914 // However, situation is different for composed string properties
915 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE) )
916 {
917 tempListVariant = value;
918 pList = &tempListVariant;
919 }
920
1c4293cb
VZ
921 wxVariant newValue;
922 AdaptListToValue(value, &newValue);
923 value = newValue;
924 //wxLogDebug(wxT(">> %s.SetValue() adapted list value to type '%s'"),GetName().c_str(),value.GetType().c_str());
925 }
926
927 if ( HasFlag( wxPG_PROP_AGGREGATE) )
928 flags |= wxPG_SETVAL_AGGREGATED;
929
930 if ( pList && !pList->IsNull() )
931 {
0372d42e 932 wxASSERT( pList->GetType() == wxPG_VARIANT_TYPE_LIST );
1c4293cb
VZ
933 wxASSERT( GetChildCount() );
934 wxASSERT( !IsCategory() );
935
936 wxVariantList& list = pList->GetList();
937 wxVariantList::iterator node;
938 unsigned int i = 0;
939
940 //wxLogDebug(wxT(">> %s.SetValue() pList parsing"),GetName().c_str());
941
942 // Children in list can be in any order, but we will give hint to
d665918b 943 // GetPropertyByNameWH(). This optimizes for full list parsing.
1c4293cb
VZ
944 for ( node = list.begin(); node != list.end(); node++ )
945 {
946 wxVariant& childValue = *((wxVariant*)*node);
d665918b 947 wxPGProperty* child = GetPropertyByNameWH(childValue.GetName(), i);
1c4293cb
VZ
948 if ( child )
949 {
d665918b 950 //wxLogDebug(wxT("%i: child = %s, childValue.GetType()=%s"),i,child->GetBaseName().c_str(),childValue.GetType().c_str());
0372d42e 951 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
1c4293cb
VZ
952 {
953 if ( child->HasFlag(wxPG_PROP_AGGREGATE) && !(flags & wxPG_SETVAL_AGGREGATED) )
954 {
955 wxVariant listRefCopy = childValue;
956 child->SetValue(childValue, &listRefCopy, flags|wxPG_SETVAL_FROM_PARENT);
957 }
958 else
959 {
960 wxVariant oldVal = child->GetValue();
961 child->SetValue(oldVal, &childValue, flags|wxPG_SETVAL_FROM_PARENT);
962 }
963 }
0372d42e 964 else if ( child->GetValue() != childValue )
1c4293cb
VZ
965 {
966 // For aggregate properties, we will trust RefreshChildren()
967 // to update child values.
968 if ( !HasFlag(wxPG_PROP_AGGREGATE) )
969 child->SetValue(childValue, NULL, flags|wxPG_SETVAL_FROM_PARENT);
8f18b252
JS
970 if ( flags & wxPG_SETVAL_BY_USER )
971 child->SetFlag(wxPG_PROP_MODIFIED);
1c4293cb
VZ
972 }
973 }
974 i++;
975 }
976 }
977
978 if ( !value.IsNull() )
979 {
0372d42e 980 m_value = value;
1c4293cb
VZ
981 OnSetValue();
982
983 if ( !(flags & wxPG_SETVAL_FROM_PARENT) )
984 UpdateParentValues();
985 }
986
8f18b252 987 if ( flags & wxPG_SETVAL_BY_USER )
1c4293cb
VZ
988 SetFlag(wxPG_PROP_MODIFIED);
989
990 if ( HasFlag(wxPG_PROP_AGGREGATE) )
991 RefreshChildren();
992 }
993 else
994 {
995 if ( m_commonValue != -1 )
996 {
997 wxPropertyGrid* pg = GetGrid();
998 if ( !pg || m_commonValue != pg->GetUnspecifiedCommonValue() )
999 SetCommonValue(-1);
1000 }
1001
1002 m_value = value;
1003
1004 // Set children to unspecified, but only if aggregate or
1005 // value is <composed>
1006 if ( AreChildrenComponents() )
1007 {
1008 unsigned int i;
1009 for ( i=0; i<GetChildCount(); i++ )
1010 Item(i)->SetValue(value, NULL, flags|wxPG_SETVAL_FROM_PARENT);
1011 }
1012 }
1013
1014 //
1015 // Update editor control
1016 //
1017
1018 // We need to check for these, otherwise GetGrid() may fail.
1019 if ( flags & wxPG_SETVAL_REFRESH_EDITOR )
1020 RefreshEditor();
1021}
1022
1023
1024void wxPGProperty::SetValueInEvent( wxVariant value ) const
1025{
1026 GetGrid()->ValueChangeInEvent(value);
1027}
1028
1029void wxPGProperty::SetFlagRecursively( FlagType flag, bool set )
1030{
1031 if ( set )
1032 SetFlag(flag);
1033 else
1034 ClearFlag(flag);
1035
1036 unsigned int i;
1037 for ( i = 0; i < GetChildCount(); i++ )
1038 Item(i)->SetFlagRecursively(flag, set);
1039}
1040
1041void wxPGProperty::RefreshEditor()
1042{
1043 if ( m_parent && GetParentState() )
1044 {
1045 wxPropertyGrid* pg = GetParentState()->GetGrid();
1046 if ( pg->GetSelectedProperty() == this )
1047 {
1048 wxWindow* editor = pg->GetEditorControl();
1049 if ( editor )
1050 GetEditorClass()->UpdateControl( this, editor );
1051 }
1052 }
1053}
1054
1055
1056wxVariant wxPGProperty::GetDefaultValue() const
1057{
1058 wxVariant defVal = GetAttribute(wxS("DefaultValue"));
1059 if ( !defVal.IsNull() )
1060 return defVal;
1061
1062 wxVariant value = GetValue();
1063
1064 if ( !value.IsNull() )
1065 {
0372d42e
JS
1066 wxString valueType(value.GetType());
1067
1068 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1c4293cb 1069 return wxPGVariant_Zero;
0372d42e 1070 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1c4293cb 1071 return wxPGVariant_EmptyString;
0372d42e 1072 if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1c4293cb 1073 return wxPGVariant_False;
0372d42e 1074 if ( valueType == wxPG_VARIANT_TYPE_DOUBLE )
1c4293cb 1075 return wxVariant(0.0);
0372d42e 1076 if ( valueType == wxPG_VARIANT_TYPE_ARRSTRING )
1c4293cb 1077 return wxVariant(wxArrayString());
0372d42e
JS
1078 if ( valueType == wxS("wxLongLong") )
1079 return WXVARIANT(wxLongLong(0));
1080 if ( valueType == wxS("wxULongLong") )
1081 return WXVARIANT(wxULongLong(0));
1082 if ( valueType == wxS("wxColour") )
1083 return WXVARIANT(*wxBLACK);
1c4293cb 1084#if wxUSE_DATETIME
0372d42e 1085 if ( valueType == wxPG_VARIANT_TYPE_DATETIME )
1c4293cb
VZ
1086 return wxVariant(wxDateTime::Now());
1087#endif
0372d42e
JS
1088 if ( valueType == wxS("wxFont") )
1089 return WXVARIANT(*wxNORMAL_FONT);
1090 if ( valueType == wxS("wxPoint") )
1091 return WXVARIANT(wxPoint(0, 0));
1092 if ( valueType == wxS("wxSize") )
1093 return WXVARIANT(wxSize(0, 0));
1c4293cb
VZ
1094 }
1095
1096 return wxVariant();
1097}
1098
1099void wxPGProperty::SetCell( int column, wxPGCell* cellObj )
1100{
1101 if ( column >= (int)m_cells.size() )
1102 m_cells.SetCount(column+1, NULL);
1103
1104 delete (wxPGCell*) m_cells[column];
1105 m_cells[column] = cellObj;
1106}
1107
1c4293cb
VZ
1108wxPGEditorDialogAdapter* wxPGProperty::GetEditorDialog() const
1109{
1110 return NULL;
1111}
1112
1113bool wxPGProperty::DoSetAttribute( const wxString& WXUNUSED(name), wxVariant& WXUNUSED(value) )
1114{
1115 return false;
1116}
1117
1118void wxPGProperty::SetAttribute( const wxString& name, wxVariant value )
1119{
1120 if ( DoSetAttribute( name, value ) )
1121 {
1122 // Support working without grid, when possible
1123 if ( wxPGGlobalVars->HasExtraStyle( wxPG_EX_WRITEONLY_BUILTIN_ATTRIBUTES ) )
1124 return;
1125 }
1126
1127 m_attributes.Set( name, value );
1128}
1129
1130void wxPGProperty::SetAttributes( const wxPGAttributeStorage& attributes )
1131{
1132 wxPGAttributeStorage::const_iterator it = attributes.StartIteration();
1133 wxVariant variant;
1134
1135 while ( attributes.GetNext(it, variant) )
1136 SetAttribute( variant.GetName(), variant );
1137}
1138
1139wxVariant wxPGProperty::DoGetAttribute( const wxString& WXUNUSED(name) ) const
1140{
1141 return wxVariant();
1142}
1143
1144
1145wxVariant wxPGProperty::GetAttribute( const wxString& name ) const
1146{
1147 return m_attributes.FindValue(name);
1148}
1149
1150wxString wxPGProperty::GetAttribute( const wxString& name, const wxString& defVal ) const
1151{
1152 wxVariant variant = m_attributes.FindValue(name);
1153
1154 if ( !variant.IsNull() )
1155 return variant.GetString();
1156
1157 return defVal;
1158}
1159
1160long wxPGProperty::GetAttributeAsLong( const wxString& name, long defVal ) const
1161{
1162 wxVariant variant = m_attributes.FindValue(name);
1163
1164 return wxPGVariantToInt(variant, defVal);
1165}
1166
1167double wxPGProperty::GetAttributeAsDouble( const wxString& name, double defVal ) const
1168{
1169 double retVal;
1170 wxVariant variant = m_attributes.FindValue(name);
1171
1172 if ( wxPGVariantToDouble(variant, &retVal) )
1173 return retVal;
1174
1175 return defVal;
1176}
1177
1178wxVariant wxPGProperty::GetAttributesAsList() const
1179{
1180 wxVariantList tempList;
1181 wxVariant v( tempList, wxString::Format(wxS("@%s@attr"),m_name.c_str()) );
1182
1183 wxPGAttributeStorage::const_iterator it = m_attributes.StartIteration();
1184 wxVariant variant;
1185
1186 while ( m_attributes.GetNext(it, variant) )
1187 v.Append(variant);
1188
1189 return v;
1190}
1191
1192// Slots of utility flags are NULL
1193const unsigned int gs_propFlagToStringSize = 14;
1194
1195static const wxChar* gs_propFlagToString[gs_propFlagToStringSize] = {
1196 NULL,
1197 wxT("DISABLED"),
1198 wxT("HIDDEN"),
1199 NULL,
1200 wxT("NOEDITOR"),
1201 wxT("COLLAPSED"),
1202 NULL,
1203 NULL,
1204 NULL,
1205 NULL,
1206 NULL,
1207 NULL,
1208 NULL,
1209 NULL
1210};
1211
1212wxString wxPGProperty::GetFlagsAsString( FlagType flagsMask ) const
1213{
1214 wxString s;
1215 int relevantFlags = m_flags & flagsMask & wxPG_STRING_STORED_FLAGS;
1216 FlagType a = 1;
1217
1218 unsigned int i = 0;
1219 for ( i=0; i<gs_propFlagToStringSize; i++ )
1220 {
1221 if ( relevantFlags & a )
1222 {
1223 const wxChar* fs = gs_propFlagToString[i];
1224 wxASSERT(fs);
1225 if ( s.length() )
1226 s << wxS("|");
1227 s << fs;
1228 }
1229 a = a << 1;
1230 }
1231
1232 return s;
1233}
1234
1235void wxPGProperty::SetFlagsFromString( const wxString& str )
1236{
1237 FlagType flags = 0;
1238
1239 WX_PG_TOKENIZER1_BEGIN(str, wxS('|'))
1240 unsigned int i;
1241 for ( i=0; i<gs_propFlagToStringSize; i++ )
1242 {
1243 const wxChar* fs = gs_propFlagToString[i];
1244 if ( fs && str == fs )
1245 {
1246 flags |= (1<<i);
1247 break;
1248 }
1249 }
1250 WX_PG_TOKENIZER1_END()
1251
1252 m_flags = (m_flags & ~wxPG_STRING_STORED_FLAGS) | flags;
1253}
1254
1255wxValidator* wxPGProperty::DoGetValidator() const
1256{
1257 return (wxValidator*) NULL;
1258}
1259
939d9364 1260int wxPGProperty::InsertChoice( const wxString& label, int index, int value )
1c4293cb 1261{
939d9364
JS
1262 wxPropertyGrid* pg = GetGrid();
1263 int sel = GetChoiceSelection();
1264
1265 int newSel = sel;
1266
1267 if ( index == wxNOT_FOUND )
1268 index = m_choices.GetCount();
1269
1270 if ( index <= sel )
1271 newSel++;
1272
1273 m_choices.Insert(label, index, value);
1274
1275 if ( sel != newSel )
1276 SetChoiceSelection(newSel);
1277
1278 if ( this == pg->GetSelection() )
1279 GetEditorClass()->InsertItem(pg->GetEditorControl(),label,index);
1280
1281 return index;
1c4293cb
VZ
1282}
1283
939d9364
JS
1284
1285void wxPGProperty::DeleteChoice( int index )
1c4293cb 1286{
939d9364
JS
1287 wxPropertyGrid* pg = GetGrid();
1288
1289 int sel = GetChoiceSelection();
1290 int newSel = sel;
1291
1292 // Adjust current value
1293 if ( sel == index )
1294 {
1295 SetValueToUnspecified();
1296 newSel = 0;
1297 }
1298 else if ( index < sel )
1299 {
1300 newSel--;
1301 }
1302
1303 m_choices.RemoveAt(index);
1304
1305 if ( sel != newSel )
1306 SetChoiceSelection(newSel);
1307
1308 if ( this == pg->GetSelection() )
1309 GetEditorClass()->DeleteItem(pg->GetEditorControl(), index);
1c4293cb
VZ
1310}
1311
939d9364 1312int wxPGProperty::GetChoiceSelection() const
1c4293cb 1313{
939d9364
JS
1314 wxVariant value = GetValue();
1315 wxString valueType = value.GetType();
1316 int index = wxNOT_FOUND;
1317
1318 if ( IsValueUnspecified() || !m_choices.GetCount() )
1319 return wxNOT_FOUND;
1320
1321 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1322 {
1323 index = value.GetLong();
1324 }
1325 else if ( valueType == wxPG_VARIANT_TYPE_STRING )
1326 {
1327 index = m_choices.Index(value.GetString());
1328 }
1329 else if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1330 {
1331 index = value.GetBool()? 1 : 0;
1332 }
1333
1334 return index;
1c4293cb
VZ
1335}
1336
939d9364 1337void wxPGProperty::SetChoiceSelection( int newValue )
1c4293cb 1338{
939d9364
JS
1339 // Changes value of a property with choices, but only
1340 // works if the value type is long or string.
1341 wxString valueType = GetValue().GetType();
1342
1343 wxCHECK_RET( m_choices.IsOk(), wxT("invalid choiceinfo") );
1c4293cb 1344
939d9364
JS
1345 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1346 {
1347 SetValue( m_choices.GetLabel(newValue) );
1348 }
1349 else // if ( valueType == wxPG_VARIANT_TYPE_LONG )
1350 {
1351 SetValue( (long) newValue );
1352 }
1c4293cb
VZ
1353}
1354
1355bool wxPGProperty::SetChoices( wxPGChoices& choices )
1356{
939d9364 1357 m_choices.Assign(choices);
1c4293cb 1358
1c4293cb 1359 {
939d9364
JS
1360 // This may be needed to trigger some initialization
1361 // (but don't do it if property is somewhat uninitialized)
1362 wxVariant defVal = GetDefaultValue();
1363 if ( defVal.IsNull() )
1364 return false;
1c4293cb 1365
939d9364 1366 SetValue(defVal);
1c4293cb 1367 }
939d9364
JS
1368
1369 return true;
1c4293cb
VZ
1370}
1371
1372
1373const wxPGEditor* wxPGProperty::GetEditorClass() const
1374{
1375 const wxPGEditor* editor;
1376
1377 if ( !m_customEditor )
1378 {
1379 editor = DoGetEditorClass();
1380 }
1381 else
1382 editor = m_customEditor;
1383
1384 //
1385 // Maybe override editor if common value specified
1386 if ( GetDisplayedCommonValueCount() )
1387 {
1388 // TextCtrlAndButton -> ComboBoxAndButton
1389 if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlAndButtonEditor)) )
c26873c8 1390 editor = wxPGEditor_ChoiceAndButton;
1c4293cb
VZ
1391
1392 // TextCtrl -> ComboBox
1393 else if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlEditor)) )
c26873c8 1394 editor = wxPGEditor_ComboBox;
1c4293cb
VZ
1395 }
1396
1397 return editor;
1398}
1399
1c4293cb
VZ
1400bool wxPGProperty::HasVisibleChildren() const
1401{
1402 unsigned int i;
1403
1404 for ( i=0; i<GetChildCount(); i++ )
1405 {
1406 wxPGProperty* child = Item(i);
1407
1408 if ( !child->HasFlag(wxPG_PROP_HIDDEN) )
1409 return true;
1410 }
1411
1412 return false;
1413}
1414
1c4293cb
VZ
1415bool wxPGProperty::RecreateEditor()
1416{
1417 wxPropertyGrid* pg = GetGrid();
1418 wxASSERT(pg);
1419
1420 wxPGProperty* selected = pg->GetSelection();
1421 if ( this == selected )
1422 {
1423 pg->DoSelectProperty(this, wxPG_SEL_FORCE);
1424 return true;
1425 }
1426 return false;
1427}
1428
1429
1430void wxPGProperty::SetValueImage( wxBitmap& bmp )
1431{
1432 delete m_valueBitmap;
1433
1434 if ( &bmp && bmp.Ok() )
1435 {
1436 // Resize the image
1437 wxSize maxSz = GetGrid()->GetImageSize();
1438 wxSize imSz(bmp.GetWidth(),bmp.GetHeight());
1439
1440 if ( imSz.x != maxSz.x || imSz.y != maxSz.y )
1441 {
1442 // Create a memory DC
1443 wxBitmap* bmpNew = new wxBitmap(maxSz.x,maxSz.y,bmp.GetDepth());
1444
1445 wxMemoryDC dc;
1446 dc.SelectObject(*bmpNew);
1447
1448 // Scale
1449 // FIXME: This is ugly - use image or wait for scaling patch.
1450 double scaleX = (double)maxSz.x / (double)imSz.x;
1451 double scaleY = (double)maxSz.y / (double)imSz.y;
1452
1453 dc.SetUserScale(scaleX,scaleY);
1454
1455 dc.DrawBitmap( bmp, 0, 0 );
1456
1457 m_valueBitmap = bmpNew;
1458 }
1459 else
1460 {
1461 m_valueBitmap = new wxBitmap(bmp);
1462 }
1463
1464 m_flags |= wxPG_PROP_CUSTOMIMAGE;
1465 }
1466 else
1467 {
1468 m_valueBitmap = NULL;
1469 m_flags &= ~(wxPG_PROP_CUSTOMIMAGE);
1470 }
1471}
1472
1473
1474wxPGProperty* wxPGProperty::GetMainParent() const
1475{
1476 const wxPGProperty* curChild = this;
1477 const wxPGProperty* curParent = m_parent;
1478
1479 while ( curParent && !curParent->IsCategory() )
1480 {
1481 curChild = curParent;
1482 curParent = curParent->m_parent;
1483 }
1484
1485 return (wxPGProperty*) curChild;
1486}
1487
1488
1489const wxPGProperty* wxPGProperty::GetLastVisibleSubItem() const
1490{
1491 //
1492 // Returns last visible sub-item, recursively.
1493 if ( !IsExpanded() || !GetChildCount() )
1494 return this;
1495
1496 return Last()->GetLastVisibleSubItem();
1497}
1498
1499
1500bool wxPGProperty::IsVisible() const
1501{
1502 const wxPGProperty* parent;
1503
1504 if ( HasFlag(wxPG_PROP_HIDDEN) )
1505 return false;
1506
1507 for ( parent = GetParent(); parent != NULL; parent = parent->GetParent() )
1508 {
1509 if ( !parent->IsExpanded() || parent->HasFlag(wxPG_PROP_HIDDEN) )
1510 return false;
1511 }
1512
1513 return true;
1514}
1515
1516wxPropertyGrid* wxPGProperty::GetGridIfDisplayed() const
1517{
1518 wxPropertyGridPageState* state = GetParentState();
1519 wxPropertyGrid* propGrid = state->GetGrid();
1520 if ( state == propGrid->GetState() )
1521 return propGrid;
1522 return NULL;
1523}
1524
1525
1526int wxPGProperty::GetY2( int lh ) const
1527{
1528 const wxPGProperty* parent;
1529 const wxPGProperty* child = this;
1530
1531 int y = 0;
1532
1533 for ( parent = GetParent(); parent != NULL; parent = child->GetParent() )
1534 {
1535 if ( !parent->IsExpanded() )
1536 return -1;
1537 y += parent->GetChildrenHeight(lh, child->GetIndexInParent());
1538 y += lh;
1539 child = parent;
1540 }
1541
1542 y -= lh; // need to reduce one level
1543
1544 return y;
1545}
1546
1547
1548int wxPGProperty::GetY() const
1549{
1550 return GetY2(GetGrid()->GetRowHeight());
1551}
1552
1c4293cb
VZ
1553// This is used by Insert etc.
1554void wxPGProperty::AddChild2( wxPGProperty* prop, int index, bool correct_mode )
1555{
f7a094e1 1556 if ( index < 0 || (size_t)index >= m_children.size() )
1c4293cb 1557 {
f7a094e1
JS
1558 if ( correct_mode ) prop->m_arrIndex = m_children.size();
1559 m_children.push_back( prop );
1c4293cb
VZ
1560 }
1561 else
1562 {
f7a094e1 1563 m_children.insert( m_children.begin()+index, prop);
5acb9127 1564 if ( correct_mode ) FixIndecesOfChildren( index );
1c4293cb
VZ
1565 }
1566
1567 prop->m_parent = this;
1568}
1569
1570// This is used by properties that have fixed sub-properties
1571void wxPGProperty::AddChild( wxPGProperty* prop )
1572{
d665918b
JS
1573 wxASSERT_MSG( prop->GetBaseName().length(),
1574 "Property's children must have unique, non-empty names within their scope" );
1575
f7a094e1
JS
1576 prop->m_arrIndex = m_children.size();
1577 m_children.push_back( prop );
1c4293cb
VZ
1578
1579 int custImgHeight = prop->OnMeasureImage().y;
1580 if ( custImgHeight < 0 /*|| custImgHeight > 1*/ )
1581 prop->m_flags |= wxPG_PROP_CUSTOMIMAGE;
1582
1583 prop->m_parent = this;
1584}
1585
d8c74d04
JS
1586void wxPGProperty::RemoveChild( wxPGProperty* p )
1587{
1588 wxArrayPGProperty::iterator it;
1589 wxArrayPGProperty& children = m_children;
1590
1591 for ( it=children.begin(); it != children.end(); it++ )
1592 {
1593 if ( *it == p )
1594 {
1595 m_children.erase(it);
1596 break;
1597 }
1598 }
1599}
1c4293cb
VZ
1600
1601void wxPGProperty::AdaptListToValue( wxVariant& list, wxVariant* value ) const
1602{
1603 wxASSERT( GetChildCount() );
1604 wxASSERT( !IsCategory() );
1605
1606 *value = GetValue();
1607
1608 if ( !list.GetCount() )
1609 return;
1610
1611 wxASSERT( GetChildCount() >= (unsigned int)list.GetCount() );
1612
1613 bool allChildrenSpecified;
1614
1615 // Don't fully update aggregate properties unless all children have
1616 // specified value
1617 if ( HasFlag(wxPG_PROP_AGGREGATE) )
1618 allChildrenSpecified = AreAllChildrenSpecified(&list);
1619 else
1620 allChildrenSpecified = true;
1621
1622 wxVariant childValue = list[0];
1623 unsigned int i;
1624 unsigned int n = 0;
1625
d665918b 1626 //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
1c4293cb
VZ
1627
1628 for ( i=0; i<GetChildCount(); i++ )
1629 {
1630 const wxPGProperty* child = Item(i);
1631
d665918b 1632 if ( childValue.GetName() == child->GetBaseName() )
1c4293cb
VZ
1633 {
1634 //wxLogDebug(wxT(" %s(n=%i), %s"),childValue.GetName().c_str(),n,childValue.GetType().c_str());
1635
0372d42e 1636 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
1c4293cb
VZ
1637 {
1638 wxVariant cv2(child->GetValue());
1639 child->AdaptListToValue(childValue, &cv2);
1640 childValue = cv2;
1641 }
1642
1643 if ( allChildrenSpecified )
1644 ChildChanged(*value, i, childValue);
1645 n++;
1646 if ( n == (unsigned int)list.GetCount() )
1647 break;
1648 childValue = list[n];
1649 }
1650 }
1651}
1652
1653
5acb9127 1654void wxPGProperty::FixIndecesOfChildren( unsigned int starthere )
1c4293cb
VZ
1655{
1656 size_t i;
1657 for ( i=starthere;i<GetChildCount();i++)
1658 Item(i)->m_arrIndex = i;
1659}
1660
1661
1662// Returns (direct) child property with given name (or NULL if not found)
1663wxPGProperty* wxPGProperty::GetPropertyByName( const wxString& name ) const
1664{
1665 size_t i;
1666
1667 for ( i=0; i<GetChildCount(); i++ )
1668 {
1669 wxPGProperty* p = Item(i);
1670 if ( p->m_name == name )
1671 return p;
1672 }
1673
1674 // Does it have point, then?
1675 int pos = name.Find(wxS('.'));
1676 if ( pos <= 0 )
1677 return (wxPGProperty*) NULL;
1678
1679 wxPGProperty* p = GetPropertyByName(name. substr(0,pos));
1680
1681 if ( !p || !p->GetChildCount() )
1682 return NULL;
1683
1684 return p->GetPropertyByName(name.substr(pos+1,name.length()-pos-1));
1685}
1686
d665918b 1687wxPGProperty* wxPGProperty::GetPropertyByNameWH( const wxString& name, unsigned int hintIndex ) const
1c4293cb
VZ
1688{
1689 unsigned int i = hintIndex;
1690
1691 if ( i >= GetChildCount() )
1692 i = 0;
1693
1694 unsigned int lastIndex = i - 1;
1695
1696 if ( lastIndex >= GetChildCount() )
1697 lastIndex = GetChildCount() - 1;
1698
1699 for (;;)
1700 {
1701 wxPGProperty* p = Item(i);
d665918b 1702 if ( p->m_name == name )
1c4293cb
VZ
1703 return p;
1704
1705 if ( i == lastIndex )
1706 break;
1707
1708 i++;
1709 if ( i == GetChildCount() )
1710 i = 0;
1711 };
1712
1713 return NULL;
1714}
1715
1716int wxPGProperty::GetChildrenHeight( int lh, int iMax_ ) const
1717{
1718 // Returns height of children, recursively, and
1719 // by taking expanded/collapsed status into account.
1720 //
1721 // iMax is used when finding property y-positions.
1722 //
1723 unsigned int i = 0;
1724 int h = 0;
1725
1726 if ( iMax_ == -1 )
1727 iMax_ = GetChildCount();
1728
1729 unsigned int iMax = iMax_;
1730
1731 wxASSERT( iMax <= GetChildCount() );
1732
1733 if ( !IsExpanded() && GetParent() )
1734 return 0;
1735
1736 while ( i < iMax )
1737 {
1738 wxPGProperty* pwc = (wxPGProperty*) Item(i);
1739
1740 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1741 {
1742 if ( !pwc->IsExpanded() ||
1743 pwc->GetChildCount() == 0 )
1744 h += lh;
1745 else
1746 h += pwc->GetChildrenHeight(lh) + lh;
1747 }
1748
1749 i++;
1750 }
1751
1752 return h;
1753}
1754
1755wxPGProperty* wxPGProperty::GetItemAtY( unsigned int y, unsigned int lh, unsigned int* nextItemY ) const
1756{
1757 wxASSERT( nextItemY );
1758
1759 // Linear search at the moment
1760 //
1761 // nextItemY = y of next visible property, final value will be written back.
1762 wxPGProperty* result = NULL;
1763 wxPGProperty* current = NULL;
1764 unsigned int iy = *nextItemY;
1765 unsigned int i = 0;
1766 unsigned int iMax = GetChildCount();
1767
1768 while ( i < iMax )
1769 {
1770 wxPGProperty* pwc = Item(i);
1771
1772 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1773 {
1774 // Found?
1775 if ( y < iy )
1776 {
1777 result = current;
1778 break;
1779 }
1780
1781 iy += lh;
1782
1783 if ( pwc->IsExpanded() &&
1784 pwc->GetChildCount() > 0 )
1785 {
1786 result = (wxPGProperty*) pwc->GetItemAtY( y, lh, &iy );
1787 if ( result )
1788 break;
1789 }
1790
1791 current = pwc;
1792 }
1793
1794 i++;
1795 }
1796
1797 // Found?
1798 if ( !result && y < iy )
1799 result = current;
1800
1801 *nextItemY = iy;
1802
1803 /*
1804 if ( current )
1805 wxLogDebug(wxT("%s::GetItemAtY(%i) -> %s"),this->GetLabel().c_str(),y,current->GetLabel().c_str());
1806 else
1807 wxLogDebug(wxT("%s::GetItemAtY(%i) -> NULL"),this->GetLabel().c_str(),y);
1808 */
1809
1810 return (wxPGProperty*) result;
1811}
1812
1813void wxPGProperty::Empty()
1814{
1815 size_t i;
1816 if ( !HasFlag(wxPG_PROP_CHILDREN_ARE_COPIES) )
1817 {
1818 for ( i=0; i<GetChildCount(); i++ )
1819 {
f7a094e1 1820 delete m_children[i];
1c4293cb
VZ
1821 }
1822 }
1823
f7a094e1 1824 m_children.clear();
1c4293cb
VZ
1825}
1826
1827void wxPGProperty::ChildChanged( wxVariant& WXUNUSED(thisValue),
1828 int WXUNUSED(childIndex),
1829 wxVariant& WXUNUSED(childValue) ) const
1830{
1831}
1832
1833bool wxPGProperty::AreAllChildrenSpecified( wxVariant* pendingList ) const
1834{
1835 unsigned int i;
1836
1837 const wxVariantList* pList = NULL;
1838 wxVariantList::const_iterator node;
1839
1840 if ( pendingList )
1841 {
1842 pList = &pendingList->GetList();
1843 node = pList->begin();
1844 }
1845
1c4293cb
VZ
1846 for ( i=0; i<GetChildCount(); i++ )
1847 {
1848 wxPGProperty* child = Item(i);
1849 const wxVariant* listValue = NULL;
1850 wxVariant value;
1851
1852 if ( pendingList )
1853 {
d665918b 1854 const wxString& childName = child->GetBaseName();
1c4293cb
VZ
1855
1856 for ( ; node != pList->end(); node++ )
1857 {
1858 const wxVariant& item = *((const wxVariant*)*node);
d665918b 1859 if ( item.GetName() == childName )
1c4293cb
VZ
1860 {
1861 listValue = &item;
1862 value = item;
1863 break;
1864 }
1865 }
1866 }
1867
1868 if ( !listValue )
1869 value = child->GetValue();
1870
1871 if ( value.IsNull() )
1872 return false;
1873
1874 // Check recursively
1875 if ( child->GetChildCount() )
1876 {
1877 const wxVariant* childList = NULL;
1878
0372d42e 1879 if ( listValue && listValue->GetType() == wxPG_VARIANT_TYPE_LIST )
1c4293cb
VZ
1880 childList = listValue;
1881
1882 if ( !child->AreAllChildrenSpecified((wxVariant*)childList) )
1883 return false;
1884 }
1885 }
1886
1887 return true;
1888}
1889
1890wxPGProperty* wxPGProperty::UpdateParentValues()
1891{
1892 wxPGProperty* parent = m_parent;
1893 if ( parent && parent->HasFlag(wxPG_PROP_COMPOSED_VALUE) &&
1894 !parent->IsCategory() && !parent->IsRoot() )
1895 {
1896 wxString s;
1897 parent->GenerateComposedValue(s, 0);
1898 parent->m_value = s;
1899 return parent->UpdateParentValues();
1900 }
1901 return this;
1902}
1903
1904bool wxPGProperty::IsTextEditable() const
1905{
1906 if ( HasFlag(wxPG_PROP_READONLY) )
1907 return false;
1908
1909 if ( HasFlag(wxPG_PROP_NOEDITOR) &&
1910 (GetChildCount() ||
1911 wxString(GetEditorClass()->GetClassInfo()->GetClassName()).EndsWith(wxS("Button")))
1912 )
1913 return false;
1914
1915 return true;
1916}
1917
1918// Call for after sub-properties added with AddChild
1919void wxPGProperty::PrepareSubProperties()
1920{
1921 wxPropertyGridPageState* state = GetParentState();
1922
1923 wxASSERT(state);
1924
1925 if ( !GetChildCount() )
1926 return;
1927
1928 wxByte depth = m_depth + 1;
1929 wxByte depthBgCol = m_depthBgCol;
1930
1931 FlagType inheritFlags = m_flags & wxPG_INHERITED_PROPFLAGS;
1932
1933 wxByte bgColIndex = m_bgColIndex;
1934 wxByte fgColIndex = m_fgColIndex;
1935
1936 //
1937 // Set some values to the children
1938 //
1939 size_t i = 0;
1940 wxPGProperty* nparent = this;
1941
1942 while ( i < nparent->GetChildCount() )
1943 {
1944 wxPGProperty* np = nparent->Item(i);
1945
1946 np->m_parentState = state;
1947 np->m_flags |= inheritFlags; // Hideable also if parent.
1948 np->m_depth = depth;
1949 np->m_depthBgCol = depthBgCol;
1950 np->m_bgColIndex = bgColIndex;
1951 np->m_fgColIndex = fgColIndex;
1952
1953 // Also handle children of children
1954 if ( np->GetChildCount() > 0 )
1955 {
1956 nparent = np;
1957 i = 0;
1958
1959 // Init
1960 nparent->SetParentalType(wxPG_PROP_AGGREGATE);
1961 nparent->SetExpanded(false);
1962 depth++;
1963 }
1964 else
1965 {
1966 // Next sibling
1967 i++;
1968 }
1969
1970 // After reaching last sibling, go back to processing
1971 // siblings of the parent
1972 while ( i >= nparent->GetChildCount() )
1973 {
1974 // Exit the loop when top parent hit
1975 if ( nparent == this )
1976 break;
1977
1978 depth--;
1979
b96a14e3 1980 i = nparent->GetIndexInParent() + 1;
1c4293cb
VZ
1981 nparent = nparent->GetParent();
1982 }
1983 }
1984}
1985
1986// Call after fixed sub-properties added/removed after creation.
1987// if oldSelInd >= 0 and < new max items, then selection is
1988// moved to it. Note: oldSelInd -2 indicates that this property
1989// should be selected.
1990void wxPGProperty::SubPropsChanged( int oldSelInd )
1991{
1992 wxPropertyGridPageState* state = GetParentState();
1993 wxPropertyGrid* grid = state->GetGrid();
1994
1995 PrepareSubProperties();
1996
1997 wxPGProperty* sel = (wxPGProperty*) NULL;
f7a094e1
JS
1998 if ( oldSelInd >= (int)m_children.size() )
1999 oldSelInd = (int)m_children.size() - 1;
1c4293cb
VZ
2000
2001 if ( oldSelInd >= 0 )
f7a094e1 2002 sel = m_children[oldSelInd];
1c4293cb
VZ
2003 else if ( oldSelInd == -2 )
2004 sel = this;
2005
2006 if ( sel )
2007 state->DoSelectProperty(sel);
2008
2009 if ( state == grid->GetState() )
2010 {
2011 grid->GetPanel()->Refresh();
2012 }
2013}
2014
2015// -----------------------------------------------------------------------
2016// wxPGRootProperty
2017// -----------------------------------------------------------------------
2018
2019WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPGRootProperty,none,TextCtrl)
2020IMPLEMENT_DYNAMIC_CLASS(wxPGRootProperty, wxPGProperty)
2021
2022
2023wxPGRootProperty::wxPGRootProperty()
2024 : wxPGProperty()
2025{
2026#ifdef __WXDEBUG__
2027 m_name = wxS("<root>");
2028#endif
2029 SetParentalType(0);
2030 m_depth = 0;
2031}
2032
2033
2034wxPGRootProperty::~wxPGRootProperty()
2035{
2036}
2037
2038
2039// -----------------------------------------------------------------------
2040// wxPropertyCategory
2041// -----------------------------------------------------------------------
2042
2043WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPropertyCategory,none,TextCtrl)
2044IMPLEMENT_DYNAMIC_CLASS(wxPropertyCategory, wxPGProperty)
2045
2046void wxPropertyCategory::Init()
2047{
2048 // don't set colour - prepareadditem method should do this
2049 SetParentalType(wxPG_PROP_CATEGORY);
2050 m_capFgColIndex = 1;
2051 m_textExtent = -1;
2052}
2053
2054wxPropertyCategory::wxPropertyCategory()
2055 : wxPGProperty()
2056{
2057 Init();
2058}
2059
2060
2061wxPropertyCategory::wxPropertyCategory( const wxString &label, const wxString& name )
2062 : wxPGProperty(label,name)
2063{
2064 Init();
2065}
2066
2067
2068wxPropertyCategory::~wxPropertyCategory()
2069{
2070}
2071
2072
2073wxString wxPropertyCategory::GetValueAsString( int ) const
2074{
2075 return wxEmptyString;
2076}
2077
2078int wxPropertyCategory::GetTextExtent( const wxWindow* wnd, const wxFont& font ) const
2079{
2080 if ( m_textExtent > 0 )
2081 return m_textExtent;
2082 int x = 0, y = 0;
2083 ((wxWindow*)wnd)->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2084 return x;
2085}
2086
2087void wxPropertyCategory::CalculateTextExtent( wxWindow* wnd, const wxFont& font )
2088{
2089 int x = 0, y = 0;
2090 wnd->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2091 m_textExtent = x;
2092}
2093
2094// -----------------------------------------------------------------------
2095// wxPGAttributeStorage
2096// -----------------------------------------------------------------------
2097
2098wxPGAttributeStorage::wxPGAttributeStorage()
2099{
2100}
2101
2102wxPGAttributeStorage::~wxPGAttributeStorage()
2103{
2104 wxPGHashMapS2P::iterator it;
2105
2106 for ( it = m_map.begin(); it != m_map.end(); it++ )
2107 {
2108 wxVariantData* data = (wxVariantData*) it->second;
2109 data->DecRef();
2110 }
2111}
2112
2113void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value )
2114{
2115 wxVariantData* data = value.GetData();
2116
2117 // Free old, if any
2118 wxPGHashMapS2P::iterator it = m_map.find(name);
2119 if ( it != m_map.end() )
1802a91d 2120 {
1c4293cb
VZ
2121 ((wxVariantData*)it->second)->DecRef();
2122
1802a91d
JS
2123 if ( !data )
2124 {
2125 // If Null variant, just remove from set
2126 m_map.erase(it);
2127 return;
2128 }
2129 }
2130
1c4293cb 2131 if ( data )
1802a91d 2132 {
1c4293cb
VZ
2133 data->IncRef();
2134
1802a91d
JS
2135 m_map[name] = data;
2136 }
1c4293cb
VZ
2137}
2138
f4bc1aa2 2139#endif // wxUSE_PROPGRID