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