Removed obsolete conditional compile option wxPG_CREATE_CONTROLS_HIDDEN
[wxWidgets.git] / src / propgrid / propgrid.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/propgrid.cpp
3 // Purpose: wxPropertyGrid
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: 2004-09-25
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/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/sizer.h"
44 #include "wx/textdlg.h"
45 #include "wx/filedlg.h"
46 #include "wx/statusbr.h"
47 #include "wx/intl.h"
48 #include "wx/frame.h"
49 #endif
50
51
52 // This define is necessary to prevent macro clearing
53 #define __wxPG_SOURCE_FILE__
54
55 #include "wx/propgrid/propgrid.h"
56 #include "wx/propgrid/editors.h"
57
58 #if wxPG_USE_RENDERER_NATIVE
59 #include "wx/renderer.h"
60 #endif
61
62 #include "wx/odcombo.h"
63
64 #include "wx/timer.h"
65 #include "wx/dcbuffer.h"
66
67 #ifdef __WXMSW__
68 #include "wx/msw/private.h"
69 #endif
70
71 // Two pics for the expand / collapse buttons.
72 // Files are not supplied with this project (since it is
73 // recommended to use either custom or native rendering).
74 // If you want them, get wxTreeMultiCtrl by Jorgen Bodde,
75 // and copy xpm files from archive to wxPropertyGrid src directory
76 // (and also comment/undef wxPG_ICON_WIDTH in propGrid.h
77 // and set wxPG_USE_RENDERER_NATIVE to 0).
78 #ifndef wxPG_ICON_WIDTH
79 #if defined(__WXMAC__)
80 #include "mac_collapse.xpm"
81 #include "mac_expand.xpm"
82 #elif defined(__WXGTK__)
83 #include "linux_collapse.xpm"
84 #include "linux_expand.xpm"
85 #else
86 #include "default_collapse.xpm"
87 #include "default_expand.xpm"
88 #endif
89 #endif
90
91
92 //#define wxPG_TEXT_INDENT 4 // For the wxComboControl
93 //#define wxPG_ALLOW_CLIPPING 1 // If 1, GetUpdateRegion() in OnPaint event handler is not ignored
94 #define wxPG_GUTTER_DIV 3 // gutter is max(iconwidth/gutter_div,gutter_min)
95 #define wxPG_GUTTER_MIN 3 // gutter before and after image of [+] or [-]
96 #define wxPG_YSPACING_MIN 1
97 #define wxPG_DEFAULT_VSPACING 2 // This matches .NET propertygrid's value,
98 // but causes normal combobox to spill out under MSW
99
100 //#define wxPG_OPTIMAL_WIDTH 200 // Arbitrary
101
102 //#define wxPG_MIN_SCROLLBAR_WIDTH 10 // Smallest scrollbar width on any platform
103 // Must be larger than largest control border
104 // width * 2.
105
106
107 #define wxPG_DEFAULT_CURSOR wxNullCursor
108
109
110 //#define wxPG_NAT_CHOICE_BORDER_ANY 0
111
112 //#define wxPG_HIDER_BUTTON_HEIGHT 25
113
114 #define wxPG_PIXELS_PER_UNIT m_lineHeight
115
116 #ifdef wxPG_ICON_WIDTH
117 #define m_iconHeight m_iconWidth
118 #endif
119
120 //#define wxPG_TOOLTIP_DELAY 1000
121
122 // -----------------------------------------------------------------------
123
124 #if wxUSE_INTL
125 void wxPropertyGrid::AutoGetTranslation ( bool enable )
126 {
127 wxPGGlobalVars->m_autoGetTranslation = enable;
128 }
129 #else
130 void wxPropertyGrid::AutoGetTranslation ( bool ) { }
131 #endif
132
133 // -----------------------------------------------------------------------
134
135 const char wxPropertyGridNameStr[] = "wxPropertyGrid";
136
137 // -----------------------------------------------------------------------
138 // Statics in one class for easy destruction.
139 // -----------------------------------------------------------------------
140
141 #include "wx/module.h"
142
143 class wxPGGlobalVarsClassManager : public wxModule
144 {
145 DECLARE_DYNAMIC_CLASS(wxPGGlobalVarsClassManager)
146 public:
147 wxPGGlobalVarsClassManager() {}
148 virtual bool OnInit() { wxPGGlobalVars = new wxPGGlobalVarsClass(); return true; }
149 virtual void OnExit() { delete wxPGGlobalVars; wxPGGlobalVars = NULL; }
150 };
151
152 IMPLEMENT_DYNAMIC_CLASS(wxPGGlobalVarsClassManager, wxModule)
153
154
155 // When wxPG is loaded dynamically after the application is already running
156 // then the built-in module system won't pick this one up. Add it manually.
157 void wxPGInitResourceModule()
158 {
159 wxModule* module = new wxPGGlobalVarsClassManager;
160 module->Init();
161 wxModule::RegisterModule(module);
162 }
163
164 wxPGGlobalVarsClass* wxPGGlobalVars = NULL;
165
166
167 wxPGGlobalVarsClass::wxPGGlobalVarsClass()
168 {
169 wxPGProperty::sm_wxPG_LABEL = new wxString(wxPG_LABEL_STRING);
170
171 m_boolChoices.Add(_("False"));
172 m_boolChoices.Add(_("True"));
173
174 m_fontFamilyChoices = NULL;
175
176 m_defaultRenderer = new wxPGDefaultRenderer();
177
178 m_autoGetTranslation = false;
179
180 m_offline = 0;
181
182 m_extraStyle = 0;
183
184 wxVariant v;
185
186 // Prepare some shared variants
187 m_vEmptyString = wxString();
188 m_vZero = (long) 0;
189 m_vMinusOne = (long) -1;
190 m_vTrue = true;
191 m_vFalse = false;
192
193 // Prepare cached string constants
194 m_strstring = wxS("string");
195 m_strlong = wxS("long");
196 m_strbool = wxS("bool");
197 m_strlist = wxS("list");
198 m_strDefaultValue = wxS("DefaultValue");
199 m_strMin = wxS("Min");
200 m_strMax = wxS("Max");
201 m_strUnits = wxS("Units");
202 m_strInlineHelp = wxS("InlineHelp");
203
204 m_warnings = 0;
205 }
206
207
208 wxPGGlobalVarsClass::~wxPGGlobalVarsClass()
209 {
210 size_t i;
211
212 delete m_defaultRenderer;
213
214 // This will always have one ref
215 delete m_fontFamilyChoices;
216
217 #if wxUSE_VALIDATORS
218 for ( i=0; i<m_arrValidators.size(); i++ )
219 delete ((wxValidator*)m_arrValidators[i]);
220 #endif
221
222 //
223 // Destroy value type class instances.
224 wxPGHashMapS2P::iterator vt_it;
225
226 // Destroy editor class instances.
227 // iterate over all the elements in the class
228 for( vt_it = m_mapEditorClasses.begin(); vt_it != m_mapEditorClasses.end(); ++vt_it )
229 {
230 delete ((wxPGEditor*)vt_it->second);
231 }
232
233 delete wxPGProperty::sm_wxPG_LABEL;
234 }
235
236 void wxPropertyGridInitGlobalsIfNeeded()
237 {
238 }
239
240 // -----------------------------------------------------------------------
241 // wxPGCanvas
242 // -----------------------------------------------------------------------
243
244 //
245 // wxPGCanvas acts as a graphics sub-window of the
246 // wxScrolledWindow that wxPropertyGrid is.
247 //
248 class wxPGCanvas : public wxPanel
249 {
250 public:
251 wxPGCanvas() : wxPanel()
252 {
253 }
254 virtual ~wxPGCanvas() { }
255
256 protected:
257 void OnMouseMove( wxMouseEvent &event )
258 {
259 wxPropertyGrid* pg = wxStaticCast(GetParent(), wxPropertyGrid);
260 pg->OnMouseMove( event );
261 }
262
263 void OnMouseClick( wxMouseEvent &event )
264 {
265 wxPropertyGrid* pg = wxStaticCast(GetParent(), wxPropertyGrid);
266 pg->OnMouseClick( event );
267 }
268
269 void OnMouseUp( wxMouseEvent &event )
270 {
271 wxPropertyGrid* pg = wxStaticCast(GetParent(), wxPropertyGrid);
272 pg->OnMouseUp( event );
273 }
274
275 void OnMouseRightClick( wxMouseEvent &event )
276 {
277 wxPropertyGrid* pg = wxStaticCast(GetParent(), wxPropertyGrid);
278 pg->OnMouseRightClick( event );
279 }
280
281 void OnMouseDoubleClick( wxMouseEvent &event )
282 {
283 wxPropertyGrid* pg = wxStaticCast(GetParent(), wxPropertyGrid);
284 pg->OnMouseDoubleClick( event );
285 }
286
287 void OnKey( wxKeyEvent& event )
288 {
289 wxPropertyGrid* pg = wxStaticCast(GetParent(), wxPropertyGrid);
290 pg->OnKey( event );
291 }
292
293 void OnPaint( wxPaintEvent& event );
294
295 // Always be focussable, even with child windows
296 virtual void SetCanFocus(bool WXUNUSED(canFocus))
297 { wxPanel::SetCanFocus(true); }
298
299
300 private:
301 DECLARE_EVENT_TABLE()
302 DECLARE_ABSTRACT_CLASS(wxPGCanvas)
303 };
304
305
306 IMPLEMENT_ABSTRACT_CLASS(wxPGCanvas,wxPanel)
307
308 BEGIN_EVENT_TABLE(wxPGCanvas, wxPanel)
309 EVT_MOTION(wxPGCanvas::OnMouseMove)
310 EVT_PAINT(wxPGCanvas::OnPaint)
311 EVT_LEFT_DOWN(wxPGCanvas::OnMouseClick)
312 EVT_LEFT_UP(wxPGCanvas::OnMouseUp)
313 EVT_RIGHT_UP(wxPGCanvas::OnMouseRightClick)
314 EVT_LEFT_DCLICK(wxPGCanvas::OnMouseDoubleClick)
315 EVT_KEY_DOWN(wxPGCanvas::OnKey)
316 END_EVENT_TABLE()
317
318
319 void wxPGCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
320 {
321 wxPropertyGrid* pg = wxStaticCast(GetParent(), wxPropertyGrid);
322 wxASSERT( pg->IsKindOf(CLASSINFO(wxPropertyGrid)) );
323
324 wxPaintDC dc(this);
325
326 // Don't paint after destruction has begun
327 if ( !(pg->GetInternalFlags() & wxPG_FL_INITIALIZED) )
328 return;
329
330 // Update everything inside the box
331 wxRect r = GetUpdateRegion().GetBox();
332
333 // FIXME: This is just a workaround for a bug that causes splitters not
334 // to paint when other windows are being dragged over the grid.
335 wxRect fullRect = GetRect();
336 r.x = fullRect.x;
337 r.width = fullRect.width;
338
339 // Repaint this rectangle
340 pg->DrawItems( dc, r.y, r.y + r.height, &r );
341
342 // We assume that the size set when grid is shown
343 // is what is desired.
344 pg->SetInternalFlag(wxPG_FL_GOOD_SIZE_SET);
345 }
346
347 // -----------------------------------------------------------------------
348 // wxPropertyGrid
349 // -----------------------------------------------------------------------
350
351 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGrid, wxScrolledWindow)
352
353 BEGIN_EVENT_TABLE(wxPropertyGrid, wxScrolledWindow)
354 EVT_IDLE(wxPropertyGrid::OnIdle)
355 EVT_MOTION(wxPropertyGrid::OnMouseMoveBottom)
356 EVT_PAINT(wxPropertyGrid::OnPaint)
357 EVT_SIZE(wxPropertyGrid::OnResize)
358 EVT_ENTER_WINDOW(wxPropertyGrid::OnMouseEntry)
359 EVT_LEAVE_WINDOW(wxPropertyGrid::OnMouseEntry)
360 EVT_MOUSE_CAPTURE_CHANGED(wxPropertyGrid::OnCaptureChange)
361 EVT_SCROLLWIN(wxPropertyGrid::OnScrollEvent)
362 EVT_CHILD_FOCUS(wxPropertyGrid::OnChildFocusEvent)
363 EVT_SET_FOCUS(wxPropertyGrid::OnFocusEvent)
364 EVT_KILL_FOCUS(wxPropertyGrid::OnFocusEvent)
365 EVT_SYS_COLOUR_CHANGED(wxPropertyGrid::OnSysColourChanged)
366 END_EVENT_TABLE()
367
368
369 // -----------------------------------------------------------------------
370
371 wxPropertyGrid::wxPropertyGrid()
372 : wxScrolledWindow()
373 {
374 Init1();
375 }
376
377 // -----------------------------------------------------------------------
378
379 wxPropertyGrid::wxPropertyGrid( wxWindow *parent,
380 wxWindowID id,
381 const wxPoint& pos,
382 const wxSize& size,
383 long style,
384 const wxString& name )
385 : wxScrolledWindow()
386 {
387 Init1();
388 Create(parent,id,pos,size,style,name);
389 }
390
391 // -----------------------------------------------------------------------
392
393 bool wxPropertyGrid::Create( wxWindow *parent,
394 wxWindowID id,
395 const wxPoint& pos,
396 const wxSize& size,
397 long style,
398 const wxString& name )
399 {
400
401 if ( !(style&wxBORDER_MASK) )
402 style |= wxSIMPLE_BORDER;
403
404 style |= wxVSCROLL;
405
406 // Filter out wxTAB_TRAVERSAL - we will handle TABs manually
407 style &= ~(wxTAB_TRAVERSAL);
408 style |= wxWANTS_CHARS;
409
410 wxScrolledWindow::Create(parent,id,pos,size,style,name);
411
412 Init2();
413
414 return true;
415 }
416
417 // -----------------------------------------------------------------------
418
419 //
420 // Initialize values to defaults
421 //
422 void wxPropertyGrid::Init1()
423 {
424 // Register editor classes, if necessary.
425 if ( wxPGGlobalVars->m_mapEditorClasses.empty() )
426 wxPropertyGrid::RegisterDefaultEditors();
427
428 m_iFlags = 0;
429 m_pState = NULL;
430 m_wndEditor = m_wndEditor2 = NULL;
431 m_selColumn = 1;
432 m_colHover = 1;
433 m_propHover = NULL;
434 m_labelEditor = NULL;
435 m_labelEditorProperty = NULL;
436 m_eventObject = this;
437 m_curFocused = NULL;
438 m_sortFunction = NULL;
439 m_inDoPropertyChanged = 0;
440 m_inCommitChangesFromEditor = 0;
441 m_inDoSelectProperty = 0;
442 m_permanentValidationFailureBehavior = wxPG_VFB_DEFAULT;
443 m_dragStatus = 0;
444 m_mouseSide = 16;
445 m_editorFocused = 0;
446
447 // Set default keys
448 AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_RIGHT );
449 AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_DOWN );
450 AddActionTrigger( wxPG_ACTION_PREV_PROPERTY, WXK_LEFT );
451 AddActionTrigger( wxPG_ACTION_PREV_PROPERTY, WXK_UP );
452 AddActionTrigger( wxPG_ACTION_EXPAND_PROPERTY, WXK_RIGHT);
453 AddActionTrigger( wxPG_ACTION_COLLAPSE_PROPERTY, WXK_LEFT);
454 AddActionTrigger( wxPG_ACTION_CANCEL_EDIT, WXK_ESCAPE );
455 AddActionTrigger( wxPG_ACTION_PRESS_BUTTON, WXK_DOWN, wxMOD_ALT );
456 AddActionTrigger( wxPG_ACTION_PRESS_BUTTON, WXK_F4 );
457
458 m_coloursCustomized = 0;
459 m_frozen = 0;
460
461 m_canvas = NULL;
462
463 #if wxPG_DOUBLE_BUFFER
464 m_doubleBuffer = NULL;
465 #endif
466
467 #ifndef wxPG_ICON_WIDTH
468 m_expandbmp = NULL;
469 m_collbmp = NULL;
470 m_iconWidth = 11;
471 m_iconHeight = 11;
472 #else
473 m_iconWidth = wxPG_ICON_WIDTH;
474 #endif
475
476 m_prevVY = -1;
477
478 m_gutterWidth = wxPG_GUTTER_MIN;
479 m_subgroup_extramargin = 10;
480
481 m_lineHeight = 0;
482
483 m_width = m_height = 0;
484
485 m_commonValues.push_back(new wxPGCommonValue(_("Unspecified"), wxPGGlobalVars->m_defaultRenderer) );
486 m_cvUnspecified = 0;
487
488 m_chgInfo_changedProperty = NULL;
489 }
490
491 // -----------------------------------------------------------------------
492
493 //
494 // Initialize after parent etc. set
495 //
496 void wxPropertyGrid::Init2()
497 {
498 wxASSERT( !(m_iFlags & wxPG_FL_INITIALIZED ) );
499
500 #ifdef __WXMAC__
501 // Smaller controls on Mac
502 SetWindowVariant(wxWINDOW_VARIANT_SMALL);
503 #endif
504
505 // Now create state, if one didn't exist already
506 // (wxPropertyGridManager might have created it for us).
507 if ( !m_pState )
508 {
509 m_pState = CreateState();
510 m_pState->m_pPropGrid = this;
511 m_iFlags |= wxPG_FL_CREATEDSTATE;
512 }
513
514 if ( !(m_windowStyle & wxPG_SPLITTER_AUTO_CENTER) )
515 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
516
517 if ( m_windowStyle & wxPG_HIDE_CATEGORIES )
518 {
519 m_pState->InitNonCatMode();
520
521 m_pState->m_properties = m_pState->m_abcArray;
522 }
523
524 GetClientSize(&m_width,&m_height);
525
526 #ifndef wxPG_ICON_WIDTH
527 // create two bitmap nodes for drawing
528 m_expandbmp = new wxBitmap(expand_xpm);
529 m_collbmp = new wxBitmap(collapse_xpm);
530
531 // calculate average font height for bitmap centering
532
533 m_iconWidth = m_expandbmp->GetWidth();
534 m_iconHeight = m_expandbmp->GetHeight();
535 #endif
536
537 m_curcursor = wxCURSOR_ARROW;
538 m_cursorSizeWE = new wxCursor( wxCURSOR_SIZEWE );
539
540 // adjust bitmap icon y position so they are centered
541 m_vspacing = wxPG_DEFAULT_VSPACING;
542
543 CalculateFontAndBitmapStuff( wxPG_DEFAULT_VSPACING );
544
545 // Allocate cell datas indirectly by calling setter
546 m_propertyDefaultCell.SetBgCol(*wxBLACK);
547 m_categoryDefaultCell.SetBgCol(*wxBLACK);
548
549 RegainColours();
550
551 // This helps with flicker
552 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
553
554 // Hook the top-level parent
555 m_tlp = NULL;
556 m_tlpClosed = NULL;
557 m_tlpClosedTime = 0;
558 OnTLPChanging(::wxGetTopLevelParent(this));
559
560 // set virtual size to this window size
561 wxSize wndsize = GetSize();
562 SetVirtualSize(wndsize.GetWidth(), wndsize.GetWidth());
563
564 m_timeCreated = ::wxGetLocalTimeMillis();
565
566 m_canvas = new wxPGCanvas();
567 m_canvas->Create(this, 1, wxPoint(0, 0), GetClientSize(),
568 wxWANTS_CHARS | wxCLIP_CHILDREN);
569 m_canvas->SetBackgroundStyle( wxBG_STYLE_CUSTOM );
570
571 m_iFlags |= wxPG_FL_INITIALIZED;
572
573 m_ncWidth = wndsize.GetWidth();
574
575 // Need to call OnResize handler or size given in constructor/Create
576 // will never work.
577 wxSizeEvent sizeEvent(wndsize,0);
578 OnResize(sizeEvent);
579 }
580
581 // -----------------------------------------------------------------------
582
583 wxPropertyGrid::~wxPropertyGrid()
584 {
585 size_t i;
586
587 DoSelectProperty(NULL, wxPG_SEL_NOVALIDATE|wxPG_SEL_DONT_SEND_EVENT);
588
589 // This should do prevent things from going too badly wrong
590 m_iFlags &= ~(wxPG_FL_INITIALIZED);
591
592 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
593 m_canvas->ReleaseMouse();
594
595 // Call with NULL to disconnect event handling
596 OnTLPChanging(NULL);
597
598 wxASSERT_MSG( !IsEditorsValueModified(),
599 wxS("Most recent change in property editor was lost!!! ")
600 wxS("(if you don't want this to happen, close your frames ")
601 wxS("and dialogs using Close(false).)") );
602
603 #if wxPG_DOUBLE_BUFFER
604 if ( m_doubleBuffer )
605 delete m_doubleBuffer;
606 #endif
607
608 if ( m_iFlags & wxPG_FL_CREATEDSTATE )
609 delete m_pState;
610
611 delete m_cursorSizeWE;
612
613 #ifndef wxPG_ICON_WIDTH
614 delete m_expandbmp;
615 delete m_collbmp;
616 #endif
617
618 // Delete common value records
619 for ( i=0; i<m_commonValues.size(); i++ )
620 {
621 delete GetCommonValue(i);
622 }
623 }
624
625 // -----------------------------------------------------------------------
626
627 bool wxPropertyGrid::Destroy()
628 {
629 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
630 m_canvas->ReleaseMouse();
631
632 return wxScrolledWindow::Destroy();
633 }
634
635 // -----------------------------------------------------------------------
636
637 wxPropertyGridPageState* wxPropertyGrid::CreateState() const
638 {
639 return new wxPropertyGridPageState();
640 }
641
642 // -----------------------------------------------------------------------
643 // wxPropertyGrid overridden wxWindow methods
644 // -----------------------------------------------------------------------
645
646 void wxPropertyGrid::SetWindowStyleFlag( long style )
647 {
648 long old_style = m_windowStyle;
649
650 if ( m_iFlags & wxPG_FL_INITIALIZED )
651 {
652 wxASSERT( m_pState );
653
654 if ( !(style & wxPG_HIDE_CATEGORIES) && (old_style & wxPG_HIDE_CATEGORIES) )
655 {
656 // Enable categories
657 EnableCategories( true );
658 }
659 else if ( (style & wxPG_HIDE_CATEGORIES) && !(old_style & wxPG_HIDE_CATEGORIES) )
660 {
661 // Disable categories
662 EnableCategories( false );
663 }
664 if ( !(old_style & wxPG_AUTO_SORT) && (style & wxPG_AUTO_SORT) )
665 {
666 //
667 // Autosort enabled
668 //
669 if ( !m_frozen )
670 PrepareAfterItemsAdded();
671 else
672 m_pState->m_itemsAdded = 1;
673 }
674 #if wxPG_SUPPORT_TOOLTIPS
675 if ( !(old_style & wxPG_TOOLTIPS) && (style & wxPG_TOOLTIPS) )
676 {
677 //
678 // Tooltips enabled
679 //
680 /*
681 wxToolTip* tooltip = new wxToolTip ( wxEmptyString );
682 SetToolTip ( tooltip );
683 tooltip->SetDelay ( wxPG_TOOLTIP_DELAY );
684 */
685 }
686 else if ( (old_style & wxPG_TOOLTIPS) && !(style & wxPG_TOOLTIPS) )
687 {
688 //
689 // Tooltips disabled
690 //
691 m_canvas->SetToolTip( NULL );
692 }
693 #endif
694 }
695
696 wxScrolledWindow::SetWindowStyleFlag ( style );
697
698 if ( m_iFlags & wxPG_FL_INITIALIZED )
699 {
700 if ( (old_style & wxPG_HIDE_MARGIN) != (style & wxPG_HIDE_MARGIN) )
701 {
702 CalculateFontAndBitmapStuff( m_vspacing );
703 Refresh();
704 }
705 }
706 }
707
708 // -----------------------------------------------------------------------
709
710 void wxPropertyGrid::Freeze()
711 {
712 if ( !m_frozen )
713 {
714 wxScrolledWindow::Freeze();
715 }
716 m_frozen++;
717 }
718
719 // -----------------------------------------------------------------------
720
721 void wxPropertyGrid::Thaw()
722 {
723 m_frozen--;
724
725 if ( !m_frozen )
726 {
727 wxScrolledWindow::Thaw();
728 RecalculateVirtualSize();
729 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
730 m_canvas->Refresh();
731 #endif
732
733 // Force property re-selection
734 // NB: We must copy the selection.
735 wxArrayPGProperty selection = m_pState->m_selection;
736 DoSetSelection(selection, wxPG_SEL_FORCE);
737 }
738 }
739
740 // -----------------------------------------------------------------------
741
742 bool wxPropertyGrid::DoAddToSelection( wxPGProperty* prop, int selFlags )
743 {
744 wxCHECK( prop, false );
745
746 if ( !(GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION) )
747 return DoSelectProperty(prop, selFlags);
748
749 wxArrayPGProperty& selection = m_pState->m_selection;
750
751 if ( !selection.size() )
752 {
753 return DoSelectProperty(prop, selFlags);
754 }
755 else
756 {
757 // For categories, only one can be selected at a time
758 if ( prop->IsCategory() || selection[0]->IsCategory() )
759 return true;
760
761 selection.push_back(prop);
762
763 if ( !(selFlags & wxPG_SEL_DONT_SEND_EVENT) )
764 {
765 SendEvent( wxEVT_PG_SELECTED, prop, NULL );
766 }
767
768 DrawItem(prop);
769 }
770
771 return true;
772 }
773
774 // -----------------------------------------------------------------------
775
776 bool wxPropertyGrid::DoRemoveFromSelection( wxPGProperty* prop, int selFlags )
777 {
778 wxCHECK( prop, false );
779 bool res;
780
781 wxArrayPGProperty& selection = m_pState->m_selection;
782 if ( selection.size() <= 1 )
783 {
784 res = DoSelectProperty(NULL, selFlags);
785 }
786 else
787 {
788 m_pState->DoRemoveFromSelection(prop);
789 DrawItem(prop);
790 res = true;
791 }
792
793 return res;
794 }
795
796 // -----------------------------------------------------------------------
797
798 bool wxPropertyGrid::DoSelectAndEdit( wxPGProperty* prop,
799 unsigned int colIndex,
800 unsigned int selFlags )
801 {
802 //
803 // NB: Enable following if label editor background colour is
804 // ever changed to any other than m_colSelBack.
805 //
806 // We use this workaround to prevent visible flicker when editing
807 // a cell. Atleast on wxMSW, there is a difficult to find
808 // (and perhaps prevent) redraw somewhere between making property
809 // selected and enabling label editing.
810 //
811 //wxColour prevColSelBack = m_colSelBack;
812 //m_colSelBack = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
813
814 bool res;
815
816 if ( colIndex == 1 )
817 {
818 res = DoSelectProperty(prop, selFlags);
819 }
820 else
821 {
822 // send event
823 DoClearSelection(false, wxPG_SEL_NO_REFRESH);
824
825 if ( m_pState->m_editableColumns.Index(colIndex) == wxNOT_FOUND )
826 {
827 res = DoAddToSelection(prop, selFlags);
828 }
829 else
830 {
831 res = DoAddToSelection(prop, selFlags|wxPG_SEL_NO_REFRESH);
832
833 DoBeginLabelEdit(colIndex, selFlags);
834 }
835 }
836
837 //m_colSelBack = prevColSelBack;
838 return res;
839 }
840
841 // -----------------------------------------------------------------------
842
843 bool wxPropertyGrid::AddToSelectionFromInputEvent( wxPGProperty* prop,
844 unsigned int colIndex,
845 wxMouseEvent* mouseEvent,
846 int selFlags )
847 {
848 bool alreadySelected = m_pState->DoIsPropertySelected(prop);
849 bool res = true;
850 bool addToExistingSelection;
851
852 if ( GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION )
853 {
854 if ( mouseEvent )
855 {
856 if ( mouseEvent->GetEventType() == wxEVT_RIGHT_DOWN ||
857 mouseEvent->GetEventType() == wxEVT_RIGHT_UP )
858 {
859 // Allow right-click for context menu without
860 // disturbing the selection.
861 if ( GetSelectedProperties().size() <= 1 ||
862 !alreadySelected )
863 return DoSelectAndEdit(prop, colIndex, selFlags);
864 return true;
865 }
866 else
867 {
868 addToExistingSelection = mouseEvent->ShiftDown();
869 }
870 }
871 else
872 {
873 addToExistingSelection = false;
874 }
875 }
876 else
877 {
878 addToExistingSelection = false;
879 }
880
881 if ( addToExistingSelection )
882 {
883 if ( !alreadySelected )
884 {
885 res = DoAddToSelection(prop, selFlags);
886 }
887 else if ( GetSelectedProperties().size() > 1 )
888 {
889 res = DoRemoveFromSelection(prop, selFlags);
890 }
891 }
892 else
893 {
894 res = DoSelectAndEdit(prop, colIndex, selFlags);
895 }
896
897 return res;
898 }
899
900 // -----------------------------------------------------------------------
901
902 void wxPropertyGrid::DoSetSelection( const wxArrayPGProperty& newSelection,
903 int selFlags )
904 {
905 if ( newSelection.size() > 0 )
906 {
907 if ( !DoSelectProperty(newSelection[0], selFlags) )
908 return;
909 }
910 else
911 {
912 DoClearSelection(false, selFlags);
913 }
914
915 for ( unsigned int i = 1; i < newSelection.size(); i++ )
916 {
917 DoAddToSelection(newSelection[i], selFlags);
918 }
919
920 Refresh();
921 }
922
923 // -----------------------------------------------------------------------
924
925 void wxPropertyGrid::DoBeginLabelEdit( unsigned int colIndex,
926 int selFlags )
927 {
928 wxPGProperty* selected = GetSelection();
929 wxCHECK_RET(selected, wxT("No property selected"));
930 wxCHECK_RET(colIndex != 1, wxT("Do not use this for column 1"));
931
932 if ( !(selFlags & wxPG_SEL_DONT_SEND_EVENT) )
933 {
934 if ( SendEvent( wxEVT_PG_LABEL_EDIT_BEGIN,
935 selected, NULL, 0,
936 colIndex ) )
937 return;
938 }
939
940 wxString text;
941 const wxPGCell* cell = NULL;
942 if ( selected->HasCell(colIndex) )
943 {
944 cell = &selected->GetCell(colIndex);
945 if ( !cell->HasText() && colIndex == 0 )
946 text = selected->GetLabel();
947 }
948
949 if ( !cell )
950 {
951 if ( colIndex == 0 )
952 text = selected->GetLabel();
953 else
954 cell = &selected->GetOrCreateCell(colIndex);
955 }
956
957 if ( cell && cell->HasText() )
958 text = cell->GetText();
959
960 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE); // send event
961
962 m_selColumn = colIndex;
963
964 wxRect r = GetEditorWidgetRect(selected, m_selColumn);
965
966 wxWindow* tc = GenerateEditorTextCtrl(r.GetPosition(),
967 r.GetSize(),
968 text,
969 NULL,
970 wxTE_PROCESS_ENTER,
971 0,
972 colIndex);
973
974 wxWindowID id = tc->GetId();
975 tc->Connect(id, wxEVT_COMMAND_TEXT_ENTER,
976 wxCommandEventHandler(wxPropertyGrid::OnLabelEditorEnterPress),
977 NULL, this);
978 tc->Connect(id, wxEVT_KEY_DOWN,
979 wxKeyEventHandler(wxPropertyGrid::OnLabelEditorKeyPress),
980 NULL, this);
981
982 tc->SetFocus();
983
984 m_labelEditor = wxStaticCast(tc, wxTextCtrl);
985 m_labelEditorProperty = selected;
986 }
987
988 // -----------------------------------------------------------------------
989
990 void
991 wxPropertyGrid::OnLabelEditorEnterPress( wxCommandEvent& WXUNUSED(event) )
992 {
993 DoEndLabelEdit(true);
994 }
995
996 // -----------------------------------------------------------------------
997
998 void wxPropertyGrid::OnLabelEditorKeyPress( wxKeyEvent& event )
999 {
1000 int keycode = event.GetKeyCode();
1001
1002 if ( keycode == WXK_ESCAPE )
1003 {
1004 DoEndLabelEdit(false);
1005 }
1006 else
1007 {
1008 event.Skip();
1009 }
1010 }
1011
1012 // -----------------------------------------------------------------------
1013
1014 void wxPropertyGrid::DoEndLabelEdit( bool commit, int selFlags )
1015 {
1016 if ( !m_labelEditor )
1017 return;
1018
1019 wxPGProperty* prop = m_labelEditorProperty;
1020 wxASSERT(prop);
1021
1022 if ( commit )
1023 {
1024 if ( !(selFlags & wxPG_SEL_DONT_SEND_EVENT) )
1025 {
1026 // wxPG_SEL_NOVALIDATE is passed correctly in selFlags
1027 if ( SendEvent( wxEVT_PG_LABEL_EDIT_ENDING,
1028 prop, NULL, selFlags,
1029 m_selColumn ) )
1030 return;
1031 }
1032
1033 wxString text = m_labelEditor->GetValue();
1034 wxPGCell* cell = NULL;
1035 if ( prop->HasCell(m_selColumn) )
1036 {
1037 cell = &prop->GetCell(m_selColumn);
1038 }
1039 else
1040 {
1041 if ( m_selColumn == 0 )
1042 prop->SetLabel(text);
1043 else
1044 cell = &prop->GetOrCreateCell(m_selColumn);
1045 }
1046
1047 if ( cell )
1048 cell->SetText(text);
1049 }
1050
1051 m_selColumn = 1;
1052
1053 DestroyEditorWnd(m_labelEditor);
1054 m_labelEditor = NULL;
1055 m_labelEditorProperty = NULL;
1056
1057 DrawItem(prop);
1058 }
1059
1060 // -----------------------------------------------------------------------
1061
1062 void wxPropertyGrid::SetExtraStyle( long exStyle )
1063 {
1064 if ( exStyle & wxPG_EX_NATIVE_DOUBLE_BUFFERING )
1065 {
1066 #if defined(__WXMSW__)
1067
1068 /*
1069 // Don't use WS_EX_COMPOSITED just now.
1070 HWND hWnd;
1071
1072 if ( m_iFlags & wxPG_FL_IN_MANAGER )
1073 hWnd = (HWND)GetParent()->GetHWND();
1074 else
1075 hWnd = (HWND)GetHWND();
1076
1077 ::SetWindowLong( hWnd, GWL_EXSTYLE,
1078 ::GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_COMPOSITED );
1079 */
1080
1081 //#elif defined(__WXGTK20__)
1082 #endif
1083 // Only apply wxPG_EX_NATIVE_DOUBLE_BUFFERING if the window
1084 // truly was double-buffered.
1085 if ( !this->IsDoubleBuffered() )
1086 {
1087 exStyle &= ~(wxPG_EX_NATIVE_DOUBLE_BUFFERING);
1088 }
1089 else
1090 {
1091 #if wxPG_DOUBLE_BUFFER
1092 delete m_doubleBuffer;
1093 m_doubleBuffer = NULL;
1094 #endif
1095 }
1096 }
1097
1098 wxScrolledWindow::SetExtraStyle( exStyle );
1099
1100 if ( exStyle & wxPG_EX_INIT_NOCAT )
1101 m_pState->InitNonCatMode();
1102
1103 if ( exStyle & wxPG_EX_HELP_AS_TOOLTIPS )
1104 m_windowStyle |= wxPG_TOOLTIPS;
1105
1106 // Set global style
1107 wxPGGlobalVars->m_extraStyle = exStyle;
1108 }
1109
1110 // -----------------------------------------------------------------------
1111
1112 // returns the best acceptable minimal size
1113 wxSize wxPropertyGrid::DoGetBestSize() const
1114 {
1115 int lineHeight = wxMax(15, m_lineHeight);
1116
1117 // don't make the grid too tall (limit height to 10 items) but don't
1118 // make it too small neither
1119 int numLines = wxMin
1120 (
1121 wxMax(m_pState->m_properties->GetChildCount(), 3),
1122 10
1123 );
1124
1125 const wxSize sz = wxSize(60, lineHeight*numLines + 40);
1126 CacheBestSize(sz);
1127 return sz;
1128 }
1129
1130 // -----------------------------------------------------------------------
1131
1132 void wxPropertyGrid::OnTLPChanging( wxWindow* newTLP )
1133 {
1134 wxLongLong currentTime = ::wxGetLocalTimeMillis();
1135
1136 //
1137 // Parent changed so let's redetermine and re-hook the
1138 // correct top-level window.
1139 if ( m_tlp )
1140 {
1141 m_tlp->Disconnect( wxEVT_CLOSE_WINDOW,
1142 wxCloseEventHandler(wxPropertyGrid::OnTLPClose),
1143 NULL, this );
1144 m_tlpClosed = m_tlp;
1145 m_tlpClosedTime = currentTime;
1146 }
1147
1148 if ( newTLP )
1149 {
1150 // Only accept new tlp if same one was not just dismissed.
1151 if ( newTLP != m_tlpClosed ||
1152 m_tlpClosedTime+250 < currentTime )
1153 {
1154 newTLP->Connect( wxEVT_CLOSE_WINDOW,
1155 wxCloseEventHandler(wxPropertyGrid::OnTLPClose),
1156 NULL, this );
1157 m_tlpClosed = NULL;
1158 }
1159 else
1160 {
1161 newTLP = NULL;
1162 }
1163 }
1164
1165 m_tlp = newTLP;
1166 }
1167
1168 // -----------------------------------------------------------------------
1169
1170 void wxPropertyGrid::OnTLPClose( wxCloseEvent& event )
1171 {
1172 // ClearSelection forces value validation/commit.
1173 if ( event.CanVeto() && !DoClearSelection() )
1174 {
1175 event.Veto();
1176 return;
1177 }
1178
1179 // Ok, it can close, set tlp pointer to NULL. Some other event
1180 // handler can of course veto the close, but our OnIdle() should
1181 // then be able to regain the tlp pointer.
1182 OnTLPChanging(NULL);
1183
1184 event.Skip();
1185 }
1186
1187 // -----------------------------------------------------------------------
1188
1189 bool wxPropertyGrid::Reparent( wxWindowBase *newParent )
1190 {
1191 OnTLPChanging((wxWindow*)newParent);
1192
1193 bool res = wxScrolledWindow::Reparent(newParent);
1194
1195 return res;
1196 }
1197
1198 // -----------------------------------------------------------------------
1199 // wxPropertyGrid Font and Colour Methods
1200 // -----------------------------------------------------------------------
1201
1202 void wxPropertyGrid::CalculateFontAndBitmapStuff( int vspacing )
1203 {
1204 int x = 0, y = 0;
1205
1206 m_captionFont = wxScrolledWindow::GetFont();
1207
1208 GetTextExtent(wxS("jG"), &x, &y, 0, 0, &m_captionFont);
1209 m_subgroup_extramargin = x + (x/2);
1210 m_fontHeight = y;
1211
1212 #if wxPG_USE_RENDERER_NATIVE
1213 m_iconWidth = wxPG_ICON_WIDTH;
1214 #elif wxPG_ICON_WIDTH
1215 // scale icon
1216 m_iconWidth = (m_fontHeight * wxPG_ICON_WIDTH) / 13;
1217 if ( m_iconWidth < 5 ) m_iconWidth = 5;
1218 else if ( !(m_iconWidth & 0x01) ) m_iconWidth++; // must be odd
1219
1220 #endif
1221
1222 m_gutterWidth = m_iconWidth / wxPG_GUTTER_DIV;
1223 if ( m_gutterWidth < wxPG_GUTTER_MIN )
1224 m_gutterWidth = wxPG_GUTTER_MIN;
1225
1226 int vdiv = 6;
1227 if ( vspacing <= 1 ) vdiv = 12;
1228 else if ( vspacing >= 3 ) vdiv = 3;
1229
1230 m_spacingy = m_fontHeight / vdiv;
1231 if ( m_spacingy < wxPG_YSPACING_MIN )
1232 m_spacingy = wxPG_YSPACING_MIN;
1233
1234 m_marginWidth = 0;
1235 if ( !(m_windowStyle & wxPG_HIDE_MARGIN) )
1236 m_marginWidth = m_gutterWidth*2 + m_iconWidth;
1237
1238 m_captionFont.SetWeight(wxBOLD);
1239 GetTextExtent(wxS("jG"), &x, &y, 0, 0, &m_captionFont);
1240
1241 m_lineHeight = m_fontHeight+(2*m_spacingy)+1;
1242
1243 // button spacing
1244 m_buttonSpacingY = (m_lineHeight - m_iconHeight) / 2;
1245 if ( m_buttonSpacingY < 0 ) m_buttonSpacingY = 0;
1246
1247 if ( m_pState )
1248 m_pState->CalculateFontAndBitmapStuff(vspacing);
1249
1250 if ( m_iFlags & wxPG_FL_INITIALIZED )
1251 RecalculateVirtualSize();
1252
1253 InvalidateBestSize();
1254 }
1255
1256 // -----------------------------------------------------------------------
1257
1258 void wxPropertyGrid::OnSysColourChanged( wxSysColourChangedEvent &WXUNUSED(event) )
1259 {
1260 RegainColours();
1261 Refresh();
1262 }
1263
1264 // -----------------------------------------------------------------------
1265
1266 static wxColour wxPGAdjustColour(const wxColour& src, int ra,
1267 int ga = 1000, int ba = 1000,
1268 bool forceDifferent = false)
1269 {
1270 if ( ga >= 1000 )
1271 ga = ra;
1272 if ( ba >= 1000 )
1273 ba = ra;
1274
1275 // Recursion guard (allow 2 max)
1276 static int isinside = 0;
1277 isinside++;
1278 wxCHECK_MSG( isinside < 3,
1279 *wxBLACK,
1280 wxT("wxPGAdjustColour should not be recursively called more than once") );
1281
1282 wxColour dst;
1283
1284 int r = src.Red();
1285 int g = src.Green();
1286 int b = src.Blue();
1287 int r2 = r + ra;
1288 if ( r2>255 ) r2 = 255;
1289 else if ( r2<0) r2 = 0;
1290 int g2 = g + ga;
1291 if ( g2>255 ) g2 = 255;
1292 else if ( g2<0) g2 = 0;
1293 int b2 = b + ba;
1294 if ( b2>255 ) b2 = 255;
1295 else if ( b2<0) b2 = 0;
1296
1297 // Make sure they are somewhat different
1298 if ( forceDifferent && (abs((r+g+b)-(r2+g2+b2)) < abs(ra/2)) )
1299 dst = wxPGAdjustColour(src,-(ra*2));
1300 else
1301 dst = wxColour(r2,g2,b2);
1302
1303 // Recursion guard (allow 2 max)
1304 isinside--;
1305
1306 return dst;
1307 }
1308
1309
1310 static int wxPGGetColAvg( const wxColour& col )
1311 {
1312 return (col.Red() + col.Green() + col.Blue()) / 3;
1313 }
1314
1315
1316 void wxPropertyGrid::RegainColours()
1317 {
1318 if ( !(m_coloursCustomized & 0x0002) )
1319 {
1320 wxColour col = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
1321
1322 // Make sure colour is dark enough
1323 #ifdef __WXGTK__
1324 int colDec = wxPGGetColAvg(col) - 230;
1325 #else
1326 int colDec = wxPGGetColAvg(col) - 200;
1327 #endif
1328 if ( colDec > 0 )
1329 m_colCapBack = wxPGAdjustColour(col,-colDec);
1330 else
1331 m_colCapBack = col;
1332 m_categoryDefaultCell.GetData()->SetBgCol(m_colCapBack);
1333 }
1334
1335 if ( !(m_coloursCustomized & 0x0001) )
1336 m_colMargin = m_colCapBack;
1337
1338 if ( !(m_coloursCustomized & 0x0004) )
1339 {
1340 #ifdef __WXGTK__
1341 int colDec = -90;
1342 #else
1343 int colDec = -72;
1344 #endif
1345 wxColour capForeCol = wxPGAdjustColour(m_colCapBack,colDec,5000,5000,true);
1346 m_colCapFore = capForeCol;
1347 m_categoryDefaultCell.GetData()->SetFgCol(capForeCol);
1348 }
1349
1350 if ( !(m_coloursCustomized & 0x0008) )
1351 {
1352 wxColour bgCol = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
1353 m_colPropBack = bgCol;
1354 m_propertyDefaultCell.GetData()->SetBgCol(bgCol);
1355 }
1356
1357 if ( !(m_coloursCustomized & 0x0010) )
1358 {
1359 wxColour fgCol = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
1360 m_colPropFore = fgCol;
1361 m_propertyDefaultCell.GetData()->SetFgCol(fgCol);
1362 }
1363
1364 if ( !(m_coloursCustomized & 0x0020) )
1365 m_colSelBack = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
1366
1367 if ( !(m_coloursCustomized & 0x0040) )
1368 m_colSelFore = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
1369
1370 if ( !(m_coloursCustomized & 0x0080) )
1371 m_colLine = m_colCapBack;
1372
1373 if ( !(m_coloursCustomized & 0x0100) )
1374 m_colDisPropFore = m_colCapFore;
1375
1376 m_colEmptySpace = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
1377 }
1378
1379 // -----------------------------------------------------------------------
1380
1381 void wxPropertyGrid::ResetColours()
1382 {
1383 m_coloursCustomized = 0;
1384
1385 RegainColours();
1386
1387 Refresh();
1388 }
1389
1390 // -----------------------------------------------------------------------
1391
1392 bool wxPropertyGrid::SetFont( const wxFont& font )
1393 {
1394 // Must disable active editor.
1395 DoClearSelection();
1396
1397 bool res = wxScrolledWindow::SetFont( font );
1398 if ( res && GetParent()) // may not have been Create()ed yet
1399 {
1400 CalculateFontAndBitmapStuff( m_vspacing );
1401 Refresh();
1402 }
1403
1404 return res;
1405 }
1406
1407 // -----------------------------------------------------------------------
1408
1409 void wxPropertyGrid::SetLineColour( const wxColour& col )
1410 {
1411 m_colLine = col;
1412 m_coloursCustomized |= 0x80;
1413 Refresh();
1414 }
1415
1416 // -----------------------------------------------------------------------
1417
1418 void wxPropertyGrid::SetMarginColour( const wxColour& col )
1419 {
1420 m_colMargin = col;
1421 m_coloursCustomized |= 0x01;
1422 Refresh();
1423 }
1424
1425 // -----------------------------------------------------------------------
1426
1427 void wxPropertyGrid::SetCellBackgroundColour( const wxColour& col )
1428 {
1429 m_colPropBack = col;
1430 m_coloursCustomized |= 0x08;
1431
1432 m_propertyDefaultCell.GetData()->SetBgCol(col);
1433
1434 Refresh();
1435 }
1436
1437 // -----------------------------------------------------------------------
1438
1439 void wxPropertyGrid::SetCellTextColour( const wxColour& col )
1440 {
1441 m_colPropFore = col;
1442 m_coloursCustomized |= 0x10;
1443
1444 m_propertyDefaultCell.GetData()->SetFgCol(col);
1445
1446 Refresh();
1447 }
1448
1449 // -----------------------------------------------------------------------
1450
1451 void wxPropertyGrid::SetEmptySpaceColour( const wxColour& col )
1452 {
1453 m_colEmptySpace = col;
1454
1455 Refresh();
1456 }
1457
1458 // -----------------------------------------------------------------------
1459
1460 void wxPropertyGrid::SetCellDisabledTextColour( const wxColour& col )
1461 {
1462 m_colDisPropFore = col;
1463 m_coloursCustomized |= 0x100;
1464 Refresh();
1465 }
1466
1467 // -----------------------------------------------------------------------
1468
1469 void wxPropertyGrid::SetSelectionBackgroundColour( const wxColour& col )
1470 {
1471 m_colSelBack = col;
1472 m_coloursCustomized |= 0x20;
1473 Refresh();
1474 }
1475
1476 // -----------------------------------------------------------------------
1477
1478 void wxPropertyGrid::SetSelectionTextColour( const wxColour& col )
1479 {
1480 m_colSelFore = col;
1481 m_coloursCustomized |= 0x40;
1482 Refresh();
1483 }
1484
1485 // -----------------------------------------------------------------------
1486
1487 void wxPropertyGrid::SetCaptionBackgroundColour( const wxColour& col )
1488 {
1489 m_colCapBack = col;
1490 m_coloursCustomized |= 0x02;
1491
1492 m_categoryDefaultCell.GetData()->SetBgCol(col);
1493
1494 Refresh();
1495 }
1496
1497 // -----------------------------------------------------------------------
1498
1499 void wxPropertyGrid::SetCaptionTextColour( const wxColour& col )
1500 {
1501 m_colCapFore = col;
1502 m_coloursCustomized |= 0x04;
1503
1504 m_categoryDefaultCell.GetData()->SetFgCol(col);
1505
1506 Refresh();
1507 }
1508
1509 // -----------------------------------------------------------------------
1510 // wxPropertyGrid property adding and removal
1511 // -----------------------------------------------------------------------
1512
1513 void wxPropertyGrid::PrepareAfterItemsAdded()
1514 {
1515 if ( !m_pState || !m_pState->m_itemsAdded ) return;
1516
1517 m_pState->m_itemsAdded = 0;
1518
1519 if ( m_windowStyle & wxPG_AUTO_SORT )
1520 Sort(wxPG_SORT_TOP_LEVEL_ONLY);
1521
1522 RecalculateVirtualSize();
1523 }
1524
1525 // -----------------------------------------------------------------------
1526 // wxPropertyGrid property operations
1527 // -----------------------------------------------------------------------
1528
1529 bool wxPropertyGrid::EnsureVisible( wxPGPropArg id )
1530 {
1531 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
1532
1533 Update();
1534
1535 bool changed = false;
1536
1537 // Is it inside collapsed section?
1538 if ( !p->IsVisible() )
1539 {
1540 // expand parents
1541 wxPGProperty* parent = p->GetParent();
1542 wxPGProperty* grandparent = parent->GetParent();
1543
1544 if ( grandparent && grandparent != m_pState->m_properties )
1545 Expand( grandparent );
1546
1547 Expand( parent );
1548 changed = true;
1549 }
1550
1551 // Need to scroll?
1552 int vx, vy;
1553 GetViewStart(&vx,&vy);
1554 vy*=wxPG_PIXELS_PER_UNIT;
1555
1556 int y = p->GetY();
1557
1558 if ( y < vy )
1559 {
1560 Scroll(vx, y/wxPG_PIXELS_PER_UNIT );
1561 m_iFlags |= wxPG_FL_SCROLLED;
1562 changed = true;
1563 }
1564 else if ( (y+m_lineHeight) > (vy+m_height) )
1565 {
1566 Scroll(vx, (y-m_height+(m_lineHeight*2))/wxPG_PIXELS_PER_UNIT );
1567 m_iFlags |= wxPG_FL_SCROLLED;
1568 changed = true;
1569 }
1570
1571 if ( changed )
1572 DrawItems( p, p );
1573
1574 return changed;
1575 }
1576
1577 // -----------------------------------------------------------------------
1578 // wxPropertyGrid helper methods called by properties
1579 // -----------------------------------------------------------------------
1580
1581 // Control font changer helper.
1582 void wxPropertyGrid::SetCurControlBoldFont()
1583 {
1584 wxASSERT( m_wndEditor );
1585 m_wndEditor->SetFont( m_captionFont );
1586 }
1587
1588 // -----------------------------------------------------------------------
1589
1590 wxPoint wxPropertyGrid::GetGoodEditorDialogPosition( wxPGProperty* p,
1591 const wxSize& sz )
1592 {
1593 #if wxPG_SMALL_SCREEN
1594 // On small-screen devices, always show dialogs with default position and size.
1595 return wxDefaultPosition;
1596 #else
1597 int splitterX = GetSplitterPosition();
1598 int x = splitterX;
1599 int y = p->GetY();
1600
1601 wxCHECK_MSG( y >= 0, wxPoint(-1,-1), wxT("invalid y?") );
1602
1603 ImprovedClientToScreen( &x, &y );
1604
1605 int sw = wxSystemSettings::GetMetric( ::wxSYS_SCREEN_X );
1606 int sh = wxSystemSettings::GetMetric( ::wxSYS_SCREEN_Y );
1607
1608 int new_x;
1609 int new_y;
1610
1611 if ( x > (sw/2) )
1612 // left
1613 new_x = x + (m_width-splitterX) - sz.x;
1614 else
1615 // right
1616 new_x = x;
1617
1618 if ( y > (sh/2) )
1619 // above
1620 new_y = y - sz.y;
1621 else
1622 // below
1623 new_y = y + m_lineHeight;
1624
1625 return wxPoint(new_x,new_y);
1626 #endif
1627 }
1628
1629 // -----------------------------------------------------------------------
1630
1631 wxString& wxPropertyGrid::ExpandEscapeSequences( wxString& dst_str, wxString& src_str )
1632 {
1633 if ( src_str.length() == 0 )
1634 {
1635 dst_str = src_str;
1636 return src_str;
1637 }
1638
1639 bool prev_is_slash = false;
1640
1641 wxString::const_iterator i = src_str.begin();
1642
1643 dst_str.clear();
1644
1645 for ( ; i != src_str.end(); ++i )
1646 {
1647 wxUniChar a = *i;
1648
1649 if ( a != wxS('\\') )
1650 {
1651 if ( !prev_is_slash )
1652 {
1653 dst_str << a;
1654 }
1655 else
1656 {
1657 if ( a == wxS('n') )
1658 {
1659 #ifdef __WXMSW__
1660 dst_str << wxS('\n');
1661 #else
1662 dst_str << wxS('\n');
1663 #endif
1664 }
1665 else if ( a == wxS('t') )
1666 dst_str << wxS('\t');
1667 else
1668 dst_str << a;
1669 }
1670 prev_is_slash = false;
1671 }
1672 else
1673 {
1674 if ( prev_is_slash )
1675 {
1676 dst_str << wxS('\\');
1677 prev_is_slash = false;
1678 }
1679 else
1680 {
1681 prev_is_slash = true;
1682 }
1683 }
1684 }
1685 return dst_str;
1686 }
1687
1688 // -----------------------------------------------------------------------
1689
1690 wxString& wxPropertyGrid::CreateEscapeSequences( wxString& dst_str, wxString& src_str )
1691 {
1692 if ( src_str.length() == 0 )
1693 {
1694 dst_str = src_str;
1695 return src_str;
1696 }
1697
1698 wxString::const_iterator i = src_str.begin();
1699 wxUniChar prev_a = wxS('\0');
1700
1701 dst_str.clear();
1702
1703 for ( ; i != src_str.end(); ++i )
1704 {
1705 wxChar a = *i;
1706
1707 if ( a >= wxS(' ') )
1708 {
1709 // This surely is not something that requires an escape sequence.
1710 dst_str << a;
1711 }
1712 else
1713 {
1714 // This might need...
1715 if ( a == wxS('\r') )
1716 {
1717 // DOS style line end.
1718 // Already taken care below
1719 }
1720 else if ( a == wxS('\n') )
1721 // UNIX style line end.
1722 dst_str << wxS("\\n");
1723 else if ( a == wxS('\t') )
1724 // Tab.
1725 dst_str << wxS('\t');
1726 else
1727 {
1728 //wxLogDebug(wxT("WARNING: Could not create escape sequence for character #%i"),(int)a);
1729 dst_str << a;
1730 }
1731 }
1732
1733 prev_a = a;
1734 }
1735 return dst_str;
1736 }
1737
1738 // -----------------------------------------------------------------------
1739
1740 wxPGProperty* wxPropertyGrid::DoGetItemAtY( int y ) const
1741 {
1742 // Outside?
1743 if ( y < 0 )
1744 return NULL;
1745
1746 unsigned int a = 0;
1747 return m_pState->m_properties->GetItemAtY(y, m_lineHeight, &a);
1748 }
1749
1750 // -----------------------------------------------------------------------
1751 // wxPropertyGrid graphics related methods
1752 // -----------------------------------------------------------------------
1753
1754 void wxPropertyGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
1755 {
1756 wxPaintDC dc(this);
1757
1758 // Update everything inside the box
1759 wxRect r = GetUpdateRegion().GetBox();
1760
1761 dc.SetPen(m_colEmptySpace);
1762 dc.SetBrush(m_colEmptySpace);
1763 dc.DrawRectangle(r);
1764 }
1765
1766 // -----------------------------------------------------------------------
1767
1768 void wxPropertyGrid::DrawExpanderButton( wxDC& dc, const wxRect& rect,
1769 wxPGProperty* property ) const
1770 {
1771 // Prepare rectangle to be used
1772 wxRect r(rect);
1773 r.x += m_gutterWidth; r.y += m_buttonSpacingY;
1774 r.width = m_iconWidth; r.height = m_iconHeight;
1775
1776 #if (wxPG_USE_RENDERER_NATIVE)
1777 //
1778 #elif wxPG_ICON_WIDTH
1779 // Drawing expand/collapse button manually
1780 dc.SetPen(m_colPropFore);
1781 if ( property->IsCategory() )
1782 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1783 else
1784 dc.SetBrush(m_colPropBack);
1785
1786 dc.DrawRectangle( r );
1787 int _y = r.y+(m_iconWidth/2);
1788 dc.DrawLine(r.x+2,_y,r.x+m_iconWidth-2,_y);
1789 #else
1790 wxBitmap* bmp;
1791 #endif
1792
1793 if ( property->IsExpanded() )
1794 {
1795 // wxRenderer functions are non-mutating in nature, so it
1796 // should be safe to cast "const wxPropertyGrid*" to "wxWindow*".
1797 // Hopefully this does not cause problems.
1798 #if (wxPG_USE_RENDERER_NATIVE)
1799 wxRendererNative::Get().DrawTreeItemButton(
1800 (wxWindow*)this,
1801 dc,
1802 r,
1803 wxCONTROL_EXPANDED
1804 );
1805 #elif wxPG_ICON_WIDTH
1806 //
1807 #else
1808 bmp = m_collbmp;
1809 #endif
1810
1811 }
1812 else
1813 {
1814 #if (wxPG_USE_RENDERER_NATIVE)
1815 wxRendererNative::Get().DrawTreeItemButton(
1816 (wxWindow*)this,
1817 dc,
1818 r,
1819 0
1820 );
1821 #elif wxPG_ICON_WIDTH
1822 int _x = r.x+(m_iconWidth/2);
1823 dc.DrawLine(_x,r.y+2,_x,r.y+m_iconWidth-2);
1824 #else
1825 bmp = m_expandbmp;
1826 #endif
1827 }
1828
1829 #if (wxPG_USE_RENDERER_NATIVE)
1830 //
1831 #elif wxPG_ICON_WIDTH
1832 //
1833 #else
1834 dc.DrawBitmap( *bmp, r.x, r.y, true );
1835 #endif
1836 }
1837
1838 // -----------------------------------------------------------------------
1839
1840 //
1841 // This is the one called by OnPaint event handler and others.
1842 // topy and bottomy are already unscrolled (ie. physical)
1843 //
1844 void wxPropertyGrid::DrawItems( wxDC& dc,
1845 unsigned int topy,
1846 unsigned int bottomy,
1847 const wxRect* clipRect )
1848 {
1849 if ( m_frozen || m_height < 1 || bottomy < topy || !m_pState ) return;
1850
1851 m_pState->EnsureVirtualHeight();
1852
1853 wxRect tempClipRect;
1854 if ( !clipRect )
1855 {
1856 tempClipRect = wxRect(0,topy,m_pState->m_width,bottomy);
1857 clipRect = &tempClipRect;
1858 }
1859
1860 // items added check
1861 if ( m_pState->m_itemsAdded ) PrepareAfterItemsAdded();
1862
1863 int paintFinishY = 0;
1864
1865 if ( m_pState->m_properties->GetChildCount() > 0 )
1866 {
1867 wxDC* dcPtr = &dc;
1868 bool isBuffered = false;
1869
1870 #if wxPG_DOUBLE_BUFFER
1871 wxMemoryDC* bufferDC = NULL;
1872
1873 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
1874 {
1875 if ( !m_doubleBuffer )
1876 {
1877 paintFinishY = clipRect->y;
1878 dcPtr = NULL;
1879 }
1880 else
1881 {
1882 bufferDC = new wxMemoryDC();
1883
1884 // If nothing was changed, then just copy from double-buffer
1885 bufferDC->SelectObject( *m_doubleBuffer );
1886 dcPtr = bufferDC;
1887
1888 isBuffered = true;
1889 }
1890 }
1891 #endif
1892
1893 if ( dcPtr )
1894 {
1895 dc.SetClippingRegion( *clipRect );
1896 paintFinishY = DoDrawItems( *dcPtr, clipRect, isBuffered );
1897 }
1898
1899 #if wxPG_DOUBLE_BUFFER
1900 if ( bufferDC )
1901 {
1902 dc.Blit( clipRect->x, clipRect->y, clipRect->width, clipRect->height,
1903 bufferDC, 0, 0, wxCOPY );
1904 dc.DestroyClippingRegion(); // Is this really necessary?
1905 delete bufferDC;
1906 }
1907 #endif
1908 }
1909
1910 // Clear area beyond bottomY?
1911 if ( paintFinishY < (clipRect->y+clipRect->height) )
1912 {
1913 dc.SetPen(m_colEmptySpace);
1914 dc.SetBrush(m_colEmptySpace);
1915 dc.DrawRectangle( 0, paintFinishY, m_width, (clipRect->y+clipRect->height) );
1916 }
1917 }
1918
1919 // -----------------------------------------------------------------------
1920
1921 int wxPropertyGrid::DoDrawItems( wxDC& dc,
1922 const wxRect* clipRect,
1923 bool isBuffered ) const
1924 {
1925 const wxPGProperty* firstItem;
1926 const wxPGProperty* lastItem;
1927
1928 firstItem = DoGetItemAtY(clipRect->y);
1929 lastItem = DoGetItemAtY(clipRect->y+clipRect->height-1);
1930
1931 if ( !lastItem )
1932 lastItem = GetLastItem( wxPG_ITERATE_VISIBLE );
1933
1934 if ( m_frozen || m_height < 1 || firstItem == NULL )
1935 return clipRect->y;
1936
1937 wxCHECK_MSG( !m_pState->m_itemsAdded, clipRect->y, wxT("no items added") );
1938 wxASSERT( m_pState->m_properties->GetChildCount() );
1939
1940 int lh = m_lineHeight;
1941
1942 int firstItemTopY;
1943 int lastItemBottomY;
1944
1945 firstItemTopY = clipRect->y;
1946 lastItemBottomY = clipRect->y + clipRect->height;
1947
1948 // Align y coordinates to item boundaries
1949 firstItemTopY -= firstItemTopY % lh;
1950 lastItemBottomY += lh - (lastItemBottomY % lh);
1951 lastItemBottomY -= 1;
1952
1953 // Entire range outside scrolled, visible area?
1954 if ( firstItemTopY >= (int)m_pState->GetVirtualHeight() || lastItemBottomY <= 0 )
1955 return clipRect->y;
1956
1957 wxCHECK_MSG( firstItemTopY < lastItemBottomY, clipRect->y, wxT("invalid y values") );
1958
1959
1960 /*
1961 wxLogDebug(wxT(" -> DoDrawItems ( \"%s\" -> \"%s\", height=%i (ch=%i), clipRect = 0x%lX )"),
1962 firstItem->GetLabel().c_str(),
1963 lastItem->GetLabel().c_str(),
1964 (int)(lastItemBottomY - firstItemTopY),
1965 (int)m_height,
1966 (unsigned long)clipRect );
1967 */
1968
1969 wxRect r;
1970
1971 long windowStyle = m_windowStyle;
1972
1973 int xRelMod = 0;
1974 int yRelMod = 0;
1975
1976 //
1977 // With wxPG_DOUBLE_BUFFER, do double buffering
1978 // - buffer's y = 0, so align cliprect and coordinates to that
1979 //
1980 #if wxPG_DOUBLE_BUFFER
1981
1982 wxRect cr2;
1983
1984 if ( isBuffered )
1985 {
1986 xRelMod = clipRect->x;
1987 yRelMod = clipRect->y;
1988
1989 //
1990 // clipRect conversion
1991 cr2 = *clipRect;
1992 cr2.x -= xRelMod;
1993 cr2.y -= yRelMod;
1994 clipRect = &cr2;
1995 firstItemTopY -= yRelMod;
1996 lastItemBottomY -= yRelMod;
1997 }
1998 #else
1999 wxUnusedVar(isBuffered);
2000 #endif
2001
2002 int x = m_marginWidth - xRelMod;
2003
2004 wxFont normalFont = GetFont();
2005
2006 bool reallyFocused = (m_iFlags & wxPG_FL_FOCUSED) != 0;
2007
2008 bool isPgEnabled = IsEnabled();
2009
2010 //
2011 // Prepare some pens and brushes that are often changed to.
2012 //
2013
2014 wxBrush marginBrush(m_colMargin);
2015 wxPen marginPen(m_colMargin);
2016 wxBrush capbgbrush(m_colCapBack,wxSOLID);
2017 wxPen linepen(m_colLine,1,wxSOLID);
2018
2019 wxColour selBackCol;
2020 if ( isPgEnabled )
2021 selBackCol = m_colSelBack;
2022 else
2023 selBackCol = m_colMargin;
2024
2025 // pen that has same colour as text
2026 wxPen outlinepen(m_colPropFore,1,wxSOLID);
2027
2028 //
2029 // Clear margin with background colour
2030 //
2031 dc.SetBrush( marginBrush );
2032 if ( !(windowStyle & wxPG_HIDE_MARGIN) )
2033 {
2034 dc.SetPen( *wxTRANSPARENT_PEN );
2035 dc.DrawRectangle(-1-xRelMod,firstItemTopY-1,x+2,lastItemBottomY-firstItemTopY+2);
2036 }
2037
2038 const wxPGProperty* firstSelected = GetSelection();
2039 const wxPropertyGridPageState* state = m_pState;
2040
2041 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2042 bool wasSelectedPainted = false;
2043 #endif
2044
2045 // TODO: Only render columns that are within clipping region.
2046
2047 dc.SetFont(normalFont);
2048
2049 wxPropertyGridConstIterator it( state, wxPG_ITERATE_VISIBLE, firstItem );
2050 int endScanBottomY = lastItemBottomY + lh;
2051 int y = firstItemTopY;
2052
2053 //
2054 // Pregenerate list of visible properties.
2055 wxArrayPGProperty visPropArray;
2056 visPropArray.reserve((m_height/m_lineHeight)+6);
2057
2058 for ( ; !it.AtEnd(); it.Next() )
2059 {
2060 const wxPGProperty* p = *it;
2061
2062 if ( !p->HasFlag(wxPG_PROP_HIDDEN) )
2063 {
2064 visPropArray.push_back((wxPGProperty*)p);
2065
2066 if ( y > endScanBottomY )
2067 break;
2068
2069 y += lh;
2070 }
2071 }
2072
2073 visPropArray.push_back(NULL);
2074
2075 wxPGProperty* nextP = visPropArray[0];
2076
2077 int gridWidth = state->m_width;
2078
2079 y = firstItemTopY;
2080 for ( unsigned int arrInd=1;
2081 nextP && y <= lastItemBottomY;
2082 arrInd++ )
2083 {
2084 wxPGProperty* p = nextP;
2085 nextP = visPropArray[arrInd];
2086
2087 int rowHeight = m_fontHeight+(m_spacingy*2)+1;
2088 int textMarginHere = x;
2089 int renderFlags = 0;
2090
2091 int greyDepth = m_marginWidth;
2092 if ( !(windowStyle & wxPG_HIDE_CATEGORIES) )
2093 greyDepth = (((int)p->m_depthBgCol)-1) * m_subgroup_extramargin + m_marginWidth;
2094
2095 int greyDepthX = greyDepth - xRelMod;
2096
2097 // Use basic depth if in non-categoric mode and parent is base array.
2098 if ( !(windowStyle & wxPG_HIDE_CATEGORIES) || p->GetParent() != m_pState->m_properties )
2099 {
2100 textMarginHere += ((unsigned int)((p->m_depth-1)*m_subgroup_extramargin));
2101 }
2102
2103 // Paint margin area
2104 dc.SetBrush(marginBrush);
2105 dc.SetPen(marginPen);
2106 dc.DrawRectangle( -xRelMod, y, greyDepth, lh );
2107
2108 dc.SetPen( linepen );
2109
2110 int y2 = y + lh;
2111
2112 // Margin Edge
2113 dc.DrawLine( greyDepthX, y, greyDepthX, y2 );
2114
2115 // Splitters
2116 unsigned int si;
2117 int sx = x;
2118
2119 for ( si=0; si<state->m_colWidths.size(); si++ )
2120 {
2121 sx += state->m_colWidths[si];
2122 dc.DrawLine( sx, y, sx, y2 );
2123 }
2124
2125 // Horizontal Line, below
2126 // (not if both this and next is category caption)
2127 if ( p->IsCategory() &&
2128 nextP && nextP->IsCategory() )
2129 dc.SetPen(m_colCapBack);
2130
2131 dc.DrawLine( greyDepthX, y2-1, gridWidth-xRelMod, y2-1 );
2132
2133 //
2134 // Need to override row colours?
2135 wxColour rowFgCol;
2136 wxColour rowBgCol;
2137
2138 bool isSelected = state->DoIsPropertySelected(p);
2139
2140 if ( !isSelected )
2141 {
2142 // Disabled may get different colour.
2143 if ( !p->IsEnabled() )
2144 {
2145 renderFlags |= wxPGCellRenderer::Disabled |
2146 wxPGCellRenderer::DontUseCellFgCol;
2147 rowFgCol = m_colDisPropFore;
2148 }
2149 }
2150 else
2151 {
2152 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2153 if ( p == firstSelected )
2154 wasSelectedPainted = true;
2155 #endif
2156
2157 renderFlags |= wxPGCellRenderer::Selected;
2158
2159 if ( !p->IsCategory() )
2160 {
2161 renderFlags |= wxPGCellRenderer::DontUseCellFgCol |
2162 wxPGCellRenderer::DontUseCellBgCol;
2163
2164 if ( reallyFocused && p == firstSelected )
2165 {
2166 rowFgCol = m_colSelFore;
2167 rowBgCol = selBackCol;
2168 }
2169 else if ( isPgEnabled )
2170 {
2171 rowFgCol = m_colPropFore;
2172 if ( p == firstSelected )
2173 rowBgCol = m_colMargin;
2174 else
2175 rowBgCol = selBackCol;
2176 }
2177 else
2178 {
2179 rowFgCol = m_colDisPropFore;
2180 rowBgCol = selBackCol;
2181 }
2182 }
2183 }
2184
2185 wxBrush rowBgBrush;
2186
2187 if ( rowBgCol.IsOk() )
2188 rowBgBrush = wxBrush(rowBgCol);
2189
2190 if ( HasInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL) )
2191 renderFlags = renderFlags & ~wxPGCellRenderer::DontUseCellColours;
2192
2193 //
2194 // Fill additional margin area with background colour of first cell
2195 if ( greyDepthX < textMarginHere )
2196 {
2197 if ( !(renderFlags & wxPGCellRenderer::DontUseCellBgCol) )
2198 {
2199 wxPGCell& cell = p->GetCell(0);
2200 rowBgCol = cell.GetBgCol();
2201 rowBgBrush = wxBrush(rowBgCol);
2202 }
2203 dc.SetBrush(rowBgBrush);
2204 dc.SetPen(rowBgCol);
2205 dc.DrawRectangle(greyDepthX+1, y,
2206 textMarginHere-greyDepthX, lh-1);
2207 }
2208
2209 bool fontChanged = false;
2210
2211 // Expander button rectangle
2212 wxRect butRect( ((p->m_depth - 1) * m_subgroup_extramargin) - xRelMod,
2213 y,
2214 m_marginWidth,
2215 lh );
2216
2217 if ( p->IsCategory() )
2218 {
2219 // Captions have their cell areas merged as one
2220 dc.SetFont(m_captionFont);
2221 fontChanged = true;
2222 wxRect cellRect(greyDepthX, y, gridWidth - greyDepth + 2, rowHeight-1 );
2223
2224 if ( renderFlags & wxPGCellRenderer::DontUseCellBgCol )
2225 {
2226 dc.SetBrush(rowBgBrush);
2227 dc.SetPen(rowBgCol);
2228 }
2229
2230 if ( renderFlags & wxPGCellRenderer::DontUseCellFgCol )
2231 {
2232 dc.SetTextForeground(rowFgCol);
2233 }
2234
2235 wxPGCellRenderer* renderer = p->GetCellRenderer(0);
2236 renderer->Render( dc, cellRect, this, p, 0, -1, renderFlags );
2237
2238 // Tree Item Button
2239 if ( !HasFlag(wxPG_HIDE_MARGIN) && p->HasVisibleChildren() )
2240 DrawExpanderButton( dc, butRect, p );
2241 }
2242 else
2243 {
2244 if ( p->m_flags & wxPG_PROP_MODIFIED && (windowStyle & wxPG_BOLD_MODIFIED) )
2245 {
2246 dc.SetFont(m_captionFont);
2247 fontChanged = true;
2248 }
2249
2250 unsigned int ci;
2251 int cellX = x + 1;
2252 int nextCellWidth = state->m_colWidths[0] -
2253 (greyDepthX - m_marginWidth);
2254 wxRect cellRect(greyDepthX+1, y, 0, rowHeight-1);
2255 int textXAdd = textMarginHere - greyDepthX;
2256
2257 for ( ci=0; ci<state->m_colWidths.size(); ci++ )
2258 {
2259 cellRect.width = nextCellWidth - 1;
2260
2261 wxWindow* cellEditor = NULL;
2262 int cellRenderFlags = renderFlags;
2263
2264 // Tree Item Button (must be drawn before clipping is set up)
2265 if ( ci == 0 && !HasFlag(wxPG_HIDE_MARGIN) && p->HasVisibleChildren() )
2266 DrawExpanderButton( dc, butRect, p );
2267
2268 // Background
2269 if ( isSelected && (ci == 1 || ci == m_selColumn) )
2270 {
2271 if ( p == firstSelected )
2272 {
2273 if ( ci == 1 && m_wndEditor )
2274 cellEditor = m_wndEditor;
2275 else if ( ci == m_selColumn && m_labelEditor )
2276 cellEditor = m_labelEditor;
2277 }
2278
2279 if ( cellEditor )
2280 {
2281 wxColour editorBgCol =
2282 cellEditor->GetBackgroundColour();
2283 dc.SetBrush(editorBgCol);
2284 dc.SetPen(editorBgCol);
2285 dc.SetTextForeground(m_colPropFore);
2286 dc.DrawRectangle(cellRect);
2287
2288 if ( m_dragStatus != 0 ||
2289 (m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE) )
2290 cellEditor = NULL;
2291 }
2292 else
2293 {
2294 dc.SetBrush(m_colPropBack);
2295 dc.SetPen(m_colPropBack);
2296 dc.SetTextForeground(m_colDisPropFore);
2297 if ( p->IsEnabled() )
2298 dc.SetTextForeground(rowFgCol);
2299 else
2300 dc.SetTextForeground(m_colDisPropFore);
2301 }
2302 }
2303 else
2304 {
2305 if ( renderFlags & wxPGCellRenderer::DontUseCellBgCol )
2306 {
2307 dc.SetBrush(rowBgBrush);
2308 dc.SetPen(rowBgCol);
2309 }
2310
2311 if ( renderFlags & wxPGCellRenderer::DontUseCellFgCol )
2312 {
2313 dc.SetTextForeground(rowFgCol);
2314 }
2315 }
2316
2317 dc.SetClippingRegion(cellRect);
2318
2319 cellRect.x += textXAdd;
2320 cellRect.width -= textXAdd;
2321
2322 // Foreground
2323 if ( !cellEditor )
2324 {
2325 wxPGCellRenderer* renderer;
2326 int cmnVal = p->GetCommonValue();
2327 if ( cmnVal == -1 || ci != 1 )
2328 {
2329 renderer = p->GetCellRenderer(ci);
2330 renderer->Render( dc, cellRect, this, p, ci, -1,
2331 cellRenderFlags );
2332 }
2333 else
2334 {
2335 renderer = GetCommonValue(cmnVal)->GetRenderer();
2336 renderer->Render( dc, cellRect, this, p, ci, -1,
2337 cellRenderFlags );
2338 }
2339 }
2340
2341 cellX += state->m_colWidths[ci];
2342 if ( ci < (state->m_colWidths.size()-1) )
2343 nextCellWidth = state->m_colWidths[ci+1];
2344 cellRect.x = cellX;
2345 dc.DestroyClippingRegion(); // Is this really necessary?
2346 textXAdd = 0;
2347 }
2348 }
2349
2350 if ( fontChanged )
2351 dc.SetFont(normalFont);
2352
2353 y += rowHeight;
2354 }
2355
2356 // Refresh editor controls (seems not needed on msw)
2357 // NOTE: This code is mandatory for GTK!
2358 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2359 if ( wasSelectedPainted )
2360 {
2361 if ( m_wndEditor )
2362 m_wndEditor->Refresh();
2363 if ( m_wndEditor2 )
2364 m_wndEditor2->Refresh();
2365 }
2366 #endif
2367
2368 return y + yRelMod;
2369 }
2370
2371 // -----------------------------------------------------------------------
2372
2373 wxRect wxPropertyGrid::GetPropertyRect( const wxPGProperty* p1, const wxPGProperty* p2 ) const
2374 {
2375 wxRect r;
2376
2377 if ( m_width < 10 || m_height < 10 ||
2378 !m_pState->m_properties->GetChildCount() ||
2379 p1 == NULL )
2380 return wxRect(0,0,0,0);
2381
2382 int vy = 0;
2383
2384 //
2385 // Return rect which encloses the given property range
2386
2387 int visTop = p1->GetY();
2388 int visBottom;
2389 if ( p2 )
2390 visBottom = p2->GetY() + m_lineHeight;
2391 else
2392 visBottom = m_height + visTop;
2393
2394 // If seleced property is inside the range, we'll extend the range to include
2395 // control's size.
2396 wxPGProperty* selected = GetSelection();
2397 if ( selected )
2398 {
2399 int selectedY = selected->GetY();
2400 if ( selectedY >= visTop && selectedY < visBottom )
2401 {
2402 wxWindow* editor = GetEditorControl();
2403 if ( editor )
2404 {
2405 int visBottom2 = selectedY + editor->GetSize().y;
2406 if ( visBottom2 > visBottom )
2407 visBottom = visBottom2;
2408 }
2409 }
2410 }
2411
2412 return wxRect(0,visTop-vy,m_pState->m_width,visBottom-visTop);
2413 }
2414
2415 // -----------------------------------------------------------------------
2416
2417 void wxPropertyGrid::DrawItems( const wxPGProperty* p1, const wxPGProperty* p2 )
2418 {
2419 if ( m_frozen )
2420 return;
2421
2422 if ( m_pState->m_itemsAdded )
2423 PrepareAfterItemsAdded();
2424
2425 wxRect r = GetPropertyRect(p1, p2);
2426 if ( r.width > 0 )
2427 {
2428 m_canvas->RefreshRect(r);
2429 }
2430 }
2431
2432 // -----------------------------------------------------------------------
2433
2434 void wxPropertyGrid::RefreshProperty( wxPGProperty* p )
2435 {
2436 if ( m_pState->DoIsPropertySelected(p) )
2437 {
2438 // NB: We must copy the selection.
2439 wxArrayPGProperty selection = m_pState->m_selection;
2440 DoSetSelection(selection, wxPG_SEL_FORCE);
2441 }
2442
2443 DrawItemAndChildren(p);
2444 }
2445
2446 // -----------------------------------------------------------------------
2447
2448 void wxPropertyGrid::DrawItemAndValueRelated( wxPGProperty* p )
2449 {
2450 if ( m_frozen )
2451 return;
2452
2453 // Draw item, children, and parent too, if it is not category
2454 wxPGProperty* parent = p->GetParent();
2455
2456 while ( parent &&
2457 !parent->IsCategory() &&
2458 parent->GetParent() )
2459 {
2460 DrawItem(parent);
2461 parent = parent->GetParent();
2462 }
2463
2464 DrawItemAndChildren(p);
2465 }
2466
2467 void wxPropertyGrid::DrawItemAndChildren( wxPGProperty* p )
2468 {
2469 wxCHECK_RET( p, wxT("invalid property id") );
2470
2471 // Do not draw if in non-visible page
2472 if ( p->GetParentState() != m_pState )
2473 return;
2474
2475 // do not draw a single item if multiple pending
2476 if ( m_pState->m_itemsAdded || m_frozen )
2477 return;
2478
2479 // Update child control.
2480 wxPGProperty* selected = GetSelection();
2481 if ( selected && selected->GetParent() == p )
2482 RefreshEditor();
2483
2484 const wxPGProperty* lastDrawn = p->GetLastVisibleSubItem();
2485
2486 DrawItems(p, lastDrawn);
2487 }
2488
2489 // -----------------------------------------------------------------------
2490
2491 void wxPropertyGrid::Refresh( bool WXUNUSED(eraseBackground),
2492 const wxRect *rect )
2493 {
2494 PrepareAfterItemsAdded();
2495
2496 wxWindow::Refresh(false);
2497 if ( m_canvas )
2498 // TODO: Coordinate translation
2499 m_canvas->Refresh(false, rect);
2500
2501 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2502 // I think this really helps only GTK+1.2
2503 if ( m_wndEditor ) m_wndEditor->Refresh();
2504 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
2505 #endif
2506 }
2507
2508 // -----------------------------------------------------------------------
2509 // wxPropertyGrid global operations
2510 // -----------------------------------------------------------------------
2511
2512 void wxPropertyGrid::Clear()
2513 {
2514 m_pState->DoClear();
2515
2516 m_propHover = NULL;
2517
2518 m_prevVY = 0;
2519
2520 RecalculateVirtualSize();
2521
2522 // Need to clear some area at the end
2523 if ( !m_frozen )
2524 RefreshRect(wxRect(0, 0, m_width, m_height));
2525 }
2526
2527 // -----------------------------------------------------------------------
2528
2529 bool wxPropertyGrid::EnableCategories( bool enable )
2530 {
2531 DoClearSelection();
2532
2533 if ( enable )
2534 {
2535 //
2536 // Enable categories
2537 //
2538
2539 m_windowStyle &= ~(wxPG_HIDE_CATEGORIES);
2540 }
2541 else
2542 {
2543 //
2544 // Disable categories
2545 //
2546 m_windowStyle |= wxPG_HIDE_CATEGORIES;
2547 }
2548
2549 if ( !m_pState->EnableCategories(enable) )
2550 return false;
2551
2552 if ( !m_frozen )
2553 {
2554 if ( m_windowStyle & wxPG_AUTO_SORT )
2555 {
2556 m_pState->m_itemsAdded = 1; // force
2557 PrepareAfterItemsAdded();
2558 }
2559 }
2560 else
2561 m_pState->m_itemsAdded = 1;
2562
2563 // No need for RecalculateVirtualSize() here - it is already called in
2564 // wxPropertyGridPageState method above.
2565
2566 Refresh();
2567
2568 return true;
2569 }
2570
2571 // -----------------------------------------------------------------------
2572
2573 void wxPropertyGrid::SwitchState( wxPropertyGridPageState* pNewState )
2574 {
2575 wxASSERT( pNewState );
2576 wxASSERT( pNewState->GetGrid() );
2577
2578 if ( pNewState == m_pState )
2579 return;
2580
2581 wxArrayPGProperty oldSelection = m_pState->m_selection;
2582
2583 // Call ClearSelection() instead of DoClearSelection()
2584 // so that selection clear events are not sent.
2585 ClearSelection();
2586
2587 m_pState->m_selection = oldSelection;
2588
2589 bool orig_mode = m_pState->IsInNonCatMode();
2590 bool new_state_mode = pNewState->IsInNonCatMode();
2591
2592 m_pState = pNewState;
2593
2594 // Validate width
2595 int pgWidth = GetClientSize().x;
2596 if ( HasVirtualWidth() )
2597 {
2598 int minWidth = pgWidth;
2599 if ( pNewState->m_width < minWidth )
2600 {
2601 pNewState->m_width = minWidth;
2602 pNewState->CheckColumnWidths();
2603 }
2604 }
2605 else
2606 {
2607 //
2608 // Just in case, fully re-center splitter
2609 if ( HasFlag( wxPG_SPLITTER_AUTO_CENTER ) )
2610 pNewState->m_fSplitterX = -1.0;
2611
2612 pNewState->OnClientWidthChange( pgWidth, pgWidth - pNewState->m_width );
2613 }
2614
2615 m_propHover = NULL;
2616
2617 // If necessary, convert state to correct mode.
2618 if ( orig_mode != new_state_mode )
2619 {
2620 // This should refresh as well.
2621 EnableCategories( orig_mode?false:true );
2622 }
2623 else if ( !m_frozen )
2624 {
2625 // Refresh, if not frozen.
2626 m_pState->PrepareAfterItemsAdded();
2627
2628 // Reselect (Use SetSelection() instead of Do-variant so that
2629 // events won't be sent).
2630 SetSelection(m_pState->m_selection);
2631
2632 RecalculateVirtualSize(0);
2633 Refresh();
2634 }
2635 else
2636 m_pState->m_itemsAdded = 1;
2637 }
2638
2639 // -----------------------------------------------------------------------
2640
2641 // Call to SetSplitterPosition will always disable splitter auto-centering
2642 // if parent window is shown.
2643 void wxPropertyGrid::DoSetSplitterPosition_( int newxpos, bool refresh, int splitterIndex, bool allPages )
2644 {
2645 if ( ( newxpos < wxPG_DRAG_MARGIN ) )
2646 return;
2647
2648 wxPropertyGridPageState* state = m_pState;
2649
2650 state->DoSetSplitterPosition( newxpos, splitterIndex, allPages );
2651
2652 if ( refresh )
2653 {
2654 if ( GetSelection() )
2655 CorrectEditorWidgetSizeX();
2656
2657 Refresh();
2658 }
2659 }
2660
2661 // -----------------------------------------------------------------------
2662
2663 void wxPropertyGrid::CenterSplitter( bool enableAutoCentering )
2664 {
2665 SetSplitterPosition( m_width/2, true );
2666 if ( enableAutoCentering && ( m_windowStyle & wxPG_SPLITTER_AUTO_CENTER ) )
2667 m_iFlags &= ~(wxPG_FL_DONT_CENTER_SPLITTER);
2668 }
2669
2670 // -----------------------------------------------------------------------
2671 // wxPropertyGrid item iteration (GetNextProperty etc.) methods
2672 // -----------------------------------------------------------------------
2673
2674 // Returns nearest paint visible property (such that will be painted unless
2675 // window is scrolled or resized). If given property is paint visible, then
2676 // it itself will be returned
2677 wxPGProperty* wxPropertyGrid::GetNearestPaintVisible( wxPGProperty* p ) const
2678 {
2679 int vx,vy1;// Top left corner of client
2680 GetViewStart(&vx,&vy1);
2681 vy1 *= wxPG_PIXELS_PER_UNIT;
2682
2683 int vy2 = vy1 + m_height;
2684 int propY = p->GetY2(m_lineHeight);
2685
2686 if ( (propY + m_lineHeight) < vy1 )
2687 {
2688 // Too high
2689 return DoGetItemAtY( vy1 );
2690 }
2691 else if ( propY > vy2 )
2692 {
2693 // Too low
2694 return DoGetItemAtY( vy2 );
2695 }
2696
2697 // Itself paint visible
2698 return p;
2699
2700 }
2701
2702 // -----------------------------------------------------------------------
2703 // Methods related to change in value, value modification and sending events
2704 // -----------------------------------------------------------------------
2705
2706 // commits any changes in editor of selected property
2707 // return true if validation did not fail
2708 // flags are same as with DoSelectProperty
2709 bool wxPropertyGrid::CommitChangesFromEditor( wxUint32 flags )
2710 {
2711 // Committing already?
2712 if ( m_inCommitChangesFromEditor )
2713 return true;
2714
2715 // Don't do this if already processing editor event. It might
2716 // induce recursive dialogs and crap like that.
2717 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
2718 {
2719 if ( m_inDoPropertyChanged )
2720 return true;
2721
2722 return false;
2723 }
2724
2725 wxPGProperty* selected = GetSelection();
2726
2727 if ( m_wndEditor &&
2728 IsEditorsValueModified() &&
2729 (m_iFlags & wxPG_FL_INITIALIZED) &&
2730 selected )
2731 {
2732 m_inCommitChangesFromEditor = 1;
2733
2734 wxVariant variant(selected->GetValueRef());
2735 bool valueIsPending = false;
2736
2737 // JACS - necessary to avoid new focus being found spuriously within OnIdle
2738 // due to another window getting focus
2739 wxWindow* oldFocus = m_curFocused;
2740
2741 bool validationFailure = false;
2742 bool forceSuccess = (flags & (wxPG_SEL_NOVALIDATE|wxPG_SEL_FORCE)) ? true : false;
2743
2744 m_chgInfo_changedProperty = NULL;
2745
2746 // If truly modified, schedule value as pending.
2747 if ( selected->GetEditorClass()->
2748 GetValueFromControl( variant,
2749 selected,
2750 GetEditorControl() ) )
2751 {
2752 if ( DoEditorValidate() &&
2753 PerformValidation(selected, variant) )
2754 {
2755 valueIsPending = true;
2756 }
2757 else
2758 {
2759 validationFailure = true;
2760 }
2761 }
2762 else
2763 {
2764 EditorsValueWasNotModified();
2765 }
2766
2767 bool res = true;
2768
2769 m_inCommitChangesFromEditor = 0;
2770
2771 if ( validationFailure && !forceSuccess )
2772 {
2773 if (oldFocus)
2774 {
2775 oldFocus->SetFocus();
2776 m_curFocused = oldFocus;
2777 }
2778
2779 res = OnValidationFailure(selected, variant);
2780
2781 // Now prevent further validation failure messages
2782 if ( res )
2783 {
2784 EditorsValueWasNotModified();
2785 OnValidationFailureReset(selected);
2786 }
2787 }
2788 else if ( valueIsPending )
2789 {
2790 DoPropertyChanged( selected, flags );
2791 EditorsValueWasNotModified();
2792 }
2793
2794 return res;
2795 }
2796
2797 return true;
2798 }
2799
2800 // -----------------------------------------------------------------------
2801
2802 bool wxPropertyGrid::PerformValidation( wxPGProperty* p, wxVariant& pendingValue,
2803 int flags )
2804 {
2805 //
2806 // Runs all validation functionality.
2807 // Returns true if value passes all tests.
2808 //
2809
2810 m_validationInfo.m_failureBehavior = m_permanentValidationFailureBehavior;
2811
2812 if ( pendingValue.GetType() == wxPG_VARIANT_TYPE_LIST )
2813 {
2814 if ( !p->ValidateValue(pendingValue, m_validationInfo) )
2815 return false;
2816 }
2817
2818 //
2819 // Adapt list to child values, if necessary
2820 wxVariant listValue = pendingValue;
2821 wxVariant* pPendingValue = &pendingValue;
2822 wxVariant* pList = NULL;
2823
2824 // If parent has wxPG_PROP_AGGREGATE flag, or uses composite
2825 // string value, then we need treat as it was changed instead
2826 // (or, in addition, as is the case with composite string parent).
2827 // This includes creating list variant for child values.
2828
2829 wxPGProperty* pwc = p->GetParent();
2830 wxPGProperty* changedProperty = p;
2831 wxPGProperty* baseChangedProperty = changedProperty;
2832 wxVariant bcpPendingList;
2833
2834 listValue = pendingValue;
2835 listValue.SetName(p->GetBaseName());
2836
2837 while ( pwc &&
2838 (pwc->HasFlag(wxPG_PROP_AGGREGATE) || pwc->HasFlag(wxPG_PROP_COMPOSED_VALUE)) )
2839 {
2840 wxVariantList tempList;
2841 wxVariant lv(tempList, pwc->GetBaseName());
2842 lv.Append(listValue);
2843 listValue = lv;
2844 pPendingValue = &listValue;
2845
2846 if ( pwc->HasFlag(wxPG_PROP_AGGREGATE) )
2847 {
2848 baseChangedProperty = pwc;
2849 bcpPendingList = lv;
2850 }
2851
2852 changedProperty = pwc;
2853 pwc = pwc->GetParent();
2854 }
2855
2856 wxVariant value;
2857 wxPGProperty* evtChangingProperty = changedProperty;
2858
2859 if ( pPendingValue->GetType() != wxPG_VARIANT_TYPE_LIST )
2860 {
2861 value = *pPendingValue;
2862 }
2863 else
2864 {
2865 // Convert list to child values
2866 pList = pPendingValue;
2867 changedProperty->AdaptListToValue( *pPendingValue, &value );
2868 }
2869
2870 wxVariant evtChangingValue = value;
2871
2872 if ( flags & SendEvtChanging )
2873 {
2874 // FIXME: After proper ValueToString()s added, remove
2875 // this. It is just a temporary fix, as evt_changing
2876 // will simply not work for wxPG_PROP_COMPOSED_VALUE
2877 // (unless it is selected, and textctrl editor is open).
2878 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
2879 {
2880 evtChangingProperty = baseChangedProperty;
2881 if ( evtChangingProperty != p )
2882 {
2883 evtChangingProperty->AdaptListToValue( bcpPendingList, &evtChangingValue );
2884 }
2885 else
2886 {
2887 evtChangingValue = pendingValue;
2888 }
2889 }
2890
2891 if ( evtChangingProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
2892 {
2893 if ( changedProperty == GetSelection() )
2894 {
2895 wxWindow* editor = GetEditorControl();
2896 wxASSERT( editor->IsKindOf(CLASSINFO(wxTextCtrl)) );
2897 evtChangingValue = wxStaticCast(editor, wxTextCtrl)->GetValue();
2898 }
2899 else
2900 {
2901 wxLogDebug(wxT("WARNING: wxEVT_PG_CHANGING is about to happen with old value."));
2902 }
2903 }
2904 }
2905
2906 wxASSERT( m_chgInfo_changedProperty == NULL );
2907 m_chgInfo_changedProperty = changedProperty;
2908 m_chgInfo_baseChangedProperty = baseChangedProperty;
2909 m_chgInfo_pendingValue = value;
2910
2911 if ( pList )
2912 m_chgInfo_valueList = *pList;
2913 else
2914 m_chgInfo_valueList.MakeNull();
2915
2916 // If changedProperty is not property which value was edited,
2917 // then call wxPGProperty::ValidateValue() for that as well.
2918 if ( p != changedProperty && value.GetType() != wxPG_VARIANT_TYPE_LIST )
2919 {
2920 if ( !changedProperty->ValidateValue(value, m_validationInfo) )
2921 return false;
2922 }
2923
2924 if ( flags & SendEvtChanging )
2925 {
2926 // SendEvent returns true if event was vetoed
2927 if ( SendEvent( wxEVT_PG_CHANGING, evtChangingProperty,
2928 &evtChangingValue ) )
2929 return false;
2930 }
2931
2932 if ( flags & IsStandaloneValidation )
2933 {
2934 // If called in 'generic' context, we need to reset
2935 // m_chgInfo_changedProperty and write back translated value.
2936 m_chgInfo_changedProperty = NULL;
2937 pendingValue = value;
2938 }
2939
2940 return true;
2941 }
2942
2943 // -----------------------------------------------------------------------
2944
2945 void wxPropertyGrid::DoShowPropertyError( wxPGProperty* WXUNUSED(property), const wxString& msg )
2946 {
2947 if ( !msg.length() )
2948 return;
2949
2950 #if wxUSE_STATUSBAR
2951 if ( !wxPGGlobalVars->m_offline )
2952 {
2953 wxWindow* topWnd = ::wxGetTopLevelParent(this);
2954 if ( topWnd )
2955 {
2956 wxFrame* pFrame = wxDynamicCast(topWnd, wxFrame);
2957 if ( pFrame )
2958 {
2959 wxStatusBar* pStatusBar = pFrame->GetStatusBar();
2960 if ( pStatusBar )
2961 {
2962 pStatusBar->SetStatusText(msg);
2963 return;
2964 }
2965 }
2966 }
2967 }
2968 #endif
2969
2970 ::wxMessageBox(msg, wxT("Property Error"));
2971 }
2972
2973 // -----------------------------------------------------------------------
2974
2975 bool wxPropertyGrid::OnValidationFailure( wxPGProperty* property,
2976 wxVariant& invalidValue )
2977 {
2978 wxWindow* editor = GetEditorControl();
2979
2980 // First call property's handler
2981 property->OnValidationFailure(invalidValue);
2982
2983 bool res = DoOnValidationFailure(property, invalidValue);
2984
2985 //
2986 // For non-wxTextCtrl editors, we do need to revert the value
2987 if ( !editor->IsKindOf(CLASSINFO(wxTextCtrl)) &&
2988 property == GetSelection() )
2989 {
2990 property->GetEditorClass()->UpdateControl(property, editor);
2991 }
2992
2993 property->SetFlag(wxPG_PROP_INVALID_VALUE);
2994
2995 return res;
2996 }
2997
2998 bool wxPropertyGrid::DoOnValidationFailure( wxPGProperty* property, wxVariant& WXUNUSED(invalidValue) )
2999 {
3000 int vfb = m_validationInfo.m_failureBehavior;
3001
3002 if ( vfb & wxPG_VFB_BEEP )
3003 ::wxBell();
3004
3005 if ( (vfb & wxPG_VFB_MARK_CELL) &&
3006 !property->HasFlag(wxPG_PROP_INVALID_VALUE) )
3007 {
3008 unsigned int colCount = m_pState->GetColumnCount();
3009
3010 // We need backup marked property's cells
3011 m_propCellsBackup = property->m_cells;
3012
3013 wxColour vfbFg = *wxWHITE;
3014 wxColour vfbBg = *wxRED;
3015
3016 property->EnsureCells(colCount);
3017
3018 for ( unsigned int i=0; i<colCount; i++ )
3019 {
3020 wxPGCell& cell = property->m_cells[i];
3021 cell.SetFgCol(vfbFg);
3022 cell.SetBgCol(vfbBg);
3023 }
3024
3025 DrawItemAndChildren(property);
3026
3027 if ( property == GetSelection() )
3028 {
3029 SetInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3030
3031 wxWindow* editor = GetEditorControl();
3032 if ( editor )
3033 {
3034 editor->SetForegroundColour(vfbFg);
3035 editor->SetBackgroundColour(vfbBg);
3036 }
3037 }
3038 }
3039
3040 if ( vfb & wxPG_VFB_SHOW_MESSAGE )
3041 {
3042 wxString msg = m_validationInfo.m_failureMessage;
3043
3044 if ( !msg.length() )
3045 msg = wxT("You have entered invalid value. Press ESC to cancel editing.");
3046
3047 DoShowPropertyError(property, msg);
3048 }
3049
3050 return (vfb & wxPG_VFB_STAY_IN_PROPERTY) ? false : true;
3051 }
3052
3053 // -----------------------------------------------------------------------
3054
3055 void wxPropertyGrid::DoOnValidationFailureReset( wxPGProperty* property )
3056 {
3057 int vfb = m_validationInfo.m_failureBehavior;
3058
3059 if ( vfb & wxPG_VFB_MARK_CELL )
3060 {
3061 // Revert cells
3062 property->m_cells = m_propCellsBackup;
3063
3064 ClearInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3065
3066 if ( property == GetSelection() && GetEditorControl() )
3067 {
3068 // Calling this will recreate the control, thus resetting its colour
3069 RefreshProperty(property);
3070 }
3071 else
3072 {
3073 DrawItemAndChildren(property);
3074 }
3075 }
3076 }
3077
3078 // -----------------------------------------------------------------------
3079
3080 // flags are same as with DoSelectProperty
3081 bool wxPropertyGrid::DoPropertyChanged( wxPGProperty* p, unsigned int selFlags )
3082 {
3083 if ( m_inDoPropertyChanged )
3084 return true;
3085
3086 wxWindow* editor = GetEditorControl();
3087 wxPGProperty* selected = GetSelection();
3088
3089 m_pState->m_anyModified = 1;
3090
3091 m_inDoPropertyChanged = 1;
3092
3093 // Maybe need to update control
3094 wxASSERT( m_chgInfo_changedProperty != NULL );
3095
3096 // These values were calculated in PerformValidation()
3097 wxPGProperty* changedProperty = m_chgInfo_changedProperty;
3098 wxVariant value = m_chgInfo_pendingValue;
3099
3100 wxPGProperty* topPaintedProperty = changedProperty;
3101
3102 while ( !topPaintedProperty->IsCategory() &&
3103 !topPaintedProperty->IsRoot() )
3104 {
3105 topPaintedProperty = topPaintedProperty->GetParent();
3106 }
3107
3108 changedProperty->SetValue(value, &m_chgInfo_valueList, wxPG_SETVAL_BY_USER);
3109
3110 // Set as Modified (not if dragging just began)
3111 if ( !(p->m_flags & wxPG_PROP_MODIFIED) )
3112 {
3113 p->m_flags |= wxPG_PROP_MODIFIED;
3114 if ( p == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3115 {
3116 if ( editor )
3117 SetCurControlBoldFont();
3118 }
3119 }
3120
3121 wxPGProperty* pwc;
3122
3123 // Propagate updates to parent(s)
3124 pwc = p;
3125 wxPGProperty* prevPwc = NULL;
3126
3127 while ( prevPwc != topPaintedProperty )
3128 {
3129 pwc->m_flags |= wxPG_PROP_MODIFIED;
3130
3131 if ( pwc == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3132 {
3133 if ( editor )
3134 SetCurControlBoldFont();
3135 }
3136
3137 prevPwc = pwc;
3138 pwc = pwc->GetParent();
3139 }
3140
3141 // Draw the actual property
3142 DrawItemAndChildren( topPaintedProperty );
3143
3144 //
3145 // If value was set by wxPGProperty::OnEvent, then update the editor
3146 // control.
3147 if ( selFlags & wxPG_SEL_DIALOGVAL )
3148 {
3149 RefreshEditor();
3150 }
3151 else
3152 {
3153 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
3154 if ( m_wndEditor ) m_wndEditor->Refresh();
3155 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
3156 #endif
3157 }
3158
3159 // Sanity check
3160 wxASSERT( !changedProperty->GetParent()->HasFlag(wxPG_PROP_AGGREGATE) );
3161
3162 // If top parent has composite string value, then send to child parents,
3163 // starting from baseChangedProperty.
3164 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
3165 {
3166 pwc = m_chgInfo_baseChangedProperty;
3167
3168 while ( pwc != changedProperty )
3169 {
3170 SendEvent( wxEVT_PG_CHANGED, pwc, NULL );
3171 pwc = pwc->GetParent();
3172 }
3173 }
3174
3175 SendEvent( wxEVT_PG_CHANGED, changedProperty, NULL );
3176
3177 m_inDoPropertyChanged = 0;
3178
3179 return true;
3180 }
3181
3182 // -----------------------------------------------------------------------
3183
3184 bool wxPropertyGrid::ChangePropertyValue( wxPGPropArg id, wxVariant newValue )
3185 {
3186 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
3187
3188 m_chgInfo_changedProperty = NULL;
3189
3190 if ( PerformValidation(p, newValue) )
3191 {
3192 DoPropertyChanged(p);
3193 return true;
3194 }
3195 else
3196 {
3197 OnValidationFailure(p, newValue);
3198 }
3199
3200 return false;
3201 }
3202
3203 // -----------------------------------------------------------------------
3204
3205 wxVariant wxPropertyGrid::GetUncommittedPropertyValue()
3206 {
3207 wxPGProperty* prop = GetSelectedProperty();
3208
3209 if ( !prop )
3210 return wxNullVariant;
3211
3212 wxTextCtrl* tc = GetEditorTextCtrl();
3213 wxVariant value = prop->GetValue();
3214
3215 if ( !tc || !IsEditorsValueModified() )
3216 return value;
3217
3218 if ( !prop->StringToValue(value, tc->GetValue()) )
3219 return value;
3220
3221 if ( !PerformValidation(prop, value, IsStandaloneValidation) )
3222 return prop->GetValue();
3223
3224 return value;
3225 }
3226
3227 // -----------------------------------------------------------------------
3228
3229 // Runs wxValidator for the selected property
3230 bool wxPropertyGrid::DoEditorValidate()
3231 {
3232 return true;
3233 }
3234
3235 // -----------------------------------------------------------------------
3236
3237 void wxPropertyGrid::HandleCustomEditorEvent( wxEvent &event )
3238 {
3239 wxPGProperty* selected = GetSelection();
3240
3241 // Somehow, event is handled after property has been deselected.
3242 // Possibly, but very rare.
3243 if ( !selected || selected->HasFlag(wxPG_PROP_BEING_DELETED) )
3244 return;
3245
3246 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
3247 return;
3248
3249 wxVariant pendingValue(selected->GetValueRef());
3250 wxWindow* wnd = GetEditorControl();
3251 wxWindow* editorWnd = wxDynamicCast(event.GetEventObject(), wxWindow);
3252 int selFlags = 0;
3253 bool wasUnspecified = selected->IsValueUnspecified();
3254 int usesAutoUnspecified = selected->UsesAutoUnspecified();
3255 bool valueIsPending = false;
3256
3257 m_chgInfo_changedProperty = NULL;
3258
3259 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED|wxPG_FL_VALUE_CHANGE_IN_EVENT);
3260
3261 //
3262 // Filter out excess wxTextCtrl modified events
3263 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED &&
3264 wnd &&
3265 wnd->IsKindOf(CLASSINFO(wxTextCtrl)) )
3266 {
3267 wxTextCtrl* tc = (wxTextCtrl*) wnd;
3268
3269 wxString newTcValue = tc->GetValue();
3270 if ( m_prevTcValue == newTcValue )
3271 return;
3272
3273 m_prevTcValue = newTcValue;
3274 }
3275
3276 SetInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3277
3278 bool validationFailure = false;
3279 bool buttonWasHandled = false;
3280
3281 //
3282 // Try common button handling
3283 if ( m_wndEditor2 && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3284 {
3285 wxPGEditorDialogAdapter* adapter = selected->GetEditorDialog();
3286
3287 if ( adapter )
3288 {
3289 buttonWasHandled = true;
3290 // Store as res2, as previously (and still currently alternatively)
3291 // dialogs can be shown by handling wxEVT_COMMAND_BUTTON_CLICKED
3292 // in wxPGProperty::OnEvent().
3293 adapter->ShowDialog( this, selected );
3294 delete adapter;
3295 }
3296 }
3297
3298 if ( !buttonWasHandled )
3299 {
3300 if ( wnd || m_wndEditor2 )
3301 {
3302 // First call editor class' event handler.
3303 const wxPGEditor* editor = selected->GetEditorClass();
3304
3305 if ( editor->OnEvent( this, selected, editorWnd, event ) )
3306 {
3307 // If changes, validate them
3308 if ( DoEditorValidate() )
3309 {
3310 if ( editor->GetValueFromControl( pendingValue,
3311 selected,
3312 wnd ) )
3313 valueIsPending = true;
3314 }
3315 else
3316 {
3317 validationFailure = true;
3318 }
3319 }
3320 }
3321
3322 // Then the property's custom handler (must be always called, unless
3323 // validation failed).
3324 if ( !validationFailure )
3325 buttonWasHandled = selected->OnEvent( this, editorWnd, event );
3326 }
3327
3328 // SetValueInEvent(), as called in one of the functions referred above
3329 // overrides editor's value.
3330 if ( m_iFlags & wxPG_FL_VALUE_CHANGE_IN_EVENT )
3331 {
3332 valueIsPending = true;
3333 pendingValue = m_changeInEventValue;
3334 selFlags |= wxPG_SEL_DIALOGVAL;
3335 }
3336
3337 if ( !validationFailure && valueIsPending )
3338 if ( !PerformValidation(selected, pendingValue) )
3339 validationFailure = true;
3340
3341 if ( validationFailure)
3342 {
3343 OnValidationFailure(selected, pendingValue);
3344 }
3345 else if ( valueIsPending )
3346 {
3347 selFlags |= ( !wasUnspecified && selected->IsValueUnspecified() && usesAutoUnspecified ) ? wxPG_SEL_SETUNSPEC : 0;
3348
3349 DoPropertyChanged(selected, selFlags);
3350 EditorsValueWasNotModified();
3351
3352 // Regardless of editor type, unfocus editor on
3353 // text-editing related enter press.
3354 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3355 {
3356 SetFocusOnCanvas();
3357 }
3358 }
3359 else
3360 {
3361 // No value after all
3362
3363 // Regardless of editor type, unfocus editor on
3364 // text-editing related enter press.
3365 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3366 {
3367 SetFocusOnCanvas();
3368 }
3369
3370 // Let unhandled button click events go to the parent
3371 if ( !buttonWasHandled && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3372 {
3373 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED,GetId());
3374 GetEventHandler()->AddPendingEvent(evt);
3375 }
3376 }
3377
3378 ClearInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3379 }
3380
3381 // -----------------------------------------------------------------------
3382 // wxPropertyGrid editor control helper methods
3383 // -----------------------------------------------------------------------
3384
3385 wxRect wxPropertyGrid::GetEditorWidgetRect( wxPGProperty* p, int column ) const
3386 {
3387 int itemy = p->GetY2(m_lineHeight);
3388 int vy = 0;
3389 int splitterX = m_pState->DoGetSplitterPosition(column-1);
3390 int colEnd = splitterX + m_pState->m_colWidths[column];
3391 int imageOffset = 0;
3392
3393 // TODO: If custom image detection changes from current, change this.
3394 if ( m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE )
3395 {
3396 //m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3397 int iw = p->OnMeasureImage().x;
3398 if ( iw < 1 )
3399 iw = wxPG_CUSTOM_IMAGE_WIDTH;
3400 imageOffset = p->GetImageOffset(iw);
3401 }
3402
3403 return wxRect
3404 (
3405 splitterX+imageOffset+wxPG_XBEFOREWIDGET+wxPG_CONTROL_MARGIN+1,
3406 itemy-vy,
3407 colEnd-splitterX-wxPG_XBEFOREWIDGET-wxPG_CONTROL_MARGIN-imageOffset-1,
3408 m_lineHeight-1
3409 );
3410 }
3411
3412 // -----------------------------------------------------------------------
3413
3414 wxRect wxPropertyGrid::GetImageRect( wxPGProperty* p, int item ) const
3415 {
3416 wxSize sz = GetImageSize(p, item);
3417 return wxRect(wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
3418 wxPG_CUSTOM_IMAGE_SPACINGY,
3419 sz.x,
3420 sz.y);
3421 }
3422
3423 // return size of custom paint image
3424 wxSize wxPropertyGrid::GetImageSize( wxPGProperty* p, int item ) const
3425 {
3426 // If called with NULL property, then return default image
3427 // size for properties that use image.
3428 if ( !p )
3429 return wxSize(wxPG_CUSTOM_IMAGE_WIDTH,wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight));
3430
3431 wxSize cis = p->OnMeasureImage(item);
3432
3433 int choiceCount = p->m_choices.GetCount();
3434 int comVals = p->GetDisplayedCommonValueCount();
3435 if ( item >= choiceCount && comVals > 0 )
3436 {
3437 unsigned int cvi = item-choiceCount;
3438 cis = GetCommonValue(cvi)->GetRenderer()->GetImageSize(NULL, 1, cvi);
3439 }
3440 else if ( item >= 0 && choiceCount == 0 )
3441 return wxSize(0, 0);
3442
3443 if ( cis.x < 0 )
3444 {
3445 if ( cis.x <= -1 )
3446 cis.x = wxPG_CUSTOM_IMAGE_WIDTH;
3447 }
3448 if ( cis.y <= 0 )
3449 {
3450 if ( cis.y >= -1 )
3451 cis.y = wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight);
3452 else
3453 cis.y = -cis.y;
3454 }
3455 return cis;
3456 }
3457
3458 // -----------------------------------------------------------------------
3459
3460 // takes scrolling into account
3461 void wxPropertyGrid::ImprovedClientToScreen( int* px, int* py )
3462 {
3463 int vx, vy;
3464 GetViewStart(&vx,&vy);
3465 vy*=wxPG_PIXELS_PER_UNIT;
3466 vx*=wxPG_PIXELS_PER_UNIT;
3467 *px -= vx;
3468 *py -= vy;
3469 ClientToScreen( px, py );
3470 }
3471
3472 // -----------------------------------------------------------------------
3473
3474 wxPropertyGridHitTestResult wxPropertyGrid::HitTest( const wxPoint& pt ) const
3475 {
3476 wxPoint pt2;
3477 GetViewStart(&pt2.x,&pt2.y);
3478 pt2.x *= wxPG_PIXELS_PER_UNIT;
3479 pt2.y *= wxPG_PIXELS_PER_UNIT;
3480 pt2.x += pt.x;
3481 pt2.y += pt.y;
3482
3483 return m_pState->HitTest(pt2);
3484 }
3485
3486 // -----------------------------------------------------------------------
3487
3488 // custom set cursor
3489 void wxPropertyGrid::CustomSetCursor( int type, bool override )
3490 {
3491 if ( type == m_curcursor && !override ) return;
3492
3493 wxCursor* cursor = &wxPG_DEFAULT_CURSOR;
3494
3495 if ( type == wxCURSOR_SIZEWE )
3496 cursor = m_cursorSizeWE;
3497
3498 m_canvas->SetCursor( *cursor );
3499
3500 m_curcursor = type;
3501 }
3502
3503 // -----------------------------------------------------------------------
3504 // wxPropertyGrid property selection, editor creation
3505 // -----------------------------------------------------------------------
3506
3507 //
3508 // This class forwards events from property editor controls to wxPropertyGrid.
3509 class wxPropertyGridEditorEventForwarder : public wxEvtHandler
3510 {
3511 public:
3512 wxPropertyGridEditorEventForwarder( wxPropertyGrid* propGrid )
3513 : wxEvtHandler(), m_propGrid(propGrid)
3514 {
3515 }
3516
3517 virtual ~wxPropertyGridEditorEventForwarder()
3518 {
3519 }
3520
3521 private:
3522 bool ProcessEvent( wxEvent& event )
3523 {
3524 // Always skip
3525 event.Skip();
3526
3527 m_propGrid->HandleCustomEditorEvent(event);
3528
3529 return wxEvtHandler::ProcessEvent(event);
3530 }
3531
3532 wxPropertyGrid* m_propGrid;
3533 };
3534
3535 // Setups event handling for child control
3536 void wxPropertyGrid::SetupChildEventHandling( wxWindow* argWnd )
3537 {
3538 wxWindowID id = argWnd->GetId();
3539
3540 if ( argWnd == m_wndEditor )
3541 {
3542 argWnd->Connect(id, wxEVT_MOTION,
3543 wxMouseEventHandler(wxPropertyGrid::OnMouseMoveChild),
3544 NULL, this);
3545 argWnd->Connect(id, wxEVT_LEFT_UP,
3546 wxMouseEventHandler(wxPropertyGrid::OnMouseUpChild),
3547 NULL, this);
3548 argWnd->Connect(id, wxEVT_LEFT_DOWN,
3549 wxMouseEventHandler(wxPropertyGrid::OnMouseClickChild),
3550 NULL, this);
3551 argWnd->Connect(id, wxEVT_RIGHT_UP,
3552 wxMouseEventHandler(wxPropertyGrid::OnMouseRightClickChild),
3553 NULL, this);
3554 argWnd->Connect(id, wxEVT_ENTER_WINDOW,
3555 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3556 NULL, this);
3557 argWnd->Connect(id, wxEVT_LEAVE_WINDOW,
3558 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3559 NULL, this);
3560 }
3561
3562 wxPropertyGridEditorEventForwarder* forwarder;
3563 forwarder = new wxPropertyGridEditorEventForwarder(this);
3564 argWnd->PushEventHandler(forwarder);
3565
3566 argWnd->Connect(id, wxEVT_KEY_DOWN,
3567 wxCharEventHandler(wxPropertyGrid::OnChildKeyDown),
3568 NULL, this);
3569 }
3570
3571 void wxPropertyGrid::DestroyEditorWnd( wxWindow* wnd )
3572 {
3573 if ( !wnd )
3574 return;
3575
3576 wnd->Hide();
3577
3578 // Do not free editors immediately (for sake of processing events)
3579 wxPendingDelete.Append(wnd);
3580 }
3581
3582 void wxPropertyGrid::FreeEditors()
3583 {
3584 //
3585 // Return focus back to canvas from children (this is required at least for
3586 // GTK+, which, unlike Windows, clears focus when control is destroyed
3587 // instead of moving it to closest parent).
3588 wxWindow* focus = wxWindow::FindFocus();
3589 if ( focus )
3590 {
3591 wxWindow* parent = focus->GetParent();
3592 while ( parent )
3593 {
3594 if ( parent == m_canvas )
3595 {
3596 SetFocusOnCanvas();
3597 break;
3598 }
3599 parent = parent->GetParent();
3600 }
3601 }
3602
3603 // Do not free editors immediately if processing events
3604 if ( m_wndEditor2 )
3605 {
3606 wxEvtHandler* handler = m_wndEditor2->PopEventHandler(false);
3607 m_wndEditor2->Hide();
3608 wxPendingDelete.Append( handler );
3609 DestroyEditorWnd(m_wndEditor2);
3610 m_wndEditor2 = NULL;
3611 }
3612
3613 if ( m_wndEditor )
3614 {
3615 wxEvtHandler* handler = m_wndEditor->PopEventHandler(false);
3616 m_wndEditor->Hide();
3617 wxPendingDelete.Append( handler );
3618 DestroyEditorWnd(m_wndEditor);
3619 m_wndEditor = NULL;
3620 }
3621 }
3622
3623 // Call with NULL to de-select property
3624 bool wxPropertyGrid::DoSelectProperty( wxPGProperty* p, unsigned int flags )
3625 {
3626 /*
3627 if (p)
3628 {
3629 wxLogDebug(wxT("SelectProperty( %s (%s[%i]) )"),p->m_label.c_str(),
3630 p->m_parent->m_label.c_str(),p->GetIndexInParent());
3631 }
3632 else
3633 {
3634 wxLogDebug(wxT("SelectProperty( NULL, -1 )"));
3635 }
3636 */
3637
3638 if ( m_inDoSelectProperty )
3639 return true;
3640
3641 m_inDoSelectProperty = 1;
3642
3643 if ( !m_pState )
3644 {
3645 m_inDoSelectProperty = 0;
3646 return false;
3647 }
3648
3649 wxArrayPGProperty prevSelection = m_pState->m_selection;
3650 wxPGProperty* prevFirstSel;
3651
3652 if ( prevSelection.size() > 0 )
3653 prevFirstSel = prevSelection[0];
3654 else
3655 prevFirstSel = NULL;
3656
3657 if ( prevFirstSel && prevFirstSel->HasFlag(wxPG_PROP_BEING_DELETED) )
3658 prevFirstSel = NULL;
3659
3660 // Always send event, as this is indirect call
3661 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
3662
3663 /*
3664 if ( prevFirstSel )
3665 wxPrintf( "Selected %s\n", prevFirstSel->GetClassInfo()->GetClassName() );
3666 else
3667 wxPrintf( "None selected\n" );
3668
3669 if (p)
3670 wxPrintf( "P = %s\n", p->GetClassInfo()->GetClassName() );
3671 else
3672 wxPrintf( "P = NULL\n" );
3673 */
3674
3675 // If we are frozen, then just set the values.
3676 if ( m_frozen )
3677 {
3678 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3679 m_editorFocused = 0;
3680 m_pState->DoSetSelection(p);
3681
3682 // If frozen, always free controls. But don't worry, as Thaw will
3683 // recall SelectProperty to recreate them.
3684 FreeEditors();
3685
3686 // Prevent any further selection measures in this call
3687 p = NULL;
3688 }
3689 else
3690 {
3691 // Is it the same?
3692 if ( prevFirstSel == p &&
3693 prevSelection.size() <= 1 &&
3694 !(flags & wxPG_SEL_FORCE) )
3695 {
3696 // Only set focus if not deselecting
3697 if ( p )
3698 {
3699 if ( flags & wxPG_SEL_FOCUS )
3700 {
3701 if ( m_wndEditor )
3702 {
3703 m_wndEditor->SetFocus();
3704 m_editorFocused = 1;
3705 }
3706 }
3707 else
3708 {
3709 SetFocusOnCanvas();
3710 }
3711 }
3712
3713 m_inDoSelectProperty = 0;
3714 return true;
3715 }
3716
3717 //
3718 // First, deactivate previous
3719 if ( prevFirstSel )
3720 {
3721 OnValidationFailureReset(prevFirstSel);
3722
3723 // Must double-check if this is an selected in case of forceswitch
3724 if ( p != prevFirstSel )
3725 {
3726 if ( !CommitChangesFromEditor(flags) )
3727 {
3728 // Validation has failed, so we can't exit the previous editor
3729 //::wxMessageBox(_("Please correct the value or press ESC to cancel the edit."),
3730 // _("Invalid Value"),wxOK|wxICON_ERROR);
3731 m_inDoSelectProperty = 0;
3732 return false;
3733 }
3734 }
3735
3736 FreeEditors();
3737
3738 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3739 EditorsValueWasNotModified();
3740 }
3741
3742 SetInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3743
3744 m_pState->DoSetSelection(p);
3745
3746 // Redraw unselected
3747 for ( unsigned int i=0; i<prevSelection.size(); i++ )
3748 {
3749 DrawItem(prevSelection[i]);
3750 }
3751
3752 //
3753 // Then, activate the one given.
3754 if ( p )
3755 {
3756 int propY = p->GetY2(m_lineHeight);
3757
3758 int splitterX = GetSplitterPosition();
3759 m_editorFocused = 0;
3760 m_iFlags |= wxPG_FL_PRIMARY_FILLS_ENTIRE;
3761 if ( p != prevFirstSel )
3762 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED);
3763
3764 wxASSERT( m_wndEditor == NULL );
3765
3766 //
3767 // Only create editor for non-disabled non-caption
3768 if ( !p->IsCategory() && !(p->m_flags & wxPG_PROP_DISABLED) )
3769 {
3770 // do this for non-caption items
3771
3772 m_selColumn = 1;
3773
3774 // Do we need to paint the custom image, if any?
3775 m_iFlags &= ~(wxPG_FL_CUR_USES_CUSTOM_IMAGE);
3776 if ( (p->m_flags & wxPG_PROP_CUSTOMIMAGE) &&
3777 !p->GetEditorClass()->CanContainCustomImage()
3778 )
3779 m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3780
3781 wxRect grect = GetEditorWidgetRect(p, m_selColumn);
3782 wxPoint goodPos = grect.GetPosition();
3783
3784 const wxPGEditor* editor = p->GetEditorClass();
3785 wxCHECK_MSG(editor, false,
3786 wxT("NULL editor class not allowed"));
3787
3788 m_iFlags &= ~wxPG_FL_FIXED_WIDTH_EDITOR;
3789
3790 wxPGWindowList wndList = editor->CreateControls(this,
3791 p,
3792 goodPos,
3793 grect.GetSize());
3794
3795 m_wndEditor = wndList.m_primary;
3796 m_wndEditor2 = wndList.m_secondary;
3797 wxWindow* primaryCtrl = GetEditorControl();
3798
3799 //
3800 // Essentially, primaryCtrl == m_wndEditor
3801 //
3802
3803 // NOTE: It is allowed for m_wndEditor to be NULL - in this case
3804 // value is drawn as normal, and m_wndEditor2 is assumed
3805 // to be a right-aligned button that triggers a separate editorCtrl
3806 // window.
3807
3808 if ( m_wndEditor )
3809 {
3810 wxASSERT_MSG( m_wndEditor->GetParent() == GetPanel(),
3811 wxT("CreateControls must use result of wxPropertyGrid::GetPanel() as parent of controls.") );
3812
3813 // Set validator, if any
3814 #if wxUSE_VALIDATORS
3815 wxValidator* validator = p->GetValidator();
3816 if ( validator )
3817 primaryCtrl->SetValidator(*validator);
3818 #endif
3819
3820 if ( m_wndEditor->GetSize().y > (m_lineHeight+6) )
3821 m_iFlags |= wxPG_FL_ABNORMAL_EDITOR;
3822
3823 // If it has modified status, use bold font
3824 // (must be done before capturing m_ctrlXAdjust)
3825 if ( (p->m_flags & wxPG_PROP_MODIFIED) && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3826 SetCurControlBoldFont();
3827
3828 //
3829 // Fix TextCtrl indentation
3830 #if defined(__WXMSW__) && !defined(__WXWINCE__)
3831 wxTextCtrl* tc = NULL;
3832 if ( primaryCtrl->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
3833 tc = ((wxOwnerDrawnComboBox*)primaryCtrl)->GetTextCtrl();
3834 else
3835 tc = wxDynamicCast(primaryCtrl, wxTextCtrl);
3836 if ( tc )
3837 ::SendMessage(GetHwndOf(tc), EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELONG(0, 0));
3838 #endif
3839
3840 // Store x relative to splitter (we'll need it).
3841 m_ctrlXAdjust = m_wndEditor->GetPosition().x - splitterX;
3842
3843 // Check if background clear is not necessary
3844 wxPoint pos = m_wndEditor->GetPosition();
3845 if ( pos.x > (splitterX+1) || pos.y > propY )
3846 {
3847 m_iFlags &= ~(wxPG_FL_PRIMARY_FILLS_ENTIRE);
3848 }
3849
3850 m_wndEditor->SetSizeHints(3, 3);
3851
3852 SetupChildEventHandling(primaryCtrl);
3853
3854 // Focus and select all (wxTextCtrl, wxComboBox etc)
3855 if ( flags & wxPG_SEL_FOCUS )
3856 {
3857 primaryCtrl->SetFocus();
3858
3859 p->GetEditorClass()->OnFocus(p, primaryCtrl);
3860 }
3861 }
3862
3863 if ( m_wndEditor2 )
3864 {
3865 wxASSERT_MSG( m_wndEditor2->GetParent() == GetPanel(),
3866 wxT("CreateControls must use result of wxPropertyGrid::GetPanel() as parent of controls.") );
3867
3868 // Get proper id for wndSecondary
3869 m_wndSecId = m_wndEditor2->GetId();
3870 wxWindowList children = m_wndEditor2->GetChildren();
3871 wxWindowList::iterator node = children.begin();
3872 if ( node != children.end() )
3873 m_wndSecId = ((wxWindow*)*node)->GetId();
3874
3875 m_wndEditor2->SetSizeHints(3,3);
3876
3877 m_wndEditor2->Show();
3878
3879 SetupChildEventHandling(m_wndEditor2);
3880
3881 // If no primary editor, focus to button to allow
3882 // it to interprete ENTER etc.
3883 // NOTE: Due to problems focusing away from it, this
3884 // has been disabled.
3885 /*
3886 if ( (flags & wxPG_SEL_FOCUS) && !m_wndEditor )
3887 m_wndEditor2->SetFocus();
3888 */
3889 }
3890
3891 if ( flags & wxPG_SEL_FOCUS )
3892 m_editorFocused = 1;
3893
3894 }
3895 else
3896 {
3897 // Make sure focus is in grid canvas (important for wxGTK, at least)
3898 SetFocusOnCanvas();
3899 }
3900
3901 EditorsValueWasNotModified();
3902
3903 // If it's inside collapsed section, expand parent, scroll, etc.
3904 // Also, if it was partially visible, scroll it into view.
3905 if ( !(flags & wxPG_SEL_NONVISIBLE) )
3906 EnsureVisible( p );
3907
3908 if ( m_wndEditor )
3909 {
3910 m_wndEditor->Show(true);
3911 }
3912
3913 if ( !(flags & wxPG_SEL_NO_REFRESH) )
3914 DrawItem(p);
3915 }
3916 else
3917 {
3918 // Make sure focus is in grid canvas
3919 SetFocusOnCanvas();
3920 }
3921
3922 ClearInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3923 }
3924
3925 #if wxUSE_STATUSBAR
3926
3927 //
3928 // Show help text in status bar.
3929 // (if found and grid not embedded in manager with help box and
3930 // style wxPG_EX_HELP_AS_TOOLTIPS is not used).
3931 //
3932
3933 if ( !(GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS) )
3934 {
3935 wxStatusBar* statusbar = NULL;
3936 if ( !(m_iFlags & wxPG_FL_NOSTATUSBARHELP) )
3937 {
3938 wxFrame* frame = wxDynamicCast(::wxGetTopLevelParent(this),wxFrame);
3939 if ( frame )
3940 statusbar = frame->GetStatusBar();
3941 }
3942
3943 if ( statusbar )
3944 {
3945 const wxString* pHelpString = (const wxString*) NULL;
3946
3947 if ( p )
3948 {
3949 pHelpString = &p->GetHelpString();
3950 if ( pHelpString->length() )
3951 {
3952 // Set help box text.
3953 statusbar->SetStatusText( *pHelpString );
3954 m_iFlags |= wxPG_FL_STRING_IN_STATUSBAR;
3955 }
3956 }
3957
3958 if ( (!pHelpString || !pHelpString->length()) &&
3959 (m_iFlags & wxPG_FL_STRING_IN_STATUSBAR) )
3960 {
3961 // Clear help box - but only if it was written
3962 // by us at previous time.
3963 statusbar->SetStatusText( m_emptyString );
3964 m_iFlags &= ~(wxPG_FL_STRING_IN_STATUSBAR);
3965 }
3966 }
3967 }
3968 #endif
3969
3970 m_inDoSelectProperty = 0;
3971
3972 // call wx event handler (here so that it also occurs on deselection)
3973 if ( !(flags & wxPG_SEL_DONT_SEND_EVENT) )
3974 SendEvent( wxEVT_PG_SELECTED, p, NULL );
3975
3976 return true;
3977 }
3978
3979 // -----------------------------------------------------------------------
3980
3981 bool wxPropertyGrid::UnfocusEditor()
3982 {
3983 wxPGProperty* selected = GetSelection();
3984
3985 if ( !selected || !m_wndEditor || m_frozen )
3986 return true;
3987
3988 if ( !CommitChangesFromEditor(0) )
3989 return false;
3990
3991 SetFocusOnCanvas();
3992 DrawItem(selected);
3993
3994 return true;
3995 }
3996
3997 // -----------------------------------------------------------------------
3998
3999 void wxPropertyGrid::RefreshEditor()
4000 {
4001 wxPGProperty* p = GetSelection();
4002 if ( !p )
4003 return;
4004
4005 wxWindow* wnd = GetEditorControl();
4006 if ( !wnd )
4007 return;
4008
4009 // Set editor font boldness - must do this before
4010 // calling UpdateControl().
4011 if ( HasFlag(wxPG_BOLD_MODIFIED) )
4012 {
4013 if ( p->HasFlag(wxPG_PROP_MODIFIED) )
4014 wnd->SetFont(GetCaptionFont());
4015 else
4016 wnd->SetFont(GetFont());
4017 }
4018
4019 const wxPGEditor* editorClass = p->GetEditorClass();
4020
4021 editorClass->UpdateControl(p, wnd);
4022
4023 if ( p->IsValueUnspecified() )
4024 editorClass ->SetValueToUnspecified(p, wnd);
4025 }
4026
4027 // -----------------------------------------------------------------------
4028
4029 bool wxPropertyGrid::SelectProperty( wxPGPropArg id, bool focus )
4030 {
4031 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
4032
4033 int flags = wxPG_SEL_DONT_SEND_EVENT;
4034 if ( focus )
4035 flags |= wxPG_SEL_FOCUS;
4036
4037 return DoSelectProperty(p, flags);
4038 }
4039
4040 // -----------------------------------------------------------------------
4041 // wxPropertyGrid expand/collapse state
4042 // -----------------------------------------------------------------------
4043
4044 bool wxPropertyGrid::DoCollapse( wxPGProperty* p, bool sendEvents )
4045 {
4046 wxPGProperty* pwc = wxStaticCast(p, wxPGProperty);
4047 wxPGProperty* selected = GetSelection();
4048
4049 // If active editor was inside collapsed section, then disable it
4050 if ( selected && selected->IsSomeParent(p) )
4051 {
4052 DoClearSelection();
4053 }
4054
4055 // Store dont-center-splitter flag 'cause we need to temporarily set it
4056 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
4057 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4058
4059 bool res = m_pState->DoCollapse(pwc);
4060
4061 if ( res )
4062 {
4063 if ( sendEvents )
4064 SendEvent( wxEVT_PG_ITEM_COLLAPSED, p );
4065
4066 RecalculateVirtualSize();
4067
4068 // Redraw etc. only if collapsed was visible.
4069 if (pwc->IsVisible() &&
4070 !m_frozen &&
4071 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) ) )
4072 {
4073 // When item is collapsed so that scrollbar would move,
4074 // graphics mess is about (unless we redraw everything).
4075 Refresh();
4076 }
4077 }
4078
4079 // Clear dont-center-splitter flag if it wasn't set
4080 m_iFlags = (m_iFlags & ~wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
4081
4082 return res;
4083 }
4084
4085 // -----------------------------------------------------------------------
4086
4087 bool wxPropertyGrid::DoExpand( wxPGProperty* p, bool sendEvents )
4088 {
4089 wxCHECK_MSG( p, false, wxT("invalid property id") );
4090
4091 wxPGProperty* pwc = (wxPGProperty*)p;
4092
4093 // Store dont-center-splitter flag 'cause we need to temporarily set it
4094 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
4095 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4096
4097 bool res = m_pState->DoExpand(pwc);
4098
4099 if ( res )
4100 {
4101 if ( sendEvents )
4102 SendEvent( wxEVT_PG_ITEM_EXPANDED, p );
4103
4104 RecalculateVirtualSize();
4105
4106 // Redraw etc. only if expanded was visible.
4107 if ( pwc->IsVisible() && !m_frozen &&
4108 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) )
4109 )
4110 {
4111 // Redraw
4112 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4113 Refresh();
4114 #else
4115 DrawItems(pwc, NULL);
4116 #endif
4117 }
4118 }
4119
4120 // Clear dont-center-splitter flag if it wasn't set
4121 m_iFlags = (m_iFlags & ~wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
4122
4123 return res;
4124 }
4125
4126 // -----------------------------------------------------------------------
4127
4128 bool wxPropertyGrid::DoHideProperty( wxPGProperty* p, bool hide, int flags )
4129 {
4130 if ( m_frozen )
4131 return m_pState->DoHideProperty(p, hide, flags);
4132
4133 wxArrayPGProperty selection = m_pState->m_selection; // Must use a copy
4134 int selRemoveCount = 0;
4135 for ( unsigned int i=0; i<selection.size(); i++ )
4136 {
4137 wxPGProperty* selected = selection[i];
4138 if ( selected == p || selected->IsSomeParent(p) )
4139 {
4140 if ( !DoRemoveFromSelection(p, flags) )
4141 return false;
4142 selRemoveCount += 1;
4143 }
4144 }
4145
4146 m_pState->DoHideProperty(p, hide, flags);
4147
4148 RecalculateVirtualSize();
4149 Refresh();
4150
4151 return true;
4152 }
4153
4154
4155 // -----------------------------------------------------------------------
4156 // wxPropertyGrid size related methods
4157 // -----------------------------------------------------------------------
4158
4159 void wxPropertyGrid::RecalculateVirtualSize( int forceXPos )
4160 {
4161 if ( (m_iFlags & wxPG_FL_RECALCULATING_VIRTUAL_SIZE) || m_frozen )
4162 return;
4163
4164 //
4165 // If virtual height was changed, then recalculate editor control position(s)
4166 if ( m_pState->m_vhCalcPending )
4167 CorrectEditorWidgetPosY();
4168
4169 m_pState->EnsureVirtualHeight();
4170
4171 wxASSERT_LEVEL_2_MSG(
4172 m_pState->GetVirtualHeight() == m_pState->GetActualVirtualHeight(),
4173 "VirtualHeight and ActualVirtualHeight should match"
4174 );
4175
4176 m_iFlags |= wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4177
4178 int x = m_pState->m_width;
4179 int y = m_pState->m_virtualHeight;
4180
4181 int width, height;
4182 GetClientSize(&width,&height);
4183
4184 // Now adjust virtual size.
4185 SetVirtualSize(x, y);
4186
4187 int xAmount = 0;
4188 int xPos = 0;
4189
4190 //
4191 // Adjust scrollbars
4192 if ( HasVirtualWidth() )
4193 {
4194 xAmount = x/wxPG_PIXELS_PER_UNIT;
4195 xPos = GetScrollPos( wxHORIZONTAL );
4196 }
4197
4198 if ( forceXPos != -1 )
4199 xPos = forceXPos;
4200 // xPos too high?
4201 else if ( xPos > (xAmount-(width/wxPG_PIXELS_PER_UNIT)) )
4202 xPos = 0;
4203
4204 int yAmount = y / wxPG_PIXELS_PER_UNIT;
4205 int yPos = GetScrollPos( wxVERTICAL );
4206
4207 SetScrollbars( wxPG_PIXELS_PER_UNIT, wxPG_PIXELS_PER_UNIT,
4208 xAmount, yAmount, xPos, yPos, true );
4209
4210 // Must re-get size now
4211 GetClientSize(&width,&height);
4212
4213 if ( !HasVirtualWidth() )
4214 {
4215 m_pState->SetVirtualWidth(width);
4216 x = width;
4217 }
4218
4219 m_width = width;
4220 m_height = height;
4221
4222 m_canvas->SetSize( x, y );
4223
4224 m_pState->CheckColumnWidths();
4225
4226 if ( GetSelection() )
4227 CorrectEditorWidgetSizeX();
4228
4229 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4230 }
4231
4232 // -----------------------------------------------------------------------
4233
4234 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4235 {
4236 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4237 return;
4238
4239 int width, height;
4240 GetClientSize(&width,&height);
4241
4242 m_width = width;
4243 m_height = height;
4244
4245 #if wxPG_DOUBLE_BUFFER
4246 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4247 {
4248 int dblh = (m_lineHeight*2);
4249 if ( !m_doubleBuffer )
4250 {
4251 // Create double buffer bitmap to draw on, if none
4252 int w = (width>250)?width:250;
4253 int h = height + dblh;
4254 h = (h>400)?h:400;
4255 m_doubleBuffer = new wxBitmap( w, h );
4256 }
4257 else
4258 {
4259 int w = m_doubleBuffer->GetWidth();
4260 int h = m_doubleBuffer->GetHeight();
4261
4262 // Double buffer must be large enough
4263 if ( w < width || h < (height+dblh) )
4264 {
4265 if ( w < width ) w = width;
4266 if ( h < (height+dblh) ) h = height + dblh;
4267 delete m_doubleBuffer;
4268 m_doubleBuffer = new wxBitmap( w, h );
4269 }
4270 }
4271 }
4272
4273 #endif
4274
4275 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4276 m_ncWidth = event.GetSize().x;
4277
4278 if ( !m_frozen )
4279 {
4280 if ( m_pState->m_itemsAdded )
4281 PrepareAfterItemsAdded();
4282 else
4283 // Without this, virtual size (atleast under wxGTK) will be skewed
4284 RecalculateVirtualSize();
4285
4286 Refresh();
4287 }
4288 }
4289
4290 // -----------------------------------------------------------------------
4291
4292 void wxPropertyGrid::SetVirtualWidth( int width )
4293 {
4294 if ( width == -1 )
4295 {
4296 // Disable virtual width
4297 width = GetClientSize().x;
4298 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4299 }
4300 else
4301 {
4302 // Enable virtual width
4303 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4304 }
4305 m_pState->SetVirtualWidth( width );
4306 }
4307
4308 void wxPropertyGrid::SetFocusOnCanvas()
4309 {
4310 m_canvas->SetFocusIgnoringChildren();
4311 m_editorFocused = 0;
4312 }
4313
4314 // -----------------------------------------------------------------------
4315 // wxPropertyGrid mouse event handling
4316 // -----------------------------------------------------------------------
4317
4318 // selFlags uses same values DoSelectProperty's flags
4319 // Returns true if event was vetoed.
4320 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p,
4321 wxVariant* pValue,
4322 unsigned int selFlags,
4323 unsigned int column )
4324 {
4325 // Send property grid event of specific type and with specific property
4326 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4327 evt.SetPropertyGrid(this);
4328 evt.SetEventObject(m_eventObject);
4329 evt.SetProperty(p);
4330 evt.SetColumn(column);
4331 if ( pValue )
4332 {
4333 evt.SetCanVeto(true);
4334 evt.SetupValidationInfo();
4335 m_validationInfo.m_pValue = pValue;
4336 }
4337 else if ( !(selFlags & wxPG_SEL_NOVALIDATE) )
4338 {
4339 evt.SetCanVeto(true);
4340 }
4341
4342 wxEvtHandler* evtHandler = m_eventObject->GetEventHandler();
4343
4344 evtHandler->ProcessEvent(evt);
4345
4346 return evt.WasVetoed();
4347 }
4348
4349 // -----------------------------------------------------------------------
4350
4351 // Return false if should be skipped
4352 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4353 {
4354 bool res = true;
4355
4356 // Need to set focus?
4357 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4358 {
4359 SetFocusOnCanvas();
4360 }
4361
4362 wxPropertyGridPageState* state = m_pState;
4363 int splitterHit;
4364 int splitterHitOffset;
4365 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4366
4367 wxPGProperty* p = DoGetItemAtY(y);
4368
4369 if ( p )
4370 {
4371 int depth = (int)p->GetDepth() - 1;
4372
4373 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4374
4375 if ( x >= marginEnds )
4376 {
4377 // Outside margin.
4378
4379 if ( p->IsCategory() )
4380 {
4381 // This is category.
4382 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4383
4384 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4385
4386 // Expand, collapse, activate etc. if click on text or left of splitter.
4387 if ( x >= textX
4388 &&
4389 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4390 columnHit == 0
4391 )
4392 )
4393 {
4394 if ( !AddToSelectionFromInputEvent( p,
4395 columnHit,
4396 &event ) )
4397 return res;
4398
4399 // On double-click, expand/collapse.
4400 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4401 {
4402 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4403 else DoExpand( p, true );
4404 }
4405 }
4406 }
4407 else if ( splitterHit == -1 )
4408 {
4409 // Click on value.
4410 unsigned int selFlag = 0;
4411 if ( columnHit == 1 )
4412 {
4413 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4414 selFlag = wxPG_SEL_FOCUS;
4415 }
4416 if ( !AddToSelectionFromInputEvent( p,
4417 columnHit,
4418 &event,
4419 selFlag ) )
4420 return res;
4421
4422 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4423
4424 if ( p->GetChildCount() && !p->IsCategory() )
4425 // On double-click, expand/collapse.
4426 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4427 {
4428 wxPGProperty* pwc = (wxPGProperty*)p;
4429 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4430 else DoExpand( p, true );
4431 }
4432
4433 res = false;
4434 }
4435 else
4436 {
4437 // click on splitter
4438 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4439 {
4440 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4441 {
4442 // Double-clicking the splitter causes auto-centering
4443 CenterSplitter( true );
4444 }
4445 else if ( m_dragStatus == 0 )
4446 {
4447 //
4448 // Begin draggin the splitter
4449 //
4450
4451 // send event
4452 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
4453
4454 if ( m_wndEditor )
4455 {
4456 // Changes must be committed here or the
4457 // value won't be drawn correctly
4458 if ( !CommitChangesFromEditor() )
4459 return res;
4460
4461 m_wndEditor->Show ( false );
4462 }
4463
4464 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4465 {
4466 m_canvas->CaptureMouse();
4467 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4468 }
4469
4470 m_dragStatus = 1;
4471 m_draggedSplitter = splitterHit;
4472 m_dragOffset = splitterHitOffset;
4473
4474 wxClientDC dc(m_canvas);
4475
4476 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4477 // Fixes button disappearance bug
4478 if ( m_wndEditor2 )
4479 m_wndEditor2->Show ( false );
4480 #endif
4481
4482 m_startingSplitterX = x - splitterHitOffset;
4483 }
4484 }
4485 }
4486 }
4487 else
4488 {
4489 // Click on margin.
4490 if ( p->GetChildCount() )
4491 {
4492 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4493
4494 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4495 {
4496 int y2 = y % m_lineHeight;
4497 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4498 {
4499 // On click on expander button, expand/collapse
4500 if ( ((wxPGProperty*)p)->IsExpanded() )
4501 DoCollapse( p, true );
4502 else
4503 DoExpand( p, true );
4504 }
4505 }
4506 }
4507 }
4508 }
4509 return res;
4510 }
4511
4512 // -----------------------------------------------------------------------
4513
4514 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x),
4515 unsigned int WXUNUSED(y),
4516 wxMouseEvent& event )
4517 {
4518 if ( m_propHover )
4519 {
4520 // Select property here as well
4521 wxPGProperty* p = m_propHover;
4522 AddToSelectionFromInputEvent(p, m_colHover, &event);
4523
4524 // Send right click event.
4525 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4526
4527 return true;
4528 }
4529 return false;
4530 }
4531
4532 // -----------------------------------------------------------------------
4533
4534 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x),
4535 unsigned int WXUNUSED(y),
4536 wxMouseEvent& event )
4537 {
4538 if ( m_propHover )
4539 {
4540 // Select property here as well
4541 wxPGProperty* p = m_propHover;
4542
4543 AddToSelectionFromInputEvent(p, m_colHover, &event);
4544
4545 // Send double-click event.
4546 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4547
4548 return true;
4549 }
4550 return false;
4551 }
4552
4553 // -----------------------------------------------------------------------
4554
4555 #if wxPG_SUPPORT_TOOLTIPS
4556
4557 void wxPropertyGrid::SetToolTip( const wxString& tipString )
4558 {
4559 if ( tipString.length() )
4560 {
4561 m_canvas->SetToolTip(tipString);
4562 }
4563 else
4564 {
4565 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4566 m_canvas->SetToolTip( m_emptyString );
4567 #else
4568 m_canvas->SetToolTip( NULL );
4569 #endif
4570 }
4571 }
4572
4573 #endif // #if wxPG_SUPPORT_TOOLTIPS
4574
4575 // -----------------------------------------------------------------------
4576
4577 // Return false if should be skipped
4578 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y, wxMouseEvent &event )
4579 {
4580 // Safety check (needed because mouse capturing may
4581 // otherwise freeze the control)
4582 if ( m_dragStatus > 0 && !event.Dragging() )
4583 {
4584 HandleMouseUp(x,y,event);
4585 }
4586
4587 wxPropertyGridPageState* state = m_pState;
4588 int splitterHit;
4589 int splitterHitOffset;
4590 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4591 int splitterX = x - splitterHitOffset;
4592
4593 m_colHover = columnHit;
4594
4595 if ( m_dragStatus > 0 )
4596 {
4597 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4598 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4599 {
4600
4601 int newSplitterX = x - m_dragOffset;
4602 int splitterX = x - splitterHitOffset;
4603
4604 // Splitter redraw required?
4605 if ( newSplitterX != splitterX )
4606 {
4607 // Move everything
4608 SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
4609 state->DoSetSplitterPosition( newSplitterX, m_draggedSplitter, false );
4610 state->m_fSplitterX = (float) newSplitterX;
4611
4612 if ( GetSelection() )
4613 CorrectEditorWidgetSizeX();
4614
4615 Update();
4616 Refresh();
4617 }
4618
4619 m_dragStatus = 2;
4620 }
4621
4622 return false;
4623 }
4624 else
4625 {
4626
4627 int ih = m_lineHeight;
4628 int sy = y;
4629
4630 #if wxPG_SUPPORT_TOOLTIPS
4631 wxPGProperty* prevHover = m_propHover;
4632 unsigned char prevSide = m_mouseSide;
4633 #endif
4634 int curPropHoverY = y - (y % ih);
4635
4636 // On which item it hovers
4637 if ( !m_propHover
4638 ||
4639 ( sy < m_propHoverY || sy >= (m_propHoverY+ih) )
4640 )
4641 {
4642 // Mouse moves on another property
4643
4644 m_propHover = DoGetItemAtY(y);
4645 m_propHoverY = curPropHoverY;
4646
4647 // Send hover event
4648 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
4649 }
4650
4651 #if wxPG_SUPPORT_TOOLTIPS
4652 // Store which side we are on
4653 m_mouseSide = 0;
4654 if ( columnHit == 1 )
4655 m_mouseSide = 2;
4656 else if ( columnHit == 0 )
4657 m_mouseSide = 1;
4658
4659 //
4660 // If tooltips are enabled, show label or value as a tip
4661 // in case it doesn't otherwise show in full length.
4662 //
4663 if ( m_windowStyle & wxPG_TOOLTIPS )
4664 {
4665 wxToolTip* tooltip = m_canvas->GetToolTip();
4666
4667 if ( m_propHover != prevHover || prevSide != m_mouseSide )
4668 {
4669 if ( m_propHover && !m_propHover->IsCategory() )
4670 {
4671
4672 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
4673 {
4674 // Show help string as a tooltip
4675 wxString tipString = m_propHover->GetHelpString();
4676
4677 SetToolTip(tipString);
4678 }
4679 else
4680 {
4681 // Show cropped value string as a tooltip
4682 wxString tipString;
4683 int space = 0;
4684
4685 if ( m_mouseSide == 1 )
4686 {
4687 tipString = m_propHover->m_label;
4688 space = splitterX-m_marginWidth-3;
4689 }
4690 else if ( m_mouseSide == 2 )
4691 {
4692 tipString = m_propHover->GetDisplayedString();
4693
4694 space = m_width - splitterX;
4695 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
4696 space -= wxPG_CUSTOM_IMAGE_WIDTH + wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
4697 }
4698
4699 if ( space )
4700 {
4701 int tw, th;
4702 GetTextExtent( tipString, &tw, &th, 0, 0 );
4703 if ( tw > space )
4704 {
4705 SetToolTip( tipString );
4706 }
4707 }
4708 else
4709 {
4710 if ( tooltip )
4711 {
4712 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4713 m_canvas->SetToolTip( m_emptyString );
4714 #else
4715 m_canvas->SetToolTip( NULL );
4716 #endif
4717 }
4718 }
4719
4720 }
4721 }
4722 else
4723 {
4724 if ( tooltip )
4725 {
4726 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4727 m_canvas->SetToolTip( m_emptyString );
4728 #else
4729 m_canvas->SetToolTip( NULL );
4730 #endif
4731 }
4732 }
4733 }
4734 }
4735 #endif
4736
4737 if ( splitterHit == -1 ||
4738 !m_propHover ||
4739 HasFlag(wxPG_STATIC_SPLITTER) )
4740 {
4741 // hovering on something else
4742 if ( m_curcursor != wxCURSOR_ARROW )
4743 CustomSetCursor( wxCURSOR_ARROW );
4744 }
4745 else
4746 {
4747 // Do not allow splitter cursor on caption items.
4748 // (also not if we were dragging and its started
4749 // outside the splitter region)
4750
4751 if ( !m_propHover->IsCategory() &&
4752 !event.Dragging() )
4753 {
4754
4755 // hovering on splitter
4756
4757 // NB: Condition disabled since MouseLeave event (from the editor control) cannot be
4758 // reliably detected.
4759 //if ( m_curcursor != wxCURSOR_SIZEWE )
4760 CustomSetCursor( wxCURSOR_SIZEWE, true );
4761
4762 return false;
4763 }
4764 else
4765 {
4766 // hovering on something else
4767 if ( m_curcursor != wxCURSOR_ARROW )
4768 CustomSetCursor( wxCURSOR_ARROW );
4769 }
4770 }
4771
4772 //
4773 // Multi select by dragging
4774 //
4775 if ( GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION &&
4776 event.LeftIsDown() &&
4777 m_propHover &&
4778 GetSelection() &&
4779 !state->DoIsPropertySelected(m_propHover) )
4780 {
4781 DoAddToSelection(m_propHover);
4782 }
4783 }
4784 return true;
4785 }
4786
4787 // -----------------------------------------------------------------------
4788
4789 // Also handles Leaving event
4790 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
4791 wxMouseEvent &WXUNUSED(event) )
4792 {
4793 wxPropertyGridPageState* state = m_pState;
4794 bool res = false;
4795
4796 int splitterHit;
4797 int splitterHitOffset;
4798 state->HitTestH( x, &splitterHit, &splitterHitOffset );
4799
4800 // No event type check - basicly calling this method should
4801 // just stop dragging.
4802 // Left up after dragged?
4803 if ( m_dragStatus >= 1 )
4804 {
4805 //
4806 // End Splitter Dragging
4807 //
4808 // DO NOT ENABLE FOLLOWING LINE!
4809 // (it is only here as a reminder to not to do it)
4810 //splitterX = x;
4811
4812 // Disable splitter auto-centering
4813 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4814
4815 // This is necessary to return cursor
4816 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
4817 {
4818 m_canvas->ReleaseMouse();
4819 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
4820 }
4821
4822 // Set back the default cursor, if necessary
4823 if ( splitterHit == -1 ||
4824 !m_propHover )
4825 {
4826 CustomSetCursor( wxCURSOR_ARROW );
4827 }
4828
4829 m_dragStatus = 0;
4830
4831 // Control background needs to be cleared
4832 wxPGProperty* selected = GetSelection();
4833 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && selected )
4834 DrawItem( selected );
4835
4836 if ( m_wndEditor )
4837 {
4838 m_wndEditor->Show ( true );
4839 }
4840
4841 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4842 // Fixes button disappearance bug
4843 if ( m_wndEditor2 )
4844 m_wndEditor2->Show ( true );
4845 #endif
4846
4847 // This clears the focus.
4848 m_editorFocused = 0;
4849
4850 }
4851 return res;
4852 }
4853
4854 // -----------------------------------------------------------------------
4855
4856 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
4857 {
4858 int splitterX = GetSplitterPosition();
4859
4860 //int ux, uy;
4861 //CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
4862 int ux = event.m_x;
4863 int uy = event.m_y;
4864
4865 wxWindow* wnd = GetEditorControl();
4866
4867 // Hide popup on clicks
4868 if ( event.GetEventType() != wxEVT_MOTION )
4869 if ( wnd && wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
4870 {
4871 ((wxOwnerDrawnComboBox*)wnd)->HidePopup();
4872 }
4873
4874 wxRect r;
4875 if ( wnd )
4876 r = wnd->GetRect();
4877 if ( wnd == NULL || m_dragStatus ||
4878 (
4879 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
4880 ux >= (r.x+r.width) ||
4881 event.m_y < r.y ||
4882 event.m_y >= (r.y+r.height)
4883 )
4884 )
4885 {
4886 *px = ux;
4887 *py = uy;
4888 return true;
4889 }
4890 else
4891 {
4892 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
4893 }
4894 return false;
4895 }
4896
4897 // -----------------------------------------------------------------------
4898
4899 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
4900 {
4901 int x, y;
4902 if ( OnMouseCommon( event, &x, &y ) )
4903 {
4904 HandleMouseClick(x,y,event);
4905 }
4906 event.Skip();
4907 }
4908
4909 // -----------------------------------------------------------------------
4910
4911 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
4912 {
4913 int x, y;
4914 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
4915 HandleMouseRightClick(x,y,event);
4916 event.Skip();
4917 }
4918
4919 // -----------------------------------------------------------------------
4920
4921 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
4922 {
4923 // Always run standard mouse-down handler as well
4924 OnMouseClick(event);
4925
4926 int x, y;
4927 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
4928 HandleMouseDoubleClick(x,y,event);
4929 event.Skip();
4930 }
4931
4932 // -----------------------------------------------------------------------
4933
4934 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
4935 {
4936 int x, y;
4937 if ( OnMouseCommon( event, &x, &y ) )
4938 {
4939 HandleMouseMove(x,y,event);
4940 }
4941 event.Skip();
4942 }
4943
4944 // -----------------------------------------------------------------------
4945
4946 void wxPropertyGrid::OnMouseMoveBottom( wxMouseEvent& WXUNUSED(event) )
4947 {
4948 // Called when mouse moves in the empty space below the properties.
4949 CustomSetCursor( wxCURSOR_ARROW );
4950 }
4951
4952 // -----------------------------------------------------------------------
4953
4954 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
4955 {
4956 int x, y;
4957 if ( OnMouseCommon( event, &x, &y ) )
4958 {
4959 HandleMouseUp(x,y,event);
4960 }
4961 event.Skip();
4962 }
4963
4964 // -----------------------------------------------------------------------
4965
4966 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
4967 {
4968 // This may get called from child control as well, so event's
4969 // mouse position cannot be relied on.
4970
4971 if ( event.Entering() )
4972 {
4973 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
4974 {
4975 // TODO: Fix this (detect parent and only do
4976 // cursor trick if it is a manager).
4977 wxASSERT( GetParent() );
4978 GetParent()->SetCursor(wxNullCursor);
4979
4980 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
4981 }
4982 else
4983 GetParent()->SetCursor(wxNullCursor);
4984 }
4985 else if ( event.Leaving() )
4986 {
4987 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
4988 m_canvas->SetCursor( wxNullCursor );
4989
4990 // Get real cursor position
4991 wxPoint pt = ScreenToClient(::wxGetMousePosition());
4992
4993 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
4994 {
4995 {
4996 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
4997 {
4998 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
4999 }
5000
5001 if ( m_dragStatus )
5002 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
5003 }
5004 }
5005 }
5006
5007 event.Skip();
5008 }
5009
5010 // -----------------------------------------------------------------------
5011
5012 // Common code used by various OnMouseXXXChild methods.
5013 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
5014 {
5015 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
5016 wxASSERT( topCtrlWnd );
5017 int x, y;
5018 event.GetPosition(&x,&y);
5019
5020 int splitterX = GetSplitterPosition();
5021
5022 wxRect r = topCtrlWnd->GetRect();
5023 if ( !m_dragStatus &&
5024 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
5025 y >= 0 && y < r.height \
5026 )
5027 {
5028 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5029 event.Skip();
5030 }
5031 else
5032 {
5033 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
5034 px, py );
5035 return true;
5036 }
5037 return false;
5038 }
5039
5040 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
5041 {
5042 int x,y;
5043 if ( OnMouseChildCommon(event,&x,&y) )
5044 {
5045 bool res = HandleMouseClick(x,y,event);
5046 if ( !res ) event.Skip();
5047 }
5048 }
5049
5050 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
5051 {
5052 int x,y;
5053 wxASSERT( m_wndEditor );
5054 // These coords may not be exact (about +-2),
5055 // but that should not matter (right click is about item, not position).
5056 wxPoint pt = m_wndEditor->GetPosition();
5057 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
5058
5059 // FIXME: Used to set m_propHover to selection here. Was it really
5060 // necessary?
5061
5062 bool res = HandleMouseRightClick(x,y,event);
5063 if ( !res ) event.Skip();
5064 }
5065
5066 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
5067 {
5068 int x,y;
5069 if ( OnMouseChildCommon(event,&x,&y) )
5070 {
5071 bool res = HandleMouseMove(x,y,event);
5072 if ( !res ) event.Skip();
5073 }
5074 }
5075
5076 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
5077 {
5078 int x,y;
5079 if ( OnMouseChildCommon(event,&x,&y) )
5080 {
5081 bool res = HandleMouseUp(x,y,event);
5082 if ( !res ) event.Skip();
5083 }
5084 }
5085
5086 // -----------------------------------------------------------------------
5087 // wxPropertyGrid keyboard event handling
5088 // -----------------------------------------------------------------------
5089
5090 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
5091 {
5092 // Translates wxKeyEvent to wxPG_ACTION_XXX
5093
5094 int keycode = event.GetKeyCode();
5095 int modifiers = event.GetModifiers();
5096
5097 wxASSERT( !(modifiers&~(0xFFFF)) );
5098
5099 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5100
5101 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
5102
5103 if ( it == m_actionTriggers.end() )
5104 return 0;
5105
5106 if ( pSecond )
5107 {
5108 int second = (it->second>>16) & 0xFFFF;
5109 *pSecond = second;
5110 }
5111
5112 return (it->second & 0xFFFF);
5113 }
5114
5115 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
5116 {
5117 wxASSERT( !(modifiers&~(0xFFFF)) );
5118
5119 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5120
5121 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
5122
5123 if ( it != m_actionTriggers.end() )
5124 {
5125 // This key combination is already used
5126
5127 // Can add secondary?
5128 wxASSERT_MSG( !(it->second&~(0xFFFF)),
5129 wxT("You can only add up to two separate actions per key combination.") );
5130
5131 action = it->second | (action<<16);
5132 }
5133
5134 m_actionTriggers[hashMapKey] = action;
5135 }
5136
5137 void wxPropertyGrid::ClearActionTriggers( int action )
5138 {
5139 wxPGHashMapI2I::iterator it;
5140
5141 for ( it = m_actionTriggers.begin(); it != m_actionTriggers.end(); ++it )
5142 {
5143 if ( it->second == action )
5144 {
5145 m_actionTriggers.erase(it);
5146 }
5147 }
5148 }
5149
5150 void wxPropertyGrid::HandleKeyEvent( wxKeyEvent &event, bool fromChild )
5151 {
5152 //
5153 // Handles key event when editor control is not focused.
5154 //
5155
5156 wxCHECK2(!m_frozen, return);
5157
5158 // Travelsal between items, collapsing/expanding, etc.
5159 wxPGProperty* selected = GetSelection();
5160 int keycode = event.GetKeyCode();
5161 bool editorFocused = IsEditorFocused();
5162
5163 if ( keycode == WXK_TAB )
5164 {
5165 wxWindow* mainControl;
5166
5167 if ( HasInternalFlag(wxPG_FL_IN_MANAGER) )
5168 mainControl = GetParent();
5169 else
5170 mainControl = this;
5171
5172 if ( !event.ShiftDown() )
5173 {
5174 if ( !editorFocused && m_wndEditor )
5175 {
5176 DoSelectProperty( selected, wxPG_SEL_FOCUS );
5177 }
5178 else
5179 {
5180 // Tab traversal workaround for platforms on which
5181 // wxWindow::Navigate() may navigate into first child
5182 // instead of next sibling. Does not work perfectly
5183 // in every scenario (for instance, when property grid
5184 // is either first or last control).
5185 #if defined(__WXGTK__)
5186 wxWindow* sibling = mainControl->GetNextSibling();
5187 if ( sibling )
5188 sibling->SetFocusFromKbd();
5189 #else
5190 Navigate(wxNavigationKeyEvent::IsForward);
5191 #endif
5192 }
5193 }
5194 else
5195 {
5196 if ( editorFocused )
5197 {
5198 UnfocusEditor();
5199 }
5200 else
5201 {
5202 #if defined(__WXGTK__)
5203 wxWindow* sibling = mainControl->GetPrevSibling();
5204 if ( sibling )
5205 sibling->SetFocusFromKbd();
5206 #else
5207 Navigate(wxNavigationKeyEvent::IsBackward);
5208 #endif
5209 }
5210 }
5211
5212 return;
5213 }
5214
5215 // Ignore Alt and Control when they are down alone
5216 if ( keycode == WXK_ALT ||
5217 keycode == WXK_CONTROL )
5218 {
5219 event.Skip();
5220 return;
5221 }
5222
5223 int secondAction;
5224 int action = KeyEventToActions(event, &secondAction);
5225
5226 if ( editorFocused && action == wxPG_ACTION_CANCEL_EDIT )
5227 {
5228 //
5229 // Esc cancels any changes
5230 if ( IsEditorsValueModified() )
5231 {
5232 EditorsValueWasNotModified();
5233
5234 // Update the control as well
5235 selected->GetEditorClass()->
5236 SetControlStringValue( selected,
5237 GetEditorControl(),
5238 selected->GetDisplayedString() );
5239 }
5240
5241 OnValidationFailureReset(selected);
5242
5243 UnfocusEditor();
5244 return;
5245 }
5246
5247 // Except for TAB and ESC, handle child control events in child control
5248 if ( fromChild )
5249 {
5250 // Only propagate event if it had modifiers
5251 if ( !event.HasModifiers() )
5252 {
5253 event.StopPropagation();
5254 }
5255 event.Skip();
5256 return;
5257 }
5258
5259 bool wasHandled = false;
5260
5261 if ( selected )
5262 {
5263 // Show dialog?
5264 if ( ButtonTriggerKeyTest(action, event) )
5265 return;
5266
5267 wxPGProperty* p = selected;
5268
5269 // Travel and expand/collapse
5270 int selectDir = -2;
5271
5272 if ( p->GetChildCount() )
5273 {
5274 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5275 {
5276 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5277 wasHandled = true;
5278 }
5279 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5280 {
5281 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5282 wasHandled = true;
5283 }
5284 }
5285
5286 if ( !wasHandled )
5287 {
5288 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5289 {
5290 selectDir = -1;
5291 }
5292 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5293 {
5294 selectDir = 1;
5295 }
5296 }
5297
5298 if ( selectDir >= -1 )
5299 {
5300 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5301 if ( p )
5302 DoSelectProperty(p);
5303 wasHandled = true;
5304 }
5305 }
5306 else
5307 {
5308 // If nothing was selected, select the first item now
5309 // (or navigate out of tab).
5310 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5311 {
5312 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5313 if ( p ) DoSelectProperty(p);
5314 wasHandled = true;
5315 }
5316 }
5317
5318 if ( !wasHandled )
5319 event.Skip();
5320 }
5321
5322 // -----------------------------------------------------------------------
5323
5324 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5325 {
5326 // If there was editor open and focused, then this event should not
5327 // really be processed here.
5328 if ( IsEditorFocused() )
5329 {
5330 // However, if event had modifiers, it is probably still best
5331 // to skip it.
5332 if ( event.HasModifiers() )
5333 event.Skip();
5334 else
5335 event.StopPropagation();
5336 return;
5337 }
5338
5339 HandleKeyEvent(event, false);
5340 }
5341
5342 // -----------------------------------------------------------------------
5343
5344 bool wxPropertyGrid::ButtonTriggerKeyTest( int action, wxKeyEvent& event )
5345 {
5346 if ( action == -1 )
5347 {
5348 int secondAction;
5349 action = KeyEventToActions(event, &secondAction);
5350 }
5351
5352 // Does the keycode trigger button?
5353 if ( action == wxPG_ACTION_PRESS_BUTTON &&
5354 m_wndEditor2 )
5355 {
5356 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, m_wndEditor2->GetId());
5357 GetEventHandler()->AddPendingEvent(evt);
5358 return true;
5359 }
5360
5361 return false;
5362 }
5363
5364 // -----------------------------------------------------------------------
5365
5366 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5367 {
5368 HandleKeyEvent(event, true);
5369 }
5370
5371 // -----------------------------------------------------------------------
5372 // wxPropertyGrid miscellaneous event handling
5373 // -----------------------------------------------------------------------
5374
5375 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5376 {
5377 //
5378 // Check if the focus is in this control or one of its children
5379 wxWindow* newFocused = wxWindow::FindFocus();
5380
5381 if ( newFocused != m_curFocused )
5382 HandleFocusChange( newFocused );
5383
5384 //
5385 // Check if top-level parent has changed
5386 wxWindow* tlp = ::wxGetTopLevelParent(this);
5387 if ( tlp != m_tlp )
5388 {
5389 OnTLPChanging(tlp);
5390 }
5391 }
5392
5393 bool wxPropertyGrid::IsEditorFocused() const
5394 {
5395 wxWindow* focus = wxWindow::FindFocus();
5396
5397 if ( focus == m_wndEditor || focus == m_wndEditor2 ||
5398 focus == GetEditorControl() )
5399 return true;
5400
5401 return false;
5402 }
5403
5404 // Called by focus event handlers. newFocused is the window that becomes focused.
5405 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5406 {
5407 unsigned int oldFlags = m_iFlags;
5408
5409 m_iFlags &= ~(wxPG_FL_FOCUSED);
5410
5411 wxWindow* parent = newFocused;
5412
5413 // This must be one of nextFocus' parents.
5414 while ( parent )
5415 {
5416 // Use m_eventObject, which is either wxPropertyGrid or
5417 // wxPropertyGridManager, as appropriate.
5418 if ( parent == m_eventObject )
5419 {
5420 m_iFlags |= wxPG_FL_FOCUSED;
5421 break;
5422 }
5423 parent = parent->GetParent();
5424 }
5425
5426 m_curFocused = newFocused;
5427
5428 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5429 (oldFlags & wxPG_FL_FOCUSED) )
5430 {
5431 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5432 {
5433 // Need to store changed value
5434 CommitChangesFromEditor();
5435 }
5436 else
5437 {
5438 /*
5439 //
5440 // Preliminary code for tab-order respecting
5441 // tab-traversal (but should be moved to
5442 // OnNav handler)
5443 //
5444 wxWindow* prevFocus = event.GetWindow();
5445 wxWindow* useThis = this;
5446 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5447 useThis = GetParent();
5448
5449 if ( prevFocus &&
5450 prevFocus->GetParent() == useThis->GetParent() )
5451 {
5452 wxList& children = useThis->GetParent()->GetChildren();
5453
5454 wxNode* node = children.Find(prevFocus);
5455
5456 if ( node->GetNext() &&
5457 useThis == node->GetNext()->GetData() )
5458 DoSelectProperty(GetFirst());
5459 else if ( node->GetPrevious () &&
5460 useThis == node->GetPrevious()->GetData() )
5461 DoSelectProperty(GetLastProperty());
5462
5463 }
5464 */
5465 }
5466
5467 // Redraw selected
5468 wxPGProperty* selected = GetSelection();
5469 if ( selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5470 DrawItem( selected );
5471 }
5472 }
5473
5474 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5475 {
5476 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5477 HandleFocusChange((wxWindow*)event.GetEventObject());
5478 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5479 //else if ( event.GetWindow() )
5480 else
5481 HandleFocusChange(event.GetWindow());
5482
5483 event.Skip();
5484 }
5485
5486 // -----------------------------------------------------------------------
5487
5488 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5489 {
5490 HandleFocusChange((wxWindow*)event.GetEventObject());
5491 event.Skip();
5492 }
5493
5494 // -----------------------------------------------------------------------
5495
5496 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5497 {
5498 m_iFlags |= wxPG_FL_SCROLLED;
5499
5500 event.Skip();
5501 }
5502
5503 // -----------------------------------------------------------------------
5504
5505 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5506 {
5507 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5508 {
5509 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5510 }
5511 }
5512
5513 // -----------------------------------------------------------------------
5514 // Property editor related functions
5515 // -----------------------------------------------------------------------
5516
5517 // noDefCheck = true prevents infinite recursion.
5518 wxPGEditor* wxPropertyGrid::DoRegisterEditorClass( wxPGEditor* editorClass,
5519 const wxString& editorName,
5520 bool noDefCheck )
5521 {
5522 wxASSERT( editorClass );
5523
5524 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5525 RegisterDefaultEditors();
5526
5527 wxString name = editorName;
5528 if ( name.length() == 0 )
5529 name = editorClass->GetName();
5530
5531 // Existing editor under this name?
5532 wxPGHashMapS2P::iterator vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5533
5534 if ( vt_it != wxPGGlobalVars->m_mapEditorClasses.end() )
5535 {
5536 // If this name was already used, try class name.
5537 name = editorClass->GetClassInfo()->GetClassName();
5538 vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5539 }
5540
5541 wxCHECK_MSG( vt_it == wxPGGlobalVars->m_mapEditorClasses.end(),
5542 (wxPGEditor*) vt_it->second,
5543 "Editor with given name was already registered" );
5544
5545 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorClass;
5546
5547 return editorClass;
5548 }
5549
5550 // Use this in RegisterDefaultEditors.
5551 #define wxPGRegisterDefaultEditorClass(EDITOR) \
5552 if ( wxPGEditor_##EDITOR == NULL ) \
5553 { \
5554 wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
5555 new wxPG##EDITOR##Editor, true ); \
5556 }
5557
5558 // Registers all default editor classes
5559 void wxPropertyGrid::RegisterDefaultEditors()
5560 {
5561 wxPGRegisterDefaultEditorClass( TextCtrl );
5562 wxPGRegisterDefaultEditorClass( Choice );
5563 wxPGRegisterDefaultEditorClass( ComboBox );
5564 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
5565 #if wxPG_INCLUDE_CHECKBOX
5566 wxPGRegisterDefaultEditorClass( CheckBox );
5567 #endif
5568 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
5569
5570 // Register SpinCtrl etc. editors before use
5571 RegisterAdditionalEditors();
5572 }
5573
5574 // -----------------------------------------------------------------------
5575 // wxPGStringTokenizer
5576 // Needed to handle C-style string lists (e.g. "str1" "str2")
5577 // -----------------------------------------------------------------------
5578
5579 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
5580 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
5581 {
5582 }
5583
5584 wxPGStringTokenizer::~wxPGStringTokenizer()
5585 {
5586 }
5587
5588 bool wxPGStringTokenizer::HasMoreTokens()
5589 {
5590 const wxString& str = *m_str;
5591
5592 wxString::const_iterator i = m_curPos;
5593
5594 wxUniChar delim = m_delimeter;
5595 wxUniChar a;
5596 wxUniChar prev_a = wxT('\0');
5597
5598 bool inToken = false;
5599
5600 while ( i != str.end() )
5601 {
5602 a = *i;
5603
5604 if ( !inToken )
5605 {
5606 if ( a == delim )
5607 {
5608 inToken = true;
5609 m_readyToken.clear();
5610 }
5611 }
5612 else
5613 {
5614 if ( prev_a != wxT('\\') )
5615 {
5616 if ( a != delim )
5617 {
5618 if ( a != wxT('\\') )
5619 m_readyToken << a;
5620 }
5621 else
5622 {
5623 ++i;
5624 m_curPos = i;
5625 return true;
5626 }
5627 prev_a = a;
5628 }
5629 else
5630 {
5631 m_readyToken << a;
5632 prev_a = wxT('\0');
5633 }
5634 }
5635 ++i;
5636 }
5637
5638 m_curPos = str.end();
5639
5640 if ( inToken )
5641 return true;
5642
5643 return false;
5644 }
5645
5646 wxString wxPGStringTokenizer::GetNextToken()
5647 {
5648 return m_readyToken;
5649 }
5650
5651 // -----------------------------------------------------------------------
5652 // wxPGChoiceEntry
5653 // -----------------------------------------------------------------------
5654
5655 wxPGChoiceEntry::wxPGChoiceEntry()
5656 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
5657 {
5658 }
5659
5660 // -----------------------------------------------------------------------
5661 // wxPGChoicesData
5662 // -----------------------------------------------------------------------
5663
5664 wxPGChoicesData::wxPGChoicesData()
5665 {
5666 }
5667
5668 wxPGChoicesData::~wxPGChoicesData()
5669 {
5670 Clear();
5671 }
5672
5673 void wxPGChoicesData::Clear()
5674 {
5675 m_items.clear();
5676 }
5677
5678 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
5679 {
5680 wxASSERT( m_items.size() == 0 );
5681
5682 m_items = data->m_items;
5683 }
5684
5685 wxPGChoiceEntry& wxPGChoicesData::Insert( int index,
5686 const wxPGChoiceEntry& item )
5687 {
5688 wxVector<wxPGChoiceEntry>::iterator it;
5689 if ( index == -1 )
5690 {
5691 it = m_items.end();
5692 index = (int) m_items.size();
5693 }
5694 else
5695 {
5696 it = m_items.begin() + index;
5697 }
5698
5699 m_items.insert(it, item);
5700
5701 wxPGChoiceEntry& ownEntry = m_items[index];
5702
5703 // Need to fix value?
5704 if ( ownEntry.GetValue() == wxPG_INVALID_VALUE )
5705 ownEntry.SetValue(index);
5706
5707 return ownEntry;
5708 }
5709
5710 // -----------------------------------------------------------------------
5711 // wxPropertyGridEvent
5712 // -----------------------------------------------------------------------
5713
5714 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
5715
5716
5717 wxDEFINE_EVENT( wxEVT_PG_SELECTED, wxPropertyGridEvent );
5718 wxDEFINE_EVENT( wxEVT_PG_CHANGING, wxPropertyGridEvent );
5719 wxDEFINE_EVENT( wxEVT_PG_CHANGED, wxPropertyGridEvent );
5720 wxDEFINE_EVENT( wxEVT_PG_HIGHLIGHTED, wxPropertyGridEvent );
5721 wxDEFINE_EVENT( wxEVT_PG_RIGHT_CLICK, wxPropertyGridEvent );
5722 wxDEFINE_EVENT( wxEVT_PG_PAGE_CHANGED, wxPropertyGridEvent );
5723 wxDEFINE_EVENT( wxEVT_PG_ITEM_EXPANDED, wxPropertyGridEvent );
5724 wxDEFINE_EVENT( wxEVT_PG_ITEM_COLLAPSED, wxPropertyGridEvent );
5725 wxDEFINE_EVENT( wxEVT_PG_DOUBLE_CLICK, wxPropertyGridEvent );
5726 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_BEGIN, wxPropertyGridEvent );
5727 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_ENDING, wxPropertyGridEvent );
5728
5729 // -----------------------------------------------------------------------
5730
5731 void wxPropertyGridEvent::Init()
5732 {
5733 m_validationInfo = NULL;
5734 m_column = 1;
5735 m_canVeto = false;
5736 m_wasVetoed = false;
5737 }
5738
5739 // -----------------------------------------------------------------------
5740
5741 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
5742 : wxCommandEvent(commandType,id)
5743 {
5744 m_property = NULL;
5745 Init();
5746 }
5747
5748 // -----------------------------------------------------------------------
5749
5750 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
5751 : wxCommandEvent(event)
5752 {
5753 m_eventType = event.GetEventType();
5754 m_eventObject = event.m_eventObject;
5755 m_pg = event.m_pg;
5756 m_property = event.m_property;
5757 m_validationInfo = event.m_validationInfo;
5758 m_canVeto = event.m_canVeto;
5759 m_wasVetoed = event.m_wasVetoed;
5760 }
5761
5762 // -----------------------------------------------------------------------
5763
5764 wxPropertyGridEvent::~wxPropertyGridEvent()
5765 {
5766 }
5767
5768 // -----------------------------------------------------------------------
5769
5770 wxEvent* wxPropertyGridEvent::Clone() const
5771 {
5772 return new wxPropertyGridEvent( *this );
5773 }
5774
5775 // -----------------------------------------------------------------------
5776 // wxPropertyGridPopulator
5777 // -----------------------------------------------------------------------
5778
5779 wxPropertyGridPopulator::wxPropertyGridPopulator()
5780 {
5781 m_state = NULL;
5782 m_pg = NULL;
5783 wxPGGlobalVars->m_offline++;
5784 }
5785
5786 // -----------------------------------------------------------------------
5787
5788 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
5789 {
5790 m_state = state;
5791 m_propHierarchy.clear();
5792 }
5793
5794 // -----------------------------------------------------------------------
5795
5796 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
5797 {
5798 m_pg = pg;
5799 pg->Freeze();
5800 }
5801
5802 // -----------------------------------------------------------------------
5803
5804 wxPropertyGridPopulator::~wxPropertyGridPopulator()
5805 {
5806 //
5807 // Free unused sets of choices
5808 wxPGHashMapS2P::iterator it;
5809
5810 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
5811 {
5812 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
5813 data->DecRef();
5814 }
5815
5816 if ( m_pg )
5817 {
5818 m_pg->Thaw();
5819 m_pg->GetPanel()->Refresh();
5820 }
5821 wxPGGlobalVars->m_offline--;
5822 }
5823
5824 // -----------------------------------------------------------------------
5825
5826 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
5827 const wxString& propLabel,
5828 const wxString& propName,
5829 const wxString* propValue,
5830 wxPGChoices* pChoices )
5831 {
5832 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
5833 wxPGProperty* parent = GetCurParent();
5834
5835 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
5836 {
5837 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
5838 return NULL;
5839 }
5840
5841 if ( !classInfo || !classInfo->IsKindOf(CLASSINFO(wxPGProperty)) )
5842 {
5843 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
5844 return NULL;
5845 }
5846
5847 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
5848
5849 property->SetLabel(propLabel);
5850 property->DoSetName(propName);
5851
5852 if ( pChoices && pChoices->IsOk() )
5853 property->SetChoices(*pChoices);
5854
5855 m_state->DoInsert(parent, -1, property);
5856
5857 if ( propValue )
5858 property->SetValueFromString( *propValue, wxPG_FULL_VALUE|
5859 wxPG_PROGRAMMATIC_VALUE );
5860
5861 return property;
5862 }
5863
5864 // -----------------------------------------------------------------------
5865
5866 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
5867 {
5868 m_propHierarchy.push_back(property);
5869 DoScanForChildren();
5870 m_propHierarchy.pop_back();
5871 }
5872
5873 // -----------------------------------------------------------------------
5874
5875 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
5876 const wxString& idString )
5877 {
5878 wxPGChoices choices;
5879
5880 // Using id?
5881 if ( choicesString[0] == wxT('@') )
5882 {
5883 wxString ids = choicesString.substr(1);
5884 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
5885 if ( it == m_dictIdChoices.end() )
5886 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
5887 else
5888 choices.AssignData((wxPGChoicesData*)it->second);
5889 }
5890 else
5891 {
5892 bool found = false;
5893 if ( idString.length() )
5894 {
5895 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
5896 if ( it != m_dictIdChoices.end() )
5897 {
5898 choices.AssignData((wxPGChoicesData*)it->second);
5899 found = true;
5900 }
5901 }
5902
5903 if ( !found )
5904 {
5905 // Parse choices string
5906 wxString::const_iterator it = choicesString.begin();
5907 wxString label;
5908 wxString value;
5909 int state = 0;
5910 bool labelValid = false;
5911
5912 for ( ; it != choicesString.end(); ++it )
5913 {
5914 wxChar c = *it;
5915
5916 if ( state != 1 )
5917 {
5918 if ( c == wxT('"') )
5919 {
5920 if ( labelValid )
5921 {
5922 long l;
5923 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
5924 choices.Add(label, l);
5925 }
5926 labelValid = false;
5927 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
5928 value.clear();
5929 label.clear();
5930 state = 1;
5931 }
5932 else if ( c == wxT('=') )
5933 {
5934 if ( labelValid )
5935 {
5936 state = 2;
5937 }
5938 }
5939 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
5940 {
5941 value << c;
5942 }
5943 }
5944 else
5945 {
5946 if ( c == wxT('"') )
5947 {
5948 state = 0;
5949 labelValid = true;
5950 }
5951 else
5952 label << c;
5953 }
5954 }
5955
5956 if ( labelValid )
5957 {
5958 long l;
5959 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
5960 choices.Add(label, l);
5961 }
5962
5963 if ( !choices.IsOk() )
5964 {
5965 choices.EnsureData();
5966 }
5967
5968 // Assign to id
5969 if ( idString.length() )
5970 m_dictIdChoices[idString] = choices.GetData();
5971 }
5972 }
5973
5974 return choices;
5975 }
5976
5977 // -----------------------------------------------------------------------
5978
5979 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
5980 {
5981 if ( s.Last() == wxT('%') )
5982 {
5983 wxString s2 = s.substr(0,s.length()-1);
5984 long val;
5985 if ( s2.ToLong(&val, 10) )
5986 {
5987 *pval = (val*max)/100;
5988 return true;
5989 }
5990 return false;
5991 }
5992
5993 return s.ToLong(pval, 10);
5994 }
5995
5996 // -----------------------------------------------------------------------
5997
5998 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
5999 const wxString& type,
6000 const wxString& value )
6001 {
6002 int l = m_propHierarchy.size();
6003 if ( !l )
6004 return false;
6005
6006 wxPGProperty* p = m_propHierarchy[l-1];
6007 wxString valuel = value.Lower();
6008 wxVariant variant;
6009
6010 if ( type.length() == 0 )
6011 {
6012 long v;
6013
6014 // Auto-detect type
6015 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6016 variant = true;
6017 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6018 variant = false;
6019 else if ( value.ToLong(&v, 0) )
6020 variant = v;
6021 else
6022 variant = value;
6023 }
6024 else
6025 {
6026 if ( type == wxT("string") )
6027 {
6028 variant = value;
6029 }
6030 else if ( type == wxT("int") )
6031 {
6032 long v = 0;
6033 value.ToLong(&v, 0);
6034 variant = v;
6035 }
6036 else if ( type == wxT("bool") )
6037 {
6038 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6039 variant = true;
6040 else
6041 variant = false;
6042 }
6043 else
6044 {
6045 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6046 return false;
6047 }
6048 }
6049
6050 p->SetAttribute( name, variant );
6051
6052 return true;
6053 }
6054
6055 // -----------------------------------------------------------------------
6056
6057 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6058 {
6059 wxLogError(_("Error in resource: %s"),msg.c_str());
6060 }
6061
6062 // -----------------------------------------------------------------------
6063
6064 #endif // wxUSE_PROPGRID