Store property name and value in wxPropertyGridEvent, keep track of live event instan...
[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_THREAD
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 if ( pendingValue.GetType() == wxPG_VARIANT_TYPE_LIST )
2907 {
2908 if ( !p->ValidateValue(pendingValue, m_validationInfo) )
2909 return false;
2910 }
2911
2912 //
2913 // Adapt list to child values, if necessary
2914 wxVariant listValue = pendingValue;
2915 wxVariant* pPendingValue = &pendingValue;
2916 wxVariant* pList = NULL;
2917
2918 // If parent has wxPG_PROP_AGGREGATE flag, or uses composite
2919 // string value, then we need treat as it was changed instead
2920 // (or, in addition, as is the case with composite string parent).
2921 // This includes creating list variant for child values.
2922
2923 wxPGProperty* pwc = p->GetParent();
2924 wxPGProperty* changedProperty = p;
2925 wxPGProperty* baseChangedProperty = changedProperty;
2926 wxVariant bcpPendingList;
2927
2928 listValue = pendingValue;
2929 listValue.SetName(p->GetBaseName());
2930
2931 while ( pwc &&
2932 (pwc->HasFlag(wxPG_PROP_AGGREGATE) || pwc->HasFlag(wxPG_PROP_COMPOSED_VALUE)) )
2933 {
2934 wxVariantList tempList;
2935 wxVariant lv(tempList, pwc->GetBaseName());
2936 lv.Append(listValue);
2937 listValue = lv;
2938 pPendingValue = &listValue;
2939
2940 if ( pwc->HasFlag(wxPG_PROP_AGGREGATE) )
2941 {
2942 baseChangedProperty = pwc;
2943 bcpPendingList = lv;
2944 }
2945
2946 changedProperty = pwc;
2947 pwc = pwc->GetParent();
2948 }
2949
2950 wxVariant value;
2951 wxPGProperty* evtChangingProperty = changedProperty;
2952
2953 if ( pPendingValue->GetType() != wxPG_VARIANT_TYPE_LIST )
2954 {
2955 value = *pPendingValue;
2956 }
2957 else
2958 {
2959 // Convert list to child values
2960 pList = pPendingValue;
2961 changedProperty->AdaptListToValue( *pPendingValue, &value );
2962 }
2963
2964 wxVariant evtChangingValue = value;
2965
2966 if ( flags & SendEvtChanging )
2967 {
2968 // FIXME: After proper ValueToString()s added, remove
2969 // this. It is just a temporary fix, as evt_changing
2970 // will simply not work for wxPG_PROP_COMPOSED_VALUE
2971 // (unless it is selected, and textctrl editor is open).
2972 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
2973 {
2974 evtChangingProperty = baseChangedProperty;
2975 if ( evtChangingProperty != p )
2976 {
2977 evtChangingProperty->AdaptListToValue( bcpPendingList, &evtChangingValue );
2978 }
2979 else
2980 {
2981 evtChangingValue = pendingValue;
2982 }
2983 }
2984
2985 if ( evtChangingProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
2986 {
2987 if ( changedProperty == GetSelection() )
2988 {
2989 wxWindow* editor = GetEditorControl();
2990 wxASSERT( editor->IsKindOf(CLASSINFO(wxTextCtrl)) );
2991 evtChangingValue = wxStaticCast(editor, wxTextCtrl)->GetValue();
2992 }
2993 else
2994 {
2995 wxLogDebug(wxT("WARNING: wxEVT_PG_CHANGING is about to happen with old value."));
2996 }
2997 }
2998 }
2999
3000 wxASSERT( m_chgInfo_changedProperty == NULL );
3001 m_chgInfo_changedProperty = changedProperty;
3002 m_chgInfo_baseChangedProperty = baseChangedProperty;
3003 m_chgInfo_pendingValue = value;
3004
3005 if ( pList )
3006 m_chgInfo_valueList = *pList;
3007 else
3008 m_chgInfo_valueList.MakeNull();
3009
3010 // If changedProperty is not property which value was edited,
3011 // then call wxPGProperty::ValidateValue() for that as well.
3012 if ( p != changedProperty && value.GetType() != wxPG_VARIANT_TYPE_LIST )
3013 {
3014 if ( !changedProperty->ValidateValue(value, m_validationInfo) )
3015 return false;
3016 }
3017
3018 if ( flags & SendEvtChanging )
3019 {
3020 // SendEvent returns true if event was vetoed
3021 if ( SendEvent( wxEVT_PG_CHANGING, evtChangingProperty,
3022 &evtChangingValue ) )
3023 return false;
3024 }
3025
3026 if ( flags & IsStandaloneValidation )
3027 {
3028 // If called in 'generic' context, we need to reset
3029 // m_chgInfo_changedProperty and write back translated value.
3030 m_chgInfo_changedProperty = NULL;
3031 pendingValue = value;
3032 }
3033
3034 return true;
3035 }
3036
3037 // -----------------------------------------------------------------------
3038
3039 void wxPropertyGrid::DoShowPropertyError( wxPGProperty* WXUNUSED(property), const wxString& msg )
3040 {
3041 if ( !msg.length() )
3042 return;
3043
3044 #if wxUSE_STATUSBAR
3045 if ( !wxPGGlobalVars->m_offline )
3046 {
3047 wxWindow* topWnd = ::wxGetTopLevelParent(this);
3048 if ( topWnd )
3049 {
3050 wxFrame* pFrame = wxDynamicCast(topWnd, wxFrame);
3051 if ( pFrame )
3052 {
3053 wxStatusBar* pStatusBar = pFrame->GetStatusBar();
3054 if ( pStatusBar )
3055 {
3056 pStatusBar->SetStatusText(msg);
3057 return;
3058 }
3059 }
3060 }
3061 }
3062 #endif
3063
3064 ::wxMessageBox(msg, wxT("Property Error"));
3065 }
3066
3067 // -----------------------------------------------------------------------
3068
3069 bool wxPropertyGrid::OnValidationFailure( wxPGProperty* property,
3070 wxVariant& invalidValue )
3071 {
3072 wxWindow* editor = GetEditorControl();
3073
3074 // First call property's handler
3075 property->OnValidationFailure(invalidValue);
3076
3077 bool res = DoOnValidationFailure(property, invalidValue);
3078
3079 //
3080 // For non-wxTextCtrl editors, we do need to revert the value
3081 if ( !editor->IsKindOf(CLASSINFO(wxTextCtrl)) &&
3082 property == GetSelection() )
3083 {
3084 property->GetEditorClass()->UpdateControl(property, editor);
3085 }
3086
3087 property->SetFlag(wxPG_PROP_INVALID_VALUE);
3088
3089 return res;
3090 }
3091
3092 bool wxPropertyGrid::DoOnValidationFailure( wxPGProperty* property, wxVariant& WXUNUSED(invalidValue) )
3093 {
3094 int vfb = m_validationInfo.m_failureBehavior;
3095
3096 if ( vfb & wxPG_VFB_BEEP )
3097 ::wxBell();
3098
3099 if ( (vfb & wxPG_VFB_MARK_CELL) &&
3100 !property->HasFlag(wxPG_PROP_INVALID_VALUE) )
3101 {
3102 unsigned int colCount = m_pState->GetColumnCount();
3103
3104 // We need backup marked property's cells
3105 m_propCellsBackup = property->m_cells;
3106
3107 wxColour vfbFg = *wxWHITE;
3108 wxColour vfbBg = *wxRED;
3109
3110 property->EnsureCells(colCount);
3111
3112 for ( unsigned int i=0; i<colCount; i++ )
3113 {
3114 wxPGCell& cell = property->m_cells[i];
3115 cell.SetFgCol(vfbFg);
3116 cell.SetBgCol(vfbBg);
3117 }
3118
3119 DrawItemAndChildren(property);
3120
3121 if ( property == GetSelection() )
3122 {
3123 SetInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3124
3125 wxWindow* editor = GetEditorControl();
3126 if ( editor )
3127 {
3128 editor->SetForegroundColour(vfbFg);
3129 editor->SetBackgroundColour(vfbBg);
3130 }
3131 }
3132 }
3133
3134 if ( vfb & wxPG_VFB_SHOW_MESSAGE )
3135 {
3136 wxString msg = m_validationInfo.m_failureMessage;
3137
3138 if ( !msg.length() )
3139 msg = wxT("You have entered invalid value. Press ESC to cancel editing.");
3140
3141 DoShowPropertyError(property, msg);
3142 }
3143
3144 return (vfb & wxPG_VFB_STAY_IN_PROPERTY) ? false : true;
3145 }
3146
3147 // -----------------------------------------------------------------------
3148
3149 void wxPropertyGrid::DoOnValidationFailureReset( wxPGProperty* property )
3150 {
3151 int vfb = m_validationInfo.m_failureBehavior;
3152
3153 if ( vfb & wxPG_VFB_MARK_CELL )
3154 {
3155 // Revert cells
3156 property->m_cells = m_propCellsBackup;
3157
3158 ClearInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3159
3160 if ( property == GetSelection() && GetEditorControl() )
3161 {
3162 // Calling this will recreate the control, thus resetting its colour
3163 RefreshProperty(property);
3164 }
3165 else
3166 {
3167 DrawItemAndChildren(property);
3168 }
3169 }
3170 }
3171
3172 // -----------------------------------------------------------------------
3173
3174 // flags are same as with DoSelectProperty
3175 bool wxPropertyGrid::DoPropertyChanged( wxPGProperty* p, unsigned int selFlags )
3176 {
3177 if ( m_inDoPropertyChanged )
3178 return true;
3179
3180 wxWindow* editor = GetEditorControl();
3181 wxPGProperty* selected = GetSelection();
3182
3183 m_pState->m_anyModified = 1;
3184
3185 m_inDoPropertyChanged = 1;
3186
3187 // Maybe need to update control
3188 wxASSERT( m_chgInfo_changedProperty != NULL );
3189
3190 // These values were calculated in PerformValidation()
3191 wxPGProperty* changedProperty = m_chgInfo_changedProperty;
3192 wxVariant value = m_chgInfo_pendingValue;
3193
3194 wxPGProperty* topPaintedProperty = changedProperty;
3195
3196 while ( !topPaintedProperty->IsCategory() &&
3197 !topPaintedProperty->IsRoot() )
3198 {
3199 topPaintedProperty = topPaintedProperty->GetParent();
3200 }
3201
3202 changedProperty->SetValue(value, &m_chgInfo_valueList, wxPG_SETVAL_BY_USER);
3203
3204 // Set as Modified (not if dragging just began)
3205 if ( !(p->m_flags & wxPG_PROP_MODIFIED) )
3206 {
3207 p->m_flags |= wxPG_PROP_MODIFIED;
3208 if ( p == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3209 {
3210 if ( editor )
3211 SetCurControlBoldFont();
3212 }
3213 }
3214
3215 wxPGProperty* pwc;
3216
3217 // Propagate updates to parent(s)
3218 pwc = p;
3219 wxPGProperty* prevPwc = NULL;
3220
3221 while ( prevPwc != topPaintedProperty )
3222 {
3223 pwc->m_flags |= wxPG_PROP_MODIFIED;
3224
3225 if ( pwc == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3226 {
3227 if ( editor )
3228 SetCurControlBoldFont();
3229 }
3230
3231 prevPwc = pwc;
3232 pwc = pwc->GetParent();
3233 }
3234
3235 // Draw the actual property
3236 DrawItemAndChildren( topPaintedProperty );
3237
3238 //
3239 // If value was set by wxPGProperty::OnEvent, then update the editor
3240 // control.
3241 if ( selFlags & wxPG_SEL_DIALOGVAL )
3242 {
3243 RefreshEditor();
3244 }
3245 else
3246 {
3247 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
3248 if ( m_wndEditor ) m_wndEditor->Refresh();
3249 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
3250 #endif
3251 }
3252
3253 // Sanity check
3254 wxASSERT( !changedProperty->GetParent()->HasFlag(wxPG_PROP_AGGREGATE) );
3255
3256 // If top parent has composite string value, then send to child parents,
3257 // starting from baseChangedProperty.
3258 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
3259 {
3260 pwc = m_chgInfo_baseChangedProperty;
3261
3262 while ( pwc != changedProperty )
3263 {
3264 SendEvent( wxEVT_PG_CHANGED, pwc, NULL );
3265 pwc = pwc->GetParent();
3266 }
3267 }
3268
3269 SendEvent( wxEVT_PG_CHANGED, changedProperty, NULL );
3270
3271 m_inDoPropertyChanged = 0;
3272
3273 return true;
3274 }
3275
3276 // -----------------------------------------------------------------------
3277
3278 bool wxPropertyGrid::ChangePropertyValue( wxPGPropArg id, wxVariant newValue )
3279 {
3280 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
3281
3282 m_chgInfo_changedProperty = NULL;
3283
3284 if ( PerformValidation(p, newValue) )
3285 {
3286 DoPropertyChanged(p);
3287 return true;
3288 }
3289 else
3290 {
3291 OnValidationFailure(p, newValue);
3292 }
3293
3294 return false;
3295 }
3296
3297 // -----------------------------------------------------------------------
3298
3299 wxVariant wxPropertyGrid::GetUncommittedPropertyValue()
3300 {
3301 wxPGProperty* prop = GetSelectedProperty();
3302
3303 if ( !prop )
3304 return wxNullVariant;
3305
3306 wxTextCtrl* tc = GetEditorTextCtrl();
3307 wxVariant value = prop->GetValue();
3308
3309 if ( !tc || !IsEditorsValueModified() )
3310 return value;
3311
3312 if ( !prop->StringToValue(value, tc->GetValue()) )
3313 return value;
3314
3315 if ( !PerformValidation(prop, value, IsStandaloneValidation) )
3316 return prop->GetValue();
3317
3318 return value;
3319 }
3320
3321 // -----------------------------------------------------------------------
3322
3323 // Runs wxValidator for the selected property
3324 bool wxPropertyGrid::DoEditorValidate()
3325 {
3326 return true;
3327 }
3328
3329 // -----------------------------------------------------------------------
3330
3331 void wxPropertyGrid::HandleCustomEditorEvent( wxEvent &event )
3332 {
3333 wxPGProperty* selected = GetSelection();
3334
3335 // Somehow, event is handled after property has been deselected.
3336 // Possibly, but very rare.
3337 if ( !selected || selected->HasFlag(wxPG_PROP_BEING_DELETED) )
3338 return;
3339
3340 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
3341 return;
3342
3343 wxVariant pendingValue(selected->GetValueRef());
3344 wxWindow* wnd = GetEditorControl();
3345 wxWindow* editorWnd = wxDynamicCast(event.GetEventObject(), wxWindow);
3346 int selFlags = 0;
3347 bool wasUnspecified = selected->IsValueUnspecified();
3348 int usesAutoUnspecified = selected->UsesAutoUnspecified();
3349 bool valueIsPending = false;
3350
3351 m_chgInfo_changedProperty = NULL;
3352
3353 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED|wxPG_FL_VALUE_CHANGE_IN_EVENT);
3354
3355 //
3356 // Filter out excess wxTextCtrl modified events
3357 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED &&
3358 wnd &&
3359 wnd->IsKindOf(CLASSINFO(wxTextCtrl)) )
3360 {
3361 wxTextCtrl* tc = (wxTextCtrl*) wnd;
3362
3363 wxString newTcValue = tc->GetValue();
3364 if ( m_prevTcValue == newTcValue )
3365 return;
3366
3367 m_prevTcValue = newTcValue;
3368 }
3369
3370 SetInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3371
3372 bool validationFailure = false;
3373 bool buttonWasHandled = false;
3374
3375 //
3376 // Try common button handling
3377 if ( m_wndEditor2 && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3378 {
3379 wxPGEditorDialogAdapter* adapter = selected->GetEditorDialog();
3380
3381 if ( adapter )
3382 {
3383 buttonWasHandled = true;
3384 // Store as res2, as previously (and still currently alternatively)
3385 // dialogs can be shown by handling wxEVT_COMMAND_BUTTON_CLICKED
3386 // in wxPGProperty::OnEvent().
3387 adapter->ShowDialog( this, selected );
3388 delete adapter;
3389 }
3390 }
3391
3392 if ( !buttonWasHandled )
3393 {
3394 if ( wnd || m_wndEditor2 )
3395 {
3396 // First call editor class' event handler.
3397 const wxPGEditor* editor = selected->GetEditorClass();
3398
3399 if ( editor->OnEvent( this, selected, editorWnd, event ) )
3400 {
3401 // If changes, validate them
3402 if ( DoEditorValidate() )
3403 {
3404 if ( editor->GetValueFromControl( pendingValue,
3405 selected,
3406 wnd ) )
3407 valueIsPending = true;
3408 }
3409 else
3410 {
3411 validationFailure = true;
3412 }
3413 }
3414 }
3415
3416 // Then the property's custom handler (must be always called, unless
3417 // validation failed).
3418 if ( !validationFailure )
3419 buttonWasHandled = selected->OnEvent( this, editorWnd, event );
3420 }
3421
3422 // SetValueInEvent(), as called in one of the functions referred above
3423 // overrides editor's value.
3424 if ( m_iFlags & wxPG_FL_VALUE_CHANGE_IN_EVENT )
3425 {
3426 valueIsPending = true;
3427 pendingValue = m_changeInEventValue;
3428 selFlags |= wxPG_SEL_DIALOGVAL;
3429 }
3430
3431 if ( !validationFailure && valueIsPending )
3432 if ( !PerformValidation(selected, pendingValue) )
3433 validationFailure = true;
3434
3435 if ( validationFailure)
3436 {
3437 OnValidationFailure(selected, pendingValue);
3438 }
3439 else if ( valueIsPending )
3440 {
3441 selFlags |= ( !wasUnspecified && selected->IsValueUnspecified() && usesAutoUnspecified ) ? wxPG_SEL_SETUNSPEC : 0;
3442
3443 DoPropertyChanged(selected, selFlags);
3444 EditorsValueWasNotModified();
3445
3446 // Regardless of editor type, unfocus editor on
3447 // text-editing related enter press.
3448 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3449 {
3450 SetFocusOnCanvas();
3451 }
3452 }
3453 else
3454 {
3455 // No value after all
3456
3457 // Regardless of editor type, unfocus editor on
3458 // text-editing related enter press.
3459 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3460 {
3461 SetFocusOnCanvas();
3462 }
3463
3464 // Let unhandled button click events go to the parent
3465 if ( !buttonWasHandled && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3466 {
3467 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED,GetId());
3468 GetEventHandler()->AddPendingEvent(evt);
3469 }
3470 }
3471
3472 ClearInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3473 }
3474
3475 // -----------------------------------------------------------------------
3476 // wxPropertyGrid editor control helper methods
3477 // -----------------------------------------------------------------------
3478
3479 wxRect wxPropertyGrid::GetEditorWidgetRect( wxPGProperty* p, int column ) const
3480 {
3481 int itemy = p->GetY2(m_lineHeight);
3482 int vy = 0;
3483 int splitterX = m_pState->DoGetSplitterPosition(column-1);
3484 int colEnd = splitterX + m_pState->m_colWidths[column];
3485 int imageOffset = 0;
3486
3487 // TODO: If custom image detection changes from current, change this.
3488 if ( m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE )
3489 {
3490 //m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3491 int iw = p->OnMeasureImage().x;
3492 if ( iw < 1 )
3493 iw = wxPG_CUSTOM_IMAGE_WIDTH;
3494 imageOffset = p->GetImageOffset(iw);
3495 }
3496
3497 return wxRect
3498 (
3499 splitterX+imageOffset+wxPG_XBEFOREWIDGET+wxPG_CONTROL_MARGIN+1,
3500 itemy-vy,
3501 colEnd-splitterX-wxPG_XBEFOREWIDGET-wxPG_CONTROL_MARGIN-imageOffset-1,
3502 m_lineHeight-1
3503 );
3504 }
3505
3506 // -----------------------------------------------------------------------
3507
3508 wxRect wxPropertyGrid::GetImageRect( wxPGProperty* p, int item ) const
3509 {
3510 wxSize sz = GetImageSize(p, item);
3511 return wxRect(wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
3512 wxPG_CUSTOM_IMAGE_SPACINGY,
3513 sz.x,
3514 sz.y);
3515 }
3516
3517 // return size of custom paint image
3518 wxSize wxPropertyGrid::GetImageSize( wxPGProperty* p, int item ) const
3519 {
3520 // If called with NULL property, then return default image
3521 // size for properties that use image.
3522 if ( !p )
3523 return wxSize(wxPG_CUSTOM_IMAGE_WIDTH,wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight));
3524
3525 wxSize cis = p->OnMeasureImage(item);
3526
3527 int choiceCount = p->m_choices.GetCount();
3528 int comVals = p->GetDisplayedCommonValueCount();
3529 if ( item >= choiceCount && comVals > 0 )
3530 {
3531 unsigned int cvi = item-choiceCount;
3532 cis = GetCommonValue(cvi)->GetRenderer()->GetImageSize(NULL, 1, cvi);
3533 }
3534 else if ( item >= 0 && choiceCount == 0 )
3535 return wxSize(0, 0);
3536
3537 if ( cis.x < 0 )
3538 {
3539 if ( cis.x <= -1 )
3540 cis.x = wxPG_CUSTOM_IMAGE_WIDTH;
3541 }
3542 if ( cis.y <= 0 )
3543 {
3544 if ( cis.y >= -1 )
3545 cis.y = wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight);
3546 else
3547 cis.y = -cis.y;
3548 }
3549 return cis;
3550 }
3551
3552 // -----------------------------------------------------------------------
3553
3554 // takes scrolling into account
3555 void wxPropertyGrid::ImprovedClientToScreen( int* px, int* py )
3556 {
3557 int vx, vy;
3558 GetViewStart(&vx,&vy);
3559 vy*=wxPG_PIXELS_PER_UNIT;
3560 vx*=wxPG_PIXELS_PER_UNIT;
3561 *px -= vx;
3562 *py -= vy;
3563 ClientToScreen( px, py );
3564 }
3565
3566 // -----------------------------------------------------------------------
3567
3568 wxPropertyGridHitTestResult wxPropertyGrid::HitTest( const wxPoint& pt ) const
3569 {
3570 wxPoint pt2;
3571 GetViewStart(&pt2.x,&pt2.y);
3572 pt2.x *= wxPG_PIXELS_PER_UNIT;
3573 pt2.y *= wxPG_PIXELS_PER_UNIT;
3574 pt2.x += pt.x;
3575 pt2.y += pt.y;
3576
3577 return m_pState->HitTest(pt2);
3578 }
3579
3580 // -----------------------------------------------------------------------
3581
3582 // custom set cursor
3583 void wxPropertyGrid::CustomSetCursor( int type, bool override )
3584 {
3585 if ( type == m_curcursor && !override ) return;
3586
3587 wxCursor* cursor = &wxPG_DEFAULT_CURSOR;
3588
3589 if ( type == wxCURSOR_SIZEWE )
3590 cursor = m_cursorSizeWE;
3591
3592 m_canvas->SetCursor( *cursor );
3593
3594 m_curcursor = type;
3595 }
3596
3597 // -----------------------------------------------------------------------
3598 // wxPropertyGrid property selection, editor creation
3599 // -----------------------------------------------------------------------
3600
3601 //
3602 // This class forwards events from property editor controls to wxPropertyGrid.
3603 class wxPropertyGridEditorEventForwarder : public wxEvtHandler
3604 {
3605 public:
3606 wxPropertyGridEditorEventForwarder( wxPropertyGrid* propGrid )
3607 : wxEvtHandler(), m_propGrid(propGrid)
3608 {
3609 }
3610
3611 virtual ~wxPropertyGridEditorEventForwarder()
3612 {
3613 }
3614
3615 private:
3616 bool ProcessEvent( wxEvent& event )
3617 {
3618 // Always skip
3619 event.Skip();
3620
3621 m_propGrid->HandleCustomEditorEvent(event);
3622
3623 return wxEvtHandler::ProcessEvent(event);
3624 }
3625
3626 wxPropertyGrid* m_propGrid;
3627 };
3628
3629 // Setups event handling for child control
3630 void wxPropertyGrid::SetupChildEventHandling( wxWindow* argWnd )
3631 {
3632 wxWindowID id = argWnd->GetId();
3633
3634 if ( argWnd == m_wndEditor )
3635 {
3636 argWnd->Connect(id, wxEVT_MOTION,
3637 wxMouseEventHandler(wxPropertyGrid::OnMouseMoveChild),
3638 NULL, this);
3639 argWnd->Connect(id, wxEVT_LEFT_UP,
3640 wxMouseEventHandler(wxPropertyGrid::OnMouseUpChild),
3641 NULL, this);
3642 argWnd->Connect(id, wxEVT_LEFT_DOWN,
3643 wxMouseEventHandler(wxPropertyGrid::OnMouseClickChild),
3644 NULL, this);
3645 argWnd->Connect(id, wxEVT_RIGHT_UP,
3646 wxMouseEventHandler(wxPropertyGrid::OnMouseRightClickChild),
3647 NULL, this);
3648 argWnd->Connect(id, wxEVT_ENTER_WINDOW,
3649 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3650 NULL, this);
3651 argWnd->Connect(id, wxEVT_LEAVE_WINDOW,
3652 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3653 NULL, this);
3654 }
3655
3656 wxPropertyGridEditorEventForwarder* forwarder;
3657 forwarder = new wxPropertyGridEditorEventForwarder(this);
3658 argWnd->PushEventHandler(forwarder);
3659
3660 argWnd->Connect(id, wxEVT_KEY_DOWN,
3661 wxCharEventHandler(wxPropertyGrid::OnChildKeyDown),
3662 NULL, this);
3663 }
3664
3665 void wxPropertyGrid::DestroyEditorWnd( wxWindow* wnd )
3666 {
3667 if ( !wnd )
3668 return;
3669
3670 wnd->Hide();
3671
3672 // Do not free editors immediately (for sake of processing events)
3673 wxPendingDelete.Append(wnd);
3674 }
3675
3676 void wxPropertyGrid::FreeEditors()
3677 {
3678 //
3679 // Return focus back to canvas from children (this is required at least for
3680 // GTK+, which, unlike Windows, clears focus when control is destroyed
3681 // instead of moving it to closest parent).
3682 wxWindow* focus = wxWindow::FindFocus();
3683 if ( focus )
3684 {
3685 wxWindow* parent = focus->GetParent();
3686 while ( parent )
3687 {
3688 if ( parent == m_canvas )
3689 {
3690 SetFocusOnCanvas();
3691 break;
3692 }
3693 parent = parent->GetParent();
3694 }
3695 }
3696
3697 // Do not free editors immediately if processing events
3698 if ( m_wndEditor2 )
3699 {
3700 wxEvtHandler* handler = m_wndEditor2->PopEventHandler(false);
3701 m_wndEditor2->Hide();
3702 wxPendingDelete.Append( handler );
3703 DestroyEditorWnd(m_wndEditor2);
3704 m_wndEditor2 = NULL;
3705 }
3706
3707 if ( m_wndEditor )
3708 {
3709 wxEvtHandler* handler = m_wndEditor->PopEventHandler(false);
3710 m_wndEditor->Hide();
3711 wxPendingDelete.Append( handler );
3712 DestroyEditorWnd(m_wndEditor);
3713 m_wndEditor = NULL;
3714 }
3715 }
3716
3717 // Call with NULL to de-select property
3718 bool wxPropertyGrid::DoSelectProperty( wxPGProperty* p, unsigned int flags )
3719 {
3720 /*
3721 if (p)
3722 {
3723 wxLogDebug(wxT("SelectProperty( %s (%s[%i]) )"),p->m_label.c_str(),
3724 p->m_parent->m_label.c_str(),p->GetIndexInParent());
3725 }
3726 else
3727 {
3728 wxLogDebug(wxT("SelectProperty( NULL, -1 )"));
3729 }
3730 */
3731
3732 if ( m_inDoSelectProperty )
3733 return true;
3734
3735 m_inDoSelectProperty = 1;
3736
3737 if ( !m_pState )
3738 {
3739 m_inDoSelectProperty = 0;
3740 return false;
3741 }
3742
3743 wxArrayPGProperty prevSelection = m_pState->m_selection;
3744 wxPGProperty* prevFirstSel;
3745
3746 if ( prevSelection.size() > 0 )
3747 prevFirstSel = prevSelection[0];
3748 else
3749 prevFirstSel = NULL;
3750
3751 if ( prevFirstSel && prevFirstSel->HasFlag(wxPG_PROP_BEING_DELETED) )
3752 prevFirstSel = NULL;
3753
3754 // Always send event, as this is indirect call
3755 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
3756
3757 /*
3758 if ( prevFirstSel )
3759 wxPrintf( "Selected %s\n", prevFirstSel->GetClassInfo()->GetClassName() );
3760 else
3761 wxPrintf( "None selected\n" );
3762
3763 if (p)
3764 wxPrintf( "P = %s\n", p->GetClassInfo()->GetClassName() );
3765 else
3766 wxPrintf( "P = NULL\n" );
3767 */
3768
3769 // If we are frozen, then just set the values.
3770 if ( m_frozen )
3771 {
3772 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3773 m_editorFocused = 0;
3774 m_pState->DoSetSelection(p);
3775
3776 // If frozen, always free controls. But don't worry, as Thaw will
3777 // recall SelectProperty to recreate them.
3778 FreeEditors();
3779
3780 // Prevent any further selection measures in this call
3781 p = NULL;
3782 }
3783 else
3784 {
3785 // Is it the same?
3786 if ( prevFirstSel == p &&
3787 prevSelection.size() <= 1 &&
3788 !(flags & wxPG_SEL_FORCE) )
3789 {
3790 // Only set focus if not deselecting
3791 if ( p )
3792 {
3793 if ( flags & wxPG_SEL_FOCUS )
3794 {
3795 if ( m_wndEditor )
3796 {
3797 m_wndEditor->SetFocus();
3798 m_editorFocused = 1;
3799 }
3800 }
3801 else
3802 {
3803 SetFocusOnCanvas();
3804 }
3805 }
3806
3807 m_inDoSelectProperty = 0;
3808 return true;
3809 }
3810
3811 //
3812 // First, deactivate previous
3813 if ( prevFirstSel )
3814 {
3815 OnValidationFailureReset(prevFirstSel);
3816
3817 // Must double-check if this is an selected in case of forceswitch
3818 if ( p != prevFirstSel )
3819 {
3820 if ( !CommitChangesFromEditor(flags) )
3821 {
3822 // Validation has failed, so we can't exit the previous editor
3823 //::wxMessageBox(_("Please correct the value or press ESC to cancel the edit."),
3824 // _("Invalid Value"),wxOK|wxICON_ERROR);
3825 m_inDoSelectProperty = 0;
3826 return false;
3827 }
3828 }
3829
3830 FreeEditors();
3831
3832 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3833 EditorsValueWasNotModified();
3834 }
3835
3836 SetInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3837
3838 m_pState->DoSetSelection(p);
3839
3840 // Redraw unselected
3841 for ( unsigned int i=0; i<prevSelection.size(); i++ )
3842 {
3843 DrawItem(prevSelection[i]);
3844 }
3845
3846 //
3847 // Then, activate the one given.
3848 if ( p )
3849 {
3850 int propY = p->GetY2(m_lineHeight);
3851
3852 int splitterX = GetSplitterPosition();
3853 m_editorFocused = 0;
3854 m_iFlags |= wxPG_FL_PRIMARY_FILLS_ENTIRE;
3855 if ( p != prevFirstSel )
3856 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED);
3857
3858 wxASSERT( m_wndEditor == NULL );
3859
3860 //
3861 // Only create editor for non-disabled non-caption
3862 if ( !p->IsCategory() && !(p->m_flags & wxPG_PROP_DISABLED) )
3863 {
3864 // do this for non-caption items
3865
3866 m_selColumn = 1;
3867
3868 // Do we need to paint the custom image, if any?
3869 m_iFlags &= ~(wxPG_FL_CUR_USES_CUSTOM_IMAGE);
3870 if ( (p->m_flags & wxPG_PROP_CUSTOMIMAGE) &&
3871 !p->GetEditorClass()->CanContainCustomImage()
3872 )
3873 m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3874
3875 wxRect grect = GetEditorWidgetRect(p, m_selColumn);
3876 wxPoint goodPos = grect.GetPosition();
3877
3878 const wxPGEditor* editor = p->GetEditorClass();
3879 wxCHECK_MSG(editor, false,
3880 wxT("NULL editor class not allowed"));
3881
3882 m_iFlags &= ~wxPG_FL_FIXED_WIDTH_EDITOR;
3883
3884 wxPGWindowList wndList = editor->CreateControls(this,
3885 p,
3886 goodPos,
3887 grect.GetSize());
3888
3889 m_wndEditor = wndList.m_primary;
3890 m_wndEditor2 = wndList.m_secondary;
3891 wxWindow* primaryCtrl = GetEditorControl();
3892
3893 //
3894 // Essentially, primaryCtrl == m_wndEditor
3895 //
3896
3897 // NOTE: It is allowed for m_wndEditor to be NULL - in this case
3898 // value is drawn as normal, and m_wndEditor2 is assumed
3899 // to be a right-aligned button that triggers a separate editorCtrl
3900 // window.
3901
3902 if ( m_wndEditor )
3903 {
3904 wxASSERT_MSG( m_wndEditor->GetParent() == GetPanel(),
3905 wxT("CreateControls must use result of wxPropertyGrid::GetPanel() as parent of controls.") );
3906
3907 // Set validator, if any
3908 #if wxUSE_VALIDATORS
3909 wxValidator* validator = p->GetValidator();
3910 if ( validator )
3911 primaryCtrl->SetValidator(*validator);
3912 #endif
3913
3914 if ( m_wndEditor->GetSize().y > (m_lineHeight+6) )
3915 m_iFlags |= wxPG_FL_ABNORMAL_EDITOR;
3916
3917 // If it has modified status, use bold font
3918 // (must be done before capturing m_ctrlXAdjust)
3919 if ( (p->m_flags & wxPG_PROP_MODIFIED) && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3920 SetCurControlBoldFont();
3921
3922 // Store x relative to splitter (we'll need it).
3923 m_ctrlXAdjust = m_wndEditor->GetPosition().x - splitterX;
3924
3925 // Check if background clear is not necessary
3926 wxPoint pos = m_wndEditor->GetPosition();
3927 if ( pos.x > (splitterX+1) || pos.y > propY )
3928 {
3929 m_iFlags &= ~(wxPG_FL_PRIMARY_FILLS_ENTIRE);
3930 }
3931
3932 m_wndEditor->SetSizeHints(3, 3);
3933
3934 SetupChildEventHandling(primaryCtrl);
3935
3936 // Focus and select all (wxTextCtrl, wxComboBox etc)
3937 if ( flags & wxPG_SEL_FOCUS )
3938 {
3939 primaryCtrl->SetFocus();
3940
3941 p->GetEditorClass()->OnFocus(p, primaryCtrl);
3942 }
3943 }
3944
3945 if ( m_wndEditor2 )
3946 {
3947 wxASSERT_MSG( m_wndEditor2->GetParent() == GetPanel(),
3948 wxT("CreateControls must use result of wxPropertyGrid::GetPanel() as parent of controls.") );
3949
3950 // Get proper id for wndSecondary
3951 m_wndSecId = m_wndEditor2->GetId();
3952 wxWindowList children = m_wndEditor2->GetChildren();
3953 wxWindowList::iterator node = children.begin();
3954 if ( node != children.end() )
3955 m_wndSecId = ((wxWindow*)*node)->GetId();
3956
3957 m_wndEditor2->SetSizeHints(3,3);
3958
3959 m_wndEditor2->Show();
3960
3961 SetupChildEventHandling(m_wndEditor2);
3962
3963 // If no primary editor, focus to button to allow
3964 // it to interprete ENTER etc.
3965 // NOTE: Due to problems focusing away from it, this
3966 // has been disabled.
3967 /*
3968 if ( (flags & wxPG_SEL_FOCUS) && !m_wndEditor )
3969 m_wndEditor2->SetFocus();
3970 */
3971 }
3972
3973 if ( flags & wxPG_SEL_FOCUS )
3974 m_editorFocused = 1;
3975
3976 }
3977 else
3978 {
3979 // Make sure focus is in grid canvas (important for wxGTK, at least)
3980 SetFocusOnCanvas();
3981 }
3982
3983 EditorsValueWasNotModified();
3984
3985 // If it's inside collapsed section, expand parent, scroll, etc.
3986 // Also, if it was partially visible, scroll it into view.
3987 if ( !(flags & wxPG_SEL_NONVISIBLE) )
3988 EnsureVisible( p );
3989
3990 if ( m_wndEditor )
3991 {
3992 m_wndEditor->Show(true);
3993 }
3994
3995 if ( !(flags & wxPG_SEL_NO_REFRESH) )
3996 DrawItem(p);
3997 }
3998 else
3999 {
4000 // Make sure focus is in grid canvas
4001 SetFocusOnCanvas();
4002 }
4003
4004 ClearInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
4005 }
4006
4007 #if wxUSE_STATUSBAR
4008
4009 //
4010 // Show help text in status bar.
4011 // (if found and grid not embedded in manager with help box and
4012 // style wxPG_EX_HELP_AS_TOOLTIPS is not used).
4013 //
4014
4015 if ( !(GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS) )
4016 {
4017 wxStatusBar* statusbar = NULL;
4018 if ( !(m_iFlags & wxPG_FL_NOSTATUSBARHELP) )
4019 {
4020 wxFrame* frame = wxDynamicCast(::wxGetTopLevelParent(this),wxFrame);
4021 if ( frame )
4022 statusbar = frame->GetStatusBar();
4023 }
4024
4025 if ( statusbar )
4026 {
4027 const wxString* pHelpString = (const wxString*) NULL;
4028
4029 if ( p )
4030 {
4031 pHelpString = &p->GetHelpString();
4032 if ( pHelpString->length() )
4033 {
4034 // Set help box text.
4035 statusbar->SetStatusText( *pHelpString );
4036 m_iFlags |= wxPG_FL_STRING_IN_STATUSBAR;
4037 }
4038 }
4039
4040 if ( (!pHelpString || !pHelpString->length()) &&
4041 (m_iFlags & wxPG_FL_STRING_IN_STATUSBAR) )
4042 {
4043 // Clear help box - but only if it was written
4044 // by us at previous time.
4045 statusbar->SetStatusText( m_emptyString );
4046 m_iFlags &= ~(wxPG_FL_STRING_IN_STATUSBAR);
4047 }
4048 }
4049 }
4050 #endif
4051
4052 m_inDoSelectProperty = 0;
4053
4054 // call wx event handler (here so that it also occurs on deselection)
4055 if ( !(flags & wxPG_SEL_DONT_SEND_EVENT) )
4056 SendEvent( wxEVT_PG_SELECTED, p, NULL );
4057
4058 return true;
4059 }
4060
4061 // -----------------------------------------------------------------------
4062
4063 bool wxPropertyGrid::UnfocusEditor()
4064 {
4065 wxPGProperty* selected = GetSelection();
4066
4067 if ( !selected || !m_wndEditor || m_frozen )
4068 return true;
4069
4070 if ( !CommitChangesFromEditor(0) )
4071 return false;
4072
4073 SetFocusOnCanvas();
4074 DrawItem(selected);
4075
4076 return true;
4077 }
4078
4079 // -----------------------------------------------------------------------
4080
4081 void wxPropertyGrid::RefreshEditor()
4082 {
4083 wxPGProperty* p = GetSelection();
4084 if ( !p )
4085 return;
4086
4087 wxWindow* wnd = GetEditorControl();
4088 if ( !wnd )
4089 return;
4090
4091 // Set editor font boldness - must do this before
4092 // calling UpdateControl().
4093 if ( HasFlag(wxPG_BOLD_MODIFIED) )
4094 {
4095 if ( p->HasFlag(wxPG_PROP_MODIFIED) )
4096 wnd->SetFont(GetCaptionFont());
4097 else
4098 wnd->SetFont(GetFont());
4099 }
4100
4101 const wxPGEditor* editorClass = p->GetEditorClass();
4102
4103 editorClass->UpdateControl(p, wnd);
4104
4105 if ( p->IsValueUnspecified() )
4106 editorClass ->SetValueToUnspecified(p, wnd);
4107 }
4108
4109 // -----------------------------------------------------------------------
4110
4111 bool wxPropertyGrid::SelectProperty( wxPGPropArg id, bool focus )
4112 {
4113 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
4114
4115 int flags = wxPG_SEL_DONT_SEND_EVENT;
4116 if ( focus )
4117 flags |= wxPG_SEL_FOCUS;
4118
4119 return DoSelectProperty(p, flags);
4120 }
4121
4122 // -----------------------------------------------------------------------
4123 // wxPropertyGrid expand/collapse state
4124 // -----------------------------------------------------------------------
4125
4126 bool wxPropertyGrid::DoCollapse( wxPGProperty* p, bool sendEvents )
4127 {
4128 wxPGProperty* pwc = wxStaticCast(p, wxPGProperty);
4129 wxPGProperty* selected = GetSelection();
4130
4131 // If active editor was inside collapsed section, then disable it
4132 if ( selected && selected->IsSomeParent(p) )
4133 {
4134 DoClearSelection();
4135 }
4136
4137 // Store dont-center-splitter flag 'cause we need to temporarily set it
4138 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
4139 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4140
4141 bool res = m_pState->DoCollapse(pwc);
4142
4143 if ( res )
4144 {
4145 if ( sendEvents )
4146 SendEvent( wxEVT_PG_ITEM_COLLAPSED, p );
4147
4148 RecalculateVirtualSize();
4149
4150 // Redraw etc. only if collapsed was visible.
4151 if (pwc->IsVisible() &&
4152 !m_frozen &&
4153 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) ) )
4154 {
4155 // When item is collapsed so that scrollbar would move,
4156 // graphics mess is about (unless we redraw everything).
4157 Refresh();
4158 }
4159 }
4160
4161 // Clear dont-center-splitter flag if it wasn't set
4162 m_iFlags = (m_iFlags & ~wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
4163
4164 return res;
4165 }
4166
4167 // -----------------------------------------------------------------------
4168
4169 bool wxPropertyGrid::DoExpand( wxPGProperty* p, bool sendEvents )
4170 {
4171 wxCHECK_MSG( p, false, wxT("invalid property id") );
4172
4173 wxPGProperty* pwc = (wxPGProperty*)p;
4174
4175 // Store dont-center-splitter flag 'cause we need to temporarily set it
4176 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
4177 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4178
4179 bool res = m_pState->DoExpand(pwc);
4180
4181 if ( res )
4182 {
4183 if ( sendEvents )
4184 SendEvent( wxEVT_PG_ITEM_EXPANDED, p );
4185
4186 RecalculateVirtualSize();
4187
4188 // Redraw etc. only if expanded was visible.
4189 if ( pwc->IsVisible() && !m_frozen &&
4190 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) )
4191 )
4192 {
4193 // Redraw
4194 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4195 Refresh();
4196 #else
4197 DrawItems(pwc, NULL);
4198 #endif
4199 }
4200 }
4201
4202 // Clear dont-center-splitter flag if it wasn't set
4203 m_iFlags = (m_iFlags & ~wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
4204
4205 return res;
4206 }
4207
4208 // -----------------------------------------------------------------------
4209
4210 bool wxPropertyGrid::DoHideProperty( wxPGProperty* p, bool hide, int flags )
4211 {
4212 if ( m_frozen )
4213 return m_pState->DoHideProperty(p, hide, flags);
4214
4215 wxArrayPGProperty selection = m_pState->m_selection; // Must use a copy
4216 int selRemoveCount = 0;
4217 for ( unsigned int i=0; i<selection.size(); i++ )
4218 {
4219 wxPGProperty* selected = selection[i];
4220 if ( selected == p || selected->IsSomeParent(p) )
4221 {
4222 if ( !DoRemoveFromSelection(p, flags) )
4223 return false;
4224 selRemoveCount += 1;
4225 }
4226 }
4227
4228 m_pState->DoHideProperty(p, hide, flags);
4229
4230 RecalculateVirtualSize();
4231 Refresh();
4232
4233 return true;
4234 }
4235
4236
4237 // -----------------------------------------------------------------------
4238 // wxPropertyGrid size related methods
4239 // -----------------------------------------------------------------------
4240
4241 void wxPropertyGrid::RecalculateVirtualSize( int forceXPos )
4242 {
4243 if ( (m_iFlags & wxPG_FL_RECALCULATING_VIRTUAL_SIZE) || m_frozen )
4244 return;
4245
4246 //
4247 // If virtual height was changed, then recalculate editor control position(s)
4248 if ( m_pState->m_vhCalcPending )
4249 CorrectEditorWidgetPosY();
4250
4251 m_pState->EnsureVirtualHeight();
4252
4253 wxASSERT_LEVEL_2_MSG(
4254 m_pState->GetVirtualHeight() == m_pState->GetActualVirtualHeight(),
4255 "VirtualHeight and ActualVirtualHeight should match"
4256 );
4257
4258 m_iFlags |= wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4259
4260 int x = m_pState->m_width;
4261 int y = m_pState->m_virtualHeight;
4262
4263 int width, height;
4264 GetClientSize(&width,&height);
4265
4266 // Now adjust virtual size.
4267 SetVirtualSize(x, y);
4268
4269 int xAmount = 0;
4270 int xPos = 0;
4271
4272 //
4273 // Adjust scrollbars
4274 if ( HasVirtualWidth() )
4275 {
4276 xAmount = x/wxPG_PIXELS_PER_UNIT;
4277 xPos = GetScrollPos( wxHORIZONTAL );
4278 }
4279
4280 if ( forceXPos != -1 )
4281 xPos = forceXPos;
4282 // xPos too high?
4283 else if ( xPos > (xAmount-(width/wxPG_PIXELS_PER_UNIT)) )
4284 xPos = 0;
4285
4286 int yAmount = y / wxPG_PIXELS_PER_UNIT;
4287 int yPos = GetScrollPos( wxVERTICAL );
4288
4289 SetScrollbars( wxPG_PIXELS_PER_UNIT, wxPG_PIXELS_PER_UNIT,
4290 xAmount, yAmount, xPos, yPos, true );
4291
4292 // Must re-get size now
4293 GetClientSize(&width,&height);
4294
4295 if ( !HasVirtualWidth() )
4296 {
4297 m_pState->SetVirtualWidth(width);
4298 x = width;
4299 }
4300
4301 m_width = width;
4302 m_height = height;
4303
4304 // Explicitly pass the position - works around a bug in wxWidgets when the property grid
4305 // has a native XP border and a contained window creeps up-and-left when size is set without
4306 // the position.
4307 m_canvas->SetSize( 0, 0, x, y );
4308
4309 m_pState->CheckColumnWidths();
4310
4311 if ( GetSelection() )
4312 CorrectEditorWidgetSizeX();
4313
4314 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4315 }
4316
4317 // -----------------------------------------------------------------------
4318
4319 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4320 {
4321 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4322 return;
4323
4324 int width, height;
4325 GetClientSize(&width,&height);
4326
4327 m_width = width;
4328 m_height = height;
4329
4330 #if wxPG_DOUBLE_BUFFER
4331 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4332 {
4333 int dblh = (m_lineHeight*2);
4334 if ( !m_doubleBuffer )
4335 {
4336 // Create double buffer bitmap to draw on, if none
4337 int w = (width>250)?width:250;
4338 int h = height + dblh;
4339 h = (h>400)?h:400;
4340 m_doubleBuffer = new wxBitmap( w, h );
4341 }
4342 else
4343 {
4344 int w = m_doubleBuffer->GetWidth();
4345 int h = m_doubleBuffer->GetHeight();
4346
4347 // Double buffer must be large enough
4348 if ( w < width || h < (height+dblh) )
4349 {
4350 if ( w < width ) w = width;
4351 if ( h < (height+dblh) ) h = height + dblh;
4352 delete m_doubleBuffer;
4353 m_doubleBuffer = new wxBitmap( w, h );
4354 }
4355 }
4356 }
4357
4358 #endif
4359
4360 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4361 m_ncWidth = event.GetSize().x;
4362
4363 if ( !m_frozen )
4364 {
4365 if ( m_pState->m_itemsAdded )
4366 PrepareAfterItemsAdded();
4367 else
4368 // Without this, virtual size (atleast under wxGTK) will be skewed
4369 RecalculateVirtualSize();
4370
4371 Refresh();
4372 }
4373 }
4374
4375 // -----------------------------------------------------------------------
4376
4377 void wxPropertyGrid::SetVirtualWidth( int width )
4378 {
4379 if ( width == -1 )
4380 {
4381 // Disable virtual width
4382 width = GetClientSize().x;
4383 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4384 }
4385 else
4386 {
4387 // Enable virtual width
4388 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4389 }
4390 m_pState->SetVirtualWidth( width );
4391 }
4392
4393 void wxPropertyGrid::SetFocusOnCanvas()
4394 {
4395 m_canvas->SetFocusIgnoringChildren();
4396 m_editorFocused = 0;
4397 }
4398
4399 // -----------------------------------------------------------------------
4400 // wxPropertyGrid mouse event handling
4401 // -----------------------------------------------------------------------
4402
4403 // selFlags uses same values DoSelectProperty's flags
4404 // Returns true if event was vetoed.
4405 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p,
4406 wxVariant* pValue,
4407 unsigned int selFlags,
4408 unsigned int column )
4409 {
4410 // Send property grid event of specific type and with specific property
4411 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4412 evt.SetPropertyGrid(this);
4413 evt.SetEventObject(m_eventObject);
4414 evt.SetProperty(p);
4415 evt.SetColumn(column);
4416 if ( eventType == wxEVT_PG_CHANGING )
4417 {
4418 wxASSERT( pValue );
4419 evt.SetCanVeto(true);
4420 m_validationInfo.m_pValue = pValue;
4421 evt.SetupValidationInfo();
4422 }
4423 else
4424 {
4425 if ( p )
4426 evt.SetPropertyValue(p->GetValue());
4427
4428 if ( !(selFlags & wxPG_SEL_NOVALIDATE) )
4429 evt.SetCanVeto(true);
4430 }
4431
4432 m_processedEvent = &evt;
4433 m_eventObject->HandleWindowEvent(evt);
4434 m_processedEvent = NULL;
4435
4436 return evt.WasVetoed();
4437 }
4438
4439 // -----------------------------------------------------------------------
4440
4441 // Return false if should be skipped
4442 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4443 {
4444 bool res = true;
4445
4446 // Need to set focus?
4447 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4448 {
4449 SetFocusOnCanvas();
4450 }
4451
4452 wxPropertyGridPageState* state = m_pState;
4453 int splitterHit;
4454 int splitterHitOffset;
4455 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4456
4457 wxPGProperty* p = DoGetItemAtY(y);
4458
4459 if ( p )
4460 {
4461 int depth = (int)p->GetDepth() - 1;
4462
4463 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4464
4465 if ( x >= marginEnds )
4466 {
4467 // Outside margin.
4468
4469 if ( p->IsCategory() )
4470 {
4471 // This is category.
4472 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4473
4474 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4475
4476 // Expand, collapse, activate etc. if click on text or left of splitter.
4477 if ( x >= textX
4478 &&
4479 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4480 columnHit == 0
4481 )
4482 )
4483 {
4484 if ( !AddToSelectionFromInputEvent( p,
4485 columnHit,
4486 &event ) )
4487 return res;
4488
4489 // On double-click, expand/collapse.
4490 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4491 {
4492 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4493 else DoExpand( p, true );
4494 }
4495 }
4496 }
4497 else if ( splitterHit == -1 )
4498 {
4499 // Click on value.
4500 unsigned int selFlag = 0;
4501 if ( columnHit == 1 )
4502 {
4503 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4504 selFlag = wxPG_SEL_FOCUS;
4505 }
4506 if ( !AddToSelectionFromInputEvent( p,
4507 columnHit,
4508 &event,
4509 selFlag ) )
4510 return res;
4511
4512 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4513
4514 if ( p->GetChildCount() && !p->IsCategory() )
4515 // On double-click, expand/collapse.
4516 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4517 {
4518 wxPGProperty* pwc = (wxPGProperty*)p;
4519 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4520 else DoExpand( p, true );
4521 }
4522
4523 res = false;
4524 }
4525 else
4526 {
4527 // click on splitter
4528 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4529 {
4530 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4531 {
4532 // Double-clicking the splitter causes auto-centering
4533 CenterSplitter( true );
4534 }
4535 else if ( m_dragStatus == 0 )
4536 {
4537 //
4538 // Begin draggin the splitter
4539 //
4540
4541 // send event
4542 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
4543
4544 if ( m_wndEditor )
4545 {
4546 // Changes must be committed here or the
4547 // value won't be drawn correctly
4548 if ( !CommitChangesFromEditor() )
4549 return res;
4550
4551 m_wndEditor->Show ( false );
4552 }
4553
4554 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4555 {
4556 m_canvas->CaptureMouse();
4557 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4558 }
4559
4560 m_dragStatus = 1;
4561 m_draggedSplitter = splitterHit;
4562 m_dragOffset = splitterHitOffset;
4563
4564 wxClientDC dc(m_canvas);
4565
4566 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4567 // Fixes button disappearance bug
4568 if ( m_wndEditor2 )
4569 m_wndEditor2->Show ( false );
4570 #endif
4571
4572 m_startingSplitterX = x - splitterHitOffset;
4573 }
4574 }
4575 }
4576 }
4577 else
4578 {
4579 // Click on margin.
4580 if ( p->GetChildCount() )
4581 {
4582 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4583
4584 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4585 {
4586 int y2 = y % m_lineHeight;
4587 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4588 {
4589 // On click on expander button, expand/collapse
4590 if ( ((wxPGProperty*)p)->IsExpanded() )
4591 DoCollapse( p, true );
4592 else
4593 DoExpand( p, true );
4594 }
4595 }
4596 }
4597 }
4598 }
4599 return res;
4600 }
4601
4602 // -----------------------------------------------------------------------
4603
4604 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x),
4605 unsigned int WXUNUSED(y),
4606 wxMouseEvent& event )
4607 {
4608 if ( m_propHover )
4609 {
4610 // Select property here as well
4611 wxPGProperty* p = m_propHover;
4612 AddToSelectionFromInputEvent(p, m_colHover, &event);
4613
4614 // Send right click event.
4615 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4616
4617 return true;
4618 }
4619 return false;
4620 }
4621
4622 // -----------------------------------------------------------------------
4623
4624 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x),
4625 unsigned int WXUNUSED(y),
4626 wxMouseEvent& event )
4627 {
4628 if ( m_propHover )
4629 {
4630 // Select property here as well
4631 wxPGProperty* p = m_propHover;
4632
4633 AddToSelectionFromInputEvent(p, m_colHover, &event);
4634
4635 // Send double-click event.
4636 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4637
4638 return true;
4639 }
4640 return false;
4641 }
4642
4643 // -----------------------------------------------------------------------
4644
4645 #if wxPG_SUPPORT_TOOLTIPS
4646
4647 void wxPropertyGrid::SetToolTip( const wxString& tipString )
4648 {
4649 if ( tipString.length() )
4650 {
4651 m_canvas->SetToolTip(tipString);
4652 }
4653 else
4654 {
4655 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4656 m_canvas->SetToolTip( m_emptyString );
4657 #else
4658 m_canvas->SetToolTip( NULL );
4659 #endif
4660 }
4661 }
4662
4663 #endif // #if wxPG_SUPPORT_TOOLTIPS
4664
4665 // -----------------------------------------------------------------------
4666
4667 // Return false if should be skipped
4668 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y, wxMouseEvent &event )
4669 {
4670 // Safety check (needed because mouse capturing may
4671 // otherwise freeze the control)
4672 if ( m_dragStatus > 0 && !event.Dragging() )
4673 {
4674 HandleMouseUp(x,y,event);
4675 }
4676
4677 wxPropertyGridPageState* state = m_pState;
4678 int splitterHit;
4679 int splitterHitOffset;
4680 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4681 int splitterX = x - splitterHitOffset;
4682
4683 m_colHover = columnHit;
4684
4685 if ( m_dragStatus > 0 )
4686 {
4687 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4688 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4689 {
4690
4691 int newSplitterX = x - m_dragOffset;
4692 int splitterX = x - splitterHitOffset;
4693
4694 // Splitter redraw required?
4695 if ( newSplitterX != splitterX )
4696 {
4697 // Move everything
4698 SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
4699 state->DoSetSplitterPosition( newSplitterX, m_draggedSplitter, false );
4700 state->m_fSplitterX = (float) newSplitterX;
4701
4702 if ( GetSelection() )
4703 CorrectEditorWidgetSizeX();
4704
4705 Update();
4706 Refresh();
4707 }
4708
4709 m_dragStatus = 2;
4710 }
4711
4712 return false;
4713 }
4714 else
4715 {
4716
4717 int ih = m_lineHeight;
4718 int sy = y;
4719
4720 #if wxPG_SUPPORT_TOOLTIPS
4721 wxPGProperty* prevHover = m_propHover;
4722 unsigned char prevSide = m_mouseSide;
4723 #endif
4724 int curPropHoverY = y - (y % ih);
4725
4726 // On which item it hovers
4727 if ( !m_propHover
4728 ||
4729 ( sy < m_propHoverY || sy >= (m_propHoverY+ih) )
4730 )
4731 {
4732 // Mouse moves on another property
4733
4734 m_propHover = DoGetItemAtY(y);
4735 m_propHoverY = curPropHoverY;
4736
4737 // Send hover event
4738 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
4739 }
4740
4741 #if wxPG_SUPPORT_TOOLTIPS
4742 // Store which side we are on
4743 m_mouseSide = 0;
4744 if ( columnHit == 1 )
4745 m_mouseSide = 2;
4746 else if ( columnHit == 0 )
4747 m_mouseSide = 1;
4748
4749 //
4750 // If tooltips are enabled, show label or value as a tip
4751 // in case it doesn't otherwise show in full length.
4752 //
4753 if ( m_windowStyle & wxPG_TOOLTIPS )
4754 {
4755 wxToolTip* tooltip = m_canvas->GetToolTip();
4756
4757 if ( m_propHover != prevHover || prevSide != m_mouseSide )
4758 {
4759 if ( m_propHover && !m_propHover->IsCategory() )
4760 {
4761
4762 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
4763 {
4764 // Show help string as a tooltip
4765 wxString tipString = m_propHover->GetHelpString();
4766
4767 SetToolTip(tipString);
4768 }
4769 else
4770 {
4771 // Show cropped value string as a tooltip
4772 wxString tipString;
4773 int space = 0;
4774
4775 if ( m_mouseSide == 1 )
4776 {
4777 tipString = m_propHover->m_label;
4778 space = splitterX-m_marginWidth-3;
4779 }
4780 else if ( m_mouseSide == 2 )
4781 {
4782 tipString = m_propHover->GetDisplayedString();
4783
4784 space = m_width - splitterX;
4785 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
4786 space -= wxPG_CUSTOM_IMAGE_WIDTH + wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
4787 }
4788
4789 if ( space )
4790 {
4791 int tw, th;
4792 GetTextExtent( tipString, &tw, &th, 0, 0 );
4793 if ( tw > space )
4794 {
4795 SetToolTip( tipString );
4796 }
4797 }
4798 else
4799 {
4800 if ( tooltip )
4801 {
4802 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4803 m_canvas->SetToolTip( m_emptyString );
4804 #else
4805 m_canvas->SetToolTip( NULL );
4806 #endif
4807 }
4808 }
4809
4810 }
4811 }
4812 else
4813 {
4814 if ( tooltip )
4815 {
4816 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4817 m_canvas->SetToolTip( m_emptyString );
4818 #else
4819 m_canvas->SetToolTip( NULL );
4820 #endif
4821 }
4822 }
4823 }
4824 }
4825 #endif
4826
4827 if ( splitterHit == -1 ||
4828 !m_propHover ||
4829 HasFlag(wxPG_STATIC_SPLITTER) )
4830 {
4831 // hovering on something else
4832 if ( m_curcursor != wxCURSOR_ARROW )
4833 CustomSetCursor( wxCURSOR_ARROW );
4834 }
4835 else
4836 {
4837 // Do not allow splitter cursor on caption items.
4838 // (also not if we were dragging and its started
4839 // outside the splitter region)
4840
4841 if ( !m_propHover->IsCategory() &&
4842 !event.Dragging() )
4843 {
4844
4845 // hovering on splitter
4846
4847 // NB: Condition disabled since MouseLeave event (from the editor control) cannot be
4848 // reliably detected.
4849 //if ( m_curcursor != wxCURSOR_SIZEWE )
4850 CustomSetCursor( wxCURSOR_SIZEWE, true );
4851
4852 return false;
4853 }
4854 else
4855 {
4856 // hovering on something else
4857 if ( m_curcursor != wxCURSOR_ARROW )
4858 CustomSetCursor( wxCURSOR_ARROW );
4859 }
4860 }
4861
4862 //
4863 // Multi select by dragging
4864 //
4865 if ( (GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION) &&
4866 event.LeftIsDown() &&
4867 m_propHover &&
4868 GetSelection() &&
4869 columnHit != 1 &&
4870 !state->DoIsPropertySelected(m_propHover) )
4871 {
4872 // Additional requirement is that the hovered property
4873 // is adjacent to edges of selection.
4874 const wxArrayPGProperty& selection = GetSelectedProperties();
4875
4876 // Since categories cannot be selected along with 'other'
4877 // properties, exclude them from iterator flags.
4878 int iterFlags = wxPG_ITERATE_VISIBLE & (~wxPG_PROP_CATEGORY);
4879
4880 for ( int i=(selection.size()-1); i>=0; i-- )
4881 {
4882 // TODO: This could be optimized by keeping track of
4883 // which properties are at the edges of selection.
4884 wxPGProperty* selProp = selection[i];
4885 if ( state->ArePropertiesAdjacent(m_propHover, selProp,
4886 iterFlags) )
4887 {
4888 DoAddToSelection(m_propHover);
4889 break;
4890 }
4891 }
4892 }
4893 }
4894 return true;
4895 }
4896
4897 // -----------------------------------------------------------------------
4898
4899 // Also handles Leaving event
4900 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
4901 wxMouseEvent &WXUNUSED(event) )
4902 {
4903 wxPropertyGridPageState* state = m_pState;
4904 bool res = false;
4905
4906 int splitterHit;
4907 int splitterHitOffset;
4908 state->HitTestH( x, &splitterHit, &splitterHitOffset );
4909
4910 // No event type check - basicly calling this method should
4911 // just stop dragging.
4912 // Left up after dragged?
4913 if ( m_dragStatus >= 1 )
4914 {
4915 //
4916 // End Splitter Dragging
4917 //
4918 // DO NOT ENABLE FOLLOWING LINE!
4919 // (it is only here as a reminder to not to do it)
4920 //splitterX = x;
4921
4922 // Disable splitter auto-centering
4923 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4924
4925 // This is necessary to return cursor
4926 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
4927 {
4928 m_canvas->ReleaseMouse();
4929 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
4930 }
4931
4932 // Set back the default cursor, if necessary
4933 if ( splitterHit == -1 ||
4934 !m_propHover )
4935 {
4936 CustomSetCursor( wxCURSOR_ARROW );
4937 }
4938
4939 m_dragStatus = 0;
4940
4941 // Control background needs to be cleared
4942 wxPGProperty* selected = GetSelection();
4943 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && selected )
4944 DrawItem( selected );
4945
4946 if ( m_wndEditor )
4947 {
4948 m_wndEditor->Show ( true );
4949 }
4950
4951 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4952 // Fixes button disappearance bug
4953 if ( m_wndEditor2 )
4954 m_wndEditor2->Show ( true );
4955 #endif
4956
4957 // This clears the focus.
4958 m_editorFocused = 0;
4959
4960 }
4961 return res;
4962 }
4963
4964 // -----------------------------------------------------------------------
4965
4966 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
4967 {
4968 int splitterX = GetSplitterPosition();
4969
4970 //int ux, uy;
4971 //CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
4972 int ux = event.m_x;
4973 int uy = event.m_y;
4974
4975 wxWindow* wnd = GetEditorControl();
4976
4977 // Hide popup on clicks
4978 if ( event.GetEventType() != wxEVT_MOTION )
4979 if ( wnd && wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
4980 {
4981 ((wxOwnerDrawnComboBox*)wnd)->HidePopup();
4982 }
4983
4984 wxRect r;
4985 if ( wnd )
4986 r = wnd->GetRect();
4987 if ( wnd == NULL || m_dragStatus ||
4988 (
4989 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
4990 ux >= (r.x+r.width) ||
4991 event.m_y < r.y ||
4992 event.m_y >= (r.y+r.height)
4993 )
4994 )
4995 {
4996 *px = ux;
4997 *py = uy;
4998 return true;
4999 }
5000 else
5001 {
5002 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5003 }
5004 return false;
5005 }
5006
5007 // -----------------------------------------------------------------------
5008
5009 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
5010 {
5011 int x, y;
5012 if ( OnMouseCommon( event, &x, &y ) )
5013 {
5014 HandleMouseClick(x,y,event);
5015 }
5016 event.Skip();
5017 }
5018
5019 // -----------------------------------------------------------------------
5020
5021 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
5022 {
5023 int x, y;
5024 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5025 HandleMouseRightClick(x,y,event);
5026 event.Skip();
5027 }
5028
5029 // -----------------------------------------------------------------------
5030
5031 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
5032 {
5033 // Always run standard mouse-down handler as well
5034 OnMouseClick(event);
5035
5036 int x, y;
5037 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5038 HandleMouseDoubleClick(x,y,event);
5039 event.Skip();
5040 }
5041
5042 // -----------------------------------------------------------------------
5043
5044 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
5045 {
5046 int x, y;
5047 if ( OnMouseCommon( event, &x, &y ) )
5048 {
5049 HandleMouseMove(x,y,event);
5050 }
5051 event.Skip();
5052 }
5053
5054 // -----------------------------------------------------------------------
5055
5056 void wxPropertyGrid::OnMouseMoveBottom( wxMouseEvent& WXUNUSED(event) )
5057 {
5058 // Called when mouse moves in the empty space below the properties.
5059 CustomSetCursor( wxCURSOR_ARROW );
5060 }
5061
5062 // -----------------------------------------------------------------------
5063
5064 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
5065 {
5066 int x, y;
5067 if ( OnMouseCommon( event, &x, &y ) )
5068 {
5069 HandleMouseUp(x,y,event);
5070 }
5071 event.Skip();
5072 }
5073
5074 // -----------------------------------------------------------------------
5075
5076 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
5077 {
5078 // This may get called from child control as well, so event's
5079 // mouse position cannot be relied on.
5080
5081 if ( event.Entering() )
5082 {
5083 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5084 {
5085 // TODO: Fix this (detect parent and only do
5086 // cursor trick if it is a manager).
5087 wxASSERT( GetParent() );
5088 GetParent()->SetCursor(wxNullCursor);
5089
5090 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
5091 }
5092 else
5093 GetParent()->SetCursor(wxNullCursor);
5094 }
5095 else if ( event.Leaving() )
5096 {
5097 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
5098 m_canvas->SetCursor( wxNullCursor );
5099
5100 // Get real cursor position
5101 wxPoint pt = ScreenToClient(::wxGetMousePosition());
5102
5103 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
5104 {
5105 {
5106 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5107 {
5108 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
5109 }
5110
5111 if ( m_dragStatus )
5112 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
5113 }
5114 }
5115 }
5116
5117 event.Skip();
5118 }
5119
5120 // -----------------------------------------------------------------------
5121
5122 // Common code used by various OnMouseXXXChild methods.
5123 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
5124 {
5125 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
5126 wxASSERT( topCtrlWnd );
5127 int x, y;
5128 event.GetPosition(&x,&y);
5129
5130 int splitterX = GetSplitterPosition();
5131
5132 wxRect r = topCtrlWnd->GetRect();
5133 if ( !m_dragStatus &&
5134 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
5135 y >= 0 && y < r.height \
5136 )
5137 {
5138 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5139 event.Skip();
5140 }
5141 else
5142 {
5143 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
5144 px, py );
5145 return true;
5146 }
5147 return false;
5148 }
5149
5150 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
5151 {
5152 int x,y;
5153 if ( OnMouseChildCommon(event,&x,&y) )
5154 {
5155 bool res = HandleMouseClick(x,y,event);
5156 if ( !res ) event.Skip();
5157 }
5158 }
5159
5160 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
5161 {
5162 int x,y;
5163 wxASSERT( m_wndEditor );
5164 // These coords may not be exact (about +-2),
5165 // but that should not matter (right click is about item, not position).
5166 wxPoint pt = m_wndEditor->GetPosition();
5167 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
5168
5169 // FIXME: Used to set m_propHover to selection here. Was it really
5170 // necessary?
5171
5172 bool res = HandleMouseRightClick(x,y,event);
5173 if ( !res ) event.Skip();
5174 }
5175
5176 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
5177 {
5178 int x,y;
5179 if ( OnMouseChildCommon(event,&x,&y) )
5180 {
5181 bool res = HandleMouseMove(x,y,event);
5182 if ( !res ) event.Skip();
5183 }
5184 }
5185
5186 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
5187 {
5188 int x,y;
5189 if ( OnMouseChildCommon(event,&x,&y) )
5190 {
5191 bool res = HandleMouseUp(x,y,event);
5192 if ( !res ) event.Skip();
5193 }
5194 }
5195
5196 // -----------------------------------------------------------------------
5197 // wxPropertyGrid keyboard event handling
5198 // -----------------------------------------------------------------------
5199
5200 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
5201 {
5202 // Translates wxKeyEvent to wxPG_ACTION_XXX
5203
5204 int keycode = event.GetKeyCode();
5205 int modifiers = event.GetModifiers();
5206
5207 wxASSERT( !(modifiers&~(0xFFFF)) );
5208
5209 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5210
5211 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
5212
5213 if ( it == m_actionTriggers.end() )
5214 return 0;
5215
5216 if ( pSecond )
5217 {
5218 int second = (it->second>>16) & 0xFFFF;
5219 *pSecond = second;
5220 }
5221
5222 return (it->second & 0xFFFF);
5223 }
5224
5225 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
5226 {
5227 wxASSERT( !(modifiers&~(0xFFFF)) );
5228
5229 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5230
5231 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
5232
5233 if ( it != m_actionTriggers.end() )
5234 {
5235 // This key combination is already used
5236
5237 // Can add secondary?
5238 wxASSERT_MSG( !(it->second&~(0xFFFF)),
5239 wxT("You can only add up to two separate actions per key combination.") );
5240
5241 action = it->second | (action<<16);
5242 }
5243
5244 m_actionTriggers[hashMapKey] = action;
5245 }
5246
5247 void wxPropertyGrid::ClearActionTriggers( int action )
5248 {
5249 wxPGHashMapI2I::iterator it;
5250 bool didSomething;
5251
5252 do
5253 {
5254 didSomething = false;
5255
5256 for ( it = m_actionTriggers.begin();
5257 it != m_actionTriggers.end();
5258 it++ )
5259 {
5260 if ( it->second == action )
5261 {
5262 m_actionTriggers.erase(it);
5263 didSomething = true;
5264 break;
5265 }
5266 }
5267 }
5268 while ( didSomething );
5269 }
5270
5271 void wxPropertyGrid::HandleKeyEvent( wxKeyEvent &event, bool fromChild )
5272 {
5273 //
5274 // Handles key event when editor control is not focused.
5275 //
5276
5277 wxCHECK2(!m_frozen, return);
5278
5279 // Travelsal between items, collapsing/expanding, etc.
5280 wxPGProperty* selected = GetSelection();
5281 int keycode = event.GetKeyCode();
5282 bool editorFocused = IsEditorFocused();
5283
5284 if ( keycode == WXK_TAB )
5285 {
5286 wxWindow* mainControl;
5287
5288 if ( HasInternalFlag(wxPG_FL_IN_MANAGER) )
5289 mainControl = GetParent();
5290 else
5291 mainControl = this;
5292
5293 if ( !event.ShiftDown() )
5294 {
5295 if ( !editorFocused && m_wndEditor )
5296 {
5297 DoSelectProperty( selected, wxPG_SEL_FOCUS );
5298 }
5299 else
5300 {
5301 // Tab traversal workaround for platforms on which
5302 // wxWindow::Navigate() may navigate into first child
5303 // instead of next sibling. Does not work perfectly
5304 // in every scenario (for instance, when property grid
5305 // is either first or last control).
5306 #if defined(__WXGTK__)
5307 wxWindow* sibling = mainControl->GetNextSibling();
5308 if ( sibling )
5309 sibling->SetFocusFromKbd();
5310 #else
5311 Navigate(wxNavigationKeyEvent::IsForward);
5312 #endif
5313 }
5314 }
5315 else
5316 {
5317 if ( editorFocused )
5318 {
5319 UnfocusEditor();
5320 }
5321 else
5322 {
5323 #if defined(__WXGTK__)
5324 wxWindow* sibling = mainControl->GetPrevSibling();
5325 if ( sibling )
5326 sibling->SetFocusFromKbd();
5327 #else
5328 Navigate(wxNavigationKeyEvent::IsBackward);
5329 #endif
5330 }
5331 }
5332
5333 return;
5334 }
5335
5336 // Ignore Alt and Control when they are down alone
5337 if ( keycode == WXK_ALT ||
5338 keycode == WXK_CONTROL )
5339 {
5340 event.Skip();
5341 return;
5342 }
5343
5344 int secondAction;
5345 int action = KeyEventToActions(event, &secondAction);
5346
5347 if ( editorFocused && action == wxPG_ACTION_CANCEL_EDIT )
5348 {
5349 //
5350 // Esc cancels any changes
5351 if ( IsEditorsValueModified() )
5352 {
5353 EditorsValueWasNotModified();
5354
5355 // Update the control as well
5356 selected->GetEditorClass()->
5357 SetControlStringValue( selected,
5358 GetEditorControl(),
5359 selected->GetDisplayedString() );
5360 }
5361
5362 OnValidationFailureReset(selected);
5363
5364 UnfocusEditor();
5365 return;
5366 }
5367
5368 // Except for TAB and ESC, handle child control events in child control
5369 if ( fromChild )
5370 {
5371 // Only propagate event if it had modifiers
5372 if ( !event.HasModifiers() )
5373 {
5374 event.StopPropagation();
5375 }
5376 event.Skip();
5377 return;
5378 }
5379
5380 bool wasHandled = false;
5381
5382 if ( selected )
5383 {
5384 // Show dialog?
5385 if ( ButtonTriggerKeyTest(action, event) )
5386 return;
5387
5388 wxPGProperty* p = selected;
5389
5390 // Travel and expand/collapse
5391 int selectDir = -2;
5392
5393 if ( p->GetChildCount() )
5394 {
5395 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5396 {
5397 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5398 wasHandled = true;
5399 }
5400 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5401 {
5402 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5403 wasHandled = true;
5404 }
5405 }
5406
5407 if ( !wasHandled )
5408 {
5409 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5410 {
5411 selectDir = -1;
5412 }
5413 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5414 {
5415 selectDir = 1;
5416 }
5417 }
5418
5419 if ( selectDir >= -1 )
5420 {
5421 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5422 if ( p )
5423 DoSelectProperty(p);
5424 wasHandled = true;
5425 }
5426 }
5427 else
5428 {
5429 // If nothing was selected, select the first item now
5430 // (or navigate out of tab).
5431 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5432 {
5433 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5434 if ( p ) DoSelectProperty(p);
5435 wasHandled = true;
5436 }
5437 }
5438
5439 if ( !wasHandled )
5440 event.Skip();
5441 }
5442
5443 // -----------------------------------------------------------------------
5444
5445 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5446 {
5447 // If there was editor open and focused, then this event should not
5448 // really be processed here.
5449 if ( IsEditorFocused() )
5450 {
5451 // However, if event had modifiers, it is probably still best
5452 // to skip it.
5453 if ( event.HasModifiers() )
5454 event.Skip();
5455 else
5456 event.StopPropagation();
5457 return;
5458 }
5459
5460 HandleKeyEvent(event, false);
5461 }
5462
5463 // -----------------------------------------------------------------------
5464
5465 bool wxPropertyGrid::ButtonTriggerKeyTest( int action, wxKeyEvent& event )
5466 {
5467 if ( action == -1 )
5468 {
5469 int secondAction;
5470 action = KeyEventToActions(event, &secondAction);
5471 }
5472
5473 // Does the keycode trigger button?
5474 if ( action == wxPG_ACTION_PRESS_BUTTON &&
5475 m_wndEditor2 )
5476 {
5477 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, m_wndEditor2->GetId());
5478 GetEventHandler()->AddPendingEvent(evt);
5479 return true;
5480 }
5481
5482 return false;
5483 }
5484
5485 // -----------------------------------------------------------------------
5486
5487 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5488 {
5489 HandleKeyEvent(event, true);
5490 }
5491
5492 // -----------------------------------------------------------------------
5493 // wxPropertyGrid miscellaneous event handling
5494 // -----------------------------------------------------------------------
5495
5496 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5497 {
5498 //
5499 // Check if the focus is in this control or one of its children
5500 wxWindow* newFocused = wxWindow::FindFocus();
5501
5502 if ( newFocused != m_curFocused )
5503 HandleFocusChange( newFocused );
5504
5505 //
5506 // Check if top-level parent has changed
5507 if ( GetExtraStyle() & wxPG_EX_ENABLE_TLP_TRACKING )
5508 {
5509 wxWindow* tlp = ::wxGetTopLevelParent(this);
5510 if ( tlp != m_tlp )
5511 OnTLPChanging(tlp);
5512 }
5513 }
5514
5515 bool wxPropertyGrid::IsEditorFocused() const
5516 {
5517 wxWindow* focus = wxWindow::FindFocus();
5518
5519 if ( focus == m_wndEditor || focus == m_wndEditor2 ||
5520 focus == GetEditorControl() )
5521 return true;
5522
5523 return false;
5524 }
5525
5526 // Called by focus event handlers. newFocused is the window that becomes focused.
5527 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5528 {
5529 unsigned int oldFlags = m_iFlags;
5530
5531 m_iFlags &= ~(wxPG_FL_FOCUSED);
5532
5533 wxWindow* parent = newFocused;
5534
5535 // This must be one of nextFocus' parents.
5536 while ( parent )
5537 {
5538 // Use m_eventObject, which is either wxPropertyGrid or
5539 // wxPropertyGridManager, as appropriate.
5540 if ( parent == m_eventObject )
5541 {
5542 m_iFlags |= wxPG_FL_FOCUSED;
5543 break;
5544 }
5545 parent = parent->GetParent();
5546 }
5547
5548 m_curFocused = newFocused;
5549
5550 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5551 (oldFlags & wxPG_FL_FOCUSED) )
5552 {
5553 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5554 {
5555 // Need to store changed value
5556 CommitChangesFromEditor();
5557 }
5558 else
5559 {
5560 /*
5561 //
5562 // Preliminary code for tab-order respecting
5563 // tab-traversal (but should be moved to
5564 // OnNav handler)
5565 //
5566 wxWindow* prevFocus = event.GetWindow();
5567 wxWindow* useThis = this;
5568 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5569 useThis = GetParent();
5570
5571 if ( prevFocus &&
5572 prevFocus->GetParent() == useThis->GetParent() )
5573 {
5574 wxList& children = useThis->GetParent()->GetChildren();
5575
5576 wxNode* node = children.Find(prevFocus);
5577
5578 if ( node->GetNext() &&
5579 useThis == node->GetNext()->GetData() )
5580 DoSelectProperty(GetFirst());
5581 else if ( node->GetPrevious () &&
5582 useThis == node->GetPrevious()->GetData() )
5583 DoSelectProperty(GetLastProperty());
5584
5585 }
5586 */
5587 }
5588
5589 // Redraw selected
5590 wxPGProperty* selected = GetSelection();
5591 if ( selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5592 DrawItem( selected );
5593 }
5594 }
5595
5596 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5597 {
5598 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5599 HandleFocusChange((wxWindow*)event.GetEventObject());
5600 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5601 //else if ( event.GetWindow() )
5602 else
5603 HandleFocusChange(event.GetWindow());
5604
5605 event.Skip();
5606 }
5607
5608 // -----------------------------------------------------------------------
5609
5610 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5611 {
5612 HandleFocusChange((wxWindow*)event.GetEventObject());
5613 event.Skip();
5614 }
5615
5616 // -----------------------------------------------------------------------
5617
5618 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5619 {
5620 m_iFlags |= wxPG_FL_SCROLLED;
5621
5622 event.Skip();
5623 }
5624
5625 // -----------------------------------------------------------------------
5626
5627 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5628 {
5629 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5630 {
5631 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5632 }
5633 }
5634
5635 // -----------------------------------------------------------------------
5636 // Property editor related functions
5637 // -----------------------------------------------------------------------
5638
5639 // noDefCheck = true prevents infinite recursion.
5640 wxPGEditor* wxPropertyGrid::DoRegisterEditorClass( wxPGEditor* editorClass,
5641 const wxString& editorName,
5642 bool noDefCheck )
5643 {
5644 wxASSERT( editorClass );
5645
5646 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5647 RegisterDefaultEditors();
5648
5649 wxString name = editorName;
5650 if ( name.length() == 0 )
5651 name = editorClass->GetName();
5652
5653 // Existing editor under this name?
5654 wxPGHashMapS2P::iterator vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5655
5656 if ( vt_it != wxPGGlobalVars->m_mapEditorClasses.end() )
5657 {
5658 // If this name was already used, try class name.
5659 name = editorClass->GetClassInfo()->GetClassName();
5660 vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5661 }
5662
5663 wxCHECK_MSG( vt_it == wxPGGlobalVars->m_mapEditorClasses.end(),
5664 (wxPGEditor*) vt_it->second,
5665 "Editor with given name was already registered" );
5666
5667 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorClass;
5668
5669 return editorClass;
5670 }
5671
5672 // Use this in RegisterDefaultEditors.
5673 #define wxPGRegisterDefaultEditorClass(EDITOR) \
5674 if ( wxPGEditor_##EDITOR == NULL ) \
5675 { \
5676 wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
5677 new wxPG##EDITOR##Editor, true ); \
5678 }
5679
5680 // Registers all default editor classes
5681 void wxPropertyGrid::RegisterDefaultEditors()
5682 {
5683 wxPGRegisterDefaultEditorClass( TextCtrl );
5684 wxPGRegisterDefaultEditorClass( Choice );
5685 wxPGRegisterDefaultEditorClass( ComboBox );
5686 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
5687 #if wxPG_INCLUDE_CHECKBOX
5688 wxPGRegisterDefaultEditorClass( CheckBox );
5689 #endif
5690 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
5691
5692 // Register SpinCtrl etc. editors before use
5693 RegisterAdditionalEditors();
5694 }
5695
5696 // -----------------------------------------------------------------------
5697 // wxPGStringTokenizer
5698 // Needed to handle C-style string lists (e.g. "str1" "str2")
5699 // -----------------------------------------------------------------------
5700
5701 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
5702 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
5703 {
5704 }
5705
5706 wxPGStringTokenizer::~wxPGStringTokenizer()
5707 {
5708 }
5709
5710 bool wxPGStringTokenizer::HasMoreTokens()
5711 {
5712 const wxString& str = *m_str;
5713
5714 wxString::const_iterator i = m_curPos;
5715
5716 wxUniChar delim = m_delimeter;
5717 wxUniChar a;
5718 wxUniChar prev_a = wxT('\0');
5719
5720 bool inToken = false;
5721
5722 while ( i != str.end() )
5723 {
5724 a = *i;
5725
5726 if ( !inToken )
5727 {
5728 if ( a == delim )
5729 {
5730 inToken = true;
5731 m_readyToken.clear();
5732 }
5733 }
5734 else
5735 {
5736 if ( prev_a != wxT('\\') )
5737 {
5738 if ( a != delim )
5739 {
5740 if ( a != wxT('\\') )
5741 m_readyToken << a;
5742 }
5743 else
5744 {
5745 ++i;
5746 m_curPos = i;
5747 return true;
5748 }
5749 prev_a = a;
5750 }
5751 else
5752 {
5753 m_readyToken << a;
5754 prev_a = wxT('\0');
5755 }
5756 }
5757 ++i;
5758 }
5759
5760 m_curPos = str.end();
5761
5762 if ( inToken )
5763 return true;
5764
5765 return false;
5766 }
5767
5768 wxString wxPGStringTokenizer::GetNextToken()
5769 {
5770 return m_readyToken;
5771 }
5772
5773 // -----------------------------------------------------------------------
5774 // wxPGChoiceEntry
5775 // -----------------------------------------------------------------------
5776
5777 wxPGChoiceEntry::wxPGChoiceEntry()
5778 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
5779 {
5780 }
5781
5782 // -----------------------------------------------------------------------
5783 // wxPGChoicesData
5784 // -----------------------------------------------------------------------
5785
5786 wxPGChoicesData::wxPGChoicesData()
5787 {
5788 }
5789
5790 wxPGChoicesData::~wxPGChoicesData()
5791 {
5792 Clear();
5793 }
5794
5795 void wxPGChoicesData::Clear()
5796 {
5797 m_items.clear();
5798 }
5799
5800 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
5801 {
5802 wxASSERT( m_items.size() == 0 );
5803
5804 m_items = data->m_items;
5805 }
5806
5807 wxPGChoiceEntry& wxPGChoicesData::Insert( int index,
5808 const wxPGChoiceEntry& item )
5809 {
5810 wxVector<wxPGChoiceEntry>::iterator it;
5811 if ( index == -1 )
5812 {
5813 it = m_items.end();
5814 index = (int) m_items.size();
5815 }
5816 else
5817 {
5818 it = m_items.begin() + index;
5819 }
5820
5821 m_items.insert(it, item);
5822
5823 wxPGChoiceEntry& ownEntry = m_items[index];
5824
5825 // Need to fix value?
5826 if ( ownEntry.GetValue() == wxPG_INVALID_VALUE )
5827 ownEntry.SetValue(index);
5828
5829 return ownEntry;
5830 }
5831
5832 // -----------------------------------------------------------------------
5833 // wxPropertyGridEvent
5834 // -----------------------------------------------------------------------
5835
5836 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
5837
5838
5839 wxDEFINE_EVENT( wxEVT_PG_SELECTED, wxPropertyGridEvent );
5840 wxDEFINE_EVENT( wxEVT_PG_CHANGING, wxPropertyGridEvent );
5841 wxDEFINE_EVENT( wxEVT_PG_CHANGED, wxPropertyGridEvent );
5842 wxDEFINE_EVENT( wxEVT_PG_HIGHLIGHTED, wxPropertyGridEvent );
5843 wxDEFINE_EVENT( wxEVT_PG_RIGHT_CLICK, wxPropertyGridEvent );
5844 wxDEFINE_EVENT( wxEVT_PG_PAGE_CHANGED, wxPropertyGridEvent );
5845 wxDEFINE_EVENT( wxEVT_PG_ITEM_EXPANDED, wxPropertyGridEvent );
5846 wxDEFINE_EVENT( wxEVT_PG_ITEM_COLLAPSED, wxPropertyGridEvent );
5847 wxDEFINE_EVENT( wxEVT_PG_DOUBLE_CLICK, wxPropertyGridEvent );
5848 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_BEGIN, wxPropertyGridEvent );
5849 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_ENDING, wxPropertyGridEvent );
5850
5851 // -----------------------------------------------------------------------
5852
5853 void wxPropertyGridEvent::Init()
5854 {
5855 m_validationInfo = NULL;
5856 m_column = 1;
5857 m_canVeto = false;
5858 m_wasVetoed = false;
5859 }
5860
5861 // -----------------------------------------------------------------------
5862
5863 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
5864 : wxCommandEvent(commandType,id)
5865 {
5866 m_property = NULL;
5867 Init();
5868 }
5869
5870 // -----------------------------------------------------------------------
5871
5872 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
5873 : wxCommandEvent(event)
5874 {
5875 m_eventType = event.GetEventType();
5876 m_eventObject = event.m_eventObject;
5877 m_pg = event.m_pg;
5878 OnPropertyGridSet();
5879 m_property = event.m_property;
5880 m_validationInfo = event.m_validationInfo;
5881 m_canVeto = event.m_canVeto;
5882 m_wasVetoed = event.m_wasVetoed;
5883 }
5884
5885 // -----------------------------------------------------------------------
5886
5887 void wxPropertyGridEvent::OnPropertyGridSet()
5888 {
5889 if ( !m_pg )
5890 return;
5891
5892 #if wxUSE_THREAD
5893 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
5894 #endif
5895 m_pg->m_liveEvents.push_back(this);
5896 }
5897
5898 // -----------------------------------------------------------------------
5899
5900 wxPropertyGridEvent::~wxPropertyGridEvent()
5901 {
5902 if ( m_pg )
5903 {
5904 #if wxUSE_THREAD
5905 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
5906 #endif
5907
5908 // Use iterate from the back since it is more likely that the event
5909 // being desroyed is at the end of the array.
5910 wxVector<wxPropertyGridEvent*>& liveEvents = m_pg->m_liveEvents;
5911
5912 for ( int i = liveEvents.size()-1; i >= 0; i-- )
5913 {
5914 if ( liveEvents[i] == this )
5915 {
5916 liveEvents.erase(liveEvents.begin() + i);
5917 break;
5918 }
5919 }
5920 }
5921 }
5922
5923 // -----------------------------------------------------------------------
5924
5925 wxEvent* wxPropertyGridEvent::Clone() const
5926 {
5927 return new wxPropertyGridEvent( *this );
5928 }
5929
5930 // -----------------------------------------------------------------------
5931 // wxPropertyGridPopulator
5932 // -----------------------------------------------------------------------
5933
5934 wxPropertyGridPopulator::wxPropertyGridPopulator()
5935 {
5936 m_state = NULL;
5937 m_pg = NULL;
5938 wxPGGlobalVars->m_offline++;
5939 }
5940
5941 // -----------------------------------------------------------------------
5942
5943 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
5944 {
5945 m_state = state;
5946 m_propHierarchy.clear();
5947 }
5948
5949 // -----------------------------------------------------------------------
5950
5951 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
5952 {
5953 m_pg = pg;
5954 pg->Freeze();
5955 }
5956
5957 // -----------------------------------------------------------------------
5958
5959 wxPropertyGridPopulator::~wxPropertyGridPopulator()
5960 {
5961 //
5962 // Free unused sets of choices
5963 wxPGHashMapS2P::iterator it;
5964
5965 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
5966 {
5967 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
5968 data->DecRef();
5969 }
5970
5971 if ( m_pg )
5972 {
5973 m_pg->Thaw();
5974 m_pg->GetPanel()->Refresh();
5975 }
5976 wxPGGlobalVars->m_offline--;
5977 }
5978
5979 // -----------------------------------------------------------------------
5980
5981 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
5982 const wxString& propLabel,
5983 const wxString& propName,
5984 const wxString* propValue,
5985 wxPGChoices* pChoices )
5986 {
5987 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
5988 wxPGProperty* parent = GetCurParent();
5989
5990 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
5991 {
5992 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
5993 return NULL;
5994 }
5995
5996 if ( !classInfo || !classInfo->IsKindOf(CLASSINFO(wxPGProperty)) )
5997 {
5998 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
5999 return NULL;
6000 }
6001
6002 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
6003
6004 property->SetLabel(propLabel);
6005 property->DoSetName(propName);
6006
6007 if ( pChoices && pChoices->IsOk() )
6008 property->SetChoices(*pChoices);
6009
6010 m_state->DoInsert(parent, -1, property);
6011
6012 if ( propValue )
6013 property->SetValueFromString( *propValue, wxPG_FULL_VALUE|
6014 wxPG_PROGRAMMATIC_VALUE );
6015
6016 return property;
6017 }
6018
6019 // -----------------------------------------------------------------------
6020
6021 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
6022 {
6023 m_propHierarchy.push_back(property);
6024 DoScanForChildren();
6025 m_propHierarchy.pop_back();
6026 }
6027
6028 // -----------------------------------------------------------------------
6029
6030 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
6031 const wxString& idString )
6032 {
6033 wxPGChoices choices;
6034
6035 // Using id?
6036 if ( choicesString[0] == wxT('@') )
6037 {
6038 wxString ids = choicesString.substr(1);
6039 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
6040 if ( it == m_dictIdChoices.end() )
6041 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
6042 else
6043 choices.AssignData((wxPGChoicesData*)it->second);
6044 }
6045 else
6046 {
6047 bool found = false;
6048 if ( idString.length() )
6049 {
6050 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
6051 if ( it != m_dictIdChoices.end() )
6052 {
6053 choices.AssignData((wxPGChoicesData*)it->second);
6054 found = true;
6055 }
6056 }
6057
6058 if ( !found )
6059 {
6060 // Parse choices string
6061 wxString::const_iterator it = choicesString.begin();
6062 wxString label;
6063 wxString value;
6064 int state = 0;
6065 bool labelValid = false;
6066
6067 for ( ; it != choicesString.end(); ++it )
6068 {
6069 wxChar c = *it;
6070
6071 if ( state != 1 )
6072 {
6073 if ( c == wxT('"') )
6074 {
6075 if ( labelValid )
6076 {
6077 long l;
6078 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6079 choices.Add(label, l);
6080 }
6081 labelValid = false;
6082 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
6083 value.clear();
6084 label.clear();
6085 state = 1;
6086 }
6087 else if ( c == wxT('=') )
6088 {
6089 if ( labelValid )
6090 {
6091 state = 2;
6092 }
6093 }
6094 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
6095 {
6096 value << c;
6097 }
6098 }
6099 else
6100 {
6101 if ( c == wxT('"') )
6102 {
6103 state = 0;
6104 labelValid = true;
6105 }
6106 else
6107 label << c;
6108 }
6109 }
6110
6111 if ( labelValid )
6112 {
6113 long l;
6114 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6115 choices.Add(label, l);
6116 }
6117
6118 if ( !choices.IsOk() )
6119 {
6120 choices.EnsureData();
6121 }
6122
6123 // Assign to id
6124 if ( idString.length() )
6125 m_dictIdChoices[idString] = choices.GetData();
6126 }
6127 }
6128
6129 return choices;
6130 }
6131
6132 // -----------------------------------------------------------------------
6133
6134 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
6135 {
6136 if ( s.Last() == wxT('%') )
6137 {
6138 wxString s2 = s.substr(0,s.length()-1);
6139 long val;
6140 if ( s2.ToLong(&val, 10) )
6141 {
6142 *pval = (val*max)/100;
6143 return true;
6144 }
6145 return false;
6146 }
6147
6148 return s.ToLong(pval, 10);
6149 }
6150
6151 // -----------------------------------------------------------------------
6152
6153 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
6154 const wxString& type,
6155 const wxString& value )
6156 {
6157 int l = m_propHierarchy.size();
6158 if ( !l )
6159 return false;
6160
6161 wxPGProperty* p = m_propHierarchy[l-1];
6162 wxString valuel = value.Lower();
6163 wxVariant variant;
6164
6165 if ( type.length() == 0 )
6166 {
6167 long v;
6168
6169 // Auto-detect type
6170 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6171 variant = true;
6172 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6173 variant = false;
6174 else if ( value.ToLong(&v, 0) )
6175 variant = v;
6176 else
6177 variant = value;
6178 }
6179 else
6180 {
6181 if ( type == wxT("string") )
6182 {
6183 variant = value;
6184 }
6185 else if ( type == wxT("int") )
6186 {
6187 long v = 0;
6188 value.ToLong(&v, 0);
6189 variant = v;
6190 }
6191 else if ( type == wxT("bool") )
6192 {
6193 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6194 variant = true;
6195 else
6196 variant = false;
6197 }
6198 else
6199 {
6200 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6201 return false;
6202 }
6203 }
6204
6205 p->SetAttribute( name, variant );
6206
6207 return true;
6208 }
6209
6210 // -----------------------------------------------------------------------
6211
6212 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6213 {
6214 wxLogError(_("Error in resource: %s"),msg.c_str());
6215 }
6216
6217 // -----------------------------------------------------------------------
6218
6219 #endif // wxUSE_PROPGRID