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