Correct empty space drawing
[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 // itemRect is in virtual grid space
1949 wxRect drawRect(itemsRect->x - vx,
1950 itemsRect->y - vy,
1951 itemsRect->width,
1952 itemsRect->height);
1953
1954 // items added check
1955 if ( m_pState->m_itemsAdded ) PrepareAfterItemsAdded();
1956
1957 int paintFinishY = 0;
1958
1959 if ( m_pState->m_properties->GetChildCount() > 0 )
1960 {
1961 wxDC* dcPtr = &dc;
1962 bool isBuffered = false;
1963
1964 #if wxPG_DOUBLE_BUFFER
1965 wxMemoryDC* bufferDC = NULL;
1966
1967 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
1968 {
1969 if ( !m_doubleBuffer )
1970 {
1971 paintFinishY = itemsRect->y;
1972 dcPtr = NULL;
1973 }
1974 else
1975 {
1976 bufferDC = new wxMemoryDC();
1977
1978 // If nothing was changed, then just copy from double-buffer
1979 bufferDC->SelectObject( *m_doubleBuffer );
1980 dcPtr = bufferDC;
1981
1982 isBuffered = true;
1983 }
1984 }
1985 #endif
1986
1987 if ( dcPtr )
1988 {
1989 // paintFinishY and drawBottomY are in buffer/physical space
1990 paintFinishY = DoDrawItems( *dcPtr, itemsRect, isBuffered );
1991 int drawBottomY = itemsRect->y + itemsRect->height - vy;
1992
1993 // Clear area beyond last painted property
1994 if ( paintFinishY < drawBottomY )
1995 {
1996 dcPtr->SetPen(m_colEmptySpace);
1997 dcPtr->SetBrush(m_colEmptySpace);
1998 dcPtr->DrawRectangle(0, paintFinishY,
1999 m_width,
2000 drawBottomY );
2001 }
2002 }
2003
2004 #if wxPG_DOUBLE_BUFFER
2005 if ( bufferDC )
2006 {
2007 dc.Blit( drawRect.x, drawRect.y, drawRect.width,
2008 drawRect.height,
2009 bufferDC, 0, 0, wxCOPY );
2010 delete bufferDC;
2011 }
2012 #endif
2013 }
2014 else
2015 {
2016 // Just clear the area
2017 dc.SetPen(m_colEmptySpace);
2018 dc.SetBrush(m_colEmptySpace);
2019 dc.DrawRectangle(drawRect);
2020 }
2021 }
2022
2023 // -----------------------------------------------------------------------
2024
2025 int wxPropertyGrid::DoDrawItems( wxDC& dc,
2026 const wxRect* itemsRect,
2027 bool isBuffered ) const
2028 {
2029 const wxPGProperty* firstItem;
2030 const wxPGProperty* lastItem;
2031
2032 firstItem = DoGetItemAtY(itemsRect->y);
2033 lastItem = DoGetItemAtY(itemsRect->y+itemsRect->height-1);
2034
2035 if ( !lastItem )
2036 lastItem = GetLastItem( wxPG_ITERATE_VISIBLE );
2037
2038 if ( m_frozen || m_height < 1 || firstItem == NULL )
2039 return itemsRect->y;
2040
2041 wxCHECK_MSG( !m_pState->m_itemsAdded, itemsRect->y,
2042 "no items added" );
2043 wxASSERT( m_pState->m_properties->GetChildCount() );
2044
2045 int lh = m_lineHeight;
2046
2047 int firstItemTopY;
2048 int lastItemBottomY;
2049
2050 firstItemTopY = itemsRect->y;
2051 lastItemBottomY = itemsRect->y + itemsRect->height;
2052
2053 // Align y coordinates to item boundaries
2054 firstItemTopY -= firstItemTopY % lh;
2055 lastItemBottomY += lh - (lastItemBottomY % lh);
2056 lastItemBottomY -= 1;
2057
2058 // Entire range outside scrolled, visible area?
2059 if ( firstItemTopY >= (int)m_pState->GetVirtualHeight() ||
2060 lastItemBottomY <= 0 )
2061 return itemsRect->y;
2062
2063 wxCHECK_MSG( firstItemTopY < lastItemBottomY,
2064 itemsRect->y,
2065 "invalid y values" );
2066
2067 /*
2068 wxLogDebug(" -> DoDrawItems ( \"%s\" -> \"%s\"
2069 "height=%i (ch=%i), itemsRect = 0x%lX )",
2070 firstItem->GetLabel().c_str(),
2071 lastItem->GetLabel().c_str(),
2072 (int)(lastItemBottomY - firstItemTopY),
2073 (int)m_height,
2074 (unsigned long)&itemsRect );
2075 */
2076
2077 wxRect r;
2078
2079 long windowStyle = m_windowStyle;
2080
2081 int xRelMod = 0;
2082
2083 //
2084 // With wxPG_DOUBLE_BUFFER, do double buffering
2085 // - buffer's y = 0, so align itemsRect and coordinates to that
2086 //
2087 #if wxPG_DOUBLE_BUFFER
2088 int yRelMod = 0;
2089
2090 wxRect cr2;
2091
2092 if ( isBuffered )
2093 {
2094 xRelMod = itemsRect->x;
2095 yRelMod = itemsRect->y;
2096
2097 //
2098 // itemsRect conversion
2099 cr2 = *itemsRect;
2100 cr2.x -= xRelMod;
2101 cr2.y -= yRelMod;
2102 itemsRect = &cr2;
2103 firstItemTopY -= yRelMod;
2104 lastItemBottomY -= yRelMod;
2105 }
2106 #else
2107 wxUnusedVar(isBuffered);
2108 #endif
2109
2110 int x = m_marginWidth - xRelMod;
2111
2112 wxFont normalFont = GetFont();
2113
2114 bool reallyFocused = (m_iFlags & wxPG_FL_FOCUSED) != 0;
2115
2116 bool isPgEnabled = IsEnabled();
2117
2118 //
2119 // Prepare some pens and brushes that are often changed to.
2120 //
2121
2122 wxBrush marginBrush(m_colMargin);
2123 wxPen marginPen(m_colMargin);
2124 wxBrush capbgbrush(m_colCapBack,wxSOLID);
2125 wxPen linepen(m_colLine,1,wxSOLID);
2126
2127 wxColour selBackCol;
2128 if ( isPgEnabled )
2129 selBackCol = m_colSelBack;
2130 else
2131 selBackCol = m_colMargin;
2132
2133 // pen that has same colour as text
2134 wxPen outlinepen(m_colPropFore,1,wxSOLID);
2135
2136 //
2137 // Clear margin with background colour
2138 //
2139 dc.SetBrush( marginBrush );
2140 if ( !(windowStyle & wxPG_HIDE_MARGIN) )
2141 {
2142 dc.SetPen( *wxTRANSPARENT_PEN );
2143 dc.DrawRectangle(-1-xRelMod,firstItemTopY-1,x+2,lastItemBottomY-firstItemTopY+2);
2144 }
2145
2146 const wxPGProperty* firstSelected = GetSelection();
2147 const wxPropertyGridPageState* state = m_pState;
2148 const wxArrayInt& colWidths = state->m_colWidths;
2149
2150 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2151 bool wasSelectedPainted = false;
2152 #endif
2153
2154 // TODO: Only render columns that are within clipping region.
2155
2156 dc.SetFont(normalFont);
2157
2158 wxPropertyGridConstIterator it( state, wxPG_ITERATE_VISIBLE, firstItem );
2159 int endScanBottomY = lastItemBottomY + lh;
2160 int y = firstItemTopY;
2161
2162 //
2163 // Pregenerate list of visible properties.
2164 wxArrayPGProperty visPropArray;
2165 visPropArray.reserve((m_height/m_lineHeight)+6);
2166
2167 for ( ; !it.AtEnd(); it.Next() )
2168 {
2169 const wxPGProperty* p = *it;
2170
2171 if ( !p->HasFlag(wxPG_PROP_HIDDEN) )
2172 {
2173 visPropArray.push_back((wxPGProperty*)p);
2174
2175 if ( y > endScanBottomY )
2176 break;
2177
2178 y += lh;
2179 }
2180 }
2181
2182 visPropArray.push_back(NULL);
2183
2184 wxPGProperty* nextP = visPropArray[0];
2185
2186 int gridWidth = state->m_width;
2187
2188 y = firstItemTopY;
2189 for ( unsigned int arrInd=1;
2190 nextP && y <= lastItemBottomY;
2191 arrInd++ )
2192 {
2193 wxPGProperty* p = nextP;
2194 nextP = visPropArray[arrInd];
2195
2196 int rowHeight = m_fontHeight+(m_spacingy*2)+1;
2197 int textMarginHere = x;
2198 int renderFlags = 0;
2199
2200 int greyDepth = m_marginWidth;
2201 if ( !(windowStyle & wxPG_HIDE_CATEGORIES) )
2202 greyDepth = (((int)p->m_depthBgCol)-1) * m_subgroup_extramargin + m_marginWidth;
2203
2204 int greyDepthX = greyDepth - xRelMod;
2205
2206 // Use basic depth if in non-categoric mode and parent is base array.
2207 if ( !(windowStyle & wxPG_HIDE_CATEGORIES) || p->GetParent() != m_pState->m_properties )
2208 {
2209 textMarginHere += ((unsigned int)((p->m_depth-1)*m_subgroup_extramargin));
2210 }
2211
2212 // Paint margin area
2213 dc.SetBrush(marginBrush);
2214 dc.SetPen(marginPen);
2215 dc.DrawRectangle( -xRelMod, y, greyDepth, lh );
2216
2217 dc.SetPen( linepen );
2218
2219 int y2 = y + lh;
2220
2221 #ifdef __WXMSW__
2222 // Margin Edge
2223 // Modified by JACS to not draw a margin if wxPG_HIDE_MARGIN is specified, since it
2224 // looks better, at least under Windows when we have a themed border (the themed-window-specific
2225 // whitespace between the real border and the propgrid margin exacerbates the double-border look).
2226
2227 // Is this or its parent themed?
2228 bool suppressMarginEdge = (GetWindowStyle() & wxPG_HIDE_MARGIN) &&
2229 (((GetWindowStyle() & wxBORDER_MASK) == wxBORDER_THEME) ||
2230 (((GetWindowStyle() & wxBORDER_MASK) == wxBORDER_NONE) && ((GetParent()->GetWindowStyle() & wxBORDER_MASK) == wxBORDER_THEME)));
2231 #else
2232 bool suppressMarginEdge = false;
2233 #endif
2234 if (!suppressMarginEdge)
2235 dc.DrawLine( greyDepthX, y, greyDepthX, y2 );
2236 else
2237 {
2238 // Blank out the margin edge
2239 dc.SetPen(wxPen(GetBackgroundColour()));
2240 dc.DrawLine( greyDepthX, y, greyDepthX, y2 );
2241 dc.SetPen( linepen );
2242 }
2243
2244 // Splitters
2245 unsigned int si;
2246 int sx = x;
2247
2248 for ( si=0; si<colWidths.size(); si++ )
2249 {
2250 sx += colWidths[si];
2251 dc.DrawLine( sx, y, sx, y2 );
2252 }
2253
2254 // Horizontal Line, below
2255 // (not if both this and next is category caption)
2256 if ( p->IsCategory() &&
2257 nextP && nextP->IsCategory() )
2258 dc.SetPen(m_colCapBack);
2259
2260 dc.DrawLine( greyDepthX, y2-1, gridWidth-xRelMod, y2-1 );
2261
2262 //
2263 // Need to override row colours?
2264 wxColour rowFgCol;
2265 wxColour rowBgCol;
2266
2267 bool isSelected = state->DoIsPropertySelected(p);
2268
2269 if ( !isSelected )
2270 {
2271 // Disabled may get different colour.
2272 if ( !p->IsEnabled() )
2273 {
2274 renderFlags |= wxPGCellRenderer::Disabled |
2275 wxPGCellRenderer::DontUseCellFgCol;
2276 rowFgCol = m_colDisPropFore;
2277 }
2278 }
2279 else
2280 {
2281 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2282 if ( p == firstSelected )
2283 wasSelectedPainted = true;
2284 #endif
2285
2286 renderFlags |= wxPGCellRenderer::Selected;
2287
2288 if ( !p->IsCategory() )
2289 {
2290 renderFlags |= wxPGCellRenderer::DontUseCellFgCol |
2291 wxPGCellRenderer::DontUseCellBgCol;
2292
2293 if ( reallyFocused && p == firstSelected )
2294 {
2295 rowFgCol = m_colSelFore;
2296 rowBgCol = selBackCol;
2297 }
2298 else if ( isPgEnabled )
2299 {
2300 rowFgCol = m_colPropFore;
2301 if ( p == firstSelected )
2302 rowBgCol = m_colMargin;
2303 else
2304 rowBgCol = selBackCol;
2305 }
2306 else
2307 {
2308 rowFgCol = m_colDisPropFore;
2309 rowBgCol = selBackCol;
2310 }
2311 }
2312 }
2313
2314 wxBrush rowBgBrush;
2315
2316 if ( rowBgCol.IsOk() )
2317 rowBgBrush = wxBrush(rowBgCol);
2318
2319 if ( HasInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL) )
2320 renderFlags = renderFlags & ~wxPGCellRenderer::DontUseCellColours;
2321
2322 //
2323 // Fill additional margin area with background colour of first cell
2324 if ( greyDepthX < textMarginHere )
2325 {
2326 if ( !(renderFlags & wxPGCellRenderer::DontUseCellBgCol) )
2327 {
2328 wxPGCell& cell = p->GetCell(0);
2329 rowBgCol = cell.GetBgCol();
2330 rowBgBrush = wxBrush(rowBgCol);
2331 }
2332 dc.SetBrush(rowBgBrush);
2333 dc.SetPen(rowBgCol);
2334 dc.DrawRectangle(greyDepthX+1, y,
2335 textMarginHere-greyDepthX, lh-1);
2336 }
2337
2338 bool fontChanged = false;
2339
2340 // Expander button rectangle
2341 wxRect butRect( ((p->m_depth - 1) * m_subgroup_extramargin) - xRelMod,
2342 y,
2343 m_marginWidth,
2344 lh );
2345
2346 // Default cell rect fill the entire row
2347 wxRect cellRect(greyDepthX, y,
2348 gridWidth - greyDepth + 2, rowHeight-1 );
2349
2350 bool isCategory = p->IsCategory();
2351
2352 if ( isCategory )
2353 {
2354 dc.SetFont(m_captionFont);
2355 fontChanged = true;
2356
2357 if ( renderFlags & wxPGCellRenderer::DontUseCellBgCol )
2358 {
2359 dc.SetBrush(rowBgBrush);
2360 dc.SetPen(rowBgCol);
2361 }
2362
2363 if ( renderFlags & wxPGCellRenderer::DontUseCellFgCol )
2364 {
2365 dc.SetTextForeground(rowFgCol);
2366 }
2367 }
2368 else
2369 {
2370 // Fine tune button rectangle to actually fit the cell
2371 if ( butRect.x > 0 )
2372 butRect.x += IN_CELL_EXPANDER_BUTTON_X_ADJUST;
2373
2374 if ( p->m_flags & wxPG_PROP_MODIFIED &&
2375 (windowStyle & wxPG_BOLD_MODIFIED) )
2376 {
2377 dc.SetFont(m_captionFont);
2378 fontChanged = true;
2379 }
2380
2381 // Magic fine-tuning for non-category rows
2382 cellRect.x += 1;
2383 }
2384
2385 int firstCellWidth = colWidths[0] - (greyDepthX - m_marginWidth);
2386 int firstCellX = cellRect.x;
2387
2388 // Calculate cellRect.x for the last cell
2389 unsigned int ci = 0;
2390 int cellX = x + 1;
2391 for ( ci=0; ci<colWidths.size(); ci++ )
2392 cellX += colWidths[ci];
2393 cellRect.x = cellX;
2394
2395 // Draw cells from back to front so that we can easily tell if the
2396 // cell on the right was empty from text
2397 bool prevFilled = true;
2398 ci = colWidths.size();
2399 do
2400 {
2401 ci--;
2402
2403 int textXAdd = 0;
2404
2405 if ( ci == 0 )
2406 {
2407 textXAdd = textMarginHere - greyDepthX;
2408 cellRect.width = firstCellWidth;
2409 cellRect.x = firstCellX;
2410 }
2411 else
2412 {
2413 int colWidth = colWidths[ci];
2414 cellRect.width = colWidth;
2415 cellRect.x -= colWidth;
2416 }
2417
2418 // Merge with column to the right?
2419 if ( !prevFilled && isCategory )
2420 {
2421 cellRect.width += colWidths[ci+1];
2422 }
2423
2424 if ( !isCategory )
2425 cellRect.width -= 1;
2426
2427 wxWindow* cellEditor = NULL;
2428 int cellRenderFlags = renderFlags;
2429
2430 // Tree Item Button (must be drawn before clipping is set up)
2431 if ( ci == 0 && !HasFlag(wxPG_HIDE_MARGIN) && p->HasVisibleChildren() )
2432 DrawExpanderButton( dc, butRect, p );
2433
2434 // Background
2435 if ( isSelected && (ci == 1 || ci == m_selColumn) )
2436 {
2437 if ( p == firstSelected )
2438 {
2439 if ( ci == 1 && m_wndEditor )
2440 cellEditor = m_wndEditor;
2441 else if ( ci == m_selColumn && m_labelEditor )
2442 cellEditor = m_labelEditor;
2443 }
2444
2445 if ( cellEditor )
2446 {
2447 wxColour editorBgCol =
2448 cellEditor->GetBackgroundColour();
2449 dc.SetBrush(editorBgCol);
2450 dc.SetPen(editorBgCol);
2451 dc.SetTextForeground(m_colPropFore);
2452 dc.DrawRectangle(cellRect);
2453
2454 if ( m_dragStatus != 0 ||
2455 (m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE) )
2456 cellEditor = NULL;
2457 }
2458 else
2459 {
2460 dc.SetBrush(m_colPropBack);
2461 dc.SetPen(m_colPropBack);
2462 if ( p->IsEnabled() )
2463 dc.SetTextForeground(m_colPropFore);
2464 else
2465 dc.SetTextForeground(m_colDisPropFore);
2466 }
2467 }
2468 else
2469 {
2470 if ( renderFlags & wxPGCellRenderer::DontUseCellBgCol )
2471 {
2472 dc.SetBrush(rowBgBrush);
2473 dc.SetPen(rowBgCol);
2474 }
2475
2476 if ( renderFlags & wxPGCellRenderer::DontUseCellFgCol )
2477 {
2478 dc.SetTextForeground(rowFgCol);
2479 }
2480 }
2481
2482 dc.SetClippingRegion(cellRect);
2483
2484 cellRect.x += textXAdd;
2485 cellRect.width -= textXAdd;
2486
2487 // Foreground
2488 if ( !cellEditor )
2489 {
2490 wxPGCellRenderer* renderer;
2491 int cmnVal = p->GetCommonValue();
2492 if ( cmnVal == -1 || ci != 1 )
2493 {
2494 renderer = p->GetCellRenderer(ci);
2495 prevFilled = renderer->Render(dc, cellRect, this,
2496 p, ci, -1,
2497 cellRenderFlags );
2498 }
2499 else
2500 {
2501 renderer = GetCommonValue(cmnVal)->GetRenderer();
2502 prevFilled = renderer->Render(dc, cellRect, this,
2503 p, ci, -1,
2504 cellRenderFlags );
2505 }
2506 }
2507 else
2508 {
2509 prevFilled = true;
2510 }
2511
2512 dc.DestroyClippingRegion(); // Is this really necessary?
2513 }
2514 while ( ci > 0 );
2515
2516 if ( fontChanged )
2517 dc.SetFont(normalFont);
2518
2519 y += rowHeight;
2520 }
2521
2522 // Refresh editor controls (seems not needed on msw)
2523 // NOTE: This code is mandatory for GTK!
2524 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2525 if ( wasSelectedPainted )
2526 {
2527 if ( m_wndEditor )
2528 m_wndEditor->Refresh();
2529 if ( m_wndEditor2 )
2530 m_wndEditor2->Refresh();
2531 }
2532 #endif
2533
2534 return y;
2535 }
2536
2537 // -----------------------------------------------------------------------
2538
2539 wxRect wxPropertyGrid::GetPropertyRect( const wxPGProperty* p1, const wxPGProperty* p2 ) const
2540 {
2541 wxRect r;
2542
2543 if ( m_width < 10 || m_height < 10 ||
2544 !m_pState->m_properties->GetChildCount() ||
2545 p1 == NULL )
2546 return wxRect(0,0,0,0);
2547
2548 int vy = 0;
2549
2550 //
2551 // Return rect which encloses the given property range
2552 // (in logical grid coordinates)
2553 //
2554
2555 int visTop = p1->GetY();
2556 int visBottom;
2557 if ( p2 )
2558 visBottom = p2->GetY() + m_lineHeight;
2559 else
2560 visBottom = m_height + visTop;
2561
2562 // If seleced property is inside the range, we'll extend the range to include
2563 // control's size.
2564 wxPGProperty* selected = GetSelection();
2565 if ( selected )
2566 {
2567 int selectedY = selected->GetY();
2568 if ( selectedY >= visTop && selectedY < visBottom )
2569 {
2570 wxWindow* editor = GetEditorControl();
2571 if ( editor )
2572 {
2573 int visBottom2 = selectedY + editor->GetSize().y;
2574 if ( visBottom2 > visBottom )
2575 visBottom = visBottom2;
2576 }
2577 }
2578 }
2579
2580 return wxRect(0,visTop-vy,m_pState->m_width,visBottom-visTop);
2581 }
2582
2583 // -----------------------------------------------------------------------
2584
2585 void wxPropertyGrid::DrawItems( const wxPGProperty* p1, const wxPGProperty* p2 )
2586 {
2587 if ( m_frozen )
2588 return;
2589
2590 if ( m_pState->m_itemsAdded )
2591 PrepareAfterItemsAdded();
2592
2593 wxRect r = GetPropertyRect(p1, p2);
2594 if ( r.width > 0 )
2595 {
2596 // Convert rectangle from logical grid coordinates to physical ones
2597 int vx, vy;
2598 GetViewStart(&vx, &vy);
2599 vx *= wxPG_PIXELS_PER_UNIT;
2600 vy *= wxPG_PIXELS_PER_UNIT;
2601 r.x -= vx;
2602 r.y -= vy;
2603 RefreshRect(r);
2604 }
2605 }
2606
2607 // -----------------------------------------------------------------------
2608
2609 void wxPropertyGrid::RefreshProperty( wxPGProperty* p )
2610 {
2611 if ( m_pState->DoIsPropertySelected(p) )
2612 {
2613 // NB: We must copy the selection.
2614 wxArrayPGProperty selection = m_pState->m_selection;
2615 DoSetSelection(selection, wxPG_SEL_FORCE);
2616 }
2617
2618 DrawItemAndChildren(p);
2619 }
2620
2621 // -----------------------------------------------------------------------
2622
2623 void wxPropertyGrid::DrawItemAndValueRelated( wxPGProperty* p )
2624 {
2625 if ( m_frozen )
2626 return;
2627
2628 // Draw item, children, and parent too, if it is not category
2629 wxPGProperty* parent = p->GetParent();
2630
2631 while ( parent &&
2632 !parent->IsCategory() &&
2633 parent->GetParent() )
2634 {
2635 DrawItem(parent);
2636 parent = parent->GetParent();
2637 }
2638
2639 DrawItemAndChildren(p);
2640 }
2641
2642 void wxPropertyGrid::DrawItemAndChildren( wxPGProperty* p )
2643 {
2644 wxCHECK_RET( p, wxT("invalid property id") );
2645
2646 // Do not draw if in non-visible page
2647 if ( p->GetParentState() != m_pState )
2648 return;
2649
2650 // do not draw a single item if multiple pending
2651 if ( m_pState->m_itemsAdded || m_frozen )
2652 return;
2653
2654 // Update child control.
2655 wxPGProperty* selected = GetSelection();
2656 if ( selected && selected->GetParent() == p )
2657 RefreshEditor();
2658
2659 const wxPGProperty* lastDrawn = p->GetLastVisibleSubItem();
2660
2661 DrawItems(p, lastDrawn);
2662 }
2663
2664 // -----------------------------------------------------------------------
2665
2666 void wxPropertyGrid::Refresh( bool WXUNUSED(eraseBackground),
2667 const wxRect *rect )
2668 {
2669 PrepareAfterItemsAdded();
2670
2671 wxWindow::Refresh(false, rect);
2672
2673 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2674 // I think this really helps only GTK+1.2
2675 if ( m_wndEditor ) m_wndEditor->Refresh();
2676 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
2677 #endif
2678 }
2679
2680 // -----------------------------------------------------------------------
2681 // wxPropertyGrid global operations
2682 // -----------------------------------------------------------------------
2683
2684 void wxPropertyGrid::Clear()
2685 {
2686 m_pState->DoClear();
2687
2688 m_propHover = NULL;
2689
2690 m_prevVY = 0;
2691
2692 RecalculateVirtualSize();
2693
2694 // Need to clear some area at the end
2695 if ( !m_frozen )
2696 RefreshRect(wxRect(0, 0, m_width, m_height));
2697 }
2698
2699 // -----------------------------------------------------------------------
2700
2701 bool wxPropertyGrid::EnableCategories( bool enable )
2702 {
2703 DoClearSelection();
2704
2705 if ( enable )
2706 {
2707 //
2708 // Enable categories
2709 //
2710
2711 m_windowStyle &= ~(wxPG_HIDE_CATEGORIES);
2712 }
2713 else
2714 {
2715 //
2716 // Disable categories
2717 //
2718 m_windowStyle |= wxPG_HIDE_CATEGORIES;
2719 }
2720
2721 if ( !m_pState->EnableCategories(enable) )
2722 return false;
2723
2724 if ( !m_frozen )
2725 {
2726 if ( m_windowStyle & wxPG_AUTO_SORT )
2727 {
2728 m_pState->m_itemsAdded = 1; // force
2729 PrepareAfterItemsAdded();
2730 }
2731 }
2732 else
2733 m_pState->m_itemsAdded = 1;
2734
2735 // No need for RecalculateVirtualSize() here - it is already called in
2736 // wxPropertyGridPageState method above.
2737
2738 Refresh();
2739
2740 return true;
2741 }
2742
2743 // -----------------------------------------------------------------------
2744
2745 void wxPropertyGrid::SwitchState( wxPropertyGridPageState* pNewState )
2746 {
2747 wxASSERT( pNewState );
2748 wxASSERT( pNewState->GetGrid() );
2749
2750 if ( pNewState == m_pState )
2751 return;
2752
2753 wxArrayPGProperty oldSelection = m_pState->m_selection;
2754
2755 // Call ClearSelection() instead of DoClearSelection()
2756 // so that selection clear events are not sent.
2757 ClearSelection();
2758
2759 m_pState->m_selection = oldSelection;
2760
2761 bool orig_mode = m_pState->IsInNonCatMode();
2762 bool new_state_mode = pNewState->IsInNonCatMode();
2763
2764 m_pState = pNewState;
2765
2766 // Validate width
2767 int pgWidth = GetClientSize().x;
2768 if ( HasVirtualWidth() )
2769 {
2770 int minWidth = pgWidth;
2771 if ( pNewState->m_width < minWidth )
2772 {
2773 pNewState->m_width = minWidth;
2774 pNewState->CheckColumnWidths();
2775 }
2776 }
2777 else
2778 {
2779 //
2780 // Just in case, fully re-center splitter
2781 //if ( HasFlag( wxPG_SPLITTER_AUTO_CENTER ) )
2782 // pNewState->m_fSplitterX = -1.0;
2783
2784 pNewState->OnClientWidthChange(pgWidth,
2785 pgWidth - pNewState->m_width);
2786 }
2787
2788 m_propHover = NULL;
2789
2790 // If necessary, convert state to correct mode.
2791 if ( orig_mode != new_state_mode )
2792 {
2793 // This should refresh as well.
2794 EnableCategories( orig_mode?false:true );
2795 }
2796 else if ( !m_frozen )
2797 {
2798 // Refresh, if not frozen.
2799 m_pState->PrepareAfterItemsAdded();
2800
2801 // Reselect (Use SetSelection() instead of Do-variant so that
2802 // events won't be sent).
2803 SetSelection(m_pState->m_selection);
2804
2805 RecalculateVirtualSize(0);
2806 Refresh();
2807 }
2808 else
2809 m_pState->m_itemsAdded = 1;
2810 }
2811
2812 // -----------------------------------------------------------------------
2813
2814 // Call to SetSplitterPosition will always disable splitter auto-centering
2815 // if parent window is shown.
2816 void wxPropertyGrid::DoSetSplitterPosition( int newxpos,
2817 int splitterIndex,
2818 int flags )
2819 {
2820 if ( ( newxpos < wxPG_DRAG_MARGIN ) )
2821 return;
2822
2823 wxPropertyGridPageState* state = m_pState;
2824
2825 if ( flags & wxPG_SPLITTER_FROM_EVENT )
2826 state->m_dontCenterSplitter = true;
2827
2828 state->DoSetSplitterPosition(newxpos, splitterIndex, flags);
2829
2830 if ( flags & wxPG_SPLITTER_REFRESH )
2831 {
2832 if ( GetSelection() )
2833 CorrectEditorWidgetSizeX();
2834
2835 Refresh();
2836 }
2837
2838 return;
2839 }
2840
2841 // -----------------------------------------------------------------------
2842
2843 void wxPropertyGrid::ResetColumnSizes( bool enableAutoResizing )
2844 {
2845 wxPropertyGridPageState* state = m_pState;
2846 if ( state )
2847 state->ResetColumnSizes(0);
2848
2849 if ( enableAutoResizing && HasFlag(wxPG_SPLITTER_AUTO_CENTER) )
2850 m_pState->m_dontCenterSplitter = false;
2851 }
2852
2853 // -----------------------------------------------------------------------
2854
2855 void wxPropertyGrid::CenterSplitter( bool enableAutoResizing )
2856 {
2857 SetSplitterPosition( m_width/2 );
2858 if ( enableAutoResizing && HasFlag(wxPG_SPLITTER_AUTO_CENTER) )
2859 m_pState->m_dontCenterSplitter = false;
2860 }
2861
2862 // -----------------------------------------------------------------------
2863 // wxPropertyGrid item iteration (GetNextProperty etc.) methods
2864 // -----------------------------------------------------------------------
2865
2866 // Returns nearest paint visible property (such that will be painted unless
2867 // window is scrolled or resized). If given property is paint visible, then
2868 // it itself will be returned
2869 wxPGProperty* wxPropertyGrid::GetNearestPaintVisible( wxPGProperty* p ) const
2870 {
2871 int vx,vy1;// Top left corner of client
2872 GetViewStart(&vx,&vy1);
2873 vy1 *= wxPG_PIXELS_PER_UNIT;
2874
2875 int vy2 = vy1 + m_height;
2876 int propY = p->GetY2(m_lineHeight);
2877
2878 if ( (propY + m_lineHeight) < vy1 )
2879 {
2880 // Too high
2881 return DoGetItemAtY( vy1 );
2882 }
2883 else if ( propY > vy2 )
2884 {
2885 // Too low
2886 return DoGetItemAtY( vy2 );
2887 }
2888
2889 // Itself paint visible
2890 return p;
2891
2892 }
2893
2894 // -----------------------------------------------------------------------
2895 // Methods related to change in value, value modification and sending events
2896 // -----------------------------------------------------------------------
2897
2898 // commits any changes in editor of selected property
2899 // return true if validation did not fail
2900 // flags are same as with DoSelectProperty
2901 bool wxPropertyGrid::CommitChangesFromEditor( wxUint32 flags )
2902 {
2903 // Committing already?
2904 if ( m_inCommitChangesFromEditor )
2905 return true;
2906
2907 // Don't do this if already processing editor event. It might
2908 // induce recursive dialogs and crap like that.
2909 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
2910 {
2911 if ( m_inDoPropertyChanged )
2912 return true;
2913
2914 return false;
2915 }
2916
2917 wxPGProperty* selected = GetSelection();
2918
2919 if ( m_wndEditor &&
2920 IsEditorsValueModified() &&
2921 (m_iFlags & wxPG_FL_INITIALIZED) &&
2922 selected )
2923 {
2924 m_inCommitChangesFromEditor = true;
2925
2926 wxVariant variant(selected->GetValueRef());
2927 bool valueIsPending = false;
2928
2929 // JACS - necessary to avoid new focus being found spuriously within OnIdle
2930 // due to another window getting focus
2931 wxWindow* oldFocus = m_curFocused;
2932
2933 bool validationFailure = false;
2934 bool forceSuccess = (flags & (wxPG_SEL_NOVALIDATE|wxPG_SEL_FORCE)) ? true : false;
2935
2936 m_chgInfo_changedProperty = NULL;
2937
2938 // If truly modified, schedule value as pending.
2939 if ( selected->GetEditorClass()->
2940 GetValueFromControl( variant,
2941 selected,
2942 GetEditorControl() ) )
2943 {
2944 if ( DoEditorValidate() &&
2945 PerformValidation(selected, variant) )
2946 {
2947 valueIsPending = true;
2948 }
2949 else
2950 {
2951 validationFailure = true;
2952 }
2953 }
2954 else
2955 {
2956 EditorsValueWasNotModified();
2957 }
2958
2959 m_inCommitChangesFromEditor = false;
2960
2961 bool res = true;
2962
2963 if ( validationFailure && !forceSuccess )
2964 {
2965 if (oldFocus)
2966 {
2967 oldFocus->SetFocus();
2968 m_curFocused = oldFocus;
2969 }
2970
2971 res = OnValidationFailure(selected, variant);
2972
2973 // Now prevent further validation failure messages
2974 if ( res )
2975 {
2976 EditorsValueWasNotModified();
2977 OnValidationFailureReset(selected);
2978 }
2979 }
2980 else if ( valueIsPending )
2981 {
2982 DoPropertyChanged( selected, flags );
2983 EditorsValueWasNotModified();
2984 }
2985
2986 return res;
2987 }
2988
2989 return true;
2990 }
2991
2992 // -----------------------------------------------------------------------
2993
2994 bool wxPropertyGrid::PerformValidation( wxPGProperty* p, wxVariant& pendingValue,
2995 int flags )
2996 {
2997 //
2998 // Runs all validation functionality.
2999 // Returns true if value passes all tests.
3000 //
3001
3002 m_validationInfo.m_failureBehavior = m_permanentValidationFailureBehavior;
3003 m_validationInfo.m_isFailing = true;
3004
3005 //
3006 // Variant list a special value that cannot be validated
3007 // by normal means.
3008 if ( pendingValue.GetType() != wxPG_VARIANT_TYPE_LIST )
3009 {
3010 if ( !p->ValidateValue(pendingValue, m_validationInfo) )
3011 return false;
3012 }
3013
3014 //
3015 // Adapt list to child values, if necessary
3016 wxVariant listValue = pendingValue;
3017 wxVariant* pPendingValue = &pendingValue;
3018 wxVariant* pList = NULL;
3019
3020 // If parent has wxPG_PROP_AGGREGATE flag, or uses composite
3021 // string value, then we need treat as it was changed instead
3022 // (or, in addition, as is the case with composite string parent).
3023 // This includes creating list variant for child values.
3024
3025 wxPGProperty* pwc = p->GetParent();
3026 wxPGProperty* changedProperty = p;
3027 wxPGProperty* baseChangedProperty = changedProperty;
3028 wxVariant bcpPendingList;
3029
3030 listValue = pendingValue;
3031 listValue.SetName(p->GetBaseName());
3032
3033 while ( pwc &&
3034 (pwc->HasFlag(wxPG_PROP_AGGREGATE) || pwc->HasFlag(wxPG_PROP_COMPOSED_VALUE)) )
3035 {
3036 wxVariantList tempList;
3037 wxVariant lv(tempList, pwc->GetBaseName());
3038 lv.Append(listValue);
3039 listValue = lv;
3040 pPendingValue = &listValue;
3041
3042 if ( pwc->HasFlag(wxPG_PROP_AGGREGATE) )
3043 {
3044 baseChangedProperty = pwc;
3045 bcpPendingList = lv;
3046 }
3047
3048 changedProperty = pwc;
3049 pwc = pwc->GetParent();
3050 }
3051
3052 wxVariant value;
3053 wxPGProperty* evtChangingProperty = changedProperty;
3054
3055 if ( pPendingValue->GetType() != wxPG_VARIANT_TYPE_LIST )
3056 {
3057 value = *pPendingValue;
3058 }
3059 else
3060 {
3061 // Convert list to child values
3062 pList = pPendingValue;
3063 changedProperty->AdaptListToValue( *pPendingValue, &value );
3064 }
3065
3066 wxVariant evtChangingValue = value;
3067
3068 if ( flags & SendEvtChanging )
3069 {
3070 // FIXME: After proper ValueToString()s added, remove
3071 // this. It is just a temporary fix, as evt_changing
3072 // will simply not work for wxPG_PROP_COMPOSED_VALUE
3073 // (unless it is selected, and textctrl editor is open).
3074 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
3075 {
3076 evtChangingProperty = baseChangedProperty;
3077 if ( evtChangingProperty != p )
3078 {
3079 evtChangingProperty->AdaptListToValue( bcpPendingList, &evtChangingValue );
3080 }
3081 else
3082 {
3083 evtChangingValue = pendingValue;
3084 }
3085 }
3086
3087 if ( evtChangingProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
3088 {
3089 if ( changedProperty == GetSelection() )
3090 {
3091 wxWindow* editor = GetEditorControl();
3092 wxASSERT( editor->IsKindOf(CLASSINFO(wxTextCtrl)) );
3093 evtChangingValue = wxStaticCast(editor, wxTextCtrl)->GetValue();
3094 }
3095 else
3096 {
3097 wxLogDebug(wxT("WARNING: wxEVT_PG_CHANGING is about to happen with old value."));
3098 }
3099 }
3100 }
3101
3102 wxASSERT( m_chgInfo_changedProperty == NULL );
3103 m_chgInfo_changedProperty = changedProperty;
3104 m_chgInfo_baseChangedProperty = baseChangedProperty;
3105 m_chgInfo_pendingValue = value;
3106
3107 if ( pList )
3108 m_chgInfo_valueList = *pList;
3109 else
3110 m_chgInfo_valueList.MakeNull();
3111
3112 // If changedProperty is not property which value was edited,
3113 // then call wxPGProperty::ValidateValue() for that as well.
3114 if ( p != changedProperty && value.GetType() != wxPG_VARIANT_TYPE_LIST )
3115 {
3116 if ( !changedProperty->ValidateValue(value, m_validationInfo) )
3117 return false;
3118 }
3119
3120 if ( flags & SendEvtChanging )
3121 {
3122 // SendEvent returns true if event was vetoed
3123 if ( SendEvent( wxEVT_PG_CHANGING, evtChangingProperty,
3124 &evtChangingValue ) )
3125 return false;
3126 }
3127
3128 if ( flags & IsStandaloneValidation )
3129 {
3130 // If called in 'generic' context, we need to reset
3131 // m_chgInfo_changedProperty and write back translated value.
3132 m_chgInfo_changedProperty = NULL;
3133 pendingValue = value;
3134 }
3135
3136 m_validationInfo.m_isFailing = false;
3137
3138 return true;
3139 }
3140
3141 // -----------------------------------------------------------------------
3142
3143 #if wxUSE_STATUSBAR
3144 wxStatusBar* wxPropertyGrid::GetStatusBar()
3145 {
3146 wxWindow* topWnd = ::wxGetTopLevelParent(this);
3147 if ( topWnd && topWnd->IsKindOf(CLASSINFO(wxFrame)) )
3148 {
3149 wxFrame* pFrame = wxStaticCast(topWnd, wxFrame);
3150 if ( pFrame )
3151 return pFrame->GetStatusBar();
3152 }
3153 return NULL;
3154 }
3155 #endif
3156
3157 // -----------------------------------------------------------------------
3158
3159 void wxPropertyGrid::DoShowPropertyError( wxPGProperty* WXUNUSED(property), const wxString& msg )
3160 {
3161 if ( !msg.length() )
3162 return;
3163
3164 #if wxUSE_STATUSBAR
3165 if ( !wxPGGlobalVars->m_offline )
3166 {
3167 wxStatusBar* pStatusBar = GetStatusBar();
3168 if ( pStatusBar )
3169 {
3170 pStatusBar->SetStatusText(msg);
3171 return;
3172 }
3173 }
3174 #endif
3175
3176 ::wxMessageBox(msg, _("Property Error"));
3177 }
3178
3179 // -----------------------------------------------------------------------
3180
3181 void wxPropertyGrid::DoHidePropertyError( wxPGProperty* WXUNUSED(property) )
3182 {
3183 #if wxUSE_STATUSBAR
3184 if ( !wxPGGlobalVars->m_offline )
3185 {
3186 wxStatusBar* pStatusBar = GetStatusBar();
3187 if ( pStatusBar )
3188 {
3189 pStatusBar->SetStatusText(wxEmptyString);
3190 return;
3191 }
3192 }
3193 #endif
3194 }
3195
3196 // -----------------------------------------------------------------------
3197
3198 bool wxPropertyGrid::OnValidationFailure( wxPGProperty* property,
3199 wxVariant& invalidValue )
3200 {
3201 if ( m_inOnValidationFailure )
3202 return true;
3203
3204 m_inOnValidationFailure = true;
3205 wxON_BLOCK_EXIT_SET(m_inOnValidationFailure, false);
3206
3207 wxWindow* editor = GetEditorControl();
3208 int vfb = m_validationInfo.m_failureBehavior;
3209
3210 if ( m_inDoSelectProperty )
3211 {
3212 // When property selection is being changed, do not display any
3213 // messages, if some were already shown for this property.
3214 if ( property->HasFlag(wxPG_PROP_INVALID_VALUE) )
3215 {
3216 m_validationInfo.m_failureBehavior =
3217 vfb & ~(wxPG_VFB_SHOW_MESSAGE |
3218 wxPG_VFB_SHOW_MESSAGEBOX |
3219 wxPG_VFB_SHOW_MESSAGE_ON_STATUSBAR);
3220 }
3221 }
3222
3223 // First call property's handler
3224 property->OnValidationFailure(invalidValue);
3225
3226 bool res = DoOnValidationFailure(property, invalidValue);
3227
3228 //
3229 // For non-wxTextCtrl editors, we do need to revert the value
3230 if ( !editor->IsKindOf(CLASSINFO(wxTextCtrl)) &&
3231 property == GetSelection() )
3232 {
3233 property->GetEditorClass()->UpdateControl(property, editor);
3234 }
3235
3236 property->SetFlag(wxPG_PROP_INVALID_VALUE);
3237
3238 return res;
3239 }
3240
3241 bool wxPropertyGrid::DoOnValidationFailure( wxPGProperty* property, wxVariant& WXUNUSED(invalidValue) )
3242 {
3243 int vfb = m_validationInfo.m_failureBehavior;
3244
3245 if ( vfb & wxPG_VFB_BEEP )
3246 ::wxBell();
3247
3248 if ( (vfb & wxPG_VFB_MARK_CELL) &&
3249 !property->HasFlag(wxPG_PROP_INVALID_VALUE) )
3250 {
3251 unsigned int colCount = m_pState->GetColumnCount();
3252
3253 // We need backup marked property's cells
3254 m_propCellsBackup = property->m_cells;
3255
3256 wxColour vfbFg = *wxWHITE;
3257 wxColour vfbBg = *wxRED;
3258
3259 property->EnsureCells(colCount);
3260
3261 for ( unsigned int i=0; i<colCount; i++ )
3262 {
3263 wxPGCell& cell = property->m_cells[i];
3264 cell.SetFgCol(vfbFg);
3265 cell.SetBgCol(vfbBg);
3266 }
3267
3268 DrawItemAndChildren(property);
3269
3270 if ( property == GetSelection() )
3271 {
3272 SetInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3273
3274 wxWindow* editor = GetEditorControl();
3275 if ( editor )
3276 {
3277 editor->SetForegroundColour(vfbFg);
3278 editor->SetBackgroundColour(vfbBg);
3279 }
3280 }
3281 }
3282
3283 if ( vfb & (wxPG_VFB_SHOW_MESSAGE |
3284 wxPG_VFB_SHOW_MESSAGEBOX |
3285 wxPG_VFB_SHOW_MESSAGE_ON_STATUSBAR) )
3286 {
3287 wxString msg = m_validationInfo.m_failureMessage;
3288
3289 if ( !msg.length() )
3290 msg = _("You have entered invalid value. Press ESC to cancel editing.");
3291
3292 #if wxUSE_STATUSBAR
3293 if ( vfb & wxPG_VFB_SHOW_MESSAGE_ON_STATUSBAR )
3294 {
3295 if ( !wxPGGlobalVars->m_offline )
3296 {
3297 wxStatusBar* pStatusBar = GetStatusBar();
3298 if ( pStatusBar )
3299 pStatusBar->SetStatusText(msg);
3300 }
3301 }
3302 #endif
3303
3304 if ( vfb & wxPG_VFB_SHOW_MESSAGE )
3305 DoShowPropertyError(property, msg);
3306
3307 if ( vfb & wxPG_VFB_SHOW_MESSAGEBOX )
3308 ::wxMessageBox(msg, _("Property Error"));
3309 }
3310
3311 return (vfb & wxPG_VFB_STAY_IN_PROPERTY) ? false : true;
3312 }
3313
3314 // -----------------------------------------------------------------------
3315
3316 void wxPropertyGrid::DoOnValidationFailureReset( wxPGProperty* property )
3317 {
3318 int vfb = m_validationInfo.m_failureBehavior;
3319
3320 if ( vfb & wxPG_VFB_MARK_CELL )
3321 {
3322 // Revert cells
3323 property->m_cells = m_propCellsBackup;
3324
3325 ClearInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3326
3327 if ( property == GetSelection() && GetEditorControl() )
3328 {
3329 // Calling this will recreate the control, thus resetting its colour
3330 RefreshProperty(property);
3331 }
3332 else
3333 {
3334 DrawItemAndChildren(property);
3335 }
3336 }
3337
3338 #if wxUSE_STATUSBAR
3339 if ( vfb & wxPG_VFB_SHOW_MESSAGE_ON_STATUSBAR )
3340 {
3341 if ( !wxPGGlobalVars->m_offline )
3342 {
3343 wxStatusBar* pStatusBar = GetStatusBar();
3344 if ( pStatusBar )
3345 pStatusBar->SetStatusText(wxEmptyString);
3346 }
3347 }
3348 #endif
3349
3350 if ( vfb & wxPG_VFB_SHOW_MESSAGE )
3351 {
3352 DoHidePropertyError(property);
3353 }
3354
3355 m_validationInfo.m_isFailing = false;
3356 }
3357
3358 // -----------------------------------------------------------------------
3359
3360 // flags are same as with DoSelectProperty
3361 bool wxPropertyGrid::DoPropertyChanged( wxPGProperty* p, unsigned int selFlags )
3362 {
3363 if ( m_inDoPropertyChanged )
3364 return true;
3365
3366 m_inDoPropertyChanged = true;
3367 wxON_BLOCK_EXIT_SET(m_inDoPropertyChanged, false);
3368
3369 wxPGProperty* selected = GetSelection();
3370
3371 m_pState->m_anyModified = 1;
3372
3373 // If property's value is being changed, assume it is valid
3374 OnValidationFailureReset(selected);
3375
3376 // Maybe need to update control
3377 wxASSERT( m_chgInfo_changedProperty != NULL );
3378
3379 // These values were calculated in PerformValidation()
3380 wxPGProperty* changedProperty = m_chgInfo_changedProperty;
3381 wxVariant value = m_chgInfo_pendingValue;
3382
3383 wxPGProperty* topPaintedProperty = changedProperty;
3384
3385 while ( !topPaintedProperty->IsCategory() &&
3386 !topPaintedProperty->IsRoot() )
3387 {
3388 topPaintedProperty = topPaintedProperty->GetParent();
3389 }
3390
3391 changedProperty->SetValue(value, &m_chgInfo_valueList, wxPG_SETVAL_BY_USER);
3392
3393 // NB: Call GetEditorControl() as late as possible, because OnSetValue()
3394 // and perhaps other user-defined virtual functions may change it.
3395 wxWindow* editor = GetEditorControl();
3396
3397 // Set as Modified (not if dragging just began)
3398 if ( !(p->m_flags & wxPG_PROP_MODIFIED) )
3399 {
3400 p->m_flags |= wxPG_PROP_MODIFIED;
3401 if ( p == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3402 {
3403 if ( editor )
3404 SetCurControlBoldFont();
3405 }
3406 }
3407
3408 wxPGProperty* pwc;
3409
3410 // Propagate updates to parent(s)
3411 pwc = p;
3412 wxPGProperty* prevPwc = NULL;
3413
3414 while ( prevPwc != topPaintedProperty )
3415 {
3416 pwc->m_flags |= wxPG_PROP_MODIFIED;
3417
3418 if ( pwc == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3419 {
3420 if ( editor )
3421 SetCurControlBoldFont();
3422 }
3423
3424 prevPwc = pwc;
3425 pwc = pwc->GetParent();
3426 }
3427
3428 // Draw the actual property
3429 DrawItemAndChildren( topPaintedProperty );
3430
3431 //
3432 // If value was set by wxPGProperty::OnEvent, then update the editor
3433 // control.
3434 if ( selFlags & wxPG_SEL_DIALOGVAL )
3435 {
3436 RefreshEditor();
3437 }
3438 else
3439 {
3440 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
3441 if ( m_wndEditor ) m_wndEditor->Refresh();
3442 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
3443 #endif
3444 }
3445
3446 // Sanity check
3447 wxASSERT( !changedProperty->GetParent()->HasFlag(wxPG_PROP_AGGREGATE) );
3448
3449 // If top parent has composite string value, then send to child parents,
3450 // starting from baseChangedProperty.
3451 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
3452 {
3453 pwc = m_chgInfo_baseChangedProperty;
3454
3455 while ( pwc != changedProperty )
3456 {
3457 SendEvent( wxEVT_PG_CHANGED, pwc, NULL );
3458 pwc = pwc->GetParent();
3459 }
3460 }
3461
3462 SendEvent( wxEVT_PG_CHANGED, changedProperty, NULL );
3463
3464 return true;
3465 }
3466
3467 // -----------------------------------------------------------------------
3468
3469 bool wxPropertyGrid::ChangePropertyValue( wxPGPropArg id, wxVariant newValue )
3470 {
3471 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
3472
3473 m_chgInfo_changedProperty = NULL;
3474
3475 if ( PerformValidation(p, newValue) )
3476 {
3477 DoPropertyChanged(p);
3478 return true;
3479 }
3480 else
3481 {
3482 OnValidationFailure(p, newValue);
3483 }
3484
3485 return false;
3486 }
3487
3488 // -----------------------------------------------------------------------
3489
3490 wxVariant wxPropertyGrid::GetUncommittedPropertyValue()
3491 {
3492 wxPGProperty* prop = GetSelectedProperty();
3493
3494 if ( !prop )
3495 return wxNullVariant;
3496
3497 wxTextCtrl* tc = GetEditorTextCtrl();
3498 wxVariant value = prop->GetValue();
3499
3500 if ( !tc || !IsEditorsValueModified() )
3501 return value;
3502
3503 if ( !prop->StringToValue(value, tc->GetValue()) )
3504 return value;
3505
3506 if ( !PerformValidation(prop, value, IsStandaloneValidation) )
3507 return prop->GetValue();
3508
3509 return value;
3510 }
3511
3512 // -----------------------------------------------------------------------
3513
3514 // Runs wxValidator for the selected property
3515 bool wxPropertyGrid::DoEditorValidate()
3516 {
3517 #if wxUSE_VALIDATORS
3518 wxRecursionGuard guard(m_validatingEditor);
3519 if ( guard.IsInside() )
3520 return false;
3521
3522 wxPGProperty* selected = GetSelection();
3523 if ( selected )
3524 {
3525 wxWindow* wnd = GetEditorControl();
3526
3527 wxValidator* validator = selected->GetValidator();
3528 if ( validator && wnd )
3529 {
3530 validator->SetWindow(wnd);
3531 if ( !validator->Validate(this) )
3532 return false;
3533 }
3534 }
3535 #endif
3536 return true;
3537 }
3538
3539 // -----------------------------------------------------------------------
3540
3541 void wxPropertyGrid::HandleCustomEditorEvent( wxEvent &event )
3542 {
3543 // It is possible that this handler receives event even before
3544 // the control has been properly initialized. Let's skip the
3545 // event handling in that case.
3546 if ( !m_pState )
3547 return;
3548
3549 // Don't care about the event if it originated from the
3550 // 'label editor'. In this function we only care about the
3551 // property value editor.
3552 if ( m_labelEditor && event.GetId() == m_labelEditor->GetId() )
3553 {
3554 event.Skip();
3555 return;
3556 }
3557
3558 wxPGProperty* selected = GetSelection();
3559
3560 // Somehow, event is handled after property has been deselected.
3561 // Possibly, but very rare.
3562 if ( !selected ||
3563 selected->HasFlag(wxPG_PROP_BEING_DELETED) ||
3564 m_inOnValidationFailure ||
3565 // Also don't handle editor event if wxEVT_PG_CHANGED or
3566 // similar is currently doing something (showing a
3567 // message box, for instance).
3568 m_processedEvent )
3569 return;
3570
3571 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
3572 return;
3573
3574 wxVariant pendingValue(selected->GetValueRef());
3575 wxWindow* wnd = GetEditorControl();
3576 wxWindow* editorWnd = wxDynamicCast(event.GetEventObject(), wxWindow);
3577 int selFlags = 0;
3578 bool wasUnspecified = selected->IsValueUnspecified();
3579 int usesAutoUnspecified = selected->UsesAutoUnspecified();
3580 bool valueIsPending = false;
3581
3582 m_chgInfo_changedProperty = NULL;
3583
3584 m_iFlags &= ~wxPG_FL_VALUE_CHANGE_IN_EVENT;
3585
3586 //
3587 // Filter out excess wxTextCtrl modified events
3588 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED && wnd )
3589 {
3590 if ( wnd->IsKindOf(CLASSINFO(wxTextCtrl)) )
3591 {
3592 wxTextCtrl* tc = (wxTextCtrl*) wnd;
3593
3594 wxString newTcValue = tc->GetValue();
3595 if ( m_prevTcValue == newTcValue )
3596 return;
3597 m_prevTcValue = newTcValue;
3598 }
3599 else if ( wnd->IsKindOf(CLASSINFO(wxComboCtrl)) )
3600 {
3601 wxComboCtrl* cc = (wxComboCtrl*) wnd;
3602
3603 wxString newTcValue = cc->GetTextCtrl()->GetValue();
3604 if ( m_prevTcValue == newTcValue )
3605 return;
3606 m_prevTcValue = newTcValue;
3607 }
3608 }
3609
3610 SetInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3611
3612 bool validationFailure = false;
3613 bool buttonWasHandled = false;
3614
3615 //
3616 // Try common button handling
3617 if ( m_wndEditor2 && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3618 {
3619 wxPGEditorDialogAdapter* adapter = selected->GetEditorDialog();
3620
3621 if ( adapter )
3622 {
3623 buttonWasHandled = true;
3624 // Store as res2, as previously (and still currently alternatively)
3625 // dialogs can be shown by handling wxEVT_COMMAND_BUTTON_CLICKED
3626 // in wxPGProperty::OnEvent().
3627 adapter->ShowDialog( this, selected );
3628 delete adapter;
3629 }
3630 }
3631
3632 if ( !buttonWasHandled )
3633 {
3634 if ( wnd || m_wndEditor2 )
3635 {
3636 // First call editor class' event handler.
3637 const wxPGEditor* editor = selected->GetEditorClass();
3638
3639 if ( editor->OnEvent( this, selected, editorWnd, event ) )
3640 {
3641 // If changes, validate them
3642 if ( DoEditorValidate() )
3643 {
3644 if ( editor->GetValueFromControl( pendingValue,
3645 selected,
3646 wnd ) )
3647 valueIsPending = true;
3648
3649 // Mark value always as pending if validation is currently
3650 // failing and value was not unspecified
3651 if ( !valueIsPending &&
3652 !pendingValue.IsNull() &&
3653 m_validationInfo.m_isFailing )
3654 valueIsPending = true;
3655 }
3656 else
3657 {
3658 validationFailure = true;
3659 }
3660 }
3661 }
3662
3663 // Then the property's custom handler (must be always called, unless
3664 // validation failed).
3665 if ( !validationFailure )
3666 buttonWasHandled = selected->OnEvent( this, editorWnd, event );
3667 }
3668
3669 // SetValueInEvent(), as called in one of the functions referred above
3670 // overrides editor's value.
3671 if ( m_iFlags & wxPG_FL_VALUE_CHANGE_IN_EVENT )
3672 {
3673 valueIsPending = true;
3674 pendingValue = m_changeInEventValue;
3675 selFlags |= wxPG_SEL_DIALOGVAL;
3676 }
3677
3678 if ( !validationFailure && valueIsPending )
3679 if ( !PerformValidation(selected, pendingValue) )
3680 validationFailure = true;
3681
3682 if ( validationFailure)
3683 {
3684 OnValidationFailure(selected, pendingValue);
3685 }
3686 else if ( valueIsPending )
3687 {
3688 selFlags |= ( !wasUnspecified && selected->IsValueUnspecified() && usesAutoUnspecified ) ? wxPG_SEL_SETUNSPEC : 0;
3689
3690 DoPropertyChanged(selected, selFlags);
3691 EditorsValueWasNotModified();
3692
3693 // Regardless of editor type, unfocus editor on
3694 // text-editing related enter press.
3695 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3696 {
3697 SetFocusOnCanvas();
3698 }
3699 }
3700 else
3701 {
3702 // No value after all
3703
3704 // Regardless of editor type, unfocus editor on
3705 // text-editing related enter press.
3706 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3707 {
3708 SetFocusOnCanvas();
3709 }
3710
3711 // Let unhandled button click events go to the parent
3712 if ( !buttonWasHandled && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3713 {
3714 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED,GetId());
3715 GetEventHandler()->AddPendingEvent(evt);
3716 }
3717 }
3718
3719 ClearInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3720 }
3721
3722 // -----------------------------------------------------------------------
3723 // wxPropertyGrid editor control helper methods
3724 // -----------------------------------------------------------------------
3725
3726 wxRect wxPropertyGrid::GetEditorWidgetRect( wxPGProperty* p, int column ) const
3727 {
3728 int itemy = p->GetY2(m_lineHeight);
3729 int splitterX = m_pState->DoGetSplitterPosition(column-1);
3730 int colEnd = splitterX + m_pState->m_colWidths[column];
3731 int imageOffset = 0;
3732
3733 int vx, vy; // Top left corner of client
3734 GetViewStart(&vx, &vy);
3735 vy *= wxPG_PIXELS_PER_UNIT;
3736
3737 if ( column == 1 )
3738 {
3739 // TODO: If custom image detection changes from current, change this.
3740 if ( m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE )
3741 {
3742 //m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3743 int iw = p->OnMeasureImage().x;
3744 if ( iw < 1 )
3745 iw = wxPG_CUSTOM_IMAGE_WIDTH;
3746 imageOffset = p->GetImageOffset(iw);
3747 }
3748 }
3749 else if ( column == 0 )
3750 {
3751 splitterX += (p->m_depth - 1) * m_subgroup_extramargin;
3752 }
3753
3754 return wxRect
3755 (
3756 splitterX+imageOffset+wxPG_XBEFOREWIDGET+wxPG_CONTROL_MARGIN+1,
3757 itemy-vy,
3758 colEnd-splitterX-wxPG_XBEFOREWIDGET-wxPG_CONTROL_MARGIN-imageOffset-1,
3759 m_lineHeight-1
3760 );
3761 }
3762
3763 // -----------------------------------------------------------------------
3764
3765 wxRect wxPropertyGrid::GetImageRect( wxPGProperty* p, int item ) const
3766 {
3767 wxSize sz = GetImageSize(p, item);
3768 return wxRect(wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
3769 wxPG_CUSTOM_IMAGE_SPACINGY,
3770 sz.x,
3771 sz.y);
3772 }
3773
3774 // return size of custom paint image
3775 wxSize wxPropertyGrid::GetImageSize( wxPGProperty* p, int item ) const
3776 {
3777 // If called with NULL property, then return default image
3778 // size for properties that use image.
3779 if ( !p )
3780 return wxSize(wxPG_CUSTOM_IMAGE_WIDTH,wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight));
3781
3782 wxSize cis = p->OnMeasureImage(item);
3783
3784 int choiceCount = p->m_choices.GetCount();
3785 int comVals = p->GetDisplayedCommonValueCount();
3786 if ( item >= choiceCount && comVals > 0 )
3787 {
3788 unsigned int cvi = item-choiceCount;
3789 cis = GetCommonValue(cvi)->GetRenderer()->GetImageSize(NULL, 1, cvi);
3790 }
3791 else if ( item >= 0 && choiceCount == 0 )
3792 return wxSize(0, 0);
3793
3794 if ( cis.x < 0 )
3795 {
3796 if ( cis.x <= -1 )
3797 cis.x = wxPG_CUSTOM_IMAGE_WIDTH;
3798 }
3799 if ( cis.y <= 0 )
3800 {
3801 if ( cis.y >= -1 )
3802 cis.y = wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight);
3803 else
3804 cis.y = -cis.y;
3805 }
3806 return cis;
3807 }
3808
3809 // -----------------------------------------------------------------------
3810
3811 // takes scrolling into account
3812 void wxPropertyGrid::ImprovedClientToScreen( int* px, int* py )
3813 {
3814 int vx, vy;
3815 GetViewStart(&vx,&vy);
3816 vy*=wxPG_PIXELS_PER_UNIT;
3817 vx*=wxPG_PIXELS_PER_UNIT;
3818 *px -= vx;
3819 *py -= vy;
3820 ClientToScreen( px, py );
3821 }
3822
3823 // -----------------------------------------------------------------------
3824
3825 wxPropertyGridHitTestResult wxPropertyGrid::HitTest( const wxPoint& pt ) const
3826 {
3827 wxPoint pt2;
3828 GetViewStart(&pt2.x,&pt2.y);
3829 pt2.x *= wxPG_PIXELS_PER_UNIT;
3830 pt2.y *= wxPG_PIXELS_PER_UNIT;
3831 pt2.x += pt.x;
3832 pt2.y += pt.y;
3833
3834 return m_pState->HitTest(pt2);
3835 }
3836
3837 // -----------------------------------------------------------------------
3838
3839 // custom set cursor
3840 void wxPropertyGrid::CustomSetCursor( int type, bool override )
3841 {
3842 if ( type == m_curcursor && !override ) return;
3843
3844 wxCursor* cursor = &wxPG_DEFAULT_CURSOR;
3845
3846 if ( type == wxCURSOR_SIZEWE )
3847 cursor = m_cursorSizeWE;
3848
3849 SetCursor( *cursor );
3850
3851 m_curcursor = type;
3852 }
3853
3854 // -----------------------------------------------------------------------
3855
3856 wxString
3857 wxPropertyGrid::GetUnspecifiedValueText( int argFlags ) const
3858 {
3859 const wxPGCell& ua = GetUnspecifiedValueAppearance();
3860
3861 if ( ua.HasText() &&
3862 !(argFlags & wxPG_FULL_VALUE) &&
3863 !(argFlags & wxPG_EDITABLE_VALUE) )
3864 return ua.GetText();
3865
3866 return wxEmptyString;
3867 }
3868
3869 // -----------------------------------------------------------------------
3870 // wxPropertyGrid property selection, editor creation
3871 // -----------------------------------------------------------------------
3872
3873 //
3874 // This class forwards events from property editor controls to wxPropertyGrid.
3875 class wxPropertyGridEditorEventForwarder : public wxEvtHandler
3876 {
3877 public:
3878 wxPropertyGridEditorEventForwarder( wxPropertyGrid* propGrid )
3879 : wxEvtHandler(), m_propGrid(propGrid)
3880 {
3881 }
3882
3883 virtual ~wxPropertyGridEditorEventForwarder()
3884 {
3885 }
3886
3887 private:
3888 bool ProcessEvent( wxEvent& event )
3889 {
3890 // Always skip
3891 event.Skip();
3892
3893 m_propGrid->HandleCustomEditorEvent(event);
3894
3895 //
3896 // NB: On wxMSW, a wxTextCtrl with wxTE_PROCESS_ENTER
3897 // may beep annoyingly if that event is skipped
3898 // and passed to parent event handler.
3899 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3900 return true;
3901
3902 return wxEvtHandler::ProcessEvent(event);
3903 }
3904
3905 wxPropertyGrid* m_propGrid;
3906 };
3907
3908 // Setups event handling for child control
3909 void wxPropertyGrid::SetupChildEventHandling( wxWindow* argWnd )
3910 {
3911 wxWindowID id = argWnd->GetId();
3912
3913 if ( argWnd == m_wndEditor )
3914 {
3915 argWnd->Connect(id, wxEVT_MOTION,
3916 wxMouseEventHandler(wxPropertyGrid::OnMouseMoveChild),
3917 NULL, this);
3918 argWnd->Connect(id, wxEVT_LEFT_UP,
3919 wxMouseEventHandler(wxPropertyGrid::OnMouseUpChild),
3920 NULL, this);
3921 argWnd->Connect(id, wxEVT_LEFT_DOWN,
3922 wxMouseEventHandler(wxPropertyGrid::OnMouseClickChild),
3923 NULL, this);
3924 argWnd->Connect(id, wxEVT_RIGHT_UP,
3925 wxMouseEventHandler(wxPropertyGrid::OnMouseRightClickChild),
3926 NULL, this);
3927 argWnd->Connect(id, wxEVT_ENTER_WINDOW,
3928 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3929 NULL, this);
3930 argWnd->Connect(id, wxEVT_LEAVE_WINDOW,
3931 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3932 NULL, this);
3933 }
3934
3935 wxPropertyGridEditorEventForwarder* forwarder;
3936 forwarder = new wxPropertyGridEditorEventForwarder(this);
3937 argWnd->PushEventHandler(forwarder);
3938
3939 argWnd->Connect(id, wxEVT_KEY_DOWN,
3940 wxCharEventHandler(wxPropertyGrid::OnChildKeyDown),
3941 NULL, this);
3942 }
3943
3944 void wxPropertyGrid::DestroyEditorWnd( wxWindow* wnd )
3945 {
3946 if ( !wnd )
3947 return;
3948
3949 wnd->Hide();
3950
3951 // Do not free editors immediately (for sake of processing events)
3952 wxPendingDelete.Append(wnd);
3953 }
3954
3955 void wxPropertyGrid::FreeEditors()
3956 {
3957 //
3958 // Return focus back to canvas from children (this is required at least for
3959 // GTK+, which, unlike Windows, clears focus when control is destroyed
3960 // instead of moving it to closest parent).
3961 wxWindow* focus = wxWindow::FindFocus();
3962 if ( focus )
3963 {
3964 wxWindow* parent = focus->GetParent();
3965 while ( parent )
3966 {
3967 if ( parent == this )
3968 {
3969 SetFocusOnCanvas();
3970 break;
3971 }
3972 parent = parent->GetParent();
3973 }
3974 }
3975
3976 // Do not free editors immediately if processing events
3977 if ( m_wndEditor2 )
3978 {
3979 wxEvtHandler* handler = m_wndEditor2->PopEventHandler(false);
3980 m_wndEditor2->Hide();
3981 wxPendingDelete.Append( handler );
3982 DestroyEditorWnd(m_wndEditor2);
3983 m_wndEditor2 = NULL;
3984 }
3985
3986 if ( m_wndEditor )
3987 {
3988 wxEvtHandler* handler = m_wndEditor->PopEventHandler(false);
3989 m_wndEditor->Hide();
3990 wxPendingDelete.Append( handler );
3991 DestroyEditorWnd(m_wndEditor);
3992 m_wndEditor = NULL;
3993 }
3994 }
3995
3996 // Call with NULL to de-select property
3997 bool wxPropertyGrid::DoSelectProperty( wxPGProperty* p, unsigned int flags )
3998 {
3999 /*
4000 if (p)
4001 {
4002 wxLogDebug(wxT("SelectProperty( %s (%s[%i]) )"),p->m_label.c_str(),
4003 p->m_parent->m_label.c_str(),p->GetIndexInParent());
4004 }
4005 else
4006 {
4007 wxLogDebug(wxT("SelectProperty( NULL, -1 )"));
4008 }
4009 */
4010
4011 if ( m_inDoSelectProperty )
4012 return true;
4013
4014 m_inDoSelectProperty = true;
4015 wxON_BLOCK_EXIT_SET(m_inDoSelectProperty, false);
4016
4017 if ( !m_pState )
4018 return false;
4019
4020 wxArrayPGProperty prevSelection = m_pState->m_selection;
4021 wxPGProperty* prevFirstSel;
4022
4023 if ( prevSelection.size() > 0 )
4024 prevFirstSel = prevSelection[0];
4025 else
4026 prevFirstSel = NULL;
4027
4028 if ( prevFirstSel && prevFirstSel->HasFlag(wxPG_PROP_BEING_DELETED) )
4029 prevFirstSel = NULL;
4030
4031 // Always send event, as this is indirect call
4032 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
4033
4034 /*
4035 if ( prevFirstSel )
4036 wxPrintf( "Selected %s\n", prevFirstSel->GetClassInfo()->GetClassName() );
4037 else
4038 wxPrintf( "None selected\n" );
4039
4040 if (p)
4041 wxPrintf( "P = %s\n", p->GetClassInfo()->GetClassName() );
4042 else
4043 wxPrintf( "P = NULL\n" );
4044 */
4045
4046 wxWindow* primaryCtrl = NULL;
4047
4048 // If we are frozen, then just set the values.
4049 if ( m_frozen )
4050 {
4051 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
4052 m_editorFocused = 0;
4053 m_pState->DoSetSelection(p);
4054
4055 // If frozen, always free controls. But don't worry, as Thaw will
4056 // recall SelectProperty to recreate them.
4057 FreeEditors();
4058
4059 // Prevent any further selection measures in this call
4060 p = NULL;
4061 }
4062 else
4063 {
4064 // Is it the same?
4065 if ( prevFirstSel == p &&
4066 prevSelection.size() <= 1 &&
4067 !(flags & wxPG_SEL_FORCE) )
4068 {
4069 // Only set focus if not deselecting
4070 if ( p )
4071 {
4072 if ( flags & wxPG_SEL_FOCUS )
4073 {
4074 if ( m_wndEditor )
4075 {
4076 m_wndEditor->SetFocus();
4077 m_editorFocused = 1;
4078 }
4079 }
4080 else
4081 {
4082 SetFocusOnCanvas();
4083 }
4084 }
4085
4086 return true;
4087 }
4088
4089 //
4090 // First, deactivate previous
4091 if ( prevFirstSel )
4092 {
4093 // Must double-check if this is an selected in case of forceswitch
4094 if ( p != prevFirstSel )
4095 {
4096 if ( !CommitChangesFromEditor(flags) )
4097 {
4098 // Validation has failed, so we can't exit the previous editor
4099 //::wxMessageBox(_("Please correct the value or press ESC to cancel the edit."),
4100 // _("Invalid Value"),wxOK|wxICON_ERROR);
4101 return false;
4102 }
4103 }
4104
4105 // This should be called after CommitChangesFromEditor(), so that
4106 // OnValidationFailure() still has information on property's
4107 // validation state.
4108 OnValidationFailureReset(prevFirstSel);
4109
4110 FreeEditors();
4111
4112 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
4113 EditorsValueWasNotModified();
4114 }
4115
4116 SetInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
4117
4118 m_pState->DoSetSelection(p);
4119
4120 // Redraw unselected
4121 for ( unsigned int i=0; i<prevSelection.size(); i++ )
4122 {
4123 DrawItem(prevSelection[i]);
4124 }
4125
4126 //
4127 // Then, activate the one given.
4128 if ( p )
4129 {
4130 int propY = p->GetY2(m_lineHeight);
4131
4132 int splitterX = GetSplitterPosition();
4133 m_editorFocused = 0;
4134 m_iFlags |= wxPG_FL_PRIMARY_FILLS_ENTIRE;
4135
4136 wxASSERT( m_wndEditor == NULL );
4137
4138 //
4139 // Only create editor for non-disabled non-caption
4140 if ( !p->IsCategory() && !(p->m_flags & wxPG_PROP_DISABLED) )
4141 {
4142 // do this for non-caption items
4143
4144 m_selColumn = 1;
4145
4146 // Do we need to paint the custom image, if any?
4147 m_iFlags &= ~(wxPG_FL_CUR_USES_CUSTOM_IMAGE);
4148 if ( (p->m_flags & wxPG_PROP_CUSTOMIMAGE) &&
4149 !p->GetEditorClass()->CanContainCustomImage()
4150 )
4151 m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
4152
4153 wxRect grect = GetEditorWidgetRect(p, m_selColumn);
4154 wxPoint goodPos = grect.GetPosition();
4155
4156 // Editor appearance can now be considered clear
4157 m_editorAppearance.SetEmptyData();
4158
4159 const wxPGEditor* editor = p->GetEditorClass();
4160 wxCHECK_MSG(editor, false,
4161 wxT("NULL editor class not allowed"));
4162
4163 m_iFlags &= ~wxPG_FL_FIXED_WIDTH_EDITOR;
4164
4165 wxPGWindowList wndList =
4166 editor->CreateControls(this,
4167 p,
4168 goodPos,
4169 grect.GetSize());
4170
4171 m_wndEditor = wndList.m_primary;
4172 m_wndEditor2 = wndList.m_secondary;
4173 primaryCtrl = GetEditorControl();
4174
4175 //
4176 // Essentially, primaryCtrl == m_wndEditor
4177 //
4178
4179 // NOTE: It is allowed for m_wndEditor to be NULL - in this
4180 // case value is drawn as normal, and m_wndEditor2 is
4181 // assumed to be a right-aligned button that triggers
4182 // a separate editorCtrl window.
4183
4184 if ( m_wndEditor )
4185 {
4186 wxASSERT_MSG( m_wndEditor->GetParent() == GetPanel(),
4187 "CreateControls must use result of "
4188 "wxPropertyGrid::GetPanel() as parent "
4189 "of controls." );
4190
4191 // Set validator, if any
4192 #if wxUSE_VALIDATORS
4193 wxValidator* validator = p->GetValidator();
4194 if ( validator )
4195 primaryCtrl->SetValidator(*validator);
4196 #endif
4197
4198 if ( m_wndEditor->GetSize().y > (m_lineHeight+6) )
4199 m_iFlags |= wxPG_FL_ABNORMAL_EDITOR;
4200
4201 // If it has modified status, use bold font
4202 // (must be done before capturing m_ctrlXAdjust)
4203 if ( (p->m_flags & wxPG_PROP_MODIFIED) &&
4204 (m_windowStyle & wxPG_BOLD_MODIFIED) )
4205 SetCurControlBoldFont();
4206
4207 // Store x relative to splitter (we'll need it).
4208 m_ctrlXAdjust = m_wndEditor->GetPosition().x - splitterX;
4209
4210 // Check if background clear is not necessary
4211 wxPoint pos = m_wndEditor->GetPosition();
4212 if ( pos.x > (splitterX+1) || pos.y > propY )
4213 {
4214 m_iFlags &= ~(wxPG_FL_PRIMARY_FILLS_ENTIRE);
4215 }
4216
4217 m_wndEditor->SetSizeHints(3, 3);
4218
4219 SetupChildEventHandling(primaryCtrl);
4220
4221 // Focus and select all (wxTextCtrl, wxComboBox etc)
4222 if ( flags & wxPG_SEL_FOCUS )
4223 {
4224 primaryCtrl->SetFocus();
4225
4226 p->GetEditorClass()->OnFocus(p, primaryCtrl);
4227 }
4228 else
4229 {
4230 if ( p->IsValueUnspecified() )
4231 SetEditorAppearance(m_unspecifiedAppearance,
4232 true);
4233 }
4234 }
4235
4236 if ( m_wndEditor2 )
4237 {
4238 wxASSERT_MSG( m_wndEditor2->GetParent() == GetPanel(),
4239 "CreateControls must use result of "
4240 "wxPropertyGrid::GetPanel() as parent "
4241 "of controls." );
4242
4243 // Get proper id for wndSecondary
4244 m_wndSecId = m_wndEditor2->GetId();
4245 wxWindowList children = m_wndEditor2->GetChildren();
4246 wxWindowList::iterator node = children.begin();
4247 if ( node != children.end() )
4248 m_wndSecId = ((wxWindow*)*node)->GetId();
4249
4250 m_wndEditor2->SetSizeHints(3,3);
4251
4252 m_wndEditor2->Show();
4253
4254 SetupChildEventHandling(m_wndEditor2);
4255
4256 // If no primary editor, focus to button to allow
4257 // it to interprete ENTER etc.
4258 // NOTE: Due to problems focusing away from it, this
4259 // has been disabled.
4260 /*
4261 if ( (flags & wxPG_SEL_FOCUS) && !m_wndEditor )
4262 m_wndEditor2->SetFocus();
4263 */
4264 }
4265
4266 if ( flags & wxPG_SEL_FOCUS )
4267 m_editorFocused = 1;
4268
4269 }
4270 else
4271 {
4272 // Make sure focus is in grid canvas (important for wxGTK,
4273 // at least)
4274 SetFocusOnCanvas();
4275 }
4276
4277 EditorsValueWasNotModified();
4278
4279 // If it's inside collapsed section, expand parent, scroll, etc.
4280 // Also, if it was partially visible, scroll it into view.
4281 if ( !(flags & wxPG_SEL_NONVISIBLE) )
4282 EnsureVisible( p );
4283
4284 if ( m_wndEditor )
4285 {
4286 m_wndEditor->Show(true);
4287 }
4288
4289 if ( !(flags & wxPG_SEL_NO_REFRESH) )
4290 DrawItem(p);
4291 }
4292 else
4293 {
4294 // Make sure focus is in grid canvas
4295 SetFocusOnCanvas();
4296 }
4297
4298 ClearInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
4299 }
4300
4301 const wxString* pHelpString = NULL;
4302
4303 if ( p )
4304 pHelpString = &p->GetHelpString();
4305
4306 if ( !(GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS) )
4307 {
4308 #if wxUSE_STATUSBAR
4309
4310 //
4311 // Show help text in status bar.
4312 // (if found and grid not embedded in manager with help box and
4313 // style wxPG_EX_HELP_AS_TOOLTIPS is not used).
4314 //
4315 wxStatusBar* statusbar = GetStatusBar();
4316 if ( statusbar )
4317 {
4318 if ( pHelpString && pHelpString->length() )
4319 {
4320 // Set help box text.
4321 statusbar->SetStatusText( *pHelpString );
4322 m_iFlags |= wxPG_FL_STRING_IN_STATUSBAR;
4323 }
4324 else if ( m_iFlags & wxPG_FL_STRING_IN_STATUSBAR )
4325 {
4326 // Clear help box - but only if it was written
4327 // by us at previous time.
4328 statusbar->SetStatusText( m_emptyString );
4329 m_iFlags &= ~(wxPG_FL_STRING_IN_STATUSBAR);
4330 }
4331 }
4332 #endif
4333 }
4334 else
4335 {
4336 #if wxPG_SUPPORT_TOOLTIPS
4337 //
4338 // Show help as a tool tip on the editor control.
4339 //
4340 if ( pHelpString && pHelpString->length() &&
4341 primaryCtrl )
4342 {
4343 primaryCtrl->SetToolTip(*pHelpString);
4344 }
4345 #endif
4346 }
4347
4348 // call wx event handler (here so that it also occurs on deselection)
4349 if ( !(flags & wxPG_SEL_DONT_SEND_EVENT) )
4350 SendEvent( wxEVT_PG_SELECTED, p, NULL );
4351
4352 return true;
4353 }
4354
4355 // -----------------------------------------------------------------------
4356
4357 bool wxPropertyGrid::UnfocusEditor()
4358 {
4359 wxPGProperty* selected = GetSelection();
4360
4361 if ( !selected || !m_wndEditor || m_frozen )
4362 return true;
4363
4364 if ( !CommitChangesFromEditor(0) )
4365 return false;
4366
4367 SetFocusOnCanvas();
4368 DrawItem(selected);
4369
4370 return true;
4371 }
4372
4373 // -----------------------------------------------------------------------
4374
4375 void wxPropertyGrid::RefreshEditor()
4376 {
4377 wxPGProperty* p = GetSelection();
4378 if ( !p )
4379 return;
4380
4381 wxWindow* wnd = GetEditorControl();
4382 if ( !wnd )
4383 return;
4384
4385 // Set editor font boldness - must do this before
4386 // calling UpdateControl().
4387 if ( HasFlag(wxPG_BOLD_MODIFIED) )
4388 {
4389 if ( p->HasFlag(wxPG_PROP_MODIFIED) )
4390 wnd->SetFont(GetCaptionFont());
4391 else
4392 wnd->SetFont(GetFont());
4393 }
4394
4395 const wxPGEditor* editorClass = p->GetEditorClass();
4396
4397 editorClass->UpdateControl(p, wnd);
4398
4399 if ( p->IsValueUnspecified() )
4400 SetEditorAppearance(m_unspecifiedAppearance, true);
4401 }
4402
4403 // -----------------------------------------------------------------------
4404
4405 bool wxPropertyGrid::SelectProperty( wxPGPropArg id, bool focus )
4406 {
4407 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
4408
4409 int flags = wxPG_SEL_DONT_SEND_EVENT;
4410 if ( focus )
4411 flags |= wxPG_SEL_FOCUS;
4412
4413 return DoSelectProperty(p, flags);
4414 }
4415
4416 // -----------------------------------------------------------------------
4417 // wxPropertyGrid expand/collapse state
4418 // -----------------------------------------------------------------------
4419
4420 bool wxPropertyGrid::DoCollapse( wxPGProperty* p, bool sendEvents )
4421 {
4422 wxPGProperty* pwc = wxStaticCast(p, wxPGProperty);
4423 wxPGProperty* selected = GetSelection();
4424
4425 // If active editor was inside collapsed section, then disable it
4426 if ( selected && selected->IsSomeParent(p) )
4427 {
4428 DoClearSelection();
4429 }
4430
4431 // Store dont-center-splitter flag 'cause we need to temporarily set it
4432 bool prevDontCenterSplitter = m_pState->m_dontCenterSplitter;
4433 m_pState->m_dontCenterSplitter = true;
4434
4435 bool res = m_pState->DoCollapse(pwc);
4436
4437 if ( res )
4438 {
4439 if ( sendEvents )
4440 SendEvent( wxEVT_PG_ITEM_COLLAPSED, p );
4441
4442 RecalculateVirtualSize();
4443 Refresh();
4444 }
4445
4446 m_pState->m_dontCenterSplitter = prevDontCenterSplitter;
4447
4448 return res;
4449 }
4450
4451 // -----------------------------------------------------------------------
4452
4453 bool wxPropertyGrid::DoExpand( wxPGProperty* p, bool sendEvents )
4454 {
4455 wxCHECK_MSG( p, false, wxT("invalid property id") );
4456
4457 wxPGProperty* pwc = (wxPGProperty*)p;
4458
4459 // Store dont-center-splitter flag 'cause we need to temporarily set it
4460 bool prevDontCenterSplitter = m_pState->m_dontCenterSplitter;
4461 m_pState->m_dontCenterSplitter = true;
4462
4463 bool res = m_pState->DoExpand(pwc);
4464
4465 if ( res )
4466 {
4467 if ( sendEvents )
4468 SendEvent( wxEVT_PG_ITEM_EXPANDED, p );
4469
4470 RecalculateVirtualSize();
4471 Refresh();
4472 }
4473
4474 m_pState->m_dontCenterSplitter = prevDontCenterSplitter;
4475
4476 return res;
4477 }
4478
4479 // -----------------------------------------------------------------------
4480
4481 bool wxPropertyGrid::DoHideProperty( wxPGProperty* p, bool hide, int flags )
4482 {
4483 if ( m_frozen )
4484 return m_pState->DoHideProperty(p, hide, flags);
4485
4486 wxArrayPGProperty selection = m_pState->m_selection; // Must use a copy
4487 int selRemoveCount = 0;
4488 for ( unsigned int i=0; i<selection.size(); i++ )
4489 {
4490 wxPGProperty* selected = selection[i];
4491 if ( selected == p || selected->IsSomeParent(p) )
4492 {
4493 if ( !DoRemoveFromSelection(p, flags) )
4494 return false;
4495 selRemoveCount += 1;
4496 }
4497 }
4498
4499 m_pState->DoHideProperty(p, hide, flags);
4500
4501 RecalculateVirtualSize();
4502 Refresh();
4503
4504 return true;
4505 }
4506
4507
4508 // -----------------------------------------------------------------------
4509 // wxPropertyGrid size related methods
4510 // -----------------------------------------------------------------------
4511
4512 void wxPropertyGrid::RecalculateVirtualSize( int forceXPos )
4513 {
4514 // Don't check for !HasInternalFlag(wxPG_FL_INITIALIZED) here. Otherwise
4515 // virtual size calculation may go wrong.
4516 if ( HasInternalFlag(wxPG_FL_RECALCULATING_VIRTUAL_SIZE) ||
4517 m_frozen ||
4518 !m_pState )
4519 return;
4520
4521 //
4522 // If virtual height was changed, then recalculate editor control position(s)
4523 if ( m_pState->m_vhCalcPending )
4524 CorrectEditorWidgetPosY();
4525
4526 m_pState->EnsureVirtualHeight();
4527
4528 wxASSERT_LEVEL_2_MSG(
4529 m_pState->GetVirtualHeight() == m_pState->GetActualVirtualHeight(),
4530 "VirtualHeight and ActualVirtualHeight should match"
4531 );
4532
4533 m_iFlags |= wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4534
4535 int x = m_pState->m_width;
4536 int y = m_pState->m_virtualHeight;
4537
4538 int width, height;
4539 GetClientSize(&width,&height);
4540
4541 // Now adjust virtual size.
4542 SetVirtualSize(x, y);
4543
4544 int xAmount = 0;
4545 int xPos = 0;
4546
4547 //
4548 // Adjust scrollbars
4549 if ( HasVirtualWidth() )
4550 {
4551 xAmount = x/wxPG_PIXELS_PER_UNIT;
4552 xPos = GetScrollPos( wxHORIZONTAL );
4553 }
4554
4555 if ( forceXPos != -1 )
4556 xPos = forceXPos;
4557 // xPos too high?
4558 else if ( xPos > (xAmount-(width/wxPG_PIXELS_PER_UNIT)) )
4559 xPos = 0;
4560
4561 int yAmount = y / wxPG_PIXELS_PER_UNIT;
4562 int yPos = GetScrollPos( wxVERTICAL );
4563
4564 SetScrollbars( wxPG_PIXELS_PER_UNIT, wxPG_PIXELS_PER_UNIT,
4565 xAmount, yAmount, xPos, yPos, true );
4566
4567 // Must re-get size now
4568 GetClientSize(&width,&height);
4569
4570 if ( !HasVirtualWidth() )
4571 {
4572 m_pState->SetVirtualWidth(width);
4573 x = width;
4574 }
4575
4576 m_width = width;
4577 m_height = height;
4578
4579 m_pState->CheckColumnWidths();
4580
4581 if ( GetSelection() )
4582 CorrectEditorWidgetSizeX();
4583
4584 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4585 }
4586
4587 // -----------------------------------------------------------------------
4588
4589 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4590 {
4591 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4592 return;
4593
4594 int width, height;
4595 GetClientSize(&width, &height);
4596
4597 m_width = width;
4598 m_height = height;
4599
4600 #if wxPG_DOUBLE_BUFFER
4601 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4602 {
4603 int dblh = (m_lineHeight*2);
4604 if ( !m_doubleBuffer )
4605 {
4606 // Create double buffer bitmap to draw on, if none
4607 int w = (width>250)?width:250;
4608 int h = height + dblh;
4609 h = (h>400)?h:400;
4610 m_doubleBuffer = new wxBitmap( w, h );
4611 }
4612 else
4613 {
4614 int w = m_doubleBuffer->GetWidth();
4615 int h = m_doubleBuffer->GetHeight();
4616
4617 // Double buffer must be large enough
4618 if ( w < width || h < (height+dblh) )
4619 {
4620 if ( w < width ) w = width;
4621 if ( h < (height+dblh) ) h = height + dblh;
4622 delete m_doubleBuffer;
4623 m_doubleBuffer = new wxBitmap( w, h );
4624 }
4625 }
4626 }
4627
4628 #endif
4629
4630 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4631 m_ncWidth = event.GetSize().x;
4632
4633 if ( !m_frozen )
4634 {
4635 if ( m_pState->m_itemsAdded )
4636 PrepareAfterItemsAdded();
4637 else
4638 // Without this, virtual size (atleast under wxGTK) will be skewed
4639 RecalculateVirtualSize();
4640
4641 Refresh();
4642 }
4643 }
4644
4645 // -----------------------------------------------------------------------
4646
4647 void wxPropertyGrid::SetVirtualWidth( int width )
4648 {
4649 if ( width == -1 )
4650 {
4651 // Disable virtual width
4652 width = GetClientSize().x;
4653 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4654 }
4655 else
4656 {
4657 // Enable virtual width
4658 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4659 }
4660 m_pState->SetVirtualWidth( width );
4661 }
4662
4663 void wxPropertyGrid::SetFocusOnCanvas()
4664 {
4665 SetFocus();
4666 m_editorFocused = 0;
4667 }
4668
4669 // -----------------------------------------------------------------------
4670 // wxPropertyGrid mouse event handling
4671 // -----------------------------------------------------------------------
4672
4673 // selFlags uses same values DoSelectProperty's flags
4674 // Returns true if event was vetoed.
4675 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p,
4676 wxVariant* pValue,
4677 unsigned int selFlags,
4678 unsigned int column )
4679 {
4680 // selFlags should have wxPG_SEL_NOVALIDATE if event is not
4681 // vetoable.
4682
4683 // Send property grid event of specific type and with specific property
4684 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4685 evt.SetPropertyGrid(this);
4686 evt.SetEventObject(m_eventObject);
4687 evt.SetProperty(p);
4688 evt.SetColumn(column);
4689 if ( eventType == wxEVT_PG_CHANGING )
4690 {
4691 wxASSERT( pValue );
4692 evt.SetCanVeto(true);
4693 m_validationInfo.m_pValue = pValue;
4694 evt.SetupValidationInfo();
4695 }
4696 else
4697 {
4698 if ( p )
4699 evt.SetPropertyValue(p->GetValue());
4700
4701 if ( !(selFlags & wxPG_SEL_NOVALIDATE) )
4702 evt.SetCanVeto(true);
4703 }
4704
4705 wxPropertyGridEvent* prevProcessedEvent = m_processedEvent;
4706 m_processedEvent = &evt;
4707 m_eventObject->HandleWindowEvent(evt);
4708 m_processedEvent = prevProcessedEvent;
4709
4710 return evt.WasVetoed();
4711 }
4712
4713 // -----------------------------------------------------------------------
4714
4715 // Return false if should be skipped
4716 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4717 {
4718 bool res = true;
4719
4720 // Need to set focus?
4721 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4722 {
4723 SetFocusOnCanvas();
4724 }
4725
4726 wxPropertyGridPageState* state = m_pState;
4727 int splitterHit;
4728 int splitterHitOffset;
4729 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4730
4731 wxPGProperty* p = DoGetItemAtY(y);
4732
4733 if ( p )
4734 {
4735 int depth = (int)p->GetDepth() - 1;
4736
4737 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4738
4739 if ( x >= marginEnds )
4740 {
4741 // Outside margin.
4742
4743 if ( p->IsCategory() )
4744 {
4745 // This is category.
4746 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4747
4748 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4749
4750 // Expand, collapse, activate etc. if click on text or left of splitter.
4751 if ( x >= textX
4752 &&
4753 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4754 columnHit == 0
4755 )
4756 )
4757 {
4758 if ( !AddToSelectionFromInputEvent( p,
4759 columnHit,
4760 &event ) )
4761 return res;
4762
4763 // On double-click, expand/collapse.
4764 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4765 {
4766 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4767 else DoExpand( p, true );
4768 }
4769 }
4770 }
4771 else if ( splitterHit == -1 )
4772 {
4773 // Click on value.
4774 unsigned int selFlag = 0;
4775 if ( columnHit == 1 )
4776 {
4777 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4778 selFlag = wxPG_SEL_FOCUS;
4779 }
4780 if ( !AddToSelectionFromInputEvent( p,
4781 columnHit,
4782 &event,
4783 selFlag ) )
4784 return res;
4785
4786 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4787
4788 if ( p->GetChildCount() && !p->IsCategory() )
4789 // On double-click, expand/collapse.
4790 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4791 {
4792 wxPGProperty* pwc = (wxPGProperty*)p;
4793 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4794 else DoExpand( p, true );
4795 }
4796
4797 // Do not Skip() the event after selection has been made.
4798 // Otherwise default event handling behavior kicks in
4799 // and may revert focus back to the main canvas.
4800 res = true;
4801 }
4802 else
4803 {
4804 // click on splitter
4805 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4806 {
4807 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4808 {
4809 // Double-clicking the splitter causes auto-centering
4810 if ( m_pState->GetColumnCount() <= 2 )
4811 {
4812 ResetColumnSizes( true );
4813
4814 SendEvent(wxEVT_PG_COL_DRAGGING,
4815 m_propHover,
4816 NULL,
4817 wxPG_SEL_NOVALIDATE,
4818 (unsigned int)m_draggedSplitter);
4819 }
4820 }
4821 else if ( m_dragStatus == 0 )
4822 {
4823 //
4824 // Begin draggin the splitter
4825 //
4826
4827 // send event
4828 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
4829
4830 // Allow application to veto dragging
4831 if ( !SendEvent(wxEVT_PG_COL_BEGIN_DRAG,
4832 p, NULL, 0,
4833 (unsigned int)splitterHit) )
4834 {
4835 if ( m_wndEditor )
4836 {
4837 // Changes must be committed here or the
4838 // value won't be drawn correctly
4839 if ( !CommitChangesFromEditor() )
4840 return res;
4841
4842 m_wndEditor->Show ( false );
4843 }
4844
4845 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4846 {
4847 CaptureMouse();
4848 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4849 }
4850
4851 m_dragStatus = 1;
4852 m_draggedSplitter = splitterHit;
4853 m_dragOffset = splitterHitOffset;
4854
4855 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4856 // Fixes button disappearance bug
4857 if ( m_wndEditor2 )
4858 m_wndEditor2->Show ( false );
4859 #endif
4860
4861 m_startingSplitterX = x - splitterHitOffset;
4862 }
4863 }
4864 }
4865 }
4866 }
4867 else
4868 {
4869 // Click on margin.
4870 if ( p->GetChildCount() )
4871 {
4872 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4873
4874 // Fine tune cell button x
4875 if ( !p->IsCategory() )
4876 nx -= IN_CELL_EXPANDER_BUTTON_X_ADJUST;
4877
4878 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4879 {
4880 int y2 = y % m_lineHeight;
4881 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4882 {
4883 // On click on expander button, expand/collapse
4884 if ( ((wxPGProperty*)p)->IsExpanded() )
4885 DoCollapse( p, true );
4886 else
4887 DoExpand( p, true );
4888 }
4889 }
4890 }
4891 }
4892 }
4893 return res;
4894 }
4895
4896 // -----------------------------------------------------------------------
4897
4898 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x),
4899 unsigned int WXUNUSED(y),
4900 wxMouseEvent& event )
4901 {
4902 if ( m_propHover )
4903 {
4904 // Select property here as well
4905 wxPGProperty* p = m_propHover;
4906 AddToSelectionFromInputEvent(p, m_colHover, &event);
4907
4908 // Send right click event.
4909 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4910
4911 return true;
4912 }
4913 return false;
4914 }
4915
4916 // -----------------------------------------------------------------------
4917
4918 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x),
4919 unsigned int WXUNUSED(y),
4920 wxMouseEvent& event )
4921 {
4922 if ( m_propHover )
4923 {
4924 // Select property here as well
4925 wxPGProperty* p = m_propHover;
4926
4927 AddToSelectionFromInputEvent(p, m_colHover, &event);
4928
4929 // Send double-click event.
4930 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4931
4932 return true;
4933 }
4934 return false;
4935 }
4936
4937 // -----------------------------------------------------------------------
4938
4939 // Return false if should be skipped
4940 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y,
4941 wxMouseEvent &event )
4942 {
4943 // Safety check (needed because mouse capturing may
4944 // otherwise freeze the control)
4945 if ( m_dragStatus > 0 && !event.Dragging() )
4946 {
4947 HandleMouseUp(x, y, event);
4948 }
4949
4950 wxPropertyGridPageState* state = m_pState;
4951 int splitterHit;
4952 int splitterHitOffset;
4953 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4954 int splitterX = x - splitterHitOffset;
4955
4956 m_colHover = columnHit;
4957
4958 if ( m_dragStatus > 0 )
4959 {
4960 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4961 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4962 {
4963
4964 int newSplitterX = x - m_dragOffset;
4965
4966 // Splitter redraw required?
4967 if ( newSplitterX != splitterX )
4968 {
4969 // Move everything
4970 DoSetSplitterPosition(newSplitterX,
4971 m_draggedSplitter,
4972 wxPG_SPLITTER_REFRESH |
4973 wxPG_SPLITTER_FROM_EVENT);
4974
4975 SendEvent(wxEVT_PG_COL_DRAGGING,
4976 m_propHover,
4977 NULL,
4978 wxPG_SEL_NOVALIDATE,
4979 (unsigned int)m_draggedSplitter);
4980 }
4981
4982 m_dragStatus = 2;
4983 }
4984
4985 return false;
4986 }
4987 else
4988 {
4989
4990 int ih = m_lineHeight;
4991 int sy = y;
4992
4993 #if wxPG_SUPPORT_TOOLTIPS
4994 wxPGProperty* prevHover = m_propHover;
4995 unsigned char prevSide = m_mouseSide;
4996 #endif
4997 int curPropHoverY = y - (y % ih);
4998
4999 // On which item it hovers
5000 if ( !m_propHover
5001 ||
5002 ( sy < m_propHoverY || sy >= (m_propHoverY+ih) )
5003 )
5004 {
5005 // Mouse moves on another property
5006
5007 m_propHover = DoGetItemAtY(y);
5008 m_propHoverY = curPropHoverY;
5009
5010 // Send hover event
5011 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
5012 }
5013
5014 #if wxPG_SUPPORT_TOOLTIPS
5015 // Store which side we are on
5016 m_mouseSide = 0;
5017 if ( columnHit == 1 )
5018 m_mouseSide = 2;
5019 else if ( columnHit == 0 )
5020 m_mouseSide = 1;
5021
5022 //
5023 // If tooltips are enabled, show label or value as a tip
5024 // in case it doesn't otherwise show in full length.
5025 //
5026 if ( m_windowStyle & wxPG_TOOLTIPS )
5027 {
5028 if ( m_propHover != prevHover || prevSide != m_mouseSide )
5029 {
5030 if ( m_propHover && !m_propHover->IsCategory() )
5031 {
5032
5033 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
5034 {
5035 // Show help string as a tooltip
5036 wxString tipString = m_propHover->GetHelpString();
5037
5038 SetToolTip(tipString);
5039 }
5040 else
5041 {
5042 // Show cropped value string as a tooltip
5043 wxString tipString;
5044 int space = 0;
5045
5046 if ( m_mouseSide == 1 )
5047 {
5048 tipString = m_propHover->m_label;
5049 space = splitterX-m_marginWidth-3;
5050 }
5051 else if ( m_mouseSide == 2 )
5052 {
5053 tipString = m_propHover->GetDisplayedString();
5054
5055 space = m_width - splitterX;
5056 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
5057 space -= wxPG_CUSTOM_IMAGE_WIDTH +
5058 wxCC_CUSTOM_IMAGE_MARGIN1 +
5059 wxCC_CUSTOM_IMAGE_MARGIN2;
5060 }
5061
5062 if ( space )
5063 {
5064 int tw, th;
5065 GetTextExtent( tipString, &tw, &th, 0, 0 );
5066 if ( tw > space )
5067 SetToolTip( tipString );
5068 }
5069 else
5070 {
5071 SetToolTip( m_emptyString );
5072 }
5073
5074 }
5075 }
5076 else
5077 {
5078 SetToolTip( m_emptyString );
5079 }
5080 }
5081 }
5082 #endif
5083
5084 if ( splitterHit == -1 ||
5085 !m_propHover ||
5086 HasFlag(wxPG_STATIC_SPLITTER) )
5087 {
5088 // hovering on something else
5089 if ( m_curcursor != wxCURSOR_ARROW )
5090 CustomSetCursor( wxCURSOR_ARROW );
5091 }
5092 else
5093 {
5094 // Do not allow splitter cursor on caption items.
5095 // (also not if we were dragging and its started
5096 // outside the splitter region)
5097
5098 if ( !m_propHover->IsCategory() &&
5099 !event.Dragging() )
5100 {
5101
5102 // hovering on splitter
5103
5104 // NB: Condition disabled since MouseLeave event (from the
5105 // editor control) cannot be reliably detected.
5106 //if ( m_curcursor != wxCURSOR_SIZEWE )
5107 CustomSetCursor( wxCURSOR_SIZEWE, true );
5108
5109 return false;
5110 }
5111 else
5112 {
5113 // hovering on something else
5114 if ( m_curcursor != wxCURSOR_ARROW )
5115 CustomSetCursor( wxCURSOR_ARROW );
5116 }
5117 }
5118
5119 //
5120 // Multi select by dragging
5121 //
5122 if ( (GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION) &&
5123 event.LeftIsDown() &&
5124 m_propHover &&
5125 GetSelection() &&
5126 columnHit != 1 &&
5127 !state->DoIsPropertySelected(m_propHover) )
5128 {
5129 // Additional requirement is that the hovered property
5130 // is adjacent to edges of selection.
5131 const wxArrayPGProperty& selection = GetSelectedProperties();
5132
5133 // Since categories cannot be selected along with 'other'
5134 // properties, exclude them from iterator flags.
5135 int iterFlags = wxPG_ITERATE_VISIBLE & (~wxPG_PROP_CATEGORY);
5136
5137 for ( int i=(selection.size()-1); i>=0; i-- )
5138 {
5139 // TODO: This could be optimized by keeping track of
5140 // which properties are at the edges of selection.
5141 wxPGProperty* selProp = selection[i];
5142 if ( state->ArePropertiesAdjacent(m_propHover, selProp,
5143 iterFlags) )
5144 {
5145 DoAddToSelection(m_propHover);
5146 break;
5147 }
5148 }
5149 }
5150 }
5151 return true;
5152 }
5153
5154 // -----------------------------------------------------------------------
5155
5156 // Also handles Leaving event
5157 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
5158 wxMouseEvent &WXUNUSED(event) )
5159 {
5160 wxPropertyGridPageState* state = m_pState;
5161 bool res = false;
5162
5163 int splitterHit;
5164 int splitterHitOffset;
5165 state->HitTestH( x, &splitterHit, &splitterHitOffset );
5166
5167 // No event type check - basicly calling this method should
5168 // just stop dragging.
5169 // Left up after dragged?
5170 if ( m_dragStatus >= 1 )
5171 {
5172 //
5173 // End Splitter Dragging
5174 //
5175 // DO NOT ENABLE FOLLOWING LINE!
5176 // (it is only here as a reminder to not to do it)
5177 //splitterX = x;
5178
5179 SendEvent(wxEVT_PG_COL_END_DRAG,
5180 m_propHover,
5181 NULL,
5182 wxPG_SEL_NOVALIDATE,
5183 (unsigned int)m_draggedSplitter);
5184
5185 // Disable splitter auto-centering (but only if moved any -
5186 // otherwise we end up disabling auto-center even after a
5187 // recentering double-click).
5188 int posDiff = abs(m_startingSplitterX -
5189 GetSplitterPosition(m_draggedSplitter));
5190
5191 if ( posDiff > 1 )
5192 state->m_dontCenterSplitter = true;
5193
5194 // This is necessary to return cursor
5195 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5196 {
5197 ReleaseMouse();
5198 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5199 }
5200
5201 // Set back the default cursor, if necessary
5202 if ( splitterHit == -1 ||
5203 !m_propHover )
5204 {
5205 CustomSetCursor( wxCURSOR_ARROW );
5206 }
5207
5208 m_dragStatus = 0;
5209
5210 // Control background needs to be cleared
5211 wxPGProperty* selected = GetSelection();
5212 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && selected )
5213 DrawItem( selected );
5214
5215 if ( m_wndEditor )
5216 {
5217 m_wndEditor->Show ( true );
5218 }
5219
5220 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
5221 // Fixes button disappearance bug
5222 if ( m_wndEditor2 )
5223 m_wndEditor2->Show ( true );
5224 #endif
5225
5226 // This clears the focus.
5227 m_editorFocused = 0;
5228
5229 }
5230 return res;
5231 }
5232
5233 // -----------------------------------------------------------------------
5234
5235 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
5236 {
5237 int splitterX = GetSplitterPosition();
5238
5239 int ux, uy;
5240 CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
5241
5242 wxWindow* wnd = GetEditorControl();
5243
5244 // Hide popup on clicks
5245 if ( event.GetEventType() != wxEVT_MOTION )
5246 if ( wnd && wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
5247 {
5248 ((wxOwnerDrawnComboBox*)wnd)->HidePopup();
5249 }
5250
5251 wxRect r;
5252 if ( wnd )
5253 r = wnd->GetRect();
5254 if ( wnd == NULL || m_dragStatus ||
5255 (
5256 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
5257 ux >= (r.x+r.width) ||
5258 event.m_y < r.y ||
5259 event.m_y >= (r.y+r.height)
5260 )
5261 )
5262 {
5263 *px = ux;
5264 *py = uy;
5265 return true;
5266 }
5267 else
5268 {
5269 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5270 }
5271 return false;
5272 }
5273
5274 // -----------------------------------------------------------------------
5275
5276 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
5277 {
5278 int x, y;
5279 if ( OnMouseCommon( event, &x, &y ) )
5280 {
5281 if ( !HandleMouseClick(x, y, event) )
5282 event.Skip();
5283 }
5284 else
5285 {
5286 event.Skip();
5287 }
5288 }
5289
5290 // -----------------------------------------------------------------------
5291
5292 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
5293 {
5294 int x, y;
5295 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5296 HandleMouseRightClick(x,y,event);
5297 event.Skip();
5298 }
5299
5300 // -----------------------------------------------------------------------
5301
5302 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
5303 {
5304 // Always run standard mouse-down handler as well
5305 OnMouseClick(event);
5306
5307 int x, y;
5308 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5309 HandleMouseDoubleClick(x,y,event);
5310
5311 // Do not Skip() event here - OnMouseClick() call above
5312 // should have already taken care of it.
5313 }
5314
5315 // -----------------------------------------------------------------------
5316
5317 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
5318 {
5319 int x, y;
5320 if ( OnMouseCommon( event, &x, &y ) )
5321 {
5322 HandleMouseMove(x,y,event);
5323 }
5324 event.Skip();
5325 }
5326
5327 // -----------------------------------------------------------------------
5328
5329 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
5330 {
5331 int x, y;
5332 if ( OnMouseCommon( event, &x, &y ) )
5333 {
5334 if ( !HandleMouseUp(x, y, event) )
5335 event.Skip();
5336 }
5337 else
5338 {
5339 event.Skip();
5340 }
5341 }
5342
5343 // -----------------------------------------------------------------------
5344
5345 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
5346 {
5347 // This may get called from child control as well, so event's
5348 // mouse position cannot be relied on.
5349
5350 if ( event.Entering() )
5351 {
5352 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5353 {
5354 // TODO: Fix this (detect parent and only do
5355 // cursor trick if it is a manager).
5356 wxASSERT( GetParent() );
5357 GetParent()->SetCursor(wxNullCursor);
5358
5359 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
5360 }
5361 else
5362 GetParent()->SetCursor(wxNullCursor);
5363 }
5364 else if ( event.Leaving() )
5365 {
5366 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
5367 SetCursor( wxNullCursor );
5368
5369 // Get real cursor position
5370 wxPoint pt = ScreenToClient(::wxGetMousePosition());
5371
5372 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
5373 {
5374 {
5375 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5376 {
5377 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
5378 }
5379
5380 if ( m_dragStatus )
5381 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
5382 }
5383 }
5384 }
5385
5386 event.Skip();
5387 }
5388
5389 // -----------------------------------------------------------------------
5390
5391 // Common code used by various OnMouseXXXChild methods.
5392 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
5393 {
5394 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
5395 wxASSERT( topCtrlWnd );
5396 int x, y;
5397 event.GetPosition(&x,&y);
5398
5399 int splitterX = GetSplitterPosition();
5400
5401 wxRect r = topCtrlWnd->GetRect();
5402 if ( !m_dragStatus &&
5403 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
5404 y >= 0 && y < r.height \
5405 )
5406 {
5407 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5408 event.Skip();
5409 }
5410 else
5411 {
5412 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
5413 px, py );
5414 return true;
5415 }
5416 return false;
5417 }
5418
5419 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
5420 {
5421 int x,y;
5422 if ( OnMouseChildCommon(event,&x,&y) )
5423 {
5424 bool res = HandleMouseClick(x,y,event);
5425 if ( !res ) event.Skip();
5426 }
5427 }
5428
5429 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
5430 {
5431 int x,y;
5432 wxASSERT( m_wndEditor );
5433 // These coords may not be exact (about +-2),
5434 // but that should not matter (right click is about item, not position).
5435 wxPoint pt = m_wndEditor->GetPosition();
5436 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
5437
5438 // FIXME: Used to set m_propHover to selection here. Was it really
5439 // necessary?
5440
5441 bool res = HandleMouseRightClick(x,y,event);
5442 if ( !res ) event.Skip();
5443 }
5444
5445 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
5446 {
5447 int x,y;
5448 if ( OnMouseChildCommon(event,&x,&y) )
5449 {
5450 bool res = HandleMouseMove(x,y,event);
5451 if ( !res ) event.Skip();
5452 }
5453 }
5454
5455 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
5456 {
5457 int x,y;
5458 if ( OnMouseChildCommon(event,&x,&y) )
5459 {
5460 bool res = HandleMouseUp(x,y,event);
5461 if ( !res ) event.Skip();
5462 }
5463 }
5464
5465 // -----------------------------------------------------------------------
5466 // wxPropertyGrid keyboard event handling
5467 // -----------------------------------------------------------------------
5468
5469 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
5470 {
5471 // Translates wxKeyEvent to wxPG_ACTION_XXX
5472
5473 int keycode = event.GetKeyCode();
5474 int modifiers = event.GetModifiers();
5475
5476 wxASSERT( !(modifiers&~(0xFFFF)) );
5477
5478 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5479
5480 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
5481
5482 if ( it == m_actionTriggers.end() )
5483 return 0;
5484
5485 if ( pSecond )
5486 {
5487 int second = (it->second>>16) & 0xFFFF;
5488 *pSecond = second;
5489 }
5490
5491 return (it->second & 0xFFFF);
5492 }
5493
5494 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
5495 {
5496 wxASSERT( !(modifiers&~(0xFFFF)) );
5497
5498 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5499
5500 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
5501
5502 if ( it != m_actionTriggers.end() )
5503 {
5504 // This key combination is already used
5505
5506 // Can add secondary?
5507 wxASSERT_MSG( !(it->second&~(0xFFFF)),
5508 wxT("You can only add up to two separate actions per key combination.") );
5509
5510 action = it->second | (action<<16);
5511 }
5512
5513 m_actionTriggers[hashMapKey] = action;
5514 }
5515
5516 void wxPropertyGrid::ClearActionTriggers( int action )
5517 {
5518 wxPGHashMapI2I::iterator it;
5519 bool didSomething;
5520
5521 do
5522 {
5523 didSomething = false;
5524
5525 for ( it = m_actionTriggers.begin();
5526 it != m_actionTriggers.end();
5527 it++ )
5528 {
5529 if ( it->second == action )
5530 {
5531 m_actionTriggers.erase(it);
5532 didSomething = true;
5533 break;
5534 }
5535 }
5536 }
5537 while ( didSomething );
5538 }
5539
5540 void wxPropertyGrid::HandleKeyEvent( wxKeyEvent &event, bool fromChild )
5541 {
5542 //
5543 // Handles key event when editor control is not focused.
5544 //
5545
5546 wxCHECK2(!m_frozen, return);
5547
5548 // Travelsal between items, collapsing/expanding, etc.
5549 wxPGProperty* selected = GetSelection();
5550 int keycode = event.GetKeyCode();
5551 bool editorFocused = IsEditorFocused();
5552
5553 if ( keycode == WXK_TAB )
5554 {
5555 wxWindow* mainControl;
5556
5557 if ( HasInternalFlag(wxPG_FL_IN_MANAGER) )
5558 mainControl = GetParent();
5559 else
5560 mainControl = this;
5561
5562 if ( !event.ShiftDown() )
5563 {
5564 if ( !editorFocused && m_wndEditor )
5565 {
5566 DoSelectProperty( selected, wxPG_SEL_FOCUS );
5567 }
5568 else
5569 {
5570 // Tab traversal workaround for platforms on which
5571 // wxWindow::Navigate() may navigate into first child
5572 // instead of next sibling. Does not work perfectly
5573 // in every scenario (for instance, when property grid
5574 // is either first or last control).
5575 #if defined(__WXGTK__)
5576 wxWindow* sibling = mainControl->GetNextSibling();
5577 if ( sibling )
5578 sibling->SetFocusFromKbd();
5579 #else
5580 Navigate(wxNavigationKeyEvent::IsForward);
5581 #endif
5582 }
5583 }
5584 else
5585 {
5586 if ( editorFocused )
5587 {
5588 UnfocusEditor();
5589 }
5590 else
5591 {
5592 #if defined(__WXGTK__)
5593 wxWindow* sibling = mainControl->GetPrevSibling();
5594 if ( sibling )
5595 sibling->SetFocusFromKbd();
5596 #else
5597 Navigate(wxNavigationKeyEvent::IsBackward);
5598 #endif
5599 }
5600 }
5601
5602 return;
5603 }
5604
5605 // Ignore Alt and Control when they are down alone
5606 if ( keycode == WXK_ALT ||
5607 keycode == WXK_CONTROL )
5608 {
5609 event.Skip();
5610 return;
5611 }
5612
5613 int secondAction;
5614 int action = KeyEventToActions(event, &secondAction);
5615
5616 if ( editorFocused && action == wxPG_ACTION_CANCEL_EDIT )
5617 {
5618 //
5619 // Esc cancels any changes
5620 if ( IsEditorsValueModified() )
5621 {
5622 EditorsValueWasNotModified();
5623
5624 // Update the control as well
5625 selected->GetEditorClass()->
5626 SetControlStringValue( selected,
5627 GetEditorControl(),
5628 selected->GetDisplayedString() );
5629 }
5630
5631 OnValidationFailureReset(selected);
5632
5633 UnfocusEditor();
5634 return;
5635 }
5636
5637 // Except for TAB, ESC, and any keys specifically dedicated to
5638 // wxPropertyGrid itself, handle child control events in child control.
5639 if ( fromChild &&
5640 wxPGFindInVector(m_dedicatedKeys, keycode) == wxNOT_FOUND )
5641 {
5642 // Only propagate event if it had modifiers
5643 if ( !event.HasModifiers() )
5644 {
5645 event.StopPropagation();
5646 }
5647 event.Skip();
5648 return;
5649 }
5650
5651 bool wasHandled = false;
5652
5653 if ( selected )
5654 {
5655 // Show dialog?
5656 if ( ButtonTriggerKeyTest(action, event) )
5657 return;
5658
5659 wxPGProperty* p = selected;
5660
5661 // Travel and expand/collapse
5662 int selectDir = -2;
5663
5664 if ( p->GetChildCount() )
5665 {
5666 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5667 {
5668 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5669 wasHandled = true;
5670 }
5671 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5672 {
5673 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5674 wasHandled = true;
5675 }
5676 }
5677
5678 if ( !wasHandled )
5679 {
5680 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5681 {
5682 selectDir = -1;
5683 }
5684 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5685 {
5686 selectDir = 1;
5687 }
5688 }
5689
5690 if ( selectDir >= -1 )
5691 {
5692 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5693 if ( p )
5694 {
5695 int selFlags = 0;
5696 int reopenLabelEditorCol = -1;
5697
5698 if ( editorFocused )
5699 {
5700 // If editor was focused, then make the next editor
5701 // focused as well
5702 selFlags |= wxPG_SEL_FOCUS;
5703 }
5704 else
5705 {
5706 // Also maintain the same label editor focus state
5707 if ( m_labelEditor )
5708 reopenLabelEditorCol = m_selColumn;
5709 }
5710
5711 DoSelectProperty(p, selFlags);
5712
5713 if ( reopenLabelEditorCol >= 0 )
5714 DoBeginLabelEdit(reopenLabelEditorCol);
5715 }
5716 wasHandled = true;
5717 }
5718 }
5719 else
5720 {
5721 // If nothing was selected, select the first item now
5722 // (or navigate out of tab).
5723 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5724 {
5725 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5726 if ( p ) DoSelectProperty(p);
5727 wasHandled = true;
5728 }
5729 }
5730
5731 if ( !wasHandled )
5732 event.Skip();
5733 }
5734
5735 // -----------------------------------------------------------------------
5736
5737 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5738 {
5739 // If there was editor open and focused, then this event should not
5740 // really be processed here.
5741 if ( IsEditorFocused() )
5742 {
5743 // However, if event had modifiers, it is probably still best
5744 // to skip it.
5745 if ( event.HasModifiers() )
5746 event.Skip();
5747 else
5748 event.StopPropagation();
5749 return;
5750 }
5751
5752 HandleKeyEvent(event, false);
5753 }
5754
5755 // -----------------------------------------------------------------------
5756
5757 bool wxPropertyGrid::ButtonTriggerKeyTest( int action, wxKeyEvent& event )
5758 {
5759 if ( action == -1 )
5760 {
5761 int secondAction;
5762 action = KeyEventToActions(event, &secondAction);
5763 }
5764
5765 // Does the keycode trigger button?
5766 if ( action == wxPG_ACTION_PRESS_BUTTON &&
5767 m_wndEditor2 )
5768 {
5769 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, m_wndEditor2->GetId());
5770 GetEventHandler()->AddPendingEvent(evt);
5771 return true;
5772 }
5773
5774 return false;
5775 }
5776
5777 // -----------------------------------------------------------------------
5778
5779 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5780 {
5781 HandleKeyEvent(event, true);
5782 }
5783
5784 // -----------------------------------------------------------------------
5785 // wxPropertyGrid miscellaneous event handling
5786 // -----------------------------------------------------------------------
5787
5788 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5789 {
5790 //
5791 // Check if the focus is in this control or one of its children
5792 wxWindow* newFocused = wxWindow::FindFocus();
5793
5794 if ( newFocused != m_curFocused )
5795 HandleFocusChange( newFocused );
5796
5797 //
5798 // Check if top-level parent has changed
5799 if ( GetExtraStyle() & wxPG_EX_ENABLE_TLP_TRACKING )
5800 {
5801 wxWindow* tlp = ::wxGetTopLevelParent(this);
5802 if ( tlp != m_tlp )
5803 OnTLPChanging(tlp);
5804 }
5805
5806 //
5807 // Resolve pending property removals
5808 if ( m_deletedProperties.size() > 0 )
5809 {
5810 wxArrayPGProperty& arr = m_deletedProperties;
5811 for ( unsigned int i=0; i<arr.size(); i++ )
5812 {
5813 DeleteProperty(arr[i]);
5814 }
5815 arr.clear();
5816 }
5817 if ( m_removedProperties.size() > 0 )
5818 {
5819 wxArrayPGProperty& arr = m_removedProperties;
5820 for ( unsigned int i=0; i<arr.size(); i++ )
5821 {
5822 RemoveProperty(arr[i]);
5823 }
5824 arr.clear();
5825 }
5826 }
5827
5828 bool wxPropertyGrid::IsEditorFocused() const
5829 {
5830 wxWindow* focus = wxWindow::FindFocus();
5831
5832 if ( focus == m_wndEditor || focus == m_wndEditor2 ||
5833 focus == GetEditorControl() )
5834 return true;
5835
5836 return false;
5837 }
5838
5839 // Called by focus event handlers. newFocused is the window that becomes focused.
5840 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5841 {
5842 //
5843 // Never allow focus to be changed when handling editor event.
5844 // Especially because they may be displaing a dialog which
5845 // could cause all kinds of weird (native) focus changes.
5846 if ( HasInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT) )
5847 return;
5848
5849 unsigned int oldFlags = m_iFlags;
5850 bool wasEditorFocused = false;
5851 wxWindow* wndEditor = m_wndEditor;
5852
5853 m_iFlags &= ~(wxPG_FL_FOCUSED);
5854
5855 wxWindow* parent = newFocused;
5856
5857 // This must be one of nextFocus' parents.
5858 while ( parent )
5859 {
5860 if ( parent == wndEditor )
5861 {
5862 wasEditorFocused = true;
5863 }
5864 // Use m_eventObject, which is either wxPropertyGrid or
5865 // wxPropertyGridManager, as appropriate.
5866 else if ( parent == m_eventObject )
5867 {
5868 m_iFlags |= wxPG_FL_FOCUSED;
5869 break;
5870 }
5871 parent = parent->GetParent();
5872 }
5873
5874 // Notify editor control when it receives a focus
5875 if ( wasEditorFocused && m_curFocused != newFocused )
5876 {
5877 wxPGProperty* p = GetSelection();
5878 if ( p )
5879 {
5880 const wxPGEditor* editor = p->GetEditorClass();
5881 ResetEditorAppearance();
5882 editor->OnFocus(p, GetEditorControl());
5883 }
5884 }
5885
5886 m_curFocused = newFocused;
5887
5888 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5889 (oldFlags & wxPG_FL_FOCUSED) )
5890 {
5891 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5892 {
5893 // Need to store changed value
5894 CommitChangesFromEditor();
5895 }
5896 else
5897 {
5898 /*
5899 //
5900 // Preliminary code for tab-order respecting
5901 // tab-traversal (but should be moved to
5902 // OnNav handler)
5903 //
5904 wxWindow* prevFocus = event.GetWindow();
5905 wxWindow* useThis = this;
5906 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5907 useThis = GetParent();
5908
5909 if ( prevFocus &&
5910 prevFocus->GetParent() == useThis->GetParent() )
5911 {
5912 wxList& children = useThis->GetParent()->GetChildren();
5913
5914 wxNode* node = children.Find(prevFocus);
5915
5916 if ( node->GetNext() &&
5917 useThis == node->GetNext()->GetData() )
5918 DoSelectProperty(GetFirst());
5919 else if ( node->GetPrevious () &&
5920 useThis == node->GetPrevious()->GetData() )
5921 DoSelectProperty(GetLastProperty());
5922
5923 }
5924 */
5925 }
5926
5927 // Redraw selected
5928 wxPGProperty* selected = GetSelection();
5929 if ( selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5930 DrawItem( selected );
5931 }
5932 }
5933
5934 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5935 {
5936 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5937 HandleFocusChange((wxWindow*)event.GetEventObject());
5938 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5939 //else if ( event.GetWindow() )
5940 else
5941 HandleFocusChange(event.GetWindow());
5942
5943 event.Skip();
5944 }
5945
5946 // -----------------------------------------------------------------------
5947
5948 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5949 {
5950 HandleFocusChange((wxWindow*)event.GetEventObject());
5951 event.Skip();
5952 }
5953
5954 // -----------------------------------------------------------------------
5955
5956 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5957 {
5958 m_iFlags |= wxPG_FL_SCROLLED;
5959
5960 event.Skip();
5961 }
5962
5963 // -----------------------------------------------------------------------
5964
5965 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5966 {
5967 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5968 {
5969 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5970 }
5971 }
5972
5973 // -----------------------------------------------------------------------
5974 // Property editor related functions
5975 // -----------------------------------------------------------------------
5976
5977 // noDefCheck = true prevents infinite recursion.
5978 wxPGEditor* wxPropertyGrid::DoRegisterEditorClass( wxPGEditor* editorClass,
5979 const wxString& editorName,
5980 bool noDefCheck )
5981 {
5982 wxASSERT( editorClass );
5983
5984 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5985 RegisterDefaultEditors();
5986
5987 wxString name = editorName;
5988 if ( name.length() == 0 )
5989 name = editorClass->GetName();
5990
5991 // Existing editor under this name?
5992 wxPGHashMapS2P::iterator vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5993
5994 if ( vt_it != wxPGGlobalVars->m_mapEditorClasses.end() )
5995 {
5996 // If this name was already used, try class name.
5997 name = editorClass->GetClassInfo()->GetClassName();
5998 vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5999 }
6000
6001 wxCHECK_MSG( vt_it == wxPGGlobalVars->m_mapEditorClasses.end(),
6002 (wxPGEditor*) vt_it->second,
6003 "Editor with given name was already registered" );
6004
6005 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorClass;
6006
6007 return editorClass;
6008 }
6009
6010 // Use this in RegisterDefaultEditors.
6011 #define wxPGRegisterDefaultEditorClass(EDITOR) \
6012 if ( wxPGEditor_##EDITOR == NULL ) \
6013 { \
6014 wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
6015 new wxPG##EDITOR##Editor, true ); \
6016 }
6017
6018 // Registers all default editor classes
6019 void wxPropertyGrid::RegisterDefaultEditors()
6020 {
6021 wxPGRegisterDefaultEditorClass( TextCtrl );
6022 wxPGRegisterDefaultEditorClass( Choice );
6023 wxPGRegisterDefaultEditorClass( ComboBox );
6024 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
6025 #if wxPG_INCLUDE_CHECKBOX
6026 wxPGRegisterDefaultEditorClass( CheckBox );
6027 #endif
6028 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
6029
6030 // Register SpinCtrl etc. editors before use
6031 RegisterAdditionalEditors();
6032 }
6033
6034 // -----------------------------------------------------------------------
6035 // wxPGStringTokenizer
6036 // Needed to handle C-style string lists (e.g. "str1" "str2")
6037 // -----------------------------------------------------------------------
6038
6039 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
6040 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
6041 {
6042 }
6043
6044 wxPGStringTokenizer::~wxPGStringTokenizer()
6045 {
6046 }
6047
6048 bool wxPGStringTokenizer::HasMoreTokens()
6049 {
6050 const wxString& str = *m_str;
6051
6052 wxString::const_iterator i = m_curPos;
6053
6054 wxUniChar delim = m_delimeter;
6055 wxUniChar a;
6056 wxUniChar prev_a = wxT('\0');
6057
6058 bool inToken = false;
6059
6060 while ( i != str.end() )
6061 {
6062 a = *i;
6063
6064 if ( !inToken )
6065 {
6066 if ( a == delim )
6067 {
6068 inToken = true;
6069 m_readyToken.clear();
6070 }
6071 }
6072 else
6073 {
6074 if ( prev_a != wxT('\\') )
6075 {
6076 if ( a != delim )
6077 {
6078 if ( a != wxT('\\') )
6079 m_readyToken << a;
6080 }
6081 else
6082 {
6083 ++i;
6084 m_curPos = i;
6085 return true;
6086 }
6087 prev_a = a;
6088 }
6089 else
6090 {
6091 m_readyToken << a;
6092 prev_a = wxT('\0');
6093 }
6094 }
6095 ++i;
6096 }
6097
6098 m_curPos = str.end();
6099
6100 if ( inToken )
6101 return true;
6102
6103 return false;
6104 }
6105
6106 wxString wxPGStringTokenizer::GetNextToken()
6107 {
6108 return m_readyToken;
6109 }
6110
6111 // -----------------------------------------------------------------------
6112 // wxPGChoiceEntry
6113 // -----------------------------------------------------------------------
6114
6115 wxPGChoiceEntry::wxPGChoiceEntry()
6116 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
6117 {
6118 }
6119
6120 // -----------------------------------------------------------------------
6121 // wxPGChoicesData
6122 // -----------------------------------------------------------------------
6123
6124 wxPGChoicesData::wxPGChoicesData()
6125 {
6126 }
6127
6128 wxPGChoicesData::~wxPGChoicesData()
6129 {
6130 Clear();
6131 }
6132
6133 void wxPGChoicesData::Clear()
6134 {
6135 m_items.clear();
6136 }
6137
6138 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
6139 {
6140 wxASSERT( m_items.size() == 0 );
6141
6142 m_items = data->m_items;
6143 }
6144
6145 wxPGChoiceEntry& wxPGChoicesData::Insert( int index,
6146 const wxPGChoiceEntry& item )
6147 {
6148 wxVector<wxPGChoiceEntry>::iterator it;
6149 if ( index == -1 )
6150 {
6151 it = m_items.end();
6152 index = (int) m_items.size();
6153 }
6154 else
6155 {
6156 it = m_items.begin() + index;
6157 }
6158
6159 m_items.insert(it, item);
6160
6161 wxPGChoiceEntry& ownEntry = m_items[index];
6162
6163 // Need to fix value?
6164 if ( ownEntry.GetValue() == wxPG_INVALID_VALUE )
6165 ownEntry.SetValue(index);
6166
6167 return ownEntry;
6168 }
6169
6170 // -----------------------------------------------------------------------
6171 // wxPropertyGridEvent
6172 // -----------------------------------------------------------------------
6173
6174 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
6175
6176
6177 wxDEFINE_EVENT( wxEVT_PG_SELECTED, wxPropertyGridEvent );
6178 wxDEFINE_EVENT( wxEVT_PG_CHANGING, wxPropertyGridEvent );
6179 wxDEFINE_EVENT( wxEVT_PG_CHANGED, wxPropertyGridEvent );
6180 wxDEFINE_EVENT( wxEVT_PG_HIGHLIGHTED, wxPropertyGridEvent );
6181 wxDEFINE_EVENT( wxEVT_PG_RIGHT_CLICK, wxPropertyGridEvent );
6182 wxDEFINE_EVENT( wxEVT_PG_PAGE_CHANGED, wxPropertyGridEvent );
6183 wxDEFINE_EVENT( wxEVT_PG_ITEM_EXPANDED, wxPropertyGridEvent );
6184 wxDEFINE_EVENT( wxEVT_PG_ITEM_COLLAPSED, wxPropertyGridEvent );
6185 wxDEFINE_EVENT( wxEVT_PG_DOUBLE_CLICK, wxPropertyGridEvent );
6186 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_BEGIN, wxPropertyGridEvent );
6187 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_ENDING, wxPropertyGridEvent );
6188 wxDEFINE_EVENT( wxEVT_PG_COL_BEGIN_DRAG, wxPropertyGridEvent );
6189 wxDEFINE_EVENT( wxEVT_PG_COL_DRAGGING, wxPropertyGridEvent );
6190 wxDEFINE_EVENT( wxEVT_PG_COL_END_DRAG, wxPropertyGridEvent );
6191
6192 // -----------------------------------------------------------------------
6193
6194 void wxPropertyGridEvent::Init()
6195 {
6196 m_validationInfo = NULL;
6197 m_column = 1;
6198 m_canVeto = false;
6199 m_wasVetoed = false;
6200 }
6201
6202 // -----------------------------------------------------------------------
6203
6204 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
6205 : wxCommandEvent(commandType,id)
6206 {
6207 m_property = NULL;
6208 Init();
6209 }
6210
6211 // -----------------------------------------------------------------------
6212
6213 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
6214 : wxCommandEvent(event)
6215 {
6216 m_eventType = event.GetEventType();
6217 m_eventObject = event.m_eventObject;
6218 m_pg = event.m_pg;
6219 OnPropertyGridSet();
6220 m_property = event.m_property;
6221 m_validationInfo = event.m_validationInfo;
6222 m_canVeto = event.m_canVeto;
6223 m_wasVetoed = event.m_wasVetoed;
6224 }
6225
6226 // -----------------------------------------------------------------------
6227
6228 void wxPropertyGridEvent::OnPropertyGridSet()
6229 {
6230 if ( !m_pg )
6231 return;
6232
6233 #if wxUSE_THREADS
6234 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
6235 #endif
6236 m_pg->m_liveEvents.push_back(this);
6237 }
6238
6239 // -----------------------------------------------------------------------
6240
6241 wxPropertyGridEvent::~wxPropertyGridEvent()
6242 {
6243 if ( m_pg )
6244 {
6245 #if wxUSE_THREADS
6246 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
6247 #endif
6248
6249 // Use iterate from the back since it is more likely that the event
6250 // being desroyed is at the end of the array.
6251 wxVector<wxPropertyGridEvent*>& liveEvents = m_pg->m_liveEvents;
6252
6253 for ( int i = liveEvents.size()-1; i >= 0; i-- )
6254 {
6255 if ( liveEvents[i] == this )
6256 {
6257 liveEvents.erase(liveEvents.begin() + i);
6258 break;
6259 }
6260 }
6261 }
6262 }
6263
6264 // -----------------------------------------------------------------------
6265
6266 wxEvent* wxPropertyGridEvent::Clone() const
6267 {
6268 return new wxPropertyGridEvent( *this );
6269 }
6270
6271 // -----------------------------------------------------------------------
6272 // wxPropertyGridPopulator
6273 // -----------------------------------------------------------------------
6274
6275 wxPropertyGridPopulator::wxPropertyGridPopulator()
6276 {
6277 m_state = NULL;
6278 m_pg = NULL;
6279 wxPGGlobalVars->m_offline++;
6280 }
6281
6282 // -----------------------------------------------------------------------
6283
6284 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
6285 {
6286 m_state = state;
6287 m_propHierarchy.clear();
6288 }
6289
6290 // -----------------------------------------------------------------------
6291
6292 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
6293 {
6294 m_pg = pg;
6295 pg->Freeze();
6296 }
6297
6298 // -----------------------------------------------------------------------
6299
6300 wxPropertyGridPopulator::~wxPropertyGridPopulator()
6301 {
6302 //
6303 // Free unused sets of choices
6304 wxPGHashMapS2P::iterator it;
6305
6306 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
6307 {
6308 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
6309 data->DecRef();
6310 }
6311
6312 if ( m_pg )
6313 {
6314 m_pg->Thaw();
6315 m_pg->GetPanel()->Refresh();
6316 }
6317 wxPGGlobalVars->m_offline--;
6318 }
6319
6320 // -----------------------------------------------------------------------
6321
6322 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
6323 const wxString& propLabel,
6324 const wxString& propName,
6325 const wxString* propValue,
6326 wxPGChoices* pChoices )
6327 {
6328 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
6329 wxPGProperty* parent = GetCurParent();
6330
6331 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
6332 {
6333 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
6334 return NULL;
6335 }
6336
6337 if ( !classInfo || !classInfo->IsKindOf(CLASSINFO(wxPGProperty)) )
6338 {
6339 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
6340 return NULL;
6341 }
6342
6343 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
6344
6345 property->SetLabel(propLabel);
6346 property->DoSetName(propName);
6347
6348 if ( pChoices && pChoices->IsOk() )
6349 property->SetChoices(*pChoices);
6350
6351 m_state->DoInsert(parent, -1, property);
6352
6353 if ( propValue )
6354 property->SetValueFromString( *propValue, wxPG_FULL_VALUE|
6355 wxPG_PROGRAMMATIC_VALUE );
6356
6357 return property;
6358 }
6359
6360 // -----------------------------------------------------------------------
6361
6362 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
6363 {
6364 m_propHierarchy.push_back(property);
6365 DoScanForChildren();
6366 m_propHierarchy.pop_back();
6367 }
6368
6369 // -----------------------------------------------------------------------
6370
6371 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
6372 const wxString& idString )
6373 {
6374 wxPGChoices choices;
6375
6376 // Using id?
6377 if ( choicesString[0] == wxT('@') )
6378 {
6379 wxString ids = choicesString.substr(1);
6380 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
6381 if ( it == m_dictIdChoices.end() )
6382 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
6383 else
6384 choices.AssignData((wxPGChoicesData*)it->second);
6385 }
6386 else
6387 {
6388 bool found = false;
6389 if ( idString.length() )
6390 {
6391 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
6392 if ( it != m_dictIdChoices.end() )
6393 {
6394 choices.AssignData((wxPGChoicesData*)it->second);
6395 found = true;
6396 }
6397 }
6398
6399 if ( !found )
6400 {
6401 // Parse choices string
6402 wxString::const_iterator it = choicesString.begin();
6403 wxString label;
6404 wxString value;
6405 int state = 0;
6406 bool labelValid = false;
6407
6408 for ( ; it != choicesString.end(); ++it )
6409 {
6410 wxChar c = *it;
6411
6412 if ( state != 1 )
6413 {
6414 if ( c == wxT('"') )
6415 {
6416 if ( labelValid )
6417 {
6418 long l;
6419 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6420 choices.Add(label, l);
6421 }
6422 labelValid = false;
6423 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
6424 value.clear();
6425 label.clear();
6426 state = 1;
6427 }
6428 else if ( c == wxT('=') )
6429 {
6430 if ( labelValid )
6431 {
6432 state = 2;
6433 }
6434 }
6435 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
6436 {
6437 value << c;
6438 }
6439 }
6440 else
6441 {
6442 if ( c == wxT('"') )
6443 {
6444 state = 0;
6445 labelValid = true;
6446 }
6447 else
6448 label << c;
6449 }
6450 }
6451
6452 if ( labelValid )
6453 {
6454 long l;
6455 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6456 choices.Add(label, l);
6457 }
6458
6459 if ( !choices.IsOk() )
6460 {
6461 choices.EnsureData();
6462 }
6463
6464 // Assign to id
6465 if ( idString.length() )
6466 m_dictIdChoices[idString] = choices.GetData();
6467 }
6468 }
6469
6470 return choices;
6471 }
6472
6473 // -----------------------------------------------------------------------
6474
6475 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
6476 {
6477 if ( s.Last() == wxT('%') )
6478 {
6479 wxString s2 = s.substr(0,s.length()-1);
6480 long val;
6481 if ( s2.ToLong(&val, 10) )
6482 {
6483 *pval = (val*max)/100;
6484 return true;
6485 }
6486 return false;
6487 }
6488
6489 return s.ToLong(pval, 10);
6490 }
6491
6492 // -----------------------------------------------------------------------
6493
6494 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
6495 const wxString& type,
6496 const wxString& value )
6497 {
6498 int l = m_propHierarchy.size();
6499 if ( !l )
6500 return false;
6501
6502 wxPGProperty* p = m_propHierarchy[l-1];
6503 wxString valuel = value.Lower();
6504 wxVariant variant;
6505
6506 if ( type.length() == 0 )
6507 {
6508 long v;
6509
6510 // Auto-detect type
6511 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6512 variant = true;
6513 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6514 variant = false;
6515 else if ( value.ToLong(&v, 0) )
6516 variant = v;
6517 else
6518 variant = value;
6519 }
6520 else
6521 {
6522 if ( type == wxT("string") )
6523 {
6524 variant = value;
6525 }
6526 else if ( type == wxT("int") )
6527 {
6528 long v = 0;
6529 value.ToLong(&v, 0);
6530 variant = v;
6531 }
6532 else if ( type == wxT("bool") )
6533 {
6534 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6535 variant = true;
6536 else
6537 variant = false;
6538 }
6539 else
6540 {
6541 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6542 return false;
6543 }
6544 }
6545
6546 p->SetAttribute( name, variant );
6547
6548 return true;
6549 }
6550
6551 // -----------------------------------------------------------------------
6552
6553 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6554 {
6555 wxLogError(_("Error in resource: %s"),msg.c_str());
6556 }
6557
6558 // -----------------------------------------------------------------------
6559
6560 #endif // wxUSE_PROPGRID