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