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