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