Do not incorrectly interprete StringToValue() returning false to mean that it failed
[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 oldChildValue = child->GetValue();
778 wxVariant variant(oldChildValue);
779 bool stvRes = child->StringToValue( variant, token, propagatedFlags );
780 if ( stvRes || (variant != oldChildValue) )
781 {
782 if ( stvRes )
783 changed = true;
784 }
785 else
786 {
787 // Failed, becomes unspecified
788 variant.MakeNull();
789 changed = true;
790 }
791
792 variant.SetName(child->GetBaseName());
793 list.Append(variant);
794
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
837 bool 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
846 bool 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
855 wxSize 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
863 wxPGCellRenderer* wxPGProperty::GetCellRenderer( int WXUNUSED(column) ) const
864 {
865 return wxPGGlobalVars->m_defaultRenderer;
866 }
867
868 void 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
881 const wxPGEditor* wxPGProperty::DoGetEditorClass() const
882 {
883 return wxPG_EDITOR(TextCtrl);
884 }
885
886 // Default extra property event handling - that is, none at all.
887 bool wxPGProperty::OnEvent( wxPropertyGrid*, wxWindow*, wxEvent& )
888 {
889 return false;
890 }
891
892
893 void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
894 {
895 if ( !value.IsNull() )
896 {
897 wxVariant tempListVariant;
898
899 SetCommonValue(-1);
900 // List variants are reserved a special purpose
901 // as intermediate containers for child values
902 // of properties with children.
903 if ( value.GetType() == wxPG_VARIANT_TYPE_LIST )
904 {
905 //
906 // However, situation is different for composed string properties
907 if ( HasFlag(wxPG_PROP_COMPOSED_VALUE) )
908 {
909 tempListVariant = value;
910 pList = &tempListVariant;
911 }
912
913 wxVariant newValue;
914 AdaptListToValue(value, &newValue);
915 value = newValue;
916 //wxLogDebug(wxT(">> %s.SetValue() adapted list value to type '%s'"),GetName().c_str(),value.GetType().c_str());
917 }
918
919 if ( HasFlag( wxPG_PROP_AGGREGATE) )
920 flags |= wxPG_SETVAL_AGGREGATED;
921
922 if ( pList && !pList->IsNull() )
923 {
924 wxASSERT( pList->GetType() == wxPG_VARIANT_TYPE_LIST );
925 wxASSERT( GetChildCount() );
926 wxASSERT( !IsCategory() );
927
928 wxVariantList& list = pList->GetList();
929 wxVariantList::iterator node;
930 unsigned int i = 0;
931
932 //wxLogDebug(wxT(">> %s.SetValue() pList parsing"),GetName().c_str());
933
934 // Children in list can be in any order, but we will give hint to
935 // GetPropertyByNameWH(). This optimizes for full list parsing.
936 for ( node = list.begin(); node != list.end(); node++ )
937 {
938 wxVariant& childValue = *((wxVariant*)*node);
939 wxPGProperty* child = GetPropertyByNameWH(childValue.GetName(), i);
940 if ( child )
941 {
942 //wxLogDebug(wxT("%i: child = %s, childValue.GetType()=%s"),i,child->GetBaseName().c_str(),childValue.GetType().c_str());
943 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
944 {
945 if ( child->HasFlag(wxPG_PROP_AGGREGATE) && !(flags & wxPG_SETVAL_AGGREGATED) )
946 {
947 wxVariant listRefCopy = childValue;
948 child->SetValue(childValue, &listRefCopy, flags|wxPG_SETVAL_FROM_PARENT);
949 }
950 else
951 {
952 wxVariant oldVal = child->GetValue();
953 child->SetValue(oldVal, &childValue, flags|wxPG_SETVAL_FROM_PARENT);
954 }
955 }
956 else if ( child->GetValue() != childValue )
957 {
958 // For aggregate properties, we will trust RefreshChildren()
959 // to update child values.
960 if ( !HasFlag(wxPG_PROP_AGGREGATE) )
961 child->SetValue(childValue, NULL, flags|wxPG_SETVAL_FROM_PARENT);
962 if ( flags & wxPG_SETVAL_BY_USER )
963 child->SetFlag(wxPG_PROP_MODIFIED);
964 }
965 }
966 i++;
967 }
968 }
969
970 if ( !value.IsNull() )
971 {
972 m_value = value;
973 OnSetValue();
974
975 if ( !(flags & wxPG_SETVAL_FROM_PARENT) )
976 UpdateParentValues();
977 }
978
979 if ( flags & wxPG_SETVAL_BY_USER )
980 SetFlag(wxPG_PROP_MODIFIED);
981
982 if ( HasFlag(wxPG_PROP_AGGREGATE) )
983 RefreshChildren();
984 }
985 else
986 {
987 if ( m_commonValue != -1 )
988 {
989 wxPropertyGrid* pg = GetGrid();
990 if ( !pg || m_commonValue != pg->GetUnspecifiedCommonValue() )
991 SetCommonValue(-1);
992 }
993
994 m_value = value;
995
996 // Set children to unspecified, but only if aggregate or
997 // value is <composed>
998 if ( AreChildrenComponents() )
999 {
1000 unsigned int i;
1001 for ( i=0; i<GetChildCount(); i++ )
1002 Item(i)->SetValue(value, NULL, flags|wxPG_SETVAL_FROM_PARENT);
1003 }
1004 }
1005
1006 //
1007 // Update editor control
1008 //
1009
1010 // We need to check for these, otherwise GetGrid() may fail.
1011 if ( flags & wxPG_SETVAL_REFRESH_EDITOR )
1012 RefreshEditor();
1013 }
1014
1015
1016 void wxPGProperty::SetValueInEvent( wxVariant value ) const
1017 {
1018 GetGrid()->ValueChangeInEvent(value);
1019 }
1020
1021 void wxPGProperty::SetFlagRecursively( FlagType flag, bool set )
1022 {
1023 if ( set )
1024 SetFlag(flag);
1025 else
1026 ClearFlag(flag);
1027
1028 unsigned int i;
1029 for ( i = 0; i < GetChildCount(); i++ )
1030 Item(i)->SetFlagRecursively(flag, set);
1031 }
1032
1033 void wxPGProperty::RefreshEditor()
1034 {
1035 if ( m_parent && GetParentState() )
1036 {
1037 wxPropertyGrid* pg = GetParentState()->GetGrid();
1038 if ( pg->GetSelectedProperty() == this )
1039 {
1040 wxWindow* editor = pg->GetEditorControl();
1041 if ( editor )
1042 GetEditorClass()->UpdateControl( this, editor );
1043 }
1044 }
1045 }
1046
1047
1048 wxVariant wxPGProperty::GetDefaultValue() const
1049 {
1050 wxVariant defVal = GetAttribute(wxS("DefaultValue"));
1051 if ( !defVal.IsNull() )
1052 return defVal;
1053
1054 wxVariant value = GetValue();
1055
1056 if ( !value.IsNull() )
1057 {
1058 wxString valueType(value.GetType());
1059
1060 if ( valueType == wxPG_VARIANT_TYPE_LONG )
1061 return wxPGVariant_Zero;
1062 if ( valueType == wxPG_VARIANT_TYPE_STRING )
1063 return wxPGVariant_EmptyString;
1064 if ( valueType == wxPG_VARIANT_TYPE_BOOL )
1065 return wxPGVariant_False;
1066 if ( valueType == wxPG_VARIANT_TYPE_DOUBLE )
1067 return wxVariant(0.0);
1068 if ( valueType == wxPG_VARIANT_TYPE_ARRSTRING )
1069 return wxVariant(wxArrayString());
1070 if ( valueType == wxS("wxLongLong") )
1071 return WXVARIANT(wxLongLong(0));
1072 if ( valueType == wxS("wxULongLong") )
1073 return WXVARIANT(wxULongLong(0));
1074 if ( valueType == wxS("wxColour") )
1075 return WXVARIANT(*wxBLACK);
1076 #if wxUSE_DATETIME
1077 if ( valueType == wxPG_VARIANT_TYPE_DATETIME )
1078 return wxVariant(wxDateTime::Now());
1079 #endif
1080 if ( valueType == wxS("wxFont") )
1081 return WXVARIANT(*wxNORMAL_FONT);
1082 if ( valueType == wxS("wxPoint") )
1083 return WXVARIANT(wxPoint(0, 0));
1084 if ( valueType == wxS("wxSize") )
1085 return WXVARIANT(wxSize(0, 0));
1086 }
1087
1088 return wxVariant();
1089 }
1090
1091 void wxPGProperty::SetCell( int column, wxPGCell* cellObj )
1092 {
1093 if ( column >= (int)m_cells.size() )
1094 m_cells.SetCount(column+1, NULL);
1095
1096 delete (wxPGCell*) m_cells[column];
1097 m_cells[column] = cellObj;
1098 }
1099
1100 void wxPGProperty::SetChoiceSelection( int newValue, const wxPGChoiceInfo& choiceInfo )
1101 {
1102 // Changes value of a property with choices, but only
1103 // works if the value type is long or string.
1104 wxString ts = GetValue().GetType();
1105
1106 wxCHECK_RET( choiceInfo.m_choices, wxT("invalid choiceinfo") );
1107
1108 if ( ts == wxS("long") )
1109 {
1110 SetValue( (long) newValue );
1111 }
1112 else if ( ts == wxS("string") )
1113 {
1114 SetValue( choiceInfo.m_choices->GetLabel(newValue) );
1115 }
1116 }
1117
1118
1119 wxString wxPGProperty::GetChoiceString( unsigned int index )
1120 {
1121 wxPGChoiceInfo ci;
1122 GetChoiceInfo(&ci);
1123 wxASSERT(ci.m_choices);
1124 return ci.m_choices->GetLabel(index);
1125 }
1126
1127 int wxPGProperty::InsertChoice( const wxString& label, int index, int value )
1128 {
1129 wxPropertyGrid* pg = GetGrid();
1130
1131 wxPGChoiceInfo ci;
1132 ci.m_choices = (wxPGChoices*) NULL;
1133 int sel = GetChoiceInfo(&ci);
1134
1135 if ( ci.m_choices )
1136 {
1137 int newSel = sel;
1138
1139 if ( index < 0 )
1140 index = ci.m_choices->GetCount();
1141
1142 if ( index <= sel )
1143 newSel++;
1144
1145 ci.m_choices->Insert(label, index, value);
1146
1147 if ( sel != newSel )
1148 SetChoiceSelection(newSel, ci);
1149
1150 if ( this == pg->GetSelection() )
1151 GetEditorClass()->InsertItem(pg->GetEditorControl(),label,index);
1152
1153 return index;
1154 }
1155
1156 return -1;
1157 }
1158
1159
1160 void wxPGProperty::DeleteChoice( int index )
1161 {
1162 wxPropertyGrid* pg = GetGrid();
1163
1164 wxPGChoiceInfo ci;
1165 ci.m_choices = (wxPGChoices*) NULL;
1166 int sel = GetChoiceInfo(&ci);
1167
1168 if ( ci.m_choices )
1169 {
1170 int newSel = sel;
1171
1172 // Adjust current value
1173 if ( sel == index )
1174 {
1175 SetValueToUnspecified();
1176 newSel = 0;
1177 }
1178 else if ( index < sel )
1179 {
1180 newSel--;
1181 }
1182
1183 ci.m_choices->RemoveAt(index);
1184
1185 if ( sel != newSel )
1186 SetChoiceSelection(newSel, ci);
1187
1188 if ( this == pg->GetSelection() )
1189 GetEditorClass()->DeleteItem(pg->GetEditorControl(), index);
1190 }
1191 }
1192
1193 int wxPGProperty::GetChoiceInfo( wxPGChoiceInfo* WXUNUSED(info) )
1194 {
1195 return -1;
1196 }
1197
1198 wxPGEditorDialogAdapter* wxPGProperty::GetEditorDialog() const
1199 {
1200 return NULL;
1201 }
1202
1203 bool wxPGProperty::DoSetAttribute( const wxString& WXUNUSED(name), wxVariant& WXUNUSED(value) )
1204 {
1205 return false;
1206 }
1207
1208 void wxPGProperty::SetAttribute( const wxString& name, wxVariant value )
1209 {
1210 if ( DoSetAttribute( name, value ) )
1211 {
1212 // Support working without grid, when possible
1213 if ( wxPGGlobalVars->HasExtraStyle( wxPG_EX_WRITEONLY_BUILTIN_ATTRIBUTES ) )
1214 return;
1215 }
1216
1217 m_attributes.Set( name, value );
1218 }
1219
1220 void wxPGProperty::SetAttributes( const wxPGAttributeStorage& attributes )
1221 {
1222 wxPGAttributeStorage::const_iterator it = attributes.StartIteration();
1223 wxVariant variant;
1224
1225 while ( attributes.GetNext(it, variant) )
1226 SetAttribute( variant.GetName(), variant );
1227 }
1228
1229 wxVariant wxPGProperty::DoGetAttribute( const wxString& WXUNUSED(name) ) const
1230 {
1231 return wxVariant();
1232 }
1233
1234
1235 wxVariant wxPGProperty::GetAttribute( const wxString& name ) const
1236 {
1237 return m_attributes.FindValue(name);
1238 }
1239
1240 wxString wxPGProperty::GetAttribute( const wxString& name, const wxString& defVal ) const
1241 {
1242 wxVariant variant = m_attributes.FindValue(name);
1243
1244 if ( !variant.IsNull() )
1245 return variant.GetString();
1246
1247 return defVal;
1248 }
1249
1250 long wxPGProperty::GetAttributeAsLong( const wxString& name, long defVal ) const
1251 {
1252 wxVariant variant = m_attributes.FindValue(name);
1253
1254 return wxPGVariantToInt(variant, defVal);
1255 }
1256
1257 double wxPGProperty::GetAttributeAsDouble( const wxString& name, double defVal ) const
1258 {
1259 double retVal;
1260 wxVariant variant = m_attributes.FindValue(name);
1261
1262 if ( wxPGVariantToDouble(variant, &retVal) )
1263 return retVal;
1264
1265 return defVal;
1266 }
1267
1268 wxVariant wxPGProperty::GetAttributesAsList() const
1269 {
1270 wxVariantList tempList;
1271 wxVariant v( tempList, wxString::Format(wxS("@%s@attr"),m_name.c_str()) );
1272
1273 wxPGAttributeStorage::const_iterator it = m_attributes.StartIteration();
1274 wxVariant variant;
1275
1276 while ( m_attributes.GetNext(it, variant) )
1277 v.Append(variant);
1278
1279 return v;
1280 }
1281
1282 // Slots of utility flags are NULL
1283 const unsigned int gs_propFlagToStringSize = 14;
1284
1285 static const wxChar* gs_propFlagToString[gs_propFlagToStringSize] = {
1286 NULL,
1287 wxT("DISABLED"),
1288 wxT("HIDDEN"),
1289 NULL,
1290 wxT("NOEDITOR"),
1291 wxT("COLLAPSED"),
1292 NULL,
1293 NULL,
1294 NULL,
1295 NULL,
1296 NULL,
1297 NULL,
1298 NULL,
1299 NULL
1300 };
1301
1302 wxString wxPGProperty::GetFlagsAsString( FlagType flagsMask ) const
1303 {
1304 wxString s;
1305 int relevantFlags = m_flags & flagsMask & wxPG_STRING_STORED_FLAGS;
1306 FlagType a = 1;
1307
1308 unsigned int i = 0;
1309 for ( i=0; i<gs_propFlagToStringSize; i++ )
1310 {
1311 if ( relevantFlags & a )
1312 {
1313 const wxChar* fs = gs_propFlagToString[i];
1314 wxASSERT(fs);
1315 if ( s.length() )
1316 s << wxS("|");
1317 s << fs;
1318 }
1319 a = a << 1;
1320 }
1321
1322 return s;
1323 }
1324
1325 void wxPGProperty::SetFlagsFromString( const wxString& str )
1326 {
1327 FlagType flags = 0;
1328
1329 WX_PG_TOKENIZER1_BEGIN(str, wxS('|'))
1330 unsigned int i;
1331 for ( i=0; i<gs_propFlagToStringSize; i++ )
1332 {
1333 const wxChar* fs = gs_propFlagToString[i];
1334 if ( fs && str == fs )
1335 {
1336 flags |= (1<<i);
1337 break;
1338 }
1339 }
1340 WX_PG_TOKENIZER1_END()
1341
1342 m_flags = (m_flags & ~wxPG_STRING_STORED_FLAGS) | flags;
1343 }
1344
1345 wxValidator* wxPGProperty::DoGetValidator() const
1346 {
1347 return (wxValidator*) NULL;
1348 }
1349
1350 wxPGChoices& wxPGProperty::GetChoices()
1351 {
1352 wxPGChoiceInfo choiceInfo;
1353 choiceInfo.m_choices = NULL;
1354 GetChoiceInfo(&choiceInfo);
1355 return *choiceInfo.m_choices;
1356 }
1357
1358 const wxPGChoices& wxPGProperty::GetChoices() const
1359 {
1360 return (const wxPGChoices&) ((wxPGProperty*)this)->GetChoices();
1361 }
1362
1363 unsigned int wxPGProperty::GetChoiceCount() const
1364 {
1365 const wxPGChoices& choices = GetChoices();
1366 if ( &choices && choices.IsOk() )
1367 return choices.GetCount();
1368 return 0;
1369 }
1370
1371 const wxPGChoiceEntry* wxPGProperty::GetCurrentChoice() const
1372 {
1373 wxPGChoiceInfo ci;
1374 ci.m_choices = (wxPGChoices*) NULL;
1375 int index = ((wxPGProperty*)this)->GetChoiceInfo(&ci);
1376 if ( index == -1 || !ci.m_choices || index >= (int)ci.m_choices->GetCount() )
1377 return NULL;
1378
1379 return &(*ci.m_choices)[index];
1380 }
1381
1382 bool wxPGProperty::SetChoices( wxPGChoices& choices )
1383 {
1384 wxPGChoiceInfo ci;
1385 ci.m_choices = (wxPGChoices*) NULL;
1386
1387 // Unref existing
1388 GetChoiceInfo(&ci);
1389 if ( ci.m_choices )
1390 {
1391 ci.m_choices->Assign(choices);
1392
1393 //if ( m_parent )
1394 {
1395 // This may be needed to trigger some initialization
1396 // (but don't do it if property is somewhat uninitialized)
1397 wxVariant defVal = GetDefaultValue();
1398 if ( defVal.IsNull() )
1399 return false;
1400
1401 SetValue(defVal);
1402
1403 return true;
1404 }
1405 }
1406 return false;
1407 }
1408
1409
1410 const wxPGEditor* wxPGProperty::GetEditorClass() const
1411 {
1412 const wxPGEditor* editor;
1413
1414 if ( !m_customEditor )
1415 {
1416 editor = DoGetEditorClass();
1417 }
1418 else
1419 editor = m_customEditor;
1420
1421 //
1422 // Maybe override editor if common value specified
1423 if ( GetDisplayedCommonValueCount() )
1424 {
1425 // TextCtrlAndButton -> ComboBoxAndButton
1426 if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlAndButtonEditor)) )
1427 editor = wxPG_EDITOR(ChoiceAndButton);
1428
1429 // TextCtrl -> ComboBox
1430 else if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlEditor)) )
1431 editor = wxPG_EDITOR(ComboBox);
1432 }
1433
1434 return editor;
1435 }
1436
1437
1438 // Privatizes set of choices
1439 void wxPGProperty::SetChoicesExclusive()
1440 {
1441 wxPGChoiceInfo ci;
1442 ci.m_choices = (wxPGChoices*) NULL;
1443
1444 GetChoiceInfo(&ci);
1445 if ( ci.m_choices )
1446 ci.m_choices->SetExclusive();
1447 }
1448
1449 bool wxPGProperty::HasVisibleChildren() const
1450 {
1451 unsigned int i;
1452
1453 for ( i=0; i<GetChildCount(); i++ )
1454 {
1455 wxPGProperty* child = Item(i);
1456
1457 if ( !child->HasFlag(wxPG_PROP_HIDDEN) )
1458 return true;
1459 }
1460
1461 return false;
1462 }
1463
1464 bool wxPGProperty::PrepareValueForDialogEditing( wxPropertyGrid* propGrid )
1465 {
1466 return propGrid->EditorValidate();
1467 }
1468
1469
1470 bool wxPGProperty::RecreateEditor()
1471 {
1472 wxPropertyGrid* pg = GetGrid();
1473 wxASSERT(pg);
1474
1475 wxPGProperty* selected = pg->GetSelection();
1476 if ( this == selected )
1477 {
1478 pg->DoSelectProperty(this, wxPG_SEL_FORCE);
1479 return true;
1480 }
1481 return false;
1482 }
1483
1484
1485 void wxPGProperty::SetValueImage( wxBitmap& bmp )
1486 {
1487 delete m_valueBitmap;
1488
1489 if ( &bmp && bmp.Ok() )
1490 {
1491 // Resize the image
1492 wxSize maxSz = GetGrid()->GetImageSize();
1493 wxSize imSz(bmp.GetWidth(),bmp.GetHeight());
1494
1495 if ( imSz.x != maxSz.x || imSz.y != maxSz.y )
1496 {
1497 // Create a memory DC
1498 wxBitmap* bmpNew = new wxBitmap(maxSz.x,maxSz.y,bmp.GetDepth());
1499
1500 wxMemoryDC dc;
1501 dc.SelectObject(*bmpNew);
1502
1503 // Scale
1504 // FIXME: This is ugly - use image or wait for scaling patch.
1505 double scaleX = (double)maxSz.x / (double)imSz.x;
1506 double scaleY = (double)maxSz.y / (double)imSz.y;
1507
1508 dc.SetUserScale(scaleX,scaleY);
1509
1510 dc.DrawBitmap( bmp, 0, 0 );
1511
1512 m_valueBitmap = bmpNew;
1513 }
1514 else
1515 {
1516 m_valueBitmap = new wxBitmap(bmp);
1517 }
1518
1519 m_flags |= wxPG_PROP_CUSTOMIMAGE;
1520 }
1521 else
1522 {
1523 m_valueBitmap = NULL;
1524 m_flags &= ~(wxPG_PROP_CUSTOMIMAGE);
1525 }
1526 }
1527
1528
1529 wxPGProperty* wxPGProperty::GetMainParent() const
1530 {
1531 const wxPGProperty* curChild = this;
1532 const wxPGProperty* curParent = m_parent;
1533
1534 while ( curParent && !curParent->IsCategory() )
1535 {
1536 curChild = curParent;
1537 curParent = curParent->m_parent;
1538 }
1539
1540 return (wxPGProperty*) curChild;
1541 }
1542
1543
1544 const wxPGProperty* wxPGProperty::GetLastVisibleSubItem() const
1545 {
1546 //
1547 // Returns last visible sub-item, recursively.
1548 if ( !IsExpanded() || !GetChildCount() )
1549 return this;
1550
1551 return Last()->GetLastVisibleSubItem();
1552 }
1553
1554
1555 bool wxPGProperty::IsVisible() const
1556 {
1557 const wxPGProperty* parent;
1558
1559 if ( HasFlag(wxPG_PROP_HIDDEN) )
1560 return false;
1561
1562 for ( parent = GetParent(); parent != NULL; parent = parent->GetParent() )
1563 {
1564 if ( !parent->IsExpanded() || parent->HasFlag(wxPG_PROP_HIDDEN) )
1565 return false;
1566 }
1567
1568 return true;
1569 }
1570
1571 wxPropertyGrid* wxPGProperty::GetGridIfDisplayed() const
1572 {
1573 wxPropertyGridPageState* state = GetParentState();
1574 wxPropertyGrid* propGrid = state->GetGrid();
1575 if ( state == propGrid->GetState() )
1576 return propGrid;
1577 return NULL;
1578 }
1579
1580
1581 int wxPGProperty::GetY2( int lh ) const
1582 {
1583 const wxPGProperty* parent;
1584 const wxPGProperty* child = this;
1585
1586 int y = 0;
1587
1588 for ( parent = GetParent(); parent != NULL; parent = child->GetParent() )
1589 {
1590 if ( !parent->IsExpanded() )
1591 return -1;
1592 y += parent->GetChildrenHeight(lh, child->GetIndexInParent());
1593 y += lh;
1594 child = parent;
1595 }
1596
1597 y -= lh; // need to reduce one level
1598
1599 return y;
1600 }
1601
1602
1603 int wxPGProperty::GetY() const
1604 {
1605 return GetY2(GetGrid()->GetRowHeight());
1606 }
1607
1608 // This is used by Insert etc.
1609 void wxPGProperty::AddChild2( wxPGProperty* prop, int index, bool correct_mode )
1610 {
1611 if ( index < 0 || (size_t)index >= m_children.GetCount() )
1612 {
1613 if ( correct_mode ) prop->m_arrIndex = m_children.GetCount();
1614 m_children.Add( prop );
1615 }
1616 else
1617 {
1618 m_children.Insert( prop, index );
1619 if ( correct_mode ) FixIndexesOfChildren( index );
1620 }
1621
1622 prop->m_parent = this;
1623 }
1624
1625 // This is used by properties that have fixed sub-properties
1626 void wxPGProperty::AddChild( wxPGProperty* prop )
1627 {
1628 wxASSERT_MSG( prop->GetBaseName().length(),
1629 "Property's children must have unique, non-empty names within their scope" );
1630
1631 prop->m_arrIndex = m_children.GetCount();
1632 m_children.Add( prop );
1633
1634 int custImgHeight = prop->OnMeasureImage().y;
1635 if ( custImgHeight < 0 /*|| custImgHeight > 1*/ )
1636 prop->m_flags |= wxPG_PROP_CUSTOMIMAGE;
1637
1638 prop->m_parent = this;
1639 }
1640
1641
1642 void wxPGProperty::AdaptListToValue( wxVariant& list, wxVariant* value ) const
1643 {
1644 wxASSERT( GetChildCount() );
1645 wxASSERT( !IsCategory() );
1646
1647 *value = GetValue();
1648
1649 if ( !list.GetCount() )
1650 return;
1651
1652 wxASSERT( GetChildCount() >= (unsigned int)list.GetCount() );
1653
1654 bool allChildrenSpecified;
1655
1656 // Don't fully update aggregate properties unless all children have
1657 // specified value
1658 if ( HasFlag(wxPG_PROP_AGGREGATE) )
1659 allChildrenSpecified = AreAllChildrenSpecified(&list);
1660 else
1661 allChildrenSpecified = true;
1662
1663 wxVariant childValue = list[0];
1664 unsigned int i;
1665 unsigned int n = 0;
1666
1667 //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
1668
1669 for ( i=0; i<GetChildCount(); i++ )
1670 {
1671 const wxPGProperty* child = Item(i);
1672
1673 if ( childValue.GetName() == child->GetBaseName() )
1674 {
1675 //wxLogDebug(wxT(" %s(n=%i), %s"),childValue.GetName().c_str(),n,childValue.GetType().c_str());
1676
1677 if ( childValue.GetType() == wxPG_VARIANT_TYPE_LIST )
1678 {
1679 wxVariant cv2(child->GetValue());
1680 child->AdaptListToValue(childValue, &cv2);
1681 childValue = cv2;
1682 }
1683
1684 if ( allChildrenSpecified )
1685 ChildChanged(*value, i, childValue);
1686 n++;
1687 if ( n == (unsigned int)list.GetCount() )
1688 break;
1689 childValue = list[n];
1690 }
1691 }
1692 }
1693
1694
1695 void wxPGProperty::FixIndexesOfChildren( size_t starthere )
1696 {
1697 size_t i;
1698 for ( i=starthere;i<GetChildCount();i++)
1699 Item(i)->m_arrIndex = i;
1700 }
1701
1702
1703 // Returns (direct) child property with given name (or NULL if not found)
1704 wxPGProperty* wxPGProperty::GetPropertyByName( const wxString& name ) const
1705 {
1706 size_t i;
1707
1708 for ( i=0; i<GetChildCount(); i++ )
1709 {
1710 wxPGProperty* p = Item(i);
1711 if ( p->m_name == name )
1712 return p;
1713 }
1714
1715 // Does it have point, then?
1716 int pos = name.Find(wxS('.'));
1717 if ( pos <= 0 )
1718 return (wxPGProperty*) NULL;
1719
1720 wxPGProperty* p = GetPropertyByName(name. substr(0,pos));
1721
1722 if ( !p || !p->GetChildCount() )
1723 return NULL;
1724
1725 return p->GetPropertyByName(name.substr(pos+1,name.length()-pos-1));
1726 }
1727
1728 wxPGProperty* wxPGProperty::GetPropertyByNameWH( const wxString& name, unsigned int hintIndex ) const
1729 {
1730 unsigned int i = hintIndex;
1731
1732 if ( i >= GetChildCount() )
1733 i = 0;
1734
1735 unsigned int lastIndex = i - 1;
1736
1737 if ( lastIndex >= GetChildCount() )
1738 lastIndex = GetChildCount() - 1;
1739
1740 for (;;)
1741 {
1742 wxPGProperty* p = Item(i);
1743 if ( p->m_name == name )
1744 return p;
1745
1746 if ( i == lastIndex )
1747 break;
1748
1749 i++;
1750 if ( i == GetChildCount() )
1751 i = 0;
1752 };
1753
1754 return NULL;
1755 }
1756
1757 int wxPGProperty::GetChildrenHeight( int lh, int iMax_ ) const
1758 {
1759 // Returns height of children, recursively, and
1760 // by taking expanded/collapsed status into account.
1761 //
1762 // iMax is used when finding property y-positions.
1763 //
1764 unsigned int i = 0;
1765 int h = 0;
1766
1767 if ( iMax_ == -1 )
1768 iMax_ = GetChildCount();
1769
1770 unsigned int iMax = iMax_;
1771
1772 wxASSERT( iMax <= GetChildCount() );
1773
1774 if ( !IsExpanded() && GetParent() )
1775 return 0;
1776
1777 while ( i < iMax )
1778 {
1779 wxPGProperty* pwc = (wxPGProperty*) Item(i);
1780
1781 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1782 {
1783 if ( !pwc->IsExpanded() ||
1784 pwc->GetChildCount() == 0 )
1785 h += lh;
1786 else
1787 h += pwc->GetChildrenHeight(lh) + lh;
1788 }
1789
1790 i++;
1791 }
1792
1793 return h;
1794 }
1795
1796 wxPGProperty* wxPGProperty::GetItemAtY( unsigned int y, unsigned int lh, unsigned int* nextItemY ) const
1797 {
1798 wxASSERT( nextItemY );
1799
1800 // Linear search at the moment
1801 //
1802 // nextItemY = y of next visible property, final value will be written back.
1803 wxPGProperty* result = NULL;
1804 wxPGProperty* current = NULL;
1805 unsigned int iy = *nextItemY;
1806 unsigned int i = 0;
1807 unsigned int iMax = GetChildCount();
1808
1809 while ( i < iMax )
1810 {
1811 wxPGProperty* pwc = Item(i);
1812
1813 if ( !pwc->HasFlag(wxPG_PROP_HIDDEN) )
1814 {
1815 // Found?
1816 if ( y < iy )
1817 {
1818 result = current;
1819 break;
1820 }
1821
1822 iy += lh;
1823
1824 if ( pwc->IsExpanded() &&
1825 pwc->GetChildCount() > 0 )
1826 {
1827 result = (wxPGProperty*) pwc->GetItemAtY( y, lh, &iy );
1828 if ( result )
1829 break;
1830 }
1831
1832 current = pwc;
1833 }
1834
1835 i++;
1836 }
1837
1838 // Found?
1839 if ( !result && y < iy )
1840 result = current;
1841
1842 *nextItemY = iy;
1843
1844 /*
1845 if ( current )
1846 wxLogDebug(wxT("%s::GetItemAtY(%i) -> %s"),this->GetLabel().c_str(),y,current->GetLabel().c_str());
1847 else
1848 wxLogDebug(wxT("%s::GetItemAtY(%i) -> NULL"),this->GetLabel().c_str(),y);
1849 */
1850
1851 return (wxPGProperty*) result;
1852 }
1853
1854 void wxPGProperty::Empty()
1855 {
1856 size_t i;
1857 if ( !HasFlag(wxPG_PROP_CHILDREN_ARE_COPIES) )
1858 {
1859 for ( i=0; i<GetChildCount(); i++ )
1860 {
1861 wxPGProperty* p = (wxPGProperty*) Item(i);
1862 delete p;
1863 }
1864 }
1865
1866 m_children.Empty();
1867 }
1868
1869 void wxPGProperty::ChildChanged( wxVariant& WXUNUSED(thisValue),
1870 int WXUNUSED(childIndex),
1871 wxVariant& WXUNUSED(childValue) ) const
1872 {
1873 }
1874
1875 bool wxPGProperty::AreAllChildrenSpecified( wxVariant* pendingList ) const
1876 {
1877 unsigned int i;
1878
1879 const wxVariantList* pList = NULL;
1880 wxVariantList::const_iterator node;
1881
1882 if ( pendingList )
1883 {
1884 pList = &pendingList->GetList();
1885 node = pList->begin();
1886 }
1887
1888 for ( i=0; i<GetChildCount(); i++ )
1889 {
1890 wxPGProperty* child = Item(i);
1891 const wxVariant* listValue = NULL;
1892 wxVariant value;
1893
1894 if ( pendingList )
1895 {
1896 const wxString& childName = child->GetBaseName();
1897
1898 for ( ; node != pList->end(); node++ )
1899 {
1900 const wxVariant& item = *((const wxVariant*)*node);
1901 if ( item.GetName() == childName )
1902 {
1903 listValue = &item;
1904 value = item;
1905 break;
1906 }
1907 }
1908 }
1909
1910 if ( !listValue )
1911 value = child->GetValue();
1912
1913 if ( value.IsNull() )
1914 return false;
1915
1916 // Check recursively
1917 if ( child->GetChildCount() )
1918 {
1919 const wxVariant* childList = NULL;
1920
1921 if ( listValue && listValue->GetType() == wxPG_VARIANT_TYPE_LIST )
1922 childList = listValue;
1923
1924 if ( !child->AreAllChildrenSpecified((wxVariant*)childList) )
1925 return false;
1926 }
1927 }
1928
1929 return true;
1930 }
1931
1932 wxPGProperty* wxPGProperty::UpdateParentValues()
1933 {
1934 wxPGProperty* parent = m_parent;
1935 if ( parent && parent->HasFlag(wxPG_PROP_COMPOSED_VALUE) &&
1936 !parent->IsCategory() && !parent->IsRoot() )
1937 {
1938 wxString s;
1939 parent->GenerateComposedValue(s, 0);
1940 parent->m_value = s;
1941 return parent->UpdateParentValues();
1942 }
1943 return this;
1944 }
1945
1946 bool wxPGProperty::IsTextEditable() const
1947 {
1948 if ( HasFlag(wxPG_PROP_READONLY) )
1949 return false;
1950
1951 if ( HasFlag(wxPG_PROP_NOEDITOR) &&
1952 (GetChildCount() ||
1953 wxString(GetEditorClass()->GetClassInfo()->GetClassName()).EndsWith(wxS("Button")))
1954 )
1955 return false;
1956
1957 return true;
1958 }
1959
1960 // Call for after sub-properties added with AddChild
1961 void wxPGProperty::PrepareSubProperties()
1962 {
1963 wxPropertyGridPageState* state = GetParentState();
1964
1965 wxASSERT(state);
1966
1967 if ( !GetChildCount() )
1968 return;
1969
1970 wxByte depth = m_depth + 1;
1971 wxByte depthBgCol = m_depthBgCol;
1972
1973 FlagType inheritFlags = m_flags & wxPG_INHERITED_PROPFLAGS;
1974
1975 wxByte bgColIndex = m_bgColIndex;
1976 wxByte fgColIndex = m_fgColIndex;
1977
1978 //
1979 // Set some values to the children
1980 //
1981 size_t i = 0;
1982 wxPGProperty* nparent = this;
1983
1984 while ( i < nparent->GetChildCount() )
1985 {
1986 wxPGProperty* np = nparent->Item(i);
1987
1988 np->m_parentState = state;
1989 np->m_flags |= inheritFlags; // Hideable also if parent.
1990 np->m_depth = depth;
1991 np->m_depthBgCol = depthBgCol;
1992 np->m_bgColIndex = bgColIndex;
1993 np->m_fgColIndex = fgColIndex;
1994
1995 // Also handle children of children
1996 if ( np->GetChildCount() > 0 )
1997 {
1998 nparent = np;
1999 i = 0;
2000
2001 // Init
2002 nparent->SetParentalType(wxPG_PROP_AGGREGATE);
2003 nparent->SetExpanded(false);
2004 depth++;
2005 }
2006 else
2007 {
2008 // Next sibling
2009 i++;
2010 }
2011
2012 // After reaching last sibling, go back to processing
2013 // siblings of the parent
2014 while ( i >= nparent->GetChildCount() )
2015 {
2016 // Exit the loop when top parent hit
2017 if ( nparent == this )
2018 break;
2019
2020 depth--;
2021
2022 i = nparent->GetArrIndex() + 1;
2023 nparent = nparent->GetParent();
2024 }
2025 }
2026 }
2027
2028 // Call after fixed sub-properties added/removed after creation.
2029 // if oldSelInd >= 0 and < new max items, then selection is
2030 // moved to it. Note: oldSelInd -2 indicates that this property
2031 // should be selected.
2032 void wxPGProperty::SubPropsChanged( int oldSelInd )
2033 {
2034 wxPropertyGridPageState* state = GetParentState();
2035 wxPropertyGrid* grid = state->GetGrid();
2036
2037 PrepareSubProperties();
2038
2039 wxPGProperty* sel = (wxPGProperty*) NULL;
2040 if ( oldSelInd >= (int)m_children.GetCount() )
2041 oldSelInd = (int)m_children.GetCount() - 1;
2042
2043 if ( oldSelInd >= 0 )
2044 sel = (wxPGProperty*) m_children[oldSelInd];
2045 else if ( oldSelInd == -2 )
2046 sel = this;
2047
2048 if ( sel )
2049 state->DoSelectProperty(sel);
2050
2051 if ( state == grid->GetState() )
2052 {
2053 grid->GetPanel()->Refresh();
2054 }
2055 }
2056
2057 // -----------------------------------------------------------------------
2058 // wxPGRootProperty
2059 // -----------------------------------------------------------------------
2060
2061 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPGRootProperty,none,TextCtrl)
2062 IMPLEMENT_DYNAMIC_CLASS(wxPGRootProperty, wxPGProperty)
2063
2064
2065 wxPGRootProperty::wxPGRootProperty()
2066 : wxPGProperty()
2067 {
2068 #ifdef __WXDEBUG__
2069 m_name = wxS("<root>");
2070 #endif
2071 SetParentalType(0);
2072 m_depth = 0;
2073 }
2074
2075
2076 wxPGRootProperty::~wxPGRootProperty()
2077 {
2078 }
2079
2080
2081 // -----------------------------------------------------------------------
2082 // wxPropertyCategory
2083 // -----------------------------------------------------------------------
2084
2085 WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxPropertyCategory,none,TextCtrl)
2086 IMPLEMENT_DYNAMIC_CLASS(wxPropertyCategory, wxPGProperty)
2087
2088 void wxPropertyCategory::Init()
2089 {
2090 // don't set colour - prepareadditem method should do this
2091 SetParentalType(wxPG_PROP_CATEGORY);
2092 m_capFgColIndex = 1;
2093 m_textExtent = -1;
2094 }
2095
2096 wxPropertyCategory::wxPropertyCategory()
2097 : wxPGProperty()
2098 {
2099 Init();
2100 }
2101
2102
2103 wxPropertyCategory::wxPropertyCategory( const wxString &label, const wxString& name )
2104 : wxPGProperty(label,name)
2105 {
2106 Init();
2107 }
2108
2109
2110 wxPropertyCategory::~wxPropertyCategory()
2111 {
2112 }
2113
2114
2115 wxString wxPropertyCategory::GetValueAsString( int ) const
2116 {
2117 return wxEmptyString;
2118 }
2119
2120 int wxPropertyCategory::GetTextExtent( const wxWindow* wnd, const wxFont& font ) const
2121 {
2122 if ( m_textExtent > 0 )
2123 return m_textExtent;
2124 int x = 0, y = 0;
2125 ((wxWindow*)wnd)->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2126 return x;
2127 }
2128
2129 void wxPropertyCategory::CalculateTextExtent( wxWindow* wnd, const wxFont& font )
2130 {
2131 int x = 0, y = 0;
2132 wnd->GetTextExtent( m_label, &x, &y, 0, 0, &font );
2133 m_textExtent = x;
2134 }
2135
2136 // -----------------------------------------------------------------------
2137 // wxPGAttributeStorage
2138 // -----------------------------------------------------------------------
2139
2140 wxPGAttributeStorage::wxPGAttributeStorage()
2141 {
2142 }
2143
2144 wxPGAttributeStorage::~wxPGAttributeStorage()
2145 {
2146 wxPGHashMapS2P::iterator it;
2147
2148 for ( it = m_map.begin(); it != m_map.end(); it++ )
2149 {
2150 wxVariantData* data = (wxVariantData*) it->second;
2151 data->DecRef();
2152 }
2153 }
2154
2155 void wxPGAttributeStorage::Set( const wxString& name, const wxVariant& value )
2156 {
2157 wxVariantData* data = value.GetData();
2158
2159 // Free old, if any
2160 wxPGHashMapS2P::iterator it = m_map.find(name);
2161 if ( it != m_map.end() )
2162 ((wxVariantData*)it->second)->DecRef();
2163
2164 if ( data )
2165 data->IncRef();
2166
2167 m_map[name] = data;
2168 }
2169