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