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