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