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